Skip to content

light9639/React-Chart-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

โšก React, Chart.js๋ฅผ ์ด์šฉํ•œ ํ…œํ”Œ๋ฆฟ.

:octocat: ๋ฐ”๋กœ๊ฐ€๊ธฐ : https://light9639.github.io/React-Chart-js/


โœจ React, Chart.js๋ฅผ ์ด์šฉํ•œ ํ…œํ”Œ๋ฆฟ โœจ

๐ŸŽ‰ React๋กœ ๋งŒ๋“œ๋Š” Chart.js

  • CRA๋กœ React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

    yarn create react-app my-app
  • Chart.js, React-chartjs-2 ์„ค์น˜

    yarn add chart.js react-chartjs-2

โœ๏ธ Data.js ์ž‘์„ฑ

  • Data.js ์ž‘์„ฑ

    export const UserData = [
      {
        id: 1,
        year: 2016,
        userGain: 80000,
        userLost: 823,
      },
      {
        id: 2,
        year: 2017,
        userGain: 45677,
        userLost: 345,
      },
      {
        id: 3,
        year: 2018,
        userGain: 78888,
        userLost: 555,
      },
      {
        id: 4,
        year: 2019,
        userGain: 90000,
        userLost: 4555,
      },
      {
        id: 5,
        year: 2020,
        userGain: 4300,
        userLost: 234,
      },
    ];

โœ’๏ธ App.js ์ž‘์„ฑ

  • App.js ์ž‘์„ฑ

    import { useState } from "react";
    import "./App.css";
    import BarChart from "./components/BarChart";
    import LineChart from "./components/LineChart";
    import PieChart from "./components/PieChart";
    import { UserData } from "./Data";
    
    function App() {
      const [userData, setUserData] = useState({
        labels: UserData.map((data) => data.year),
        datasets: [
          {
            label: "Users Gained",
            data: UserData.map((data) => data.userGain),
            backgroundColor: [
              'rgba(255, 99, 132, 0.2)',
              'rgba(54, 162, 235, 0.2)',
              'rgba(255, 206, 86, 0.2)',
              'rgba(75, 192, 192, 0.2)',
              'rgba(153, 102, 255, 0.2)',
              'rgba(255, 159, 64, 0.2)',
            ],
            borderColor: [
              'rgba(255, 99, 132, 1)',
              'rgba(54, 162, 235, 1)',
              'rgba(255, 206, 86, 1)',
              'rgba(75, 192, 192, 1)',
              'rgba(153, 102, 255, 1)',
              'rgba(255, 159, 64, 1)',
            ],
            borderWidth: 2,
          },
        ],
      });
    
      // IF YOU SEE THIS COMMENT: I HAVE GOOD EYESIGHT
    
      return (
        <div className="App">
          <div style={{ width: 700 }}>
            <BarChart chartData={userData} />
          </div>
          <div style={{ width: 700 }}>
            <LineChart chartData={userData} />
          </div>
          <div style={{ width: 700 }}>
            <PieChart chartData={userData} />
          </div>
        </div>
      );
    }
    
    export default App;

๐Ÿ“ BarChart.js, LineChart.js, PieChart.js ์ž‘์„ฑ

  • components ํด๋” ์ƒ์„ฑ.

  • BarChart.js ์ž‘์„ฑ

    import React from "react";
    import { Bar } from "react-chartjs-2";
    import { Chart as ChartJS } from "chart.js/auto";
    
    function BarChart({ chartData }) {
      return <Bar data={chartData} />;
    }
    
    export default BarChart;
  • LineChart.js ์ž‘์„ฑ

    import React from "react";
    import { Line } from "react-chartjs-2";
    import { Chart as ChartJS } from "chart.js/auto";
    
    function LineChart({ chartData }) {
      return <Line data={chartData} />;
    }
    
    export default LineChart;
  • PieChart.js ์ž‘์„ฑ

    import React from "react";
    import { Pie } from "react-chartjs-2";
    import { Chart as ChartJS } from "chart.js/auto";
    
    function PieChart({ chartData }) {
      return <Pie data={chartData} />;
    }
    
    export default PieChart;

๐Ÿš€ ์‹คํ–‰๋ฐ ๋นŒ๋“œ ๋ฐฉ๋ฒ•

  • ์‹คํ–‰ ๋ช…๋ น์–ด

    yarn start
  • ๋นŒ๋“œ ๋ช…๋ น์–ด

    yarn build

๐Ÿ“Ž ์ถœ์ฒ˜

About

๐Ÿ“Š React, Chart.js๋กœ ๋งŒ๋“  ๊ธฐ์ดˆ ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published