Google 图表 json 日期格式

Jon*_*Jon 5 json google-visualization

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

规范:JSON 不支持 JavaScript 日期值(例如“new Date(2008,1,28,0,31,26)”;API 实现支持。但是,API 现在支持日期的自定义有效 JSON 表示形式作为以下格式的字符串: Date(year,month,day[,hour,min,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 
Run Code Online (Sandbox Code Playgroud)

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"}}}}
Run Code Online (Sandbox Code Playgroud)

没有什么让我感到惊讶,因为这应该遵循规范要求的格式。我缺少什么?

小智 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"}}}}
Run Code Online (Sandbox Code Playgroud)

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

{"c":[{"v":"Date(2012,11)"},{"v":6657}.....
Run Code Online (Sandbox Code Playgroud)

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