Apply Flexmonster’s number formatting to Highcharts

Integration with Highcharts

by Flexmonster Pivot Table

HTML

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

<script src="https://code.highcharts.com/12.5.0/highcharts.js"></script>
<script src="https://code.highcharts.com/12.5.0/modules/accessibility.js"></script>

<div>
  <h2>How to apply Flexmonster's number formatting to Highcharts</h2>
  <p>
    When integrating with Highcharts, you can apply Flexmonster's number
    formatting to the created chart. It's possible with our Connector for
    Highcharts and the
    <a
      href="https://www.flexmonster.com/doc/integration-with-highcharts/#number-formatting"
      target="_blank"
    >methods it provides</a>. These methods convert the component's 
    number format to a Highcharts' format string.
  </p>
  <p>
    Switch to the JavaScript box and see how number formatting is applied to
    Highcharts:
  </p>
  <ul>
    <li>Lines 66, 71 - setting the number format for yAxis labels.</li>
    <li>Lines 85, 90 - setting the number format for the tooltips.</li>
  </ul>
  <p>
    <a
      href="https://www.flexmonster.com/doc/integration-with-highcharts/"
      target="_blank"
    >Visit our docs to learn more about the integration with Highcharts</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: 20px;
}

JavaScript

let pivot = new Flexmonster({
  container: "#pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  licenseFilePath: "https://cdn.flexmonster.com/jsfiddle.charts.key",
  height: 350,
  toolbar: true,
  report: {
    dataSource: {
      filename: "data/region-data.json"
    },
    slice: {
      rows: [{
        uniqueName: "Region"
      }],
      columns: [{
        uniqueName: "[Measures]"
      }],
      measures: [
        {
          uniqueName: "Price",
          format: "currency"
        },
        {
          uniqueName: "Quantity"
        }
      ],
    },
    formats: [{
      name: "currency",
      thousandsSeparator: " ",
      decimalSeparator: ".",
      decimalPlaces: 2,
      maxDecimalPlaces: -1,
      maxSymbols: 20,
      currencySymbol: "$",
    }]
  },
  reportcomplete: function() {
    pivot.off("reportcomplete");
    createChartWithBothAxesNumeric();
  }
});

function createChartWithBothAxesNumeric() {
  pivot.highcharts.getData({
      type: "spline",
    },
    createAndUpdateChart,
    createAndUpdateChart
  );
}

function applyFontSize(config) {
  config.chart.style = {
    fontSize: "18px",
    fontFamily: "Arial, sans-serif"
  };
}

function createAndUpdateChart(chartConfig, rawData) {

  applyFontSize(chartConfig); 
  
	let currencyFormat = rawData.meta.formats.find(format => format.name == "currency");
  let generalFormat = rawData.meta.formats.find(format => format.name == "");

  chartConfig.title.text = "Number Formatting";
  chartConfig.colors = ["#f76231", "#76528bff"];
  
  if (chartConfig.yAxis == undefined) chartConfig.yAxis = {};

	for (let i = 0; i < chartConfig.yAxis.length; i++) {
  	switch (chartConfig.yAxis[i].title.text) {
    	case "Sum of Price":
        chartConfig.yAxis[i].labels = {
          format: pivot.highcharts.getAxisFormat(currencyFormat)
        }
        break;
      case "Sum of Quantity":
     ...