Anonymous
Anonymous

Reputation: 456

Generate a shadow on circle click with D3.js

I would like to add a shadow when I click on my circle with D3.js. My click is successfully detected because I generate information about it on the same page but when I want to add a style, it doesn't works.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
    text {
        font: 10px sans-serif;
    }
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>

    var diameter = 960,
            format = d3.format(",d"),
            color = d3.scale.category20c();

    var bubble = d3.layout.pack()
            .sort(null)
            .size([diameter, diameter])
            .padding(1.5);

    var svg = d3.select("body").append("svg")
            .attr("width", diameter)
            .attr("height", diameter)
            .attr("class", "bubble");

    d3.json("./data.json", function(error, root) {
        if (error) throw error;

        var node = svg.selectAll(".node")
                .data(bubble.nodes(classes(root))
                 .filter(function(d) { return !d.children; }))
                .enter().append("g")
                .attr("class", "node")
                .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

        node.append("title")
                .text(function(d) { return d.className + ": " + format(d.value); });

        node.append("circle")
                .attr("r", function(d) { return d.r; })
                .style("fill", function(d) { return color(d.value); });

        node.append("text")
                .attr("dy", ".3em")
                .style("text-anchor", "middle")
                .text(function(d) { return d.className.substring(0, d.r / 3); });

        node.on("click", click);
        function click(d){


            d.style("box-shadow: 8px 8px 0px #aaa;"); // don't put a shadow
        }
    });

    // Returns a flattened hierarchy containing all leaf nodes under the root.
    function classes(root) {
        var classes = [];
        function recurse(name, node) {
            if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
            else classes.push({packageName: name, className: node.name});
        }
        recurse(null, root);
        return {children: classes};
    }
    d3.select(self.frameElement).style("height", diameter + "px");

</script>
</body>

Thanks.

Upvotes: 0

Views: 2470

Answers (1)

mgraham
mgraham

Reputation: 6207

There's a few issues here.

The easy one to fix is you're calling d.style("box-shadow: 8px 8px 0px #aaa;"); // don't put a shadow - that's just going to throw an error as d is the data not the dom element. You need to be doing d3.select(this).select("circle").style etc

on top of this the box-shadow style doesn't work on elements inside an svg, eventually filter: drop-shadow will work cross-browser but only for firefox at the moment e.g. in css: filter: drop-shadow(12px 12px 7px rgba(0,0,0,0.5));

So you need to use the svg filter effects pointed to in the comment above and apply it as style("filter", ...) to the d3.select(this).select("circle")

See https://plnkr.co/edit/PLJWW0BG4F0e81fD9xB1 for an example forked from yours

Upvotes: 1

Related Questions