I'm trying to create a chart using AMCharts V4. The values are given in absolute, and I want to have the chart in percentages: values, y-axis, etc.
The docs are here: https://www.amcharts.com/docs/v4/tutorials/plotting-series-from-calculated-values/
However, I'm having some trouble. Pls consider the following codepen forked from the official AMCharts documentation:
https://codepen.io/otmezger/pen/RwVzmjv
As the docs suggest, I have valueAxis.calculateTotals = true;
enabled.
The line series.dataFields.valueYShow = "totalPercent";
or series.dataFields.valueYShow = "percent";
renders the graph useless.
only if I disable them, the graph will show in absolute numbers.
what am I missing? how can I make series.dataFields.valueYShow = "percent";
work?
If you want to use percent values in the series, you need to set calculatePercent
to true on the series as explained here
series.calculatePercent = true;
series.dataFields.valueYShow = "percent";
series.tooltipText = "{dateX}: [b]{valueY.percent}[/]%";
Demo:
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"date": new Date(2018, 0, 1),
"value": 10
}, {
"date": new Date(2018, 0, 2),
"value": 15
}, {
"date": new Date(2018, 0, 3),
"value": 2
}];
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.minGridDistance = 30;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.calculateTotals = true;
valueAxis.renderer.labels.template.adapter.add("text", function(text) {
return text + "%";
});
// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value";
series.dataFields.dateX = "date";
series.calculatePercent = true;
series.dataFields.valueYShow = "percent";
series.tooltipText = "{dateX}: [b]{valueY.percent}[/]%";
series.strokeWidth = 2;
var bullet = series.bullets.push(new am4charts.CircleBullet());
bullet.circle.stroke = am4core.color("#fff");
bullet.circle.strokeWidth = 2;
// Finish up setting chart up
chart.cursor = new am4charts.XYCursor();
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 250px;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>