Skip to content

Commit

Permalink
feat: 添加 K 线图
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Jul 11, 2023
1 parent 7e71139 commit e0b04ea
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 0 deletions.
88 changes: 88 additions & 0 deletions packages/f2/src/components/candlestick/candlestickView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { jsx } from '@antv/f-engine';
import { deepMix } from '@antv/util';

export default (props) => {
const { records, animation, y0, onClick } = props;
return (
<group>
{records.map((record) => {
const { key, children } = record;
return (
<group key={key}>
{children.map((item) => {
const { key, xMin, xMax, yMin, yMax, y, color, shape } = item;
if (isNaN(xMin) || isNaN(xMax) || isNaN(yMin) || isNaN(yMax)) {
return null;
}
const xMiddle = (xMin + xMax) / 2;
return (
<group>
<line
style={{
x1: xMiddle,
y1: y[2],
x2: xMiddle,
y2: y[3],
stroke: color,
lineWidth: '2px',
lineCap: 'round',
}}
animation={{
appear: {
easing: 'linear',
duration: 300,
property: ['y1', 'y2'],
// @ts-ignore
start: {
y1: 0,
y2: 0,
},
},
update: {
easing: 'linear',
duration: 300,
property: ['x1', 'y1', 'x2', 'y2'],
},
}}
/>
<rect
key={key}
style={{
x: xMin,
y: yMin,
width: xMax - xMin,
height: yMax - yMin,
fill: color,
radius: '2px',
...shape,
}}
onClick={onClick}
animation={deepMix(
{
appear: {
easing: 'linear',
duration: 300,
property: ['y', 'height'],
start: {
y: y0,
height: 0,
},
},
update: {
easing: 'linear',
duration: 300,
property: ['x', 'y', 'width', 'height'],
},
},
animation
)}
/>
</group>
);
})}
</group>
);
})}
</group>
);
};
5 changes: 5 additions & 0 deletions packages/f2/src/components/candlestick/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import withCandlestick, { CandlestickProps } from './withCandlestick';
import CandlestickView from './candlestickView';

export { CandlestickProps, CandlestickView };
export default withCandlestick(CandlestickView);
86 changes: 86 additions & 0 deletions packages/f2/src/components/candlestick/withCandlestick.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { jsx, ComponentType } from '@antv/f-engine';
import { isNil, mix } from '@antv/util';
import Geometry, { GeometryProps } from '../geometry';
import { DataRecord } from '../../chart/Data';

// 默认配色
const COLORS = [
'#E62C3B', // 上涨
'#0E9976', // 下跌
'#999999', // 平盘
];

export interface CandlestickProps<TRecord extends DataRecord = DataRecord>
extends GeometryProps<TRecord> {
/**
* 柱子的显示比例,默认 0.5
*/
sizeRatio?: number;
}

export default (View: ComponentType) => {
return class Candlestick<
TRecord extends DataRecord = DataRecord,
IProps extends CandlestickProps<TRecord> = CandlestickProps<TRecord>
> extends Geometry<TRecord, IProps> {
getDefaultCfg() {
return {
geomType: 'candlestick',
};
}

getSize() {
const { attrs, props } = this;
const { sizeRatio = 0.5 } = props;
const { x } = attrs;
const { scale } = x;
const { values } = scale;

return (1 / values.length) * sizeRatio;
}

mapping() {
const records = super.mapping();
const { props } = this;
const { coord } = props;
const y0 = this.getY0Value();
const defaultSize = this.getSize();
const colorAttr = this.getAttr('color');

const colors = colorAttr ? colorAttr.range : COLORS;

for (let i = 0, len = records.length; i < len; i++) {
const record = records[i];
const { children } = record;
for (let j = 0, len = children.length; j < len; j++) {
const child = children[j];
const { normalized, size: mappedSize } = child;

// 没有指定size,则根据数据来计算默认size
if (isNil(mappedSize)) {
const { x, y, size = defaultSize } = normalized;
mix(child, coord.convertRect({ x, y, y0, size: size }));
} else {
const { x, y } = child;
const rect = { x, y, y0, size: mappedSize };
mix(child, coord.transformToRect(rect));
}

// 处理颜色
const { y } = normalized;
const [open, close] = y;
child.color = close > open ? colors[0] : close < open ? colors[1] : colors[2];

mix(child.shape, this.getSelectionStyle(child));
}
}
return records;
}

render() {
const { props } = this;
const records = this.mapping();
return <View {...props} records={records} />;
}
};
};
1 change: 1 addition & 0 deletions packages/f2/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export { default as PieLabel, PieLabelProps, withPieLabel, PieLabelView } from '
export { default as Gauge, GaugeProps, withGauge, GaugeView } from './gauge';
export { default as Zoom, ZoomProps } from './zoom';
export { default as ScrollBar, ScrollBarProps, withScrollBar, ScrollBarView } from './scrollBar';
export { default as Candlestick } from './candlestick';
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 101 additions & 0 deletions packages/f2/test/components/candlestick/basic.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { jsx } from '../../../src';
import { Canvas, Chart, Candlestick, Axis } from '../../../src';
import { createContext, delay } from '../../util';
const context = createContext();

const data = [
{
time: '2017-10-24',
value: [20, 34, 10, 38], // [open, close, lowest, highest]
},
{
time: '2017-10-25',
value: [40, 35, 30, 50],
},
{
time: '2017-10-26',
value: [31, 38, 33, 44],
},
{
time: '2017-10-27',
value: [38, 38, 5, 42],
},
{
time: '2017-10-28',
value: [38, 15, 5, 42],
},
];

describe('candlestick', () => {
it('basic', async () => {
const { props } = (
<Canvas context={context} animate={false} pixelRatio={1}>
<Chart data={data}>
<Axis field="time" type="timeCat" tickCount={3} />
<Axis field="value" />
<Candlestick x="time" y="value" />
</Chart>
</Canvas>
);

const canvas = new Canvas(props);
await canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
});

it('color', async () => {
const { props } = (
<Canvas context={context} animate={false} pixelRatio={1}>
<Chart data={data}>
<Axis field="time" type="timeCat" tickCount={3} />
<Axis field="value" />
<Candlestick x="time" y="value" color={{ range: ['red', 'green', 'gray'] }} />
</Chart>
</Canvas>
);

const canvas = new Canvas(props);
await canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
});

it('size', async () => {
const { props } = (
<Canvas context={context} animate={false} pixelRatio={1}>
<Chart data={data}>
<Axis field="time" type="timeCat" tickCount={3} />
<Axis field="value" />
<Candlestick x="time" y="value" size={10} />
</Chart>
</Canvas>
);

const canvas = new Canvas(props);
await canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
});

it('sizeRatio', async () => {
const { props } = (
<Canvas context={context} animate={false} pixelRatio={1}>
<Chart data={data}>
<Axis field="time" type="timeCat" tickCount={3} />
<Axis field="value" />
<Candlestick x="time" y="value" sizeRatio={0.8} />
</Chart>
</Canvas>
);

const canvas = new Canvas(props);
await canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
});
});

0 comments on commit e0b04ea

Please sign in to comment.