chart.js๐
: 8๊ฐ์ ์ฐจํธ ์ ํ์ ์ง์ํ๋ ๋ฐ์ดํฐ ์๊ฐํ๋ฅผ ์ํ ์คํ์์ค JavaScript ๋ผ์ด๋ธ๋ฌ๋ฆฌ
https://www.chartjs.org/docs/latest/getting-started/
Getting Started | Chart.js
Getting Started Let's get started with Chart.js! Alternatively, see the example below or check samples. Create a Chart In this example, we create a bar chart for a single dataset and render it on an HTML page. Add this code snippet to your page: You should
www.chartjs.org
์ฌ์ฉ ๋ฐฉ๋ฒ
https://www.chartjs.org/docs/latest/getting-started/installation.html
Installation | Chart.js
Installation npm (opens new window) (opens new window) CDN CDNJS (opens new window) Chart.js built files are available on CDNJS (opens new window): https://cdnjs.com/libraries/Chart.js (opens new window) jsDelivr (opens new window) (opens new window) Chart
www.chartjs.org
์ฌ์ฉ๋ฐฉ๋ฒ์๋ npm install chart.js์ผ๋ก npm์ ์ค์นํ๋ ๋ฐฉ๋ฒ๊ณผ, cdn์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋ค. ๋๋ ๊ฐ๋จํ๊ฒ cdn์ ์ฌ์ฉํ๊ณ ์ ํ๋ค.
<div>
<canvas id="myChart"></canvas>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
๋๋ ๊ฐ๋จํ๊ฒ cdn์ ์ฌ์ฉํด๋ดค๊ณ , ๊ณต์ ํํ์ด์ง์ ์๋ ์์ ๋ฅผ ๊ฐ์ ธ์์ ํ๋ฒ ํ์ธํด๋ดค๋ค.
const ctx = document.getElementById("myChart");
let myChart = new Chart(ctx, {
type: "bar",
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "# of Votes",
data: [12, 19, 3, 5, 2, 3],
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: 1,
},
],
},
options: {
scales: {
y: {
beginAtZero: true,
},
},
},
});
์ผ๋จ ์ฐจํธ์ ์์ฑ๋ค์ ๋ค์๊ณผ ๊ฐ๋ค
type : ์ฐจํธ์ ํํ
์ง๊ธ์ 'bar'๋ผ์ ๋ง๋๊ธฐ ๊ทธ๋ํ๋ก ๋ณด์ด์ง๋ง, ๊ทธ ์ธ์ ๊ฐ์ผ๋ก๋ 'line', 'bubble', 'doughnut', 'polarArea', 'radar', 'scatter' ๊ฐ ์๋๋ฐ ์ด๋ฅผ ๋ณ๊ฒฝํด ๊ทธ๋ํ์ ํ์ ์ด ๋ฐ๋๊ฒ ๋๋ค.
data : ๋ง ๊ทธ๋๋ก ๋ฐ์ดํฐ. ์ฐจํธ์ ๋ด์ฉ
- labels : ๋ฐ์ดํฐ์ ํญ๋ชฉ๋ค์ ์ด๋ฆ(์ถ์ ์ ๋ชฉ)
- datasets : ๋ฐ์ดํฐ์ ์์ฑ
- data : datasets์ ๊ฐ
- labels : datasets์ ์ด๋ฆ(๋ฒ๋ก)
- backgroundColor : ๊ฐ ํญ๋ชฉ์ ๋ฐฐ๊ฒฝ์
- borderColor : ๊ฐ ํญ๋ชฉ์ ํ ๋๋ฆฌ์
- borderWidth : dataset์ ํ ๋๋ฆฌ ๋๊ป
options : ์ต์
- scales : x, y์ถ์ ์ง์ ํด์ค
- beginAtZero : ๋ง๊ทธ๋๋ก 0์์๋ถํฐ ์์ํ๋๋ ์๋ฏธ. true๋ผ๋ฉด ์ต์๊ฐ๊ณผ ๊ด๊ณ ์์ด y์ถ์ ๋ฒ์๊ฐ 0์์๋ถํฐ ์์๋๋ค.
- ๋ง์ฝ ๊ฐ์ด false๋ผ๋ฉด ์ต์๊ฐ์ธ purple์ ๊ฐ(2)๋ฅผ ๊ธฐ์ค์ผ๋ก y์ถ์ ๋ฒ์๊ฐ ์ ํด์ง๋ค.
๊ทธ ์ธ์๋ ์ฌ๋ฌ๊ฐ์ง ์ต์ ์ด ์์ผ๋ฉฐ ์์ธํ ์ฌํญ์ ๊ณต์ ํํ์ด์ง์์ ํ์ธํด๋ณด์
'javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
javascript/์์ ๋ณต์ฌ์ ๊น์ ๋ณต์ฌ (0) | 2025.03.20 |
---|---|
250313 to-do ๋ฆฌ์คํธ ๋ง๋ค๊ธฐ (2) (0) | 2025.03.14 |
250313 to-do ๋ฆฌ์คํธ ๋ง๋ค๊ธฐ (1) (1) | 2025.03.13 |
javascript/var๋ฅผ ๋์ด์ ์ฌ์ฉํ์ง ์๋ ์ด์ (2) | 2025.03.12 |
250307 ํ๋ก๊ทธ๋๋จธ์ค (0) | 2025.03.07 |