Permalink
Browse files

Fix path.bounds with fallback stream.

Fixes #987.
  • Loading branch information...
mbostock committed Jan 1, 2013
1 parent d2ad9f7 commit 2e072713e3cedcbe265198bdb7d8df90ea96abfe
Showing with 32 additions and 13 deletions.
  1. +4 −5 d3.js
  2. +2 −2 d3.min.js
  3. +3 −5 src/geo/bounds.js
  4. +1 −1 src/geo/path.js
  5. +22 −0 test/geo/path-test.js
View
9 d3.js
@@ -5557,8 +5557,8 @@
(d3.geo.azimuthalEquidistant = function() {
return d3_geo_projection(d3_geo_azimuthalEquidistant);
}).raw = d3_geo_azimuthalEquidistant;
- d3.geo.bounds = d3_geo_bounds();
- function d3_geo_bounds(projection) {
+ d3.geo.bounds = d3_geo_bounds(d3_identity);
+ function d3_geo_bounds(projectStream) {
var x0, y0, x1, y1;
var bound = {
point: boundPoint,
@@ -5571,7 +5571,6 @@
bound.point = boundPoint;
}
};
- var projectBound = projection ? projection.stream(bound) : bound;
function boundPoint(x, y) {
if (x < x0) x0 = x;
if (x > x1) x1 = x;
@@ -5583,7 +5582,7 @@
}
return function(feature) {
y1 = x1 = -(x0 = y0 = Infinity);
- d3.geo.stream(feature, projectBound);
+ d3.geo.stream(feature, projectStream(bound));
return [ [ x0, y0 ], [ x1, y1 ] ];
};
}
@@ -6229,7 +6228,7 @@
return d3_geo_centroidZ ? [ d3_geo_centroidX / d3_geo_centroidZ, d3_geo_centroidY / d3_geo_centroidZ ] : undefined;
};
path.bounds = function(object) {
- return d3_geo_bounds(projection)(object);
+ return d3_geo_bounds(projectStream)(object);
};
path.projection = function(_) {
if (!arguments.length) return projection;
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,6 +1,6 @@
-d3.geo.bounds = d3_geo_bounds();
+d3.geo.bounds = d3_geo_bounds(d3_identity);
-function d3_geo_bounds(projection) {
+function d3_geo_bounds(projectStream) {
var x0, y0, x1, y1;
var bound = {
@@ -13,8 +13,6 @@ function d3_geo_bounds(projection) {
polygonEnd: function() { bound.point = boundPoint; }
};
- var projectBound = projection ? projection.stream(bound) : bound;
-
function boundPoint(x, y) {
if (x < x0) x0 = x;
if (x > x1) x1 = x;
@@ -28,7 +26,7 @@ function d3_geo_bounds(projection) {
return function(feature) {
y1 = x1 = -(x0 = y0 = Infinity);
- d3.geo.stream(feature, projectBound);
+ d3.geo.stream(feature, projectStream(bound));
return [[x0, y0], [x1, y1]];
};
}
View
@@ -29,7 +29,7 @@ d3.geo.path = function() {
};
path.bounds = function(object) {
- return d3_geo_bounds(projection)(object);
+ return d3_geo_bounds(projectStream)(object);
};
path.projection = function(_) {
View
@@ -186,6 +186,22 @@ suite.addBatch({
},
"Sphere": function(area) {
assert.strictEqual(area({type: "Sphere"}), 1620000);
+ },
+ "supports fallback stream": function() {
+ var path = d3.geo.path(),
+ area = path.area({type: "Polygon", coordinates: [[[-122, 37], [-71, 42], [-80, 25], [-122, 37]]]});
+ assert.inDelta(area, 109021.503, 1e-3);
+ }
+ },
+
+ "bounds": {
+ "supports fallback stream": function() {
+ var path = d3.geo.path(),
+ bounds = path.bounds({type: "LineString", coordinates: [[-122, 37], [-74, 40], [-100, 0]]});
+ assert.inDelta(bounds[0][0], -5.1214, 1e-3);
+ assert.inDelta(bounds[0][1], 174.825, 1e-3);
+ assert.inDelta(bounds[1][0], 794.602, 1e-3);
+ assert.inDelta(bounds[1][1], 856.501, 1e-3);
}
},
@@ -321,6 +337,12 @@ suite.addBatch({
assert.deepEqual(testContext.buffer().filter(function(d) { return d.type === "moveTo"; }), [
{type: "moveTo", x: 1370, y: 243}
]);
+ },
+ "supports fallback stream": function() {
+ var path = d3.geo.path(),
+ centroid = path.centroid({type: "LineString", coordinates: [[-122, 37], [-74, 40], [-100, 0]]});
+ assert.inDelta(centroid[0], 434.655, 1e-3);
+ assert.inDelta(centroid[1], 397.940, 1e-3);
}
},

0 comments on commit 2e07271

Please sign in to comment.