2012-07-11 198 views

回答

2

没有改变每个栏的颜色的官方文档,但有一个简单的技巧来实现这一点。您可以使用isStacked:true并重叠两个小节。如果您只需要其他颜色,请使用第二栏。

Final Output

代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 

    <title> 
     Google Visualization API Sample 
    </title> 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
     google.load('visualization', '1', {packages: ['corechart']}); 
    </script> 
    <script type="text/javascript"> 
     function drawVisualization() { 
     // Create and populate the data table. 


     var data = new google.visualization.DataTable(); 
     // Add columns 
     data.addColumn('string', 'Category'); 
     data.addColumn('number', 'value'); 
     data.addColumn('number','value');//Used for red bar 

     data.addRows(5); 
     data.setCell(0, 0, 'Category 1'); 
     data.setCell(0, 1, 4.25); 
     data.setCell(1, 0, 'Category 2'); 
     data.setCell(1, 1, 2.5); 
     data.setCell(2, 0, 'Category 3'); 
     data.setCell(2, 1, 3.5); 
     data.setCell(3, 0, 'Category 4'); 

     data.setCell(3, 2, 4.5);//Used because we need red or other color bar 
     data.setCell(4, 0, 'Category 5'); 
     data.setCell(4, 1, 3.75); 



     // Create and draw the visualization. 
     new google.visualization.ColumnChart(document.getElementById('visualization')). 
      draw(data,{isStacked: true}); 
     } 


     google.setOnLoadCallback(drawVisualization); 
    </script> 
    </head> 
    <body style="font-family: Arial;border: 0 none;"> 
    <div id="visualization" style="width: 600px; height: 400px;"></div> 
    </body> 
</html> 

您可以检查一个快速演示在这里http://jsfiddle.net/sXmzq/

+0

不错的做法!谢谢! – 2012-07-14 13:19:40

+0

不客气:)这是否解决了你的问题? – iNan 2012-07-14 13:20:18

+0

是的,我希望如此:D – 2012-07-14 13:44:37

0

你必须申报所有的颜色使用您的柱形图。没有理由他们不能像['blue','blue','blue','red','blue']那样不同,或者你有什么不同。

它在“颜色”here下进行了说明。

+0

谢谢您的答复,但它并不能帮助。我在图表中使用了一个系列,我需要强调其中的一点。 'colors'选项用于指定每个系列的颜色,而不是每个点。 – 2012-07-13 05:32:03