Skip to content

Commit 0924230

Browse files
committed
Fix #54 - Add random colours if not enough for the data or allow passing a function that will return a colour
1 parent 2e483ca commit 0924230

File tree

6 files changed

+127
-23
lines changed

6 files changed

+127
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ They all use mostly the same API:
2828
- `options`: chart options (as from [Chart.js documentation](http://www.chartjs.org/docs/))
2929
- `series`: (default: `[]`): series labels (line, bar, radar)
3030
- `colours`: data colours (will use default colours if not specified)
31+
- `getColour`: function that returns a colour in case there are not enough (will use random colours if not specified)
3132
- `click`: onclick event handler (line, radar)
3233
- `legend`: (default: `false`): show legend below the chart
3334

angular-chart.js

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080
labels: '=',
8181
options: '=',
8282
series: '=',
83-
colours: '=',
83+
colours: '=?',
84+
getColour: '=?',
8485
chartType: '=',
8586
legend: '@',
8687
click: '='
@@ -155,6 +156,8 @@
155156

156157
function createChart (type, scope, elem) {
157158
if (! scope.data || ! scope.data.length) return;
159+
scope.getColour = typeof scope.getColour === 'function' ? scope.getColour : getRandomColour;
160+
scope.colours = getColours(scope);
158161
var cvs = elem[0], ctx = cvs.getContext('2d');
159162
var data = Array.isArray(scope.data[0]) ?
160163
getDataSets(scope.labels, scope.data, scope.series || [], scope.colours) :
@@ -176,32 +179,39 @@
176179
return chart;
177180
}
178181

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;
185188
}
186189

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(',') + ')';
201212
}
202213

203214
function getDataSets (labels, data, series, colours) {
204-
colours = colours || Chart.defaults.global.colours;
205215
return {
206216
labels: labels,
207217
datasets: data.map(function (item, i) {
@@ -214,7 +224,6 @@
214224
}
215225

216226
function getData (labels, data, colours) {
217-
colours = colours || Chart.defaults.global.colours;
218227
return labels.map(function (label, i) {
219228
return {
220229
label: label,
@@ -225,6 +234,30 @@
225234
});
226235
}
227236

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+
228261
function isEmpty (value) {
229262
return ! value ||
230263
(Array.isArray(value) && ! value.length) ||
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>Not enough colours</title>
6+
<link rel="stylesheet" href="../../dist/angular-chart.css">
7+
<link href="../../examples/bootstrap.css" rel="stylesheet">
8+
</head>
9+
<body ng-app="pie" id="top">
10+
<div class="container">
11+
<section id="charts">
12+
<div class="page-header">
13+
<h1>Charts</h1>
14+
</div>
15+
<div class="row">
16+
<div class="col-lg-4 col-sm-12" ng-controller="PieCtrl">
17+
<div class="panel panel-default">
18+
<div class="panel-heading">Pie Chart</div>
19+
<div class="panel-body">
20+
<canvas class="chart chart-pie" data="data" labels="labels"
21+
colours="colours" get-colour="getColour"></canvas>
22+
</div>
23+
<p align="center"><a href="https://github.com/jtblin/angular-chart.js/issues/51">
24+
https://github.com/jtblin/angular-chart.js/issues/54
25+
</a></p>
26+
</div>
27+
</div>
28+
</div>
29+
</section>
30+
</div>
31+
32+
<script src="../../bower_components/angular/angular.min.js"></script>
33+
<script src="../../bower_components/Chart.js/Chart.js"></script>
34+
<script src="../../angular-chart.js"></script>
35+
<script src="54-not-enough-colours.js"></script>
36+
</body>
37+
</html>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(function () {
2+
'use strict';
3+
4+
var app = angular.module('pie', ['chart.js']);
5+
app.controller('PieCtrl', ['$scope', function ($scope) {
6+
var cnt = 0;
7+
$scope.colours = [];
8+
$scope.labels = ['Series A', 'Series B'];
9+
$scope.getColour = function () {
10+
return ++cnt % 2 > 0 ?
11+
{ // red
12+
fillColor: 'rgba(247,70,74,0.2)',
13+
strokeColor: 'rgba(247,70,74,1)',
14+
pointColor: 'rgba(247,70,74,1)',
15+
pointStrokeColor: '#fff',
16+
pointHighlightFill: '#fff',
17+
pointHighlightStroke: 'rgba(247,70,74,0.8)'
18+
}
19+
:
20+
{ // green
21+
fillColor: 'rgba(70,191,189,0.2)',
22+
strokeColor: 'rgba(70,191,189,1)',
23+
pointColor: 'rgba(70,191,189,1)',
24+
pointStrokeColor: '#fff',
25+
pointHighlightFill: '#fff',
26+
pointHighlightStroke: 'rgba(70,191,189,0.8)'
27+
};
28+
};
29+
$scope.data = [49, 65];
30+
}]);
31+
32+
})();
35 KB
Loading

test/test.integration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('integration', function () {
3030
mkdirp(WEBSHOT_FAILED_DIR);
3131

3232
[
33+
'54-not-enough-colours',
3334
'51-pie-update-colours',
3435
'charts'
3536
].forEach(function (name) {

0 commit comments

Comments
 (0)