index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <div>
  3. <div id='main'></div>
  4. </div>
  5. </template>
  6. <script>
  7. import * as echarts from 'echarts/core'
  8. import {
  9. DatasetComponent,
  10. TitleComponent,
  11. TooltipComponent,
  12. GridComponent,
  13. TransformComponent,
  14. } from 'echarts/components'
  15. import { LineChart } from 'echarts/charts'
  16. import { LabelLayout, UniversalTransition } from 'echarts/features'
  17. import { CanvasRenderer } from 'echarts/renderers'
  18. echarts.use([
  19. DatasetComponent,
  20. TitleComponent,
  21. TooltipComponent,
  22. GridComponent,
  23. TransformComponent,
  24. LineChart,
  25. CanvasRenderer,
  26. LabelLayout,
  27. UniversalTransition,
  28. ])
  29. export default {
  30. name: 'dataPlayBack',
  31. data() {
  32. return {
  33. //eCharts
  34. myChart: '',
  35. //开始和结束时间
  36. // 查询参数
  37. startTime: '2023-01-02 00:00:00',
  38. endTime: '2023-01-02 06:00:00',
  39. }
  40. },
  41. mounted() {
  42. this.creatECharts()
  43. },
  44. methods: {
  45. creatECharts() {
  46. if (this.myChart) {
  47. this.myChart.dispose()
  48. }
  49. var dom = document.getElementById('main')
  50. this.myChart = echarts.init(dom, null, {
  51. renderer: 'canvas',
  52. useDirtyRect: false,
  53. })
  54. let option
  55. const a = [
  56. ['Income', 'Life Expectancy', 'Population', 'Country', 'Year'],
  57. [615, 34.05, 251014, 'Finland', '2023-01-02 00:00:00'],
  58. [315, 34.05, 251014, 'Finland', '2023-01-02 01:00:00'],
  59. [725, 34.05, 251014, 'Finland', '2023-01-02 02:00:00'],
  60. [315, 34.05, 251014, 'Finland', '2023-01-02 03:00:00'],
  61. [515, 34.05, 251014, 'Finland', '2023-01-02 04:00:00'],
  62. [815, 34.05, 351014, 'Finland', '2023-01-02 05:00:00'],
  63. [1815, 34.05, 351014, 'Finland', '2023-01-02 06:00:00'],
  64. [814, 39, 645526, 'France', '2023-01-02 00:00:00'],
  65. [144, 39, 645526, 'France', '2023-01-02 01:00:00'],
  66. [454, 39, 645526, 'France', '2023-01-02 02:00:00'],
  67. [734, 39, 645526, 'France', '2023-01-02 03:00:00'],
  68. [144, 39, 645526, 'France', '2023-01-02 04:00:00'],
  69. [999, 39, 645526, 'France', '2023-01-02 05:00:00'],
  70. [814, 39, 645526, 'France', '2023-01-02 06:00:00'],
  71. ]
  72. let that = this
  73. run(a)
  74. function run(_rawData) {
  75. const countries = ['Finland', 'France']
  76. const datasetWithFilters = []
  77. const seriesList = []
  78. echarts.util.each(countries, function (country) {
  79. var datasetId = 'dataset_' + country
  80. datasetWithFilters.push({
  81. id: datasetId,
  82. fromDatasetId: 'dataset_raw',
  83. transform: {
  84. type: 'filter',
  85. config: {
  86. and: [
  87. {
  88. dimension: 'Year',
  89. gte: that.startTime,
  90. parser: 'time',
  91. },
  92. {
  93. dimension: 'Year',
  94. lt: that.endTime,
  95. parser: 'time',
  96. },
  97. { dimension: 'Country', '=': country },
  98. ],
  99. },
  100. },
  101. })
  102. seriesList.push({
  103. type: 'line',
  104. datasetId: datasetId,
  105. showSymbol: false,
  106. name: country,
  107. endLabel: {
  108. show: true,
  109. formatter: function (params) {
  110. return params.value[3] + ': ' + params.value[0]
  111. },
  112. },
  113. labelLayout: {
  114. moveOverlap: 'shiftY',
  115. },
  116. emphasis: {
  117. focus: 'series',
  118. },
  119. encode: {
  120. x: 'Year',
  121. y: 'Income',
  122. label: ['Country', 'Income'],
  123. itemName: 'Year',
  124. tooltip: ['Income'],
  125. },
  126. })
  127. })
  128. option = {
  129. animationDuration: that.speed,
  130. dataset: [
  131. {
  132. id: 'dataset_raw',
  133. source: _rawData,
  134. },
  135. ...datasetWithFilters,
  136. ],
  137. title: {
  138. text: '',
  139. },
  140. tooltip: {
  141. order: 'valueDesc',
  142. trigger: 'axis',
  143. },
  144. xAxis: {
  145. type: 'category',
  146. nameLocation: 'middle',
  147. },
  148. yAxis: {
  149. name: 'Income',
  150. },
  151. grid: {
  152. right: 140,
  153. },
  154. series: seriesList,
  155. }
  156. that.myChart.setOption(option)
  157. }
  158. },
  159. }
  160. }
  161. </script>
  162. <style scoped>
  163. #main {
  164. width: 1200px;
  165. height: 500px;
  166. }
  167. </style>