-
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
base: main
Are you sure you want to change the base?
add d3.trail() #168
Changes from 4 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() | ||||||
|
||||||
| var trail = d3.trail() | |
| const trail = d3.trail() |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,261 @@ | ||||||
| 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){ | ||||||
| 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 | ||||||
| }; | ||||||
| } | ||||||
|
||||||
| } | |
| } else { |
Seems odd to have else on a new line.
Outdated
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.
Perhaps better to lift these declarations to the top, with the others.
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.