-
Notifications
You must be signed in to change notification settings - Fork 312
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
add d3.trail() #168
base: main
Are you sure you want to change the base?
add d3.trail() #168
Changes from 5 commits
b9c0b9c
d8452da
8eb130b
dc9fc0b
6f9c047
c7afae0
fdaa556
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -44,6 +44,7 @@ var line = d3.line(); | |||||
* [Arcs](#arcs) | ||||||
* [Pies](#pies) | ||||||
* [Lines](#lines) | ||||||
* [Trails](#trails) | ||||||
* [Areas](#areas) | ||||||
* [Curves](#curves) | ||||||
* [Custom Curves](#custom-curves) | ||||||
|
@@ -453,6 +454,93 @@ Equivalent to [*line*.curve](#line_curve). Note that [curveMonotoneX](#curveMono | |||||
|
||||||
Equivalent to [*line*.context](#line_context). | ||||||
|
||||||
### Trails | ||||||
|
||||||
[<img width="295" height="160" alt="Trail Chart" src="https://raw.githubusercontent.com/d3/d3-shape/master/img/trail.png">] | ||||||
|
||||||
Trail marks are similar to line marks, but can have variable widths determined by backing data. Trail marks are useful if one wishes to draw lines that change size to reflect the underlying data. | ||||||
|
||||||
<a name="trail" href="#trail">#</a> d3.<b>trail</b>([<i>x</x>][, <i>y</i>][, <i>size</i>]) · [Source](https://github.com/d3/d3-shape/blob/master/src/trail.js) | ||||||
|
||||||
Constructs a new trail generator with default settings. If *x*, *y* or *size* are specified, sets the corresponding accessors to the specified function or number and returns this trail generator. | ||||||
|
||||||
<a name="_trail" href="#_trail">#</a> <i>trail</i>(<i>data</i>) · [Source](https://github.com/d3/d3-shape/blob/master/src/trail.js) | ||||||
|
||||||
Generates a trail for the given array of *data*. If the trail generator has a [context](#trail_context), then the trail is rendered to this context as a sequence of [path method](http://www.w3.org/TR/2dcontext/#canvaspathmethods) calls and this function returns void. Otherwise, a [path data](http://www.w3.org/TR/SVG/paths.html#PathData) string is returned. | ||||||
|
||||||
<a name="trail_x" href="#trail_x">#</a> <i>trail</i>.<b>x</b>([<i>x</i>]) · [Source](https://github.com/d3/d3-shape/blob/master/src/trail.js) | ||||||
|
||||||
If *x* is specified, sets the x accessor to the specified function or number and returns this trail generator. If *x* is not specified, returns the current x accessor, which defaults to: | ||||||
|
||||||
```js | ||||||
function x(d) { | ||||||
return d[0]; | ||||||
} | ||||||
``` | ||||||
|
||||||
When a trail is [generated](#_trail), the x accessor will be invoked for each [defined](#trail_defined) element in the input data array, being passed the element `d`, the index `i`, and the array `data` as three arguments. The default x accessor assumes that the input data are three-element arrays of numbers. If your data are in a different format, or if you wish to transform the data before rendering, then you should specify a custom accessor. For example: | ||||||
|
||||||
```js | ||||||
var data = [ | ||||||
{u: 1, v: 8, size: 40}, | ||||||
{u: 10, v: 35, size: 12}, | ||||||
{u: 19, v: 22, size: 30}, | ||||||
{u: 28, v: 14, size: 16}, | ||||||
{u: 37, v: 16, size: 24}, | ||||||
{u: 46, v: 28, size: 6}, | ||||||
… | ||||||
]; | ||||||
|
||||||
var trail = d3.trail() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
.x(d => x(d.u)) | ||||||
.y(d => y(d.v)) | ||||||
.size(d => size(d.size)); | ||||||
``` | ||||||
|
||||||
<a name="trail_y" href="#trail_y">#</a> <i>trail</i>.<b>y</b>([<i>y</i>]) · [Source](https://github.com/d3/d3-shape/blob/master/src/trail.js) | ||||||
|
||||||
If *y* is specified, sets the y accessor to the specified function or number and returns this trail generator. If *y* is not specified, returns the current y accessor, which defaults to: | ||||||
|
||||||
```js | ||||||
function y(d) { | ||||||
return d[1]; | ||||||
} | ||||||
``` | ||||||
|
||||||
When a trail is [generated](#_trail), the y accessor will be invoked for each [defined](#trail_defined) element in the input data array, being passed the element `d`, the index `i`, and the array `data` as three arguments. The default y accessor assumes that the input data are three-element arrays of numbers. See [*trail*.x](#trail_x) for more information. | ||||||
|
||||||
<a name="trail_size" href="#trail_size">#</a> <i>trail</i>.<b>size</b>([<i>size</i>]) · [Source](https://github.com/d3/d3-shape/blob/master/src/trail.js) | ||||||
|
||||||
The *size* describes the width in pixels of the trail at the given data point, which should be greater than 0. If *size* is specified, sets the size accessor to the specified function or number and returns this trail generator. If *size* is not specified, returns the current size accessor, which defaults to: | ||||||
|
||||||
```js | ||||||
function size(d) { | ||||||
return d[2]; | ||||||
} | ||||||
``` | ||||||
|
||||||
When a trail is [generated](#_trail), the size accessor will be invoked for each [defined](#trail_defined) element in the input data array, being passed the element `d`, the index `i`, and the array `data` as three arguments. The default size accessor assumes that the input data are three-element arrays of numbers. See [*trail*.x](#trail_x) for more information. | ||||||
|
||||||
<a name="trail_defined" href="#trail_defined">#</a> <i>trail</i>.<b>defined</b>([<i>defined</i>]) · [Source](https://github.com/d3/d3-shape/blob/master/src/trail.js) | ||||||
|
||||||
If *defined* is specified, sets the defined accessor to the specified function or boolean and returns this trail generator. If *defined* is not specified, returns the current defined accessor, which defaults to: | ||||||
|
||||||
```js | ||||||
function defined() { | ||||||
return true; | ||||||
} | ||||||
``` | ||||||
|
||||||
The default accessor thus assumes that the input data is always defined. When a trail is [generated](#_trail), the defined accessor will be invoked for each element in the input data array, being passed the element `d`, the index `i`, and the array `data` as three arguments. If the given element is defined (*i.e.*, if the defined accessor returns a truthy value for this element), the [x](#trail_x), [y](#trail_y) and [size](#trail_size) accessors will subsequently be evaluated and the point will be added to the current trail segment. Otherwise, the element will be skipped, the current trail segment will be ended, and a new trail segment will be generated for the next defined point. As a result, the generated trail may have several discrete segments. For example: | ||||||
|
||||||
[<img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/trail-defined.png" width="295" height="160" alt="Trail with Missing Data">] | ||||||
|
||||||
Note that if a trail segment consists of only a single point, it may appear as a circle, which is different from [*line*.defined](#line_defined). | ||||||
|
||||||
<a name="trail_context" href="#trail_context">#</a> <i>trail</i>.<b>context</b>([<i>context</i>]) · [Source](https://github.com/d3/d3-shape/blob/master/src/trail.js) | ||||||
|
||||||
If *context* is specified, sets the context and returns this trail generator. If *context* is not specified, returns the current context, which defaults to null. If the context is not null, then the [generated trail](#_trail) 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 trail is returned. | ||||||
|
||||||
### Areas | ||||||
|
||||||
[<img alt="Area Chart" width="295" height="154" src="https://raw.githubusercontent.com/d3/d3-shape/master/img/area.png">](https://observablehq.com/@d3/area-chart)[<img alt="Stacked Area Chart" width="295" height="154" src="https://raw.githubusercontent.com/d3/d3-shape/master/img/area-stacked.png">](https://observablehq.com/@d3/stacked-area-chart)[<img alt="Difference Chart" width="295" height="154" src="https://raw.githubusercontent.com/d3/d3-shape/master/img/area-difference.png">](https://observablehq.com/@d3/difference-chart) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,7 @@ export function x(p) { | |
export function y(p) { | ||
return p[1]; | ||
} | ||
|
||
export function z(p) { | ||
return p[2]; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,260 @@ | ||||||
import {path} from "d3-path"; | ||||||
import constant from "./constant.js"; | ||||||
import {x as pointX, y as pointY, z as pointSize} from "./point.js"; | ||||||
import {abs, atan2, cos, pi, tau, sin, sqrt, max, min} from "./math.js"; | ||||||
|
||||||
export default function(x, y, size) { | ||||||
var defined = constant(true), | ||||||
context = null, | ||||||
ready, | ||||||
ready2, | ||||||
scaleRatio_min = 1, | ||||||
ct1, | ||||||
px1, py1, pr1, | ||||||
arp = []; | ||||||
|
||||||
x = typeof x === "function" ? x : (x === undefined) ? pointX : constant(x); | ||||||
y = typeof y === "function" ? y : (y === undefined) ? pointY : constant(y); | ||||||
size = typeof size === "function" ? size : (size === undefined) ? pointSize : constant(size); | ||||||
|
||||||
function cross(x1, y1, x2, y2) { | ||||||
return x1 * y2 - x2 * y1; | ||||||
} | ||||||
|
||||||
function isIntersect(x1, y1, x2, y2, x3, y3, x4, y4) { | ||||||
// rapid repulsion | ||||||
if (max(x3, x4) < min(x1, x2) || max(y3, y4) < min(y1, y2) || max(x1, x2) < min(x3, x4) || max(y1, y2) < min(y3, y4)) { | ||||||
return false; | ||||||
} | ||||||
|
||||||
// straddle experiment | ||||||
if (cross(x1 - x4, y1 - y4, x3 - x4, y3 - y4) * cross(x2 - x4, y2 - y4, x3 - x4, y3 - y4) > 0 || | ||||||
cross(x3 - x2, y3 - y2, x1 - x2, y1 - y2) * cross(x4 - x2, y4 - y2, x1 - x2, y1 - y2) > 0) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Alignment reads well. This is a lot for a single expression also. May be worth factoring out into variables. |
||||||
return false; | ||||||
} | ||||||
return true; | ||||||
} | ||||||
|
||||||
function intersectPoint(x1, y1, x2, y2, x3, y3, x4, y4) { | ||||||
var d1 = abs(cross(x4 - x3, y4 - y3, x1 - x3, y1 - y3)), | ||||||
d2 = abs(cross(x4 - x3, y4 - y3, x2 - x3, y2 - y3)), | ||||||
t; | ||||||
|
||||||
if (!(d1 || d2)) { | ||||||
return { | ||||||
x: x2, | ||||||
y: y2 | ||||||
}; | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Seems odd to have else on a new line. |
||||||
else{ | ||||||
t = d1 / (d1 + d2); | ||||||
return { | ||||||
x: x1 + (x2 - x1) * t, | ||||||
y: y1 + (y2 - y1) * t | ||||||
}; | ||||||
} | ||||||
} | ||||||
|
||||||
function commonTangent(x1, y1, w1, x2, y2, w2) { | ||||||
var r1 = w1 / 2, | ||||||
r2 = w2 / 2, | ||||||
alpha, // the smallest angle in right-angled trapezoid | ||||||
alpha_x, | ||||||
alpha_y, | ||||||
beta, // the slope of line segment | ||||||
cos_x, | ||||||
sin_y; | ||||||
|
||||||
alpha_x = abs(r2 - r1); | ||||||
alpha_y = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) - alpha_x * alpha_x); | ||||||
alpha = atan2(alpha_y, alpha_x); | ||||||
beta = atan2(y2 - y1, x2 - x1); | ||||||
|
||||||
if (r1 < r2) { | ||||||
cos_x = -cos(alpha + beta); | ||||||
sin_y = -sin(alpha + beta); | ||||||
|
||||||
return { | ||||||
sg1_x: x1 + r1 * cos_x, | ||||||
sg1_y: y1 + r1 * sin_y, | ||||||
sg2_x: x2 + r2 * cos_x, | ||||||
sg2_y: y2 + r2 * sin_y, | ||||||
angle: alpha + beta - pi | ||||||
}; | ||||||
} | ||||||
else{ | ||||||
cos_x = cos(beta - alpha); | ||||||
sin_y = sin(beta - alpha); | ||||||
|
||||||
return { | ||||||
sg1_x: x1 + r1 * cos_x, | ||||||
sg1_y: y1 + r1 * sin_y, | ||||||
sg2_x: x2 + r2 * cos_x, | ||||||
sg2_y: y2 + r2 * sin_y, | ||||||
angle: beta - alpha | ||||||
}; | ||||||
} | ||||||
} | ||||||
|
||||||
function segment(x1, y1, w1, x2, y2, w2) { | ||||||
var ct2 = commonTangent(x1, y1, w1, x2, y2, w2); | ||||||
|
||||||
if (ready) { | ||||||
if(isIntersect(ct1.sg1_x, ct1.sg1_y, ct1.sg2_x, ct1.sg2_y, ct2.sg1_x, ct2.sg1_y, ct2.sg2_x, ct2.sg2_y)) { | ||||||
var p = intersectPoint(ct1.sg1_x, ct1.sg1_y, ct1.sg2_x, ct1.sg2_y, ct2.sg1_x, ct2.sg1_y, ct2.sg2_x, ct2.sg2_y); | ||||||
context.lineTo(p.x, p.y); | ||||||
} | ||||||
else{ | ||||||
context.lineTo(ct1.sg2_x, ct1.sg2_y); | ||||||
context.arc(x1, y1, w1 / 2, ct1.angle, ct2.angle, 0); | ||||||
} | ||||||
} | ||||||
else{ | ||||||
ready = 1; | ||||||
context.moveTo(ct2.sg1_x, ct2.sg1_y); | ||||||
} | ||||||
ct1 = ct2; | ||||||
} | ||||||
|
||||||
function scaleRatio(px2, py2, pw2, i) { | ||||||
var pr2 = pw2 / 2; | ||||||
|
||||||
if (ready2) { | ||||||
var lxy = sqrt((px1 - px2) * (px1 - px2) + (py1 - py2) * (py1 - py2)); | ||||||
if (lxy > 1) { | ||||||
if (pr1 + pr2 > lxy) { | ||||||
var sr = lxy / (pr1 + pr2); | ||||||
if (sr < scaleRatio_min) { | ||||||
scaleRatio_min = sr; | ||||||
} | ||||||
} | ||||||
arp[i] = false; | ||||||
} | ||||||
else{ | ||||||
arp[i] = true; // when p1.x == p2.x and p1.y == p2.y | ||||||
} | ||||||
} | ||||||
else{ | ||||||
ready2 = 1; | ||||||
arp[i] = false; | ||||||
} | ||||||
px1 = px2; | ||||||
py1 = py2; | ||||||
pr1 = pr2; | ||||||
} | ||||||
|
||||||
function trail(data) { | ||||||
var i, | ||||||
n = data.length, | ||||||
d, | ||||||
def, | ||||||
defined0 = false, | ||||||
defined1 = false, | ||||||
buffer, | ||||||
arx = [], // arrays of x(), y(), size(), defined() | ||||||
ary = [], | ||||||
ars = [], | ||||||
ard = []; | ||||||
|
||||||
// make global optimization for radius when r1 + r2 > lxy | ||||||
for (i = 0; i < n; i++) { | ||||||
def = (ard[i] = defined(d = data[i], i, data)) && (ars[i] = +size(d, i, data)); | ||||||
if (!(i < n && def) === defined1) { | ||||||
if (defined1 = !defined1) ready2 = 0; | ||||||
} | ||||||
if (defined1) { | ||||||
scaleRatio(arx[i] = +x(d, i, data), ary[i] = +y(d, i, data), ars[i], i); | ||||||
} | ||||||
} | ||||||
|
||||||
if (context == null) context = buffer = path(); | ||||||
|
||||||
var start, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps better to lift these declarations to the top, with the others. |
||||||
j, | ||||||
d1, | ||||||
def1, | ||||||
tag = false; | ||||||
|
||||||
for (i = 0; i < n; i++) { | ||||||
def = ard[i] && ars[i]; | ||||||
if (!(i < n && def) === defined0) { | ||||||
if (defined0 = !defined0) { | ||||||
ready = 0; | ||||||
start = i; | ||||||
} | ||||||
} | ||||||
if (defined0) { | ||||||
j = i + 1; | ||||||
if (j < n) { | ||||||
def1 = ard[j] && ars[j]; | ||||||
if (def1) { | ||||||
if (arp[j]) { | ||||||
tag = true; | ||||||
} | ||||||
else{ | ||||||
segment(arx[i], ary[i], ars[i] * scaleRatio_min, arx[j], ary[j], ars[j] * scaleRatio_min); | ||||||
} | ||||||
} | ||||||
else{ | ||||||
tag = true; | ||||||
} | ||||||
} | ||||||
else{ | ||||||
tag = true; | ||||||
} | ||||||
if (tag) { | ||||||
j = i - 1; | ||||||
if (j < start) { | ||||||
// draw a circle | ||||||
context.moveTo(arx[i] + ars[i] * scaleRatio_min / 2, ary[i]); | ||||||
context.arc(arx[i], ary[i], ars[i] * scaleRatio_min / 2, 0, tau, 0); | ||||||
context.closePath(); | ||||||
} | ||||||
else{ | ||||||
while(j >= start) { | ||||||
d1 = data[j]; | ||||||
segment(arx[j + 1], ary[j + 1], ars[j + 1] * scaleRatio_min, arx[j], ary[j], ars[j] * scaleRatio_min); | ||||||
d = d1; | ||||||
j -= 1; | ||||||
} | ||||||
j += 1; | ||||||
d1 = data[j + 1]; | ||||||
segment(arx[j], ary[j], ars[j] * scaleRatio_min, arx[j + 1], ary[j + 1], ars[j + 1] * scaleRatio_min); | ||||||
|
||||||
context.closePath(); | ||||||
} | ||||||
tag = false; | ||||||
ready = 0; | ||||||
start = i + 1; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
if (buffer) { | ||||||
context = null; | ||||||
return buffer + '' || null; | ||||||
} | ||||||
} | ||||||
|
||||||
trail.x = function(_) { | ||||||
return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), trail) : x; | ||||||
}; | ||||||
|
||||||
trail.y = function(_) { | ||||||
return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), trail) : y; | ||||||
}; | ||||||
|
||||||
trail.size = function(_) { | ||||||
return arguments.length ? (size = typeof _ === "function" ? _ : constant(+_), trail) : size; | ||||||
}; | ||||||
|
||||||
trail.defined = function(_) { | ||||||
return arguments.length ? (defined = typeof _ === "function" ? _ : constant(!!_), trail) : defined; | ||||||
}; | ||||||
|
||||||
trail.context = function(_) { | ||||||
return arguments.length ? ((context = _ == null ? null : _), trail) : context; | ||||||
}; | ||||||
|
||||||
return trail; | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.