Skip to content

Commit 3a7b20f

Browse files
committed
add support for custom click handlers
see metricsgraphics#925
1 parent e64520e commit 3a7b20f

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

packages/lib/src/js/charts/line2.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class LineChart extends AbstractChart {
1212
delaunay = null
1313
delaunayPoint = null
1414

15-
constructor ({ area, confidenceBand, ...args }) {
15+
constructor ({ area, confidenceBand, voronoi, ...args }) {
1616
super(args)
1717

1818
// compute lines
@@ -121,7 +121,8 @@ export default class LineChart extends AbstractChart {
121121
onLeave: () => {
122122
this.delaunayPoint.hide()
123123
if (this.tooltip) this.tooltip.hide()
124-
}
124+
},
125+
...voronoi
125126
})
126127
this.delaunay.mountTo(this.container)
127128

packages/lib/src/js/components/delaunay.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class Delaunay {
1111
onLeave = () => null
1212
rect = null
1313

14-
constructor ({ points, xAccessor, yAccessor, xScale, yScale, onPoint, onLeave, nested }) {
14+
constructor ({ points, xAccessor, yAccessor, xScale, yScale, onPoint, onLeave, onClick, nested }) {
1515
// Case 1: There is only one dimension of points (e.g. one line).
1616
// In this case, only use the x-distance by setting all y values to zero.
1717
// if the points are one-dimensional, treat them like that.
@@ -42,6 +42,15 @@ export default class Delaunay {
4242
this.onPoint({ ...this.points[index], index })
4343
}
4444

45+
clickedPoint (rawX, rawY) {
46+
const x = this.xScale.scaleObject.invert(rawX)
47+
const y = this.yScale.scaleObject.invert(rawY)
48+
49+
// find nearest point
50+
const index = this.delaunay.find(x, y)
51+
this.onClick({ ...this.points[index], index })
52+
}
53+
4554
mountTo (svg) {
4655
this.rect = svg
4756
.append('rect')
@@ -58,5 +67,9 @@ export default class Delaunay {
5867
this.rect.on('mouseleave', () => {
5968
this.onLeave()
6069
})
70+
this.rect.on('click', () => {
71+
const rawCoords = mouse(svg.node())
72+
this.clickedPoint(rawCoords[0], rawCoords[1])
73+
})
6174
}
6275
}

0 commit comments

Comments
 (0)