Reputation: 197
I am trying to draw a graph with data coming from php code as an array passed on to the draw function. Colors show if I am using PieChart, but NOT ColumnChart. How can I get the bars to show different colors?
Here is my script:
<script type="text/javascript">
google.load("visualization", "1.1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(<?php
$baseText = '';
$trimmedPercentbf = '';
$js_arraybf = "['Percentage','$baseText'],";
$counter = 0;
$getBasicFunctionality = [SQL Query Here];
while ($basicFunctionality = dbfetch($getBasicFunctionality)):
$baseTextbf = $basicFunctionality["sat_lower_level_text"];
$weightsTotal = $basicFunctionality["total"];
$average = $basicFunctionality["weightAVG"];
$countTotal = $basicFunctionality["total_users"];
$counter += $countTotal;
$percentage = ($average)/5 * 100;
$trimmedPercentbf = number_format($percentage, $decimals=2);
if($counter != $countTotal):
$sep = ",";
else:
$sep = ",";
endif;
$js_arraybf .= "[ '$baseTextbf', $trimmedPercentbf ]".$sep;
endwhile;
$js_array = rtrim($js_arraybf, ', ');
print "[".$js_array."]";
?>);
var chart = new google.visualization.ColumnChart(document.getElementById('community'));
chart.draw(data, {width: 800, height: 800, is3D: true,/*vAxis: {format:'#%'},pieHole: 0.4,*/colors: ['#242424','#437346','#97D95C','#7D252B','#EB9781','#FFD2B5','#4A4147','#3796c8', 'red','yellow','green', 'blue'],axisFontSize:12, title: '2.Leadership Management & Communication'});
}
</script>
// The sample data that comes through from the php code as an array is as follows: var data = google.visualization.arrayToDataTable([['Percentage',''],[ 'label1', 51.43 ],[ 'label2', 60.00 ],[ 'label3', 66.67 ],[ 'label4 ', 60.00 ],[ 'label5', 66.67 ],[ 'label7', 100.00 ],[ 'label8', 100.00 ],[ 'label9', 100.00 ],[ 'label10', 100.00 ],[ 'label11', 100.00 ],[ 'label12', 80.00 ],[ 'label13', 40.00 ]]);
//The current graph that has only one color is as follows:
//I need the graph to look like the one below which I used jqbargraph, but cant use jq since it's causing layout issues with my long labels:
Upvotes: 1
Views: 2376
Reputation: 117364
The colors you have defined via the colors
-option apply to columns.
When you want to set colors for rows as in your case you must do it via a style-column, e.g.:
var data = new google.visualization.arrayToDataTable([
['Percentage', '',{ role: 'style' }],
['label1', 51.43, '#242424'],
['label2', 60.00, '#437346'],
['label3', 66.67, '#97D95C'],
['label4', 60.00, '#7D252B'],
['label5', 66.67, '#EB9781'],
['label7', 100.00,'#FFD2B5'],
['label8', 100.00,'#4A4147'],
['label9', 100.00,'#3796c8'],
['label10', 100.00,'red'],
['label11', 100.00,'yellow'],
['label12', 80.00, 'green'],
['label13', 40.00, 'blue']
]);
https://jsfiddle.net/qdaw7ssx/
Upvotes: 2