Stacked column chart

Integration with amCharts 4

by Flexmonster Pivot Table

HTML

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

<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/kelly.js"></script>

<div>
  <h2>How to create a stacked column chart</h2>
  <p>
    This example shows how to create a stacked column chart using
    <a
      href="https://www.flexmonster.com/api/all-methods-amcharts/"
      target="_blank"
    >Flexmonster Connector for amCharts</a>.
  </p>
  <p>
    A stacked column chart consists of several series, which represent different
    measures. To create series for each measure in the slice, we use the
    Connector's
    <a
      href="https://www.flexmonster.com/api/amcharts-getnumberofmeasures/"
      target="_blank"
    >getNumberOfMeasures()</a>
    and
    <a
      href="https://www.flexmonster.com/api/amcharts-getmeasurenamebyindex/"
      target="_blank"
    >getMeasureNameByIndex()</a> methods (lines 73-81 in the JavaScript box).
  </p>
  <p>
    In this example, we've used the <code>Price</code> and
    <code>Quantity</code> measures for the chart. Try adding other measures to
    the slice and see how the chart changes.
  </p>
  <p>
    <a
      href="https://www.flexmonster.com/doc/integration-with-amcharts/"
      target="_blank"
    >Visit our docs to learn more about the integration with amCharts</a>.
  </p>
</div>

<div id="pivotContainer">The component will appear here</div>

<div id="amcharts-container"></div>

CSS

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

#amcharts-container {
  margin: 20px 0 20px 0;
  height: 500px;
  font-size: 15px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

JavaScript

let pivot = new Flexmonster({
  container: "pivotContainer",
  componentFolder: "https://cdn.flexmonster.com/",
  licenseFilePath: "https://cdn.flexmonster.com/jsfiddle.charts.key",
  height: 400,
  report: {
    dataSource: {
      filename: "data/data.json"
    },
    slice: {
      rows: [
      	{
        	uniqueName: "Country"
      	}
      ],
      columns: [
      	{
          uniqueName: "Color",
          filter: {
          	exclude: ["color.[white]", "color.[purple]"]
          }
        },
        {
          uniqueName: "[Measures]"
        }
      ],
      measures: [
      	{
          uniqueName: "Price"
        },
        {
          uniqueName: "Quantity"
        }
      ]
    }
  },
  reportcomplete: function() {
    pivot.off("reportcomplete");
    createChart();
  }
});

let chart;

function createChart() {
  pivot.amcharts.getData({}, drawChart, updateChart);
}

function drawChart(chartConfig, rawData) {
  // Apply amCharts theme
  am4core.useTheme(am4themes_animated);
  am4core.useTheme(am4themes_kelly);
  
  /* Get number formatting from Flexmonster */

	/* Initialize the chart */
  chart = am4core.create("amcharts-container", am4charts.XYChart);
  chart.data = chartConfig.data;

	/* Create and configure the category axis */
  let categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
  categoryAxis.dataFields.category = pivot.amcharts.getCategoryName(rawData);
  categoryAxis.renderer.minGridDistance = 40;
  categoryAxis.renderer.grid.template.location = 0;
  categoryAxis.renderer.labels.template.maxWidth = 100;
  categoryAxis.renderer.labels.template.wrap = true;
  categoryAxis.cursorTooltipEnabled = false;

	/* Create the value axis */
  let valueAxis = chart.yAxes.push(new am4charts.ValueAxis());

	/* Create and configure series */
  for (let s = 0; s < pivot.amcharts.getNumberOfMeasures(rawData); s++) {
    let series = chart.series.push(new...