index.js 2.45 KB
Newer Older
niko's avatar
niko committed
1
import React, { Component } from 'react';
偏右's avatar
偏右 committed
2 3
import { MiniArea } from '../Charts';
import NumberInfo from '../NumberInfo';
nikogu's avatar
nikogu committed
4 5
import styles from './index.less';

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

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

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

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

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

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

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

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