为d3.js中的多个元素生成clipPath

And*_*cik 3 javascript geometry svg d3.js force-layout

我试图创建部分填充的圈子,就像最后的NYT政治会议可视化中的圈子一样:http://www.nytimes.com/interactive/2012/09/06/us/politics/convention-word-counts.html

在此输入图像描述

我在d3(https://gist.github.com/1067636http://bl.ocks.org/3422480)中为clipPaths找到的两个最清晰的代码示例为每个剪辑路径创建了具有唯一ID的各个div元素然后将这些路径应用于单个元素.

我无法弄清楚如何从这些示例到基于数据值的一组元素中的每个元素具有唯一圆形clipPath的可视化,以便我可以创建我的效果.

这是我到目前为止:

给定具有以下结构的数据:

data = [        
    {value: 500, pctFull: 0.20, name: "20%"}, 
    {value: 250, pctFull: 0.75, name: "75%"},
    {value: 700, pctFull: 0.50, name: "50%"},        
]
Run Code Online (Sandbox Code Playgroud)

1)为数据集中的每个对象创建一个带圆的力图.圆的面积来自对象值.

2)使用mbostock示例中的算法从每个数据点的比例(pctFull)计算k(和h)http://bl.ocks.org/3422480

3)使用k为每个覆盖圆的适当区域的数据点生成一个矩形.

我想如果我可以将每个矩形的可见度限制在各自的圆圈中,那么就可以完成插图,但这就是我被困住的地方.我尝试过很多东西,但都没有.

这是jsfilddle:http://jsfiddle.net/G8YxU/2/

在此输入图像描述

nra*_*itz 7

在这里看到一个工作小提琴:http://jsfiddle.net/nrabinowitz/79yry/

在此输入图像描述

// blue circle
node.append("circle")
    .attr("r", function(d, i) {return rVals[i];})
    .style("fill", "#80dabe")                
    .style("stroke", "#1a4876");   

// clip path for the brown circle
node.append("clipPath")
    // make an id unique to this node
    .attr('id', function(d) { return "clip" + d.index })
  // use the rectangle to specify the clip path itself 
  .append('rect')
    .attr("x", function(d, i){ return rVals[i] * (-1);})
    .attr("width", function(d, i){ return rVals[i] * 2;})
    .attr("y", function(d, i) {return rVals[i] - (2  * rVals[i] * kVals[i]);})
    .attr("height", function(d, i) {return 2  * rVals[i] * kVals[i];});

// brown circle
node.append("circle")
    // clip with the node-specific clip path
    .attr("clip-path", function(d) { return "url(#clip" + d.index + ")"})
    .attr("r", function(d, i) {return rVals[i];})
    .style("fill", "#dabe80")                
    .style("stroke", "#1a4876");   
Run Code Online (Sandbox Code Playgroud)
  • 看起来为元素指定剪辑路径的唯一方法是使用属性中的url(IRI)符号clip-path,这意味着您将需要基于节点数据的每个剪辑路径的唯一ID.我已经使用了表单clip<node index>作为id - 所以每个节点都有自己的剪辑路径,节点的其他子元素可以引用它.

  • 按照Mike的例子,制作两个不同颜色的圆圈并将矩形本身用于剪辑路径,而不是制作基于圆的剪辑路径,这似乎是最简单的.但你可以这样做.