index.js 2.17 KB
Newer Older
niko's avatar
niko committed
1 2 3
import React from 'react';
import { Chart, Axis, Tooltip, Geom } from 'bizcharts';
import autoHeight from '../autoHeight';
4 5
import styles from '../index.less';

niko's avatar
niko committed
6 7 8
@autoHeight()
export default class MiniArea extends React.Component {
  render() {
afc163's avatar
afc163 committed
9
    const {
niko's avatar
niko committed
10 11 12 13 14 15 16 17 18 19
      height,
      data = [],
      forceFit = true,
      color = 'rgba(24, 144, 255, 0.2)',
      scale = {},
      borderWidth = 2,
      line,
      xAxis,
      yAxis,
      animate = true,
afc163's avatar
afc163 committed
20
    } = this.props;
21

niko's avatar
niko committed
22
    const borderColor = this.props.borderColor || color;
23

niko's avatar
niko committed
24
    const padding = [36, 5, 30, 5];
25

niko's avatar
niko committed
26
    const scaleProps = {
27 28 29
      x: {
        type: 'cat',
        range: [0, 1],
niko's avatar
niko committed
30
        ...scale.x,
31 32 33
      },
      y: {
        min: 0,
niko's avatar
niko committed
34
        ...scale.y,
35
      },
niko's avatar
niko committed
36
    };
37

niko's avatar
niko committed
38 39 40 41 42 43 44
    const tooltip = [
      'x*y',
      (x, y) => ({
        name: x,
        value: y,
      }),
    ];
niko's avatar
niko committed
45

niko's avatar
niko committed
46
    const chartHeight = height + 54;
47 48 49

    return (
      <div className={styles.miniChart} style={{ height }}>
50
        <div className={styles.chartContent}>
niko's avatar
niko committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
          {height > 0 && (
            <Chart
              animate={animate}
              scale={scaleProps}
              height={chartHeight}
              forceFit={forceFit}
              data={data}
              padding={padding}
            >
              <Axis name="x" label={false} line={false} tickLine={false} grid={false} {...xAxis} />
              <Axis name="y" label={false} line={false} tickLine={false} grid={false} {...yAxis} />
              <Tooltip showTitle={false} crosshairs={false} />
              <Geom
                type="area"
                position="x*y"
                color={color}
                tooltip={tooltip}
                shape="smooth"
                style={{
                  fillOpacity: 1,
                }}
              />
              {line ? (
                <Geom
                  type="line"
                  position="x*y"
                  shape="smooth"
                  color={borderColor}
                  size={borderWidth}
                  tooltip={false}
                />
              ) : (
                <span style={{ display: 'none' }} />
              )}
            </Chart>
          )}
87 88 89 90 91
        </div>
      </div>
    );
  }
}