I am quite new at Chart.js 2, so any help will be appreciated.
I'm trying to show values for each bar in the chart. As suggested in Display values in Pareto chart using Chart.js 2.0.2 and other users I'm using this solution. However, this part throws an exception TypeError: dataset.metaData is undefined
:
dataset.metaData.forEach(function(p) {
ctx.fillText(p._chart.config.data.datasets[p._datasetIndex].data[p._index], p._model.x, p._model.y + 20);
});
I just want to show the value over the bar. Any help?
In addition to potatopeelings's answer, using Chart.js v2.5, I had to adjust the following:
switch (dataset.type)
toswitch ( chart.getDatasetMeta(i).type )
With the below updated
onComplete
, your code should work with Chart.js v2.1Fiddle - http://jsfiddle.net/0j4g7kxy/
I've assumed you needed only the bar, but I have retained the code for line in the
onComplete
, so it should work for lines too. If you don't need either, just remove the relatedcase
from theonComplete
codeWorks for me! chart.js 2.5.0
In addition to potatopeelings's and slashpm answers.
To handle horizontalBar you can add this case (change the padding "offsetY" to "offsetX")
Here is the full function
I have adapted it a little bit for horizontal Bars to show the sum of values instead of the stacked values. maybe someone can beautify the code a little bit but it works this way: