Skip to content

Commit

Permalink
Refactor tooltips to use data attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
envex committed Feb 6, 2025
1 parent a81b589 commit d66a912
Show file tree
Hide file tree
Showing 50 changed files with 1,262 additions and 1,911 deletions.
2 changes: 1 addition & 1 deletion packages/polaris-viz-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface DataGroup {
yAxisOptions?: YAxisOptions;
}

export type Shape = 'Line' | 'Bar' | 'Donut';
export type Shape = 'Line' | 'Bar' | 'Default';

export type LineStyle = 'solid' | 'dotted' | 'dashed';

Expand Down
1 change: 1 addition & 0 deletions packages/polaris-viz/src/components/Arc/Arc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export function Arc({
height={radius * 4}
width={radius * 4}
gradient={gradient}
index={index}
/>
</g>
</Fragment>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type {AriaRole, Dispatch, ReactNode, SetStateAction} from 'react';
import {XMLNS} from '@shopify/polaris-viz-core';

import {getTooltipDataAttr} from '../TooltipWrapper';

import styles from './ChartElements.scss';

interface ChartSVGProps {
Expand Down Expand Up @@ -33,6 +35,13 @@ export function ChartSVG({
aria-label={emptyState ? emptyStateText : undefined}
ref={setRef}
>
<rect
height={height}
width={width}
{...getTooltipDataAttr({
index: -1,
})}
/>
{children}
</svg>
);
Expand Down
19 changes: 0 additions & 19 deletions packages/polaris-viz/src/components/ComboChart/Chart.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {useState} from 'react';
import * as React from 'react';
import {
ChartMargin,
DataType,
COLOR_VISION_SINGLE_ITEM,
useTheme,
useChartPositions,
LINE_HEIGHT,
InternalChartType,
} from '@shopify/polaris-viz-core';
import type {
DataGroup,
Expand All @@ -23,7 +20,6 @@ import {
checkAvailableAnnotations,
YAxisAnnotations,
} from '../Annotations';
import {TooltipWrapper} from '../TooltipWrapper';
import type {
AnnotationLookupTable,
RenderLegendContent,
Expand Down Expand Up @@ -317,21 +313,6 @@ export function Chart({
)}
</ChartElements.Svg>

<TooltipWrapper
bandwidth={labelWidth}
chartBounds={chartBounds}
chartType={InternalChartType.Bar}
data={barChartData.series}
focusElementDataType={DataType.BarGroup}
getMarkup={getTooltipMarkup}
longestSeriesIndex={0}
margin={ChartMargin}
onIndexChange={(index) => setActiveIndex(index)}
parentElement={svgRef}
xScale={barXScale}
yScale={barYScale}
/>

{showLegend && (
<LegendContainer
colorVisionType={COLOR_VISION_SINGLE_ITEM}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type {GradientStop} from '@shopify/polaris-viz-core';
import {Fragment} from 'react';

import {getTooltipDataAttr} from '../TooltipWrapper';
import {createCSSConicGradient} from '../../utilities';

export interface ConicGradientWithStopsProps {
gradient: GradientStop[];
height: number;
width: number;
index: number;
x?: number;
y?: number;
}
Expand All @@ -16,18 +19,31 @@ export function ConicGradientWithStops({
width,
x = 0,
y = 0,
index,
}: ConicGradientWithStopsProps) {
const conicGradientValue = createCSSConicGradient(gradient);

return (
<foreignObject x={x} y={y} width={width} height={height}>
<div
style={{
width: `${width}px`,
height: `${height}px`,
backgroundImage: conicGradientValue,
}}
<Fragment>
<foreignObject x={x} y={y} width={width} height={height}>
<div
style={{
width: `${width}px`,
height: `${height}px`,
backgroundImage: conicGradientValue,
}}
/>
</foreignObject>
<rect
{...getTooltipDataAttr({
index,
})}
width={width}
height={height}
x={x}
y={y}
fill="transparent"
/>
</foreignObject>
</Fragment>
);
}
33 changes: 16 additions & 17 deletions packages/polaris-viz/src/components/DonutChart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import {
useChartContext,
THIN_ARC_CORNER_THICKNESS,
isInfinity,
DataType,
ChartMargin,
InternalChartType,
} from '@shopify/polaris-viz-core';
import type {
DataPoint,
DataSeries,
LabelFormatter,
Direction,
BoundingRect,
} from '@shopify/polaris-viz-core';

import {getAnimationDelayForItems} from '../../utilities/getAnimationDelayForItems';
import {getContainerAlignmentForLegend} from '../../utilities';
import {useDonutChartTooltipContents} from '../../hooks/useDonutChartTooltipContents';
import {TooltipWrapper} from '../../components/TooltipWrapper';
import {
getTooltipDataAttr,
TooltipWrapperNext,
} from '../../components/TooltipWrapper';
import type {ComparisonMetricProps} from '../ComparisonMetric';
import {LegendContainer, useLegend} from '../../components/LegendContainer';
import {
Expand Down Expand Up @@ -103,13 +103,6 @@ export function Chart({

const seriesColor = getSeriesColors(seriesCount, selectedTheme);

const chartBounds: BoundingRect = {
width: containerBounds.width,
height: containerBounds.height,
x: 0,
y: 0,
};

const getTooltipMarkup = useDonutChartTooltipContents({
renderTooltipContent,
data,
Expand Down Expand Up @@ -232,6 +225,17 @@ export function Chart({
ref={setSvgRef}
>
<g className={styles.DonutChart}>
<rect
x={minX}
y={minY}
width={viewBoxDimensions.width}
height={viewBoxDimensions.height}
{...getTooltipDataAttr({
index: -1,
x: 0,
y: 0,
})}
/>
{emptyState ? (
<g aria-hidden>
<Arc
Expand Down Expand Up @@ -320,15 +324,10 @@ export function Chart({
}
/>
)}
<TooltipWrapper
chartBounds={chartBounds}
<TooltipWrapperNext
chartType={InternalChartType.Donut}
focusElementDataType={DataType.Arc}
forceActiveIndex={activeIndex}
getMarkup={getTooltipMarkup}
margin={ChartMargin}
parentElement={svgRef}
usePortal
/>
</div>
);
Expand Down
77 changes: 25 additions & 52 deletions packages/polaris-viz/src/components/HorizontalBarChart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {ReactNode} from 'react';
import {useMemo, useState} from 'react';
import {
uniqueId,
DataType,
COLOR_VISION_SINGLE_ITEM,
HORIZONTAL_SPACE_BETWEEN_CHART_AND_AXIS,
useAriaLabel,
Expand All @@ -15,11 +14,12 @@ import type {
ChartType,
XAxisOptions,
YAxisOptions,
BoundingRect,
LabelFormatter,
} from '@shopify/polaris-viz-core';
import {animated} from '@react-spring/web';

import {getHighestValueForSeries} from '../../utilities/getHighestValueForSeries';
import {TooltipWrapperNext} from '../TooltipWrapper';
import {ChartElements} from '../ChartElements';
import {checkAvailableAnnotations} from '../../components/Annotations';
import {useFormattedLabels} from '../../hooks/useFormattedLabels';
Expand All @@ -44,8 +44,6 @@ import {
useTheme,
} from '../../hooks';
import {ChartMargin, ANNOTATIONS_LABELS_OFFSET} from '../../constants';
import {formatDataIntoGroups} from '../../utilities';
import {TooltipWrapper} from '../TooltipWrapper';

import {
VerticalGridLines,
Expand Down Expand Up @@ -91,8 +89,7 @@ export function Chart({
const [xAxisHeight, setXAxisHeight] = useState(LINE_HEIGHT);
const [annotationsHeight, setAnnotationsHeight] = useState(0);

const {longestSeriesCount, seriesColors, longestSeriesIndex} =
useHorizontalSeriesColors(data);
const {longestSeriesCount, seriesColors} = useHorizontalSeriesColors(data);

const {legend, setLegendDimensions, height, width} = useLegend({
data: [
Expand All @@ -108,25 +105,13 @@ export function Chart({

const {allNumbers, longestLabel, areAllNegative} = useDataForHorizontalChart({
data,
isSimple: false,
isSimple: xAxisOptions.hide,
isStacked,
labelFormatter: xAxisOptions.labelFormatter,
});

const highestValueForSeries = useMemo(() => {
const groups = formatDataIntoGroups(data);

const maxes = groups.map((numbers) => {
const values = numbers.map((value) => value).filter(Boolean) as number[];

if (values.length === 0) {
return 0;
}

return areAllNegative ? Math.min(...values) : Math.max(...values);
});

return maxes;
return getHighestValueForSeries(data, areAllNegative);
}, [data, areAllNegative]);

const {stackedValues, stackedMin, stackedMax} = useHorizontalStackedValues({
Expand All @@ -143,21 +128,21 @@ export function Chart({
stackedMin,
stackedMax,
isStacked,
maxWidth: width - longestLabel.negative - longestLabel.positive,
maxWidth: width,
labelFormatter: xAxisOptions.labelFormatter,
longestLabel,
});

const {barHeight, chartHeight, groupBarsAreaHeight, groupHeight} =
useHorizontalBarSizes({
chartDimensions: {width: drawableWidth, height: drawableHeight},
isSimple: xAxisOptions.hide,
isStacked,
seriesLength: longestSeriesCount,
singleBarCount: data.length,
xAxisHeight,
});

const {barHeight, chartHeight, groupHeight} = useHorizontalBarSizes({
chartDimensions: {width: drawableWidth, height: drawableHeight},
isSimple: xAxisOptions.hide,
isStacked,
seriesLength: longestSeriesCount,
singleBarCount: data.length,
xAxisHeight,
});

const annotationsDrawableHeight =
chartYPosition + chartHeight + ANNOTATIONS_LABELS_OFFSET;

Expand All @@ -174,15 +159,9 @@ export function Chart({
chartXPosition,
});

const zeroPosition = longestLabel.negative + xScale(0);
const zeroPosition = xScale(0);

const labelWidth = drawableWidth / ticks.length;
const chartBounds: BoundingRect = {
width,
height,
x: chartXPosition,
y: 0,
};

const {hasXAxisAnnotations, hasYAxisAnnotations} = checkAvailableAnnotations(
annotationsLookupTable,
Expand Down Expand Up @@ -247,13 +226,17 @@ export function Chart({
areAllNegative={areAllNegative}
ariaLabel={ariaLabel}
barHeight={barHeight}
chartXPosition={chartXPosition}
chartYPosition={chartYPosition}
containerWidth={width}
data={data}
groupHeight={groupHeight}
highestValueForSeries={highestValueForSeries}
id={id}
index={index}
isSimple={false}
isStacked={isStacked}
longestLabel={longestLabel}
name={name}
stackedValues={stackedValues}
xAxisOptions={xAxisOptions}
Expand Down Expand Up @@ -291,21 +274,11 @@ export function Chart({
)}
</ChartElements.Svg>

{highestValueForSeries.length !== 0 && (
<TooltipWrapper
bandwidth={groupBarsAreaHeight}
chartBounds={chartBounds}
chartType={InternalChartType.HorizontalBar}
data={data}
focusElementDataType={DataType.BarGroup}
getMarkup={getTooltipMarkup}
margin={ChartMargin}
parentElement={svgRef}
longestSeriesIndex={longestSeriesIndex}
xScale={xScale}
type={type}
/>
)}
<TooltipWrapperNext
chartType={InternalChartType.HorizontalBar}
getMarkup={getTooltipMarkup}
parentElement={svgRef}
/>

{showLegend && (
<LegendContainer
Expand Down
Loading

0 comments on commit d66a912

Please sign in to comment.