外汇牌价趋势波动图
实现本文程序的几个关键技术:
1、趋势波动图的数据源,来自于中国建设银行官方提供的实时外汇牌价,共有13个币种的现钞买入卖出价和现汇买入卖出价,每2分钟对外公布一次最新价格。
2、本程序的对外展示使用了highstock.js
3、远程向建行官网读取报文数据时,使用了java编写的jsoup解析器。
我们通过访问中国建设银行官网,在其“外汇行情”栏目可以看到如下的牌价信息页面:
我们通过查看页面javascript源代码,经过分析,可以找到如下代码
var shpjBody_HTML=""; var sqpjBody_HTML=""; var link='/cn/home/news/jshckpj.xml'; $.ajax({ type:"POST", url:link, async:true, dataType:"xml", success:function(ResponseText){ //更新时间插入 var AVL_DT = $($(ResponseText).find('AVL_DT').first()).text(); var TIMESTAMP = $($(ResponseText).find('TIMESTAMP').first()).text(); $(ResponseText).find('ReferencePriceSettlement').each(function () { //币别 var CM_CURR_COD = $(this).find('CM_CURR_COD').text(); if (CM_CURR_COD==1) CM_CURR_COD = $(this).find('SELL_OUT_CURR_TYPE').text(); //现汇买入汇率 var FXR_XCH_BUYIN = $(this).find('FXR_XCH_BUYIN').text(); //现汇卖出汇率 var FXR_XCH_SELLOUT = $(this).find('FXR_XCH_SELLOUT').text(); //现钞买入汇率 var FXR_CUR_BUYIN = $(this).find('FXR_CUR_BUYIN').text(); //现钞卖出汇率 var FXR_CUR_SELLOUT = $(this).find('FXR_CUR_SELLOUT').text(); //shpjBody_HTML += "<tr><td>"+CM_CURR_NAME+"</td><td>"+FXR_XCH_BUYIN+"</td><td>"+FXR_XCH_SELLOUT+"</td></tr>"; //sqpjBody_HTML += "<tr><td>"+CM_CURR_NAME+"</td><td>"+FXR_CUR_BUYIN+"5</td><td>"+FXR_CUR_SELLOUT+"</td></tr>"; getCMCURRName(CM_CURR_COD, FXR_XCH_BUYIN, FXR_XCH_SELLOUT, false); getCMCURRName(CM_CURR_COD, FXR_CUR_BUYIN, FXR_CUR_SELLOUT, true); });
按上述ajax调用的url找到http://forex.ccb.com/cn/home/news/jshckpj.xml 经过分析,后台主机每隔2分钟便更新一次该xml,其报文格式如下
<ReferencePriceSettlements> <AVL_DT>20150612</AVL_DT> <TIMESTAMP>2015-06-12 15:40</TIMESTAMP> <ReferencePriceSettlement name="1"> <CM_CURR_COD>12</CM_CURR_COD> <FXR_XCH_BUYIN>9.58980000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>9.66680000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>9.28170000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>9.66680000</FXR_CUR_SELLOUT> <MID_RATE>9.62830000</MID_RATE> <BUY_IN_RATE>9.62630000</BUY_IN_RATE> <SELL_OUT_RATIO>9.63020000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="2"> <CM_CURR_COD>13</CM_CURR_COD> <FXR_XCH_BUYIN>0.79910000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>0.80210000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>0.79260000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>0.80210000</FXR_CUR_SELLOUT> <MID_RATE>0.80060000</MID_RATE> <BUY_IN_RATE>0.80050000</BUY_IN_RATE> <SELL_OUT_RATIO>0.80070000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="3"> <CM_CURR_COD>14</CM_CURR_COD> <FXR_XCH_BUYIN>6.19420000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>6.21900000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>6.14450000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>6.21900000</FXR_CUR_SELLOUT> <MID_RATE>6.20660000</MID_RATE> <BUY_IN_RATE>6.20610000</BUY_IN_RATE> <SELL_OUT_RATIO>6.20700000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="4"> <CM_CURR_COD>15</CM_CURR_COD> <FXR_XCH_BUYIN>6.62290000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>6.67610000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>6.41010000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>6.67610000</FXR_CUR_SELLOUT> <MID_RATE>6.64950000</MID_RATE> <BUY_IN_RATE>6.64750000</BUY_IN_RATE> <SELL_OUT_RATIO>6.65200000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="5"> <CM_CURR_COD>18</CM_CURR_COD> <FXR_XCH_BUYIN>4.58680000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>4.61900000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>4.43720000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>4.62130000</FXR_CUR_SELLOUT> <MID_RATE>4.60290000</MID_RATE> <BUY_IN_RATE>4.60190000</BUY_IN_RATE> <SELL_OUT_RATIO>4.60430000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="6"> <CM_CURR_COD>21</CM_CURR_COD> <FXR_XCH_BUYIN>0.75040000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>0.75640000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>0.72630000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>0.75640000</FXR_CUR_SELLOUT> <MID_RATE>0.75340000</MID_RATE> <BUY_IN_RATE>0.75310000</BUY_IN_RATE> <SELL_OUT_RATIO>0.75370000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="7"> <CM_CURR_COD>22</CM_CURR_COD> <FXR_XCH_BUYIN>0.93090000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>0.93830000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>0.90100000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>0.93830000</FXR_CUR_SELLOUT> <MID_RATE>0.93460000</MID_RATE> <BUY_IN_RATE>0.93440000</BUY_IN_RATE> <SELL_OUT_RATIO>0.93480000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="8"> <CM_CURR_COD>23</CM_CURR_COD> <FXR_XCH_BUYIN>0.78590000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>0.79230000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>0.76070000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>0.79230000</FXR_CUR_SELLOUT> <MID_RATE>0.78910000</MID_RATE> <BUY_IN_RATE>0.78870000</BUY_IN_RATE> <SELL_OUT_RATIO>0.78950000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="9"> <CM_CURR_COD>27</CM_CURR_COD> <FXR_XCH_BUYIN>0.04999500</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>0.05038700</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>0.04838400</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>0.05039700</FXR_CUR_SELLOUT> <MID_RATE>0.05019100</MID_RATE> <BUY_IN_RATE>0.05017900</BUY_IN_RATE> <SELL_OUT_RATIO>0.05020600</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="10"> <CM_CURR_COD>28</CM_CURR_COD> <FXR_XCH_BUYIN>5.02420000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>5.06460000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>4.86280000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>5.06460000</FXR_CUR_SELLOUT> <MID_RATE>5.04440000</MID_RATE> <BUY_IN_RATE>5.04310000</BUY_IN_RATE> <SELL_OUT_RATIO>5.04590000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="11"> <CM_CURR_COD>29</CM_CURR_COD> <FXR_XCH_BUYIN>4.77230000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>4.81070000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>4.61900000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>4.81070000</FXR_CUR_SELLOUT> <MID_RATE>4.79150000</MID_RATE> <BUY_IN_RATE>4.78920000</BUY_IN_RATE> <SELL_OUT_RATIO>4.79300000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="12"> <CM_CURR_COD>33</CM_CURR_COD> <FXR_XCH_BUYIN>6.94750000</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>6.99630000</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>6.72090000</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>7.00050000</FXR_CUR_SELLOUT> <MID_RATE>6.97190000</MID_RATE> <BUY_IN_RATE>6.96950000</BUY_IN_RATE> <SELL_OUT_RATIO>6.97360000</SELL_OUT_RATIO> </ReferencePriceSettlement> <ReferencePriceSettlement name="13"> <CM_CURR_COD>72</CM_CURR_COD> <FXR_XCH_BUYIN>0.00542300</FXR_XCH_BUYIN> <FXR_XCH_SELLOUT>0.00576200</FXR_XCH_SELLOUT> <FXR_CUR_BUYIN>0.00536200</FXR_CUR_BUYIN> <FXR_CUR_SELLOUT>0.00576200</FXR_CUR_SELLOUT> <MID_RATE>0.00556200</MID_RATE> <BUY_IN_RATE>0.00555500</BUY_IN_RATE> <SELL_OUT_RATIO>0.00556900</SELL_OUT_RATIO> </ReferencePriceSettlement> </ReferencePriceSettlements>
各个节点含义如下
CM_CURR_COD 币种
FXR_XCH_BUYIN 现汇买入价
FXR_XCH_SELLOUT 现汇卖出价
FXR_CUR_BUYIN 现钞买入价
FXR_CUR_SELLOUT 现钞卖出价
综上,我们便有了实现实时汇率趋势波动图的数据源基础,我们通过jsoup来远程获取该报文并解析:
private void fxread(){ String actualUrl = null; actualUrl = "http://forex.ccb.com/cn/home/news/jshckpj.xml"; Document doc = null; try { doc = Jsoup.connect(actualUrl).get(); } catch (IOException e) { System.out.println("远程获取汇率牌价失败" + e); } String avl_dt = ""; String timestamp = ""; Elements AVL_DT = doc.getElementsByTag("AVL_DT"); Elements TIMESTAMP = doc.getElementsByTag("TIMESTAMP"); avl_dt = AVL_DT.first().html(); timestamp = TIMESTAMP.first().html(); Elements eles = doc.getElementsByTag("ReferencePriceSettlement"); Iterator ite = eles.iterator(); while(ite.hasNext()){ Element ele = (Element)ite.next(); String CM_CURR_COD = ele.getElementsByTag("CM_CURR_COD").first().html(); String FXR_XCH_BUYIN = ele.getElementsByTag("FXR_XCH_BUYIN").first().html(); String FXR_XCH_SELLOUT = ele.getElementsByTag("FXR_XCH_SELLOUT").first().html(); String FXR_CUR_BUYIN = ele.getElementsByTag("FXR_CUR_BUYIN").first().html(); String FXR_CUR_SELLOUT = ele.getElementsByTag("FXR_CUR_SELLOUT").first().html(); HashMap hm = new HashMap(); hm.put("AVL_DT", avl_dt); hm.put("TIMESTAMP", timestamp); hm.put("CM_CURR_COD", CM_CURR_COD); hm.put("FXR_XCH_BUYIN", FXR_XCH_BUYIN); hm.put("FXR_XCH_SELLOUT", FXR_XCH_SELLOUT); hm.put("FXR_CUR_BUYIN", FXR_CUR_BUYIN); hm.put("FXR_CUR_SELLOUT", FXR_CUR_SELLOUT); DBTools.insertfx(hm); //将牌价记录保存到数据库 } }
之后,我们将每天的牌价记录组装成json格式返回给前台的highstock.js控件做展示即可。
其json格式, 例如,下列为2015-06-11 美元全天24小时现汇买入价的数据:
[[1433980800000,6.19360000],[1433980920000,6.19360000],[1433981040000,6.19360000],[1433981160000,6.19360000],[1433981280000,6.19360000],[1433981400000,6.19360000],[1433981520000,6.19360000],[1433981640000,6.19360000],[1433981760000,6.19360000],[1433981880000,6.19360000],[1433982000000,6.19360000],[1433982120000,6.19360000],[1433982240000,6.19360000],[1433982360000,6.19360000],[1433982480000,6.19360000],[1433982600000,6.19360000],[1433982720000,6.19360000],[1433982840000,6.19360000],[1433982960000,6.19360000],[1433983080000,6.19360000],[1433983200000,6.19360000],[1433983320000,6.19360000],[1433983440000,6.19360000],[1433983560000,6.19360000],[1433983680000,6.19360000],[1433983800000,6.19360000],[1433983920000,6.19360000],[1433984040000,6.19360000],[1433984160000,6.19360000],[1433984280000,6.19360000],[1433984400000,6.19360000],[1433984520000,6.19360000],[1433984640000,6.19360000],[1433984760000,6.19360000],[1433984880000,6.19360000],[1433985000000,6.19360000],[1433985120000,6.19360000],[1433985240000,6.19360000],[1433985360000,6.19360000],[1433985480000,6.19360000],[1433985600000,6.19360000],[1433985720000,6.19360000],[1433985840000,6.19360000],[1433985960000,6.19360000],[1433986080000,6.19360000],[1433986200000,6.19360000],[1433986320000,6.19360000],[1433986440000,6.19360000],[1433986560000,6.19360000],[1433986680000,6.19360000],[1433986800000,6.19360000],[1433986920000,6.19360000],[1433987040000,6.19360000],[1433987160000,6.19360000],[1433987280000,6.19360000],[1433987400000,6.19360000],[1433987520000,6.19360000],[1433987640000,6.19360000],[1433987760000,6.19360000],[1433987880000,6.19360000],[1433988000000,6.19360000],[1433988120000,6.19360000],[1433988240000,6.19360000],[1433988360000,6.19360000],[1433988480000,6.19360000],[1433988600000,6.19360000],[1433988720000,6.19360000],[1433988840000,6.19360000],[1433988960000,6.19360000],[1433989080000,6.19360000],[1433989200000,6.19360000],[1433989320000,6.19360000],[1433989440000,6.19360000],[1433989560000,6.19360000],[1433989680000,6.19360000],[1433989800000,6.19360000],[1433989920000,6.19360000],[1433990040000,6.19360000],[1433990160000,6.19360000],[1433990280000,6.19360000],[1433990400000,6.19360000],[1433990520000,6.19360000],[1433990640000,6.19360000],[1433990760000,6.19360000],[1433990880000,6.19360000],[1433991000000,6.19360000],[1433991120000,6.19360000],[1433991240000,6.19360000],[1433991360000,6.19360000],[1433991480000,6.19360000],[1433991600000,6.19360000],[1433991720000,6.19360000],[1433991840000,6.19360000],[1433991960000,6.19360000],[1433992080000,6.19360000],[1433992200000,6.19360000],[1433992320000,6.19360000],[1433992440000,6.19360000],[1433992560000,6.19360000],[1433992680000,6.19360000],[1433992800000,6.19360000],[1433992920000,6.19360000],[1433993040000,6.19360000],[1433993160000,6.19360000],[1433993280000,6.19360000],[1433993400000,6.19360000],[1433993520000,6.19360000],[1433993640000,6.19360000],[1433993760000,6.19360000],[1433993880000,6.19360000],[1433994000000,6.19360000],[1433994120000,6.19360000],[1433994240000,6.19360000],[1433994360000,6.19360000],[1433994480000,6.19360000],[1433994600000,6.19360000],[1433994720000,6.19360000],[1433994840000,6.19360000],[1433994960000,6.19360000],[1433995080000,6.19360000],[1433995200000,6.19360000],[1433995320000,6.19360000],[1433995440000,6.19360000],[1433995560000,6.19360000],[1433995680000,6.19360000],[1433995800000,6.19360000],[1433995920000,6.19360000],[1433996040000,6.19360000],[1433996160000,6.19360000],[1433996280000,6.19360000],[1433996400000,6.19360000],[1433996520000,6.19360000],[1433996640000,6.19360000],[1433996760000,6.19360000],[1433996880000,6.19360000],[1433997000000,6.19360000],[1433997120000,6.19360000],[1433997240000,6.19360000],[1433997360000,6.19360000],[1433997480000,6.19360000],[1433997600000,6.19360000],[1433997720000,6.19360000],[1433997840000,6.19360000],[1433997960000,6.19360000],[1433998080000,6.19360000],[1433998200000,6.19360000],[1433998320000,6.19360000],[1433998440000,6.19360000],[1433998560000,6.19360000],[1433998680000,6.19360000],[1433999040000,6.19360000],[1433999160000,6.19360000],[1433999280000,6.19360000],[1433999400000,6.19360000],[1433999520000,6.19360000],[1433999640000,6.19360000],[1433999760000,6.19360000],[1433999880000,6.19360000],[1434000000000,6.19360000],[1434000120000,6.19360000],[1434000240000,6.19360000],[1434000360000,6.19360000],[1434000480000,6.19360000],[1434000600000,6.19360000],[1434000720000,6.19360000],[1434000840000,6.19360000],[1434000960000,6.19360000],[1434001080000,6.19360000],[1434001200000,6.19360000],[1434001320000,6.19360000],[1434001440000,6.19360000],[1434001560000,6.19360000],[1434001800000,6.19360000],[1434001920000,6.19360000],[1434002040000,6.19360000],[1434002160000,6.19360000],[1434002280000,6.19360000],[1434002520000,6.19360000],[1434002640000,6.19360000],[1434002760000,6.19360000],[1434002880000,6.19360000],[1434003000000,6.19360000],[1434003120000,6.19360000],[1434003240000,6.19360000],[1434003360000,6.19360000],[1434003480000,6.19360000],[1434003600000,6.19360000],[1434003720000,6.19360000],[1434003840000,6.19360000],[1434003960000,6.19360000],[1434004080000,6.19360000],[1434004200000,6.19360000],[1434004320000,6.19360000],[1434004440000,6.19360000],[1434004560000,6.19360000],[1434004680000,6.19360000],[1434004800000,6.19360000],[1434004920000,6.19360000],[1434005040000,6.19360000],[1434005160000,6.19360000],[1434005280000,6.19360000],[1434005400000,6.19360000],[1434005520000,6.19360000],[1434005640000,6.19360000],[1434005760000,6.19360000],[1434005880000,6.19360000],[1434006000000,6.19360000],[1434006120000,6.19360000],[1434006240000,6.19360000],[1434006360000,6.19360000],[1434006480000,6.19360000],[1434006600000,6.19360000],[1434006720000,6.19360000],[1434006840000,6.19360000],[1434006960000,6.19360000],[1434007080000,6.19360000],[1434007200000,6.19360000],[1434007320000,6.19360000],[1434007440000,6.19360000],[1434007560000,6.19360000],[1434007680000,6.19360000],[1434007800000,6.19360000],[1434007920000,6.19360000],[1434008040000,6.19360000],[1434008160000,6.19360000],[1434008280000,6.19360000],[1434008400000,6.19360000],[1434008520000,6.19360000],[1434008640000,6.19360000],[1434008760000,6.19360000],[1434008880000,6.19360000],[1434009000000,6.19360000],[1434009120000,6.19360000],[1434009240000,6.19360000],[1434009360000,6.19360000],[1434009480000,6.19360000],[1434009600000,6.19360000],[1434009720000,6.19360000],[1434009840000,6.19360000],[1434009960000,6.19360000],[1434010080000,6.19360000],[1434010200000,6.19360000],[1434010320000,6.19360000],[1434010440000,6.19360000],[1434010560000,6.19360000],[1434010680000,6.19360000],[1434010800000,6.19360000],[1434010920000,6.19360000],[1434011040000,6.19360000],[1434011160000,6.19360000],[1434011280000,6.19360000],[1434011400000,6.19360000],[1434011520000,6.19360000],[1434011640000,6.19360000],[1434011760000,6.19360000],[1434011880000,6.19360000],[1434012000000,6.19360000],[1434012240000,6.19360000],[1434012360000,6.19360000],[1434012480000,6.19360000],[1434012600000,6.19360000],[1434012720000,6.19360000],[1434012840000,6.19360000],[1434012960000,6.19360000],[1434013080000,6.19360000],[1434013200000,6.19360000],[1434013320000,6.19360000],[1434013440000,6.19360000],[1434013560000,6.19360000],[1434013680000,6.19360000],[1434013800000,6.19360000],[1434013920000,6.19360000],[1434014040000,6.19360000],[1434014160000,6.19360000],[1434014280000,6.19360000],[1434014400000,6.19360000],[1434014520000,6.19360000],[1434014640000,6.19360000],[1434014760000,6.19360000],[1434014880000,6.19310000],[1434015000000,6.19310000],[1434015120000,6.19330000],[1434015240000,6.19330000],[1434015360000,6.19330000],[1434015480000,6.19330000],[1434015600000,6.19340000],[1434015720000,6.19340000],[1434015840000,6.19370000],[1434015960000,6.19380000],[1434016080000,6.19380000],[1434016200000,6.19380000],[1434016320000,6.19380000],[1434016440000,6.19400000],[1434016560000,6.19400000],[1434016680000,6.19410000],[1434016800000,6.19410000],[1434016920000,6.19410000],[1434017040000,6.19400000],[1434017160000,6.19430000],[1434017280000,6.19430000],[1434017400000,6.19430000],[1434017520000,6.19430000],[1434017640000,6.19430000],[1434017760000,6.19430000],[1434017880000,6.19420000],[1434018000000,6.19430000],[1434018120000,6.19430000],[1434018240000,6.19430000],[1434018360000,6.19430000],[1434018480000,6.19430000],[1434018600000,6.19430000],[1434018720000,6.19440000],[1434018840000,6.19400000],[1434018960000,6.19420000],[1434019080000,6.19400000],[1434019200000,6.19410000],[1434019320000,6.19420000],[1434019440000,6.19420000],[1434019560000,6.19420000],[1434019680000,6.19420000],[1434019800000,6.19410000],[1434019920000,6.19410000],[1434020040000,6.19400000],[1434020160000,6.19400000],[1434020400000,6.19400000],[1434020520000,6.19420000],[1434020640000,6.19420000],[1434020760000,6.19410000],[1434020880000,6.19410000],[1434021000000,6.19410000],[1434021120000,6.19410000],[1434021240000,6.19410000],[1434021360000,6.19410000],[1434021480000,6.19410000],[1434021600000,6.19410000],[1434021720000,6.19410000],[1434021840000,6.19420000],[1434021960000,6.19420000],[1434022080000,6.19420000],[1434022200000,6.19420000],[1434022320000,6.19420000],[1434022440000,6.19420000],[1434022560000,6.19420000],[1434022680000,6.19420000],[1434022800000,6.19440000],[1434022920000,6.19440000],[1434023040000,6.19440000],[1434023160000,6.19460000],[1434023280000,6.19460000],[1434023400000,6.19460000],[1434023520000,6.19460000],[1434023640000,6.19460000],[1434023760000,6.19460000],[1434023880000,6.19460000],[1434024000000,6.19460000],[1434024120000,6.19460000],[1434024240000,6.19460000],[1434024360000,6.19460000],[1434024480000,6.19460000],[1434024600000,6.19460000],[1434024720000,6.19460000],[1434024840000,6.19460000],[1434024960000,6.19460000],[1434025080000,6.19460000],[1434025200000,6.19460000],[1434025320000,6.19460000],[1434025440000,6.19460000],[1434025560000,6.19460000],[1434025680000,6.19460000],[1434025800000,6.19460000],[1434025920000,6.19460000],[1434026040000,6.19460000],[1434026160000,6.19460000],[1434026280000,6.19460000],[1434026400000,6.19460000],[1434026520000,6.19460000],[1434026640000,6.19460000],[1434026760000,6.19460000],[1434026880000,6.19460000],[1434027000000,6.19460000],[1434027120000,6.19460000],[1434027240000,6.19460000],[1434027360000,6.19460000],[1434027480000,6.19460000],[1434027600000,6.19460000],[1434027720000,6.19460000],[1434027840000,6.19460000],[1434027960000,6.19460000],[1434028080000,6.19460000],[1434028200000,6.19460000],[1434028320000,6.19460000],[1434028440000,6.19460000],[1434028560000,6.19460000],[1434028680000,6.19460000],[1434028800000,6.19460000],[1434028920000,6.19460000],[1434029040000,6.19460000],[1434029160000,6.19460000],[1434029280000,6.19460000],[1434029400000,6.19460000],[1434029520000,6.19460000],[1434029640000,6.19460000],[1434029760000,6.19460000],[1434029880000,6.19460000],[1434030000000,6.19460000],[1434030120000,6.19460000],[1434030240000,6.19470000],[1434030360000,6.19470000],[1434030480000,6.19470000],[1434030600000,6.19470000],[1434030720000,6.19470000],[1434030840000,6.19470000],[1434030960000,6.19470000],[1434031080000,6.19470000],[1434031200000,6.19470000],[1434031320000,6.19470000],[1434031440000,6.19470000],[1434031560000,6.19470000],[1434031680000,6.19470000],[1434031800000,6.19470000],[1434031920000,6.19470000],[1434032040000,6.19470000],[1434032160000,6.19470000],[1434032280000,6.19480000],[1434032400000,6.19460000],[1434032520000,6.19410000],[1434032640000,6.19420000],[1434032760000,6.19440000],[1434032880000,6.19430000],[1434033000000,6.19430000],[1434033120000,6.19380000],[1434033240000,6.19390000],[1434033360000,6.19380000],[1434033480000,6.19380000],[1434033600000,6.19350000],[1434033720000,6.19360000],[1434057720000,6.19410000],[1434057840000,6.19410000],[1434057960000,6.19410000],[1434058080000,6.19410000],[1434058200000,6.19410000],[1434058320000,6.19410000],[1434058440000,6.19410000],[1434058560000,6.19410000],[1434058680000,6.19410000],[1434058800000,6.19410000],[1434058920000,6.19410000],[1434059040000,6.19410000],[1434059160000,6.19410000],[1434059280000,6.19410000],[1434059400000,6.19410000],[1434059520000,6.19410000],[1434059640000,6.19410000],[1434059760000,6.19410000],[1434059880000,6.19410000],[1434060000000,6.19410000],[1434060120000,6.19410000],[1434060240000,6.19410000],[1434060360000,6.19410000],[1434060480000,6.19410000],[1434060600000,6.19410000],[1434060720000,6.19410000],[1434060840000,6.19410000],[1434060960000,6.19410000],[1434061080000,6.19410000],[1434061200000,6.19410000],[1434061320000,6.19410000],[1434061560000,6.19410000],[1434061680000,6.19410000],[1434061800000,6.19410000],[1434061920000,6.19410000],[1434062040000,6.19410000],[1434062160000,6.19410000],[1434062280000,6.19410000],[1434062400000,6.19410000],[1434062520000,6.19410000],[1434062640000,6.19410000],[1434062760000,6.19410000],[1434062880000,6.19410000],[1434063000000,6.19410000],[1434063120000,6.19410000],[1434063240000,6.19410000],[1434063360000,6.19410000],[1434063480000,6.19410000],[1434063600000,6.19410000],[1434063720000,6.19410000],[1434063840000,6.19410000],[1434063960000,6.19410000],[1434064080000,6.19410000],[1434064200000,6.19410000],[1434064320000,6.19410000],[1434064440000,6.19410000],[1434064560000,6.19410000],[1434064680000,6.19410000],[1434064800000,6.19410000],[1434064920000,6.19410000],[1434065040000,6.19410000],[1434065160000,6.19410000],[1434065280000,6.19410000],[1434065400000,6.19410000],[1434065640000,6.19410000],[1434065760000,6.19410000],[1434065880000,6.19410000],[1434066000000,6.19410000],[1434066120000,6.19410000],[1434066240000,6.19410000],[1434066360000,6.19410000],[1434066480000,6.19410000],[1434066600000,6.19410000],[1434066720000,6.19410000],[1434066840000,6.19410000],[1434066960000,6.19410000],[1434067080000,6.19410000]]
关于highstock的详细使用,有官方文档,这里不再叙述。唯一需要说明的是上述json格式中的时间为UTC时间的毫秒数,我们计算的时候在返回之前,要按照东八区的北京时间来显示,那么就必须加上8小时了。
javascript代码如下,这里使用了jquery的getJSON方法:
/** 外汇牌价趋势图 liny 2015-6-7 */ /* date 查询日期 yyyy-MM-dd currency 币种代码 price_type 牌价种类 :FXR_XCH_BUYIN 现汇买入价,FXR_XCH_SELLOUT 现汇卖出价,FXR_CUR_BUYIN 现钞买入价,FXR_CUR_SELLOUT 现钞卖出价 */ function show_tendency(date,currency,price_type){ var priceTypeObj = {'FXR_XCH_BUYIN':'现汇买入价', 'FXR_XCH_SELLOUT':'现汇卖出价', 'FXR_CUR_BUYIN':'现钞买入价', 'FXR_CUR_SELLOUT':'现钞卖出价'}; var currencyObj = {'12':'英镑', '13':'港币', '14':'美元', '15':'瑞士法郎', '18':'新加坡元', '21':'瑞典克朗', '22':'丹麦克郎', '23':'挪威克郎', '27':'日元', '28':'加拿大元', '29':'澳大利亚元', '33':'欧元', '72':'韩元'}; $.getJSON('FXTendencyAction.do?date='+date+'&type='+price_type+'&currcode='+currency, function (data){ $('#tendency_container').highcharts('StockChart', { credits : { text : 'liny' }, exporting: { enabled: false }, title : { text : currencyObj[currency] + ' ' + date + ' ' + priceTypeObj[price_type]//标题 }, xAxis: { tickPixelInterval: 100, type: 'datetime', labels: { formatter: function() { var vDate=new Date(this.value); //alert(this.value); return vDate.getUTCFullYear()+"-"+(vDate.getUTCMonth()+1)+"-"+vDate.getUTCDate()+" "+vDate.getUTCHours()+":"+vDate.getUTCMinutes(); }, align: 'center' } }, yAxis : { title: { text: '外汇牌价' } }, tooltip: { xDateFormat: '%Y-%m-%d %H:%M:%S, %A' }, rangeSelector: { buttons: [{ type: 'minute', count: 10, text: '10分钟' },{ type: 'minute', count: 30, text: '半小时' }, { type: 'minute', count: 60, text: '1小时' }, { type: 'minute', count: 180, text: '3小时' }, { type: 'minute', count: 300, text: '5小时' }, { type: 'minute', count: 720, text: '12小时' }, { type: 'minute', count:1440, text: '24小时' }], selected: 1 }, series: [{ name: '外汇牌价', data: data }] }); }); }