Published
Edited
Jan 4, 2021
1 fork
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

svg
.append('rect')
.attr('width', width)
.attr('height', height)
.attr('fill', 'black');

svg.on("click", function(event) {
console.log([event.x, event.y], projection.invert([event.x, event.y]));
});

var pattern = svg
.append("defs")
.append("pattern")
.attr('id', "hash4_4")
.attr("width", "4")
.attr('height', "4")
.attr('patternUnits', "userSpaceOnUse")
.attr('patternTransform', "rotate(60)")
.append("rect")
.attr('width', "1")
.attr('height', "4")
.attr('transform', "translate(0,0)")
.attr('opacity', 0.4)
.attr('fill', "red");

var streets = svg
// .append('g')
.selectAll('path')
.data(aggDataGeoJSON.features)
.enter()
.append("path")
.attr('id', (d, i) => `path_${i}`)
.attr("fill", d => {
if (d.properties.highway == 'pedestrian') {
return 'url(#hash4_4)';
} else {
return 'none';
}
})
.attr("stroke", "red")
.attr("stroke-linejoin", "round")
.attr('stroke-dasharray', d => {
if (d.properties.highway == 'footway') {
return '8 3';
} else if (d.properties.highway == 'steps') {
return '1';
} else {
return 'none';
}
})
.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 == 'footway') {
return 1;
} else if (d.properties.highway == 'secondary') {
return 1;
} else if (d.properties.highway == 'tertiary') {
return 0.6;
} else if (d.properties.highway == 'residential') {
return 0.6;
} else if (d.properties.highway == 'pedestrian') {
return 1;
} else {
return 1;
}
})
.attr('stroke-width', d => {
if (
d.properties.highway == 'motorway' ||
d.properties.highway == 'trunk'
) {
return 2;
} else if (d.properties.highway == 'footway') {
return 1.5;
} else if (d.properties.highway == 'primary') {
return 2;
} else if (d.properties.highway == 'secondary') {
return 2;
} else if (d.properties.highway == 'tertiary') {
return 1;
} else if (d.properties.highway == 'residential') {
return 1.5;
} else if (d.properties.highway == 'service') {
return 0.3;
} else {
return 1;
}
})
.attr("d", path)
.attr('transform', (d, i) => {
let row = Math.floor(i / cols);
let col = i % cols;
let centroid = path.centroid(d);
return `translate(${col * (width / cols) - centroid[0]},${row * 50 -
centroid[1]})`;
});

streets
.transition()
.duration(1000)
.delay((d, i) => 500 + i * 50)
.attr('transform', 'translate(0,0)');

const location = projection(myBoat);
svg
.append('text')
.attr('x', location[0])
.attr('y', location[1] - 150)
.attr('fill', 'white')
.attr('font-size', 30)
.attr('font-family', 'monospace')
.style('opacity', 0)
.text('Rovinj')
.transition()
.delay(500 + aggDataGeoJSON.features.length * 50)
.duration(2000)
.style('opacity', 1);

return svg.node();
}
Insert cell
projection(myBoat)
Insert cell
myBoat
Insert cell
height
Insert cell
myBoat = [13.633399571159558, 45.07830424900721]
Insert cell
cols = 30
Insert cell
(aggDataGeoJSON.features = aggDataGeoJSON.features.sort(
(a, b) => path.measure(a) - path.measure(b)
))
Insert cell
[...new Set(d3.map(aggDataGeoJSON.features, d => d.properties.highway))]
Insert cell
aggDataGeoJSON = {
let retval = topojson.feature(aggData, aggData.objects.rovinj2);

retval.features.push(
...topojson.feature(aggData, aggData.objects.rovinj1).features
);
// retval.features.push(
// ...topojson.feature(aggData, aggData.objects.rovinj3).features
// );

return retval;
}
// aggDataGeoJSON = topojson.mesh(aggData, aggData.objects.sf2)
Insert cell
aggData = JSON.parse(await FileAttachment('rovinj.json').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
Insert cell
height = (2 / 3) * width
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