I want to display the value of a bar permanently on the bar itself or on top. How can I achieve this? It should not interfere with the tool tip functionality. I am using a stacked bar, so this should work for each bar part.
Will work with this approach:
Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.ctBarLabels = function (options) {
options = Chartist.extend({}, defaultOptions, options);
return function ctBarLabels(chart) {
if (chart instanceof Chartist.Bar) {
chart.on('draw', function (data) {
var barHorizontalCenter, barVerticalCenter, label, value;
if (data.type === "bar") {
barHorizontalCenter = data.x1 + (data.element.width() * .5) + 20;
barVerticalCenter = data.y1 + (data.element.height() * -1) - 10;
value = data.element.attr('ct:value');
if (value !== '0') {
label = new Chartist.Svg('text');
label.text(value);
label.addClass("ct-barlabel");
label.attr({
x: barHorizontalCenter,
y: barVerticalCenter,
'text-anchor': 'middle'
});
return data.group.append(label);
}
}
});
}
};
};
Thanks.That works!
Should it work with 0.11.4 version of Chartist.js? I tried it is not working.
Most helpful comment
Will work with this approach: