PS: 这篇文章适合有一定D3基础的童鞋,因为没有多余的注释,只是笔者比较业余的代码分享,勿喷
^ HTML
<div class="content-panel">
<div id="object"></div>
<div id="object-arrow"></div>
</div>
^ JS
(angularjs Controller的代码)
var url = $location.path();
$scope.$storage = $localStorage;
var defaultLocation;
if (_.has($localStorage, 'multiObject')) {
defaultLocation = $localStorage.multiObject;
}
var nodeNameMap = {};
var newNodes = _.map(abjectData.nodes, function(node) {
nodeNameMap[node.objectId] = node.name;
var rNode = {};
if (_.has(defaultLocation, node.objectId)) {
rNode.name = node.name;
rNode.objectId = node.objectId;
rNode.instanceCount = + node.instanceCount;
rNode.attrs = node.attrs;
rNode.fixed = true;
rNode.x = defaultLocation[node.objectId].x;
rNode.y = defaultLocation[node.objectId].y;
} else {
rNode = node;
}
return rNode;
});
var width = window.innerWidth - 250,
height = window.innerHeight - 200,
boxWidth = 160,
countR = 15;
var objectContainer = d3.select('#object').append('div')
.style('position', 'absolute')
.attr('width', width)
.attr('height', height);
var force = d3.layout.force();
force
.nodes(newNodes)
.links(abjectData.links)
.size([width, height])
.linkDistance(200)
.charge(-2000)
.chargeDistance(250)
.start();
var svg = d3.select('#object-arrow').append('svg')
.attr('width', width)
.attr('height', height);
svg.append('defs').append('marker')
.attr('id', 'end-arrow')
.attr('refX', 4)
.attr('refY', 4)
.attr('viewBox', '0 0 8 8')
.at