candlestick.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1" />
  5. <script src="esl.js"></script>
  6. <script src="config.js"></script>
  7. <script src="lib/facePrint.js"></script>
  8. </head>
  9. <body>
  10. <style>
  11. html, body, #main {
  12. width: 100%;
  13. height: 100%;
  14. }
  15. </style>
  16. <div id="info"></div>
  17. <div id="main"></div>
  18. <script>
  19. /**
  20. * @see <https://en.wikipedia.org/wiki/Michelson%E2%80%93Morley_experiment>
  21. * @see <http://bl.ocks.org/mbostock/4061502>
  22. */
  23. var chart;
  24. var data;
  25. require([
  26. 'echarts',
  27. 'data/security-sh-2013.json',
  28. 'echarts/chart/candlestick',
  29. 'echarts/chart/line',
  30. 'echarts/component/title',
  31. 'echarts/component/legend',
  32. 'echarts/component/grid',
  33. 'echarts/component/tooltip',
  34. 'echarts/component/dataZoom',
  35. 'echarts/component/markPoint',
  36. 'echarts/component/markLine'
  37. ], function (echarts, rawData) {
  38. chart = echarts.init(document.getElementById('main'), null, {
  39. renderer: 'canvas'
  40. });
  41. data = splitData(rawData);
  42. update();
  43. chart.on('click', function (info) {
  44. console.log(info);
  45. if (info.data && info.data.length === 4) {
  46. alert([
  47. 'clicked on: ',
  48. 'DATA: ' + info.name,
  49. 'OPEN: ' + info.data[0],
  50. 'CLOSE: ' + info.data[1],
  51. 'LOWEST: ' + info.data[2],
  52. 'HIGHEST: ' + info.data[3]
  53. ].join('\n'));
  54. }
  55. else if (info.data && info.data.length === 2) {
  56. // Markpoint
  57. alert('mark point');
  58. }
  59. });
  60. })
  61. function splitData(rawData) {
  62. var categoryData = [];
  63. var values = []
  64. for (var i = 0; i < rawData.length; i++) {
  65. categoryData.push(rawData[i].splice(0, 1)[0]);
  66. values.push(rawData[i])
  67. }
  68. return {
  69. categoryData: categoryData,
  70. values: values
  71. };
  72. }
  73. function parseDate(timestamp) {
  74. var date = new Date(timestamp);
  75. return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
  76. }
  77. function update() {
  78. chart.setOption({
  79. legend: {
  80. data: ['上证指数', '开盘']
  81. },
  82. tooltip: {
  83. trigger: 'axis',
  84. axisPointer: {
  85. type: 'line'
  86. }
  87. },
  88. grid: {
  89. left: '10%',
  90. right: '10%',
  91. bottom: '15%'
  92. },
  93. xAxis: {
  94. type: 'category',
  95. data: data.categoryData,
  96. scale: true,
  97. boundaryGap : false,
  98. axisLine: {onZero: false},
  99. splitLine: {show: false},
  100. splitNumber: 20,
  101. min: 'dataMin',
  102. max: 'dataMax'
  103. },
  104. yAxis: {
  105. scale: true,
  106. splitArea: {
  107. show: true
  108. }
  109. },
  110. dataZoom: [
  111. {
  112. type: 'inside',
  113. start: 50,
  114. end: 100
  115. },
  116. {
  117. show: true,
  118. type: 'slider',
  119. y: '90%',
  120. start: 50,
  121. end: 100
  122. }
  123. ],
  124. series: [
  125. {
  126. name: '开盘',
  127. type: 'line',
  128. data: (function () {
  129. opens = [];
  130. for (var i = 0, len = data.values.length; i < len; i++) {
  131. opens.push(data.values[i][0]);
  132. }
  133. return opens;
  134. })(),
  135. smooth: true,
  136. lineStyle: {
  137. normal: {color: '#aaa'}
  138. }
  139. },
  140. {
  141. name: '上证指数',
  142. type: 'candlestick',
  143. data: data.values,
  144. tooltip: {
  145. formatter: function (param) {
  146. var param = param[0];
  147. return [
  148. '日期:' + param.name + '<hr size=1 style="margin: 3px 0">',
  149. '开盘:' + param.data[0] + '<br/>',
  150. '收盘:' + param.data[1] + '<br/>',
  151. '日最低:' + param.data[2] + '<br/>',
  152. '日最高:' + param.data[3] + '<br/>'
  153. ].join('')
  154. }
  155. },
  156. markPoint: {
  157. data: [
  158. {
  159. name: '某个坐标',
  160. coord: ['2013/5/21', 2300]
  161. },
  162. {
  163. name: '某个屏幕坐标',
  164. x: 100,
  165. y: 200,
  166. label: {
  167. normal: {
  168. show: false
  169. },
  170. emphasis: {
  171. show: true,
  172. position: 'top',
  173. formatter: 'zxcv'
  174. }
  175. }
  176. },
  177. {
  178. name: 'max value (default)',
  179. type: 'max'
  180. },
  181. {
  182. name: 'min value (default)',
  183. type: 'min'
  184. },
  185. {
  186. name: 'max value (dim:close)',
  187. type: 'max',
  188. valueDim: 'close'
  189. },
  190. {
  191. name: 'average value (dim:close)',
  192. type: 'average',
  193. valueDim: 'close'
  194. }
  195. ],
  196. tooltip: {
  197. formatter: function (param) {
  198. return param.name + '<br>' + (param.data.coord || '');
  199. }
  200. }
  201. },
  202. markLine: {
  203. data: [
  204. [
  205. {name: '两个坐标之间的标线', coord: ['2013/4/25', 2130]},
  206. {coord: ['2013/5/27', 2220]}
  207. ],
  208. [
  209. {name: '两个屏幕坐标之间的标线', x: 100, y: 100},
  210. {x: 250, y: 130}
  211. ],
  212. [
  213. {name: 'max - min', type: 'max'},
  214. {type: 'min'}
  215. ],
  216. {
  217. name: 'min line',
  218. type: 'min'
  219. },
  220. {
  221. name: 'max line on dim:open',
  222. type: 'max',
  223. valueDim: 'open'
  224. }
  225. ]
  226. }
  227. }
  228. ]
  229. });
  230. // setTimeout(function () {
  231. // chart.setOption({
  232. // dataZoom: {
  233. // startValue: '2013/1/28',
  234. // endValue: '2013/1/30'
  235. // }
  236. // });
  237. // }, 1000);
  238. }
  239. </script>
  240. </body>
  241. </html>