How to create circular word cloud using Json Daives' D3 word cloud

。_饼干妹妹 提交于 2019-12-23 09:27:33

问题


I'm using Json Daives' D3 word cloud in R. I have generated the word cloud but the problem I face is that the shape of the word cloud is not circular. I was going through d3.layout.cloud.js file to try and modify the code to achieve this but in vain. The file has a function called "place" which defines the shape of the cloud. But I could not figure out how to modify this function so as to achieve the circular shape. Has anyone else tried to achieve this circular representation? I am pasting the code of the place function below.

function place(board, tag, bounds) {
  var perimeter = [{x: 0, y: 0}, {x: size[0], y: size[1]}],
      startX = tag.x,
      startY = tag.y,
      maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]),
      s = spiral(size),
      dt = Math.random() < .5 ? 1 : -1,
      t = -dt,
      dxdy,
      dx,
      dy;

  while (dxdy = s(t += dt)) {
    dx = ~~dxdy[0];
    dy = ~~dxdy[1];

    if (Math.min(dx, dy) > maxDelta) break;

    tag.x = startX + dx;
    tag.y = startY + dy;

    if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 ||
        tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue;
    // TODO only check for collisions within current bounds.
    if (!bounds || !cloudCollide(tag, board, size[0])) {
      if (!bounds || collideRects(tag, bounds)) {
        var sprite = tag.sprite,
            w = tag.width >> 5,
            sw = size[0] >> 5,
            lx = tag.x - (w << 4),
            sx = lx & 0x7f,
            msx = 32 - sx,
            h = tag.y1 - tag.y0,
            x = (tag.y + tag.y0) * sw + (lx >> 5),
            last;
        for (var j = 0; j < h; j++) {
          last = 0;
          for (var i = 0; i <= w; i++) {
            board[x + i] |= (last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0);
          }
          x += sw;
        }
        delete tag.sprite;
        return true;
      }
    }
  }
  return false;
}

来源:https://stackoverflow.com/questions/36444049/how-to-create-circular-word-cloud-using-json-daives-d3-word-cloud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!