This commit is contained in:
Mike Bostock 2012-09-25 15:19:50 -07:00
Родитель 714f47cd2f 5cf9d299ab
Коммит 24d1f6051b
5 изменённых файлов: 93 добавлений и 24 удалений

54
d3.v2.js поставляемый
Просмотреть файл

@ -2030,22 +2030,46 @@
coordinates = projectRotate.invert((coordinates[0] - δx) / k, (δy - coordinates[1]) / k); coordinates = projectRotate.invert((coordinates[0] - δx) / k, (δy - coordinates[1]) / k);
return [ coordinates[0] * d3_degrees, coordinates[1] * d3_degrees ]; return [ coordinates[0] * d3_degrees, coordinates[1] * d3_degrees ];
} }
function intersect(λ0, φ0, λ1, φ1) {
var cosφ0, cosφ1, sinλ0_λ1 = Math.sin(λ0 - λ1);
return Math.abs(sinλ0_λ1) > ε ? Math.atan((Math.sin(φ0) * (cosφ1 = Math.cos(φ1)) * Math.sin(λ1) - Math.sin(φ1) * (cosφ0 = Math.cos(φ0)) * Math.sin(λ0)) / (cosφ0 * cosφ1 * sinλ0_λ1)) : (φ0 + φ1) / 2;
}
function rotateLocation(coordinates) {
return rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians);
}
function transformPoint(λ, φ) {
var point = project(λ, φ);
return [ point[0] * k + δx, δy - point[1] * k ];
}
function reset() { function reset() {
projectRotate = d3_geo_compose(d3_geo_rotation(δλ, δφ, δγ), project); projectRotate = d3_geo_compose(rotate = d3_geo_rotation(δλ, δφ, δγ), project);
var center = project(λ, φ); var center = project(λ, φ);
δx = x - center[0] * k; δx = x - center[0] * k;
δy = y + center[1] * k; δy = y + center[1] * k;
return p; return p;
} }
var project, projectRotate, k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx = x, δy = y; var project, rotate, projectRotate, k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx = x, δy = y;
p.point = function(coordinates, context) { p.point = function(coordinates, context) {
var point = p(coordinates); var point = p(coordinates);
context.point(point[0], point[1]); context.point(point[0], point[1]);
}; };
p.line = function(coordinates, context) { p.line = function(coordinates, context) {
var point = p(coordinates[0]), i = 0, n = coordinates.length; if (!(n = coordinates.length)) return;
var location = rotateLocation(coordinates[0]), λ0 = location[0], φ0 = location[1], point = transformPoint(λ0, φ0), λ1, φ1, δλ, sλ0, n;
context.moveTo(point[0], point[1]); context.moveTo(point[0], point[1]);
while (++i < n) context.lineTo((point = p(coordinates[i]))[0], point[1]); for (var i = 0; i < n; i++) {
location = rotateLocation(coordinates[i]);
λ1 = location[0];
φ1 = location[1];
δλ = (Math.abs(λ1 - λ0) + 2 * π) % (2 * π);
sλ0 = λ0 > 0;
if (i && sλ0 ^ λ1 > 0 && (δλ >= π || δλ < ε && Math.abs(Math.abs(λ0) - π) < ε)) {
φ0 = intersect(λ0, φ0, λ1, φ1);
context.lineTo((point = transformPoint(sλ0 ? π : -π, φ0))[0], point[1]);
context.moveTo((point = transformPoint(sλ0 ? -π : π, φ0))[0], point[1]);
}
context.lineTo((point = transformPoint(λ0 = λ1, φ0 = φ1))[0], point[1]);
}
}; };
p.ring = function(coordinates, context) { p.ring = function(coordinates, context) {
p.line(coordinates, context); p.line(coordinates, context);
@ -2707,7 +2731,7 @@
d3 = { d3 = {
version: "2.10.2" version: "2.10.2"
}; };
var π = Math.PI, d3_radians = π / 180, d3_degrees = 180 / π; var π = Math.PI, ε = 1e-6, d3_radians = π / 180, d3_degrees = 180 / π;
var d3_array = d3_arraySlice; var d3_array = d3_arraySlice;
try { try {
d3_array(document.documentElement.childNodes)[0].nodeType; d3_array(document.documentElement.childNodes)[0].nodeType;
@ -6218,12 +6242,12 @@
} }
var x1 = 180, x0 = -x1, y1 = 90, y0 = -y1, dx = 22.5, dy = dx; var x1 = 180, x0 = -x1, y1 = 90, y0 = -y1, dx = 22.5, dy = dx;
graticule.lines = function() { graticule.lines = function() {
var xLines = d3.range(Math.ceil(x0 / dx) * dx, y0, dx).map(function(x) { var xSteps = d3.range(Math.ceil(x0 / dx) * dx, x1, dx).concat(x1), ySteps = d3.range(Math.ceil(y0 / dy) * dy, y1, dy).concat(y1), xLines = xSteps.map(function(x) {
return [ y0, y1 ].map(function(y) { return ySteps.map(function(y) {
return [ x, y ]; return [ x, y ];
}); });
}), yLines = d3.range(Math.ceil(x1 / dy) * dy, y1, dy).map(function(y) { }), yLines = ySteps.map(function(y) {
return [ x0, x1 ].map(function(x) { return xSteps.map(function(x) {
return [ x, y ]; return [ x, y ];
}); });
}); });
@ -6323,6 +6347,10 @@
}); });
return buffer.length ? buffer.join("") : null; return buffer.length ? buffer.join("") : null;
} }
function pathObject(object, context) {
var pathType = pathObjectByType.get(object.type);
if (pathType) pathType(object, context);
}
function pathGeometry(geometry, context) { function pathGeometry(geometry, context) {
var pathType = pathGeometryByType.get(geometry.type); var pathType = pathGeometryByType.get(geometry.type);
if (pathType) pathType(geometry, context); if (pathType) pathType(geometry, context);
@ -6363,11 +6391,7 @@
var coordinates = polygon.coordinates, i = -1, n = coordinates.length; var coordinates = polygon.coordinates, i = -1, n = coordinates.length;
while (++i < n) projection.ring(coordinates[i], context); while (++i < n) projection.ring(coordinates[i], context);
} }
function pathObject(object, context) { var pointRadius = 4.5, pointCircle = d3_geo_path_circle(pointRadius), projection = d3.geo.albersUsa(), context;
var pathType = pathObjectByType.get(object.type);
if (pathType) pathType(object, context);
}
var pointRadius = 4.5, pointCircle = d3_geo_path_circle(pointRadius), projection, context;
var pathObjectByType = d3.map({ var pathObjectByType = d3.map({
Feature: pathFeature, Feature: pathFeature,
FeatureCollection: pathFeatureCollection, FeatureCollection: pathFeatureCollection,
@ -6403,7 +6427,7 @@
if (typeof x === "function") pointRadius = x; else pointCircle = d3_geo_path_circle(pointRadius = +x); if (typeof x === "function") pointRadius = x; else pointCircle = d3_geo_path_circle(pointRadius = +x);
return path; return path;
}; };
return path.projection(d3.geo.albersUsa()); return path;
}; };
d3.geo.projection = d3_geo_projection; d3.geo.projection = d3_geo_projection;
var d3_geo_stereographic = d3_geo_azimuthal(function(cosλcosφ) { var d3_geo_stereographic = d3_geo_azimuthal(function(cosλcosφ) {

8
d3.v2.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -1,5 +1,6 @@
d3 = {version: "2.10.2"}; // semver d3 = {version: "2.10.2"}; // semver
var π = Math.PI, var π = Math.PI,
ε = 1e-6,
d3_radians = π / 180, d3_radians = π / 180,
d3_degrees = 180 / π; d3_degrees = 180 / π;

Просмотреть файл

@ -12,8 +12,10 @@ d3.geo.graticule = function() {
} }
graticule.lines = function() { graticule.lines = function() {
var xLines = d3.range(Math.ceil(x0 / dx) * dx, y0, dx).map(function(x) { return [y0, y1].map(function(y) { return [x, y]; }); }), var xSteps = d3.range(Math.ceil(x0 / dx) * dx, x1, dx).concat(x1),
yLines = d3.range(Math.ceil(x1 / dy) * dy, y1, dy).map(function(y) { return [x0, x1].map(function(x) { return [x, y]; }); }); ySteps = d3.range(Math.ceil(y0 / dy) * dy, y1, dy).concat(y1),
xLines = xSteps.map(function(x) { return ySteps.map(function(y) { return [x, y]; }); }),
yLines = ySteps.map(function(y) { return xSteps.map(function(x) { return [x, y]; }); });
return xLines.concat(yLines).map(function(coordinates) { return xLines.concat(yLines).map(function(coordinates) {
return { return {
type: "LineString", type: "LineString",

Просмотреть файл

@ -7,6 +7,7 @@ function d3_geo_projection(project) {
// TODO Expose this API? Not that happy with it. // TODO Expose this API? Not that happy with it.
function d3_geo_projectionMutator(projectAt) { function d3_geo_projectionMutator(projectAt) {
var project, var project,
rotate,
projectRotate, projectRotate,
k = 150, k = 150,
x = 480, x = 480,
@ -35,9 +36,30 @@ function d3_geo_projectionMutator(projectAt) {
}; };
p.line = function(coordinates, context) { p.line = function(coordinates, context) {
var point = p(coordinates[0]), i = 0, n = coordinates.length; if (!(n = coordinates.length)) return;
var location = rotateLocation(coordinates[0]),
λ0 = location[0],
φ0 = location[1],
point = transformPoint(λ0, φ0),
λ1,
φ1,
δλ,
sλ0,
n;
context.moveTo(point[0], point[1]); context.moveTo(point[0], point[1]);
while (++i < n) context.lineTo((point = p(coordinates[i]))[0], point[1]); for (var i = 0; i < n; i++) {
location = rotateLocation(coordinates[i]);
λ1 = location[0];
φ1 = location[1];
δλ = (Math.abs(λ1 - λ0) + 2 * π) % (2 * π);
sλ0 = λ0 > 0;
if (i && sλ0 ^ (λ1 > 0) && (δλ >= π || δλ < ε && Math.abs(Math.abs(λ0) - π) < ε)) {
φ0 = intersect(λ0, φ0, λ1, φ1);
context.lineTo((point = transformPoint(sλ0 ? π : -π, φ0))[0], point[1]);
context.moveTo((point = transformPoint(sλ0 ? -π : π, φ0))[0], point[1]);
}
context.lineTo((point = transformPoint(λ0 = λ1, φ0 = φ1))[0], point[1]);
}
}; };
p.ring = function(coordinates, context) { p.ring = function(coordinates, context) {
@ -45,6 +67,26 @@ function d3_geo_projectionMutator(projectAt) {
context.closePath(); context.closePath();
}; };
function intersect(λ0, φ0, λ1, φ1) {
var cosφ0,
cosφ1,
sinλ0_λ1 = Math.sin(λ0 - λ1);
return Math.abs(sinλ0_λ1) > ε
? Math.atan((Math.sin(φ0) * (cosφ1 = Math.cos(φ1)) * Math.sin(λ1)
- Math.sin(φ1) * (cosφ0 = Math.cos(φ0)) * Math.sin(λ0))
/ (cosφ0 * cosφ1 * sinλ0_λ1))
: (φ0 + φ1) / 2;
}
function rotateLocation(coordinates) {
return rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians);
}
function transformPoint(λ, φ) {
var point = project(λ, φ);
return [point[0] * k + δx, δy - point[1] * k];
}
p.scale = function(_) { p.scale = function(_) {
if (!arguments.length) return k; if (!arguments.length) return k;
k = +_; k = +_;
@ -74,7 +116,7 @@ function d3_geo_projectionMutator(projectAt) {
}; };
function reset() { function reset() {
projectRotate = d3_geo_compose(d3_geo_rotation(δλ, δφ, δγ), project); projectRotate = d3_geo_compose(rotate = d3_geo_rotation(δλ, δφ, δγ), project);
var center = project(λ, φ); var center = project(λ, φ);
δx = x - center[0] * k; δx = x - center[0] * k;
δy = y + center[1] * k; δy = y + center[1] * k;