Skip to content

Commit 99075c8

Browse files
author
xinming
committed
fix(pie-plot): fix cr suggestions
1 parent 2448da4 commit 99075c8

File tree

6 files changed

+47
-27
lines changed

6 files changed

+47
-27
lines changed

__tests__/unit/plots/pie/index-spec.ts

+33-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { POSITIVE_NEGATIVE_DATA } from '../../../data/common';
33
import { createDiv } from '../../../utils/dom';
44

55
describe('pie', () => {
6-
const data = POSITIVE_NEGATIVE_DATA.filter((o) => o.value > 0).map((d, idx) => idx === 1 ? { ...d, type: 'item1' } : d);
6+
const data = POSITIVE_NEGATIVE_DATA.filter((o) => o.value > 0).map((d, idx) =>
7+
idx === 1 ? { ...d, type: 'item1' } : d
8+
);
79
it('angleField: single color', () => {
810
const pie = new Pie(createDiv(), {
911
width: 400,
@@ -40,8 +42,29 @@ describe('pie', () => {
4042
// @ts-ignore
4143
expect(elements.length).toBe(data.length);
4244
// 绘图数据
43-
expect(elements[0].getModel().style.fill || elements[0].getModel().color).toBe('blue');
44-
expect(elements[1].getModel().style.fill || elements[1].getModel().color).toBe('red');
45+
expect(elements[0].getModel().style?.fill || elements[0].getModel().color).toBe('blue');
46+
expect(elements[1].getModel().style?.fill || elements[1].getModel().color).toBe('red');
47+
});
48+
49+
it('no radius', () => {
50+
const pie = new Pie(createDiv(), {
51+
width: 400,
52+
height: 300,
53+
data,
54+
angleField: 'value',
55+
colorField: 'type',
56+
});
57+
58+
pie.render();
59+
60+
const coordinate = pie.chart.getCoordinate();
61+
const { radius } = coordinate;
62+
const polarRadius = coordinate.getRadius();
63+
expect(radius).toBeUndefined();
64+
expect(polarRadius).toBeGreaterThan(0);
65+
66+
const geometry = pie.chart.geometries[0];
67+
const elements = geometry.elements;
4568
});
4669

4770
it('innerRadius', () => {
@@ -84,10 +107,10 @@ describe('pie', () => {
84107

85108
const geometry = pie.chart.geometries[0];
86109
const elements = geometry.elements;
87-
expect(elements[0].getModel().style.fill).toBe('red');
88-
expect(elements[1].getModel().style.fill).toBe('red');
89-
expect(elements[1].getModel().style.lineWidth).toBe(3);
90-
expect(elements[1].getModel().style.stroke).toBe('yellow');
110+
expect(elements[0].getModel().style?.fill).toBe('red');
111+
expect(elements[1].getModel().style?.fill).toBe('red');
112+
expect(elements[1].getModel().style?.lineWidth).toBe(3);
113+
expect(elements[1].getModel().style?.stroke).toBe('yellow');
91114
});
92115

93116
it('pieStyle: with callback', () => {
@@ -111,8 +134,8 @@ describe('pie', () => {
111134

112135
const geometry = pie.chart.geometries[0];
113136
const elements = geometry.elements;
114-
expect(elements[0].getModel().style.fill).toBe('red');
115-
expect(elements[1].getModel().style.fill).toBe('blue');
116-
expect(elements[2].getModel().style.fill).toBe('red');
137+
expect(elements[0].getModel().style?.fill).toBe('red');
138+
expect(elements[1].getModel().style?.fill).toBe('blue');
139+
expect(elements[2].getModel().style?.fill).toBe('red');
117140
});
118141
});

src/common/adaptor.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Options } from '../types';
88
* 通用 tooltip 配置
99
* @param params
1010
*/
11-
function tooltip<O extends Options>(params: Params<O>): Params<O> {
11+
export function tooltip<O extends Options>(params: Params<O>): Params<O> {
1212
const { chart, options } = params;
1313
const { tooltip } = options;
1414

@@ -18,6 +18,3 @@ function tooltip<O extends Options>(params: Params<O>): Params<O> {
1818

1919
return params;
2020
}
21-
22-
23-
export { tooltip };

src/core/adaptor.ts

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { Options } from '../types';
77
export type Params<O extends Options> = {
88
readonly chart: Chart;
99
readonly options: O;
10-
readonly geometry?: Geometry;
1110
};
1211

1312
/**

src/plots/pie/adaptor.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function field(params: Params<PieOptions>): Params<PieOptions> {
2020
geometry.color(colorField, color);
2121
}
2222

23-
return { ...params, geometry };
23+
return params;
2424
}
2525

2626
/**
@@ -46,7 +46,7 @@ function meta(params: Params<PieOptions>): Params<PieOptions> {
4646
*/
4747
function coord(params: Params<PieOptions>): Params<PieOptions> {
4848
const { chart, options } = params;
49-
const { radius, innerRadius = 0 } = options;
49+
const { radius, innerRadius } = options;
5050

5151
chart.coordinate({
5252
type: 'theta',
@@ -79,10 +79,11 @@ function legend(params: Params<PieOptions>): Params<PieOptions> {
7979
* @param params
8080
*/
8181
function style(params: Params<PieOptions>): Params<PieOptions> {
82-
const { options, geometry } = params;
83-
const { pieStyle = {}, angleField, colorField } = options;
82+
const { chart, options } = params;
83+
const { pieStyle, angleField, colorField } = options;
8484

85-
if (geometry) {
85+
const geometry = chart.geometries[0];
86+
if (pieStyle && geometry) {
8687
if (isFunction(pieStyle)) {
8788
// 为了兼容,colorField 放第一位
8889
geometry.style(colorField ? `${colorField}*${angleField}` : angleField, pieStyle);

src/plots/pie/types.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { ShapeStyle } from '../../types/style';
33

44
export interface PieOptions extends Options {
55
/** 角度映射字段 */
6-
angleField: string;
7-
colorField?: string;
8-
radius: number;
9-
innerRadius?: number;
6+
readonly angleField: string;
7+
readonly colorField?: string;
8+
readonly radius?: number;
9+
readonly innerRadius?: number;
1010

1111
/** 饼图图形样式 */
12-
pieStyle?: ShapeStyle | ((...args: string[]) => ShapeStyle);
12+
readonly pieStyle?: ShapeStyle | ((...args: string[]) => ShapeStyle);
1313
}

src/types/style.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/** G shape style 配置, 按道理应该从 G 中引入 */
2-
export type ShapeStyle = {
2+
export type ShapeStyle = Readonly<{
33
fill?: string;
44
stroke?: string;
55
lineWidth?: number;
66
lineDash?: number[];
77
opacity?: number;
88
fillOpacity?: number;
99
strokeOpacity?: number;
10-
};
10+
}>;

0 commit comments

Comments
 (0)