使用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轴的标签。该函数返回每个值后面添加的单位

chartjs如何纵从标显示单位

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

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