1

so I have an object that I need to convert into something viewable in a sankey diagram. the object looks like this:

"DecisionTreeRegressionModel": [
{
  "  If (feature 28 <= 16.0)": [
    {
       "   If (feature 0 in {0.0})": [
         {
            "    Predict: 13.0": []
         }
       ]
    },
    {
      "   Else (feature 0 not in {0.0})": [
        {
          "    Predict: 16.0": []
        }
      ]
    }
  ]
},
{
  "  Else (feature 28 > 16.0)": [
    {
       "   If (feature 28 <= 40.0)": [
         {
           "    Predict: 40.0": []
         }
       ]
     },
     {
       "   Else (feature 28 > 40.0)": [
         {
           "    If (feature 0 in {0.0})": [
             {
                "     Predict: 45.0": []
             }
           ]
         },
         {
           "    Else (feature 0 not in {0.0})": [
             {
               "     Predict: 50.0": []
             }
           ]
         }
       ]
     }
   ]
 }
]

the problem is that I don't know the depth of the object array so it must be automatically generated. what is the best way to loop through the object array and fill the sankey chart dataset?

Bill Newton
  • 45
  • 1
  • 12

1 Answers1

0

I found the answer by creating 2 recursive methods one for labels and one for values. the value dataset still needs work but the charts are appearing correctly now.

build_labels(node : Node){
   let key : String = node.name;
   let already_added : boolean = false;
   if(this.labels_array.indexOf(key) > -1){
     already_added = true;
   }
   if(!already_added){
     this.labels_array.push(key);
   }

   if(node.children!=null && node.children.length>0){
     for(let i : number = 0 ; i < node.children.length ; i++){
       this.build_labels(node.children[i]);
     }
   }

}


build_chart(node : Node){

   let key : String = node.name;
   let father_index : number = this.labels_array.indexOf(key);

   if(node.children!=null && node.children.length>0){
     for(let i : number = 0 ; i < node.children.length ; i++){
       let child_key : String = node.children[i].name;
       let child_index : number = this.labels_array.indexOf(child_key);
       this.source_array.push(father_index);
       this.target_array.push(child_index);
       this.value_array.push(1);
       this.build_chart(node.children[i]);
     }
   }

}
Bill Newton
  • 45
  • 1
  • 12