diff --git a/README.md b/README.md index 754df39..ec0ecf4 100644 --- a/README.md +++ b/README.md @@ -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 + +[Trail Chart] + +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. + +# d3.trail([x][, y][, size]) · [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. + +# trail(data) · [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. + +# trail.x([x]) · [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 +const 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}, + … +]; + +const trail = d3.trail() + .x(d => x(d.u)) + .y(d => y(d.v)) + .size(d => size(d.size)); +``` + +# trail.y([y]) · [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. + +# trail.size([size]) · [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. + +# trail.defined([defined]) · [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: + +[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). + +# trail.context([context]) · [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 [Area Chart](https://observablehq.com/@d3/area-chart)[Stacked Area Chart](https://observablehq.com/@d3/stacked-area-chart)[Difference Chart](https://observablehq.com/@d3/difference-chart) diff --git a/img/trail-defined.png b/img/trail-defined.png new file mode 100644 index 0000000..187d44f Binary files /dev/null and b/img/trail-defined.png differ diff --git a/img/trail.png b/img/trail.png new file mode 100644 index 0000000..2179415 Binary files /dev/null and b/img/trail.png differ diff --git a/src/index.js b/src/index.js index aca91a5..c28e861 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ export {default as arc} from "./arc.js"; export {default as area} from "./area.js"; export {default as line} from "./line.js"; +export {default as trail} from "./trail.js"; export {default as pie} from "./pie.js"; export {default as areaRadial, default as radialArea} from "./areaRadial.js"; // Note: radialArea is deprecated! export {default as lineRadial, default as radialLine} from "./lineRadial.js"; // Note: radialLine is deprecated! diff --git a/src/point.js b/src/point.js index c345257..998e565 100644 --- a/src/point.js +++ b/src/point.js @@ -5,3 +5,7 @@ export function x(p) { export function y(p) { return p[1]; } + +export function z(p) { + return p[2]; +} diff --git a/src/trail.js b/src/trail.js new file mode 100644 index 0000000..efaad32 --- /dev/null +++ b/src/trail.js @@ -0,0 +1,250 @@ +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, ct2, + px1, py1, pr1, + arp = [], + p; + + 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. + 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 { + 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 + }; + } + + 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) { + 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)) { + 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; // if p1 is too close to p2. + } + } 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 = []; + + if (context == null) context = buffer = path(); + + var start, + j, + d1, + def1, + tag = false; + + // make global optimization for radius when r1 + r2 > lxy and mark the points whose positions are coincided. + 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); + } + } + + 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; +} diff --git a/test/trail-test.js b/test/trail-test.js new file mode 100644 index 0000000..3f3229b --- /dev/null +++ b/test/trail-test.js @@ -0,0 +1,98 @@ +var tape = require("tape"), + shape = require("../"); + +require("./pathEqual"); + +tape("trail() returns a default trail shape", function(test) { + var t = shape.trail(); + test.equal(t.x()([10, 20, 30]), 10); + test.equal(t.y()([10, 20, 30]), 20); + test.equal(t.size()([10, 20, 30]), 30); + test.equal(t.defined()([10, 20, 30]), true); + test.equal(t.context(), null); + test.pathEqual(t([[10, 10, 20], [50, 50, 50], [90, 90, 20]]), "M14.942945,1.307055L62.357363,28.267637A25,25,0,0,1,71.732363,37.642637L98.692945,85.057055A10,10,0,0,1,85.057055,98.692945L37.642637,71.732363A25,25,0,0,1,28.267637,62.357363L1.307055,14.942945A10,10,0,0,1,14.942945,1.307055Z"); + test.end(); +}); + +tape("trail(x, y, size) sets x, y and size", function(test) { + var x = function() {}, + y = function() {}, + size = function() {}; + test.equal(shape.trail(x).x(), x); + test.equal(shape.trail(x, y).y(), y); + test.equal(shape.trail(x, y, size).size(), size); + test.equal(shape.trail(1, 2, 3).x()("aa"), 1); + test.equal(shape.trail(1, 2, 3).y()("aa"), 2); + test.equal(shape.trail(1, 2, 3).size()("aa"), 3); + test.end(); +}); + +tape("trail.x(f)(data) passes d, i, and data to the specified function f", function(test) { + var data = [[1, 2, 3], [4, 5, 6]], actual = []; + shape.trail().x(function() { actual.push([].slice.call(arguments)); })(data); + test.deepEqual(actual, [[[1, 2, 3], 0, data], [[4, 5, 6], 1, data]]); + test.end(); +}); + +tape("trail.y(f)(data) passes d, i and data to the specified function f", function(test) { + var data = [[1, 2, 3], [4, 5, 6]], actual = []; + shape.trail().y(function() { actual.push([].slice.call(arguments)); })(data); + test.deepEqual(actual, [[[1, 2, 3], 0, data], [[4, 5, 6], 1, data]]); + test.end(); +}); + +tape("trail.size(f)(data) passes d, i and data to the specified function f", function(test) { + var data = ["a", "b"], actual = []; + shape.trail().size(function() { actual.push([].slice.call(arguments)); })(data); + test.deepEqual(actual, [['a', 0, data], ['b', 1, data]]); + test.end(); +}); + +tape("trail.defined(f)(data) passes d, i and data to the specified function f", function(test) { + var data = ["a", "b"], actual = []; + shape.trail().defined(function() { actual.push([].slice.call(arguments)); })(data); + test.deepEqual(actual, [["a", 0, data], ["b", 1, data]]); + test.end(); +}); + +tape("trail.x(x)(data) observes the specified function", function(test) { + var t = shape.trail().x(function(d) { return d.x; }); + test.pathEqual(t([{x: 10, 1: 10, 2: 20}, {x: 50, 1: 50, 2: 50}, {x: 90, 1: 90, 2: 20}]), "M14.942945,1.307055L62.357363,28.267637A25,25,0,0,1,71.732363,37.642637L98.692945,85.057055A10,10,0,0,1,85.057055,98.692945L37.642637,71.732363A25,25,0,0,1,28.267637,62.357363L1.307055,14.942945A10,10,0,0,1,14.942945,1.307055Z"); + test.end(); +}); + +tape("trail.x(x)(data) observes the specified constant", function(test) { + var t = shape.trail().x(0); + test.pathEqual(t([{1: 10, 2: 20}, {1: 50, 2: 50}, {1: 90, 2: 20}]), "M9.270248,6.250000L23.175620,40.625000A25,25,0,0,1,23.175620,59.375000L9.270248,93.750000A10,10,0,0,1,-9.270248,93.750000L-23.175620,59.375000A25,25,0,0,1,-23.175620,40.625000L-9.270248,6.250000A10,10,0,0,1,9.270248,6.250000Z"); + test.end(); +}); + +tape("trail.y(x)(data) observes the specified function", function(test) { + var t = shape.trail().y(function(d) { return d.y; }); + test.pathEqual(t([{0: 10, y: 10, 2: 20}, {0: 50, y: 50, 2: 50}, {0: 90, y: 90, 2: 20}]), "M14.942945,1.307055L62.357363,28.267637A25,25,0,0,1,71.732363,37.642637L98.692945,85.057055A10,10,0,0,1,85.057055,98.692945L37.642637,71.732363A25,25,0,0,1,28.267637,62.357363L1.307055,14.942945A10,10,0,0,1,14.942945,1.307055Z"); + test.end(); +}); + +tape("trail.y(x)(data) observes the specified constant", function(test) { + var t = shape.trail().y(0); + test.pathEqual(t([{0: 10, 2: 20}, {0: 50, 2: 50}, {0: 90, 2: 20}]), "M6.250000,-9.270248L40.625000,-23.175620A25,25,0,0,1,59.375000,-23.175620L93.750000,-9.270248A10,10,0,0,1,93.750000,9.270248L59.375000,23.175620A25,25,0,0,1,40.625000,23.175620L6.250000,9.270248A10,10,0,0,1,6.250000,-9.270248Z"); + test.end(); +}); + +tape("trail.size(x)(data) observes the specified function", function(test) { + var t = shape.trail().size(function(d) { return d.size; }); + test.pathEqual(t([{0: 10, 1: 10, size: 20}, {0: 50, 1: 50, size: 50}, {0: 90, 1: 90, size: 20}]), "M14.942945,1.307055L62.357363,28.267637A25,25,0,0,1,71.732363,37.642637L98.692945,85.057055A10,10,0,0,1,85.057055,98.692945L37.642637,71.732363A25,25,0,0,1,28.267637,62.357363L1.307055,14.942945A10,10,0,0,1,14.942945,1.307055Z"); + test.end(); +}); + +tape("trail.size(x)(data) observes the specified constant", function(test) { + var t = shape.trail().size(20); + test.pathEqual(t([{0: 10, 1: 10}, {0: 50, 1: 50}, {0: 90, 1: 10}]), "M17.071068,2.928932L50,35.857864L82.928932,2.928932A10,10,0,1,1,97.071068,17.071068L57.071068,57.071068A10,10,0,0,1,42.928932,57.071068L2.928932,17.071068A10,10,0,1,1,17.071068,2.928932Z"); + test.end(); +}); + +tape("test about coincident points", function(test) { + var t = shape.trail(); + test.pathEqual(t([[0, 0, 10], [0, 0, 20]]), "M5,0A5,5,0,1,1,-5,0A5,5,0,1,1,5,0ZM10,0A10,10,0,1,1,-10,0A10,10,0,1,1,10,0Z"); + test.end(); +});