I am using d3.js to make a simple donut chart.
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:
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. Does anybody know if this is possible with CSS or know of a wordaround of some sort?
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.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.
An example of a dropshadow svg filter can be seen here . 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
我使用@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..
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)");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.