Skip to content

Commit

Permalink
fix: annotations tooltips positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme committed Apr 27, 2020
1 parent f159273 commit 96784a3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/chart_types/xy_chart/annotations/annotation_tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@ export function getFinalAnnotationTooltipPosition(
tooltip: Dimensions,
/** the tooltip computed position not adjusted within chart bounds */
tooltipAnchor: { top: number; left: number },
/** the width of the tooltip portal container */
portalWidth: number,
padding = 10,
): {
left: string | null;
top: string | null;
anchor: 'left' | 'right';
} {
let left = 0;
let anchor: 'left' | 'right' = 'left' as 'left';

const annotationXOffset = window.pageXOffset + container.left + chartDimensions.left + tooltipAnchor.left;
if (chartDimensions.left + tooltipAnchor.left + tooltip.width + padding >= container.width) {
left = annotationXOffset - tooltip.width - padding;
if (chartDimensions.left + tooltipAnchor.left + portalWidth + padding >= container.width) {
left = annotationXOffset - portalWidth - padding;
anchor = 'right' as 'right';
} else {
left = annotationXOffset + padding;
}
Expand All @@ -50,5 +55,6 @@ export function getFinalAnnotationTooltipPosition(
return {
left: `${Math.round(left)}px`,
top: `${Math.round(top)}px`,
anchor,
};
}
21 changes: 21 additions & 0 deletions src/chart_types/xy_chart/renderer/dom/annotation_tooltips.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class AnnotationTooltipComponent extends React.Component<AnnotationTooltipProps>
static displayName = 'AnnotationTooltip';
portalNode: HTMLDivElement | null = null;
tooltipRef: React.RefObject<HTMLDivElement>;
/**
* Max allowable width for tooltip to grow to. Used to determine container fit.
*
* @unit px
*/
static maxWidth = 256;

constructor(props: AnnotationTooltipProps) {
super(props);
Expand All @@ -71,6 +77,7 @@ class AnnotationTooltipComponent extends React.Component<AnnotationTooltipProps>
} else {
this.portalNode = document.createElement('div');
this.portalNode.id = ANNOTATION_CONTAINER_ID;
this.portalNode.style.width = `${AnnotationTooltipComponent.maxWidth}px`;
document.body.appendChild(this.portalNode);
}
}
Expand All @@ -96,12 +103,15 @@ class AnnotationTooltipComponent extends React.Component<AnnotationTooltipProps>
}

const chartContainerBBox = chartContainerRef.current.getBoundingClientRect();
const width = Math.min(AnnotationTooltipComponent.maxWidth, chartContainerBBox.width * 0.7);
this.portalNode.style.width = `${width}px`;
const tooltipBBox = this.tooltipRef.current.getBoundingClientRect();
const tooltipStyle = getFinalAnnotationTooltipPosition(
chartContainerBBox,
chartDimensions,
tooltipBBox,
tooltipState.anchor,
width,
);

if (tooltipStyle.left) {
Expand All @@ -110,6 +120,17 @@ class AnnotationTooltipComponent extends React.Component<AnnotationTooltipProps>
if (tooltipStyle.top) {
this.portalNode.style.top = tooltipStyle.top;
}

if (tooltipStyle.left) {
this.portalNode.style.left = tooltipStyle.left;
if (this.tooltipRef.current) {
this.tooltipRef.current.style.left = tooltipStyle.anchor === 'right' ? 'auto' : '0px';
this.tooltipRef.current.style.right = tooltipStyle.anchor === 'right' ? '0px' : 'auto';
}
}
if (tooltipStyle.top) {
this.portalNode.style.top = tooltipStyle.top;
}
}

componentWillUnmount() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/_annotation.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#echAnnotationContainerPortal {
position: absolute;
width: 256px;
pointer-events: none;
}
.echAnnotation {
pointer-events: none;
Expand Down

0 comments on commit 96784a3

Please sign in to comment.