index.js 2.13 KB
Newer Older
niko's avatar
niko committed
1
import React, { Component } from 'react';
nikogu's avatar
nikogu committed
2

偏右's avatar
偏右 committed
3 4
import { MiniArea } from '../Charts';
import NumberInfo from '../NumberInfo';
nikogu's avatar
nikogu committed
5 6 7

import styles from './index.less';

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);
nikogu's avatar
nikogu committed
34
  }
jim's avatar
jim committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48
  loopData = () => {
    this.timer = setTimeout(() => {
      this.setState(
        {
          activeData: getActiveData(),
        },
        () => {
          requestAnimationFrame(() => {
            this.loopData();
          });
        }
      );
    }, 1000);
  };
nikogu's avatar
nikogu committed
49 50 51 52 53
  render() {
    const { activeData = [] } = this.state;

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