Skip to content

Commit edc1cee

Browse files
authored
feat(tooltip): add tooltip.showNil (#3216)
* feat(tooltip): add tooltip.showNil * chore: add default value for function
1 parent 56bc7f1 commit edc1cee

4 files changed

Lines changed: 59 additions & 10 deletions

File tree

src/chart/controller/tooltip.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,13 @@ export default class Tooltip extends Controller<TooltipOption> {
694694
}
695695

696696
private getTooltipItemsByHitShape(geometry, point, title) {
697+
const { showNil } = this.getTooltipCfg();
697698
const result = [];
698699
const container = geometry.container;
699700
const shape = container.getShape(point.x, point.y);
700701
if (shape && shape.get('visible') && shape.get('origin')) {
701702
const mappingData = shape.get('origin').mappingData;
702-
const items = getTooltipItems(mappingData, geometry, title);
703+
const items = getTooltipItems(mappingData, geometry, title, showNil);
703704
if (items.length) {
704705
result.push(items);
705706
}
@@ -709,6 +710,7 @@ export default class Tooltip extends Controller<TooltipOption> {
709710
}
710711

711712
private getTooltipItemsByFindData(geometry: Geometry, point, title) {
713+
const { showNil } = this.getTooltipCfg();
712714
const result = [];
713715
const dataArray = geometry.dataArray;
714716
if (!isEmpty(dataArray)) {
@@ -721,7 +723,7 @@ export default class Tooltip extends Controller<TooltipOption> {
721723
if (geometry.type === 'heatmap' || element.visible) {
722724
// Heatmap 没有 Element
723725
// 如果图形元素隐藏了,怎不再 tooltip 上展示相关数据
724-
const items = getTooltipItems(record, geometry, title);
726+
const items = getTooltipItems(record, geometry, title, showNil);
725727
if (items.length) {
726728
result.push(items);
727729
}

src/interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,8 @@ export interface TooltipCfg {
12351235
offset?: number;
12361236
/** 是否将 tooltip items 逆序 */
12371237
reversed?: boolean;
1238+
/** 是否显示空值的 tooltip 项目 */
1239+
showNil?: boolean;
12381240
/** 支持自定义模板 */
12391241
customContent?: (title: string, data: any[]) => string | HTMLElement;
12401242
}

src/util/tooltip.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ export function findDataByPoint(point: Point, data: MappingDatum[], geometry: Ge
295295
* @param [title]
296296
* @returns
297297
*/
298-
export function getTooltipItems(data: MappingDatum, geometry: Geometry, title: string = '') {
298+
export function getTooltipItems(data: MappingDatum, geometry: Geometry, title: string = '', showNil: boolean = false) {
299299
const originData = data[FIELD_ORIGIN];
300300
const tooltipTitle = getTooltipTitle(originData, geometry, title);
301301
const tooltipOption = geometry.tooltipOption;
@@ -305,7 +305,7 @@ export function getTooltipItems(data: MappingDatum, geometry: Geometry, title: s
305305
let value;
306306

307307
function addItem(itemName, itemValue) {
308-
if (!isNil(itemValue) && itemValue !== '') {
308+
if (showNil || (!isNil(itemValue) && itemValue !== '')) {
309309
// 值为 null的时候,忽视
310310
const item = {
311311
title: tooltipTitle,
@@ -353,12 +353,10 @@ export function getTooltipItems(data: MappingDatum, geometry: Geometry, title: s
353353
}
354354
} else {
355355
const valueScale = getTooltipValueScale(geometry);
356-
if (!isNil(originData[valueScale.field])) {
357-
// 字段数据为null ,undefined时不显示
358-
value = getTooltipValue(originData, valueScale);
359-
name = getTooltipName(originData, geometry);
360-
addItem(name, value);
361-
}
356+
// 字段数据为null ,undefined时不显示
357+
value = getTooltipValue(originData, valueScale);
358+
name = getTooltipName(originData, geometry);
359+
addItem(name, value);
362360
}
363361
return items;
364362
}

tests/bugs/1518-spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 欢迎使用全新的 G2 4.0
2+
import { Chart } from '../../src';
3+
import { createDiv } from '../util/dom';
4+
5+
describe('#1518', () => {
6+
it('tooltip.showNil', () => {
7+
const chart = new Chart({
8+
container: createDiv(),
9+
width: 500,
10+
height: 500,
11+
autoFit: false,
12+
});
13+
14+
chart.data([
15+
{ x: 'A', type: '分类一', value: 30 },
16+
{ x: 'A', type: '分类二', value: 10 },
17+
{ x: 'A', type: '分类三', value: null },
18+
{ x: 'B', type: '分类一', value: 15 },
19+
{ x: 'B', type: '分类二', value: undefined },
20+
{ x: 'B', type: '分类三', value: 20 },
21+
]);
22+
23+
chart.interval().position('x*value').color('type').adjust('stack');
24+
25+
chart.tooltip({
26+
shared: true,
27+
// showNil: true,
28+
});
29+
chart.render();
30+
chart.showTooltip({ x: 150, y: 150 });
31+
expect(chart.ele.querySelectorAll('.g2-tooltip-list-item').length).toBe(2);
32+
33+
chart.hideTooltip();
34+
35+
chart.tooltip({
36+
shared: true,
37+
showNil: true,
38+
});
39+
chart.render();
40+
chart.showTooltip({ x: 150, y: 150 });
41+
42+
expect(chart.ele.querySelectorAll('.g2-tooltip-list-item').length).toBe(3);
43+
expect(chart.ele.querySelector('.g2-tooltip-list-item:nth-child(3) span:nth-child(3)').innerHTML).toBe('');
44+
45+
chart.destroy();
46+
});
47+
});

0 commit comments

Comments
 (0)