Skip to content

Commit 3ed974a

Browse files
committed
Trigger event only when last point(s) change
1 parent fdc6b5d commit 3ed974a

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

angular-chart.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -199,25 +199,23 @@
199199
}
200200

201201
function getEventHandler (scope, action, triggerOnlyOnChange) {
202-
var lastState = null;
202+
var lastState = {
203+
point: void 0,
204+
points: void 0
205+
};
203206
return function (evt) {
204207
var atEvent = scope.chart.getElementAtEvent || scope.chart.getPointAtEvent;
205208
var atEvents = scope.chart.getElementsAtEvent || scope.chart.getPointsAtEvent;
206209
if (atEvents) {
207-
// get all point
208210
var points = atEvents.call(scope.chart, evt);
209-
var activePoint;
210-
// get active point
211-
if (atEvent) {
212-
var arrayActivePoint = atEvent.call(scope.chart, evt); // return array of 0 or 1 point
213-
if (arrayActivePoint.length) {
214-
activePoint = arrayActivePoint[0];
215-
}
216-
}
217-
218-
if (triggerOnlyOnChange === false || angular.equals(lastState, points) === false) {
219-
lastState = points;
220-
scope[action](points, evt, activePoint);
211+
var point = atEvent ? atEvent.call(scope.chart, evt)[0] : void 0;
212+
213+
if (triggerOnlyOnChange === false ||
214+
(! angular.equals(lastState.points, points) && ! angular.equals(lastState.point, point))
215+
) {
216+
lastState.point = point;
217+
lastState.points = points;
218+
scope[action](points, evt, point);
221219
}
222220
}
223221
};
@@ -255,7 +253,7 @@
255253

256254
function getColor (color) {
257255
var alpha = color[3] || 1;
258-
color = color.slice(0, 3);
256+
color = color.slice(0, 3);
259257
return {
260258
backgroundColor: rgba(color, 0.2),
261259
pointBackgroundColor: rgba(color, alpha),
@@ -288,7 +286,7 @@
288286
function rgbStringToRgb (color) {
289287
var match = color.match(/^rgba?\(([\d,.]+)\)$/);
290288
if (! match) throw new Error('Cannot parse rgb value');
291-
color = match[1].split(',');
289+
color = match[1].split(',');
292290
return color.map(Number);
293291
}
294292

@@ -387,4 +385,4 @@
387385
scope.$emit('chart-destroy', scope.chart);
388386
}
389387
}
390-
}));
388+
}));

0 commit comments

Comments
 (0)