解决echartsY轴溢出以及多Y轴动态设置offest

一、描述

由于上一篇文章已经介绍了处理方法,这篇文章主要展示处理数据的完整代码

1、处理数据的hooks

import http from '../../../../utils';

import rest from '../../../../common/rest.json';

import dayjs from 'dayjs';

import Utils from '../../../../tools'

import { useState } from 'react';

const useMultidimensionAlanalysisApi = (baseUrl) => {

  const [tableData, setTableData] = useState([]); // 图表数据

  const [loading, setLoading] = useState(false)

  const [seriesData, setSeriesData] = useState([]);

  const [Xdata, setXData] = useState([]);

  const [lengedData, setLengedData] = useState([]);

  const color = ['#FFC627', '#7EB338', '#009FF5', "#5E7DEC", "#BE271C"]

  // 处理图表数据

  const [yAxisArr, setYAxisArr] = useState([])

  // 获取数据

  const getData = (index, data, timeRange) => {

    if (!data.length) return;

    setLoading(true)

    http.post(baseUrl + rest.energy.multidimension.pointData, {

      pointNames: data.map((el) => el.text),

      startTime: dayjs(timeRange[0]).format('YYYY-MM-DD 00:00:00'),

      endTime: dayjs(timeRange[1]).format('YYYY-MM-DD 23:59:59')

    }).then((res) => {

      formatData(res.data.data);

      setLoading(false)

    }).catch((e) => {

      console.log(e);

    });

  };

  const formatData = (lineData) => {

    if (lineData.length) {

      lineData = lineData.sort(function (a, b) {

        return b.unit.length  - a.unit.length

      })

      const lengedArr = [];

      let yAxisData = []

      let timeData = [];

      let unitData = []

      lineData.forEach((el) => {

        el.samples = el.samples.map(el => ({

          ...el,

          value: el.value,

          collectTime: Date.parse(el.collectTime)

        })).sort(function (a, b) {

          return a.collectTime - b.collectTime

        })

        timeData = el.samples.map(el => dayjs(el.collectTime).format("YYYY-MM-DD HH:mm"))

        unitData.push(el?.unit)

      });

      setTableData(lineData)

      unitData = [...new Set(unitData)]

      setXData(timeData);

      unitData.forEach((el, index) => {

        yAxisData.push({

          position: index === 0 ? "left" : "right",

          // offset: formatdDistance(index),

          type: 'value',

          name: el,

          nameTextStyle: {

            color: "#A0A0A0",

            fontSize: '.875rem',

            padding: index === 0 ? [0, 35, 15, 0] : [0, 0, 15, 40]

          },

          alignTicks: true, // !!配置多坐标轴标签对齐

          splitLine: {

         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值