2017-09-02 42 views
2

如何设置Google图表中特定栏的特定颜色?如何在Google图表中为特定栏设置特定颜色?

我试图应用这种着色风格,但似乎没有工作!

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
<div id="barchart_material" style="width: 'auto'; height: 500px;"></div> 

的JavaScript

google.charts.load('current', {'packages':['bar']}); 
google.charts.setOnLoadCallback(drawChart); 

function drawChart() { 
    var data = google.visualization.arrayToDataTable([ 
     ['Statistics', 'Amount', { role: 'style' }], 
     ['Categories', 5, '#D9534F'], 
     ['Posts', 4, '#337AB7'], 
     ['Comments', 8, '#5CB85C'], 
     ['Users', 3, '#F0AD4E'], 
     ]); 

    var options = { 
     chart: { 
     title: 'Analysis', 
     subtitle: '', 
     }, 
    /* colors: ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'], // Another coloring method*/ 
     bars: 'horizontal' // Required for Material Bar Charts. 
    }; 

    var chart = new google.charts.Bar(document.getElementById('barchart_material')); 

    chart.draw(data, google.charts.Bar.convertOptions(options)); 
} 

我认为这个列表包含色彩特征,而不是目前的支持......如果你认为这是不正确的请回答! https://github.com/google/google-visualization-issues/issues/2143 至于“经典”图,那么由@عارفبنالأزرق提供的解决方案肯定是在这些图上工作。然而,我从来没有对它们感兴趣!

+1

列的角色不由_material_图支持,检查[这个答案](https://stackoverflow.com/a/36452554/5090771)的选项... – WhiteHat

+0

是的...谢谢。 – Sky7ure

回答

0

fiddle provided by googlË
我改变google.charts.load('current', {'packages':['corechart','bar']});

var chart = new google.visualization.BarChart(document.getElementById('barchart_material')); chart.draw(data, options); ,它的工作 这里你的代码的working fiddle

+0

嗯...就我而言,你已经使用了不同的条形图风格!代码正在运行,但没有我宁愿拥有的外观。有没有其他方法为该条形图提供颜色? – Sky7ure

+1

好吧,你需要matrial图表,那么你不能在matrial图表中添加角色样式解决方案是使用corechart与主题matrial,这里的解决方案https://stackoverflow.com/questions/40473417/google-chart-material-type-bar- data-in-arraytodatatable-with-role-style-ko –

+0

Roger那。谢谢。 – Sky7ure

相关问题