index.js 2.46 KB
Newer Older
niko's avatar
niko committed
1
import React, { Component } from 'react';
愚道's avatar
愚道 committed
2
import { NumberInfo, Charts } from 'ant-design-pro';
nikogu's avatar
nikogu committed
3 4 5

import styles from './index.less';

6
const { MiniArea } = Charts;
愚道's avatar
愚道 committed
7

8 9 10 11
function fixedZero(val) {
  return val * 1 < 10 ? `0${val}` : val;
}

nikogu's avatar
nikogu committed
12 13 14 15 16
function getActiveData() {
  const activeData = [];
  for (let i = 0; i < 24; i += 1) {
    activeData.push({
      x: `${fixedZero(i)}:00`,
jim's avatar
jim committed
17
      y: Math.floor(Math.random() * 200) + i * 50,
nikogu's avatar
nikogu committed
18 19 20 21 22
    });
  }
  return activeData;
}

niko's avatar
niko committed
23
export default class ActiveChart extends Component {
nikogu's avatar
nikogu committed
24 25
  state = {
    activeData: getActiveData(),
niko's avatar
niko committed
26
  };
nikogu's avatar
nikogu committed
27 28

  componentDidMount() {
jim's avatar
jim committed
29
    this.loopData();
nikogu's avatar
nikogu committed
30 31
  }

nikogu's avatar
nikogu committed
32
  componentWillUnmount() {
jim's avatar
jim committed
33
    clearTimeout(this.timer);
陈帅's avatar
陈帅 committed
34
    cancelAnimationFrame(this.requestRef);
nikogu's avatar
nikogu committed
35
  }
陈帅's avatar
陈帅 committed
36

jim's avatar
jim committed
37
  loopData = () => {
陈帅's avatar
陈帅 committed
38
    this.requestRef = requestAnimationFrame(() => {
陈帅's avatar
陈帅 committed
39 40 41 42 43 44
      this.timer = setTimeout(() => {
        this.setState(
          {
            activeData: getActiveData(),
          },
          () => {
jim's avatar
jim committed
45
            this.loopData();
陈帅's avatar
陈帅 committed
46 47 48 49
          }
        );
      }, 1000);
    });
jim's avatar
jim committed
50
  };
陈帅's avatar
陈帅 committed
51

nikogu's avatar
nikogu committed
52 53 54 55 56
  render() {
    const { activeData = [] } = this.state;

    return (
      <div className={styles.activeChart}>
niko's avatar
niko committed
57
        <NumberInfo subTitle="目标评估" total="有望达到预期" />
nikogu's avatar
nikogu committed
58 59 60 61
        <div style={{ marginTop: 32 }}>
          <MiniArea
            animate={false}
            line
niko's avatar
niko committed
62
            borderWidth={2}
nikogu's avatar
nikogu committed
63
            height={84}
niko's avatar
niko committed
64 65 66 67 68
            scale={{
              y: {
                tickCount: 3,
              },
            }}
nikogu's avatar
nikogu committed
69 70
            yAxis={{
              tickLine: false,
niko's avatar
niko committed
71
              label: false,
nikogu's avatar
nikogu committed
72 73 74 75 76 77
              title: false,
              line: false,
            }}
            data={activeData}
          />
        </div>
niko's avatar
niko committed
78
        {activeData && (
79 80 81 82 83 84 85 86 87 88 89
          <div>
            <div className={styles.activeChartGrid}>
              <p>{[...activeData].sort()[activeData.length - 1].y + 200} 亿元</p>
              <p>{[...activeData].sort()[Math.floor(activeData.length / 2)].y} 亿元</p>
            </div>
            <div className={styles.dashedLine}>
              <div className={styles.line} />
            </div>
            <div className={styles.dashedLine}>
              <div className={styles.line} />
            </div>
niko's avatar
niko committed
90 91 92 93 94 95 96 97 98
          </div>
        )}
        {activeData && (
          <div className={styles.activeChartLegend}>
            <span>00:00</span>
            <span>{activeData[Math.floor(activeData.length / 2)].x}</span>
            <span>{activeData[activeData.length - 1].x}</span>
          </div>
        )}
nikogu's avatar
nikogu committed
99 100 101 102
      </div>
    );
  }
}