Skip to content
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

curveBump? #152

Closed
mbostock opened this issue Nov 9, 2019 · 1 comment · Fixed by #174
Closed

curveBump? #152

mbostock opened this issue Nov 9, 2019 · 1 comment · Fixed by #174

Comments

@mbostock
Copy link
Member

mbostock commented Nov 9, 2019

As in for bump charts. Like d3.linkHorizontal, but as a curve. Maybe d3.curveLinkHorizontal is a better name? Or d3.curveSankeyHorizontal? Here’s an implementation that works for lines:

function curveBump(context) {
  let _point, _x, _y;
  return {
    lineStart() {
      _point = 0;
    },
    lineEnd() {
      _point = NaN;
    },
    point(x, y) {
      if (++_point === 1) context.moveTo(x, y);
      else _x = (_x + x) / 2, context.bezierCurveTo(_x, _y, _x, y, x, y);
      _x = x, _y = y;
    }
  }
}
@mbostock
Copy link
Member Author

mbostock commented Mar 1, 2021

This works for area, too:

function curveBump(context) {
  let line, point, x0, y0;
  return {
    areaStart() {
      line = 0;
    },
    areaEnd() {
      line = NaN;
    },
    lineStart() {
      point = 0;
    },
    lineEnd() {
      if (line || (line !== 0 && point === 1)) context.closePath();
      line = 1 - line;
    },
    point(x, y) {
      x = +x, y = +y;
      switch (point) {
        case 0: {
          point = 1;
          if (line) context.lineTo(x, y);
          else context.moveTo(x, y);
          break;
        }
        case 1: point = 2; // proceed
        default: {
          x0 = (x0 + x) / 2;
          context.bezierCurveTo(x0, y0, x0, y, x, y);
          break;
        }
      }
      x0 = x, y0 = y;
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant