echarts 数据展示 随后端数据变化而变化 showData多的展示为其他

import React, { useEffect, useState } from 'react';
import { connect } from 'dva';
import moment from 'moment';

const showData = [
  {
    date: '20211108',
    industryDetails: [
      {
        industryName: '贸易与零售',
        industryProportion: '5',
      },
      {
        industryName: '轻工',
        industryProportion: '7',
      },
      {
        industryName: '有色金属',
        industryProportion: '9',
      },
      {
        industryName: '国防军工',
        industryProportion: '1',
      },
      {
        industryName: '金融',
        industryProportion: '10',
      },
      {
        industryName: '食品',
        industryProportion: '11',
      },
    ],
  },
  {
    date: '20211115',
    industryDetails: [
      {
        industryName: '贸易与零售',
        industryProportion: '12',
      },
      {
        industryName: '轻工',
        industryProportion: '31',
      },
      {
        industryName: '有色金属',
        industryProportion: '43',
      },
      {
        industryName: '国防军工',
        industryProportion: '22',
      },
      {
        industryName: '金融',
        industryProportion: '43',
      },
      {
        industryName: '食品',
        industryProportion: '21',
      },
    ],
  },
  {
    date: '20211108',
    industryDetails: [
      {
        industryName: '贸易与零售',
        industryProportion: '54',
      },
      {
        industryName: '轻工',
        industryProportion: '34',
      },
      {
        industryName: '有色金属',
        industryProportion: '76',
      },
      {
        industryName: '国防军工',
        industryProportion: '21',
      },
      {
        industryName: '金融',
        industryProportion: '5',
      },
      {
        industryName: '食品',
        industryProportion: '76',
      },
    ],
  },
];

const data = [
  {
    date: '20211108',
    industryDetails: [
      {
        industryName: '贸易与零售',
        industryProportion: '5',
      },
      {
        industryName: '轻工',
        industryProportion: '7',
      },
      {
        industryName: '有色金属',
        industryProportion: '9',
      },
      {
        industryName: '国防军工',
        industryProportion: '1',
      },
    ],
  },
  {
    date: '20211115',
    industryDetails: [
      {
        industryName: '贸易与零售',
        industryProportion: '12',
      },
      {
        industryName: '轻工',
        industryProportion: '31',
      },
      {
        industryName: '有色金属',
        industryProportion: '43',
      },
      {
        industryName: '国防军工',
        industryProportion: '22',
      },
    ],
  },
  {
    date: '20211108',
    industryDetails: [
      {
        industryName: '贸易与零售',
        industryProportion: '54',
      },
      {
        industryName: '轻工',
        industryProportion: '34',
      },
      {
        industryName: '有色金属',
        industryProportion: '76',
      },
      {
        industryName: '国防军工',
        industryProportion: '21',
      },
    ],
  },
];

const Index = (props) => {
  console.log('全部数据:', showData);
  console.log('原始数据:', data);

  const tenpData = [];
  const xAxisData = [];
  (data || []).forEach((element) => { // 获取所有detail数据
    tenpData.push(...element?.industryDetails);
    xAxisData.push(moment(element.date + '').format('YYYY-MM-DD'));
  });
  console.log('x轴时间:', xAxisData);
  console.log('所有详情数据:', tenpData);

  // 将数据处理成echart需要的数据
  const datas = new Map();
  (tenpData || []).forEach((el) => {
    const old = datas.get(el.industryName) || []; // 之前存在的数据
    datas.set(el.industryName, [...old, (el.industryProportion * 100).toFixed(2)]); // 前 后合并 -- 现有数据
  });

  console.log('将数据处理成echart需要的数据:', datas);

  // 重构数据结构
  const result = [];
  (Array.from(datas) || []).forEach((el) => {
    result.push({
      key: el[0],
      value: el[1],
    });
  });

  console.log('重构数据结构:', result);

  const newQryTrendsData = [];
  const lastQryTrendsData = [];
  const other = [];
  (result || []).forEach((e) => {
    // 判断是否展示,还是在其它内
    const flag = showData.includes(e.key);
    if (flag) {
      newQryTrendsData.push(e);
    } else {
      other.push(e.value);
    }
  });

  // 其它数据的处理
  let otherA;
  if (other.length > 0) {
    otherA = other.reduce((r, a) => {
      (a || []).forEach((b, i) => {
        r[i] = (r[i] || 0) + Number(b);
      });
      return r;
    }, []);
  }
  console.log('其它数据的处理:', otherA);

  const obj = {};
  obj.key = '其他';
  obj.value = otherA || [];
  lastQryTrendsData.push(obj);
  console.log('最终展示数据:', lastQryTrendsData);

  return (
    <div>
      原始数据:
    </div>
  );
};

export default connect(
  ({ testM, multiAssetWorkbench, loading }) => ({ testM, multiAssetWorkbench, loading }),
  null,
)(Index);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值