index.js 2.41 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
  render() {
nikogu's avatar
nikogu committed
58 59 60 61 62 63 64 65
    const {
      height,
      title,
      forceFit = true,
      data,
      color = 'rgba(24, 144, 255, 0.85)',
      padding,
    } = this.props;
66

niko's avatar
niko committed
67
    const { autoHideXLabels } = this.state;
68

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

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

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

export default Bar;