<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>D3coder</title>
	<atom:link href="/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.d3coder.com</link>
	<description>Decoding D3.js programming</description>
	<lastBuildDate>Thu, 18 Oct 2012 15:14:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>Importing JSON Data into D3</title>
		<link>http://www.d3coder.com/2012/10/15/importing-json-data-into-d3/</link>
		<comments>http://www.d3coder.com/2012/10/15/importing-json-data-into-d3/#comments</comments>
		<pubDate>Mon, 15 Oct 2012 23:38:02 +0000</pubDate>
		<dc:creator>EvanZ</dc:creator>
				<category><![CDATA[data joins]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.d3coder.com/?p=178</guid>
		<description><![CDATA[I just wanted to write a quick post showing a relatively simple example of importing and visualizing data stored in JSON format. I&#8217;m using shot location data from a basketball game between the Golden State Warriors and Denver Nuggets that took place recently. I wrote a Ruby scraper which pulls the shot data from ESPN (using the Nokogiri gem), and simply pipes it to a JSON data file (using the json gem). The JSON data looks like&#8230; [{"id":"4004005102","player_name":"Andre Iguodala","desc":"Made 17ft jumper 11:37 in 1st Qtr.","player_id":"2386","quarter":"1","x":"42","y":"86","made":"true","home":"h"},{"id":"4004005103","player_name":"Stephen Curry","desc":"Miss 22ft jumper 11:19 in 1st Qtr.","player_id":"3975","quarter":"1","x":"37","y":"25","made":"false","home":"a"},... Result The "h" and "a" stand for home &#8230;<span class="read-more"><a href="/2012/10/15/importing-json-data-into-d3/">Read more &#187;</a></span>]]></description>
			<content:encoded><![CDATA[<p>I just wanted to write a quick post showing a relatively simple example of importing and visualizing data stored in JSON format. I&#8217;m using shot location data from a basketball game between the Golden State Warriors and Denver Nuggets that took place recently. I wrote a Ruby scraper which pulls the shot data from ESPN (using the Nokogiri gem), and simply pipes it to a JSON data file (using the json gem). <span id="more-178"></span></p>
<p>The JSON data looks like&#8230;</p>
<pre><code>[{"id":"4004005102","player_name":"Andre  Iguodala","desc":"Made 17ft jumper 11:37 in 1st Qtr.","player_id":"2386","quarter":"1","x":"42","y":"86","made":"true","home":"h"},{"id":"4004005103","player_name":"Stephen Curry","desc":"Miss 22ft jumper 11:19 in 1st Qtr.","player_id":"3975","quarter":"1","x":"37","y":"25","made":"false","home":"a"},...</code></pre>
<h3>Result</h3>
<p>The "h" and "a" stand for home and away, respectively. Made shots are colored blue and missed shots are red. If you hover over a shot, a tooltip will give you some more detail about the shooter and time of the game.<br />
<iframe src="/js/shot_viz_small.html" width = 750, height =450></iframe></p>
<h3>Method</h3>
<p>First define the svg drawing area:</p>
<pre><code>var screenHeight = 400,
	screenWidth = 800,
	vis = d3.select("#chart").append("svg")
    .attr("width", screenWidth)
    .attr("height", screenHeight),
    padding = 20;
</code></pre>
<p>Next, call the function to import the data and set up a couple of scales to transform the raw (x,y) coordinates (given in ft) to screen coordinates:</p>
<pre><code>
d3.json("shots.json", function(shots) {
	var scaleScreenX = d3.scale.linear()
				.domain([0,100])
				.range([padding,screenWidth-padding]),
		scaleScreenY = d3.scale.linear()
				.domain([0,50])
				.range([padding,screenHeight-padding]);
</code></pre>
<p>Draw a filled circle for each shot by binding the data to svg elements:</p>
<pre><code>
	vis.selectAll("circle")
		.data(shots)
		.enter()
		.append("circle")
		.attr("cx", function(d) {
        	return scaleScreenY(d.y)+5*(Math.random()-0.5);
   			})
   		.attr("cy", function(d) {
        	return scaleScreenX(d.x)+5*(Math.random()-0.5);
   			})
   		.attr("r", function() {return 2*Math.random()+6;})
   		.attr("fill", function(d) { return d.desc.match(/Made/) ? "steelblue" : "red" })
   		.attr("opacity",0.60)
   		.attr("stroke-width", 2)
   		.attr("stroke","black")
   		.append("svg:title")
   		.text(function(d) { return d.player_name+" "+d.desc; });
</code></pre>
<p>And do the same thing for adding the text labels to each shot:</p>
<pre><code>
   	vis.selectAll("text")
   		.data(shots)
  	 	.enter()
   		.append("text")
   		.text(function(d) {
       		 return d.home;
  			 })
  		.attr("x", function(d) {
			return scaleScreenY(d.y)-2;
		   })
	    .attr("y", function(d) {
			return scaleScreenX(d.x) + 3;
	   		})
	   	.attr("font-family", "serif")
   		.attr("font-size", "12px")
   		.attr("fill", "black");
});
</code></pre>
<p>That&#8217;s it, really. In the future, I need to overlay the shots onto the background of an actual basketball court. Also, I&#8217;d like to add some filters for selecting games, players, teams, seasons, etc, and maybe add some whiz-bang D3 transitions in there as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.d3coder.com/2012/10/15/importing-json-data-into-d3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drawing Chemical Structures with Force Layout</title>
		<link>http://www.d3coder.com/2012/07/01/drawing-chemical-structures-with-force-layout/</link>
		<comments>http://www.d3coder.com/2012/07/01/drawing-chemical-structures-with-force-layout/#comments</comments>
		<pubDate>Sun, 01 Jul 2012 21:28:24 +0000</pubDate>
		<dc:creator>EvanZ</dc:creator>
				<category><![CDATA[layouts]]></category>

		<guid isPermaLink="false">http://www.d3coder.com/?p=133</guid>
		<description><![CDATA[I&#8217;m trying to learn how to use the different layouts in D3, so I can start making more sophisticated visualizations. The force layout lets you specify an array of nodes and links between those nodes, and the nature of the physical interaction between the nodes. I realized a few hours into playing around with it, that this could be a great toolbox for visualizing chemical structures. I&#8217;ll show you how in this post. Let&#8217;s draw Alanine! Alanine is the simplest amino acid, consisting of 3 carbons, 1 nitrogen, 2 oxygens, and 7 hydrogen atoms. Try clicking on an atom and &#8230;<span class="read-more"><a href="/2012/07/01/drawing-chemical-structures-with-force-layout/">Read more &#187;</a></span>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m trying to learn how to use the different layouts in D3, so I can start making more sophisticated visualizations. The <a href="https://github.com/mbostock/d3/wiki/Force-Layout">force layout</a> lets you specify an array of nodes and links between those nodes, and the nature of the physical interaction between the nodes. I realized a few hours into playing around with it, that this could be a great toolbox for visualizing chemical structures. I&#8217;ll show you how in this post.<span id="more-133"></span></p>
<p>Let&#8217;s draw <a href="http://en.wikipedia.org/wiki/Alanine">Alanine</a>! Alanine is the simplest amino acid, consisting of 3 carbons, 1 nitrogen, 2 oxygens, and 7 hydrogen atoms. <strong>Try clicking on an atom and dragging it!</strong></p>
<h3>Result</h3>
<p><iframe src="/js/orgo.html" width = 450, height =350></iframe></p>
<h3>Method</h3>
<p>To start, we need to define an array of nodes:</p>
<pre><code>			var nodes = [
				{"atom":"C", "size":12},
				{"atom":"C", "size":12},
				{"atom":"C", "size":12},
				{"atom":"N", "size":7},
				{"atom":"H", "size":1},
				{"atom":"O", "size":16},
				{"atom":"O", "size":16},
				{"atom":"H", "size":1},
				{"atom":"H", "size":1},
				{"atom":"H", "size":1},
				{"atom":"H", "size":1},
				{"atom":"H", "size":1},
				{"atom":"H", "size":1} 
			],
</code></pre>
<p>Each object in the array represents one of the atoms. We&#8217;ll use the &#8220;atom&#8221; key later to draw the atomic symbol, and we&#8217;ll use the &#8220;size&#8221; key to vary the size of the node, which will be represented by a circle. An array of links is needed to connect the atoms:</p>
<pre><code>

			links = [
				{"source":0, "target":1},
				{"source":1, "target":2},
				{"source":1, "target":3},
				{"source":2, "target":5},
				{"source":2, "target":6},
				{"source":1, "target":4},
				{"source":3, "target":10},
				{"source":3, "target":11},
				{"source":0, "target":7},
				{"source":0, "target":8},
				{"source":0, "target":9},
				{"source":5, "target":12}
			],
</code></pre>
<p>Next we start up the force layout, bring in the nodes and links, and set a few of the &#8220;physical&#8221; parameters:</p>
<pre><code>
			force = d3.layout.force()
				.nodes(nodes)
				.links(links)
				.charge(-1500)
				.friction(0.8)
				.gravity(0.5)
				.size([w,h])
				.start(),
</code></pre>
<p>Initialize the visualization elements for the links (&#8220;bonds&#8221;):</p>
<pre><code>
			link = vis.selectAll("line")
				.data(links)
				.enter()
					.append("line")
					.attr("class","link"),
</code></pre>
<p>And do the same for the nodes (&#8220;atoms&#8221;):</p>
<pre><code>
			node = vis.selectAll(".node")
				.data(nodes)
				.enter()
					.append("g")
					.attr("class","node")
					.call(force.drag); //allows you to drag atoms
			
				node.append("circle")
					.attr("r", function(d) {
						return Math.pow(40*d.size,1/3);
						})
					.attr("fill",function(d) {
						return fill(d.size);
						})
					.attr("stroke","black")
					.attr("stroke-width",2);
					
				node.append("text")
					.attr("dx",function(d) {
						return Math.pow(40*d.size,1/3)+1;
						})
					.attr("dy",".35em")
					.text(function(d) {return d.atom;});
</code></pre>
<p>At each &#8220;tick&#8221; of the simulation, the following function updates the viz:</p>
<pre><code>
			force.on("tick", function() {
				link.attr("x1", function(d) { return d.source.x; })
					.attr("y1", function(d) { return d.source.y; })
					.attr("x2", function(d) { return d.target.x; })
					.attr("y2", function(d) { return d.target.y; });
					
				node.attr("transform", function(d) { 
					return "translate(" + d.x + "," + d.y + ")"; 
					});		
				});
</code></pre>
<p>This is a pretty good start for a chemical visualization library. I need to figure out how to implement double and triple bonds, and also figure out how to simulate different bond strengths.</p>
<h3>Full Code</h3>
<div id="gist3029682" class="gist">
        <div class="gist-file">
          <div class="gist-data gist-syntax">
            



    <div class="file-data">
      <table cellpadding="0" cellspacing="0" class="lines highlight">
        <tr>
          <td class="line-numbers">
            <span class="line-number" id="file-gistfile1-html-L1" rel="file-gistfile1-html-L1">1</span>
            <span class="line-number" id="file-gistfile1-html-L2" rel="file-gistfile1-html-L2">2</span>
            <span class="line-number" id="file-gistfile1-html-L3" rel="file-gistfile1-html-L3">3</span>
            <span class="line-number" id="file-gistfile1-html-L4" rel="file-gistfile1-html-L4">4</span>
            <span class="line-number" id="file-gistfile1-html-L5" rel="file-gistfile1-html-L5">5</span>
            <span class="line-number" id="file-gistfile1-html-L6" rel="file-gistfile1-html-L6">6</span>
            <span class="line-number" id="file-gistfile1-html-L7" rel="file-gistfile1-html-L7">7</span>
            <span class="line-number" id="file-gistfile1-html-L8" rel="file-gistfile1-html-L8">8</span>
            <span class="line-number" id="file-gistfile1-html-L9" rel="file-gistfile1-html-L9">9</span>
            <span class="line-number" id="file-gistfile1-html-L10" rel="file-gistfile1-html-L10">10</span>
            <span class="line-number" id="file-gistfile1-html-L11" rel="file-gistfile1-html-L11">11</span>
            <span class="line-number" id="file-gistfile1-html-L12" rel="file-gistfile1-html-L12">12</span>
            <span class="line-number" id="file-gistfile1-html-L13" rel="file-gistfile1-html-L13">13</span>
            <span class="line-number" id="file-gistfile1-html-L14" rel="file-gistfile1-html-L14">14</span>
            <span class="line-number" id="file-gistfile1-html-L15" rel="file-gistfile1-html-L15">15</span>
            <span class="line-number" id="file-gistfile1-html-L16" rel="file-gistfile1-html-L16">16</span>
            <span class="line-number" id="file-gistfile1-html-L17" rel="file-gistfile1-html-L17">17</span>
            <span class="line-number" id="file-gistfile1-html-L18" rel="file-gistfile1-html-L18">18</span>
            <span class="line-number" id="file-gistfile1-html-L19" rel="file-gistfile1-html-L19">19</span>
            <span class="line-number" id="file-gistfile1-html-L20" rel="file-gistfile1-html-L20">20</span>
            <span class="line-number" id="file-gistfile1-html-L21" rel="file-gistfile1-html-L21">21</span>
            <span class="line-number" id="file-gistfile1-html-L22" rel="file-gistfile1-html-L22">22</span>
            <span class="line-number" id="file-gistfile1-html-L23" rel="file-gistfile1-html-L23">23</span>
            <span class="line-number" id="file-gistfile1-html-L24" rel="file-gistfile1-html-L24">24</span>
            <span class="line-number" id="file-gistfile1-html-L25" rel="file-gistfile1-html-L25">25</span>
            <span class="line-number" id="file-gistfile1-html-L26" rel="file-gistfile1-html-L26">26</span>
            <span class="line-number" id="file-gistfile1-html-L27" rel="file-gistfile1-html-L27">27</span>
            <span class="line-number" id="file-gistfile1-html-L28" rel="file-gistfile1-html-L28">28</span>
            <span class="line-number" id="file-gistfile1-html-L29" rel="file-gistfile1-html-L29">29</span>
            <span class="line-number" id="file-gistfile1-html-L30" rel="file-gistfile1-html-L30">30</span>
            <span class="line-number" id="file-gistfile1-html-L31" rel="file-gistfile1-html-L31">31</span>
            <span class="line-number" id="file-gistfile1-html-L32" rel="file-gistfile1-html-L32">32</span>
            <span class="line-number" id="file-gistfile1-html-L33" rel="file-gistfile1-html-L33">33</span>
            <span class="line-number" id="file-gistfile1-html-L34" rel="file-gistfile1-html-L34">34</span>
            <span class="line-number" id="file-gistfile1-html-L35" rel="file-gistfile1-html-L35">35</span>
            <span class="line-number" id="file-gistfile1-html-L36" rel="file-gistfile1-html-L36">36</span>
            <span class="line-number" id="file-gistfile1-html-L37" rel="file-gistfile1-html-L37">37</span>
            <span class="line-number" id="file-gistfile1-html-L38" rel="file-gistfile1-html-L38">38</span>
            <span class="line-number" id="file-gistfile1-html-L39" rel="file-gistfile1-html-L39">39</span>
            <span class="line-number" id="file-gistfile1-html-L40" rel="file-gistfile1-html-L40">40</span>
            <span class="line-number" id="file-gistfile1-html-L41" rel="file-gistfile1-html-L41">41</span>
            <span class="line-number" id="file-gistfile1-html-L42" rel="file-gistfile1-html-L42">42</span>
            <span class="line-number" id="file-gistfile1-html-L43" rel="file-gistfile1-html-L43">43</span>
            <span class="line-number" id="file-gistfile1-html-L44" rel="file-gistfile1-html-L44">44</span>
            <span class="line-number" id="file-gistfile1-html-L45" rel="file-gistfile1-html-L45">45</span>
            <span class="line-number" id="file-gistfile1-html-L46" rel="file-gistfile1-html-L46">46</span>
            <span class="line-number" id="file-gistfile1-html-L47" rel="file-gistfile1-html-L47">47</span>
            <span class="line-number" id="file-gistfile1-html-L48" rel="file-gistfile1-html-L48">48</span>
            <span class="line-number" id="file-gistfile1-html-L49" rel="file-gistfile1-html-L49">49</span>
            <span class="line-number" id="file-gistfile1-html-L50" rel="file-gistfile1-html-L50">50</span>
            <span class="line-number" id="file-gistfile1-html-L51" rel="file-gistfile1-html-L51">51</span>
            <span class="line-number" id="file-gistfile1-html-L52" rel="file-gistfile1-html-L52">52</span>
            <span class="line-number" id="file-gistfile1-html-L53" rel="file-gistfile1-html-L53">53</span>
            <span class="line-number" id="file-gistfile1-html-L54" rel="file-gistfile1-html-L54">54</span>
            <span class="line-number" id="file-gistfile1-html-L55" rel="file-gistfile1-html-L55">55</span>
            <span class="line-number" id="file-gistfile1-html-L56" rel="file-gistfile1-html-L56">56</span>
            <span class="line-number" id="file-gistfile1-html-L57" rel="file-gistfile1-html-L57">57</span>
            <span class="line-number" id="file-gistfile1-html-L58" rel="file-gistfile1-html-L58">58</span>
            <span class="line-number" id="file-gistfile1-html-L59" rel="file-gistfile1-html-L59">59</span>
            <span class="line-number" id="file-gistfile1-html-L60" rel="file-gistfile1-html-L60">60</span>
            <span class="line-number" id="file-gistfile1-html-L61" rel="file-gistfile1-html-L61">61</span>
            <span class="line-number" id="file-gistfile1-html-L62" rel="file-gistfile1-html-L62">62</span>
            <span class="line-number" id="file-gistfile1-html-L63" rel="file-gistfile1-html-L63">63</span>
            <span class="line-number" id="file-gistfile1-html-L64" rel="file-gistfile1-html-L64">64</span>
            <span class="line-number" id="file-gistfile1-html-L65" rel="file-gistfile1-html-L65">65</span>
            <span class="line-number" id="file-gistfile1-html-L66" rel="file-gistfile1-html-L66">66</span>
            <span class="line-number" id="file-gistfile1-html-L67" rel="file-gistfile1-html-L67">67</span>
            <span class="line-number" id="file-gistfile1-html-L68" rel="file-gistfile1-html-L68">68</span>
            <span class="line-number" id="file-gistfile1-html-L69" rel="file-gistfile1-html-L69">69</span>
            <span class="line-number" id="file-gistfile1-html-L70" rel="file-gistfile1-html-L70">70</span>
            <span class="line-number" id="file-gistfile1-html-L71" rel="file-gistfile1-html-L71">71</span>
            <span class="line-number" id="file-gistfile1-html-L72" rel="file-gistfile1-html-L72">72</span>
            <span class="line-number" id="file-gistfile1-html-L73" rel="file-gistfile1-html-L73">73</span>
            <span class="line-number" id="file-gistfile1-html-L74" rel="file-gistfile1-html-L74">74</span>
            <span class="line-number" id="file-gistfile1-html-L75" rel="file-gistfile1-html-L75">75</span>
            <span class="line-number" id="file-gistfile1-html-L76" rel="file-gistfile1-html-L76">76</span>
            <span class="line-number" id="file-gistfile1-html-L77" rel="file-gistfile1-html-L77">77</span>
            <span class="line-number" id="file-gistfile1-html-L78" rel="file-gistfile1-html-L78">78</span>
            <span class="line-number" id="file-gistfile1-html-L79" rel="file-gistfile1-html-L79">79</span>
            <span class="line-number" id="file-gistfile1-html-L80" rel="file-gistfile1-html-L80">80</span>
            <span class="line-number" id="file-gistfile1-html-L81" rel="file-gistfile1-html-L81">81</span>
            <span class="line-number" id="file-gistfile1-html-L82" rel="file-gistfile1-html-L82">82</span>
            <span class="line-number" id="file-gistfile1-html-L83" rel="file-gistfile1-html-L83">83</span>
            <span class="line-number" id="file-gistfile1-html-L84" rel="file-gistfile1-html-L84">84</span>
            <span class="line-number" id="file-gistfile1-html-L85" rel="file-gistfile1-html-L85">85</span>
            <span class="line-number" id="file-gistfile1-html-L86" rel="file-gistfile1-html-L86">86</span>
            <span class="line-number" id="file-gistfile1-html-L87" rel="file-gistfile1-html-L87">87</span>
            <span class="line-number" id="file-gistfile1-html-L88" rel="file-gistfile1-html-L88">88</span>
            <span class="line-number" id="file-gistfile1-html-L89" rel="file-gistfile1-html-L89">89</span>
            <span class="line-number" id="file-gistfile1-html-L90" rel="file-gistfile1-html-L90">90</span>
            <span class="line-number" id="file-gistfile1-html-L91" rel="file-gistfile1-html-L91">91</span>
            <span class="line-number" id="file-gistfile1-html-L92" rel="file-gistfile1-html-L92">92</span>
            <span class="line-number" id="file-gistfile1-html-L93" rel="file-gistfile1-html-L93">93</span>
            <span class="line-number" id="file-gistfile1-html-L94" rel="file-gistfile1-html-L94">94</span>
            <span class="line-number" id="file-gistfile1-html-L95" rel="file-gistfile1-html-L95">95</span>
            <span class="line-number" id="file-gistfile1-html-L96" rel="file-gistfile1-html-L96">96</span>
            <span class="line-number" id="file-gistfile1-html-L97" rel="file-gistfile1-html-L97">97</span>
            <span class="line-number" id="file-gistfile1-html-L98" rel="file-gistfile1-html-L98">98</span>
            <span class="line-number" id="file-gistfile1-html-L99" rel="file-gistfile1-html-L99">99</span>
            <span class="line-number" id="file-gistfile1-html-L100" rel="file-gistfile1-html-L100">100</span>
            <span class="line-number" id="file-gistfile1-html-L101" rel="file-gistfile1-html-L101">101</span>
            <span class="line-number" id="file-gistfile1-html-L102" rel="file-gistfile1-html-L102">102</span>
            <span class="line-number" id="file-gistfile1-html-L103" rel="file-gistfile1-html-L103">103</span>
            <span class="line-number" id="file-gistfile1-html-L104" rel="file-gistfile1-html-L104">104</span>
            <span class="line-number" id="file-gistfile1-html-L105" rel="file-gistfile1-html-L105">105</span>
            <span class="line-number" id="file-gistfile1-html-L106" rel="file-gistfile1-html-L106">106</span>
            <span class="line-number" id="file-gistfile1-html-L107" rel="file-gistfile1-html-L107">107</span>
            <span class="line-number" id="file-gistfile1-html-L108" rel="file-gistfile1-html-L108">108</span>
            <span class="line-number" id="file-gistfile1-html-L109" rel="file-gistfile1-html-L109">109</span>
            <span class="line-number" id="file-gistfile1-html-L110" rel="file-gistfile1-html-L110">110</span>
            <span class="line-number" id="file-gistfile1-html-L111" rel="file-gistfile1-html-L111">111</span>
            <span class="line-number" id="file-gistfile1-html-L112" rel="file-gistfile1-html-L112">112</span>
            <span class="line-number" id="file-gistfile1-html-L113" rel="file-gistfile1-html-L113">113</span>
            <span class="line-number" id="file-gistfile1-html-L114" rel="file-gistfile1-html-L114">114</span>
          </td>
          <td class="line-data">
            <pre class="line-pre"><div class="line" id="file-gistfile1-html-LC1"><span class="cp">&lt;!DOCTYPE html&gt;</span></div><div class="line" id="file-gistfile1-html-LC2"><span class="nt">&lt;html&gt;</span></div><div class="line" id="file-gistfile1-html-LC3">  <span class="nt">&lt;head&gt;</span></div><div class="line" id="file-gistfile1-html-LC4">    <span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;http://mbostock.github.com/d3/d3.v2.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class="line" id="file-gistfile1-html-LC5"><span class="nt">&lt;style </span><span class="na">type=</span><span class="s">&quot;text/css&quot;</span><span class="nt">&gt;</span></div><div class="line" id="file-gistfile1-html-LC6">	<span class="nc">.link</span> <span class="p">{</span></div><div class="line" id="file-gistfile1-html-LC7">  <span class="n">fill</span><span class="o">:</span> <span class="nb">steelblue</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC8">  <span class="n">stroke</span><span class="o">:</span> <span class="n">lightgray</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC9">  <span class="n">stroke</span><span class="o">-</span><span class="k">width</span><span class="o">:</span> <span class="m">2px</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC10"><span class="p">}</span></div><div class="line" id="file-gistfile1-html-LC11"> <span class="nc">.node</span> <span class="nt">circle</span> <span class="p">{</span></div><div class="line" id="file-gistfile1-html-LC12">  <span class="n">stroke</span><span class="o">:</span> <span class="k">solid</span> <span class="nb">black</span> <span class="m">1px</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC13"><span class="p">}</span></div><div class="line" id="file-gistfile1-html-LC14">&nbsp;</div><div class="line" id="file-gistfile1-html-LC15"><span class="nc">.node</span> <span class="nt">text</span> <span class="p">{</span></div><div class="line" id="file-gistfile1-html-LC16">  <span class="k">pointer</span><span class="o">-</span><span class="n">events</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC17">  <span class="k">font</span><span class="o">:</span> <span class="m">14px</span> <span class="k">sans-serif</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC18">  <span class="k">color</span><span class="o">:</span> <span class="nb">blue</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC19"><span class="p">}</span></div><div class="line" id="file-gistfile1-html-LC20"><span class="nt">&lt;/style&gt;</span></div><div class="line" id="file-gistfile1-html-LC21">  <span class="nt">&lt;/head&gt;</span></div><div class="line" id="file-gistfile1-html-LC22">	<span class="nt">&lt;body&gt;</span></div><div class="line" id="file-gistfile1-html-LC23">	<span class="nt">&lt;script&gt;</span></div><div class="line" id="file-gistfile1-html-LC24">		<span class="kd">var</span> <span class="nx">w</span><span class="o">=</span><span class="mi">300</span><span class="p">,</span> </div><div class="line" id="file-gistfile1-html-LC25">			<span class="nx">h</span><span class="o">=</span><span class="mi">300</span><span class="p">,</span></div><div class="line" id="file-gistfile1-html-LC26">			<span class="nx">fill</span> <span class="o">=</span> <span class="nx">d3</span><span class="p">.</span><span class="nx">scale</span><span class="p">.</span><span class="nx">category20</span><span class="p">(),</span></div><div class="line" id="file-gistfile1-html-LC27">			<span class="nx">nodes</span> <span class="o">=</span> <span class="p">[</span></div><div class="line" id="file-gistfile1-html-LC28">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;C&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">12</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC29">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;C&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">12</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC30">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;C&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">12</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC31">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;N&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">7</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC32">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;H&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC33">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;O&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">16</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC34">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;O&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">16</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC35">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;H&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC36">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;H&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC37">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;H&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC38">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;H&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC39">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;H&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC40">				<span class="p">{</span><span class="s2">&quot;atom&quot;</span><span class="o">:</span><span class="s2">&quot;H&quot;</span><span class="p">,</span> <span class="s2">&quot;size&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">}</span> </div><div class="line" id="file-gistfile1-html-LC41">			<span class="p">],</span></div><div class="line" id="file-gistfile1-html-LC42">			</div><div class="line" id="file-gistfile1-html-LC43">			<span class="nx">links</span> <span class="o">=</span> <span class="p">[</span></div><div class="line" id="file-gistfile1-html-LC44">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC45">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">2</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC46">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">3</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC47">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">2</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">5</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC48">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">2</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">6</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC49">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">4</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC50">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">3</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">10</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC51">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">3</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">11</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC52">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">7</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC53">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">8</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC54">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">9</span><span class="p">},</span></div><div class="line" id="file-gistfile1-html-LC55">				<span class="p">{</span><span class="s2">&quot;source&quot;</span><span class="o">:</span><span class="mi">5</span><span class="p">,</span> <span class="s2">&quot;target&quot;</span><span class="o">:</span><span class="mi">12</span><span class="p">}</span></div><div class="line" id="file-gistfile1-html-LC56">			<span class="p">],</span></div><div class="line" id="file-gistfile1-html-LC57">&nbsp;</div><div class="line" id="file-gistfile1-html-LC58">			<span class="nx">vis</span> <span class="o">=</span> <span class="nx">d3</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="s2">&quot;body&quot;</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;svg&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC59">				<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;width&quot;</span><span class="p">,</span> <span class="nx">w</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC60">				<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;height&quot;</span><span class="p">,</span> <span class="nx">h</span><span class="p">),</span></div><div class="line" id="file-gistfile1-html-LC61">			</div><div class="line" id="file-gistfile1-html-LC62">			<span class="nx">force</span> <span class="o">=</span> <span class="nx">d3</span><span class="p">.</span><span class="nx">layout</span><span class="p">.</span><span class="nx">force</span><span class="p">()</span></div><div class="line" id="file-gistfile1-html-LC63">				<span class="p">.</span><span class="nx">nodes</span><span class="p">(</span><span class="nx">nodes</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC64">				<span class="p">.</span><span class="nx">links</span><span class="p">(</span><span class="nx">links</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC65">				<span class="p">.</span><span class="nx">charge</span><span class="p">(</span><span class="o">-</span><span class="mi">1500</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC66">				<span class="p">.</span><span class="nx">friction</span><span class="p">(</span><span class="mf">0.8</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC67">				<span class="p">.</span><span class="nx">gravity</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC68">				<span class="p">.</span><span class="nx">size</span><span class="p">([</span><span class="nx">w</span><span class="p">,</span><span class="nx">h</span><span class="p">])</span></div><div class="line" id="file-gistfile1-html-LC69">				<span class="p">.</span><span class="nx">start</span><span class="p">(),</span></div><div class="line" id="file-gistfile1-html-LC70">							</div><div class="line" id="file-gistfile1-html-LC71">			<span class="nx">link</span> <span class="o">=</span> <span class="nx">vis</span><span class="p">.</span><span class="nx">selectAll</span><span class="p">(</span><span class="s2">&quot;line&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC72">				<span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="nx">links</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC73">				<span class="p">.</span><span class="nx">enter</span><span class="p">()</span></div><div class="line" id="file-gistfile1-html-LC74">					<span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;line&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC75">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;class&quot;</span><span class="p">,</span><span class="s2">&quot;link&quot;</span><span class="p">),</span></div><div class="line" id="file-gistfile1-html-LC76">					</div><div class="line" id="file-gistfile1-html-LC77">			<span class="nx">node</span> <span class="o">=</span> <span class="nx">vis</span><span class="p">.</span><span class="nx">selectAll</span><span class="p">(</span><span class="s2">&quot;.node&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC78">				<span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="nx">nodes</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC79">				<span class="p">.</span><span class="nx">enter</span><span class="p">()</span></div><div class="line" id="file-gistfile1-html-LC80">					<span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;g&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC81">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;class&quot;</span><span class="p">,</span><span class="s2">&quot;node&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC82">					<span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">force</span><span class="p">.</span><span class="nx">drag</span><span class="p">);</span></div><div class="line" id="file-gistfile1-html-LC83">			</div><div class="line" id="file-gistfile1-html-LC84">				<span class="nx">node</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;circle&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC85">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;r&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-gistfile1-html-LC86">						<span class="k">return</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mi">40</span><span class="o">*</span><span class="nx">d</span><span class="p">.</span><span class="nx">size</span><span class="p">,</span><span class="mi">1</span><span class="o">/</span><span class="mi">3</span><span class="p">);</span></div><div class="line" id="file-gistfile1-html-LC87">						<span class="p">})</span></div><div class="line" id="file-gistfile1-html-LC88">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;fill&quot;</span><span class="p">,</span><span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-gistfile1-html-LC89">						<span class="k">return</span> <span class="nx">fill</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">size</span><span class="p">);</span></div><div class="line" id="file-gistfile1-html-LC90">						<span class="p">})</span></div><div class="line" id="file-gistfile1-html-LC91">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;stroke&quot;</span><span class="p">,</span><span class="s2">&quot;black&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC92">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;stroke-width&quot;</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span></div><div class="line" id="file-gistfile1-html-LC93">					</div><div class="line" id="file-gistfile1-html-LC94">				<span class="nx">node</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC95">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;dx&quot;</span><span class="p">,</span><span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-gistfile1-html-LC96">						<span class="k">return</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mi">40</span><span class="o">*</span><span class="nx">d</span><span class="p">.</span><span class="nx">size</span><span class="p">,</span><span class="mi">1</span><span class="o">/</span><span class="mi">3</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC97">						<span class="p">})</span></div><div class="line" id="file-gistfile1-html-LC98">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;dy&quot;</span><span class="p">,</span><span class="s2">&quot;.35em&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC99">					<span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span><span class="k">return</span> <span class="nx">d</span><span class="p">.</span><span class="nx">atom</span><span class="p">;});</span></div><div class="line" id="file-gistfile1-html-LC100">				</div><div class="line" id="file-gistfile1-html-LC101">			<span class="nx">force</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;tick&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class="line" id="file-gistfile1-html-LC102">				<span class="nx">link</span><span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;x1&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">d</span><span class="p">.</span><span class="nx">source</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="p">})</span></div><div class="line" id="file-gistfile1-html-LC103">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;y1&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">d</span><span class="p">.</span><span class="nx">source</span><span class="p">.</span><span class="nx">y</span><span class="p">;</span> <span class="p">})</span></div><div class="line" id="file-gistfile1-html-LC104">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;x2&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">d</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="p">})</span></div><div class="line" id="file-gistfile1-html-LC105">					<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;y2&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">d</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">y</span><span class="p">;</span> <span class="p">});</span></div><div class="line" id="file-gistfile1-html-LC106">					</div><div class="line" id="file-gistfile1-html-LC107">				<span class="nx">node</span><span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;transform&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span> </div><div class="line" id="file-gistfile1-html-LC108">					<span class="k">return</span> <span class="s2">&quot;translate(&quot;</span> <span class="o">+</span> <span class="nx">d</span><span class="p">.</span><span class="nx">x</span> <span class="o">+</span> <span class="s2">&quot;,&quot;</span> <span class="o">+</span> <span class="nx">d</span><span class="p">.</span><span class="nx">y</span> <span class="o">+</span> <span class="s2">&quot;)&quot;</span><span class="p">;</span> </div><div class="line" id="file-gistfile1-html-LC109">					<span class="p">});</span>		</div><div class="line" id="file-gistfile1-html-LC110">				<span class="p">});</span></div><div class="line" id="file-gistfile1-html-LC111">		<span class="nt">&lt;/script&gt;</span></div><div class="line" id="file-gistfile1-html-LC112">		Chemical structure visualized using D3.</div><div class="line" id="file-gistfile1-html-LC113">	<span class="nt">&lt;/body&gt;</span></div><div class="line" id="file-gistfile1-html-LC114"><span class="nt">&lt;/html&gt;</span></div></pre>
          </td>
        </tr>
      </table>
    </div>

          </div>
          <div class="gist-meta">
            <a href="https://gist.github.com/EvanZ/3029682/raw/8501995c1e28d243e090a60fd0cd1130d20a22ae/gistfile1.html" style="float:right">view raw</a>
            <a href="https://gist.github.com/EvanZ/3029682#file-gistfile1-html">gistfile1.html</a>
            hosted with &#10084; by <a href="https://github.com">GitHub</a>
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.d3coder.com/2012/07/01/drawing-chemical-structures-with-force-layout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My First Chart</title>
		<link>http://www.d3coder.com/2012/06/17/my-first-chart/</link>
		<comments>http://www.d3coder.com/2012/06/17/my-first-chart/#comments</comments>
		<pubDate>Sun, 17 Jun 2012 23:24:18 +0000</pubDate>
		<dc:creator>EvanZ</dc:creator>
				<category><![CDATA[bar charts]]></category>
		<category><![CDATA[data joins]]></category>

		<guid isPermaLink="false">http://www.d3coder.com/?p=97</guid>
		<description><![CDATA[Let&#8217;s make a chart! I&#8217;m going to try to replicate a chart from Golden State of Mind where we were voting on players for a NBA draft board (fear not, you won&#8217;t have to know what the heck that means to follow along here). Here&#8217;s a snapshot of it: The Result Here&#8217;s my version followed by the code to implement it: The Method First, we define our dataset, which is an array of objects: var players = [ {"First": "Harrison", "Last": "Barnes", "Votes": 61}, {"First":"Andre", "Last":"Drummond", "Votes":27}, {"First":"Bradley", "Last":"Beal", "Votes":"58"}, {"First":"Meyers", "Last":"Leonard", "Votes":8}, {"First":"Jared", "Last":"Sullinger", "Votes":8}, {"First":"Damian", "Last":"Lillard", "Votes":5}, {"First":"Terrence", &#8230;<span class="read-more"><a href="/2012/06/17/my-first-chart/">Read more &#187;</a></span>]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s make a chart! I&#8217;m going to try to replicate a chart from Golden State of Mind where we were voting on players for a NBA draft board (fear not, you won&#8217;t have to know what the heck that means to follow along here). <span id="more-97"></span>Here&#8217;s a snapshot of it:</p>
<div id="attachment_98" class="wp-caption aligncenter" style="width: 310px"><a href="/wp-content/uploads/2012/06/Screen-Shot-2012-06-17-at-3.56.10-PM.png"><img src="/wp-content/uploads/2012/06/Screen-Shot-2012-06-17-at-3.56.10-PM-300x279.png" alt="" title="Screen Shot 2012-06-17 at 3.56.10 PM" width="300" height="279" class="size-medium wp-image-98" /></a><p class="wp-caption-text">Snapshot of poll results that we&#8217;re trying to replicate.</p></div>
<h1>The Result</h1>
<p>Here&#8217;s my version followed by the code to implement it:</p>
<p><iframe src="/js/draftboard.html" height="590"></iframe></p>
<h1>The Method</h1>
<p>First, we define our dataset, which is an array of objects:<br />
<code>
<pre>
var players = [
		{"First": "Harrison", "Last": "Barnes", "Votes": 61},
		{"First":"Andre", "Last":"Drummond", "Votes":27},
		{"First":"Bradley", "Last":"Beal", "Votes":"58"},
		{"First":"Meyers", "Last":"Leonard", "Votes":8},
		{"First":"Jared", "Last":"Sullinger", "Votes":8},
		{"First":"Damian", "Last":"Lillard", "Votes":5},
		{"First":"Terrence", "Last":"Jones", "Votes":2},
		{"First":"Austin", "Last":"Rivers", "Votes":2},
		{"First":"Perry", "Last":"Jones", "Votes":2},
		{"First":"Others","Last":"","Votes":6}
		],
</pre>
<p></code></p>
<p>There are surely many different ways to read in the data, but that one made sense to me. Now we&#8217;ll define some other variables that will help us make the chart later:<br />
<code>
<pre>
    maxVotes = Math.max.apply(Math,players.map(function(o){return o.Votes;})),
    minVotes = Math.min.apply(Math,players.map(function(o){return o.Votes;})),
    numVotes = players.reduce(function(a,b) {return a+parseInt(b.Votes);},0),
    xOffset = 50,
    numPlayers = players.length,
    svgHeight = numPlayers*55,
    svgWidth = maxVotes + 200,
    yOffset = svgHeight/(numPlayers+1);
</pre>
<p></code></p>
<p>We&#8217;ll sort the array by votes in descending order (actually this is an improvement over the above chart):<br />
<code>
<pre>
    function sorter(a,b) {
	 return b.Votes - a.Votes;
	 }
    players.sort(sorter);
</pre>
<p></code></p>
<p>Next we insert the svg and draw a border around it:<br />
<code>
<pre>
    d3.select("body").append("svg")
        .attr("width",svgWidth)
        .attr("height",svgHeight);
    d3.select("svg").append("rect")
        .attr("x",0)
        .attr("y",0)
        .attr("width",svgWidth)
        .attr("height",svgHeight)
        .attr("style","fill-opacity:0; stroke:black;stroke-width:6px");
</pre>
<p></code></p>
<p>Then we draw the bars:<br />
<code>
<pre>
    d3.select("svg")
        .append("g").attr("class","bars")
        .selectAll("rect")
        .data(players)
        .enter()
            .append("rect")
                .attr("x",xOffset)
                .attr("style","fill:gold; stroke:black")
                .attr("y", function(d,i) {
                    return yOffset*i+50;
                    })
                .attr("width", function(d,i) {
                    return  d.Votes*2;
                    })
                .attr("height", 20);
</pre>
<p></code></p>
<p>Followed by the labels above each bar containing player names:<br />
<code>
<pre>
    d3.select("svg")
        .append("g").attr("class","names")
        .selectAll("text")
        .data(players)
        .enter()
            .append("text")
                .attr("x", xOffset)
                .attr("y", function(d,i) {
                    return yOffset*i+45;
                    })
                .attr("style","font-family: sans-serif; font-size: 12pt")
                .text(function(d) {return d.First+" "+d.Last;});
</pre>
<p></code></p>
<p>The number of votes each player received is placed to the right of the bars (I think an improvement in readability over the original):<br />
<code>
<pre>
    d3.select("svg")
        .append("g").attr("class","votes")
        .selectAll("text")
        .data(players)
        .enter()
            .append("text")
                .attr("x", function(d,i) {
                    return xOffset+d.Votes*2+5;
                    })
                .attr("y", function(d,i) {
                    return yOffset*i+45+20;
                    })
                .attr("style","font-family: sans-serif; font-size: 10pt")
                .text(function(d) {return parseInt(d.Votes) + " votes";});
</pre>
<p></code></p>
<p>And the % of total votes each player received is placed to the left of the bars:<br />
<code>
<pre>
    d3.select("svg")
        .append("g").attr("class","percent")
        .selectAll("text")
        .data(players)
        .enter()
            .append("text")
                .attr("x", 10)
                .attr("y", function(d,i) {
                    return yOffset*i+45+20;
                    })
                .attr("style","font-family: sans-serif; font-size: 10pt")
                .text(function(d) {
                    return d3.format(".2p")(d.Votes/numVotes);
                    });
</pre>
<p></code></p>
<p>Finally, I put the total number of votes below the chart:<br />
<code>
<pre>
    d3.select("body").append("div")
        .text(numVotes+" votes");
</pre>
<p></code></p>
<h1>Code</h1>
<div id="gist2946791" class="gist">
        <div class="gist-file">
          <div class="gist-data gist-syntax">
            



    <div class="file-data">
      <table cellpadding="0" cellspacing="0" class="lines highlight">
        <tr>
          <td class="line-numbers">
            <span class="line-number" id="file-index-html-L1" rel="file-index-html-L1">1</span>
            <span class="line-number" id="file-index-html-L2" rel="file-index-html-L2">2</span>
            <span class="line-number" id="file-index-html-L3" rel="file-index-html-L3">3</span>
            <span class="line-number" id="file-index-html-L4" rel="file-index-html-L4">4</span>
            <span class="line-number" id="file-index-html-L5" rel="file-index-html-L5">5</span>
            <span class="line-number" id="file-index-html-L6" rel="file-index-html-L6">6</span>
            <span class="line-number" id="file-index-html-L7" rel="file-index-html-L7">7</span>
            <span class="line-number" id="file-index-html-L8" rel="file-index-html-L8">8</span>
            <span class="line-number" id="file-index-html-L9" rel="file-index-html-L9">9</span>
            <span class="line-number" id="file-index-html-L10" rel="file-index-html-L10">10</span>
            <span class="line-number" id="file-index-html-L11" rel="file-index-html-L11">11</span>
            <span class="line-number" id="file-index-html-L12" rel="file-index-html-L12">12</span>
            <span class="line-number" id="file-index-html-L13" rel="file-index-html-L13">13</span>
            <span class="line-number" id="file-index-html-L14" rel="file-index-html-L14">14</span>
            <span class="line-number" id="file-index-html-L15" rel="file-index-html-L15">15</span>
            <span class="line-number" id="file-index-html-L16" rel="file-index-html-L16">16</span>
            <span class="line-number" id="file-index-html-L17" rel="file-index-html-L17">17</span>
            <span class="line-number" id="file-index-html-L18" rel="file-index-html-L18">18</span>
            <span class="line-number" id="file-index-html-L19" rel="file-index-html-L19">19</span>
            <span class="line-number" id="file-index-html-L20" rel="file-index-html-L20">20</span>
            <span class="line-number" id="file-index-html-L21" rel="file-index-html-L21">21</span>
            <span class="line-number" id="file-index-html-L22" rel="file-index-html-L22">22</span>
            <span class="line-number" id="file-index-html-L23" rel="file-index-html-L23">23</span>
            <span class="line-number" id="file-index-html-L24" rel="file-index-html-L24">24</span>
            <span class="line-number" id="file-index-html-L25" rel="file-index-html-L25">25</span>
            <span class="line-number" id="file-index-html-L26" rel="file-index-html-L26">26</span>
            <span class="line-number" id="file-index-html-L27" rel="file-index-html-L27">27</span>
            <span class="line-number" id="file-index-html-L28" rel="file-index-html-L28">28</span>
            <span class="line-number" id="file-index-html-L29" rel="file-index-html-L29">29</span>
            <span class="line-number" id="file-index-html-L30" rel="file-index-html-L30">30</span>
            <span class="line-number" id="file-index-html-L31" rel="file-index-html-L31">31</span>
            <span class="line-number" id="file-index-html-L32" rel="file-index-html-L32">32</span>
            <span class="line-number" id="file-index-html-L33" rel="file-index-html-L33">33</span>
            <span class="line-number" id="file-index-html-L34" rel="file-index-html-L34">34</span>
            <span class="line-number" id="file-index-html-L35" rel="file-index-html-L35">35</span>
            <span class="line-number" id="file-index-html-L36" rel="file-index-html-L36">36</span>
            <span class="line-number" id="file-index-html-L37" rel="file-index-html-L37">37</span>
            <span class="line-number" id="file-index-html-L38" rel="file-index-html-L38">38</span>
            <span class="line-number" id="file-index-html-L39" rel="file-index-html-L39">39</span>
            <span class="line-number" id="file-index-html-L40" rel="file-index-html-L40">40</span>
            <span class="line-number" id="file-index-html-L41" rel="file-index-html-L41">41</span>
            <span class="line-number" id="file-index-html-L42" rel="file-index-html-L42">42</span>
            <span class="line-number" id="file-index-html-L43" rel="file-index-html-L43">43</span>
            <span class="line-number" id="file-index-html-L44" rel="file-index-html-L44">44</span>
            <span class="line-number" id="file-index-html-L45" rel="file-index-html-L45">45</span>
            <span class="line-number" id="file-index-html-L46" rel="file-index-html-L46">46</span>
            <span class="line-number" id="file-index-html-L47" rel="file-index-html-L47">47</span>
            <span class="line-number" id="file-index-html-L48" rel="file-index-html-L48">48</span>
            <span class="line-number" id="file-index-html-L49" rel="file-index-html-L49">49</span>
            <span class="line-number" id="file-index-html-L50" rel="file-index-html-L50">50</span>
            <span class="line-number" id="file-index-html-L51" rel="file-index-html-L51">51</span>
            <span class="line-number" id="file-index-html-L52" rel="file-index-html-L52">52</span>
            <span class="line-number" id="file-index-html-L53" rel="file-index-html-L53">53</span>
            <span class="line-number" id="file-index-html-L54" rel="file-index-html-L54">54</span>
            <span class="line-number" id="file-index-html-L55" rel="file-index-html-L55">55</span>
            <span class="line-number" id="file-index-html-L56" rel="file-index-html-L56">56</span>
            <span class="line-number" id="file-index-html-L57" rel="file-index-html-L57">57</span>
            <span class="line-number" id="file-index-html-L58" rel="file-index-html-L58">58</span>
            <span class="line-number" id="file-index-html-L59" rel="file-index-html-L59">59</span>
            <span class="line-number" id="file-index-html-L60" rel="file-index-html-L60">60</span>
            <span class="line-number" id="file-index-html-L61" rel="file-index-html-L61">61</span>
            <span class="line-number" id="file-index-html-L62" rel="file-index-html-L62">62</span>
            <span class="line-number" id="file-index-html-L63" rel="file-index-html-L63">63</span>
            <span class="line-number" id="file-index-html-L64" rel="file-index-html-L64">64</span>
            <span class="line-number" id="file-index-html-L65" rel="file-index-html-L65">65</span>
            <span class="line-number" id="file-index-html-L66" rel="file-index-html-L66">66</span>
            <span class="line-number" id="file-index-html-L67" rel="file-index-html-L67">67</span>
            <span class="line-number" id="file-index-html-L68" rel="file-index-html-L68">68</span>
            <span class="line-number" id="file-index-html-L69" rel="file-index-html-L69">69</span>
            <span class="line-number" id="file-index-html-L70" rel="file-index-html-L70">70</span>
            <span class="line-number" id="file-index-html-L71" rel="file-index-html-L71">71</span>
            <span class="line-number" id="file-index-html-L72" rel="file-index-html-L72">72</span>
            <span class="line-number" id="file-index-html-L73" rel="file-index-html-L73">73</span>
            <span class="line-number" id="file-index-html-L74" rel="file-index-html-L74">74</span>
            <span class="line-number" id="file-index-html-L75" rel="file-index-html-L75">75</span>
            <span class="line-number" id="file-index-html-L76" rel="file-index-html-L76">76</span>
            <span class="line-number" id="file-index-html-L77" rel="file-index-html-L77">77</span>
            <span class="line-number" id="file-index-html-L78" rel="file-index-html-L78">78</span>
            <span class="line-number" id="file-index-html-L79" rel="file-index-html-L79">79</span>
            <span class="line-number" id="file-index-html-L80" rel="file-index-html-L80">80</span>
            <span class="line-number" id="file-index-html-L81" rel="file-index-html-L81">81</span>
            <span class="line-number" id="file-index-html-L82" rel="file-index-html-L82">82</span>
            <span class="line-number" id="file-index-html-L83" rel="file-index-html-L83">83</span>
            <span class="line-number" id="file-index-html-L84" rel="file-index-html-L84">84</span>
            <span class="line-number" id="file-index-html-L85" rel="file-index-html-L85">85</span>
            <span class="line-number" id="file-index-html-L86" rel="file-index-html-L86">86</span>
            <span class="line-number" id="file-index-html-L87" rel="file-index-html-L87">87</span>
            <span class="line-number" id="file-index-html-L88" rel="file-index-html-L88">88</span>
            <span class="line-number" id="file-index-html-L89" rel="file-index-html-L89">89</span>
            <span class="line-number" id="file-index-html-L90" rel="file-index-html-L90">90</span>
            <span class="line-number" id="file-index-html-L91" rel="file-index-html-L91">91</span>
            <span class="line-number" id="file-index-html-L92" rel="file-index-html-L92">92</span>
            <span class="line-number" id="file-index-html-L93" rel="file-index-html-L93">93</span>
            <span class="line-number" id="file-index-html-L94" rel="file-index-html-L94">94</span>
            <span class="line-number" id="file-index-html-L95" rel="file-index-html-L95">95</span>
            <span class="line-number" id="file-index-html-L96" rel="file-index-html-L96">96</span>
            <span class="line-number" id="file-index-html-L97" rel="file-index-html-L97">97</span>
            <span class="line-number" id="file-index-html-L98" rel="file-index-html-L98">98</span>
            <span class="line-number" id="file-index-html-L99" rel="file-index-html-L99">99</span>
            <span class="line-number" id="file-index-html-L100" rel="file-index-html-L100">100</span>
            <span class="line-number" id="file-index-html-L101" rel="file-index-html-L101">101</span>
            <span class="line-number" id="file-index-html-L102" rel="file-index-html-L102">102</span>
          </td>
          <td class="line-data">
            <pre class="line-pre"><div class="line" id="file-index-html-LC1"><span class="cp">&lt;!DOCTYPE html&gt;</span></div><div class="line" id="file-index-html-LC2"><span class="nt">&lt;html&gt;</span></div><div class="line" id="file-index-html-LC3">  <span class="nt">&lt;head&gt;</span></div><div class="line" id="file-index-html-LC4">    <span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;http://mbostock.github.com/d3/d3.v2.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class="line" id="file-index-html-LC5"><span class="nt">&lt;style </span><span class="na">type=</span><span class="s">&quot;text/css&quot;</span><span class="nt">&gt;</span></div><div class="line" id="file-index-html-LC6"><span class="nt">&lt;/style&gt;</span></div><div class="line" id="file-index-html-LC7">  <span class="nt">&lt;/head&gt;</span></div><div class="line" id="file-index-html-LC8">    <span class="nt">&lt;body&gt;</span></div><div class="line" id="file-index-html-LC9">    <span class="nt">&lt;script&gt;</span></div><div class="line" id="file-index-html-LC10">    <span class="kd">var</span> <span class="nx">players</span> <span class="o">=</span> <span class="p">[</span></div><div class="line" id="file-index-html-LC11">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span> <span class="s2">&quot;Harrison&quot;</span><span class="p">,</span> <span class="s2">&quot;Last&quot;</span><span class="o">:</span> <span class="s2">&quot;Barnes&quot;</span><span class="p">,</span> <span class="s2">&quot;Votes&quot;</span><span class="o">:</span> <span class="mi">61</span><span class="p">},</span></div><div class="line" id="file-index-html-LC12">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span><span class="s2">&quot;Andre&quot;</span><span class="p">,</span> <span class="s2">&quot;Last&quot;</span><span class="o">:</span><span class="s2">&quot;Drummond&quot;</span><span class="p">,</span> <span class="s2">&quot;Votes&quot;</span><span class="o">:</span><span class="mi">27</span><span class="p">},</span></div><div class="line" id="file-index-html-LC13">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span><span class="s2">&quot;Bradley&quot;</span><span class="p">,</span> <span class="s2">&quot;Last&quot;</span><span class="o">:</span><span class="s2">&quot;Beal&quot;</span><span class="p">,</span> <span class="s2">&quot;Votes&quot;</span><span class="o">:</span><span class="s2">&quot;58&quot;</span><span class="p">},</span></div><div class="line" id="file-index-html-LC14">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span><span class="s2">&quot;Meyers&quot;</span><span class="p">,</span> <span class="s2">&quot;Last&quot;</span><span class="o">:</span><span class="s2">&quot;Leonard&quot;</span><span class="p">,</span> <span class="s2">&quot;Votes&quot;</span><span class="o">:</span><span class="mi">8</span><span class="p">},</span></div><div class="line" id="file-index-html-LC15">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span><span class="s2">&quot;Jared&quot;</span><span class="p">,</span> <span class="s2">&quot;Last&quot;</span><span class="o">:</span><span class="s2">&quot;Sullinger&quot;</span><span class="p">,</span> <span class="s2">&quot;Votes&quot;</span><span class="o">:</span><span class="mi">8</span><span class="p">},</span></div><div class="line" id="file-index-html-LC16">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span><span class="s2">&quot;Damian&quot;</span><span class="p">,</span> <span class="s2">&quot;Last&quot;</span><span class="o">:</span><span class="s2">&quot;Lillard&quot;</span><span class="p">,</span> <span class="s2">&quot;Votes&quot;</span><span class="o">:</span><span class="mi">5</span><span class="p">},</span></div><div class="line" id="file-index-html-LC17">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span><span class="s2">&quot;Terrence&quot;</span><span class="p">,</span> <span class="s2">&quot;Last&quot;</span><span class="o">:</span><span class="s2">&quot;Jones&quot;</span><span class="p">,</span> <span class="s2">&quot;Votes&quot;</span><span class="o">:</span><span class="mi">2</span><span class="p">},</span></div><div class="line" id="file-index-html-LC18">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span><span class="s2">&quot;Austin&quot;</span><span class="p">,</span> <span class="s2">&quot;Last&quot;</span><span class="o">:</span><span class="s2">&quot;Rivers&quot;</span><span class="p">,</span> <span class="s2">&quot;Votes&quot;</span><span class="o">:</span><span class="mi">2</span><span class="p">},</span></div><div class="line" id="file-index-html-LC19">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span><span class="s2">&quot;Perry&quot;</span><span class="p">,</span> <span class="s2">&quot;Last&quot;</span><span class="o">:</span><span class="s2">&quot;Jones&quot;</span><span class="p">,</span> <span class="s2">&quot;Votes&quot;</span><span class="o">:</span><span class="mi">2</span><span class="p">},</span></div><div class="line" id="file-index-html-LC20">        <span class="p">{</span><span class="s2">&quot;First&quot;</span><span class="o">:</span><span class="s2">&quot;Others&quot;</span><span class="p">,</span><span class="s2">&quot;Last&quot;</span><span class="o">:</span><span class="s2">&quot;&quot;</span><span class="p">,</span><span class="s2">&quot;Votes&quot;</span><span class="o">:</span><span class="mi">6</span><span class="p">}</span></div><div class="line" id="file-index-html-LC21">        <span class="p">],</span></div><div class="line" id="file-index-html-LC22">     <span class="nx">maxVotes</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">max</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nb">Math</span><span class="p">,</span><span class="nx">players</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">o</span><span class="p">){</span><span class="k">return</span> <span class="nx">o</span><span class="p">.</span><span class="nx">Votes</span><span class="p">;})),</span></div><div class="line" id="file-index-html-LC23">     <span class="nx">minVotes</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">min</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nb">Math</span><span class="p">,</span><span class="nx">players</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">o</span><span class="p">){</span><span class="k">return</span> <span class="nx">o</span><span class="p">.</span><span class="nx">Votes</span><span class="p">;})),</span></div><div class="line" id="file-index-html-LC24">     <span class="nx">numVotes</span> <span class="o">=</span> <span class="nx">players</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">)</span> <span class="p">{</span><span class="k">return</span> <span class="nx">a</span><span class="o">+</span><span class="nb">parseInt</span><span class="p">(</span><span class="nx">b</span><span class="p">.</span><span class="nx">Votes</span><span class="p">);},</span><span class="mi">0</span><span class="p">),</span></div><div class="line" id="file-index-html-LC25">     <span class="nx">xOffset</span> <span class="o">=</span> <span class="mi">50</span><span class="p">,</span></div><div class="line" id="file-index-html-LC26">     <span class="nx">numPlayers</span> <span class="o">=</span> <span class="nx">players</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span></div><div class="line" id="file-index-html-LC27">     <span class="nx">svgHeight</span> <span class="o">=</span> <span class="nx">numPlayers</span><span class="o">*</span><span class="mi">55</span><span class="p">,</span></div><div class="line" id="file-index-html-LC28">     <span class="nx">svgWidth</span> <span class="o">=</span> <span class="nx">maxVotes</span> <span class="o">+</span> <span class="mi">200</span><span class="p">,</span></div><div class="line" id="file-index-html-LC29">     <span class="nx">yOffset</span> <span class="o">=</span> <span class="nx">svgHeight</span><span class="o">/</span><span class="p">(</span><span class="nx">numPlayers</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span></div><div class="line" id="file-index-html-LC30">     <span class="kd">function</span> <span class="nx">sorter</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-index-html-LC31">        <span class="k">return</span> <span class="nx">b</span><span class="p">.</span><span class="nx">Votes</span> <span class="o">-</span> <span class="nx">a</span><span class="p">.</span><span class="nx">Votes</span><span class="p">;</span></div><div class="line" id="file-index-html-LC32">        <span class="p">}</span></div><div class="line" id="file-index-html-LC33">     <span class="nx">players</span><span class="p">.</span><span class="nx">sort</span><span class="p">(</span><span class="nx">sorter</span><span class="p">);</span></div><div class="line" id="file-index-html-LC34">    <span class="nx">d3</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="s2">&quot;body&quot;</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;svg&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC35">        <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;width&quot;</span><span class="p">,</span><span class="nx">svgWidth</span><span class="p">)</span></div><div class="line" id="file-index-html-LC36">        <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;height&quot;</span><span class="p">,</span><span class="nx">svgHeight</span><span class="p">);</span></div><div class="line" id="file-index-html-LC37">    <span class="nx">d3</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="s2">&quot;svg&quot;</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;rect&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC38">        <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;x&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span></div><div class="line" id="file-index-html-LC39">        <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;y&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span></div><div class="line" id="file-index-html-LC40">        <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;width&quot;</span><span class="p">,</span><span class="nx">svgWidth</span><span class="p">)</span></div><div class="line" id="file-index-html-LC41">        <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;height&quot;</span><span class="p">,</span><span class="nx">svgHeight</span><span class="p">)</span></div><div class="line" id="file-index-html-LC42">        <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;style&quot;</span><span class="p">,</span><span class="s2">&quot;fill-opacity:0; stroke:black;stroke-width:6px&quot;</span><span class="p">);</span></div><div class="line" id="file-index-html-LC43">    <span class="nx">d3</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="s2">&quot;svg&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC44">        <span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;g&quot;</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;class&quot;</span><span class="p">,</span><span class="s2">&quot;bars&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC45">        <span class="p">.</span><span class="nx">selectAll</span><span class="p">(</span><span class="s2">&quot;rect&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC46">        <span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="nx">players</span><span class="p">)</span></div><div class="line" id="file-index-html-LC47">        <span class="p">.</span><span class="nx">enter</span><span class="p">()</span></div><div class="line" id="file-index-html-LC48">            <span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;rect&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC49">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;x&quot;</span><span class="p">,</span><span class="nx">xOffset</span><span class="p">)</span></div><div class="line" id="file-index-html-LC50">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;style&quot;</span><span class="p">,</span><span class="s2">&quot;fill:gold; stroke:black&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC51">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;y&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">,</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-index-html-LC52">                    <span class="k">return</span> <span class="nx">yOffset</span><span class="o">*</span><span class="nx">i</span><span class="o">+</span><span class="mi">50</span><span class="p">;</span></div><div class="line" id="file-index-html-LC53">                    <span class="p">})</span></div><div class="line" id="file-index-html-LC54">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;width&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">,</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-index-html-LC55">                    <span class="k">return</span>  <span class="nx">d</span><span class="p">.</span><span class="nx">Votes</span><span class="o">*</span><span class="mi">2</span><span class="p">;</span></div><div class="line" id="file-index-html-LC56">                    <span class="p">})</span></div><div class="line" id="file-index-html-LC57">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;height&quot;</span><span class="p">,</span> <span class="mi">20</span><span class="p">);</span></div><div class="line" id="file-index-html-LC58">    <span class="nx">d3</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="s2">&quot;svg&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC59">        <span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;g&quot;</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;class&quot;</span><span class="p">,</span><span class="s2">&quot;names&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC60">        <span class="p">.</span><span class="nx">selectAll</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC61">        <span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="nx">players</span><span class="p">)</span></div><div class="line" id="file-index-html-LC62">        <span class="p">.</span><span class="nx">enter</span><span class="p">()</span></div><div class="line" id="file-index-html-LC63">            <span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC64">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;x&quot;</span><span class="p">,</span> <span class="nx">xOffset</span><span class="p">)</span></div><div class="line" id="file-index-html-LC65">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;y&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">,</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-index-html-LC66">                    <span class="k">return</span> <span class="nx">yOffset</span><span class="o">*</span><span class="nx">i</span><span class="o">+</span><span class="mi">45</span><span class="p">;</span></div><div class="line" id="file-index-html-LC67">                    <span class="p">})</span></div><div class="line" id="file-index-html-LC68">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;style&quot;</span><span class="p">,</span><span class="s2">&quot;font-family: sans-serif; font-size: 12pt&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC69">                <span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span><span class="k">return</span> <span class="nx">d</span><span class="p">.</span><span class="nx">First</span><span class="o">+</span><span class="s2">&quot; &quot;</span><span class="o">+</span><span class="nx">d</span><span class="p">.</span><span class="nx">Last</span><span class="p">;});</span></div><div class="line" id="file-index-html-LC70">    <span class="nx">d3</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="s2">&quot;svg&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC71">        <span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;g&quot;</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;class&quot;</span><span class="p">,</span><span class="s2">&quot;votes&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC72">        <span class="p">.</span><span class="nx">selectAll</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC73">        <span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="nx">players</span><span class="p">)</span></div><div class="line" id="file-index-html-LC74">        <span class="p">.</span><span class="nx">enter</span><span class="p">()</span></div><div class="line" id="file-index-html-LC75">            <span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC76">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;x&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">,</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-index-html-LC77">                    <span class="k">return</span> <span class="nx">xOffset</span><span class="o">+</span><span class="nx">d</span><span class="p">.</span><span class="nx">Votes</span><span class="o">*</span><span class="mi">2</span><span class="o">+</span><span class="mi">5</span><span class="p">;</span></div><div class="line" id="file-index-html-LC78">                    <span class="p">})</span></div><div class="line" id="file-index-html-LC79">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;y&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">,</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-index-html-LC80">                    <span class="k">return</span> <span class="nx">yOffset</span><span class="o">*</span><span class="nx">i</span><span class="o">+</span><span class="mi">45</span><span class="o">+</span><span class="mi">20</span><span class="p">;</span></div><div class="line" id="file-index-html-LC81">                    <span class="p">})</span></div><div class="line" id="file-index-html-LC82">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;style&quot;</span><span class="p">,</span><span class="s2">&quot;font-family: sans-serif; font-size: 10pt&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC83">                <span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span><span class="k">return</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">Votes</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; votes&quot;</span><span class="p">;});</span></div><div class="line" id="file-index-html-LC84">    <span class="nx">d3</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="s2">&quot;svg&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC85">        <span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;g&quot;</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;class&quot;</span><span class="p">,</span><span class="s2">&quot;percent&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC86">        <span class="p">.</span><span class="nx">selectAll</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC87">        <span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="nx">players</span><span class="p">)</span></div><div class="line" id="file-index-html-LC88">        <span class="p">.</span><span class="nx">enter</span><span class="p">()</span></div><div class="line" id="file-index-html-LC89">            <span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC90">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;x&quot;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span></div><div class="line" id="file-index-html-LC91">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;y&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">,</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-index-html-LC92">                    <span class="k">return</span> <span class="nx">yOffset</span><span class="o">*</span><span class="nx">i</span><span class="o">+</span><span class="mi">45</span><span class="o">+</span><span class="mi">20</span><span class="p">;</span></div><div class="line" id="file-index-html-LC93">                    <span class="p">})</span></div><div class="line" id="file-index-html-LC94">                <span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;style&quot;</span><span class="p">,</span><span class="s2">&quot;font-family: sans-serif; font-size: 10pt&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC95">                <span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-index-html-LC96">                    <span class="k">return</span> <span class="nx">d3</span><span class="p">.</span><span class="nx">format</span><span class="p">(</span><span class="s2">&quot;.2p&quot;</span><span class="p">)(</span><span class="nx">d</span><span class="p">.</span><span class="nx">Votes</span><span class="o">/</span><span class="nx">numVotes</span><span class="p">);</span></div><div class="line" id="file-index-html-LC97">                    <span class="p">});</span></div><div class="line" id="file-index-html-LC98">    <span class="nx">d3</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="s2">&quot;body&quot;</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;div&quot;</span><span class="p">)</span></div><div class="line" id="file-index-html-LC99">        <span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="nx">numVotes</span><span class="o">+</span><span class="s2">&quot; votes&quot;</span><span class="p">);</span></div><div class="line" id="file-index-html-LC100">    <span class="nt">&lt;/script&gt;</span></div><div class="line" id="file-index-html-LC101">    <span class="nt">&lt;/body&gt;</span></div><div class="line" id="file-index-html-LC102"><span class="nt">&lt;/html&gt;</span></div></pre>
          </td>
        </tr>
      </table>
    </div>

          </div>
          <div class="gist-meta">
            <a href="https://gist.github.com/EvanZ/2946791/raw/33c386340c142240cc18449483c48796ca892198/index.html" style="float:right">view raw</a>
            <a href="https://gist.github.com/EvanZ/2946791#file-index-html">index.html</a>
            hosted with &#10084; by <a href="https://github.com">GitHub</a>
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.d3coder.com/2012/06/17/my-first-chart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started with Data Joins</title>
		<link>http://www.d3coder.com/2012/06/16/getting-started-with-data-joins/</link>
		<comments>http://www.d3coder.com/2012/06/16/getting-started-with-data-joins/#comments</comments>
		<pubDate>Sat, 16 Jun 2012 07:27:23 +0000</pubDate>
		<dc:creator>EvanZ</dc:creator>
				<category><![CDATA[data joins]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.d3coder.com/?p=56</guid>
		<description><![CDATA[To begin to use d3 effectively, I realize I need to have a very good grasp of how data joins work. The idea of the &#8220;data join&#8221; is essentially what it sounds like: the act of joining (or binding) data to DOM elements. Here I&#8217;ll demonstrate a few things I&#8217;ve picked up so far. You can follow along here by creating an empty HTML document, loading it with Chrome, and using the JavaScript console to type the commands and see the results. First, let&#8217;s create an array containing some data. In this case, a set of names: > var names &#8230;<span class="read-more"><a href="/2012/06/16/getting-started-with-data-joins/">Read more &#187;</a></span>]]></description>
			<content:encoded><![CDATA[<p>To begin to use d3 effectively, I realize I need to have a very good grasp of <a href="http://mbostock.github.com/d3/tutorial/circle.html">how data joins work</a>. The idea of the &#8220;data join&#8221; is essentially what it sounds like: the act of joining (or binding) data to DOM elements. Here I&#8217;ll demonstrate a few things I&#8217;ve picked up so far. <span id="more-56"></span>You can follow along here by creating an empty HTML document, loading it with Chrome, and using the JavaScript console to type the commands and see the results.</p>
<p>First, let&#8217;s create an array containing some data. In this case, a set of names:</p>
<p><code>> var names = ["Sam", "Diane", "Norm", "Cliff"]</code></p>
<p>Now we create an empty table row that we can populate with these names:</p>
<p><code>> d3.select("body").append("table").append("tr")</code></p>
<p>This should produce an empty table containing one (empty) row in the html:<br />
<code><br />
...<br />
&lt;table&gt;<br />
&lt;tr&gt;&lt;/tr&gt;<br />
&lt;table&gt;<br />
...<br />
</code></p>
<p>Before joining data to the table the &#8220;correct&#8221; way, I think it&#8217;s instructive to first see what happens when we try to do it incorrectly. Here&#8217;s one way that fails:</p>
<p><code>> d3.select("tr").append("td").data(names).text(function(d) {return d;})</code></p>
<p>This will result in only one column, having joined just the first element of the array, and the output in your browser will look like:</p>
<p><code>Sam</code></p>
<p>Interestingly, if you run the same command a second time, you will simply get:</p>
<p><code>Sam Sam</code></p>
<p>We can start over from scratch to get it right, but even from here we can fix it:</p>
<p><code>> d3.select("tr").selectAll("td").data(names).text(function(d) {return d;})</code></p>
<p>This gives:</p>
<p><code>Sam Diane</code></p>
<p>Since we had already created two <code>td</code> elements, the last command joined the first two elements of the <code>names</code> array to those elements. This is an <strong>update</strong> according to the terminology of d3. However, we still want to get those other two names in the table. To do this, we must use the <code>enter()</code> function and <code>append</code> two columns:</p>
<p><code>> d3.select("tr").selectAll("td").data(names).enter().append("td").text(function(d) {return d;})</code></p>
<p><code>Sam Diane Norm Cliff</code></p>
<p>What this command did was figure out that there were 4 elements in the data array, but only two <code>td</code> elements to join, so it put the &#8220;extra&#8221; two elements (Norm and Cliff) into the <code>enter</code> array. It then appended two new <code>td</code> elements and bound Norm and Cliff to those.</p>
<p>You might think it also re-wrote the first two names, but that is not the case. It&#8217;s an important point, so let&#8217;s extend the example further. From here, let&#8217;s shift the names in the array and add one more:</p>
<p><code>> names.shift(1);<br />
> names.push("Sam");<br />
> names.push("Carla");<br />
> names.push("Woody");</code></p>
<p>Now <code>names = ["Diane", "Norm", "Cliff", "Sam", "Carla", "Woody"]</code>.</p>
<p>Let&#8217;s see what happens when we re-run the command from before:</p>
<p><code>> d3.select("tr").selectAll("td").data(names).enter().append("td").text(function(d) {return d;})</code></p>
<p><code>Sam	Diane	Norm	Cliff	Carla	Woody</code></p>
<p>Ah, notice that while the last two (&#8220;entering&#8221;) elements have been added to the table, the first four elements did not change. To change those elements, we must run an update:</p>
<p><code>> d3.select("tr").selectAll("td").data(names).text(function(d) {return d;})</code></p>
<p><code>Diane	Norm	Cliff	Sam	Carla	Woody</code></p>
<p>Note that an update is simply the previous command minus the <code>enter()</code> and <code>append()</code> functions.</p>
<p>So how do we delete elements? Let&#8217;s get rid of Diane (don&#8217;t worry, in time you&#8217;ll get over it).</p>
<p><code>> names = names.slice(1)</code></p>
<p><code>["Norm", "Cliff", "Sam", "Carla", "Woody"]</code></p>
<p>We can run the update first and then remove the extra element or vice-versa. Let&#8217;s run the update first:</p>
<p><code>> d3.select("tr").selectAll("td").data(names).text(function(d) {return d;})</code></p>
<p><code>Norm	Cliff	Sam	Carla	Woody	Woody</code></p>
<p>To get rid of the extra Woody, we use the <code>exit()</code> function:</p>
<p><code>> d3.selectAll("td").data(names).exit().remove()</code></p>
<p><code>Norm	Cliff	Sam	Carla	Woody</code></p>
<p>Voila! Just to show one more piece of functionality, be aware that you can use the <code>data()</code> function to simply return the current data join:</p>
<p><code>> d3.selectAll("td").data()</code></p>
<p><code>> ["Norm", "Cliff", "Sam", "Carla", "Woody"]</code></p>
<p>This seems like a good place to stop for now. There is a lot more to the <code>data</code> function that I haven&#8217;t quite wrapped my head around yet, namely, the second argument (not used here) which creates a key function to replace the default one which simply uses the array index. In the meantime, I hope this post has been useful to you in some way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.d3coder.com/2012/06/16/getting-started-with-data-joins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple Beginning</title>
		<link>http://www.d3coder.com/2012/05/19/a-simple-beginning/</link>
		<comments>http://www.d3coder.com/2012/05/19/a-simple-beginning/#comments</comments>
		<pubDate>Sat, 19 May 2012 20:36:19 +0000</pubDate>
		<dc:creator>EvanZ</dc:creator>
				<category><![CDATA[animation]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[D3]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Mike Bostock]]></category>

		<guid isPermaLink="false">http://www.d3coder.com/?p=22</guid>
		<description><![CDATA[These are my first baby steps in D3, a new(ish) JavaScript library that was developed by Mike Bostock (i.e. a data science guru) for creating data visualizations. In fact, D3 stands for &#8220;Data Driven Documents&#8221;, and as the name suggests, it&#8217;s really a general purpose framework for doing pretty much anything one could want to do with data these days. I&#8217;m not only new to D3, but also to JS, which I am forcing myself to learn so I can take advantage of D3 (having seen the coolness of it, I couldn&#8217;t let something as small as not knowing JS &#8230;<span class="read-more"><a href="/2012/05/19/a-simple-beginning/">Read more &#187;</a></span>]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:large;font-family:serif">These are my first baby steps in <a href="http://d3js.org/">D3</a>, a new(ish) JavaScript library that was developed by <a href="http://bost.ocks.org/mike/">Mike Bostock</a> (i.e. a data science guru) for creating data visualizations. In fact, D3 stands for &#8220;Data Driven Documents&#8221;, and as the name suggests, it&#8217;s really a general purpose framework for doing pretty much anything one could want to do with data these days.<span id="more-22"></span> I&#8217;m not only new to D3, but also to JS, which I am forcing myself to learn so I can take advantage of D3 (having seen the coolness of it, I couldn&#8217;t let something as small as not knowing JS hold me back).<br />
<span style="font-size:large;font-family:serif"><br />
There doesn&#8217;t appear to be a D3 book yet, so one has to go about finding tutorials in various places. There are <a href="https://github.com/mbostock/d3/wiki/Gallery">plenty of examples</a> going around, though. I found <a href="http://alignedleft.com/tutorials/d3/">this tutorial</a> from Scott Murray (<a href="http://twitter.com/alignedleft">@alignedleft</a>) particularly valuable.<br />
<span style="font-size:large;font-family:serif"><br />
As I learn D3, I intend to post from time to time during each stage of my development. Hopefully, progress will be evident in both my understanding of D3 and JavaScript. Here, I just wanted to get going by doing a very simple animation of a &#8220;gun&#8221; &#8220;shooting&#8221; a &#8220;bullet&#8221;. To make it just a little bit more interesting, I added a random component which changes how far the bullet goes each time. Click on the &#8220;Shoot!&#8221; button to refresh the page which will re-load (i.e. restart) the animation.<br />
<span style="font-size:large;font-family:serif"><br />
To simulate recoil in the gun, I used the <code>.ease("bounce")</code> function, and to simulate the bullet slowing down, I used <code>.ease("exp-out")</code>. I tried to make it somewhat realistic by multiplying the recoil distance and bullet travel by the same random factor (<code>factor = Math.random()</code>). Everything else should be fairly self-explanatory. One thing I like about D3 is that it almost seems to document itself (how meta!).<br />
<span style="font-size:large;font-family:sans-serif"><br />
<span class="badge badge-info">Simulation of gun firing bullet</span><br />
<iframe src="/js/test.html" width=100% height=350></iframe><br />
<span class="badge badge-info">HTML/JS Code</span><br />
<div id="gist2758253" class="gist">
        <div class="gist-file">
          <div class="gist-data gist-syntax">
            



    <div class="file-data">
      <table cellpadding="0" cellspacing="0" class="lines highlight">
        <tr>
          <td class="line-numbers">
            <span class="line-number" id="file-gistfile1-html-L1" rel="file-gistfile1-html-L1">1</span>
            <span class="line-number" id="file-gistfile1-html-L2" rel="file-gistfile1-html-L2">2</span>
            <span class="line-number" id="file-gistfile1-html-L3" rel="file-gistfile1-html-L3">3</span>
            <span class="line-number" id="file-gistfile1-html-L4" rel="file-gistfile1-html-L4">4</span>
            <span class="line-number" id="file-gistfile1-html-L5" rel="file-gistfile1-html-L5">5</span>
            <span class="line-number" id="file-gistfile1-html-L6" rel="file-gistfile1-html-L6">6</span>
            <span class="line-number" id="file-gistfile1-html-L7" rel="file-gistfile1-html-L7">7</span>
            <span class="line-number" id="file-gistfile1-html-L8" rel="file-gistfile1-html-L8">8</span>
            <span class="line-number" id="file-gistfile1-html-L9" rel="file-gistfile1-html-L9">9</span>
            <span class="line-number" id="file-gistfile1-html-L10" rel="file-gistfile1-html-L10">10</span>
            <span class="line-number" id="file-gistfile1-html-L11" rel="file-gistfile1-html-L11">11</span>
            <span class="line-number" id="file-gistfile1-html-L12" rel="file-gistfile1-html-L12">12</span>
            <span class="line-number" id="file-gistfile1-html-L13" rel="file-gistfile1-html-L13">13</span>
            <span class="line-number" id="file-gistfile1-html-L14" rel="file-gistfile1-html-L14">14</span>
            <span class="line-number" id="file-gistfile1-html-L15" rel="file-gistfile1-html-L15">15</span>
            <span class="line-number" id="file-gistfile1-html-L16" rel="file-gistfile1-html-L16">16</span>
            <span class="line-number" id="file-gistfile1-html-L17" rel="file-gistfile1-html-L17">17</span>
            <span class="line-number" id="file-gistfile1-html-L18" rel="file-gistfile1-html-L18">18</span>
            <span class="line-number" id="file-gistfile1-html-L19" rel="file-gistfile1-html-L19">19</span>
            <span class="line-number" id="file-gistfile1-html-L20" rel="file-gistfile1-html-L20">20</span>
            <span class="line-number" id="file-gistfile1-html-L21" rel="file-gistfile1-html-L21">21</span>
            <span class="line-number" id="file-gistfile1-html-L22" rel="file-gistfile1-html-L22">22</span>
            <span class="line-number" id="file-gistfile1-html-L23" rel="file-gistfile1-html-L23">23</span>
            <span class="line-number" id="file-gistfile1-html-L24" rel="file-gistfile1-html-L24">24</span>
            <span class="line-number" id="file-gistfile1-html-L25" rel="file-gistfile1-html-L25">25</span>
            <span class="line-number" id="file-gistfile1-html-L26" rel="file-gistfile1-html-L26">26</span>
            <span class="line-number" id="file-gistfile1-html-L27" rel="file-gistfile1-html-L27">27</span>
            <span class="line-number" id="file-gistfile1-html-L28" rel="file-gistfile1-html-L28">28</span>
            <span class="line-number" id="file-gistfile1-html-L29" rel="file-gistfile1-html-L29">29</span>
            <span class="line-number" id="file-gistfile1-html-L30" rel="file-gistfile1-html-L30">30</span>
            <span class="line-number" id="file-gistfile1-html-L31" rel="file-gistfile1-html-L31">31</span>
            <span class="line-number" id="file-gistfile1-html-L32" rel="file-gistfile1-html-L32">32</span>
            <span class="line-number" id="file-gistfile1-html-L33" rel="file-gistfile1-html-L33">33</span>
            <span class="line-number" id="file-gistfile1-html-L34" rel="file-gistfile1-html-L34">34</span>
            <span class="line-number" id="file-gistfile1-html-L35" rel="file-gistfile1-html-L35">35</span>
            <span class="line-number" id="file-gistfile1-html-L36" rel="file-gistfile1-html-L36">36</span>
            <span class="line-number" id="file-gistfile1-html-L37" rel="file-gistfile1-html-L37">37</span>
            <span class="line-number" id="file-gistfile1-html-L38" rel="file-gistfile1-html-L38">38</span>
            <span class="line-number" id="file-gistfile1-html-L39" rel="file-gistfile1-html-L39">39</span>
            <span class="line-number" id="file-gistfile1-html-L40" rel="file-gistfile1-html-L40">40</span>
            <span class="line-number" id="file-gistfile1-html-L41" rel="file-gistfile1-html-L41">41</span>
            <span class="line-number" id="file-gistfile1-html-L42" rel="file-gistfile1-html-L42">42</span>
            <span class="line-number" id="file-gistfile1-html-L43" rel="file-gistfile1-html-L43">43</span>
            <span class="line-number" id="file-gistfile1-html-L44" rel="file-gistfile1-html-L44">44</span>
            <span class="line-number" id="file-gistfile1-html-L45" rel="file-gistfile1-html-L45">45</span>
            <span class="line-number" id="file-gistfile1-html-L46" rel="file-gistfile1-html-L46">46</span>
            <span class="line-number" id="file-gistfile1-html-L47" rel="file-gistfile1-html-L47">47</span>
            <span class="line-number" id="file-gistfile1-html-L48" rel="file-gistfile1-html-L48">48</span>
            <span class="line-number" id="file-gistfile1-html-L49" rel="file-gistfile1-html-L49">49</span>
            <span class="line-number" id="file-gistfile1-html-L50" rel="file-gistfile1-html-L50">50</span>
            <span class="line-number" id="file-gistfile1-html-L51" rel="file-gistfile1-html-L51">51</span>
            <span class="line-number" id="file-gistfile1-html-L52" rel="file-gistfile1-html-L52">52</span>
            <span class="line-number" id="file-gistfile1-html-L53" rel="file-gistfile1-html-L53">53</span>
          </td>
          <td class="line-data">
            <pre class="line-pre"><div class="line" id="file-gistfile1-html-LC1"><span class="cp">&lt;!DOCTYPE html&gt;</span></div><div class="line" id="file-gistfile1-html-LC2"><span class="nt">&lt;html&gt;</span></div><div class="line" id="file-gistfile1-html-LC3">  <span class="nt">&lt;head&gt;</span></div><div class="line" id="file-gistfile1-html-LC4">    <span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;http://mbostock.github.com/d3/d3.v2.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class="line" id="file-gistfile1-html-LC5">  <span class="nt">&lt;/head&gt;</span></div><div class="line" id="file-gistfile1-html-LC6">  <span class="nt">&lt;body&gt;</span></div><div class="line" id="file-gistfile1-html-LC7">  <span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;chart&quot;</span><span class="nt">&gt;&lt;/div&gt;</span></div><div class="line" id="file-gistfile1-html-LC8">  <span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;button&quot;</span><span class="nt">&gt;</span></div><div class="line" id="file-gistfile1-html-LC9">  	<span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">&quot;button&quot;</span> <span class="na">id=</span><span class="s">&quot;Button1&quot;</span> <span class="na">value=</span><span class="s">&quot;Shoot!&quot;</span> <span class="na">onclick=</span><span class="s">&quot;history.go(0)&quot;</span><span class="nt">/&gt;</span></div><div class="line" id="file-gistfile1-html-LC10"><span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;</span></div><div class="line" id="file-gistfile1-html-LC11"><span class="kd">var</span> <span class="nx">w</span> <span class="o">=</span> <span class="mi">500</span><span class="p">,</span></div><div class="line" id="file-gistfile1-html-LC12">	<span class="nx">h</span> <span class="o">=</span> <span class="mi">300</span><span class="p">,</span></div><div class="line" id="file-gistfile1-html-LC13">	<span class="nx">factor</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">(),</span></div><div class="line" id="file-gistfile1-html-LC14">	<span class="nx">draw</span> <span class="o">=</span> <span class="nx">d3</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="s2">&quot;#chart&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC15">			<span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;svg&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC16">			<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;width&quot;</span><span class="p">,</span><span class="nx">w</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC17">			<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;height&quot;</span><span class="p">,</span><span class="nx">h</span><span class="p">);</span></div><div class="line" id="file-gistfile1-html-LC18">	<span class="nx">draw</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;rect&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC19">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;x&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC20">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;y&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC21">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;width&quot;</span><span class="p">,</span><span class="mi">500</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC22">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;height&quot;</span><span class="p">,</span><span class="mi">300</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC23">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;fill&quot;</span><span class="p">,</span><span class="s2">&quot;lightgray&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC24">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;stroke&quot;</span><span class="p">,</span><span class="s2">&quot;black&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC25">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;stroke-width&quot;</span><span class="p">,</span><span class="mi">5</span><span class="p">);</span></div><div class="line" id="file-gistfile1-html-LC26">	<span class="nx">draw</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;circle&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC27">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;id&quot;</span><span class="p">,</span><span class="s2">&quot;cir1&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC28">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;cx&quot;</span><span class="p">,</span><span class="mi">170</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC29">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;cy&quot;</span><span class="p">,</span><span class="mi">160</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC30">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;r&quot;</span><span class="p">,</span><span class="mf">12.5</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC31">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;fill&quot;</span><span class="p">,</span><span class="s2">&quot;steelblue&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC32">	<span class="p">.</span><span class="nx">transition</span><span class="p">()</span></div><div class="line" id="file-gistfile1-html-LC33">		<span class="p">.</span><span class="nx">ease</span><span class="p">(</span><span class="s2">&quot;exp-out&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC34">		<span class="p">.</span><span class="nx">duration</span><span class="p">(</span><span class="mi">1500</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC35">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;transform&quot;</span><span class="p">,</span><span class="kd">function</span><span class="p">(){</span></div><div class="line" id="file-gistfile1-html-LC36">			<span class="k">return</span> <span class="s2">&quot;translate(&quot;</span><span class="o">+</span><span class="nx">factor</span><span class="o">*</span><span class="mi">250</span><span class="o">+</span><span class="s2">&quot;,0)&quot;</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC37">		<span class="p">});</span></div><div class="line" id="file-gistfile1-html-LC38">	<span class="nx">draw</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s2">&quot;rect&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC39">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;id&quot;</span><span class="p">,</span><span class="s2">&quot;rect1&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC40">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;x&quot;</span><span class="p">,</span><span class="mi">50</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC41">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;y&quot;</span><span class="p">,</span><span class="mi">150</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC42">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;width&quot;</span><span class="p">,</span><span class="mi">100</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC43">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;height&quot;</span><span class="p">,</span><span class="mi">25</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC44">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;fill&quot;</span><span class="p">,</span><span class="s2">&quot;black&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC45">	<span class="p">.</span><span class="nx">transition</span><span class="p">()</span></div><div class="line" id="file-gistfile1-html-LC46">		<span class="p">.</span><span class="nx">ease</span><span class="p">(</span><span class="s2">&quot;bounce&quot;</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC47">		<span class="p">.</span><span class="nx">duration</span><span class="p">(</span><span class="mi">1500</span><span class="p">)</span></div><div class="line" id="file-gistfile1-html-LC48">		<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;transform&quot;</span><span class="p">,</span><span class="kd">function</span><span class="p">(){</span></div><div class="line" id="file-gistfile1-html-LC49">			<span class="k">return</span> <span class="s2">&quot;translate(&quot;</span><span class="o">+</span><span class="nx">factor</span><span class="o">*</span><span class="p">(</span><span class="o">-</span><span class="mi">25</span><span class="p">)</span><span class="o">+</span><span class="s2">&quot;)&quot;</span><span class="p">;</span></div><div class="line" id="file-gistfile1-html-LC50">		<span class="p">});</span>		</div><div class="line" id="file-gistfile1-html-LC51"><span class="nt">&lt;/script&gt;</span></div><div class="line" id="file-gistfile1-html-LC52">  <span class="nt">&lt;/body&gt;</span></div><div class="line" id="file-gistfile1-html-LC53"><span class="nt">&lt;/html&gt;</span></div></pre>
          </td>
        </tr>
      </table>
    </div>

          </div>
          <div class="gist-meta">
            <a href="https://gist.github.com/EvanZ/2c7fef83e0b38d3de91f/raw/c90fd645ccfc4005aa82ad10299f6cb142f02eca/gistfile1.html" style="float:right">view raw</a>
            <a href="https://gist.github.com/EvanZ/2c7fef83e0b38d3de91f#file-gistfile1-html">gistfile1.html</a>
            hosted with &#10084; by <a href="https://github.com">GitHub</a>
          </div>
        </div>
</div>
</span></span></span></span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.d3coder.com/2012/05/19/a-simple-beginning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
