html d3.js,图片在Firefox中呈现,但不在chrome中呈现

时间:2018-11-30 20:58:09

标签: javascript html json google-chrome d3.js

我有一个运行D3图形的html和json文件,该图形在firefox中运行,但不在chrome中运行。

控制台中的错误是:

  

d3.v3.min.js:1从源头访问'D3_least_example.json'处的XMLHttpRequest   “空”已被CORS政策阻止:仅跨源请求   支持协议方案:http,数据,chrome,chrome扩展名,   https。

您能帮我找到一种使图像呈现为chrome的方法吗?

我发现这与question相关,但没有帮助。

.html和.json是一个最小的完整示例。

这是.html

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.node {
  font: 300 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
  fill: #bbb;
}

.node:hover {
  fill: #000;
}

.link {
  stroke: steelblue;
  stroke-opacity: .4;
  fill: none;
  pointer-events: none;
}

.node:hover,
.node--source,
.node--target {
  font-weight: 700;
}

.node--source {
  fill: #2ca02c;
}

.node--target {
  fill: #d62728;
}

.link--source,
.link--target {
  stroke-opacity: 1;
  stroke-width: 2px;
}

.link--source {
  stroke: #d62728;
}

.link--target {
  stroke: #2ca02c;
}

</style>
<body>
</br ></br >
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var diameter = 960,
    radius = diameter / 2,
    innerRadius = radius - 200;

var cluster = d3.layout.cluster()
    .size([360, innerRadius])
    .sort(null)
    .value(function(d) { return d.size; });

var bundle = d3.layout.bundle();

var line = d3.svg.line.radial()
    .interpolate("bundle")
    .tension(.85)
    .radius(function(d) { return d.y; })
    .angle(function(d) { return d.x / 180 * Math.PI; });

var svg = d3.select("body").append("svg")
    .attr("width", diameter)
    .attr("height", diameter)
  .append("g")
    .attr("transform", "translate(" + radius + "," + radius + ")");

var link = svg.append("g").selectAll(".link"),
    node = svg.append("g").selectAll(".node");

d3.json("D3_least_example.json", function(error, classes) {
  var nodes = cluster.nodes(packageHierarchy(classes)),
      links = packageImports(nodes);

  link = link
      .data(bundle(links))
    .enter().append("path")
      .each(function(d) { d.source = d[0], d.target = d[d.length - 1]; })
      .attr("class", "link")
      .attr("d", line);

  node = node
      .data(nodes.filter(function(n) { return !n.children; }))
    .enter().append("text")
      .attr("class", "node")
      .attr("dy", ".31em")
      .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + (d.y + 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
      .style("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
      .text(function(d) { return d.key; })
      .on("mouseover", mouseovered)
      .on("mouseout", mouseouted);
});

function mouseovered(d) {
  node
      .each(function(n) { n.target = n.source = false; });

  link
      .classed("link--target", function(l) { if (l.target === d) return l.source.source = true; })
      .classed("link--source", function(l) { if (l.source === d) return l.target.target = true; })
    .filter(function(l) { return l.target === d || l.source === d; })
      .each(function() { this.parentNode.appendChild(this); });

  node
      .classed("node--target", function(n) { return n.target; })
      .classed("node--source", function(n) { return n.source; });
}

function mouseouted(d) {
  link
      .classed("link--target", false)
      .classed("link--source", false);

  node
      .classed("node--target", false)
      .classed("node--source", false);
}

d3.select(self.frameElement).style("height", diameter + "px");

// Lazily construct the package hierarchy from class names.
function packageHierarchy(classes) {
  var map = {};

  function find(name, data) {
    var node = map[name], i;
    if (!node) {
      node = map[name] = data || {name: name, children: []};
      if (name.length) {
        node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
        node.parent.children.push(node);
        node.key = name.substring(i + 1);
      }
    }
    return node;
  }

  classes.forEach(function(d) {
    find(d.name, d);
  });

  return map[""];
}

// Return a list of imports for the given array of nodes.
function packageImports(nodes) {
  var map = {},
      imports = [];

  // Compute a map from name to node.
  nodes.forEach(function(d) {
    map[d.name] = d;
  });

  // For each import, construct a link from the source to target node.
  nodes.forEach(function(d) {
    if (d.imports) d.imports.forEach(function(i) {
      imports.push({source: map[d.name], target: map[i]});
    });
  });

  return imports;
}

</script>

这是.html文件中名为D3_least_example.json的.json文件

[

{"name":"flare.Name.3"},
{"name":"flare.Name.4"},
{"name":"flare.Name.6"},
{"name":"flare.Name.7"},
{"name":"flare.Name.8"},
{"name":"flare.Name.1"},
{"name":"flare.Name.3"},
{"name":"flare.Name.7"},
{"name":"flare.Name.8"},
{"name":"flare.Name.9"},
{"name":"flare.Name.3"},
{"name":"flare.Name.5"},
{"name":"flare.Name.4"},
{"name":"flare.Name.1"},
{"name":"flare.Name.3"},
{"name":"flare.Name.8"},
{"name":"flare.Name.9"},
{"name":"flare.Name.0"},
{"name":"flare.Name.6"},
{"name":"flare.Name.4"},
{"name":"flare.Name.3"},
{"name":"flare.Name.4"},
{"name":"flare.Name.5"},
{"name":"flare.Name.7"},
{"name":"flare.Name.6"},
{"name":"flare.Name.2"},
{"name":"flare.Name.4"},
{"name":"flare.Name.6"},
{"name":"flare.Name.8"},
{"name":"flare.Name.4"},
{"name":"flare.Name.5"},
{"name":"flare.Name.3"},
{"name":"flare.Name.2"},
{"name":"flare.Name.1"},
{"name":"flare.Name.4"},
{"name":"flare.Name.6"},
{"name":"flare.Name.9"},
{"name":"flare.Name.0"},
{"name":"flare.Name.6"},
{"name":"flare.Name.5"},
{"name":"flare.Name.4"},
{"name":"flare.Name.3"},




{"name":"flare.Library","imports":["flare.Name.3",
"flare.Name.4",
"flare.Name.6",
"flare.Name.7",
"flare.Name.8",
"flare.Name.1",
"flare.Name.3",
"flare.Name.7"]},
{"name":"flare.Home","imports":["flare.Name.8",
"flare.Name.9",
"flare.Name.3",
"flare.Name.5",
"flare.Name.4"]},
{"name":"flare.Courthouse","imports":["flare.Name.1",
"flare.Name.3",
"flare.Name.8",
"flare.Name.9",
"flare.Name.0"]},
{"name":"flare.Pub","imports":["flare.Name.6",
"flare.Name.4",
"flare.Name.3",
"flare.Name.4",
"flare.Name.5",
"flare.Name.7"]},
{"name":"flare.University","imports":["flare.Name.6",
"flare.Name.2",
"flare.Name.4",
"flare.Name.6"]},
{"name":"flare.High School","imports":["flare.Name.8",
"flare.Name.4",
"flare.Name.5",
"flare.Name.3",
"flare.Name.2",
"flare.Name.1"]},
{"name":"flare.Grocery Store","imports":["flare.Name.4",
"flare.Name.6",
"flare.Name.9",
"flare.Name.0",
"flare.Name.6",
"flare.Name.5",
"flare.Name.4",
"flare.Name.3"]}

]

这是在Firefox和chrome中显示的屏幕空白。

Firefox view

Windows 10企业版1803,操作系统内部版本17134.407

Chrome版本70.0.3538.110(正式版本)(64位)

1 个答案:

答案 0 :(得分:0)

Chrome安全性不允许访问本地文件。 (即.json文件)

从终端打开Chrome以允许访问是一种解决方案。

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files

编辑chrome.exe所在位置的路径,然后尝试打开.html本地文件。

"Cross origin requests are only supported for HTTP." error when loading a local file

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https

http://www.chrome-allow-file-access-from-file.com/windows.html

How do I use spaces in the Command Prompt?