2017-01-03 65 views
0

鑑於Mike Bostock在層次邊捆綁(https://bl.ocks.org/mbostock/7607999)上的示例,如何爲圖添加定量維?d3js:將定量維添加到分層邊捆綁

我想要實現的是,例如,間「可視化」(在12點)和「SpanningTree」(在1點鐘方向)的鏈接具有50%的透明度和「可視化」「AspectRatioBanker」(也是1點)之間的鏈接具有0%的透明度。讓我們假設「SpanningTree」是一個不太重要的進口「可視化」「AspectRatiobanker」並且我想用這個漸變色標表示這個方面。

考慮到數據,"imports"數組中的每個元素都必須有一個數值,表示該元素的「重要性」。

考慮到d3,每個鏈接都必須根據此數值進行着色。

有沒有可能在不改變太多示例代碼的情況下添加這樣的功能?

+1

我認爲這是堆棧溢出的錯誤類型的問題。你沒有確定的問題嘗試過某些東西,但無法讓它起作用,但是更多地瞭解你想要做什麼並尋找某個人爲你做到這一點?一般來說,如果每個導入都有一個數值,那麼您可以在該值上使用顏色和不透明度。在實際中,這將需要工作,並瞭解示例代碼中發生了什麼。 要回答你的問題,它的可能,但沒有改變太多的代碼?我不知道,可能不是。什麼太多了?你有沒有試過改變這個例子? – Craicerjack

+0

當你說'每個鏈接都必須根據此數值進行着色「時,是指基於值的顏色還是基於值或兩者的不透明度? – Mark

+0

不要緊,我得到你,你說它是目前使用不透明度重要,你寧願在顏色上做... – Mark

回答

2

如果我正確理解你的問題,你要開始像數據:

[ 
    { 
    "name": "flare.analytics.cluster.AgglomerativeCluster", 
    "size": 3938, 
    "imports": [ 
     { 
     "link": "flare.animate.Transitioner", 
     "value": 47.194114234125514 
     }, 
     { 
     "link": "flare.vis.data.DataList", 
     "value": 66.57002100495298 
     }, 
     { 
     "link": "flare.util.math.IMatrix", 
     "value": 5.987508739765435 
     }, 
     { 
     "link": "flare.analytics.cluster.MergeEdge", 
     "value": 31.750046370493678 
     }, 
     { 
     "link": "flare.analytics.cluster.HierarchicalCluster", 
     "value": 10.186873728884827 
     }, 
     { 
     "link": "flare.vis.data.Data", 
     "value": 28.60757703865271 
     } 
    ] 
    }, 
    ... 

然後顏色基於進口value屬性的鏈接。

要做到這一點給出鏈接的例子,修改packageImports功能保留值屬性links集合中:

var colorScale = d3.scale.quantize() 
    .range(["#2c7bb6", "#00a6ca","#00ccbc","#90eb9d","#ffff8c","#f9d057","#f29e2e","#e76818","#d7191c"]) 
    .domain([0,100]); 

... 

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) 
    .style("stroke", function(d){ 
    return colorScale(d.target.value); //<-- add color 
    }); 

// 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) { 
     var target = map[i.link]; // find the target 
     target.value = i.value; // retain the value 
     imports.push({source: map[d.name], target: target}); 
    }); 
    }); 
    return imports; 
} 

然後,當你追加節點添加顏色全面運行代碼:

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

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

 
.node:hover { 
 
    fill: #000; 
 
} 
 

 
.link { 
 
    fill: none; 
 
    pointer-events: none; 
 
} 
 

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

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

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

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

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

 
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://jsonblob.com/api/851bd2f2-d85d-11e6-b16a-ad927fd57221", function(error, classes) { 
 
    if (error) throw error; 
 
    
 
    var nodes = cluster.nodes(packageHierarchy(classes)), 
 
     links = packageImports(nodes); 
 
     
 
    var colorScale = d3.scale.quantize() 
 
    .range(["#2c7bb6", "#00a6ca","#00ccbc","#90eb9d","#ffff8c","#f9d057","#f29e2e","#e76818","#d7191c"]) 
 
    .domain([0,100]); 
 

 
    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) 
 
     .style("stroke", function(d){ 
 
     return colorScale(d.target.value); 
 
     }) 
 

 
    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) { 
 
     var target = map[i.link]; 
 
     target.value = i.value; 
 
     imports.push({source: map[d.name], target: target}); 
 
    }); 
 
    }); 
 
    return imports; 
 
} 
 

 
</script>

+1

感謝您的努力!我會接受你的答案,因爲它通常回答我的問題。我想要的顏色只出現'.on(mousover)',但這似乎是一個微不足道的任務,我不需要任何幫助。 –