编程语言
首页 > 编程语言> > Javascript-Google可视化,您可以根据列的值分配切片的颜色吗?

Javascript-Google可视化,您可以根据列的值分配切片的颜色吗?

作者:互联网

我正在尝试以下操作根据值分配颜色,但这不起作用,它只是分配默认值.此方法适用于其他图表类型.请记住,我不能在选项中使用颜色,因为行的排列顺序可以不同.有任何想法吗?

var data = new google.visualization.DataTable();        
  data.addColumn('string', 'Status');
  data.addColumn('number', 'Count');
  data.addColumn({type: 'string', role: 'style'});
  data.addRow(['Failure', 8,'color:#EE4A49']);
  data.addRow(['Warning', 1,'color:#E3d157']);
  data.addRow(['Success', 1,'color:#45B7AF']); 

http://jsfiddle.net/zYS27/15/

解决方法:

基于on the current API,我认为您可能需要对数据进行预处理,然后再将其传递给Google图表.

var data = new google.visualization.DataTable();        
  data.addColumn('string', 'Status');
  data.addColumn('number', 'Count');
  data.addColumn({type: 'string', role: 'style'});
  data.addRow(['Failure', 8,'color:#EE4A49']);
  data.addRow(['Warning', 1,'color:#E3d157']);
  data.addRow(['Success', 1,'color:#45B7AF']);

var myColors = { 'Failure' : 'red', 'Success': 'green', 'Warning': 'yellow' };

var slicesColor = {};

for( var i=0; i < data.getNumberOfRows(); i++){
  // I assume the label is in the first column of each row here
  // this is based on string matching, but you can define any criteria you want
  slicesColor[i] = {color: myColors[data.getValue(i, 0)] };
}

var options = {
  ...
  slices: slicesColor
}

var chart = new google.visualization.PieChart(document.getElementById('chart'));
  chart.draw(data, option);   
}
...

标签:google-visualization,javascript
来源: https://codeday.me/bug/20191121/2049866.html