2017-04-06 17 views
1

これはd3.jsバージョン4(v3のthis chartに基づいています)のコードダイアグラムです。あなたはそれを実行するか、単に画像を見てすることができますd3.jsのコードダイアグラムの弧にラベルを追加するには

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> 
 
    <title>Chord Diagramm</title> 
 

 
    <!-- D3.js --> 
 
    <script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script> 
 
    <!-- Google Fonts --> 
 
    <link href='https://fonts.googleapis.com/css?family=Lato:400,900' rel='stylesheet' type='text/css'> 
 
    <!-- bootstrap 4(!) --> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> 
 
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> 
 

 
    <style> 
 
     body { 
 
     font-size: 12px; 
 
     font-family: 'Lato', sans-serif; 
 
     text-align: center; 
 
     fill: #2B2B2B; 
 
     cursor: default; 
 
     } 
 

 
     @media (min-width: 600px) { 
 
     #chart{ 
 
      font-size: 16px; 
 
     } 
 
     } 
 
    </style> 
 
    </head> 
 
    <body> 
 
    <h1> Chord Diagramm </h1> 
 
    <div id = "chart"></div> 
 
    </body> 
 

 
    <script> 
 
    //////////////////////////////////////////////////////////// 
 
    //////////////////////// Set-Up //////////////////////////// 
 
    //////////////////////////////////////////////////////////// 
 

 
    var margin = {left:90, top:90, right:90, bottom:90}, 
 
    width = 1000 - margin.left - margin.right, // more flexibility: Math.min(window.innerWidth, 1000) 
 
    height = 1000 - margin.top - margin.bottom, // same: Math.min(window.innerWidth, 1000) 
 
    innerRadius = Math.min(width, height) * .39, 
 
    outerRadius = innerRadius * 1.1; 
 

 
    var names = ["Test1","Test2","AMO-DB","YouTube","Twitter", "Google+", "Pflegeratgeber" ,"O-Mag","RuV"], 
 
    colors = ["#301E1E", "#083E77", "#342350", "#567235", "#8B161C", "#DF7C00"], 
 
    opacityDefault = 0.8; 
 

 
    var matrix = [ 
 
    [0,1,1,1,1,1,1,1,1], //Test1 
 
    [0,0,1,1,1,1,1,0,1], //Test2 
 
    [0,1,0,1,1,1,1,1,1], //Hawkeye 
 
    [0,1,1,0,1,1,0,1,1], //The Hulk 
 
    [0,1,1,1,0,1,1,1,1], //Iron Man 
 
    [0,1,1,1,1,0,1,1,1], 
 
    [0,1,1,1,1,1,0,1,1], //Iron Man 
 
    [0,1,1,1,1,1,1,0,1], 
 
    [0,1,1,1,1,1,1,0,0] //Thor //Thor 
 
    ]; 
 

 
    //////////////////////////////////////////////////////////// 
 
    /////////// Create scale and layout functions ////////////// 
 
    //////////////////////////////////////////////////////////// 
 

 
    var colors = d3.scaleOrdinal() 
 
     .domain(d3.range(names.length)) 
 
    .range(colors); 
 

 
    var chord = d3.chord() 
 
    .padAngle(.15) 
 
    .sortChords(d3.descending) 
 

 
    var arc = d3.arc() 
 
    .innerRadius(innerRadius*1.01) 
 
    .outerRadius(outerRadius); 
 

 
    var path = d3.ribbon() 
 
    .radius(innerRadius); 
 

 
//////////////////////////////////////////////////////////// 
 
////////////////////// Create SVG ////////////////////////// 
 
//////////////////////////////////////////////////////////// 
 

 
var svg = d3.select("#chart").append("svg") 
 
    .attr("width", width + margin.left + margin.right) 
 
    .attr("height", height + margin.top + margin.bottom) 
 
    .append("g") 
 
    .attr("transform", "translate(" + (width/2 + margin.left) + "," + (height/2 + margin.top) + ")") 
 
    .datum(chord(matrix)); 
 

 
//////////////////////////////////////////////////////////// 
 
////////////////// Draw outer Arcs ///////////////////////// 
 
//////////////////////////////////////////////////////////// 
 

 
var outerArcs = svg.selectAll("g.group") 
 
    .data(function(chords) { return chords.groups; }) 
 
    .enter().append("g") 
 
    .attr("class", "group") 
 
    .on("mouseover", fade(.1)) 
 
    .on("mouseout", fade(opacityDefault)) 
 

 
    // text popups 
 
    .on("click", mouseoverChord) 
 
    .on("mouseout", mouseoutChord); 
 

 
outerArcs.append("path") 
 
    .style("fill", function(d) { return colors(d.index); }) 
 
    .attr("d", arc); 
 

 
//////////////////////////////////////////////////////////// 
 
////////////////////// Append names //////////////////////// 
 
//////////////////////////////////////////////////////////// 
 

 
//Append the label names on the outside 
 
outerArcs.append("text") 
 
    .each(function(d) { d.angle = (d.startAngle + d.endAngle)/2; }) 
 
    .attr("dy", ".35em") 
 
    .attr("class", "titles") 
 
    .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(" + (outerRadius + 10) + ")" 
 
    + (d.angle > Math.PI ? "rotate(180)" : ""); 
 
    }) 
 
    .text(function(d,i) { return names[i]; }); 
 

 

 
//////////////////////////////////////////////////////////// 
 
////////////////// Draw inner chords /////////////////////// 
 
//////////////////////////////////////////////////////////// 
 

 
svg.selectAll("path.chord") 
 
    .data(function(chords) { return chords; }) 
 
    .enter().append("path") 
 
    .attr("class", "chord") 
 
    .style("fill", function(d) { return colors(d.source.index); }) 
 
    .style("opacity", opacityDefault) 
 
    .attr("d", path); 
 

 
//////////////////////////////////////////////////////////// 
 
////////////////// Extra Functions ///////////////////////// 
 
//////////////////////////////////////////////////////////// 
 

 
function popup() { 
 
    return function(d,i) { 
 
    console.log("love"); 
 
    }; 
 
}//popup 
 

 
//Returns an event handler for fading a given chord group. 
 
function fade(opacity) { 
 
    return function(d,i) { 
 
    svg.selectAll("path.chord") 
 
     .filter(function(d) { return d.source.index != i && d.target.index != i; }) 
 
    .transition() 
 
     .style("opacity", opacity); 
 
    }; 
 
}//fade 
 

 
    //Highlight hovered over chord 
 
function mouseoverChord(d,i) { 
 

 
    //Decrease opacity to all 
 
    svg.selectAll("path.chord") 
 
    .transition() 
 
    .style("opacity", 0.1); 
 
    //Show hovered over chord with full opacity 
 
    d3.select(this) 
 
    .transition() 
 
     .style("opacity", 1); 
 

 
    //Define and show the tooltip over the mouse location 
 
    $(this).popover({ 
 
    //placement: 'auto top', 
 
    title: 'test', 
 
    placement: 'right', 
 
    container: 'body', 
 
    animation: false, 
 
    offset: "20px -100px", 
 
    followMouse: true, 
 
    trigger: 'click', 
 
    html : true, 
 
    content: function() { 
 
     return "<p style='font-size: 11px; text-align: center;'><span style='font-weight:900'>" + 
 
      "</span> text <span style='font-weight:900'>" + 
 
      "</span> folgt hier <span style='font-weight:900'>" + "</span> movies </p>"; } 
 
    }); 
 
    $(this).popover('show'); 
 
} 
 
//Bring all chords back to default opacity 
 
function mouseoutChord(d) { 
 
    //Hide the tooltip 
 
    $('.popover').each(function() { 
 
    $(this).remove(); 
 
    }) 
 
    //Set opacity back to default for all 
 
    svg.selectAll("path.chord") 
 
    .transition() 
 
    .style("opacity", opacityDefault); 
 
    }  //function mouseoutChord 
 
    </script> 
 
</html>

私はこの例に従うことを試みた:http://bl.ocks.org/syntagmatic/9eadf19bd2976653fa50ともここで同様のスレッドが見つかりました:d3.js Add labels in Chord diagram

しかし、私はそれを私のコードに統合する方法について私の頭を包み込むことはできません。どのような助け本当に感謝します。 enter image description here

答えて

2

私たちは、このようにそれに近づくことができます:あなたはそれがどのグループに基づいてそれらを追加している間

  1. は、パスにidを追加

    outerArcs.append("path") 
        .style("fill", function(d) { return colors(d.index); }) 
        .attr("id", function(d, i) { return "group" + d.index; }) //add id here 
        .attr("d", arc); 
    
  2. テキストを追加します。 pathに直接アクセスし、次にtextpathを使用して、次のような円の内側に配置します。

    ここでのコードの作業

からhttps://jsfiddle.net/rjonean4/

+0

はありがとうございました!ちょっとしたフォローアップの質問/注記:https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href州xlinkは推奨されていません。私はちょうど '' 'href'''を保っており、それもうまくいくようです。 –

+1

最後の注記:どのようにテキストを中央揃えすることは可能ですか?あなたのすべての協力に感謝します :) –

関連する問題