Show values on top of bars in chart.js

后端 未结 7 1131
自闭症患者
自闭症患者 2020-12-04 13:34

Please refer this Fiddle : https://jsfiddle.net/4mxhogmd/1/

I am working on chart.js If you see in fiddle, you will notice that value which is top on bar is not pro

7条回答
  •  臣服心动
    2020-12-04 14:02

    this works in my case but its show values in mid of the bar.

    chart.chart.config.options.animation["onComplete"] =  function () {
            var ctx = chart.chart.ctx;
            ctx.font = '22px "Helvetica Neue", Helvetica, Arial, sans-serif';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'bottom';
    
            this.data.datasets.forEach(function (dataset) {
                for (var i = 0; i < dataset.data.length; i++) {
                    var model = dataset._meta[Object.keys(dataset._meta)[0]].data[i]._model,
                        scale_max = dataset._meta[Object.keys(dataset._meta)[0]].data[i]._yScale.maxHeight;
                    ctx.fillStyle = '#444';
                    var y_pos = model.y + 50;
                    if ((scale_max - model.y) / scale_max >= 0.5)
                        y_pos = model.y + 20;
                    ctx.fillText(dataset.data[i], model.x, y_pos);    
                }
            });                
        }
    

提交回复
热议问题