D3, Bubble Sample

15 篇文章 0 订阅
9 篇文章 0 订阅
<!DOCTYPE html>

<html>
<head>
<script type="text/javascript" src="d3js/d3.v2.min.js"></script>
<script type="text/javascript">

var svggraph;
var divgraph;
var svgwidth = 960;
var svgheight = 960;

function init()
{
	svggraph= d3.select("#graphdiv")
	 .append("svg")
	 .attr("width", svgwidth)
	 .attr("height", svgheight);
	
	divgraph= d3.select("#graphdiv");
}

var jsonendpoint = "/tryrest/contact/list/json";

function xmlhttpPost() {
	
	var xhr;
	var method = "GET";
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xhr = new XMLHttpRequest();
	  	if ("withCredentials" in xhr)
	  	{ 	    
        	xhr.open(method,jsonendpoint,true);

	    } 
	    else if (typeof XDomainRequest != "undefined")
	    {
	        //xhr = new XDomainRequest();
	        xhr=new ActiveXObject("Microsoft.XMLHTTP");
	        xhr.open(method,jsonendpoint);
	    } else {
	        xhr = null;
	    }
	}
	else
	{// code for IE6, IE5
	  	xhr=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xhr.onreadystatechange=function()
	{
	    if (xhr.readyState == 4)
	    {
	    	document.getElementById("maindiv").innerHTML = xhr.responseText;
	    }
	};
	xhr.setRequestHeader("Content-type", "application/json");
	xhr.send(/*"condition="+document.getElementById("sql_condition").value*/);	
}

function show()
{
	document.getElementById("maindiv").innerHTML = "lala";
}

var r = 960, format = d3.format(",d"), fill = d3.scale.category20c();
	
function drawMyGraph()
{
 	var bubble = d3.layout.pack()
			      .sort(null)
			      .size([r, r]);

	var svggraph_b = svggraph.attr("class", "bubble");
	
	d3.json(jsonendpoint, function(json)
	{	   
		 var node = svggraph_b.selectAll("g.node")
		        .data(bubble.nodes(classes(json))
		        		.filter(function(d) { return !d.contactInfoHana; }))
		        .enter().append("g")
		        .attr("class", "node")
		        .attr("transform", function(d) { return "translate(" + d.gx + "," + d.gy + ")"; });
		
		 node.append("circle")
		        .attr("r", function(d){return d.age;})
		        .style("fill", function(d) {return d.gcol;})
		        .style("fill-opacity",".3");
		 
		 node.append("text")
		        .attr("text-anchor", "middle")
		        .style("fill","white")
		        .text(function(d) { return d.name; });
		 
	});
	
}

 // Returns a flattened hierarchy containing all leaf nodes under the root.
 function classes(root) {
   var classes = [];
 
   function recurse(name, node) {
     if (node.contactInfoHana) 
   	 { 
    	 node.contactInfoHana.forEach(function(child) { recurse(node.name, child); });
   	 }
     else 
     {
    	 var rm1=Math.floor(Math.random()*r);
         var rm2=Math.floor(Math.random()*r);
         
         var rcol;
         
         if(node.gender == "Male")
         	 rcol = "blue";
         else
        	 rcol = "red";
         
    	 classes.push({gx: rm1, gy: rm2, age: node.age, name: node.name, gcol: rcol});
    	
    	 console.log(node.name + "," + node.age + "," + rm1 + "," + rcol);
     }
    	      
   }
 
   recurse(null, root);
   return {children: classes};
 }
</script>
	
</head>
<body οnlοad="javascript:init()">

	<input size="100" id="sql_condition" name="sql_condition" />
	<button οnclick='javascript:xmlhttpPost()'>Execute Query</button>
	<button οnclick='javascript:drawMyGraph()'>Execute Graph</button>
	
	<div id="graphdiv"></div>
	<div id="maindiv"></div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值