Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js Bar graph will not start at zero as minimum value

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.

like image 463
omega1 Avatar asked Oct 09 '18 07:10

omega1


People also ask

Can bar graphs not start at 0?

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.

How do you make a bar graph start at 0?

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.

Should charts always start with 0?

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.

Why should the Y-axis start from 0 in a bar chart?

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.

Should a bar chart start with a zero?

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.

How do I create a horizontal bar chart?

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.

What are the options available on the bar chart?

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.

What is the default x-axis for a horizontal bar chart?

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.


2 Answers

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.

like image 82
robbannn Avatar answered Oct 04 '22 01:10

robbannn


options: {
     resposive: true,
     scales: {
             y: {
                suggestedMin: 0,
                suggestedMax: 100
                }
             }
     }
like image 31
Muhammad Zakaria Avatar answered Oct 04 '22 00:10

Muhammad Zakaria