google chart:fix the Uncaught (in promise) Error:

2019-08-05 06:25发布

问题:

using google chart i plotted a line chart like this.

In the graph i have created a custom tool tip as shown in the jfiddle. In the above graph there is no problems with displaying the chart with tool tip.

Am fetching chart data from db.Based on some chart data am not able to render the chart due to some js erros.

in the below example i changed the chart data and graph is not diplaying due to js erros in line 57 of jfidle code

im facing more script issues inside below method whenever the chart data varies.

  function toolTipVal(dt, row, index1, index2) {           
                 label1 = dt.getColumnLabel(index1).split("\n");
                 label2 = dt.getColumnLabel(index2).split("\n");
                 yValue1 =0; yValue2 =0;           
                  if (dt.getValue(row, index1) === null ) {
                    yValue1 = dt.getFormattedValue(row -1, index1);               
                  } 
                  else {
                    yValue1 = dt.getFormattedValue(row, index1);                  
                  }            
                  if (dt.getValue(row, index2) === null) {
                    if (row > 0) {
                      yValue2 = dt.getFormattedValue(row - 1, index2);
                    }
                  } else {
                    yValue2 = dt.getFormattedValue(row, index2);
                  }              
                  return [yValue1, yValue2, label1, label2];
          }; 

If am using the second data set in jfiddle am getting follwoing js error

 jsapi_compiled_default_module.js:74 Uncaught (in promise) Error: Invalid row index -1. Should be in the range [0-799] in above methodline

yValue1 = dt.getFormattedValue(row -1, index1); due to this graph is not rendering in the UI.

How can i modify the above method based on these chart data's

回答1:

row is a zero indexed value. You are calling

yValue1 = dt.getFormattedValue(row -1, index1);

without testing if the row isn't the first.

I don't understend completely the logic but I guess that you have to add some logic for the tooltip of the first node.

You will get into that exception every time the

dt.getValue(row, index1) === null

happens to be true for row == 0.