index.js 2.42 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() {
jim's avatar
jim committed
15
    window.addEventListener('resize', this.resize, { passive: true });
16 17
  }

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

22 23 24 25 26 27 28 29
  handleRoot = n => {
    this.root = n;
  };

  handleRef = n => {
    this.node = n;
  };

afc163's avatar
afc163 committed
30
  @Bind()
niko's avatar
niko committed
31
  @Debounce(400)
afc163's avatar
afc163 committed
32
  resize() {
33 34 35
    if (!this.node) {
      return;
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
36 37 38 39 40 41 42
    const canvasWidth = this.node.parentNode.clientWidth;
    const { data = [], autoLabel = true } = this.props;
    if (!autoLabel) {
      return;
    }
    const minWidth = data.length * 30;
    const { autoHideXLabels } = this.state;
43

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44 45
    if (canvasWidth <= minWidth) {
      if (!autoHideXLabels) {
46
        this.setState({
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
47
          autoHideXLabels: true,
48 49
        });
      }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50 51 52 53 54
    } else if (autoHideXLabels) {
      this.setState({
        autoHideXLabels: false,
      });
    }
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;