chart1 = {
const svg = d3
.create("svg")
.attr("width", 1000)
.attr("height", height + 100)
.style("text-anchor", "middle");
svg.append("g").call(yAxis);
svg.append("line").drawData = sel => {
const curveData2 = filteredPolicy.filter(function(d) {
return d.stateCode == sel;
console.log(sel);
});
const line = svg
.selectAll("line")
.data(curveData2, d => d.game)
.join(
enter =>
enter
.append("line")
.style("stroke", "black")
.style("opacity", 1)
.attr("x1", d => x(d.StartDateofMask))
.attr("y1", 0)
.attr("x2", d => x(d.StartDateofMask))
.attr("y2", max2)
);
};
svg.node().drawData = sel => {
const curveData = filteredData.filter(function(d) {
return d.state == sel;
});
const mask = svg
.append("rect")
.attr("x", x(filteredPolicy.get(sel)[0].StartDateofMask))
.attr("y", y(max2) + margin.bottom)
.attr("width", 1)
.attr("height", y(0))
.attr("fill", "blue");
const SAHRect = svg
.append("rect")
.attr("x", x(filteredPolicy.get(sel)[0].StartDateOfSAH))
.attr("y", y(max2) + margin.bottom)
.attr(
"width",
x(filteredPolicy.get(sel)[0].EndDateOfSAH) -
x(filteredPolicy.get(sel)[0].StartDateOfSAH)
)
.attr("height", y(0))
.attr("fill-opacity", 0.2)
.attr("stroke", "green")
.attr("fill", "green");
const CRRect = svg
.append("rect")
.attr("x", x(filteredPolicy.get(sel)[0].StartDateOfCR))
.attr("y", y(max2) + margin.bottom)
.attr(
"width",
x(filteredPolicy.get(sel)[0].EndDateOfCR) -
x(filteredPolicy.get(sel)[0].StartDateOfCR)
)
.attr("height", y(0))
.attr("fill-opacity", 0.2)
.attr("stroke", "red")
.attr("fill", "red");
const circles = svg
.selectAll("circle")
.data(curveData, d => d.game)
.join(
enter =>
enter
.append("circle")
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y) + 50)
.attr("opacity", .6)
.attr("fill", "Purple")
.attr("r", 4)
/*
.on("click", function() {
const target = d3.select(this);
target.attr("fill", "black");
})
*/
);
const tip = svg
.append("g") // append a group of stuff
.style("pointer-events", "none")
.style("text-anchor", "middle");
circles.on("mouseover", event => {
const value = event.target.__data__;
const pointer = d3.pointer(event);
const text = [value.date, "New Cases: " + value.y];
const side_padding = 3;
console.log("called", value, pointer);
tip
.style("text-anchor", "middle")
.attr("transform", `translate(${pointer[0]}, ${pointer[1]})`)
.selectAll("text")
.data(text)
.join('text')
.style("dominant-baseline", "ideographic")
.text(d => d)
.attr("y", (d, i) => (i - (text.length - 1)) * 15)
.style("font-weight", (d, i) => (i === 0 ? "bold" : "normal"));
const bbox = tip.node().getBBox();
// Add a rectangle (as background)
tip
.append("rect")
.attr("y", bbox.y)
.attr("x", bbox.x - side_padding)
.attr("width", bbox.width + side_padding * 2)
.attr("height", bbox.height)
.style("fill", "white")
.style("stroke", "#d3d3d3")
.lower();
// Make sure it doesn't go beyond the left of the chart
if (pointer[0] + bbox.x < margin.left) {
tip.attr(
"transform",
`translate(${margin.left + bbox.width / 2}, ${pointer[1]})`
);
}
// Make sure it doesn't go to the right of the chart
if (pointer[0] - bbox.x > width - margin.right) {
tip.attr(
"transform",
`translate(${width - margin.right - bbox.width / 2}, ${pointer[1]})`
);
}
});
circles.on("mouseout", event => {
tip.selectAll("*").remove();
});
svg
.append("text")
.attr("x", 1000 / 2)
.attr("y", height + 60)
.style("text-anchor", "middle")
.text("Date");
svg
.append("text")
.attr("text-anchor", "middle")
.attr("y", 55)
.attr("x", 98)
.text("New Cases");
svg
.append("text")
.attr("x", 1000 / 2)
.attr("y", 20)
.style("text-anchor", "middle")
.text("New Covid Cases in the Selected US State 2020");
svg
.append("g")
.call(xAxis)
.attr("transform", `translate(0,${height - margin.bottom + 50})`);
};
return svg.node();
}