index.tsx 2.59 KB
Newer Older
niko's avatar
niko committed
1
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
2 3
import Charts from '../Charts';
import { Statistic } from 'antd';
nikogu's avatar
nikogu committed
4 5
import styles from './index.less';

6 7
const { MiniArea } = Charts;

陈帅's avatar
陈帅 committed
8
function fixedZero(val: number) {
9 10 11
  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
  }
陈帅's avatar
陈帅 committed
31 32
  timer: number | undefined;
  requestRef: number | undefined;
nikogu's avatar
nikogu committed
33
  componentWillUnmount() {
jim's avatar
jim committed
34
    clearTimeout(this.timer);
陈帅's avatar
陈帅 committed
35 36 37
    if (this.requestRef) {
      cancelAnimationFrame(this.requestRef);
    }
nikogu's avatar
nikogu committed
38
  }
陈帅's avatar
陈帅 committed
39

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

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

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