当前位置: 首页 > news >正文

vue3+ts+echarts多Y轴图表

HTML 

 <!-- 温湿度传感器 --><el-row v-if="deviceTypeId === '2'"><el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24"><div class="chart-container"><div class="filter-container"><!-- <el-select v-model="WenduTimeType" placeholder="请选择时间维度" style="width: 200px;margin-right: 20px;"@change="handleWenduTimeTypeChange"><el-option label="小时" value="hour" /><el-option label="日" value="day" /><el-option label="月" value="month" /><el-option label="年" value="year" /></el-select> --><el-radio-group v-model="WenduTimeType" size="small" @change="generateWenduData"><el-radio-button label="day">日</el-radio-button><el-radio-button label="month">月</el-radio-button><el-radio-button label="year">年</el-radio-button></el-radio-group></div><div id="wenduRef" ref="wendurRef" style=" width: 100%;height: 400px;" class="chart wendu-chart"></div></div></el-col></el-row>

初始化 

const wenduChart = ref<any>(null);
const wenduRef = ref<HTMLElement | null>(null)
const WenduTimeType = ref<'day' | 'month' | 'year' | 'hour'>('day')
const WenduSelectedHour = ref<Date>(new Date())
const WenduSelectedDay = ref<Date>(new Date())
const WenduSelectedMonth = ref<Date>(new Date())
const WenduSelectedYear = ref<Date>(new Date())
const WenduXAxisDatatem = ref<string[]>([])
const WenduDatatem = ref<number[]>([])
const WenduXAxisDatahum = ref<string[]>([])
const WenduDatahum = ref<number[]>([])
const WenduXAxisDataill = ref<string[]>([])
const WenduDataill = ref<number[]>([])

函数 

// 新增温湿度图数据生成函数
function generateWenduData() {let timeAmount = '1';let timeUnit = 'd';if (WenduTimeType.value === 'day') timeUnit = 'd';else if (WenduTimeType.value === 'month') timeUnit = 'mo';else if (WenduTimeType.value === 'year') timeUnit = 'y';else if (WenduTimeType.value === 'hour') timeUnit = 'h';DashAPI.getsensor({// deviceCode: props.deviceCode,deviceCode: deviceCodeCS.value,timeAmount,timeUnit}).then(res => {const data = Array.isArray(res) ? res : [];const tem = data.find((item: any) => item.name === "temperature");const hum = data.find((item: any) => item.name === "humidity");const ill = data.find((item: any) => item.name === "illuminance");WenduXAxisDatatem.value = tem?.time || [];WenduDatatem.value = tem?.value || [];WenduXAxisDatahum.value = hum?.time || [];WenduDatahum.value = hum?.value || [];WenduXAxisDataill.value = ill?.time || [];WenduDataill.value = ill?.value || [];console.log("getsensor", data);updateWenduChart();});
}
// const botlineType = ref<'day' | 'month' | 'year'>('day');
// 新增温湿度图更新方法
function updateWenduChart(retry = 0) {function tryWendu() {const chartDom = document.querySelector('.wendu-chart') as HTMLElement;if (!chartDom || chartDom.offsetWidth === 0 || chartDom.offsetHeight === 0) {requestAnimationFrame(tryWendu);return;}// 1. ECharts实例和DOM同步if (wenduChart.value &&typeof (wenduChart.value as any).getDom === 'function' &&(wenduChart.value as any).getDom() !== chartDom) {wenduChart.value.dispose();wenduChart.value = null;}if (!wenduChart.value) {wenduChart.value = echarts.init(chartDom);}const option = {color: ['#5470C6', '#91CC75', '#EE6666'],tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } },grid: {right: '20%'},xAxis: [{type: 'category',axisTick: { alignWithLabel: true, color: getTextColor() },data: WenduXAxisDatatem.value, // 以温度时间为主axisLine: { lineStyle: { color: getTextColor() } }}],yAxis: [{ type: 'value', name: '温度°C', position: 'left', alignTicks: true, axisLine: { show: true, lineStyle: { color: '#5470C6' } }, axisLabel: { formatter: '{value}' } },{ type: 'value', name: '湿度%', position: 'right', alignTicks: true, axisLine: { show: true, lineStyle: { color: '#91CC75' } }, axisLabel: { formatter: '{value} ' } },{ type: 'value', name: '光照lux', position: 'right', alignTicks: true, offset: 80, axisLine: { show: true, lineStyle: { color: '#EE6666' } }, axisLabel: { formatter: '{value} ' } }],series: [{ name: '温度', type: 'line', yAxisIndex: 0, data: WenduDatatem.value },{ name: '湿度', type: 'line', yAxisIndex: 1, data: WenduDatahum.value },{ name: '光照', type: 'line', yAxisIndex: 2, data: WenduDataill.value }]};wenduChart.value.setOption(option);wenduChart.value.resize();};tryWendu()
}
// 新增温湿度时间维度切换
const handleWenduTimeTypeChange = () => {if (WenduTimeType.value === 'day') {WenduSelectedHour.value = new Date()} else if (WenduTimeType.value === 'month') {WenduSelectedMonth.value = new Date()} else if (WenduTimeType.value === 'year') {WenduSelectedYear.value = new Date()} else if (WenduTimeType.value === 'hour') {WenduSelectedHour.value = new Date()}generateWenduData()
}
// 监听暗黑模式变化时,初始化新图表--若有需要
const observer = new MutationObserver((mutations) => {mutations.forEach((mutation) => {if (mutation.attributeName === 'class') {nextTick(() => {if (props.deviceTypeId === '2') {generateWenduData();} });}});
});
//渲染
// 页面加载和激活时都调用
function waitForContainerAndInitChart() {const chartDomwendu = document.querySelector('.wendu-chart') as HTMLElement;if (!chartDom || !chartDomradar || !chartDomwendu) {requestAnimationFrame(waitForContainerAndInitChart);return;}const { width, height } = chartDom.getBoundingClientRect();const { width: wenduWidth, height: wenduHeight } = chartDomwendu.getBoundingClientRect();if (width === 0 || height === 0 || radarWidth === 0 || radarHeight === 0 || wenduWidth === 0 || wenduHeight === 0) {requestAnimationFrame(waitForContainerAndInitChart);return;}
if (props.deviceTypeId === '2') {generateWenduData();}
}
// 生命周期钩子模拟
window.addEventListener('resize', handleResize);
observer.observe(document.documentElement, {attributes: true,attributeFilter: ['class']
});
// 用 requestAnimationFrame 递归检测容器宽高直到可用再初始化 ECharts
// 页面加载和激活时都调用
waitForContainerAndInitChart();
window.addEventListener('pageshow', () => {waitForContainerAndInitChart();
});
// 页面卸载时清理
window.addEventListener('beforeunload', () => {window.removeEventListener('resize', handleResize);if (wenduChart.value) wenduChart.value.dispose && wenduChart.value.dispose();observer.disconnect();
});// 页面加载时请求一次
onMounted(() => {if (props.deviceTypeId === '2') {generateWenduData();} window.addEventListener('resize', handleResize);observer.observe(document.documentElement, {attributes: true,attributeFilter: ['class']});
});onUnmounted(() => {window.removeEventListener('resize', handleResize);if (wenduChart.value) wenduChart.value.dispose && wenduChart.value.dispose();observer.disconnect();
});

http://www.dtcms.com/a/270793.html

相关文章:

  • 【WEB】Polar靶场 21-25题 详细笔记
  • ProxySQL 入门到实战
  • Grafana容器化部署
  • Android-重学kotlin(协程基础)新学习总结
  • 基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(4)集成Allure报表
  • 2.4G收发SOC芯片 XL2417D,集成高性能2.4GHz射频收发器、32位MCU
  • 基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(5)失败用例截图与重试
  • OneCode AIGC时代优秀的三码合一框架实现
  • Python通关秘籍之基础教程(一)
  • 【视频观看系统】- 技术与架构选型
  • 恒盾C#混淆加密大师最新版本1.4.0更新 - 增强各类加密效果, 提升兼容性, 使.NET加密更简单
  • OneCode框架事件基础模型架构深度剖析与代码实现
  • Go语言Gin框架实战:开发技巧
  • PCIe基础知识之Linux内核中PCIe子系统的架构
  • youtube图论
  • 深度解析:将SymPy符号表达式转化为高效NumPy计算函数的通用解决方案
  • 底盘机械臂仿真fetch_gazebo实践
  • 从0开始学习R语言--Day42--LM检验
  • Flume日志采集工具
  • 深入理解图像二值化:从静态图像到视频流实时处理
  • 迁移Oracle SH 示例 schema 到 PostgreSQL
  • qml加载html以及交互
  • python安装pandas模块报错问题
  • Opencv探索之旅:从像素变化到世界轮廓的奥秘
  • Adobe Illustrator 2025 安装图文教程 | 快速上手平面设计
  • 让AI绘图更可控!ComfyUI-Cosmos-Predict2基础使用指南
  • 分治算法---快排
  • ts学习1
  • 宏集案例 | 基于CODESYS的自动化控制系统,开放架构 × 高度集成 × 远程运维
  • 打破传统,开启 AR 智慧课堂​