Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:49:05+00:00 2026-06-18T08:49:05+00:00

I want to draw multiple svg lines (using D3.js) from a nested object structure

  • 0

I want to draw multiple svg lines (using D3.js) from a nested object structure like this:

data = [ 
{ Name: "jim", color: "blue", Points: [{x:0, y:5 }, {x:25, y:7 }, {x:50, y:13}] },
{ Name: "bob", color: "green", Points: [{x:0, y:10}, {x:25, y:30}, {x:50, y:60}] }
];

var line_gen = d3.svg.line().interpolate("linear")
                .x(function(d){return d.x;})
                .y(function(d){return d.y;}); 

My first attempt, based on this question was to use svg:g to represent the first nesting level, then access the second nesting level within the group to render the lines:

var g = svg.selectAll(".linegroup")
.data(data).enter()
.append("g")
.attr("class", "linegroup")
.style("stroke", function(d) { return d.color; })
.style("fill", "none");

g.selectAll(".line")
.data(function(d) { return d.Points;})
.enter().append("svg:path")
  .attr("d", line_gen);

But this did not render any lines. Instead, inside each group I got an empty path tag for each of the three data points. So apparently the shape of the data is causing problems.

<g class="linegroup" style="stroke: #0000ff; fill: none;">
    <path></path>
    <path></path>
    <path></path>
</g>

I did find one solution (will post as answer) that doesn’t use groups, but I would still like to know why the group solution doesn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:49:06+00:00Added an answer on June 18, 2026 at 8:49 am

    Here is an answer, largely based on this question except that it doesn’t use d3.entries:

    data = [ 
    { Name: "jim", color: "blue", Points: [{x:0, y:5 }, {x:25, y:7 }, {x:50, y:13}] },
    { Name: "bob", color: "green", Points: [{x:0, y:10}, {x:25, y:30}, {x:50, y:60}] }
    ];
    
    svg = d3.select("body").append("svg:svg").attr("width", 100).attr("height", 100)
    
    var line_gen = d3.svg.line().interpolate("linear")
                    .x(function(d){return d.x;})
                    .y(function(d){return d.y;});
    
    svg.selectAll(".line")
      .data(data)
      .enter().append("svg:path")
        .attr("d", function(d) { return line_gen(d.Points); })   // <<<
        .style("stroke", function(d) { return d.color; })
        .style("fill", "none");
    

    The key approach here is to not return the line_gen function directly to the “d” attribute but to wrap it in another function that passes the correct nested array from the outer object.

    See also this great discussion on d3.entries vs. d3.values. d3.entries seems to be suggested a lot for similar problems but I don’t think it really helps in my case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to draw multiple circles on a map, using the Google Maps API
I want to draw an area from given points on a map using the
I am using Graphics.DrawString to draw my usercontrol's text like this: protected override void
In my Custom View, I draw multiple filled Arc s like this: canvas.drawArc(oval, startAngle,
i'm learning opengl and I want to draw 50 texture(from one variable) this is
I want to draw multiple canvases on a single canvas using drawImage() method but
I want to Draw multiple line Graph with each line have different color and
i want to draw zebra lines in my form not a table, just lines
I want to draw a widget (in this example, a canvas) and then remove
I'm using http://raphaeljs.com/ to try and draw multiple small circles. The problem I'm having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.