function mouseOver(el) {
let mouseEvent = d3.mouse(el);
let x0 = x.invert(mouseEvent[0]);
let xLevel = +d3.timeDay(x0)
let selectedData = null
const myReduce = (list, initialValue, reducer) => {
if(list.length === 0) {
return initialValue;
} else {
const [first, ...rest] = list;
const updatedAcc = reducer(initialValue, first);
const timer = setTimeout(function(){
return myReduce(rest, updatedAcc, reducer);
}, 500)
if(list.length === 0) clearTimeout(timer);
}
}
d3.select(`#references-group-App`)
.append("line")
.attr("x1",x(xLevel))
.attr("y1",0)
.attr("x2",x(xLevel))
.attr("y2",100)
.attr('stroke', 'black')
if(data && data.length > 0){
selectedData = myReduce(data, data[0], (a,b) => {
d3.select(`#references-group-App`)
.append("line")
.attr("x1",x(a["date"]))
.attr("y1",0)
.attr("x2",x(a["date"]))
.attr("y2",100)
.attr('stroke', 'red')
.attr('opacity', 0.1)
d3.select(`#references-group-App`)
.append("text")
.attr("x",x(a["date"]))
.attr("y",100)
.html(a["date"])
.attr('font-size', '8px')
.attr('opacity', 1)
.transition().duration(300)
.attr('opacity', 0)
d3.select(`#references-group-App`)
.append("line")
.attr("x1",x(b["date"]))
.attr("y1",0)
.attr("x2",x(b["date"]))
.attr("y2",100)
.attr('stroke', 'blue')
.attr('opacity', 0.1)
d3.select(`#references-group-App`)
.append("text")
.attr("x",x(b["date"]))
.attr("y",10)
.html(b["date"])
.attr('font-size', '10px')
.attr('opacity', 1)
.transition().duration(300)
.attr('opacity', 0)
return Math.abs(b['date'] - xLevel) <= Math.abs(a["date"] - xLevel) ? b :a
})
}
if(selectedData){
let xPos = x(selectedData['date'])
let yPos = y(selectedData['value'])
console.log(xPos, yPos)
}
}