LoginSignup
0
0

More than 3 years have passed since last update.

[D3.js] たのしいグラフ描画 - 単純な円グラフ

Last updated at Posted at 2020-04-29

D3.js を使って単純な円グラフを描画します。

元データ

元データです。
キーと値のハッシュになっています。

testdata = { a: 40, b: 30, c: 20, d: 10 } 

できあがりイメージ

大変単純な円グラフができあがりました。

image.png

プログラム

プログラムの一部です。

  // 描画領域
  const svg = d3
    .create('svg')
    .attr('viewBox', [0, 0, width, height])
    .attr('width', width)
    .attr('height', height);
  // グラフを真ん中にするためのエレメント
  const chart = svg
    .append('g')
    .attr("transform", `translate(${width / 2}, ${height / 2})`); // 真ん中へ移動

  // 扇型を生成する
  const arc = d3
    .arc()
    .innerRadius(0)
    .outerRadius(radius);

  chart
    .selectAll(null) // 空のSelection
    .data(testdataReady)
    .enter()
    .append('path')
    .attr('d', arc) // 扇型を描画する
    .attr('fill', datum => colorSeq(datum.index)) // 塗りつぶし
    .attr("stroke", "black") // 枠線
    .style("stroke-width", "1px");

全体はObservableに置いています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0