Applying Flexmonster number formatting to amCharts

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/material.js"></script>

<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;
  height: 400px;
}

JavaScript

let pivot = new Flexmonster({
  container: "pivotContainer",
  componentFolder: "https://cdn.flexmonster.com/",
  licenseFilePath: "https://cdn.flexmonster.com/jsfiddle.charts.key",
  toolbar: true,
  height: 350,
  report: {
    dataSource: {
      type: "json",
      data: getDataForFlexmonster()
    },
    slice: {
      rows: [{
        uniqueName: "Country",
      }],
      columns: [{
        uniqueName: "[Measures]"
      }],
      measures: [{
          uniqueName: "Price",
          format: "currency"
        },
        {
          uniqueName: "Quantity",
        }
      ]
    },
    formats: [{
      name: "currency",
      thousandsSeparator: ",",
      currencySymbol: '$',
      positiveCurrencyFormat: '$1',
      negativeCurrencyFormat: '($1)',
      isPercent: false
    }]
  },
  reportcomplete: () => {
    pivot.off("reportcomplete");
    createChart();
}
});

let chart;

am4core.useTheme(am4themes_animated);
am4core.useTheme(am4themes_material);

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

function drawChart(chartConfig, rawData) {
  // Create chart
  chart = am4core.create("amcharts-container", am4charts.XYChart);
  chart.data = chartConfig.data;
  
  // Create axes
  let categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
  categoryAxis.dataFields.category = pivot.amcharts.getCategoryName(rawData);
  categoryAxis.renderer.minGridDistance = 40;
  categoryAxis.fontSize = 15;
  
  let valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
  
  // Convert the Flexmonster number format to a format required by amCharts
  // In this example, the numberFormat will be "'$'#,###|'($'#,###s')'"
  let numberFormat = pivot.amcharts.getNumberFormatPattern(rawData.meta.formats[0]);
    
  // Apply the number format to the chart's axis
  valueAxis.numberFormatter = new am4core.NumberFormatter();
  valueAxis.numberFormatter.numberFormat =...