【发布时间】:2021-04-08 18:18:58
【问题描述】:
D3 的新手。我从http://jsfiddle.net/7HZcR/3/ 的工作示例开始 并且正在修改我的目的。我得到了在我自己的 html 中工作的确切代码,但是当我试图修改它时,我遇到了错误
d3_test02.html:155 Uncaught ReferenceError: path is not defined
at _.tick (d3_test02.html:155)
at _.t [as tick] (d3.v3.min.js:1)
at Object.l.tick [as c] (d3.v3.min.js:4)
at Rn (d3.v3.min.js:1)
at Tn (d3.v3.min.js:1)
我使用的代码是:
<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Example</title>
<style>
path.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
marker#licensing {
fill: green;
}
path.link.licensing {
stroke: green;
}
path.link.resolved {
stroke-dasharray: 0,2 1;
}
circle {
fill: #ccc;
stroke: #333;
stroke-width: 1.5px;
}
text {
font: 10px sans-serif;
pointer-events: none;
}
text.shadow {
stroke: #fff;
stroke-width: 3px;
stroke-opacity: .8;
}
</style>
<!-- This sets the size of the box you're working with and calls the script source, which is D3
It also calls 2 scripts to help with saving the output -->
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/canvg/1.5/canvg.min.js"></script>
<script src="saveSvgAsPng.js"></script>
<script>
function makeGraph() {
// Length of the running time in milliseconds //
const simulationDurationInMs = 50000;
whichMap = document.getElementById("ddlPrm").value + ".json";
$("#svgGraph").empty(); // Clear out any existing graph
// document.getElementById('svgGraph').style.display = "block";
$("#b1").css('color', 'red')
d3.json(whichMap, function (error, graph) {
if (error) throw error;
links = graph['links']
nodes = graph['nodes']
console.log(nodes)
let startTime = Date.now()
let endTime = startTime + simulationDurationInMs
// Compute the distinct nodes from the links.
links.forEach(function (link) {
link.source = nodes[link.source] || (nodes[link.source] = { name: link.source });
link.target = nodes[link.target] || (nodes[link.target] = { name: link.target });
});
// Can I use the nodes from Python/JSON?
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h);
var w = 600,
h = 600;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([w, h])
.linkDistance(60)
.charge(-300)
.on("tick", tick)
.start();
// Per-type markers, as they don't inherit styles.
svg.append("svg:defs").selectAll("marker")
.data(["suit", "licensing", "resolved"])
.enter().append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto-start-reverse")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
var path = svg.append("svg:g").selectAll("path")
.data(force.links())
.enter().append("svg:path")
.attr("class", function(d) { return "link " + d.type; })
.attr("marker-start", function(d) { return "url(#" + d.type + ")"; })
.attr("marker-end", function(d) { return "url(#" + d.type + ")"; });
var circle = svg.append("svg:g").selectAll("circle")
.data(force.nodes())
.enter().append("svg:circle")
.attr("r", 6)
.call(force.drag);
var text = svg.append("svg:g").selectAll("g")
.data(force.nodes())
.enter().append("svg:g");
// A copy of the text with a thick white stroke for legibility.
text.append("svg:text")
.attr("x", 8)
.attr("y", ".31em")
.attr("class", "shadow")
.text(function(d) { return d.name; });
text.append("svg:text")
.attr("x", 8)
.attr("y", ".31em")
.text(function(d) { return d.name; });
});
} // makeGraph()
// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
path.attr("d", function(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = 75/d.linknum; //linknum is defined above
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
});
circle.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
text.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
function saveGraph4() {
document.title = whichMap = document.getElementById("ddlProcessMap").value;
window.print()
}
</script>
</head>
<html>
<body>
<label for="ddlProcessMap"> Choose a process map:</label>
<select name="process_map" id="ddlPRM">
<option value="prm1">prm1</option>
<option value="prm2">prm2</option>
<option value="prm3">prm3</option>
</select>
<button id="b1" type="button" style="background-color: lightblue;" onclick="makeGraph();">Make Graph</button>
<button id="b2" type="button" onclick="saveGraph4();">Save Image</button>
<div id="svg-container">
<svg id='svgGraph'
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
xml:space="preserve" width="800" height="600">
</svg>
</div>
<canvas id="canvas" width="800" height="400"></canvas>
<div id="png-container"></div>
</body>
我正在查看定义路径数据的代码 - 与我开始的示例完全相同。请注意,我的源数据是在文件中定义的,而不是在行中定义的。我有不同的领域——当我说到点子上时,我希望能调试这些领域。但我无法解决这个路径问题。 我尝试在 d3.json() 中移动刻度函数
我错过了什么?
【问题讨论】:
标签: javascript svg d3.js