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: violin #2593

Merged
merged 18 commits into from
Jun 4, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/auto-inspection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v2
- name: Get branch name (pull request)
run: echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF})" >> $GITHUB_ENV
- uses: lxfu1/surge-preview@v2.0.1
- uses: lxfu1/surge-preview@v2.0.1
id: preview_step
with:
project_name: 'G2Plot'
Expand Down
602 changes: 602 additions & 0 deletions __tests__/data/violin.ts

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions __tests__/unit/plots/violin/animation-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Violin } from '../../../../src';
import { BASE_VIOLIN_DATA } from '../../../data/violin';
import { createDiv } from '../../../utils/dom';

describe('violin legend', () => {
const violin = new Violin(createDiv(), {
width: 400,
height: 500,
data: BASE_VIOLIN_DATA,
xField: 'type',
yField: 'value',
animation: {
enter: {
animation: 'fade-in',
},
leave: {
animation: 'fade-out',
},
},
});

violin.render();

it('default', () => {
// 新的 geometry violin 暂时未追加
expect(violin.chart.views[0].geometries[0].animateOption).toEqual({
enter: {
animation: 'fade-in',
},
leave: {
animation: 'fade-out',
},
});

// 追加默认的动画配置
expect(violin.chart.views[1].geometries[0].animateOption).toMatchObject({
appear: {
duration: 450,
easing: 'easeQuadOut',
},
update: {
duration: 400,
easing: 'easeQuadInOut',
},
enter: {
duration: 400,
easing: 'easeQuadInOut',
animation: 'fade-in',
},
leave: {
duration: 350,
easing: 'easeQuadIn',
animation: 'fade-out',
},
});
});

it('update', () => {
violin.update({
animation: {
appear: {
animation: 'fade-in',
},
leave: {
animation: 'wave-out',
},
},
});
expect(violin.chart.views[0].geometries[0].animateOption).toEqual({
appear: {
animation: 'fade-in',
},
enter: {
animation: 'fade-in',
},
leave: {
animation: 'wave-out',
},
});
});

afterAll(() => {
violin.destroy();
});
});
65 changes: 65 additions & 0 deletions __tests__/unit/plots/violin/annotation-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Violin } from '../../../../src';
import { BASE_VIOLIN_DATA } from '../../../data/violin';
import { createDiv } from '../../../utils/dom';

describe('violin legend', () => {
const violin = new Violin(createDiv(), {
width: 400,
height: 500,
data: BASE_VIOLIN_DATA,
xField: 'type',
yField: 'value',
});

violin.render();

it('text annotation', () => {
violin.update({
annotations: [
{
type: 'text',
position: ['median', 'median'],
content: '辅助文本',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 annotation 是什么效果?
是不是应该加个 example

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

图形标注(我不知道有什么应用场景)

},
],
});

const controller = violin.chart.views[0].getController('annotation');
expect(controller.getComponents().length).toBe(1);
expect(controller.getComponents()[0].component.get('content')).toBe('辅助文本');
});

it('text annotation and line annotation', () => {
violin.update({
...violin.options,
annotations: [
{
type: 'text',
position: ['min', 'median'],
content: '辅助文本',
offsetY: -4,
style: {
textBaseviolin: 'bottom',
},
},
{
type: 'line',
start: ['min', 'median'],
end: ['max', 'median'],
style: {
stroke: 'red',
violinDash: [2, 2],
},
},
],
});
const controller = violin.chart.views[0].getController('annotation');
expect(controller.getComponents().length).toBe(2);
expect(controller.getComponents()[0].component.get('content')).toBe('辅助文本');
expect(controller.getComponents()[1].component.get('type')).toBe('line');
});

afterAll(() => {
violin.destroy();
});
});
65 changes: 65 additions & 0 deletions __tests__/unit/plots/violin/axis-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Violin } from '../../../../src';
import { BASE_VIOLIN_DATA } from '../../../data/violin';
import { createDiv } from '../../../utils/dom';

describe('violin axis', () => {
it('没有 seriesField', () => {
const violin = new Violin(createDiv(), {
width: 400,
height: 500,
data: BASE_VIOLIN_DATA,
xField: 'type',
yField: 'value',
});

violin.render();

let axisController = violin.chart.views[0].getController('axis');
expect(axisController.getComponents().length).toBe(4);

violin.update({ xAxis: { grid: null } });
axisController = violin.chart.views[0].getController('axis');
expect(axisController.getComponents().length).toBe(3);

violin.update({ xAxis: { title: { text: 'xx' } } });
axisController = violin.chart.views[0].getController('axis');
expect(axisController.getComponents()[0].component.get('title').text).toBe('xx');

// 关闭 xAxis
violin.update({ xAxis: false });
axisController = violin.chart.views[0].getController('axis');
expect(axisController.getComponents().length).toBe(2);

violin.destroy();
});

it('有 seriesField', () => {
const violin = new Violin(createDiv(), {
width: 400,
height: 500,
data: BASE_VIOLIN_DATA,
xField: 'type',
yField: 'value',
seriesField: 'species',
});

violin.render();
let axisController = violin.chart.views[0].getController('axis');
expect(axisController.getComponents().length).toBe(4);

violin.update({ xAxis: { grid: null } });
axisController = violin.chart.views[0].getController('axis');
expect(axisController.getComponents().length).toBe(3);

violin.update({ xAxis: { title: { text: 'xx' } } });
axisController = violin.chart.views[0].getController('axis');
expect(axisController.getComponents()[0].component.get('title').text).toBe('xx');

// 关闭 xAxis
violin.update({ xAxis: false });
axisController = violin.chart.views[0].getController('axis');
expect(axisController.getComponents().length).toBe(2);

violin.destroy();
});
});
50 changes: 50 additions & 0 deletions __tests__/unit/plots/violin/box-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Violin } from '../../../../src';
import { MIN_MAX_VIEW_ID, QUANTILE_VIEW_ID, MEDIAN_VIEW_ID } from '../../../../src/plots/violin/constant';
import { BASE_VIOLIN_DATA } from '../../../data/violin';
import { createDiv } from '../../../utils/dom';

describe('violin', () => {
it('renders box views.', () => {
const violin = new Violin(createDiv(), {
width: 400,
height: 500,
data: BASE_VIOLIN_DATA,
xField: 'type',
yField: 'value',
});

violin.render();
const minMaxView = violin.chart.views.find((view) => view.id === MIN_MAX_VIEW_ID);
const quantileView = violin.chart.views.find((view) => view.id === QUANTILE_VIEW_ID);
const medianView = violin.chart.views.find((view) => view.id === MEDIAN_VIEW_ID);

expect(minMaxView.geometries[0].type).toBe('interval');
expect(quantileView.geometries[0].type).toBe('interval');
expect(medianView.geometries[0].type).toBe('point');

violin.destroy();
});

// 暂时不开放 box 配置
it("should not render box views when 'box' set to false.", () => {
const violin = new Violin(createDiv(), {
width: 400,
height: 500,
data: BASE_VIOLIN_DATA,
xField: 'type',
yField: 'value',
box: false,
});

violin.render();
const minMaxView = violin.chart.views.find((view) => view.id === MIN_MAX_VIEW_ID);
const quantileView = violin.chart.views.find((view) => view.id === QUANTILE_VIEW_ID);
const medianView = violin.chart.views.find((view) => view.id === MEDIAN_VIEW_ID);

expect(minMaxView).toBeUndefined();
expect(quantileView).toBeUndefined();
expect(medianView).toBeUndefined();

violin.destroy();
});
});
30 changes: 30 additions & 0 deletions __tests__/unit/plots/violin/change-data-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { group } from '@antv/util';
import { Violin } from '../../../../src';
import { VIOLIN_VIEW_ID } from '../../../../src/plots/violin/constant';
import { BASE_VIOLIN_DATA } from '../../../data/violin';
import { createDiv } from '../../../utils/dom';

describe('violin change data', () => {
it('renders new violins when data changed', () => {
const violin = new Violin(createDiv(), {
width: 400,
height: 500,
data: BASE_VIOLIN_DATA,
xField: 'type',
yField: 'value',
});

violin.render();
const g = violin.chart.views.find((view) => view.id === VIOLIN_VIEW_ID).geometries[0];
expect(g.elements.length).toBe(group(BASE_VIOLIN_DATA, 'type').length);

const newData = BASE_VIOLIN_DATA.filter((data) => data.type !== 'PetalWidth');

violin.changeData(newData);
const newG = violin.chart.views.find((view) => view.id === VIOLIN_VIEW_ID).geometries[0];
expect(violin.options.data).toEqual(newData);
expect(newG.elements.length).toBe(group(newData, 'type').length);

violin.destroy();
});
});
Loading