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

守給你的承諾、 提交于 2020-02-28 07:04:22

问题


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:


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


来源:https://stackoverflow.com/questions/29397152/is-there-a-way-to-add-a-highlight-to-pie-chart-in-d3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!