D3js filter selection
Asked Answered
V

2

5

I am creating an interactive bubble chart of fortune 500 data. I am trying to reduce a selection but can't figure out why it's not working as I expect.

var bubble = d3.layout.pack()
    ...

d3.json("js/data/fortune2009.json", function(json) {

    var node = svg.data([json]).selectAll("g.node")
        .data(bubble.nodes); // filter here?

    node.enter()
        .append("g")
        .attr("class", "node")
        .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

    node.append("circle")
        .attr("r", function(d) { return d.r; })
        .attr("class", function(d) { return "q" + color(d.Profit) + "-9"; });

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

The JSON data looks like this:

{
    "children":[
    {
        "Year" : "2009",
        "Rank" : "1",
        "Revenue" : "442",
        "Profit" : "851.00",
        "Company" : "Exxon Mobil"
    },
    ....
}

I want to remove the parent node. I am trying to use a filter function like described here.

.filter(function(d) { return !d.children; })

But if i add my filter function where I initialize the node selection, either nothing happens or I get an error.

(The filter works when I apply it before appending text)

How do I solve this?

UPDATE: Here is a JSFIDDLE http://jsfiddle.net/B9w4N/8/ showing my problem.

enter image description here

Volta answered 28/1, 2013 at 12:43 Comment(1)
At a glance I'd guess that you'd need to put your filter between node.enter() and .append("g"), but it'd take more time than I have to spare to get an example running so it'd help if you could put something working on JSFiddle...Marjorie
C
4

You can filter the array that the layout outputs:

var node = svg.data([fortune2009]).selectAll("g.node")
    .data(function(d) { 
        return bubble.nodes(d).filter(function(v) {
            return v.depth > 0;
        });
    });

See updated fiddle: http://jsfiddle.net/B9w4N/10/

Cacophony answered 29/1, 2013 at 1:39 Comment(2)
Thank you @nautat. It solved my problem, but created another one. Why is the first circle acting weird when I interact with the chart?Volta
You did something funny with reassigning data inside the interaction function. Also you want to use a key function to make sure a bubble corresponds to the same company, even after resorting of the data array. See updated fiddle: jsfiddle.net/B9w4N/11Cacophony
M
8

You had the right idea. You just needed to apply the filter where the circles are created (in addition to where the text is created):

node
    .filter(function(d) { return !d.children; })
    .append("circle")
    .attr("r", function(d) { return d.r; });

Forked version on JSFiddle

Marjorie answered 28/1, 2013 at 17:46 Comment(4)
Thank you @Richard. I actually tried this earlier, but it doesn't feel like the optimal solution, since a <g class="node" transform="translate(300,300)"></g> is created. I believe the filter could be avoided where the text is created if I just filter the selection earlier...Volta
What should i do when i have multiple child inside node and for each node i want to append circle with node?Inheritance
@AmitRana can you post a separate question, the comments of this one isn't a great medium for it. If you reply with a link to your question I'd be happy to have a go at answering it.Marjorie
#20354359Inheritance
C
4

You can filter the array that the layout outputs:

var node = svg.data([fortune2009]).selectAll("g.node")
    .data(function(d) { 
        return bubble.nodes(d).filter(function(v) {
            return v.depth > 0;
        });
    });

See updated fiddle: http://jsfiddle.net/B9w4N/10/

Cacophony answered 29/1, 2013 at 1:39 Comment(2)
Thank you @nautat. It solved my problem, but created another one. Why is the first circle acting weird when I interact with the chart?Volta
You did something funny with reassigning data inside the interaction function. Also you want to use a key function to make sure a bubble corresponds to the same company, even after resorting of the data array. See updated fiddle: jsfiddle.net/B9w4N/11Cacophony

© 2022 - 2024 — McMap. All rights reserved.