Published
Edited
Dec 21, 2020
1 star
Insert cell
md`# Geography to bar chart
An attempt for a transition between map view and bar chart display. Click on a region to start the transition`
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

geojson.features = geojson.features.sort(
(a, b) => +a.properties.SIFRA - +b.properties.SIFRA
);
geojson.features.forEach(zupanija => (zupanija.properties.selected = false));

let zupanije = svg
.selectAll('path')
.data(geojson.features)
.enter()
.append("path")
.attr("fill", "steelblue")
.style("cursor", "pointer")
.attr('class', d => d.properties.NAZIV)
.attr("stroke", "white")
.attr("stroke-width", 1)
.attr("stroke-linejoin", "round")
.attr("d", d => path(d))
.on("click", (event, d) => {
// geojson.features();
d.properties.selected = true;
d3.select(`.${d.properties.NAZIV}`).attr('fill', 'lightsteelblue');
transform();
});

var transform = () => {
let transitions = [];

transitions.push(
svg
.selectAll('path')
.transition()
.delay((d, i) => 500 + i * 100)
.duration(2000)
.attrTween('d', function(d, i) {
return flubber.toRect(
path(d),
xOffset,
y(d.properties.NAZIV),
lengthScale(geojson.features[i].properties.length),
barHeight
);
})
.end()
);

Promise.all(transitions).then(() => {
const gy = svg.append("g").call(yAxis);

let bars = svg
.append("g")
.selectAll("rect")
.data(geojson.features)
.join("rect")
.attr("fill", d =>
d.properties && d.properties.selected ? "lightsteelblue" : "steelblue"
)
.attr('opacity', 1)
.style("mix-blend-mode", "multiply")
.attr("x", xOffset)
.attr("y", (d, i) => y(d.properties.NAZIV))
//margin.top + i * (barHeight * 1.25))
.attr("width", (d, i) =>
lengthScale(geojson.features[i].properties.length)
)
.attr("height", barHeight);

//svg.selectAll('path').on("click", null);
svg
.selectAll('path')
// .transition()
// .duration(1000)
.remove();

reorder(bars, gy);
});
};

function reorder(bars, gy) {
geojson.features.sort((a, b) => a.properties.length - b.properties.length);

y.domain(geojson.features.map(d => d.properties.NAZIV));

const t = svg.transition().duration(1000);

bars
// .data(geojson, d => d.properties.NAZIV)
.data(geojson.features, d => d.properties.NAZIV)
.order()
.transition(t)
.delay((d, i) => i * 20)
.attr("y", d => {
console.log('idem za ', d, ' postaviti y na ', y(d.properties.NAZIV));
return y(d.properties.NAZIV);
});

gy.transition(t)
.call(yAxis)
.selectAll(".tick")
.delay((d, i) => i * 20);

return svg.node();
}

return svg.node();
}
Insert cell
geojson = {
let retval = topojson.feature(topoHrv, topoHrv.objects['hrvatska.1']);
// let geojson = topojson.mesh(topoHrv);
return retval;
}
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${xOffset},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
Insert cell
y = d3
.scaleBand()
.domain(geojson.features.map(d => d.properties.NAZIV))
.range([height - margin.bottom, margin.top])
.padding(0.1)
Insert cell
xOffset = margin.left + 150
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
geojson.features.forEach(feature => {
feature.properties.length = path.measure(feature);
feature.properties.area = path.area(feature);
feature.properties.name = feature.properties.NAZIV;
});
}
Insert cell
topoHrv = {
let retval = await FileAttachment("hrvatska.1.tomo.topojson.json").json();
let simplified = topojson.presimplify(retval);
let min_weight = topojson.quantile(simplified, 0.35);
return (simplified = topojson.simplify(simplified, min_weight));
}
Insert cell
Insert cell
Insert cell
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