<?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>Forem: Enes Akkaya</title>
    <description>The latest articles on Forem by Enes Akkaya (@enesakk0).</description>
    <link>https://forem.com/enesakk0</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%2F1363557%2F550c493c-2e8f-4bbc-8a76-4746709db1ae.jpeg</url>
      <title>Forem: Enes Akkaya</title>
      <link>https://forem.com/enesakk0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/enesakk0"/>
    <language>en</language>
    <item>
      <title>React Read Excel and Data Visualize</title>
      <dc:creator>Enes Akkaya</dc:creator>
      <pubDate>Mon, 18 Mar 2024 21:38:06 +0000</pubDate>
      <link>https://forem.com/enesakk0/react-read-excel-and-data-visualize-1498</link>
      <guid>https://forem.com/enesakk0/react-read-excel-and-data-visualize-1498</guid>
      <description>&lt;p&gt;Hello everyone, I will tell you Excel data read and fetched data visualization in this article. We will use xlsx and tremor.so libraries and in this article after, you can create interactive components.&lt;/p&gt;

&lt;p&gt;First, I will install the requriment packages and create a sample Excel Data. In addition, we will install React via Vite and write code JSX.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm create vite@latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And, starting "npm install" command also, We installed React via Vite!&lt;/p&gt;

&lt;p&gt;Okay, let's install the xlsx and tremor.so libraries;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;xlsx; You can read Excel and convert to json objects.&lt;/li&gt;
&lt;li&gt;tremor; You created objects of json, you can visualize or KPI Metric card via Tremor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Install xlsx via NPM;&lt;br&gt;
&lt;code&gt;npm install xlsx&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Install tremor.so via NPM;&lt;br&gt;
&lt;code&gt;npm install @tremor/react&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And "tremor" also has package dependency with tailwindcss we will install tailwindcss.&lt;br&gt;
&lt;code&gt;npm install -D tailwindcss postcss autoprefixer &lt;br&gt;
npx tailwindcss init -p&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At last, we finished installing the packages and let's start the project.&lt;br&gt;
&lt;code&gt;npm run dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Before, We will create an xlsx file via Excel. For example, We have a company and company sales data on hand in addition this data want analysis and visualization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;And, We will create KPI metric cards' and inside add month change percentange.&lt;/li&gt;
&lt;li&gt;In addition, We will do trend and visualization with company's sales.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have sample data on Excel;&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%2Fcibakjfj43ruow950hrh.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%2Fcibakjfj43ruow950hrh.png" alt="Example Excel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, We can read Excel data and convert json object. First, We can import libraries dependency. In addition We will usecustom hooks.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { useEffect, useState } from "react";&lt;br&gt;
import * as XLSX from "xlsx";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And, create file for custom hooks, custom hook will run below code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const useFile = () =&amp;gt; {
  // Returned Excel JSON datas keep.
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    // Fetch Excel file.
    fetch("./datas.xlsx")
      // Convert to ArrayBuffer.
      .then((res) =&amp;gt; res.arrayBuffer())
      .then((data) =&amp;gt; {
        const wb = XLSX.read(data, { type: "buffer" });

        const wsname = wb.SheetNames[0];
        const ws = wb.Sheets[wsname];

        // Convert to JSON.
        const json = XLSX.utils.sheet_to_json(ws);

        setData(json);
      });
  }, []);

  return data;
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let's check the returned JSON data. We have done this and output the result to the console. &lt;/p&gt;

&lt;p&gt;When I checked the output, we saw the image we wanted, but we should make some changes and the "tremor" library needs a data format.&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%2F8ji2g5iivuwicww9peoq.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%2F8ji2g5iivuwicww9peoq.png" alt="Data Format"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we will change the object shown this in the picture. We will create a new component.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const useConverted = (value) =&amp;gt; {
  const result = {};

  value.forEach(({ Month, Product, Amount, TotalPrice }) =&amp;gt; {
    if (!result[Product]) {
      result[Product] = { fyAmount: 0, trend: {} };
    }

    if (!result[Product].trend[Month]) {
      result[Product].trend[Month] = { amount: 0, totalPrice: 0 };
    }

    result[Product].fyAmount += Amount;
    result[Product].trend[Month]["amount"] += Amount;
    result[Product].trend[Month]["totalPrice"] += TotalPrice;
  });

  const final = Object.keys(result).map((key) =&amp;gt; {
    return { product: key, ...result[key] };
  });

  return final;
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We created a new component and gave it component a custom hook. Finally, we completed the code output.&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%2Fdi2864bbprrwcvnkfsd5.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%2Fdi2864bbprrwcvnkfsd5.png" alt="Changed Output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have completed the sales data. And we begin the data visualization stage. &lt;br&gt;
First, we will choose which line chart component to use. In addition, we will trend analysis, and can line chart.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;section className="p-10 flex justify-center items-center h-screen gap-3"&amp;gt;
        {data.map((item) =&amp;gt; {
          const trend = Object.keys(item.trend).map((key) =&amp;gt; {
            return {
              date: key,
              ...item.trend[key],
            };
          });

          return (
            &amp;lt;Card key={crypto.randomUUID()}&amp;gt;
              &amp;lt;Title&amp;gt;{item.product} Product Sales Trend&amp;lt;/Title&amp;gt;
              &amp;lt;LineChart
                className="mt-6"
                data={trend}
                index="date"
                categories={["totalPrice"]}
                colors={["emerald", "gray"]}
                yAxisWidth={40}
                curveType="monotone"
              /&amp;gt;
            &amp;lt;/Card&amp;gt;
          );
        })}
&amp;lt;/section&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Finally, We created chart components at bellow.&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%2F00bk2l9knpxi5oxervlh.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%2F00bk2l9knpxi5oxervlh.png" alt="Final Result"&gt;&lt;/a&gt;&lt;/p&gt;

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