| 1 | import array from "./array.js";
|
|---|
| 2 | import constant from "./constant.js";
|
|---|
| 3 | import curveLinear from "./curve/linear.js";
|
|---|
| 4 | import line from "./line.js";
|
|---|
| 5 | import {withPath} from "./path.js";
|
|---|
| 6 | import {x as pointX, y as pointY} from "./point.js";
|
|---|
| 7 |
|
|---|
| 8 | export default function(x0, y0, y1) {
|
|---|
| 9 | var x1 = null,
|
|---|
| 10 | defined = constant(true),
|
|---|
| 11 | context = null,
|
|---|
| 12 | curve = curveLinear,
|
|---|
| 13 | output = null,
|
|---|
| 14 | path = withPath(area);
|
|---|
| 15 |
|
|---|
| 16 | x0 = typeof x0 === "function" ? x0 : (x0 === undefined) ? pointX : constant(+x0);
|
|---|
| 17 | y0 = typeof y0 === "function" ? y0 : (y0 === undefined) ? constant(0) : constant(+y0);
|
|---|
| 18 | y1 = typeof y1 === "function" ? y1 : (y1 === undefined) ? pointY : constant(+y1);
|
|---|
| 19 |
|
|---|
| 20 | function area(data) {
|
|---|
| 21 | var i,
|
|---|
| 22 | j,
|
|---|
| 23 | k,
|
|---|
| 24 | n = (data = array(data)).length,
|
|---|
| 25 | d,
|
|---|
| 26 | defined0 = false,
|
|---|
| 27 | buffer,
|
|---|
| 28 | x0z = new Array(n),
|
|---|
| 29 | y0z = new Array(n);
|
|---|
| 30 |
|
|---|
| 31 | if (context == null) output = curve(buffer = path());
|
|---|
| 32 |
|
|---|
| 33 | for (i = 0; i <= n; ++i) {
|
|---|
| 34 | if (!(i < n && defined(d = data[i], i, data)) === defined0) {
|
|---|
| 35 | if (defined0 = !defined0) {
|
|---|
| 36 | j = i;
|
|---|
| 37 | output.areaStart();
|
|---|
| 38 | output.lineStart();
|
|---|
| 39 | } else {
|
|---|
| 40 | output.lineEnd();
|
|---|
| 41 | output.lineStart();
|
|---|
| 42 | for (k = i - 1; k >= j; --k) {
|
|---|
| 43 | output.point(x0z[k], y0z[k]);
|
|---|
| 44 | }
|
|---|
| 45 | output.lineEnd();
|
|---|
| 46 | output.areaEnd();
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 | if (defined0) {
|
|---|
| 50 | x0z[i] = +x0(d, i, data), y0z[i] = +y0(d, i, data);
|
|---|
| 51 | output.point(x1 ? +x1(d, i, data) : x0z[i], y1 ? +y1(d, i, data) : y0z[i]);
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | if (buffer) return output = null, buffer + "" || null;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | function arealine() {
|
|---|
| 59 | return line().defined(defined).curve(curve).context(context);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | area.x = function(_) {
|
|---|
| 63 | return arguments.length ? (x0 = typeof _ === "function" ? _ : constant(+_), x1 = null, area) : x0;
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | area.x0 = function(_) {
|
|---|
| 67 | return arguments.length ? (x0 = typeof _ === "function" ? _ : constant(+_), area) : x0;
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | area.x1 = function(_) {
|
|---|
| 71 | return arguments.length ? (x1 = _ == null ? null : typeof _ === "function" ? _ : constant(+_), area) : x1;
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | area.y = function(_) {
|
|---|
| 75 | return arguments.length ? (y0 = typeof _ === "function" ? _ : constant(+_), y1 = null, area) : y0;
|
|---|
| 76 | };
|
|---|
| 77 |
|
|---|
| 78 | area.y0 = function(_) {
|
|---|
| 79 | return arguments.length ? (y0 = typeof _ === "function" ? _ : constant(+_), area) : y0;
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | area.y1 = function(_) {
|
|---|
| 83 | return arguments.length ? (y1 = _ == null ? null : typeof _ === "function" ? _ : constant(+_), area) : y1;
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | area.lineX0 =
|
|---|
| 87 | area.lineY0 = function() {
|
|---|
| 88 | return arealine().x(x0).y(y0);
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 | area.lineY1 = function() {
|
|---|
| 92 | return arealine().x(x0).y(y1);
|
|---|
| 93 | };
|
|---|
| 94 |
|
|---|
| 95 | area.lineX1 = function() {
|
|---|
| 96 | return arealine().x(x1).y(y0);
|
|---|
| 97 | };
|
|---|
| 98 |
|
|---|
| 99 | area.defined = function(_) {
|
|---|
| 100 | return arguments.length ? (defined = typeof _ === "function" ? _ : constant(!!_), area) : defined;
|
|---|
| 101 | };
|
|---|
| 102 |
|
|---|
| 103 | area.curve = function(_) {
|
|---|
| 104 | return arguments.length ? (curve = _, context != null && (output = curve(context)), area) : curve;
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 | area.context = function(_) {
|
|---|
| 108 | return arguments.length ? (_ == null ? context = output = null : output = curve(context = _), area) : context;
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 | return area;
|
|---|
| 112 | }
|
|---|