0

I have the following Line chart with two datasets that share the same x-axis labels. I'd like to know if is it possible to hide one of them but keep the labels (e.g. from 100 to 500) on the right y-axis.

My intent is two show just one line that references to two different values (the left one from -800 to 800 and the right one from 100 to 500) for instance.

UPDATE: I don't know if this is the right way of doing it. In an ideal world I have to show on the same label two different values on y-axis. So maybe there's a better way to show this using just one dataset with two datapoints anchored to the different y-axis.

I'm currently exploiting https://github.com/reactchartjs/react-chartjs-2

enter image description here

Barnercart
  • 1,523
  • 1
  • 11
  • 23

1 Answers1

0

You can add a second axis, specify a min and max so that even when you hide the dataset the ticks stay:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderColor: 'pink'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderColor: 'orange',
        yAxisID: 'secondY'
      }
    ]
  },
  options: {
    scales: {
      y: {
        display: true,
        min: 2,
        max: 20
      },
      secondY: {
        position: 'right',
        display: true,
        min: 3,
        max: 11
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.js"></script>
</body>
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69