Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/core/core.datasetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1062,10 +1062,14 @@ export default class DatasetController {
}

_onDataSplice(start, count) {
if (arguments.length <= 1) {
// splice(start) with no deleteCount removes everything from start onwards
count = this._cachedMeta.data.length - start;
}
if (count) {
this._sync(['_removeElements', start, count]);
}
const newCount = arguments.length - 2;
const newCount = Math.max(arguments.length - 2, 0);
if (newCount) {
this._sync(['_insertElements', start, newCount]);
}
Expand Down
44 changes: 44 additions & 0 deletions test/specs/core.datasetController.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,50 @@ describe('Chart.DatasetController', function() {
expect(controller.getParsed(2)).toBe(data[2]);
});

it('should correctly handle splice with a single argument (no deleteCount)', function() {
var data = [0, 1, 2, 3, 4, 5];
var chart = acquireChart({
type: 'line',
data: {
datasets: [{
data: data
}]
}
});

var meta = chart.getDatasetMeta(0);
expect(meta.data.length).toBe(6);

// splice(0) with no deleteCount should remove all elements (per spec)
data.splice(0);
chart.update();

expect(data.length).toBe(0);
expect(meta.data.length).toBe(0);
});

it('should correctly handle splice with a single argument from a mid-index', function() {
var data = [0, 1, 2, 3, 4, 5];
var chart = acquireChart({
type: 'line',
data: {
datasets: [{
data: data
}]
}
});

var meta = chart.getDatasetMeta(0);
expect(meta.data.length).toBe(6);

// splice(2) should remove elements from index 2 onwards
data.splice(2);
chart.update();

expect(data.length).toBe(2);
expect(meta.data.length).toBe(2);
});

it('should re-synchronize metadata when the data object reference changes', function() {
var data0 = [0, 1, 2, 3, 4, 5];
var data1 = [6, 7, 8];
Expand Down