function hover(svg, path) {
svg
.style("position", "relative");
if ("ontouchstart" in document) svg
.style("-webkit-tap-highlight-color", "transparent")
.on("touchmove", moved)
.on("touchstart", entered)
.on("touchend", left)
else svg
.on("mousemove", moved)
.on("mouseenter", entered)
.on("mouseleave", left);
const dot = svg.append("g")
.attr("display", "none");
dot.append("circle")
.attr("r", 2.5);
dot.append("text")
.style("font", "10px sans-serif")
.attr("text-anchor", "middle")
.attr("y", -8);
function moved() {
d3.event.preventDefault();
const years= ['2012','2013','2014','2015','2016','2017','2018'];
const ym = y.invert(d3.event.layerY);
const xm = x.invert(d3.event.layerX);
var month = xm.getMonth();
var day = xm.getDay();
var dd = new Date(2020,month,day)
var ddd = new Date(dd);
ddd.setDate(dd.getDate()+1);
var d_day = ddd.getDay();
var d_month = ddd.getMonth();
day = dd.getDay();
var min = Infinity;
var idx;
var final_x;
var final_y;
for (var i =0; i< years.length ;i++)
{
for (var j = 0 ;j <data1.series[i].values.length;j++)
{
var this_day = data1.series[i].values[j]['date'].getDay();
var this_month = data1.series[i].values[j]['date'].getMonth();
if (this_month=== month && this_day === day || this_month === d_month && this_day === d_day)
{
if (Math.abs(data1.series[i].values[j]['price']-ym) < min)
{
min = Math.abs(data1.series[i].values[j]['price']-ym);
final_x = data1.series[i].values[j]['date'];
final_y = data1.series[i].values[j]['price'];
idx = i;
}
}
}
}
const s = data1.series[idx];
path.attr("stroke", d => d === s ? color(idx) : "#ddd").filter(d => d === s).raise();
dot.attr("transform", `translate(${x(final_x)},${y(final_y)})`);
dot.select("text").text(s.name);
}
function entered() {
path.style("mix-blend-mode", null).attr("stroke", "#ddd");
dot.attr("display", null);
}
function left() {
dot.attr("display", "none");
}
}