makeIntensityChart = (chart, annotateIntensity, annotateAverage) => {
const svg = chart.append("div").append("svg")
.attr("width", chartwidth + marginH * 2)
.attr("height", heightIntensity + marginIntensity.top + marginIntensity.bottom)
.style("margin-bottom", "-9px")
const g = svg.append("g")
.attr("transform", `translate(${[marginH, marginIntensity.top]})`);
const intensityLabelG = g.selectAll(".intensity-label-g")
.data(({entries}) => entries)
.join("g")
.attr("class", "intensity-label-g")
.attr("transform", d => `translate(${x(d.year)})`);
const usLabelG = g.selectAll(".us-label-g")
.data(average.entries)
.join("g")
.attr("class", "us-label-g")
.attr("transform", d => `translate(${x(d.year)})`);
intensityLabelG
.filter(d => [2000, 2010, 2020].includes(d.year))
.append("line")
.attr("transform", d => `translate(${d.year === yearExtent[0] ? 1 : 0})`)
.attr("class", "intensity-label-line")
.attr("y1", d => yIntensity(d.intensity))
.attr("y2", heightIntensity);
g.append("path")
.datum(average.entries)
.attr("class", "us-intensity-line")
.attr("d", d => line(d));
g.append("path")
.datum(d => d.entries)
.attr("class", "intensity-line")
.attr("d", d => line(d));
if (annotateAverage){
g.append("text")
.attr("class", "us-label-annotation")
.attr("dy", -7)
.attr("transform", () => {
const anno = average.entries.find(d => d.year === 2019)
return `translate(${[x(anno.year), yIntensity(anno.intensity)]})`
})
.text("U.S. average")
}
if (annotateIntensity){
const anglers = ["Vermont", "Iowa"];
g.append("text")
.attr("class", "intensity-line-annotation")
.attr("dy", -7)
.attr("transform", d => {
const anno = d.entries.find(d => d.year === 2001);
const annoPoint = [x(anno.year), yIntensity(anno.intensity)]
const annoNext = d.entries.find(d => d.year === (d.state === "Iowa" ? 2004 : d.state === "Vermont" ? 2006 : 2020));
const annoNextPoint = [x(annoNext.year), yIntensity(annoNext.intensity)]
const dIntensityAngle = geometric.lineAngle([annoPoint, annoNextPoint])
return `translate(${annoPoint}) rotate(${anglers.includes(d.state) ? dIntensityAngle : 0})`
})
.text("Carbon intensity");
}
return chart;
}