Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept iterables. #153

Merged
merged 18 commits into from
Aug 19, 2020
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ The pad angle here means the angular separation between each adjacent arc. The t

The line generator produces a [spline](https://en.wikipedia.org/wiki/Spline_\(mathematics\)) or [polyline](https://en.wikipedia.org/wiki/Polygonal_chain), as in a line chart. Lines also appear in many other visualization types, such as the links in [hierarchical edge bundling](https://observablehq.com/@d3/hierarchical-edge-bundling).

<a name="line" href="#line">#</a> d3.<b>line</b>() · [Source](https://github.com/d3/d3-shape/blob/master/src/line.js), [Examples](https://observablehq.com/@d3/d3-line)
<a name="line" href="#line">#</a> d3.<b>line</b>([<i>x</i>][, <i>y</i>]) · [Source](https://github.com/d3/d3-shape/blob/master/src/line.js), [Examples](https://observablehq.com/@d3/d3-line)

Constructs a new line generator with the default settings.
Constructs a new line generator with the default settings. If *x* or *y* are specified, sets the corresponding accessors to the specified function or number and returns this line generator.

<a name="_line" href="#_line">#</a> <i>line</i>(<i>data</i>) · [Source](https://github.com/d3/d3-shape/blob/master/src/line.js), [Examples](https://observablehq.com/@d3/d3-line)

Expand Down Expand Up @@ -459,9 +459,9 @@ Equivalent to [*line*.context](#line_context).

The area generator produces an area, as in an area chart. An area is defined by two bounding [lines](#lines), either splines or polylines. Typically, the two lines share the same [*x*-values](#area_x) ([x0](#area_x0) = [x1](#area_x1)), differing only in *y*-value ([y0](#area_y0) and [y1](#area_y1)); most commonly, y0 is defined as a constant representing [zero](http://www.vox.com/2015/11/19/9758062/y-axis-zero-chart). The first line (the <i>topline</i>) is defined by x1 and y1 and is rendered first; the second line (the <i>baseline</i>) is defined by x0 and y0 and is rendered second, with the points in reverse order. With a [curveLinear](#curveLinear) [curve](#area_curve), this produces a clockwise polygon.

<a name="area" href="#area">#</a> d3.<b>area</b>() · [Source](https://github.com/d3/d3-shape/blob/master/src/area.js)
<a name="area" href="#area">#</a> d3.<b>area</b>([<i>x</i>][, <i>y0</i>][, <i>y1</i>]) · [Source](https://github.com/d3/d3-shape/blob/master/src/area.js)

Constructs a new area generator with the default settings.
Constructs a new area generator with the default settings. If *x*, *y0* or *y1* are specified, sets the corresponding accessors to the specified function or number and returns this area generator.

<a name="_area" href="#_area">#</a> <i>area</i>(<i>data</i>) · [Source](https://github.com/d3/d3-shape/blob/master/src/area.js)

Expand Down Expand Up @@ -495,8 +495,8 @@ var data = [
];

var area = d3.area()
.x(function(d) { return x(d.date); })
.y1(function(d) { return y(d.value); })
.x(d => x(d.date))
.y1(d => y(d.value))
.y0(y(0));
```

Expand Down Expand Up @@ -637,9 +637,7 @@ While [lines](#lines) are defined as a sequence of two-dimensional [*x*, *y*] po
Curves are typically not constructed or used directly, instead being passed to [*line*.curve](#line_curve) and [*area*.curve](#area_curve). For example:

```js
var line = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); })
var line = d3.line(d => d.date, d => d.value)
.curve(d3.curveCatmullRom.alpha(0.5));
```

Expand Down
11 changes: 6 additions & 5 deletions src/area.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import curveLinear from "./curve/linear.js";
import line from "./line.js";
import {x as pointX, y as pointY} from "./point.js";

export default function() {
var x0 = pointX,
x1 = null,
y0 = constant(0),
y1 = pointY,
export default function(x0, y0, y1) {
var x1 = null,
defined = constant(true),
context = null,
curve = curveLinear,
output = null;

x0 = typeof x0 === "function" ? x0 : (x0 === undefined) ? pointX : constant(+x0);
y0 = typeof y0 === "function" ? y0 : (y0 === undefined) ? constant(0) : constant(+y0);
y1 = typeof y1 === "function" ? y1 : (y1 === undefined) ? pointY : constant(+y1);

function area(data) {
var i,
j,
Expand Down
9 changes: 5 additions & 4 deletions src/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import constant from "./constant.js";
import curveLinear from "./curve/linear.js";
import {x as pointX, y as pointY} from "./point.js";

export default function() {
var x = pointX,
y = pointY,
defined = constant(true),
export default function(x, y) {
var defined = constant(true),
context = null,
curve = curveLinear,
output = null;

x = typeof x === "function" ? x : (x === undefined) ? pointX : constant(x);
y = typeof y === "function" ? y : (y === undefined) ? pointY : constant(y);

function line(data) {
var i,
n = (data = array(data)).length,
Expand Down
11 changes: 11 additions & 0 deletions test/area-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ tape("area() returns a default area shape", function(test) {
test.end();
});

tape("area(x, y0, y1) sets x0, y0 and y1", function(test) {
var x = function() {}, y = function() {};
test.equal(shape.area(x).x0(), x);
test.equal(shape.area(x, y).y0(), y);
test.equal(shape.area(x, 0, y).y1(), y);
test.equal(shape.area(3, 2, 1).x0()("aa"), 3);
test.equal(shape.area(3, 2, 1).y0()("aa"), 2);
test.equal(shape.area(3, 2, 1).y1()("aa"), 1);
test.end();
});

tape("area.x(f)(data) passes d, i and data to the specified function f", function(test) {
var data = ["a", "b"], actual = [];
shape.area().x(function() { actual.push([].slice.call(arguments)); })(data);
Expand Down
9 changes: 9 additions & 0 deletions test/line-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ tape("line() returns a default line shape", function(test) {
test.end();
});

tape.only("line(x, y) sets x and y", function(test) {
var x = function() {}, y = function() {};
test.equal(shape.line(x).x(), x);
test.equal(shape.line(x, y).y(), y);
test.equal(shape.line(3, 2).x()("aa"), 3);
test.equal(shape.line(3, 2).y()("aa"), 2);
test.end();
});

tape("line.x(f)(data) passes d, i and data to the specified function f", function(test) {
var data = ["a", "b"], actual = [];
shape.line().x(function() { actual.push([].slice.call(arguments)); })(data);
Expand Down