PieChart3.vue 3.8 KB

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