d3-jsnext
Version:
d3, but futuristic
89 lines (76 loc) • 3.58 kB
JavaScript
import { d3_geo_projectionRadians } from './projection';
import { d3_degrees } from '../math/trigonometry';
import { d3_geo_resample } from './resample';
import { d3_geo_pathContext } from './path-context';
import { d3_geo_pathBuffer } from './path-buffer';
import { d3_identity } from '../core/identity';
import { d3_geo_pathBoundsY1, d3_geo_pathBoundsX1, d3_geo_pathBoundsY0, d3_geo_pathBoundsX0, d3_geo_pathBounds } from './path-bounds';
import { d3_geo_centroidZ0, d3_geo_centroidY0, d3_geo_centroidX0, d3_geo_centroidZ1, d3_geo_centroidY1, d3_geo_centroidX1, d3_geo_centroidZ2, d3_geo_centroidY2, d3_geo_centroidX2 } from './centroid';
import { d3_geo_pathCentroid } from './path-centroid';
import { d3_geo_pathAreaSum, d3_geo_pathArea } from './path-area';
import { d3$geo$albersUsa } from './albers-usa';
import { d3$geo } from './geo';
import { d3$geo$stream } from './stream';
var d3$geo$path;
d3$geo$path = function() {
var pointRadius = 4.5,
projection,
context,
projectStream,
contextStream,
cacheStream;
function path(object) {
if (object) {
if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments));
if (!cacheStream || !cacheStream.valid) cacheStream = projectStream(contextStream);
d3$geo$stream(object, cacheStream);
}
return contextStream.result();
}
path.area = function(object) {
d3_geo_pathAreaSum = 0;
d3$geo$stream(object, projectStream(d3_geo_pathArea));
return d3_geo_pathAreaSum;
};
path.centroid = function(object) {
d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 =
d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 =
d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0;
d3$geo$stream(object, projectStream(d3_geo_pathCentroid));
return d3_geo_centroidZ2 ? [d3_geo_centroidX2 / d3_geo_centroidZ2, d3_geo_centroidY2 / d3_geo_centroidZ2]
: d3_geo_centroidZ1 ? [d3_geo_centroidX1 / d3_geo_centroidZ1, d3_geo_centroidY1 / d3_geo_centroidZ1]
: d3_geo_centroidZ0 ? [d3_geo_centroidX0 / d3_geo_centroidZ0, d3_geo_centroidY0 / d3_geo_centroidZ0]
: [NaN, NaN];
};
path.bounds = function(object) {
d3_geo_pathBoundsX1 = d3_geo_pathBoundsY1 = -(d3_geo_pathBoundsX0 = d3_geo_pathBoundsY0 = Infinity);
d3$geo$stream(object, projectStream(d3_geo_pathBounds));
return [[d3_geo_pathBoundsX0, d3_geo_pathBoundsY0], [d3_geo_pathBoundsX1, d3_geo_pathBoundsY1]];
};
path.projection = function(_) {
if (!arguments.length) return projection;
projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity;
return reset();
};
path.context = function(_) {
if (!arguments.length) return context;
contextStream = (context = _) == null ? new d3_geo_pathBuffer : new d3_geo_pathContext(_);
if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius);
return reset();
};
path.pointRadius = function(_) {
if (!arguments.length) return pointRadius;
pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_);
return path;
};
function reset() {
cacheStream = null;
return path;
}
return path.projection(d3$geo$albersUsa()).context(null);
};
function d3_geo_pathProjectStream(project) {
var resample = d3_geo_resample(function(x, y) { return project([x * d3_degrees, y * d3_degrees]); });
return function(stream) { return d3_geo_projectionRadians(resample(stream)); };
}
export { d3$geo$path, d3_geo_pathProjectStream };