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

fix(tooltip): handle multiple points (#979) #1734

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 12 additions & 3 deletions packages/visx-xychart/src/components/XYChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import React, { useContext, useEffect } from 'react';
import ParentSize from '@visx/responsive/lib/components/ParentSize';
import { ResizeObserverPolyfill } from '@visx/responsive/lib/types';
import { AxisScaleOutput } from '@visx/axis';
import { AxisScale, AxisScaleOutput } from '@visx/axis';
import { ScaleConfig } from '@visx/scale';

import DataContext from '../context/DataContext';
import { Margin, EventHandlerParams } from '../types';
import { Margin, EventHandlerParams, NearestDatumArgs, NearestDatumReturnType } from '../types';
import useEventEmitter from '../hooks/useEventEmitter';
import EventEmitterProvider from '../providers/EventEmitterProvider';
import TooltipContext from '../context/TooltipContext';
Expand All @@ -25,6 +25,8 @@ export type XYChartProps<
XScaleConfig extends ScaleConfig<AxisScaleOutput, any, any>,
YScaleConfig extends ScaleConfig<AxisScaleOutput, any, any>,
Datum extends object,
XScale extends AxisScale,
YScale extends AxisScale,
> = {
/** aria-label for the chart svg element. */
accessibilityLabel?: string;
Expand Down Expand Up @@ -90,6 +92,9 @@ export type XYChartProps<
* into this component.
*/
resizeObserverPolyfill?: ResizeObserverPolyfill;

/** Passed to useEventHandlers to override findNearestDatum logic */
findNearestDatumOverride?: (params: NearestDatumArgs<XScale, YScale, Datum>,) => NearestDatumReturnType<Datum>;
};

const allowedEventSources = [XYCHART_EVENT_SOURCE];
Expand All @@ -98,7 +103,9 @@ export default function XYChart<
XScaleConfig extends ScaleConfig<AxisScaleOutput, any, any>,
YScaleConfig extends ScaleConfig<AxisScaleOutput, any, any>,
Datum extends object,
>(props: XYChartProps<XScaleConfig, YScaleConfig, Datum>) {
XScale extends AxisScale,
YScale extends AxisScale,
>(props: XYChartProps<XScaleConfig, YScaleConfig, Datum, XScale, YScale>) {
const {
accessibilityLabel = 'XYChart',
captureEvents = true,
Expand All @@ -117,6 +124,7 @@ export default function XYChart<
yScale,
// pass prop to context so it can be used anywhere
resizeObserverPolyfill: resizeObserverPolyfillProp,
findNearestDatumOverride,
} = props;
const { setDimensions, resizeObserverPolyfill } = useContext(DataContext);
const tooltipContext = useContext(TooltipContext);
Expand All @@ -137,6 +145,7 @@ export default function XYChart<
onPointerUp,
onPointerDown,
allowedSources: allowedEventSources,
findNearestDatum: findNearestDatumOverride,
});

// if Context or dimensions are not available, wrap self in the needed providers
Expand Down
4 changes: 3 additions & 1 deletion packages/visx-xychart/src/components/series/GlyphSeries.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxisScale } from '@visx/axis';
import React, { useCallback } from 'react';
import { GlyphProps, GlyphsProps } from '../../types';
import { GlyphProps, GlyphsProps, NearestDatumArgs, NearestDatumReturnType } from '../../types';
import BaseGlyphSeries, { BaseGlyphSeriesProps } from './private/BaseGlyphSeries';
import defaultRenderGlyph from './private/defaultRenderGlyph';

Expand All @@ -13,6 +13,8 @@ export default function GlyphSeries<
...props
}: Omit<BaseGlyphSeriesProps<XScale, YScale, Datum>, 'renderGlyphs'> & {
renderGlyph?: React.FC<GlyphProps<Datum>>;
/** Passed to useEventHandlers to override findNearestDatum logic */
findNearestDatumOverride?: (params: NearestDatumArgs<XScale, YScale, Datum>,) => NearestDatumReturnType<Datum>;
}) {
const renderGlyphs = useCallback(
({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext, useMemo } from 'react';
import { AxisScale } from '@visx/axis';
import DataContext from '../../../context/DataContext';
import { GlyphProps, GlyphsProps, SeriesProps } from '../../../types';
import { GlyphProps, GlyphsProps, SeriesProps, NearestDatumArgs, NearestDatumReturnType } from '../../../types';
import withRegisteredData, { WithRegisteredDataProps } from '../../../enhancers/withRegisteredData';
import getScaledValueFactory from '../../../utils/getScaledValueFactory';
import isValidNumber from '../../../typeguards/isValidNumber';
Expand All @@ -19,6 +19,8 @@ export type BaseGlyphSeriesProps<
size?: number | ((d: Datum) => number);
/** Function which handles rendering glyphs. */
renderGlyphs: (glyphsProps: GlyphsProps<XScale, YScale, Datum>) => React.ReactNode;
/** Passed to useEventHandlers to override findNearestDatum logic */
findNearestDatumOverride?: (params: NearestDatumArgs<XScale, YScale, Datum>,) => NearestDatumReturnType<Datum>;
};

export function BaseGlyphSeries<
Expand All @@ -42,6 +44,7 @@ export function BaseGlyphSeries<
xScale,
yAccessor,
yScale,
findNearestDatumOverride,
}: BaseGlyphSeriesProps<XScale, YScale, Datum> & WithRegisteredDataProps<XScale, YScale, Datum>) {
const { colorScale, theme, horizontal } = useContext(DataContext);
const getScaledX = useMemo(() => getScaledValueFactory(xScale, xAccessor), [xScale, xAccessor]);
Expand All @@ -60,6 +63,7 @@ export function BaseGlyphSeries<
onPointerDown,
source: ownEventSourceKey,
allowedSources: [XYCHART_EVENT_SOURCE, ownEventSourceKey],
findNearestDatum: findNearestDatumOverride,
});

const glyphs = useMemo(
Expand Down