itemPieChart.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. const DEFAULT_DATA = [
  17. { name: "照明插座系统用电", value: 0, color: "#4a90e2" },
  18. { name: "空调系统用电", value: 0, color: "#40b883" },
  19. { name: "动力系统用电", value: 0, color: "#f5c542" },
  20. { name: "特殊系统用电", value: 0, color: "#ef6b6b" },
  21. ];
  22. function buildOption() {
  23. const data = props.chartData.length ? props.chartData : DEFAULT_DATA;
  24. return {
  25. color: data.map((item) => item.color),
  26. tooltip: {
  27. trigger: "item",
  28. formatter: "{b}: {c} ({d}%)",
  29. },
  30. legend: {
  31. bottom: 0,
  32. left: "center",
  33. width: "92%",
  34. icon: "circle",
  35. itemWidth: 8,
  36. itemHeight: 8,
  37. itemGap: 10,
  38. textStyle: { fontSize: 10, color: "#666" },
  39. },
  40. series: [
  41. {
  42. type: "pie",
  43. radius: ["38%", "58%"],
  44. center: ["50%", "42%"],
  45. avoidLabelOverlap: true,
  46. label: {
  47. show: true,
  48. formatter: "{b}\n{d}%",
  49. fontSize: 10,
  50. color: "#666",
  51. },
  52. labelLine: {
  53. length: 10,
  54. length2: 6,
  55. },
  56. data: data.map((item) => ({
  57. name: item.name,
  58. value: item.value,
  59. })),
  60. },
  61. ],
  62. };
  63. }
  64. function renderChart() {
  65. nextTick(() => {
  66. if (!chartRef.value) return;
  67. chartRef.value.init(echarts, (instance) => {
  68. instance.setOption(buildOption(), true);
  69. });
  70. });
  71. }
  72. watch(() => props.chartData, renderChart, { deep: true, immediate: true });
  73. </script>
  74. <style lang="scss" scoped>
  75. .overview-chart {
  76. &__echart {
  77. width: 100%;
  78. height: 520rpx;
  79. }
  80. }
  81. </style>