Work around to place indexLabel at the bottom in column chart - CanvasJS JavaScript Charts
Work around to place indexLabel at the bottom in column chart - CanvasJS JavaScript Charts
by canvasjs
HTML
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
JavaScript
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "Placing indexLabel at the bottom in column chart"
},
animationEnabled: true,
animationDuration: 1000,
axisX:{
lineThickness: 0,
tickLength: 0,
interval: 1,
valueFormatString: "####"
},
axisY: {
lineThickness: 0,
gridThickness: 0,
tickLength: 0,
suffix: "M",
maximum: 3
},
data: [
{
type: "column",
color: "rgb(29, 51, 61)",
indexLabelPlacement: "inside",
xValueFormatString: "####",
toolTipContent: "{x}: {y}M",
dataPoints: [
{ x: 2011, y: 1.11 },
{ x: 2012, y: 0.95 },
{ x: 2013, y: 0.88 },
{ x: 2014, y: 1.88 },
{ x: 2015, y: 2.23 },
{ x: 2016, y: 3.1 }
]
},
{
type: "line",
color: "transparent",
markerType: "none",
showInLegend: false,
toolTipEnabled: false,
indexLabelFontColor: "white",
indexLabelFontStyle: "bolder",
indexLabelFontSize: 20,
dataPoints: []
}
]
});
chart.render();
// Adding indexLabels at the bottom
for(var i = 0; i < chart.options.data[0].dataPoints.length; i++) {
chart.options.data[1].dataPoints.push({
x: chart.options.data[0].dataPoints[i].x,
y: 0,
indexLabel: chart.options.data[0].dataPoints[i].y + "M"
});
}
if(chart.options.animationEnabled) {
if(!chart.options.animationDuration) chart.options.animationDuration = 1200;
setTimeout(function() {
chart.render();
}, chart.options.animationDuration);
} else {
chart.render();
}