更新 src/uni_modules/cxc-szcx-multiLineChart/components/cxc-szcx-multiLineChart/cxc-szcx-multiLineChart.vue

This commit is contained in:
ldeyun 2025-09-15 15:05:38 +00:00
parent 6bac6dc89d
commit 92d48433b8

View File

@ -1,21 +1,7 @@
<route lang="json5" type="page">
{
layout: 'default',
style: {
navigationStyle: 'custom',
navigationBarTitleText: '历史数据图表',
},
}
</route>
<template> <template>
<PageLayout :navbarShow="false"> <view >
<view> <l-echart ref="chart" @finished="drawChart" />
<!-- ECharts图表 --> </view>
<view class="chart-container">
<l-echart ref="chart" @finished="initChart" />
</view>
</view>
</PageLayout>
</template> </template>
<script setup> <script setup>
@ -75,7 +61,7 @@
// 1. chartOption.value // 1. chartOption.value
const generateOptions = () => { const generateOptions = () => {
console.log(23)
const serie = []; // const serie = []; //
const yData = []; // Y const yData = []; // Y
const xData = []; // X const xData = []; // X
@ -114,7 +100,7 @@
const legendName = `${yFieldItem.name}(${yFieldItem.unit})`; const legendName = `${yFieldItem.name}(${yFieldItem.unit})`;
legendData.push(legendName); legendData.push(legendName);
console.log(1, legendData)
// Yssll // Yssll
if (yFieldItem.field === 'ssll') { if (yFieldItem.field === 'ssll') {
@ -122,7 +108,7 @@
yaxisTemp.nameGap = -15; yaxisTemp.nameGap = -15;
} else { } else {
yaxisTemp.position = 'right'; yaxisTemp.position = 'right';
yaxisTemp.offset = 40 * positionIndex; yaxisTemp.offset = 30 * positionIndex;
positionIndex++; positionIndex++;
} }
@ -131,9 +117,10 @@
yaxisTemp.min = yFieldItem.min ?? 0; // min0 yaxisTemp.min = yFieldItem.min ?? 0; // min0
yaxisTemp.max = yFieldItem.max ?? 100; // max100 yaxisTemp.max = yFieldItem.max ?? 100; // max100
yaxisTemp.axisLine.lineStyle.color = colorData.value[i]?.color || '#61A0A8'; // yaxisTemp.axisLine.lineStyle.color = colorData.value[i]?.color || '#61A0A8'; //
// yaxisTemp.show=false
yData.push(yaxisTemp); yData.push(yaxisTemp);
// //
const serieTemp = { const serieTemp = {
field: yFieldItem.field, field: yFieldItem.field,
@ -149,6 +136,7 @@
// this.serie.pushseriepush // this.serie.pushseriepush
serie.push(serieTemp); serie.push(serieTemp);
} }
// //
@ -162,12 +150,13 @@
trigger: 'axis', trigger: 'axis',
alwaysShowContent: true, alwaysShowContent: true,
axisPointer: { axisPointer: {
type: 'cross' type: 'line',
show:false
} }
}, },
toolbox: { toolbox: {
showTitle: true, showTitle: true,
orient: 'vertical', orient: 'horizontal',
feature: { feature: {
dataView: { dataView: {
show: true, show: true,
@ -180,19 +169,21 @@
show: true show: true
} }
}, },
left: '0%', right: '10%',
top: '10%' top: '0'
}, },
legend: { legend: {
data: legendData, data: legendData,
itemGap: 5, itemGap: 5,
padding: [35, 5, 25, 5], padding: [35, 5, 25, 5],
type: 'scroll' type: 'scroll',
show:true
}, },
grid: { grid: {
top: '15%', width:'auto',
left: '5%', top: '20%',
right: `${10 + positionIndex * 10}%`, // Y left: '5px',
right: '5px', // Y
bottom: 20, bottom: 20,
containLabel: true containLabel: true
}, },
@ -201,6 +192,7 @@
axisTick: { axisTick: {
alignWithLabel: true alignWithLabel: true
}, },
show:true,
data: xData data: xData
}, },
yAxis: yData, yAxis: yData,
@ -211,6 +203,7 @@
// 2. 访 // 2. 访
const processSeriesData = () => { const processSeriesData = () => {
const ssData = props.dataList; const ssData = props.dataList;
// 访chartOption.value // 访chartOption.value
const xData = [...chartOption.value.xAxis.data]; // X series const xData = [...chartOption.value.xAxis.data]; // X series
const serie = [...chartOption.value.series]; // const serie = [...chartOption.value.series]; //
@ -236,9 +229,11 @@
}); });
// Ythis.seriethis.maxArrValue // Ythis.seriethis.maxArrValue
serie.forEach((serieItem, mm) => { serie.forEach((serieItem, mm) => {
const maxValue = maxArrValue(serieItem.data); const maxValue = maxArrValue(serieItem.data);
yData[mm].max = Math.round(maxValue * 1.2) || 100; // 0100 console.log(1,maxValue)
yData[mm].max = Math.round(maxValue * 12)/10 || 100; // 0100
console.log(2,yData[mm].max)
}); });
// .value // .value
@ -251,7 +246,7 @@
series: serie, series: serie,
yAxis: yData yAxis: yData
}; };
console.log(22, chartOption.value)
}; };
// 3. NaN // 3. NaN
@ -271,11 +266,9 @@
watch( watch(
() => props.dataList, () => props.dataList,
(newVal) => { (newVal) => {
if (newVal.length === 0) return; // if (newVal.length === 0) return; //
console.log(33, props.dataList) drawChart()
console.log(44, props.yFields)
initChart()
updateChart();
}, { }, {
deep: true, deep: true,
immediate: true immediate: true
@ -283,36 +276,24 @@
); );
// 5. chartOption.value // 5. chartOption.value
const initChart = async () => { const drawChart = async () => {
// //
chartOption.value = generateOptions(); chartOption.value = generateOptions();
processSeriesData()
if (!chart.value) return; if (!chart.value) return;
// EChartssetTimeoutasync/await // EChartssetTimeoutasync/await
const myChart = await chart.value.init(echarts); const myChart = await chart.value.init(echarts);
myChart.setOption(chartOption.value); myChart.setOption(chartOption.value);
// resize
window.addEventListener('resize', () => {
myChart.resize();
});
}; };
// 6. 访chartOption.value
const updateChart = () => {
if (!myChart.value || !chartOption.value) {
console.warn('ECharts实例未初始化');
return;
}
processSeriesData();
myChart.value.setOption(chartOption.value, true);
};
// //
onMounted(() => { onMounted(() => {
setTimeout(() => { setTimeout(() => {
initChart(); drawChart();
}, 500) }, 500)
}); });
@ -329,45 +310,5 @@
</script> </script>
<style scoped> <style scoped>
.chart-container {
width: 100%;
height: 30vh;
min-height: 200px;
padding: 20rpx;
background: linear-gradient(145deg, #f8f9fa 0%, #ffffff 100%);
border-radius: 16rpx;
box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
position: relative;
overflow: hidden;
}
/* 加载占位符(数据为空时显示) */
.chart-container::before {
content: '数据加载中...';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #666;
font-size: 28rpx;
z-index: 1;
/* 数据加载完成后隐藏 */
opacity: v-bind('chartOption.value.series?.length > 0 ? 0 : 1');
}
.chart-wrapper {
width: 100%;
height: 100%;
transition: opacity 0.3s ease;
}
/* 移动端优化调整右侧间距避免Y轴标签被截断 */
@media screen and (max-width: 768px) {
.chart-container {
height: 30vh;
min-height: 200px;
padding: 10rpx;
border-radius: 12rpx;
}
}
</style> </style>