Published
Edited
Dec 3, 2020
1 fork
Insert cell
md`# Vascular chart of San Francisco

Different levels of road infrastructure portrayed as the vascular system.

Data: Open Street Map`
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

//Container for the gradients
var defs = svg.append("defs");

//Filter for the outside glow
// var filter = defs.append("filter").attr("id", "glow");
// filter
// .append("feGaussianBlur")
// .attr("stdDeviation", "2")
// .attr("result", "coloredBlur");
// var feMerge = filter.append("feMerge");
// feMerge.append("feMergeNode").attr("in", "coloredBlur");
// feMerge.append("feMergeNode").attr("in", "SourceGraphic");

svg
.append('rect')
.attr('width', width)
.attr('height', height)
.attr('fill', 'black');
svg
.append('g')
.selectAll('path')
.data(aggDataGeoJSON.features)
.enter()
.append("path")
.attr("fill", 'none')
.attr("stroke", "red")
.attr("stroke-linejoin", "round")
.attr('class', d => d.properties.highway)
.attr('opacity', d => {
if (
d.properties.highway == 'motorway' ||
d.properties.highway == 'trunk'
) {
return 0.95;
} else if (d.properties.highway == 'primary') {
return 0.9;
} else if (d.properties.highway == 'secondary') {
return 0.8;
} else if (d.properties.highway == 'tertiary') {
return 0.7;
} else if (d.properties.highway == 'residential') {
return 0.6;
} else {
return 0.4;
}
})
.attr('stroke-width', d => {
if (
d.properties.highway == 'motorway' ||
d.properties.highway == 'trunk'
) {
return 3;
} else if (d.properties.highway == 'primary') {
return 1.5;
} else if (d.properties.highway == 'secondary') {
return 0.6;
} else if (d.properties.highway == 'tertiary') {
return 0.4;
} else if (d.properties.highway == 'residential') {
return 0.3;
} else {
return 0.2;
}
})
// .style("filter", d => {
// if (
// d.properties.highway == 'motorway' ||
// d.properties.highway == 'trunk'
// ) {
// return "url(#glow)";
// }
// })
.attr("d", path);

return svg.node();
}
Insert cell
[...new Set(d3.map(aggDataGeoJSON.features, d => d.properties.highway))]
Insert cell
aggDataGeoJSON = topojson.feature(aggData, aggData.objects.sf2)
// aggDataGeoJSON = topojson.mesh(aggData, aggData.objects.sf2)
Insert cell
//aggData = JSON.parse(await FileAttachment('sf.all.topojson').text())
aggData = JSON.parse(await FileAttachment('sf.all.trimmed.topojson').text())
Insert cell
// canvas = {
// const context = DOM.context2d(width, height * 1.1);
// const path = d3.geoPath(null, context);
// context.canvas.style.maxWidth = "100%";
// context.lineJoin = "round";
// context.lineCap = "round";
// context.fillStyle = "black";
// context.fillRect(0, 0, width, height * 1.1);
// var geoGenerator = d3
// .geoPath()
// .pointRadius(5)
// .projection(projection)
// .context(context);
// context.strokeStyle = "red";
// context.beginPath();
// context.lineWidth = 3;
// geoGenerator({ type: 'FeatureCollection', features: data.features });
// context.stroke();
// context.beginPath();
// context.lineWidth = 3;
// geoGenerator({ type: 'FeatureCollection', features: trunk.features });
// context.stroke();
// context.beginPath();
// context.lineWidth = 1;
// geoGenerator({
// type: 'FeatureCollection',
// features: primary.features
// });
// context.stroke();
// context.beginPath();
// context.lineWidth = 0.6;
// geoGenerator({
// type: 'FeatureCollection',
// features: secondary.features
// });
// context.stroke();
// context.beginPath();
// context.lineWidth = 0.4;
// geoGenerator({
// type: 'FeatureCollection',
// features: tertiary.features
// });
// context.stroke();
// context.beginPath();
// context.lineWidth = 0.3;
// geoGenerator({
// type: 'FeatureCollection',
// features: residential.features
// });
// context.stroke();
// return context.canvas;
// }
Insert cell
path = d3.geoPath().projection(projection)
Insert cell
height = 600
Insert cell
projection = d3.geoMercator().fitSize([width, height], data)
Insert cell
// residential = JSON.parse(await FileAttachment('sf.residential.geojson').text())
Insert cell
data = JSON.parse(await FileAttachment('sf.motorway@1.geojson').text())
Insert cell
// secondary = JSON.parse(await FileAttachment('sf.secondary.geojson').text())
Insert cell
// tertiary = JSON.parse(await FileAttachment('sf.tertiary.geojson').text())
Insert cell
// trunk = JSON.parse(await FileAttachment('sf.trunk.geojson').text())
Insert cell
// primary = JSON.parse(await FileAttachment('sf.primary.geojson').text())
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require('d3@6')
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