index.js 2.27 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    if (!this.node) {
      return;
    }
    const canvasWidth = this.node.parentNode.clientWidth;
    const { data = [], autoLabel = true } = this.props;
    if (!autoLabel) {
      return;
    }
    const minWidth = data.length * 30;
    const { autoHideXLabels } = this.state;

    if (canvasWidth <= minWidth) {
      if (!autoHideXLabels) {
        this.setState({
          autoHideXLabels: true,
        });
      }
    } else if (autoHideXLabels) {
      this.setState({
        autoHideXLabels: false,
      });
    }
  }

niko's avatar
niko committed
49 50 51 52
  handleRoot = (n) => {
    this.root = n;
  };

53 54
  handleRef = (n) => {
    this.node = n;
niko's avatar
niko committed
55
  };
56

niko's avatar
niko committed
57 58
  render() {
    const { height, title, forceFit = true, data, color = 'rgba(24, 144, 255, 0.85)', padding } = this.props;
59

niko's avatar
niko committed
60
    const { autoHideXLabels } = this.state;
61

niko's avatar
niko committed
62
    const scale = {
63 64 65 66 67 68
      x: {
        type: 'cat',
      },
      y: {
        min: 0,
      },
niko's avatar
niko committed
69
    };
70

niko's avatar
niko committed
71 72 73 74 75 76 77
    const tooltip = [
      'x*y',
      (x, y) => ({
        name: x,
        value: y,
      }),
    ];
78 79

    return (
niko's avatar
niko committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      <div className={styles.chart} style={{ height }} ref={this.handleRoot}>
        <div ref={this.handleRef}>
          {title && <h4>{title}</h4>}
          <Chart
            scale={scale}
            height={height}
            forceFit={forceFit}
            data={data}
            padding={padding || 'auto'}
          >
            <Axis name="x" title={false} label={!autoHideXLabels} tickLine={!autoHideXLabels} />
            <Axis name="y" title={false} line={false} tickLine={false} min={0} />
            <Tooltip showTitle={false} crosshairs={false} />
            <Geom type="interval" position="x*y" color={color} tooltip={tooltip} />
          </Chart>
95 96 97 98 99 100 101
        </div>
      </div>
    );
  }
}

export default Bar;