map = () => {
const wrapper = d3.create("div");
const svg = wrapper.append("svg")
.style("position", "absolute");
const canvas = wrapper.append("canvas")
.style("position", "absolute");
const annoSvg = wrapper.append("svg")
.style("position", "absolute");
const context = canvas.node().getContext("2d");
path.context(context);
const e = svg.append("path")
.datum(equator)
.attr("stroke", "#666")
.attr("stroke-dasharray", [5,5])
.attr("fill", "none");
const countries = svg.selectAll(".country")
.data(countriesGeo.features)
.join("path")
.attr("class", "country")
.attr("fill", "white")
.attr("fill-opacity", 0.9)
.attr("stroke", "#d5d5d5")
.attr("stroke-linejoin", "round");
const annotationG = annoSvg.selectAll(".anno-g")
.data(annotations)
.join("g")
.attr("class", "anno-g");
const annotationPolyline = annotationG.append("polyline");
const annotationNameG = annotationG.append("g")
.attr("class", "anno-name");
annotationNameG.append("text")
.attr("class", "bg")
.text(d => d.label);
annotationNameG.append("text")
.attr("class", "fg")
.text(d => d.label);
const annotationValueG = annotationG.append("g")
.attr("class", "anno-value");
annotationValueG.append("text")
.attr("class", "bg")
.text(d => `${d.days} days`);
annotationValueG.append("text")
.attr("class", "fg")
.attr("fill", d => color(Math.max(20, d.days)))
.text(d => `${d.days} days`);
return Object.assign(wrapper.node(), {
update(width, height) {
projection.fitSize([width, height], countriesGeo);
wrapper.style("height", `${height + 2}px`);
canvas.node().width = width;
canvas.node().height = height;
data.forEach(d => {
const [ x, y ] = projection([d.lon, d.lat]);
context.beginPath()
context.fillStyle = color(d.days);
context.arc(x, y, 2, 0, Math.PI * 2);
context.fill()
});
path.context(null);
svg
.attr("width", width)
.attr("height", height);
annoSvg
.attr("width", width)
.attr("height", height);
e.attr("d", path);
countries.attr("d", path);
annotationG
.attr("transform", d => `translate(${projection([d.lon, d.lat])})`);
annotationPolyline
.attr("points", d => d.leaderPoints);
annotationNameG
.attr("text-anchor", d => d.textAnchor)
.attr("transform", d => `translate(${d.leaderX + d.textX}, ${d.leaderY + d.textY})`)
annotationValueG
.attr("text-anchor", d => d.textAnchor)
.attr("transform", d => `translate(${d.leaderX + d.textX}, ${17 + d.leaderY + d.textY})`)
}
})
}