Google ColumnChart的不同颜色

时间:2015-09-07 11:31:40

标签: javascript php google-visualization

我正在尝试使用来自php代码的数据绘制一个图表作为传递给draw函数的数组。颜色显示我是否使用PieChart,但不是ColumnChart。如何让酒吧显示不同的颜色?

这是我的剧本:

<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>

//作为数组从php代码传来的示例数据如下: 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]]);

//只有一种颜色的当前图形如下: single color graph

//我需要图形看起来像下面我使用jqbargraph的图形,但是不能使用jq,因为它导致了我的长标签的布局问题: multi color graph

1 个答案:

答案 0 :(得分:1)

您通过colors - 选项定义的颜色适用于列。

如果要为行设置颜色,则必须通过样式列进行设置,例如:

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/