728x90
SMALL
1. D3.js란 무엇인가?
D3.js(Data-Driven Documents)는 데이터를 바탕으로 웹 문서를 조작하는 JavaScript 라이브러리입니다. D3.js를 사용하면 데이터를 기반으로 SVG, HTML, Canvas 등을 동적으로 생성하고 조작할 수 있습니다. 이는 데이터를 효과적으로 시각화하는 데 매우 유용합니다.
D3.js의 주요 특징:
- 데이터 바인딩: 데이터를 DOM 요소에 바인딩하여 데이터를 시각화하는데 사용합니다.
- 선택과 조작: CSS 선택자와 유사한 방식으로 DOM 요소를 선택하고 조작할 수 있습니다.
- 애니메이션: 트랜지션과 애니메이션을 쉽게 구현할 수 있습니다.
- 확장성: 다양한 차트와 그래프를 쉽게 확장하고 커스터마이징할 수 있습니다.
2. D3.js의 장점
- 유연성: D3.js는 특정한 차트 유형에 국한되지 않고, 모든 유형의 시각화를 가능하게 합니다.
- 강력한 데이터 조작: 데이터를 바인딩하고 조작하는 강력한 기능을 제공합니다.
- 광범위한 브라우저 지원: 대부분의 최신 브라우저에서 작동합니다.
- 활발한 커뮤니티: 많은 예제와 플러그인이 존재하며, 커뮤니티가 활발합니다.
3. D3.js 설치 및 설정
D3.js를 설치하고 사용하는 방법은 매우 간단합니다. 다음은 D3.js를 설치하고 간단한 웹 페이지에 추가하는 방법입니다.
설치 방법
- CDN 사용:
<script src="https://d3js.org/d3.v6.min.js"></script>
- npm 사용:
npm install d3
- yarn 사용:
yarn add d3
기본 설정
HTML 파일에 D3.js를 포함하고 간단한 차트를 만드는 예제입니다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>D3.js Example</title>
<script src="https://d3js.org/d3.v6.min.js"></script>
<style>
.bar {
fill: steelblue;
}
</style>
</head>
<body>
<svg width="500" height="500"></svg>
<script>
const data = [10, 20, 30, 40, 50];
const svg = d3.select("svg");
const margin = { top: 20, right: 30, bottom: 40, left: 90 };
const width = +svg.attr("width") - margin.left - margin.right;
const height = +svg.attr("height") - margin.top - margin.bottom;
const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const x = d3.scaleLinear()
.domain([0, d3.max(data)])
.range([0, width]);
const y = d3.scaleBand()
.domain(data)
.range([height, 0])
.padding(0.1);
g.append("g")
.attr("class", "x-axis")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "y-axis")
.call(d3.axisLeft(y));
g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", 0)
.attr("y", d => y(d))
.attr("width", d => x(d))
.attr("height", y.bandwidth());
</script>
</body>
</html>
4. D3.js의 주요 기능
데이터 바인딩
D3.js의 핵심 기능은 데이터와 DOM 요소를 바인딩하는 것입니다. 이를 통해 데이터를 시각화할 수 있습니다.
const data = [1, 2, 3, 4, 5];
const circles = d3.select('svg').selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('cx', d => d * 50)
.attr('cy', 50)
.attr('r', d => d * 5);
축 생성
D3.js를 사용하여 축을 쉽게 생성할 수 있습니다.
const x = d3.scaleLinear()
.domain([0, 100])
.range([0, 400]);
const xAxis = d3.axisBottom(x);
d3.select('svg')
.append('g')
.attr('transform', 'translate(0, 400)')
.call(xAxis);
애니메이션과 트랜지션
D3.js의 트랜지션 기능을 사용하여 애니메이션을 쉽게 추가할 수 있습니다.
d3.select('circle')
.transition()
.duration(1000)
.attr('r', 20);
5. D3.js 예제
바 차트 예제
다음은 D3.js로 간단한 바 차트를 만드는 예제입니다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>D3.js Bar Chart Example</title>
<script src="https://d3js.org/d3.v6.min.js"></script>
<style>
.bar {
fill: steelblue;
}
</style>
</head>
<body>
<svg width="600" height="400"></svg>
<script>
const data = [30, 86, 168, 281, 303, 365];
const svg = d3.select("svg");
const width = +svg.attr("width");
const height = +svg.attr("height");
const x = d3.scaleBand()
.domain(d3.range(data.length))
.range([0, width])
.padding(0.1);
const y = d3.scaleLinear()
.domain([0, d3.max(data)])
.nice()
.range([height, 0]);
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", (d, i) => x(i))
.attr("y", d => y(d))
.attr("height", d => y(0) - y(d))
.attr("width", x.bandwidth());
</script>
</body>
</html>
6. 추가 리소스
D3.js는 매우 강력하고 유연한 도구로, 학습 리소스도 다양합니다. 다음은 D3.js를 더 깊이 이해하는 데 도움이 되는 리소스입니다.
- D3.js 공식 문서: D3.js의 모든 기능과 사용법을 다루는 공식 문서입니다.
- Observable: D3.js 창시자 Mike Bostock가 만든 인터랙티브 노트북 플랫폼으로, 다양한 예제와 튜토리얼을 제공합니다.
- D3 Graph Gallery: 다양한 D3.js 그래프와 차트 예제를 제공합니다.
이 포스팅이 D3.js의 기본 개념과 사용법을 이해하는 데 도움이 되기를 바랍니다. D3.js를 활용하면 데이터를 더욱 생동감 있게 시각화할 수 있으며, 웹 애플리케이션에 강력한 데이터 시각화 기능을 추가할 수 있습니다.
LIST