WEB/src/views/dashboard/components/ExpressRatio.vue
2026-03-16 14:21:04 +08:00

227 lines
4.3 KiB
Vue

<template>
<el-card shadow="never" class="dashboard-module">
<template #header>
<div class="module-header">
<span class="module-title">{{ title }}</span>
<div class="module-actions">
<el-button
type="primary"
link
:icon="Refresh"
:loading="loading"
@click="refreshData"
/>
</div>
</div>
</template>
<div class="module-content">
<div v-if="error" class="error-message">
<el-alert
:title="error"
type="error"
:closable="false"
/>
</div>
<div v-else>
<div v-if="loading && !chartData.length" class="loading-wrapper">
<!-- Skeleton or loading can go here -->
</div>
<div v-else ref="chartRef" style="height: 300px; width: 100%;"></div>
</div>
</div>
</el-card>
</template>
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, watch, computed, nextTick } from 'vue'
import * as echarts from 'echarts'
import { Refresh } from '@element-plus/icons-vue'
import { useDashboard } from '../composables/useDashboard'
import { useI18n } from 'vue-i18n'
defineOptions({
name: 'ExpressRatio'
})
const { t } = useI18n()
const props = defineProps({
title: {
type: String,
default: ''
},
method: {
type: String,
default: 'dashboard.express_ratio'
}
})
const { loading, error, data, loadData, refreshData } = useDashboard({
method: props.method
})
const chartRef = ref<HTMLElement | null>(null)
let chartInstance: echarts.ECharts | null = null
const chartData = computed(() => data.value || [])
const initChart = () => {
if (chartRef.value) {
chartInstance = echarts.init(chartRef.value)
// Render initial empty chart or wait for data
if (chartData.value.length > 0) {
renderChart()
}
window.addEventListener('resize', handleResize)
}
}
const handleResize = () => {
if (chartInstance) {
chartInstance.resize()
}
}
const renderChart = () => {
if (!chartInstance) return
const option = {
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b}: {c} ({d}%)'
},
legend: {
orient: 'vertical',
right: 10,
top: 'center',
data: chartData.value.map((item: any) => item.name)
},
series: [
{
name: t('dashboard.expressDistribution'),
type: 'pie',
radius: ['40%', '70%'],
center: ['40%', '50%'],
avoidLabelOverlap: false,
itemStyle: {
borderRadius: 10,
borderColor: '#fff',
borderWidth: 2
},
label: {
show: false,
position: 'center'
},
emphasis: {
label: {
show: true,
fontSize: '14',
fontWeight: 'bold'
}
},
labelLine: {
show: false
},
data: chartData.value
}
]
}
chartInstance.setOption(option)
}
// Watch for data changes to update chart
watch(chartData, (val) => {
if (val && val.length > 0) {
nextTick(() => {
if (!chartInstance) {
initChart()
}
renderChart()
})
}
})
onMounted(() => {
loadData()
// Initialize chart after mount, but data might not be ready
nextTick(() => {
initChart()
})
})
onBeforeUnmount(() => {
if (chartInstance) {
chartInstance.dispose()
chartInstance = null
}
window.removeEventListener('resize', handleResize)
})
// Expose refresh method
defineExpose({
refresh: refreshData
})
</script>
<style scoped>
.dashboard-module {
margin-bottom: 0;
height: 100%;
display: flex;
flex-direction: column;
border-radius: 8px;
border: 1px solid #ebeef5;
}
:deep(.el-card__header) {
padding: 0;
border-bottom: none;
}
:deep(.el-card__body) {
padding: 0;
flex: 1;
display: flex;
flex-direction: column;
min-height: 0;
}
.module-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 20px 0;
}
.module-title {
font-weight: 600;
color: #303133;
font-size: 16px;
}
.module-actions {
display: flex;
align-items: center;
gap: 8px;
}
.module-content {
flex: 1;
padding: 0 20px 20px;
min-height: 0;
}
.error-message {
padding: 20px 0;
}
.loading-wrapper {
padding: 20px 0;
}
</style>