Published
Edited
Aug 26, 2020
1 fork
Insert cell
Insert cell
svg = {
let svg_width = width;
let svg_height = 0.4 * svg_width;
let svg = d3
.create('svg')
.attr('width', svg_width)
.attr('height', svg_height);

let x_scale = d3
.scaleLinear()
.domain([bounds.xmin, bounds.xmax])
.range([0, svg_width]);
let y_scale = d3
.scaleLinear()
.domain([bounds.ymin, bounds.ymax])
.range([svg_height, 0]);
let pts_to_path = d3
.line()
.x(function(d) {
return x_scale(d[0]);
})
.y(function(d) {
return y_scale(d[1]);
});

let triangles = triangulated_counties
.map(c => extract_geometry(c, 'triangle'))
.flat(1);

svg
.selectAll('path.tri')
.data(triangles)
.enter()
.append("path")
.attr('class', 'tri')
.attr('d', function(d) {
// console.log(d);
return pts_to_path(d);
})
.attr("stroke", "black")
.attr("stroke-width", .1)
.attr('fill', 'white');

let segments = triangulated_counties
.map(c => extract_geometry(c, 'segment'))
.flat(1);

svg
.selectAll('path.seg')
.data(segments)
.enter()
.append("path")
.attr('class', 'seg')
.attr('d', function(d) {
console.log(['seg', d]);
return pts_to_path(d);
})
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr('fill', 'white');

return svg.node();
}
Insert cell
triangulated_counties.map(c => extract_geometry(c, 'segment')).flat(1)
Insert cell
bounds = {
let xpts = triangulated_counties.map(c => c.pts.map(xy => xy[0])).flat();
let ypts = triangulated_counties.map(c => c.pts.map(xy => xy[1])).flat();
let xmin = d3.min(xpts);
let xmax = d3.max(xpts);
let ymin = d3.min(ypts);
let ymax = d3.max(ypts);
return { xmin: xmin, ymin: ymin, xmax: xmax, ymax: ymax };
}
Insert cell
extract_geometry(triangulated_counties[0], 'triangle')
Insert cell
//triangulated_counties.map(c => extract_geometry(c, 'triangle')).flat(2)
Insert cell
function extract_geometry(county, type) {
let pts = county.pts;
let geo;
if (type == 'triangle') {
geo = county.triangles;
} else if (type == 'segment') {
geo = county.segments;
}
return geo.map(function(indices) {
return indices.map(i => pts[i]);
});
}
Insert cell
triangulated_counties = FileAttachment('nc_triangulated3.json').json()
Insert cell
d3 = require('d3@5')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more