diff --git a/src/svg/line.js b/src/svg/line.js index 05d5698f..03cd004c 100644 --- a/src/svg/line.js +++ b/src/svg/line.js @@ -92,6 +92,7 @@ var d3_svg_lineInterpolators = d3.map({ "linear": d3_svg_lineLinear, "linear-closed": d3_svg_lineLinearClosed, "step-before": d3_svg_lineStepBefore, + "step-middle": d3_svg_lineStepMiddle, "step-after": d3_svg_lineStepAfter, "basis": d3_svg_lineBasis, "basis-open": d3_svg_lineBasisOpen, @@ -127,6 +128,16 @@ function d3_svg_lineStepBefore(points) { return path.join(""); } +// Step interpolation; generates "H" and "V" commands. +function d3_svg_lineStepMiddle(points) { + var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; + while (++i < n) { + var prev = points[i-1], p = points[i]; + path.push("H", (p[0] - prev[0])/2 + prev[0], "V", p[1], "H", p[0]); + } + return path.join(""); +} + // Step interpolation; generates "H" and "V" commands. function d3_svg_lineStepAfter(points) { var i = 0, diff --git a/test/svg/line-test.js b/test/svg/line-test.js index b773fe7a..0ef6c830 100644 --- a/test/svg/line-test.js +++ b/test/svg/line-test.js @@ -93,6 +93,12 @@ suite.addBatch({ assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0V1H1"); assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), "M0,0V1H1V0H2"); }, + "supports step-middle interpolation": function(line) { + var l = line().interpolate("step-middle"); + assert.pathEqual(l([[0, 0]]), "M0,0"); + assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0H0.5V1H1"); + assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), "M0,0H0.5V1H1H1.5V0H2"); + }, "supports step-after interpolation": function(line) { var l = line().interpolate("step-after"); assert.pathEqual(l([[0, 0]]), "M0,0");