Public
Edited
May 26, 2018
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
streets = d3.json('https://gist.githubusercontent.com/floledermann/92a56b764a92b55a857f5115236146b2/raw/b80ac35cc40de5651c6715c6abc4f8dd23b78c5e/streetnames_gender.geojson')
Insert cell
street = streets.features.filter(street => street.properties.name == "Billrothstraße")[0]
Insert cell
Insert cell
projection = d3.geoMercator().fitExtent([[margin, margin], [width - margin, height - margin]], street)
Insert cell
Insert cell
pathGenerator = d3.geoPath().projection(projection)
Insert cell
mapPath = pathGenerator(street)
Insert cell
streetMap = {
// create SVG element
let svg = d3.select(DOM.svg(width, height))
// construct the element
svg.append('path')
.datum(street)
.attr('d', pathGenerator)
.attr('fill', 'none')
.attr('stroke', '#999999')
.attr('stroke-width', '2')
// pass to Observable to draw this block
return svg.node()
}
Insert cell
viennaProjection = d3.geoMercator().fitExtent([[margin, margin], [width - margin, height - margin]], streets)
Insert cell
viennaPathGenerator = d3.geoPath().projection(viennaProjection)
Insert cell
streetsMap = {
let svg = d3.select(DOM.svg(width, height))
// construct the path elements using the D3 data join
svg.selectAll('path')
// data() expects an Array, so make sure to pass the features entry of our FeatureCollection
.data(streets.features)
// select all data items that are not represented on the map yet, and add them
.enter()
.append('path')
.attr('d', viennaPathGenerator)
.attr('fill', 'none')
.attr('stroke', '#999999')
.attr('stroke-width', '0.5')
return svg.node()
}
Insert cell
Insert cell
widthScale = d3.scaleLinear()
.domain([0,5]) // input values will be between 0 and 5
.range([0.3,4]) // output values should be between 0.3 (for the lowest input) and 4 (for the highest)
Insert cell
streetsMapDynamic = {
let svg = d3.select(DOM.svg(width, height))
svg.selectAll('path')
.data(streets.features)
.enter()
.append('path')
.attr('d', viennaPathGenerator)
.attr('fill', 'none')
.attr('stroke', '#999999')
// use a callback function to calculate the stroke width based on the 'w' property
.attr('stroke-width', function(d) {
// access the 'w' property and use our scale to calculate the correct width
return widthScale(d.properties.w);
})
return svg.node()
}
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