<?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 Eve Mendelevich on Medium]]></title>
        <description><![CDATA[Stories by Eve Mendelevich on Medium]]></description>
        <link>https://medium.com/@ym9382?source=rss-b4ac70c6150f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*TZwmOV_EtPlGvICVR2Pciw.jpeg</url>
            <title>Stories by Eve Mendelevich on Medium</title>
            <link>https://medium.com/@ym9382?source=rss-b4ac70c6150f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 05:52:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ym9382/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[Learning D3.js with React]]></title>
            <link>https://medium.com/@ym9382/learning-d3-js-with-react-2d21797aac75?source=rss-b4ac70c6150f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d21797aac75</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[nasa]]></category>
            <category><![CDATA[d3js]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Eve Mendelevich]]></dc:creator>
            <pubDate>Mon, 28 Jan 2019 10:44:35 GMT</pubDate>
            <atom:updated>2019-01-28T10:44:35.239Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re interested in data visualization, there’s almost no doubt that you’ve heard of D3.js. Upon starting to research it, you may have heard that the New York Times uses D3 for its <a href="https://twitter.com/nytgraphics">beautiful and interactive graphics</a>. You may have seen Mike Bostock’s (a key developer of the library who previously worked for NYT) <a href="https://bl.ocks.org/mbostock">awe-inspiring tutorials</a> of all that you could do with D3. And there’s a chance that you may have read that <a href="https://medium.com/@Elijah_Meeks/d3-is-not-a-data-visualization-library-67ba549e8520">D3 is not a data visualization library at all</a>.</p><p>I stumbled upon these resources as I was researching D3.js. I had long been interested in the library and decided to try learning it as part of a short personal project (which you can play with <a href="http://asteroid-maps.herokuapp.com/">here</a>, or you can look at its code <a href="https://github.com/mendelevich/asteroid-maps">here</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rSnDYbLRVqIe7ugxtaIiLg.png" /><figcaption>What I ended up building with React and D3.js: an interactive asteroid calendar.</figcaption></figure><p>In this post I will walk through my beginning of learning to use D3 with React, and some of the bugs I encountered along the way.</p><p>First, I had to come with an idea of what to use D3 for. D3 can be used for <a href="https://beta.observablehq.com/@mbostock/epicyclic-gearing">graphics that don’t really touch any data at all</a>, but I really wanted to start using D3 in the way that it was best known for, as a data visualization tool. However, I’m no statistician (despite a couple of behavioral science statistics classes in college) and I did not feel comfortable using complex data analysis methods that I did not understand already. I wanted something relatively simple that I could plot with little to no risk of misrepresenting the data. I found that NASA’s APIs were perfectly suited for this purpose. Specifically, its Near Earth Object Web Service (<a href="https://api.nasa.gov/api.html#NeoWS">NeoWs</a>) provided excellent, easily graphable information.</p><blockquote>Really quickly: NEOs are Near Earth Objects, or asteroids and comets. The Center for NEO Studies (<a href="https://cneos.jpl.nasa.gov/">CNEOS</a>) is in charge of “computing asteroid and comet orbits and their odds of Earth impact”. A <em>little</em> scary, to be honest, but this makes for some exciting data. NASA’s NEO APIs allow you to retrieve data by date (the NEOs on approaching on a date and their distance from Earth) or by a specific object (the history of one NEO’s approaches to Earth).</blockquote><p>Now came the hard part: beginning learning a new library. I have grown fairly comfortable with React over the past few months, and wanted to keep using it for the project to create an interactive UI. I quickly realized, however, the issue with using both React and D3 together. Both libraries deal with the DOM in some sense — D3 by modifying it directly, and React by using a virtual DOM. As Adil Baaj’s <a href="https://blog.sicara.com/a-starting-point-on-using-d3-with-react-869fdf3dfaf">very helpful blog post</a> put it:</p><blockquote>“The problem about using D3 with React is that if D3 modifies an element of the DOM, this modification won’t be reflected on the virtual DOM used by React.”</blockquote><p>In that same blog post, Adil included a tutorial on <a href="https://github.com/Olical/react-faux-dom">react-faux-dom</a>, a library created to allow React and D3 to “play nice” with each other.</p><p>My approach with this information may not have been the best one, but it <em>kind of </em>worked for me: I followed basic D3 tutorials for bar charts and line graphs in React-free Javascript to get the feel of things. After I completed the tutorial, I translated the tutorial to React. This lead to me learning D3 and react-faux-dom almost concurrently. I quickly realized that the “formula” of D3 was to select an element on the DOM, such as the body element, append an svg element (the canvas on which you’ll be sticking all your cool shapes onto), and then append elements to that svg element (lines, bars, and other data points). With react-faux-dom, you create an element on the, well, faux DOM, and then append an svg element to that. At the end, you convert the original faux DOM element to React.</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Frunkit.com%2Fmendelevich%2F5c4ec2e7cf562000133b362a&amp;src=https%3A%2F%2Frunkit.com%2Fe%3Fname%3Deed50732-78dc-443c-b5ec-023fe7e313bc%26base64source%3DY29uc3QgZDMgPSByZXF1aXJlKCdkMycpOwoKY29uc3QgY3JlYXRlU3ZnID0gKCkgPT4gewogIGNvbnN0IHN2ZyA9IGQzCiAgICAuc2VsZWN0KCdib2R5JykKICAgIC5hcHBlbmQoJ3N2ZycpCiAgICAuYXR0cignd2lkdGgnLCA0MDApCiAgICAuYXR0cignaGVpZ2h0JywgNDAwKQogICAgfQoKZXhwb3J0IGRlZmF1bHQgY3JlYXRlU3Zn%26minHeight%3D273%26fixedHeight%3D413%26location%3D%26title%3D%26oembed%3Dtrue&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=runkit" width="900" height="413" frameborder="0" scrolling="no"><a href="https://medium.com/media/0db18655796d6f4d37c7e946c873b72a/href">https://medium.com/media/0db18655796d6f4d37c7e946c873b72a/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Frunkit.com%2Fmendelevich%2F5c4ec3ec4ea3a50012e6123c&amp;src=https%3A%2F%2Frunkit.com%2Fe%3Fname%3D1404b913-f2aa-44b9-bacc-de7b9bdd49dc%26base64source%3DLy8gV2l0aCBSZWFjdApjb25zdCBSZWFjdCA9IHJlcXVpcmUoJ3JlYWN0Jyk7CmNvbnN0IFJlYWN0RmF1eERPTSA9IHJlcXVpcmUoJ3JlYWN0LWZhdXgtZG9tJyk7CmNvbnN0IGQzID0gcmVxdWlyZSgnZDMnKTsKCmNsYXNzIEFzdGVyb2lkTWFwIGV4dGVuZHMgUmVhY3QuQ29tcG9uZW50IHsKICBkcmF3Q2hhcnQoKSB7CiAgICBjb25zdCBkaXYgPSBuZXcgUmVhY3RGYXV4RE9NLmNyZWF0ZUVsZW1lbnQoJ2RpdicpOwoKICAgIGNvbnN0IHN2ZyA9IGQzCiAgICAgIC5zZWxlY3QoZGl2KQogICAgICAuYXBwZW5kKCdzdmcnKQogICAgICAuYXR0cignd2lkdGgnLCA0MDApCiAgICAgIC5hdHRyKCdoZWlnaHQnLCA0MDApOwogICAgICAKICAgIHJldHVybiBkaXYudG9SZWFjdCgpOwogIH0KCiAgcmVuZGVyKCkgewogICAgcmV0dXJuIHRoaXMuZHJhd0NoYXJ0KCk7CiAgfQp9CgpleHBvcnQgZGVmYXVsdCBBc3Rlcm9pZE1hcDs%253D%26minHeight%3D546%26fixedHeight%3D686%26location%3D%26title%3D%26oembed%3Dtrue&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=runkit" width="900" height="686" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9c06ae5c249c394c8494581f314547c/href">https://medium.com/media/c9c06ae5c249c394c8494581f314547c/href</a></iframe><p>Now you have a blank canvas. But not so fast! Before you can start appending lines and circles, you have to write some functions to scale your data. This was probably the most confusing part for me, as tutorials/code examples didn’t seem to explain this part too well.</p><p>D3 brings some math and formatting functions to the table. In fact, one approach of merging these two libraries together is to use <a href="https://mikewilliamson.wordpress.com/2016/06/03/d3-and-react-3-ways/">D3 only for math, and to hand over the reins for DOM over to React</a>.</p><p>Let’s look at my goal/what I was ultimately able to accomplish:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GQ9ZI5fmnCaOzAx4xBGb9Q.png" /></figure><p>Since you’re not painstakingly plotting every data point yourself, you need to let D3 how to spread out the points along the axes with the x and y coordinates that it calculates itself, based on your data.</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Frunkit.com%2Fmendelevich%2Fscalelinear&amp;src=https%3A%2F%2Frunkit.com%2Fe%3Fname%3D80419399-0f68-4fdb-aee0-cf59dd070a68%26base64source%3DY29uc3Qgc2NhbGVEaXN0ID0gZDMKICAuc2NhbGVMaW5lYXIoKQogICAgLmRvbWFpbihbCiAgICAvLyBUaGUgKyBjb2VyY2VzIHRoZSB2YWx1ZSB0byBhIG51bWJlcgogICAgICBkMy5taW4oZGF0YSwgZCA9PiArZC52YWx1ZSksIAogICAgICBkMy5tYXgoZGF0YSwgZCA9PiArZC52YWx1ZSksCiAgICBdKQogICAgLnJhbmdlKFs1MCwgd2lkdGggLSA1MF0pOw%253D%253D%26minHeight%3D210%26fixedHeight%3D350%26location%3D%26title%3D%26oembed%3Dtrue&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=runkit" width="900" height="350" frameborder="0" scrolling="no"><a href="https://medium.com/media/b04747f42899c4912acc2525a3deb624/href">https://medium.com/media/b04747f42899c4912acc2525a3deb624/href</a></iframe><p>So while my data dealt with NEOs millions of kilometers away, D3 would know <em>relatively</em> where to plot them with respect to each other on the canvas. The .range() is usually the size of your canvas, minus some margins. Likely, you would need to do this twice, for the x coordinates and the y coordinates. For the y-axis, I decided to just plot all the nodes in the center of the graph. However, I wanted to represent the size of the NEOs by the relative size of the nodes, so I had to write a very similar function.</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Frunkit.com%2Fmendelevich%2F5c4ecd1fcf562000133b4091&amp;src=https%3A%2F%2Frunkit.com%2Fe%3Fname%3D8b90364f-db06-432e-b059-a8ae368d7b45%26base64source%3DY29uc3Qgc2NhbGVSYWRpdXMgPSBkMwogLnNjYWxlTGluZWFyKCkKICAgICAgLmRvbWFpbihbCiAgICAgICAgZDMubWluKGRhdGEsIGQgPT4gK2QuZGlhbWV0ZXIpLAogICAgICAgIGQzLm1heChkYXRhLCBkID0%252BICtkLmRpYW1ldGVyKSwKICAgICAgXSkKICAgICAgLnJhbmdlKFs1LCAyMF0pOw%253D%253D%26minHeight%3D189%26fixedHeight%3D329%26location%3D%26title%3D%26oembed%3Dtrue&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=runkit" width="900" height="329" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea91eba5ed2e551f0828e380fe70d34c/href">https://medium.com/media/ea91eba5ed2e551f0828e380fe70d34c/href</a></iframe><p>But your work isn’t done yet — if you stop here, you still won’t have anything to show for your hard work other than an empty svg element. Now you need to actually add the nodes, and use these scale functions to define their coordinates.</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Frunkit.com%2Fmendelevich%2Fadd-nodes&amp;src=https%3A%2F%2Frunkit.com%2Fe%3Fname%3Dd328790c-453f-4515-be93-4caa974afdc1%26base64source%3DY29uc3Qgbm9kZXMgPSBzdmcKICAgICAgLnNlbGVjdEFsbCgnY2lyY2xlJykKICAgICAgLmRhdGEobmVhckVhcnRoT2JqZWN0cykKICAgICAgLmVudGVyKCkKICAgICAgLmFwcGVuZCgnY2lyY2xlJykKICAgICAgLmF0dHIoJ2N4JywgKGQsIGkpID0%252BIHNjYWxlRGlzdChkYXRhW2ldLmRpc3RhbmNlKSkKICAgICAgLmF0dHIoJ2N5JywgKCkgPT4gaGVpZ2h0IC8gMikKICAgICAgLmF0dHIoJ3InLCAoZCwgaSkgPT4gc2NhbGVSYWRpdXMoZGF0YVtpXS5kaWFtZXRlcikpCiAgICAgIC8vIGFwcGVuZCBtb3JlIGF0dHIgYWZ0ZXIKICAgICAgLmF0dHIoJ2ZpbGwnLCBkID0%252BCiAgICAgICAgZC5wb3RlbnRpYWxfaGF6YXJkID8gJyNEQzZBQ0YnIDogJ3doaXRlJwogICAgICAp%26minHeight%3D294%26fixedHeight%3D434%26location%3D%26title%3D%26oembed%3Dtrue&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=runkit" width="900" height="434" frameborder="0" scrolling="no"><a href="https://medium.com/media/13649b4ee13273e49d2f1f337091b0c9/href">https://medium.com/media/13649b4ee13273e49d2f1f337091b0c9/href</a></iframe><p>It’s still a little confusing to me why you .selectAll() of the circles on the svg when you haven’t placed any yet. But that’s the way you do it, folks. You selectAll one of the <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Basic_Shapes">basic svg element shapes</a>, add your .data(yourData) , .enter() , and then .append(yourShape) . Then, you go on to define each shape’s attributes. cx , cy , and r , in this case, define the x and y-coordinates (notice that we applyscaleDist() to our cx data so it knows how to spread out), and the radius of the nodes (ditto forscaleRadius() ). You can easily fall down a rabbit hole of appending all sorts of aesthetic attributes one-by-one, but it’s far easier to just append a class and define the fill, stroke, or opacity with a stylesheet. That is, however, you want to make certain nodes stand out. In my example, I color certain nodes pink instead of white based on the data that NASA provided.</p><p>Next, you might want to add some <a href="https://bl.ocks.org/d3noob/23e42c8f67210ac6c678db2cd07a747e">axes</a> and some <a href="http://bl.ocks.org/d3noob/a22c42db65eb00d4e369">tooltips</a> to provide extra info for when you mouse over a node. I’m skipping ahead a little to cover some of the issues hinted at earlier.</p><p>At the time of writing this post, my little project is still a prototype, but I implemented a pretty cool feature. When you click on a node in the graph, up pops a new graph below it that plots that specific NEO’s past and future path.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JJG8W4YDIc3eUihuBC4lCg.png" /><figcaption>The selected NEO is in light blue-green.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_VYJuFPWtPwPuCqFJ1erzw.png" /><figcaption>This NEO’s data makes for an especially pretty graph.</figcaption></figure><p>I created this feature using a state in React. However, when you click multiple objects…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*bgvVCFV5U86l4hnkl8GkOA.png" /></figure><p>…It keeps appending them to the DOM, instead of replacing the current graph. I plan to revisit this issue, as I’m not sure if its due to react-faux-dom or “human error”.</p><p>Learning D3 with React was definitely a learning experience. As I continue to improve the project, I might stick with react-faux-dom or try out some of the other approaches. react-faux-dom was helpful for getting the project off the ground, but it is seemingly less performant, because of the additional layer of fake DOM. I’d love to experiment with some animations as well. I think I’ll try a <a href="https://www.smashingmagazine.com/2018/02/react-d3-ecosystem/#top">React-D3 library</a>, like Recharts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d21797aac75" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>