Skip to content

Commit

Permalink
fix: conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
liufu.lf committed Dec 7, 2020
1 parent bee20b9 commit a8042a0
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 57 deletions.
37 changes: 0 additions & 37 deletions __tests__/unit/plots/scatter/data-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,6 @@ describe('scatter', () => {
expect(yScale.nice).toBe(false);
expect(yScale.min).toBe(0);
expect(yScale.max).toBe(data[0].height * 2);

const data1 = [{ gender: 'female', height: -161.2, weight: -51.6 }];
scatter.changeData(data1);
const c1_xScale = scatter.chart.getScaleByField('weight');
const c1_yScale = scatter.chart.getScaleByField('height');
expect(c1_xScale.max).toBe(0);
expect(c1_xScale.min).toBe(data1[0].weight * 2);
expect(c1_yScale.max).toBe(0);
expect(c1_yScale.min).toBe(data1[0].height * 2);

const data2 = [{ gender: 'female', height: 161.2, weight: -51.6 }];
scatter.changeData(data2);
const c2_xScale = scatter.chart.getScaleByField('weight');
const c2_yScale = scatter.chart.getScaleByField('height');
expect(c2_xScale.max).toBe(0);
expect(c2_xScale.min).toBe(data2[0].weight * 2);
expect(c2_yScale.min).toBe(0);
expect(c2_yScale.max).toBe(data2[0].height * 2);

const data3 = [{ gender: 'female', height: -161.2, weight: 51.6 }];
scatter.changeData(data3);
const c3_xScale = scatter.chart.getScaleByField('weight');
const c3_yScale = scatter.chart.getScaleByField('height');
expect(c3_xScale.min).toBe(0);
expect(c3_xScale.max).toBe(data3[0].weight * 2);
expect(c3_yScale.max).toBe(0);
expect(c3_yScale.min).toBe(data3[0].height * 2);

const data4 = [{ gender: 'female', height: 0, weight: 0 }];
scatter.changeData(data4);
const c4_xScale = scatter.chart.getScaleByField('weight');
const c4_yScale = scatter.chart.getScaleByField('height');
expect(c4_xScale.min).toBe(0);
expect(c4_xScale.max).toBe(0);
expect(c4_yScale.max).toBe(0);
expect(c4_yScale.min).toBe(0);
scatter.destroy();
});
it('transformOptions & axis min max', () => {
const scatter = new Scatter(createDiv(), {
Expand Down
147 changes: 147 additions & 0 deletions __tests__/unit/utils/get-meta-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { getMeta } from '../../../src/plots/scatter/util';

describe('getMeta', () => {
it('adjustMeta & data.length === 1', () => {
// default
expect(
getMeta({
data: [{ gender: 'female', weight: 50, height: 160 }],
xField: 'weight',
yField: 'height',
})
).toEqual({
weight: {
min: 0,
max: 100,
},
height: {
min: 0,
max: 320,
},
});

// isPositiveNumber: false
expect(
getMeta({
data: [{ gender: 'female', weight: -50, height: -160 }],
xField: 'weight',
yField: 'height',
})
).toEqual({
weight: {
max: 0,
min: -100,
},
height: {
max: 0,
min: -320,
},
});
// meta 0
expect(
getMeta({
data: [{ gender: 'female', weight: 50, height: 160 }],
xField: 'weight',
yField: 'height',
meta: {
weight: {
min: 0,
max: 10,
},
height: {
min: 0,
max: 20,
},
},
})
).toEqual({
weight: {
min: 0,
max: 10,
},
height: {
min: 0,
max: 20,
},
});
// meta undefined or null
expect(
getMeta({
data: [{ gender: 'female', weight: 50, height: 160 }],
xField: 'weight',
yField: 'height',
meta: {
weight: {
min: undefined,
max: null,
},
height: {
min: null,
max: undefined,
},
},
})
).toEqual({
weight: {
min: 0,
max: 100,
},
height: {
min: 0,
max: 320,
},
});

// other config
expect(
getMeta({
data: [{ gender: 'female', weight: 50, height: 160 }],
xField: 'weight',
yField: 'height',
meta: {
gender: {
max: 100,
},
weight: {
range: [0, 1],
},
height: {
values: [1.2, 24],
},
},
})
).toEqual({
gender: {
max: 100,
},
weight: {
min: 0,
max: 100,
range: [0, 1],
},
height: {
min: 0,
max: 320,
values: [1.2, 24],
},
});

// 边沿值
expect(
getMeta({
data: [{ gender: 'female', weight: 0, height: 0 }],
xField: 'weight',
yField: 'height',
})
).toEqual({
weight: {
min: 0,
max: 0,
},
height: {
min: 0,
max: 0,
},
});
});
});
23 changes: 4 additions & 19 deletions src/plots/scatter/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isBoolean, get } from '@antv/util';
import { isBoolean } from '@antv/util';
import { Params } from '../../core/adaptor';
import { flow, deepAssign } from '../../utils';
import { point } from '../../adaptor/geometries';
import { interaction, animation, theme, scale, annotation } from '../../adaptor/common';
import { findGeometry, transformLabel } from '../../utils';
import { getQuadrantDefaultConfig, getPath } from './util';
import { getQuadrantDefaultConfig, getPath, getMeta } from './util';
import { ScatterOptions } from './types';

/**
Expand All @@ -17,25 +17,10 @@ export function transformOptions(params: Params<ScatterOptions>): Params<Scatter
const { data = [] } = options;
// 仅对 data.length === 1 的情况进行处理
if (data.length === 1) {
const { xAxis, yAxis, meta, xField, yField } = options;
const xFieldValue = data[0][xField];
const yFieldValue = data[0][yField];
const xIsPositiveNumber = xFieldValue > 0;
const yIsPositiveNumber = yFieldValue > 0;
return deepAssign({}, params, {
options: {
...options,
xAxis: {
...xAxis,
// 优先级 xAxis > meta[xField]
min: get(xAxis, 'min') || get(meta, [xField, 'min']) || (xIsPositiveNumber ? 0 : xFieldValue * 2),
max: get(xAxis, 'max') || get(meta, [xField, 'max']) || (xIsPositiveNumber ? xFieldValue * 2 : 0),
},
yAxis: {
...yAxis,
min: get(yAxis, 'min') || get(meta, [yField, 'min']) || (yIsPositiveNumber ? 0 : yFieldValue * 2),
max: get(yAxis, 'max') || get(meta, [yField, 'max']) || (yIsPositiveNumber ? yFieldValue * 2 : 0),
},
meta: getMeta(options),
},
});
}
Expand Down Expand Up @@ -123,7 +108,7 @@ function axis(params: Params<ScatterOptions>): Params<ScatterOptions> {
*/
function legend(params: Params<ScatterOptions>): Params<ScatterOptions> {
const { chart, options } = params;
const { legend, colorField, shapeField, sizeField, data } = options;
const { legend, colorField, shapeField, sizeField } = options;
// legend 没有指定时根据 shapeField 和 colorField 来设置默认值
const showLegend = isBoolean(legend) ? legend : legend || !!(shapeField || colorField);
if (showLegend) {
Expand Down
43 changes: 42 additions & 1 deletion src/plots/scatter/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
regressionPow,
regressionQuad,
} from 'd3-regression';
import { isArray } from '@antv/util';
import { isArray, get, isNumber } from '@antv/util';
import { View } from '@antv/g2';
import { getSplinePath } from '../../utils';
import { ScatterOptions } from './types';
Expand Down Expand Up @@ -161,3 +161,44 @@ export const getPath = (config: RenderOptions) => {
}
return splinePath(pathData, config);
};

// 散点图data.length === 1时调整 meta: {min, max}
export const getMeta = (options: ScatterOptions): ScatterOptions['meta'] => {
const { meta = {}, xField, yField, data } = options;
const xFieldValue = data[0][xField];
const yFieldValue = data[0][yField];
const xIsPositiveNumber = xFieldValue > 0;
const yIsPositiveNumber = yFieldValue > 0;

const getValue = (field: string, type: 'min' | 'max', axis: 'x' | 'y') => {
const customValue = get(meta, [field, type]);
if (isNumber(customValue)) {
return customValue;
}
if (axis === 'x') {
const rangeX = {
min: xIsPositiveNumber ? 0 : xFieldValue * 2,
max: xIsPositiveNumber ? xFieldValue * 2 : 0,
};
return rangeX[type];
}
const rangeY = {
min: yIsPositiveNumber ? 0 : yFieldValue * 2,
max: yIsPositiveNumber ? yFieldValue * 2 : 0,
};
return rangeY[type];
};
return {
...meta,
[xField]: {
...meta[xField],
min: getValue(xField, 'min', 'x'),
max: getValue(xField, 'max', 'x'),
},
[yField]: {
...meta[yField],
min: getValue(yField, 'min', 'y'),
max: getValue(yField, 'max', 'y'),
},
};
};

0 comments on commit a8042a0

Please sign in to comment.