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 8892397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:59:16+00:00 2026-06-14T22:59:16+00:00

I’m trying to draw lines with d3js using d3.svg.line() with no success. var line

  • 0

I’m trying to draw lines with d3js using d3.svg.line() with no success.

var line = d3.svg.line()
    .x(function(d) { return Math.random() * 1000 })
    .y(function(d) { return Math.random() * 1000});

svg.selectAll("path")
        .data([1,2,3,4,5,6]).enter()
        .append("path")
            .attr("d", line) # line generator 
            .attr("class", "line")
            .style("stroke", "black" );

I’ve inserted gğıgğı on purpose to see whether it will give an error but i didn’get any error.
seems that x and y functions are not called. with or without gğıgğı all i managed to do is create empty path elements.

<path class="line"></path>

If i replace line generator “line” with

"M0,0l100,100"

lines are succesfully drawn.

Sample code is at http://jsfiddle.net/99mnK/1/

What am i doing wrong here?

Edit
A working version is at http://jsfiddle.net/99mnK/2/ . It seems that d3.svg.line().data expects a 2d data array such as

[[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]]

instead of

[1,2,3,4,5,6]
  • 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-14T22:59:17+00:00Added an answer on June 14, 2026 at 10:59 pm

    d3.svg.line() by itself is not quite made to work with selectors. As such, you’re supposed to just call it with an array of values – rather than passing it as a function into .attr() function as you’re doing.

    So, say you have:

    var array = [1,2,3,4,5,6]
    var line = d3.svg.line()
        .x(function(d) { return Math.random() * 1000 })
        .y(function(d) { return Math.random() * 1000});
    

    then, to get a path description that has 6 random points, you simply need:

    line(array);
    

    and, to apply it to an svg path:

    .append('path')
    .attr('d', line(array))
    

    The reason your edited fiddle works, with a 2d array binding as you have it, is because your line function is being called with each sub-element of the array:

    .line([1,1])
    .line([2,2])
    .line([3,3])
    

    That explains why you’re seeing lines drawn with just 2 points, as opposed to 6 points, as you might have expected.

    Finally, here’s an edited fiddle showing how you might draw 6 paths, as your example was maybe attempting to do: http://jsfiddle.net/mharen/3cv3T/5/

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to render a haml file in a javascript response like so:
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.