Skip to content

Commit

Permalink
test(runtime): add point
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed Apr 5, 2022
1 parent fe4575f commit 979954f
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 10 deletions.
2 changes: 1 addition & 1 deletion __tests__/unit/infer/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('infer', () => {

const e2 = {};
expect(infer({ encode: e2 }).encode).toEqual({
size: { type: 'constant', value: 2 },
size: { type: 'constant', value: 3 },
});
});

Expand Down
69 changes: 69 additions & 0 deletions __tests__/unit/runtime/point.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { G2Spec, render } from '../../../src';
import { createDiv, mount } from '../../utils/dom';

describe('point', () => {
it('render({...}) should render basic scatter', () => {
const chart = render<G2Spec>({
type: 'point',
transform: [
{
type: 'fetch',
url: 'https://gw.alipayobjects.com/os/basement_prod/6b4aa721-b039-49b9-99d8-540b3f87d339.json',
},
],
encode: {
x: 'height',
y: 'weight',
color: 'gender',
shape: 'hollowPoint',
},
});

mount(createDiv(), chart);
});

it('render({...}) should render scatter with only one dimension', () => {
const chart = render<G2Spec>({
type: 'point',
transform: [
{
type: 'fetch',
url: 'https://gw.alipayobjects.com/os/basement_prod/6b4aa721-b039-49b9-99d8-540b3f87d339.json',
},
],
height: 120,
scale: { y: { guide: null } },
encode: {
x: 'height',
shape: 'hollowPoint',
},
});

mount(createDiv(), chart);
});

it('render({...}) should render bubble chart', () => {
const chart = render<G2Spec>({
type: 'point',
transform: [
{
type: 'fetch',
url: 'https://gw.alipayobjects.com/os/antvdemo/assets/data/bubble.json',
},
],
scale: { size: { type: 'log', range: [4, 20] }, y: { field: 'Life' } },
encode: {
x: 'GDP',
y: 'LifeExpectancy',
size: 'Population',
color: 'continent',
},
style: {
fillOpacity: 0.3,
lineWidth: 1,
},
});

mount(createDiv(), chart);
});
});
15 changes: 14 additions & 1 deletion __tests__/unit/stdlib/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createLibrary } from '../../../src/stdlib';
import { Canvas } from '../../../src/renderer';
import { Cartesian, Polar, Transpose, Parallel } from '../../../src/coordinate';
import { Constant, Field, Transform } from '../../../src/encode';
import { Interval, Line } from '../../../src/geometry';
import { Interval, Line, Point as PointGeometry } from '../../../src/geometry';
import {
MaybeTuple,
MaybeZeroX1,
Expand All @@ -11,6 +11,8 @@ import {
MaybeSplitPosition,
MaybeStackY,
MaybeKey,
MaybeSize,
MaybeZeroY1,
} from '../../../src/infer';
import { Category10, Category20 } from '../../../src/palette';
import {
Expand All @@ -20,12 +22,16 @@ import {
Identity,
Point,
Time,
Log,
Pow,
} from '../../../src/scale';
import {
Rect as RectShape,
HollowRect,
Line as LineShape,
Smooth,
Point as PointShape,
HollowPoint,
} from '../../../src/shape';
import {
DodgeX,
Expand Down Expand Up @@ -57,13 +63,16 @@ describe('stdlib', () => {
'encode.transform': Transform,
'mark.interval': Interval,
'mark.line': Line,
'mark.point': PointGeometry,
'infer.maybeTuple': MaybeTuple,
'infer.maybeZeroX1': MaybeZeroX1,
'infer.maybeZeroY2': MaybeZeroY2,
'infer.maybeSeries': MaybeSeries,
'infer.maybeStackY': MaybeStackY,
'infer.maybeSplitPosition': MaybeSplitPosition,
'infer.maybeKey': MaybeKey,
'infer.maybeSize': MaybeSize,
'infer.maybeZeroY1': MaybeZeroY1,
'palette.category10': Category10,
'palette.category20': Category20,
'scale.linear': Linear,
Expand All @@ -72,10 +81,14 @@ describe('stdlib', () => {
'scale.identity': Identity,
'scale.point': Point,
'scale.time': Time,
'scale.log': Log,
'scale.pow': Pow,
'shape.rect': RectShape,
'shape.hollowRect': HollowRect,
'shape.line': LineShape,
'shape.smooth': Smooth,
'shape.point': PointShape,
'shape.hollowPoint': HollowPoint,
'statistic.stackY': StackY,
'statistic.dodgeX': DodgeX,
'statistic.stackEnter': StackEnter,
Expand Down
2 changes: 1 addition & 1 deletion src/infer/maybeSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function constant(value: any) {
return { type: 'constant', value };
}

function inferEncode({ size = constant(2), ...rest }: InferredEncode) {
function inferEncode({ size = constant(3), ...rest }: InferredEncode) {
return { size, ...rest };
}

Expand Down
19 changes: 14 additions & 5 deletions src/spec/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ import { Coordinate } from './coordinate';
import { Statistic } from './statistic';
import { Animation } from './animate';

export type Geometry = IntervalGeometry | LineGeometry | CustomComponent;
export type Geometry =
| IntervalGeometry
| LineGeometry
| PointGeometry
| CustomComponent;

export type GeometryTypes = 'interval' | 'line' | MarkComponent;
export type GeometryTypes = 'interval' | 'line' | 'point' | MarkComponent;

export type ChannelTypes =
| 'x'
| 'y'
| 'color'
| 'shape'
| 'series'
| 'enterType'
| 'enterEasing'
| 'enterDuration'
| 'enterDelay';
| 'enterDelay'
| 'size';

export type BaseGeometry<
T extends GeometryTypes,
Expand All @@ -43,11 +47,16 @@ export type BaseGeometry<
zIndex?: number;
};

export type IntervalGeometry = BaseGeometry<'interval'>;
export type IntervalGeometry = BaseGeometry<
'interval',
ChannelTypes | 'series'
>;

export type LineGeometry = BaseGeometry<
'line',
ChannelTypes | 'position' | `position[${number}]` | 'size'
>;

export type PointGeometry = BaseGeometry<'point'>;

export type CustomComponent = BaseGeometry<MarkComponent>;
10 changes: 10 additions & 0 deletions src/spec/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
BandOptions,
PointOptions,
TimeOptions,
LogOptions,
PowOptions,
} from '@antv/scale';
import { ScaleComponent } from '../runtime';
import { Palette } from './palette';
Expand All @@ -16,6 +18,8 @@ export type Scale =
| BandScale
| PointScale
| TimeScale
| LogScale
| PowScale
| CustomScale;

export type ScaleTypes =
Expand All @@ -25,6 +29,8 @@ export type ScaleTypes =
| 'band'
| 'point'
| 'time'
| 'log'
| 'pow'
| ScaleComponent;

export type BaseScale<T extends ScaleTypes, O> = {
Expand All @@ -48,4 +54,8 @@ export type PointScale = BaseScale<'point', PointOptions>;

export type TimeScale = BaseScale<'time', TimeOptions>;

export type LogScale = BaseScale<'log', LogOptions>;

export type PowScale = BaseScale<'pow', PowOptions>;

export type CustomScale = BaseScale<ScaleComponent, { [key: string]: any }>;
24 changes: 22 additions & 2 deletions src/stdlib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { G2Library } from '../runtime';
import { Canvas } from '../renderer';
import { Cartesian, Polar, Transpose, Parallel } from '../coordinate';
import { Constant, Field, Transform } from '../encode';
import { Interval, Line } from '../geometry';
import { Interval, Line, Point as PointGeometry } from '../geometry';
import {
MaybeTuple,
MaybeZeroX1,
Expand All @@ -11,14 +11,27 @@ import {
MaybeStackY,
MaybeSplitPosition,
MaybeKey,
MaybeSize,
MaybeZeroY1,
} from '../infer';
import { Category10, Category20 } from '../palette';
import { Linear, Ordinal, Band, Identity, Point, Time } from '../scale';
import {
Linear,
Ordinal,
Band,
Identity,
Point,
Time,
Log,
Pow,
} from '../scale';
import {
Rect as RectShape,
HollowRect,
Line as LineShape,
Smooth,
Point as PointShape,
HollowPoint,
} from '../shape';
import { DodgeX, StackY, StackEnter, SplitPosition, Key } from '../statistic';
import { Light } from '../theme';
Expand All @@ -38,13 +51,16 @@ export function createLibrary(): G2Library {
'encode.transform': Transform,
'mark.interval': Interval,
'mark.line': Line,
'mark.point': PointGeometry,
'infer.maybeTuple': MaybeTuple,
'infer.maybeZeroX1': MaybeZeroX1,
'infer.maybeZeroY2': MaybeZeroY2,
'infer.maybeSeries': MaybeSeries,
'infer.maybeStackY': MaybeStackY,
'infer.maybeSplitPosition': MaybeSplitPosition,
'infer.maybeKey': MaybeKey,
'infer.maybeSize': MaybeSize,
'infer.maybeZeroY1': MaybeZeroY1,
'palette.category10': Category10,
'palette.category20': Category20,
'scale.linear': Linear,
Expand All @@ -53,10 +69,14 @@ export function createLibrary(): G2Library {
'scale.identity': Identity,
'scale.point': Point,
'scale.time': Time,
'scale.log': Log,
'scale.pow': Pow,
'shape.rect': RectShape,
'shape.hollowRect': HollowRect,
'shape.line': LineShape,
'shape.smooth': Smooth,
'shape.point': PointShape,
'shape.hollowPoint': HollowPoint,
'statistic.stackY': StackY,
'statistic.dodgeX': DodgeX,
'statistic.stackEnter': StackEnter,
Expand Down

0 comments on commit 979954f

Please sign in to comment.