I am trying to display a D3 bubble chart for an array which can contain some 0 values. If the number of 0s gets to high bubbles start to "disappear". See this example (it should show 10 bubbles): Example on jsfiddle
Am I doing something wrong?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta name="language" content="de">
<title>Disappearing bubble Problem</title>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript" src="http://d3js.org/d3.v2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
var r = 700
var bubble_layout = d3.layout.pack()
.size([r,r])
.padding(1.5);
var vis = d3.select("#chart").append("svg")
.attr("width" , r)
.attr("height", r)
var arr = [
{ name : '1', value: 1 }
,{ name : '2', value: 1 }
,{ name : '3', value: 1 }
,{ name : '4', value: 1 }
,{ name : '5', value: 1 }
,{ name : '6', value: 1 }
,{ name : '7', value: 1 }
,{ name : '8', value: 1 }
,{ name : '9', value: 1 }
,{ name : '10', value: 1 }
,{ name : '11', value: 0 } //Adding data elements with value 0 makes bubbles disappear
,{ name : '12', value: 0 }
,{ name : '13', value: 0 }
,{ name : '14', value: 0 }
,{ name : '15', value: 0 }
,{ name : '16', value: 0 }
,{ name : '17', value: 0 }
,{ name : '18', value: 0 }
,{ name : '19', value: 0 }
,{ name : '20', value: 0 }
,{ name : '21', value: 0 }
,{ name : '22', value: 0 }
,{ name : '23', value: 0 }
,{ name : '24', value: 0 }
,{ name : '25', value: 0 }
,{ name : '26', value: 0 }
,{ name : '27', value: 0 }
,{ name : '28', value: 0 }
,{ name : '29', value: 0 }
,{ name : '30', value: 0 }
,{ name : '31', value: 0 }
,{ name : '32', value: 0 }
,{ name : '33', value: 0 }
,{ name : '34', value: 0 }
,{ name : '35', value: 0 }
,{ name : '36', value: 0 }
];
var selection = vis.selectAll("g.node")
.data(bubble_layout.nodes({children: arr}).filter(function(d) { return !d.children; }) );
//Enter
node = selection.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + ", " + d.y + ")"; });
node.append("circle")
.attr("r", function(d) { return d.r; })
.style("fill", function(d) { return 'aaaaaa'; });
node.append("text")
.attr("text-anchor", "middle")
.attr("dy", ".3em")
.text(function(d) { return d.name; });
} );
</script>
</head>
<body>
<span id='chart'>
</body>
</html>
Your circles don't disappear, they are rendered but are covered by others.
You can change the sort order of the pack layout to something other than ascending - which is the default. It can be set to null
var bubble_layout = d3.layout.pack()
.sort(null) // <-- HERE
.size([r,r])
.padding(1.5);
Also, you might want to filter your selection to include only items with a value greater than 0, to avoid creating circles with zero radius.
var node = selection.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.x + ", " + d.y + ")";
})
.filter(function(d){
return d.value > 0; // <-- HERE
})
Updated JSFiddle: http://jsfiddle.net/jaimem/WWxqh/4/
To prevent the uniform look you need to change the value
property, since d3.js uses it to do its magic. Check this fiddle: http://jsfiddle.net/jaimem/WWxqh/5/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.