简体   繁体   中英

d3js - How to ignore specific object in JSON

i have a json file like the following one:

    {
 "children": [
  {
   "name": "المصاريف",
   "children": [
    {"name": "بنزين","size": 14230,"colour": "rgb(220,230,180)"},
    {"name": "تاكسي","size": 25220,"colour": "rgb(220,230,200)"},
    {"name": "شاي","size": 30523,"colour": "rgb(220,230,220)"}
   ]
  },
  {
   "name": "الدخل",
   "children": [
    {"name": "مرتب","size": 50657,"colour": "rgb(150,230,180)"},
    {"name": "ايجار","size": 24320,"colour": "rgb(150,230,200)"},
    {"name": "مصنع","size": 163460,"colour": "rgb(150,230,220)"}
   ]
  },{
   "name": "الربح","size": 168464,"colour": "rgb(180,230,220)"}
 ]
}

as you see that the root object doesn't have name and i'm binding the data using

  .data(partition.nodes(root))

so i need to ignore the first node in my d3 chart

in other words i need to ignore any node that doesn't have null name or ignore the empty node that appear in my legend

here is a screenshot for my issue http://postimg.org/image/bp44yryuh/

The method partition.nodes(root) returns an array.

You can therefore filter the array based on any feature of the individual data objects, such as whether the name is non-null:

.data(partition.nodes(root).filter(function(d){return d.name;}) )
    //any nodes with no value or an empty string for d.name will be filtered out

However, in the specific case of filtering out the root from a d3 partition layout node array, you can take advantage of the fact that the root node will always be the first node in the returned array . It is therefore much faster to just slice the array , instead of running a filter test on every element:

.data( partition.nodes(root).slice(1) );

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM