Browse Source

fix: 图表动态渲染

master
guoapeng 2 weeks ago
parent
commit
af77efefe4
  1. 61
      src/components/home/LineChart.vue
  2. 16
      src/stores/homeStore.ts
  3. 23
      src/views/home/chart.vue
  4. 4
      src/views/home/index.vue

61
src/components/home/LineChart.vue

@ -1,41 +1,30 @@
<script lang="ts" setup> <script lang="ts" setup>
import * as echarts from 'echarts' import * as echarts from 'echarts'
import { onMounted, ref, watchEffect } from 'vue'
import { onMounted, ref, watch } from 'vue'
const props = defineProps<{ const props = defineProps<{
title: string
chartId: string
envData: Home.DisplayrelyMgrParams envData: Home.DisplayrelyMgrParams
}>() }>()
const temperatureData = ref<(number)[]>([0])
const humidityData = ref<(number)[]>([0])
const h2o2ConcentrationData = ref<(number)[]>([0])
const h2o2SaturationData = ref<(number)[]>([0])
const dataLength = ref(10)
watchEffect(() => {
temperatureData.value.push(props.envData.temp)
humidityData.value.push(props.envData.rh)
h2o2ConcentrationData.value.push(props.envData.rs)
h2o2SaturationData.value.push(props.envData.h2o2)
dataLength.value = temperatureData.value.length
})
onMounted(() => {
const chartDom = document.getElementById(props.chartId)
if (chartDom) {
const myChart = echarts.init(chartDom, null, { renderer: 'svg' })
const dataLength = ref(30)
watch(
() => props.envData,
(newValue) => {
if (!chartRef.value) {
return
}
let myChart = echarts.getInstanceByDom(chartRef.value)
if (!myChart) {
myChart = echarts.init(chartRef.value, null, { renderer: 'svg' })
}
const option = { const option = {
title: { title: {
text: props.title,
text: '哈哈哈',
}, },
tooltip: { tooltip: {
trigger: 'axis', trigger: 'axis',
}, },
legend: { legend: {
// data: ['', '湿', 'H2O2', 'H2O2'],
data: ['温度', '湿度', 'H2O2浓度', 'H2O2饱和度'], data: ['温度', '湿度', 'H2O2浓度', 'H2O2饱和度'],
orient: 'horizontal', // orient: 'horizontal', //
itemWidth: 10, // itemWidth: 10, //
@ -56,7 +45,7 @@ onMounted(() => {
{ {
name: '温度', name: '温度',
type: 'line', type: 'line',
data: temperatureData.value.sort((a, b) => a - b),
data: newValue.map(item => item.temp),
symbol: 'circle', symbol: 'circle',
symbolSize: 2, symbolSize: 2,
lineStyle: { lineStyle: {
@ -67,7 +56,7 @@ onMounted(() => {
{ {
name: '湿度', name: '湿度',
type: 'line', type: 'line',
data: humidityData.value.sort((a, b) => a - b),
data: newValue.map(item => item.rh),
symbol: 'circle', symbol: 'circle',
symbolSize: 2, symbolSize: 2,
lineStyle: { lineStyle: {
@ -78,7 +67,7 @@ onMounted(() => {
{ {
name: 'H2O2浓度', name: 'H2O2浓度',
type: 'line', type: 'line',
data: h2o2ConcentrationData.value.sort((a, b) => a - b),
data: newValue.map(item => item.h2o2),
symbol: 'circle', symbol: 'circle',
symbolSize: 2, symbolSize: 2,
lineStyle: { lineStyle: {
@ -89,7 +78,7 @@ onMounted(() => {
{ {
name: 'H2O2饱和度', name: 'H2O2饱和度',
type: 'line', type: 'line',
data: h2o2SaturationData.value.sort((a, b) => a - b),
data: newValue.map(item => item.rs),
symbol: 'circle', symbol: 'circle',
symbolSize: 2, symbolSize: 2,
lineStyle: { lineStyle: {
@ -100,15 +89,23 @@ onMounted(() => {
], ],
} }
myChart.setOption(option) myChart.setOption(option)
}
},
{
deep: true,
},
)
const chartRef = ref<HTMLElement | null>(null)
onMounted(() => {
echarts.init(chartRef.value, null, { renderer: 'svg' })
}) })
</script> </script>
<template> <template>
<div class="line-chart"> <div class="line-chart">
<div :id="chartId" style="height: 50vh;" />
<div ref="chartRef" style="height: 50vh" />
</div> </div>
</template> </template>
<style scoped>
</style>
<style scoped></style>

16
src/stores/homeStore.ts

@ -43,6 +43,8 @@ export const useHomeStore = defineStore('home', () => {
const defaultIntensityValue = ref(10) const defaultIntensityValue = ref(10)
const defaultIntensityTypeValue = ref() const defaultIntensityTypeValue = ref()
const allData = ref<any[]>([])
/** /**
* @function updateHomeData * @function updateHomeData
* @param {Home.DisplayrelyMgr[]} data - * @param {Home.DisplayrelyMgr[]} data -
@ -52,11 +54,24 @@ export const useHomeStore = defineStore('home', () => {
const sensordata = data.sensordata const sensordata = data.sensordata
if (sensordata && sensordata.length) { if (sensordata && sensordata.length) {
sensordata.forEach((item, index) => { sensordata.forEach((item, index) => {
if (allData.value[index]) {
allData.value[index].data.push(item)
// data中的数据只保留最后30条
if (allData.value[index].data.length > 30) {
allData.value[index].data.shift()
}
}
else {
allData.value.push({
data: [item],
})
}
h2O2SensorData.value[index] = { h2O2SensorData.value[index] = {
...h2O2SensorData.value[index], ...h2O2SensorData.value[index],
...item, ...item,
} }
}) })
console.log('allData', allData.value)
} }
} }
@ -140,6 +155,7 @@ export const useHomeStore = defineStore('home', () => {
isDeviceIdle, isDeviceIdle,
h2O2SensorData, h2O2SensorData,
allData,
updateHomeData, updateHomeData,
updatePressure, updatePressure,

23
src/views/home/chart.vue

@ -83,7 +83,7 @@ const onClose = () => {
<main v-loading="loading" class="main-content"> <main v-loading="loading" class="main-content">
<div class="line-chart-title"> <div class="line-chart-title">
<div v-if="formulaInfo && formulaInfo.name" class="line-chart-formula"> <div v-if="formulaInfo && formulaInfo.name" class="line-chart-formula">
<HomeFormula style="background: #E0F0FF;" />
<HomeFormula style="background: #e0f0ff" />
</div> </div>
<div class="line-chart-set"> <div class="line-chart-set">
<bt-button <bt-button
@ -101,21 +101,20 @@ const onClose = () => {
</bt-button> </bt-button>
</div> </div>
</div> </div>
<div class="line-chart-content" :style="{ 'grid-template-columns': `repeat(${h2O2SensorData.length},1fr)` }">
<div v-for="item in h2O2SensorData" :key=" item.title">
<LineChart :title="item.title" :chart-id="item.chartId" :env-data="item" />
<div
class="line-chart-content"
:style="{ 'grid-template-columns': `repeat(${homeStore.h2O2SensorData.length},1fr)` }"
>
<div v-for="(item, index) in homeStore.allData" :key="index">
<LineChart :env-data="item.data" />
</div> </div>
</div> </div>
<div class="line-chart-bottom"> <div class="line-chart-bottom">
<div class="home-chart-time"> <div class="home-chart-time">
<div v-if="!homeStore.isDeviceIdle" class="home-remain-time"> <div v-if="!homeStore.isDeviceIdle" class="home-remain-time">
<div class="home-chart-label"> <div class="home-chart-label">
<span v-if="disinfectionState.state === 'disinfection'">
预计剩余时间:
</span>
<span v-else>
消毒状态
</span>
<span v-if="disinfectionState.state === 'disinfection'"> 预计剩余时间: </span>
<span v-else> 消毒状态 </span>
</div> </div>
<div v-if="curStateRemainTime" class="home-chart-value"> <div v-if="curStateRemainTime" class="home-chart-value">
{{ curStateRemainTime }} {{ curStateRemainTime }}
@ -203,7 +202,7 @@ const onClose = () => {
.home-chart-time { .home-chart-time {
width: 35%; width: 35%;
.home-remain-time { .home-remain-time {
background: #F6FAFE;
background: #f6fafe;
width: 27vw; width: 27vw;
height: 8vh; height: 8vh;
border-radius: 12px; border-radius: 12px;
@ -214,7 +213,7 @@ const onClose = () => {
gap: 10px; gap: 10px;
margin-left: 1rem; margin-left: 1rem;
.home-chart-value { .home-chart-value {
color: #2892F3;
color: #2892f3;
} }
} }
} }

4
src/views/home/index.vue

@ -35,7 +35,7 @@ onMounted(() => {
loading.value = systemStore.loading loading.value = systemStore.loading
if (homeStore.h2O2SensorData && homeStore.h2O2SensorData.length) { if (homeStore.h2O2SensorData && homeStore.h2O2SensorData.length) {
styles.value = computedStyle() styles.value = computedStyle()
console.log('homeStore.h2O2SensorData', homeStore.h2O2SensorData)
// console.log('homeStore.h2O2SensorData', homeStore.h2O2SensorData)
environmentParams.value = { environmentParams.value = {
...homeStore.h2O2SensorData[0], ...homeStore.h2O2SensorData[0],
title: '仓内', title: '仓内',
@ -64,7 +64,7 @@ const styles = ref<any>({
}) })
const computedStyle = () => { const computedStyle = () => {
console.log('homeStore.h2O2SensorData', homeStore.h2O2SensorData.length)
// console.log('homeStore.h2O2SensorData', homeStore.h2O2SensorData.length)
if (deviceStore.isLowCost) { if (deviceStore.isLowCost) {
if (homeStore.h2O2SensorData.length === 1) { if (homeStore.h2O2SensorData.length === 1) {
return { return {

Loading…
Cancel
Save