Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

element._view is undefined in parseVisibleItems when adding multiple datapoints at once

Open lupusmeatus opened this issue 9 years ago • 2 comments

With chartjs 2.5.0 I got the following error:

TypeError: element._view is undefined in parseVisibleItems

  		var element = meta.data[j];
  		if (!element._view.skip) {
  			handler(element);
  		}

parseVisibleItems Chart.bundle.js:10298:9 getIntersectItems Chart.bundle.js:10314:3 indexMode Chart.bundle.js:10364:35 [23]</module.exports/<.getElementsAtEventForMode Chart.bundle.js:8629:12 [23]</module.exports/<.handleEvent Chart.bundle.js:8842:17 [23]</module.exports/<.eventHandler Chart.bundle.js:8800:18 [23]</module.exports/<.bindEvents/listener Chart.bundle.js:8735:5 Chart.bundle.js:14840:5

Because of the timing the issue may not be deterministic. But I can reproduce the issue using

var ctx = document.getElementById("myChart"); var myChart = new Chart(ctx, { type: 'line', data: { labels: [], datasets: [{label: 'value', data: [] }] }, options: { animation: { duration: 0 } } }); function f() { myChart.data.labels.push(myChart.data.labels.length); myChart.data.datasets[0].data.push(1); if (myChart.data.labels.length%20==0) myChart.update(); setTimeout(f, 400); } f();

see http://codepen.io/anon/pen/BpeVzN

lupusmeatus avatar Feb 18 '17 08:02 lupusmeatus

Thanks @lupusmeatus, seems a duplicated of #3753

simonbrunel avatar Feb 18 '17 10:02 simonbrunel

This is definitely related to #3753. If the if (myChart.data.labels.length%20==0) line is commented out, this doesn't crash. The reason there is a problem is that the chart has new data, but chart.update() has not been called yet.

If you want to buffer up 20 points and then append them to the chart, I would do it like this

var dataBuffer = [];
var labelBuffer = [];

function f() {
  dataBuffer.push(myChart.data.labels.length + dataBuffer.length);
  dataBuffer.push(1);
  if (labelBuffer.length === 20) {
    myChart.data.labels = myChart.data.labels.concat(labelBuffer);
    Array.prototype.push.apply(myChart.data.datasets[0].data, dataBuffer);
    myChart.update();
    labelBuffer = [];
    dataBuffer = [];
  }
  setTimeout(f, 400);
}
f();

etimberg avatar Apr 23 '17 12:04 etimberg