[Chartjs]-How do I set a bar chart to default to a suggested maximum in chart.js v1.01?

1👍

Looking through the documentation included with v1.0.1 (zip file), there doesn’t appear to be a way to do this. I can’t see any option to set the scale values.

In v2.7.3 this is quite simple. A working example is below. The chart starts empty, with a y-axis scale from 0-10. Clicking ‘Add Series’ adds a new bar with a value of 5. Clicking a bar increments value by 1.

let btn1 = document.getElementById('add'),
  canvas = document.getElementById('chart'),
  chart = new Chart(canvas, {
    type: 'bar',
    data: {
      labels: [],
      datasets: []
    },
    options: {
      scales: {
        yAxes: [{
          ticks: {
            min: 0,
            suggestedMax: 10
          }
        }]
      }
    }
  });

canvas.addEventListener('click', function(e) {
  let idx = chart.getDatasetAtEvent(e)[0]._datasetIndex;
  chart.config.data.datasets[idx].data[0]++;
  chart.update();
});

btn1.addEventListener('click', function() {
  chart.config.data.datasets.push({
    data: [5]
  });
  chart.update();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<button id="add">Add Series</button> Click a bar to increment its value by 1.
<canvas id="chart"></canvas>

Leave a comment