LineChart.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <div>
  3. <el-row :gutter="5">
  4. <el-col :span="14">
  5. <el-card>
  6. <div id="main" ref="line" style="height: 400px;"></div>
  7. </el-card>
  8. </el-col>
  9. <el-col :span="10">
  10. <el-card>
  11. <div class="grid-content ep-bg-purple-light" style="background-color:#9b9da1; height: 400px;">占个位置</div>
  12. </el-card>
  13. </el-col>
  14. </el-row>
  15. </div>
  16. </template>
  17. <script>
  18. import { onMounted, onBeforeUnmount, ref, } from 'vue'
  19. import * as echarts from 'echarts'
  20. export default {
  21. props: ['chartData'],
  22. setup(props, context) {
  23. let lineChart = ref()
  24. let line = ref()
  25. onMounted(() => {
  26. lineChart = echarts.init(document.getElementById('main'))
  27. updateChart()
  28. // updateChart()
  29. setTimeout(()=>{
  30. resizeChart()
  31. }, 0)
  32. window.addEventListener('resize', resizeChart) // 监听窗口大小变化,调整图表大小
  33. })
  34. onBeforeUnmount(() => {
  35. window.removeEventListener('resize', resizeChart) // 在组件销毁前移除事件监听,避免内存泄漏
  36. })
  37. // 获取图像数据 lieChartData
  38. let lieChartData = props.chartData
  39. console.log('lieChartData', lieChartData)
  40. function updateChart() {
  41. const option = {
  42. tooltip: {
  43. trigger: 'axis',
  44. axisPointer: {
  45. type: 'cross',
  46. label: {
  47. backgroundColor: '#6a7985'
  48. }
  49. }
  50. },
  51. // legend: {data: ['数据1', '数据2'], right: '50%',left: '50%'},
  52. toolbox: {
  53. feature: {
  54. saveAsImage: { yAxisIndex: 'none' }
  55. }
  56. },
  57. grid: {
  58. top: 70, right: 60, bottom: 30, left: 55,
  59. },
  60. xAxis: [
  61. {
  62. type: 'category',
  63. boundaryGap: false,
  64. data: lieChartData.xData
  65. }
  66. ],
  67. yAxis: [
  68. {
  69. type: 'value',
  70. // name: yTitle1,
  71. splitLine: {
  72. show: true // 设置显示分割线
  73. },
  74. axisLabel: {
  75. formatter: '{value} 单位1'
  76. }
  77. },
  78. {
  79. type: 'value',
  80. // name: yTitle2,
  81. splitLine: {
  82. show: false
  83. },
  84. axisLabel: {
  85. formatter: '{value} 单位2'
  86. }
  87. }
  88. ],
  89. series: [
  90. {
  91. name: '数据1',
  92. type: 'line',
  93. symbolSize: 6,
  94. symbol: 'circle',
  95. smooth: true,
  96. data: lieChartData.yData1,
  97. yAxisIndex: 0,
  98. lineStyle: { color: '#fe9a8b' },
  99. itemStyle: { color: '#fe9a8b', borderColor: '#fe9a8b' },
  100. areaStyle: {
  101. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  102. { offset: 0, color: '#fe9a8bb3' },
  103. { offset: 1, color: '#fe9a8b03' },
  104. ]),
  105. },
  106. },
  107. {
  108. name: '数据2',
  109. type: 'line',
  110. symbolSize: 6,
  111. symbol: 'circle',
  112. smooth: true,
  113. data: lieChartData.yData2,
  114. yAxisIndex: 1,
  115. lineStyle: { color: '#9E87FF' },
  116. itemStyle: { color: '#9E87FF', borderColor: '#9E87FF' },
  117. areaStyle: {
  118. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  119. { offset: 0, color: '#9E87FFb3' },
  120. { offset: 1, color: '#9E87FF03' },
  121. ]),
  122. },
  123. emphasis: {
  124. itemStyle: {
  125. color: {
  126. type: 'radial',
  127. x: 0.5,
  128. y: 0.5,
  129. r: 0.5,
  130. colorStops: [
  131. { offset: 0, color: '#9E87FF' },
  132. { offset: 0.4, color: '#9E87FF' },
  133. { offset: 0.5, color: '#fff' },
  134. { offset: 0.7, color: '#fff' },
  135. { offset: 0.8, color: '#fff' },
  136. { offset: 1, color: '#fff' },
  137. ],
  138. },
  139. borderColor: '#9E87FF',
  140. borderWidth: 2,
  141. },
  142. },
  143. },
  144. ],
  145. }
  146. lineChart.setOption(option)
  147. }
  148. // 自适应调整窗口
  149. function resizeChart() {
  150. if (lineChart) {
  151. lineChart.resize()
  152. }
  153. }
  154. return { }
  155. }
  156. }
  157. // let props = defineProps({
  158. // chartData: {
  159. // type: Object,
  160. // required: true,
  161. // },
  162. // })
  163. // let { title, xData, yData1, yData2 } = props.chartData // 将需要传入的数据解构出来方便在option中调用
  164. </script>
  165. <style scoped lang="scss">
  166. </style>