2015-11-02 88 views
0

這裏是我需要繪製一個和絃但在eclipse形狀而不是圓形的示例js文件。其次,我需要知道創建和絃圖時矩陣使用的是什麼,並且我們能夠使用簡單的json文件(不使用矩陣)繪製和絃,如http://www.delimited.io/blog/2013/12/8/chord-diagrams-in-d3。因爲在每一個和絃的例子中都有一些矩陣可以用來繪製它。我對d3很陌生,我需要學習很多東西。任何一個可以幫助才能真正體會到它我需要在d3中繪製一個和絃形狀而不是圓形

var outerRadius = 500/2, 
innerRadius = outerRadius - 100; 

var fill = d3.scale.category20c(); 

var chord = d3.layout.chord() 
.padding(.04) 
.sortSubgroups(d3.descending) 
.sortChords(d3.descending); 

var arc = d3.svg.arc() 
.innerRadius(innerRadius) 
.outerRadius(innerRadius + 20); 

var svg = d3.select('#content').append("svg") 
.attr("width", outerRadius * 2) 
.attr("height", outerRadius * 2) 
.append("g") 
.attr("transform", "translate(" + outerRadius + "," + outerRadius +  

")"); 


d3.json("readme.json", function(error, imports) { 
if (error) throw error; 

var indexByName = d3.map(), 
    nameByIndex = d3.map(), 
    matrix = [], 
    n = 0; 

// Returns the Flare package name for the given class name. 
function name(name) { 
return name.substring(0, name.lastIndexOf(".")).substring(6); 
} 

// Compute a unique index for each package name. 
imports.forEach(function(d) { 
if (!indexByName.has(d = name(d.name))) { 
    nameByIndex.set(n, d); 
    indexByName.set(d, n++); 
} 
}); 

// Construct a square matrix counting package imports. 
imports.forEach(function(d) { 
    var source = indexByName.get(name(d.name)), 
    row = matrix[source]; 
    if (!row) { 
    row = matrix[source] = []; 
    for (var i = -1; ++i < n;) row[i] = 0; 
} 
    d.imports.forEach(function(d) { row[indexByName.get(name(d))]++; }); 
    }); 

chord.matrix(matrix); 

var g = svg.selectAll("g.group") 
    .data(chord.groups()) 
    .enter().append("svg:g") 
    .attr("class", "group") 

.on("mouseover", fade(.02)) 
    .on("mouseout", fade(.80)); 
// .on("mouseover", mouseover); 
    //.on("mouseout", fade(1)); 

    g.append("svg:path") 
    .style("stroke", "none") 
    .style("fill", function(d) { return fill(d.index); }) 
    .attr("d", arc); 
/* g.append("path") 
    .style("fill", function(d) { return fill(d.index); }) 
    .style("stroke", function(d) { return fill(d.index); }) 
    .attr("d", arc);*/ 

    g.append("text") 
    .each(function(d) { d.angle = (d.startAngle + d.endAngle)/2; }) 
    .attr("dy", ".35em") 
    .attr("transform", function(d) { 
    return "rotate(" + (d.angle * 180/Math.PI - 90) + ")" 
    + "translate(" + (innerRadius + 26) + ")" 
    + (d.angle > Math.PI ? "rotate(180)" : ""); 
    }) 
    .style("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; }) 
    .text(function(d) { return nameByIndex.get(d.index); }); 

    svg.selectAll(".chord") 
    .data(chord.chords) 
.enter().append("path") 
    .attr("class", "chord") 
    .style("stroke", function(d) { return 
    d3.rgb(fill(d.source.index)).darker(); }) 
    .style("fill", function(d) { return fill(d.source.index); }) 
//.style("opacity", 1) 
    .attr("d", d3.svg.chord().radius(innerRadius)); 

}); 


    d3.select(self.frameElement).style("height", outerRadius * 5 + "px"); 
+0

什麼是日食形狀可以是你能不能詳細說...在這個例子中你粘貼您從JSON d3.json讀(」 readme.json「,...我不明白你的意思是由這裏的matrx。 – Cyril

+0

https://github.com/sghall/d3-chord-diagrams/blob/master/uber.html在這個例子中看到數據被調用兩個文件.defer(d3.json,'data/uber-matrix.json')和.defer(d3.csv,'data/uber-cities.csv'),所以我需要知道這個矩陣文件是什麼?和絃總是繪製成圓形,我需要繪製一個和絃,但在中橢圓形休息會和絃看起來一樣。 – Sam

回答

0
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<style> 
    #tooltip { 
    color: white; 
    opacity: .9; 
    background: #333; 
    padding: 5px; 
    border: 1px solid lightgrey; 
    border-radius: 5px; 
    position: absolute; 
    z-index: 10; 
    visibility: hidden; 
    white-space: nowrap; 
    pointer-events: none; 
    } 
    #circle circle { 
    fill: none; 
    pointer-events: all; 
    } 
    path.group { 
    fill-opacity: .8; 
    } 
    path.chord { 
    fill-opacity: .8; 
    stroke: #000; 
    stroke-width: .25px; 
    } 
    #circle:hover path.fade { 
    display: none; 
    } 
</style> 
    </head> 
    <body> 
<div id="tooltip"></div> 
<script src="lib/d3.js"></script> 
<script src="lib/underscore.js"></script> 
<script src="js/mapper.js"></script> 
<script> 


//******************************************************************* 
    // CREATE MATRIX AND MAP 

//******************************************************************* 
    d3.csv('data/CNV.csv', function (error, data) { 


    var mpr = chordMpr(data); 

    mpr 
     .addValuesToMap('chr_no') 
     .addValuesToMap('MUT') 

    mpr .setFilter(function (row, a, b) { 
     return (row.chr_no === a.name && row.MUT === b.name) || 
       (row.chr_no === b.name && row.MUT === a.name) 

     }) 
     .setAccessor(function (recs, a, b) { 
     if (!recs[0]) return 0; 
      return recs[0].MUT === a.name ? +recs[0].chr_start :  
    +recs[0].chr_stop ; 
     }); 
    drawChords(mpr.getMatrix(), mpr.getMap()); 
    }); 


//******************************************************************* 
    // DRAW THE CHORD DIAGRAM 

//******************************************************************* 
    function drawChords (matrix, mmap) { 
    var w = 980, h = 800, r1 = h/2, r0 = r1 - 110; 

    var fill = d3.scale.ordinal() 

    .range(['#c7b570','#c6cdc7','#335c64','#768935', 
    '#507282','#5c4a56','#aa7455','#574109','#837722', 
'#73342d','#0a5564','#9c8f57','#7895a4','#4a5456', 
'#b0a690','#0a3542',]); 

    var chord = d3.layout.chord() 
     .padding(.04) 
     .sortSubgroups(d3.descending) 
     .sortChords(d3.descending); 

    var arc = d3.svg.arc() 
     .innerRadius(r0) 
     .outerRadius(r0 + 20); 

    var svg = d3.select("body").append("svg:svg") 
     .attr("width", w) 
     .attr("height", h) 
     .append("svg:g") 
     .attr("id", "circle") 
     .attr("transform", "translate(" + w/2 + "," + h/2 + ")"); 

     svg.append("circle") 
      .attr("r", r0 + 20); 

    var rdr = chordRdr(matrix, mmap); 
    chord.matrix(matrix); 

    var g = svg.selectAll("g.group") 
     .data(chord.groups()) 
     .enter().append("svg:g") 
     .attr("class", "group") 
     .on("mouseover", mouseover) 
     .on("mouseout", function (d) {  
    d3.select("#tooltip").style("visibility", "hidden") }); 

    g.append("svg:path") 
     .style("stroke", "black") 
     .style("fill", function(d) { return fill(rdr(d).gname); }) 
     .attr("d", arc); 

    g.append("svg:text") 
     .each(function(d) { d.angle = (d.startAngle + d.endAngle)/2;  
    }) 
     .attr("dy", ".35em") 
     .style("font-family", "helvetica, arial, sans-serif") 
     .style("font-size", "9px") 
     .attr("text-anchor", function(d) { return d.angle > Math.PI ? 
     "end" : null; }) 
     .attr("transform", function(d) { 
      return "rotate(" + (d.angle * 180/Math.PI - 90) + ")" 
       + "translate(" + (r0 + 26) + ")" 
       + (d.angle > Math.PI ? "rotate(180)" : ""); 
     }) 
     .text(function(d) { return rdr(d).gname; }); 

     var chordPaths = svg.selectAll("path.chord") 
      .data(chord.chords()) 
      .enter().append("svg:path") 
      .attr("class", "chord") 
      .style("stroke", function(d) { return 
     d3.rgb(fill(rdr(d).sname)).darker(); }) 
      .style("fill", function(d) { return fill(rdr(d).sname); }) 
      .attr("d", d3.svg.chord().radius(r0)) 
      .on("mouseover", function (d) { 
       d3.select("#tooltip") 
       .style("visibility", "visible") 
       .html(chordTip(rdr(d))) 
       .style("top", function() { return (d3.event.pageY - 
     170)+"px"}) 
       .style("left", function() { return (d3.event.pageX - 
      100)+"px";}) 
      }) 
      .on("mouseout", function (d) { 
     d3.select("#tooltip").style("visibility", "hidden") }); 


     function chordTip (d) { 

    var p = d3.format(".0%"), q = d3.format("0d") 
     return "Choromosome information:<br/>" 
      + q(d.sname) + " overlap with " + d.tname +" " + 
"<br/>chromosome starts at"+" "+ d.sdata +" " + 
"<br/>chromosome ends at"+ " "+ d.tdata 

     } 


     } 

     function mouseover(d, i) { 
     d3.select("#tooltip") 
      .style("visibility", "visible") 
      .html(groupTip(rdr(d))) 
      .style("top", function() { return (d3.event.pageY - 80)+"px"}) 
      .style("left", function() { return (d3.event.pageX - 130)+"px";}) 

     chordPaths.classed("fade", function(p) { 
      return p.source.index != i 
       && p.target.index != i; 
     }); 
     } 
    } 

</script> 

+0

這是我在這段代碼中的代碼,我映射了chr_start,chr_stop,chr_no和MUT。我想要的是顯示其餘的csv文件中的字段作爲工具提示信息。我第一次繪製和絃,我不知道該怎麼做。在節點圖中,我們使用特定的字段作爲d.name等,現在在和絃圖中,我可以如何獲得其餘的值,我在csv中擁有屬性,名稱,年齡以顯示爲工具提示。 thakns提前 – Sam