PieChart2.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <!-- 按等级分布 -->
  3. <div ref="chart2" :style="myChartStyle"></div>
  4. </template>
  5. <script>
  6. import echarts from 'echarts';
  7. export default {
  8. props: {
  9. tableData: {
  10. type: Array,
  11. default: () => []
  12. },
  13. gradeLevels: {
  14. type: Array,
  15. default: () => []
  16. },
  17. },
  18. data() {
  19. return {
  20. myChart: null,
  21. myChartStyle: { width: "100%", height: "300px" },
  22. colorList: [
  23. { c1: ' #40d3ff', c2: '#409EFF' }, { c1: ' #ede737', c2: '#FF9600' }, { c1: '#89e398', c2: '#67c23a' },
  24. { c1: ' #85E9C7', c2: '#00C4CB' }, { c1: ' #f393a9', c2: '#f56c6c' }, { c1: '#e69cf3', c2: '#de43f9' },
  25. ],
  26. }
  27. },
  28. watch: { //此处为关键,监听tableData值的变化,进行echarts渲染
  29. levelStatus(v) {
  30. this.initEcharts2(); //值发生改变则渲染一次echarts
  31. }
  32. },
  33. computed: {
  34. // 考核等级
  35. levelStatus() {
  36. let levelStatus = []
  37. levelStatus = this.gradeLevels.map(item => ({
  38. name: item.name, value: this.tableData.filter(data => data.levelName === item.name).length,
  39. }))
  40. levelStatus.unshift({ name: "未评分", value: this.tableData.filter(data => !data.levelName).length })
  41. return levelStatus
  42. },
  43. },
  44. mounted() {
  45. this.initEcharts2();
  46. },
  47. beforeDestroy() {
  48. if (this.myChart) {
  49. this.myChart.clear();//清空当前实例,会移除实例中所有的组件和图表
  50. this.myChart.dispose();//销毁实例,实例销毁后无法再被使用
  51. }
  52. },
  53. methods: {
  54. initEcharts2() {
  55. const colorList = this.colorList;
  56. const option = {
  57. tooltip: {//悬浮提示组件
  58. trigger: 'item',
  59. },
  60. legend: {
  61. show: true,
  62. x: 'center',
  63. y: 'bottom',
  64. icon: "circle",
  65. itemWidth: 10, // 设置宽度
  66. itemHeight: 10, // 设置高度
  67. itemGap: 30
  68. },
  69. series: [
  70. {
  71. type: "pie",
  72. radius: ['40%', '65%'],//小的是内径,大的是外径
  73. data: this.levelStatus,
  74. itemStyle: {
  75. emphasis: {
  76. shadowBlur: 9,
  77. shadowOffsetX: 0,
  78. shadowColor: 'rgba(0, 0, 0, 0.5)'
  79. },
  80. normal: {
  81. label: {
  82. show: true,
  83. formatter: '{b} : {c}'
  84. },
  85. labelLine: { show: true },
  86. color: function (params) {//颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上
  87. return new echarts.graphic.LinearGradient(0, 1, 0, 0, [{ offset: 0, color: colorList[params.dataIndex].c1 }, { offset: 1, color: colorList[params.dataIndex].c2 }])
  88. }
  89. }
  90. }
  91. }
  92. ]
  93. };
  94. this.myChart = this.$echarts.init(this.$refs.chart2);
  95. this.myChart.setOption(option);
  96. window.addEventListener("resize", () => {
  97. this.myChart.resize();
  98. });
  99. },
  100. }
  101. }
  102. </script>