Skip to content

Commit b9dbc25

Browse files
committed
tooltip clipping issue
1 parent 0a95e50 commit b9dbc25

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

changes.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# change history
22

3+
## version 1.0.1 (tooltip clipping issue)
4+
Fixed issue where the tooltip was getting clipped at the top of the plot under certain circumstances.
5+
36
## version 1.0.0 (composability, multi-axes, performance, spline, axes scales, cadence)
47
1. A chart's configuration in no longer passed as properties of the component, but rather a `<Chart>` is composed by adding components such as axes, tracker, tooltip, and a plot.
58
2. Charts can now have two x-axes and two y-axes.

src/app/charts/RasterPlotTooltipContent.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function addTooltipContent(
245245
// set the header text location
246246
// const spikeHeight = plotDimensions.height / liveDataRef.current.size
247247
const xCoord = tooltipX(x, tooltipWidth, plotDimensions, tooltipStyle, margin)
248-
const yCoord = categoryTooltipY(seriesName, textHeight, axis, tooltipStyle, margin, axis.categorySize)
248+
const yCoord = categoryTooltipY(seriesName, textHeight, axis, tooltipStyle, margin, axis.categorySize, plotDimensions)
249249
const xTooltip = xCoord + tooltipStyle.paddingLeft
250250
const yTooltip = yCoord + tooltipStyle.paddingTop
251251
header

src/app/charts/tooltipUtils.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function removeTooltip() {
8383
}
8484

8585
/**
86-
* Calculates the x-coordinate of the lower left-hand side of the tooltip rectangle (obviously without
86+
* Calculates the x-coordinate of the upper left-hand side of the tooltip rectangle (obviously without
8787
* "rounded corners"). Adjusts the x-coordinate so that tooltip is visible on the edges of the plot.
8888
* @param x The current x-coordinate of the mouse
8989
* @param textWidth The width of the tooltip text
@@ -93,38 +93,39 @@ export function removeTooltip() {
9393
* @return The x-coordinate of the lower left-hand side of the tooltip rectangle
9494
*/
9595
export function tooltipX(x: number, textWidth: number, plotDimensions: Dimensions, tooltipStyle: TooltipStyle, margin: Margin): number {
96-
if (x + textWidth + tooltipStyle.paddingLeft + 10 > plotDimensions.width + margin.left) {
96+
if (x > plotDimensions.width + margin.left - (textWidth + tooltipStyle.paddingLeft + tooltipStyle.paddingRight)) {
9797
return x - textWidth - margin.right + tooltipStyle.paddingRight + tooltipStyle.paddingLeft
9898
}
9999
return x + tooltipStyle.paddingLeft
100100
}
101101

102102
/**
103-
* Calculates the y-coordinate of the lower-left-hand corner of the tooltip rectangle. Adjusts the y-coordinate
103+
* Calculates the y-coordinate of the upper-left-hand corner of the tooltip rectangle. Adjusts the y-coordinate
104104
* so that the tooltip is visible on the upper edge of the plot
105-
* @param y The y-coordinate of the series
105+
* @param y The y-coordinate of the mouse
106106
* @param textHeight The height of the header and neuron ID text
107107
* @param plotDimensions The dimensions of the plot
108108
* @param tooltipStyle The tooltip style information
109109
* @param margin The plot margin
110110
* @return The y-coordinate of the lower-left-hand corner of the tooltip rectangle
111111
*/
112112
export function tooltipY(y: number, textHeight: number, plotDimensions: Dimensions, tooltipStyle: TooltipStyle, margin: Margin): number {
113-
return Math.min(
114-
y + margin.top - textHeight,
115-
plotDimensions.height - margin.top + tooltipStyle.paddingTop + tooltipStyle.paddingBottom
116-
)
113+
if (y > plotDimensions.height + margin.top - (textHeight + tooltipStyle.paddingTop + tooltipStyle.paddingBottom)) {
114+
return plotDimensions.height + margin.top - (textHeight + tooltipStyle.paddingTop + tooltipStyle.paddingBottom)
115+
}
116+
return y + tooltipStyle.paddingTop
117117
}
118118

119119
/**
120-
* Calculates the y-coordinate of the lower-left-hand corner of the tooltip rectangle. Adjusts the y-coordinate
121-
* so that the tooltip is visible on the upper edge of the plot
120+
* Calculates the y-coordinate for the upper-left-hand the tooltip rectangle. Adjusts the y-coordinate so
121+
* that the tooltip is visible on the upper and lower edges of the plot
122122
* @param seriesName The name of the series
123123
* @param textHeight The height of the header and neuron ID text
124124
* @param axis The category axis for determining the y-value of the tooltip
125125
* @param tooltipStyle The tooltip style
126126
* @param margin The plot margin
127127
* @param categoryHeight The height (in pixels) of the category
128+
* @param plotDimensions The dimensions of the plot
128129
* @return The y-coordinate of the lower-left-hand corner of the tooltip rectangle
129130
*/
130131
export function categoryTooltipY(
@@ -133,10 +134,18 @@ export function categoryTooltipY(
133134
axis: CategoryAxis,
134135
tooltipStyle: TooltipStyle,
135136
margin: Margin,
136-
categoryHeight: number
137+
categoryHeight: number,
138+
plotDimensions: Dimensions
137139
): number {
138-
const y = (axis.scale(seriesName) || 0) + margin.top - tooltipStyle.paddingBottom
139-
return y > 0 ? y : y + tooltipStyle.paddingBottom + textHeight + tooltipStyle.paddingTop + categoryHeight
140+
const y = axis.scale(seriesName) || 0
141+
const halfHeight = (tooltipStyle.paddingBottom + textHeight + tooltipStyle.paddingTop) / 2
142+
if (y < halfHeight) {
143+
return margin.top
144+
}
145+
if (y > plotDimensions.height + margin.top - halfHeight) {
146+
return plotDimensions.height + margin.top - textHeight
147+
}
148+
return y + margin.top - halfHeight + categoryHeight / 2
140149
}
141150

142151

0 commit comments

Comments
 (0)