index.js 2.19 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 6

import styles from './index.less';

7 8 9 10
function fixedZero(val) {
  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
  };
nikogu's avatar
nikogu committed
26 27

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

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

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

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

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