2016-10-22 108 views
2

每當Google有主要版本的圖表庫時,都會發生事情中斷。下面的代碼大約3年前開始實施,並於幾年運行良好,並在過去一年中它打破了這樣Google Table Chart Number當使用帶逗號的格式化數字值時,格式化顯示NaN

我填充數據表以及與此代碼格式的數字

  var formatter = new google.visualization.NumberFormat({pattern:'#,###.##'}); 

     var cl = jsonChartData.rows[0].c.length; 
     for (var c=0;c<cl;c++) { 
      if (jsonChartData.cols[c].type == 'number'){ 
       formatter.format(popDataTable, c); 
      } 
     } 

和這種運作良好,並會產生這樣的結果:v: 6155177.2549019605, f: '6,155,177.25'

,並通過文檔這裏Google Table Chart,這裏Generic formatters支持,並在這裏ICU 58.1

我使用以下代碼

  var tabOpt = { 
       width: '100%' 
       ,allowHtml: true 
       ,cssClassNames: { 
        tableRow: 'chartTabRow' 
        ,oddTableRow: 'chartTabRow' 
        ,headerRow: 'chartHeadRow' 
        ,tableCell: 'chartTabRow' 
        ,headerCell: 'chartHeadRow' 
        } 
       }; 
     var wdt = popDataTable; 
     wdt.sort([ 
      {column: colID(wdt, 'sumlev')} 
      ,{column: colID(wdt, 'estyear')} 
      ,{column: colID(wdt, 'estdata')} 
      ,{column: popFactCol, desc: true}]); 
     dView = new google.visualization.DataView(wdt); 
     dView.setRows(dView.getFilteredRows([ 
      {column: colID(dView, 'sumlev'), value: '010'} 
      ,{column: colID(dView, 'estdata'), value: popFact} 
      ,{column: colID(dView, 'estyear'), value: popFactYear}])); 

     var jsonPopParams = { 
      "cols": 
       [ 
       {"id":"","label":"Parameter(N)","pattern":"","type":"string"}, 
       {"id":"","label":"Value","pattern":"","type":"number"} 
       ] 
       , 
       "rows": 
       [ 
       {"c": 
       [ 
       {"v":"N","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Sum","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Min","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Max","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Range","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Mean","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Median","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Variance","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Standard Dev","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Skewness","f":null}, 
       {"v":0,"f":null} 
       ] 
       }, 
       {"c": 
       [ 
       {"v":"Kurtosis","f":null}, 
       {"v":0,"f":null} 
       ] 
       }] 
       , 
       "p":null 
       }; 

     jsonPopParams.rows[0].c[1].f = dView.getFormattedValue(0, colID(dView, 'pn')); 
     jsonPopParams.rows[1].c[1].f = dView.getFormattedValue(0, colID(dView, 'psum')); 
     jsonPopParams.rows[2].c[1].f = dView.getFormattedValue(0, colID(dView, 'pmin')); 
     jsonPopParams.rows[3].c[1].f = dView.getFormattedValue(0, colID(dView, 'pmax')); 
     jsonPopParams.rows[4].c[1].f = dView.getFormattedValue(0, colID(dView, 'prange')); 
     jsonPopParams.rows[5].c[1].f = dView.getFormattedValue(0, colID(dView, 'pmean')); 
     jsonPopParams.rows[6].c[1].f = dView.getFormattedValue(0, colID(dView, 'pmedian')); 
     jsonPopParams.rows[7].c[1].f = dView.getFormattedValue(0, colID(dView, 'pvariance')); 
     jsonPopParams.rows[8].c[1].f = dView.getFormattedValue(0, colID(dView, 'pstndev')); 
     jsonPopParams.rows[9].c[1].f = dView.getFormattedValue(0, colID(dView, 'pskew')); 
     jsonPopParams.rows[10].c[1].f = dView.getFormattedValue(0, colID(dView, 'pkurt')); 

     var tblChart = new google.visualization.Table(document.getElementById(paramDiv)); 
     wdt = new google.visualization.DataTable(jsonPopParams); 
     var tblView = new google.visualization.DataView(wdt); 
     tblChart.draw(tblView, tabOpt); 

設置斷點和步進雖然每個指令填充傳遞到圖表對象的視圖,我可以看到,對於格式的值是越來越填充在JSON對象jsonPopParams。

因此,例如pmean具有'6,155,177.25'

當圖表呈現的格式的值,具有逗號在格式化顯示一個NaN的任何值。

I created a working pared down example in JS Fiddle here:

在那裏我測試三種情形

  • 格式化值沒有逗號和2位小數###.##使用view.getFormattedValue併產生6,155,177.25這當然是工作圍繞

  • 格式化值有逗號和2位小數#,###.##使用view.getFormattedValue併產生NaN它現在正在做的事

  • 格式化的值有逗號和2位小數,但使用view.getValue併產生6,155,177.255默認的低位位置必須是3 ...它的工作原理,但不符合規範。

所以我不知道,如果

1)我在做之前就要用它,現在它錯了谷歌固定的根本原因,現在我的錯誤代碼是不能工作任何更多

2)谷歌破了一些東西,現在我的正確的代碼不再工作了。

希望第二組眼睛有新鮮的視角可以看到。

任何幫助將不勝感激。謝謝

+0

更新後的代碼示例和JSFiddle將指定給具有格式化值的正確元素'.f'。 'jsonPopParams.rows [5] .c [1] .f = dView.getFormattedValue(0,colID(dView,'pmean'));' – Threadid

回答

1

問題似乎是這些行...

jsonPopParams.rows[0].c[1].v = dView.getFormattedValue(0, colID(dView, 'pn')); 
jsonPopParams.rows[1].c[1].v = dView.getFormattedValue(0, colID(dView, 'psum')); 
.... 

屬性(v)被設定與格式化的值(f

所以應該或者是...

jsonPopParams.rows[0].c[1].f = dView.getFormattedValue(0, colID(dView, 'pn')); 

或...

jsonPopParams.rows[0].c[1].v = dView.getValue(0, colID(dView, 'pn')); 

或兩者兼有...

當數據表在這裏創建
jsonPopParams.rows[0].c[1].v = dView.getValue(0, colID(dView, 'pn')); 
jsonPopParams.rows[0].c[1].f = dView.getFormattedValue(0, colID(dView, 'pn')); 

...

var tblChart = new google.visualization.Table(document.getElementById(paramDiv)); 
wdt = new google.visualization.DataTable(jsonPopParams); 
.... 

串正在使用的不是數字,
因爲列類型是數字 - >{"id":"","label":"Value","pattern":"","type":"number"}
NaN返回

+0

是的,就是這樣。這非常合理:數字值爲v(數字),格式化值爲f(字符串)。較小的數字沒有逗號,因此作爲字符串的數字可以轉換爲整數。較新版本的庫必須執行更好的類型安全性。我不記得3年前,但分配給v是工作的答案。謝謝 – Threadid