-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add tinyLine * feat: add tiny-line * feat: add tinyLine * feat: add tiny-line * feat: add tiny-line * feat: add tiny-line * feat: add tiny-line Co-authored-by: yinshi.wyl <yinshi.wyl@antfin.com>
- Loading branch information
Showing
6 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |