I am using Chart.js to display a line chart which is working correctly, except that I would like the y axis to start at 0 as the minimum value. Right now, the y axis starts at whatever the minimum value is in the data being displayed. I would like it to always show from zero.
This is the code I am using (whcih works, except does not start at 0 on the y axis).
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url : "Get data from here",
type : "GET",
success : function(data){
console.log(data);
var userid = [];
var viewers = [];
for(var i in data) {
userid.push(data[i].date_time);
viewers.push(data[i].viewers);
}
var chartdata =
{
labels: userid,
datasets:
[
{
label: "Viewers for <?php echo $month . " - " . $year; ?>",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(59, 89, 152, 0.75)",
borderColor: "rgba(59, 89, 152, 1)",
pointHoverBackgroundColor: "rgba(59, 89, 152, 1)",
pointHoverBorderColor: "rgba(59, 89, 152, 1)",
data: viewers,
options:
{
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
}
]
};
var ctx = $("#mycanvas");
var LineGraph = new Chart(ctx, {
type: 'line',
data: chartdata
});
},
error : function(data) {
}
});
});
</script>
I added this part:
options:
{
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
But it doesn't seem to do anything and continues to display from whatever the minimum value in the data displayed is.
Any help would be appreciated.
Thank you.
Data in a line chart is encoded by position (x, y coordinates), whereas in a bar chart data is represented by length. This subtle difference changes the way a reader uses the chart, meaning that in a line chart it's ok to start the axis at a value other than zero, despite many claims that they are always misleading.
always start your axis at zero for bar (or column) charts: Simple. Set the axis start point to zero (Select axis, press Ctrl+1, and from Axis options set minimum to 0). So there you go.
The common advice from the data visualization experts is to always start your measurement axis at zero so that the relative size of the columns or bars is always showing an accurate picture of the values being represented.
In the case of bar charts, this means that the y-axis must always start at zero. The bars in a bar chart encode the data by their length, so if we truncate the length by starting the axis at something other than zero, we distort the visual in a bad way.
When a bar chart does not start with a zero, the information you visualise can still remain correct but the people who’ll see your bar chart will comprehend it in a wrong way. The difference between the values of the bars will either be too big or too small.
A horizontal bar chart is a variation on a vertical bar chart. It is sometimes used to show trend data, and the comparison of multiple data sets side by side. To achieve this you will have to set the indexAxis property in the options object to 'y'. The default for this property is 'x' and thus will show vertical bars.
options - options for the whole chart The bar chart allows a number of properties to be specified for each dataset. These are used to set display properties for a specific dataset. For example, the color of the bars is generally set this way.
The default for this property is 'x' and thus will show vertical bars. The configuration options for the horizontal bar chart are the same as for the bar chart. However, any options specified on the x-axis in a bar chart, are applied to the y-axis in a horizontal bar chart.
Move the options
part to the Chart creation:
var LineGraph = new Chart(ctx, {
type: 'line',
data: chartdata,
options: {
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
Here's an example from the docs.
options: {
resposive: true,
scales: {
y: {
suggestedMin: 0,
suggestedMax: 100
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With