Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3: Skip Item based on condition

Tags:

d3.js

I have a data array like the following:

mydata = [ {
           "title": "key1",
           "description": "some description 1",
           "visible": "1",
         },
         {
           "title": "key2",
           "description": "some description 2",
           "visible": "0",
         },
         {
           "title": "key3",
           "description": "some description 3",
           "visible": "1",
         }
  ]

...and with the following code:

        var chart = svg.selectAll("g.chart")
        .data(mydata, function(i, d)
        {
            return d;
        })
        .enter()
        .append("svg:g")
        .attr("class", "chart")
        .attr("style", "position:fixed");

With the following code, how can I skip the item with "visible" = 0?

Basically, show everything with visibility = 1?

Thanks

like image 666
Paul Avatar asked Aug 17 '26 17:08

Paul


1 Answers

You can use .filter():

.data(mydata.filter(function(d) { return d.visible == "1"; }))
like image 172
Lars Kotthoff Avatar answered Aug 19 '26 15:08

Lars Kotthoff