Skip to content

Commit

Permalink
feat(tooltip): expose datum in the TooltipValue (#1082)
Browse files Browse the repository at this point in the history
Adds the datum, when possible, to the TooltipValue object.
Currently available for cartesian, gauge/goal, heatmap.
Disabled for partition charts as we cannot represent a single datum for a partition chart
due to its aggregated and nested nature.

fix #1042
  • Loading branch information
markov00 authored Mar 23, 2021
1 parent f08f4c9 commit 0246784
Show file tree
Hide file tree
Showing 8 changed files with 2,511 additions and 2,471 deletions.
4,941 changes: 2,478 additions & 2,463 deletions api/charts.api.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/chart_types/goal_chart/state/selectors/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const getTooltipInfoSelector = createCachedSelector(
},
value: shape.actual,
formattedValue: `${shape.actual}`,
datum: shape.actual,
});
});

Expand Down
4 changes: 4 additions & 0 deletions src/chart_types/heatmap/state/selectors/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const getTooltipInfoSelector = createCachedSelector(
},
value: `${shape.datum.x}`,
formattedValue: config.xAxisLabel.formatter(shape.datum.x),
datum: shape.datum,
});

// Y-axis value
Expand All @@ -74,6 +75,7 @@ export const getTooltipInfoSelector = createCachedSelector(
},
value: `${shape.datum.y}`,
formattedValue: config.yAxisLabel.formatter(shape.datum.y),
datum: shape.datum,
});

// Cell value
Expand All @@ -88,6 +90,7 @@ export const getTooltipInfoSelector = createCachedSelector(
},
value: `${shape.value}`,
formattedValue: `${shape.formatted}`,
datum: shape.datum,
});
});
} else {
Expand All @@ -102,6 +105,7 @@ export const getTooltipInfoSelector = createCachedSelector(
},
value: `${pickedShapes.value}`,
formattedValue: `${pickedShapes.value}`,
datum: pickedShapes.value,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function getTooltipInfo(
valueFormatter: ValueFormatter,
percentFormatter: ValueFormatter,
id: string,
) {
): TooltipInfo {
if (!valueFormatter || !labelFormatters) {
return EMPTY_TOOLTIP;
}
Expand Down Expand Up @@ -65,6 +65,7 @@ export function getTooltipInfo(
value,
formattedValue: `${valueFormatter(value)} (${percentFormatter(percentValueGetter(shape))})`,
valueAccessor: shape.depth,
// the datum is omitted ATM due to the aggregated and nested nature of a partition section
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/chart_types/xy_chart/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function getHighligthedValues(

/** @internal */
export function formatTooltip(
{ color, value: { x, y, mark, accessor }, seriesIdentifier }: IndexedGeometry,
{ color, value: { x, y, mark, accessor, datum }, seriesIdentifier }: IndexedGeometry,
spec: BasicSeriesSpec,
isHeader: boolean,
isHighlighted: boolean,
Expand Down Expand Up @@ -104,5 +104,6 @@ export function formatTooltip(
color,
isHighlighted: isHeader ? false : isHighlighted,
isVisible,
datum,
};
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ export {
VerticalAlignment,
HorizontalAlignment,
RecursivePartial,
NonAny,
IsAny,
IsUnknown,
ColorVariant,
Color,
LabelAccessor,
Expand Down
6 changes: 6 additions & 0 deletions src/specs/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ export interface TooltipValue {
* The accessor linked to the current tooltip value
*/
valueAccessor?: Accessor;

/**
* The datum associated with the current tooltip value
* Maybe not available
*/
datum?: unknown;
}

/**
Expand Down
21 changes: 15 additions & 6 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,25 @@ export type RecursivePartial<T> = {
? Set<RecursivePartial<V>>
: T[P] extends Map<infer K, infer V> // checks for Maps
? Map<K, RecursivePartial<V>>
: T[P] extends NonAny // checks for primative values
: T[P] extends NonAny // checks for primitive values
? T[P]
: RecursivePartial<T[P]>; // recurse for all non-array and non-primative values
: IsUnknown<T[P], 1, 0> extends 1
? T[P]
: RecursivePartial<T[P]>; // recurse for all non-array and non-primitive values
};
type NonAny = number | boolean | string | symbol | null;

// return True if T is `any`, otherwise return False
export type IsAny<T, True, False = never> = True | False extends (T extends never ? True : False) ? True : False;

// return True if T is `unknown`, otherwise return False
export type IsUnknown<T, True, False = never> = unknown extends T ? IsAny<T, False, True> : False;

export type NonAny = number | boolean | string | symbol | null;

export interface MergeOptions {
/**
* Includes all available keys of every provided partial at a given level.
* This is opposite to normal behavoir, which only uses keys from the base
* This is opposite to normal behavior, which only uses keys from the base
* object to merge values.
*
* @defaultValue false
Expand Down Expand Up @@ -253,7 +262,7 @@ export function getPartialValue<T>(base: T, partial?: RecursivePartial<T>, parti
* @internal
*/
export function getAllKeys(object: any, objects: any[] = []): string[] {
const initalKeys = object instanceof Map ? [...object.keys()] : Object.keys(object);
const initialKeys = object instanceof Map ? [...object.keys()] : Object.keys(object);

return objects.reduce((keys: any[], obj) => {
if (obj && typeof obj === 'object') {
Expand All @@ -262,7 +271,7 @@ export function getAllKeys(object: any, objects: any[] = []): string[] {
}

return keys;
}, initalKeys);
}, initialKeys);
}

/** @internal */
Expand Down

0 comments on commit 0246784

Please sign in to comment.