The Artima Developer Community
Sponsored Link

Java Buzz Forum
Extending D3.js

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Mathias Bogaert

Posts: 618
Nickname: pathos
Registered: Aug, 2003

Mathias Bogaert is a senior software architect at Intrasoft mainly doing projects for the EC.
Extending D3.js Posted: Jun 3, 2014 7:07 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Mathias Bogaert.
Original Post: Extending D3.js
Feed Title: Scuttlebutt
Feed URL: http://feeds.feedburner.com/AtlassianDeveloperBlog
Feed Description: tech gossip by mathias
Latest Java Buzz Posts
Latest Java Buzz Posts by Mathias Bogaert
Latest Posts From Scuttlebutt

Advertisement

I’m a big fan of using D3.js for building content from structured data. You can write expressive JavaScript using the D3.js API to convert your data to structured markup. A real quick example might be… <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script></p> <div class="content">     <ul></ul> </div> var d = d3.selectAll(".content ul").data([             "one",             "two",             "three",             "four"         ]); d.enter()  .append("li")  .classed("number", true)  .text(String); Which really just says take the elements at .content ul selector and bind them to the data ["one", "two, ...]. The d.enter() starts expressing what happens when a new data element is found in the bound dataset. In this case we create a new li element (.append(“li”)), set the CSS class, then apply the content of the data element as the text of this element. So this is pretty reasonable, but a common activity I see is applying a couple classes or maybe an ID to this and it would be great if I could express that in a new way. I’d like to be able to do like d.enter()  .appendSelector("li.number.second-class#num-id")  .text(String); To generate <li class=”number second-class” id=”num-id”> much easier. Well, D3.js does provide a couple extension points, the easiest one to interact with is just expand the API with additional calls. d3.selection.enter.prototype.appendSelector = d3.selection.prototype.appendSelector = function(selector) {     var self = this;     var selectorParts = selector.split(/([&#92;.&#92;#])/);     if (selectorParts.length == 0) return self;     self = self.append(selectorParts.shift());     while (selectorParts.length > 1) {         var selectorModifier = selectorParts.shift();         var selectorItem = selectorParts.shift();         if (selectorModifier === ".") {             self = self.classed(selectorItem, true);         } else if (selectorModifier === "#") {             self = self.attr('id', selectorItem);         }     }     return self; }; So the prototypes for d3.selection and d3.selection.enter are the primary things that you’ll interact with via the API. This allows d.enter().appendSelector(…) to work brilliantly. For those who don’t want to extend the D3.js prototypes, call and each can be used to apply a function to a selector. Using these tools you can make repeatable and reusable expressions of intent when building things with D3.js.

The post Extending D3.js appeared first on Atlassian Blogs.

Read: Extending D3.js

Topic: My Move From ColdFusion to Java Development Previous Topic   Next Topic Topic: Playing with Java 8 – Lambdas, Paths and Files

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use