Skip to content

D3.js チートシート

Data-Driven Documents — データを DOM 要素に束縛し、SVG で可視化するライブラリ。

01

はじめに

要素の選択

DOM 要素を選択し、操作をチェーンしてつなげます。

d3
d3.select("body")
  .append("p")
  .text("Hello D3!");

データバインディング

データ結合のパターン: select、data、enter、append。

d3
const data = [10, 20, 30, 40];

d3.select("svg")
  .selectAll("rect")
  .data(data)
  .join("rect")
  .attr("height", d => d)
  .attr("width", 20);

スケール

スケールはデータのドメインを視覚的なレンジに対応付けます。

d3
const scale = d3.scaleLinear()
  .domain([0, 100])
  .range([0, 500]);

scale(50); // 250
02

チャート

軸

チャートの軸を描くための Axis コンポーネント。

d3
const axis = d3.axisBottom(scale);
d3.select("svg")
  .append("g")
  .attr("transform", "translate(0,200)")
  .call(axis);

折れ線グラフ

折れ線グラフ用のラインジェネレーター。

d3
const line = d3.line()
  .x(d => xScale(d.x))
  .y(d => yScale(d.y));

d3.select("svg")
  .append("path")
  .datum(data)
  .attr("d", line);

Was this helpful?