-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a tooltip to the campaign details chart (#97075)
* Adds a black box tooltip to the graphs * Hide the chart legend * Add points permanently * Move formatting functions to inside the memo * Code refactoring and performance improvements
- Loading branch information
Showing
3 changed files
with
186 additions
and
31 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
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
98 changes: 98 additions & 0 deletions
98
client/my-sites/promote-post-i2/components/campaign-stats-line-chart/index.tsx/tooltip.tsx
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,98 @@ | ||
import { debounce } from 'lodash'; | ||
import React from 'react'; | ||
import uPlot from 'uplot'; | ||
|
||
export function tooltip( | ||
tooltipRef: React.MutableRefObject< HTMLDivElement | null >, | ||
formatDate: ( date: Date, hourly: boolean ) => string, | ||
hourly: boolean, | ||
formatValue: ( rawValue: number ) => string | ||
) { | ||
return { | ||
hooks: { | ||
init: ( u: uPlot ) => { | ||
// Create the tooltip element | ||
if ( ! tooltipRef.current ) { | ||
tooltipRef.current = document.createElement( 'div' ); | ||
tooltipRef.current.className = 'campaign-item-details__chart-tooltip'; | ||
u.over.parentNode?.appendChild( tooltipRef.current ); | ||
} | ||
|
||
// Wrap mouse move in a Debounce to reduce the number of updates | ||
const handleMouseMove = debounce( ( e ) => { | ||
if ( ! tooltipRef?.current ) { | ||
return; | ||
} | ||
|
||
// Get the mouse position relative to the chart | ||
const { left } = u.over.getBoundingClientRect(); | ||
const mouseLeft = e.clientX - left; | ||
const activePoint = u.posToIdx( mouseLeft ); | ||
|
||
// If a point is active, update the tooltip | ||
if ( activePoint >= 0 && tooltipRef.current ) { | ||
window.requestAnimationFrame( () => { | ||
if ( ! tooltipRef.current ) { | ||
return; | ||
} | ||
|
||
// Get the highlighted point | ||
const xPoint = u.data[ 0 ][ activePoint ]; | ||
const yPoint = u.data[ 1 ][ activePoint ]; | ||
if ( xPoint == null || yPoint == null ) { | ||
tooltipRef.current.style.display = 'none'; | ||
return; | ||
} | ||
|
||
// Find where that is on the page | ||
const xPos = Math.round( u.valToPos( xPoint, 'x', true ) ); | ||
const yPos = Math.round( u.valToPos( yPoint, 'y', true ) ); | ||
|
||
// Get the date / data value | ||
const date = u.data[ 0 ][ activePoint ]; | ||
const value = u.data[ 1 ][ activePoint ]; | ||
|
||
// If we have a value, put the tooltip just above it. | ||
if ( value != null ) { | ||
const tooltip = tooltipRef.current; | ||
tooltip.style.display = 'block'; | ||
tooltip.style.left = `${ xPos - tooltip.offsetWidth / 2 }px`; | ||
tooltip.style.top = `${ yPos - 16 - tooltip.offsetHeight }px`; | ||
|
||
// Only update the content if it has changed to avoid unnecessary reflows | ||
const newTooltipContent = ` | ||
<div class="campaign-item-details__chart-tooltip-date"> | ||
<strong>${ formatDate( new Date( date * 1000 ), hourly ) }</strong> | ||
</div> | ||
<div class="campaign-item-details__chart-tooltip-divider"></div> | ||
<div class="campaign-item-details__chart-tooltip-data"> | ||
${ formatValue( value ) } | ||
</div> | ||
`; | ||
|
||
if ( tooltip.innerHTML !== newTooltipContent ) { | ||
tooltip.innerHTML = newTooltipContent; | ||
} | ||
} | ||
} ); | ||
} else if ( tooltipRef.current ) { | ||
tooltipRef.current.style.display = 'none'; | ||
} | ||
}, 8 ); // 120mhz (1000/120 = 8.333ms) | ||
|
||
u.over.addEventListener( 'mousemove', handleMouseMove ); | ||
u.over.addEventListener( 'mouseleave', () => { | ||
if ( tooltipRef.current ) { | ||
tooltipRef.current.style.display = 'none'; | ||
} | ||
} ); | ||
}, | ||
destroy: () => { | ||
if ( tooltipRef.current && tooltipRef.current.parentNode ) { | ||
tooltipRef.current.parentNode.removeChild( tooltipRef.current ); | ||
tooltipRef.current = null; | ||
} | ||
}, | ||
}, | ||
}; | ||
} |