有没有办法在d3中为饼图添加高亮?

时间:2015-04-01 17:31:00

标签: svg d3.js svg-filters

我希望我使用的是正确的术语,但基本上我试图在D3的饼图顶部创建高亮显示。我已经看到了许多用于添加阴影的东西,但却无法将其用于突出显示。因此,我尝试在图表顶部添加一个弧并为其添加高斯模糊,但它有两个问题:它不会与图表的其余部分一起转换,突出显示在图表上方,我可以'似乎让它保持在图表的边缘。

以下是一个示例:http://jsfiddle.net/hf3adsj5/

我用来尝试添加突出显示的代码如下:

var arc2 = d3.svg.arc()
    .innerRadius(innerRadius)
    .outerRadius(outerRadius)
    .startAngle(Math.PI/4)
    .endAngle(-7/12*Math.PI);

var filter2 = defs.append("filter")
    .attr("id","highlight");

filter2.append("feGaussianBlur")
    .attr("in","SourceAlpha")
    .attr("stdDeviation",2)
    .attr("result","blur");
filter2.append("feColorMatrix")
    .attr("in", "blur")
    .attr("type", "matrix")
    .attr("values", "0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  0 0 0 1 0")
    .attr("result", "whiteblur");
filter2.append("feOffset")
    .attr("in","whiteblur")
    .attr("dx",3)
    .attr("dy",3)
    .attr("result","offsetBlur");

var feMerge2 = filter2.append("feMerge");

feMerge2.append("feMergeNode")
    .attr("in","offsetBlur");
feMerge2.append("feMergeNode")
    .attr("in","SourceGraphic");

svg.append("path")
    .attr("d",arc2)
    .style("filter","url(#highlight)");

有没有办法在不添加额外弧线的情况下执行此操作?或者至少让它像投影那样过渡?

1 个答案:

答案 0 :(得分:1)

您可以在一个过滤器中完成所有操作。在计算了阴影后,您可以返回到sourcegraphic,创建一个突出显示,并在源上组合突出显示和阴影。这是您的投影滤镜,用于添加突出显示。

var filter = defs.append("filter")
    .attr("id","drop-shadow");

filter.append("feGaussianBlur")
    .attr("in","SourceAlpha")
    .attr("stdDeviation",3)
    .attr("result","blur");
filter.append("feOffset")
    .attr("in","blur")
    .attr("dx",3)
    .attr("dy",3)
    .attr("result","offsetBlur");
filter.append("feOffset")
    .attr("in", "SourceGraphic")
    .attr("dx",3)
    .attr("dy",3)
    .attr("result","plainOffset");
filter.append("feComposite")
    .attr("operator","out")
    .attr("in","SourceGraphic")
    .attr("in2","plainOffset")
    .attr("result","preHighlight");
filter.append("feColorMatrix")
    .attr("type","matrix")
    .attr("values","0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  0 0 0 1 0")
    .attr("result","preHighlightWhite");
filter.append("feGaussianBlur")
    .attr("stdDeviation",3)
    .attr("result","preHighlightBlur");
filter.append("feComposite")
    .attr("operator","in")
    .attr("in2","SourceGraphic")
    .attr("result","Highlight");
filter.append("feComposite")
    .attr("operator","over")
    .attr("in2","SourceGraphic")
    .attr("result","final");
filter.append("feComposite")
    .attr("operator","over")
    .attr("in2","offsetBlur")
    .attr("result","finalWithDrop");