This example demonstrates how to create a custom Cartesian projection by implementing a geometry transform on top of two linear scales; the geometry transform is then used in lieu of a geographic projection, which are intended for displaying spherical geometry.
Last active
October 27, 2016 18:07
-
-
Save mbostock/6216797 to your computer and use it in GitHub Desktop.
Custom Cartesian Projection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.lot { | |
fill: lightgray; | |
stroke: black; | |
} | |
</style> | |
<svg width="960" height="500"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
width = +svg.attr("width"), | |
height = +svg.attr("height"); | |
var x = d3.scaleLinear() | |
.range([0, width]); | |
var y = d3.scaleLinear() | |
.range([0, height]); | |
var projection = d3.geoTransform({ | |
point: function(px, py) { | |
this.stream.point(x(px), y(py)); | |
} | |
}); | |
var path = d3.geoPath() | |
.projection(projection); | |
d3.json("geo.json", function(error, geo) { | |
if (error) throw error; | |
x.domain(d3.extent(geo.features, function(d) { return d.properties.Easting; })); | |
y.domain(d3.extent(geo.features, function(d) { return d.properties.Northing; })); | |
svg.append("path") | |
.datum(geo) | |
.attr("class", "lot") | |
.attr("d", path); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment