Skip to content

Commit 2c2ce3c

Browse files
committed
refactor(stock): 股票图优化,crosshairs 迁移至 constant 中,不在 adaptor 逻辑处理
添加自定义 demo
1 parent 814f9fa commit 2c2ce3c

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Stock } from '@antv/g2plot';
2+
3+
fetch('https://gw.alipayobjects.com/os/antfincdn/qtQ9nYfYJe/stock-data.json')
4+
.then((data) => data.json())
5+
.then((data) => {
6+
const stockPlot = new Stock('container', {
7+
data,
8+
xField: 'trade_date',
9+
yField: ['open', 'close', 'high', 'low'],
10+
meta: {
11+
vol: { alias: '成交量' },
12+
open: { alias: '开盘价' },
13+
close: { alias: '收盘价' },
14+
high: { alias: '最高价' },
15+
low: { alias: '最低价' },
16+
},
17+
tooltip: {
18+
crosshairs: {
19+
// 自定义 crosshairs line 样式
20+
line: {
21+
style: {
22+
lineWidth: 0.5,
23+
stroke: 'rgba(0,0,0,0.25)',
24+
},
25+
},
26+
text: (type, defaultContent, items) => {
27+
let textContent;
28+
if (type === 'x') {
29+
const item = items[0];
30+
textContent = item ? item.title : defaultContent;
31+
} else {
32+
textContent = `${defaultContent.toFixed(2)}`;
33+
}
34+
return {
35+
position: type === 'y' ? 'start' : 'end',
36+
content: textContent,
37+
// 自定义 crosshairs text 样式
38+
style: {
39+
fill: '#dfdfdf',
40+
},
41+
};
42+
},
43+
// 自定义 crosshairs textBackground 样式
44+
textBackground: {
45+
padding: [2, 4],
46+
style: {
47+
fill: '#666',
48+
},
49+
},
50+
},
51+
},
52+
});
53+
54+
stockPlot.render();
55+
});

examples/more-plots/stock/demo/meta.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535
"en": "Custom tooltip fields"
3636
},
3737
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/4EtM9REqIP/8edb0a9e-293e-4b87-bc61-d814e958acbd.png"
38+
},
39+
{
40+
"filename": "custom-crosshairs.ts",
41+
"title": {
42+
"zh": "自定义 crosshairs 指示器",
43+
"en": "Custom crosshairs pointer"
44+
},
45+
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/UKHCDVwzFB/stock-crosshairs.png"
3846
}
3947
]
4048
}

src/plots/stock/adaptor.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,10 @@ export function axis(params: Params<StockOptions>): Params<StockOptions> {
103103
*/
104104
export function tooltip(params: Params<StockOptions>): Params<StockOptions> {
105105
const { chart, options } = params;
106-
const { xField, tooltip } = options;
106+
const { tooltip } = options;
107107

108108
if (tooltip !== false) {
109-
const chartTooltip = deepAssign(
110-
{},
111-
{
112-
crosshairs: {
113-
text: (type, defaultContent, items) => {
114-
const tooltipCrosshairsText = { position: 'end' };
115-
if (type === 'x') {
116-
const item = items[0];
117-
tooltipCrosshairsText['content'] = item ? item.data[xField] : defaultContent;
118-
} else {
119-
tooltipCrosshairsText['content'] = defaultContent;
120-
}
121-
return tooltipCrosshairsText;
122-
},
123-
},
124-
},
125-
tooltip
126-
);
127-
chart.tooltip(chartTooltip);
109+
chart.tooltip(tooltip);
128110
} else {
129111
chart.tooltip(false);
130112
}

src/plots/stock/constant.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ export const DEFAULT_TOOLTIP_OPTIONS = {
1515
crosshairs: {
1616
type: 'xy',
1717
follow: true,
18+
text: (type, defaultContent, items) => {
19+
const tooltipCrosshairsText = { position: type === 'y' ? 'start' : 'end' };
20+
if (type === 'x') {
21+
const item = items[0];
22+
tooltipCrosshairsText['content'] = item ? item.title : defaultContent;
23+
} else {
24+
tooltipCrosshairsText['content'] = defaultContent;
25+
}
26+
return tooltipCrosshairsText;
27+
},
1828
},
1929
};
2030

0 commit comments

Comments
 (0)