{"id":40476,"date":"2016-10-07T11:45:07","date_gmt":"2016-10-07T06:15:07","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=40476"},"modified":"2016-10-07T14:46:55","modified_gmt":"2016-10-07T09:16:55","slug":"how-to-use-d3-with-react-components","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-use-d3-with-react-components\/","title":{"rendered":"How to use D3 with React components?"},"content":{"rendered":"<p>D3 (Data Driven Documents) is a JS library that helps us build visualizations. It binds arbitrary data to a DOM, where the data-driven transformations can be applied to the document. It works well by mutating the DOM element, usually a root node that was\u00a0placed in the HTML. You call .append (&#8216;span&#8217;) and it inserts a &lt;span&gt;&lt;\/span&gt; as a child of the node you selected.<\/p>\n<p>Whereas, React on the other hand, builds the application through one big call stack to various render methods.<br \/>\nWe create a tree of objects that represent the application and its state, and then React works out what DOM elements it should add, remove or mutate on your behalf.<\/p>\n<p>The problem arises when we need to use both of them together. D3 needs DOM which is generated by React. Therefore, we need a fake DOM with enough methods to trick D3.<\/p>\n<p>React-faux-dom supports a wide range of DOM operations. We can use the full D3 API with the help of react-faux-dom to have efficient D3 components and use React to render on the server side.<\/p>\n<p><strong>Steps to use D3 with React components<\/strong><\/p>\n<p><span style=\"line-height: 1.71429;font-size: 1rem\">Installing :<\/span><\/p>\n<p>[java]<\/p>\n<p>npm install d3<br \/>\nnpm install react-faux-dom<\/p>\n<p>[\/java]<\/p>\n<p>Importing D3 and ReactFauxDOM :<\/p>\n<p>[java]<br \/>\nimport ReactFauxDOM from &quot;{path-to-ReactFauxDOM-js-file}&quot;<br \/>\nimport d3 from &quot;{path-to-d3-js-file}&quot;<br \/>\n[\/java]<\/p>\n<p>Creating react component with empty render method:<\/p>\n<p>[java]<\/p>\n<p>const MyChart = React.createClass({<br \/>\nrender () {},<br \/>\n});<\/p>\n<p>[\/java]<\/p>\n<p>Inside the render create a SVG\u00a0using ReactFauxDOM:<\/p>\n<p>[java]<\/p>\n<p>var node = ReactFauxDOM.createElement(&#8216;svg&#8217;);<\/p>\n<p>[\/java]<\/p>\n<p>Now we have an SVG\u00a0element and, we can do what ever we want to do with this SVG element using D3:<\/p>\n<p>[java]<\/p>\n<p>var svg = d3.select(&quot;node&quot;);<br \/>\nsvg.append(&quot;div&quot;).html(&quot;First heading using d3 and React.&quot;)<br \/>\n.style(&quot;font-size&quot;,&quot;22px&quot;)<br \/>\n.style(&quot;color&quot;,&quot;green&quot;);<\/p>\n<p>svg.append(&quot;circle&quot;)<br \/>\n   .style(&quot;fill&quot;, &quot;blue&quot;)<br \/>\n   .attr(&quot;r&quot;, 40)<br \/>\n   .attr(&quot;cx&quot;, 150)<br \/>\n   .attr(&quot;cy&quot;, 350)<br \/>\n   .on(&quot;mouseover&quot;, function() {<br \/>\n        var myCircle = this;<br \/>\n        d3.select(myCircle).style(&quot;fill&quot;,&quot;red&quot;);<br \/>\n   })<br \/>\n   .on(&quot;mouseout&quot;, function() {<br \/>\n        var myCircle = this;<br \/>\n        d3.select(myCircle).style(&quot;fill&quot;,&quot;green&quot;);<br \/>\n });<br \/>\n[\/java]<\/p>\n<p>Once\u00a0you are done with your SVG, then you just need to send it to React to render it accordingly:<\/p>\n<p>[java]<\/p>\n<p>return node.toReact();<\/p>\n<p>[\/java]<\/p>\n<p>And you are done!<\/p>\n<p>Hope this helps!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>D3 (Data Driven Documents) is a JS library that helps us build visualizations. It binds arbitrary data to a DOM, where the data-driven transformations can be applied to the document. It works well by mutating the DOM element, usually a root node that was\u00a0placed in the HTML. You call .append (&#8216;span&#8217;) and it inserts a [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[4006,4100,4064,4101],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/40476"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=40476"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/40476\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=40476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=40476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=40476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}