Applying Flexmonster number formatting to amCharts
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 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: 400px;
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: {
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 root;
function createChart() {
pivot.amcharts.getData({}, drawChart, updateChart);
}
function drawChart(chartConfig, rawData) {
// Create root element
root = am5.Root.new("amcharts-container");
root.setThemes([am5themes_Animated.new(root), am5themes_Kelly.new(root)]);
let data = chartConfig.data;
// 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 amCharts
root.numberFormatter.set("numberFormat", numberFormat);
// Create chart
let chart = root.container.children.push(am5xy.XYChart.new(root, {
layout: root.verticalLayout
}));
// Create axes
let xRenderer = am5xy.AxisRendererX.new(root, {
cellStartLocation: 0.1,
cellEndLocation: 0.9,
minGridDistance: 40
});
xRenderer.labels.template.setAll({
fontSize: 15,
})
let xAxis =...