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

use colors in tooltip cache #97

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export const useOptions = ({
columnSettings,
datasetOptions,
hasMismatchedTooltipsAndMeasures,
disableTooltip
disableTooltip,
colors
});

const options: ChartProps<ChartJSChartType>['options'] = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface UseTooltipOptionsProps {
datasetOptions: DatasetOption[];
hasMismatchedTooltipsAndMeasures: boolean;
disableTooltip: boolean;
colors: string[];
}

export const useTooltipOptions = ({
Expand All @@ -44,7 +45,8 @@ export const useTooltipOptions = ({
columnSettings,
selectedAxis,
datasetOptions,
disableTooltip
disableTooltip,
colors
}: UseTooltipOptionsProps): DeepPartial<TooltipOptions> => {
const tooltipCache = useRef<Record<string, string>>({});

Expand All @@ -53,6 +55,10 @@ export const useTooltipOptions = ({
[columnLabelFormats]
);

const colorsStringCache = useMemo(() => {
return colors.map((c) => String(c)).join('');
}, [colors]);
dallinbentley marked this conversation as resolved.
Show resolved Hide resolved

const mode: TooltipOptions['mode'] = useMemo(() => {
if (selectedChartType === 'scatter') {
return 'nearest';
Expand Down Expand Up @@ -107,7 +113,12 @@ export const useTooltipOptions = ({
}, [useGlobalPercentage, selectedChartType, columnSettings, tooltipKeys]);

const memoizedExternal = useMemoizedFn((context: TooltipContext) => {
const key = createTooltipCacheKey(context.chart, keyToUsePercentage, columnLabelFormatsString);
const key = createTooltipCacheKey(
context.chart,
keyToUsePercentage,
columnLabelFormatsString,
colorsStringCache
);
const matchedCacheItem = tooltipCache.current[key];
const result = externalTooltip(
context,
Expand Down Expand Up @@ -149,7 +160,8 @@ export const useTooltipOptions = ({
const createTooltipCacheKey = (
chart: ChartJSOrUndefined,
keyToUsePercentage: string[],
columnLabelFormatsString: string
columnLabelFormatsString: string,
colorsStringCache: string
) => {
if (!chart?.tooltip) return '';

Expand All @@ -159,7 +171,8 @@ const createTooltipCacheKey = (
chart.tooltip.title.join(''),
chart.tooltip.body?.map((b) => b.lines.join('')).join(''),
keyToUsePercentage.join(''),
columnLabelFormatsString
columnLabelFormatsString,
colorsStringCache
];

return parts.join('');
Expand Down