javascript - svg/d3.js 矩形一侧的圆 Angular

标签 javascript svg d3.js rounded-corners

我知道 svg 有一个内置函数来做圆 Angular ,但我只需要在四个 Angular 中的两个上做圆 Angular 。

我知道我可以在彼此的顶部绘制多个矩形来模仿它,但这看起来有点俗气。有什么方法可以使用剪辑或任何 d3.js 方法来实现吗?

现在我有一个水平条形图,其矩形如下:

    rects.enter().append("rect")
        .attr("x",function(d,i) { return x(0); })
        .attr("width",function(d) { return x(d.value) - x(0); })
        .attr("height",y.rangeBand())
        .attr("y",function(d) { return y(d.name); })

我正在尝试在矩形的右侧制作圆 Angular ,但不确定该怎么做。

最佳答案

扩展@robert-longson 的回答,您可以使用 SVG 的 elliptical arc commandslineto commands一起制作 Angular 落对于直边。这些与 path elements 一起使用.这是一种可能的实现方式:

// Returns path data for a rectangle with rounded right corners.
// The top-left corner is ⟨x,y⟩.
function rightRoundedRect(x, y, width, height, radius) {
  return "M" + x + "," + y
       + "h" + (width - radius)
       + "a" + radius + "," + radius + " 0 0 1 " + radius + "," + radius
       + "v" + (height - 2 * radius)
       + "a" + radius + "," + radius + " 0 0 1 " + -radius + "," + radius
       + "h" + (radius - width)
       + "z";
}

然后您可以调用此函数来计算“d”属性。例如:

rects.enter().append("path")
    .attr("d", function(d) {
      return rightRoundedRect(x(0), y(d.name), x(d.value) - x(0), y.rangeBand(), 10);
    });

实例:

可选:如果您愿意,可以重构 rightRoundedRect 函数以使其可配置,而不是采用大量参数。这种方法类似于 D3 的内置 shape generators。 .例如,您可以像这样使用矩形生成器:

rects.enter().append("path")
    .attr("d", rightRoundedRect()
      .x(x(0))
      .y(function(d) { return y(d.name); })
      .width(function(d) { return x(d.value) - x(0); })
      .height(y.rangeBand())
      .radius(10));

有关该方法的更多详细信息,请参阅 configurable function tutorial .

关于javascript - svg/d3.js 矩形一侧的圆 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12115691/

相关文章:

svg - 从中间逐渐向外画一条路径

javascript - ajax从数据库请求接收数据不起作用

javascript - 无法获取 SVG 路径 - Snap.svg

javascript - 为什么我的 getElementsByTagName 返回一个空的 NodeList?

javascript - 使用 jQuery ui slider 更改 d3.js 绘制的矩形元素的高度

将带有 300dpi 位图的 pdf 转换为 svg

javascript - 使用 javascript 实现下拉菜单更改时的重绘功能

javascript - 如何设置默认选项 d3.js

javascript - POST 数据未使用 XMLHttpRequest 发送

javascript - 如何使用字符串数组过滤嵌套对象