Skip to content

Commit 0a9632e

Browse files
committed
fix: 抽取统一的非法数据处理函数,修复玉玦图changedata没有更新meta
1 parent 86d5b85 commit 0a9632e

File tree

7 files changed

+41
-55
lines changed

7 files changed

+41
-55
lines changed

src/plots/pie/adaptor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { legend, animation, theme, state, annotation } from '../../adaptor/commo
44
import { getMappingFunction } from '../../adaptor/geometries/base';
55
import { interval } from '../../adaptor/geometries';
66
import { Interaction } from '../../types/interaction';
7-
import { flow, template, transformLabel, deepAssign, renderStatistic } from '../../utils';
7+
import { flow, template, transformLabel, deepAssign, renderStatistic, processIllegalData } from '../../utils';
88
import { DEFAULT_OPTIONS } from './contants';
9-
import { adaptOffset, getTotalValue, processIllegalData, isAllZero } from './utils';
9+
import { adaptOffset, getTotalValue, isAllZero } from './utils';
1010
import { PIE_STATISTIC } from './interactions';
1111
import { PieOptions } from './types';
1212

src/plots/pie/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { VIEW_LIFE_CIRCLE, Event } from '@antv/g2';
22
import { Plot } from '../../core/plot';
33
import { Adaptor } from '../../core/adaptor';
4+
import { processIllegalData } from '../../utils';
45
import { adaptor, pieAnnotation } from './adaptor';
56
import { DEFAULT_OPTIONS } from './contants';
67
import { PieOptions } from './types';
7-
import { isAllZero, processIllegalData } from './utils';
8+
import { isAllZero } from './utils';
89
import './interactions';
910

1011
export { PieOptions };

src/plots/pie/utils.ts

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { each, every, filter, isString } from '@antv/util';
2-
import { LEVEL, log } from '../../utils';
1+
import { each, every, isString } from '@antv/util';
32
import { Data } from '../../types';
3+
import { processIllegalData } from '../../utils';
44
import { PieOptions } from './types';
55

66
/**
@@ -41,23 +41,6 @@ export function adaptOffset(type: string, offset?: string | number): string | nu
4141
}
4242
}
4343

44-
/**
45-
* 处理不合法的数据(过滤 非数值型 和 NaN,保留 null)
46-
* @param data
47-
* @param angleField
48-
*/
49-
export function processIllegalData(data: PieOptions['data'], angleField: string) {
50-
const processData = filter(data, (d) => {
51-
const v = d[angleField];
52-
return v === null || (typeof v === 'number' && !isNaN(v));
53-
});
54-
55-
// 打印异常数据情况
56-
log(LEVEL.WARN, processData.length === data.length, 'illegal data existed in chart data.');
57-
58-
return processData;
59-
}
60-
6144
/**
6245
* 判断数据是否全部为 0
6346
* @param data

src/plots/radial-bar/adaptor.ts

+12-30
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
1-
import { filter } from '@antv/util';
21
import { interaction, animation, theme, scale, tooltip, legend, annotation } from '../../adaptor/common';
32
import { Params } from '../../core/adaptor';
4-
import { flow, deepAssign, findGeometry, transformLabel, log, LEVEL } from '../../utils';
3+
import { flow, deepAssign, findGeometry, transformLabel } from '../../utils';
54
import { interval, point } from '../../adaptor/geometries';
5+
import { processIllegalData } from '../../utils';
66
import { RadialBarOptions } from './types';
77
import { getScaleMax } from './utils';
8-
/**
9-
* data 处理,过滤非法数据
10-
* @param params
11-
*/
12-
function data(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
13-
const { chart, options } = params;
14-
const { data } = options;
15-
const { yField } = options;
16-
17-
const processData = filter(data, (d) => {
18-
const v = d[yField];
19-
return v === null || (typeof v === 'number' && !isNaN(v));
20-
});
21-
22-
// 打印异常数据情况
23-
log(LEVEL.WARN, processData.length === data.length, 'illegal data existed in chart data.');
24-
25-
chart.data(processData);
26-
27-
return params;
28-
}
298

309
/**
3110
* geometry 处理
3211
* @param params
3312
*/
3413
function geometry(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
3514
const { chart, options } = params;
36-
const { barStyle: style, color, tooltip, colorField, type, xField, yField } = options;
15+
const { barStyle: style, color, tooltip, colorField, type, xField, yField, data } = options;
16+
17+
// 处理不合法的数据
18+
const processData = processIllegalData(data, yField);
19+
chart.data(processData);
20+
3721
const p = deepAssign({}, params, {
3822
options: {
3923
tooltip,
@@ -64,16 +48,15 @@ function geometry(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
6448
* @param params
6549
*/
6650
export function meta(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
67-
const { options, chart } = params;
68-
const { yField, maxAngle } = options;
51+
const { options } = params;
52+
const { yField, maxAngle, data } = options;
6953

70-
// data使用chart.data()之后的,因为原始data中可能存在非法数据
71-
const { data } = chart.getOptions();
54+
const processData = processIllegalData(data, yField);
7255
return flow(
7356
scale({
7457
[yField]: {
7558
min: 0,
76-
max: getScaleMax(maxAngle, yField, data),
59+
max: getScaleMax(maxAngle, yField, processData),
7760
},
7861
})
7962
)(params);
@@ -147,7 +130,6 @@ function label(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
147130
*/
148131
export function adaptor(params: Params<RadialBarOptions>) {
149132
return flow(
150-
data,
151133
geometry,
152134
meta,
153135
axis,

src/plots/radial-bar/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class RadialBar extends Plot<RadialBarOptions> {
1919
*/
2020
public changeData(data) {
2121
this.updateOption({ data });
22-
// 更新玉珏图的scale
22+
// 更新玉珏图的 scale
2323
meta({ chart: this.chart, options: this.options });
2424
this.chart.changeData(data);
2525
}

src/utils/data.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { get, isNumber } from '@antv/util';
2-
import { Data, Datum, Meta } from '../types';
1+
import { get, isNumber, filter } from '@antv/util';
2+
import { Data, Datum, Meta, Options } from '../types';
33
import { NodeLinkData } from '../types/relation-data';
4+
import { LEVEL, log } from './invariant';
45

56
/**
67
* 查看数据是否是全负数、或者全正数
@@ -26,6 +27,7 @@ export function adjustYMetaByZero(data: Data, field: string): Meta {
2627
}
2728
return {};
2829
}
30+
2931
/**
3032
* 转换数据格式为带有节点与边的数据格式
3133
* @param data
@@ -84,3 +86,20 @@ export function transformDataToNodeLinkData(
8486
links,
8587
};
8688
}
89+
90+
/**
91+
* 处理不合法的数据(过滤 非数值型 和 NaN,保留 null)
92+
* @param data
93+
* @param angleField
94+
*/
95+
export function processIllegalData(data: Options['data'], field: string) {
96+
const processData = filter(data, (d) => {
97+
const v = d[field];
98+
return v === null || (typeof v === 'number' && !isNaN(v));
99+
});
100+
101+
// 打印异常数据情况
102+
log(LEVEL.WARN, processData.length === data.length, 'illegal data existed in chart data.');
103+
104+
return processData;
105+
}

src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export { kebabCase } from './kebab-case';
1212
export { renderStatistic, renderGaugeStatistic } from './statistic';
1313
export { measureTextWidth } from './measure-text';
1414
export { isBetween, isRealNumber } from './number';
15+
export { processIllegalData } from './data';

0 commit comments

Comments
 (0)