Skip to content

Commit

Permalink
Update closest point on stacked charts.
Browse files Browse the repository at this point in the history
- Hovering without touching points should select max value for row.
  • Loading branch information
novykh committed Oct 31, 2024
1 parent 7fb5cd5 commit 80fc04a
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/chartLibraries/dygraph/hoverX.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
import getOffsets from "@/helpers/eventOffset"
import { isValidPoint } from "dygraphs/src/dygraph-utils"

const shouldFindMaxValue = {
stacked: true,
stackedBar: true,
}

export default chartUI => {
const findClosest = (event, points) => {
if (!Array.isArray(points)) return {}

const _dygraph = chartUI.getDygraph()

const { offsetX, offsetY } = getOffsets(event)

if (offsetY > chartUI.getDygraph().getArea().h - 10) return { seriesName: "ANNOTATIONS" }
if (offsetY > _dygraph.getArea().h - 10) return { seriesName: "ANNOTATIONS" }
if (offsetY < 15) return { seriesName: "ANOMALY_RATE" }

if (chartUI.chart.getAttribute("chartType"))
return chartUI.getDygraph().findStackedPoint(offsetX, offsetY)

return chartUI.getDygraph().findClosestPoint(offsetX, offsetY)
if (shouldFindMaxValue[chartUI.chart.getAttribute("chartType")]) {
let closestPoint = _dygraph.findStackedPoint(offsetX, offsetY)

if (closestPoint.point?.canvasy > offsetY) {
return points.reduce(
(max, p) => {
if (!isValidPoint(p)) return max

if (p.yval > max.point.yval) {
return {
point: p,
row: p.idx,
seriesName: p.name,
}
}

return max
},
{
point: { yval: -Infinity },
row: null,
seriesName: null,
}
)
}
return closestPoint
}

return _dygraph.findClosestPoint(offsetX, offsetY)
}

let lastX
Expand Down

0 comments on commit 80fc04a

Please sign in to comment.