我对此有一些经验。
我把我的项目代码留给你(在 xampp 中执行)
<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
text-anchor: middle;
}
.node--hover circle {
stroke: #000;
stroke-width: 1.2px;
}
.node {
cursor: pointer;
}
.node:hover {
/* stroke: #000;
stroke-width: 0.5px; */
opacity: 0.3;
}
.node--leaf {
fill: #fff;
}
.label {
font: 12px "Maison Neue", Helvetica, Arial, sans-serif;
text-anchor: middle;
/* text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff; */
}
.label,
.node--root,
.node--leaf {
pointer-events: none;
}
</style>
<svg width="960" height="960"><g transform="translate(1,1)"></g></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
margin = 1,
diameter = +svg.attr("width"),
g = svg.append("g").attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
var format = d3.format(",d");
var color = d3.scaleLinear()
.domain([-1, 10])
.range(["rgb(200,80,77)", "rgb(245,255,255)"])
.interpolate(d3.interpolateHcl);
var stratify = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
var pack = d3.pack()
.size([width - margin, height - margin])
.padding(1);
d3.csv("./Visualise.csv", function(error, data) {
if (error) throw error;
var root = stratify(data)
.sum(function(d) { return d.value; })
.sort(function(a, b) { return b.value - a.value; });
var focus = root,
nodes = pack(root).descendants(),
view;
var circle = g.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("class", function(d) {
return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root";
})
.style("fill", function(d) {
if(d.data.value == 0){
return color(d.depth-1);
}
return d.children ? color(d.depth) : null;
})
.on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });
var text = g.selectAll("text")
.data(nodes)
.enter().append("text")
.attr("class", "label")
.style("fill-opacity", function(d) {
if(d.data.value == 0){return 0;}
return d.parent === root ? 1 : 0; })
.style("display", function(d) {
if( d.data.value == 0 || d.data.value == "" ) {return "none";}
return d.parent === root ? "inline" : "none"; })
.text(function(d) {
if(d.data.value == 0){return;};
return d.data.id.split(".").pop(-1) + "\n:\n"+ format(d.data.value); });//////
var node = g.selectAll("circle,text");
svg
.style("background", color(-1))
.on("click", function() { zoom(root); });
zoomTo([root.x, root.y, root.r * 2 + margin]);
function zoom(d) {
var focus0 = focus; focus = d;
var transition = d3.transition()
.duration(d3.event.altKey ? 7500 : 750)
.tween("zoom", function(d) {
var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
return function(t) { zoomTo(i(t)); };
});
transition.selectAll("text")
.filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
.style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
.on("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
.on("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
}
function zoomTo(v) {
var k = diameter / v[2]; view = v;
node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
circle.attr("r", function(d) { return d.r * k; });
}
});
</script>
//////visualise.csv
level,CategoryID,CategoryParentID,value,CategoryCount,id
0,-1,0,10000000,1000000,Cat
1,1,-1,263752,173805,Cat.egory_1
2,12,20081,0,0,Cat.egory_29.Leaf_1
3,13,50693,2470,337,Cat.egory_13.Leaf_2.Leaf_1
4,14,13,0,0,Cat.egory_13.Leaf_2.Leaf_1.Leaf_1
5,18,2735,0,0,Cat.egory_13.Leaf_2.Leaf_1.Leaf_11.Leaf_1
5,22,2705,0,0,Cat.egory_13.Leaf_2.Leaf_1.Leaf_8.Leaf_1
5,23,1020,0,0,Cat.egory_13.Leaf_2.Leaf_1.Leaf_5.Leaf_1
3,24,18875,3017,440,Cat.egory_13.Leaf_1.Leaf_1
4,25,24,0,0,Cat.egory_13.Leaf_1.Leaf_1.Leaf_1
4,26,24,0,0,Cat.egory_13.Leaf_1.Leaf_1.Leaf_2
3,27,18875,1173,227,Cat.egory_13.Leaf_1.Leaf_2
3,28,1430,0,0,Cat.egory_1.Leaf_21.Leaf_1
3,30,717,0,0,Cat.egory_3.Leaf_5.Leaf_1
3,31,386,5,5,Cat.egory_4.Leaf_2.Leaf_1
4,32,326,0,0,Cat.egory_4.Leaf_1.Leaf_1.Leaf_1
3,33,940,0,0,Cat.egory_1.Leaf_16.Leaf_1
2,34,1,3527,811,Cat.egory_1.Leaf_1
4,35,13623,0,0,Cat.egory_1.Leaf_1.Leaf_13.Leaf_1
3,36,34,616,84,Cat.egory_1.Leaf_1.Leaf_1
5,37,35692,0,0,Cat.egory_1.Leaf_1.Leaf_1.Leaf_16.Leaf_1
5,38,13600,0,0,Cat.egory_1.Leaf_1.Leaf_1.Leaf_13.Leaf_1
3,39,34,677,221,Cat.egory_1.Leaf_1.Leaf_2
4,40,39,0,0,Cat.egory_1.Leaf_1.Leaf_2.Leaf_1
5,41,156204,0,0,Cat.egory_1.Leaf_1.Leaf_2.Leaf_2.Leaf_1
6,42,35672,0,0,Cat.egory_1.Leaf_1.Leaf_2.Leaf_2.Leaf_20.Leaf_1
3,44,593,64,64,Cat.egory_1.Leaf_11.Leaf_1
4,48,14281,0,0,Cat.egory_1.Leaf_35.Leaf_2.Leaf_1
2,51,64482,2797,323,Cat.egory_33.Leaf_1
3,52,51,0,0,Cat.egory_33.Leaf_1.Leaf_1
4,53,27259,0,0,Cat.egory_33.Leaf_1.Leaf_10.Leaf_1
3,56,51,0,0,Cat.egory_33.Leaf_1.Leaf_2
2,57,45100,388,132,Cat.egory_31.Leaf_1
3,58,57,0,0,Cat.egory_31.Leaf_1.Leaf_1
4,59,32987,0,0,Cat.egory_31.Leaf_1.Leaf_5.Leaf_1
3,60,57,1,1,Cat.egory_31.Leaf_1.Leaf_2
5,61,108726,0,0,Cat.egory_31.Leaf_1.Leaf_4.Leaf_8.Leaf_1
2,63,1,15791,5506,Cat.egory_1.Leaf_2
3,64,63,0,0,Cat.egory_1.Leaf_2.Leaf_1
3,65,63,0,0,Cat.egory_1.Leaf_2.Leaf_2
3,66,63,175,52,Cat.egory_1.Leaf_2.Leaf_3
4,67,66,0,0,Cat.egory_1.Leaf_2.Leaf_3.Leaf_1
4,68,66,78,78,Cat.egory_1.Leaf_2.Leaf_3.Leaf_2
4,69,66,0,0,Cat.egory_1.Leaf_2.Leaf_3.Leaf_3
4,70,66,34,34,Cat.egory_1.Leaf_2.Leaf_3.Leaf_4
4,71,66,0,0,Cat.egory_1.Leaf_2.Leaf_3.Leaf_5
3,73,63,2002,144,Cat.egory_1.Leaf_2.Leaf_4
4,74,73,0,0,Cat.egory_1.Leaf_2.Leaf_4.Leaf_1
5,75,32723,0,0,Cat.egory_1.Leaf_2.Leaf_4.Leaf_8.Leaf_1
4,76,73,1,1,Cat.egory_1.Leaf_2.Leaf_4.Leaf_2
4,77,900,0,0,Cat.egory_1.Leaf_2.Leaf_7.Leaf_1
3,79,63,6,6,Cat.egory_1.Leaf_2.Leaf_5
3,80,63,447,447,Cat.egory_1.Leaf_2.Leaf_6
3,81,13905,2115,145,Cat.egory_1.Leaf_32.Leaf_1
4,82,81,0,0,Cat.egory_1.Leaf_32.Leaf_1.Leaf_1
6,83,38140,0,0,Cat.egory_1.Leaf_32.Leaf_1.Leaf_4.Leaf_3.Leaf_1
5,84,4049,0,0,Cat.egory_1.Leaf_32.Leaf_1.Leaf_5.Leaf_1
4,86,27,0,0,Cat.egory_13.Leaf_1.Leaf_2.Leaf_1
4,87,24,34,34,Cat.egory_13.Leaf_1.Leaf_1.Leaf_3
4,88,24,0,0,Cat.egory_13.Leaf_1.Leaf_1.Leaf_4
4,89,27,64,64,Cat.egory_13.Leaf_1.Leaf_2.Leaf_2
4,90,27,21,21,Cat.egory_13.Leaf_1.Leaf_2.Leaf_3
4,91,24,1,1,Cat.egory_13.Leaf_1.Leaf_1.Leaf_5
4,92,24,139,139,Cat.egory_13.Leaf_1.Leaf_1.Leaf_6
4,93,24,12,12,Cat.egory_13.Leaf_1.Leaf_1.Leaf_7
4,95,24,10,10,Cat.egory_13.Leaf_1.Leaf_1.Leaf_8
4,96,27,155,155,Cat.egory_13.Leaf_1.Leaf_2.Leaf_4
5,97,13779,0,0,Cat.egory_1.Leaf_29.Leaf_37.Leaf_29.Leaf_1