5

我正在使用 Google Charts 并尝试生成自己的 JSON 格式来呈现图表,而不是使用库。一切都很好,除了试图弄清楚如何在 json 中表示谷歌图表可以理解的日期格式......

规范:JSON 不支持 JavaScript 日期值(例如,“new Date(2008,1,28,0,31,26)”;API 实现支持。但是,API 现在支持自定义的有效 JSON 日期表示作为以下格式的字符串: Date(year, month, day[,hour, minute, second[, millisecond]]) 其中 day 之后的所有内容都是可选的,而月份是从零开始的。

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

阅读上述规范似乎表明将 json 中的日期格式表示为 Date(year, date, month) 会起作用,但这似乎对我不起作用。

错误:

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

json响应:

{"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"}}}}

没有什么能引起我的注意,因为这应该遵循规范所需的格式。我错过了什么?

4

1 回答 1

2

你的语法是错误的,我认为......你应该尝试这样的事情:

{"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"}}}}

对我来说,这段代码有效:

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

但这是更改月份,而不是日期并使用 json....

于 2012-10-17T12:02:10.403 回答