Inserting percentage charts.js doughnut

前端 未结 4 719
失恋的感觉
失恋的感觉 2021-02-06 08:39

i\'m, using charts.js librarie and would like to know how could I add some mark to the hole of a doughnut chart (sth like a percentage)-

4条回答
  •  佛祖请我去吃肉
    2021-02-06 09:16

    I think the accepted answer does not work, at least for me. Here is my solution:

    let canvas = document.getElementById('chart-area');
    let ctx = canvas.getContext('2d');
    let perc = 25;
    
    const config = {
        type: 'doughnut',
        data: {
            datasets: [{
                data: [
                    perc,
                    100-perc
                ],
                backgroundColor: [
                    window.chartColors.green,
                    window.chartColors.gray
                ]
            }]
        },
        options: {
            responsive: true,
            animation: {
                animateScale: true,
                animateRotate: true,
                onComplete : function() {
                    var cx = canvas.width / 2;
                    var cy = canvas.height / 2;
                    ctx.textAlign = 'center';
                    ctx.textBaseline = 'middle';
                    ctx.font = '16px verdana';
                    ctx.fillStyle = 'black';
                    ctx.fillText(perc+"%", cx, cy);
                }
            },
        }
    };
    
    new Chart(ctx, config);
    

提交回复
热议问题