Chrome不运行我的D3.js,但它在safari中工作良好

Chrome not running my D3.js, but it is working fine in safari

本文关键字:safari 工作 运行 我的 D3 js Chrome      更新时间:2023-09-26

我对D3.js有点陌生。Chrome没有运行我的D3.js,但它在safari中工作良好。我的代码在这里有什么问题?请协助

这是由于一些权限问题,其中Chrome不允许XMLHttpRequest (d3。

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
  stroke: #fff;
  stroke-width: 1.5px;
}
.link {
  stroke: #999;
  stroke-opacity: .6;
}
</style>
<body>
Show: <select id="graph">
  <option value="init">Before topic identification</option>
  <option value="final">After topic identification</option>
</select>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
    height = 1000;
var color = d3.scale.category20();
var force = d3.layout.force()
    .friction(0.99)
    .charge(-120)
    .linkDistance(100)
    .size([width, height]);
var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);
d3.json("cluster.json", function(error, graph) {
  force
      .nodes(graph.nodes)
      .links(graph.links)
      .start();
  var link = svg.selectAll(".link")
      .data(graph.links)
    .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", 0);
  var gnodes = svg.selectAll("g.gnode")
      .data(graph.nodes)
    .enter()
      .append('g')
      .classed('gnode', true);
  // Add one circle in each group
  var node = gnodes.append("circle")
    .attr("class", "node")
    .attr("r", function(d) { return d.initValue*2; })
    .style("fill", color(0))
    .style("fill-opacity", function(d) { return (d.type=="term") ? 0 : 0.75; })
    .call(force.drag);
  // Append the labels to each group
  var labels = gnodes.append("text")
      .style("opacity", function(d) {return d.initValue*2; })
      .attr("dy", ".3em")
      .style("text-anchor", "middle")
      .style("font-size", function(d) {return 24+"px"; })
      .text(function(d) { return d.name; })
      .on("mouseover", function(d, i) {
        d3.select(this)
        .transition()
        .duration(200)
        .style('font-size', function(d) { return 45 })
        .style("opacity", 1)
        .style('fill', "red");
        d3.select(this).moveToFront();      
        })
    .on("mouseout", function(d, i) {
        d3.select(this)
        .transition()
        .duration(200)
        .style('font-size', function(d) { return 24 })
        .style("opacity", function(d) {return d.initValue*2; })
        .style('fill', "black");
    });
  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; });
    // Translate the groups
    gnodes.attr("transform", function(d) { 
      return 'translate(' + [d.x, d.y] + ')'; 
    });    
  });
  d3.select("#graph").on("change", function() {    
    // Compute index per node.
    var thisValue = this.value;
    node.attr("r", function(d) { return (thisValue=="init") ? d.initValue*2 : ((d.type=="term") ? 50 : d.finalValue*2); });
    node.style("fill", function(d) { return (thisValue=="init") ? color(0) : color(d.group); });
    link.style("stroke-width", function(d) { return (thisValue=="init") ? 0 : 1; });
    force.linkDistance( function(d) { return ((thisValue=="init") ? d.initValue : d.finalValue*20); });
    force.start();
  });
});

</script>

cluster.json

有时你无法在Chrome中加载本地文件d3.json。但是你可以使用jquery。获取,像这样:

$.get( "cluster.json", function( data ) {

//你的代码在这里});

PS:最好使用$。延迟,在你的代码中解决。

但是我猜,你用错了cluster.json。你能提供吗?

您需要在一些本地服务器上运行您的代码。像

  localhost:8080

如果你安装了Wamp或任何php服务器,只需将你的代码复制到www根目录下,然后从phpMyAdmin

运行它

如果你安装了python,打开命令提示符,进入该目录,并为

输入以下命令
   python -m SimpleHTTPServer 8000

,您将能够在该目录下运行本地服务器