LoginSignup
11
12

More than 5 years have passed since last update.

d3jsでかっこいいグラフ作る

Last updated at Posted at 2015-06-05

以下のコードで何ができるか。。。サンプル

<script>
  var links = [
    {source: "Ruby", target: "Ruby on Rails", type: "programming"},
    {source: "Vim", target: "Ruby", type: "editor"},
    {source: "NERDTree", target: "Vim", type: "editor"},
    {source: "GNU screen", target: "Vim", type: "editor"},
    {source: "JQuery", target: "Javascript", type: "front"},
    {source: "Javascript", target: "Ruby on Rails", type: "front"},
    {source: "Ajax", target: "Ruby on Rails", type: "programming"},
    {source: "React", target: "Javascript", type: "front"},
    {source: "d3js", target: "Javascript", type: "front"},
    {source: "Ubuntu", target: "Linux", type: "OS"},
    {source: "Ubuntu", target: "MacOSX", type: "OS"},
    {source: "MacOSX", target: "Linux", type: "OS"},
    {source: "MySQL", target: "Linux", type: "DB"},
    {source: "MySQL", target: "Ruby on Rails", type: "DB"},
    {source: "MongoDB", target: "Linux", type: "DB"},
    {source: "Munin", target: "Linux", type: "tool"},
    {source: "Monit", target: "Linux", type: "tool"},
    {source: "Monit", target: "Munin", type: "tool"},
    {source: "Nginx", target: "Ruby on Rails", type: "rails-server"},
    {source: "Unicorn", target: "Ruby on Rails", type: "rails-server"},
    {source: "Unicorn", target: "Nginx", type: "rails-server"},
    {source: "Capistrano", target: "Ruby on Rails", type: "rails-deploy"},
    {source: "Replication", target: "MySQL", type: "db-architecture"},
    {source: "ReplicaSet", target: "MongoDB", type: "db-architecture"},
    {source: "Sharding", target: "MongoDB", type: "db-architecture"},
    {source: "bash", target: "Linux", type: "shell"},
    {source: "zsh", target: "Linux", type: "shell"},
    {source: "zsh", target: "bash", type: "shell"},
    {source: "Ansible", target: "Linux", type: "tool"},
    {source: "AWS", target: "SAKURA", type: "server-company"},
    {source: "iTerm2", target: "Vim", type: "mac-tool"},
    {source: "Git", target: "Ruby on Rails", type: "development"},
    {source: "cron", target: "Linux", type: "tool"}
  ];
  var nodes = {};
  links.forEach(function(link) {
    link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
    link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
  });
  var width = $(window).width(),
      height = $(window).height();
  var force = d3.layout.force()
      .nodes(d3.values(nodes))
      .links(links)
      .size([width, height])
      .linkDistance(60)
      .charge(-300)
      .on("tick", tick)
      .start();
  var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);
  var link = svg.selectAll(".link")
      .data(force.links())
    .enter().append("line")
      .attr("class", "link");
  var node = svg.selectAll(".node")
      .data(force.nodes())
    .enter().append("g")
      .attr("class", "node")
      .on("mouseover", mouseover)
      .on("mouseout", mouseout)
      .call(force.drag);
  node.append("circle")
      .attr("r", 8);
  node.append("text")
      .attr("x", 12)
      .attr("dy", ".35em")
      .attr("fill", "white")
      .text(function(d) { return d.name; });
  function tick() {
    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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
  }
  function mouseover() {
    d3.select(this).select("circle").transition()
        .duration(750)
        .attr("r", 16);
  }
  function mouseout() {
    d3.select(this).select("circle").transition()
        .duration(750)
        .attr("r", 8);
  }
</script>

ここみた http://bl.ocks.org/mbostock/4062045

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12