RingChart.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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'); // echarts 主题
  7. import resize from './mixins/resize';
  8. export default {
  9. mixins: [resize],
  10. props: {
  11. className: {
  12. type: String,
  13. default: 'chart',
  14. },
  15. width: {
  16. type: String,
  17. default: '100%',
  18. },
  19. height: {
  20. type: String,
  21. default: '350px',
  22. },
  23. chartData: {
  24. type: Array,
  25. required: true
  26. }
  27. },
  28. data() {
  29. return {
  30. chart: null,
  31. };
  32. },
  33. watch: {
  34. chartData: {
  35. deep: true,
  36. handler(val) {
  37. this.setOptions(val);
  38. }
  39. }
  40. },
  41. mounted() {
  42. this.$nextTick(() => {
  43. this.initChart();
  44. });
  45. },
  46. beforeDestroy() {
  47. if (!this.chart) {
  48. return;
  49. }
  50. this.chart.dispose();
  51. this.chart = null;
  52. },
  53. methods: {
  54. initChart() {
  55. this.chart = echarts.init(this.$el, 'macarons');
  56. this.setOptions(this.chartData);
  57. },
  58. setOptions(chartData) {
  59. // 格式化数据
  60. const data = chartData.map(item => ({
  61. value: item.counts, // 将 counts 作为 value
  62. name: item.typeName // 将 typeName 作为 name
  63. }));
  64. this.chart.setOption({
  65. tooltip: {
  66. trigger: 'item',
  67. formatter: '{a} <br/>{b} : {c} ({d}%)',
  68. },
  69. legend: {
  70. left: 'center',
  71. bottom: '10',
  72. data: data.map(item => item.name) // 使用提取的 typeName 作为图例
  73. },
  74. series: [
  75. {
  76. name: '会员类型', // 可以根据需要修改名称
  77. type: 'pie',
  78. radius: ['45%', '60%'], // 环形图,使用内外径
  79. center: ['50%', '42%'],
  80. data: data, // 使用提取的数据
  81. label: {
  82. show: true,
  83. //formatter: '{b}: {c} ({d}%)',
  84. formatter: '{b}: {c} 人',
  85. },
  86. labelLine: {
  87. show: true,
  88. },
  89. animationEasing: 'cubicInOut',
  90. animationDuration: 2600,
  91. },
  92. ],
  93. });
  94. },
  95. },
  96. };
  97. </script>
  98. <style scoped>
  99. .chart {
  100. width: 100%; /* 或者设置为具体的像素值 */
  101. height: 300px; /* 确保有高度 */
  102. }
  103. </style>