-
Notifications
You must be signed in to change notification settings - Fork 310
add d3.trail() #168
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
Open
wyhleonard
wants to merge
7
commits into
d3:main
Choose a base branch
from
wyhleonard:trailMarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add d3.trail() #168
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9c0b9c
add d3.trail()
wyhleonard d8452da
perfect d3.trail()
wyhleonard 8eb130b
solve the two small issues
wyhleonard dc9fc0b
change-requested
wyhleonard 6f9c047
cleaner merge
wyhleonard c7afae0
the sixth commit
wyhleonard fdaa556
refine codes and docs
wyhleonard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [ | ||||||
|
Contributor
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
|
||||||
| {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() | ||||||
|
Contributor
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(function(d) { return x(d.u*10); }) | ||||||
| .y(function(d) { return y(d.v*10); }) | ||||||
| .size(function(d) { return size(d.size); }); | ||||||
|
wyhleonard marked this conversation as resolved.
Outdated
|
||||||
| ``` | ||||||
|
|
||||||
| <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) | ||||||
|
|
||||||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| 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), | ||
|
wyhleonard marked this conversation as resolved.
Outdated
|
||
| context = null, | ||
| ready, | ||
| ready2, | ||
| scaleRatio_min = 1, | ||
| ct1, | ||
| px1, py1, pr1; | ||
|
|
||
| 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 crossProduct(x1, y1, x2, y2){ | ||
| return x1*y2 - x2*y1; | ||
| } | ||
|
wyhleonard marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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(crossProduct(x1 - x4, y1 - y4, x3 - x4, y3 - y4)*crossProduct(x2 - x4, y2 - y4, x3 - x4, y3 - y4) > 0 || | ||
| crossProduct(x3 - x2, y3 - y2, x1 - x2, y1 - y2)*crossProduct(x4 - x2, y4 - y2, x1 - x2, y1 - y2) > 0){ | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function intersectPoint(x1, y1, x2, y2, x3, y3, x4, y4){ | ||
| var d1 = abs(crossProduct(x4 - x3, y4 - y3, x1 - x3, y1 - y3)), | ||
| d2 = abs(crossProduct(x4 - x3, y4 - y3, x2 - x3, y2 - y3)), | ||
| t = d1 / (d1 + d2); | ||
|
wyhleonard marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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){ | ||
| var pr2 = pw2 / 2; | ||
|
|
||
| if(ready2){ | ||
| var lxy = sqrt((px1 - px2)*(px1 - px2) + (py1 - py2)*(py1 - py2)); | ||
| if(pr1 + pr2 > lxy){ | ||
| var sr = lxy / (pr1 + pr2); | ||
| if(sr < scaleRatio_min){ | ||
| scaleRatio_min = sr; | ||
| } | ||
| } | ||
| } | ||
| else{ | ||
| ready2 = 1; | ||
| } | ||
| px1 = px2; | ||
| py1 = py2; | ||
| pr1 = pr2; | ||
| } | ||
|
|
||
| function trail(data){ | ||
| var i, | ||
| n = data.length, | ||
| d, | ||
| def, | ||
| s, | ||
| defined0 = false, | ||
| defined1 = false, | ||
| buffer; | ||
|
|
||
| //make global optimization for radius when r1 + r2 > lxy | ||
| for(i = 0; i < n; i++){ | ||
| def = defined(d = data[i], i, data) && (s = +size(d, i, data)); | ||
| if(!(i < n && def) === defined1){ | ||
| if (defined1 = !defined1) ready2 = 0; | ||
| } | ||
| if(defined1) { | ||
| scaleRatio(+x(d, i, data), +y(d, i, data), s); | ||
| } | ||
| } | ||
|
|
||
| if(context == null) context = buffer = path(); | ||
|
|
||
| var start, | ||
| j, | ||
| d1, | ||
| def1, | ||
| s1, | ||
| tag = false; | ||
|
|
||
| for(i = 0; i < n; i++){ | ||
| def = defined(d = data[i], i, data) && (s = +size(d, i, data)); | ||
| if(!(i < n && def) === defined0){ | ||
| if(defined0 = !defined0){ | ||
| ready = 0; | ||
| start = i; | ||
| } | ||
| } | ||
| if(defined0){ | ||
| j = i + 1; | ||
| if(j < n){ | ||
| def1 = defined(d1 = data[j], j, data) && (s1 = +size(d1, j, data)); | ||
| if(def1){ | ||
| segment(+x(d, i, data), +y(d, i, data), s*scaleRatio_min, +x(d1, j, data), +y(d1, j, data), s1*scaleRatio_min); | ||
| } | ||
| else{ | ||
| tag = true; | ||
| } | ||
| } | ||
| else{ | ||
| tag = true; | ||
| } | ||
| if(tag){ | ||
| j = i - 1; | ||
| if(j < start){ | ||
| var tx = +x(d, i, data), | ||
| ty = +y(d, i, data), | ||
| ts = +size(d, i, data); | ||
|
|
||
| context.moveTo(tx + ts*scaleRatio_min / 2, ty); | ||
| context.arc(tx, ty, ts*scaleRatio_min / 2, 0, tau, 0); | ||
| context.closePath(); | ||
| } | ||
| else{ | ||
| while(j >= start){ | ||
| d1 = data[j]; | ||
| segment(+x(d, j + 1, data), +y(d, j + 1, data), +size(d, j + 1, data)*scaleRatio_min, +x(d1, j, data), +y(d1, j, data), +size(d1, j, data)*scaleRatio_min); | ||
| d = d1; | ||
| j -= 1; | ||
| } | ||
| j += 1; | ||
| d1 = data[j + 1]; | ||
| segment(+x(d, j, data), +y(d, j, data), +size(d, j, data)*scaleRatio_min, +x(d1, j + 1, data), +y(d1, j + 1, data), +size(d1, j + 1, data)*scaleRatio_min); | ||
|
|
||
| context.closePath(); | ||
| } | ||
| tag = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if(buffer){ | ||
| context = null; | ||
| return buffer + '' || null; | ||
|
wyhleonard marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.