Skip to content

Commit

Permalink
Merge pull request #744 from antvis/yuxi-fix-ntv
Browse files Browse the repository at this point in the history
fix: ntv can handle number string
  • Loading branch information
LAI-X authored Nov 3, 2023
2 parents d3e41f2 + 5b206db commit 3414476
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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),
},
};

Expand All @@ -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),
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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),
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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),
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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),
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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),
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/ava-react/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { getPrefixCls } from './getPrefixCls';
export { classnames } from './classnames';
export * from './isType';
6 changes: 6 additions & 0 deletions packages/ava-react/src/utils/isType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { toNumber, isNaN } from 'lodash';

export function isNumberLike(val: unknown) {
const numVal = toNumber(val);
return !isNaN(numVal);
}
5 changes: 3 additions & 2 deletions packages/ava/src/ntv/generate/generateTextSpec.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 },
},
Expand Down
8 changes: 4 additions & 4 deletions packages/ava/src/ntv/generate/utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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));
}
Expand Down
7 changes: 7 additions & 0 deletions packages/ava/src/utils/isType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { toNumber, isNaN } from 'lodash';

export function isString(val: unknown): val is string {
return typeof val === 'string';
}
Expand All @@ -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);
}

0 comments on commit 3414476

Please sign in to comment.