【发布时间】:2015-08-04 21:09:00
【问题描述】:
我用谷歌图表创建了一个水平条形图,我想改变属于某个范围的条形的颜色,我尝试了很多方法 但徒劳无功,这是我的代码:
google.load("visualization", "1.1", {packages:["bar"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.arrayToDataTable([
['Agents', 'Percentage' ],
<?php
for($i=0;$i<sizeof($name);$i++){
echo '[\''.$name[$i].'\','.$count[$i].'],';
}
?>
]);
var options = {
title: 'EKMS Usage per agent',
width: 900,
legend: { position: 'none' },
chart: { title: 'EKMS Usage per agent',
subtitle: 'By percentage' },
bars: 'horizontal', // Required for Material Bar Charts.
axes: {
x: {
0: { side: 'top', label: 'Percentage'} // Top x-axis.
}
},
bar: { groupWidth: "90%" }
};
var chart = new google.charts.Bar(document.getElementById('top_x_div'));
chart.draw(data, options);
};
$name 和 $count 是我从数据库中检索到的两个数组,它们的长度相同,我尝试在变量 data 中添加一个列 { role: "style" } 并 使用这样的测试分配颜色:
var data = new google.visualization.arrayToDataTable([
['Agents', 'Percentage', { role: "style" } ],
<?php
for($i=0;$i<sizeof($name);$i++){
if($count[$i]<50)
echo '[\''.$name[$i].'\','.$count[$i].',\'red\'],';
else
echo '[\''.$name[$i].'\','.$count[$i].',\'blue\'],';
}
?>
]);
我还尝试添加颜色:['blue','red'] 它会改变所有条形的颜色。
P.S:请注意,我使用的图表是:Top X Chart 这是它的链接: https://google-developers.appspot.com/chart/interactive/docs/gallery/barchart#top-x-charts
【问题讨论】:
标签: javascript html css charts google-visualization