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

这篇具有很好参考价值的文章主要介绍了echarts自定义x轴和tooltip数据格式。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

echarts自定义x轴和tooltip数据格式,echarts,后台管理系统,echarts,前端,javascript

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	

echarts自定义x轴和tooltip数据格式,echarts,后台管理系统,echarts,前端,javascript文章来源地址https://www.toymoban.com/news/detail-806166.html

修改后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
            );
        }

到了这里,关于echarts自定义x轴和tooltip数据格式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue中echart的tooltip自定义显示内容

    目的:让tooltip显示自定义格式数据,甚至是显示横纵坐标以外的数据。 1. 先介绍一下常用情况 。 tooltip的trigger有三种选项“item”“axis”“none”,选择axis就是显示横坐标的内容。 这样设置,鼠标滑过对应点时,tooltip就会显示横坐标内容和对应纵坐标的温度和信号。 2. 若要

    2024年02月09日
    浏览(34)
  • Echarts图表,利用formatter自定义tooltip的内容和样式

    在展示多数据图表的时候 有的时候需要图例也展示出一些内容来,例如官方这样子:鼠标悬停的时候展示该点数据 但是,官方提供的样式有时不适用所有的开发场景 我的项目需要实现鼠标悬停在某一点的时候,只展示该条线的数据,以及一些图表中未表现的数据。参照原型

    2024年02月11日
    浏览(57)
  • Echarts图表如何利用formatter自定义tooltip的内容和样式

    在展示多数据图表的时候 有的时候需要图例也展示出一些内容来,例如官方这样子:鼠标悬停的时候展示该点数据 但是,官方提供的样式有时不适用所有的开发场景 我的项目需要实现鼠标悬停在某一点的时候,只展示该条线的数据,以及一些图表中未表现的数据。参照原型

    2024年02月01日
    浏览(30)
  • uniapp 如何使用echarts 以及解决tooltip自定义不生效问题

    使用的是echarts-for-wx插件; 正常写法案例:给tooltip数值加个% 这样的写法formatter自定义是不会生效的; 想要自定义生效的正确写法 顺带提一嘴在开发工具上看会有echarts层级太高遮挡显示层问题;这个问题不必理会,真机上显示是正常的 再使用dataZoom组件的时候会报错e.prev

    2024年01月15日
    浏览(50)
  • vue echarts kline 在tooltip trigger: ‘axis‘时 自定义参数名已经加振幅参数

    1 trigger: \\\'axis\\\' 报错 Cannot read properties of undefined (reading \\\'1\\\') TypeError: Cannot read properties of undefined (reading \\\'1\\\') at formatter (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/components/HelloWorld.vue?vuetype=scriptlang=js:3

    2024年02月13日
    浏览(37)
  • echarts图表的x轴和y轴的配置

    xAxis与yAxis中有很多配置项,下面我以xAxis进行详解,yAxis参考xAxis即可 nameTextStyle:坐标轴名称的文字样式 axisLine:坐标轴轴线相关设置。 axisTick:坐标轴刻度相关设置。 axisLabel:坐标轴刻度标签的相关设置。 splitLine: 坐标轴在 grid 区域中的分隔线设置。 splitArea :坐标轴在

    2024年02月12日
    浏览(24)
  • 【vue后台管理系统】基于Vue+Element-UI+ECharts开发通用管理后台(中)

    点击菜单图标之前: 点击菜单图标之后: 首先我们要知道菜单栏的收缩,由el-menu的collapse属性控制: 我们通过分析可以知道: 菜单按钮的点击是在CommonHeader.vue组件中,而我们修改的collapse属性却在CommonAside.vue中,这是两个不同的组件。很明显这涉及到了组件间的通信问题,

    2024年02月03日
    浏览(38)
  • vue+echarts 实现地图tooltip点击事件;toolTip数据动态渲染;同时鼠标滑过涟漪点时实现地图多区域联动

    最终做出来的效果是这样的: 最近做项目时,遇到这样的需求:         1、toolTip上的数据根据后台动态渲染         2、鼠标移入地图涟漪点时显示tootTip,点击toolTip上的文字,携带动态数据id进行路由跳转         3、鼠标移入地图涟漪点,与涟漪点相关的省份多区域联动

    2024年02月09日
    浏览(43)
  • vue3管理系统中后台返回pdf格式的文件流,前端如何预览?以及uniapp微信小程序中后台返回的base64位的pdf文件如何预览?

    后台返回的是base64格式的pdf文件,首先需要解析base64文件的插件 image-tools 1 安装并引入插件 2 封装预览pdf的函数 3 调用接口获取数据

    2024年01月18日
    浏览(46)
  • 【vue会员管理系统】篇三之自定义Axios、初试后台接口、跨域问题

            因为本项目很多组件需要通过Axios发送一步请求,所以封装Axios对象,自己封装的Axios在后续可以使用axios中提供的拦截器。 1.在src文件夹下创建utils文件夹,再在utils文件夹下创建request.js文件 2.填入以下代码 1.在public文件夹下新建db.json文件,并添加数据。 2.在src文

    2024年02月07日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包