• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

Parse and add to JSON new elements on children nodes based on property from parent node

Hi Experts!

I have previously posted a question with almost the same title.
I have updated the JSON used in there. As one see the main parent has 2 nodes. I would like to add to its children a property called "color" which will have the value from the array. For child one the first array value and so on.

Next is that the all children will inherit their parent color and modify it to a brighter one.
So for example parent[0] : child[0] -> color #ff0000
              parent[0] : child[0] : child [0] -> color #ff0001 and so on

However the color variation has an additional parameter. The children color from the data node have the color calculated variation based its parent imp so data1 color is base color/(parent.imp / child.imp)

{
    "name":"data",
    "i":0,
    "imp":"10",
    "children":[
        {
            "name":"data1",
            "i":1,
            "imp":"5",
            "children":[
                {
                    "name":"data11",
                    "i":11,
                    "imp":"5",
                    "children":[
                        {
                            "name":"data111",
                            "i":111,
                            "imp":"3",
                            "children":[
                                {
                                    "imp":"3",
                                    "name":"data1111",
                                    "i":1111
                                }
                            ]
                        },
                        {
                            "name":"data112",
                            "i":112,
                            "imp":"2",
                            "children":[
                                {
                                    "imp":"1",
                                    "name":"data1121",
                                    "i":1121
                                },
                                {
                                    "imp":"1",
                                    "name":"data1122",
                                    "i":1122
                                }
                            ]
                        }
                    ]
                },
                {
                    "name":"data12",
                    "i":12,
                    "children":[
                        {
                            "name":"data121",
                            "i":121,
                            "children":[
                                {
                                    "name":"data1211",
                                    "i":1211
                                },
                                {
                                    "name":"data1212",
                                    "i":1212
                                }
                            ]
                        },
                        {
                            "name":"data122",
                            "i":122,
                            "children":[
                                {
                                    "name":"data1221",
                                    "i":1221
                                },
                                {
                                    "name":"data1222",
                                    "i":1222
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "name":"data2",
            "i":2,
            "children":[
                {
                    "name":"data21",
                    "i":21,
                    "children":[
                        {
                            "name":"data211",
                            "i":211,
                            "children":[
                                {
                                    "name":"data2111",
                                    "i":2111
                                }
                            ]
                        },
                        {
                            "name":"data212",
                            "i":212,
                            "children":[
                                {
                                    "name":"data2121",
                                    "i":2121
                                }
                            ]
                        }
                    ]
                },
                {
                    "name":"data22",
                    "i":22,
                    "children":[
                        {
                            "name":"data221",
                            "i":221,
                            "children":[
                                {
                                    "name":"data2211",
                                    "i":2211
                                },
                                {
                                    "name":"data2212",
                                    "i":2212
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

Open in new window

0
dlcnet
Asked:
dlcnet
  • 3
  • 3
1 Solution
 
lwadwellCommented:
I do not understand what 'imp' is for.  Please explain more and give example expected result.
0
 
dlcnetAuthor Commented:
Hi!
This is a parameter used in my hsl color calculation. Each parent has the total number of imp as a sum of imp from its children.  So I am using the parent color from the array and modify it with the formula bellow for each child.

d3.hsl(a.h, a.s * 1.2, data.children[0].imp / l2.imp);

Open in new window

0
 
lwadwellCommented:
Maybe because it is late, maybe because I am thick ... but I am not getting it.
What do you want the script to do?  Do you want the 'imp' added to the object or does it already exist and have it used in setting the 'color' value ... or something else?  What would I do with that formula?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dlcnetAuthor Commented:
The imp is already in the json. you made this nice RGB calculation in your previous answer. I need this updated instead of adding 1 to the hex color, to calculate it based on the imp parameter.
so the base color is converted to hsl (120, 03, X). the first children are getting the base color from the array.
the children color is added as previously but is based on their parent to child imp ratio as the formula shows

children are getting x in the hsl:  child.parent.imp / child.imp)
0
 
lwadwellCommented:
I am reasonably sure that this will not have the formula you want ... but it is a starting point for us to work on.
There has been new functions added, I had a go at putting 'imp' in json ... and it is being passed down in the addColor() calls so the parent value is easy to use in the function.
index2.html
0
 
dlcnetAuthor Commented:
great solution
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now