Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit f25b0b5

Browse files
committed
fix(angular-chartist): Watch chartType for changes
Fixes #25
1 parent 45ec629 commit f25b0b5

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

dist/angular-chartist.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
}, this);
2828
};
2929

30-
AngularChartistCtrl.prototype.renderChart = function (element) {
30+
AngularChartistCtrl.prototype.renderChart = function (element, chartType) {
31+
if (chartType) {
32+
this.chartType = chartType;
33+
}
34+
3135
return Chartist[this.chartType](element, this.data, this.options, this.responsiveOptions);
3236
};
3337

@@ -53,11 +57,22 @@
5357
Ctrl.bindEvents(chart);
5458

5559
// Deeply watch the data and create a new chart if data is updated
56-
scope.$watch(scope.data, function (newData, oldData) {
60+
scope.$watchGroup(['data', 'chartType'], function (newDataArray, oldDataArray) {
61+
// new data object
62+
var newData = newDataArray[0];
63+
var oldData = oldDataArray[0];
64+
65+
// new chart type
66+
var newChartType = newDataArray[1];
67+
var oldChartType = oldDataArray[1];
68+
5769
// Avoid initializing the chart twice
5870
if (newData !== oldData) {
5971
chart.update(newData);
72+
} else if (newChartType !== oldChartType) {
73+
chart = Ctrl.renderChart(elm, newChartType);
6074
}
75+
6176
}, true);
6277
}
6378
};

dist/angular-chartist.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/angular-chartist.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
}, this);
2828
};
2929

30-
AngularChartistCtrl.prototype.renderChart = function (element) {
30+
AngularChartistCtrl.prototype.renderChart = function (element, chartType) {
31+
if (chartType) {
32+
this.chartType = chartType;
33+
}
34+
3135
return Chartist[this.chartType](element, this.data, this.options, this.responsiveOptions);
3236
};
3337

@@ -53,11 +57,22 @@
5357
Ctrl.bindEvents(chart);
5458

5559
// Deeply watch the data and create a new chart if data is updated
56-
scope.$watch(scope.data, function (newData, oldData) {
60+
scope.$watchGroup(['data', 'chartType'], function (newDataArray, oldDataArray) {
61+
// new data object
62+
var newData = newDataArray[0];
63+
var oldData = oldDataArray[0];
64+
65+
// new chart type
66+
var newChartType = newDataArray[1];
67+
var oldChartType = oldDataArray[1];
68+
5769
// Avoid initializing the chart twice
5870
if (newData !== oldData) {
5971
chart.update(newData);
72+
} else if (newChartType !== oldChartType) {
73+
chart = Ctrl.renderChart(elm, newChartType);
6074
}
75+
6176
}, true);
6277
}
6378
};

example/lib/angular-chartist.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/angular-chartist.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ AngularChartistCtrl.prototype.bindEvents = function(chart) {
1717
}, this);
1818
};
1919

20-
AngularChartistCtrl.prototype.renderChart = function(element) {
20+
AngularChartistCtrl.prototype.renderChart = function(element, chartType) {
21+
if (chartType) {
22+
this.chartType = chartType;
23+
}
24+
2125
return Chartist[this.chartType](element, this.data, this.options, this.responsiveOptions);
2226
};
2327

2428
angularChartist.directive('chartist', [
29+
2530
function() {
2631
return {
2732
restrict: 'EA',
@@ -45,11 +50,22 @@ angularChartist.directive('chartist', [
4550
Ctrl.bindEvents(chart);
4651

4752
// Deeply watch the data and create a new chart if data is updated
48-
scope.$watch(scope.data, function(newData, oldData) {
53+
scope.$watchGroup(['data', 'chartType'], function(newDataArray, oldDataArray) {
54+
// new data object
55+
var newData = newDataArray[0];
56+
var oldData = oldDataArray[0];
57+
58+
// new chart type
59+
var newChartType = newDataArray[1];
60+
var oldChartType = oldDataArray[1];
61+
4962
// Avoid initializing the chart twice
5063
if (newData !== oldData) {
5164
chart.update(newData);
65+
} else if (newChartType !== oldChartType) {
66+
chart = Ctrl.renderChart(elm, newChartType);
5267
}
68+
5369
}, true);
5470
}
5571
};

0 commit comments

Comments
 (0)