index.tsx 2.57 KB
Newer Older
niko's avatar
niko committed
1
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
2

陈帅's avatar
陈帅 committed
3
import { Statistic } from 'antd';
陈帅's avatar
陈帅 committed
4
import { MiniArea } from '../Charts';
nikogu's avatar
nikogu committed
5 6
import styles from './index.less';

陈帅's avatar
陈帅 committed
7
function fixedZero(val: number) {
8 9 10
  return val * 1 < 10 ? `0${val}` : val;
}

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

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

nikogu's avatar
nikogu committed
27
  componentDidMount() {
jim's avatar
jim committed
28
    this.loopData();
nikogu's avatar
nikogu committed
29
  }
陈帅's avatar
陈帅 committed
30

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

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

陈帅's avatar
陈帅 committed
53 54 55 56
  timer: number | undefined;

  requestRef: number | undefined;

nikogu's avatar
nikogu committed
57 58 59 60 61
  render() {
    const { activeData = [] } = this.state;

    return (
      <div className={styles.activeChart}>
陈帅's avatar
陈帅 committed
62
        <Statistic title="目标评估" value="有望达到预期" />
nikogu's avatar
nikogu committed
63 64 65 66
        <div style={{ marginTop: 32 }}>
          <MiniArea
            animate={false}
            line
niko's avatar
niko committed
67
            borderWidth={2}
nikogu's avatar
nikogu committed
68
            height={84}
niko's avatar
niko committed
69 70 71 72 73
            scale={{
              y: {
                tickCount: 3,
              },
            }}
nikogu's avatar
nikogu committed
74
            yAxis={{
陈帅's avatar
陈帅 committed
75 76 77 78
              tickLine: undefined,
              label: undefined,
              title: undefined,
              line: undefined,
nikogu's avatar
nikogu committed
79 80 81 82
            }}
            data={activeData}
          />
        </div>
niko's avatar
niko committed
83
        {activeData && (
84 85 86 87 88 89 90 91 92 93 94
          <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
95 96 97 98 99 100 101 102 103
          </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
104 105 106 107
      </div>
    );
  }
}