toolkit/javascript/d3/examples/marker/marker.html
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 <!DOCTYPE html>
       
     2 <html>
       
     3   <head>
       
     4     <meta http-equiv="Content-type" content="text/html; charset=utf-8">
       
     5     <title>Mobile Patent Suits</title>
       
     6     <script type="text/javascript" src="../../d3.js"></script>
       
     7     <script type="text/javascript" src="../../d3.geom.js"></script>
       
     8     <script type="text/javascript" src="../../d3.layout.js"></script>
       
     9     <style type="text/css">
       
    10 
       
    11 path.link {
       
    12   fill: none;
       
    13   stroke: #666;
       
    14   stroke-width: 1.5px;
       
    15 }
       
    16 
       
    17 marker#licensing {
       
    18   fill: green;
       
    19 }
       
    20 
       
    21 path.link.licensing {
       
    22   stroke: green;
       
    23 }
       
    24 
       
    25 path.link.resolved {
       
    26   stroke-dasharray: 0,2 1;
       
    27 }
       
    28 
       
    29 circle {
       
    30   fill: #ccc;
       
    31   stroke: #333;
       
    32   stroke-width: 1.5px;
       
    33 }
       
    34 
       
    35 text {
       
    36   font: 10px sans-serif;
       
    37   pointer-events: none;
       
    38 }
       
    39 
       
    40 text.shadow {
       
    41   stroke: #fff;
       
    42   stroke-width: 3px;
       
    43   stroke-opacity: .8;
       
    44 }
       
    45 
       
    46     </style>
       
    47   </head>
       
    48   <body>
       
    49     <script type="text/javascript">
       
    50 
       
    51 // http://blog.thomsonreuters.com/index.php/mobile-patent-suits-graphic-of-the-day/
       
    52 var links = [
       
    53   {source: "Microsoft", target: "Amazon", type: "licensing"},
       
    54   {source: "Microsoft", target: "HTC", type: "licensing"},
       
    55   {source: "Samsung", target: "Apple", type: "suit"},
       
    56   {source: "Motorola", target: "Apple", type: "suit"},
       
    57   {source: "Nokia", target: "Apple", type: "resolved"},
       
    58   {source: "HTC", target: "Apple", type: "suit"},
       
    59   {source: "Kodak", target: "Apple", type: "suit"},
       
    60   {source: "Microsoft", target: "Barnes & Noble", type: "suit"},
       
    61   {source: "Microsoft", target: "Foxconn", type: "suit"},
       
    62   {source: "Oracle", target: "Google", type: "suit"},
       
    63   {source: "Apple", target: "HTC", type: "suit"},
       
    64   {source: "Microsoft", target: "Inventec", type: "suit"},
       
    65   {source: "Samsung", target: "Kodak", type: "resolved"},
       
    66   {source: "LG", target: "Kodak", type: "resolved"},
       
    67   {source: "RIM", target: "Kodak", type: "suit"},
       
    68   {source: "Sony", target: "LG", type: "suit"},
       
    69   {source: "Kodak", target: "LG", type: "resolved"},
       
    70   {source: "Apple", target: "Nokia", type: "resolved"},
       
    71   {source: "Qualcomm", target: "Nokia", type: "resolved"},
       
    72   {source: "Apple", target: "Motorola", type: "suit"},
       
    73   {source: "Microsoft", target: "Motorola", type: "suit"},
       
    74   {source: "Motorola", target: "Microsoft", type: "suit"},
       
    75   {source: "Huawei", target: "ZTE", type: "suit"},
       
    76   {source: "Ericsson", target: "ZTE", type: "suit"},
       
    77   {source: "Kodak", target: "Samsung", type: "resolved"},
       
    78   {source: "Apple", target: "Samsung", type: "suit"},
       
    79   {source: "Kodak", target: "RIM", type: "suit"},
       
    80   {source: "Nokia", target: "Qualcomm", type: "suit"}
       
    81 ];
       
    82 
       
    83 var nodes = {};
       
    84 
       
    85 // Compute the distinct nodes from the links.
       
    86 links.forEach(function(link) {
       
    87   link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
       
    88   link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
       
    89 });
       
    90 
       
    91 var w = 960,
       
    92     h = 500;
       
    93 
       
    94 var force = d3.layout.force()
       
    95     .nodes(d3.values(nodes))
       
    96     .links(links)
       
    97     .size([w, h])
       
    98     .linkDistance(60)
       
    99     .charge(-300)
       
   100     .on("tick", tick)
       
   101     .start();
       
   102 
       
   103 var svg = d3.select("body").append("svg:svg")
       
   104     .attr("width", w)
       
   105     .attr("height", h);
       
   106 
       
   107 // Per-type markers, as they don't inherit styles.
       
   108 svg.append("svg:defs").selectAll("marker")
       
   109     .data(["suit", "licensing", "resolved"])
       
   110   .enter().append("svg:marker")
       
   111     .attr("id", String)
       
   112     .attr("viewBox", "0 -5 10 10")
       
   113     .attr("refX", 15)
       
   114     .attr("refY", -1.5)
       
   115     .attr("markerWidth", 6)
       
   116     .attr("markerHeight", 6)
       
   117     .attr("orient", "auto")
       
   118   .append("svg:path")
       
   119     .attr("d", "M0,-5L10,0L0,5");
       
   120 
       
   121 var path = svg.append("svg:g").selectAll("path")
       
   122     .data(force.links())
       
   123   .enter().append("svg:path")
       
   124     .attr("class", function(d) { return "link " + d.type; })
       
   125     .attr("marker-end", function(d) { return "url(#" + d.type + ")"; });
       
   126 
       
   127 var circle = svg.append("svg:g").selectAll("circle")
       
   128     .data(force.nodes())
       
   129   .enter().append("svg:circle")
       
   130     .attr("r", 6)
       
   131     .call(force.drag);
       
   132 
       
   133 var text = svg.append("svg:g").selectAll("g")
       
   134     .data(force.nodes())
       
   135   .enter().append("svg:g");
       
   136 
       
   137 // A copy of the text with a thick white stroke for legibility.
       
   138 text.append("svg:text")
       
   139     .attr("x", 8)
       
   140     .attr("y", ".31em")
       
   141     .attr("class", "shadow")
       
   142     .text(function(d) { return d.name; });
       
   143 
       
   144 text.append("svg:text")
       
   145     .attr("x", 8)
       
   146     .attr("y", ".31em")
       
   147     .text(function(d) { return d.name; });
       
   148 
       
   149 // Use elliptical arc path segments to doubly-encode directionality.
       
   150 function tick() {
       
   151   path.attr("d", function(d) {
       
   152     var dx = d.target.x - d.source.x,
       
   153         dy = d.target.y - d.source.y,
       
   154         dr = Math.sqrt(dx * dx + dy * dy);
       
   155     return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
       
   156   });
       
   157 
       
   158   circle.attr("transform", function(d) {
       
   159     return "translate(" + d.x + "," + d.y + ")";
       
   160   });
       
   161 
       
   162   text.attr("transform", function(d) {
       
   163     return "translate(" + d.x + "," + d.y + ")";
       
   164   });
       
   165 }
       
   166 
       
   167     </script>
       
   168   </body>
       
   169 </html>