{"id":40476,"date":"2016-10-07T11:45:07","date_gmt":"2016-10-07T06:15:07","guid":{"rendered":"https:\/\/tothenewco.pro\/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:\/\/tothenewco.pro\/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":3,"footnotes":""},"categories":[1],"tags":[4006,4100,4064,4101],"class_list":["post-40476","post","type-post","status-publish","format-standard","hentry","category-technology","tag-d3","tag-integrating-d3","tag-react","tag-react-faux-dom"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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 placed in the HTML. You call .append (&#039;span&#039;) and it inserts a\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Mansi Arora\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"How to use D3 with React components? | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"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 placed in the HTML. You call .append (&#039;span&#039;) and it inserts a\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"How to use D3 with React components? | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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 placed in the HTML. You call .append (&#039;span&#039;) and it inserts a\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/#article\",\"name\":\"How to use D3 with React components? | TO THE NEW Blog\",\"headline\":\"How to use D3 with React components?\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2016-10-07T11:45:07+05:30\",\"dateModified\":\"2016-10-07T14:46:55+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/#webpage\"},\"articleSection\":\"Technology, d3, integrating-d3, react, react-faux-dom\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/#listItem\",\"name\":\"How to use D3 with React components?\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/#listItem\",\"position\":3,\"name\":\"How to use D3 with React components?\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/\",\"name\":\"Mansi Arora\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a58f57a35d110e644b024b38bf813f047dd8d76cc2354cdd1ac1ccfe5a2a508d?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Mansi Arora\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/\",\"name\":\"How to use D3 with React components? | TO THE NEW Blog\",\"description\":\"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 placed in the HTML. You call .append ('span') and it inserts a\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-use-d3-with-react-components\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/mansi\\\/#author\"},\"datePublished\":\"2016-10-07T11:45:07+05:30\",\"dateModified\":\"2016-10-07T14:46:55+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"How to use D3 with React components? | TO THE NEW Blog","description":"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 placed in the HTML. You call .append ('span') and it inserts a","canonical_url":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/#article","name":"How to use D3 with React components? | TO THE NEW Blog","headline":"How to use D3 with React components?","author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/#author"},"publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"},"datePublished":"2016-10-07T11:45:07+05:30","dateModified":"2016-10-07T14:46:55+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/#webpage"},"isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/#webpage"},"articleSection":"Technology, d3, integrating-d3, react, react-faux-dom"},{"@type":"BreadcrumbList","@id":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/tothenewco.pro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/tothenewco.pro\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/#listItem","name":"How to use D3 with React components?"},"previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/#listItem","position":3,"name":"How to use D3 with React components?","previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/tothenewco.pro\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/tothenewco.pro\/blog\/"},{"@type":"Person","@id":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/#author","url":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/","name":"Mansi Arora","image":{"@type":"ImageObject","@id":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a58f57a35d110e644b024b38bf813f047dd8d76cc2354cdd1ac1ccfe5a2a508d?s=96&d=mm&r=g","width":96,"height":96,"caption":"Mansi Arora"}},{"@type":"WebPage","@id":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/#webpage","url":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/","name":"How to use D3 with React components? | TO THE NEW Blog","description":"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 placed in the HTML. You call .append ('span') and it inserts a","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/#breadcrumblist"},"author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/#author"},"creator":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/mansi\/#author"},"datePublished":"2016-10-07T11:45:07+05:30","dateModified":"2016-10-07T14:46:55+05:30"},{"@type":"WebSite","@id":"https:\/\/tothenewco.pro\/blog\/#website","url":"https:\/\/tothenewco.pro\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"How to use D3 with React components? | TO THE NEW Blog","og:description":"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 placed in the HTML. You call .append ('span') and it inserts a","og:url":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/","og:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"How to use D3 with React components? | TO THE NEW Blog","twitter:description":"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 placed in the HTML. You call .append ('span') and it inserts a","twitter:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"40476","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 14:07:38","updated":"2024-02-29 09:04:14","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tHow to use D3 with React components?\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/tothenewco.pro\/blog"},{"label":"Technology","link":"https:\/\/tothenewco.pro\/blog\/category\/technology\/"},{"label":"How to use D3 with React components?","link":"https:\/\/tothenewco.pro\/blog\/how-to-use-d3-with-react-components\/"}],"_links":{"self":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/40476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/comments?post=40476"}],"version-history":[{"count":0,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/40476\/revisions"}],"wp:attachment":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/media?parent=40476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/categories?post=40476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/tags?post=40476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}