|
35 | 35 | getColour: '=?', |
36 | 36 | chartType: '=', |
37 | 37 | legend: '@', |
38 | | - click: '=' |
| 38 | + click: '=', |
| 39 | + hover: '=' |
39 | 40 | }, |
40 | 41 | link: function (scope, elem/*, attrs */) { |
41 | 42 | var chart, container = document.createElement('div'); |
|
120 | 121 | var chart = new Chart(ctx)[type](data, scope.options || {}); |
121 | 122 | scope.$emit('create', chart); |
122 | 123 |
|
123 | | - if (scope.click) { |
124 | | - cvs.onclick = function (evt) { |
125 | | - var click = chart.getPointsAtEvent || chart.getBarsAtEvent || chart.getSegmentsAtEvent; |
126 | | - |
127 | | - if (click) { |
128 | | - var activePoints = click.call(chart, evt); |
129 | | - scope.click(activePoints, evt); |
130 | | - scope.$apply(); |
131 | | - } |
132 | | - }; |
133 | | - } |
| 124 | + ['hover', 'click'].forEach(function (action) { |
| 125 | + if (scope[action]) cvs[action === 'click' ? 'onclick' : 'onmousemove'] = getEventHandler(scope, chart, action); |
| 126 | + }); |
134 | 127 | if (scope.legend && scope.legend !== 'false') setLegend(elem, chart); |
135 | 128 | return chart; |
136 | 129 | } |
137 | 130 |
|
| 131 | + function getEventHandler (scope, chart, action) { |
| 132 | + return function (evt) { |
| 133 | + var atEvent = chart.getPointsAtEvent || chart.getBarsAtEvent || chart.getSegmentsAtEvent; |
| 134 | + if (atEvent) { |
| 135 | + var activePoints = atEvent.call(chart, evt); |
| 136 | + scope[action](activePoints, evt); |
| 137 | + scope.$apply(); |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | + |
138 | 142 | function getColours (scope) { |
139 | 143 | var colours = angular.copy(scope.colours) || angular.copy(Chart.defaults.global.colours); |
140 | 144 | while (colours.length < scope.data.length) { |
|
0 commit comments