chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("stroke-miterlimit", 1);
const gradient = DOM.uid();
svg.append("linearGradient")
.attr("id", gradient.id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", 0)
.attr("y2", height)
.selectAll("stop")
.data([
{offset: 0, color: positiveColor},
{offset: 1, color: negativeColor}
])
.join("stop")
.attr("offset", d => d.offset)
.attr("stop-color", d => d.color);
svg.append("g")
.call(xAxis)
.selectAll("text")
.attr("color", "gray")
.attr("font-size", 12);
svg.append("g")
.call(yAxis)
.selectAll("text")
.attr("color", "gray")
.attr("font-size", 18);
svg.append("path")
.attr("fill", "none")
.attr("stroke", gradient)
.attr("stroke-width", 5)
.attr("d", line(data_new.series[0].values));
if (!mutable lookup) mutable lookup = new Date(data_new.dates[0]);
const bar = svg
.append("line")
.attr("stroke", "#ccc")
.attr("stroke-width", 1.5)
.attr("y2", height)
.attr("x1", x(mutable lookup))
.attr("x2", x(mutable lookup));
const legendDate = svg
.append("text")
.style("font", "30px sans-serif")
.attr("fill", "#ccc")
.attr("x", 10)
.attr("y", 100);
const legendValue = svg
.append("text")
.style("font", "30px sans-serif")
.attr("fill", "#ccc")
.attr("x", 10)
.attr("y", 130);
const formatTime = d3.utcFormat("%d/%m/%Y");
function update(date) {
const i = bisect.right(data_new.dates, date);
if (i < data_new.dates.length && i > 0) {
legendDate.text(`${formatTime(data_new.dates[i])}`);
legendDate.attr("x", (i > data_new.dates.length/2)? x(data_new.dates[i])-160 : x(data_new.dates[i]));
legendValue.text(`mobilidade: ${data_new.series[0].values[i]}%`);
legendValue.attr("x", (i > data_new.dates.length/2)? x(data_new.dates[i])-235 : x(data_new.dates[i]));
}
mutable lookup = new Date(date);
bar.attr("x1", x(mutable lookup)).attr("x2", x(mutable lookup));
}
svg.on("mousemove click touchmove", function() {
const m = d3.mouse(this);
update(x.invert(m[0]));
});
update(mutable lookup);
return svg.node();
}