Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set axes' step size in Chart.js 2?

I created a simple line chart using chart.js with 3 Y axes: https://codepen.io/anon/pen/dZVgKw

As you can see, the last one is going from 10 to 20 without any number between. How can I set step size here?

This is how I add an axe:

{
  id: 'C',
  type: 'linear',
  position: 'left',
  ticks: {
    max: 10,
    min: 20,
  },
}

Thanks.

like image 725
Damien Monni Avatar asked Nov 16 '17 16:11

Damien Monni


People also ask

What is step size in chart js?

Step Size. If set, the scale ticks will be enumerated by multiple of stepSize , having one tick per increment. If not set, the ticks are labeled automatically using the nice numbers algorithm. This example sets up a chart with a y axis that creates ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5 .

Does chart js use canvas?

Chart. js charts are rendered on user provided canvas elements. Thus, it is up to the user to create the canvas element in a way that is accessible.

Does chart js use SVG?

Chart. js still uses the HTML Canvas element, so charts generated with it look blurred when printed or zoomed in. Nowadays I prefer Apexcharts. js, which renders to SVG.

What is chart legend JavaScript?

The chart legend displays data about the datasets that are appearing on the chart.


1 Answers

How can I set step size here?

Straight from the samples (Linear Scale, step size):

By setting a stepSize value.

scales: {
  xAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Month'
    }
  }],
  yAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Value'
    },
    ticks: {
      min: 0,
      max: 100,

      // forces step size to be 5 units
      stepSize: 5 // <----- This prop sets the stepSize
    }
  }]
}

Here's a live example:

var ctx = document.getElementById('chartJSContainer').getContext('2d')

new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [
      {
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },  
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          reverse: false,
          stepSize: 3
        },
      }]
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script>
<body>
    <canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>
like image 197
nicholaswmin Avatar answered Sep 22 '22 10:09

nicholaswmin