From e3b1b0ae89259c971cd24a279950c838c439c725 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sat, 21 May 2016 13:34:47 -0700 Subject: [PATCH] Simpler symmetry for step curves. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We don’t need to check the ordering of x; we can simply reverse the behavior for the topline and baseline using the curve API. --- README.md | 4 ++-- src/curve/step.js | 10 +++------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8906cce..395efe3 100644 --- a/README.md +++ b/README.md @@ -739,13 +739,13 @@ Produces a piecewise constant function (a [step function](https://en.wikipedia.o stepAfter -Produces a piecewise constant function (a [step function](https://en.wikipedia.org/wiki/Step_function)) consisting of alternating horizontal and vertical lines. If the change in *x* is positive, the *y*-value changes after the *x*-value; otherwise the *y*-value changes before the *x*-value. +Produces a piecewise constant function (a [step function](https://en.wikipedia.org/wiki/Step_function)) consisting of alternating horizontal and vertical lines. The *y*-value changes after the *x*-value. # d3.curveStepBefore(context) stepBefore -Produces a piecewise constant function (a [step function](https://en.wikipedia.org/wiki/Step_function)) consisting of alternating horizontal and vertical lines. If the change in *x* is positive, the *y*-value changes before the *x*-value; otherwise the *y*-value changes after the *x*-value. +Produces a piecewise constant function (a [step function](https://en.wikipedia.org/wiki/Step_function)) consisting of alternating horizontal and vertical lines. The *y*-value changes before the *x*-value. ### Custom Curves diff --git a/src/curve/step.js b/src/curve/step.js index 935afb4..e9fb78f 100644 --- a/src/curve/step.js +++ b/src/curve/step.js @@ -17,7 +17,7 @@ Step.prototype = { lineEnd: function() { if (0 < this._t && this._t < 1 && this._point === 2) this._context.lineTo(this._x, this._y); if (this._line || (this._line !== 0 && this._point === 1)) this._context.closePath(); - this._line = 1 - this._line; + if (this._line >= 0) this._t = 1 - this._t, this._line = 1 - this._line; }, point: function(x, y) { x = +x, y = +y; @@ -25,15 +25,11 @@ Step.prototype = { case 0: this._point = 1; this._line ? this._context.lineTo(x, y) : this._context.moveTo(x, y); break; case 1: this._point = 2; // proceed default: { - var t = x > this._x ? this._t : 1 - this._t; - if (t <= 0) { + if (this._t <= 0) { this._context.lineTo(this._x, y); this._context.lineTo(x, y); - } else if (t >= 1) { - this._context.lineTo(x, this._y); - this._context.lineTo(x, y); } else { - var x1 = (this._x + x) * t; + var x1 = this._x * (1 - this._t) + x * this._t; this._context.lineTo(x1, this._y); this._context.lineTo(x1, y); }