categoryPieChart.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <view class="overview-chart">
  3. <l-echart ref="chartRef" class="overview-chart__echart"></l-echart>
  4. </view>
  5. </template>
  6. <script setup>
  7. import * as echarts from "echarts";
  8. import { ref, watch, nextTick } from "vue";
  9. const props = defineProps({
  10. chartData: {
  11. type: Array,
  12. default: () => [],
  13. },
  14. });
  15. const chartRef = ref(null);
  16. function buildOption() {
  17. const data = props.chartData.length
  18. ? props.chartData
  19. : [{ name: "电", value: 0, color: "#40b883" }];
  20. return {
  21. color: data.map((item) => item.color),
  22. tooltip: {
  23. trigger: "item",
  24. formatter: "{b}: {c} ({d}%)",
  25. },
  26. legend: {
  27. bottom: 0,
  28. left: "center",
  29. icon: "circle",
  30. itemWidth: 8,
  31. itemHeight: 8,
  32. textStyle: { fontSize: 11, color: "#666" },
  33. },
  34. series: [
  35. {
  36. type: "pie",
  37. radius: ["42%", "68%"],
  38. center: ["50%", "45%"],
  39. avoidLabelOverlap: true,
  40. label: {
  41. show: true,
  42. formatter: "{b}\n{d}%",
  43. fontSize: 11,
  44. color: "#666",
  45. },
  46. labelLine: {
  47. length: 12,
  48. length2: 8,
  49. },
  50. data: data.map((item) => ({
  51. name: item.name,
  52. value: item.value,
  53. })),
  54. },
  55. ],
  56. };
  57. }
  58. function renderChart() {
  59. nextTick(() => {
  60. if (!chartRef.value) return;
  61. chartRef.value.init(echarts, (instance) => {
  62. instance.setOption(buildOption(), true);
  63. });
  64. });
  65. }
  66. watch(() => props.chartData, renderChart, { deep: true, immediate: true });
  67. </script>
  68. <style lang="scss" scoped>
  69. .overview-chart {
  70. &__echart {
  71. width: 100%;
  72. height: 480rpx;
  73. }
  74. }
  75. </style>