Skip to content

Commit 5cacd70

Browse files
committed
Configurable graticule precision.
1 parent 891aced commit 5cacd70

4 files changed

Lines changed: 33 additions & 23 deletions

File tree

d3.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,16 +2164,16 @@
21642164
function d3_geo_equirectangular(λ, φ) {
21652165
return [ λ, φ ];
21662166
}
2167-
function d3_geo_graticuleX(y0, y1) {
2168-
var y = d3.range(y0, y1 - ε, d3_geo_graticulePrecision).concat(y1);
2167+
function d3_geo_graticuleX(y0, y1, dy) {
2168+
var y = d3.range(y0, y1 - ε, dy).concat(y1);
21692169
return function(x) {
21702170
return y.map(function(y) {
21712171
return [ x, y ];
21722172
});
21732173
};
21742174
}
2175-
function d3_geo_graticuleY(x0, x1) {
2176-
var x = d3.range(x0, x1 - ε, d3_geo_graticulePrecision).concat(x1);
2175+
function d3_geo_graticuleY(x0, x1, dx) {
2176+
var x = d3.range(x0, x1 - ε, dx).concat(x1);
21772177
return function(y) {
21782178
return x.map(function(x) {
21792179
return [ x, y ];
@@ -6639,7 +6639,7 @@
66396639
geometries: graticule.lines()
66406640
};
66416641
}
6642-
var x1, x0, y1, y0, dx = 22.5, dy = dx, x, y;
6642+
var x1, x0, y1, y0, dx = 22.5, dy = dx, x, y, precision = 2.5;
66436643
graticule.lines = function() {
66446644
return d3.range(Math.ceil(x0 / dx) * dx, x1, dx).map(x).concat(d3.range(Math.ceil(y0 / dy) * dy, y1, dy).map(y)).map(function(coordinates) {
66456645
return {
@@ -6660,18 +6660,22 @@
66606660
y0 = +_[0][1], y1 = +_[1][1];
66616661
if (x0 > x1) _ = x0, x0 = x1, x1 = _;
66626662
if (y0 > y1) _ = y0, y0 = y1, y1 = _;
6663-
x = d3_geo_graticuleX(y0, y1);
6664-
y = d3_geo_graticuleY(x0, x1);
6665-
return graticule;
6663+
return graticule.precision(precision);
66666664
};
66676665
graticule.step = function(_) {
66686666
if (!arguments.length) return [ dx, dy ];
66696667
dx = +_[0], dy = +_[1];
66706668
return graticule;
66716669
};
6670+
graticule.precision = function(_) {
6671+
if (!arguments.length) return precision;
6672+
precision = +_;
6673+
x = d3_geo_graticuleX(y0, y1, precision);
6674+
y = d3_geo_graticuleY(x0, x1, precision);
6675+
return graticule;
6676+
};
66726677
return graticule.extent([ [ -180 + ε, -90 + ε ], [ 180 - ε, 90 - ε ] ]);
66736678
};
6674-
var d3_geo_graticulePrecision = 3;
66756679
d3.geo.greatArc = function() {
66766680
function greatArc() {
66776681
var d = greatArc.distance.apply(this, arguments), t = 0, dt = precision / d, coordinates = [ p0 ];

d3.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/geo/graticule.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ d3.geo.graticule = function() {
22
var x1, x0,
33
y1, y0,
44
dx = 22.5, dy = dx,
5-
x, y;
5+
x, y,
6+
precision = 2.5;
67

78
function graticule() {
89
return {
@@ -40,9 +41,7 @@ d3.geo.graticule = function() {
4041
y0 = +_[0][1], y1 = +_[1][1];
4142
if (x0 > x1) _ = x0, x0 = x1, x1 = _;
4243
if (y0 > y1) _ = y0, y0 = y1, y1 = _;
43-
x = d3_geo_graticuleX(y0, y1);
44-
y = d3_geo_graticuleY(x0, x1);
45-
return graticule;
44+
return graticule.precision(precision);
4645
};
4746

4847
graticule.step = function(_) {
@@ -51,22 +50,28 @@ d3.geo.graticule = function() {
5150
return graticule;
5251
};
5352

53+
graticule.precision = function(_) {
54+
if (!arguments.length) return precision;
55+
precision = +_;
56+
x = d3_geo_graticuleX(y0, y1, precision);
57+
y = d3_geo_graticuleY(x0, x1, precision);
58+
return graticule;
59+
};
60+
5461
return graticule.extent([[-180 + ε, -90 + ε], [180 - ε, 90 - ε]]);
5562
};
5663

57-
var d3_geo_graticulePrecision = 3;
58-
59-
function d3_geo_graticuleX(y0, y1) {
60-
var y = d3.range(y0, y1 - ε, d3_geo_graticulePrecision).concat(y1);
64+
function d3_geo_graticuleX(y0, y1, dy) {
65+
var y = d3.range(y0, y1 - ε, dy).concat(y1);
6166
return function(x) {
6267
return y.map(function(y) {
6368
return [x, y];
6469
});
6570
};
6671
}
6772

68-
function d3_geo_graticuleY(x0, x1) {
69-
var x = d3.range(x0, x1 - ε, d3_geo_graticulePrecision).concat(x1);
73+
function d3_geo_graticuleY(x0, x1, dx) {
74+
var x = d3.range(x0, x1 - ε, dx).concat(x1);
7075
return function(y) {
7176
return x.map(function(x) {
7277
return [x, y];

test/geo/graticule-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ suite.addBatch({
1212
topic: function() {
1313
return d3.geo.graticule()
1414
.extent([[-90, -45], [90, 45]])
15-
.step([45, 45]);
15+
.step([45, 45])
16+
.precision(3);
1617
},
1718

1819
"extent": {

0 commit comments

Comments
 (0)