我有一个图表,我只计算两个类别的值数。
我想要的是为具有最大值的那个类别设置为绿色,对于具有最小值的那个设置为蓝色。
从两个数量值,我想在循环内确定具有更高值的值。因此,饼图内的该值将显示绿色,在这种情况下,可以在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>
我如何在这个循环中确定哪两个值具有最高值?如果他们是一样的呢? 我如何实现这一目标?
答案 0 :(得分:1)
这样的事情应该有用......
// 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]);
});