【问题标题】:path not defined in d3 exampled3 示例中未定义路径
【发布时间】: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


    【解决方案1】:

    您的 path 变量似乎没有在全局范围内声明,而是在 makeGraph() 中声明。因为它是在makeGraph() 的“范围”中声明的,所以tick() 无权访问它——因此您会收到错误path is not defined。 (在 JSFiddle 中,路径是全局声明的。)因此,您可以在函数之外声明它们以启动(只需将 var path; 粘贴在 JS 文件的顶部,然后在 @987654327 中将其设置为正确的值@function),虽然可能有更好的解决方案,但我不知道特定于 D3。

    看起来一旦你解决了这个问题,你可能还会得到 circle is not definedtext is not defined,因为它们也在 makeGraph 中声明,但是你尝试在 tick() 中使用它们——所以你会如果我没看错你的代码,也必须全局声明它们。

    【讨论】:

    • TX。我明白你的意思,但我不知道如何解决它。我试着把 var path;和 var 路径{};就在
    • 是的,您不必将整个定义移出,只需将var path; 放在脚本标记的顶部即可。你的意思是,这是一条不同的路径? (如果您在问题中为您的代码链接示例 JSON 文件,我还可以帮助您更好地调试。)
    • 对不起。我的意思是将此标记为正确的解决方案,但我已经转移到下一个问题,令人惊讶的是我理解,即使我不立即确定如何解决它。
    猜你喜欢
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2021-01-10
    • 2022-01-19
    • 2016-08-22
    相关资源
    最近更新 更多