【问题标题】:How to add drop shadow to d3.js pie or donut chart如何向 d3.js 饼图或圆环图添加阴影
【发布时间】:2012-09-05 08:56:21
【问题描述】:

我正在使用 d3.js 制作一个简单的圆环图。

我无法实现投影或框阴影效果来为图表添加一些深度。我试过添加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;
}

到路径标签和g标签,但无济于事。有谁知道这是否可以通过 CSS 实现或知道某种字眼?

非常感谢对这样一个基本问题的帮助。马特

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

【问题讨论】:

    标签: javascript svg d3.js pie-chart


    【解决方案1】:

    以防万一有人遇到这种情况。 . .

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

    然后将此过滤器附加到 svg 元素,例如线条或条形或其他任何你喜欢的东西。 . .

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

    【讨论】:

    • 非常有用的例子,谢谢。有没有办法让阴影在内部而不是外部?
    • stackoverflow.com/questions/29397152/… 是一种添加高光的方法 - 调整它以使其成为内部阴影
    • 是否可以在不为投影分配 ID 的情况下执行此操作?比如,当想要为一个页面动态创建多个这些时?
    【解决方案2】:

    如果您需要控制阴影的颜色,这对我有用:

    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:来自这个答案:https://stackoverflow.com/a/25818296/1154787

    【讨论】:

      【解决方案3】:

      您可以使用 svg 滤镜,here's one example 展示了如何应用模糊滤镜。

      可以看到一个投影 svg 过滤器的示例here。结合这两个例子来得到你需要的东西。

      【讨论】:

      【解决方案4】:

      我使用@Erik Dahlström 发布的信息为您制作了一个带有阴影的 d3.js 的简单注释示例,该示例使用 @Erik Dahlström 发布的信息:http://bl.ocks.org/cpbotha/5200394 :)

      【讨论】:

        【解决方案5】:

        根据 Google 的 Material Design,阴影应该接近真实世界的条件,因为人类具有巨大的感知能力。因此,阴影应该由环境分量和投射分量组成。请在此处查看 Google 的规范。

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

        以上都是很好的阴影示例,但我找不到任何复合阴影的示例,所以我想分享这个定制的滤镜。它似乎确实使最终结果看起来更真实。

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

        这可以应用于一个对象,就像这样..

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-02
          • 2016-09-06
          • 2013-08-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多