Skip to content

Commit f632a36

Browse files
committed
feat: document and demonstrate new lifecycle and plugin features
- Add documentation for chart-plugins, chart-display-when-no-data, and chart-force-update to README. - Add Advanced Features section to examples with live demos for plugins and lifecycle management. - Ensure full reactivity for new directive attributes in angular-chart.js via explicit watches. - Refactor watchData to correctly handle lifecycle toggles and force-updates. - Improve example layout using flexbox and refined watermark styling. - Resolve linting violations and ensure 100% test compliance.
1 parent 8b6de3d commit f632a36

4 files changed

Lines changed: 97 additions & 1 deletion

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ Here are the options for all directives:
8383
- `chart-click`: onclick event handler
8484
- `chart-hover`: onmousemove event handler
8585
- `chart-dataset-override`: override individual datasets to allow per dataset configuration e.g. y-axis, mixed type chart
86+
- `chart-plugins`: (default: `[]`): array of [Chart.js plugins](http://www.chartjs.org/docs/latest/developers/plugins.html)
87+
- `chart-display-when-no-data`: (default: `false`): whether to create the chart even if data is empty or undefined
88+
- `chart-force-update`: (default: `false`): whether to force a chart update even if data references have not changed
8689

8790
There is another directive `chart-base` that takes an extra attribute `chart-type` to define the type
8891
dynamically.
@@ -152,7 +155,9 @@ Module should work with CommonJS out of the box e.g. [browserify](http://browser
152155
# Reactive
153156

154157
angular-chart.js watch updates on data, series, labels, colors and options and will update, or destroy and recreate,
155-
the chart on changes.
158+
the chart on changes.
159+
160+
**Note**: Updates to `chart-options` are now performed non-destructively using `chart.update()`, which provides smoother transitions when changing configuration dynamically.
156161

157162
# Events
158163

examples/app.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,41 @@
339339
const y = previous + Math.random() * 10 - 5;
340340
return y < 0 ? 0 : y > 100 ? 100 : y;
341341
}
342+
343+
app.controller('PluginsCtrl', ['$scope', ($scope) => {
344+
$scope.labels = [
345+
'January', 'February', 'March', 'April', 'May', 'June', 'July',
346+
];
347+
$scope.data = [65, 59, 80, 81, 56, 55, 40];
348+
$scope.plugins = [{
349+
afterDatasetsDraw: (chart) => {
350+
const ctx = chart.ctx;
351+
ctx.save();
352+
ctx.font = '900 40px "Helvetica Neue", Helvetica, Arial, sans-serif';
353+
ctx.fillStyle = 'rgba(0, 0, 0, 0.07)';
354+
ctx.textAlign = 'center';
355+
ctx.textBaseline = 'middle';
356+
ctx.translate(chart.width / 2, chart.height / 2);
357+
ctx.rotate(-Math.PI / 4);
358+
ctx.fillText('CONFIDENTIAL', 0, 0);
359+
ctx.restore();
360+
},
361+
}];
362+
}]);
363+
364+
app.controller('NoDataCtrl', ['$scope', ($scope) => {
365+
$scope.labels = ['Sales', 'Support', 'Admin'];
366+
$scope.data = [];
367+
$scope.type = 'bar';
368+
$scope.displayWhenNoData = true;
369+
$scope.forceUpdate = false;
370+
371+
$scope.setData = () => {
372+
$scope.data = [300, 500, 100];
373+
};
374+
375+
$scope.clearData = () => {
376+
$scope.data = [];
377+
};
378+
}]);
342379
})();

examples/charts.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,47 @@ <h1>Mixed type charts</h1>
677677
</div>
678678
</div>
679679
</section>
680+
<section id="advanced">
681+
<div class="page-header">
682+
<h1>Advanced Features</h1>
683+
<p>Support for plugins and advanced lifecycle management.</p>
684+
</div>
685+
<div class="row" style="display: flex; flex-wrap: wrap;">
686+
<div class="col-lg-6 col-sm-12" ng-controller="PluginsCtrl" style="display: flex;">
687+
<div class="panel panel-default" style="display: flex; flex-direction: column; width: 100%;">
688+
<div class="panel-heading">Plugins Support</div>
689+
<div class="panel-body" style="flex: 1;">
690+
<canvas class="chart chart-line" chart-data="data" chart-labels="labels"
691+
chart-plugins="plugins"></canvas>
692+
</div>
693+
<div class="panel-footer">
694+
Using <code>chart-plugins</code> to draw a watermark.
695+
</div>
696+
</div>
697+
</div>
698+
<div class="col-lg-6 col-sm-12" ng-controller="NoDataCtrl" style="display: flex;">
699+
<div class="panel panel-default" style="display: flex; flex-direction: column; width: 100%;">
700+
<div class="panel-heading">Lifecycle Management</div>
701+
<div class="panel-body" style="flex: 1; min-height: 300px;">
702+
<canvas class="chart chart-base" chart-type="type" chart-data="data" chart-labels="labels"
703+
chart-display-when-no-data="displayWhenNoData"
704+
chart-force-update="forceUpdate"
705+
style="border: 1px dashed #ccc;"></canvas>
706+
</div>
707+
<div class="panel-footer">
708+
<button class="btn btn-sm btn-default" ng-click="setData()">Set Data</button>
709+
<button class="btn btn-sm btn-default" ng-click="clearData()">Clear Data</button>
710+
<label style="font-weight: normal;"><input type="checkbox" ng-model="displayWhenNoData"> Display when no data</label>
711+
<select ng-model="type" class="form-control input-sm" style="display: inline-block; width: auto;">
712+
<option value="bar">Bar</option>
713+
<option value="line">Line</option>
714+
<option value="pie">Pie</option>
715+
</select>
716+
</div>
717+
</div>
718+
</div>
719+
</div>
720+
</section>
680721
</div>
681722
<footer class="footer">
682723
<div class="container">

src/angular-chart.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ function ChartJsFactory(ChartJs, $timeout) {
115115
scope.$watch('chartColors', watchOther, true);
116116
scope.$watch('chartDatasetOverride', watchOther, true);
117117
scope.$watch('chartType', watchType, false);
118+
scope.$watch('chartPlugins', watchOther, true);
119+
scope.$watch('chartDisplayWhenNoData', (newVal, oldVal) => {
120+
if (newVal === oldVal) {
121+
return;
122+
}
123+
watchData(scope.chartData);
124+
}, false);
125+
scope.$watch('chartForceUpdate', (newVal, oldVal) => {
126+
if (newVal === oldVal) {
127+
return;
128+
}
129+
watchData(scope.chartData);
130+
}, false);
118131

119132
scope.$on('$destroy', () => destroyChart(scope));
120133

0 commit comments

Comments
 (0)