-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust tooltip position on StackedAreaChart when page is scrolled
- Loading branch information
1 parent
20ba951
commit e2b1793
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...laris-viz/src/components/StackedAreaChart/utilities/getAlteredStackedAreaChartPosition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type {Dimensions} from '@shopify/polaris-viz-core'; | ||
|
||
import type {TooltipPositionOffset} from '../../TooltipWrapper'; | ||
import type {Margin} from '../../../types'; | ||
|
||
// The space between the cursor and the tooltip | ||
const TOOLTIP_MARGIN = 20; | ||
|
||
export interface AlteredPositionProps { | ||
bandwidth: number; | ||
chartBounds: {x: number; y: number; width: number; height: number}; | ||
currentX: number; | ||
currentY: number; | ||
isPerformanceImpacted: boolean; | ||
margin: Margin; | ||
position: TooltipPositionOffset; | ||
tooltipDimensions: Dimensions; | ||
} | ||
|
||
export interface AlteredPositionReturn { | ||
x: number; | ||
y: number; | ||
} | ||
|
||
export type AlteredPosition = ( | ||
props: AlteredPositionProps, | ||
) => AlteredPositionReturn; | ||
|
||
export function getAlteredStackedAreaChartPosition({ | ||
currentX, | ||
currentY, | ||
chartBounds, | ||
margin, | ||
tooltipDimensions, | ||
}: AlteredPositionProps): AlteredPositionReturn { | ||
const x = Math.min( | ||
Math.max(currentX, TOOLTIP_MARGIN), | ||
chartBounds.width - tooltipDimensions.width - TOOLTIP_MARGIN, | ||
); | ||
|
||
// Y POSITIONING | ||
// If y is below the chart, adjust the tooltip position to the bottom of the chart | ||
// | ||
|
||
const y = | ||
currentY >= chartBounds.y + chartBounds.height | ||
? chartBounds.height - | ||
tooltipDimensions.height - | ||
TOOLTIP_MARGIN - | ||
margin.Bottom | ||
: currentY; | ||
|
||
return {x, y}; | ||
} |