Published
Edited
Nov 3, 2020
1 fork
Insert cell
md`# Spikemap of croatian voters`
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("path")
.datum(geojson)
.attr("fill", "lightgrey")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path);

svg.append("g")
.attr("fill", "red")
.attr("fill-opacity", 0.3)
.attr("stroke", "red")
.selectAll("path")
.data(voterData)
//.filter(d => d.position)
// .sort((a, b) => d3.ascending(a.position[1], b.position[1])
// || d3.ascending(a.position[0], b.position[0])))
.join("path")
.attr("transform", d => {
d.position = projection([d.lng,d.lat])
return `translate(${d.position})`}
)
.attr("d", d => spike(length(d.voters)))
.append("title")
.text(d => `${d.mjesto}
${d3.format(".2s")(d.voters)}`);
const legend = svg.append("g")
.attr("fill", "#777")
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("g")
.data(length.ticks(8).slice(1).reverse())
.join("g")
.attr("transform", (d, i) => `translate(${width*0.8 - (i + 1) * 28},520)`);

legend.append("path")
.attr("fill", "red")
.attr("fill-opacity", 0.3)
.attr("stroke", "red")
.attr("d", d => spike(length(d)));

legend.append("text")
.attr("dy", "1.3em")
.text(length.tickFormat(4, "s"));
return svg.node();
}
Insert cell
projection([voterData[3].lng,voterData[3].lat])
Insert cell
projection = d3.geoAlbers()
.rotate([-15,0])
.fitExtent([[0, 20], [width * 0.9, height * 0.9]], geojson)
Insert cell
Insert cell
color = d3.scaleSequential(d3.interpolateYlOrRd).domain([0,1])
Insert cell
spike = (length, width = 7) => `M${-width / 2},0L0,${-length}L${width / 2},0`
Insert cell
height = width
Insert cell
path = d3.geoPath()
.projection(projection)
Insert cell
length(100)
Insert cell
length = d3.scaleLinear([0, maxVoters], [0, 150]).clamp(true)
// length = d3.scaleLog().domain([1, maxVoters]).range([0,250])
// length = d3.scaleLinear([0, 200000], [0, 80]).clamp(true)
Insert cell
maxVoters = d3.max(voterData, d => d.voters)
// maxVoters = 80000
Insert cell
voterData = {
var retval = d3.csvParse(await FileAttachment("glasaciLokacija@2.csv").text(), function(d) {
return {
'mjesto': d.lokacija,
'voters': +d.glasaca,
'lat': +d.lat,
'lng': +d.lng
}
})
// retval = retval.filter(d=>d.voters > 2750)
return retval
}
Insert cell
//topoHrv = FileAttachment("hrvatska.topojson").json()
//topoHrv = FileAttachment("hrv.1.topojson").json()
topoHrv = FileAttachment("hrvatska.1.tomo.topojson.json").json()
Insert cell
hrvatska = FileAttachment("hrvatska.1.geojson").json()
// hrvatska = FileAttachment("hrvatska.1.tomo.topojson.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more