echarts将展示全天的数据,如一天的电费,一个停车场一天的饱和度等问题

本文主要是介绍echarts将展示全天的数据,如一天的电费,一个停车场一天的饱和度等问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

项目场景:

我们的项目是通过ai识别停车场的停车数,来展示此停车场全天的饱和度,如下
在这里插入图片描述


问题描述

后台接口给的数据,就是这种,返回所有有停车数量的时间段,但是我们的x轴要求展示全天的数据,并且可伸缩刻度展示具体时间的停车情况

[{time:'2023-10-27 08:20:20',carSaturation:100,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 09:20:20',carSaturation:60,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 10:20:20',carSaturation:20,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 10:25:20',carSaturation:50,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 10:50:20',carSaturation:70,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 11:10:20',carSaturation:20,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 11:20:20',carSaturation:90,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 11:40:20',carSaturation:0,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 12:50:20',carSaturation:20,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 13:50:20',carSaturation:80,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 14:50:20',carSaturation:30,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 15:50:20',carSaturation:110,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 16:50:20',carSaturation:70,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},]

自己定义x轴时就会导致x轴与y轴数据关联不上,出现如下的情况


解决方案:

将拿到的数据进行转化,转换为如下格式,才能正常渲染
在这里插入图片描述
第一步:将时间转换为时间戳并转为数组项格式

    dataH.forEach((item,i) => {// 将时间转换为时间戳item.time = new Date(item.time).getTime()let arr = Object.values(item)arr.pop()xAxisD = arrseriesD.push(xAxisD);});

第二步:xAxis的属性axisLabel设置格式化显示时间的属性,注意安装插件moment,设置min和max最大和最小值,不然就只展示有数据的时间轴,如下图
在这里插入图片描述
代码如下

 axisLabel: {// 格式化x轴显示formatter: function(value, index) {// 如果时间是 23:59:59 , 格式化为 24:00if (value === new Date(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')).getTime()) {return moment(value).format("24:00");} else {// 其他的时间返回格式化 00:00return moment(value).format("HH:mm");}}},interval: 3600 * 2 * 1000, // 设置x轴分隔间隔,我使用的是毫秒时间戳间隔两小时,如使用秒时间戳不需要x1000min: function (value) {// 设置x轴最小值,为当天00:00:00时时间戳// 若想要将time改为x轴数据最小值,则var time = moment(value.min).format('YYYY-MM-DD HH:mm:ss');var time = moment().startOf('day').format('YYYY-MM-DD HH:mm:ss');return new Date(time).getTime();},max: function (value) {// 设置x轴最大值,为当天23:59:59时时间戳// 若想要将time改为x轴数据最大值,则var time = moment(value.max).format('YYYY-MM-DD HH:mm:ss');var time = moment().endOf('day').format('YYYY-MM-DD HH:mm:ss');return new Date(time).getTime();}

第三步:设置时间轴的缩放及曲线不同值得不同颜色展示

   dataZoom: {type: 'inside',  //放大缩小x轴数值},

区间值是0-125

visualMap: {show:false,pieces: [{gt: 0,lte: 25,color: '#87E5FF'}, {gt: 25,lte: 50,color: '#FAFF6F'},{gt: 50,lte: 100,color: '#FF9921'},{gt: 100,lte: 125,color: '#F83F3F'}]},

具体的代码如下

<template><div class="echartBox"><divclass="echartT3"id="echart"></div></div>
</template>
<script>
import moment from 'moment'; 
import { formatTime } from "@/utils/index.js";
export default {data() {return {};},props:{historyData:{type:Array,default:[]},},mounted() {this.initEchart();},watch:{//观察option的变化option: {handler(newVal, oldVal) {//数据自动刷新,必然需要一个监听机制告诉Echarts重新设置数据if (this.myChart) {if (newVal) {this.myChart.setOption(newVal);} else {this.myChart.setOption(oldVal);}} else {this.initEchart();}},deep: true //对象内部属性的监听,关键。}},computed:{option(){// let dataH = this.historyData;//后台数据let dataH =[{time:'2023-10-27 08:20:20',carSaturation:100,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 09:20:20',carSaturation:60,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 10:20:20',carSaturation:20,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 10:25:20',carSaturation:50,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 10:50:20',carSaturation:70,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 11:10:20',carSaturation:20,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 11:20:20',carSaturation:90,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 11:40:20',carSaturation:0,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 12:50:20',carSaturation:20,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 13:50:20',carSaturation:80,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 14:50:20',carSaturation:30,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 15:50:20',carSaturation:110,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},{time:'2023-10-27 16:50:20',carSaturation:70,analysisImg:require('@/assets/AIRecognition/aiPhoto.png')},]let xAxisD=[];let seriesD=[];dataH.forEach((item,i) => {// 将时间转换为时间戳item.time = new Date(item.time).getTime()let arr = Object.values(item)arr.pop()xAxisD = arrseriesD.push(xAxisD);});return {tooltip: {trigger: 'item',borderRadius: 8,//边框圆角backgroundColor: 'rgba(11, 66, 131, 1)',//背景颜色(此时为默认色)borderColor: 'rgba(11, 66, 131, 1)',formatter: function(params) { var res = formatTime(params.data[0])+'&nbsp;'+'停车饱和度:'+params.data[1]+'%'+'<br/>';  //电厂名称dataH.forEach(ite=>{if(ite.time == params.data[0]){res+="<img style='width:139px;height:79px;' src='"+ ite.analysisImg+"'/>";}})return res;  },textStyle:{color:'#ffff',align:'center',fontSize: 18,}					},grid: {top: '30%',left: '2%',right: '3%',bottom: '8%',containLabel: true},xAxis: {axisLabel: {textStyle: {color: 'rgba(255, 255, 255, 0.4)'},},  axisLine: {lineStyle: {color:  'rgba(255, 255, 255, 0.8)',width: 1,}},boundaryGap: true,show: true,type: "time", // 这里使用时间轴模式axisLabel: {// 格式化x轴显示formatter: function(value, index) {// 如果时间是 23:59:59 , 格式化为 24:00if (value === new Date(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')).getTime()) {return moment(value).format("24:00");} else {// 其他的时间返回格式化 00:00return moment(value).format("HH:mm");}}},interval: 3600 * 2 * 1000, // 设置x轴分隔间隔,我使用的是毫秒时间戳间隔两小时,如使用秒时间戳不需要x1000min: function (value) {// 设置x轴最小值,为当天00:00:00时时间戳// 若想要将time改为x轴数据最小值,则var time = moment(value.min).format('YYYY-MM-DD HH:mm:ss');var time = moment().startOf('day').format('YYYY-MM-DD HH:mm:ss');return new Date(time).getTime();},max: function (value) {// 设置x轴最大值,为当天23:59:59时时间戳// 若想要将time改为x轴数据最大值,则var time = moment(value.max).format('YYYY-MM-DD HH:mm:ss');var time = moment().endOf('day').format('YYYY-MM-DD HH:mm:ss');return new Date(time).getTime();}},dataZoom: {type: 'inside',  //放大缩小x轴数值},yAxis: {name:'饱和度(%)',type:'value',nameTextStyle: {color: "#fff",nameLocation: "start",},min:0, //y轴的最小值max:125, //y轴最大值 interval:25, //值之间的间隔type: 'value',splitLine :{lineStyle:{type:'dashed',//虚线color: 'rgba(255, 255, 255, 0.4)'},show: true //隐藏},axisLabel: {textStyle: {color: 'rgba(255, 255, 255, 0.4)'}},  },visualMap: {show:false,pieces: [{gt: 0,lte: 25,color: '#87E5FF'}, {gt: 25,lte: 50,color: '#FAFF6F'},{gt: 50,lte: 100,color: '#FF9921'},{gt: 100,lte: 125,color: '#F83F3F'}]},series: [{data:seriesD,type: 'line',smooth: false,//折线是直线还是曲线legend:{show:false,},}, ],}}},methods: {initEchart(){this.myChart = this.$echarts.init(document.getElementById('echart'));this.myChart.setOption(this.option);},}
};
</script>
<style scoped lang="scss">
.echartT3 {height: 130px;width: 100%;
}
</style>

引入及使用请参考博客echart的数据渲染,option不刷新问题

这篇关于echarts将展示全天的数据,如一天的电费,一个停车场一天的饱和度等问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/292227

相关文章

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语