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

陈帅'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 MiniAreaProps {
陈帅's avatar
陈帅 committed
8 9 10 11 12
  color?: string;
  height?: number;
  borderColor?: string;
  line?: boolean;
  animate?: boolean;
陈帅's avatar
陈帅 committed
13
  xAxis?: AxisProps;
陈帅's avatar
陈帅 committed
14
  forceFit?: boolean;
陈帅's avatar
陈帅 committed
15 16 17 18 19 20 21 22 23
  scale?: {
    x?: {
      tickCount: number;
    };
    y?: {
      tickCount: number;
    };
  };
  yAxis?: Partial<AxisProps>;
陈帅's avatar
陈帅 committed
24
  borderWidth?: number;
陈帅's avatar
陈帅 committed
25
  data: {
陈帅's avatar
陈帅 committed
26 27
    x: number | string;
    y: number;
陈帅's avatar
陈帅 committed
28
  }[];
陈帅's avatar
陈帅 committed
29 30
}

陈帅's avatar
陈帅 committed
31 32 33
// g2 4.0 发布之前只能这样先修一下了
const FixTypeAxis: any = Axis;

陈帅's avatar
陈帅 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47
const MiniArea: React.FC<MiniAreaProps> = props => {
  const {
    height = 1,
    data = [],
    forceFit = true,
    color = 'rgba(24, 144, 255, 0.2)',
    borderColor = '#1089ff',
    scale = { x: {}, y: {} },
    borderWidth = 2,
    line,
    xAxis,
    yAxis,
    animate = true,
  } = props;
陈帅's avatar
陈帅 committed
48

陈帅's avatar
陈帅 committed
49
  const padding: [number, number, number, number] = [36, 5, 30, 5];
陈帅's avatar
陈帅 committed
50

陈帅's avatar
陈帅 committed
51 52 53 54 55 56 57 58 59 60 61
  const scaleProps = {
    x: {
      type: 'cat',
      range: [0, 1],
      ...scale.x,
    },
    y: {
      min: 0,
      ...scale.y,
    },
  };
陈帅's avatar
陈帅 committed
62

陈帅's avatar
陈帅 committed
63 64 65 66 67 68 69
  const tooltip: [string, (...args: any[]) => { name?: string; value: string }] = [
    'x*y',
    (x: string, y: string) => ({
      name: x,
      value: y,
    }),
  ];
陈帅's avatar
陈帅 committed
70

陈帅's avatar
陈帅 committed
71
  const chartHeight = height + 54;
陈帅's avatar
陈帅 committed
72

陈帅's avatar
陈帅 committed
73 74 75 76 77 78 79 80 81 82 83 84
  return (
    <div className={styles.miniChart} style={{ height }}>
      <div className={styles.chartContent}>
        {height > 0 && (
          <Chart
            animate={animate}
            scale={scaleProps}
            height={chartHeight}
            forceFit={forceFit}
            data={data}
            padding={padding}
          >
陈帅's avatar
陈帅 committed
85
            <FixTypeAxis
陈帅's avatar
陈帅 committed
86 87
              key="axis-x"
              name="x"
陈帅's avatar
陈帅 committed
88 89 90 91
              label={false}
              line={false}
              tickLine={false}
              grid={false}
陈帅's avatar
陈帅 committed
92 93
              {...xAxis}
            />
陈帅's avatar
陈帅 committed
94
            <FixTypeAxis
陈帅's avatar
陈帅 committed
95 96
              key="axis-y"
              name="y"
陈帅's avatar
陈帅 committed
97 98 99 100
              label={false}
              line={false}
              tickLine={false}
              grid={false}
陈帅's avatar
陈帅 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
              {...yAxis}
            />
            <Tooltip showTitle={false} crosshairs={false} />
            <Geom
              type="area"
              position="x*y"
              color={color}
              tooltip={tooltip}
              shape="smooth"
              style={{
                fillOpacity: 1,
              }}
            />
            {line ? (
陈帅's avatar
陈帅 committed
115
              <Geom
陈帅's avatar
陈帅 committed
116
                type="line"
陈帅's avatar
陈帅 committed
117 118
                position="x*y"
                shape="smooth"
陈帅's avatar
陈帅 committed
119 120 121
                color={borderColor}
                size={borderWidth}
                tooltip={false}
陈帅's avatar
陈帅 committed
122
              />
陈帅's avatar
陈帅 committed
123 124 125 126 127
            ) : (
              <span style={{ display: 'none' }} />
            )}
          </Chart>
        )}
陈帅's avatar
陈帅 committed
128
      </div>
陈帅's avatar
陈帅 committed
129 130 131
    </div>
  );
};
陈帅's avatar
陈帅 committed
132 133

export default autoHeight()(MiniArea);