Skip to content

Commit 3414476

Browse files
authored
Merge pull request #744 from antvis/yuxi-fix-ntv
fix: ntv can handle number string
2 parents d3e41f2 + 5b206db commit 3414476

File tree

10 files changed

+34
-21
lines changed

10 files changed

+34
-21
lines changed

packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createCompare.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React, { ReactNode } from 'react';
22

3-
import { isNumber } from 'lodash';
3+
import { ValueAssessment, EntityMetaData } from '@antv/ava';
44

55
import { ArrowDown, ArrowUp } from '../../../assets/icons';
66
import { createEntityPhraseFactory } from '../createEntityPhraseFactory';
77
import { NTV_PREFIX_CLS } from '../../../constants';
88
import { getThemeColor } from '../../../theme';
9+
import { isNumberLike } from '../../../../utils';
910

10-
import { ValueAssessment, EntityMetaData } from '@antv/ava';
1111
import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type';
1212
import type { ThemeType } from '../../../types';
1313

@@ -37,7 +37,7 @@ const defaultDeltaValueDescriptor: SpecificEntityPhraseDescriptor = {
3737
classNames: (value, { assessment }) => [`${NTV_PREFIX_CLS}-value-${assessment}`],
3838
getText: getAssessmentText,
3939
tooltip: {
40-
title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null),
40+
title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null),
4141
},
4242
};
4343

@@ -51,7 +51,7 @@ const defaultRatioValueDescriptor: SpecificEntityPhraseDescriptor = {
5151
classNames: (value, { assessment }) => [`${NTV_PREFIX_CLS}-value-${assessment}`],
5252
getText: getAssessmentText,
5353
tooltip: {
54-
title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null),
54+
title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null),
5555
},
5656
};
5757

packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createContributeRatio.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { isNumber } from 'lodash';
2-
31
import { createEntityPhraseFactory } from '../createEntityPhraseFactory';
42
import { getThemeColor } from '../../../theme';
3+
import { isNumberLike } from '../../../../utils';
54

65
import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type';
76

@@ -10,7 +9,7 @@ const defaultContributeRatioDescriptor: SpecificEntityPhraseDescriptor = {
109
color: (value, metadata, { theme }) => getThemeColor('colorConclusion', theme),
1110
},
1211
tooltip: {
13-
title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null),
12+
title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null),
1413
},
1514
};
1615

packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createMetricValue.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { isNumber } from 'lodash';
2-
31
import { createEntityPhraseFactory } from '../createEntityPhraseFactory';
42
import { getThemeColor } from '../../../theme';
3+
import { isNumberLike } from '../../../../utils';
54

65
import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type';
76

@@ -10,7 +9,7 @@ const defaultMetricValueDescriptor: SpecificEntityPhraseDescriptor = {
109
color: (value, metadata, { theme }) => getThemeColor('colorMetricValue', theme),
1110
},
1211
tooltip: {
13-
title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null),
12+
title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null),
1413
},
1514
};
1615

packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createOtherMetricValue.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { isNumber } from 'lodash';
2-
31
import { createEntityPhraseFactory } from '../createEntityPhraseFactory';
42
import { getThemeColor } from '../../../theme';
3+
import { isNumberLike } from '../../../../utils';
54

65
import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type';
76

@@ -10,7 +9,7 @@ const defaultOtherMetricValueDescriptor: SpecificEntityPhraseDescriptor = {
109
color: (value, metadata, { theme }) => getThemeColor('colorOtherValue', theme),
1110
},
1211
tooltip: {
13-
title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null),
12+
title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null),
1413
},
1514
};
1615

packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createProportion.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
22

3-
import { isNumber, isNaN } from 'lodash';
3+
import { isNaN } from 'lodash';
44

55
import { ProportionChart } from '../../../line-charts';
66
import { createEntityPhraseFactory } from '../createEntityPhraseFactory';
7+
import { isNumberLike } from '../../../../utils';
78

89
import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type';
910

@@ -23,7 +24,7 @@ const defaultProportionDescriptor: SpecificEntityPhraseDescriptor = {
2324
),
2425
},
2526
tooltip: {
26-
title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null),
27+
title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null),
2728
},
2829
};
2930

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { getPrefixCls } from './getPrefixCls';
22
export { classnames } from './classnames';
3+
export * from './isType';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { toNumber, isNaN } from 'lodash';
2+
3+
export function isNumberLike(val: unknown) {
4+
const numVal = toNumber(val);
5+
return !isNaN(numVal);
6+
}

packages/ava/src/ntv/generate/generateTextSpec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-console */
2-
import { toString, isNumber } from 'lodash';
2+
import { toString } from 'lodash';
33

4+
import { isNumberLike } from '../../utils';
45
import { isEntityType } from '../utils';
56

67
import {
@@ -63,7 +64,7 @@ class TextSpecGenerator {
6364
value: formattedValue,
6465
metadata: {
6566
entityType: varType,
66-
origin: isNumber(value) ? value : undefined,
67+
origin: isNumberLike(value) ? value : undefined,
6768
assessment: getAssessment(varType, value),
6869
generateVariableInfo: { scopeVariable, path },
6970
},

packages/ava/src/ntv/generate/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { get, isArray, isUndefined, slice, isNumber, replace, isString, startsWith } from 'lodash';
1+
import { get, isArray, isUndefined, slice, replace, isString, startsWith } from 'lodash';
22

3-
import { dataFormat } from '../../utils';
3+
import { dataFormat, isNumberLike } from '../../utils';
44

55
import type { Variable, VariableMeta } from './types';
66
import type { EntityType } from '../schema';
@@ -58,7 +58,7 @@ export function getScopeVariableArray(globalVar: Variable, scopeVar: Variable, p
5858
}
5959

6060
export function getAssessment(entityType: EntityType, value: any) {
61-
if (!isNumber(value)) return undefined;
61+
if (!isNumberLike(value)) return undefined;
6262
if (entityType === 'delta_value' || entityType === 'ratio_value') {
6363
if (value > 0) return 'positive';
6464
if (value < 0) return 'negative';
@@ -67,7 +67,7 @@ export function getAssessment(entityType: EntityType, value: any) {
6767
}
6868

6969
export function getFormattedNumberValue(varType: string, value: any, formatter?: VariableMeta['formatter']) {
70-
if (!isNumber(value)) return value;
70+
if (!isNumberLike(value)) return value;
7171
if (varType === 'delta_value' || varType === 'ratio_value') {
7272
return formatter ? formatter(Math.abs(value)) : dataFormat(Math.abs(value));
7373
}

packages/ava/src/utils/isType.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { toNumber, isNaN } from 'lodash';
2+
13
export function isString(val: unknown): val is string {
24
return typeof val === 'string';
35
}
@@ -9,3 +11,8 @@ export function isObject(val: unknown) {
911
export function isUndefined(val: unknown) {
1012
return val === undefined;
1113
}
14+
15+
export function isNumberLike(val: unknown) {
16+
const numVal = toNumber(val);
17+
return !isNaN(numVal);
18+
}

0 commit comments

Comments
 (0)