简体   繁体   English

如何将阴影添加到d3.js饼图或圆环图

[英]How to add drop shadow to d3.js pie or donut chart

I am using d3.js to make a simple donut chart. 我正在使用d3.js制作一个简单的圆环图。

I'm failing to achieve a drop-shadow or box-shadow effect to add some depth to the chart. 我没有实现阴影或盒阴影效果来为图表添加一些深度。 I've tried adding the css: 我试过添加css:

path {
  -moz-box-shadow:    3px 3px 5px 6px #ccc;
  -webkit-box-shadow: 3px 3px 5px 6px #ccc;
  box-shadow:         3px 3px 5px 6px #ccc;
}

To path tags and the g tags, but to no avail. 路径标签和g标签,但无济于事。 Does anybody know if this is possible with CSS or know of a wordaround of some sort? 有没有人知道这是可能的CSS或知道某种单词的解决方法?

Really appreciate the help on such a basic problem. 真的很感激这个基本问题的帮助。 Matt 马特

var data = [0, 35, 65];

var w = 400,
    h = 400,
    r = Math.min(w, h) / 2,
   ir = r * 0.5,
   color = d3.scale.category20(),
   donut = d3.layout.pie().sort(null),
   arc = d3.svg.arc().innerRadius(ir).outerRadius(r);

var svg = d3.select("body").append("svg:svg")
    .attr("width", w)
   .attr("height", h)
.append("svg:g")
   .attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");

var arcs = svg.selectAll("path")
    .data(donut(data))
.enter().append("svg:path")
    .attr("fill", function(d, i) { return color(i); })
    .attr("d", arc);

In case anyone comes across this. 如果有人碰到这个。 . .

 /* For the drop shadow filter... */
  var defs = svg.append("defs");

  var filter = defs.append("filter")
      .attr("id", "dropshadow")

  filter.append("feGaussianBlur")
      .attr("in", "SourceAlpha")
      .attr("stdDeviation", 4)
      .attr("result", "blur");
  filter.append("feOffset")
      .attr("in", "blur")
      .attr("dx", 2)
      .attr("dy", 2)
      .attr("result", "offsetBlur");

  var feMerge = filter.append("feMerge");

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

Then attach this filter to the svg element, such as a line or bar or whatever else tickles your fancy. 然后将此过滤器附加到svg元素,例如线条或条形或其他任何您想要的东西。 . .

svg.append("path")
  .datum(data)
  .attr("class", "line")
  .attr("d", line)
  .attr("filter", "url(#dropshadow)");

You can use svg filters, here's one example showing how to apply a blur filter. 您可以使用svg过滤器, 这是一个显示如何应用模糊过滤器的示例

An example of a dropshadow svg filter can be seen here . 这里可以看到一个drophadow svg过滤器的例子。 Combine the two examples to get what you need. 结合这两个例子来获得你需要的东西。

In case you need to control the color of the shadow, this worked for me: 如果你需要控制阴影的颜色 ,这对我有用:

var defs = svg.append("defs");

    var filter = defs.append("filter")
        .attr("id", "dropshadow")

    filter.append("feGaussianBlur")
        .attr("in", "SourceAlpha")
        .attr("stdDeviation", 4)
        .attr("result", "blur");
    filter.append("feOffset")
        .attr("in", "blur")
        .attr("dx", 2)
        .attr("dy", 2)
        .attr("result", "offsetBlur")
    filter.append("feFlood")
        .attr("in", "offsetBlur")
        .attr("flood-color", "#3d3d3d")
        .attr("flood-opacity", "0.5")
        .attr("result", "offsetColor");
    filter.append("feComposite")
        .attr("in", "offsetColor")
        .attr("in2", "offsetBlur")
        .attr("operator", "in")
        .attr("result", "offsetBlur");

    var feMerge = filter.append("feMerge");

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

d3:ized from this answer: https://stackoverflow.com/a/25818296/1154787 d3:从这个答案: https ://stackoverflow.com/a/25818296/1154787

我使用@ErikDahlström发布的信息: http ://bl.ocks.org/cpbotha/5200394 :)给你一个简单的注释示例,其中包含带有阴影的SVG rect图形的d3.js。

According to Google's Material Design, a shadow should approximate real world conditions as humans have tremendous capacity for perception. 根据谷歌的材料设计,阴影应该接近现实世界的条件,因为人类具有巨大的感知能力。 Thus, a shadow should be comprised of an ambient component and a cast component. 因此,阴影应该由环境组件和铸造组件组成。 See Google's specification here.. 在这里查看Google的规范..

http://www.google.com/design/spec/what-is-material/environment.html#environment-light-shadow http://www.google.com/design/spec/what-is-material/environment.html#environment-light-shadow

All of the above are excellent examples of shadows, but I could not find any examples of composite ones, so I though I would share this custom made filter. 以上所有都是阴影的优秀例子,但我找不到任何复合的例子,所以我会分享这个定制的过滤器。 It does seem to make the end result seem more realistic. 它似乎确实使最终结果看起来更加真实。

var id = "md-shadow";
var deviation = 2;
var offset = 2;
var slope = 0.25;

var svg = d3.select("#yoursvg");
var defs = svg.select("defs");

// create filter and assign provided id
var filter = defs.append("filter")
    .attr("height", "125%")    // adjust this if shadow is clipped
    .attr("id", id);

// ambient shadow into ambientBlur
//   may be able to offset and reuse this for cast, unless modified
filter.append("feGaussianBlur")
    .attr("in", "SourceAlpha")
    .attr("stdDeviation", deviation)
    .attr("result", "ambientBlur");

// cast shadow into castBlur
filter.append("feGaussianBlur")
    .attr("in", "SourceAlpha")
    .attr("stdDeviation", deviation)
    .attr("result", "castBlur");

// offsetting cast shadow into offsetBlur
filter.append("feOffset")
    .attr("in", "castBlur")
    .attr("dx", offset - 1)
    .attr("dy", offset)
    .attr("result", "offsetBlur");

// combining ambient and cast shadows
filter.append("feComposite")
    .attr("in", "ambientBlur")
    .attr("in2", "offsetBlur")
    .attr("result", "compositeShadow");

// applying alpha and transferring shadow
filter.append("feComponentTransfer")
    .append("feFuncA")
        .attr("type", "linear")
        .attr("slope", slope);

// merging and outputting results
var feMerge = filter.append("feMerge");
feMerge.append('feMergeNode')
feMerge.append("feMergeNode")
    .attr("in", "SourceGraphic");

This can be applied to an object, like so.. 这可以应用于一个对象,就像这样。

var r = element.append("rect")
    .attr("class", "element-frame")
    :   // other settings
    .style("filter", "url(#md-shadow)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM