Skip to content

Commit

Permalink
fix: column和bar默认type为cat (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcsin1 authored Oct 1, 2020
1 parent 4e62f10 commit 78f3a0c
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 4 deletions.
53 changes: 53 additions & 0 deletions __tests__/data/sales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,56 @@ export const subSalesByArea = [
sales: 469341.684,
},
];

export const timeColumnData = [
{
year: '1991-1-1',
value: 3,
type: 'Lon',
},
{
year: '1991-1-2',
value: 4,
type: 'Lon',
},
{
year: '1991-1-3',
value: 3.5,
type: 'Lon',
},
{
year: '1991-1-4',
value: 5,
type: 'Lon',
},
{
year: '1991-1-5',
value: 4.9,
type: 'Lon',
},
{
year: '1991-1-1',
value: 3,
type: 'Bor',
},
{
year: '1991-1-2',
value: 4,
type: 'Bor',
},
{
year: '1991-1-3',
value: 3.5,
type: 'Bor',
},
{
year: '1991-1-4',
value: 5,
type: 'Bor',
},
{
year: '1991-1-5',
value: 4.9,
type: 'Bor',
},
];
4 changes: 4 additions & 0 deletions __tests__/unit/plots/bar/axis-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ describe('bar axis', () => {
// @ts-ignore
expect(geometry.scales.sales.nice).toBe(true);
expect(geometry.scales.sales.formatter).toBe(formatter);

// 默认 yField 为 cat 类型
// @ts-ignore
expect(geometry.scales.area.type).toBe('cat');
});

it('xAxis', () => {
Expand Down
28 changes: 27 additions & 1 deletion __tests__/unit/plots/bar/index-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bar } from '../../../../src';
import { salesByArea, subSalesByArea } from '../../../data/sales';
import { salesByArea, subSalesByArea, timeColumnData } from '../../../data/sales';
import { createDiv } from '../../../utils/dom';

describe('bar', () => {
Expand All @@ -25,6 +25,10 @@ describe('bar', () => {
expect(positionFields).toHaveLength(2);
expect(positionFields[0]).toBe('area');
expect(positionFields[1]).toBe('sales');

// 默认 yField 为 cat 类型
// @ts-ignore
expect(geometry.scales.area.type).toBe('cat');
});

it('x*y*color', () => {
Expand Down Expand Up @@ -257,4 +261,26 @@ describe('bar', () => {

expect(bar.chart.interactions['active-region']).toBeDefined();
});

it('x*y*cat', () => {
const bar = new Bar(createDiv('x*y*cat'), {
width: 300,
height: 400,
data: timeColumnData,
isStack: true,
xField: 'value',
yField: 'year',
seriesField: 'type',
});

bar.render();
const geometry = bar.chart.geometries[0];
expect(geometry.getAdjust('stack')).toMatchObject({
xField: 'year',
yField: 'value',
});
// 默认 yField 为 cat 类型
// @ts-ignore
expect(geometry.scales.year.type).toBe('cat');
});
});
4 changes: 4 additions & 0 deletions __tests__/unit/plots/column/axis-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ describe('column axis', () => {
// @ts-ignore
expect(geometry.scales.sales.nice).toBe(true);
expect(geometry.scales.sales.formatter).toBe(formatter);

// 柱状图默认为 cat 类型
// @ts-ignore
expect(geometry.scales.area.type).toBe('cat');
});

it('xAxis', () => {
Expand Down
28 changes: 27 additions & 1 deletion __tests__/unit/plots/column/index-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Column } from '../../../../src';
import { salesByArea, subSalesByArea } from '../../../data/sales';
import { salesByArea, subSalesByArea, timeColumnData } from '../../../data/sales';
import { createDiv } from '../../../utils/dom';

describe('column', () => {
Expand All @@ -25,6 +25,10 @@ describe('column', () => {
expect(positionFields).toHaveLength(2);
expect(positionFields[0]).toBe('area');
expect(positionFields[1]).toBe('sales');

// 柱状图 xField 默认为 cat 类型
// @ts-ignore
expect(geometry.scales.area.type).toBe('cat');
});

it('x*y*color', () => {
Expand Down Expand Up @@ -267,4 +271,26 @@ describe('column', () => {

expect(column.chart.interactions['active-region']).toBeDefined();
});
it('x*y*cat', () => {
const column = new Column(createDiv('x*y*cat'), {
width: 300,
height: 400,
isStack: true,
data: timeColumnData,
xField: 'year',
yField: 'value',
seriesField: 'type',
});

column.render();
const geometry = column.chart.geometries[0];

expect(geometry.getAdjust('stack')).toMatchObject({
xField: 'year',
yField: 'value',
});
// 柱状图 xField 默认为 cat 类型
// @ts-ignore
expect(geometry.scales.year.type).toBe('cat');
});
});
7 changes: 6 additions & 1 deletion src/plots/bar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Bar extends Plot<BarOptions> {
* 获取 条形图 默认配置
*/
protected getDefaultOptions(options: BarOptions) {
const { isPercent, isRange, label, xField } = options;
const { isPercent, isRange, label, xField, yField } = options;
return deepMix({}, super.getDefaultOptions(), {
label:
label && isRange
Expand All @@ -40,6 +40,11 @@ export class Bar extends Plot<BarOptions> {
: '',
},
interactions: [{ type: 'active-region' }],
meta: {
[yField]: {
type: 'cat',
},
},
});
}

Expand Down
7 changes: 6 additions & 1 deletion src/plots/column/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Column extends Plot<ColumnOptions> {
* 获取 柱形图 默认配置
*/
protected getDefaultOptions(options: ColumnOptions) {
const { isPercent, isRange, label, yField } = options;
const { isPercent, isRange, label, yField, xField } = options;
return deepMix({}, super.getDefaultOptions(), {
label:
label && isRange
Expand All @@ -40,6 +40,11 @@ export class Column extends Plot<ColumnOptions> {
: '',
},
interactions: [{ type: 'active-region' }],
meta: {
[xField]: {
type: 'cat',
},
},
});
}

Expand Down

0 comments on commit 78f3a0c

Please sign in to comment.