Chart.js
그래프.js는 HTML을 기반으로 차트를 만드는 데 사용되는 무료 JavaScript 라이브러리입니다.
그래프.js는 가장 간단한 JavaScript 시각화 라이브러리 중 하나로, 다음과 같은 내장 차트 유형을 가집니다:
- 산점도(Scatter Plot)
- 라인 차트(Line Chart)
- 바 차트(Bar Chart)
- 파이 차트(Pie Chart)
- 도넛 차트(Donut Chart)
- 버블 차트(Bubble Chart)
- 면적 차트(Area Chart)
- 레이더 차트(Radar Chart)
- 혼합 차트(Mixed Chart)
Chart.js를 어떻게 사용하나요?
Chart.js는 사용하기 매우 쉽습니다.
먼저, CDN(콘텐츠 배포 네트워크)을 가리키는 링크를 추가하세요:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"> </script>
그런 다음, <canvas>를 그래프를 그릴 위치에 추가하세요:
<canvas id="myChart" style="width:100%;max-width:700px"></canvas>
canvas 요소는 독특한 id를 가져야 합니다.
이렇게 됩니다!
표준 산점도 차트 문법:
const myChart = new Chart("myChart", { type: "scatter", data: {}, options: {} );
표준 라인 차트 문법:
const myChart = new Chart("myChart", { type: "line", data: {}, options: {} );
표준 바 차트 문법:
const myChart = new Chart("myChart", { type: "bar", data: {}, options: {} );
산점도
가격 대비 면적
원본 코드
const xyValues = [ {x:50, y:7}, {x:60, y:8}, {x:70, y:8}, {x:80, y:9}, {x:90, y:9}, {x:100, y:9}, {x:110, y:10}, {x:120, y:11}, {x:130, y:14}, {x:140, y:14}, {x:150, y:15} ]; new Chart("myChart", { type: "scatter", data: { datasets: [{ pointRadius: 4, pointBackgroundColor: "rgba(0,0,255,1)", data: xyValues }] }, options:{...} );
선형도표
가격 대비 면적
원본 코드
const xValues = [50,60,70,80,90,100,110,120,130,140,150]; const yValues = [7,8,8,9,9,9,10,11,14,14,15]; new Chart("myChart", { type: "line", data: { labels: xValues,, datasets: [{ backgroundColor:"rgba(0,0,255,1.0)", borderColor: "rgba(0,0,255,0.1)", data: yValues }] }, options:{...} );
borderColor를 설정하면 0
그래서 산점도로 선형도표를 그릴 수 있습니다:
borderColor: "rgba(0,0,0,0)",
다중줄
원본 코드
const xValues = [100,200,300,400,500,600,700,800,900,1000]; new Chart("myChart", { type: "line", data: { labels: xValues,, datasets: [{ data: [860,1140,1060,1060,1070,1110,1330,2210,7830,2478], borderColor: "red", fill: false },{ data: [1600,1700,1700,1900,2000,2700,4000,5000,6000,7000], borderColor: "green", fill: false },{ data: [300,700,2000,5000,6000,4000,2000,1000,200,100], borderColor: "blue", fill: false }] }, options: { legend: {display: false} } );
선형도표
원본 코드
const xValues = []; const yValues = []; generateData("x * 2 + 7", 0, 10, 0.5); new Chart("myChart", { type: "line", data: { labels: xValues,, datasets: [{ fill: false,, pointRadius: 1,, borderColor: "rgba(255,0,0,0.5)", data: yValues }] }, options: {...} ); function generateData(value, i1, i2, step = 1) { for (let x = i1; x <= i2; x += step) { yValues.push(eval(value)); xValues.push(x); } }
함수 그래프
선형 그래프와 동일. generateData 매개변수를 변경하면 됩니다:
generateData("Math.sin(x)", 0, 10, 0.5);
막대그래프
원본 코드
var xValues = ["이탈리아", "프랑스", "스페인", "미국", "아르헨티나"]; var yValues = [55, 49, 44, 24, 15]; var barColors = ["red", "green","blue","orange","brown"]; new Chart("myChart", { type: "bar", data: { labels: xValues,, datasets: [{ backgroundColor: barColors,, data: yValues }] }, options: {...} );
한 개의 막대만 색상 지정:
var barColors = ["blue"];
모든 막대가 동일한 색상:
var barColors ="red";
다양한 깊이의 색상:
var barColors = [ "rgba(0,0,255,1.0)", "rgba(0,0,255,0.8)", "rgba(0,0,255,0.6)", "rgba(0,0,255,0.4)", "rgba(0,0,255,0.2)", ];
수평 막대그래프
type을 "bar"
변경하여 "horizontalBar"
:
type: "horizontalBar",
파이그래프
인스턴스
new Chart("myChart", { type: "pie", data: { labels: xValues,, datasets: [{ backgroundColor: barColors,, data: yValues }] }, options: { title: { display: true,, text: "全球葡萄酒生산" } } );