Skip to content

Commit 81885fe

Browse files
lxfu1liufu.lf
and
liufu.lf
authored
fix: 修复散点图 tooltip 默认 fields 丢失 (#2019)
* fix: 修复散点图 tooltip 默认 fields 丢失 * test: 添加scatter legend 单测 Co-authored-by: liufu.lf <[email protected]>
1 parent 0882ca3 commit 81885fe

File tree

6 files changed

+133
-12
lines changed

6 files changed

+133
-12
lines changed

__tests__/bugs/issue-2015-spec.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Scatter } from '../../src';
2+
import { data } from '../data/gender';
3+
import { createDiv } from '../utils/dom';
4+
5+
describe('issue 2015', () => {
6+
it('scatter fields', () => {
7+
const scatter = new Scatter(createDiv(), {
8+
width: 400,
9+
height: 300,
10+
data,
11+
xField: 'weight',
12+
yField: 'height',
13+
size: 10,
14+
colorField: 'gender',
15+
shape: 'circle',
16+
xAxis: {
17+
nice: true,
18+
},
19+
tooltip: {
20+
title: 'scatter',
21+
showCrosshairs: true,
22+
crosshairs: {
23+
type: 'xy',
24+
},
25+
},
26+
});
27+
28+
scatter.render();
29+
const { geometries } = scatter.chart;
30+
// @ts-ignore
31+
expect(geometries[0].tooltipOption.fields).toEqual(['weight', 'height', 'gender', '', '']);
32+
scatter.update({
33+
tooltip: false,
34+
});
35+
// @ts-ignore
36+
expect(scatter.chart.geometries[0].tooltipOption).toBeUndefined();
37+
scatter.destroy();
38+
});
39+
});

__tests__/unit/plots/scatter/default-config-spec.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ describe('scatter', () => {
1919
scatter.render();
2020
const { options } = scatter;
2121
// @ts-ignore
22-
expect(options.tooltip.offset).toBe(20);
22+
expect(options.tooltip.showTitle).toBe(false);
23+
// @ts-ignore
24+
expect(options.tooltip.showMarkers).toBe(false);
25+
// @ts-ignore
26+
expect(options.tooltip.showCrosshairs).toBeTruthy();
27+
// @ts-ignore
28+
expect(options.tooltip.crosshairs.type).toEqual('xy');
2329

2430
scatter.destroy();
2531
});

__tests__/unit/plots/scatter/legend-spec.ts

+49
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,55 @@ import { data } from '../../../data/gender';
33
import { createDiv } from '../../../utils/dom';
44

55
describe('scatter', () => {
6+
it('legend: default', () => {
7+
const scatter = new Scatter(createDiv(), {
8+
width: 400,
9+
height: 300,
10+
appendPadding: 10,
11+
data,
12+
xField: 'weight',
13+
yField: 'height',
14+
xAxis: {
15+
nice: true,
16+
},
17+
});
18+
19+
scatter.render();
20+
const legendController = scatter.chart.getController('legend');
21+
// @ts-ignore
22+
const { option } = legendController;
23+
expect(option).toBe(false);
24+
scatter.update({
25+
shapeField: 'gender',
26+
});
27+
// @ts-ignore
28+
expect(scatter.chart.getController('legend').option).toEqual({
29+
gender: undefined,
30+
height: false,
31+
weight: false,
32+
});
33+
scatter.update({
34+
shapeField: '',
35+
colorField: 'g',
36+
});
37+
// @ts-ignore
38+
expect(scatter.chart.getController('legend').option).toEqual({
39+
g: undefined,
40+
height: false,
41+
weight: false,
42+
});
43+
scatter.update({
44+
sizeField: 'gender',
45+
});
46+
// @ts-ignore
47+
expect(scatter.chart.getController('legend').option).toEqual({
48+
g: undefined,
49+
gender: false,
50+
height: false,
51+
weight: false,
52+
});
53+
scatter.destroy();
54+
});
655
it('legend: false', () => {
756
const scatter = new Scatter(createDiv(), {
857
width: 400,

__tests__/unit/plots/scatter/tooltip-spec.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,28 @@ describe('scatter', () => {
2323

2424
// @ts-ignore
2525
expect(scatter.chart.options.tooltip.title).toBe('scatter');
26+
// @ts-ignore
27+
expect(scatter.chart.geometries[0].tooltipOption.fields).toEqual(['weight', 'height', '', '', '']);
2628
scatter.update({
27-
...scatter.options,
28-
tooltip: false,
29+
colorField: 'gender',
30+
sizeField: 'g',
31+
shapeField: 'd',
2932
});
33+
// @ts-ignore
34+
expect(scatter.chart.geometries[0].tooltipOption.fields).toEqual(['weight', 'height', 'gender', 'g', 'd']);
3035

31-
expect(scatter.chart.getOptions().tooltip).toBe(undefined);
36+
scatter.update({
37+
tooltip: {
38+
fields: ['weight', 'height'],
39+
},
40+
});
41+
// @ts-ignore
42+
expect(scatter.chart.geometries[0].tooltipOption.fields).toEqual(['weight', 'height']);
43+
scatter.update({
44+
tooltip: false,
45+
});
46+
expect(scatter.chart.getOptions().tooltip).toBe(false);
47+
expect(scatter.chart.geometries[0].tooltipOption).toBeUndefined();
3248
expect(scatter.chart.getComponents().find((co) => co.type === 'tooltip')).toBe(undefined);
3349

3450
scatter.destroy();

src/plots/scatter/adaptor.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ import { ScatterOptions } from './types';
1313
*/
1414
function geometry(params: Params<ScatterOptions>): Params<ScatterOptions> {
1515
const { chart, options } = params;
16-
const { data, type, color, shape, size, pointStyle, colorField } = options;
16+
const { data, type, color, shape, size, pointStyle, shapeField, colorField, xField, yField, sizeField } = options;
1717

18+
let { tooltip } = options;
19+
20+
if (tooltip && !tooltip.fields) {
21+
tooltip = {
22+
...tooltip,
23+
fields: [xField, yField, colorField, sizeField, shapeField],
24+
};
25+
}
1826
// 数据
1927
chart.data(data);
2028

@@ -29,6 +37,7 @@ function geometry(params: Params<ScatterOptions>): Params<ScatterOptions> {
2937
size,
3038
style: pointStyle,
3139
},
40+
tooltip,
3241
},
3342
})
3443
);
@@ -216,13 +225,12 @@ function regressionLine(params: Params<ScatterOptions>): Params<ScatterOptions>
216225
*/
217226
export function tooltip(params: Params<ScatterOptions>): Params<ScatterOptions> {
218227
const { chart, options } = params;
219-
const { tooltip, shapeField, colorField, xField, yField, sizeField } = options;
228+
const { tooltip } = options;
220229

221230
if (tooltip) {
222-
chart.tooltip({
223-
fields: [xField, yField, colorField, sizeField, shapeField],
224-
...tooltip,
225-
});
231+
chart.tooltip(tooltip);
232+
} else if (tooltip === false) {
233+
chart.tooltip(false);
226234
}
227235

228236
return params;

src/plots/scatter/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ export class Scatter extends Plot<ScatterOptions> {
2222
return deepAssign({}, super.getDefaultOptions(), {
2323
size: 4,
2424
tooltip: {
25-
shared: null,
2625
showTitle: false,
27-
offset: 20,
26+
showMarkers: false,
27+
showCrosshairs: true,
28+
crosshairs: {
29+
type: 'xy',
30+
},
2831
},
2932
});
3033
}

0 commit comments

Comments
 (0)