| 1 | import array from "./array.js";
|
|---|
| 2 | import constant from "./constant.js";
|
|---|
| 3 | import curveLinear from "./curve/linear.js";
|
|---|
| 4 | import {withPath} from "./path.js";
|
|---|
| 5 | import {x as pointX, y as pointY} from "./point.js";
|
|---|
| 6 |
|
|---|
| 7 | export default function(x, y) {
|
|---|
| 8 | var defined = constant(true),
|
|---|
| 9 | context = null,
|
|---|
| 10 | curve = curveLinear,
|
|---|
| 11 | output = null,
|
|---|
| 12 | path = withPath(line);
|
|---|
| 13 |
|
|---|
| 14 | x = typeof x === "function" ? x : (x === undefined) ? pointX : constant(x);
|
|---|
| 15 | y = typeof y === "function" ? y : (y === undefined) ? pointY : constant(y);
|
|---|
| 16 |
|
|---|
| 17 | function line(data) {
|
|---|
| 18 | var i,
|
|---|
| 19 | n = (data = array(data)).length,
|
|---|
| 20 | d,
|
|---|
| 21 | defined0 = false,
|
|---|
| 22 | buffer;
|
|---|
| 23 |
|
|---|
| 24 | if (context == null) output = curve(buffer = path());
|
|---|
| 25 |
|
|---|
| 26 | for (i = 0; i <= n; ++i) {
|
|---|
| 27 | if (!(i < n && defined(d = data[i], i, data)) === defined0) {
|
|---|
| 28 | if (defined0 = !defined0) output.lineStart();
|
|---|
| 29 | else output.lineEnd();
|
|---|
| 30 | }
|
|---|
| 31 | if (defined0) output.point(+x(d, i, data), +y(d, i, data));
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | if (buffer) return output = null, buffer + "" || null;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | line.x = function(_) {
|
|---|
| 38 | return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), line) : x;
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | line.y = function(_) {
|
|---|
| 42 | return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), line) : y;
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | line.defined = function(_) {
|
|---|
| 46 | return arguments.length ? (defined = typeof _ === "function" ? _ : constant(!!_), line) : defined;
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | line.curve = function(_) {
|
|---|
| 50 | return arguments.length ? (curve = _, context != null && (output = curve(context)), line) : curve;
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 | line.context = function(_) {
|
|---|
| 54 | return arguments.length ? (_ == null ? context = output = null : output = curve(context = _), line) : context;
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | return line;
|
|---|
| 58 | }
|
|---|