BarLineChart.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <div ref="barLine" style="height: 400px;"></div>
  3. </template>
  4. <script setup>
  5. import {onMounted, onBeforeUnmount, ref} from 'vue'
  6. import * as echarts from 'echarts'
  7. const props = defineProps({
  8. barLineData: {type: Object}
  9. })
  10. let lineChart = ref()
  11. const barLine = ref()
  12. onMounted(() => {
  13. lineChart = echarts.init(barLine.value)
  14. updateChart()
  15. addResize() // 监听窗口大小变化,调整图表大小
  16. setTimeout(() => {
  17. resizeChart()
  18. }, 0)
  19. })
  20. onBeforeUnmount(() => {
  21. if (lineChart) {
  22. lineChart.dispose()
  23. lineChart = null
  24. }
  25. removeResize() // 在组件销毁前移除事件监听,避免内存泄漏
  26. })
  27. // 获取图像数据 lieChartData
  28. let chartData = props.barLineData
  29. function updateChart() {
  30. const option = {
  31. tooltip: {
  32. trigger: 'axis',
  33. axisPointer: {
  34. type: 'cross',
  35. label: {
  36. backgroundColor: '#6a7985'
  37. }
  38. }
  39. },
  40. // legend: {data: ['数据1', '数据2'], right: '50%',left: '50%'},
  41. // toolbox: {
  42. // feature: {
  43. // saveAsImage: { yAxisIndex: 'none' }
  44. // }
  45. // },
  46. grid: {
  47. top: 50, right: 150, bottom: 30, left: 55,
  48. },
  49. xAxis: [
  50. {
  51. type: 'category',
  52. boundaryGap: true,
  53. data: chartData.xData,
  54. }
  55. ],
  56. yAxis: [
  57. {
  58. type: 'value',
  59. name: 'ACOS',
  60. splitLine: {
  61. show: true // 设置显示分割线
  62. },
  63. axisLabel: {
  64. formatter: '{value} 单位1'
  65. },
  66. axisLine: {
  67. show: true
  68. }
  69. },
  70. {
  71. type: 'value',
  72. name: '点击率',
  73. position: 'right',
  74. splitLine: {
  75. show: false
  76. },
  77. axisLabel: {
  78. formatter: '{value} 单位2'
  79. },
  80. axisLine: {
  81. show: true
  82. }
  83. },
  84. {
  85. type: 'value',
  86. position: 'right',
  87. offset: 80,
  88. name: '订单数',
  89. splitLine: {
  90. show: false
  91. },
  92. axisLabel: {
  93. formatter: '{value} 单位3'
  94. },
  95. axisLine: {
  96. show: true
  97. }
  98. }
  99. ],
  100. series: [
  101. {
  102. name: '柱状图',
  103. type: 'bar',
  104. // tooltip: {
  105. // valueFormatter: function (value) {
  106. // return value + ' ml';
  107. // }
  108. // },
  109. barWidth: '30%',
  110. data: chartData.barData,
  111. yAxisIndex: 0,
  112. itemStyle: {
  113. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  114. {offset: 0, color: 'rgba(111, 209, 206, 0.8)'},
  115. {offset: 1, color: 'rgba(111, 209, 206, 0.1)'},
  116. ]),
  117. //柱状图圆角
  118. borderRadius: [15, 15, 0, 0],
  119. },
  120. },
  121. {
  122. name: '数据1',
  123. type: 'line',
  124. symbolSize: 6,
  125. symbol: 'circle',
  126. smooth: true,
  127. data: chartData.yData1,
  128. yAxisIndex: 1,
  129. lineStyle: {color: '#fe9a8b'},
  130. itemStyle: {color: '#fe9a8b', borderColor: '#fe9a8b'},
  131. areaStyle: {
  132. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  133. {offset: 0, color: '#fe9a8bb3'},
  134. {offset: 1, color: '#fe9a8b03'},
  135. ]),
  136. },
  137. },
  138. {
  139. name: '数据2',
  140. type: 'line',
  141. symbolSize: 6,
  142. symbol: 'circle',
  143. smooth: true,
  144. data: chartData.yData2,
  145. yAxisIndex: 2,
  146. lineStyle: {color: '#9E87FF'},
  147. itemStyle: {color: '#9E87FF', borderColor: '#9E87FF'},
  148. areaStyle: {
  149. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  150. {offset: 0, color: '#9E87FFb3'},
  151. {offset: 1, color: '#9E87FF03'},
  152. ]),
  153. },
  154. emphasis: {
  155. itemStyle: {
  156. color: {
  157. type: 'radial',
  158. x: 0.5,
  159. y: 0.5,
  160. r: 0.5,
  161. colorStops: [
  162. {offset: 0, color: '#9E87FF'},
  163. {offset: 0.4, color: '#9E87FF'},
  164. {offset: 0.5, color: '#fff'},
  165. {offset: 0.7, color: '#fff'},
  166. {offset: 0.8, color: '#fff'},
  167. {offset: 1, color: '#fff'},
  168. ],
  169. },
  170. borderColor: '#9E87FF',
  171. borderWidth: 2,
  172. },
  173. },
  174. },
  175. ],
  176. }
  177. lineChart.setOption(option)
  178. }
  179. // 自适应调整窗口
  180. function resizeChart() {
  181. if (lineChart) {
  182. lineChart.resize()
  183. }
  184. }
  185. function addResize() {
  186. window.addEventListener('resize', resizeChart)
  187. }
  188. function removeResize() {
  189. window.removeEventListener('resize', resizeChart)
  190. }
  191. defineExpose({resizeChart, updateChart})
  192. </script>
  193. <style>
  194. </style>