2016-08-25 21 views
0

における埋め込みJSONデータ私はその後、ここにd3.js階層エッジバンドリング:変数

d3.json(HEBdata, function(error, classes) { 
    if (error) throw error; 
を、この変数を使用することになり、インスタンス

var HEBdata = [ 
{"name":"test.cluster1.item1","imports":["test.cluster1.item2","test.cluster1.item3"]}, 
{"name":"test.cluster1.item2","imports":["test.cluster1.item3"]}, 
{"name":"test.cluster1.item3"} 
]; 

ために、変数に直接JSONデータを読みたいです

の代わりにここで

d3.json("https://gist.githubusercontent.com/mbostock/1044242/raw/8f22cf2264e1f6ec6cb233c4b49333fb8f75bb99/readme-flare-imports.json", function(error, classes) { 
    if (error) throw error; 

あなたが変更される可能性があり、コードとフィドルです:https://fiddle.jshell.net/d1766z4r/(URLは、COMとなっていますHEBdata変数に置き換えられます)

カスタムデータを使用して、後で質問したい別の質問に簡単に使用できるようにしたいと思います。

答えて

1

d3.jsonは使用しないでください。代わりにオブジェクトを代入するだけですclasses。 XMLHttpRequestの、またはXHRを使ってブラウザに

負荷データ:

d3.jsonは、基本的にこれを行います。これにより、データを非同期にロードできるようになり(データがロードされている間はページの残りの部分が表示されるため)、JSONPよりも安全です。 D3のxhrモジュールはデータの読み込みと解析を簡単にします。データを非同期にロードするとき、ロードされたデータに依存するコードは、通常、コールバック関数内に存在する必要があります。データに依存しないコードは、ページが読み込まれたときにすぐに実行できます。

https://github.com/d3/d3-3.x-api-reference/blob/master/Requests.md

すでにあなたのデータの準備ができていますので、あなたはそれを使用するのを待つ必要はありません。

var classes = [ 
 
{"name":"test.cluster1.item1","imports":["test.cluster1.item2","test.cluster1.item3"]}, 
 
{"name":"test.cluster1.item2","imports":["test.cluster1.item3"]}, 
 
{"name":"test.cluster1.item3"} 
 
]; 
 

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

 
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("https://gist.githubusercontent.com/mbostock/1044242/raw/8f22cf2264e1f6ec6cb233c4b49333fb8f75bb99/readme-flare-imports.json", function(error, classes) { 
 
    //if (error) throw error; 
 

 
    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; 
 
}
.node { 
 
    font: 300 11px "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
    fill: #bbb; 
 
} 
 

 
.node:hover { 
 
    fill: #000; 
 
} 
 

 
.link { 
 
    stroke: steelblue; 
 
    stroke-opacity: .4; 
 
    fill: none;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

フィドル:https://fiddle.jshell.net/ga1s8bn7/

ので、通常のHTTPレスポンスを受信するまで実行するように待たなければならない任意のコードがすぐにそうように実行することができます