如何设置Y轴的最大值和最小值
How to set max and min value for Y axis
我正在使用来自 http://www.chartjs.org/ 的折线图
如您所见,Y 轴的最大值 (130) 和最小值 (60) 是自动选择的,我希望最大值 = 500,最小值 = 0。这可能吗?
您必须覆盖比例,试试这个:(applies to ChartJS v1.x)
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
scaleOverride : true,
scaleSteps : 10,
scaleStepWidth : 50,
scaleStartValue : 0
});
}
只需在您的选项中设置 scaleStartValue 的值。
var options = {
// ....
scaleStartValue: 0,
}
请参阅此 here 的文档。
以上答案对我不起作用。可能选项名称自 11 年以来已更改,但以下内容对我有用:
ChartJsProvider.setOptions
scaleBeginAtZero: true
对于 chart.js V2(测试版),使用:
var options = {
scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0, // minimum will be 0, unless there is a lower value.
// OR //
beginAtZero: true // minimum value will be 0.
}
}]
}
};
有关详细信息,请参阅 chart.js documentation on linear axes configuration。
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps:10,
stepValue:5,
max:100
}
}]
对于 1.1.1,我使用以下方法将比例固定在 0.0 和 1.0 之间:
var options = {
scaleOverride: true,
scaleStartValue: 0,
scaleSteps: 10,
scaleStepWidth: 0.1
}
这是针对 Charts.js 2.0 的:
其中一些不起作用的原因是您应该在创建图表时声明您的选项,如下所示:
$(function () {
var ctxLine = document.getElementById("myLineChart");
var myLineChart = new Chart(ctxLine, {
type: 'line',
data: dataLine,
options: {
scales: {
yAxes: [{
ticks: {
min: 0,
beginAtZero: true
}
}]
}
}
});
})
相关文档在此处:
http://www.chartjs.org/docs/#scales
var config = {
type: 'line',
data: {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
data: [10, 80, 56, 60, 6, 45, 15],
fill: false,
backgroundColor: "#eebcde ",
borderColor: "#eebcde",
borderCapStyle: 'butt',
borderDash: [5, 5],
}]
},
options: {
responsive: true,
legend: {
position: 'bottom',
},
hover: {
mode: 'label'
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps: 10,
stepValue: 5,
max: 100
}
}]
},
title: {
display: true,
text: 'Chart.js Line Chart - Legend'
}
}
};
var ctx = document.getElementById("canvas").getContext("2d");
new Chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.bundle.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<canvas id="canvas"></canvas>
</body>
由于上面的 none 的建议帮助我 charts.js 2.1.4,我通过将值 0 添加到我的数据集数组(但没有额外的标签)来解决它:
statsData.push(0);
[...]
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
datasets: [{
data: statsData,
[...]
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx ,{
type: 'line',
data: yourData,
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true,
min: 0,
max: 500
}
}]
}
}
});
我在 v2.
中配置 'options'
截至 2022 年 2 月,这有效。这是如何改变它
var options = {
scales: {
yAxes: [{
display: true,
stacked: true,
ticks: {
min: 0, // minimum value
max: 10 // maximum value
}
}]
}
};
ChartJS v2.4.0
如 2017 年 2 月 7 日 https://github.com/jtblin/angular-chart.js 中的示例所示(因为这似乎经常更改):
var options = {
yAxes: [{
ticks: {
min: 0,
max: 100,
stepSize: 20
}
}]
}
这将产生 5 个 y-axis 值:
100
80
60
40
20
0
在我的例子中,我在 yaxis ticks 中使用了回调,
我的值是百分比,当它达到 100% 时它不显示点,我使用了这个:
yAxes: [{
ticks: {
beginAtZero: true,
steps: 10,
stepValue: 5,
min: 0,
max: 100.1,
callback: function(value, index, values) {
if (value !== 100.1) {
return values[index]
}
}
}
}],
而且效果很好。
我写了一个 js 来显示 y 轴上从 0 到 100 的值,间隔为 20。
这是我的script.js
//x-axis
var vehicles = ["Trucks", "Cars", "Bikes", "Jeeps"];
//The percentage of vehicles of each type
var percentage = [41, 76, 29, 50];
var ctx = document.getElementById("barChart");
var lineChart = new Chart(ctx, {
type: 'bar',
data: {
labels: vehicles,
datasets: [{
data: percentage,
label: "Percentage of vehicles",
backgroundColor: "#3e95cd",
fill: false
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
min: 0,
max: 100,
stepSize: 20,
}
}]
}
}
});
这是网络上显示的图表。
> 最佳解决方案
"options":{
scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0, //min
suggestedMax: 100 //max
}
}]
}
}
对此有很多相互矛盾的答案,其中大部分对我没有影响。
我终于能够使用 chart.options.scales.xAxes[0].ticks.min
设置(或检索当前)X-axis 最小和最大显示值(即使最小值和最大值只是分配给图表的数据的子集。 )
在我的案例中使用时间尺度,我使用了:
chart.options.scales.xAxes[0].ticks.min = 1590969600000; //Jun 1, 2020
chart.options.scales.xAxes[0].ticks.max = 1593561600000; //Jul 1, 2020
chart.update();
(我发现不需要设置步长值或beginAtZero
等)
对于 Chart.js v3.2.0,执行此操作:
let options = {
scales: {
y: {
suggestedMin: 0,
suggestedMax: 69
},
x: {
suggestedMin: 0,
suggestedMax: 420
}
}
}
文档:https://www.chartjs.org/docs/latest/axes/#axis-range-settings
v3.X (v3.5.0)
线性笛卡尔轴
let options = {
scales: {
y: {
beginAtZero: true,
suggestedMax: 69
},
x: {
beginAtZero: true,
suggestedMax: 420
},
ticks: {
stepSize: 1
}
}
}
参考。 https://www.chartjs.org/docs/3.5.0/axes/cartesian/linear.html
线性径向轴
let options = {
scales: {
r: {
beginAtZero: true,
suggestedMax: 5,
ticks: {
stepSize: 1
}
}
}
}
参考。 https://www.chartjs.org/docs/3.5.0/axes/radial/linear.html
v2.X (v2.9.4)
线性笛卡尔轴
let options = {
scale: {
ticks: {
beginAtZero: true,
stepSize: 1,
suggestedMax: 5,
}
}
}
参考。 https://www.chartjs.org/docs/2.9.4/axes/cartesian/linear.html
线性径向轴
let options = {
scale: {
ticks: {
beginAtZero: true,
stepSize: 1,
suggestedMax: 5,
}
}
}
参考。 https://www.chartjs.org/docs/2.9.4/axes/radial/linear.html
注意:在 v2 中以单数形式缩放,在 v3 中以复数形式缩放
是的,您可以将其设置为 min
和 max
范围
options: {
resposive: true,
title: {
display: true
},
hover: {
mode: 'index',
intersect: true
},
scales: {
y: {
suggestedMin: 0,
suggestedMax: 500
}
}
}
对于"chart.js": "^3.6.1",
options: {
scales: {
y: {
min: 0,
max: 100,
}
}
}
我正在使用来自 http://www.chartjs.org/ 的折线图
如您所见,Y 轴的最大值 (130) 和最小值 (60) 是自动选择的,我希望最大值 = 500,最小值 = 0。这可能吗?
您必须覆盖比例,试试这个:(applies to ChartJS v1.x)
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
scaleOverride : true,
scaleSteps : 10,
scaleStepWidth : 50,
scaleStartValue : 0
});
}
只需在您的选项中设置 scaleStartValue 的值。
var options = {
// ....
scaleStartValue: 0,
}
请参阅此 here 的文档。
以上答案对我不起作用。可能选项名称自 11 年以来已更改,但以下内容对我有用:
ChartJsProvider.setOptions
scaleBeginAtZero: true
对于 chart.js V2(测试版),使用:
var options = {
scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0, // minimum will be 0, unless there is a lower value.
// OR //
beginAtZero: true // minimum value will be 0.
}
}]
}
};
有关详细信息,请参阅 chart.js documentation on linear axes configuration。
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps:10,
stepValue:5,
max:100
}
}]
对于 1.1.1,我使用以下方法将比例固定在 0.0 和 1.0 之间:
var options = {
scaleOverride: true,
scaleStartValue: 0,
scaleSteps: 10,
scaleStepWidth: 0.1
}
这是针对 Charts.js 2.0 的:
其中一些不起作用的原因是您应该在创建图表时声明您的选项,如下所示:
$(function () {
var ctxLine = document.getElementById("myLineChart");
var myLineChart = new Chart(ctxLine, {
type: 'line',
data: dataLine,
options: {
scales: {
yAxes: [{
ticks: {
min: 0,
beginAtZero: true
}
}]
}
}
});
})
相关文档在此处: http://www.chartjs.org/docs/#scales
var config = {
type: 'line',
data: {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
data: [10, 80, 56, 60, 6, 45, 15],
fill: false,
backgroundColor: "#eebcde ",
borderColor: "#eebcde",
borderCapStyle: 'butt',
borderDash: [5, 5],
}]
},
options: {
responsive: true,
legend: {
position: 'bottom',
},
hover: {
mode: 'label'
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps: 10,
stepValue: 5,
max: 100
}
}]
},
title: {
display: true,
text: 'Chart.js Line Chart - Legend'
}
}
};
var ctx = document.getElementById("canvas").getContext("2d");
new Chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.bundle.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<canvas id="canvas"></canvas>
</body>
由于上面的 none 的建议帮助我 charts.js 2.1.4,我通过将值 0 添加到我的数据集数组(但没有额外的标签)来解决它:
statsData.push(0);
[...]
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
datasets: [{
data: statsData,
[...]
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx ,{
type: 'line',
data: yourData,
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true,
min: 0,
max: 500
}
}]
}
}
});
我在 v2.
中配置 'options'截至 2022 年 2 月,这有效。这是如何改变它
var options = {
scales: {
yAxes: [{
display: true,
stacked: true,
ticks: {
min: 0, // minimum value
max: 10 // maximum value
}
}]
}
};
ChartJS v2.4.0
如 2017 年 2 月 7 日 https://github.com/jtblin/angular-chart.js 中的示例所示(因为这似乎经常更改):
var options = {
yAxes: [{
ticks: {
min: 0,
max: 100,
stepSize: 20
}
}]
}
这将产生 5 个 y-axis 值:
100
80
60
40
20
0
在我的例子中,我在 yaxis ticks 中使用了回调, 我的值是百分比,当它达到 100% 时它不显示点,我使用了这个:
yAxes: [{
ticks: {
beginAtZero: true,
steps: 10,
stepValue: 5,
min: 0,
max: 100.1,
callback: function(value, index, values) {
if (value !== 100.1) {
return values[index]
}
}
}
}],
而且效果很好。
我写了一个 js 来显示 y 轴上从 0 到 100 的值,间隔为 20。
这是我的script.js
//x-axis
var vehicles = ["Trucks", "Cars", "Bikes", "Jeeps"];
//The percentage of vehicles of each type
var percentage = [41, 76, 29, 50];
var ctx = document.getElementById("barChart");
var lineChart = new Chart(ctx, {
type: 'bar',
data: {
labels: vehicles,
datasets: [{
data: percentage,
label: "Percentage of vehicles",
backgroundColor: "#3e95cd",
fill: false
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
min: 0,
max: 100,
stepSize: 20,
}
}]
}
}
});
这是网络上显示的图表。
> 最佳解决方案
"options":{
scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0, //min
suggestedMax: 100 //max
}
}]
}
}
对此有很多相互矛盾的答案,其中大部分对我没有影响。
我终于能够使用 chart.options.scales.xAxes[0].ticks.min
设置(或检索当前)X-axis 最小和最大显示值(即使最小值和最大值只是分配给图表的数据的子集。 )
在我的案例中使用时间尺度,我使用了:
chart.options.scales.xAxes[0].ticks.min = 1590969600000; //Jun 1, 2020
chart.options.scales.xAxes[0].ticks.max = 1593561600000; //Jul 1, 2020
chart.update();
(我发现不需要设置步长值或beginAtZero
等)
对于 Chart.js v3.2.0,执行此操作:
let options = {
scales: {
y: {
suggestedMin: 0,
suggestedMax: 69
},
x: {
suggestedMin: 0,
suggestedMax: 420
}
}
}
文档:https://www.chartjs.org/docs/latest/axes/#axis-range-settings
v3.X (v3.5.0)
线性笛卡尔轴
let options = {
scales: {
y: {
beginAtZero: true,
suggestedMax: 69
},
x: {
beginAtZero: true,
suggestedMax: 420
},
ticks: {
stepSize: 1
}
}
}
参考。 https://www.chartjs.org/docs/3.5.0/axes/cartesian/linear.html
线性径向轴
let options = {
scales: {
r: {
beginAtZero: true,
suggestedMax: 5,
ticks: {
stepSize: 1
}
}
}
}
参考。 https://www.chartjs.org/docs/3.5.0/axes/radial/linear.html
v2.X (v2.9.4)
线性笛卡尔轴
let options = {
scale: {
ticks: {
beginAtZero: true,
stepSize: 1,
suggestedMax: 5,
}
}
}
参考。 https://www.chartjs.org/docs/2.9.4/axes/cartesian/linear.html
线性径向轴
let options = {
scale: {
ticks: {
beginAtZero: true,
stepSize: 1,
suggestedMax: 5,
}
}
}
参考。 https://www.chartjs.org/docs/2.9.4/axes/radial/linear.html
注意:在 v2 中以单数形式缩放,在 v3 中以复数形式缩放
是的,您可以将其设置为 min
和 max
范围
options: {
resposive: true,
title: {
display: true
},
hover: {
mode: 'index',
intersect: true
},
scales: {
y: {
suggestedMin: 0,
suggestedMax: 500
}
}
}
对于"chart.js": "^3.6.1",
options: {
scales: {
y: {
min: 0,
max: 100,
}
}
}