Is there a way to add a highlight to pie chart in

2019-05-10 23:45发布

I hope I'm using the correct term, but basically I am trying to create highlight along the top of a pie chart in D3. I've seen lot's of things for adding drop shadows but have been unable to make that work for a highlight. So, I tried adding an arc on top of the chart and adding a gaussian blur to it, but there are 2 issues with it: it doesn't transition with the rest of the chart and the highlighting extends above the chart, I can't seem to get it to stay within the edges of the chart.

Here's an example: http://jsfiddle.net/hf3adsj5/

The code I'm using to try to add the highlighting is as follows:

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)");

Is there a way to do this without adding the extra arc? Or at least get it to transition like the drop shadow does?

1条回答
时光不老,我们不散
2楼-- · 2019-05-11 00:13

You can do it all in one filter. After you calculate your drop-shadow, you can go back to the sourcegraphic, create a highlight and combine both the highlight and the drop-shadow on the source. Here's your drop shadow filter edited to add a highlight.

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");
查看更多
登录 后发表回答