首页 > 其他分享 >echarts自定义x轴和tooltip数据格式

echarts自定义x轴和tooltip数据格式

时间:2024-08-05 10:42:04浏览次数:7  
标签:01 自定义 18 timeValue 2024 areaNorm 12 数据格式 echarts

echarts自定义x轴和tooltip数据格式

在这里插入图片描述

x轴和y轴数据格式如下

x: [0, 1, 2, 3, 4, 5, 6, ....., 23],
y: [2.5, 3.1, 3.2, 2.2, 2.3, 3.1, 3.1, null, null, null, .... ,null]
// 接口返回0-23点的数据,每一个小时一个间隔,没有的话则为null	

在这里插入图片描述

修改后xy轴数据格式如下

// 每五分钟一个数据
[
        {
            "timeValue": "2024-01-18 00:00:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 00:05:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 00:10:11",
            "areaNorm": 1.4
        },
        {
            "timeValue": "2024-01-18 00:15:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 00:20:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 00:25:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 00:30:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 00:35:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 00:40:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 00:45:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 00:50:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 00:55:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 01:00:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 01:05:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 01:10:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 01:15:11",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 01:20:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 01:25:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 01:30:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 01:35:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 01:40:11",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 01:45:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 01:50:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 01:55:13",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 02:00:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:05:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:10:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:15:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:20:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:25:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:30:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:35:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:40:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:45:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:50:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 02:55:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 03:00:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 03:05:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 03:10:12",
            "areaNorm": 0.2
        },
        {
            "timeValue": "2024-01-18 03:15:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 03:20:13",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 03:25:11",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 03:30:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 03:35:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 03:40:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 03:45:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 03:50:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 03:55:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 04:00:12",
            "areaNorm": 0.87
        },
        {
            "timeValue": "2024-01-18 04:05:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 04:10:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 04:15:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 04:20:12",
            "areaNorm": 0.87
        },
        {
            "timeValue": "2024-01-18 04:25:12",
            "areaNorm": 0.87
        },
        {
            "timeValue": "2024-01-18 04:30:12",
            "areaNorm": 1.4
        },
        {
            "timeValue": "2024-01-18 04:35:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 04:40:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 04:45:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 04:50:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 04:55:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 05:00:13",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 05:05:13",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 05:10:12",
            "areaNorm": 0.27
        },
        {
            "timeValue": "2024-01-18 05:15:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 05:20:12",
            "areaNorm": 1.0
        },
        {
            "timeValue": "2024-01-18 05:25:12",
            "areaNorm": 1.0
        }
	]

需求:

将json数据转成折线图需要的数据格式,并且x轴始终为0 - 23 点,并且折线图上的点数不会缺失,对应时间有点位的就展示,没有就空
 getChartsData(areaType, areaCode) {
      return new Promise((resolve, reject) => {
        // areaType:1 道路 2 区域
        selectDayAreaNorm({ areaType, areaCode }).then((res) => {
          if (res.code === 1) {
            this.chartsData = {
              // 初始化数据(五分钟一个点,24 * 12)
              roadNorm: Array.from({ length: 24 * 12 }, () => null), // Initialize with [null, null, ..., null]
              time: Array.from({ length: 24 * 12 }, (_, i) => i / 12), // Initialize with [0, 0.0833, ..., 23.9167]
            };
            if (res.data && res.data.length > 0) {
              res.data.forEach((item) => {
                const timeValue = new Date(item.timeValue);
                const index = Math.floor(
                  timeValue.getHours() * 12 + timeValue.getMinutes() / 5
                );
                if (index >= 0 && index < this.chartsData.time.length) {
                  this.chartsData.roadNorm[index] = item.areaNorm;
                }
              });
            }
            resolve();
          } else {
            reject();
          }
        });
      })
        .then(() => {
          if (this.chartsData) {
            this.initChartsBox();
          }
        })
        .catch((e) => {
          console.warn("Error", e);
        });
    },

    initChartsBox() {
      this.option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            animation: false,
          },
          formatter: function (params) {
            const index = params[0].dataIndex;
            const hour = Math.floor(index / 12);
            const minute = (index % 12) * 5;
            const formattedHour = String(hour).padStart(2, "0");
            const formattedMinute = String(minute).padStart(2, "0");
            const timeStr = `${formattedHour}:${formattedMinute}`;
            const congestionIndex = params[0].value;
            const formattedIndex = congestionIndex?.toFixed(2) || "-";
            const circleIcon =
              '<span style="display:inline-block;margin-right:5px;border-radius:50%;width:8px;height:8px;background-color:#5470c6;"></span>';
            return `${timeStr}<br>${circleIcon}拥堵指数: ${formattedIndex}`;
          },
        },
        axisPointer: {
          link: {
            xAxisIndex: "all",
          },
        },
        dataZoom: [
          {
            show: false,
            realtime: true,
            start: 0,
            end: 100,
            // height: 14,
            xAxisIndex: [0, 1],
            zoomLock: true,
          },
          {
            type: "inside",
            realtime: true,
            start: 0,
            end: 100,
            xAxisIndex: [0, 1],
            zoomLock: true,
          },
        ],
        grid: [
          {
            top: 6,
            bottom: 40,
            left: 40,
            right: 40,
          },
          {
            left: 40,
            right: 40,
          },
        ],
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            axisLine: {
              onZero: true,
            },
            data: this.chartsData.time,
            axisLabel: {
              formatter: function (value, index) {
                const hour = Math.floor(value);
                return hour + ":00";
              },
            },
          },
          {
            gridIndex: 1,
          },
        ],

        yAxis: [
          {
            type: "value",
            min: 0,
            max: 10,
            // name: '时间:',
          },
          {
            gridIndex: 1,
          },
        ],
        series: [
          {
            name: "拥堵指数",
            type: "line",
            smooth: true,
            symbol: "circle",
            symbolSize: 9,
            showSymbol: false,
            lineStyle: {
              normal: {
                width: 2,
                color: "#5470c6",
              },
            },
            markPoint: {
              data: [
                {
                  type: "max",
                  name: "最大值",
                },
                {
                  type: "min",
                  name: "最小值",
                },
              ],
            },
            markArea: {
              silent: true,
              label: {
                normal: {
                  position: ["4%", "50%"],
                },
              },
              data: [
                [
                  {
                    // name: '畅通',
                    yAxis: 0,
                    itemStyle: {
                      color: "rgb(0, 128, 0)",
                    },
                  },
                  {
                    yAxis: 2,
                  },
                ],
                [
                  {
                    // name: '基本畅通',
                    yAxis: 2,
                    itemStyle: {
                      color: "rgb(153, 204, 0)",
                    },
                  },
                  {
                    yAxis: 4,
                  },
                ],
                [
                  {
                    // name: '轻度拥堵',
                    yAxis: 4,
                    itemStyle: {
                      color: "rgb(255, 255, 0)",
                    },
                  },
                  {
                    yAxis: 6,
                  },
                ],
                [
                  {
                    // name: '中度拥堵',
                    yAxis: 6,
                    itemStyle: {
                      color: "rgb(255, 153, 0)",
                    },
                  },
                  {
                    yAxis: 8,
                  },
                ],
                [
                  {
                    // name: '严重拥堵',
                    yAxis: 8,
                    itemStyle: {
                      color: "rgb(255, 0, 0)",
                    },
                  },
                  {
                    yAxis: 10,
                  },
                ],
              ],
            },
            // data: this.chartsData.roadNorm,
            data: this.chartsData.roadNorm.map((value, index) => ({
              value,
              time: this.chartsData.time[index],
            })), // 数据初始化(tooltip中使用formatter)
          },
        ],
      };
      this.myChart = this.$echarts.init(document.getElementById("chartsBox")); // 图标初始化
      this.myChart.setOption(this.option); // 渲染页面
      /* ECharts动态效果 */
      window.addEventListener("resize", () => {
        this.myChart.resize();
      });
    },
    
        mounted() {
            this.getChartsData(
                this.$store.state.monitorLeftComp.areaType,
                this.$store.state.monitorLeftComp.areaCode
            );
        }

标签:01,自定义,18,timeValue,2024,areaNorm,12,数据格式,echarts
From: https://www.cnblogs.com/ProgrammerMao-001/p/18342760

相关文章

  • echarts折线组件
    echarts折线组件echarts折线组件<template><divclass="lineChartsTemplate":id="chartsId"></div></template><script>exportdefault{name:"lineChartsTemplate",components:{},props:{......
  • echarts设置tooltip遇到值为0不展示的问题(已解决)
    echarts设置tooltip遇到值为0不展示的问题(已解决)遇到值为0时tooltip:{trigger:"axis",extraCssText:"z-index:3",axisPointer:{type:"shadow",//默认为直线,可选为:'line'|'shadow'......
  • echarts设置tooltip的层级
    echarts设置tooltip的层级tooltip:{trigger:"axis",extraCssText:'z-index:3',//修改层级borderColor:"rgba(0,170,255)",},完整的option示例如下:option={tooltip:{trig......
  • vue + quill2.0+ 工具栏自定义行高
    在网上查了好多,基本都是基于1.0+版本的,拿过来都用不了,官方又没有文档,只能参考各位前辈的经验+解析源码查找问题。目前已经解决,下面是实现过程。实现代码  先看效果图我用的是原生quill库,正常引入quill,注册行高插件importQuillfrom'quill'import'quill/dist/quill.......
  • ECharts与数据可视化:如何高效使用JavaScript实现复杂图表
    ECharts与数据可视化:如何高效使用JavaScript实现复杂图表大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!在现代网页开发中,数据可视化已成为展示复杂数据的关键手段。ECharts是一个强大的JavaScript图表库,可以帮助开发者轻松实现各种复杂的图表。本文......
  • 打造Perl中的词法分析器:深入自定义文本处理
    打造Perl中的词法分析器:深入自定义文本处理Perl作为一种强大的文本处理语言,提供了丰富的工具来实现词法分析器(Lexer)。词法分析是编译原理中将源代码分解成一系列词素(Tokens)的过程,是构建编译器或解释器的第一步。本文将详细探讨如何在Perl中实现一个自定义的词法分析器,包括......
  • 守护数据堡垒:SQL Server数据库自定义备份审计实现指南
    标题:守护数据堡垒:SQLServer数据库自定义备份审计实现指南引言数据库备份是确保数据安全和业务连续性的关键措施。SQLServer提供了多种备份策略,但有时候,为了满足特定的合规性要求或业务需求,我们需要实现更细粒度的自定义数据备份审计。本文将详细介绍如何在SQLServer中......
  • 自定义导航栏兼容ios和android
    <template>  <view class="content">    <!--距离顶部的距离刚好留出状态栏即可即statusBarHeight-->    <view class="topNav" :style="{height:navHeight+'px',paddingTop:statusBarHeight+'px'}">      <......
  • 从数据爬取到可视化展示:Flask框架与ECharts深度解析
    目录......
  • 数据安全堡垒:构建SQL Server自定义数据安全策略
    数据安全堡垒:构建SQLServer自定义数据安全策略在数字化时代,数据安全是企业的生命线。SQLServer作为企业级数据库解决方案,提供了丰富的安全特性来保护数据。然而,面对复杂的业务需求和不断演变的安全威胁,自定义数据安全策略显得尤为重要。本文将详细探讨如何在SQLServer中......