index.js 1.66 KB
Newer Older
niko's avatar
niko committed
1 2
import React, { Component } from 'react';
import { Chart, Axis, Tooltip, Geom } from 'bizcharts';
3 4
import styles from '../index.less';

niko's avatar
niko committed
5
class Bar extends Component {
6
  state = {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
7
    height: 0,
8
    autoHideXLabels: false,
niko's avatar
niko committed
9
  };
10

11 12 13 14 15 16 17 18
  handleRoot = n => {
    this.root = n;
  };

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

niko's avatar
niko committed
19
  render() {
nikogu's avatar
nikogu committed
20
    const {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
21
      height: propsHeight,
nikogu's avatar
nikogu committed
22 23 24 25 26 27
      title,
      forceFit = true,
      data,
      color = 'rgba(24, 144, 255, 0.85)',
      padding,
    } = this.props;
28

niko's avatar
niko committed
29
    const { autoHideXLabels } = this.state;
30

niko's avatar
niko committed
31
    const scale = {
32 33 34 35 36 37
      x: {
        type: 'cat',
      },
      y: {
        min: 0,
      },
niko's avatar
niko committed
38
    };
39

niko's avatar
niko committed
40 41 42 43 44 45 46
    const tooltip = [
      'x*y',
      (x, y) => ({
        name: x,
        value: y,
      }),
    ];
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
47
    const { height: stateHeight } = this.state;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
48
    const height = propsHeight || stateHeight;
49
    return (
niko's avatar
niko committed
50 51
      <div className={styles.chart} style={{ height }} ref={this.handleRoot}>
        <div ref={this.handleRef}>
nikogu's avatar
nikogu committed
52
          {title && <h4 style={{ marginBottom: 20 }}>{title}</h4>}
niko's avatar
niko committed
53 54
          <Chart
            scale={scale}
nikogu's avatar
nikogu committed
55
            height={title ? height - 41 : height}
niko's avatar
niko committed
56 57 58 59
            forceFit={forceFit}
            data={data}
            padding={padding || 'auto'}
          >
nikogu's avatar
nikogu committed
60 61 62 63 64 65 66
            <Axis
              name="x"
              title={false}
              label={autoHideXLabels ? false : {}}
              tickLine={autoHideXLabels ? false : {}}
            />
            <Axis name="y" min={0} />
niko's avatar
niko committed
67 68 69
            <Tooltip showTitle={false} crosshairs={false} />
            <Geom type="interval" position="x*y" color={color} tooltip={tooltip} />
          </Chart>
70 71 72 73 74 75 76
        </div>
      </div>
    );
  }
}

export default Bar;