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

陈帅's avatar
陈帅 committed
3
import DataSet from '@antv/data-set';
陈帅's avatar
陈帅 committed
4
import React from 'react';
陈帅's avatar
陈帅 committed
5 6 7 8
import Slider from 'bizcharts-plugin-slider';
import autoHeight from '../autoHeight';
import styles from './index.less';

陈帅's avatar
陈帅 committed
9
export interface TimelineChartProps {
陈帅's avatar
陈帅 committed
10
  data: {
陈帅's avatar
陈帅 committed
11 12 13
    x: number;
    y1: number;
    y2: number;
陈帅's avatar
陈帅 committed
14
  }[];
陈帅's avatar
陈帅 committed
15 16 17 18 19 20 21 22
  title?: string;
  titleMap: { y1: string; y2: string };
  padding?: [number, number, number, number];
  height?: number;
  style?: React.CSSProperties;
  borderWidth?: number;
}

陈帅's avatar
陈帅 committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36
const TimelineChart: React.FC<TimelineChartProps> = props => {
  const {
    title,
    height = 400,
    padding = [60, 20, 40, 40] as [number, number, number, number],
    titleMap = {
      y1: 'y1',
      y2: 'y2',
    },
    borderWidth = 2,
    data: sourceData,
  } = props;

  const data = Array.isArray(sourceData) ? sourceData : [{ x: 0, y1: 0, y2: 0 }];
陈帅's avatar
陈帅 committed
37

陈帅's avatar
陈帅 committed
38
  data.sort((a, b) => a.x - b.x);
陈帅's avatar
陈帅 committed
39

陈帅's avatar
陈帅 committed
40 41 42 43 44 45 46
  let max;
  if (data[0] && data[0].y1 && data[0].y2) {
    max = Math.max(
      [...data].sort((a, b) => b.y1 - a.y1)[0].y1,
      [...data].sort((a, b) => b.y2 - a.y2)[0].y2,
    );
  }
陈帅's avatar
陈帅 committed
47

陈帅's avatar
陈帅 committed
48 49 50 51 52 53
  const ds = new DataSet({
    state: {
      start: data[0].x,
      end: data[data.length - 1].x,
    },
  });
陈帅's avatar
陈帅 committed
54

陈帅's avatar
陈帅 committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  const dv = ds.createView();
  dv.source(data)
    .transform({
      type: 'filter',
      callback: (obj: { x: string }) => {
        const date = obj.x;
        return date <= ds.state.end && date >= ds.state.start;
      },
    })
    .transform({
      type: 'map',
      callback(row: { y1: string; y2: string }) {
        const newRow = { ...row };
        newRow[titleMap.y1] = row.y1;
        newRow[titleMap.y2] = row.y2;
        return newRow;
陈帅's avatar
陈帅 committed
71
      },
陈帅's avatar
陈帅 committed
72 73 74 75 76 77
    })
    .transform({
      type: 'fold',
      fields: [titleMap.y1, titleMap.y2], // 展开字段集
      key: 'key', // key字段
      value: 'value', // value字段
陈帅's avatar
陈帅 committed
78 79
    });

陈帅's avatar
陈帅 committed
80 81 82 83 84 85
  const timeScale = {
    type: 'time',
    tickInterval: 60 * 60 * 1000,
    mask: 'HH:mm',
    range: [0, 1],
  };
陈帅's avatar
陈帅 committed
86

陈帅's avatar
陈帅 committed
87 88 89 90 91 92 93
  const cols = {
    x: timeScale,
    value: {
      max,
      min: 0,
    },
  };
陈帅's avatar
陈帅 committed
94

陈帅's avatar
陈帅 committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  const SliderGen = () => (
    <Slider
      padding={[0, padding[1] + 20, 0, padding[3]]}
      width="auto"
      height={26}
      xAxis="x"
      yAxis="y1"
      scales={{ x: timeScale }}
      data={data}
      start={ds.state.start}
      end={ds.state.end}
      backgroundChart={{ type: 'line' }}
      onChange={({ startValue, endValue }: { startValue: string; endValue: string }) => {
        ds.setState('start', startValue);
        ds.setState('end', endValue);
      }}
    />
  );
陈帅's avatar
陈帅 committed
113

陈帅's avatar
陈帅 committed
114 115 116 117 118 119 120 121 122 123 124 125
  return (
    <div className={styles.timelineChart} style={{ height: height + 30 }}>
      <div>
        {title && <h4>{title}</h4>}
        <Chart height={height} padding={padding} data={dv} scale={cols} forceFit>
          <Axis name="x" />
          <Tooltip />
          <Legend name="key" position="top" />
          <Geom type="line" position="x*value" size={borderWidth} color="key" />
        </Chart>
        <div style={{ marginRight: -20 }}>
          <SliderGen />
陈帅's avatar
陈帅 committed
126 127
        </div>
      </div>
陈帅's avatar
陈帅 committed
128 129 130
    </div>
  );
};
陈帅's avatar
陈帅 committed
131 132

export default autoHeight()(TimelineChart);