index.js 2.48 KB
Newer Older
niko's avatar
niko committed
1 2
import React, { Component } from 'react';
import { Chart, Axis, Tooltip, Geom } from 'bizcharts';
3
import Debounce from 'lodash-decorators/debounce';
afc163's avatar
afc163 committed
4
import Bind from 'lodash-decorators/bind';
niko's avatar
niko committed
5
import autoHeight from '../autoHeight';
6 7
import styles from '../index.less';

niko's avatar
niko committed
8 9
@autoHeight()
class Bar extends Component {
10 11
  state = {
    autoHideXLabels: false,
niko's avatar
niko committed
12
  };
13

14
  componentDidMount() {
15
    window.addEventListener('resize', this.resize);
16 17
  }

18
  componentWillUnmount() {
19
    window.removeEventListener('resize', this.resize);
20 21
  }

afc163's avatar
afc163 committed
22
  @Bind()
niko's avatar
niko committed
23
  @Debounce(400)
afc163's avatar
afc163 committed
24
  resize() {
25 26 27
    if (!this.node) {
      return;
    }
jim's avatar
jim committed
28 29 30 31 32 33 34 35
    requestAnimationFrame(() => {
      const canvasWidth = this.node.parentNode.clientWidth;
      const { data = [], autoLabel = true } = this.props;
      if (!autoLabel) {
        return;
      }
      const minWidth = data.length * 30;
      const { autoHideXLabels } = this.state;
36

jim's avatar
jim committed
37 38 39 40 41 42 43
      if (canvasWidth <= minWidth) {
        if (!autoHideXLabels) {
          this.setState({
            autoHideXLabels: true,
          });
        }
      } else if (autoHideXLabels) {
44
        this.setState({
jim's avatar
jim committed
45
          autoHideXLabels: false,
46 47
        });
      }
jim's avatar
jim committed
48
    });
49 50
  }

jim's avatar
jim committed
51
  handleRoot = n => {
niko's avatar
niko committed
52 53 54
    this.root = n;
  };

jim's avatar
jim committed
55
  handleRef = n => {
56
    this.node = n;
niko's avatar
niko committed
57
  };
58

niko's avatar
niko committed
59
  render() {
nikogu's avatar
nikogu committed
60 61 62 63 64 65 66 67
    const {
      height,
      title,
      forceFit = true,
      data,
      color = 'rgba(24, 144, 255, 0.85)',
      padding,
    } = this.props;
68

niko's avatar
niko committed
69
    const { autoHideXLabels } = this.state;
70

niko's avatar
niko committed
71
    const scale = {
72 73 74 75 76 77
      x: {
        type: 'cat',
      },
      y: {
        min: 0,
      },
niko's avatar
niko committed
78
    };
79

niko's avatar
niko committed
80 81 82 83 84 85 86
    const tooltip = [
      'x*y',
      (x, y) => ({
        name: x,
        value: y,
      }),
    ];
87 88

    return (
niko's avatar
niko committed
89 90
      <div className={styles.chart} style={{ height }} ref={this.handleRoot}>
        <div ref={this.handleRef}>
nikogu's avatar
nikogu committed
91
          {title && <h4 style={{ marginBottom: 20 }}>{title}</h4>}
niko's avatar
niko committed
92 93
          <Chart
            scale={scale}
nikogu's avatar
nikogu committed
94
            height={title ? height - 41 : height}
niko's avatar
niko committed
95 96 97 98
            forceFit={forceFit}
            data={data}
            padding={padding || 'auto'}
          >
nikogu's avatar
nikogu committed
99 100 101 102 103 104 105
            <Axis
              name="x"
              title={false}
              label={autoHideXLabels ? false : {}}
              tickLine={autoHideXLabels ? false : {}}
            />
            <Axis name="y" min={0} />
niko's avatar
niko committed
106 107 108
            <Tooltip showTitle={false} crosshairs={false} />
            <Geom type="interval" position="x*y" color={color} tooltip={tooltip} />
          </Chart>
109 110 111 112 113 114 115
        </div>
      </div>
    );
  }
}

export default Bar;