在条形图的顶部显示计数-ChartJS

问题描述 投票:0回答:1

在Chart.js中的我的选项对象中

我已经尝试过

plugins: {
    afterDatasetsDraw: function (context, easing) {
        var ctx = context.chart.ctx;
        context.data.datasets.forEach(function (dataset) {
            for (var i = 0; i < dataset.data.length; i++) {
                if (dataset.data[i] != 0) {
                    var model = dataset._meta[Object.keys(dataset._meta)[0]].data[i]._model;
                    var textY = model.y + (dataset.type == "line" ? -3 : 15);

                    ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, 'normal', Chart.defaults.global.defaultFontFamily);
                    ctx.textAlign = 'start';
                    ctx.textBaseline = 'middle';
                    ctx.fillStyle = dataset.type == "line" ? "black" : "black";
                    ctx.save();
                    ctx.translate(model.x, textY-15);
                    ctx.rotate(4.7);
                    ctx.fillText(dataset.data[i], 0, 0);
                    ctx.restore();
                }
            }
        });
    }

}

我没有运气。


尽管,我已经尝试过了,但是有效

animation: {
    duration: ,
    onComplete: function() {
        var chartInstance = this.chart,
        ctx = chartInstance.ctx;

        ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
        ctx.textAlign = 'center';
        ctx.textBaseline = 'bottom';

        this.data.datasets.forEach(function(dataset, i) {
            var meta = chartInstance.controller.getDatasetMeta(i);
            meta.data.forEach(function(bar, index) {
                var data = dataset.data[index];
                ctx.fillText(data, bar._model.x, bar._model.y - 5);
            });
        });
    }
}

它似乎得到了我想要的东西,但是当我将其悬停在它上面时,它会闪烁并显得非常有故障。

enter image description here https://i.imgur.com/HKVXNXu.gif

你们知道更好的方法吗?

javascript charts chart.js
1个回答
0
投票

[https://chartjs-plugin-datalabels.netlify.com/,此options用于标签:

  • anchor:'end', ==>最高元素边界

  • [align: top ==>标签位于锚点之后,沿着相同的方向
  • 要进行微调,请更改paddingoffset(负或正),字体大小等(很多控件)。 datalabels Options

    “ Hello world example”:

    // Change default options for ALL charts Chart.helpers.merge(Chart.defaults.global.plugins.datalabels, { opacity: 1, color: 'black', borderColor: '#11469e', anchor: 'end', /*https://chartjs-plugin-datalabels.netlify.com/guide/positioning.html */ align: 'top', /* https://chartjs-plugin-datalabels.netlify.com/guide/positioning.html#alignment-and-offset */ font: { weight: 'bold', size: 14, lineHeight: 1 /* align v center */ }, offset: 1, /* 4 by deafult */ padding:{ bottom: 0 /* 4 by deafult */ } }); var data = { labels: ["Africa", "Asia", "Europe", "America"], datasets: [{ /* data */ label: "Population (millions)", backgroundColor: ["#490A3D", "#BD1550","#E97F02", '#F8CA00'], data: [1000,1500,2000, 2200] }] }; var options = { plugins: { // Change options for ALL labels of THIS CHART datalabels: { } }, animation:{ duration: 2000 /* time in ms */ }, scales: { xAxes: [{ stacked: true }], yAxes: [{ stacked: true }] } }; var myChart = new Chart(document.getElementById("chart"), { type: 'bar', data: data, options: options });
    <canvas id="chart"></canvas>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
    © www.soinside.com 2019 - 2024. All rights reserved.