2026-05-26 09:15:14 +08:00
|
|
|
|
<template>
|
2026-05-29 14:11:06 +08:00
|
|
|
|
<div ref="chartElement" class="budget-trend-chart" role="img" :aria-label="ariaLabel"></div>
|
2026-05-26 09:15:14 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2026-05-29 14:11:06 +08:00
|
|
|
|
import { computed, shallowRef } from 'vue'
|
|
|
|
|
|
import { BarChart as EChartsBarChart } from 'echarts/charts'
|
|
|
|
|
|
import { GridComponent, TooltipComponent } from 'echarts/components'
|
|
|
|
|
|
import { use } from 'echarts/core'
|
|
|
|
|
|
import { CanvasRenderer } from 'echarts/renderers'
|
|
|
|
|
|
|
|
|
|
|
|
import { useEcharts } from '../../composables/useEcharts.js'
|
2026-05-27 09:17:57 +08:00
|
|
|
|
import { useThemeColors } from '../../composables/useThemeColors.js'
|
2026-05-26 09:15:14 +08:00
|
|
|
|
|
2026-05-29 14:11:06 +08:00
|
|
|
|
use([GridComponent, TooltipComponent, EChartsBarChart, CanvasRenderer])
|
2026-05-26 09:15:14 +08:00
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
|
labels: { type: Array, required: true },
|
|
|
|
|
|
budget: { type: Array, required: true },
|
2026-05-26 20:07:56 +08:00
|
|
|
|
used: { type: Array, required: true },
|
|
|
|
|
|
occupied: { type: Array, default: () => [] },
|
|
|
|
|
|
available: { type: Array, default: () => [] }
|
2026-05-26 09:15:14 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2026-05-29 14:11:06 +08:00
|
|
|
|
const chartElement = shallowRef(null)
|
2026-05-27 09:17:57 +08:00
|
|
|
|
const themeColors = useThemeColors()
|
2026-05-28 16:24:59 +08:00
|
|
|
|
const prefersReducedMotion = () =>
|
|
|
|
|
|
typeof window !== 'undefined' && window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|
2026-05-26 09:15:14 +08:00
|
|
|
|
|
2026-05-26 20:07:56 +08:00
|
|
|
|
const currency = (value) =>
|
|
|
|
|
|
Number(value || 0).toLocaleString('zh-CN', {
|
|
|
|
|
|
minimumFractionDigits: 2,
|
|
|
|
|
|
maximumFractionDigits: 2
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const percent = (value, total) => {
|
|
|
|
|
|
const denominator = Number(total || 0)
|
|
|
|
|
|
if (!denominator) return 0
|
|
|
|
|
|
return Number(((Number(value || 0) / denominator) * 100).toFixed(2))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const percentSeries = (series) =>
|
|
|
|
|
|
props.budget.map((total, index) => percent(series[index], total))
|
|
|
|
|
|
|
|
|
|
|
|
const usedPercent = computed(() => percentSeries(props.used))
|
|
|
|
|
|
const occupiedPercent = computed(() => percentSeries(props.occupied))
|
2026-05-29 14:11:06 +08:00
|
|
|
|
const availableAmountSeries = computed(() =>
|
2026-05-26 20:07:56 +08:00
|
|
|
|
props.budget.map((total, index) => {
|
2026-05-29 14:11:06 +08:00
|
|
|
|
const explicitValue = Number(props.available[index])
|
|
|
|
|
|
if (Number.isFinite(explicitValue) && explicitValue > 0) {
|
|
|
|
|
|
return explicitValue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-26 20:07:56 +08:00
|
|
|
|
const usedValue = Number(props.used[index] || 0)
|
|
|
|
|
|
const occupiedValue = Number(props.occupied[index] || 0)
|
2026-05-29 14:11:06 +08:00
|
|
|
|
return Math.max(Number(total || 0) - usedValue - occupiedValue, 0)
|
2026-05-26 20:07:56 +08:00
|
|
|
|
})
|
|
|
|
|
|
)
|
2026-05-29 14:11:06 +08:00
|
|
|
|
const availablePercent = computed(() =>
|
|
|
|
|
|
props.budget.map((total, index) => percent(availableAmountSeries.value[index], total))
|
|
|
|
|
|
)
|
2026-05-26 20:07:56 +08:00
|
|
|
|
|
|
|
|
|
|
const yAxisMax = computed(() => {
|
|
|
|
|
|
const maxUsage = Math.max(
|
|
|
|
|
|
100,
|
|
|
|
|
|
...usedPercent.value.map((value, index) => value + Number(occupiedPercent.value[index] || 0))
|
|
|
|
|
|
)
|
|
|
|
|
|
return Math.ceil(maxUsage / 20) * 20
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-05-29 14:11:06 +08:00
|
|
|
|
const ariaLabel = computed(() =>
|
|
|
|
|
|
props.labels.map((label, index) => (
|
|
|
|
|
|
`${label}预算${currency(props.budget[index])},已使用${usedPercent.value[index] || 0}%,已占用${occupiedPercent.value[index] || 0}%`
|
|
|
|
|
|
)).join(';')
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
function buildSeriesData(percentValues, amountValues) {
|
|
|
|
|
|
return percentValues.map((value, index) => ({
|
|
|
|
|
|
value,
|
|
|
|
|
|
amount: Number(amountValues[index] || 0)
|
|
|
|
|
|
}))
|
|
|
|
|
|
}
|
2026-05-26 09:15:14 +08:00
|
|
|
|
|
2026-05-26 20:07:56 +08:00
|
|
|
|
const chartOptions = computed(() => ({
|
2026-05-29 14:11:06 +08:00
|
|
|
|
backgroundColor: 'transparent',
|
2026-05-26 09:15:14 +08:00
|
|
|
|
animation: {
|
2026-05-28 16:24:59 +08:00
|
|
|
|
duration: prefersReducedMotion() ? 0 : 760,
|
2026-05-26 09:15:14 +08:00
|
|
|
|
easing: 'easeOutQuart'
|
|
|
|
|
|
},
|
2026-05-29 14:11:06 +08:00
|
|
|
|
grid: {
|
|
|
|
|
|
top: 12,
|
|
|
|
|
|
right: 16,
|
|
|
|
|
|
bottom: 24,
|
|
|
|
|
|
left: 34,
|
|
|
|
|
|
containLabel: true
|
|
|
|
|
|
},
|
|
|
|
|
|
tooltip: {
|
|
|
|
|
|
trigger: 'axis',
|
|
|
|
|
|
confine: true,
|
|
|
|
|
|
appendToBody: true,
|
|
|
|
|
|
axisPointer: { type: 'shadow' },
|
|
|
|
|
|
backgroundColor: '#ffffff',
|
|
|
|
|
|
borderColor: '#e2e8f0',
|
|
|
|
|
|
borderWidth: 1,
|
|
|
|
|
|
padding: [10, 12],
|
|
|
|
|
|
textStyle: {
|
|
|
|
|
|
color: '#475569',
|
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
|
fontWeight: 700
|
2026-05-26 09:15:14 +08:00
|
|
|
|
},
|
2026-05-29 14:11:06 +08:00
|
|
|
|
extraCssText: 'border-radius:4px;box-shadow:0 12px 28px rgba(15,23,42,.12);',
|
|
|
|
|
|
formatter(params = []) {
|
|
|
|
|
|
const items = Array.isArray(params) ? params : [params]
|
|
|
|
|
|
const index = Number(items[0]?.dataIndex || 0)
|
|
|
|
|
|
const lines = items.map((item) => {
|
|
|
|
|
|
const percentValue = Number(item?.value || 0).toFixed(2)
|
|
|
|
|
|
const amount = currency(item?.data?.amount || 0)
|
|
|
|
|
|
return `${item.marker}${item.seriesName}: ${percentValue}%(¥${amount})`
|
|
|
|
|
|
})
|
|
|
|
|
|
return [`${items[0]?.axisValue || ''}`, ...lines, `预算总额: ¥${currency(props.budget[index])}`].join('<br/>')
|
2026-05-26 09:15:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2026-05-29 14:11:06 +08:00
|
|
|
|
xAxis: {
|
|
|
|
|
|
type: 'category',
|
|
|
|
|
|
data: props.labels,
|
|
|
|
|
|
axisTick: { show: false },
|
|
|
|
|
|
axisLine: { show: false },
|
|
|
|
|
|
axisLabel: {
|
|
|
|
|
|
color: '#64748b',
|
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
|
fontWeight: 700
|
2026-05-26 09:15:14 +08:00
|
|
|
|
}
|
2026-05-26 20:07:56 +08:00
|
|
|
|
},
|
2026-05-29 14:11:06 +08:00
|
|
|
|
yAxis: {
|
|
|
|
|
|
type: 'value',
|
|
|
|
|
|
min: 0,
|
|
|
|
|
|
max: yAxisMax.value,
|
|
|
|
|
|
splitNumber: Math.max(1, Math.ceil(yAxisMax.value / 20)),
|
|
|
|
|
|
axisLine: { show: false },
|
|
|
|
|
|
axisTick: { show: false },
|
|
|
|
|
|
axisLabel: {
|
|
|
|
|
|
color: '#64748b',
|
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
|
fontWeight: 700,
|
|
|
|
|
|
formatter: (value) => `${Number(value)}%`
|
|
|
|
|
|
},
|
|
|
|
|
|
splitLine: { lineStyle: { color: '#edf2f7' } }
|
|
|
|
|
|
},
|
|
|
|
|
|
series: [
|
|
|
|
|
|
{
|
|
|
|
|
|
name: '已使用',
|
|
|
|
|
|
type: 'bar',
|
|
|
|
|
|
stack: 'budgetUsage',
|
|
|
|
|
|
data: buildSeriesData(usedPercent.value, props.used),
|
|
|
|
|
|
barWidth: 16,
|
|
|
|
|
|
itemStyle: {
|
|
|
|
|
|
color: themeColors.value.chartPrimary,
|
|
|
|
|
|
borderRadius: [4, 4, 0, 0]
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: '已占用',
|
|
|
|
|
|
type: 'bar',
|
|
|
|
|
|
stack: 'budgetUsage',
|
|
|
|
|
|
data: buildSeriesData(occupiedPercent.value, props.occupied),
|
|
|
|
|
|
barWidth: 16,
|
|
|
|
|
|
itemStyle: {
|
|
|
|
|
|
color: themeColors.value.warning,
|
|
|
|
|
|
borderRadius: [4, 4, 0, 0]
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: '剩余可用',
|
|
|
|
|
|
type: 'bar',
|
|
|
|
|
|
stack: 'budgetUsage',
|
|
|
|
|
|
data: buildSeriesData(availablePercent.value, availableAmountSeries.value),
|
|
|
|
|
|
barWidth: 16,
|
|
|
|
|
|
itemStyle: {
|
|
|
|
|
|
color: '#e5edf3',
|
|
|
|
|
|
borderRadius: [4, 4, 0, 0]
|
|
|
|
|
|
}
|
2026-05-26 20:07:56 +08:00
|
|
|
|
}
|
2026-05-29 14:11:06 +08:00
|
|
|
|
]
|
2026-05-26 20:07:56 +08:00
|
|
|
|
}))
|
2026-05-29 14:11:06 +08:00
|
|
|
|
|
|
|
|
|
|
useEcharts(chartElement, chartOptions)
|
2026-05-26 09:15:14 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.budget-trend-chart {
|
|
|
|
|
|
position: relative;
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
height: 220px;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|