Wijmo 图表 - 从 PHP 传递日期
我正在使用 Wijmo,这是一个 jQuery 库,其中包含一些图表插件。使用日期时,使用以下格式指定数据:
seriesList: [{
label: "Q1",
legendEntry: true,
data: {
x: [new Date(1978, 0, 1), new Date(1980, 0, 1), new Date(1981, 0, 1), new Date(1982, 0, 1), new Date(1983, 0, 1)],
y: [12, 21, 9, 29, 30]
},
offset: 0
}]
注意 data
对象的 x
轴,所有项目都是 Date
的实例>。
当从 PHP 以 JSON 对象形式向下传递此数据时,日期不是 Wijmo 可以处理的格式,因此我向下传递一个字符串并使用该字符串实例化 Date
。
但是,现在我正在处理更复杂的图表,我希望不必循环遍历 JavaScript 中的数据以将日期字符串更改为 Date
对象。
所以这是我的问题:是否有可能以 JavaScript(读:Wijmo)能够理解的格式将日期从 PHP 传递到 JavaScript,而无需转换?如果没有,任何人都可以想到可以解决我的问题的替代解决方案。
I'm using Wijmo, a jQuery library that includes a few chart plugins. When working with dates, data is specified using the following format:
seriesList: [{
label: "Q1",
legendEntry: true,
data: {
x: [new Date(1978, 0, 1), new Date(1980, 0, 1), new Date(1981, 0, 1), new Date(1982, 0, 1), new Date(1983, 0, 1)],
y: [12, 21, 9, 29, 30]
},
offset: 0
}]
Notice the x
axis of the data
object, all of the items are instances of Date
.
When passing this data down in a JSON object from PHP the dates aren't in a format that Wijmo can handle, so I pass a string down and instantiate Date
with that string.
However, now I'm working with more complex charts I want to get away from having to loop through the data in JavaScript to change the date string to a Date
object.
So here's my question: is it possible to pass a date down from PHP to JavaScript, in a format that JavaScript (read: Wijmo) will understand without the need to convert? If not, can anyone think of an alternative solution that might solve my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ben,我不熟悉从 PHP 返回 JSON,但在其他 API 中,我看到过以 JSON 格式传递给客户端的实际 Date 对象,如下所示:
当您以 JSON 解析它时,应该将其转换为日期对象。请记住,JSON 是一个 JavaScript 对象,因此这只是返回一个将被执行的日期函数。
Ben, I am not familiar with returning JSON from PHP, but in other APIs I have seen actual Date objects passed to the client in JSON format like so:
This should be turned into a date object when you parse it in JSON. Remember, JSON is a JavaScript Object so this is just returning a Date function that will get executed.