<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jessica Hsu on Medium]]></title>
        <description><![CDATA[Stories by Jessica Hsu on Medium]]></description>
        <link>https://medium.com/@jessica.hsu?source=rss-4c2aaf9d0ef4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*8LWt-tfrZdAN2ZRv.</url>
            <title>Stories by Jessica Hsu on Medium</title>
            <link>https://medium.com/@jessica.hsu?source=rss-4c2aaf9d0ef4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:59:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jessica.hsu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Getting off the Ground with d3.js]]></title>
            <link>https://medium.com/@jessica.hsu/getting-off-the-ground-with-d3-js-e582f09abd60?source=rss-4c2aaf9d0ef4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e582f09abd60</guid>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[d3]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Jessica Hsu]]></dc:creator>
            <pubDate>Tue, 14 Aug 2018 00:17:37 GMT</pubDate>
            <atom:updated>2018-09-05T20:51:34.951Z</atom:updated>
            <content:encoded><![CDATA[<p>In a world full of dichotomies, I often feel forced to choose between working with my creative side versus my analytical side. However, I am drawn to both. Coming across the field of data visualization a few years ago, I was pulled in by the way in which design, communication, math and analysis were brought together. My curiosity to explore data visualization has lingered for a while, but it was only recently that I began reading tutorials and working on exercises. To me, data visualization is a playground where aesthetic and beauty is built on top of logical pieces. It is a place where logic and emotion don’t merely exist together, but actually complement one another in order to tell a story (hello, visual journalism). <a href="https://www.visualcinnamon.com/">So</a>. <a href="https://pudding.cool/">Cool</a>.</p><p><strong>So what is d3.js?</strong></p><p>Created by Mike Bostock, d3.js (<strong>d</strong>ata-<strong>d</strong>riven <strong>d</strong>ocuments, documents referring to the DOM) is a JavaScript library that enables programmers to push data to its full visual potential with HTML, CSS, SVGs and JavaScript. SVG (scalable vector graphics) are powerful in that they allow graphics to scale and resize while fully maintaining graphic quality. d3 has methods that allow programmers to append SVGs to the DOM, which flexibly change in response to the data.</p><p>To get started, I recommend choosing an open-source dataset you are interested in working with, and downloading it as a CSV (comma separated values) file. Here are some great resources to start with.</p><p><a href="https://toolbox.google.com/datasetsearch">https://toolbox.google.com/datasetsearch</a> <em>Edit: Just found out about this one!</em></p><p><a href="https://ourworldindata.org/">https://ourworldindata.org/</a></p><p><a href="https://www.kaggle.com/datasets">https://www.kaggle.com/datasets</a></p><p><a href="https://opendata.cityofnewyork.us/">https://opendata.cityofnewyork.us/</a></p><p><a href="https://data-archive.nimh.nih.gov/">https://data-archive.nimh.nih.gov/</a></p><p><strong>Bring in the d3 library</strong></p><p>To use d3 and its methods, you will of course need to import it.</p><ul><li>in js import d3 from ‘d3&#39;</li><li>or in html &lt;script src=&quot;https://d3js.org/d3.v4.min.js&quot;&gt;&lt;/script&gt;</li></ul><p><strong>d3 is chaining friendly!</strong></p><p>In d3, we will see a lot of method chaining syntax. In a nutshell, method chaining allows us to pass output as input to the following method.</p><p>For example, instead of writing three lines of code, we can chain them together in one statement.</p><pre>js<br>const svg = d3.select(&quot;body&quot;);<br>const p = svg.append(&quot;p&quot;);<br>p.text(&quot;not chained :(&quot;);</pre><pre>//but with chaining, we can write...<br>d3.select(&quot;body&quot;)<br>.append(&quot;p&quot;)<br>.text(&quot;chained!&quot;);</pre><p><strong>Building Blocks of a d3 Visual</strong></p><p>Let’s look at some d3 code and break it up, line by line.</p><pre>const svg = d3.<br>.select(“#d3-visualization”)<br>.append(“svg”)<br>.attr(“height”, h)<br>.attr(“width”, w);</pre><p>const svg = d3.select(&quot;#d3-visualization&quot;) We are selecting an element from the DOM using CSS selector syntax! (look familiar?). This method get the first DOM node with the id #d3-visualization and is where our visualization will live.</p><p>.append(“svg”) adds an SVG element to the DOM element that was selected in the previous line. With this SVG, we can think of the top left corner of the browser window as coordinates (0,0). The x-axis increases as you move right (like any old graph), but the y-axis increases as you move down.</p><p>.attr(“height”, h) &amp; .attr(&quot;width&quot;, w)</p><p>h and w are variable</p><pre>.attrs({ <em>// set multiple attributes with an object literal<br>&quot;</em>height&quot;: h,<br>&quot;width&quot;: w<br>})</pre><p>Other attributes you’ll likely see: x, y, fill</p><p>Now comes the trickier part, where we will be binding our data to the visual.</p><p>Again, let’s break the following code down!</p><pre>const chart = svg<br>.selectAll(“rect”)<br>.data(data)<br>.enter()<br>.append(“rect”)<br>.attr(“height”, function(<em>d</em>) {<br>  return d.number;<br>}));</pre><p>svg.selectAll(&quot;rect”) creates an empty set of elements where each element (eventually) is bound to a single data point. D3 works declaratively, meaning we tell it what we want, but don’t dictate <strong>how</strong> it is implemented. One instance of an element passed into selectAll() will correspond to one data point.</p><p>.data(data) where the magic happens! D3 will loop through this dataset as many times as it needs to get to the end. Each iteration will display / manipulate each row of data according to our chained methods that follow.</p><p>.enter() is also magical. It joins the data from .data and the set of elements from .selectAll. It creates an element for each unrepresented data point (remember, d3 is data-driven!), and will send this new element reference down the chain.</p><p>.append(&quot;rect&quot;) will use the element made by .enter and actually add it to the DOM.</p><p>.attr(&quot;height&quot;, function(d){return d.number})) For each DOM element made in the step above, the element’s height will be calculated from the number value within each row of data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e582f09abd60" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>