brush2.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <script src="lib/esl.js"></script>
  5. <script src="lib/config.js"></script>
  6. <script src="lib/jquery.min.js"></script>
  7. <link rel="stylesheet" href="lib/reset.css" />
  8. </head>
  9. <body>
  10. <style>
  11. .split {
  12. line-height: 60px;
  13. height: 60px;
  14. background: #333;
  15. text-align: center;
  16. font-weight: bold;
  17. font-size: 20px;
  18. color: #fff;
  19. }
  20. .block {
  21. position: relative;
  22. }
  23. body {
  24. background: #555;
  25. }
  26. body .main {
  27. height: 550px;
  28. margin-right: 220px;
  29. margin-left: 20px;
  30. }
  31. .panel {
  32. position: absolute;
  33. top: 0;
  34. right: 0;
  35. bottom: 0;
  36. width: 200px;
  37. background: #555;
  38. color: #fff;
  39. font-size: 14px;
  40. font-weight: normal;
  41. padding: 10px;
  42. }
  43. ul.panel-desc {
  44. padding-left: 20px;
  45. padding-bottom: 10px;
  46. border-bottom: 1px solid #777;
  47. }
  48. ul.panel-desc li {
  49. margin-bottom: 5px;
  50. }
  51. h3 {
  52. font-size: 14px;
  53. }
  54. #main2 {
  55. height: 450px;
  56. }
  57. #main2-st {
  58. position: relative;
  59. height: 200px;
  60. margin-right: 220px;
  61. margin-left: 20px;
  62. }
  63. strong {
  64. background: yellow;
  65. padding: 0 2px;
  66. border-radius: 2px;
  67. color: #000;
  68. }
  69. #main1-st {
  70. position: absolute;
  71. top: 0;
  72. right: 300px;
  73. height: 550px;
  74. width: 400px;
  75. z-index: 999;
  76. }
  77. .sm-settings {
  78. border-bottom: 1px solid #777;
  79. padding-bottom: 15px;
  80. }
  81. .sm-settings input {
  82. max-width: 50px;
  83. }
  84. </style>
  85. <div class="split">Candlestick</div>
  86. <div class="block">
  87. <div id="main1" class="main"></div>
  88. <!-- <div id="main1-st"></div> -->
  89. <div class="panel">
  90. <ul class="panel-desc">
  91. <li><strong>CHECK</strong>: brush an area and drag dataZoom.</li>
  92. </ul>
  93. <div id="panel1"></div>
  94. </div>
  95. </div>
  96. <div class="split">Graph</div>
  97. <div class="block">
  98. <div id="main2" class="main"></div>
  99. <!-- <div id="main1-st"></div> -->
  100. <div class="panel">
  101. <ul class="panel-desc">
  102. <li><strong>CHECK</strong>: brush an area and drag dataZoom.</li>
  103. </ul>
  104. <div id="panel2"></div>
  105. </div>
  106. </div>
  107. <!-- =================== 1 ===================== -->
  108. <script>
  109. /**
  110. * @see <https://en.wikipedia.org/wiki/Michelson%E2%80%93Morley_experiment>
  111. * @see <http://bl.ocks.org/mbostock/4061502>
  112. */
  113. var chart;
  114. var data;
  115. var panel;
  116. require([
  117. 'echarts',
  118. 'data/stock-DJI.json'
  119. // 'echarts/chart/candlestick',
  120. // 'echarts/chart/line',
  121. // 'echarts/chart/bar',
  122. // 'echarts/component/title',
  123. // 'echarts/component/legend',
  124. // 'echarts/component/grid',
  125. // 'echarts/component/tooltip',
  126. // 'echarts/component/dataZoom',
  127. // 'echarts/component/toolbox',
  128. // 'echarts/component/brush',
  129. // 'echarts/component/markPoint',
  130. // 'echarts/component/markLine'
  131. ], function (echarts, rawData) {
  132. chart = echarts.init(document.getElementById('main1'), null, {
  133. });
  134. panel = document.getElementById('panel1');
  135. data = splitData(rawData);
  136. update();
  137. chart.on('click', function (info) {
  138. if (info.data && info.componentType === 'series') {
  139. alert([
  140. 'clicked on: ',
  141. 'DATA: ' + info.name,
  142. 'OPEN: ' + info.data[0],
  143. 'CLOSE: ' + info.data[1],
  144. 'LOWEST: ' + info.data[2],
  145. 'HIGHEST: ' + info.data[3],
  146. 'VOLUMN: ' + info.data[4]
  147. ].join('\n'));
  148. }
  149. });
  150. })
  151. function splitData(rawData) {
  152. var categoryData = [];
  153. var values = [];
  154. var volumns = [];
  155. for (var i = 0; i < rawData.length; i++) {
  156. categoryData.push(rawData[i].splice(0, 1)[0]);
  157. values.push(rawData[i])
  158. volumns.push(rawData[i][4]);
  159. }
  160. return {
  161. categoryData: categoryData,
  162. values: values,
  163. volumns: volumns
  164. };
  165. }
  166. function calculateMA(dayCount, data) {
  167. var result = [];
  168. for (var i = 0, len = data.values.length; i < len; i++) {
  169. if (i < dayCount) {
  170. result.push('-');
  171. continue;
  172. }
  173. var sum = 0;
  174. for (var j = 0; j < dayCount; j++) {
  175. sum += data.values[i - j][1];
  176. }
  177. result.push(+(sum / dayCount).toFixed(3));
  178. }
  179. return result;
  180. }
  181. function update() {
  182. chart.setOption({
  183. backgroundColor: '#eee',
  184. animation: false,
  185. legend: {
  186. left: 0,
  187. data: ['Dow-Jones index', 'MA5', 'MA10', 'MA20', 'MA30']
  188. },
  189. tooltip: {
  190. trigger: 'axis',
  191. axisPointer: {
  192. type: 'line'
  193. }
  194. },
  195. toolbox: {
  196. feature: {
  197. dataZoom: {
  198. yAxisIndex: false
  199. },
  200. brush: {
  201. type: ['polygon', 'rect', 'lineX', 'lineY', 'keep', 'clear']
  202. }
  203. }
  204. },
  205. brush: {
  206. xAxisIndex: 'all',
  207. brushLink: 'all',
  208. outOfBrush: {
  209. colorAlpha: 0.1
  210. }
  211. },
  212. grid: [
  213. {
  214. left: '10%',
  215. right: '10%',
  216. height: 300
  217. },
  218. {
  219. left: '10%',
  220. right: '10%',
  221. height: 70,
  222. bottom: 80
  223. }
  224. ],
  225. xAxis: [
  226. {
  227. type: 'category',
  228. data: data.categoryData,
  229. scale: true,
  230. boundaryGap : false,
  231. axisLine: {onZero: false},
  232. splitLine: {show: false},
  233. splitNumber: 20,
  234. min: 'dataMin',
  235. max: 'dataMax'
  236. },
  237. {
  238. type: 'category',
  239. gridIndex: 1,
  240. data: data.categoryData,
  241. scale: true,
  242. boundaryGap : false,
  243. axisLine: {onZero: false},
  244. axisTick: {show: false},
  245. splitLine: {show: false},
  246. axisLabel: {show: false},
  247. splitNumber: 20,
  248. min: 'dataMin',
  249. max: 'dataMax'
  250. }
  251. ],
  252. yAxis: [
  253. {
  254. scale: true,
  255. splitArea: {
  256. show: true
  257. }
  258. },
  259. {
  260. scale: true,
  261. gridIndex: 1,
  262. splitNumber: 2,
  263. axisLabel: {show: false},
  264. axisLine: {show: false},
  265. axisTick: {show: false},
  266. splitLine: {show: false}
  267. }
  268. ],
  269. dataZoom: [
  270. {
  271. type: 'inside',
  272. xAxisIndex: [0, 1],
  273. start: 98,
  274. end: 100
  275. },
  276. {
  277. show: true,
  278. xAxisIndex: [0, 1],
  279. type: 'slider',
  280. bottom: 10,
  281. start: 98,
  282. end: 100
  283. }
  284. ],
  285. series: [
  286. {
  287. name: 'Dow-Jones index',
  288. type: 'candlestick',
  289. data: data.values,
  290. itemStyle: {
  291. normal: {
  292. borderColor: null,
  293. borderColor0: null
  294. }
  295. },
  296. tooltip: {
  297. formatter: function (param) {
  298. var param = param[0];
  299. return [
  300. 'Date: ' + param.name + '<hr size=1 style="margin: 3px 0">',
  301. 'Open: ' + param.data[0] + '<br/>',
  302. 'Close: ' + param.data[1] + '<br/>',
  303. 'Lowest: ' + param.data[2] + '<br/>',
  304. 'Highest: ' + param.data[3] + '<br/>'
  305. ].join('')
  306. }
  307. }
  308. },
  309. {
  310. name: 'MA5',
  311. type: 'line',
  312. data: calculateMA(5, data),
  313. smooth: true,
  314. lineStyle: {
  315. normal: {opacity: 0.5}
  316. }
  317. },
  318. {
  319. name: 'MA10',
  320. type: 'line',
  321. data: calculateMA(10, data),
  322. smooth: true,
  323. lineStyle: {
  324. normal: {opacity: 0.5}
  325. }
  326. },
  327. {
  328. name: 'MA20',
  329. type: 'line',
  330. data: calculateMA(20, data),
  331. smooth: true,
  332. lineStyle: {
  333. normal: {opacity: 0.5}
  334. }
  335. },
  336. {
  337. name: 'MA30',
  338. type: 'line',
  339. data: calculateMA(30, data),
  340. smooth: true,
  341. lineStyle: {
  342. normal: {opacity: 0.5}
  343. }
  344. },
  345. {
  346. name: 'Volumn',
  347. type: 'bar',
  348. xAxisIndex: 1,
  349. yAxisIndex: 1,
  350. data: data.volumns
  351. }
  352. ]
  353. });
  354. chart.on('brushSelected', renderBrushed);
  355. function renderBrushed(params) {
  356. var sum = 0;
  357. var min = Infinity;
  358. var max = -Infinity;
  359. var countBySeries = [];
  360. var brushComponent = params.batch[0];
  361. var rawIndices = brushComponent.selected[0].dataIndex;
  362. for (var i = 0; i < rawIndices.length; i++) {
  363. var val = data.values[rawIndices[i]][1];
  364. sum += val;
  365. min = Math.min(val, min);
  366. max = Math.max(val, max);
  367. }
  368. panel.innerHTML = [
  369. '<h3>STATISTICS:</h3>',
  370. 'SUM of open: ' + (sum / rawIndices.length).toFixed(4) + '<br>',
  371. 'MIN of open: ' + min.toFixed(4) + '<br>',
  372. 'MAX of open: ' + max.toFixed(4) + '<br>'
  373. ].join(' ');
  374. }
  375. chart.dispatchAction({
  376. type: 'brush',
  377. areas: [
  378. {
  379. brushType: 'lineX',
  380. coordRange: ['2016-06-02', '2016-06-20'],
  381. xAxisIndex: 0
  382. }
  383. ]
  384. });
  385. }
  386. </script>
  387. <!-- =================== 2 ===================== -->
  388. <script>
  389. require([
  390. 'echarts',
  391. 'extension/dataTool',
  392. // 'echarts/chart/graph',
  393. // 'echarts/component/title',
  394. // 'echarts/component/legend',
  395. // 'echarts/component/geo',
  396. // 'echarts/component/tooltip',
  397. // 'echarts/component/visualMap',
  398. 'theme/vintage'
  399. ], function (echarts, dataTool) {
  400. var gexf = dataTool.gexf;
  401. var chart = echarts.init(document.getElementById('main2'), 'vintage');
  402. $.get('./data/les-miserables.gexf', function (xml) {
  403. var graph = gexf.parse(xml);
  404. var categories = [];
  405. for (var i = 0; i < 9; i++) {
  406. categories[i] = {
  407. name: '类目' + i
  408. };
  409. }
  410. graph.nodes.forEach(function (node) {
  411. delete node.itemStyle;
  412. node.value = node.symbolSize;
  413. node.label = {
  414. normal: {
  415. show: node.symbolSize > 30
  416. },
  417. emphasis: {
  418. show: true
  419. }
  420. };
  421. node.category = node.attributes['modularity_class'];
  422. });
  423. graph.links.forEach(function (link) {
  424. delete link.lineStyle;
  425. });
  426. var option = {
  427. tooltip: {},
  428. legend: [{
  429. width: '70%',
  430. // selectedMode: 'single',
  431. data: categories.map(function (a) {
  432. return a.name;
  433. })
  434. }],
  435. animationDurationUpdate: 1500,
  436. animationEasingUpdate: 'quinticInOut',
  437. brush: {
  438. },
  439. series : [
  440. {
  441. name: 'Les Miserables',
  442. type: 'graph',
  443. layout: 'none',
  444. data: graph.nodes,
  445. links: graph.links,
  446. categories: categories,
  447. roam: true,
  448. draggable: true,
  449. itemStyle: {
  450. normal: {
  451. borderColor: '#fff',
  452. borderWidth: 2,
  453. shadowBlur: 10,
  454. shadowColor: 'rgba(0, 0, 0, 0.3)'
  455. }
  456. },
  457. focusNodeAdjacency: true,
  458. // edgeSymbol: ['none', 'arrow'],
  459. // scaleLimit: {
  460. // min: 1.5,
  461. // max: 2
  462. // },
  463. label: {
  464. normal: {
  465. position: 'right',
  466. formatter: '{b}'
  467. }
  468. },
  469. lineStyle: {
  470. normal: {
  471. color: 'source',
  472. curveness: 0.3
  473. }
  474. }
  475. }
  476. ]
  477. };
  478. chart.setOption(option);
  479. var config = {
  480. layout: 'none'
  481. };
  482. chart.on('click', function (params) {
  483. console.log(params, params.data);
  484. });
  485. });
  486. });
  487. </script>
  488. </body>
  489. </html>