How do I read and modify axes on an already-drawn Google chart?

后端 未结 2 744
傲寒
傲寒 2021-01-16 11:28

I have a page with several google charts on it, mostly combo charts and line charts. For instance:

chart = new google.visualization.LineChart(chartDiv);
         


        
2条回答
  •  不知归路
    2021-01-16 12:10

    According to the documentation getVAxisValue function has the following signature:

    getVAxisValue(position, optional_axis_index)

    Returns the logical vertical value at position, which is an offset from the chart container's top edge. Can be negative.

    Example: chart.getChartLayoutInterface().getVAxisValue(300).

    Call this after the chart is drawn.

    Return Type: number


    Since your goal is to read the maximum values off of the axes and then redraw the charts so that they all have the same maximum values on their axes, i would suggest the following solution

    calculate min/max values of google.visualization.DataTable:

    function calcMinMax(data, colIndex)
    {
       var totalMin  = data.getValue(0,colIndex);
       var totalMax = data.getValue(0,colIndex);
       for(var i = 0; i < data.getNumberOfRows();i++){
            if ( data.getValue(i, colIndex) < totalMin  ) {
              totalMin  = data.getValue(i, colIndex);
            }          
            if ( data.getValue(i, colIndex) > totalMax ) {
              totalMax = data.getValue(i, colIndex);
            }
       }
       return {'min': totalMin, 'max': totalMax };
    } 
    

    and set viewWindow option:

    var vAxisMinMax = calcMinMax(data,1); 
    options.vAxis = {'viewWindow': {'min': vAxisMinMax.min, 'max': vAxisMinMax.max}};
    

    Example

    google.load('visualization', '1', { packages: ['corechart', 'line'] });
    google.setOnLoadCallback(drawBasic);
    
    function drawBasic() {
    
        var data = new google.visualization.DataTable();
        data.addColumn('number', 'X');
        data.addColumn('number', 'Dogs');
    
        data.addRows([
          [0, 5], [1, 10], [2, 23], [3, 17], [4, 18], [5, 9],
          [6, 11], [7, 27], [8, 33], [9, 40], [10, 32], [11, 35],
          [12, 30], [13, 40], [14, 42], [15, 47], [16, 44], [17, 48],
          [18, 52], [19, 54], [20, 42], [21, 55], [22, 56], [23, 57],
          [24, 60], [25, 50], [26, 52], [27, 51], [28, 49], [29, 53],
          [30, 55], [31, 60], [32, 61], [33, 59], [34, 62], [35, 65],
          [36, 62], [37, 58], [38, 55], [39, 61], [40, 64], [41, 65],
          [42, 63], [43, 66], [44, 67], [45, 69], [46, 69], [47, 70],
          [48, 72], [49, 68], [50, 66], [51, 65], [52, 67], [53, 70],
          [54, 71], [55, 72], [56, 73], [57, 75], [58, 70], [59, 68],
          [60, 64], [61, 60], [62, 65], [63, 67], [64, 68], [65, 69],
          [66, 70], [67, 72], [68, 75], [69, 80]
        ]);
    
        var options = {
            hAxis: {
                title: 'Time'
            },
            vAxis: {
                title: 'Popularity'
            }
        };
    
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    
    
        var vAxisMinMax = calcMinMax(data,1); 
        options.vAxis = {'viewWindow': {'min': vAxisMinMax.min, 'max': vAxisMinMax.max}};
       
    
        chart.draw(data, options);
    
        
    }
    
    
    
    
    
    function calcMinMax(data, colIndex)
    {
       var totalMin  = data.getValue(0,colIndex);
       var totalMax = data.getValue(0,colIndex);
       for(var i = 0; i < data.getNumberOfRows();i++){
            if ( data.getValue(i, colIndex) < totalMin  ) {
              totalMin  = data.getValue(i, colIndex);
            }          
            if ( data.getValue(i, colIndex) > totalMax ) {
              totalMax = data.getValue(i, colIndex);
            }
       }
       return {'min': totalMin, 'max': totalMax };
    }
     
    

提交回复
热议问题