如何在图表上显示数值?

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

我有一个绘制饼图的脚本。我想让它写出它应该从值中获取的百分比。

它应该是这样的:

怎么做?

const uniquePercentages = [
    { value: 'Test 1', percentage: '28.6' },
    { value: 'Test 2', percentage: '71.4' }
]

const labels = uniquePercentages.map(item => item.value);
const values = uniquePercentages.map(item => item.percentage);


const pieCanvas = createCanvas(600, 400);
const pieCtx = pieCanvas.getContext('2d');

const pieChart = new Chart(pieCtx, {
    type: 'pie',
    data: {
        labels: labels,
        datasets: [{

            label: 'My Dataset',
            data: values,
            backgroundColor: [
                'rgb(255, 99, 132)',
                'rgb(54, 162, 235)',
            ],
            hoverOffset: 4
        }]
    },
});

fs.writeFileSync('pie.png', pieCanvas.toBuffer());
javascript node.js chart.js
© www.soinside.com 2019 - 2024. All rights reserved.