Skip to content

[BUG] datasets[0].data.shift() causes an error #4416

@nagix

Description

@nagix

Expected Behavior

The code below should work.

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['1', '2', '3'],
        datasets: [{
            data: [0, 2, 4]
        },{
            data: [1, 3, 5]
        }]
    }
});

chart.data.datasets.shift();
chart.data.datasets[0].data.shift(); // ---(1)
chart.update();

Current Behavior

The following error occurs at (1) of the code above.

[Error] TypeError: undefined is not an object (evaluating 'dataset._meta')
	getDatasetMeta (Chart.js:4331)
	onDataShift (Chart.js:4886)
	(anonymous function) (Chart.js:4617)
	each (Chart.js:5052)
	value (Chart.js:4615)
	onload (show:65)"

Possible Solution

It seems that an array event listener is called when the data array has been altered by 'push', pop', 'shift', 'splice' or 'unshift'. However, if the dataset array has been altered right before, listeners may try to access meta data that doesn't exist as they are not aware of changes in its parent dataset.

We may need to monitor changes in not only data but also datasets.

Steps to Reproduce

This script has no problem:
https://jsfiddle.net/nagix/vo17cz5y/

This causes the error above:
https://jsfiddle.net/nagix/s2zpf4dq/

Environment

  • Chart.js version: 2.6.0
  • Browser name and version: Chrome 59.0.3071.109, FireFox 54.0, Safari 10.1.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions