기록

D3 #1 - 기초 (svg, p 추가와 선택)

D3 · 2 / 3편
  1. D3 - 시작하며
  2. D3 #1 - 기초 (svg, p 추가와 선택)
  3. D3 #2 - 막대 차트와 선 차트 그리기

초기 학습은 html로 되어 있습니다.

기본 Base 코드

<!DOCTYPE html>
<html>
<head>
    <script src="https://d3js.org/d3.v6.min.js"></script>
</head>
<body>
    <p>
        Hello!
    </p>
</body>
</html>

위 코드를 실행후 아래 학습을 순서대로 진행 하시면 됩니다.

  • select, append
d3.select("body").append("p");
  • svg 추가

아래 코드를 콘솔 창에 입력 하여 주세요,

d3.select("body").append("svg").attr("width", 50).attr("height", 50).append("circle").attr("cx", 25).attr("cy", 25).attr("r", 25).style("fill", "purple");

위 코드는 변수로도 설정하여 추가할수 있습니다.

var bodySelection = d3.select("body");
    var svgSelection = bodySelection.append("svg")
        .attr("width", 50)
        .attr("height", 50);

    var circleSelection = svgSelection.append("circle")
        .attr("cx", 25)
        .attr("cy", 25)
        .attr("r", 25)
        .style("fill", "purple");
  • data 추가및 text 입력
var theData = [ 1, 2, 3 ];
var p = d3.select("body").selectAll("p").data(theData).enter().append("p").text("hello");

댓글

이름만 적고 남길 수 있어요