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(goal): add valueFormatter for tooltip #1529

Merged
merged 13 commits into from
Jan 5, 2022
2 changes: 2 additions & 0 deletions packages/charts/api/charts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,8 @@ export interface GoalSpec extends Spec {
ticks: number[];
// (undocumented)
tickValueFormatter: GoalLabelAccessor;
// (undocumented)
valueFormatter: ValueFormatter;
}

// @public (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { getGreensColorScale } from '../../../../common/color_library_wrappers';
import { Pixels, PointObject } from '../../../../common/geometry';
import { SpecType } from '../../../../specs/constants';
import { ValueFormatter } from '../../../../utils/common';
import { LIGHT_THEME } from '../../../../utils/themes/light_theme';
import { Theme } from '../../../../utils/themes/theme';
import { BandFillColorAccessorInput } from '../../specs';
Expand Down Expand Up @@ -44,6 +45,7 @@ export interface BulletViewModel {
belowBaseCount: number;
angleStart: number;
angleEnd: number;
valueFormatter?: ValueFormatter;
}

/** @internal */
Expand Down Expand Up @@ -80,6 +82,7 @@ export const defaultGoalSpec = {
bandLabels: [],
angleStart: Math.PI + Math.PI / 4,
angleEnd: -Math.PI / 4,
valueFormatter: (value: any) => value,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid the usage of any. ValueFormatter actually is a function with well known type as argument

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart - d41cb16 for changes

};

/** @internal */
Expand All @@ -97,6 +100,7 @@ export const nullGoalViewModel = {
belowBaseCount: 0,
angleStart: 0,
angleEnd: 0,
valueFormatter: undefined,
};

/** @internal */
Expand Down
4 changes: 3 additions & 1 deletion packages/charts/src/chart_types/goal_chart/specs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Color } from '../../../common/colors';
import { Spec } from '../../../specs';
import { SpecType } from '../../../specs/constants';
import { getConnect, specComponentFactory } from '../../../state/spec_factory';
import { LabelAccessor } from '../../../utils/common';
import { LabelAccessor, ValueFormatter } from '../../../utils/common';
import { defaultGoalSpec } from '../layout/types/viewmodel_types';
import { GoalSubtype } from './constants';

Expand Down Expand Up @@ -59,6 +59,7 @@ export interface GoalSpec extends Spec {
angleStart: number;
angleEnd: number;
bandLabels: string[];
valueFormatter: ValueFormatter;
}

type SpecRequiredProps = Pick<GoalSpec, 'id' | 'actual'>;
Expand All @@ -84,5 +85,6 @@ export const Goal: React.FunctionComponent<SpecRequiredProps & SpecOptionalProps
| 'centralMinor'
| 'angleStart'
| 'angleEnd'
| 'valueFormatter'
>(defaultProps),
);
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const getTooltipInfoSelector = createCustomCachedSelector(
if (!spec) {
return EMPTY_TOOLTIP;
}

const { valueFormatter } = spec;
const tooltipInfo: TooltipInfo = {
header: null,
values: [],
Expand All @@ -47,7 +47,7 @@ export const getTooltipInfoSelector = createCustomCachedSelector(
key: spec.id,
},
value,
formattedValue: `${value}`,
formattedValue: valueFormatter ? valueFormatter(value) : `${value}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From your typings valueFormatter is always available, no need to check for its existence.

Suggested change
formattedValue: valueFormatter ? valueFormatter(value) : `${value}`,
formattedValue: spec.valueFormatter(value),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that makes sense - d41cb16 for changes thanks

datum: value,
});
});
Expand Down
1 change: 1 addition & 0 deletions storybook/stories/goal/2_gauge_with_target.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const Example = () => {
centralMinor=""
angleStart={angleStart}
angleEnd={angleEnd}
valueFormatter={(d: any) => `${d}`}
/>
</Chart>
);
Expand Down