d3.select problem - selecting anything other than body fails?

2,036 views
Skip to first unread message

Russell Jurney

unread,
Feb 7, 2012, 7:34:47 PM2/7/12
to d3...@googlegroups.com
I am working some examples, and I am unable to place d3 charts within my web pages.

Whenever I changed the initial select from "body" to the div I actually want to place the content in, nothing displays.

For example, the histogram example works fine like this:

var vis = d3.select("body").append("svg")
    .attr("width", w)
    .attr("height", h)
  .append("g")
    .attr("transform", "translate(.5)");

But not whatsoever like this: 

var vis = d3.select("content").append("svg")
    .attr("width", w)
    .attr("height", h)
  .append("g")
    .attr("transform", "translate(.5)");

How do you select a div to put content within it?

--
Russell Jurney
twitter.com/rjurney
russell.jurney@gmail.com
datasyndrome.com

s...@alignedleft.com

unread,
Feb 7, 2012, 7:47:12 PM2/7/12
to d3...@googlegroups.com
"content" probably isn't a real HTML element.
 
Is "content" a class or ID on your div?  Try:
 
select("div.content")   //class
select("div#content")  //id
 

Mike Bostock

unread,
Feb 7, 2012, 7:54:45 PM2/7/12
to d3...@googlegroups.com
Did you mean select("#content")? I'm assuming you don't have a
<content> element, because you said you have a div.

Mike

rjurney

unread,
Feb 7, 2012, 7:55:29 PM2/7/12
to d3-js
Thanks, div#content works. I assumed it was selecting on div ids.
> [1]twitter.com/rjurney
> [2]russell.jur...@gmail.com
> [3]datasyndrome.com
>
> References
>
> 1.http://twitter.com/rjurney
> 2. mailto:russell.jur...@gmail.com
> 3.http://datasyndrome.com/

ani

unread,
Apr 15, 2013, 12:03:49 PM4/15/13
to d3...@googlegroups.com
Hi Scott, Mike,

I tried your suggestions but it didnt help..

I could successfully put charts into one website where script and html code were in same page..

But my recent project creates divs dynamic..

And i need to put chart into it..

I tried it didint work

Thanks

-Anil

Scott Murray

unread,
Apr 15, 2013, 12:06:35 PM4/15/13
to d3...@googlegroups.com
When you say it didn't work, what happened? (Any errors, wrong output, nothing?)

Remember, you can only use an ID value once, so if you're trying to control multiple charts on the same page, they can't all be called div#content. Maybe: div#chart1, div#chart2, and so on.

Scott

ani

unread,
Apr 15, 2013, 12:15:40 PM4/15/13
to d3...@googlegroups.com

I mean .. nothing happens.. My script just stops.. there and doesn't  do anything further...

my code is as below.

----------------------------
var svg = d3.select("div#42yr_bam_sorted_wig_div").append("svg:svg")
    .attr("width", width)
    .attr("height", height)
  .append("g");
------------------------------------

where '42yr_bam_sorted_wig_div' is div with id generated dynamically... and i want to draw chart in it.

Thanks

Scott Murray

unread,
Apr 15, 2013, 12:18:43 PM4/15/13
to d3...@googlegroups.com
If nothing happens and there are no errors in the console, then likely your code is being executed — it's just not the outcome you expected.  :-)

I'd start by looking at your select() statement.  It's possible that nothing is being selected (so, therefore, the rest of the statements aren't being run).  You could test this by logging the output of the select():

console.log(d3.select("div#42yr_bam_sorted_wig_div"))

…and see if anything is returned.

Scott



--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

ani

unread,
Apr 15, 2013, 12:21:37 PM4/15/13
to d3...@googlegroups.com
Hi Scott..

I tried...

Nothing returns...

Nothing happen means i have few console.log in my script after this statement... nothing prints in console.. so script just stops there..

is it possible that div generated dynamically so not loading chart into it

Thanks

Scott Murray

unread,
Apr 15, 2013, 12:23:08 PM4/15/13
to d3...@googlegroups.com
Well, there you go.  If console.log() isn't outputting anything, then obviously that line isn't being executed.

I'd take a step back, and figure out why that portion of code isn't being run.  (Is it inside a function, but that function is never called?)

Scott

ani

unread,
Apr 15, 2013, 12:28:19 PM4/15/13
to d3...@googlegroups.com
Hi Scot..

I found out the error...

Nothing was wrong with code or selection..

but Div id was starting with a number '42' i just removed 42 from there and it worked..

thanks a lot

Anil
Reply all
Reply to author
Forward
0 new messages