From 4a12718a7f4204e62a9bf29444d084fff5e14a3c Mon Sep 17 00:00:00 2001 From: bbsqq <1491812683@qq.com> Date: Fri, 3 Nov 2023 16:05:42 +0800 Subject: [PATCH 1/2] fix(ava/ntv): generateTextSpec can handle number string --- packages/ava/src/ntv/generate/generateTextSpec.ts | 5 +++-- packages/ava/src/ntv/generate/utils.ts | 8 ++++---- packages/ava/src/utils/isType.ts | 7 +++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/ava/src/ntv/generate/generateTextSpec.ts b/packages/ava/src/ntv/generate/generateTextSpec.ts index 39dfcb747..00717a25b 100644 --- a/packages/ava/src/ntv/generate/generateTextSpec.ts +++ b/packages/ava/src/ntv/generate/generateTextSpec.ts @@ -1,6 +1,7 @@ /* eslint-disable no-console */ -import { toString, isNumber } from 'lodash'; +import { toString } from 'lodash'; +import { isNumberLike } from '../../utils'; import { isEntityType } from '../utils'; import { @@ -63,7 +64,7 @@ class TextSpecGenerator { value: formattedValue, metadata: { entityType: varType, - origin: isNumber(value) ? value : undefined, + origin: isNumberLike(value) ? value : undefined, assessment: getAssessment(varType, value), generateVariableInfo: { scopeVariable, path }, }, diff --git a/packages/ava/src/ntv/generate/utils.ts b/packages/ava/src/ntv/generate/utils.ts index 51b208065..ea57c70c6 100644 --- a/packages/ava/src/ntv/generate/utils.ts +++ b/packages/ava/src/ntv/generate/utils.ts @@ -1,6 +1,6 @@ -import { get, isArray, isUndefined, slice, isNumber, replace, isString, startsWith } from 'lodash'; +import { get, isArray, isUndefined, slice, replace, isString, startsWith } from 'lodash'; -import { dataFormat } from '../../utils'; +import { dataFormat, isNumberLike } from '../../utils'; import type { Variable, VariableMeta } from './types'; import type { EntityType } from '../schema'; @@ -58,7 +58,7 @@ export function getScopeVariableArray(globalVar: Variable, scopeVar: Variable, p } export function getAssessment(entityType: EntityType, value: any) { - if (!isNumber(value)) return undefined; + if (!isNumberLike(value)) return undefined; if (entityType === 'delta_value' || entityType === 'ratio_value') { if (value > 0) return 'positive'; if (value < 0) return 'negative'; @@ -67,7 +67,7 @@ export function getAssessment(entityType: EntityType, value: any) { } export function getFormattedNumberValue(varType: string, value: any, formatter?: VariableMeta['formatter']) { - if (!isNumber(value)) return value; + if (!isNumberLike(value)) return value; if (varType === 'delta_value' || varType === 'ratio_value') { return formatter ? formatter(Math.abs(value)) : dataFormat(Math.abs(value)); } diff --git a/packages/ava/src/utils/isType.ts b/packages/ava/src/utils/isType.ts index a173d73c8..564cb6085 100644 --- a/packages/ava/src/utils/isType.ts +++ b/packages/ava/src/utils/isType.ts @@ -1,3 +1,5 @@ +import { toNumber, isNaN } from 'lodash'; + export function isString(val: unknown): val is string { return typeof val === 'string'; } @@ -9,3 +11,8 @@ export function isObject(val: unknown) { export function isUndefined(val: unknown) { return val === undefined; } + +export function isNumberLike(val: unknown) { + const numVal = toNumber(val); + return !isNaN(numVal); +} From 5b206db2c1991c9b2254f1be4a1b5e2c2f370c75 Mon Sep 17 00:00:00 2001 From: bbsqq <1491812683@qq.com> Date: Fri, 3 Nov 2023 16:08:47 +0800 Subject: [PATCH 2/2] fix(ava-react/ntv): ntv phrase.origin tooltip can handle number string --- .../chore/plugin/presets/createCompare.tsx | 8 ++++---- .../chore/plugin/presets/createContributeRatio.tsx | 5 ++--- .../chore/plugin/presets/createMetricValue.tsx | 5 ++--- .../chore/plugin/presets/createOtherMetricValue.tsx | 5 ++--- .../chore/plugin/presets/createProportion.tsx | 5 +++-- packages/ava-react/src/utils/index.ts | 1 + packages/ava-react/src/utils/isType.ts | 6 ++++++ 7 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 packages/ava-react/src/utils/isType.ts diff --git a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createCompare.tsx b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createCompare.tsx index 6f20880e8..1150429da 100644 --- a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createCompare.tsx +++ b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createCompare.tsx @@ -1,13 +1,13 @@ import React, { ReactNode } from 'react'; -import { isNumber } from 'lodash'; +import { ValueAssessment, EntityMetaData } from '@antv/ava'; import { ArrowDown, ArrowUp } from '../../../assets/icons'; import { createEntityPhraseFactory } from '../createEntityPhraseFactory'; import { NTV_PREFIX_CLS } from '../../../constants'; import { getThemeColor } from '../../../theme'; +import { isNumberLike } from '../../../../utils'; -import { ValueAssessment, EntityMetaData } from '@antv/ava'; import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type'; import type { ThemeType } from '../../../types'; @@ -37,7 +37,7 @@ const defaultDeltaValueDescriptor: SpecificEntityPhraseDescriptor = { classNames: (value, { assessment }) => [`${NTV_PREFIX_CLS}-value-${assessment}`], getText: getAssessmentText, tooltip: { - title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null), + title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null), }, }; @@ -51,7 +51,7 @@ const defaultRatioValueDescriptor: SpecificEntityPhraseDescriptor = { classNames: (value, { assessment }) => [`${NTV_PREFIX_CLS}-value-${assessment}`], getText: getAssessmentText, tooltip: { - title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null), + title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null), }, }; diff --git a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createContributeRatio.tsx b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createContributeRatio.tsx index ce74d3913..54c793f88 100644 --- a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createContributeRatio.tsx +++ b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createContributeRatio.tsx @@ -1,7 +1,6 @@ -import { isNumber } from 'lodash'; - import { createEntityPhraseFactory } from '../createEntityPhraseFactory'; import { getThemeColor } from '../../../theme'; +import { isNumberLike } from '../../../../utils'; import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type'; @@ -10,7 +9,7 @@ const defaultContributeRatioDescriptor: SpecificEntityPhraseDescriptor = { color: (value, metadata, { theme }) => getThemeColor('colorConclusion', theme), }, tooltip: { - title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null), + title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null), }, }; diff --git a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createMetricValue.tsx b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createMetricValue.tsx index 946af529b..a4b1b1766 100644 --- a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createMetricValue.tsx +++ b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createMetricValue.tsx @@ -1,7 +1,6 @@ -import { isNumber } from 'lodash'; - import { createEntityPhraseFactory } from '../createEntityPhraseFactory'; import { getThemeColor } from '../../../theme'; +import { isNumberLike } from '../../../../utils'; import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type'; @@ -10,7 +9,7 @@ const defaultMetricValueDescriptor: SpecificEntityPhraseDescriptor = { color: (value, metadata, { theme }) => getThemeColor('colorMetricValue', theme), }, tooltip: { - title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null), + title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null), }, }; diff --git a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createOtherMetricValue.tsx b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createOtherMetricValue.tsx index 446935aaf..88d44e77e 100644 --- a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createOtherMetricValue.tsx +++ b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createOtherMetricValue.tsx @@ -1,7 +1,6 @@ -import { isNumber } from 'lodash'; - import { createEntityPhraseFactory } from '../createEntityPhraseFactory'; import { getThemeColor } from '../../../theme'; +import { isNumberLike } from '../../../../utils'; import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type'; @@ -10,7 +9,7 @@ const defaultOtherMetricValueDescriptor: SpecificEntityPhraseDescriptor = { color: (value, metadata, { theme }) => getThemeColor('colorOtherValue', theme), }, tooltip: { - title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null), + title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null), }, }; diff --git a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createProportion.tsx b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createProportion.tsx index 5dcce83da..a1b8b98a3 100644 --- a/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createProportion.tsx +++ b/packages/ava-react/src/NarrativeTextVis/chore/plugin/presets/createProportion.tsx @@ -1,9 +1,10 @@ import React from 'react'; -import { isNumber, isNaN } from 'lodash'; +import { isNaN } from 'lodash'; import { ProportionChart } from '../../../line-charts'; import { createEntityPhraseFactory } from '../createEntityPhraseFactory'; +import { isNumberLike } from '../../../../utils'; import type { SpecificEntityPhraseDescriptor } from '../plugin-protocol.type'; @@ -23,7 +24,7 @@ const defaultProportionDescriptor: SpecificEntityPhraseDescriptor = { ), }, tooltip: { - title: (value, metadata) => (isNumber(metadata.origin) ? `${metadata.origin}` : null), + title: (value, metadata) => (isNumberLike(metadata.origin) ? `${metadata.origin}` : null), }, }; diff --git a/packages/ava-react/src/utils/index.ts b/packages/ava-react/src/utils/index.ts index 3016550de..2ff1a157c 100644 --- a/packages/ava-react/src/utils/index.ts +++ b/packages/ava-react/src/utils/index.ts @@ -1,2 +1,3 @@ export { getPrefixCls } from './getPrefixCls'; export { classnames } from './classnames'; +export * from './isType'; diff --git a/packages/ava-react/src/utils/isType.ts b/packages/ava-react/src/utils/isType.ts new file mode 100644 index 000000000..e014eda6a --- /dev/null +++ b/packages/ava-react/src/utils/isType.ts @@ -0,0 +1,6 @@ +import { toNumber, isNaN } from 'lodash'; + +export function isNumberLike(val: unknown) { + const numVal = toNumber(val); + return !isNaN(numVal); +}