addCursor = (
g,
fmt,
{ height, pHeight, pWidth, margin, xScale, yScale, origin, graph }
) => {
const path = ([x, y]) => {
const p = d3.path();
p.moveTo(x, graph.y.min * yScale + origin.y);
p.lineTo(x, graph.y.max * yScale + origin.y);
p.moveTo(graph.x.min * xScale + origin.x, y);
p.lineTo(graph.x.max * xScale + origin.x, y);
return p;
};
const cursor = g.select('g.cursor');
const show = p =>
cursor
.selectAll('path')
.data(p)
.join('path')
.attr("stroke", "red")
.attr('d', d => path(d).toString());
const mouseLoc = evt => [
evt.layerX - margin.left,
pHeight - evt.layerY + margin.top
];
const scaleLoc = ([x, y]) => [
(x - origin.x) / xScale,
(y - origin.y) / yScale
];
const click = Generators.observe(
change => (g.on('click', () => change(scaleLoc(mouseLoc(d3.event)))), null)
);
const move = Generators.observe(
change => (
g
.call(() => change([0, 0]))
.on('mousemove mouseenter', () => {
const loc = mouseLoc(d3.event);
change(scaleLoc(loc));
show([loc]);
})
.on('mouseleave', () => show([])),
null
)
);
return { click, move };
}