Link to home
Start Free TrialLog in
Avatar of dlcnet
dlcnetFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

I do not understand what 'imp' is for.  Please explain more and give example expected result.
Avatar of dlcnet

ASKER

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

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?
Avatar of dlcnet

ASKER

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)
ASKER CERTIFIED SOLUTION
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dlcnet

ASKER

great solution