Google line charts how to NOT interpolate missing

2019-04-10 22:04发布

问题:

I have a database table with a series of events, these events have dates for when they happen. Now I would like to display the number of events for each month and show them in a line chart. I get the data via php and generate the js-code in a loop using data.addrow

Now this works fine, but if there is a month with no events it will not be added as a row and the line will simply interpolate the missing month. So for example january 5 events, february 0 and march 5 events, would create a horizontal line going from january to march making it look like there where 5 events in february.

How can i make the line go down to zero for a "empty" month?

Do I need to fill in empty rows/months with null? This would not seem very practical.

回答1:

If you want the line to go down to zero, you are interpolating, just in a different way. Google gives you two ways to deal with empty values (nulls) using the interpolateNulls option:

  1. True will guess what value belongs in between (the current behavior in your chart)
  2. False will leave a break in the line for empty data points (not what you want)

To get all null values to show as zero, you would need to go through your data with a for loop and change any null value to 0. Something like the below code.

for (i = 0; i < data.getNumberOfRows(); i++) {
  if ( data.getValue(1, i) == null )
    data.setValue(1, i) = 0
}

The above code isn't tested, and I don't know how your data is set up so I didn't spend much time testing it (I have no idea how many columns, etc. your data has, you can adjust and test as needed).