搜索强制布局d3.js中的节点时出现Jquery错误

时间:2015-06-11 19:26:03

标签: jquery d3.js force-layout

我正在处理来自this example fiddle.

的强制布局d3.js中的搜索节点
<script type='text/javascript' src="http://code.jquery.com/ui/1.8.3/jquery-ui.min.js"></script>
<script type='text/javascript' src="http://code.jquery.com/ui/1.8.3/themes/smoothness/jquery-ui.css"></script>
<script src="http://d3js.org/d3.v3.js"></script>
<style>
.node {
    stroke: #fff;
    stroke-width: 1.5px;
}
.link {
    stroke: #999;
    stroke-opacity: .6;
}
</style>

</head>
<body>
<div class="ui-widget">
    <input id="search">
    <button type="button" onclick="searchNode()">Search</button>
</div>


//Constants for the SVG
var width = 500,
    height = 500;

//Set up the colour scale
var color = d3.scale.category20();

//Set up the force layout
var force = d3.layout.force()
    .charge(-120)
    .linkDistance(30)
    .size([width, height]);

//Append a SVG to the body of the html page. Assign this SVG as an object to svg
var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

//Read the data from the mis element 
var mis = document.getElementById('mis').innerHTML;
graph = JSON.parse(mis);

//Creates the graph data structure out of the json data
force.nodes(graph.nodes)
    .links(graph.links)
    .start();

//Create all the line svgs but without locations yet
var link = svg.selectAll(".link")
    .data(graph.links)
    .enter().append("line")
    .attr("class", "link")
    .style("stroke-width", function (d) {
    return Math.sqrt(d.value);
});

//Do the same with the circles for the nodes - no 
var node = svg.selectAll(".node")
    .data(graph.nodes)
    .enter().append("circle")
    .attr("class", "node")
    .attr("r", 8)
    .style("fill", function (d) {
    return color(d.group);
})
    .call(force.drag);


//Now we are giving the SVGs co-ordinates - the force layout is generating the co-ordinates which this code is using to update the attributes of the SVG elements
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;
    });
});



var optArray = [];
for (var i = 0; i < graph.nodes.length - 1; i++) {
    optArray.push(graph.nodes[i].name);
}

optArray = optArray.sort();

$(function () {
    $("#search").autocomplete({
        source: optArray
    });
});


function searchNode() {

    //find the node

    var selectedVal = document.getElementById('search').value;
    var node = svg.selectAll(".node");

    if (selectedVal == "none") {
        node.style("stroke", "white").style("stroke-width", "1");
    } else {
        var selected = node.filter(function (d, i) {
            return d.name != selectedVal;
        });
        selected.style("opacity", "0");
        var link = svg.selectAll(".link")
        link.style("opacity", "0");
        d3.selectAll(".node, .link").transition()
            .duration(5000)
            .style("opacity", 1);


    }
}



  </script>
</body>
</html>e

我收到错误:

  

未捕获的ReferenceError:未定义jQuery(匿名函数)@   jquery-ui.min.js:8 jquery-ui.css:13未捕获的SyntaxError:意外   令牌。 searchingforcelayout.html:1362未捕获的ReferenceError:$ is   未定义

我已经包含了

<script type='text/javascript' src="http://code.jquery.com/ui/1.8.3/jquery-ui.min.js"></script>
<script type='text/javascript' src="http://code.jquery.com/ui/1.8.3/themes/smoothness/jquery-ui.css"></script>

它适用于小提琴,但不适用于我的系统。我使用Chrome作为浏览器。

1 个答案:

答案 0 :(得分:0)

你是否包含jQuery?在包含jQuery UI JS库之前确保包含它