我正在使用Google Charts并尝试生成自己的
JSON格式来渲染图表而不是使用库.一切都很好,除了试图弄清楚如何在json中表示日期格式,谷歌图表将理解…
规格:
JSON不支持JavaScript Date值(例如,“new Date(2008,1,28,0,31,26)”; API实现.但是,API现在支持日期的自定义有效JSON表示形式字符串格式如下:日期(年,月,日[,小时,分钟,秒[,毫秒]]),其中一天中的所有内容都是可选的,而月份是从零开始的.
参考:
https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#jsondatatable
阅读上述规范似乎表明将日期格式表示为json为日期(年,日,月)可行,但这似乎对我不起作用.
错误:
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"}}}}
没有什么是跳出来的,因为这应该遵循规范要求的格式.我错过了什么?
最佳答案 你的语法错了,我想……
你应该尝试这样的事情:
{"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 ….