ChartJs-标记Y轴的最小值和最大值

时间:2020-04-04 14:29:58

标签: javascript php charts chart.js

我找到了一个在Y轴上的每个值上添加标签的代码,并且已经尝试过。这是代码

var chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    // Include a dollar sign in the ticks
                    callback: function(value, index, values) {
                        return '$' + value;
                    }
                }
            }]
        }
    }
});

但是,我只能给出最小值和最大值吗?如果可以,怎么办?

以下是我的示例图表。最小和最大标签上都标有红色标记。

我的意思是示例图表

Example chart that I mean

1 个答案:

答案 0 :(得分:0)

您可以直接指定最小值和最大值,请检查https://www.chartjs.org/docs/latest/axes/cartesian/linear.html#step-size

例如

var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
    scales: {
        yAxes: [{
            ticks: {
                // Include a dollar sign in the ticks
                callback: function(value, index, values) {
                    return '$' + value;
                },
             min: 1,
             max: 100
            }
        }]
    }
}});

您还可以使用轴范围设置,它仅更改用于缩放轴的数据值,并且在保持自动装配行为的同时,对扩展轴范围很有用。 https://www.chartjs.org/docs/latest/axes/cartesian/linear.html#axis-range-settings

例如

var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
    scales: {
        yAxes: [{
            ticks: {
                // Include a dollar sign in the ticks
                callback: function(value, index, values) {
                    return '$' + value;
                },
             suggestedMin: 1,
             suggestedMax: 100
            }
        }]
    }
}});