index.tsx 2.85 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import { Axis, AxisProps, Chart, Geom, Tooltip } 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
}
陈帅's avatar
陈帅 committed
30 31
// g2 4.0 发布之前只能这样先修一下了
const FixTypeAxis: any = Axis;
陈帅's avatar
陈帅 committed
32

陈帅's avatar
陈帅 committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
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
47

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

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

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

陈帅's avatar
陈帅 committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83
  const chartHeight = height + 54;

  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
84
            <FixTypeAxis
陈帅's avatar
陈帅 committed
85 86
              key="axis-x"
              name="x"
陈帅's avatar
陈帅 committed
87 88 89 90
              label={false}
              line={false}
              tickLine={false}
              grid={false}
陈帅's avatar
陈帅 committed
91 92
              {...xAxis}
            />
陈帅's avatar
陈帅 committed
93
            <FixTypeAxis
陈帅's avatar
陈帅 committed
94 95
              key="axis-y"
              name="y"
陈帅's avatar
陈帅 committed
96 97 98 99
              label={false}
              line={false}
              tickLine={false}
              grid={false}
陈帅's avatar
陈帅 committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113
              {...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
114
              <Geom
陈帅's avatar
陈帅 committed
115
                type="line"
陈帅's avatar
陈帅 committed
116 117
                position="x*y"
                shape="smooth"
陈帅's avatar
陈帅 committed
118 119 120
                color={borderColor}
                size={borderWidth}
                tooltip={false}
陈帅's avatar
陈帅 committed
121
              />
陈帅's avatar
陈帅 committed
122 123 124 125 126
            ) : (
              <span style={{ display: 'none' }} />
            )}
          </Chart>
        )}
陈帅's avatar
陈帅 committed
127
      </div>
陈帅's avatar
陈帅 committed
128 129 130
    </div>
  );
};
陈帅's avatar
陈帅 committed
131 132

export default autoHeight()(MiniArea);