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: add tiny-line #1304

Merged
merged 8 commits into from
Jul 20, 2020
Merged
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
82 changes: 82 additions & 0 deletions __tests__/unit/plots/tiny-line/index-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { TinyLine } from '../../../../src';
import { partySupport } from '../../../data/party-support';
import { createDiv } from '../../../utils/dom';

describe('tiny-line', () => {
it('data', () => {
const tinyLine = new TinyLine(createDiv(), {
width: 80,
height: 40,
meta: {
value: {
min: 0,
max: 5000,
},
},
data: partySupport
.filter((o) => o.type === 'FF')
.map((item) => {
return item.value;
}),
autoFit: false,
});

tinyLine.render();
expect(tinyLine.chart.geometries[0].elements.length).toBe(1);
});

it('data with smooth', () => {
const tinyLine = new TinyLine(createDiv(), {
width: 80,
height: 40,
meta: {
value: {
min: 0,
max: 5000,
},
},
data: partySupport
.filter((o) => o.type === 'FF')
.map((item) => {
return item.value;
}),
autoFit: false,
smooth: true,
});

tinyLine.render();
expect(tinyLine.chart.geometries[0].attributes.shape.values).toEqual(['smooth']);
expect(tinyLine.chart.geometries[0].elements.length).toBe(1);
});

it('data with style', () => {
const tinyLine = new TinyLine(createDiv(), {
width: 80,
height: 40,
data: partySupport
.filter((o) => o.type === 'FF')
.map((item) => {
return item.value;
}),
lineStyle: {
lineDash: [2, 2],
},
autoFit: false,
appendPadding: 10,
});

tinyLine.render();

const geometry = tinyLine.chart.geometries[0];
const elements = geometry.elements;
expect(elements[0].shape.attr('lineDash')).toEqual([2, 2]);

tinyLine.update({
...tinyLine.options,
lineStyle: () => {
return { lineDash: [4, 4] };
},
});
expect(tinyLine.chart.geometries[0].elements[0].shape.attr('lineDash')).toEqual([4, 4]);
});
});
5 changes: 2 additions & 3 deletions src/core/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Chart } from '@antv/g2';
import { Options } from '../types';

/**
* adaptor flow 的参数
*/
export type Params<O extends Options> = {
export type Params<O> = {
readonly chart: Chart;
readonly options: O;
};
@@ -13,4 +12,4 @@ export type Params<O extends Options> = {
* schema 转 G2 的适配器基类
* 使用 纯函数的方式,这里只是类型定义
*/
export type Adaptor<O extends Options> = (params: Params<O>) => void;
export type Adaptor<O> = (params: Params<O>) => void;
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -10,3 +10,5 @@ export { Pie, PieOptions } from './plots/pie';

//散点图及类型定义
export { Scatter, ScatterOptions } from './plots/scatter';

export { TinyLine, TinyLineOptions } from './plots/tiny-line';
116 changes: 116 additions & 0 deletions src/plots/tiny-line/adaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Geometry } from '@antv/g2';
import { Params } from '../../core/adaptor';
import { flow } from '../../utils';
import { TinyLineOptions } from './types';
import { isFunction } from '@antv/util';

/**
* 字段
* @param params
*/
function field(params: Params<TinyLineOptions>): Params<TinyLineOptions> {
const { chart, options } = params;
const { data, connectNulls } = options;

const seriesData = data.map((y: number, x: number) => {
return { x, y };
});

chart.data(seriesData);

chart.line({ connectNulls }).position('x*y');

return params;
}

/**
* meta 配置
* @param params
*/
function meta(params: Params<TinyLineOptions>): Params<TinyLineOptions> {
const { chart, options } = params;
const { meta } = options;

chart.scale(meta);

return params;
}

/**
* axis 配置
* @param params
*/
function axis(params: Params<TinyLineOptions>): Params<TinyLineOptions> {
const { chart } = params;

chart.axis(false);

return params;
}

/**
* legend 配置
* @param params
*/
function legend(params: Params<TinyLineOptions>): Params<TinyLineOptions> {
const { chart } = params;

chart.legend(false);

return params;
}

/**
* tooltip 配置
* @param params
*/
function tooltip(params: Params<TinyLineOptions>): Params<TinyLineOptions> {
const { chart } = params;

chart.tooltip(false);

return params;
}

/**
* 样式
* @param params
*/
function style(params: Params<TinyLineOptions>): Params<TinyLineOptions> {
const { chart, options } = params;
const { lineStyle } = options;

const geometry = chart.geometries[0];
if (lineStyle && geometry) {
if (isFunction(lineStyle)) {
geometry.style('x*y', lineStyle);
} else {
geometry.style(lineStyle);
}
}
return params;
}

/**
* shape 的配置处理
* @param params
*/
function shape(params: Params<TinyLineOptions>): Params<TinyLineOptions> {
const { chart, options } = params;
const { smooth } = options;

const lineGeometry = chart.geometries.find((g: Geometry) => g.type === 'line');

lineGeometry.shape(smooth ? 'smooth' : 'line');
return params;
}

/**
* 迷你折线图适配器
* @param chart
* @param options
*/
export function adaptor(params: Params<TinyLineOptions>) {
// flow 的方式处理所有的配置到 G2 API
flow(field, meta, axis, legend, tooltip, style, shape)(params);
}
18 changes: 18 additions & 0 deletions src/plots/tiny-line/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Plot } from '../../core/plot';
import { TinyLineOptions } from './types';
import { adaptor } from './adaptor';
import { Adaptor } from '../../core/adaptor';

export { TinyLineOptions };

export class TinyLine extends Plot<TinyLineOptions> {
/** 图表类型 */
public type: string = 'tiny-line';

/**
* 获取 迷你折线图 的适配器
*/
protected getSchemaAdaptor(): Adaptor<TinyLineOptions> {
return adaptor;
}
}
13 changes: 13 additions & 0 deletions src/plots/tiny-line/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Options } from '../../types';
import { ShapeStyle } from '../../types/style';

export interface TinyLineOptions extends Options {
/** 具体的数据 */
data: any[];
/** 是否平滑 */
smooth?: boolean;
/** 是否连接空数据 */
connectNulls?: boolean;
/** 折线extra图形样式 */
lineStyle?: ShapeStyle | ((x?: number, y?: number) => ShapeStyle);
}