2016-04-30 12 views
0

私のスクリプトのデバッグに役立つどんなボディもあります!私はjsonファイルから外部データを読み込むことができません..それはコード内の変数で動作しますが、ファイル内でそれらを区切ると..それはありません! JSON ASD3.js tree diagramme外部データインポートデバッグ

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

 
\t \t .node { 
 
\t \t cursor: pointer; 
 
\t \t } 
 

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

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

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

 

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

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

 
\t \t var i = 0, 
 
\t \t duration = 350, 
 
\t \t root; 
 

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

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

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

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

 
//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; 
 
    } 
 
} 
 

 

 

 
\t </script> 
 
</body> 
 

 
</html>
、私は、このファイルでテスト!
{ 
 
    "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 !!

+0

あなたのJSONファイルは、JSと同じファイルの場所にあるに;を持っていますか? – thatOneGuy

+0

はい彼らは同じ場所にあります –

+0

エラーは何ですか? – thatOneGuy

答えて

1

あなたのJSONは終わり

Plunkr demo

+0

はどちらも動作しません! :/ –

+0

あなたはplunkrのデモを見たことがありますか?それは働いています:) –

+0

Khaoula、ちょうどMauricioが言ったように、それは働いています:それはセミコロンだけでした。だから、将来の読者を助けるつもりはないので、彼の答えを受け入れるか、さらにはこの質問を削除してください。 –