Skip to content

Commit 97ba76b

Browse files
committed
feat(event): emit 'angular-chart-rendered' event when the chart is generated
1 parent 9ba150d commit 97ba76b

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,23 @@ Every time a new chart instance is created the `instance` of the directive will
191191
You can call all [c3js API calls](http://c3js.org/reference.html#api) such as `flow()`, `resize()`, ... on the chart instance.
192192

193193

194+
## Events
195+
196+
The `angular-chart-rendered` event is emitted every time a chart is generated or regenerated. It gives access to the `options` the chart is generated with and to the newly created chart instance.
197+
198+
```javascript
199+
$scope.$on('angular-chart-rendered', function(event, options, instance) {
200+
console.log(options, instance);
201+
});
202+
```
203+
204+
194205
## custom Style
206+
195207
The whole chart is based on SVG, which allows you to stlye most parts using CSS.
196208
The documentation of c3.js provides a few [examples](http://c3js.org/examples.html#style) on how to style your chart.
197209

210+
198211
## Upgrade 0.2.x to 0.3.0
199212

200213
* `<angular-chart></angular-chart>` should be used instead of `<angularchart></angularchart>`

angular-chart.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,19 @@
601601
// TODO evaluate if it makes sense to destroy the chart first
602602

603603
var chart = c3.generate(chartService.configuration);
604+
604605
chartService.deferredChart.resolve(chart);
605606
chartService.scopeReference.instance = chart;
606607
chartService.chart = chart;
608+
609+
/*
610+
Custom event: 'angular-chart-rendered'
611+
- new chart instance generated
612+
- { configuration, chart } - data format
613+
- configuration - the options used to generate the chart
614+
- chart - the newly created chart instance
615+
*/
616+
this.scopeReference.$emit('angular-chart-rendered', chartService.configuration, chartService.chart);
607617
};
608618

609619
/**

angular-chart.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.

debug/debug-controller.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
'use strict';
44

5-
function DebugController($q, $timeout, DebugData) {
5+
function DebugController($scope, $q, $timeout, DebugData) {
66
var vm = this;
77

88
/*
@@ -135,6 +135,11 @@
135135
console.log('instance', vm.instance);
136136
};
137137

138+
// inform about chart rendering
139+
$scope.$on('angular-chart-rendered', function(event, conf, instance) {
140+
console.log(conf, instance);
141+
});
142+
138143
}
139144

140145
angular

src/js/service.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,19 @@
135135
// TODO evaluate if it makes sense to destroy the chart first
136136

137137
var chart = c3.generate(chartService.configuration);
138+
138139
chartService.deferredChart.resolve(chart);
139140
chartService.scopeReference.instance = chart;
140141
chartService.chart = chart;
142+
143+
/*
144+
Custom event: 'angular-chart-rendered'
145+
- new chart instance generated
146+
- { configuration, chart } - data format
147+
- configuration - the options used to generate the chart
148+
- chart - the newly created chart instance
149+
*/
150+
this.scopeReference.$emit('angular-chart-rendered', chartService.configuration, chartService.chart);
141151
};
142152

143153
/**

test/service_spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ describe('Service: AngularChartService', function () {
4444
$timeout.flush();
4545
});
4646

47+
// init()
48+
it('emit event on initialization.', function () {
49+
// wait for events
50+
var eventEmitted = false;
51+
var eventConfiguration = {};
52+
var eventChartInstance = {};
53+
$scope.$on('angular-chart-rendered', function(conf, instance) {
54+
eventEmitted = true;
55+
eventConfiguration = conf;
56+
eventChartInstance = instance;
57+
});
58+
59+
// setup
60+
var configuration = angular.copy(baseConfiguration);
61+
AngularChartService.getInstance(configuration, $scope);
62+
$scope.$apply();
63+
$timeout.flush();
64+
65+
// check result
66+
expect(eventEmitted).toBe(true);
67+
expect(eventConfiguration).toBeDefined();
68+
expect(eventChartInstance).toBeDefined();
69+
});
70+
4771

4872
// destroyChart()
4973
it('destroy a created chart.', function () {

0 commit comments

Comments
 (0)