Skip to content

Instantly share code, notes, and snippets.

@getsetbro
Created November 28, 2012 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save getsetbro/4163494 to your computer and use it in GitHub Desktop.
Save getsetbro/4163494 to your computer and use it in GitHub Desktop.
d3.js bubbles with zoom & dblclick to select one
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Bubble Chart</title>
<link type="text/css" rel="stylesheet" href="bubble.css"/>
<style type="text/css">body{margin:0;font: 10px sans-serif;}</style>
</head>
<body>
<div id="chart"></div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript" >
var r = $(window).width() - 99,
h = $(window).height() - 99,
format = d3.format(",d"),
fill = d3.scale.category20c();
var bubble = d3.layout.pack().sort(null).size([r,h]);
var vis = d3.select("#chart").append("svg").attr("class", "bubble")
.call(d3.behavior.zoom().on("zoom", redraw))
.append("g").attr("class", "group2");
d3.json("cantGetRidOfThis", function() {
var node = vis.selectAll("g.node")
.data(bubble.nodes(flat).filter(function(d) {return !d.children;}))
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {return "translate(" + d.x + "," + d.y + ")";});
node.append("title").text(function(d) {return d.className + ": " + format(d.value);});
node.append("circle")
.attr("r", function(d) {return d.r;})
.attr("class", "nodecircle")
.style("fill", '#ff4719')
.attr("data-classname", function(d) {return d.className;});
node.append("text")
.attr("text-anchor", "middle")
.attr("class", "nodetext")
.attr("data-classname", function(d) {return d.className;})
.attr("style", function(d) {return "font-size:" + d.r/5;})
.attr("data-classname", function(d) {return d.className;})
.each(function(d, i) {
var nm = d.className;
var arr = nm.replace(/[\(\)\\/,-]/g, " ").replace(/\s+/g, " ").split(" "),arrlength = (arr.length > 7) ? 8 : arr.length;
d3.select(this).attr('y',"-" + (arrlength/2) + "em");
//if text is over 7 words then ellipse the 8th
for(var n = 0; n < arrlength; n++) {
var tsp = d3.select(this).append('tspan').attr("x", "0").attr("dy", "1em").attr("data-classname", nm);
if(n === 7) {
tsp.text("...");
} else {
tsp.text(arr[n]);
}
}
});
});
function clickOnCircleFunc(el){
var selection = el.target.__data__.className;
//bubble select
$('.nodecircle').each(function (id, v) {
var $this = $(this),d_nm = $this.attr('data-classname');
if (d_nm === selection && $this.attr('data-selected') !== 'y') {
$this.attr('data-selected','y').css('fill', '#3182bd');
}
else {
$this.attr('data-selected','n').css('fill', '#ff4719');
}
});
}
function redraw() {
var targNN = d3.event.sourceEvent.target.nodeName,deTP = d3.event.sourceEvent.type;
if( (targNN === "circle" || targNN === "text" || targNN === "tspan") && (deTP === "dblclick" || deTP === "touchstart") ){
clickOnCircleFunc(d3.event.sourceEvent);
}else{
vis.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");
}
}
var flat = {"children":[{"className":"Brokerage","value":87232},{"className":"Corporate Solutions","value":17645},{"className":"Capital Markets","value":6385},{"className":"Project & Development Services - Building Surveying","value":6189},{"className":"Markets - Agency Leasing","value":4758},{"className":"Project & Development Services - Project Management - Variable","value":3145},{"className":"Professional & Advisory - Regional Lease Advisory","value":2984},{"className":"Professional & Advisory - Valuation Advisory","value":2949},{"className":"Markets - Property Management","value":2941},{"className":"Advisory - Valuations","value":2409},{"className":"Project & Development Services - Project Management - Development","value":2319},{"className":"Leasing - Retail Agency","value":2187},{"className":"Office Agency - National Office Agency","value":2073},{"className":"Retail","value":2029},{"className":"Professional & Advisory - Valuation","value":1854},{"className":"Corporate Solutions - Integrated Facilities Management","value":1830},{"className":"Professional & Advisory - Building Surveying","value":1807},{"className":"Professional & Advisory - Planning","value":1795},{"className":"Hotels - Hotels","value":1728},{"className":"Markets - Tenant Representation","value":1593},{"className":"Retail - National Retail Agency","value":1505},{"className":"Industrial & Logistics - Industrial & Logistics Agency","value":1393},{"className":"Markets - Corporate","value":1293},{"className":"Corporate Solutions - Transaction Management","value":1276},{"className":"Property & Asset Management - PAM Portfolios London","value":1205},{"className":"Professional & Advisory - Rating","value":1131},{"className":"Leasing - Office Agency","value":1128},{"className":"Capital Markets - Investment Sales","value":1033},{"className":"Canada","value":1005},{"className":"Capital Markets - National Investment","value":940},{"className":"Corporate Solutions - Project & Development Services","value":929},{"className":"Agency- Office","value":827},{"className":"Retail - Leasing","value":795},{"className":"Management Services - Project & Development Services","value":745},{"className":"Professional & Advisory - Lease Advisory","value":706},{"className":"International Desk - Transactions","value":706},{"className":"Corporate Solutions - Tenant Representation","value":698},{"className":"Project & Development Services","value":698},{"className":"Retail - Management","value":680},{"className":"Residential - Affordable Housing","value":671},{"className":"Residential - Residential Sales","value":634},{"className":"Professional & Advisory - Strategic Consulting","value":618},{"className":"Corporate Solutions - Lease Administration","value":614},{"className":"Agency - Retail","value":601},{"className":"Professional & Advisory - Valuations","value":596},{"className":"Management Services - Property Management","value":569},{"className":"Project & Development Services - Project Management","value":557},{"className":"Strategic Consulting - Workplace Strategy","value":540},{"className":"Office Agency - Business Space Agency","value":540},{"className":"Capital Markets - Real Estate Investment Banking (REIB)","value":528}]};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment