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

fix(radial-bar): 修复玉钰图存在非法数据崩溃问题 (#2394) #2447

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 38 additions & 0 deletions __tests__/bugs/issue-2394-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { RadialBar } from '../../src';
import { createDiv } from '../utils/dom';

describe('#2394', () => {
it('filter illegal data', () => {
visiky marked this conversation as resolved.
Show resolved Hide resolved
const data = [
{ name: 'X6', star: '297a' },
{ name: 'G', star: 506 },
{ name: 'AVA', star: 805 },
{ name: 'G2Plot', star: 1478 },
{ name: 'L7', star: 2029 },
{ name: 'G6', star: 7100 },
{ name: 'F2', star: 7346 },
{ name: 'G2', star: 10178 },
];

const radialBar = new RadialBar(createDiv(), {
data,
xField: 'name',
yField: 'star',
// maxAngle: 90, //最大旋转角度,
radius: 0.8,
innerRadius: 0.2,
tooltip: {
formatter: (datum) => {
return { name: 'star数', value: datum.star };
},
},
});

radialBar.render();

expect(radialBar.chart.geometries[0].elements.length).toBe(7);
expect(radialBar.chart.geometries[0].data.length).toBe(7);

radialBar.destroy();
});
});
34 changes: 29 additions & 5 deletions src/plots/radial-bar/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import { filter } from '@antv/util';
import { interaction, animation, theme, scale, tooltip, legend, annotation } from '../../adaptor/common';
import { Params } from '../../core/adaptor';
import { flow, deepAssign, findGeometry, transformLabel } from '../../utils';
import { flow, deepAssign, findGeometry, transformLabel, log, LEVEL } from '../../utils';
import { interval, point } from '../../adaptor/geometries';
import { RadialBarOptions } from './types';
import { getScaleMax } from './utils';
/**
* data 处理,过滤非法数据
* @param params
*/
function data(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
const { chart, options } = params;
const { data } = options;
const { yField } = options;
const processData = filter(data, (d) => {
const v = d[yField];
return typeof v === 'number' && !isNaN(v);
visiky marked this conversation as resolved.
Show resolved Hide resolved
});

// 打印异常数据情况
log(LEVEL.WARN, processData.length === data.length, 'illegal data existed in chart data.');

chart.data(processData);

return params;
}

/**
* geometry 处理
* @param params
*/
function geometry(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
const { chart, options } = params;
const { data, barStyle: style, color, tooltip, colorField, type, xField, yField } = options;
chart.data(data);
const { barStyle: style, color, tooltip, colorField, type, xField, yField } = options;
const p = deepAssign({}, params, {
options: {
tooltip,
Expand Down Expand Up @@ -43,8 +63,11 @@ function geometry(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
* @param params
*/
export function meta(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
const { options } = params;
const { yField, data, maxAngle } = options;
const { options, chart } = params;
const { yField, maxAngle } = options;

// data使用chart.data()之后的,因为原始data中可能存在非法数据
const { data } = chart.getOptions();
return flow(
scale({
[yField]: {
Expand Down Expand Up @@ -123,6 +146,7 @@ function label(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
*/
export function adaptor(params: Params<RadialBarOptions>) {
return flow(
data,
geometry,
meta,
axis,
Expand Down