Chartjs-Chart.js: hide dataset line but keep ticks on y-axis?

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>

Leave a comment