demo.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <view>
  3. <view @click="echarts.onClick" :change:prop="echarts.updateEcharts" id="echarts" class="echarts">
  4. </view>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {}
  11. },
  12. onLoad() {},
  13. methods: {
  14. onViewClick(options) {
  15. console.log(options)
  16. }
  17. }
  18. }
  19. </script>
  20. <script module="echarts" lang="renderjs">
  21. let myChart
  22. export default {
  23. mounted() {
  24. if (typeof window.echarts === 'function') {
  25. this.initEcharts()
  26. } else {
  27. // 动态引入较大类库避免影响页面展示
  28. const script = document.createElement('script')
  29. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  30. script.src = 'static/echarts.min.js'
  31. script.onload = this.initEcharts.bind(this)
  32. document.head.appendChild(script)
  33. }
  34. },
  35. methods: {
  36. initEcharts() {
  37. myChart = echarts.init(document.getElementById('echarts'))
  38. // 观测更新的数据在 view 层可以直接访问到
  39. var data1 = [];
  40. var data2 = [];
  41. var data3 = [];
  42. var random = function(max) {
  43. return (Math.random() * max).toFixed(3);
  44. };
  45. for (var i = 0; i < 500; i++) {
  46. data1.push([random(15), random(10), random(1)]);
  47. data2.push([random(10), random(10), random(1)]);
  48. data3.push([random(15), random(10), random(1)]);
  49. }
  50. let option = {
  51. animation: false,
  52. legend: {
  53. data: ['scatter', 'scatter2', 'scatter3']
  54. },
  55. tooltip: {},
  56. xAxis: {
  57. type: 'value',
  58. min: 'dataMin',
  59. max: 'dataMax',
  60. splitLine: {
  61. show: true
  62. },
  63. axisLabel: {
  64. interval: 60, // 自定义显示X轴坐标显示间隔
  65. textStyle: {
  66. color: '#626262',
  67. fontSize: '10',
  68. },
  69. formatter:(value, index)=> {
  70. console.log(value)
  71. return value + 'kg';
  72. }
  73. }
  74. },
  75. yAxis: {
  76. type: 'value',
  77. min: 'dataMin',
  78. max: 'dataMax',
  79. splitLine: {
  80. show: true
  81. }
  82. },
  83. dataZoom: [{
  84. type: 'slider',
  85. show: true,
  86. xAxisIndex: [0],
  87. start: 1,
  88. end: 35
  89. },
  90. {
  91. type: 'slider',
  92. show: true,
  93. yAxisIndex: [0],
  94. left: '93%',
  95. start: 29,
  96. end: 36
  97. },
  98. {
  99. type: 'inside',
  100. xAxisIndex: [0],
  101. start: 1,
  102. end: 35
  103. },
  104. {
  105. type: 'inside',
  106. yAxisIndex: [0],
  107. start: 29,
  108. end: 36
  109. }
  110. ],
  111. series: [{
  112. name: 'scatter',
  113. type: 'scatter',
  114. itemStyle: {
  115. normal: {
  116. opacity: 0.8
  117. }
  118. },
  119. symbolSize: function(val) {
  120. return val[2] * 40;
  121. },
  122. data: data1
  123. },
  124. {
  125. name: 'scatter2',
  126. type: 'scatter',
  127. itemStyle: {
  128. normal: {
  129. opacity: 0.8
  130. }
  131. },
  132. symbolSize: function(val) {
  133. return val[2] * 40;
  134. },
  135. data: data2
  136. },
  137. {
  138. name: 'scatter3',
  139. type: 'scatter',
  140. itemStyle: {
  141. normal: {
  142. opacity: 0.8
  143. }
  144. },
  145. symbolSize: function(val) {
  146. return val[2] * 40;
  147. },
  148. data: data3
  149. }
  150. ]
  151. }
  152. myChart.setOption(option)
  153. },
  154. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  155. // 监听 service 层数据变更
  156. myChart.setOption(newValue)
  157. },
  158. onClick(event, ownerInstance) {
  159. // 调用 service 层的方法
  160. ownerInstance.callMethod('onViewClick', {
  161. test: 'test'
  162. })
  163. }
  164. }
  165. }
  166. </script>
  167. <style>
  168. .content {
  169. display: flex;
  170. flex-direction: column;
  171. align-items: center;
  172. justify-content: center;
  173. }
  174. .echarts {
  175. margin-top: 100px;
  176. width: 100%;
  177. height: 300px;
  178. }
  179. </style>