|
1 | 1 | /**
|
2 | 2 | * @ngdoc directive
|
3 |
| - * @name patternfly.charts.directive:pfC3Chart |
| 3 | + * @name patternfly.charts.component:pfC3Chart |
| 4 | + * @restrict E |
4 | 5 | *
|
5 | 6 | * @description
|
6 |
| - * Directive for wrapping c3 library |
| 7 | + * Component for wrapping c3 library |
7 | 8 | *
|
8 | 9 | * Note: The 'patternfly.charts' module is not a dependency in the default angular 'patternfly' module.
|
9 | 10 | * In order to use patternfly charts you must add 'patternfly.charts' as a dependency in your application.
|
|
12 | 13 | * @param {string} id the ID of the container that the chart should bind to
|
13 | 14 | * @param {expression} config the c3 configuration options for the chart
|
14 | 15 | * @param {function (chart))=} getChartCallback the callback user function to be called once the chart is generated, containing the c3 chart object
|
15 |
| - * |
16 | 16 | * @example
|
17 | 17 |
|
18 | 18 | <example module="patternfly.charts">
|
19 | 19 | <file name="index.html">
|
20 | 20 | <div ng-controller="ChartCtrl">
|
21 |
| - <div pf-c3-chart id="chartId" config="chartConfig" get-chart-callback="getChart"></div> |
| 21 | + <pf-c3-chart id="chartId" config="chartConfig" get-chart-callback="getChart"></pf-c3-chart> |
22 | 22 |
|
23 | 23 | <form role="form" style="width:300px">
|
24 | 24 | Total = {{total}}, Used = {{used}}, Available = {{available}}
|
|
64 | 64 | }
|
65 | 65 |
|
66 | 66 | $scope.submitform = function (val) {
|
| 67 | + console.log("submitform"); |
67 | 68 | $scope.used = val;
|
68 | 69 | $scope.updateAvailable();
|
69 | 70 | $scope.chartConfig.data.columns = [["Used",$scope.used],["Available",$scope.available]];
|
|
75 | 76 | (function () {
|
76 | 77 | 'use strict';
|
77 | 78 |
|
78 |
| - angular.module('patternfly.charts').directive('pfC3Chart', function ($timeout) { |
79 |
| - return { |
80 |
| - restrict: 'A', |
81 |
| - scope: { |
82 |
| - config: '=', |
83 |
| - getChartCallback: '=' |
84 |
| - }, |
85 |
| - template: '<div id=""></div>', |
86 |
| - replace: true, |
87 |
| - link: function (scope, element, attrs) { |
88 |
| - scope.$watch('config', function () { |
89 |
| - $timeout(function () { |
90 |
| - // store the chart object |
91 |
| - var chart; |
92 |
| - //generate c3 chart data |
93 |
| - var chartData = scope.config; |
94 |
| - if (chartData) { |
95 |
| - chartData.bindto = '#' + attrs.id; |
96 |
| - chart = c3.generate(chartData); |
97 |
| - if (scope.getChartCallback) { |
98 |
| - scope.getChartCallback(chart); |
99 |
| - } |
100 |
| - } |
101 |
| - }); |
102 |
| - }, true); |
103 |
| - } |
104 |
| - }; |
| 79 | + angular.module('patternfly.charts').component('pfC3Chart', { |
| 80 | + bindings: { |
| 81 | + config: '<', |
| 82 | + getChartCallback: '<' |
| 83 | + }, |
| 84 | + template: '<div id=""></div>', |
| 85 | + controller: function ($timeout, $attrs) { |
| 86 | + var ctrl = this, prevConfig; |
| 87 | + |
| 88 | + ctrl.generateChart = function () { |
| 89 | + var chart; |
| 90 | + var chartData; |
| 91 | + |
| 92 | + // Need to deep watch changes in chart config |
| 93 | + prevConfig = angular.copy(ctrl.config); |
| 94 | + |
| 95 | + $timeout(function () { |
| 96 | + chartData = ctrl.config; |
| 97 | + if (chartData) { |
| 98 | + chartData.bindto = '#' + $attrs.id; |
| 99 | + chart = c3.generate(chartData); |
| 100 | + ctrl.getChartCallback(chart); |
| 101 | + prevConfig = angular.copy(ctrl.config); |
| 102 | + } |
| 103 | + }); |
| 104 | + }; |
| 105 | + |
| 106 | + ctrl.$doCheck = function () { |
| 107 | + // do a deep compare on config |
| 108 | + if (!angular.equals(ctrl.config, prevConfig)) { |
| 109 | + ctrl.generateChart(); |
| 110 | + } |
| 111 | + }; |
| 112 | + } |
105 | 113 | });
|
106 | 114 | }());
|
0 commit comments