gpt4 book ai didi

javascript - Angular D3 不显示

转载 作者:行者123 更新时间:2023-11-28 06:34:20 24 4
gpt4 key购买 nike

我正在尝试创建 Angular D3 力定向图,但由于某种原因它没有显示,并且控制台中没有错误。现在它当前加载一个 JSON,但是我也在尝试弄清楚如何加载变量......任何帮助将不胜感激

index.html

<head>
<!--<script src="css/angular.css"></script>-->
</head>
<style>

.node {
stroke: #fff;
stroke-width: 1.5px;
}

.link {
stroke: #999;
stroke-opacity: .6;
}

</style>

<body>
<div ng-controller="MainCtrl">
<d3-bars data="d3Data"></d3-bars>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.12/d3.js"></script>
<script src="js/index.js"></script>
</body>

app.js

  app = angular.module("triForce", [])

app.controller("MainCtrl", function($scope, $http) {
$scope.width = 900;
$scope.height = 900;

var color = d3.scale.category20()

var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([$scope.width, $scope.height]);
$scope.data=[{
"nodes": [
{
"name": "hblodget",
"group": 1,
"size": 1,
"image": null
},
{
"name": "DowntownDonna69",
"group": 1,
"size": 20,
"image": "http://pbs.twimg.com/profile_images/636139174672732160/L5cd008s_normal.jpg"
},
{
"name": "PupsherLive",
"group": 1,
"size": 19,
"image": "http://pbs.twimg.com/profile_images/378800000210840839/93a8ba3852a8e20364957eb8b907b6b3_normal.jpeg"
},
{
"name": "PupsherLive",
"group": 1,
"size": 19,
"image": "http://pbs.twimg.com/profile_images/378800000210840839/93a8ba3852a8e20364957eb8b907b6b3_normal.jpeg"
},
{
"name": "PupsherLive",
"group": 1,
"size": 19,
"image": "http://pbs.twimg.com/profile_images/378800000210840839/93a8ba3852a8e20364957eb8b907b6b3_normal.jpeg"
},
{
"name": "PupsherLive",
"group": 1,
"size": 19,
"image": "http://pbs.twimg.com/profile_images/378800000210840839/93a8ba3852a8e20364957eb8b907b6b3_normal.jpeg"
},
{
"name": "PupsherLive",
"group": 1,
"size": 19,
"image": "http://pbs.twimg.com/profile_images/378800000210840839/93a8ba3852a8e20364957eb8b907b6b3_normal.jpeg"
},
{
"name": "PupsherLive",
"group": 1,
"size": 19,
"image": "http://pbs.twimg.com/profile_images/378800000210840839/93a8ba3852a8e20364957eb8b907b6b3_normal.jpeg"
}
],
"links": [
{
"source": 1,
"target": 0,
"value": 1
},
{
"source": 2,
"target": 0,
"value": 1
},
{
"source": 2,
"target": 0,
"value": 1
},
{
"source": 2,
"target": 0,
"value": 1
},
{
"source": 2,
"target": 0,
"value": 1
},
{
"source": 2,
"target": 0,
"value": 1
},
{
"source": 1,
"target": 0,
"value": 1
}
]
}
];

$scope.nodes = graph.nodes;
$scope.links = graph.links;

for(var i=0; i < $scope.links.length ; i++){
$scope.links[i].strokeWidth = Math.round(Math.sqrt($scope.links[i].value))
}

for(var i=0; i < $scope.nodes.length ; i++){
$scope.nodes[i].color = color($scope.nodes[i].group)
}

force
.nodes($scope.nodes)
.links($scope.links)
.on("tick", function(){$scope.$apply()})
.start();
})

最佳答案

我没有深入研究你的代码,但乍一看,你似乎没有绑定(bind) <d3-bars data="d3Data"></d3-bars>指令到正确的变量。看起来您的 Controller 正在设置属性 $scope.data不是$scope.d3Data

关于javascript - Angular D3 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34422798/

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