0

can any body help with debugging my script!! I can't load the external data from the json file.. it works with a variable in the code but when I separate them in a file.. it's not!!

<!DOCTYPE html>
<html>
<head>
 <meta charset='utf-8'>
    <title>tree 1</title>
     <style>

  .node {
    cursor: pointer;
  }

  .node circle {
    fill: #fff;
    stroke: steelblue;
    stroke-width: 1.5px;
  }

  .node text {
    font: 10px sans-serif;
  }

  .link {
    fill: none;
    stroke: #ccc;
    stroke-width: 1.5px;
  }
    </style>
</head>


<body>
 <script src='http://d3js.org/d3.v3.min.js'></script>
 <script type="text/javascript">
// ************** Generate the tree diagram  *****************
  var diameter = 1000;

  var margin = {
      top: 20,
      right: 120,
      bottom: 20,
      left: 120
    },
    width = diameter,
    height = diameter;

  var i = 0,
    duration = 350,
    root;

  var tree = d3.layout.tree()
    .size([360, diameter / 2 - 80])
    .separation(function(a, b) {
      return (a.parent == b.parent ? 1 : 10) / a.depth;
    });

  var diagonal = self.diagonal = d3.svg.line().interpolate('step')
    .x(function(d) {
      return d.x;
    })
    .y(function(d) {
      return d.y;
    });

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

  // load the external data
  d3.json("dataFile.json", function(error, dataFile) {
   root = dataFile;
   root.x0 = height / 2;
   root.y0 = 0;
   update(root);
  });

//root = pubs;
//root.x0 = height / 2;
//root.y0 = 0;

//root.children.forEach(collapse); // start with all children collapsed
//update(root);

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

function update(source) {

  // Compute the new tree layout.
  var nodes = tree.nodes(root),
    links = tree.links(nodes);

  // Normalize for fixed-depth.
  nodes.forEach(function(d) {
    d.y = d.depth * 80;
  });

  // Update the nodes…
  var node = svg.selectAll("g.node")
    .data(nodes, function(d) {
      return d.id || (d.id = ++i);
    });

  // Enter any new nodes at the parent's previous position.
  var nodeEnter = node.enter().append("g")
    .attr("class", "node")
    //.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
    .on("click", click);

  nodeEnter.append("circle")
    .attr("r", 1e-6)
    .style("fill", function(d) {
      return d._children ? "lightsteelblue" : "#fff";
    });

  nodeEnter.append("text")
    .attr("x", 10)
    .attr("dy", ".35em")
    .attr("text-anchor", "start")
    //.attr("transform", function(d) { return d.x < 180 ? "translate(0)" : "rotate(180)translate(-" + (d.name.length * 8.5)  + ")"; })
    .text(function(d) {
      return d.name;
    })
    .style("fill-opacity", 1e-6);

  // Transition nodes to their new position.
  var nodeUpdate = node.transition()
    .duration(duration)
    .attr("transform", function(d) {
      return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")";
    })

  nodeUpdate.select("circle")
    .attr("r", 4.5)
    .style("fill", function(d) {
      return d._children ? "lightsteelblue" : "#fff";
    });

  nodeUpdate.select("text")
    .style("fill-opacity", 1)
    .attr("transform", function(d) {
      return d.x < 180 ? "translate(0)" : "rotate(180)translate(-" + (d.name.length + 50) + ")";
    });

  // TODO: appropriate transform
  var nodeExit = node.exit().transition()
    .duration(duration)
    .attr("transform", function(d) {
      return "diagonal(" + source.y + "," + source.x + ")";
    })
    .remove();

  nodeExit.select("circle")
    .attr("r", 1e-6);

  nodeExit.select("text")
    .style("fill-opacity", 1e-6);

  // Update the links…
  var link = svg.selectAll("path.link")
    .data(links, function(d) {
      return d.target.id;
    });

  // Enter any new links at the parent's previous position.
  link.enter().append('svg:path', 'g')
    .duration(self.duration)
    .attr('d', function(d) {
      return self.diagonal([{
        y: d.source.x,
        x: d.source.y
      }, {
        y: d.target.x,
        x: d.target.y
      }]);
    });

  // Transition links to their new position.
  link.transition()
    .duration(duration)
    .attr("d", diagonal);

  // Transition exiting nodes to the parent's new position.
  link.exit().transition()
    .duration(duration)
    .attr("d", function(d) {
      var o = {
        x: source.x,
        y: source.y
      };
      return diagonal({
        source: o,
        target: o
      });
    })
    .remove();

  // Stash the old positions for transition.
  nodes.forEach(function(d) {
    d.x0 = d.x;
    d.y0 = d.y;
  });
}

// Toggle children on click.
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }

  update(d);
}

// Collapse nodes
function collapse(d) {
  if (d.children) {
    d._children = d.children;
    d._children.forEach(collapse);
    d.children = null;
  }
}



 </script>
</body>

</html>
AS a json, I test with this file!!!
  {
  "name": "Top Level",
  "parent": "null",
  "children": [
    {
      "name": "Level 2: A",
      "parent": "Top Level",
      "children": [
        {
          "name": "Son of A",
          "parent": "Level 2: A"
        },
        {
          "name": "Daughter of A",
          "parent": "Level 2: A"
        }
      ]
    },
    {
      "name": "Level 2: B",
      "parent": "Top Level"
    }
  ]
}
;

THANKS!!

ana akana
  • 43
  • 1
  • 6

1 Answers1

1

Your json has a ; at the end

Plunkr demo

Mauricio Poppe
  • 4,817
  • 1
  • 22
  • 30
  • Khaoula, It is working, just as Mauricio said: It was only the semicolon. So, please accept his answer or, even better, delete this question, as it is not going to help any future readers. – Gerardo Furtado May 01 '16 at 01:31
  • thank you Mauricio one more question please! why is it perfectly working on the Plunkr but when I load the files on my laptop they arn't: error msg like : d3.v3.min.js:1 XMLHttpRequest cannot load data.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. Gerardo your intervention is not helping anyone either.. so keep your mean comment to yourself please .. we're all trying to learn here.. – ana akana May 01 '16 at 03:20
  • it seems like you're loading your data from another domain and the server is not configured to receive requests from the calling domain, if this happens in localhost read http://stackoverflow.com/questions/10883211/deadly-cors-when-http-localhost-is-the-origin, read more about CORS here https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS – Mauricio Poppe May 01 '16 at 03:45
  • easiest solition was to change browser to firefox. Thank you! – ana akana May 01 '16 at 21:52