Chart.js
- 이전 페이지 JS Plotly.js
- 다음 페이지 JS 구글 그래프
Chart.js는 HTML을 기반으로 차트를 만드는 데 사용되는 무료 JavaScript 라이브러리입니다.
그것은 가장 간단한 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", { data: {} options: {} });
형식적인 선 차트 문법:
const myChart = new Chart("myChart", { data: {} options: {} });
형식적인 바 차트 문법:
const myChart = new Chart("myChart", { type: "bar", data: {} options: {} });
散点图
房价 vs. 面积
원본 코드
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", { data: { datasets: [{ pointRadius: 4, pointBackgroundColor: "rgba(0,0,255,1)", data: xyValues }] }, options:{...} });
折线图
房价 vs. 面积
원본 코드
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", { 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", { 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", { data: { labels: xValues, datasets: [{ 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: "전 세계 와인 생산" } } });
- 이전 페이지 JS Plotly.js
- 다음 페이지 JS 구글 그래프