gpt4 book ai didi

javascript - 强制有向图和本地存储

转载 作者:行者123 更新时间:2023-11-28 06:38:57 26 4
gpt4 key购买 nike

我试图在本地存储中拖放后存储节点位置,但是当我重新加载页面时,链接是我保存它们的位置,节点也是如此,但链接和节点没有链接,因此节点只是从他们的初始位置。

这是我的代码,我使用的是 Angular。

angular.module('core').controller('HomeController', ['$scope', 
function($scope) {
$scope.graph = {
width : 500,
height : 400,
color : d3.scale.category20(),
force : '',
drag : '',
dragstart : function(d) {
d.x = d3.event.x;
d.y = d3.event.y;
},
dragend : function(d) {
var graphTmp = { "nodes" : $scope.graph.node.data(), "links" : $scope.graph.link.data()};
localStorage.setItem('graph',JSON.stringify(graphTmp));
},
link : [],
node : [],
links : [],
nodes : []
};
$scope.svg = d3.select("body").append("svg")
.attr("width", $scope.graph.width)
.attr("height", $scope.graph.height);
$scope.savedGraph = {};

$scope.draw = function(){
$scope.graph.force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([$scope.graph.width, $scope.graph.height]);

$scope.graph.force
.nodes($scope.graph.nodes)
.links($scope.graph.links)
.start();

$scope.graph.link = $scope.svg.selectAll(".link")
.data($scope.graph.links)
.enter().append("line")
.attr("class", "link")
.style({'stroke' : 'gray', 'stroke-width' : '1px'});

$scope.graph.drag = $scope.graph.force.drag()
.on("dragstart", $scope.graph.dragstart)
.on("dragend", $scope.graph.dragend);

$scope.graph.node = $scope.svg.selectAll(".node")
.data($scope.graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return $scope.graph.color(d.group); })
.call($scope.graph.drag);

$scope.graph.node
.append("title")
.text(function(d) { return d.name; });

$scope.graph.force.on("tick", function() {
$scope.graph.link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });

$scope.graph.node
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
};

if(localStorage.getItem('graph') === null){

$scope.graph.nodes = [
{"name":"Myriel","group":1},
{"name":"Napoleon","group":1},
{"name":"Mlle.Baptistine","group":1},
{"name":"Mme.Magloire","group":1},
{"name":"CountessdeLo","group":1},
{"name":"Geborand","group":1},
{"name":"Champtercier","group":1},
{"name":"Cravatte","group":1},
{"name":"Count","group":1}
];

$scope.graph.links = [
{"source":1,"target":0,"value":1},
{"source":2,"target":0,"value":8},
{"source":3,"target":0,"value":10},
{"source":3,"target":2,"value":6},
{"source":4,"target":0,"value":1}
];

$scope.draw();
}
else {
var graphTmp = $.parseJSON(localStorage.getItem('graph'));
$scope.graph.links = graphTmp.links;
$scope.graph.nodes = graphTmp.nodes;
$scope.draw();

}

有人知道为什么吗?我认为这是因为节点和链接无法仅使用 node.data() 和 link.data() 数据链接在一起。我可以存储更多数据吗?

谢谢!

最佳答案

问题是,最初当您加载链接时(本地存储中没有数据),它具有以下结构:

 $scope.graph.links = [{
"source": 1,
"target": 0,
"value": 1
}, {
"source": 2,
"target": 0,
"value": 8
}, {
"source": 3,
"target": 0,
"value": 10
}, {
"source": 3,
"target": 2,
"value": 6
}, {
"source": 4,
"target": 0,
"value": 1
}];

现在强制布局将用节点中的源对象替换源索引。

拖动时,您会保存节点和链接(链接对象将保存源和目标的节点对象)。

现在,当您从本地存储加载并解析链接时,链接源/目标将具有与节点对象不同的对象(因为它是深度克隆的)。

因此,节点上的拖动更改将不会反射(reflect)在链接源/目标对象上。因此链接会分离。

问题的解决方案是始终将链接的索引形式存储在本地存储中。我在这里通过保留另一个对象 initLinks 来做到这一点

$scope.graph.nodes = [{
"name": "Myriel",
"group": 1
}, {
"name": "Napoleon",
"group": 1
}, {
"name": "Mlle.Baptistine",
"group": 1
}, {
"name": "Mme.Magloire",
"group": 1
}, {
"name": "CountessdeLo",
"group": 1
}, {
"name": "Geborand",
"group": 1
}, {
"name": "Champtercier",
"group": 1
}, {
"name": "Cravatte",
"group": 1
}, {
"name": "Count",
"group": 1
}];

$scope.graph.links = [{
"source": 1,
"target": 0,
"value": 1
}, {
"source": 2,
"target": 0,
"value": 8
}, {
"source": 3,
"target": 0,
"value": 10
}, {
"source": 3,
"target": 2,
"value": 6
}, {
"source": 4,
"target": 0,
"value": 1
}];
$scope.graph.initLinks = angular.copy($scope.graph.links);

保存到本地存储时我这样做

    var graphTmp = {
"nodes": $scope.graph.nodes,
"links": $scope.graph.initLinks
};

localStorage.setItem('graph', JSON.stringify(graphTmp));

从本地存储加载时我这样做

var graphTmp = JSON.parse(localStorage.getItem('graph'));
$scope.graph.links = graphTmp.links;
$scope.graph.initLinks = angular.copy(graphTmp.links);
$scope.graph.nodes = graphTmp.nodes;
$scope.draw();

工作代码here

希望这有帮助!

关于javascript - 强制有向图和本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34019473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com