2015-10-19 76 views
0

後我使用這裏顯示ASGALLANT'S隱藏/顯示方法:谷歌圖表工具提示標準更新datachange

http://jsfiddle.net/asgallant/6gz2Q/

除了我的第四數據系列稱爲平均值。當一個系列的顯示或隱藏,我通過調整上述小提琴重新計算平均...:

function showHideSeries() { 
    var sel = chart.getChart().getSelection(); 
    var view = chart.getView() || {}; 

    // if selection length is 0, we deselected an element 
    if (sel.length > 0) { 
     // if row is undefined, we clicked on the legend 
     if (sel[0].row == null) { 
      var col = sel[0].column; 
      if (typeof(columns[col]) == 'number') { 
       var src = columns[col]; 

       // hide the data series 
       columns[col] = { 
        label: datatable.getColumnLabel(src), 
        type: datatable.getColumnType(src), 
        sourceColumn: src, 
        calc: function() { 
         return null; 
        } 
       }; 

       //record as hidden 
       hiddenSeries[col] = true; 

       // grey out the legend entry 
       series[columnsMap[src]].color = '#CCCCCC'; 
       chart.setOption('series', series); 

       //If Exists a columnLabel called "Average", check if last column (average column is currently shown), recalculate average 
       if ((datatable.getColumnLabel(datatable.getNumberOfColumns() - 1) == 'Average') && (hiddenSeries[datatable.getNumberOfColumns() - 1] == false)){      
        for(var r = 0; r < datatable.getNumberOfRows(); ++r) { 
         sum = 0; 
         k = 0 
         for(var c = 1; c < datatable.getNumberOfColumns()-1; ++c) { 
          if(hiddenSeries[c] == false){ 
           if(datatable.getValue(r, c) > 0){ 
            sum = sum + datatable.getValue(r, c); 
            k = k + 1; 
           } 
          } 
         } 
         if (k == 0) k = 1; 
         datatable.setValue(r, datatable.getNumberOfColumns() - 1, sum/k); 
        } 
       } 


       var tmpColumn = new Array(); 
       // Add each data value to the array with push() 
       for(var i = 0; i < datatable.getNumberOfRows(); ++i) { 
        tmpColumn.push(datatable.getValue(i, col)); 
       } 


      } 
      else { 
       var src = columns[col].sourceColumn; 

       // show the data series 
       columns[col] = src; 
       series[columnsMap[src]].color = null; 

       //record as shown 
       hiddenSeries[col] = false; 


       //If Exists a columnLabel called "Average", check if last column (average column is currently shown), recalculate average 
       if ((datatable.getColumnLabel(datatable.getNumberOfColumns() - 1) == 'Average') && (hiddenSeries[datatable.getNumberOfColumns() - 1] == false)){      
        for(var r = 0; r < datatable.getNumberOfRows(); ++r) { 
         sum = 0; 
         k = 0; 
         for(var c = 1; c < datatable.getNumberOfColumns() - 1; ++c) { 
          if(hiddenSeries[c] == false){ 
           if(datatable.getValue(r, c) > 0){ 
            sum = sum + datatable.getValue(r, c); 
            k = k + 1; 
           } 
          } 
         } 
         if (k == 0) k = 1; 
         datatable.setValue(r, datatable.getNumberOfColumns() - 1, sum/k); 
        } 
       } 
       //chart.setOption('series.' + i + '.type', 'bars'); 

       if(datatable.getColumnLabel(src) == 'Average' || datatable.getColumnLabel(src) == 'Peak Demand [kVA]'){ 
        chart.getOptions().series[columnsMap[src]].type = 'line'; 
        if(c.second_axis == true){ 
         chart.getOptions().series[columnsMap[src]].targetAxisIndex = 1; 
        } 

       } 

      } 
      chart.setDataTable(datatable); 
      view.columns = columns; 
      chart.setView(view); 


      google.visualization.events.addListener(chart, 'ready', function() { 
       document.getElementById('chartImg').href = chart.getChart().getImageURI(); 
    }); 
      chart.draw(); 

     } 
    } 
} 

它的偉大工程,除了標準提示不被更新圖表的重新抽獎。我想避免使用自定義工具提示作爲解決方案。我認爲工具提示值應該在圖表的每個繪圖中自動更新...如果不是,必須有辦法強制它?

+0

您確定在重新計算平均值後重新繪製圖表嗎? 能否請您提供一些您的問題.. –

+0

亨裏克,老實說..我能夠通過JSFIDDLE ... https://jsfiddle.net/n3fofk29/1/得到它的工作。只需點擊系列3,它將更改系列1的第一個值,並且工具提示得到更新...但是,它不適用於我的應用程序。 – Jglstewart

+0

另外,我一定是。因爲圖表本身正在更新。這只是不更新​​的工具提示。 – Jglstewart

回答

0

這裏的問題是,我用的是格式化的數字,如下圖所示:

var formatter = new google.visualization.NumberFormat(
      {negativeColor: 'red', negativeParens: true, pattern: '###,###'}); 
      formatter.format(datatable, i); 

因此,我不僅要使用datatable.setValue(row, column, value)但我也必須使用datatable.setFormattedValue(row, column, *String* value)