chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
let zupanije = svg
.append("g")
.selectAll("path")
.data(sortedFeatures)
.enter();
zupanije
.append("path")
.attr("id", (d, i) => `county-${i}`)
.attr("fill", (d, i) => {
let trend = weeklyTrend.filter(d => d.index == i)[0].trend;
let retval =
trend < 0 ? negativeColorScale(trend) : positiveColorScale(trend);
return retval;
})
.attr("stroke", "black")
.attr("stroke-width", 1.2)
.attr("stroke-linejoin", "round")
.attr("d", d => path(d))
.on("click", (d, i) => {
console.log(d, projection.invert([d.offsetX, d.offsetY]));
})
.append("title")
.text((d, i) => {
let trend = weeklyTrend.filter(d => d.index == i)[0].trend;
return `${counties[i].name}\nTjedni trend: ${d3.format("+.0%")(trend)}`;
});
zupanije
.append("text")
.attr("x", (d, i) => Math.floor(projection(counties[i].label)[0]))
.attr("y", (d, i) => Math.floor(projection(counties[i].label)[1]))
.attr("class", "text-shadow")
.style("stroke", "white")
.style("stroke-width", 4)
.attr("text-anchor", "start")
.attr("alignment-baseline", "hanging")
.style("opacity", 0.9)
.style("pointer-events", "none")
.text((d, i) => {
//let trend = weeklyTrendData.filter(d => d.index == i)[0].trend;
let trend = weeklyTrend.filter(d => d.index == i)[0].trend;
return numberFormat(trend);
});
zupanije
.append("text")
.attr("x", (d, i) => Math.floor(projection(counties[i].label)[0]))
.attr("y", (d, i) => Math.floor(projection(counties[i].label)[1]))
.attr("text-anchor", "start")
.attr("alignment-baseline", "hanging")
.text((d, i) => {
//let trend = weeklyTrendData.filter(d => d.index == i)[0].trend;
let trend = weeklyTrend.filter(d => d.index == i)[0].trend;
return numberFormat(trend);
});
let tableGroups = svg
.append("g")
.attr("transform", `translate(${width * 0.85},100)`)
.selectAll("g")
.data(weeklyTrend)
.enter()
.append("g")
.on("mouseover", (event, d) => {
d3.select(`#county-${d.index}`).attr("stroke-width", 3);
d3.selectAll(`.name-${d.index}`).attr("text-decoration", "underline");
d3.selectAll(`.value-${d.index}`).attr("text-decoration", "underline");
})
.on("mouseout", (event, d) => {
var selected = d3.select(`#county-${d.index}`).attr("stroke-width", 1.2);
d3.selectAll(`.name-${d.index}`).attr("text-decoration", "none");
d3.selectAll(`.value-${d.index}`).attr("text-decoration", "none");
});
tableGroups
.append("text")
.attr("text-anchor", "end")
.attr("class", d => `name-${d.index}`)
.attr("x", 0)
.attr("y", (d, i) => i * 20)
//.text((d, i) => `${counties[i].name}: ${d3.format("+.0%")(d / 100)}`);
.text((d, i) => `${counties[d.index].name}`);
tableGroups
.append("rect")
.attr("width", 20)
.attr("height", 15)
.attr("fill", (d, i) => {
let trend = d.trend;
let retval =
trend < 0 ? negativeColorScale(trend) : positiveColorScale(trend);
return retval;
})
.attr("x", 10)
.attr("y", (d, i) => i * 20 - 12);
tableGroups
.append("text")
.attr("text-anchor", "end")
.attr("class", d => `value-${d.index}`)
.attr("x", 75)
.attr("y", (d, i) => i * 20)
.text((d, i) => `${numberFormat(d.trend)}`);
return svg.node();
}