【问题标题】:How do I read and modify axes on an already-drawn Google chart?如何读取和修改已绘制的 Google 图表上的轴?
【发布时间】:2013-01-10 05:38:18
【问题描述】:

我有一个页面,上面有几个谷歌图表,主要是组合图表和折线图。例如:

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

页面已经绘制后,我希望能够读取轴上的最大值,然后重新绘制图表,以便它们在轴上都有相同的最大值(即,这样它们更容易一目了然地比较)。我事先不知道这些最大值。

我可以更改选项并重新绘制图表

chart.draw(data, options)

但我不确定如何从轴上提取数据。图表对象有一个方法叫做 .getChartLayoutInterface() 这看起来很有希望,但我不确定如何使用它。例如,这有效:

var b = c.getChartLayoutInterface().getChartAreaBoundingBox();

但是,

var b = c.getChartLayoutInterface().getVAxisValue();

返回null

【问题讨论】:

    标签: javascript google-api google-visualization


    【解决方案1】:

    你必须问@Sjoerd getChartLayoutInterface() 是如何工作的,因为它似乎没有任何在线文档。

    与此同时,您可以做的是获取您正在创建的每个图表的最小值和最大值,然后获取所有组合图表的最大值/最小值的最大值和最小值。

    使用这些最大值/最小值,您可以手动设置每个图表的最小值/最大值(带有一些不错的舍入)。为了很好地进行四舍五入,我建议如下:

    // Take the Max/Min of all data values in all graphs
    var totalMax = 345;
    var totalMin = -123;
    
    // Figure out the largest number (positive or negative)
    var biggestNumber = Math.max(Math.abs(totalMax),Math.abs(totalMin));
    
    // Round to an exponent of 10 appropriate for the biggest number
    var roundingExp = Math.floor(Math.log(biggestNumber) / Math.LN10);
    var roundingDec = Math.pow(10,roundingExp);
    
    // Round your max and min to the nearest exponent of 10
    var newMax = Math.ceil(totalMax/roundingDec)*roundingDec;
    var newMin = Math.floor(totalMin/roundingDec)*roundingDec;
    
    // Determine the range of your values
    var range = newMax - newMin;
    
    // Define the number of gridlines (default 5)
    var gridlines = 5;
    
    // Determine an appropriate gap between gridlines
    var interval = range / (gridlines - 1);
    
    // Round that interval up to the exponent of 10
    var newInterval = Math.ceil(interval/roundingDec)*roundingDec;
    
    // Re-round your max and min to the new interval
    var finalMax = Math.ceil(totalMax/newInterval)*newInterval;
    var finalMin = Math.floor(totalMin/newInterval)*newInterval;
    

    这类似于 Google 确定最大值/最小值的方式(有时,如果您有 -6.2 和 3.1 作为值,它会将 3.1 用于网格线,即使它使用的是奇数)。但是通过创建自己的四舍五入,您可以绕过需要弄清楚谷歌如何四舍五入(这是一个棘手的提议)。

    如果你没有正确理解,请告诉我。

    【讨论】:

    • 这实际上是我最终所做的,但如果知道 .getChartLayoutInterface() 是如何工作的,仍然会很高兴。感谢您的四舍五入提示。
    【解决方案2】:

    According to the documentationgetVAxisValue 函数具有以下签名:

    getVAxisValue(position, optional_axis_index)

    返回位置的逻辑垂直值,它是一个偏移量 从图表容器的顶部边缘。可以为负数。

    例如:chart.getChartLayoutInterface().getVAxisValue(300).

    绘制图表后调用它。

    返回类型:number


    由于您的目标是读取轴上的最大值,然后重新绘制图表,以便它们在轴上都具有相同的最大值,我建议采用以下解决方案

    计算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 };
    } 
    

    并设置viewWindow 选项:

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

    示例

    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 };
    }
    <script type="text/javascript" src="https://www.google.com/jsapi"></script> 
    <div id="chart_div"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多