chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
svg.append("path")
.attr("fill", "none")
.attr("stroke", "#a4a4a4")
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 3)
.attr("stroke-dasharray", ("3, 3"))
.attr("d", lineNew(data));
svg.append("path")
.attr("fill", "none")
.attr("stroke", "currentColor")
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 3)
.attr("d", avgLine(data));
const legendData = svg
.append("text")
.style("font", "22px sans-serif")
.attr("x", 10)
.attr("y", 100);
const legendR = svg
.append("text")
.style("font", "22px sans-serif")
.attr("x", 10)
.attr("y", 125);
const marker = svg
.append("circle")
.attr("r", 3)
.attr("cx", -100)
.attr("fill", "black")
.attr("fill-opacity", 0)
const formatTime = d3.utcFormat("%d/%m/%Y");
if (!mutable lookup) mutable lookup = new Date(data[0].date);
const bar = svg
.append("line")
.attr("style", "stroke-width:1")
.attr('stroke', 'blue')
.attr("y2", height*0.94)
.attr("x1", xScale(mutable lookup))
.attr("x2", xScale(mutable lookup));
function update(date) {
const i = bisect.right(data, date);
if (i < data.length && i > 0) {
const date1 = data[i].parsedDate;
console.log(i, date1)
legendData.text(`${data[i].date}`);
legendR.text(`Média 7d.: ${data[i].weeklyAvg.toFixed(0)}`);
marker.attr("cx", xScale(date1)).attr("cy", yScaleNew(data[i].ml));
legendData.attr("x", (i > data.length/2)? xScale(date1) - 170 : xScale(date1))
legendR.attr("x", (i > data.length/2)? xScale(date1) - 170 : xScale(date1))
}
mutable lookup = new Date(date);
bar.attr("x1", xScale(mutable lookup)).attr("x2", xScale(mutable lookup));
}
svg.on("mousemove click touchmove", function() {
const m = d3.mouse(this);
update(xScale.invert(m[0]));
});
return svg.node();
}