8 小时以前 1c829f9177eab675b22be4d20ef2761ed1bbb423
src/views/reportAnalysis/PSIDataAnalysis/components/left-bottom.vue
@@ -3,8 +3,11 @@
    <PanelHeader title="采购品分布" />
    <div class="main-panel panel-item-customers">
      <CarouselCards :items="cardItems" :visible-count="3" />
      <div class="pie-chart-wrapper" ref="pieWrapperRef">
        <div class="pie-background" ref="pieBackgroundRef"></div>
      <div class="pie-chart-wrapper">
        <div class="pie-center">
          <span class="pie-center-value">{{ totalAmount }}</span>
          <span class="pie-center-label">合计(元)</span>
        </div>
        <Echarts
          ref="chart"
          :chartStyle="chartStyle"
@@ -22,15 +25,12 @@
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount, computed } from 'vue'
import { ref, onMounted, computed } from 'vue'
import Echarts from '@/components/Echarts/echarts.vue'
import PanelHeader from './PanelHeader.vue'
import CarouselCards from './CarouselCards.vue'
import { rawMaterialPurchaseAmountRatio } from '@/api/viewIndex.js'
import { useChartBackground } from '@/hooks/useChartBackground.js'
const pieWrapperRef = ref(null)
const pieBackgroundRef = ref(null)
import { BI, baseChartOptions, withTooltip, formatAmount } from '../theme.js'
/**
 * @introduction 把数组中key值相同的那一项提取出来,组成一个对象
@@ -53,9 +53,13 @@
const cardItems = ref([])
// 颜色列表
const landColors = ['#26FFCB', '#24CBFF', '#35FBF4', '#2651FF', '#D1E4F5', '#5782F7', '#2F67EF', '#82BAFF']
const landColors = BI.series
const landObjData = computed(() => array2obj(dataList.value, 'name'))
const totalAmount = computed(() =>
  formatAmount(dataList.value.reduce((sum, d) => sum + (Number(d.value) || 0), 0))
)
// 图例配置(右侧竖排)
const landLegend = computed(() => {
@@ -63,7 +67,7 @@
    name: d.name,
    icon: 'circle',
    textStyle: {
      fontSize: 18,
      fontSize: 14,
      color: landColors[idx % landColors.length],
    },
  }))
@@ -72,34 +76,34 @@
    orient: 'vertical',
    top: 'center',
    left: '52%',
    itemGap: 30,
    itemGap: 24,
    data: data,
    formatter: function (name) {
      const item = landObjData.value[name]
      if (!item) return name
      return `{title|${name}}{value|${item.value}}{unit|元}{percent|${item.rate}}{unit|%}`
      return `{title|${name}}{value|${formatAmount(item.value)}}{percent|${item.rate}}{unit|%}`
    },
    textStyle: {
      rich: {
        value: {
          color: '#43e8fc',
        title: {
          fontSize: 14,
          fontWeight: 600,
          padding: [0, 0, 0, 10],
        },
        unit: {
          color: '#82baff',
          fontSize: 12,
          fontWeight: 600,
          padding: [0, 10, 0, 0],
        },
        percent: {
          color: '#43e8fc',
          fontSize: 14,
          fontWeight: 600,
          color: BI.text,
          padding: [0, 0, 0, 0],
        },
        title: {
        value: {
          color: BI.textStrong,
          fontSize: 14,
          fontWeight: 600,
          padding: [0, 6, 0, 10],
        },
        percent: {
          color: BI.accentBright,
          fontSize: 14,
          fontWeight: 600,
          padding: [0, 0, 0, 6],
        },
        unit: {
          color: BI.text,
          fontSize: 12,
          padding: [0, 0, 0, 0],
        },
@@ -109,21 +113,22 @@
})
// 提示框
const landTooltip = {
const landTooltip = withTooltip({
  trigger: 'item',
  formatter: '{a} <br/>{b} : {c}元 ({d}%)',
}
})
// 双层环形饼图
const landSeries = ref([
  {
    name: '产品采购金额分析',
    type: 'pie',
    radius: ['40%', '60%'],
    radius: ['48%', '68%'],
    center: ['25%', '50%'],
    itemStyle: {
      borderColor: '#0a1c3a',
      borderColor: BI.sliceBorder,
      borderWidth: 2,
      borderRadius: 4,
      color: function (params) {
        return landColors[params.dataIndex % landColors.length]
      },
@@ -140,9 +145,9 @@
    },
  },
  {
    // 内圈
    // 内圈轨道环
    type: 'pie',
    radius: ['40%', '45%'],
    radius: ['40%', '43%'],
    center: ['25%', '50%'],
    silent: true,
    label: {
@@ -152,7 +157,7 @@
      show: false,
    },
    itemStyle: {
      color: 'rgba(0, 127, 255, 0.25)',
      color: 'rgba(74, 108, 240, 0.22)',
    },
    data: [1],
  },
@@ -163,22 +168,7 @@
  height: '100%',
}
const pieOptions = {
  backgroundColor: 'transparent',
  textStyle: { color: '#B8C8E0' },
}
// 使用封装的背景位置调整方法
// 图表中心是 ['25%', '50%'],背景需要对齐到这个位置
const { init: initBackground, cleanup: cleanupBackground } = useChartBackground({
  wrapperRef: pieWrapperRef,
  backgroundRef: pieBackgroundRef,
  left: '25%',       // 图表中心 X 是 25%
  top: '50%',        // 图表中心 Y 是 50%
  offsetX: '-51.5%', // X 轴偏移
  offsetY: '-50%',   // Y 轴偏移
  watchData: dataList // 监听数据变化,自动调整位置
})
const pieOptions = baseChartOptions
const fetchData = () => {
  rawMaterialPurchaseAmountRatio()
@@ -207,11 +197,6 @@
onMounted(() => {
  fetchData()
  initBackground()
})
onBeforeUnmount(() => {
  cleanupBackground()
})
</script>
@@ -223,7 +208,9 @@
}
.panel-item-customers {
  border: 1px solid #1a58b0;
  border: 1px solid var(--bi-panel-border);
  background: var(--bi-panel-bg);
  border-radius: 8px;
  padding: 18px;
  width: 100%;
  height: 449px;
@@ -236,20 +223,28 @@
  background: transparent;
}
.pie-background {
.pie-center {
  position: absolute;
  width: 310px;
  height: 310px;
  background-image: url('@/assets/BI/玫瑰图边框.png');
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
  z-index: 1;
  pointer-events: none;
  /* 位置由 JS 动态设置,默认居中 */
  left: 25%;
  top: 50%;
  transform: translate(-51.5%, -50%);
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  pointer-events: none;
  z-index: 2;
}
.pie-center-value {
  font-weight: 600;
  font-size: 20px;
  color: var(--bi-text-strong);
  font-variant-numeric: tabular-nums;
}
.pie-center-label {
  font-size: 12px;
  color: var(--bi-text);
}
</style>