설치
npm install --save react-chartjs-2 chart.js
1) Line Chart
import React from "react";
import Chart from "chart.js/auto";
import { Line } from "react-chartjs-2";
const labels = ["January", "February", "March", "April", "May", "June"];
const data = {
labels: labels,
datasets: [
{
label: "My First dataset",
backgroundColor: "rgb(255, 99, 132)",
borderColor: "rgb(255, 99, 132)",
data: [0, 10, 5, 2, 20, 30, 45],
},
],
};
const LineChart = () => {
return (
<div>
<Line data={data} />
</div>
);
};
export default LineChart;
2) Bar Chart
import React from "react";
import Chart from "chart.js/auto";
import { Bar } from "react-chartjs-2";
const BarChart = () => {
const labels = ["January", "February", "March", "April", "May", "June"];
const data = {
labels: labels,
datasets: [
{
label: "My First dataset",
backgroundColor: "rgb(255, 99, 132)",
borderColor: "rgb(255, 99, 132)",
data: [0, 10, 5, 2, 20, 30, 45],
},
],
};
return (
<div>
<Bar data={data} />
</div>
);
};
export default BarChart;
3) Pie Chart
import React from "react";
import Chart from "chart.js/auto";
import { Pie } from "react-chartjs-2";
const labels = ["January", "February", "March", "April", "May", "June"];
const data = {
labels: labels,
datasets: [
{
label: "My First dataset",
backgroundColor: "rgb(255, 99, 132)",
borderColor: "rgb(0,0,255)",
data: [0, 10, 5, 2, 20, 30, 45],
},
],
};
const PieChart = () => {
return (
<div>
<Pie data={data} />
</div>
);
};
export default PieChart;
참고 사이트
https://upmostly.com/tutorials/how-to-use-chart-js-with-react
https://react-chartjs-2.js.org/examples
'Study > React' 카테고리의 다른 글
[React] MUI Button과 IconButton 정사각형으로 만들기 (0) | 2023.06.07 |
---|---|
[React] MUI Table 특정 Row의 border 지우기 (0) | 2023.06.07 |
[React] Proxy 여러 개 설정하기 (0) | 2023.05.03 |
[React] Axios 요청/응답 시간 구하기 (0) | 2023.05.03 |
[React] useState에서 객체 값 변경하기 (0) | 2023.05.03 |