Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 散点图一个数据点时调整 min max #2075

Merged
merged 2 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 230 additions & 0 deletions __tests__/unit/plots/scatter/data-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
import { Scatter } from '../../../../src';
import { createDiv } from '../../../utils/dom';

const data = [{ gender: 'female', height: 161.2, weight: 51.6 }];

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

scatter.render();

const geometry = scatter.chart.geometries[0];
const elements = geometry.elements;
expect(elements.length).toBe(1);
const xScale = scatter.chart.getScaleByField('weight');
const yScale = scatter.chart.getScaleByField('height');
// @ts-ignore
expect(xScale.nice).toBe(false);
expect(xScale.min).toBe(0);
expect(xScale.max).toBe(data[0].weight * 2);
// @ts-ignore
expect(yScale.nice).toBe(false);
expect(yScale.min).toBe(0);
expect(yScale.max).toBe(data[0].height * 2);
});
it('transformOptions & axis min max', () => {
const scatter = new Scatter(createDiv(), {
width: 400,
height: 300,
appendPadding: 10,
data,
xField: 'weight',
yField: 'height',
xAxis: {
nice: false,
max: 200,
min: -10,
},
yAxis: {
nice: false,
max: 100,
min: -20,
},
});

scatter.render();
const xScale = scatter.chart.getScaleByField('weight');
const yScale = scatter.chart.getScaleByField('height');
expect(xScale.min).toBe(-10);
expect(xScale.max).toBe(200);
expect(yScale.min).toBe(-20);
expect(yScale.max).toBe(100);
scatter.destroy();
});
it('transformOptions & meta min max', () => {
const scatter = new Scatter(createDiv(), {
width: 400,
height: 300,
appendPadding: 10,
data,
xField: 'weight',
yField: 'height',
meta: {
weight: {
max: 200,
min: -10,
},
height: {
max: 100,
min: -20,
},
},
xAxis: {
nice: false,
},
yAxis: {
nice: false,
},
});

scatter.render();
const xScale = scatter.chart.getScaleByField('weight');
const yScale = scatter.chart.getScaleByField('height');
expect(xScale.min).toBe(-10);
expect(xScale.max).toBe(200);
expect(yScale.min).toBe(-20);
expect(yScale.max).toBe(100);
scatter.destroy();
});
it('transformOptions & axis and meta min max', () => {
const scatter = new Scatter(createDiv(), {
width: 400,
height: 300,
appendPadding: 10,
data,
xField: 'weight',
yField: 'height',
meta: {
weight: {
max: 200,
min: -10,
},
height: {
max: 100,
min: -20,
},
},
xAxis: {
nice: false,
max: 300,
min: -30,
},
yAxis: {
nice: false,
max: 400,
min: -40,
},
});

scatter.render();
const xScale = scatter.chart.getScaleByField('weight');
const yScale = scatter.chart.getScaleByField('height');
expect(xScale.min).toBe(-30);
expect(xScale.max).toBe(300);
expect(yScale.min).toBe(-40);
expect(yScale.max).toBe(400);
scatter.destroy();
});

it('transformOptions & axis min max', () => {
const scatter = new Scatter(createDiv(), {
width: 400,
height: 300,
appendPadding: 10,
data,
xField: 'weight',
yField: 'height',
xAxis: {
nice: false,
max: 200,
min: -10,
},
yAxis: {
nice: false,
max: 100,
min: -20,
},
});

scatter.render();
const xScale = scatter.chart.getScaleByField('weight');
const yScale = scatter.chart.getScaleByField('height');
expect(xScale.min).toBe(-10);
expect(xScale.max).toBe(200);
expect(yScale.min).toBe(-20);
expect(yScale.max).toBe(100);
scatter.destroy();
});
it('transformOptions & meta min max', () => {
const scatter = new Scatter(createDiv(), {
width: 400,
height: 300,
appendPadding: 10,
data,
xField: 'weight',
yField: 'height',
meta: {
weight: {
max: 200,
min: -10,
},
height: {
max: 100,
min: -20,
},
},
xAxis: {
nice: false,
},
yAxis: {
nice: false,
},
});

scatter.render();
const xScale = scatter.chart.getScaleByField('weight');
const yScale = scatter.chart.getScaleByField('height');
expect(xScale.min).toBe(-10);
expect(xScale.max).toBe(200);
expect(yScale.min).toBe(-20);
expect(yScale.max).toBe(100);
scatter.destroy();
});
it('transformOptions & data.length > 1', () => {
const scatter = new Scatter(createDiv(), {
width: 400,
height: 300,
appendPadding: 10,
data: [
{ gender: 'female', height: 161.2, weight: 51.6 },
{ gender: 'female', height: 61.2, weight: 151.6 },
],
xField: 'weight',
yField: 'height',
});

scatter.render();
const xScale = scatter.chart.getScaleByField('weight');
const yScale = scatter.chart.getScaleByField('height');
expect(xScale.min).toBe(40);
expect(xScale.max).toBe(160);
expect(yScale.min).toBe(50);
expect(yScale.max).toBe(175);

scatter.destroy();
});
});
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,
},
});
});
});
Loading