connect-manually.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" href="reset.css"/>
  6. <script src="esl.js"></script>
  7. <script src="config.js"></script>
  8. </head>
  9. <body>
  10. <style>
  11. html, body, #main {
  12. width: 100%;
  13. height: 100%;
  14. margin: 0;
  15. }
  16. #chart1, #chart2 {
  17. width: 100%;
  18. height: 50%;
  19. }
  20. </style>
  21. <div id="main">
  22. <div id="chart1"></div>
  23. <div id="chart2"></div>
  24. </div>
  25. <script>
  26. require([
  27. 'echarts',
  28. 'echarts/chart/line',
  29. 'echarts/chart/scatter',
  30. 'echarts/component/title',
  31. 'echarts/component/legend',
  32. 'echarts/component/dataZoom',
  33. 'echarts/component/grid',
  34. 'echarts/component/tooltip'
  35. ], function (echarts, rawData, prepareBoxplotData, env) {
  36. var chart1 = echarts.init(document.getElementById('chart1'));
  37. var chart2 = echarts.init(document.getElementById('chart2'));
  38. var data1 = [];
  39. var data2 = [];
  40. var timeBase = +new Date();
  41. var hour = 1000 * 60 * 60;
  42. for (var i = 0; i < 100; i++) {
  43. data1.push([timeBase, Math.random() * 4]);
  44. if (i < 40) {
  45. data2.push([timeBase, Math.random() * 14]);
  46. }
  47. timeBase += hour;
  48. }
  49. chart1.setOption({
  50. legend: {
  51. top: 50,
  52. data: ['line']
  53. },
  54. tooltip: {
  55. trigger: 'axis',
  56. formatter: '{c}'
  57. },
  58. grid: {
  59. top: '26%',
  60. bottom: '26%'
  61. },
  62. xAxis: {
  63. type: 'time',
  64. max: timeBase
  65. },
  66. yAxis: {
  67. type: 'value'
  68. },
  69. dataZoom: {
  70. type: 'inside'
  71. },
  72. series: [
  73. {
  74. type: 'line',
  75. data: data1
  76. }
  77. ]
  78. });
  79. chart2.setOption({
  80. legend: {
  81. top: 50,
  82. data: ['line']
  83. },
  84. tooltip: {
  85. trigger: 'axis',
  86. formatter: '{c}'
  87. },
  88. grid: {
  89. top: '26%',
  90. bottom: '26%'
  91. },
  92. xAxis: {
  93. type: 'time',
  94. max: timeBase
  95. },
  96. yAxis: {
  97. type: 'value'
  98. },
  99. dataZoom: {
  100. type: 'inside'
  101. },
  102. series: [
  103. {
  104. type: 'line',
  105. data: data2
  106. }
  107. ]
  108. });
  109. // echarts.connect([chart1, chart2]);
  110. bindAction(chart1, chart2, data2);
  111. bindAction(chart2, chart1, data1);
  112. function bindAction(fromChart, toChart, toData) {
  113. fromChart.on('showTip', function (params) {
  114. // Determine index overflow.
  115. var rawIndex = getRawIndex(fromChart, params.dataIndex);
  116. if (rawIndex < toData.length) {
  117. toChart.dispatchAction({
  118. type: 'showTip',
  119. seriesIndex: params.seriesIndex,
  120. dataIndex: params.dataIndex
  121. }, true);
  122. }
  123. });
  124. fromChart.on('hideTip', function (params) {
  125. toChart.dispatchAction({
  126. type: 'hideTip'
  127. }, true);
  128. });
  129. fromChart.on('dataZoom', function (params) {
  130. toChart.dispatchAction({
  131. type: 'dataZoom',
  132. dataZoomIndex: params.batch[0].dataZoomIndex,
  133. start: params.batch[0].start,
  134. end: params.batch[0].end
  135. }, true);
  136. });
  137. }
  138. function getRawIndex(chart, dataIndex) {
  139. return chart.getModel().getComponent('series').getData().indexOfRawIndex(dataIndex);
  140. }
  141. });
  142. </script>
  143. </body>
  144. </html>