-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: test-live 添加 watch 参数 * feat(types): 共同类型定义 * feat(common): 抽取共同 adaptor 构建器 [x] 支持 tooltip * feat(pie-plot): 新增 饼图 [x] angleField, colorField, legend, tooltip, pieStyle, etc * fix(pie-plot): fix cr suggestions Co-authored-by: xinming <[email protected]>
- Loading branch information
Showing
10 changed files
with
313 additions
and
19 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,141 @@ | ||
import { Pie } from '../../../../src'; | ||
import { POSITIVE_NEGATIVE_DATA } from '../../../data/common'; | ||
import { createDiv } from '../../../utils/dom'; | ||
|
||
describe('pie', () => { | ||
const data = POSITIVE_NEGATIVE_DATA.filter((o) => o.value > 0).map((d, idx) => | ||
idx === 1 ? { ...d, type: 'item1' } : d | ||
); | ||
it('angleField: single color', () => { | ||
const pie = new Pie(createDiv(), { | ||
width: 400, | ||
height: 300, | ||
data, | ||
angleField: 'value', | ||
radius: 0.8, | ||
}); | ||
|
||
pie.render(); | ||
|
||
const geometry = pie.chart.geometries[0]; | ||
const elements = geometry.elements; | ||
|
||
expect(elements.length).toBe(data.length); | ||
expect(elements[0].getModel().color).toBe(elements[1].getModel().color); | ||
}); | ||
|
||
it('angleField with colorField: multiple colors', () => { | ||
const pie = new Pie(createDiv(), { | ||
width: 400, | ||
height: 300, | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
color: ['blue', 'red', 'yellow', 'lightgreen', 'lightblue', 'pink'], | ||
radius: 0.8, | ||
}); | ||
|
||
pie.render(); | ||
|
||
const geometry = pie.chart.geometries[0]; | ||
const elements = geometry.elements; | ||
// @ts-ignore | ||
expect(elements.length).toBe(data.length); | ||
// 绘图数据 | ||
expect(elements[0].getModel().style?.fill || elements[0].getModel().color).toBe('blue'); | ||
expect(elements[1].getModel().style?.fill || elements[1].getModel().color).toBe('red'); | ||
}); | ||
|
||
it('no radius', () => { | ||
const pie = new Pie(createDiv(), { | ||
width: 400, | ||
height: 300, | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
}); | ||
|
||
pie.render(); | ||
|
||
const coordinate = pie.chart.getCoordinate(); | ||
const { radius } = coordinate; | ||
const polarRadius = coordinate.getRadius(); | ||
expect(radius).toBeUndefined(); | ||
expect(polarRadius).toBeGreaterThan(0); | ||
|
||
const geometry = pie.chart.geometries[0]; | ||
const elements = geometry.elements; | ||
}); | ||
|
||
it('innerRadius', () => { | ||
const pie = new Pie(createDiv(), { | ||
width: 400, | ||
height: 300, | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
color: ['blue', 'red', 'yellow', 'lightgreen', 'lightblue', 'pink'], | ||
radius: 0.8, | ||
innerRadius: 0.5, | ||
}); | ||
|
||
pie.render(); | ||
|
||
const coordinate = pie.chart.getCoordinate(); | ||
const { innerRadius, radius } = coordinate; | ||
expect(innerRadius).toBe((radius / 0.8) * 0.5); | ||
}); | ||
|
||
it('pieStyle: custom style of pie', () => { | ||
const pie = new Pie(createDiv(), { | ||
width: 400, | ||
height: 300, | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
color: ['blue', 'red', 'yellow', 'lightgreen', 'lightblue', 'pink'], | ||
radius: 0.8, | ||
innerRadius: 0.5, | ||
pieStyle: { | ||
fill: 'red', | ||
lineWidth: 3, | ||
stroke: 'yellow', | ||
}, | ||
}); | ||
|
||
pie.render(); | ||
|
||
const geometry = pie.chart.geometries[0]; | ||
const elements = geometry.elements; | ||
expect(elements[0].getModel().style?.fill).toBe('red'); | ||
expect(elements[1].getModel().style?.fill).toBe('red'); | ||
expect(elements[1].getModel().style?.lineWidth).toBe(3); | ||
expect(elements[1].getModel().style?.stroke).toBe('yellow'); | ||
}); | ||
|
||
it('pieStyle: with callback', () => { | ||
const pie = new Pie(createDiv(), { | ||
width: 400, | ||
height: 300, | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
color: ['blue', 'red', 'yellow', 'lightgreen', 'lightblue', 'pink'], | ||
radius: 0.8, | ||
innerRadius: 0.5, | ||
pieStyle: (item) => ({ | ||
fill: item === 'item1' ? 'blue' : 'red', | ||
lineWidth: 3, | ||
stroke: 'yellow', | ||
}), | ||
}); | ||
|
||
pie.render(); | ||
|
||
const geometry = pie.chart.geometries[0]; | ||
const elements = geometry.elements; | ||
expect(elements[0].getModel().style?.fill).toBe('red'); | ||
expect(elements[1].getModel().style?.fill).toBe('blue'); | ||
expect(elements[2].getModel().style?.fill).toBe('red'); | ||
}); | ||
}); |
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,20 @@ | ||
/** | ||
* @file 通用的一些 adaptor | ||
*/ | ||
import { Params } from '../core/adaptor'; | ||
import { Options } from '../types'; | ||
|
||
/** | ||
* 通用 tooltip 配置 | ||
* @param params | ||
*/ | ||
export function tooltip<O extends Options>(params: Params<O>): Params<O> { | ||
const { chart, options } = params; | ||
const { tooltip } = options; | ||
|
||
if (tooltip) { | ||
chart.tooltip(tooltip); | ||
} | ||
|
||
return 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
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,106 @@ | ||
import { deepMix, isFunction } from '@antv/util'; | ||
import { Params } from '../../core/adaptor'; | ||
import { tooltip } from '../../common/adaptor'; | ||
import { flow } from '../../utils'; | ||
import { PieOptions } from './types'; | ||
|
||
/** | ||
* 字段 | ||
* @param params | ||
*/ | ||
function field(params: Params<PieOptions>): Params<PieOptions> { | ||
const { chart, options } = params; | ||
const { data, angleField, colorField, color } = options; | ||
|
||
// TODO 饼图数据非法处理 | ||
chart.data(data); | ||
const geometry = chart.interval().position(`1*${angleField}`).adjust({ type: 'stack' }); | ||
|
||
if (colorField) { | ||
geometry.color(colorField, color); | ||
} | ||
|
||
return params; | ||
} | ||
|
||
/** | ||
* meta 配置 | ||
* @param params | ||
*/ | ||
function meta(params: Params<PieOptions>): Params<PieOptions> { | ||
const { chart, options } = params; | ||
const { meta, colorField } = options; | ||
|
||
// meta 直接是 scale 的信息 | ||
const scales = deepMix({}, meta); | ||
chart.scale(scales, { | ||
[colorField]: { type: 'cat' }, | ||
}); | ||
|
||
return params; | ||
} | ||
|
||
/** | ||
* coord 配置 | ||
* @param params | ||
*/ | ||
function coord(params: Params<PieOptions>): Params<PieOptions> { | ||
const { chart, options } = params; | ||
const { radius, innerRadius } = options; | ||
|
||
chart.coordinate({ | ||
type: 'theta', | ||
cfg: { | ||
radius, | ||
innerRadius, | ||
}, | ||
}); | ||
|
||
return params; | ||
} | ||
|
||
/** | ||
* legend 配置 | ||
* @param params | ||
*/ | ||
function legend(params: Params<PieOptions>): Params<PieOptions> { | ||
const { chart, options } = params; | ||
const { legend, colorField } = options; | ||
|
||
if (legend && colorField) { | ||
chart.legend(colorField, legend); | ||
} | ||
|
||
return params; | ||
} | ||
|
||
/** | ||
* style 配置 | ||
* @param params | ||
*/ | ||
function style(params: Params<PieOptions>): Params<PieOptions> { | ||
const { chart, options } = params; | ||
const { pieStyle, angleField, colorField } = options; | ||
|
||
const geometry = chart.geometries[0]; | ||
if (pieStyle && geometry) { | ||
if (isFunction(pieStyle)) { | ||
// 为了兼容,colorField 放第一位 | ||
geometry.style(colorField ? `${colorField}*${angleField}` : angleField, pieStyle); | ||
} else { | ||
geometry.style(pieStyle); | ||
} | ||
} | ||
|
||
return params; | ||
} | ||
|
||
/** | ||
* 折线图适配器 | ||
* @param chart | ||
* @param options | ||
*/ | ||
export function adaptor(params: Params<PieOptions>) { | ||
// flow 的方式处理所有的配置到 G2 API | ||
flow(field, meta, coord, legend, tooltip, style)(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 { PieOptions } from './types'; | ||
import { adaptor } from './adaptor'; | ||
import { Adaptor } from '../../core/adaptor'; | ||
|
||
export { PieOptions }; | ||
|
||
export class Pie extends Plot<PieOptions> { | ||
/** 图表类型 */ | ||
public type: string = 'pie'; | ||
|
||
/** | ||
* 获取 饼图 的适配器 | ||
*/ | ||
protected getSchemaAdaptor(): Adaptor<PieOptions> { | ||
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 PieOptions extends Options { | ||
/** 角度映射字段 */ | ||
readonly angleField: string; | ||
readonly colorField?: string; | ||
readonly radius?: number; | ||
readonly innerRadius?: number; | ||
|
||
/** 饼图图形样式 */ | ||
readonly pieStyle?: ShapeStyle | ((...args: string[]) => ShapeStyle); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,10 @@ | ||
/** G shape style 配置 */ | ||
export type ShapeStyle = {}; | ||
/** G shape style 配置, 按道理应该从 G 中引入 */ | ||
export type ShapeStyle = Readonly<{ | ||
fill?: string; | ||
stroke?: string; | ||
lineWidth?: number; | ||
lineDash?: number[]; | ||
opacity?: number; | ||
fillOpacity?: number; | ||
strokeOpacity?: number; | ||
}>; |