4
votes

I am using Google Charts and trying to generate my own JSON format to render the chart rather than using the libraries. All is well except for trying to figure out how to represent a date format in json that google chart will understand...

Spec: JSON does not support JavaScript Date values (for example, "new Date(2008,1,28,0,31,26)"; the API implementation does. However, the API does now support a custom valid JSON representation of dates as a string in the following format: Date(year, month, day[,hour, minute, second[, millisecond]]) where everything after day is optional, and months are zero-based.

Reference: https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#jsondatatable

Reading the above spec would seem to indicate that having the date format represented in json as Date(year, date, month) would work but this does not appear to work for me.

Error:

Uncaught Error: Type mismatch. Value Date(2012, 10, 3) does not match type date in column index 0 

json response:

{"type":"ComboChart","cols":[["date","Date"],["number","Overall"],["number","Current"],["number","Rating Count"]],"rows":[["Date(2012, 10, 3)",4.0,4.0,69],["Date(2012, 10, 4)",4.0,4.0,69]],"options":{"title":"Rating for FI","chartArea":{"width":"90%","height":"75%"},"hAxis":{"title":"Date"},"legend":"top","curveType":"none","pointSize":8,"seriesType":"bars","series":{"0":{"type":"bars","targetAxisIndex":0},"2":{"type":"line","targetAxisIndex":1}},"vAxes":{"0":{"title":"Rating","minValue":0,"maxValue":5},"1":{"title":"Rating Count"}}}}

Nothing is jumping out at me as this should be following the spec required format. What am I missing?

1

1 Answers

2
votes

your syntax is wrong, i think... you should try something like that:

{"type":"ComboChart","cols":[["date","Date"],["number","Overall"],["number","Current"],["number","Rating Count"]],"rows":[["Date(2012, 10, 3)"]],["Date(2012, 10, 4)"]],"options":{"title":"Rating for FI","chartArea":{"width":"90%","height":"75%"},"hAxis":{"title":"Date"},"legend":"top","curveType":"none","pointSize":8,"seriesType":"bars","series":{"0":{"type":"bars","targetAxisIndex":0},"2":{"type":"line","targetAxisIndex":1}},"vAxes":{"0":{"title":"Rating","minValue":0,"maxValue":5},"1":{"title":"Rating Count"}}}}

for me, this code worked:

{"c":[{"v":"Date(2012,11)"},{"v":6657}.....

but this is to change the month, not the day and using json....