标签:
var data={
"name":"A",
"children":[
{
"name":"B01",
"children":[
{ "name":"C01" },
{ "name":"C02" },
{ "name":"C03" }
]
},
{
"name":"B02",
"children":[
{ "name":"C04" },
{ "name":"C05" }
]
}
]
};
var stratify = d3.stratify().id(function (d) {return d.name;}).parentId(function(d){return d.parent;});
var rawData=[
{name:"Eve",parent:""},
{name:"Cain",parent:"Eve"},
{name:"Seth",parent:"Eve"},
{name:"Enos",parent:"Seth"},
{name:"Noam",parent:"Seth"},
{name:"Abel",parent:"Eve"},
{name:"Awan",parent:"Eve"},
{name:"Enoch",parent:"Awan"},
{name:"Azura",parent:"Eve"}
];
var data = stratify(rawData);
d3.cluster().size([200,300]).cluster(data);
//扩展d3
(function () {
function d3_functor(v) {
return typeof v === "function" ? v : function() {
return v;
};
}
d3.functor = d3_functor;
//贝塞尔曲线连接起始和终止点
function _diagonal() {
var source = function (d, i) {return d.source;},
target = function (d, i) {return d.target;}, data=null,
projection = function (d) {return [ d.x, d.y ];};
function diagonal(d, i) {
var p0 = source.call(this, d, i), p3 = target.call(this, d, i), m = (p0.y + p3.y) / 2, p = [ p0, {
x: p0.x,
y: m
}, {
x: p3.x,
y: m
}, p3 ];
p = p.map(projection);
return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3];
}
diagonal.source = function(x) {
if (!arguments.length) return source;
source = d3_functor(x);
return diagonal;
};
diagonal.target = function(x) {
if (!arguments.length) return target;
target = d3_functor(x);
return diagonal;
};
diagonal.projection = function(x) {
if (!arguments.length) return projection;
projection = x;
return diagonal;
};
return diagonal;
}
var __proto__=d3.constructor.prototype;
__proto__.diagonal=__proto__.diagonal || function () {
return _diagonal();
};
}());
var diagonal = d3.diagonal().projection(function (d) {return [d.y,d.x]});
g.selectAll(".link")
.data(data.descendants().slice(1))//过滤掉root
.enter().append("path")
.attr("class", "link")
.attr("d", function(d) {
return diagonal({source:{x:d.x,y:d.y},target:{x:d.parent.x,y:d.parent.y}});
});
var node = g.selectAll(".node")
.data(data.descendants())
.enter().append("g")
.attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
node.append("circle")
.attr("r", 3);
node.append("text")
.attr("dy", 3)
.attr("x", function(d) { return d.children ? -8 : 8; })
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.id; });
标签:
原文地址:http://www.cnblogs.com/yaoyinglong/p/5918126.html