D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
hanshenSun
Full window
Github gist
Circle-Packing
forked from
mbostock
's block:
Circle-Packing
<!DOCTYPE html> <meta charset="utf-8"> <style> text { font: 18px sans-serif; text-anchor: middle; } .node--hover circle { stroke: #000; stroke-width: 1.2px; } </style> <svg width="960" height="960"><g transform="translate(1,1)"></g></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> function hovered(hover) { return function(d) { d3.selectAll(d.ancestors().map(function(d) { return d.node; })).classed("node--hover", hover); }; } var svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height"); var format = d3.format(",d"); var color = d3.scaleSequential(d3.interpolateMagma) .domain([-4, 4]); var sequentialScale = d3.scaleSequential() .domain([0, 5]) .interpolator(d3.interpolateRainbow); var stratify = d3.stratify() .parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); }); var pack = d3.pack() .size([width - 2, height - 2]) .padding(3); d3.csv("flare.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; }); pack(root); var node = svg.selectAll(".node") .data(root.descendants()) .enter() .append("g") .attr("class", "node") .attr("transform", function (d) {return "translate(" + d.x + "," + d.y + ")";}) .each(function(d) { d.node = this; }) .on("mouseover", hovered(true)) .on("mouseout", hovered(false)); ; var circle = node.append("circle") .attr("r", function(d) {return d.r;}) .attr("fill", function(d) {return sequentialScale(d.depth);}) .attr("fill-opacity", function(d){return (d.depth ==0) ? 0:0.15}) // .style("display", function(d) { return (d.depth == 2) ? null : (d.depth ==3) ? null : "none"; }) .attr("stroke", "#cccccc") .attr("stroke-width",function(d){return (d.depth ==1) ? 1: 1}) .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); }); node.append("title") .text(function(d) {return (d.depth == 3) ? d.id.substring(d.id.lastIndexOf(".")+1) + "\n" + format(d.value) : null}); node.append("text") .text(function(d){return d.depth==1 ? d.id.substring(d.id.lastIndexOf(".")+1) : ""}) .style("font-weight", "bold") .style("font-size", "20px") .style("fill", "black") .attr("opacity", 0.82); node.append("text") .text(function(d){return d.depth==2 ? d.id.substring(d.id.lastIndexOf(".")+1) : ""}) .attr("opacity", 1) .style("font-size", "13px") .style("font-weight", "bold") .style("fill", "grey"); node.append("text") .text(function(d){return d.depth==3 ? d.id.substring(d.id.lastIndexOf(".")+1) : ""}) .style("font-size", "3px"); var node = svg.selectAll("circle,text"); // svg // .style("background", color(-1)) // .on("click", function() { zoom(root); }); // zoomTo([root.x, root.y, root.r * 2 + 10]); // function zoomTo(v) { // var k = width / 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; }); // } // 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 + 10]); // 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"; }); // } // var node = svg.select("g") // .selectAll("g") // .data(root.descendants()) // .enter().append("g") // .text(function(d) {return d.id}) // .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) // .attr("class", function(d) { return "node" + (!d.children ? " node--leaf" : d.depth ?"" : " node--root"); }) // .text(function(d){return d}); // var leaf = node.filter(function(d) { return !d.children; }); // leaf.append("clipPath") // .attr("id", function(d) { return "clip-" + d.id; }) // .append("use") // .attr("xlink:href", function(d) { return "#node-" + d.id + ""; }); // leaf.append("text") // .attr("clip-path", function(d) { return "url(#clip-" + d.id + ")"; }) // .selectAll("tspan") // .data(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1).split(/(?=[A-Z][^A-Z])/g); }) // .enter().append("tspan") // .attr("x", 0) // .attr("y", function(d, i, nodes) { return 13 + (i - nodes.length / 2 - 0.5) * 10; }) // .text(function(d) { return d; }); // node.append("title") // .text(function(d) { return d.id + "\n" + format(d.value); }); }); // function hovered(hover) { // return function(d) { // d3.selectAll(d.ancestors().map(function(d) { return d.node; })).classed("node--hover", hover); // }; // } </script>
https://d3js.org/d3.v4.min.js