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

feat(annotation): add support for mean / median #2922

Merged
merged 3 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 6 additions & 43 deletions src/chart/controller/annotation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { contains, deepMix, each, get, isArray, isFunction, isNil, isString, keys, upperFirst } from '@antv/util';

import { Annotation as AnnotationComponent, IElement, IGroup, Scale } from '../../dependents';
import { Annotation as AnnotationComponent, IElement, IGroup } from '../../dependents';
import {
AnnotationBaseOption as BaseOption,
AnnotationPosition as Position,
Expand All @@ -25,6 +24,7 @@ import Geometry from '../../geometry/base';
import Element from '../../geometry/element';
import { getAngleByPoint, getDistanceToCenter } from '../../util/coordinate';
import { omit } from '../../util/helper';
import { getNormalizedValue } from '../../util/annotation';
import View from '../view';
import { Controller } from './base';

Expand Down Expand Up @@ -394,17 +394,17 @@ export default class Annotation extends Controller<BaseOption[]> {
return this.parsePercentPosition(position as [string, string]);
}

x = this.getNormalizedValue(xPos, xScale);
y = this.getNormalizedValue(yPos, Object.values(yScales)[0]);
x = getNormalizedValue(xPos, xScale);
y = getNormalizedValue(yPos, Object.values(yScales)[0]);
} else if (!isNil(position)) {
// 入参是 object 结构,数据点
for (const key of keys(position)) {
const value = position[key];
if (key === xScale.field) {
x = this.getNormalizedValue(value, xScale);
x = getNormalizedValue(value, xScale);
}
if (yScales[key]) {
y = this.getNormalizedValue(value, yScales[key]);
y = getNormalizedValue(value, yScales[key]);
}
}
}
Expand Down Expand Up @@ -447,43 +447,6 @@ export default class Annotation extends Controller<BaseOption[]> {
return arr;
}

/**
* parse the value position
* @param val
* @param scale
*/
private getNormalizedValue(val: number | string, scale: Scale) {
let result: number;
let scaled: number;

switch (val) {
case 'start':
result = 0;
break;
case 'end':
result = 1;
break;
case 'median': {
scaled = scale.isCategory ? (scale.values.length - 1) / 2 : (scale.min + scale.max) / 2;
result = scale.scale(scaled);
break;
}
case 'min':
case 'max':
if (scale.isCategory) {
scaled = val === 'min' ? 0 : scale.values.length - 1;
} else {
scaled = scale[val];
}
result = scale.scale(scaled);
break;
default:
result = scale.scale(val);
}

return result;
}

/**
* parse percent position
* @param position
Expand Down
37 changes: 37 additions & 0 deletions src/util/annotation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getMedian, getMean } from './stat';
import { Scale } from '../dependents';

/**
* parse the value position
* @param val
* @param scale
*/
export function getNormalizedValue(val: number | string, scale: Scale) {
let scaled: number;

switch (val) {
case 'start':
return 0;
case 'end':
return 1;
case 'median': {
scaled = scale.isCategory ? getMedian(scale.values.map((_, idx: number) => idx)) : (scale.min + scale.max) / 2;
break;
}
case 'mean': {
scaled = scale.isCategory ? (scale.values.length - 1) / 2 : getMean(scale.values);
break;
}
case 'min':
scaled = scale.isCategory ? 0 : scale[val];
break;
case 'max':
scaled = scale.isCategory ? scale.values.length - 1 : scale[val];
break;
default:
scaled = val as number;
break;
}

return scale.scale(scaled);
}
41 changes: 41 additions & 0 deletions src/util/stat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { reduce, isNumber } from '@antv/util';

/**
* 获得中位数
* @param array
*/
export function getMedian(array: number[]) {
const arr = [...array];
// 先排序
arr.sort((a: number, b: number) => {
return a - b;
});

const len = arr.length;

// median
// 0
if (len === 0) {
return 0
}

// 奇数
if (len % 2 === 1) {
return arr[(len - 1) / 2];
}

// 偶数
return (arr[len / 2] + arr[len / 2 - 1]) / 2;
}

/**
* 获得平均值
* @param array
*/
export function getMean(array: number[]) {
const sum = reduce(array, (r: number, num: number) => {
return r += (isNaN(num) || !isNumber(num) ? 0 : num);
}, 0);

return array.length === 0 ? 0 : sum / array.length;
}
38 changes: 38 additions & 0 deletions tests/unit/util/annotation-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getScale } from '../../../src/dependents';
import { getNormalizedValue } from '../../../src/util/annotation';
import { near } from '../../util/math';

const Linear = getScale('linear');
const Category = getScale('cat');

describe('util annotation', () => {
it('getNormalizedValue cat', () => {
const scale = new Category({
values: ['东北', '华北', '华东', '华中', '华南', '西部'],
});

expect(getNormalizedValue('start', scale)).toEqual(0);
expect(getNormalizedValue('end', scale)).toEqual(1);
expect(getNormalizedValue('mean', scale)).toEqual(0.5);
expect(getNormalizedValue('median', scale)).toEqual(0.5);
expect(getNormalizedValue('min', scale)).toEqual(0);
expect(getNormalizedValue('max', scale)).toEqual(1);
expect(getNormalizedValue('华东', scale)).toEqual(0.4);
});

it('getNormalizedValue linear', () => {
const scale = new Linear({
min: 0,
max: 100,
values: [0, 1, 2, 3, 10, 10, 6, 7, 8, 9],
});

expect(getNormalizedValue('start', scale)).toEqual(0);
expect(getNormalizedValue('end', scale)).toEqual(1);
expect(near(getNormalizedValue('mean', scale), 0.055999999999999994)).toBe(true);
expect(getNormalizedValue('median', scale)).toEqual(0.5);
expect(getNormalizedValue('min', scale)).toEqual(0);
expect(getNormalizedValue('max', scale)).toEqual(1);
expect(getNormalizedValue(2, scale)).toEqual(0.02);
});
});
19 changes: 19 additions & 0 deletions tests/unit/util/stat-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getMean, getMedian } from '../../../src/util/stat';

describe('util stat', () => {
it('getMean', () => {
expect(getMean([])).toEqual(0);
expect(getMean([1])).toEqual(1);
expect(getMean([1, 2, 3, 4])).toEqual(2.5);
expect(getMean([1, 2, NaN, 3, 4])).toEqual(2);
expect(getMean([1, 2, null, 3, 4])).toEqual(2);
expect(getMean([1, 2, undefined, 3, 4])).toEqual(2);
});

it('getMedian', () => {
expect(getMedian([])).toEqual(0);
expect(getMedian([1])).toEqual(1);
expect(getMedian([1, 2, 3, 4])).toEqual(2.5);
expect(getMedian([1, 2, 3, 4, 5])).toEqual(3);
});
});