Files
sgxt_web/src/views/home/echarts/lineEcharts.vue

145 lines
3.0 KiB
Vue
Raw Normal View History

2025-04-15 14:38:12 +08:00
<template>
<div style="height:100%;width:100%" :id="echartsId"></div>
</template>
<script setup>
import * as echarts from "echarts";
import { onMounted, ref, reactive, defineProps, onUnmounted, watch, nextTick } from "vue";
const props = defineProps({
echartsId:{
type:String,
default:'lineId'
},
2026-01-16 12:40:42 +08:00
color:{
2025-04-16 23:06:01 +08:00
type:String,
default:'#fff'
},
2026-01-16 12:40:42 +08:00
data:{
2025-04-15 14:38:12 +08:00
type:Array,
default:[]
}
});
2025-12-27 11:10:31 +08:00
// 保存echarts实例
const myChart = ref(null);
// 定义resize处理函数
const handleResize = () => {
if (myChart.value) {
myChart.value.resize();
}
};
2025-04-15 14:38:12 +08:00
function chartFn() {
2025-12-27 11:10:31 +08:00
// 如果已有实例,先销毁
if (myChart.value) {
myChart.value.dispose();
}
2026-01-16 12:40:42 +08:00
2025-12-27 11:10:31 +08:00
// 创建新实例
myChart.value = echarts.init(document.getElementById(props.echartsId));
2025-04-16 23:06:01 +08:00
var option = {
2025-04-15 14:38:12 +08:00
grid: {
top: "8%",
right: "2%",
2026-01-29 16:47:04 +08:00
left: "2%",
bottom: "8%",
2025-04-15 14:38:12 +08:00
containLabel:true
},
tooltip: {
trigger: "axis",
axisPointer: {
lineStyle: {
color: "#ddd"
}
},
backgroundColor: "rgba(255,255,255,1)",
padding: [5, 10],
textStyle: {
color: "#7588E4"
},
extraCssText: "box-shadow: 0 0 5px rgba(0,0,0,0.3)"
},
xAxis: {
type: "category",
data:props.data.map(v=>{return v.label}),
2025-04-15 18:56:50 +08:00
axisTick: { show: false },
axisLine: { show: false },
2026-01-16 12:40:42 +08:00
axisLabel: {
2025-04-15 18:56:50 +08:00
show: true,
2025-04-16 23:06:01 +08:00
color: props.color,
2025-04-15 18:56:50 +08:00
interval: 0, // 强制显示所有标签
// rotate: 15, // 标签旋转角度
2025-04-15 14:38:12 +08:00
}
},
yAxis: {
type: "value",
splitLine: {
show:true ,
lineStyle: {
type:'dashed',
color: "rgba(14,95,255,0.5)"
}
},
2025-04-15 18:56:50 +08:00
axisTick: { show: false },
axisLine: { show: false },
2025-04-16 23:06:01 +08:00
axisLabel: { color: props.color },
2025-04-15 14:38:12 +08:00
},
series: [
{
type: "line",
smooth:true,
showSymbol:false,
2025-04-15 18:56:50 +08:00
data: props.data.map(v=>{return v.value}),
2025-04-15 14:38:12 +08:00
areaStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(199, 237, 250,0.5)'
}, {
offset: 1,
color: 'rgba(199, 237, 250,0.2)'
}], false)
}
},
itemStyle: {
normal: {
color: "rgb(4, 145, 216)"
}
},
lineStyle: {
normal: {
width: 1,
color:'#00FFFF'
}
}
}
]
};
2025-12-27 11:10:31 +08:00
option && myChart.value.setOption(option);
2025-04-15 14:38:12 +08:00
}
2025-12-27 11:10:31 +08:00
// 监听数据变化
watch(()=>props.data,val=>{
nextTick(()=>{ chartFn() })
},{immediate:true,deep:true})
// 组件挂载时初始化图表并添加事件监听
onMounted(()=>{
chartFn();
window.addEventListener('resize', handleResize);
});
// 组件卸载时清理资源
onUnmounted(()=>{
if (myChart.value) {
myChart.value.dispose();
myChart.value = null;
}
window.removeEventListener('resize', handleResize);
});
2025-04-15 14:38:12 +08:00
</script>
<style lang="scss" scoped>
2026-01-16 12:40:42 +08:00
</style>