D3.jsでForce-Directed Graphを使ってラブライブ!のキャラ関係を可視化する
Force-Directed Graph
力指向グラフは、nodeの間と描画領域に物理的な作用を計算してうまいぐあいに表示するらしいです。
各node(円)はマウスで動かせます。
元データはPixivのタグ検索集計です。「(ラブライブ! OR ラブライブ!) にこまき」などそれぞれのキーワードで検索して出てきた作品数を集計しました。
線の太さがタグ数(「にこまき」なら“にこ”“まき”間の線が出現数に応じて太くなる)を表しています。nodeの外周を囲む線は学年ごとの塗り分けをしています。
JSONのデータの構造
nodes配列をdata()でセットして、今までと同じようにcircleとlineをappendします。
links配列は、各nodeの関係を記述します。sourceの0は、nodes[0]です。
graph = { nodes : [ { "name" : "にこ", "color" : "#ff5fdb" }, { "name" : "まき", "color" : "#ff6136" }, { "name" : "りん", "color" : "#55fbd6" }, { "name" : "はな", "color" : "#42ce70" } ], links : [ { "source" : 0, "target" : 1, "value" : 157 }, { "source" : 2, "target" : 3, "value" : 32 }, ]}
一部のみです。
force layoutの使い方
nodeとしてcircleを、linkの関係を表すものとしてlineを準備します。なお、テキスト関連の処理は除いてあります。
nodeは.call(force.drag)
とすることでドラッグ可能になります。
var color = d3.scale.category10(); var svg = d3.select("div.entry-content").append("svg") .attr("width", width) .attr("height", height); var force = d3.layout.force() .charge(-240) .linkDistance(120) .size([width, height]); force .nodes(graph.nodes) .links(graph.links) .start(); var link = svg.selectAll(".link") .data(graph.links) .enter().append("line") .attr("class", "link") .style("stroke-width", function(d) { return Math.sqrt(d.value) * 1.5; }); var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("circle") .attr("class", "node") .style("fill", function(d) { return d.color; }) .call(force.drag); node.append("title") .text(function(d) { return d.name; }); force.on("tick", function() { link.attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }); node.attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }); });