๋ฐ๋ก๊ฐ๊ธฐ : https://light9639.github.io/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 ์์ฑ
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 ์์ฑ
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;
-
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