morepieChart.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div :class="className" :style="{height:height,width:width}" />
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts';
  6. import chartMixin from '@/mixins/ChartMixin';
  7. export default {
  8. mixins: [chartMixin],
  9. props: {
  10. className: {
  11. type: String,
  12. default: 'chart'
  13. },
  14. width: {
  15. type: String,
  16. default: '100%'
  17. },
  18. height: {
  19. type: String,
  20. default: '200px'
  21. },
  22. chartData: {
  23. type: Object,
  24. required: true
  25. }
  26. },
  27. data() {
  28. return {
  29. chart: null,
  30. time:null,
  31. }
  32. },
  33. watch: {
  34. chartData: {
  35. deep: true,
  36. handler(val) {
  37. this.setOptions(val)
  38. }
  39. }
  40. },
  41. mounted() {
  42. var that=this;
  43. this.$nextTick(() => {
  44. this.initChart()
  45. that.setOptions(that.chartData)
  46. // this.time=setInterval(()=>{
  47. // that.chart.clear()
  48. // that.setOptions(that.chartData)
  49. // },30000)
  50. })
  51. },
  52. beforeDestroy() {
  53. if (!this.chart) {
  54. return
  55. }
  56. this.chart.dispose()
  57. this.chart = null
  58. if (this.time) {
  59. clearInterval(this.time);
  60. this.time = null;
  61. }
  62. },
  63. methods: {
  64. initChart() {
  65. this.chart = echarts.init(this.$el, 'macarons')
  66. this.setOptions(this.chartData)
  67. },
  68. setOptions({ data,name,num,color} = {}) {
  69. this.chart.setOption({
  70. color:color,
  71. tooltip: {
  72. trigger: 'item'
  73. },
  74. legend: {
  75. show:false,
  76. },
  77. graphic: [{
  78. type: 'text',
  79. left: 'center',
  80. top: 'center',
  81. z: 10,
  82. style: {
  83. fill: color[0],
  84. text: num+'%',
  85. fontSize: 16,
  86. fontWeight: '500'
  87. }
  88. }],
  89. series: [
  90. {
  91. name: name,
  92. type: 'pie',
  93. radius: ['75%', '85%'],
  94. avoidLabelOverlap: false,
  95. label: {
  96. show: false,
  97. position: 'center'
  98. },
  99. emphasis: {
  100. label: {
  101. show: false,
  102. fontSize: 40,
  103. fontWeight: 'bold'
  104. }
  105. },
  106. labelLine: {
  107. show: false
  108. },
  109. data: [
  110. //itemSyle是单项的背景颜色设置。
  111. { value: 50, itemStyle: { color: color[0] } },
  112. { value: 50, itemStyle: { color: '#001428' } },
  113. ],
  114. },
  115. ]
  116. },true)
  117. }
  118. }
  119. }
  120. </script>
  121. <style lang="less" scoped>
  122. </style>