Skip to content

Commit

Permalink
Clean up naming and unused props
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelnesen committed Dec 18, 2024
1 parent 82dc4b4 commit a482eb8
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 64 deletions.
7 changes: 3 additions & 4 deletions packages/polaris-viz/src/components/FunnelChartNext/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {SingleTextLine} from '../Labels';
import {ChartElements} from '../ChartElements';

import {
FunnelChartXAxisLabels,
FunnelChartLabels,
Tooltip,
FunnelTooltip,
TooltipWithPortal,
Expand Down Expand Up @@ -54,8 +54,7 @@ export function Chart({
seriesNameFormatter,
labelFormatter,
percentageFormatter = (value: number) => {
const formattedValue = labelFormatter(value);
return `${formattedValue}%`;
return labelFormatter(value);
},
renderScaleIconTooltipContent,
}: ChartProps) {
Expand Down Expand Up @@ -164,7 +163,7 @@ export function Chart({
/>

<g transform={`translate(0,${PERCENTAGE_SUMMARY_HEIGHT})`}>
<FunnelChartXAxisLabels
<FunnelChartLabels
formattedValues={formattedValues}
labels={labels}
labelWidth={sectionWidth}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const VALUE_COLOR = 'rgba(97, 97, 97, 1)';

const REDUCED_FONT_SIZE = 11;

export interface FunnelChartXAxisLabelsProps {
export interface FunnelChartLabelsProps {
formattedValues: string[];
labels: string[];
labelWidth: number;
Expand All @@ -34,7 +34,7 @@ export interface FunnelChartXAxisLabelsProps {
renderScaleIconTooltipContent?: () => ReactNode;
}

export function FunnelChartXAxisLabels({
export function FunnelChartLabels({
formattedValues,
labels,
labelWidth,
Expand All @@ -43,7 +43,7 @@ export function FunnelChartXAxisLabels({
xScale,
shouldApplyScaling,
renderScaleIconTooltipContent,
}: FunnelChartXAxisLabelsProps) {
}: FunnelChartLabelsProps) {
const {characterWidths} = useChartContext();
const [showTooltip, setShowTooltip] = useState(false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {FunnelChartXAxisLabels} from './FunnelChartXAxisLabels';
export {FunnelChartLabels} from './FunnelChartLabels';
export {Tooltip} from './Tooltip';
export {FunnelTooltip} from './FunnelTooltip';
export {TooltipWithPortal} from './TooltipWithPortal';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ const labelFormatter = (value) => {
}).format(Number(value));
};

const percentageFormatter = (value) => `${labelFormatter(value)}%`;

Default.args = {
data: DEFAULT_DATA,
labelFormatter,
percentageFormatter,
tooltipLabels: {
reached: 'Reached this step',
dropped: 'Dropped off',
Expand Down
101 changes: 51 additions & 50 deletions packages/polaris-viz/src/components/SparkFunnelChart/Chart.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Fragment} from 'react';
import {scaleBand, scaleLinear} from 'd3-scale';
import type {DataSeries} from '@shopify/polaris-viz-core';
import {useChartContext} from '@shopify/polaris-viz-core';

import {useFunnelBarScaling} from '../../hooks';
Expand All @@ -9,17 +8,13 @@ import {FunnelChartConnector, FunnelChartSegment} from '../shared';
import {ChartElements} from '../ChartElements';

import type {SparkFunnelChartProps} from './SparkFunnelChart';

export interface ChartProps {
data: DataSeries[];
tooltipLabels: SparkFunnelChartProps['tooltipLabels'];
}
import styles from './SparkFunnelChart.scss';

const LINE_OFFSET = 1;
const GAP = 1;
const SEGMENT_WIDTH_RATIO = 0.75;

export function Chart({data}: ChartProps) {
export function Chart({data, accessibilityLabel}: SparkFunnelChartProps) {
const {containerBounds} = useChartContext();

const dataSeries = data[0].data;
Expand Down Expand Up @@ -52,48 +47,54 @@ export function Chart({data}: ChartProps) {
const barWidth = sectionWidth * SEGMENT_WIDTH_RATIO;

return (
<ChartElements.Svg height={drawableHeight} width={drawableWidth}>
<FunnelChartConnectorGradient />

{dataSeries.map((dataPoint, index: number) => {
const nextPoint = dataSeries[index + 1];
const xPosition = xScale(dataPoint.key as string);
const x = xPosition == null ? 0 : xPosition;
const nextBarHeight = getBarHeight(nextPoint?.value || 0);

const barHeight = getBarHeight(dataPoint.value || 0);
const isLast = index === dataSeries.length - 1;

return (
<Fragment key={dataPoint.key}>
<g key={dataPoint.key} role="listitem">
<FunnelChartSegment
ariaLabel={`${dataPoint.key}: ${dataPoint.value}`}
barHeight={barHeight}
barWidth={barWidth}
index={index}
isLast={isLast}
x={x}
shouldApplyScaling={shouldApplyScaling}
>
{!isLast && (
<FunnelChartConnector
drawableHeight={drawableHeight}
height={drawableHeight}
index={index}
nextX={
(xScale(nextPoint?.key as string) ?? 0) - LINE_OFFSET
}
nextY={drawableHeight - nextBarHeight}
startX={x + barWidth + GAP}
startY={drawableHeight - barHeight}
/>
)}
</FunnelChartSegment>
</g>
</Fragment>
);
})}
</ChartElements.Svg>
<Fragment>
{accessibilityLabel ? (
<span className={styles.VisuallyHidden}>{accessibilityLabel}</span>
) : null}

<ChartElements.Svg height={drawableHeight} width={drawableWidth}>
<FunnelChartConnectorGradient />

{dataSeries.map((dataPoint, index: number) => {
const nextPoint = dataSeries[index + 1];
const xPosition = xScale(dataPoint.key as string);
const x = xPosition == null ? 0 : xPosition;
const nextBarHeight = getBarHeight(nextPoint?.value || 0);

const barHeight = getBarHeight(dataPoint.value || 0);
const isLast = index === dataSeries.length - 1;

return (
<Fragment key={dataPoint.key}>
<g key={dataPoint.key} role="listitem">
<FunnelChartSegment
ariaLabel={`${dataPoint.key}: ${dataPoint.value}`}
barHeight={barHeight}
barWidth={barWidth}
index={index}
isLast={isLast}
x={x}
shouldApplyScaling={shouldApplyScaling}
>
{!isLast && (
<FunnelChartConnector
drawableHeight={drawableHeight}
height={drawableHeight}
index={index}
nextX={
(xScale(nextPoint?.key as string) ?? 0) - LINE_OFFSET
}
nextY={drawableHeight - nextBarHeight}
startX={x + barWidth + GAP}
startY={drawableHeight - barHeight}
/>
)}
</FunnelChartSegment>
</g>
</Fragment>
);
})}
</ChartElements.Svg>
</Fragment>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import '../../styles/common';

.VisuallyHidden {
@include visually-hidden;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@ import {ChartSkeleton} from '../';
import {Chart} from './Chart';

export type SparkFunnelChartProps = {
tooltipLabels: {
reached: string;
dropped: string;
};
accessibilityLabel?: string;
} & ChartProps;

export function SparkFunnelChart(props: SparkFunnelChartProps) {
const {theme: defaultTheme} = useChartContext();

const {
data,
accessibilityLabel,
theme = defaultTheme,
id,
isAnimated,
state,
errorText,
onError,
tooltipLabels,
} = {
...DEFAULT_CHART_PROPS,
...props,
Expand All @@ -51,7 +48,7 @@ export function SparkFunnelChart(props: SparkFunnelChartProps) {
theme={theme}
/>
) : (
<Chart data={data} tooltipLabels={tooltipLabels} />
<Chart data={data} accessibilityLabel={accessibilityLabel} />
)}
</ChartContainer>
);
Expand Down

0 comments on commit a482eb8

Please sign in to comment.