[Chartjs]-Chart.js two Y-Axis, one with negative values

0👍

If the left chart is always -1 to 1, then you need the right chart to be from -z to z.

Given your dataset, calculate the max value (if your right set included negative numbers you’d also need the least number, then take the absolute value of both and see which is greater, but your code says it’s only positive).

Something like const yMax = Math.max(...dataset.map(d => d.y));

Set your options up like this

  options: {
    scales: {
      yAxes: [{
        id: 'left',
        type: 'linear',
        position: 'left',
      }, {
        id: 'right',
        type: 'linear',
        position: 'right'
        ticks: {
          min: -yMax,
          max: yMax
        }
      }]
    }
  }

Leave a comment