javascript - Display dataset values on bar ChartJs 2.1.6 -
as per chartjs version has changed,the dataset values display on bar has not working.the existing code has working v1.0.2. ref: how display data values on chart.js
var ctx = document.getelementbyid("mychart2").getcontext("2d"); var mybar = new chart(ctx).bar(chartdata, { showtooltips: false, onanimationcomplete: function () { var ctx = this.chart.ctx; ctx.font = this.scale.font; ctx.fillstyle = this.scale.textcolor ctx.textalign = "center"; ctx.textbaseline = "bottom"; this.datasets.foreach(function (dataset) { dataset.bars.foreach(function (bar) { ctx.filltext(bar.value, bar.x, bar.y - 5); }); }) } });
now modification must work chartjs v2.1.6? current bar graph syntax has working v2.1.6 given below.
var mybarchart = new chart(ctx, { type: 'bar', data: datasets, options: { responsive: true, tooltips: { enabled: false }, legend: { display: false }, //what should add display values on bar? } });
by using animation.oncomplete
key, follows:
animation: { duration: 500, oncomplete: function() { var ctx = this.chart.ctx; ctx.font = chart.helpers.fontstring(chart.defaults.global.defaultfontfamily, 'normal', chart.defaults.global.defaultfontfamily); ctx.fillstyle = "black"; ctx.textalign = 'center'; ctx.textbaseline = 'bottom'; this.data.datasets.foreach(function(dataset) { (var = 0; < dataset.data.length; i++) { (var key in dataset._meta) { var model = dataset._meta[key].data[i]._model; ctx.filltext(dataset.data[i], model.x, model.y - 5); } } }); } }
http://www.chartjs.org/docs/#chart-configuration-animation-configuration
Comments
Post a Comment