Sto usando un grafico a linee da http://www.chartjs.org/

Come puoi vedere il valore massimo (130) e il valore minimo (60) per l'asse Y sono scelti automaticamente, voglio valore massimo = 500 e valore minimo = 0. È possibile?
Sto usando un grafico a linee da http://www.chartjs.org/

Come puoi vedere il valore massimo (130) e il valore minimo (60) per l'asse Y sono scelti automaticamente, voglio valore massimo = 500 e valore minimo = 0. È possibile?
Risposte:
Devi scavalcare la scala, prova questo:
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
});
}
Per chart.js V2 (beta), utilizzare:
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.
}
}]
}
};
Vedere la documentazione di chart.js sulla configurazione degli assi lineari per maggiori dettagli.
min, maxe stepsizeattributi del ticksnamespace
suggestedMaxper il valore minimo minimo
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
Come mostrato negli esempi su https://github.com/jtblin/angular-chart.js il 7 febbraio 2017 (poiché questo sembra essere soggetto a frequenti cambiamenti):
var options = {
yAxes: [{
ticks: {
min: 0,
max: 100,
stepSize: 20
}
}]
}
Ciò comporterà 5 valori dell'asse y come tali:
100
80
60
40
20
0
xAxesche yAxesavrebbero dovuto essere nidificati scalesnelle opzioni. Che anche questo sia più recente della risposta di Ofer Sergev che sembra corretta è anche sorprendente.
Scrivendo questo nel 2016, mentre Chart js 2.3.0 è l'ultimo. Ecco come si può cambiarlo
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
}
}]
}
}
});
Configuro con 'opzioni' in v2.
Dovresti leggere la documentazione: http://www.chartjs.org/docs/#scales-linear-scale
Ho scritto un js per visualizzare i valori da 0 a 100 nell'asse y con uno spazio di 20.
Questo è il mio 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,
}
}]
}
}
});
Questo è il grafico visualizzato sul web.
> Migliore soluzione
"options":{
scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0, //min
suggestedMax: 100 //max
}
}]
}
}
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps:10,
stepValue:5,
max:100
}
}]
Questo è per Charts.js 2.0:
Il motivo per cui alcuni di questi non funzionano è perché dovresti dichiarare le tue opzioni quando crei il tuo grafico in questo modo:
$(function () {
var ctxLine = document.getElementById("myLineChart");
var myLineChart = new Chart(ctxLine, {
type: 'line',
data: dataLine,
options: {
scales: {
yAxes: [{
ticks: {
min: 0,
beginAtZero: true
}
}]
}
}
});
})
La documentazione per questo è qui: http://www.chartjs.org/docs/#scales
Con 1.1.1, ho usato quanto segue per correggere la scala tra 0,0 e 1,0:
var options = {
scaleOverride: true,
scaleStartValue: 0,
scaleSteps: 10,
scaleStepWidth: 0.1
}
Nel mio caso, ho usato un callback nelle zecche yaxis, i miei valori sono in percentuale e quando raggiunge il 100% non mostra il punto, ho usato questo:
yAxes: [{
ticks: {
beginAtZero: true,
steps: 10,
stepValue: 5,
max: 100.1,
callback: function(value, index, values) {
if (value !== 100.1) {
return values[index]
}
}
}
}],
E ha funzionato bene.
Poiché nessuno dei suggerimenti precedenti mi ha aiutato con chart.js 2.1.4, l'ho risolto aggiungendo il valore 0 all'array del mio set di dati (ma nessuna etichetta aggiuntiva):
statsData.push(0);
[...]
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
datasets: [{
data: statsData,
[...]
Stavo usando una vecchia versione del modello Flat Lab in diversi miei progetti che utilizzavano v1.x di chart.js di cui non sono sicuro. Non sono riuscito ad aggiornare a v2.x poiché avevo già diversi progetti che vi funzionavano . Quanto sopra menzionato (bardata,options)non funzionava per me.
Quindi ecco l'hack per la versione 1.x
calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,valueBounds.minSteps,valueBounds.maxValue,valueBounds.minValue,labelTemplateString);
Sostituiscilo con:
calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,1,valueBounds.maxValue,0,labelTemplateString);
Il grafico ha un valore "min" e "max". Documentazione su questo link
https://www.chartjs.org/docs/latest/axes/cartesian/linear.html
scalesoggetto nelle versioni 2.x è abbastanza diverso. Vedi la risposta @OferSegev di seguito e la documentazione 2.x qui .