[英]Google pie chart color depending on value
我有一個圖表,我只計算兩個類別的值的數量。
我想要的是為具有最大值的類別着色為綠色,為具有最小值的類別着色為藍色。
從這兩個數量值中,我想在循環內確定具有較高值的值。 因此,餅圖中的該值將顯示綠色,在這種情況下,可以在選項部分內的slices
處選擇 0 或 1。
到目前為止,這是我的代碼:
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(interinstituctionals_quantity);
function interinstituctionals_quantity(){/*Quantity de proyectos interinstitucionales del total de los proyectos vigentes*/
var data = new google.visualization.DataTable();
var interinstituctionals_quantity = {{interinstituctionals_quantity|json_encode|raw}};
var projectstotal = 0;
/****This is the array I get from the database:
['¿Interinstitutional?', 'Quantity'],
['yes', 70],
['no', 166],
****/
data.addColumn('string', '¿Interinstitucional?');
data.addColumn('number', 'Quantity');
$.each(interinstituctionals_quantity, function(index, value){
projectstotal += value.Quantity;/*Adding total of the quantity gotten from query*/
/*HOW can I determine inside this loop which of the two values has the highest value? And what if they're the same?*/
data.addRow([value.Interinstitucional,value.Quantity]);
});
var optionMax=1;
var optionMin=0;
var options = {
title: 'Number of interinstitutional projects (total: '+projectstotal+')',
backgroundColor: { fill:'transparent'},
height: 500,
is3D: true,
slices: {
0: { color: 'blue',offset: 0.5 },/*THIS TURNED OUT TO BE MAX AND SHOULD HAVE THESE OPTIONS*/
1: { color: 'green' }/*THIS TURNED OUT TO BE MIN AND SHOULD HAVE THESE OPTIONS*/
}
//width: 900,
};
var chart = new google.visualization.PieChart(document.getElementById('interinstituctionals_quantity_div'));
chart.draw(data, options);
function resizeHandler () {
chart.draw(data, options);
}
if (window.addEventListener) {
window.addEventListener('resize', resizeHandler, false);
}
else if (window.attachEvent) {
window.attachEvent('onresize', resizeHandler);
}
}
</script>
我怎樣才能在這個循環內確定兩個值中哪個值最高? 如果它們相同呢? 我如何做到這一點?
這樣的事情應該工作......
// assumes values will always exist and be positive
var maxAmount = -1;
var maxIndex = -1;
var areEqual = false;
$.each(interinstituctionals_quantity, function(index, value){
projectstotal += value.Quantity;/*Adding total of the quantity gotten from query*/
/*HOW can I determine inside this loop which of the two values has the highest value? And what if they're the same?*/
if (value.Quantity > maxAmount) {
maxAmount = value.Quantity;
maxIndex = index;
} else if (value.Quantity === maxAmount) {
areEqual = true;
}
data.addRow([value.Interinstitucional,value.Quantity]);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.