0

任何人都知道如何创建像carrto2 中使用的圆图?

4

2 回答 2

1

mbostock /d3 库对 Carrot2 输出具有良好的可视化效果。

Carrot2 的这个carrot2-rb ruby​​ 客户端返回一个带有 clusters 数组的对象。分数和短语属性可用于简单的圆环图。

像flare.json这样的树结构可以实现更多动态的可视化,比如可扩展的树状图。

这是一个基于 Carrot2 结果的可缩放轮。

这是我编写的使用文档元素创建flare.json 的coffeescript 代码。

clusters = [{"id":0,"size":3,"phrases":["Coupon"],"score":0.06441151442396735,"documents":["0","1","2"],"attributes":{"score":0.06441151442396735}},{"id":1,"size":2,"phrases":["Exclusive"],"score":0.7044284368639101,"documents":["0","1"],"attributes":{"score":0.7044284368639101}},{"id":2,"size":1,"phrases":["Other Topics"],"score":0.0,"documents":["3"],"attributes":{"other-topics":true,"score":0.0}}]

flare = get_flare clusters

get_children = (index, index2, clusters, documents) ->unless index == (clusters.length - 1)    # If not last cluster
  orphans = {'name': ''}
  intr    = _.intersection(documents, clusters[index2].documents);    

  if intr.length > 0    # continue drilling
    if index2 < (clusters.length - 1)    # Up until last element.
       # Get next layer of orphans
      orphan_docs = _.difference(intr, clusters[index2 + 1].documents)
      if orphan_docs.length > 0
        orphans = {'name': orphan_docs, 'size': orphan_docs.length}

      if _.intersection(intr, clusters[index2 + 1].documents).length > 0
        return [orphans, {'name': clusters[index2+1].phrases[0], 'children': get_children(index, (index2 + 1), clusters, intr)}]
      else 
        return [orphans]
    else
      # At second to last cluster, so terminate here
      return [{'name': inter}]  
  else                                # No intersection, so return bundle of current documents.
    return [{'name': documents}]  

  return [{'name': _.intersection(clusters[index].documents, clusters[index2].documents)}]



get_flare = (clusters) ->
  # Make root object
  flare =
    name: "root"
    children: []

  children = flare.children
  _.each(clusters[0..(clusters.length - 2)], (cluster, index) ->   # All clusters but the last. (It has already been compared to previous ones)
    #All documents for all remaining clusters in array
    remaining_documents =  _.flatten(_.map clusters[(index + 1)..clusters.length], (c) ->
      c.documents
    )

    root_child = {'name':  cluster.phrases[0], 'children': []}

    # Get first layer of orphans
    orphan_docs = _.difference(cluster.documents, remaining_documents)

    if orphan_docs.length > 0
      root_child.children.push {'name': orphan_docs, size: orphan_docs.length}

    for index2 in [(index + 1)..(clusters.length - 1)] by 1
      if _.intersection(cluster.documents, clusters[index2].documents).length > 0
        root_child.children.push {'name': clusters[index2].phrases[0], 'children': get_children(index, (index2), clusters, cluster.documents)}

    children.push root_child
  )  
  flare
于 2014-07-23T00:43:37.660 回答
0

你可以购买他们的 Circles Javascript 组件:http ://carrotsearch.com/circles-overview

于 2014-03-13T09:43:08.233 回答