<?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: Mathesh</title>
    <description>The latest articles on DEV Community by Mathesh (@matheshyogeswaran).</description>
    <link>https://dev.to/matheshyogeswaran</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%2F1036740%2Feea97fa0-64bd-409a-8d87-f28330f4e7c6.jpeg</url>
      <title>DEV Community: Mathesh</title>
      <link>https://dev.to/matheshyogeswaran</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/matheshyogeswaran"/>
    <language>en</language>
    <item>
      <title>ChartJS in React</title>
      <dc:creator>Mathesh</dc:creator>
      <pubDate>Fri, 31 Mar 2023 05:55:57 +0000</pubDate>
      <link>https://dev.to/matheshyogeswaran/chartjs-in-react-ci8</link>
      <guid>https://dev.to/matheshyogeswaran/chartjs-in-react-ci8</guid>
      <description>&lt;p&gt;We’ll talk about how to integrate the chartJS library with React in this section. You can build a variety of charts to utilize in your applications. It is a simple lesson that only covers the fundamentals of ChartJS. We’ll talk about bar charts, line charts, and pie charts in this blog.&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%2Fcrql20cjyjtnhszdypmr.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%2Fcrql20cjyjtnhszdypmr.png" alt="Basic File Structure"&gt;&lt;/a&gt;&lt;br&gt;
We will first develop the fundamental React app using the file structure mentioned above. The required packages will then be installed. The packages will be installed by us using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm add chart.js react-chartjs-2 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after that for demonstrating purposes here we create the Data.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const UserData = [
  {
    id: 1,
    year: 2010,
    userGain: 80000,
    userLost: 823,
  },
  {
    id: 2,
    year: 2011,
    userGain: 45677,
    userLost: 345,
  },
  {
    id: 3,
    year: 2012,
    userGain: 78888,
    userLost: 555,
  },
  {
    id: 4,
    year: 2013,
    userGain: 90000,
    userLost: 4555,
  },
  {
    id: 5,
    year: 2014,
    userGain: 4300,
    userLost: 234,
  },
  {
    id: 6,
    year: 2015,
    userGain: 90000,
    userLost: 4555,
  },
  {
    id: 7,
    year: 2016,
    userGain: 78888,
    userLost: 555,
  },
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user increase over time is displayed through this Data.js. After that, we’ll build the BarChart.js (under the components -&amp;gt; BarChart.js) file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { Bar } from "react-chartjs-2";
import { Chart as ChartJS } from "chart.js/auto";

function BarChart({ chartData }) {
  return &amp;lt;Bar data={chartData} /&amp;gt;;
}

export default BarChart;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in BarChart.js we sent the user data through props. In the Home.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import BarChart from "../components/BarChart";
import { UserData } from "../Data/Data";
import { useState } from "react";

const Home = () =&amp;gt; {
  const [userData, setUserData] = useState({
    labels: UserData.map((data) =&amp;gt; data.year),
    datasets: [
      {
        label: "Users Gained",
        data: UserData.map((data) =&amp;gt; data.userGain),
        backgroundColor: [
          "rgba(75,192,192,1)",
          "#ecf0f1",
          "#50AF95",
          "#f3ba2f",
          "#2a71d0",
          "#2a71d0",
          "#2a71d0",
        ],
        borderColor: "black",
        borderWidth: 2,
      },
    ],
  });

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Charts&amp;lt;/h1&amp;gt;
      &amp;lt;div style={{ width: 600 }}&amp;gt;
        &amp;lt;BarChart chartData={userData} /&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
export default Home;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the useState hook to convey data through Props. There are various possibilities for a bar chart, line chart, and pie chart right here in the useState. But in this case, we provide the year for the bar chart label.&lt;/p&gt;

&lt;p&gt;they are for the bar chart. Next, we switch to the Line chart, for which we develop LineChart.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { Line } from "react-chartjs-2";
import { Chart as ChartJS } from "chart.js/auto";

function LineChart({ chartData }) {
  return &amp;lt;Line data={chartData} /&amp;gt;;
}

export default LineChart;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar to a bar chart, but with the importation of a line rather than a bar component. We also sent props through here. The code for PieChart.js is available here as we finally move on to the pie chart.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { Pie } from "react-chartjs-2";
import { Chart as ChartJS } from "chart.js/auto";

function PieChart({ chartData }) {
  return &amp;lt;Pie data={chartData} /&amp;gt;;
}

export default PieChart;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar to bar and line charts, we import the pie component in this instance.&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%2Flp26k7vyvsk6eh92q09k.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%2Flp26k7vyvsk6eh92q09k.png" alt="View of the example"&gt;&lt;/a&gt;&lt;br&gt;
These are some fundamental steps for utilizing the chartJS library, which you can adapt in various other ways. I hope you learn something from this.&lt;/p&gt;

&lt;p&gt;Source code:- &lt;a href="https://github.com/matheshyogeswaran/chart.git" rel="noopener noreferrer"&gt;https://github.com/matheshyogeswaran/chart.git&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
