index.tsx 1.28 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2
import { Chart, Geom, Tooltip } from 'bizcharts';

陈帅's avatar
陈帅 committed
3 4 5 6
import React from 'react';
import autoHeight from '../autoHeight';
import styles from '../index.less';

陈帅's avatar
陈帅 committed
7
export interface MiniBarProps {
陈帅's avatar
陈帅 committed
8 9
  color?: string;
  height?: number;
陈帅's avatar
陈帅 committed
10
  data: {
陈帅's avatar
陈帅 committed
11 12
    x: number | string;
    y: number;
陈帅's avatar
陈帅 committed
13
  }[];
陈帅's avatar
陈帅 committed
14 15 16 17
  forceFit?: boolean;
  style?: React.CSSProperties;
}

陈帅's avatar
陈帅 committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
const MiniBar: React.FC<MiniBarProps> = props => {
  const { height = 0, forceFit = true, color = '#1890FF', data = [] } = props;

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

  const padding: [number, number, number, number] = [36, 5, 30, 5];

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

  // for tooltip not to be hide
  const chartHeight = height + 54;

  return (
    <div className={styles.miniChart} style={{ height }}>
      <div className={styles.chartContent}>
        <Chart scale={scale} height={chartHeight} forceFit={forceFit} data={data} padding={padding}>
          <Tooltip showTitle={false} crosshairs={false} />
          <Geom type="interval" position="x*y" color={color} tooltip={tooltip} />
        </Chart>
陈帅's avatar
陈帅 committed
50
      </div>
陈帅's avatar
陈帅 committed
51 52 53
    </div>
  );
};
陈帅's avatar
陈帅 committed
54
export default autoHeight()(MiniBar);