一、

和其他D3类一样,layout 可以链式传递,使用简明的申明添加多种自定义设置。

二、API

# d3.layout.cluster()

Creates a new cluster layout with the default settings: the default sort order is null; the default children accessor assumes each input data is an object with a children array; the default separation function uses one node width for siblings, and two node widths for non-siblings; the default size is 1×1.

使用默认配置创建新集群.

默认排序为null,

默认子节点是一个数组对象,

默认子节点之间宽度间隔为节点宽度,没有子节点为两个节点宽度,默认大小为1x1

# cluster(root
# cluster.nodes(root)

Runs the cluster layout, returning the array of nodes associated with the specified rootnode. The cluster layout is part of D3's family of hierarchical layouts. These layouts follow the same basic structure: the input argument to the layout is the root node of the hierarchy, and the output return value is an array representing the computed positions of all nodes. Several attributes are populated on each node:

运行集群布局,返回值为根节点下所有子节点列表,集群布局是D3 分层布局的一部分。这些布局都有共同的基础结构:1)输入参数根节点  2)返回值为所有计算好位置的节点列表。

每个节点有如下属性:

1)parent  - 父节点,值可能为 父节点、null 、 根节点

2)children - 子节点,值可能为 子节点列表、null、叶结点

3)depth - 深度, 值可能为 0 ~ 最底层叶结点层级

4)x - 计算好的x 坐标

5)y - 计算好的y 坐标

尽管 x y 可以有具体值,但可以为任意单位,比如x 为 半径, y 为角度 ,生成一个雷达图 而不是 笛卡尔图

# cluster.links(nodes)

Given the specified array of nodes, such as those returned by nodes, returns an array of objects representing the links from parent to child for each node. Leaf nodes will not have any links. Each link is an object with two attributes:

指定节点列表后,比如cluster.links(nodes) 返回 的是节点列表中所有节点之间的链接。叶结点没有链接。

每个关联有两个属性:

1)source - 开始节点,父节点

2)target - 结束节点,子节点

在获取对象线(diagonal)图是,这个方法非常有用,可以获取需展示的链接描述列表。

svg.selectAll("path")
.data(cluster.links(nodes))
.enter().append("path")
.attr("d", d3.svg.diagonal());

# cluster.children([children])

If children is specified, sets the specified children accessor function. If children is not specified, returns the current children accessor function, which by default assumes that the input data is an object with a children array:

如果指定了孩子节点,设定孩子节点存储函数。如果子节点没制定,返回当前子节点存储函数, 默认存储函数是children为key 的 数组,如下

function children(d) {
return d.children;
}

一般子节点文件结构如下:

{
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster", "size": 3938},
{"name": "CommunityStructure", "size": 3812},
{"name": "MergeEdge", "size": 743}
]
},
{
"name": "graph",
"children": [
{"name": "BetweennessCentrality", "size": 3534},
{"name": "LinkDistance", "size": 5731}
]
}
]
}
]
}

The children accessor is first invoked for root node in the hierarchy. If the accessor returns null, then the node is assumed to be a leaf node at the layout traversal terminates. Otherwise, the accessor should return an array of data elements representing the child nodes.

子节点首先是由root 节点为根节点。如果该节点没有子节点,那么就是叶节点,处于吐得最底端。另外,这个方法会返回子节点数据列表。

# cluster.sort([comparator])

If comparator is specified, sets the sort order of sibling nodes for the layout using the specified comparator function. If comparator is not specified, returns the current group sort order, which defaults to null for no sorting. The comparator function is invoked for pairs of nodes, being passed the input data for each node. The default comparator is null, which disables sorting and uses tree traversal order. For example, to sort sibling nodes in descending order by the associated input data's string name attribute, say:

如果指定比较函数,就会使用用户自定义比较函数布局。如果比较函数没有指定,默认是null 不排序。比较函数是节点两两比较,传递每个节点的数据。默认比较函数为null,关闭比较使用树的递归排序。比如,对子节点对名字字段进行降序排列,代码如下:

function comparator(a, b) {
return d3.ascending(a.name, b.name);
}

# cluster.separation([separation])

If separation is specified, uses the specified function to compute separation between neighboring nodes. If separation is not specified, returns the current separation function, which defaults to:

如果间隔函数指定,将使用间隔函数分割相邻节点。如果间隔函数不指定,返回当前间隔函数,默认如下:

function separation(a, b) {
return a.parent == b.parent ? 1 : 2;
}

A variation that is more appropriate for radial layouts reduces the separation gap proportionally to the radius:

对于雷达图,如下变化会更合适,根据节点层级减少节点间隔

function separation(a, b) {
return (a.parent == b.parent ? 1 : 2) / a.depth;
}

The separation function is passed two neighboring nodes a and b, and must return the desired separation between nodes. The nodes are typically siblings, though the nodes may also be cousins (or even more distant relations) if the layout decides to place such nodes adjacent.

间隔函数传入参数为两个节点,返回值为两个节点之间的间隔。如果图一定决定节点如何相邻,两个节点必须相邻,可能节点是表节点(甚至更远点),

# cluster.size([size])

If size is specified, sets the available layout size to the specified two-element array of numbers representing x and y. If size is not specified, returns the current size, which defaults to 1×1, or null if a nodeSize is in use. Although the layout has a size in x and y, this represents an arbitrary coordinate system. For example, to produce a radial layout where the tree breadth (x) in measured in degrees, and the tree depth (y) is a radius r in pixels, say [360, r].

如果大小指定,设置图大小的参数为x,y。如下:

d3.layout.cluster()
.size([height, width - 160])

如果大小没制定,将返回当前大小,默认为1x1 , 或 null (如果使用节点大小)。尽管布局有x,y 指定大小,这可以指定任意坐标系。比如,雷达图, x 的坐标为角度, y 为树的深度 单位为像素, 如 [360,r]

# cluster.nodeSize([nodeSize])

If nodeSize is specified, sets a fixed size for each node as a two-element array of numbers representing x and y. If nodeSize is not specified, returns the current node size, which defaults to null, meaning that the layout has an overall fixed size, which can be retrieved using size.

如果指定节点大小,格式为[width,height] 。 如果没指定节点大小,返回当前节点大小,默认为null,表示布局有一个整体确定的大小,可用cluster.size([size])恢复。

# cluster.value([value])

If value is specified, sets the value accessor to the specified function. If value is not specified, returns the current value accessor which defaults to null, meaning that the value attribute is not computed. If specified, the value accessor is invoked for each input data element, and must return a number representing the numeric value of the node. This value has no effect on the cluster layout, but is generic functionality provided by hierarchy layouts.

如果指定值,设置值函数。如果没指定,返回当前值函数,默认为null,表示值没有计算。如果指定,值函数将会对每个节点进行计算,返回一个数值。这个值对接点布局没有任何影响,是通用功能层次结构布局的方法。

例子:

<!DOCTYPE html>
<meta charset="utf-8">
<style> .node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
} .node {
font: 10px sans-serif;
} .link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
} </style>
<body>
<div>
请选择渲染的层级
<select id="selectDepth">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div> <div id="result"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="data.js"></script>
<script>
function renderTree (data) {
document.getElementById('result').innerHTML = ''; var width = 960,
height = 2200; var cluster = d3.layout.cluster()
.size([height, width - 160]); var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; }); var svg = d3.select("#result").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40,0)");
var nodes = cluster.nodes(data),
links = cluster.links(nodes); var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal); var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
.on('click', function (d) {
console.log(d);
}) node.append("circle")
.attr("r", 4.5); node.append("text")
.attr("dx", function(d) { return d.children ? -8 : 8; })
.attr("dy", 3)
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.name; });
}; renderTree (getTreeDataByDepth(data, 1)); function getTreeDataByDepth (data, depth) {
// console.log(data,depth);
if ((depth === 0) ||
(data === null) ||
(data === undefined)) {
return null;
} var result = {}
result['name'] = data['name'];
if (data.children !== undefined) {
for (var i = 0 ; i < data.children.length ; i++) {
var child = getTreeDataByDepth(data.children[i], depth - 1);
if (child !== null) {
if (result['children'] === undefined) {
result['children'] = [];
}
result['children'].push(child);
}
}
}
return result;
} var select = document.getElementById('selectDepth');
select.addEventListener('change', function (e) {
var depth = e.target.value;
console.log();
renderTree (getTreeDataByDepth(data, depth));
});
</script>
</body>
</html>
var data = {
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster", "size": 3938},
{"name": "CommunityStructure", "size": 3812},
{"name": "HierarchicalCluster", "size": 6714},
{"name": "MergeEdge", "size": 743}
]
},
{
"name": "graph",
"children": [
{"name": "BetweennessCentrality", "size": 3534},
{"name": "LinkDistance", "size": 5731},
{"name": "MaxFlowMinCut", "size": 7840},
{"name": "ShortestPaths", "size": 5914},
{"name": "SpanningTree", "size": 3416}
]
},
{
"name": "optimization",
"children": [
{"name": "AspectRatioBanker", "size": 7074}
]
}
]
},
{
"name": "animate",
"children": [
{
"name": "interpolate",
"children": [
{"name": "ArrayInterpolator", "size": 1983},
{"name": "ColorInterpolator", "size": 2047},
{"name": "DateInterpolator", "size": 1375},
{"name": "Interpolator", "size": 8746},
{"name": "MatrixInterpolator", "size": 2202},
{"name": "NumberInterpolator", "size": 1382},
{"name": "ObjectInterpolator", "size": 1629},
{"name": "PointInterpolator", "size": 1675},
{"name": "RectangleInterpolator", "size": 2042}
]
},
{"name": "ISchedulable", "size": 1041},
{"name": "Parallel", "size": 5176},
{"name": "Pause", "size": 449},
{"name": "Scheduler", "size": 5593},
{"name": "Sequence", "size": 5534},
{"name": "Transition", "size": 9201},
{"name": "Transitioner", "size": 19975},
{"name": "TransitionEvent", "size": 1116},
{"name": "Tween", "size": 6006},
{"name": "Easing", "size": 17010},
{"name": "FunctionSequence", "size": 5842},
]
},
{
"name": "display",
"children": [
{"name": "DirtySprite", "size": 8833},
{"name": "LineSprite", "size": 1732},
{"name": "RectSprite", "size": 3623},
{"name": "TextSprite", "size": 10066}
]
},
{
"name": "flex",
"children": [
{"name": "FlareVis", "size": 4116}
]
}]
}

【D3】cluster layout的更多相关文章

  1. 【D3】D3学习轨迹-----学习到一定层度了再更新

    1.  首先了解SVG的基本元素 http://www.w3school.com.cn/svg/ 2.  了解d3的专有名词  http://www.cnblogs.com/huxiaoyun90/p ...

  2. 【转】Auto Layout 进阶

    原文:http://blog.csdn.net/ysy441088327/article/details/12558097   引言: Auto Layout是iOS6发布后引入的一个全新的布局特性, ...

  3. 【D3】transition API

    摘要: 动画类API 一.API 使用 1. 1 d3.ease 1.2 d3.timer Start a custom animation timer, invoking the specified ...

  4. 【转载】PADS Layout将导入DXF,并转换成板框步骤

    1.在PADS Layout中选择 Import... 2.选择DXF文件(一般由结构工程师给出),直接点OK即可. 3.导入后,板框图一角视图如下.右键选择 Select Shapes,然后双击外框 ...

  5. 【ExtJS】 布局Layout

    布局用于定义容器如何组织内部子元素和控制子元素的大小. ExtJS中有两种类型的布局:Container容器类布局与Component组件类布局. Containter容器类布局:负责容器内容Extj ...

  6. 【原创】pads layout 画多边形copper,出现Self-Intersecting Polygon,解决办法

    在做线性位移传感器的电路板时,需要在一个很小的多边形Copper操作,总是提示“Self-Intersecting Polygon”报错,意思是outline线自身交叉,换句话说就是线宽与多边形尺寸没 ...

  7. 【原创】pads2007 Layout 电气连接性检查过孔显示错误

    如图所示的电源铜皮过孔,勾选stiching选项,连接性检查会报错误: 去掉stiching选项,连接错误消失.

  8. 【D3】D3词汇表

    按字母顺序 axis:数轴或坐标轴表示两个维度上数据尺度的直线 bar chart:条形图 (参见Excel)以矩形宽度反映数值大小的图表形式 bar:条形以宽度反映数值大小的矩形(rect) bin ...

  9. 【翻译】Sencha Touch2.4 The Layout System 布局

    [翻译]The Layout System 布局 In Sencha Touch there are two basic building blocks: componentsand containe ...

随机推荐

  1. Linux_Ununtu 16.04 的下载安装并部署.Net Core 网站

    第一次接触Linux也难免有些懵逼,因为公司项目必须用.Net Core 开发一个后端服务应用:第一次用Linux给我的感觉就像在用2000年的手机一样:没用智能的操作:让人崩溃的用户体验.说多了都是 ...

  2. JAVA web.xml中引用多个XML

    web.xml里加<context-param><param-name>contextConfigLocation</param-name><param-va ...

  3. Access denied for user 'root'@'localhost' (using password: NO)错误的解决方案

    在windows下使用PHP连接MYSQL数据库,确定MYSQL的服务已经启动了,而且Workbench也是可以连上去的,但是始终网页测试都提示Access Denied. 最终解决办法: set p ...

  4. Filter自动登录

    Dao层略过 Domain略过 Service层过 Web层 Select逻辑 获取表单数据,Web-service--Dao返回用户信息 如果返回不为null否则,重定向到登录页面.则判断用户是否勾 ...

  5. nodejs 字符串全排列 和 去重

    以前写了个java版的 现在写个nodejs 版的 var list = sort('CCAV');var noRepeat = {};for(var i in list){ noRepeat[lis ...

  6. IBM芯片新功能:诊断癌症

    治愈任何疾病的最好方法是什么?-------------- 当然是把它扼杀在摇篮里. "早发现早治疗"已经是老生常谈,但要准确监测疾病又谈何容易?不过现在,IBM这枚小小的芯片就能 ...

  7. Jenkins插件开发

    一.环境配置 不赘述,直接看wiki:https://wiki.jenkins.io/display/JENKINS/Extend+Jenkins 二.内容说明 1.插件代码结构 src/main/j ...

  8. 【LeetCode】110. Balanced Binary Tree

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  9. 【Android Developers Training】 73. 布局变化的动画

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  10. 如何在新浪sae服务器上面连接redis

    1.创建php空应用 2.选择