使用 Chart.js 时,可以在 Y 轴的标签中添加单位,如下所示:

var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [{
            label: 'Sales',
            data: [5, 10, 15, 20, 25, 30, 35],
            backgroundColor: 'rgba(54, 162, 235, 0.2)',
            borderColor: 'rgba(54, 162, 235, 1)',
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true,
                    callback: function(value, index, values) {
                        return value + ' $'; //添加单位
                    }
                }
            }]
        }
    }
});

在这个例子中,我们在 Y 轴的标签中添加了美元符号作为单位。在 options 中的 scales 中的 yAxes 选项中,我们添加了一个回调函数来处理 Y 轴的标签。该函数返回每个值后面添加的单位。

Chart.js Y轴标签添加单位 - 如何在图表中显示单位

原文地址: https://www.cveoy.top/t/topic/oci7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录