Chart.js Bar Chart - how to chart bars from 0












7















I'd like to chart the bars extending from zero so that positive numbers grow upwards (as normal) but negative numbers grow downwards from zero. I get instead all numbers starting from a negative baseline.










share|improve this question



























    7















    I'd like to chart the bars extending from zero so that positive numbers grow upwards (as normal) but negative numbers grow downwards from zero. I get instead all numbers starting from a negative baseline.










    share|improve this question

























      7












      7








      7


      0






      I'd like to chart the bars extending from zero so that positive numbers grow upwards (as normal) but negative numbers grow downwards from zero. I get instead all numbers starting from a negative baseline.










      share|improve this question














      I'd like to chart the bars extending from zero so that positive numbers grow upwards (as normal) but negative numbers grow downwards from zero. I get instead all numbers starting from a negative baseline.







      chart.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 3 '15 at 6:50









      user1860288user1860288

      1202312




      1202312
























          6 Answers
          6






          active

          oldest

          votes


















          29














          The other answers didn't work for me. However, I did manage to find another config option that did work for me.



          var options = {
          // All of my other bar chart option here
          scales: {
          yAxes: [{
          ticks: {
          beginAtZero:true
          }
          }]
          }
          }
          var ctx = document.getElementById("myChart").getContext("2d");
          var myBarChart = new Chart(ctx).Bar(data, options);


          Passing this for my chart options give me a bar chart with the scale starting at 0.






          share|improve this answer





















          • 2





            Thanks! This should be the accepted answer.

            – GG.
            Nov 6 '16 at 1:43



















          2














          I think you're looking for something like this



          Fiddle



          HTML



          <canvas id="myChart" width="500" height="250"></canvas>


          JS



          var data = {
          labels: ["January", "February", "March", "April", "May", "June", "July", "August"],
          datasets: [
          {
          data: [65, 59, 80, 81, 56, 55, 40, -30]
          }
          ]
          };

          var options = {
          scaleBeginAtZero: false
          };

          var ctx = document.getElementById("myChart").getContext("2d");
          var myBarChart = new Chart(ctx).Bar(data, options);





          share|improve this answer
























          • I think you meant { scaleBeginAtZero: true }, which gives partially the effect I wanted. However, the y-axis does not begin below -30. When I tried options = { scaleOverride: true, scaleStartValue: -50, scaleStepWidth: 50, scaleSteps: 3, scaleBeginAtZero: true, }, then then bars all spring from the bottom again.

            – user1860288
            Jun 3 '15 at 13:11



















          1














          You can use d3.js for the intended behaviour. Here is a good post regarding that.
          Or
          You can also follow this article tweaking the plotkit chart to show the downwards negative bar.



          To make it work it with chart.js(though not advisable as this is not supported in Chart.js) you can use this github pull and instead of using the global configuration object, i.e. setting:



          Chart.defaults.global.responsive = true;
          Chart.defaults.global.scaleBeginAtZero = false;
          Chart.defaults.global.barBeginAtOrigin = false,
          Chart.defaults.global.animation = false;


          use a separate config object and pass it into the chart constructor:



          var chartOptions = {
          responsive:true,
          scaleBeginAtZero:false,
          barBeginAtOrigin:true
          }
          var chartInstance = new Chart(ctx).Bar(myChartData, chartOptions);





          share|improve this answer































            1














            I'm using Chart.js version 2.0.2 and this could be achieved using yAxes.min, and if the chart is empty you could use yAxes.suggestedMax



            Example:



            options:{
            .
            .
            .
            scales{
            yAxes:[{
            min:0,
            //this value will be overridden if the scale is greater than this value
            suggestedMax:10
            }]
            }





            share|improve this answer































              1














              public barChartOptions: any = {
              scales: {
              yAxes: [{
              ticks: {
              beginAtZero: true}
              }]
              },
              }





              share|improve this answer































                0














                use beginat zero like below:



                options: {
                scales: {
                yAxes: [{
                ticks: {
                beginAtZero:true
                }
                }]
                }
                }





                share|improve this answer
























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30612813%2fchart-js-bar-chart-how-to-chart-bars-from-0%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  29














                  The other answers didn't work for me. However, I did manage to find another config option that did work for me.



                  var options = {
                  // All of my other bar chart option here
                  scales: {
                  yAxes: [{
                  ticks: {
                  beginAtZero:true
                  }
                  }]
                  }
                  }
                  var ctx = document.getElementById("myChart").getContext("2d");
                  var myBarChart = new Chart(ctx).Bar(data, options);


                  Passing this for my chart options give me a bar chart with the scale starting at 0.






                  share|improve this answer





















                  • 2





                    Thanks! This should be the accepted answer.

                    – GG.
                    Nov 6 '16 at 1:43
















                  29














                  The other answers didn't work for me. However, I did manage to find another config option that did work for me.



                  var options = {
                  // All of my other bar chart option here
                  scales: {
                  yAxes: [{
                  ticks: {
                  beginAtZero:true
                  }
                  }]
                  }
                  }
                  var ctx = document.getElementById("myChart").getContext("2d");
                  var myBarChart = new Chart(ctx).Bar(data, options);


                  Passing this for my chart options give me a bar chart with the scale starting at 0.






                  share|improve this answer





















                  • 2





                    Thanks! This should be the accepted answer.

                    – GG.
                    Nov 6 '16 at 1:43














                  29












                  29








                  29







                  The other answers didn't work for me. However, I did manage to find another config option that did work for me.



                  var options = {
                  // All of my other bar chart option here
                  scales: {
                  yAxes: [{
                  ticks: {
                  beginAtZero:true
                  }
                  }]
                  }
                  }
                  var ctx = document.getElementById("myChart").getContext("2d");
                  var myBarChart = new Chart(ctx).Bar(data, options);


                  Passing this for my chart options give me a bar chart with the scale starting at 0.






                  share|improve this answer















                  The other answers didn't work for me. However, I did manage to find another config option that did work for me.



                  var options = {
                  // All of my other bar chart option here
                  scales: {
                  yAxes: [{
                  ticks: {
                  beginAtZero:true
                  }
                  }]
                  }
                  }
                  var ctx = document.getElementById("myChart").getContext("2d");
                  var myBarChart = new Chart(ctx).Bar(data, options);


                  Passing this for my chart options give me a bar chart with the scale starting at 0.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 25 '17 at 20:13









                  Black Mamba

                  3,11222441




                  3,11222441










                  answered Sep 26 '16 at 14:34









                  KbyeKbye

                  29135




                  29135








                  • 2





                    Thanks! This should be the accepted answer.

                    – GG.
                    Nov 6 '16 at 1:43














                  • 2





                    Thanks! This should be the accepted answer.

                    – GG.
                    Nov 6 '16 at 1:43








                  2




                  2





                  Thanks! This should be the accepted answer.

                  – GG.
                  Nov 6 '16 at 1:43





                  Thanks! This should be the accepted answer.

                  – GG.
                  Nov 6 '16 at 1:43













                  2














                  I think you're looking for something like this



                  Fiddle



                  HTML



                  <canvas id="myChart" width="500" height="250"></canvas>


                  JS



                  var data = {
                  labels: ["January", "February", "March", "April", "May", "June", "July", "August"],
                  datasets: [
                  {
                  data: [65, 59, 80, 81, 56, 55, 40, -30]
                  }
                  ]
                  };

                  var options = {
                  scaleBeginAtZero: false
                  };

                  var ctx = document.getElementById("myChart").getContext("2d");
                  var myBarChart = new Chart(ctx).Bar(data, options);





                  share|improve this answer
























                  • I think you meant { scaleBeginAtZero: true }, which gives partially the effect I wanted. However, the y-axis does not begin below -30. When I tried options = { scaleOverride: true, scaleStartValue: -50, scaleStepWidth: 50, scaleSteps: 3, scaleBeginAtZero: true, }, then then bars all spring from the bottom again.

                    – user1860288
                    Jun 3 '15 at 13:11
















                  2














                  I think you're looking for something like this



                  Fiddle



                  HTML



                  <canvas id="myChart" width="500" height="250"></canvas>


                  JS



                  var data = {
                  labels: ["January", "February", "March", "April", "May", "June", "July", "August"],
                  datasets: [
                  {
                  data: [65, 59, 80, 81, 56, 55, 40, -30]
                  }
                  ]
                  };

                  var options = {
                  scaleBeginAtZero: false
                  };

                  var ctx = document.getElementById("myChart").getContext("2d");
                  var myBarChart = new Chart(ctx).Bar(data, options);





                  share|improve this answer
























                  • I think you meant { scaleBeginAtZero: true }, which gives partially the effect I wanted. However, the y-axis does not begin below -30. When I tried options = { scaleOverride: true, scaleStartValue: -50, scaleStepWidth: 50, scaleSteps: 3, scaleBeginAtZero: true, }, then then bars all spring from the bottom again.

                    – user1860288
                    Jun 3 '15 at 13:11














                  2












                  2








                  2







                  I think you're looking for something like this



                  Fiddle



                  HTML



                  <canvas id="myChart" width="500" height="250"></canvas>


                  JS



                  var data = {
                  labels: ["January", "February", "March", "April", "May", "June", "July", "August"],
                  datasets: [
                  {
                  data: [65, 59, 80, 81, 56, 55, 40, -30]
                  }
                  ]
                  };

                  var options = {
                  scaleBeginAtZero: false
                  };

                  var ctx = document.getElementById("myChart").getContext("2d");
                  var myBarChart = new Chart(ctx).Bar(data, options);





                  share|improve this answer













                  I think you're looking for something like this



                  Fiddle



                  HTML



                  <canvas id="myChart" width="500" height="250"></canvas>


                  JS



                  var data = {
                  labels: ["January", "February", "March", "April", "May", "June", "July", "August"],
                  datasets: [
                  {
                  data: [65, 59, 80, 81, 56, 55, 40, -30]
                  }
                  ]
                  };

                  var options = {
                  scaleBeginAtZero: false
                  };

                  var ctx = document.getElementById("myChart").getContext("2d");
                  var myBarChart = new Chart(ctx).Bar(data, options);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 3 '15 at 7:27









                  fatCopfatCop

                  1,05172445




                  1,05172445













                  • I think you meant { scaleBeginAtZero: true }, which gives partially the effect I wanted. However, the y-axis does not begin below -30. When I tried options = { scaleOverride: true, scaleStartValue: -50, scaleStepWidth: 50, scaleSteps: 3, scaleBeginAtZero: true, }, then then bars all spring from the bottom again.

                    – user1860288
                    Jun 3 '15 at 13:11



















                  • I think you meant { scaleBeginAtZero: true }, which gives partially the effect I wanted. However, the y-axis does not begin below -30. When I tried options = { scaleOverride: true, scaleStartValue: -50, scaleStepWidth: 50, scaleSteps: 3, scaleBeginAtZero: true, }, then then bars all spring from the bottom again.

                    – user1860288
                    Jun 3 '15 at 13:11

















                  I think you meant { scaleBeginAtZero: true }, which gives partially the effect I wanted. However, the y-axis does not begin below -30. When I tried options = { scaleOverride: true, scaleStartValue: -50, scaleStepWidth: 50, scaleSteps: 3, scaleBeginAtZero: true, }, then then bars all spring from the bottom again.

                  – user1860288
                  Jun 3 '15 at 13:11





                  I think you meant { scaleBeginAtZero: true }, which gives partially the effect I wanted. However, the y-axis does not begin below -30. When I tried options = { scaleOverride: true, scaleStartValue: -50, scaleStepWidth: 50, scaleSteps: 3, scaleBeginAtZero: true, }, then then bars all spring from the bottom again.

                  – user1860288
                  Jun 3 '15 at 13:11











                  1














                  You can use d3.js for the intended behaviour. Here is a good post regarding that.
                  Or
                  You can also follow this article tweaking the plotkit chart to show the downwards negative bar.



                  To make it work it with chart.js(though not advisable as this is not supported in Chart.js) you can use this github pull and instead of using the global configuration object, i.e. setting:



                  Chart.defaults.global.responsive = true;
                  Chart.defaults.global.scaleBeginAtZero = false;
                  Chart.defaults.global.barBeginAtOrigin = false,
                  Chart.defaults.global.animation = false;


                  use a separate config object and pass it into the chart constructor:



                  var chartOptions = {
                  responsive:true,
                  scaleBeginAtZero:false,
                  barBeginAtOrigin:true
                  }
                  var chartInstance = new Chart(ctx).Bar(myChartData, chartOptions);





                  share|improve this answer




























                    1














                    You can use d3.js for the intended behaviour. Here is a good post regarding that.
                    Or
                    You can also follow this article tweaking the plotkit chart to show the downwards negative bar.



                    To make it work it with chart.js(though not advisable as this is not supported in Chart.js) you can use this github pull and instead of using the global configuration object, i.e. setting:



                    Chart.defaults.global.responsive = true;
                    Chart.defaults.global.scaleBeginAtZero = false;
                    Chart.defaults.global.barBeginAtOrigin = false,
                    Chart.defaults.global.animation = false;


                    use a separate config object and pass it into the chart constructor:



                    var chartOptions = {
                    responsive:true,
                    scaleBeginAtZero:false,
                    barBeginAtOrigin:true
                    }
                    var chartInstance = new Chart(ctx).Bar(myChartData, chartOptions);





                    share|improve this answer


























                      1












                      1








                      1







                      You can use d3.js for the intended behaviour. Here is a good post regarding that.
                      Or
                      You can also follow this article tweaking the plotkit chart to show the downwards negative bar.



                      To make it work it with chart.js(though not advisable as this is not supported in Chart.js) you can use this github pull and instead of using the global configuration object, i.e. setting:



                      Chart.defaults.global.responsive = true;
                      Chart.defaults.global.scaleBeginAtZero = false;
                      Chart.defaults.global.barBeginAtOrigin = false,
                      Chart.defaults.global.animation = false;


                      use a separate config object and pass it into the chart constructor:



                      var chartOptions = {
                      responsive:true,
                      scaleBeginAtZero:false,
                      barBeginAtOrigin:true
                      }
                      var chartInstance = new Chart(ctx).Bar(myChartData, chartOptions);





                      share|improve this answer













                      You can use d3.js for the intended behaviour. Here is a good post regarding that.
                      Or
                      You can also follow this article tweaking the plotkit chart to show the downwards negative bar.



                      To make it work it with chart.js(though not advisable as this is not supported in Chart.js) you can use this github pull and instead of using the global configuration object, i.e. setting:



                      Chart.defaults.global.responsive = true;
                      Chart.defaults.global.scaleBeginAtZero = false;
                      Chart.defaults.global.barBeginAtOrigin = false,
                      Chart.defaults.global.animation = false;


                      use a separate config object and pass it into the chart constructor:



                      var chartOptions = {
                      responsive:true,
                      scaleBeginAtZero:false,
                      barBeginAtOrigin:true
                      }
                      var chartInstance = new Chart(ctx).Bar(myChartData, chartOptions);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 3 '15 at 7:34









                      sam100ravsam100rav

                      2,71921938




                      2,71921938























                          1














                          I'm using Chart.js version 2.0.2 and this could be achieved using yAxes.min, and if the chart is empty you could use yAxes.suggestedMax



                          Example:



                          options:{
                          .
                          .
                          .
                          scales{
                          yAxes:[{
                          min:0,
                          //this value will be overridden if the scale is greater than this value
                          suggestedMax:10
                          }]
                          }





                          share|improve this answer




























                            1














                            I'm using Chart.js version 2.0.2 and this could be achieved using yAxes.min, and if the chart is empty you could use yAxes.suggestedMax



                            Example:



                            options:{
                            .
                            .
                            .
                            scales{
                            yAxes:[{
                            min:0,
                            //this value will be overridden if the scale is greater than this value
                            suggestedMax:10
                            }]
                            }





                            share|improve this answer


























                              1












                              1








                              1







                              I'm using Chart.js version 2.0.2 and this could be achieved using yAxes.min, and if the chart is empty you could use yAxes.suggestedMax



                              Example:



                              options:{
                              .
                              .
                              .
                              scales{
                              yAxes:[{
                              min:0,
                              //this value will be overridden if the scale is greater than this value
                              suggestedMax:10
                              }]
                              }





                              share|improve this answer













                              I'm using Chart.js version 2.0.2 and this could be achieved using yAxes.min, and if the chart is empty you could use yAxes.suggestedMax



                              Example:



                              options:{
                              .
                              .
                              .
                              scales{
                              yAxes:[{
                              min:0,
                              //this value will be overridden if the scale is greater than this value
                              suggestedMax:10
                              }]
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Apr 25 '17 at 9:48









                              Mohamed BadrMohamed Badr

                              1,6882137




                              1,6882137























                                  1














                                  public barChartOptions: any = {
                                  scales: {
                                  yAxes: [{
                                  ticks: {
                                  beginAtZero: true}
                                  }]
                                  },
                                  }





                                  share|improve this answer




























                                    1














                                    public barChartOptions: any = {
                                    scales: {
                                    yAxes: [{
                                    ticks: {
                                    beginAtZero: true}
                                    }]
                                    },
                                    }





                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      public barChartOptions: any = {
                                      scales: {
                                      yAxes: [{
                                      ticks: {
                                      beginAtZero: true}
                                      }]
                                      },
                                      }





                                      share|improve this answer













                                      public barChartOptions: any = {
                                      scales: {
                                      yAxes: [{
                                      ticks: {
                                      beginAtZero: true}
                                      }]
                                      },
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 6 '17 at 7:07









                                      Reegan SReegan S

                                      211




                                      211























                                          0














                                          use beginat zero like below:



                                          options: {
                                          scales: {
                                          yAxes: [{
                                          ticks: {
                                          beginAtZero:true
                                          }
                                          }]
                                          }
                                          }





                                          share|improve this answer




























                                            0














                                            use beginat zero like below:



                                            options: {
                                            scales: {
                                            yAxes: [{
                                            ticks: {
                                            beginAtZero:true
                                            }
                                            }]
                                            }
                                            }





                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              use beginat zero like below:



                                              options: {
                                              scales: {
                                              yAxes: [{
                                              ticks: {
                                              beginAtZero:true
                                              }
                                              }]
                                              }
                                              }





                                              share|improve this answer













                                              use beginat zero like below:



                                              options: {
                                              scales: {
                                              yAxes: [{
                                              ticks: {
                                              beginAtZero:true
                                              }
                                              }]
                                              }
                                              }






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 26 '18 at 11:07









                                              RameshRamesh

                                              1




                                              1






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30612813%2fchart-js-bar-chart-how-to-chart-bars-from-0%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  To store a contact into the json file from server.js file using a class in NodeJS

                                                  Redirect URL with Chrome Remote Debugging Android Devices

                                                  Dieringhausen