问题
I have Google visualization where I want to set the specific colors of each pie slice/bar based on a column in the datatable.
i.e. I have a table of the number of people who voted for their favorite color:
*Input table*
id Color Num Votes
1 'Red' 10
2 'Blue' 100
3 'Green' 50
How do I set the colors on the pie chart so that the slice for 'Red' is colored red, etc? However, due to the data set, I cannot guarantee that the rows will be in a certain order so cannot create an array of colors.
I think I need to use a formatter, but cannot get it to work.
var formatter = new google.visualization.ColorFormat();
formatter.addRange('Blue', 'Blue', 'blue', 'blue');
formatter.format(data, 1);
Thanks in advance
回答1:
Easy,
Just set it on var options
it should be like this:
var options = {'title':'Your Title',
'width':XXX,
'height':XXX,
colors:['Blue', 'Blue', 'blue', 'blue']};
回答2:
Set the options via the second parameter of the draw() method.
var chartOptions = {
title: 'My Awesome Chart',
colors: ['#657CD0','#DA68A0','#06C3C0','#777B80','#7C6D70','#7C0850','#F75870']
};
var pieChart = new google.visualization.PieChart(document.getElementById('pieChart'));
pieChart.draw(someData, chartOptions);
Check out this color picker for Google Visualization: http://www.philipwhitt.com/projects/google-visualization-theme-picker, it might give you some useful color ideas.
来源:https://stackoverflow.com/questions/10959984/setting-specific-colors-in-google-charts