PieChart.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div ref="chart" style="width:100%;height:385px;" />
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. import { GetDataByName } from '@/api/common'
  7. import draggable from 'vuedraggable'
  8. import Cookies from 'js-cookie'
  9. import { parseTime } from '@/utils/index.js'
  10. import Pagination from '@/components/Pagination'
  11. require('echarts/theme/macarons')
  12. export default {
  13. name: 'PieChart',
  14. props: {
  15. chartData: {
  16. type: Object,
  17. required: true
  18. },
  19. // chartOpt: {
  20. // type: Object,
  21. // required: false
  22. // },
  23. },
  24. data() {
  25. return {
  26. chart: null
  27. }
  28. },
  29. watch: {
  30. chartData: {
  31. deep: true,
  32. handler: function (chartData) {
  33. this.renderChart(chartData)
  34. }
  35. },
  36. // schema: {
  37. // deep: true,
  38. // handler: function () {
  39. // this.renderChart(this.data)
  40. // }
  41. // }
  42. },
  43. mounted() {
  44. this.renderChart(this.chartData)
  45. this.$on('resized', this.handleResize)
  46. window.addEventListener('resize', this.handleResize)
  47. },
  48. // created() {
  49. // this.renderChart(this.chartData)
  50. // },
  51. beforeDestroy() {
  52. if (this.chart) {
  53. this.chart.dispose()
  54. }
  55. window.removeEventListener('resize', this.handleResize)
  56. },
  57. methods: {
  58. handleResize() {
  59. if (this.chart) {
  60. this.chart.resize()
  61. }
  62. },
  63. renderChart(chartData) {
  64. const option = {
  65. title: { text: 'Stacked Line' },
  66. tooltip: { trigger: 'axis' },
  67. legend: { data:chartData.legendArr, right: 10, show: true, type: 'scroll' },
  68. grid: { top: '15%', left: '8%', right: '8%', containLabel: true },
  69. xAxis: [{ type: 'category',data:chartData.xAxisArr }],
  70. yAxis: [
  71. {type: 'value', name: 'kg'},
  72. ],
  73. series: function (e) {
  74. var serie = [
  75. {
  76. name: 'Access From',
  77. type: 'pie',
  78. radius: '50%',
  79. data: [
  80. // { value: 1048, name: 'Search Engine' },
  81. // { value: 735, name: 'Direct' },
  82. // { value: 580, name: 'Email' },
  83. // { value: 484, name: 'Union Ads' },
  84. // { value: 300, name: 'Video Ads' }
  85. ],
  86. emphasis: {
  87. itemStyle: {
  88. shadowBlur: 10,
  89. shadowOffsetX: 0,
  90. shadowColor: 'rgba(0, 0, 0, 0.5)'
  91. }
  92. }
  93. }
  94. ];
  95. for (var i = 0; i < chartData.xAxisArr.length; i++) {
  96. console.log(i)
  97. var item = {
  98. name: chartData.legendArr[i],
  99. value: chartData.dataArr[i],
  100. }
  101. serie[0].data.push(item);
  102. }
  103. console.log("饼图:",serie)
  104. return serie;
  105. }()
  106. }
  107. setTimeout(() => {
  108. if (!this.chart) {
  109. this.chart = echarts.init(this.$refs.chart, 'macarons')
  110. }
  111. this.chart.clear()
  112. this.chart.setOption(option)
  113. // if (this.chartOpt) {
  114. // this.chart.setOption(this.chartOpt)
  115. // }
  116. this.chart.setOption(this.chartOpt)
  117. }, 0)
  118. }
  119. }
  120. }
  121. </script>