curve_diff = {
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height + 50)
.attr("text-anchor", "middle");
svg.node().update2 = sel => {
svg.selectAll("*").remove();
const curveData = totalData.filter(function(d) {
return d.state == sel;
});
const gLine = svg.append('g');
const gLine2 = svg.append('g');
const yLimits = d3.extent(curveData, d => +d.y);
const yScale = d3
.scaleLinear()
.domain(yLimits)
.range([
height - margin.bottom,
margin.top
])
.nice();
const timeLimits = d3.extent(curveData, d => d.x);
const xScale = d3
.scaleTime()
.domain(timeLimits)
.range([
margin.left,
width - margin.right
])
.nice();
const covidCasesLineGen = d3
.line()
.x(d => xScale(d.x))
.y(d => yScale(d.y));
const covidDeathLineGen = d3
.line()
.x(d => xScale(d.x))
.y(d => yScale(d.z));
gLine
.append("path")
.attr("stroke", "#944dff")
.datum(curveData)
.attr("fill", "none")
.attr("d", d => covidCasesLineGen(d));
gLine2
.append("path")
.attr("stroke", "red")
.datum(curveData)
.attr("d", d => covidDeathLineGen(d))
.attr("fill", "none");
const gx = svg
.append('g')
.attr('class', 'x-axis')
.attr(
'transform',
'translate(0,' + (height - margin.bottom) + ')'
);
const gy = svg
.append('g')
.attr('class', 'y-axis')
.attr('transform', 'translate(' + margin.left + ',0)');
function zoomed({ transform }) {
const zx = transform.rescaleX(xScale).interpolate(d3.interpolateRound); // calculate the new xScale
const zy = transform.rescaleY(yScale).interpolate(d3.interpolateRound); // calculate the new yScale
gLine.attr("transform", transform).attr("stroke-width", 1 / transform.k); // transform the line
gLine2.attr("transform", transform).attr("stroke-width", 2 / transform.k);
gx.call(xAxi, zx); // redraw x-axis with new "x" scale, "zx" is passed as the second arg to "xAxis"
gy.call(yAxi, zy); // redraw y-axis with new "y" scale, "zy" is passed as the second arg to "yAxis"
}
const zoom = d3
.zoom()
.scaleExtent([0.5, 32])
.on("zoom", zoomed);
svg.call(zoom).call(zoom.transform, d3.zoomIdentity);
};
svg
.append("text")
.attr("font-size", 14)
.attr("x", width / 2)
.attr("y", height + 20)
.text("Date");
svg
.append("text")
.attr("x", width / 2)
.attr("y", 20)
.text("Covid-19 Cases vs Deaths for " + selected_state);
svg
.append("text")
.attr("font-size", 12)
.attr("x", width / 2)
.attr("y", 35)
.text("Most Recent Total Cases: " + max);
svg
.append("text")
.attr("font-size", 12)
.attr("x", width / 2)
.attr("y", 50)
.text("Most Recent Total Deaths: " + max_deaths);
svg
.append("text")
.attr("font-size", 12)
.attr("x", width / 2)
.attr("y", 65)
.text(
"Death Percent of Total: " + ((max_deaths / max) * 100).toFixed(2) + "%"
);
svg
.append("text")
.attr("font-size", 14)
.attr("y", 35)
.attr("x", 220)
.text("Total Cases/Deaths");
// svg
// .append("g")
// .attr("transform", `translate(${width - 400}, 0)`)
// .append(() => death_legend);
return svg.node();
}