<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>d3+jQuery横向树图</title>
<link rel="stylesheet" type="text/css" href="css/gqct.css">
<script src="js/d3.min.js"></script>
<script src="js/jquery.min.js"></script>
</head>
<body>
<div class="container" id="treecontainer">
<div class="menu">
<div id="product_tree"></div>
</div>
</div>
<script type="text/javascript">
var container;
var zoom;
var rootData;
var depthInfo;
var rootName;//根节点名称
jQuery(document).ready(function () {
/* resizeScreen();*/
//selectChange();
var child=document.getElementById("product_tree");
child.innerHTML = '';
getData();
});
var getData =function() {
// 测试内容
rootData = {
"downward": {
"direction": "downward",
"name": "origin",
"children": [
{
"name": "某某某莫某有限公司",
"amount": "100",
"ratio": "55%",
"hasHumanholding":true,
"hasChildren":true,
"isExpand": false,
"children": [
{
"name": "公司名字",
"hasHumanholding":false,
"hasChildren":true,
"amount": "100",
"ratio": "55%",
"children": []
},
{
"name": "公司名字",
"hasHumanholding":false,
"hasChildren":true,
"amount": "100",
"ratio": "55%",
"children": []
}
]
},
{
"name": "某某某莫某有限公司",
"amount": "100",
"ratio": "55%",
"hasHumanholding":true,
"hasChildren":true,
"isExpand": false,
"children": [
{
"name": "公司名字",
"hasHumanholding":false,
"hasChildren":true,
"amount": "100",
"ratio": "55%",
"children": []
},
{
"name": "公司名字",
"hasHumanholding":false,
"hasChildren":true,
"amount": "100",
"ratio": "55%",
"children": []
}
]
},
{
"name": "某某某莫某有限公司",
"amount": "100",
"ratio": "55%",
"hasHumanholding":true,
"hasChildren":true,
"isExpand": false,
"children": [
{
"name": "公司名字",
"hasHumanholding":false,
"hasChildren":true,
"amount": "100",
"ratio": "55%",
"children": []
},
{
"name": "公司名字",
"hasHumanholding":false,
"hasChildren":true,
"amount": "100",
"ratio": "55%",
"children": []
}
]
},
{
"name": "某某某莫某有限公司",
"hasHumanholding":false,
"hasChildren":true,
"amount": "100",
"ratio": "55%",
"children": []
},
{
"name": "某某某莫某有限公司",
"hasHumanholding":false,
"hasChildren":true,
"isExpand": false,
"amount": "100",
"ratio": "55%",
"children": [
{
"name": "公司或股东名字",
"hasHumanholding":false,
"amount": "100",
"ratio": "55%",
"children": []
},
{
"name": "公司或股东名字",
"hasHumanholding":false,
"amount": "100",
"ratio": "55%",
"children": []
},
{
"name": "公司或股东名字",
"hasHumanholding":false,
"amount": "100",
"ratio": "55%",
"children": []
},
{
"name": "公司或股东名字",
"hasHumanholding":false,
"amount": "100",
"ratio": "55%",
"children": []
}
]
}
]
}
}
rootName = '某某某莫某有限公司';
drawing();
};
var drawing = function() {
var _this = this;
// var rootName = ''; //根节点的名字
var rootRectWidth = 0; //根节点rect的宽度
var forUpward = true;
var treeChart = function(d3Object) {
this.d3 = d3Object;
this.directions = ['downward'];
};
treeChart.prototype.drawChart = function() {
// First get tree data for both directions.
this.treeData = {};
var self = this;
self.directions.forEach(function(direction) {
self.treeData[direction] = _this.rootData[direction];
});
// rootName = '北京伴学科技有限公司';
rootRectWidth = _this.rootName.length * 15;
self.graphTree(self.getTreeConfig());
};
treeChart.prototype.getTreeConfig = function() {
var treeConfig = {
'margin': {
'top': 10,
'right': 5,
'bottom': 0,
'left': 5
}
}
treeConfig.chartWidth = (1600 - treeConfig.margin.right - treeConfig.margin.left);
treeConfig.chartHeight = (800 - treeConfig.margin.top - treeConfig.margin.bottom);
treeConfig.centralHeight = treeConfig.chartHeight / 2;
treeConfig.centralWidth = treeConfig.chartWidth / 6;
treeConfig.linkLength = 300;
treeConfig.duration = 500; //动画时间
return treeConfig;
};
treeChart.prototype.graphTree = function(config) {
var self = this;
var d3 = this.d3;
var linkLength = config.linkLength;
var duration = config.duration;
var hasChildNodeArr = [];
var id = 0;
var diagonal = d3.svg.diagonal()
.projection(function(d) {
return [d.y, d.x];
});
var pathFunc;
if (false) {
pathFunc = diagonal;
} else {
pathFunc = funLine;
}
var zoom = d3.behavior.zoom()
.scaleExtent([0.5, 2])
.on('zoom', redraw);
var svg = d3.select('#product_tree')
.append('svg')
.attr('width', config.chartWidth + config.margin.right + config.margin.left)
.attr('height', config.chartHeight + config.margin.top + config.margin.bottom)
.attr('xmlns','http://www.w3.org/2000/svg')
.on('mousedown', disableRightCli