【问题标题】:Uncaught TypeError: svg.append(...).attr(...).selectAll(...).data(...).enter is not a function未捕获的类型错误:svg.append(...).attr(...).selectAll(...).data(...).enter 不是函数
【发布时间】:2019-02-10 01:54:46
【问题描述】:

我正在尝试使用我拥有的数据集重新创建 Bostock 在此示例 https://bl.ocks.org/mbostock/4062045 中演示的力有向图。

我正在使用 Django 序列化程序解析 JSON 格式的数据,并在我的 D3.js 代码中指定的 URL 上提供数据。我正在使用 D3 v4。

 <style>

    .links line {
        stroke: #999;
        stroke-opacity: 0.6;
    }

    .nodes circle {
        stroke: #fff;
        stroke-width: 1.5px;
    }

</style>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

    var svg = d3.select("svg"),
        width = +svg.attr("width"),
        height = +svg.attr("height");

    var color = d3.scaleOrdinal(d3.schemeCategory20);

    var simulation = d3.forceSimulation()
        .force("link", d3.forceLink().id(function(d) { return d.se_id; }))
        .force("charge", d3.forceManyBody())
        .force("center", d3.forceCenter(width / 2, height / 2));

    d3.json("../forceLink", function(error, graph) {
        if (error) throw error;

        var link = svg.append("g")
            .attr("class", "links")
            .selectAll("line")
            .data(graph.links)
            .enter()
            .append("line")
            .attr("stroke-width", function(d) { return Math.sqrt(d.value); });

        var node = svg.append("g")
            .attr("class", "nodes")
            .selectAll("circle")
            .data(graph.nodes)
            .enter().append("circle")
            .attr("r", 5)
            .attr("fill", function(d) { return color(d.group); })
            .call(d3.drag()
                .on("start", dragstarted)
                .on("drag", dragged)
                .on("end", dragended));

        node.append("title")
            .text(function(d) { return d.se_id; });

        simulation
            .nodes(graph.nodes)
            .on("tick", ticked);

        simulation.force("link")
            .links(graph.links);

        function ticked() {
            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; });
        }
    });

    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;
    }

</script>

看起来数据没有在步骤 .data(graph.links) 上被解析

我的节点 JSON 数据示例, nodes JSON data

我的链接 JSON 数据示例, links JSON data

【问题讨论】:

    标签: javascript django python-3.x d3.js django-serializer


    【解决方案1】:

    在这个问题上花了 4 天后,我终于发现这是一个缺少 4 个“目标”值的数据问题!我删除了那些记录,因为它不是那么重要而且效果很好!

    【讨论】:

    • 由于对我们其他人来说没有什么可学习的,请考虑完全删除该问题。
    • 感谢您的建议,但我无法删除问题。不过我还是会试试的!
    猜你喜欢
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2019-06-06
    • 2019-05-24
    相关资源
    最近更新 更多