Drillable column chart

Integration with Google Charts

by Flexmonster Pivot Table

HTML

<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<script src="https://cdn.flexmonster.com/lib/flexmonster.googlecharts.js"></script>

<script src="https://www.gstatic.com/charts/loader.js"></script>

<div>
  <h2>How to create a drillable column chart</h2>
  <p>
    In this example, we've created a chart that responds to expands on the grid.
    This chart works as follows:
  </p>
  <ul>
    <li>
      When all members on the grid are collapsed, the chart displays all of
      them.
    </li>
    <li>
      When some member is expanded, the chart shows detailed info only about
      this member.
    </li>
  </ul>
  <p>
    To create such a chart, you should manually preprocess the data for it. It
    can be done using the
    <a
      href="https://www.flexmonster.com/doc/integration-with-google-charts/#prepareDataFunction"
      target="_blank"
    >prepareDataFunction</a> from Flexmonster Connector for Google Charts.
  </p>
  <p>
    Check out lines 62-114 in the JavaScript box to see how the
    <code>prepareDataFunction</code> is defined for the drillable chart.
  </p>
  <p>
    <a
      href="https://www.flexmonster.com/doc/integration-with-google-charts/"
      target="_blank"
    >Visit our docs to learn more about the integration with Google Charts</a>.
  </p>
</div>

<div id="pivot-container"></div>
<div id="chartContainer"></div>

CSS

@import url(https://cdn.flexmonster.com/assets/jsfiddle-styles.css);

#chartContainer {
  margin-top: 20px;
}

JavaScript

const pivot = new Flexmonster({
  container: "pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  licenseFilePath: "https://cdn.flexmonster.com/jsfiddle.charts.key",
  height: 350,
  report: {
    dataSource: {
      filename: "data/data.csv"
    },
    slice: {
      rows: [{
          uniqueName: "Business Type"
        },
        {
          uniqueName: "Country",
          filter: {
          	members: [
            	"Australia",
              "Canada",
              "Germany"
            ]
          }
        }
      ],
      columns: [{
        uniqueName: "[Measures]"
      }],
      measures: [{
          uniqueName: "Price"
        },
        {
          uniqueName: "Quantity"
        }
      ]
    }
  },
  reportcomplete: function() {
    pivot.off("reportcomplete");
    pivotTableReportComplete = true;
    createGoogleChart();
  }
});

let pivotTableReportComplete = false;
let googleChartsLoaded = false;

google.charts.load("52", {
  packages: ["corechart"]
});
google.charts.setOnLoadCallback(onGoogleChartsLoaded);

function onGoogleChartsLoaded() {
  googleChartsLoaded = true;
  if (pivotTableReportComplete) {
    createGoogleChart();
  }
}

function createGoogleChart() {
  if (googleChartsLoaded) {
    pivot.googlecharts.getData({
    	prepareDataFunction: (data, options) => {
      	let output = {};
        let table = [];
        let prevLevels = [];
        
        for (let i = 0; i < data.data.length; i++) {
        	let j = 0;
          let maxLevel = data.meta.rAmount - 1;
          
          if (i == 0) {
            let xAxisName = (data.meta["r" + maxLevel + "Name"] != undefined) ? data.meta["r" + maxLevel + "Name"] : "";
            let headerRow = [xAxisName];
            for (j = 0; j < data.meta.vAmount; j++) {
              headerRow.push(data.meta["v" + j + "Name"]);
            }
            table.push(headerRow);
          }

         ...