【发布时间】:2015-05-13 11:36:59
【问题描述】:
我正在使用来自http://www.chartjs.org/ 的折线图
正如您所见,Y 轴的最大值 (130) 和最小值 (60) 是自动选择的,我希望最大值 = 500 和最小值 = 0。这可能吗?
【问题讨论】:
标签: chart.js
我正在使用来自http://www.chartjs.org/ 的折线图
正如您所见,Y 轴的最大值 (130) 和最小值 (60) 是自动选择的,我希望最大值 = 500 和最小值 = 0。这可能吗?
【问题讨论】:
标签: chart.js
对于 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.
}
}]
}
};
【讨论】:
ticks命名空间的min、max和stepsize属性
suggestedMax 为最小值
你必须覆盖比例,试试这个:(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
});
}
【讨论】:
scales 对象完全不同。请参阅下面的@OferSegev 答案和2.x documentation here。
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>
【讨论】:
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 轴值:
100
80
60
40
20
0
【讨论】:
xAxes 和yAxes 应该嵌套在选项中的scales 下。再加上这比 Ofer Sergev 似乎正确的答案更新,这也令人惊讶。
在 2021 年 11 月,此方法有效。这是一个可以改变它的方法
var options = {
scales: {
yAxes: [{
display: true,
stacked: true,
ticks: {
min: 0, // minimum value
max: 10 // maximum value
}
}]
}
};
【讨论】:
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 中使用“选项”进行配置。
【讨论】:
我写了一个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,
}
}]
}
}
});
这是显示在网络上的图表。
【讨论】:
以上答案对我不起作用。自 11 年以来,选项名称可能已更改,但以下内容对我有用:
ChartJsProvider.setOptions
scaleBeginAtZero: true
【讨论】:
对于 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
【讨论】:
>最佳解决方案
"options":{
scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0, //min
suggestedMax: 100 //max
}
}]
}
}
【讨论】:
【讨论】:
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps:10,
stepValue:5,
max:100
}
}]
【讨论】:
这适用于 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
【讨论】:
beginAtZero。
就我而言,我在 yaxis 刻度中使用了回调, 我的值是百分比,当它达到 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]
}
}
}
}],
而且效果很好。
【讨论】:
对此有很多相互矛盾的答案,其中大部分对我没有影响。
我终于能够使用chart.options.scales.xAxes[0].ticks.min 设置(或检索当前)X 轴最小和最大显示值(即使最小值和最大值只是分配给图表的数据的子集。)
在我的例子中使用时间尺度,我使用了:
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等)
【讨论】:
在 1.1.1 中,我使用以下方法将比例固定在 0.0 和 1.0 之间:
var options = {
scaleOverride: true,
scaleStartValue: 0,
scaleSteps: 10,
scaleStepWidth: 0.1
}
【讨论】:
由于上述建议都没有帮助我使用 chart.js 2.1.4,我通过将值 0 添加到我的数据集数组(但没有额外的标签)来解决它:
statsData.push(0);
[...]
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
datasets: [{
data: statsData,
[...]
【讨论】:
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
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 为复数
【讨论】:
对于"chart.js": "^3.6.1",
options: {
scales: {
y: {
min: 0,
max: 100,
}
}
}
【讨论】:
是的,您可以将其设置为 min 和 max 范围
options: {
resposive: true,
title: {
display: true
},
hover: {
mode: 'index',
intersect: true
},
scales: {
y: {
suggestedMin: 0,
suggestedMax: 500
}
}
}
【讨论】: