1

I'm a pretty new to D3 and after two days I am still stuck with an issue I can't solve.

I have a this JSON structure:

var countries = [
                    {
                        name: "Germany",
                        total:1000,
                        bars:[
                            {
                                color: '#123456',
                                values: [100,200]
                            },
                            {
                                color: '#123456',
                                values: [100,200]
                            }
                        ]
                    },


                    {
                        name: "Sweden",
                        total:800,
                        bars:[
                            {
                                color: '#cccccc',
                                values: [100, 300]
                            }
                        ]
                    },

                    {
                        "name":"Netherlands",
                        total:200,
                        bars:[
                            {
                                color: '#123456',
                                values: [100,200]
                            }
                        ]
                    }
            ];

What I am trying to achieve is to build an html structure of this type:

<ul class="ranking">
    <!--one li element per object inside countries JSON structure -->
    <li>
        <ul class="bar">
             <!--one li element per bar object inside each country -->
        </ul> 
    </li>

</ul>

I can easily build the first unordered list in this way:

var rankingUL = svg.append("ul").attr ("class", "ranking")
var rankingLI = rankingUL.selectAll("li")
                .data(countries)
                .enter()
                .append("li")

var rankingULBars = rankingLI.append("ul").attr("class", "bar");

but then I have some big issue to understand how to create the second nested list. What I am basically missing is to understand how to recursively create another list inside each list item, containing a list item per each bar. Any help or explanation is really appreciated

1 Answer 1

3

You can loop through the nested structure as follows:

var rankingLI = rankingUL.selectAll("li")
            .data(countries)
            .enter()
            .append("li")
            .text( function(d) {return d.name;});

var rankingULBars = rankingLI.append("ul")
            .attr("class", "bar")
            .selectAll("li")
            .data(function(d) {return d.bars;})
            .enter()
            .append("li")
            .text("bar")
            .style("background-color", function(d) {return d.color});

See: http://jsfiddle.net/ee2todev/mpc3uzp8/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , thank you thank you. This is just what I was looking for after I have created a d3.nest() which has a key and values[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.