PieChart.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div :class="className" :style="{height:height,width:width}" />
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. require('echarts/theme/macarons') // echarts theme
  7. import resize from './mixins/resize'
  8. const animationDuration = 3000
  9. export default {
  10. mixins: [resize],
  11. props: {
  12. orderData: {
  13. type: Array,
  14. default: function() {
  15. return []
  16. }
  17. },
  18. className: {
  19. type: String,
  20. default: 'chart'
  21. },
  22. width: {
  23. type: String,
  24. default: '100%'
  25. },
  26. height: {
  27. type: String,
  28. default: '80%'
  29. }
  30. },
  31. data() {
  32. return {
  33. chart: null
  34. }
  35. },
  36. watch: {
  37. orderData: {
  38. handler: function(el) { // 监听对象的变换使用 function,箭头函数容易出现this指向不正确
  39. this.initChart(el)
  40. },
  41. deep: true
  42. }
  43. },
  44. created() {
  45. this.$nextTick(() => {
  46. this.initChart(this.orderData)
  47. })
  48. },
  49. beforeDestroy() {
  50. if (!this.chart) {
  51. return
  52. }
  53. this.chart.dispose()
  54. this.chart = null
  55. },
  56. methods: {
  57. initChart(data) {
  58. this.chart = echarts.init(this.$el, 'macarons')
  59. this.chart.setOption({
  60. tooltip: {
  61. trigger: 'hideTip',
  62. formatter: '{a} <br/>{b} : {c} ({d}%)'
  63. },
  64. color: ['#417DE6', '#E6A441', '#E67441'],
  65. legend: {
  66. // orient: 'vertical',
  67. // top: 'middle',
  68. bottom: 0,
  69. right: 0,
  70. data: ['已收款订单', '未收款订单', '部分收款订单']
  71. },
  72. series: [{
  73. name: '订单数量',
  74. type: 'pie',
  75. radius: ['50%', '70%'],
  76. avoidLabelOverlap: false,
  77. center: ['50%', '50%'],
  78. selectedMode: 'single',
  79. roseType: false,
  80. data: data,
  81. label: {
  82. fontSize: 14,
  83. color: '#7C8894',
  84. lineHeight: 22,
  85. formatter: function(param) {
  86. return param.name + '(' + Math.round(param.percent) + '%' + ')' + '\n' + param.value + '个'
  87. }
  88. },
  89. labelLine: {
  90. smooth: false,
  91. lineStyle: {
  92. width: 2
  93. }
  94. },
  95. itemStyle: {
  96. color: function(params) {
  97. switch (params.name) {
  98. case '已收款订单':
  99. return '#417DE6'
  100. case '未收款订单':
  101. return '#E6A441'
  102. case '部分收款订单':
  103. return '#E67441'
  104. default:
  105. break
  106. }
  107. }
  108. },
  109. animationDuration: animationDuration
  110. }]
  111. })
  112. }
  113. }
  114. }
  115. </script>