index.tsx 2.94 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import { Axis, Chart, Geom, Tooltip } from 'bizcharts';
陈帅's avatar
陈帅 committed
2
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
3

陈帅's avatar
陈帅 committed
4
import Debounce from 'lodash.debounce';
陈帅's avatar
陈帅 committed
5 6 7
import autoHeight from '../autoHeight';
import styles from '../index.less';

陈帅's avatar
陈帅 committed
8
export interface BarProps {
陈帅's avatar
陈帅 committed
9 10 11 12
  title: React.ReactNode;
  color?: string;
  padding?: [number, number, number, number];
  height?: number;
陈帅's avatar
陈帅 committed
13
  data: {
陈帅's avatar
陈帅 committed
14 15
    x: string;
    y: number;
陈帅's avatar
陈帅 committed
16
  }[];
陈帅's avatar
陈帅 committed
17 18 19 20 21 22
  forceFit?: boolean;
  autoLabel?: boolean;
  style?: React.CSSProperties;
}

class Bar extends Component<
陈帅's avatar
陈帅 committed
23
  BarProps,
陈帅's avatar
陈帅 committed
24 25 26 27 28 29 30
  {
    autoHideXLabels: boolean;
  }
> {
  state = {
    autoHideXLabels: false,
  };
陈帅's avatar
陈帅 committed
31

陈帅's avatar
陈帅 committed
32
  root: HTMLDivElement | undefined = undefined;
陈帅's avatar
陈帅 committed
33

陈帅's avatar
陈帅 committed
34
  node: HTMLDivElement | undefined = undefined;
陈帅's avatar
陈帅 committed
35

陈帅's avatar
陈帅 committed
36
  resize = Debounce(() => {
陈帅's avatar
陈帅 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    if (!this.node || !this.node.parentNode) {
      return;
    }
    const canvasWidth = (this.node.parentNode as HTMLDivElement).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,
      });
    }
陈帅's avatar
陈帅 committed
59 60 61 62 63 64 65 66
  }, 500);

  componentDidMount() {
    window.addEventListener('resize', this.resize, { passive: true });
  }

  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
陈帅's avatar
陈帅 committed
67 68
  }

陈帅's avatar
陈帅 committed
69 70 71 72 73 74 75 76
  handleRoot = (n: HTMLDivElement) => {
    this.root = n;
  };

  handleRef = (n: HTMLDivElement) => {
    this.node = n;
  };

陈帅's avatar
陈帅 committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  render() {
    const {
      height = 1,
      title,
      forceFit = true,
      data,
      color = 'rgba(24, 144, 255, 0.85)',
      padding,
    } = this.props;

    const { autoHideXLabels } = this.state;

    const scale = {
      x: {
        type: 'cat',
      },
      y: {
        min: 0,
      },
    };

    const tooltip: [string, (...args: any[]) => { name?: string; value: string }] = [
      'x*y',
      (x: string, y: string) => ({
        name: x,
        value: y,
      }),
    ];

    return (
      <div className={styles.chart} style={{ height }} ref={this.handleRoot}>
        <div ref={this.handleRef}>
          {title && <h4 style={{ marginBottom: 20 }}>{title}</h4>}
          <Chart
            scale={scale}
            height={title ? height - 41 : height}
            forceFit={forceFit}
            data={data}
            padding={padding || 'auto'}
          >
            <Axis
              name="x"
              title={false}
陈帅's avatar
陈帅 committed
120 121
              label={autoHideXLabels ? undefined : {}}
              tickLine={autoHideXLabels ? undefined : {}}
陈帅's avatar
陈帅 committed
122 123 124 125 126 127 128 129 130 131 132 133
            />
            <Axis name="y" min={0} />
            <Tooltip showTitle={false} crosshairs={false} />
            <Geom type="interval" position="x*y" color={color} tooltip={tooltip} />
          </Chart>
        </div>
      </div>
    );
  }
}

export default autoHeight()(Bar);