Skip to content

Commit 3891fbb

Browse files
WilliamJohnsonJrjtblin
authored andcommitted
Fix for RGB/RGBA Colors using new notation and documentation (fixes issue jtblin#476) (jtblin#488)
1 parent 5f1308c commit 3891fbb

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,54 @@ via a function through the `getColor` attribute.
178178
Hex colors are converted to Chart.js colors automatically,
179179
including different shades for highlight, fill, stroke, etc.
180180

181+
RGB colors may be input by using a string in the format "rgb(r,g,b)".
182+
183+
## Example - RGB Colors
184+
185+
```
186+
angular.module('app',['chart.js'])
187+
.controller('MainController', function($scope){
188+
$scope.colors = ["rgb(159,204,0)","rgb(250,109,33)","rgb(154,154,154)"];
189+
$scope.labels = ["Green", "Orange", "Grey"];
190+
$scope.data = [300, 500, 100];
191+
});
192+
```
193+
194+
RGBA colors may also be input by using a string in the format "rgba(r,g,b,a)".
195+
They may be used alongside RGB colors and/or Hex colors.
196+
197+
## Example - RGBA Colors
198+
```
199+
angular.module('app',['chart.js'])
200+
.controller('MainController', function($scope){
201+
$scope.colors = ["rgba(159,204,0,0.5)","rgba(250,109,33,0.7)","rgba(154,154,154,0.5)"];
202+
$scope.labels = ["Green", "Orange", "Grey"];
203+
$scope.data = [300, 500, 100];
204+
});
205+
```
206+
207+
Colors may also be input as an object by using the format in the example below.
208+
Colors input as objects, Hex colors, RGB, and RGBA colors may be mixed and matched.
209+
210+
## Example - input color as an object
211+
```
212+
angular.module('app',['chart.js'])
213+
.controller('MainController', function($scope){
214+
$scope.colors = [
215+
{
216+
backgroundColor: "rgba(159,204,0, 0.2)",
217+
pointBackgroundColor: "rgba(159,204,0, 1)",
218+
pointHoverBackgroundColor: "rgba(159,204,0, 0.8)",
219+
borderColor: "rgba(159,204,0, 1)",
220+
pointBorderColor: '#fff',
221+
pointHoverBorderColor: "rgba(159,204,0, 1)"
222+
},"rgba(250,109,33,0.5)","#9a9a9a","rgb(233,177,69)"
223+
];
224+
$scope.labels = ["Green", "Peach", "Grey", "Orange"];
225+
$scope.data = [300, 500, 100, 150];
226+
});
227+
```
228+
181229
## Browser compatibility
182230

183231
For IE8 and older browsers, you will need

angular-chart.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@
171171

172172
scope.chartGetColor = getChartColorFn(scope);
173173
var data = getChartData(type, scope);
174-
175174
// Destroy old chart if it exists to avoid ghost charts issue
176175
// https://github.com/jtblin/angular-chart.js/issues/187
177176
destroyChart(scope);
@@ -229,8 +228,12 @@
229228
}
230229

231230
function convertColor (color) {
232-
if (typeof color === 'object' && color !== null) return color;
231+
// Allows RGB and RGBA colors to be input as a string: e.g.: "rgb(159,204,0)", "rgba(159,204,0, 0.5)"
232+
if (typeof color === 'string' && color[0] === 'r') return getColor(rgbStringToRgb(color));
233+
// Allows hex colors to be input as a string.
233234
if (typeof color === 'string' && color[0] === '#') return getColor(hexToRgb(color.substr(1)));
235+
// Allows colors to be input as an object, bypassing getColor() entirely
236+
if (typeof color === 'object' && color !== null) return color;
234237
return getRandomColor();
235238
}
236239

@@ -240,13 +243,15 @@
240243
}
241244

242245
function getColor (color) {
246+
var alpha = color[3] || 1;
247+
color = color.slice(0, 3);
243248
return {
244249
backgroundColor: rgba(color, 0.2),
245-
pointBackgroundColor: rgba(color, 1),
250+
pointBackgroundColor: rgba(color, alpha),
246251
pointHoverBackgroundColor: rgba(color, 0.8),
247-
borderColor: rgba(color, 1),
252+
borderColor: rgba(color, alpha),
248253
pointBorderColor: '#fff',
249-
pointHoverBorderColor: rgba(color, 1)
254+
pointHoverBorderColor: rgba(color, alpha)
250255
};
251256
}
252257

@@ -269,6 +274,13 @@
269274
return [r, g, b];
270275
}
271276

277+
function rgbStringToRgb (color) {
278+
var match = color.match(/^rgba?\(([\d,.]+)\)$/);
279+
if (! match) throw new Error('Cannot parse rgb value');
280+
color = match[1].split(',');
281+
return color.map(Number);
282+
}
283+
272284
function hasData (scope) {
273285
return scope.chartData && scope.chartData.length;
274286
}
@@ -364,4 +376,4 @@
364376
scope.$emit('chart-destroy', scope.chart);
365377
}
366378
}
367-
}));
379+
}));

test/test.unit.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,34 @@ describe('Unit testing', function () {
9797
});
9898
});
9999

100+
describe('colors', function(){
101+
it('sets the chart colors when Hex colors, RGB colors, RGBA colors, or objects are used', function (){
102+
var datasets;
103+
var markup = '<canvas class="chart chart-pie" chart-data="data" chart-labels="labels" chart-colors="colors"></canvas>';
104+
scope.colors = [
105+
{
106+
backgroundColor: 'rgba(159,204,0,0.2)',
107+
pointBackgroundColor: 'rgba(159,204,0,1)',
108+
pointHoverBackgroundColor: 'rgba(159,204,0,0.8)',
109+
borderColor: 'rgba(159,204,0,1)',
110+
pointBorderColor: '#fff',
111+
pointHoverBorderColor: 'rgba(159,204,0,1)'
112+
},'rgba(250,109,33,0.5)','#9a9a9a','rgb(233,177,69)'
113+
];
114+
scope.labels = ['Green', 'Peach', 'Grey', 'Orange'];
115+
scope.data = [300, 500, 100, 150];
116+
scope.$on('chart-create', function (evt, chart) {
117+
datasets = chart.chart.config.data.datasets;
118+
});
119+
$compile(markup)(scope);
120+
scope.$digest();
121+
expect(datasets[0].backgroundColor[0]).to.equal('rgba(159,204,0,1)');
122+
expect(datasets[0].backgroundColor[1]).to.equal('rgba(250,109,33,0.5)');
123+
expect(datasets[0].backgroundColor[2]).to.equal('rgba(154,154,154,1)');
124+
expect(datasets[0].backgroundColor[3]).to.equal('rgba(233,177,69,1)');
125+
});
126+
});
127+
100128
describe('dataset override', function () {
101129
it('overrides the datasets for complex charts', function () {
102130
var datasets;

0 commit comments

Comments
 (0)