Skip to content

Commit 88f7c59

Browse files
author
Ng Yew Choong
committed
Improved series data update handling.
1 parent e9c2384 commit 88f7c59

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-rickshaw",
3-
"version": "0.8.0",
3+
"version": "0.10.0",
44
"main": "rickshaw.js",
55
"authors": [
66
"Nick Ng <ngyewch@gmail.com>"

rickshaw.js

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,45 @@ angular.module('angular-rickshaw', [])
4848
graph.render();
4949
}
5050

51-
function update() {
51+
function _splice(args) {
52+
var data = args.data;
53+
var series = args.series;
54+
55+
if (!args.series) {
56+
return data;
57+
}
58+
59+
series.forEach(function(s) {
60+
var seriesKey = s.key || s.name;
61+
if (!seriesKey) {
62+
throw "series needs a key or a name";
63+
}
64+
65+
data.forEach(function(d) {
66+
var dataKey = d.key || d.name;
67+
if (!dataKey) {
68+
throw "data needs a key or a name";
69+
}
70+
if (seriesKey == dataKey) {
71+
var properties = ['color', 'name', 'data'];
72+
properties.forEach(function(p) {
73+
if (d[p]) {
74+
s[p] = d[p];
75+
}
76+
});
77+
}
78+
} );
79+
});
80+
}
81+
82+
function updateData() {
83+
if (graph && settings) {
84+
_splice({ data: scope.series, series: settings.series });
85+
redraw();
86+
}
87+
}
88+
89+
function updateConfiguration() {
5290
if (!graph) {
5391
mainEl = angular.element(element);
5492
mainEl.append(graphEl);
@@ -63,9 +101,13 @@ angular.module('angular-rickshaw', [])
63101
graph = new Rickshaw.Graph(settings);
64102
}
65103
else {
66-
settings = angular.copy(scope.options, settings);
67-
settings.element = graphEl[0];
68-
settings.series = scope.series;
104+
if (scope.options) {
105+
for (var key in scope.options) {
106+
settings[key] = scope.options[key];
107+
console.log(key + '=' + scope.options[key]);
108+
}
109+
settings.element = graphEl[0];
110+
}
69111

70112
graph.configure(settings);
71113
}
@@ -174,28 +216,17 @@ angular.module('angular-rickshaw', [])
174216

175217
var optionsWatch = scope.$watch('options', function(newValue, oldValue) {
176218
if (!angular.equals(newValue, oldValue)) {
177-
update();
219+
updateConfiguration();
178220
}
179221
}, true);
180-
var seriesWatch = scope.$watch(function(scope) {
181-
if (scope.features && scope.features.directive && scope.features.directive.watchAllSeries) {
182-
var watches = {};
183-
for (var i = 0; i < scope.series.length; i++) {
184-
watches['series' + i] = scope.series[i].data;
185-
}
186-
return watches;
187-
}
188-
else {
189-
return scope.series[0].data;
190-
}
191-
}, function(newValue, oldValue) {
222+
var seriesWatch = scope.$watchCollection('series', function(newValue, oldValue) {
192223
if (!angular.equals(newValue, oldValue)) {
193-
update();
224+
updateData();
194225
}
195226
}, true);
196227
var featuresWatch = scope.$watch('features', function(newValue, oldValue) {
197228
if (!angular.equals(newValue, oldValue)) {
198-
update();
229+
updateConfiguration();
199230
}
200231
}, true);
201232

@@ -213,7 +244,7 @@ angular.module('angular-rickshaw', [])
213244
redraw();
214245
});
215246

216-
update();
247+
updateConfiguration();
217248
},
218249
controller: function($scope, $element, $attrs) {
219250
}

0 commit comments

Comments
 (0)