D3词云布局圆形和矩形

时间:2018-09-05 23:22:42

标签: javascript d3.js d3-cloud

我是D3新手,正在尝试使用以下内容构建词云页面 JasonDavies d3-cloud

但是有时所有单词都以圆形显示,但有时以矩形显示。

如何使它们始终位于圆形布局中?

1 个答案:

答案 0 :(得分:0)

d3-cloud的布局算法将单词放置在从您提供的矩形(d3.layout.cloud().size([width, height]))的中心开始的椭圆内。

如果您的容器(size([width, height]))的大小与单词数和为这些单词指定的大小相比足够大,那么您会得到一个漂亮的圆圈(或者椭圆形,如果您的容器不是一个正方形):

Math.seedrandom('hello words');

var data = [{ text: "Hello", count: 38 }, { text: "World", count: 27 }, { text: "Whatever", count: 21 }, { text: "Massive", count: 21 }, { text: "Thing", count: 16 }, { text: "Something", count: 14 }, { text: "What", count: 12 }, { text: "Else", count: 9 }, { text: "Blabla", count: 6 }, { text: "Small", count: 6 }, { text: "VeryLong", count: 6 }, { text: "Word", count: 3 }, { text: "abcdef", count: 4 }, { text: "Elsewhere", count: 9 }, { text: "Anything", count: 3 }, { text: "Placeholder", count: 14 }, { text: "WhateverSmall", count: 3 }, { text: "Work", count: 12 }, { text: "Wording", count: 7 }, { text: "Verb", count: 4 }];

  var svg = d3.select("svg").append("g");

  let fill = d3.scaleOrdinal(d3.schemeCategory20);
  let size = d3.scaleLinear().range([0, 20]).domain([0, d3.max(data, d => d.count)]);

  let word_cloud_data = data
    .map( function(d) {
      return { text: d.text, size: 9 + size(d.count) * 3.5 };
    });

  let layout = d3.layout.cloud()
    .size([600, 600])
    .words(word_cloud_data)
    .padding(2.5)
    .rotate(d => ~~(Math.random() * 2) * -90)
    .fontSize(d => d.size)
    .on("end", draw);

  layout.start();

  function draw(words) {

    svg.append("g")
      .attr("transform", "translate(250,250)")
      .selectAll("text")
      .data(words)
      .enter().append("text")
      .style("fill", (d, i) => { d.color = fill(i); return d.color; })
      .style("text-anchor", "middle")
      .attr("transform", d => "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")")
      .text(d => d.text)
      .style("font-size", d => d.size + "px");
  }
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdn.rawgit.com/jasondavies/d3-cloud/master/build/d3.layout.cloud.js"></script>
<script src="https://d3js.org/d3-scale.v1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.4/seedrandom.min.js">
</script>
<svg width="700" height="700"></svg>

如果您的容器无法将您的单词保留为椭圆/圆形,d3-cloud的布局算法将开始在您选择用来容纳云的容器的角部寻找空间。它将开始看起来像矩形/正方形。 (到目前为止,甚至没有足够的空间来添加剩余的单词):

Math.seedrandom('hello words');

var data = [{ text: "Hello", count: 38 }, { text: "World", count: 27 }, { text: "Whatever", count: 21 }, { text: "Massive", count: 21 }, { text: "Thing", count: 16 }, { text: "Something", count: 14 }, { text: "What", count: 12 }, { text: "Else", count: 9 }, { text: "Blabla", count: 6 }, { text: "Small", count: 6 }, { text: "VeryLong", count: 6 }, { text: "Word", count: 3 }];

  var svg = d3.select("svg").append("g");

  let fill = d3.scaleOrdinal(d3.schemeCategory20);
  let size = d3.scaleLinear().range([0, 20]).domain([0, d3.max(data, d => d.count)]);

  let word_cloud_data = data
    .map( function(d) {
      return { text: d.text, size: 9 + size(d.count) * 3.5 };
    });

  let layout = d3.layout.cloud()
    .size([275, 275])
    .words(word_cloud_data)
    .padding(2.5)
    .rotate(d => ~~(Math.random() * 2) * -90)
    .fontSize(d => d.size)
    .on("end", draw);

  layout.start();

  function draw(words) {

    svg.append("g")
      .attr("transform", "translate(150,150)")
      .selectAll("text")
      .data(words)
      .enter().append("text")
      .style("fill", (d, i) => { d.color = fill(i); return d.color; })
      .style("text-anchor", "middle")
      .attr("transform", d => "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")")
      .text(d => d.text)
      .style("font-size", d => d.size + "px");
  }
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdn.rawgit.com/jasondavies/d3-cloud/master/build/d3.layout.cloud.js"></script>
<script src="https://d3js.org/d3-scale.v1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.4/seedrandom.min.js">
</script>
<svg width="700" height="700"></svg>

解决方案包括缩小单词的大小以提供d3-layout足够的空间,或者增加云容器的宽度和高度。

例如通过缩小单词的大小:

// domain reduced from [0, 20] to [0, 13]:
var sizeScale = d3.scaleLinear().range([0, 13]).domain([0, d3.max(data, d => d.count)]);

Math.seedrandom('hello word');

var data = [{ text: "Hello", count: 38 }, { text: "World", count: 27 }, { text: "Whatever", count: 21 }, { text: "Massive", count: 21 }, { text: "Thing", count: 16 }, { text: "Something", count: 14 }, { text: "What", count: 12 }, { text: "Else", count: 9 }, { text: "Blabla", count: 6 }, { text: "Small", count: 6 }, { text: "VeryLong", count: 6 }, { text: "Word", count: 3 }];

  var svg = d3.select("svg").append("g");

  let fill = d3.scaleOrdinal(d3.schemeCategory20);
  let size = d3.scaleLinear().range([0, 13]).domain([0, d3.max(data, d => d.count)]);

  let word_cloud_data = data
    .map( function(d) {
      return { text: d.text, size: 9 + size(d.count) * 3.5 };
    });

  let layout = d3.layout.cloud()
    .size([275, 275])
    .words(word_cloud_data)
    .padding(2.5)
    .rotate(d => ~~(Math.random() * 2) * -90)
    .fontSize(d => d.size)
    .on("end", draw);

  layout.start();

  function draw(words) {

    svg.append("g")
      .attr("transform", "translate(150,150)")
      .selectAll("text")
      .data(words)
      .enter().append("text")
      .style("fill", (d, i) => { d.color = fill(i); return d.color; })
      .style("text-anchor", "middle")
      .attr("transform", d => "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")")
      .text(d => d.text)
      .style("font-size", d => d.size + "px");
  }
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdn.rawgit.com/jasondavies/d3-cloud/master/build/d3.layout.cloud.js"></script>
<script src="https://d3js.org/d3-scale.v1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.4/seedrandom.min.js">
</script>
<svg width="700" height="700"></svg>