PieBarChart.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <div>
  3. <el-row :gutter="5">
  4. <el-col :span="8">
  5. <div>
  6. <!--<span>{{ modelValue }}</span>-->
  7. <TextSelector :modelValue="modelValue" :options="options" style="margin-top: 5px"/>
  8. </div>
  9. <div ref="pie" style="height: 400px;"></div>
  10. </el-col>
  11. <el-col :span="16">
  12. <div style="margin-left: 40%">
  13. <span style="background: rgb(176,234,232); width: 18px; height: 10px; margin-top: 8px; display: inline-block; border-radius: 3px;"></span>
  14. <TextSelector :modelValue="modelValue" :options="options" style="margin-top: 5px; margin-left: 8px;"/>
  15. <span style="background: rgb(234,207,135); width: 18px; height: 10px; margin-top: 8px; margin-left: 20px; display: inline-block; border-radius: 3px;"></span>
  16. <TextSelector :modelValue="modelValue" :options="options" style="margin-top: 5px; margin-left: 8px;"/>
  17. </div>
  18. <div ref="bar" style="height: 400px;"></div>
  19. </el-col>
  20. </el-row>
  21. </div>
  22. </template>
  23. <script setup>
  24. import { onMounted, ref } from "vue"
  25. import * as echarts from "echarts"
  26. import TextSelector from '/@/components/TextSelector/index.vue'
  27. let props = defineProps({
  28. pieBarChartData: {
  29. type: Object,
  30. }
  31. })
  32. let pieChart = ref()
  33. let barChart = ref()
  34. const pie = ref()
  35. const bar = ref()
  36. onMounted(() => {
  37. barChart = echarts.init(bar.value)
  38. pieChart = echarts.init(pie.value)
  39. setChartData()
  40. window.addEventListener('resize', resizeChart) // 监听窗口大小变化,调整图表大小
  41. setTimeout(() => {
  42. resizeChart()
  43. }, 0)
  44. })
  45. function setChartData() {
  46. // 柱状图配置
  47. const option = {
  48. tooltip: {
  49. trigger: 'axis',
  50. axisPointer: {
  51. type: 'cross',
  52. label: {
  53. backgroundColor: '#6a7985'
  54. }
  55. }
  56. },
  57. // legend: {data: ['数据1', '数据2'],},
  58. toolbox: {
  59. feature: {
  60. saveAsImage: { yAxisIndex: 'none' }
  61. }
  62. },
  63. grid: {
  64. top: 70, right: 60, bottom: 30, left: 55,
  65. },
  66. xAxis: [
  67. {
  68. type: 'category',
  69. boundaryGap: true,
  70. data: ['商品', '品类', '关键词-精准', '关键词-广泛', '关键词-词组'],
  71. }
  72. ],
  73. yAxis: [
  74. {
  75. type: 'value',
  76. name: '数据1',
  77. axisLabel: {
  78. formatter: '{value} 单位1'
  79. },
  80. axisLine: {
  81. show: true
  82. }
  83. },
  84. {
  85. type: 'value',
  86. name: '数据2',
  87. splitLine: {
  88. show: false
  89. },
  90. axisLabel: {
  91. formatter: '{value} 单位2'
  92. },
  93. axisLine: {
  94. show: true
  95. }
  96. }
  97. ],
  98. series: [
  99. {
  100. name: '数据1',
  101. type: 'bar',
  102. // tooltip: {
  103. // valueFormatter: function (value) {
  104. // return value + ' ml';
  105. // }
  106. // },
  107. barWidth: '30%',
  108. data: props.pieBarChartData.barData[0],
  109. yAxisIndex: 0,
  110. itemStyle: {
  111. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  112. { offset: 0, color: 'rgba(111, 209, 206, 0.8)' },
  113. { offset: 1, color: 'rgba(111, 209, 206, 0.1)' },
  114. ]),
  115. // 柱状图圆角
  116. borderRadius: [15, 15, 0, 0],
  117. },
  118. },
  119. {
  120. name: '数据2',
  121. type: 'bar',
  122. barWidth: '30%',
  123. data: props.pieBarChartData.barData[1],
  124. yAxisIndex: 1,
  125. itemStyle: {
  126. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  127. { offset: 0, color: '#ebb14d' },
  128. { offset: 1, color: 'rgba(111, 209, 206, 0.1)' },
  129. ]),
  130. // 柱状图圆角
  131. borderRadius: [15, 15, 0, 0],
  132. },
  133. },
  134. ],
  135. }
  136. barChart.setOption(option)
  137. // 饼图配置
  138. const option2 = {
  139. tooltip: {
  140. trigger: 'item',
  141. },
  142. series: [
  143. {
  144. name: 'Access From',
  145. type: 'pie',
  146. radius: ['25%', '50%'],
  147. avoidLabelOverlap: false,
  148. itemStyle: {
  149. borderWidth: 1, // 设置边框的宽度
  150. borderColor: '#fff', // 将边框颜色设置为白色或图表背景颜色
  151. },
  152. emphasis: {
  153. label: {
  154. show: true,
  155. // fontSize: 40,
  156. fontWeight: 'bold'
  157. }
  158. },
  159. label: {
  160. normal: {
  161. show: true,
  162. position: 'outside',
  163. // formatter: '{b}: {c} ({d}%)' // b为数据名,c为数据值,d为百分比
  164. }
  165. },
  166. labelLine: {
  167. normal: {
  168. show: true
  169. }
  170. },
  171. data: props.pieBarChartData.pieData
  172. }
  173. ]
  174. }
  175. pieChart.setOption(option2)
  176. resizeChart()
  177. }
  178. function resizeChart() {
  179. barChart.resize()
  180. pieChart.resize()
  181. }
  182. defineExpose({ resizeChart })
  183. // 下拉框相关
  184. const options = [
  185. {
  186. value: '花费',
  187. label: '花费',
  188. },
  189. {
  190. value: 'Option2',
  191. label: 'Option2',
  192. },
  193. {
  194. value: 'Option3',
  195. label: 'Option3',
  196. }
  197. ]
  198. const modelValue = ref(options[0].value)
  199. </script>
  200. <style scoped>
  201. </style>