Skip to content

Instantly share code, notes, and snippets.

@ericmustin
Created September 20, 2016 16:31
Show Gist options
  • Save ericmustin/b0a686b8c520a12c501f1cc68a89c8ac to your computer and use it in GitHub Desktop.
Save ericmustin/b0a686b8c520a12c501f1cc68a89c8ac to your computer and use it in GitHub Desktop.
givingThisAnotherShot
<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"> </script>
<script>
var diameter = 3, //max size of the bubbles
color = d3.scale.domain(["#d5e5ee",
"#303653",
"#a2a1d3",
"#cae6d3",
"#265596",
"#70adb6",
"#c694c1",
"#ca517d",
"#a92134",
"#f06838",
"#f6e541",
"#138651",
"#515e48",
"#c5a089",
"#4b4a4b",
"#000000",
"#b3bbbc",
"#5b7d95",
"#5b7d95",
"#432666",
"#105f51",
"#771022",
"#f2c431",
"#666666",
"#8e2929",
"#c33c30",
"#c694c1",
"#4b4a4b",
"#303653",
"#ffb059"]); //color category
var bubble = d3.layout.pack()
.sort(null)
.size([diameter, diameter])
.padding(1.5);
var svg = d3.select("body")
.append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "bubble");
var data = []
for(var i = 0; i< 100,000; i++) {
//convert numerical values from strings to numbers
data = data.map(function(d){ return i; });
//bubbles needs very specific format, convert data to this.
var nodes = bubble.nodes({children:data}).filter(function(d) { return !d.children; });
//setup the chart
var bubbles = svg.append("g")
.attr("transform", "translate(0,0)")
.selectAll(".bubble")
.data(nodes)
.enter();
//create the bubbles
bubbles.append("circle")
.attr("r", function(d){ return d.r; })
.attr("cx", function(d){ return d.x; })
.attr("cy", function(d){ return d.y; })
.style("fill", function(d) { return color(d.value); });
//format the text for each bubble
bubbles.append("text")
.attr("x", function(d){ return d.x; })
.attr("y", function(d){ return d.y + 5; })
.attr("text-anchor", "middle")
.text(function(d){ return "+"; })
.style({
"fill":"white",
"font-family":"Helvetica Neue, Helvetica, Arial, san-serif",
"font-size": "12px"
});
}
// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
var classes = [];
function recurse(name, node) {
if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
else classes.push({packageName: name, className: node.name, value: node.size});
}
recurse(null, root);
return {children: classes};
}
d3.select(self.frameElement).style("height", diameter + "px");
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment