-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
packages/f2/src/components/candlestick/candlestickView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
packages/f2/src/components/candlestick/withCandlestick.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} />; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+7.73 KB
...nts/candlestick/__image_snapshots__/basic-test-tsx-candlestick-basic-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.62 KB
...nts/candlestick/__image_snapshots__/basic-test-tsx-candlestick-color-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.79 KB
...ents/candlestick/__image_snapshots__/basic-test-tsx-candlestick-size-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.76 KB
...andlestick/__image_snapshots__/basic-test-tsx-candlestick-size-ratio-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |