Skip to content

Commit

Permalink
fix: 修复散点图 tooltip 默认 fields 丢失 (#2019)
Browse files Browse the repository at this point in the history
* fix: 修复散点图 tooltip 默认 fields 丢失

* test: 添加scatter legend 单测

Co-authored-by: liufu.lf <liufu.lf@antfin.com>
  • Loading branch information
lxfu1 and liufu.lf authored Nov 29, 2020
1 parent 0882ca3 commit 81885fe
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 12 deletions.
39 changes: 39 additions & 0 deletions __tests__/bugs/issue-2015-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Scatter } from '../../src';
import { data } from '../data/gender';
import { createDiv } from '../utils/dom';

describe('issue 2015', () => {
it('scatter fields', () => {
const scatter = new Scatter(createDiv(), {
width: 400,
height: 300,
data,
xField: 'weight',
yField: 'height',
size: 10,
colorField: 'gender',
shape: 'circle',
xAxis: {
nice: true,
},
tooltip: {
title: 'scatter',
showCrosshairs: true,
crosshairs: {
type: 'xy',
},
},
});

scatter.render();
const { geometries } = scatter.chart;
// @ts-ignore
expect(geometries[0].tooltipOption.fields).toEqual(['weight', 'height', 'gender', '', '']);
scatter.update({
tooltip: false,
});
// @ts-ignore
expect(scatter.chart.geometries[0].tooltipOption).toBeUndefined();
scatter.destroy();
});
});
8 changes: 7 additions & 1 deletion __tests__/unit/plots/scatter/default-config-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ describe('scatter', () => {
scatter.render();
const { options } = scatter;
// @ts-ignore
expect(options.tooltip.offset).toBe(20);
expect(options.tooltip.showTitle).toBe(false);
// @ts-ignore
expect(options.tooltip.showMarkers).toBe(false);
// @ts-ignore
expect(options.tooltip.showCrosshairs).toBeTruthy();
// @ts-ignore
expect(options.tooltip.crosshairs.type).toEqual('xy');

scatter.destroy();
});
Expand Down
49 changes: 49 additions & 0 deletions __tests__/unit/plots/scatter/legend-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,55 @@ import { data } from '../../../data/gender';
import { createDiv } from '../../../utils/dom';

describe('scatter', () => {
it('legend: default', () => {
const scatter = new Scatter(createDiv(), {
width: 400,
height: 300,
appendPadding: 10,
data,
xField: 'weight',
yField: 'height',
xAxis: {
nice: true,
},
});

scatter.render();
const legendController = scatter.chart.getController('legend');
// @ts-ignore
const { option } = legendController;
expect(option).toBe(false);
scatter.update({
shapeField: 'gender',
});
// @ts-ignore
expect(scatter.chart.getController('legend').option).toEqual({
gender: undefined,
height: false,
weight: false,
});
scatter.update({
shapeField: '',
colorField: 'g',
});
// @ts-ignore
expect(scatter.chart.getController('legend').option).toEqual({
g: undefined,
height: false,
weight: false,
});
scatter.update({
sizeField: 'gender',
});
// @ts-ignore
expect(scatter.chart.getController('legend').option).toEqual({
g: undefined,
gender: false,
height: false,
weight: false,
});
scatter.destroy();
});
it('legend: false', () => {
const scatter = new Scatter(createDiv(), {
width: 400,
Expand Down
22 changes: 19 additions & 3 deletions __tests__/unit/plots/scatter/tooltip-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,28 @@ describe('scatter', () => {

// @ts-ignore
expect(scatter.chart.options.tooltip.title).toBe('scatter');
// @ts-ignore
expect(scatter.chart.geometries[0].tooltipOption.fields).toEqual(['weight', 'height', '', '', '']);
scatter.update({
...scatter.options,
tooltip: false,
colorField: 'gender',
sizeField: 'g',
shapeField: 'd',
});
// @ts-ignore
expect(scatter.chart.geometries[0].tooltipOption.fields).toEqual(['weight', 'height', 'gender', 'g', 'd']);

expect(scatter.chart.getOptions().tooltip).toBe(undefined);
scatter.update({
tooltip: {
fields: ['weight', 'height'],
},
});
// @ts-ignore
expect(scatter.chart.geometries[0].tooltipOption.fields).toEqual(['weight', 'height']);
scatter.update({
tooltip: false,
});
expect(scatter.chart.getOptions().tooltip).toBe(false);
expect(scatter.chart.geometries[0].tooltipOption).toBeUndefined();
expect(scatter.chart.getComponents().find((co) => co.type === 'tooltip')).toBe(undefined);

scatter.destroy();
Expand Down
20 changes: 14 additions & 6 deletions src/plots/scatter/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ import { ScatterOptions } from './types';
*/
function geometry(params: Params<ScatterOptions>): Params<ScatterOptions> {
const { chart, options } = params;
const { data, type, color, shape, size, pointStyle, colorField } = options;
const { data, type, color, shape, size, pointStyle, shapeField, colorField, xField, yField, sizeField } = options;

let { tooltip } = options;

if (tooltip && !tooltip.fields) {
tooltip = {
...tooltip,
fields: [xField, yField, colorField, sizeField, shapeField],
};
}
// 数据
chart.data(data);

Expand All @@ -29,6 +37,7 @@ function geometry(params: Params<ScatterOptions>): Params<ScatterOptions> {
size,
style: pointStyle,
},
tooltip,
},
})
);
Expand Down Expand Up @@ -216,13 +225,12 @@ function regressionLine(params: Params<ScatterOptions>): Params<ScatterOptions>
*/
export function tooltip(params: Params<ScatterOptions>): Params<ScatterOptions> {
const { chart, options } = params;
const { tooltip, shapeField, colorField, xField, yField, sizeField } = options;
const { tooltip } = options;

if (tooltip) {
chart.tooltip({
fields: [xField, yField, colorField, sizeField, shapeField],
...tooltip,
});
chart.tooltip(tooltip);
} else if (tooltip === false) {
chart.tooltip(false);
}

return params;
Expand Down
7 changes: 5 additions & 2 deletions src/plots/scatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ export class Scatter extends Plot<ScatterOptions> {
return deepAssign({}, super.getDefaultOptions(), {
size: 4,
tooltip: {
shared: null,
showTitle: false,
offset: 20,
showMarkers: false,
showCrosshairs: true,
crosshairs: {
type: 'xy',
},
},
});
}
Expand Down

0 comments on commit 81885fe

Please sign in to comment.