|
80 | 80 | labels: '=', |
81 | 81 | options: '=', |
82 | 82 | series: '=', |
83 | | - colours: '=', |
| 83 | + colours: '=?', |
| 84 | + getColour: '=?', |
84 | 85 | chartType: '=', |
85 | 86 | legend: '@', |
86 | 87 | click: '=' |
|
155 | 156 |
|
156 | 157 | function createChart (type, scope, elem) { |
157 | 158 | if (! scope.data || ! scope.data.length) return; |
| 159 | + scope.getColour = typeof scope.getColour === 'function' ? scope.getColour : getRandomColour; |
| 160 | + scope.colours = getColours(scope); |
158 | 161 | var cvs = elem[0], ctx = cvs.getContext('2d'); |
159 | 162 | var data = Array.isArray(scope.data[0]) ? |
160 | 163 | getDataSets(scope.labels, scope.data, scope.series || [], scope.colours) : |
|
176 | 179 | return chart; |
177 | 180 | } |
178 | 181 |
|
179 | | - function setLegend (elem, chart) { |
180 | | - var $parent = elem.parent(), |
181 | | - $oldLegend = $parent.find('chart-legend'), |
182 | | - legend = '<chart-legend>' + chart.generateLegend() + '</chart-legend>'; |
183 | | - if ($oldLegend.length) $oldLegend.replaceWith(legend); |
184 | | - else $parent.append(legend); |
| 182 | + function getColours (scope) { |
| 183 | + var colours = scope.colours || angular.copy(Chart.defaults.global.colours); |
| 184 | + while (colours.length < scope.data.length) { |
| 185 | + colours.push(scope.getColour()); |
| 186 | + } |
| 187 | + return colours; |
185 | 188 | } |
186 | 189 |
|
187 | | - function updateChart (chart, values, scope) { |
188 | | - if (Array.isArray(scope.data[0])) { |
189 | | - chart.datasets.forEach(function (dataset, i) { |
190 | | - (dataset.points || dataset.bars).forEach(function (dataItem, j) { |
191 | | - dataItem.value = values[i][j]; |
192 | | - }); |
193 | | - }); |
194 | | - } else { |
195 | | - chart.segments.forEach(function (segment, i) { |
196 | | - segment.value = values[i]; |
197 | | - }); |
198 | | - } |
199 | | - chart.update(); |
200 | | - scope.$emit('update', chart); |
| 190 | + function getRandomColour () { |
| 191 | + var c = [getRandomInt(0, 255), getRandomInt(0, 255), getRandomInt(0, 255)]; |
| 192 | + return getColour(c); |
| 193 | + } |
| 194 | + |
| 195 | + function getColour (c) { |
| 196 | + return { |
| 197 | + fillColor: rgba(c, 0.2), |
| 198 | + strokeColor: rgba(c, 1), |
| 199 | + pointColor: rgba(c, 1), |
| 200 | + pointStrokeColor: '#fff', |
| 201 | + pointHighlightFill: '#fff', |
| 202 | + pointHighlightStroke: rgba(c, 0.8) |
| 203 | + }; |
| 204 | + } |
| 205 | + |
| 206 | + function getRandomInt (min, max) { |
| 207 | + return Math.floor(Math.random() * (max - min + 1)) + min; |
| 208 | + } |
| 209 | + |
| 210 | + function rgba(c, a) { |
| 211 | + return 'rgba(' + c.concat(a).join(',') + ')'; |
201 | 212 | } |
202 | 213 |
|
203 | 214 | function getDataSets (labels, data, series, colours) { |
204 | | - colours = colours || Chart.defaults.global.colours; |
205 | 215 | return { |
206 | 216 | labels: labels, |
207 | 217 | datasets: data.map(function (item, i) { |
|
214 | 224 | } |
215 | 225 |
|
216 | 226 | function getData (labels, data, colours) { |
217 | | - colours = colours || Chart.defaults.global.colours; |
218 | 227 | return labels.map(function (label, i) { |
219 | 228 | return { |
220 | 229 | label: label, |
|
225 | 234 | }); |
226 | 235 | } |
227 | 236 |
|
| 237 | + function setLegend (elem, chart) { |
| 238 | + var $parent = elem.parent(), |
| 239 | + $oldLegend = $parent.find('chart-legend'), |
| 240 | + legend = '<chart-legend>' + chart.generateLegend() + '</chart-legend>'; |
| 241 | + if ($oldLegend.length) $oldLegend.replaceWith(legend); |
| 242 | + else $parent.append(legend); |
| 243 | + } |
| 244 | + |
| 245 | + function updateChart (chart, values, scope) { |
| 246 | + if (Array.isArray(scope.data[0])) { |
| 247 | + chart.datasets.forEach(function (dataset, i) { |
| 248 | + (dataset.points || dataset.bars).forEach(function (dataItem, j) { |
| 249 | + dataItem.value = values[i][j]; |
| 250 | + }); |
| 251 | + }); |
| 252 | + } else { |
| 253 | + chart.segments.forEach(function (segment, i) { |
| 254 | + segment.value = values[i]; |
| 255 | + }); |
| 256 | + } |
| 257 | + chart.update(); |
| 258 | + scope.$emit('update', chart); |
| 259 | + } |
| 260 | + |
228 | 261 | function isEmpty (value) { |
229 | 262 | return ! value || |
230 | 263 | (Array.isArray(value) && ! value.length) || |
|
0 commit comments