简体   繁体   English

Chart.js - 定义自定义 y 轴比例

[英]Chart.js - Define custom y-axis scale

I am working with Chart.js on Asp.Net and I have a bar chart.我正在 Asp.Net 上使用 Chart.js,我有一个条形图。 My dataset has not very close numbers so I can not determine any fixed stepSize for this dataset.我的数据集没有非常接近的数字,因此我无法确定此数据集的任何固定步长。 My dataset like that;我的数据集就是这样; [105000,200000,310000,0.0002] [105000,200000,310000,0.0002]

So, y-axis range seems like that [0 - 100000 - 200000 - 300000 ...] but I want to show it like that [ 0 - 0.005 - 100000 - 200000 - 300000 ...] .所以,y 轴范围看起来像[0 - 100000 - 200000 - 300000 ...]但我想这样显示[ 0 - 0.005 - 100000 - 200000 - 300000 ...]

My chart options :我的图表选项:

options = {
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true,
                responsive: true,
                mainAspectRatio: false
            }
        }]
    }
}

I tried to add to options " suggestedMin: 0.005 " but y-axis values didn't change.我尝试添加选项“ SuggestMin: 0.005 ”,但 y 轴值没有改变。

How can I define custom scale for y-axis?如何为 y 轴定义自定义比例?

Try to use the callback property:尝试使用callback属性:

options = {
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true,
                responsive: true,
                mainAspectRatio: false,
                callback: (v) => !!~[0, 0.005, 100000, 200000, 300000].indexOf(v) ? v : '',
                min: 0,
                max: 300000,
                step: 0.005
            }
        }]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM