Skip to content

Commit 09b30d3

Browse files
lxfu1liufu.lf
and
liufu.lf
authored
fix: 修复迷你图 tooltip 不统一 (#1578)
* fix: 修复 tiny 图表 tooltip 不统一问题 * fix: 修改 test 路径 Co-authored-by: liufu.lf <[email protected]>
1 parent 0598e47 commit 09b30d3

File tree

11 files changed

+21
-130
lines changed

11 files changed

+21
-130
lines changed

__tests__/unit/plots/tiny-area/index-spec.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { TooltipCfg } from '@antv/g2/lib/interface';
2-
import { GeometryTooltipOption } from '@antv/g2/lib/interface';
32
import { TinyArea } from '../../../../src';
43
import { partySupport } from '../../../data/party-support';
54
import { createDiv } from '../../../utils/dom';
@@ -149,8 +148,8 @@ describe('tiny-area', () => {
149148
autoFit: false,
150149
tooltip: {
151150
showCrosshairs: true,
152-
formatter: ({ y }) => {
153-
return `有${y / 1000}千`;
151+
customContent: (...arg) => {
152+
return `<div class="g2-tooltip">${arg[1][0]?.value / 1000}</div>`;
154153
},
155154
position: 'bottom',
156155
offset: 0,
@@ -177,9 +176,9 @@ describe('tiny-area', () => {
177176
},
178177
});
179178
const geometry = tinyArea.chart.geometries[0];
180-
const geometryTooltipOption = geometry.tooltipOption as GeometryTooltipOption;
181-
expect(geometryTooltipOption.fields).toEqual(['x', 'y']);
182-
expect(geometryTooltipOption.callback(1, '3000')).toEqual({ value: '有3千' });
179+
// @ts-ignore
180+
const { position } = geometry.attributeOption;
181+
expect(position.fields).toEqual(['x', 'y']);
183182
});
184183

185184
it('annotation', () => {

__tests__/unit/plots/tiny-column/index-spec.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { TooltipCfg } from '@antv/g2/lib/interface';
2-
import { GeometryTooltipOption } from '@antv/g2/lib/interface';
32
import { TinyColumn } from '../../../../src';
43
import { partySupport } from '../../../data/party-support';
54
import { createDiv } from '../../../utils/dom';
@@ -117,8 +116,8 @@ describe('tiny-column', () => {
117116
autoFit: false,
118117
tooltip: {
119118
showCrosshairs: true,
120-
formatter: ({ y }) => {
121-
return `有${y / 1000}千`;
119+
customContent: (...arg) => {
120+
return `<div class="g2-tooltip">${arg[1][0]?.value / 1000}</div>`;
122121
},
123122
position: 'bottom',
124123
offset: 0,
@@ -145,9 +144,9 @@ describe('tiny-column', () => {
145144
},
146145
});
147146
const geometry = tinyColumn.chart.geometries[0];
148-
const geometryTooltipOption = geometry.tooltipOption as GeometryTooltipOption;
149-
expect(geometryTooltipOption.fields).toEqual(['x', 'y']);
150-
expect(geometryTooltipOption.callback(1, '3000')).toEqual({ value: '有3千' });
147+
// @ts-ignore
148+
const { position } = geometry.attributeOption;
149+
expect(position.fields).toEqual(['x', 'y']);
151150
});
152151

153152
it('columnWidthRatio', () => {

__tests__/unit/plots/tiny-line/index-spec.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { TooltipCfg } from '@antv/g2/lib/interface';
2-
import { GeometryTooltipOption } from '@antv/g2/lib/interface';
32
import { TinyLine } from '../../../../src';
43
import { partySupport } from '../../../data/party-support';
54
import { createDiv } from '../../../utils/dom';
@@ -121,8 +120,8 @@ describe('tiny-line', () => {
121120
autoFit: false,
122121
tooltip: {
123122
showCrosshairs: true,
124-
formatter: ({ y }) => {
125-
return `有${y / 1000}千`;
123+
customContent: (...arg) => {
124+
return `<div class="g2-tooltip">${arg[1][0]?.value / 1000}</div>`;
126125
},
127126
position: 'bottom',
128127
offset: 0,
@@ -149,9 +148,9 @@ describe('tiny-line', () => {
149148
},
150149
});
151150
const geometry = tinyLine.chart.geometries[0];
152-
const geometryTooltipOption = geometry.tooltipOption as GeometryTooltipOption;
153-
expect(geometryTooltipOption.fields).toEqual(['x', 'y']);
154-
expect(geometryTooltipOption.callback(1, '3000')).toEqual({ value: '有3千' });
151+
// @ts-ignore
152+
const { position } = geometry.attributeOption;
153+
expect(position.fields).toEqual(['x', 'y']);
155154
});
156155

157156
it('annotation', () => {

src/plots/tiny-area/adaptor.ts

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { deepMix } from '@antv/util';
2-
import { theme, scale, animation, annotation } from '../../adaptor/common';
2+
import { theme, scale, animation, annotation, tooltip } from '../../adaptor/common';
33
import { Params } from '../../core/adaptor';
44
import { flow } from '../../utils';
5-
import { TinyTooltipOption } from '../../types';
65
import { area, line, point } from '../../adaptor/geometries';
76
import { TinyAreaOptions } from './types';
87

@@ -40,33 +39,6 @@ function geometry(params: Params<TinyAreaOptions>): Params<TinyAreaOptions> {
4039
return params;
4140
}
4241

43-
/**
44-
* tooltip 配置
45-
* @param params
46-
*/
47-
export function tooltip(params: Params<TinyAreaOptions>): Params<TinyAreaOptions> {
48-
const { chart, options } = params;
49-
const { tooltip } = options;
50-
51-
// false 则关闭
52-
if (tooltip === false) {
53-
chart.tooltip(false);
54-
} else {
55-
// 是如果 object,那么传入
56-
const { formatter, ...otherTooltip } = tooltip as TinyTooltipOption;
57-
58-
chart.tooltip(otherTooltip);
59-
60-
chart.geometries.map((g) => {
61-
g.tooltip('x*y', (x, y) => ({
62-
value: formatter({ x, y }),
63-
}));
64-
});
65-
}
66-
67-
return params;
68-
}
69-
7042
/**
7143
* 迷你面积图适配器
7244
* @param chart

src/plots/tiny-area/types.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Options, StyleAttr } from '../../types';
2-
import { TinyTooltipOption } from '../../types/tooltip';
32
import { MappingOptions } from '../../adaptor/geometries/base';
43

54
/** mini 图类型定义需要 omit 很多的 G2 Options 配置 */
6-
export interface TinyAreaOptions extends Omit<Options, 'data' | 'tooltip' | 'legend' | 'label'> {
5+
export interface TinyAreaOptions extends Omit<Options, 'data' | 'legend' | 'label'> {
76
/** 具体的数据 */
87
readonly data: number[];
98
/** 是否平滑 */
@@ -14,6 +13,4 @@ export interface TinyAreaOptions extends Omit<Options, 'data' | 'tooltip' | 'leg
1413
readonly line?: MappingOptions;
1514
/** 面积点图形样式 */
1615
readonly point?: MappingOptions;
17-
/** tooltip配置 */
18-
readonly tooltip?: boolean | TinyTooltipOption;
1916
}

src/plots/tiny-column/adaptor.ts

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { deepMix } from '@antv/util';
2-
import { theme, scale, animation, annotation } from '../../adaptor/common';
2+
import { theme, scale, animation, annotation, tooltip } from '../../adaptor/common';
33
import { Params } from '../../core/adaptor';
44
import { flow } from '../../utils';
5-
import { TinyTooltipOption } from '../../types';
65
import { interval } from '../../adaptor/geometries';
76
import { TinyColumnOptions } from './types';
87
/**
@@ -38,31 +37,6 @@ function geometry(params: Params<TinyColumnOptions>): Params<TinyColumnOptions>
3837
return params;
3938
}
4039

41-
/**
42-
* tooltip 配置
43-
* @param params
44-
*/
45-
export function tooltip(params: Params<TinyColumnOptions>): Params<TinyColumnOptions> {
46-
const { chart, options } = params;
47-
const { tooltip } = options;
48-
49-
// false 则关闭
50-
if (tooltip === false) {
51-
chart.tooltip(false);
52-
} else {
53-
// 是如果 object,那么传入
54-
const { formatter, ...otherTooltip } = tooltip as TinyTooltipOption;
55-
56-
chart.tooltip(otherTooltip);
57-
58-
chart.geometries[0].tooltip('x*y', (x, y) => ({
59-
value: formatter({ x, y }),
60-
}));
61-
}
62-
63-
return params;
64-
}
65-
6640
/**
6741
* 迷你柱形图适配器
6842
* @param chart

src/plots/tiny-column/types.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { Options, StyleAttr } from '../../types';
2-
import { TinyTooltipOption } from '../../types/tooltip';
32

43
/** mini 图类型定义需要 omit 很多的 G2 Options 配置 */
5-
export interface TinyColumnOptions extends Omit<Options, 'data' | 'tooltip' | 'legend' | 'label'> {
4+
export interface TinyColumnOptions extends Omit<Options, 'data' | 'legend' | 'label'> {
65
/** 具体的数据 */
76
readonly data: number[];
87
/** 柱状图宽度占比 [0-1] */
98
readonly columnWidthRatio?: number;
109
/** 迷你柱形图形样式 */
1110
readonly columnStyle?: StyleAttr;
12-
/** tooltip配置 */
13-
readonly tooltip?: boolean | TinyTooltipOption;
1411
}

src/plots/tiny-line/adaptor.ts

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { deepMix } from '@antv/util';
22
import { Params } from '../../core/adaptor';
33
import { flow } from '../../utils';
4-
import { scale, theme, animation, annotation } from '../../adaptor/common';
5-
import { TinyTooltipOption } from '../../types';
4+
import { scale, theme, animation, annotation, tooltip } from '../../adaptor/common';
65
import { line, point } from '../../adaptor/geometries';
76
import { TinyLineOptions } from './types';
87

@@ -42,31 +41,6 @@ function geometry(params: Params<TinyLineOptions>): Params<TinyLineOptions> {
4241
return params;
4342
}
4443

45-
/**
46-
* tooltip 配置
47-
* @param params
48-
*/
49-
export function tooltip(params: Params<TinyLineOptions>): Params<TinyLineOptions> {
50-
const { chart, options } = params;
51-
const { tooltip } = options;
52-
53-
// false 则关闭
54-
if (tooltip === false) {
55-
chart.tooltip(false);
56-
} else {
57-
// 是如果 object,那么传入
58-
const { formatter, ...otherTooltip } = tooltip as TinyTooltipOption;
59-
60-
chart.tooltip(otherTooltip);
61-
62-
chart.geometries[0].tooltip('x*y', (x, y) => ({
63-
value: formatter({ x, y }),
64-
}));
65-
}
66-
67-
return params;
68-
}
69-
7044
/**
7145
* 迷你折线图适配器
7246
* @param chart

src/plots/tiny-line/constants.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ export const DEFAULT_TOOLTIP_OPTIONS = {
1010
fontSize: '10px',
1111
},
1212
},
13-
formatter: (x: number, y: number) => y.toFixed(1),
1413
};

src/plots/tiny-line/types.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Options, StyleAttr } from '../../types';
2-
import { TinyTooltipOption } from '../../types/tooltip';
32
import { MappingOptions } from '../../adaptor/geometries/base';
43

54
/** mini 图类型定义需要 omit 很多的 G2 Options 配置 */
6-
export interface TinyLineOptions extends Omit<Options, 'data' | 'tooltip' | 'legend' | 'label'> {
5+
export interface TinyLineOptions extends Omit<Options, 'data' | 'legend' | 'label'> {
76
/** 具体的数据 */
87
readonly data: number[];
98
/** 是否平滑 */
@@ -14,6 +13,4 @@ export interface TinyLineOptions extends Omit<Options, 'data' | 'tooltip' | 'leg
1413
readonly lineStyle?: StyleAttr;
1514
/** 折线点图形样式 */
1615
readonly point?: MappingOptions;
17-
/** tooltip配置 */
18-
readonly tooltip?: boolean | TinyTooltipOption;
1916
}

src/types/tooltip.ts

-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
11
import { TooltipOption } from '@antv/g2/lib/interface';
2-
import { Datum } from './common';
32

43
export type Tooltip = TooltipOption;
5-
6-
export type TinyTooltipOption = {
7-
/** tootip body模版语言 */
8-
readonly formatter?: (datum: Datum) => string;
9-
/** 获取tooltip内部dom节点覆写样式 */
10-
readonly domStyles?: object;
11-
/** tooltip定位位置 */
12-
readonly position?: 'top' | 'bottom' | 'left' | 'right';
13-
/** tooltip偏移位置 */
14-
readonly offset?: number;
15-
/** 是否显示交叉线 */
16-
readonly showCrosshairs?: boolean;
17-
/** 是否显示 tooltip 数据点 marker */
18-
readonly showMarkers?: boolean;
19-
};

0 commit comments

Comments
 (0)