Change point colour based on value for Google Scatter Chart

安稳与你 提交于 2019-12-04 09:24:43

Add an extra column to your DataTable, with the role style :

data.addColumn( {'type': 'string', 'role': 'style'} );

Now add styling to each of the rows to get the desired effect :

data.addRows([[1,100, 'point {size: 14; fill-color: green'], 
              [2,150, 'point {size: 14; fill-color: green'], 
              ....
              [8,450, 'point {size: 14; fill-color: red']
             ]);

demo -> http://jsfiddle.net/v92k8rty/


Update. There is one (out of probably hundreds) javascript library that very easily can provide a gradient palette with customizeable colors and range - RainbowVis-JS. Instead of the above, create a palette by using RainbowVis in the same range as the DataTable, and then add the colors dynamically :

//create a gradient palette from green to red using RainbowVis
var rainbow = new Rainbow(); 
rainbow.setNumberRange(1, data.getNumberOfRows());
rainbow.setSpectrum('green', 'red');

//alter the DataTable
data.addColumn( {'type': 'string', 'role': 'style'} );    
for (var i=0;i<data.getNumberOfRows();i++) {
    data.setCell(i, 2, 'point { fill-color:'+rainbow.colorAt(i+1)+'}');
}    

demo -> http://jsfiddle.net/ehgfwh8z/
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!