2025-05-22 15:40:33 +08:00

100 lines
2.1 KiB
Vue

<template>
<custom-echart-bar :chart-data="state.data" height="100%" :option="state.option" />
</template>
<script setup>
import { reactive, watch } from 'vue';
import { isEmpty } from '@/utils';
const props = defineProps({
data: {
type: Array,
default: () => [],
},
});
const state = reactive({
total: 0, // 存储所有乡镇面积总和
option: {
grid: {
left: '5%',
right: '5%',
bottom: '5%',
top: '10%',
containLabel: true,
},
tooltip: {
show: true,
trigger: 'axis',
axisPointer: {
type: 'shadow',
},
backgroundColor: 'rgba(0,0,0,0.5);',
borderColor: '#35d0c0',
borderRadius: 8,
formatter: (data) => {
const params = data[0];
const percentage = ((params.value / state.total) * 100).toFixed(2);
return `<div class="custom-echarts-tips" >
<span>${params.name}</span><br/>
<span>${params.marker} ${params.data} km²</span><br/>
<span>占比 ${percentage}%</span>
</div>`;
},
extraCssText: 'backdrop-filter: blur(8px);',
},
barStyle: {
barWidth: 16,
itemStyle: {
borderRadius: 8,
},
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{ offset: 0, color: '#35d0c0' },
{ offset: 1, color: 'rgba(53,208,192,0)' },
],
global: false,
},
},
xAxis: {
type: 'category',
axisTick: {
show: false,
alignWithLabel: false,
interval: 'auto',
inside: false,
length: 5,
lineStyle: {
type: 'solid',
width: 1,
color: 'rgba(28, 158, 222, 1)',
},
},
},
yAxis: {
type: 'value',
// name: '面积(万亩)',
},
},
data: [],
});
watch(
() => props.data,
(val) => {
if (!isEmpty(val)) {
state.data = val;
state.total = val.reduce((sum, item) => sum + item.value, 0); // 计算总和
}
},
{
deep: true,
immediate: true,
}
);
</script>