questionChart.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <div class="pie-chart" ref="pieChart" style="height: 100%;width: 100%;"></div>
  3. </template>
  4. <script>
  5. export default {
  6. name:"questionChart",
  7. props:{
  8. chartData:{
  9. required:true,
  10. type:Array
  11. }
  12. },
  13. data() {
  14. return {
  15. options: {
  16. tooltip: {
  17. trigger: 'item',
  18. formatter:'{b0}:{c0}%'
  19. },
  20. legend: {
  21. bottom: '5%',
  22. left: 'center',
  23. icon:'circle'
  24. },
  25. series: [
  26. {
  27. type: 'pie',
  28. center: ['50%', '40%'],
  29. radius: ['45%', '70%'],
  30. avoidLabelOverlap: false,
  31. label: {
  32. show: false,
  33. position: 'center'
  34. },
  35. itemStyle: {
  36. borderColor: '#fff',
  37. borderWidth: 2
  38. },
  39. emphasis: {
  40. label: {
  41. show: true,
  42. fontSize: '40',
  43. fontWeight: 'bold',
  44. },
  45. },
  46. labelLine: {
  47. show: false,
  48. },
  49. data:[]
  50. },
  51. ],
  52. color: ["#0F80E7",'#61DCF3','#60E184','#7A4DDA','#F7E701','#34C8A4','#FFBB37','#E241EE','#FF9559','#FF8CE9',
  53. '#FF5BAA','#E75300','#B8ED75','#3AEBC0','#A1F8FF','#03FFD2','#A56FF8','#F0960E','#2CB2EC','#63B0FF'],
  54. },
  55. chartData:[{value:1,name:'A'},{value:4,name:'B'},{value:5,name:'C'}]
  56. }
  57. },
  58. methods:{
  59. drawChart() {
  60. const chart = this.$refs.pieChart;
  61. if (chart) {
  62. const myChart = echarts.init(chart);
  63. this.options.series[0].data = this.chartData
  64. myChart.setOption(this.options);
  65. // '禁止'点击图例后的默认行为
  66. myChart.on('legendselectchanged', ({ selected, name }) => {
  67. selected[name] = true
  68. myChart.setOption({ legend: { selected } })
  69. })
  70. window.addEventListener('resize', function () {
  71. myChart.resize();
  72. });
  73. this.$on('hook:destroyed', () => {
  74. window.removeEventListener('resize', function () {
  75. myChart.resize();
  76. });
  77. });
  78. }
  79. },
  80. },
  81. mounted() {
  82. this.drawChart();
  83. },
  84. }
  85. </script>
  86. <style lang="scss" scoped>
  87. </style>