我正在尝试在简单的网页上呈现d3.js图表。图表非常简单。我从cloud9运行它,这通常是一个非常好的错误免费的基于云的IDE。但是,当我运行它时,页面是空白的。我可以得到简单的javascript函数,但是(警告和“你好世界”等),但不是d3.js.我不知道为什么。所以我在cloud9中运行html,然后从浏览器中“查看源代码”并将代码逐字复制粘贴到我的Mac笔记本电脑上的本地html页面并运行它,它工作正常。所以基本上我不明白为什么它在cloud9上不起作用。这是代码..
<!DOCTYPE html>
<html>
<head>
<title>TBB</title>
<link data-turbolinks-track="true" href="/assets/style.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/example.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/user.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/application.js?body=1"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="5jRBVsv8vrjFDZO2I0dCoMt95++mzwENzkS+eP9ijAU=" name="csrf-token" />
<style>
.node {
fill: #ccc;
stroke: #fff;
stroke-width: 2px;
}
.link {
stroke: #777;
stroke-width: 2px;
}
</style>
</head>
<body>
<script src='http://d3js.org/d3.v3.min.js'></script>
<script>
var width = 640,
height = 480;
var nodes = [
{ x: width/3, y: height/2 },
{ x: 2*width/3, y: height/2 }
];
var links = [
{ source: 0, target: 1 }
];
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
var force = d3.layout.force()
.size([width, height])
.nodes(nodes)
.links(links);
force.linkDistance(width/2);
var link = svg.selectAll('.link')
.data(links)
.enter().append('line')
.attr('class', 'link');
var node = svg.selectAll('.node')
.data(nodes)
.enter().append('circle')
.attr('class', 'node');
force.on('end', function() {
node.attr('r', width/25)
.attr('cx', function(d) { return d.x; })
.attr('cy', function(d) { return d.y; });
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; });
});
force.start();
</script>
</body>
</html>
答案 0 :(得分:3)
我刚刚在云端9中尝试过。当我运行html文件时,它给了我一个https
链接。 Chrome正确地抱怨在安全页面上加载非安全资源:
Mixed Content: The page at 'https://demo-project-larsenmtl.c9.io/html/index.html' was loaded over HTTPS, but requested an insecure script 'http://d3js.org/d3.v3.min.js'. This request has been blocked; the content must be served over HTTPS.
index.html:38 Uncaught ReferenceError: d3 is not defined
尝试使用http
链接或从支持https
的cdn加载d3,例如cloudflare:
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js
答案 1 :(得分:1)
您有以下两次:
</head>
<body>
因此HTML无效。