我们正在开发一个涉及使用 dojo 10.0.3 绘制图表的股票相关应用程序。由于应用程序的性质,我们需要使用日期作为 X 轴。
日期(X 轴)和各种价格(Y 轴)都来自通过 Restful Web 服务调用检索的后端数据库:
var jsonStore = new JsonStore({"target": "/blah1/blah2", "idProperty": "date" });
以下是图表的代码片段:
function dateLabel (dateString) {
// While date in the backend is integer, what is passed to this function is strangely
// a string in the format of "1,415,232,000,000", irrelevant to the backend data.
var dateInt = parseInt( ( dateString.replace(/,/g, "") ) );
var dt = new Date(dateInt);
return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}
var xOpts = {
"title" : "Date",
"titleOrientation": "away", // or "axis"
"titleGap" : 10,
"majorLabels" : true,
"majorTicks" : true,
"majorTick" : {"length": 10},
"majorTickStep" : 31536000000, /** 1 year in milliseconds **/
"minorLabels" : true,
"minorTicks" : true,
"minorTick" : {"length": 6},
"minorTickStep" : 2419200000, /** 4 weeks in milliseconds **/
"microTicks" : true,
"microTick" : {"length": 3},
"microTickStep" : 86400000, /** 1 day in milliseconds **/
"labelFunc" : dateLabel
}
var chart = new Chart("chartNode");
chart.addPlot("default", { "type": Lines, "markers": true, hAxis: "x", vAxis: "y" } );
chart.addAxis("x", xOpts);
chart.addAxis("y", { vertical: true, fixLower: "major", fixUpper: "major" });
var tip = new Tooltip(chart,"default");
var mag = new Magnify(chart,"default");
var storeSeries = new StoreSeries(jsonStore, { query: { } }, {"x": "date", "y": "close"} );
chart.addSeries("Close", storeSeries, strokeAndFill1 );
chart.render();
虽然图表就 Y 轴而言是正确的,但绘制在 X 轴上的日期相当奇怪,并且它们与标记不对齐。
从 Firebug 中,从后端返回的 json 对象是:
[{"date":1414990800000,"open":164.25,"high":164.54,"low":163.38,"close":164.36,...},
{"date":1415077200000,"open":164.34,"high":164.36,"low":162.24,"close":162.65,...},
{"date":1415163600000,"open":163.13,"high":163.54,"low":161.56,"close":161.82,...},
{"date":1415250000000,"open":161.28,"high":161.53,"low":160.05,"close":161.46,...},
{"date":1415336400000,"open":161.42,"high":162.21,"low":160.85,"close":162.07,...}]
(The above "date" data correspond to Nov 3 ~ Nov 7, 2014, all at 0:00 AM GMT-5.)
但是,传递给 X 轴计算函数 dateLabel 的日期是一个字符串:“1,415,232,000,000”(即 2014 年 11 月 5 日 19:00 GMT-5),与后端数据无关。
问题是:
1) Why X-Axis behaves so strange? Why the data passed to the dateLabel function is not the date
returned from the web service call as defined in the StoreSeries?
2) Currently in the plot, when the "marker" is clicked, it only shows the price. Is it possible
to customize it to show both the date and the price?
非常感谢您的帮助!
电话