Bar chart

Integration with amCharts 5

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/5/index.js"></script>
<script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
<script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>
<script src="https://cdn.amcharts.com/lib/5/themes/Kelly.js"></script>

<div>
  <h2>How to create a bar chart</h2>
  <p>
    This example shows how to create a bar chart using
    <a
      href="https://www.flexmonster.com/api/all-methods-amcharts/"
      target="_blank"
    >Flexmonster Connector for amCharts</a>.
  </p>
  <p>In the JavaScript box, you can see how to:</p>
  <ul>
    <li>Create a chart (lines 44-58).</li>
    <li>Apply Flexmonster's number formatting to amCharts (line 66).</li>
    <li>Bind a Category axis to the chart's data (line 145).</li>
    <li>Bind series to the chart's data (lines 146-147).</li>
  </ul>
  <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",
  toolbar: true,
  height: 400,
  report: {
    dataSource: {
      filename: "data/data.csv"
    },
    slice: {
      rows: [{
        uniqueName: "Country",
        filter: {
          exclude: ["Country.[United States]"]
        }
      }],
      columns: [{
          uniqueName: "Color",
          filter: {
            exclude: ["color.[white]", "color.[purple]"]
          }
        },
        {
          uniqueName: "[Measures]"
        }
      ],
      measures: [{
        uniqueName: "Price",
        format: "currency"
      }]
    },
    formats: [{
      name: "currency",
      thousandsSeparator: ",",
      decimalSeparator: ".",
      decimalPlaces: 2,
      currencySymbol: "$",
      currencySymbolAlign: "left",
      nullValue: "",
      textAlign: "right"
    }]
  },
  reportcomplete: function() {
    pivot.off("reportcomplete");
    createChart();
  }
});

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

function drawChart(chartConfig, rawData) {
  // Create root element
  root = am5.Root.new("amcharts-container");
  let data = chartConfig.data;

  // Set themes
  root.setThemes([
    am5themes_Animated.new(root),
    am5themes_Kelly.new(root),
  ]);
  root.numberFormatter.set("numberFormat", pivot.amcharts.getNumberFormatPattern(rawData.meta.formats[0]));

  // Create chart
  let chart = root.container.children.push(am5xy.XYChart.new(root, {
    layout: root.verticalLayout
  }));

  // Create axes
  let yRenderer = am5xy.AxisRendererY.new(root, {
    cellStartLocation: 0.1,
    cellEndLocation: 0.9,
  });

  yRenderer.labels.template.setAll({
    rotation: 270,
    oversizedBehavior: "wrap",
    maxHeight: 100,
    textAlign:...