draw elements based on presence of an attribute

20 views
Skip to first unread message

Tom Herold

unread,
May 23, 2013, 10:27:24 AM5/23/13
to d3...@googlegroups.com
Hi,

I would like to draw my elements differently based on wether the underlying data object has an attribute or not. For instance, I have edge objects in graph. Some of these have comments, some do not. Is there a way to control my drawing code, based on wether the "comment" attribute is present?

Code (in Coffescript):

    @paths = @paths.data(@edges)

    #add new edges
    path = @paths.enter().append("svg:path")
    path
      .attr(
        class : "edge"
        d : (data) -> data.getLineSegment()
      )
      .style("marker-end", "url(#end-arrow)")
      .append("svg.circle") if datum().comment != undefined ???
          .attr(....)

    #update existing ones
    @paths.attr(
      d : (data) -> data.getLineSegment()
    )

    #remove deleted edges
    @paths.exit().remove()

Andy Thornton

unread,
May 23, 2013, 10:31:44 AM5/23/13
to d3...@googlegroups.com
Make your attribute a function of the data. As an example:

.append('circle')
   .attr('fill', function (d) { return d.comment ? 'red' : 'black';})

Andy


--
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.
 
 

Tom Herold

unread,
May 23, 2013, 10:35:19 AM5/23/13
to d3...@googlegroups.com
That is not really what I want. I want to append an object (or not) depending whether the attribute is present.

Andy Thornton

unread,
May 23, 2013, 11:08:06 AM5/23/13
to d3...@googlegroups.com
More like this?


You can filter the data prior to binding it to the DOM.

Tom Herold

unread,
May 23, 2013, 11:12:21 AM5/23/13
to d3...@googlegroups.com
Almost, right. What about the third circle in your demo? I still would like to draw all cirlces regardless of the "comment" attribute. But those circles that have it should get additional svg element. (e.g. callout combined from text and another circle)

Any ideas?

Ian Johnson

unread,
May 23, 2013, 11:47:20 AM5/23/13
to d3...@googlegroups.com

You can do
.each (d,i) ->
  if d.comment
     d3.select(this).append('circle')...

But you may have an issue appending a circle to a path element. You probably want to put both in a group <g> element.

Tom Herold

unread,
May 23, 2013, 1:06:17 PM5/23/13
to d3...@googlegroups.com
I suppose I could use .each(). But wouldn't that defeat the whole purpose of enter() and exit()?

But you may have an issue appending a circle to a path element. You probably want to put both in a group <g> element.
Surely, I would that. 

Ian Johnson

unread,
May 23, 2013, 1:52:52 PM5/23/13
to d3...@googlegroups.com
you're right, i played around and landed on this:

also these are good if you haven't read them:

Ian Johnson - 周彦

Tom Herold

unread,
May 24, 2013, 4:23:04 AM5/24/13
to d3...@googlegroups.com
Well, well. Thank you very Mr. Ian Johnson! I guess that is exactly what I was looking for! I appreciate your effort.
Reply all
Reply to author
Forward
0 new messages