| [e4c61dd] | 1 | import identity from "../identity.js";
|
|---|
| 2 | import stream from "../stream.js";
|
|---|
| 3 | import pathArea from "./area.js";
|
|---|
| 4 | import pathBounds from "./bounds.js";
|
|---|
| 5 | import pathCentroid from "./centroid.js";
|
|---|
| 6 | import PathContext from "./context.js";
|
|---|
| 7 | import pathMeasure from "./measure.js";
|
|---|
| 8 | import PathString from "./string.js";
|
|---|
| 9 |
|
|---|
| 10 | export default function(projection, context) {
|
|---|
| 11 | let digits = 3,
|
|---|
| 12 | pointRadius = 4.5,
|
|---|
| 13 | projectionStream,
|
|---|
| 14 | contextStream;
|
|---|
| 15 |
|
|---|
| 16 | function path(object) {
|
|---|
| 17 | if (object) {
|
|---|
| 18 | if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments));
|
|---|
| 19 | stream(object, projectionStream(contextStream));
|
|---|
| 20 | }
|
|---|
| 21 | return contextStream.result();
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | path.area = function(object) {
|
|---|
| 25 | stream(object, projectionStream(pathArea));
|
|---|
| 26 | return pathArea.result();
|
|---|
| 27 | };
|
|---|
| 28 |
|
|---|
| 29 | path.measure = function(object) {
|
|---|
| 30 | stream(object, projectionStream(pathMeasure));
|
|---|
| 31 | return pathMeasure.result();
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | path.bounds = function(object) {
|
|---|
| 35 | stream(object, projectionStream(pathBounds));
|
|---|
| 36 | return pathBounds.result();
|
|---|
| 37 | };
|
|---|
| 38 |
|
|---|
| 39 | path.centroid = function(object) {
|
|---|
| 40 | stream(object, projectionStream(pathCentroid));
|
|---|
| 41 | return pathCentroid.result();
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | path.projection = function(_) {
|
|---|
| 45 | if (!arguments.length) return projection;
|
|---|
| 46 | projectionStream = _ == null ? (projection = null, identity) : (projection = _).stream;
|
|---|
| 47 | return path;
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | path.context = function(_) {
|
|---|
| 51 | if (!arguments.length) return context;
|
|---|
| 52 | contextStream = _ == null ? (context = null, new PathString(digits)) : new PathContext(context = _);
|
|---|
| 53 | if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius);
|
|---|
| 54 | return path;
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | path.pointRadius = function(_) {
|
|---|
| 58 | if (!arguments.length) return pointRadius;
|
|---|
| 59 | pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_);
|
|---|
| 60 | return path;
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | path.digits = function(_) {
|
|---|
| 64 | if (!arguments.length) return digits;
|
|---|
| 65 | if (_ == null) digits = null;
|
|---|
| 66 | else {
|
|---|
| 67 | const d = Math.floor(_);
|
|---|
| 68 | if (!(d >= 0)) throw new RangeError(`invalid digits: ${_}`);
|
|---|
| 69 | digits = d;
|
|---|
| 70 | }
|
|---|
| 71 | if (context === null) contextStream = new PathString(digits);
|
|---|
| 72 | return path;
|
|---|
| 73 | };
|
|---|
| 74 |
|
|---|
| 75 | return path.projection(projection).digits(digits).context(context);
|
|---|
| 76 | }
|
|---|