PieChart2.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <div :id="id" :class="className" :style="{ height:height,width:width }" />
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. import resize from './mixins/resize'
  7. export default {
  8. mixins: [resize],
  9. props: {
  10. className: {
  11. type: String,
  12. default: "chart"
  13. },
  14. id: {
  15. type: String,
  16. default: "chart"
  17. },
  18. width: {
  19. type: String,
  20. default: "100%"
  21. },
  22. height: {
  23. type: String,
  24. default: "400px"
  25. },
  26. data: {
  27. type: Object,
  28. default: {}
  29. },
  30. },
  31. data() {
  32. return {
  33. chart: null
  34. };
  35. },
  36. watch: {
  37. data: {
  38. handler(newVal, oldVal) {
  39. if (newVal) {
  40. this.initChart();
  41. }
  42. },
  43. deep: true //对象内部属性的监听,关键。
  44. }
  45. },
  46. mounted() {
  47. this.$nextTick(() => {
  48. this.initChart();
  49. })
  50. },
  51. beforeDestroy() {
  52. if (!this.chart) {
  53. return;
  54. }
  55. this.chart.dispose();
  56. this.chart = null;
  57. },
  58. methods: {
  59. initChart() {
  60. let that = this;
  61. this.chart = echarts.init(document.getElementById(this.id));
  62. let legendData = [];
  63. that.data.seriesData.forEach(function(item,index){
  64. if(item.name){
  65. legendData.push(item.name);
  66. }
  67. })
  68. let option = {
  69. tooltip: {
  70. trigger: 'item',
  71. formatter: '{a} <br/>{b} : {c} ({d}%)'
  72. },
  73. color:that.data.color,
  74. legend: {
  75. left: 'right',
  76. top: 'middle',
  77. orient:'vertical',
  78. data: legendData
  79. },
  80. series: [
  81. {
  82. name: '拥有保单件数分布',
  83. type: 'pie',
  84. // roseType: 'radius',
  85. radius: ['30%', '75%'],
  86. center: ['40%', '55%'],
  87. label: {
  88. show:false,
  89. formatter: '{b}:\n{d}%'
  90. },
  91. data: this.data.seriesData,
  92. }
  93. ]
  94. };
  95. this.chart.setOption(option);
  96. }
  97. }
  98. };
  99. </script>