<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fang Ran</title>
    <description>The latest articles on DEV Community by Fang Ran (@reine0017).</description>
    <link>https://dev.to/reine0017</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F609237%2F81507c41-267b-4932-897c-675703933e3d.jpeg</url>
      <title>DEV Community: Fang Ran</title>
      <link>https://dev.to/reine0017</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/reine0017"/>
    <language>en</language>
    <item>
      <title>How to use Plotly.js in React to Visualize and Interact with Your Data</title>
      <dc:creator>Fang Ran</dc:creator>
      <pubDate>Sat, 10 Apr 2021 07:25:26 +0000</pubDate>
      <link>https://dev.to/reine0017/how-to-use-plotly-js-in-react-to-visualize-and-interact-with-your-data-2dj1</link>
      <guid>https://dev.to/reine0017/how-to-use-plotly-js-in-react-to-visualize-and-interact-with-your-data-2dj1</guid>
      <description>&lt;p&gt;I've written a &lt;a href="https://towardsdatascience.com/how-to-visualize-interactive-3d-network-with-python-plotly-4ef6989d83cc" rel="noopener noreferrer"&gt;tutorial&lt;/a&gt; on how to visualize interactive 3D networks (or data) with Python Plotly a few months ago and thought maybe it'll be a good idea to write another one for Plotly.js.&lt;br&gt;
One of the reasons why someone might prefer Plotly.js over Python Plotly could be because of loading speeds - I've once written a React + Flask application (where the datasets and Plotly figures are on Flask) and when I compared it to my React + Plotly.js application, interactivity and loading speeds are much better with Plotly.js.&lt;br&gt;
So here's a quick tutorial on how to use Plotly.js with React! ʕ•́ᴥ•̀ʔっ♡&lt;/p&gt;



&lt;p&gt;The final codes for this tutorial can be found in this GitHub repository: &lt;a href="https://github.com/Reine0017/mini-tutorials/tree/master/React-Tutorials/visualize-data-plotly" rel="noopener noreferrer"&gt;https://github.com/Reine0017/mini-tutorials/tree/master/React-Tutorials/visualize-data-plotly&lt;/a&gt;.&lt;br&gt;
Let's start off with a basic React application. In your desired project directory, run &lt;code&gt;npx create-react-app .&lt;/code&gt;. Then run &lt;code&gt;npm start&lt;/code&gt; to bring up the webpage. &lt;/p&gt;

&lt;p&gt;Next, we'll want to install the Plotly.js library. You can refer to this link for installation instructions. But basically you'll want to just run (from their npm package page linked above) &lt;code&gt;npm install react-plotly.js plotly.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Okay now that installation is done, let's begin writing some codes!&lt;/p&gt;



&lt;p&gt;There are TONS of different plots, maps and charts that you can play with with Plotly. In this tutorial, we'll look at how to create a simple helix-shaped interactive 3D scatter plot with Plotly.js.&lt;br&gt;
Let's start off by creating the Plotly component and then importing the react-plotly library. After that, just type in the  component like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqcwyenr7bplpg7n73sd2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqcwyenr7bplpg7n73sd2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we import this component into our App.js, it'll bring up an empty graph with no data (since we did not input any):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzj62qsad6o3i5af7737.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzj62qsad6o3i5af7737.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's populate the graph with some data. You can check out &lt;a href="https://plotly.com/python/" rel="noopener noreferrer"&gt;their official site&lt;/a&gt; to see what kinds of visualizations you can create with Plotly too.&lt;br&gt;
Since we want to create a Helix 3D graph in this tutorial, we'll first get the x, y, and z data coordinates for this 3D graph:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// In your PlotlyComponent.jsx
const linspaceFn = (startValue, stopValue, cardinality) =&amp;gt; {
  var arr = [];
  var step = (stopValue - startValue) / (cardinality - 1);
  for (var i = 0; i &amp;lt; cardinality; i++) {
    arr.push(parseFloat((startValue + (step * i)).toFixed(3)));
  }
  return arr;
}
  const t = linspaceFn(0, 20, 100)
  const x = t.map(i =&amp;gt; (Math.cos(i)))
  const y = t.map(i =&amp;gt; Math.sin(i))
  const z = t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's populate our  component with this data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data={[
        {
          x: x,
          y: y,
          z: z,
          mode: 'markers', 
          type:'scatter3d',
          marker: {
            size:12,
            color:z,     
            colorscale:'Viridis', 
            opacity:0.8
          }
        }
      ]}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we'll have the interactive 3D scatter plot diagram:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjvlgycit9zuu9nlguvrf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjvlgycit9zuu9nlguvrf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This 3D scatter plot is fully interactive and you can configure certain actions too when a user clicks on it. For demo purposes, I'll just make it console log its coordinates onClick.&lt;br&gt;
To register a user's clicks (on the coordinate points in the diagram), we can just write something like this inside the  component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onClick={(data) =&amp;gt; {
  console.log(data.points[0])
}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the final result:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivqebsiq0ga4ca8sn64s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivqebsiq0ga4ca8sn64s.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s it for today’s tutorial! Hope it was helpful and please feel free to comment or message me if you have any questions at all. Thank you guys for reading this and happy&lt;/p&gt;

&lt;p&gt;Plotly-ing! ʕ•́ᴥ•̀ʔっ♡&lt;/p&gt;

</description>
      <category>react</category>
      <category>datascience</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
