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
  }

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;
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
28 29 30 31 32 33 34
    const canvasWidth = this.node.parentNode.clientWidth;
    const { data = [], autoLabel = true } = this.props;
    if (!autoLabel) {
      return;
    }
    const minWidth = data.length * 30;
    const { autoHideXLabels } = this.state;
35

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
36 37
    if (canvasWidth <= minWidth) {
      if (!autoHideXLabels) {
38
        this.setState({
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
39
          autoHideXLabels: true,
40 41
        });
      }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
42 43 44 45 46
    } else if (autoHideXLabels) {
      this.setState({
        autoHideXLabels: false,
      });
    }
47 48
  }

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

jim's avatar
jim committed
53
  handleRef = n => {
54
    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;