viewof plotdata = {
function selectColorScale(mode, heatmapMode, minVal, maxVal) {
if (heatmapMode == "Change from 1990") {
return d3.scaleDiverging()
.domain([minVal, 0, maxVal])
.interpolator(d3.interpolateRdYlGn)
}
else if (heatmapMode == "Total emissions") {
return d3.scaleSequential(d3.interpolateGreys).domain([minVal, maxVal]);
}
}
function getData(heatmapMode) {
switch(heatmapMode) {
case "Total emissions":
return plotData.ghgValues;
case "Change from 1990":
return plotData.diffFromBaseValues;
}
}
const marginTop = 20;
const marginRight = 1;
const marginBottom = 40;
const marginLeft = 40;
const rowHeight = 24;
const width = 1200;
const height = rowHeight * plotData.names.length + marginTop + marginBottom;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");
// Create the scales.
const x = d3.scaleLinear()
.domain([d3.min(plotData.years), d3.max(plotData.years) + 1])
.rangeRound([marginLeft, width - marginRight])
const y = d3.scaleBand()
.domain(plotData.names)
.rangeRound([marginTop, height - marginBottom])
const minVal = d3.min(getData(heatmapMode), d => d3.min(d));
const maxVal = d3.max(getData(heatmapMode), d => d3.max(d));
const color = selectColorScale(mode, heatmapMode, minVal, maxVal);
//d3.scaleSequentialSqrt([0, d3.max(plotData.improvementValues, d => d3.max(d))], d3.interpolatePuRd);
// Append the axes.
svg.append("g")
.call(g => g.append("g")
.attr("transform", `translate(0,${marginTop})`)
.call(d3.axisTop(x).ticks(null, "d"))
.call(g => g.select(".domain").remove()))
.call(g => g.append("g")
.attr("transform", `translate(0,${height - marginBottom + 4})`)
.call(d3.axisBottom(x)
.tickValues([plotData.year])
.tickFormat(x => x)
.tickSize(marginTop + marginBottom - height - 10))
.call(g => g.select(".tick text")
.clone()
.attr("dy", "2em")
.style("font-weight", "bold")
.text("Kyoto protocol started"))
.call(g => g.select(".domain").remove()));
svg.append("g")
.attr("transform", `translate(${marginLeft - 10},0)`)
.call(d3.axisLeft(y).tickSize(0))
.call(g => g.select(".domain").remove());
// Create a cell for each (state, year) value.
const f = d3.format(",d");
const format = d => isNaN(d) ? "N/A cases"
: d;
svg.append("g")
.selectAll("g")
.data(getData(heatmapMode))
.join("g")
.attr("transform", (d, i) => `translate(0,${y(plotData.names[i])})`)
.selectAll("circle")
.data(d => d) // Buscar manera de conservar l'index del país a baix
.join("circle")
.attr("cx", (d, i) => x(plotData.years[i]))
.attr("cy", y.bandwidth() - 8)
.attr("r", 12)
// .attr("width", (d, i) => x(plotData.years[i] + 1) - x(plotData.years[i]) - 1)
.attr("fill", d => isNaN(d) ? "#eee" : d === 0 ? "#c6dbef" : color(d))
.append("title")
.text((d, i) => `${format(d)} in ${plotData.years[i]}`);
return Object.assign(svg.node(), {scales: {color}});
}