[Chartjs]-How to properly implement crosshair plugin with vue chartjs? – keep getting Cannot read properties of undefined error (reading 'dragStarted')

1πŸ‘

βœ…

This is because you gave your scales a custom ID, setting them back to the default x and y (by changing the object keys from yAxes to y and from xAxes to x) will resolve your issue:

const 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'
      }
    ]
  },
  options: {
    hover: {
      intersect: false
    },
    scales: {
      x: {
        stacked: true,
        grid: {
          color: "rgba(80,201,209,.3)",
          borderColor: "rgba(80,201,209,1)"
        },
        title: {
          display: true,
          text: 'value'
        },
        ticks: {
          color: "rgba(80,201,209,1)",
          source: 'labels',
        },
      },
      y: {
        grid: {
          color: "rgba(80,201,209,.3)",
          borderColor: "rgba(80,201,209,1)"
        },
        title: {
          display: false,
        },
        ticks: {
          color: 'rgba(80, 201, 209, 1)',
        },
      },
    },
    plugins: {
      crosshair: {
        sync: {
          enabled: false
        },
        zoom: {
          enabled: false
        },
        snap: {
          enabled: true
        }
      }
    }
  }
}

const 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.8.0/chart.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-crosshair"></script>
</body>

Leave a comment