ComStatisticPieChart.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class="chart" ref="chart"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. export default {
  7. name: 'statisticPieChart', //统计饼图
  8. props: {
  9. chartData: {}
  10. },
  11. watch: {
  12. chartData: {
  13. handler(n, o) {
  14. this.$nextTick(() => {
  15. this.initialChart()
  16. })
  17. },
  18. deep: true,
  19. immediate: true
  20. }
  21. },
  22. methods: {
  23. initialChart() {
  24. const data = this.chartData
  25. const color = [
  26. 'rgba(17, 156, 255, 1)',
  27. 'rgba(232, 190, 76, 1)',
  28. 'rgba(21, 208, 234, 1)',
  29. 'rgba(6, 174, 97, 1)',
  30. 'rgba(212, 112, 13, 1)',
  31. 'rgba(175, 45, 230, 1)'
  32. ]
  33. const color2 = [
  34. 'rgba(17, 156, 255, 0.15)',
  35. 'rgba(232, 190, 76, 0.15)',
  36. 'rgba(21, 208, 234, 0.15)',
  37. 'rgba(6, 174, 97, 0.15)',
  38. 'rgba(212, 112, 13, 0.15)',
  39. 'rgba(175, 45, 230, 0.15)'
  40. ]
  41. let total = 0
  42. data.map((item) => {
  43. total += item.value
  44. })
  45. // 设置数据
  46. function setChartOption(data) {
  47. const formatData = []
  48. data.forEach(function (item, index) {
  49. formatData.push(
  50. {
  51. value: item.value,
  52. name: item.name,
  53. label: {
  54. show: false
  55. },
  56. labelLine: {
  57. show: false
  58. },
  59. emphasis: {
  60. disabled: true,
  61. labelLine: {
  62. show: false
  63. }
  64. },
  65. itemStyle: {
  66. color: color[index],
  67. shadowBlur: 5,
  68. shadowColor: color[index]
  69. }
  70. },
  71. {
  72. value: 0.2,
  73. name: '',
  74. itemStyle: {
  75. color: 'rgba(0, 0, 0, 0)',
  76. borderColor: 'rgba(0, 0, 0, 0)',
  77. borderWidth: 0
  78. }
  79. }
  80. )
  81. })
  82. return formatData
  83. }
  84. // 设置阴影数据
  85. function setShadowOption(data) {
  86. const formatData = []
  87. data.forEach(function (item, index) {
  88. formatData.push(
  89. {
  90. value: item.value,
  91. name: item.name,
  92. label: {
  93. show: false
  94. },
  95. labelLine: {
  96. show: false
  97. },
  98. itemStyle: {
  99. color: color2[index],
  100. shadowBlur: 20,
  101. shadowColor: color2[index]
  102. }
  103. },
  104. {
  105. value: 0.2,
  106. name: '',
  107. itemStyle: {
  108. color: 'rgba(0, 0, 0, 0)',
  109. borderColor: 'rgba(0, 0, 0, 0)',
  110. borderWidth: 0
  111. }
  112. }
  113. )
  114. })
  115. return formatData
  116. }
  117. let option = {
  118. backgroundColor: 'transparent',
  119. title: {
  120. text: `{a|${total}}` + '{b|个}\n' + '{c|项目总数}',
  121. top: 'center',
  122. left: '29%',
  123. textAlign: 'center',
  124. textStyle: {
  125. rich: {
  126. a: { fontSize: this.$listeners.fontSize(24), color: 'rgba(255, 255, 255, 1)' },
  127. b: { fontSize: this.$listeners.fontSize(20), color: 'rgba(255, 255, 255, 1)' },
  128. c: { fontSize: this.$listeners.fontSize(14), color: 'rgba(0,141,236,0.9)' }
  129. }
  130. }
  131. },
  132. tooltip: {
  133. trigger: 'item',
  134. textStyle: {
  135. color: '#fff'
  136. },
  137. borderWidth: 0,
  138. backgroundColor: 'rgba(50,50,50,0.7)'
  139. },
  140. legend: {
  141. type: 'scroll',
  142. orient: 'vertical',
  143. itemWidth: 8,
  144. itemHeight: 8,
  145. itemGap: 30,
  146. icon: 'react',
  147. left: '60%',
  148. top: 'center',
  149. textStyle: {
  150. rich: {
  151. a: {
  152. fontSize: this.$listeners.fontSize(14),
  153. color: '#fff'
  154. },
  155. b: {
  156. fontSize: this.$listeners.fontSize(16),
  157. fontWeight: 'bold',
  158. color: '#fff'
  159. }
  160. }
  161. },
  162. formatter: function (name) {
  163. //图例后添加数值
  164. let data = option.series[0].data
  165. let tarValue
  166. for (let i = 0; i < data.length; i++) {
  167. if (data[i].name === name) {
  168. tarValue = data[i].value
  169. }
  170. }
  171. return '{a|' + name + '}' + ' ' + '{b|' + tarValue + '个}'
  172. },
  173. data: data
  174. },
  175. series: [
  176. {
  177. name: '',
  178. type: 'pie',
  179. radius: ['60%', '70%'],
  180. center: ['30%', '50%'],
  181. data: setChartOption(data)
  182. },
  183. {
  184. name: '',
  185. type: 'pie',
  186. radius: ['50%', '60%'],
  187. center: ['30%', '50%'],
  188. hoverAnimation: false,
  189. silent: true,
  190. data: setShadowOption(data)
  191. }
  192. ]
  193. }
  194. this.creatChart(option, this.$refs.chart)
  195. },
  196. creatChart(option, ref) {
  197. let chart = echarts.init(ref) //this.$refs.chart
  198. chart.resize()
  199. chart.setOption(option, {
  200. notMerge: true
  201. })
  202. //图表大小自适应
  203. window.addEventListener('resize', () => {
  204. chart.resize()
  205. })
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss" scoped>
  211. .chart {
  212. height: 100%;
  213. width: 100%;
  214. }
  215. </style>