PieChart.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 { debounce } from '@/utils'
  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: '300px'
  21. }
  22. },
  23. data() {
  24. return {
  25. chart: null,
  26. grid: {
  27. xl: 3,
  28. lg: 6,
  29. md: 6,
  30. sm: 8,
  31. xs: 8,
  32. },
  33. }
  34. },
  35. mounted() {
  36. this.initChart()
  37. this.__resizeHandler = debounce(() => {
  38. if (this.chart) {
  39. this.chart.resize()
  40. }
  41. }, 100)
  42. window.addEventListener('resize', this.__resizeHandler)
  43. },
  44. beforeDestroy() {
  45. if (!this.chart) {
  46. return
  47. }
  48. window.removeEventListener('resize', this.__resizeHandler)
  49. this.chart.dispose()
  50. this.chart = null
  51. },
  52. methods: {
  53. initChart() {
  54. this.chart = echarts.init(this.$el, 'macarons')
  55. this.chart.setOption({
  56. tooltip: {
  57. trigger: 'item',
  58. formatter: '{a} <br/>{b} : {c} ({d}%)'
  59. },
  60. legend: {
  61. left: 'center',
  62. bottom: '10',
  63. data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
  64. },
  65. calculable: true,
  66. series: [
  67. {
  68. name: 'WEEKLY WRITE ARTICLES',
  69. type: 'pie',
  70. roseType: 'radius',
  71. radius: [15, 95],
  72. center: ['50%', '38%'],
  73. data: [
  74. { value: 320, name: 'Industries' },
  75. { value: 240, name: 'Technology' },
  76. { value: 149, name: 'Forex' },
  77. { value: 100, name: 'Gold' },
  78. { value: 59, name: 'Forecasts' }
  79. ],
  80. animationEasing: 'cubicInOut',
  81. animationDuration: 2600
  82. }
  83. ]
  84. })
  85. }
  86. }
  87. }
  88. </script>