Plat Admin

PieChart.vue 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div :class="className" :style="{height:height,width:width}"></div>
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. require('echarts/theme/macarons')
  7. import { debounce } from './chart'
  8. export default {
  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: '350px'
  21. },
  22. chartData: {
  23. type: Object
  24. }
  25. },
  26. data() {
  27. return {
  28. chart: null
  29. }
  30. },
  31. mounted() {
  32. this.initChart()
  33. this.__resizeHanlder = debounce(() => {
  34. if (this.chart) {
  35. this.chart.resize()
  36. }
  37. }, 100)
  38. window.addEventListener('resize', this.__resizeHanlder)
  39. },
  40. beforeDestroy() {
  41. if (!this.chart) {
  42. return
  43. }
  44. window.removeEventListener('resize', this.__resizeHanlder)
  45. this.chart.dispose()
  46. this.chart = null
  47. },
  48. watch: {
  49. chartData: {
  50. deep: true,
  51. handler(val) {
  52. this.setOptions(val)
  53. }
  54. }
  55. },
  56. methods: {
  57. setOptions(datastr) {
  58. this.chart.setOption({
  59. title: {
  60. text: '合计:' + datastr.all,
  61. textStyle: {
  62. color: '#333',
  63. fontSize: 14
  64. },
  65. left: 'center',
  66. top: '8%'
  67. },
  68. toolbox: {
  69. show: true,
  70. feature: {
  71. dataView: { readOnly: false },
  72. restore: {},
  73. saveAsImage: {}
  74. }
  75. },
  76. tooltip: {
  77. trigger: 'item',
  78. formatter: '{a} <br/>{b} : {c} ({d}%)'
  79. },
  80. legend: {
  81. data: ['未激活企业', '激活企业', '合计']
  82. },
  83. series: [
  84. {
  85. name: '企业状态',
  86. type: 'pie',
  87. radius: '65%',
  88. center: ['50%', '56%'],
  89. data: [
  90. {
  91. name: '未激活企业',
  92. value: datastr.unactived
  93. },
  94. {
  95. name: '激活企业',
  96. value: datastr.actived
  97. }
  98. ],
  99. itemStyle: {
  100. emphasis: {
  101. shadowBlur: 10,
  102. shadowOffsetX: 0,
  103. shadowColor: 'rgba(0, 0, 0, 0.5)'
  104. },
  105. normal: {
  106. label: {
  107. show: true,
  108. formatter: '{b} : {c} ({d}%)'
  109. },
  110. labelLine: { show: true }
  111. }
  112. }
  113. }
  114. ]
  115. })
  116. },
  117. initChart() {
  118. this.chart = echarts.init(this.$el) // , 'macarons'
  119. var that = this
  120. setTimeout(function() {
  121. that.setOptions(that.chartData)
  122. }, 1)
  123. }
  124. }
  125. }
  126. </script>