How to make tooltip snap to smooth chart in AreaClosed? #1528
-
Hello! I love this library, but it really lacks examples and explanation. All I found is gallery and installation tips. Can someone help me combine gradient, smooth gradient and tooltip from AreaClosed chart and tooltip snap from XYChart? In the video below, AreaClosed (first clip) has a bug where tooltip just moving with mouse along X axis. coolchart.mp4While in second clip in XYChart, tooltip and point are snapping to closest data point. coolchart2.mp4From what I found in the code, I think I should change the x variable that is returned from localPoint function: // ...
const handleTooltip = useCallback(
(event: React.TouchEvent<SVGRectElement> | React.MouseEvent<SVGRectElement>) => {
const { x } = localPoint(event) || { x: 0 }
// this ^ variable is mouse position and tooltip position
const x0 = dateScale.invert(x)
const index = bisectDate(chartData, x0, 1)
const d0 = chartData[index - 1]
const d1 = chartData[index]
// ... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
well of course as always I had to figure it out myself... I analysed each of variables in tooltip handler and slightly modified it. // tooltip handler
const handleTooltip = useCallback(
(event: React.TouchEvent<SVGRectElement> | React.MouseEvent<SVGRectElement>) => {
const mouse = localPoint(event) || { x: 0 }
const x0 = dateScale.invert(mouse.x)
const closest = (target: number, list: number[]): number => {
const closestMatch = list.reduce((prev, curr) => Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev)
return closestMatch
}
const closestWithIndex = (target: number, list: number[]): number => {
const closestMatch = closest(target, list)
const closestMatchIndex = list.findIndex(p => p === closestMatch)
return closestMatchIndex
}
const index = closestWithIndex(x0.getTime(), chartData.map(d => new Date(d.date).getTime()))
const d = chartData[index]
const closestPoint = mouseX => {
const points: number[] = chartData.map((_: any, i: number) => i / (chartData.length - 1) * width)
const closestMatch = closest(mouseX, points)
return closestMatch
}
showTooltip({
tooltipData: d,
tooltipLeft: closestPoint(mouse.x),
tooltipTop: valueScale(getValue(d)),
})
},
[showTooltip, valueScale, dateScale],
) Feel free to use it and modify yourself but don't you dare asking me anything about this, I have no idea how it works |
Beta Was this translation helpful? Give feedback.
well of course as always I had to figure it out myself...
I analysed each of variables in tooltip handler and slightly modified it.