2016-03-31 13 views
1

私は2つのカテゴリの値の数だけを数えます。値に応じたGoogleの円グラフの色

私が望むのは、最大値を持つカテゴリでは緑で、最小値を持つカテゴリでは青で色を付けることです。

2つの量の値から、より高い値を持つ値のループ内を判断したいと思います。そのため、円グラフ内のその値に緑の色が表示されます(この場合は、オプション部分内の0または1をslicesに選択することができます)。

は、これまでのところ、これは私のコードです:

<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> 

私は最高の値を持つ2つの値のこのループ内で決定するにはどうすればよいですか?もし彼らが同じであれば? これはどのように実現するのですか?動作するはずです。このような

答えて

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]); 
}); 
+0

ありがとう!それが役に立った – Pathros

関連する問題