Default Colors
Simple question - I have data from backend, why the hell on earth I must specify the colors for the charts?
Can there be some default colors if you don't provide color & highlight color ?
I subscribe to this issue. Charts should be able to pick random colors by themselves, unless the user specifies his own colors. Currently we have to specify colors for each set of data, which is very hard when a variable number of data sets is displayed. I would really love & need this feature. By the way, thanks for the great work.
If you use angular, you should try angular-chart.js, it does exactly that, among other things. On 12/12/2014 7:04 PM, "Mircea" [email protected] wrote:
I subscribe to this issue. Charts should be able to pick random colors by themselves, unless the user specifies his own colors. Currently we have to specify colors for each set of data, which is very hard when a variable number of data sets is displayed. I would really love & need this feature. By the way, thanks for the great work.
— Reply to this email directly or view it on GitHub https://github.com/nnnick/Chart.js/issues/815#issuecomment-66742709.
Beside that I consider it bad style to use "why the hell on earth" when asking a humble question, you could have a look at d3 to get some color arrrays to choose from: https://github.com/mbostock/d3/wiki/Ordinal-Scales#categorical-colors
@saintedlama yeah, although the chart.js looks cool, I had to move to another library. The author does not respond to users, 300+ issues, 99+ pr, this project is pretty much dead. But, it still looks nice.
@VladShcherbin #795 has a link to a more active version. I'm not sure how compatible it is.
Sorry for the slow reply.
This project has new life breathing in. I am prioritizing this issue.
maybe this helps, i've written like this:
fix core Chart.Type function:
Chart.Type = function(data,options,chart){
this.options = options;
this.chart = chart;
this.id = uid();
//Add the chart instance to the global namespace
Chart.instances[this.id] = this;
//fix colors
if (Chart.defaults.global.colours) {
helpers.each(data.datasets, function (dataset, index) {
if ((dataset.fillColor === undefined) && (dataset.strokeColor === undefined)) {
helpers.extend(dataset, Chart.defaults.global.colours[index % Chart.defaults.global.colours.length]);
}
});
}
// Initialize is always called when a chart type is created
// By default it is a no op, but it should be extended
if (options.responsive){
this.resize();
}
this.initialize.call(this,data);
};
and define colors based on angular
Chart.defaults.global.colours = [
{ // blue
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,0.8)"
},
{ // light grey
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,0.8)"
},
{ // red
fillColor: "rgba(247,70,74,0.2)",
strokeColor: "rgba(247,70,74,1)",
pointColor: "rgba(247,70,74,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(247,70,74,0.8)"
},
{ // green
fillColor: "rgba(70,191,189,0.2)",
strokeColor: "rgba(70,191,189,1)",
pointColor: "rgba(70,191,189,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(70,191,189,0.8)"
},
{ // yellow
fillColor: "rgba(253,180,92,0.2)",
strokeColor: "rgba(253,180,92,1)",
pointColor: "rgba(253,180,92,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(253,180,92,0.8)"
},
{ // grey
fillColor: "rgba(148,159,177,0.2)",
strokeColor: "rgba(148,159,177,1)",
pointColor: "rgba(148,159,177,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(148,159,177,0.8)"
},
{ // dark grey
fillColor: "rgba(77,83,96,0.2)",
strokeColor: "rgba(77,83,96,1)",
pointColor: "rgba(77,83,96,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(77,83,96,1)"
}
];
Please take a look at https://github.com/nnnick/Chart.js/pull/711 I prefer calculated colors instead of a long list of predefined colors.
As per #711 I'm going to close this for now, as it would seem #711 is going to be merged soon
I don't believe merging #711 has fixed this; doesn't it only generate colors for a doughnut chart? I'd love not to manually specify colors for line chart as well (and probably others, too).
I second @kzemek comment - #711 seem to focus only on pie charts. It would be good if colours were defined as in a way that one didn't have to merge the data with presentation.
Ideally one have a style set for a given project - such that charts get a consistent styling from a single central location.
@dany28 's comment sounded interesting. Perhaps it can be considered for a PR?
Hey folks, was there ever resolution on how to properly do this?
+1
My solution is doing looping in backgroundColor's parameter with the value = randomColor(). This function from https://github.com/davidmerfield/randomColor, so you need to include the randomColor js
@mdd1704 would be cool to be able to get both background and hoverBackground color using that lib... https://github.com/davidmerfield/randomColor/issues/73
I'm not sure why this issue was closed as I ran into this problem today. I found the top 20 default google charts colors pretty helpful.
var default_colors = ['#3366CC','#DC3912','#FF9900','#109618','#990099','#3B3EAC','#0099C6','#DD4477','#66AA00','#B82E2E','#316395','#994499','#22AA99','#AAAA11','#6633CC','#E67300','#8B0707','#329262','#5574A6','#3B3EAC']
I've been manually mapping color data on to raw data records:
const colors = {
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1,
};
const data = {
...serverData,
datasets: mapValues(serverData.datasets, dataset => ({ ...dataset, ...colors }))
};
Where "serverData" looks like:
"data": {
"labels": ["Cows", "Sheep", "Pigs"],
"datasets": [
{
"data": [55, 40, 30]
}
]
}
Then pass "data" in to the chart. It's not perfect, but at least my input data doesn't need to be filled with color/display data.
@cjke can you please better show in your code how const data is exactly defined? E.g. what does mapValues() do and so on.
Thanks!
Thats pretty much it. The whole data object is passed into the Charts data attribute. I'm using React, so might look a tad different, but something like:
<Bar data={data} options={options} />
MapValues is simply doing a big join, basically grabbing a bit of color to mix with the data.
If it's not working for you, maybe pose it as a question on stackoverflow (and dump the link here)
It is easy:
var config = {
type: 'pie',
data: {
datasets:[{
data: ##,
backgroundColor: poolColors(total),
label: ##
}],
labels: data.labels
},
options: {
responsive: true
}
};
var poolColors = function (a) {
var pool = [];
for(i=0;i<a;i++){
pool.push(dynamicColors());
}
return pool;
}
var dynamicColors = function() {
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
return "rgb(" + r + "," + g + "," + b + ")";
}