我正在尝试运行一个简单的 d3 Javascript 程序来可视化图表。我还有这个图的 JSON 文件。为了让程序运行,我被告知应该遵循以下步骤:
1- 在终端上,我转到项目所在的文件夹。
2-我插入以下命令:python -m SimpleHTTPServer 8888 &
3- 在 Web 浏览器 (Firefox) 上,我添加以下内容:http://localhost:8888
当我执行第三步时,终端显示以下错误消息:
localhost - - [11/Nov/2013 08:07:23] code 404, message File not found
localhost - - [11/Nov/2013 08:07:23] "GET /D3/sample.json HTTP/1.1" 404 -
这是我的 d3 Javascript 图表的 HTML 文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<p> Paragraph !!! </p>
<script type="text/javascript" src="d3.v3.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("sample.json", function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
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; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
</script>
</body>
</html>
似乎无法按照上面显示的消息读取 JSON 文件 sample.json
。任何人都可以帮助我如何运行该程序并使用我上面提供的命令读取 json 文件。如果我在该 HTML 文件中添加标题和段落,它们将会出现,但无法显示图表。 JSON 文件的位置是否有问题,或者 d3.v3.js 文件是否有问题?感谢您提前提供的帮助。
`
最佳答案
据我了解,您已经在一个目录中设置了一个 python 简单服务器,并且在该目录中您有一个 html 文件,该文件显示在浏览器中。但是,当您尝试运行 js 代码并加载 json 文件时,您会收到 404 错误。
错误表明它正在名为 D3 的目录中查找 json 文件,但是,您的代码正在根目录中查找 json。尝试更改
D3.json("sample.json", function(error, graph)
行至
d3.json("D3/sample.json", 函数(错误,图表)
。
此外,在函数调用内部放置console.log(graph)
,如下所示:
d3.json("sample.json", function(error, graph) {
console.log(graph)
这会将输出发送到您的控制台,以便您可以检查正在读取的内容(如果您已经知道这一点,我们深表歉意)。
关于javascript - SimpleHTTPServer 代码 404,消息未找到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19901955/