๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

javascript

javascript/chart.js๋กœ ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ

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์ถ•์˜ ๋ฒ”์œ„๊ฐ€ ์ •ํ•ด์ง„๋‹ค.
     

beginAtZero : false

 

 

๊ทธ ์™ธ์—๋„ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์˜ต์…˜์ด ์žˆ์œผ๋ฉฐ ์ž์„ธํ•œ ์‚ฌํ•ญ์€ ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•ด๋ณด์ž