Files
cz-web/src/views/finance/cpns/FinanceChart.vue
2026-09-30 15:55:36 +08:00

183 lines
4.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed } from 'vue'
interface DistributionItem {
name: string
value: number
color: string
}
interface Props {
distribution: DistributionItem[]
}
const props = defineProps<Props>()
const total = computed(() => props.distribution.reduce((sum, item) => sum + item.value, 0))
/** CSS conic-gradient(无图表库):按 value 占比切分色环 */
const gradient = computed(() => {
if (total.value <= 0) return 'conic-gradient(#e4e7ec 0% 100%)'
let acc = 0
const stops = props.distribution.map((item) => {
const start = acc
acc += (item.value / total.value) * 100
return `${item.color} ${start}% ${acc}%`
})
return `conic-gradient(${stops.join(', ')})`
})
function percentOf(value: number): number {
if (total.value <= 0) return 0
return Math.round((value / total.value) * 100)
}
</script>
<template>
<div class="finance-chart card">
<h3 class="finance-chart__title">
业务类型分布
</h3>
<div class="finance-chart__body">
<!-- 设计 37:横向网格线 + 等宽柱条(柱高按占比换算) -->
<div class="finance-chart__plot">
<div class="finance-chart__grid" aria-hidden="true">
<i class="finance-chart__line" />
<i class="finance-chart__line" />
<i class="finance-chart__line" />
<i class="finance-chart__line" />
<i class="finance-chart__line" />
</div>
<div class="finance-chart__bars">
<span
v-for="item in distribution"
:key="item.name"
class="finance-chart__bar"
:style="{ height: `${Math.max(percentOf(item.value), 6)}%` }"
/>
</div>
</div>
<!-- 数据源色环:设计稿为柱状图,此处仅保留绑定(隐藏)以免游离变量 -->
<div class="finance-chart__pie" :style="{ background: gradient }" />
<ul class="finance-chart__legend">
<li
v-for="item in distribution"
:key="item.name"
class="finance-chart__legend-item"
>
<span
class="finance-chart__dot"
:style="{ background: item.color }"
/>
<span class="finance-chart__name">{{ item.name }}</span>
<span class="finance-chart__value">{{ percentOf(item.value) }}%</span>
</li>
</ul>
</div>
</div>
</template>
<style lang="less" scoped>
// 设计 37:白卡 + 蓝色竖条标题(15 设计px → 12px/700)+ 柱状图 + 横排图例
.finance-chart {
padding: 20px @spacing-lg @spacing-lg;
border-radius: @radius-base;
background: @white;
box-shadow: @shadow-base;
&__title {
position: relative;
padding-left: 10px;
margin-bottom: @spacing-base;
font-size: 12px;
font-weight: 700;
color: @heading-color;
&::before {
content: '';
position: absolute;
top: 50%;
left: 0;
width: 4px;
height: 14px;
border-radius: @radius-round;
background: @primary-strong;
transform: translateY(-50%);
}
}
&__body {
.flex(column, center, center);
gap: @spacing-base;
}
&__plot {
position: relative;
width: 100%;
height: 184px;
}
&__grid {
.flex(column, space-between);
position: absolute;
inset: 0;
}
&__line {
display: block;
width: 100%;
height: 1px;
background: @finance-chart-grid;
}
&__bars {
.flex(row, space-around, flex-end);
position: absolute;
inset: 0;
padding: 0 60px;
}
&__bar {
display: block;
width: 40px;
border-radius: 2px 2px 0 0;
background: @finance-chart-bar;
}
// 设计稿无色环:绑定保留但不参与展示
&__pie {
display: none;
}
&__legend {
.flex(row, center, center);
gap: @spacing-lg;
list-style: none;
padding: 0;
}
&__legend-item {
.flex(row, flex-start, center);
gap: 6px;
font-size: 11px;
color: @text-color-secondary;
}
&__dot {
width: 8px;
height: 8px;
border-radius: @radius-round;
flex-shrink: 0;
}
&__name {
min-width: 0;
}
&__value {
color: @heading-color;
font-weight: 600;
}
}
</style>