index.tsx 2.79 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 34 35 36 37 38 39 40 41 42 43 44
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
45

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

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

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

陈帅's avatar
陈帅 committed
68
  const chartHeight = height + 54;
陈帅's avatar
陈帅 committed
69

陈帅's avatar
陈帅 committed
70 71 72 73 74 75 76 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
  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}
          >
            <Axis
              key="axis-x"
              name="x"
              label={undefined}
              line={undefined}
              tickLine={undefined}
              grid={undefined}
              {...xAxis}
            />
            <Axis
              key="axis-y"
              name="y"
              label={undefined}
              line={undefined}
              tickLine={undefined}
              grid={undefined}
              {...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
112
              <Geom
陈帅's avatar
陈帅 committed
113
                type="line"
陈帅's avatar
陈帅 committed
114 115
                position="x*y"
                shape="smooth"
陈帅's avatar
陈帅 committed
116 117 118
                color={borderColor}
                size={borderWidth}
                tooltip={false}
陈帅's avatar
陈帅 committed
119
              />
陈帅's avatar
陈帅 committed
120 121 122 123 124
            ) : (
              <span style={{ display: 'none' }} />
            )}
          </Chart>
        )}
陈帅's avatar
陈帅 committed
125
      </div>
陈帅's avatar
陈帅 committed
126 127 128
    </div>
  );
};
陈帅's avatar
陈帅 committed
129 130

export default autoHeight()(MiniArea);