Skip to content

Commit 922fb2d

Browse files
committed
Add d3.link{Vertical,Horizontal}. Related #27.
1 parent c2e7090 commit 922fb2d

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

README.md

+82
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var line = d3.line();
4848
* [Areas](#areas)
4949
* [Curves](#curves)
5050
* [Custom Curves](#custom-curves)
51+
* [Links](#links)
5152
* [Symbols](#symbols)
5253
* [Custom Symbol Types](#custom-symbol-types)
5354
* [Stacks](#stacks)
@@ -797,6 +798,87 @@ Indicates the end of the current line segment.
797798

798799
Indicates a new point in the current line segment with the given *x*- and *y*-values.
799800

801+
### Links
802+
803+
[<img alt="Tidy Tree" src="https://raw.githubusercontent.com/d3/d3-hierarchy/master/img/tree.png">](http://bl.ocks.org/mbostock/9d0899acb5d3b8d839d9d613a9e1fe04)
804+
805+
The **link** shape generates a smooth cubic Bézier curve from a source point to a target point. The tangents of the curve at the start and end are either [vertical](#linkVertical) or [horizontal](#linkHorizontal).
806+
807+
<a name="linkVertical" href="#linkVertical">#</a> d3.<b>linkVertical</b>() [<>](https://github.com/d3/d3-shape/blob/master/src/link/index.js#59 "Source")
808+
809+
Returns a new [link generator](#_link) with vertical tangents. For example, to visualize [links](https://github.com/d3/d3-hierarchy/blob/master/README.md#node_links) in a [tree diagram](https://github.com/d3/d3-hierarchy/blob/master/README.md#tree) rooted on the top edge of the display, you might say:
810+
811+
```js
812+
var link = d3.linkVertical()
813+
.x(function(d) { return d.x; })
814+
.y(function(d) { return d.y; });
815+
```
816+
817+
<a name="linkHorizontal" href="#linkHorizontal">#</a> d3.<b>linkHorizontal</b>() [<>](https://github.com/d3/d3-shape/blob/master/src/link/index.js#63 "Source")
818+
819+
Returns a new [link generator](#_link) with horizontal tangents. For example, to visualize [links](https://github.com/d3/d3-hierarchy/blob/master/README.md#node_links) in a [tree diagram](https://github.com/d3/d3-hierarchy/blob/master/README.md#tree) rooted on the left edge of the display, you might say:
820+
821+
```js
822+
var link = d3.linkHorizontal()
823+
.x(function(d) { return d.y; })
824+
.y(function(d) { return d.x; });
825+
```
826+
827+
<a href="#_link" name="_link">#</a> <i>link</i>(<i>arguments…</i>) [<>](https://github.com/d3/d3-shape/blob/master/src/link/index.js#L20 "Source")
828+
829+
Generates a link for the given *arguments*. The *arguments* are arbitrary; they are simply propagated to the link generator’s accessor functions along with the `this` object. For example, with the default settings, an object expected:
830+
831+
```js
832+
link({
833+
source: [100, 100],
834+
target: [300, 300]
835+
});
836+
```
837+
838+
<a name="link_source" href="#link_source">#</a> <i>link</i>.<b>source</b>([<i>source</i>]) [<>](https://github.com/d3/d3-shape/blob/master/src/link/index.js#36 "Source")
839+
840+
If *source* is specified, sets the source accessor to the specified function and returns this link generator. If *source* is not specified, returns the current source accessor, which defaults to:
841+
842+
```js
843+
function source(d) {
844+
return d.source;
845+
}
846+
```
847+
848+
<a name="link_target" href="#link_target">#</a> <i>link</i>.<b>target</b>([<i>target</i>]) [<>](https://github.com/d3/d3-shape/blob/master/src/link/index.js#40 "Source")
849+
850+
If *target* is specified, sets the target accessor to the specified function and returns this link generator. If *target* is not specified, returns the current target accessor, which defaults to:
851+
852+
```js
853+
function target(d) {
854+
return d.target;
855+
}
856+
```
857+
858+
<a name="link_x" href="#link_x">#</a> <i>link</i>.<b>x</b>([<i>x</i>]) [<>](https://github.com/d3/d3-shape/blob/master/src/link/index.js#L44 "Source")
859+
860+
If *x* is specified, sets the *x*-accessor to the specified function or number and returns this link generator. If *x* is not specified, returns the current *x*-accessor, which defaults to:
861+
862+
```js
863+
function x(d) {
864+
return d[0];
865+
}
866+
```
867+
868+
<a name="link_x" href="#link_x">#</a> <i>link</i>.<b>y</b>([<i>y</i>]) [<>](https://github.com/d3/d3-shape/blob/master/src/link/index.js#L44 "Source")
869+
870+
If *y* is specified, sets the *y*-accessor to the specified function or number and returns this link generator. If *y* is not specified, returns the current *y*-accessor, which defaults to:
871+
872+
```js
873+
function y(d) {
874+
return d[1];
875+
}
876+
```
877+
878+
<a name="link_context" href="#link_context">#</a> <i>link</i>.<b>context</b>([<i>context</i>]) [<>](https://github.com/d3/d3-shape/blob/master/src/link/index.js#L52 "Source")
879+
880+
If *context* is specified, sets the context and returns this link generator. If *context* is not specified, returns the current context, which defaults to null. If the context is not null, then the [generated link](#_link) is rendered to this context as a sequence of [path method](http://www.w3.org/TR/2dcontext/#canvaspathmethods) calls. Otherwise, a [path data](http://www.w3.org/TR/SVG/paths.html#PathData) string representing the generated link is returned. See also [d3-path](https://github.com/d3/d3-path).
881+
800882
### Symbols
801883

802884
<a href="#symbolCircle"><img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/circle.png" width="100" height="100"></a><a href="#symbolCross"><img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/cross.png" width="100" height="100"></a><a href="#symbolDiamond"><img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/diamond.png" width="100" height="100"></a><a href="#symbolSquare"><img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/square.png" width="100" height="100"></a><a href="#symbolStar"><img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/star.png" width="100" height="100"></a><a href="#symbolTriangle"><img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/triangle.png" width="100" height="100"><a href="#symbolWye"><img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/wye.png" width="100" height="100"></a>

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export {default as line} from "./src/line";
44
export {default as pie} from "./src/pie";
55
export {default as radialArea} from "./src/radialArea";
66
export {default as radialLine} from "./src/radialLine";
7+
export {linkHorizontal, linkVertical} from "./src/link/index";
78

89
export {default as symbol, symbols} from "./src/symbol";
910
export {default as symbolCircle} from "./src/symbol/circle";

src/link/index.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {path} from "d3-path";
2+
import {slice} from "../array";
3+
import constant from "../constant";
4+
import {x as pointX, y as pointY} from "../point";
5+
6+
function linkSource(d) {
7+
return d.source;
8+
}
9+
10+
function linkTarget(d) {
11+
return d.target;
12+
}
13+
14+
function link(horizontal) {
15+
var source = linkSource,
16+
target = linkTarget,
17+
x = pointX,
18+
y = pointY,
19+
context = null;
20+
21+
function link() {
22+
var buffer,
23+
argv = slice.call(arguments),
24+
s = source.apply(this, argv),
25+
t = target.apply(this, argv),
26+
x0 = +x.apply(this, (argv[0] = s, argv)),
27+
y0 = +y.apply(this, argv),
28+
x1 = +x.apply(this, (argv[0] = t, argv)),
29+
y1 = +y.apply(this, argv);
30+
if (!context) context = buffer = path();
31+
context.moveTo(x0, y0);
32+
if (horizontal) context.bezierCurveTo((x0 + x1) / 2, y0, (x0 + x1) / 2, y1, x1, y1);
33+
else context.bezierCurveTo(x0, (y0 + y1) / 2, x1, (y0 + y1) / 2, x1, y1);
34+
if (buffer) return context = null, buffer + "" || null;
35+
}
36+
37+
link.source = function(_) {
38+
return arguments.length ? (source = _, link) : source;
39+
};
40+
41+
link.target = function(_) {
42+
return arguments.length ? (target = _, link) : target;
43+
};
44+
45+
link.x = function(_) {
46+
return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), link) : x;
47+
};
48+
49+
link.y = function(_) {
50+
return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), link) : y;
51+
};
52+
53+
link.context = function(_) {
54+
return arguments.length ? ((context = _ == null ? null : _), link) : context;
55+
};
56+
57+
return link;
58+
}
59+
60+
export function linkHorizontal() {
61+
return link(true);
62+
}
63+
64+
export function linkVertical() {
65+
return link(false);
66+
}

0 commit comments

Comments
 (0)