Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit f84c4f0

Browse files
committed
Merge pull request #17 from aaronhayes/master
Dynamically Add/Remove Data Points
2 parents d8d9b80 + 750ee87 commit f84c4f0

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ node_modules
3030
.DS_Store
3131
showcase
3232
.generator-release
33+
34+
.idea/*

lib/core.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ module.exports = {
4242
} else {
4343
dataKey = dataKey || dataKeys[chart.name];
4444
updatePoints(nextProps, chart, dataKey);
45+
chart.scale.xLabels = nextProps.data.labels;
46+
chart.scale.calculateXLabelRotation();
4547
chart.update();
4648
}
4749
};
@@ -91,14 +93,26 @@ var updatePoints = function(nextProps, chart, dataKey) {
9193
chart.segments[segmentIndex].value = segment.value;
9294
});
9395
} else {
96+
while (chart.scale.xLabels.length > nextProps.data.labels.length) {
97+
chart.removeData();
98+
}
9499
nextProps.data.datasets.forEach(function(set, setIndex) {
95100
set.data.forEach(function(val, pointIndex) {
96-
chart.datasets[setIndex][dataKey][pointIndex].value = val;
101+
if (typeof(chart.datasets[setIndex][dataKey][pointIndex]) == "undefined") {
102+
addData(nextProps, chart, setIndex, pointIndex);
103+
} else {
104+
chart.datasets[setIndex][dataKey][pointIndex].value = val;
105+
}
97106
});
98107
});
99108
}
100109
};
101110

102-
103-
111+
var addData = function(nextProps, chart, setIndex, pointIndex) {
112+
var values = [];
113+
nextProps.data.datasets.forEach(function(set) {
114+
values.push(set.data[pointIndex]);
115+
});
116+
chart.addData(values, nextProps.data.labels[setIndex]);
117+
};
104118

0 commit comments

Comments
 (0)