0

I have an html and json file running a D3 graphic that runs in firefox but does not run in chrome.

The error in the console is:

d3.v3.min.js:1 Access to XMLHttpRequest at 'D3_least_example.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

Can you help me find a way to make the image render in chrome?

I found this related question but it didn't help.

The .html and .json are a minimal complete example.

Here is the .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>

Here is the .json file named D3_least_example.json in the .html file

[

{"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"]}

]

This is what shows up in firefox and in chrome the screen is blank.

Firefox view

Windows 10 Enterprise, Version 1803, OS build 17134.407

Chrome Version 70.0.3538.110 (Official Build) (64-bit)

adm
  • 354
  • 5
  • 17
  • 2
    Define "doesn't work". – Dave Newton Nov 30 '18 at 21:22
  • 3
    What specifically happens in Firefox that doesn't happen in Chrome? What, if any, errors are displayed in the console when the expected thing doesn't happen? – The Head Rush Nov 30 '18 at 21:23
  • 1
    It looks like the window size is defined, but the graphic does not show up. The graphic does show up in firefox. I'll add a snap shot of firefox. – adm Nov 30 '18 at 21:33
  • 2
    What is your OS and version of Chrome? It displays fine in 70.0.3538.110 on OSX – duhaime Nov 30 '18 at 21:52
  • 1
    I'm in the same chrome version but on Windows. I added exact version to bottom of page. – adm Nov 30 '18 at 22:03
  • 1
    What's on your console? Running your code on Chrome the console shows a "Mixed Content" error because D3 is pulled in over HTTP. Changing that to HTTPS solves the problem for me. https://plnkr.co/edit/d3rlx0j7fTnj7q6mJL5M?p=preview – altocumulus Nov 30 '18 at 22:24
  • @altocumulus My console running it local file:///C:/.... I tried to change the D3 address in the HTML file, but it didn't work for me. `` – adm Nov 30 '18 at 22:35
  • @altocumulus In the chrome console there are two errors. "Access to XMLHttpRequest" at the .json file. Then "Uncaught TypeError: Cannot read property 'forEach' of undefined" and list 9 at under it. – adm Nov 30 '18 at 23:10
  • I just run it on Windows 8.1 Chrome 70.0 using a local server and no problems – rioV8 Nov 30 '18 at 23:56
  • I can also confirm it's working fine on Windows 10 Chrome 70. Do you see it here on codepen? https://codepen.io/asifm_/pen/YRBPbO?editors=1000 – ba_ul Dec 01 '18 at 02:34
  • Interesting, I do see it in codepen running chrome 70 on windows 10. – adm Dec 01 '18 at 15:10

1 Answers1

1

Chrome security does not allow access to local files. (i.e. the .json file)

Opening chrome from terminal to allow access is one solution.

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

edit path for your location of chrome.exe and then try to open the .html local file.

"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?

adm
  • 354
  • 5
  • 17