d3.js图表对象错误:“未捕获的SyntaxError:意外的标识符”; [英] d3.js chart object error: "Uncaught SyntaxError: Unexpected identifier"
问题描述
我正在用 D3.JS
制作力向图。示例代码摘自此处。在运行实时代码(此处)时,我可以解析我的个人 .JSON
文件,并能够制作图形。
脱机复制以下代码块后,在控制台上遇到错误,因为
未捕获的SyntaxError:意外的标识符在常量链接= data.links.map(d => Object.create(d));
处,在以下代码中为第5行。我正在使用 Chrome
I am making a force-directed graph with D3.JS
. The sample code is taken from here. While running the live code (here) I am able to parse my personal .JSON
files and is able to make the graph.
After I copied the following chunk of code offline, I encounter an error on the console as
"Uncaught SyntaxError: Unexpected identifier" at const links = data.links.map(d => Object.create(d));
which is 5th line in the following code. I am using Chrome
data = FileAttachment("ppi.json").json()
height = 600
height = 600
chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 5)
.attr("fill", color)
.call(drag(simulation));
node.append("title")
.text(d => d.id);
simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});
invalidation.then(() => simulation.stop());
return svg.node();
}
// data = FileAttachment("ppi.json").json()
// height = 600
// height = 600
color = ƒ(d)
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.group);
}
drag = ƒ(simulation)
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
d3 = require("d3@5")
推荐答案
可观察笔记本使用的语言不是Java语言。 / a>。因此,使用Observable中的代码时需要小心。
The language used by the Observable notebooks is not Javascript.. As such, you need to be careful when using code from Observable.
尤其是:
- 请勿复制粘贴单元格的值,只粘贴它们的代码。
color = ƒ(d) // don't copy this!
// copy this
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.group);
}
- 带有花括号的声明仅存在于Observable中。您可以使用IIFE 或重构代码。
- 最后,要导入文件,请使用 d3.fetch 而不是仅可观察的
FileAttachment
。 - Declarations with curly braces only exist in Observable. You can either use IIFEs or refactor the code.
- Finally, to import files, use d3.fetch instead of the Observable-only
FileAttachment
.
这篇关于d3.js图表对象错误:“未捕获的SyntaxError:意外的标识符”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!