D3.js Template

by allenday

HTML

<!--<div class="scaling-svg-container" 
   style="padding-bottom: 92% /* 100% * 55/60 */">
  <svg class="scaling-svg" viewBox="0 0 60 55" >
  </svg>
</div>-->

<!--
-->
<svg></svg>
<!--

  --second degree
  SELECT
    tx.from_address, tx.to_address, SUM(tx.value)/POWER(10,18) AS value
  FROM `bigquery-public-data.crypto_ethereum.transactions` AS tx
  WHERE from_address IN (
    SELECT
      to_address
    FROM `bigquery-public-data.crypto_ethereum.transactions` AS tx
    WHERE
      from_address = '0x0af5035a8dff53f5a89bfc083fdec94c7332ad7a'
  )
  GROUP BY from_address, to_address
--  HAVING COUNT(tx.to_address) > 1

UNION ALL

  --first degree
  SELECT
    from_address, to_address, SUM(tx.value)/POWER(10,18)
  FROM `bigquery-public-data.crypto_ethereum.transactions` AS tx
  WHERE
    from_address = '0x0af5035a8dff53f5a89bfc083fdec94c7332ad7a'
  GROUP BY from_address, to_address
-->

CSS

.links line {
  stroke: #B64A2A;
  stroke-opacity: 0.4;
}

.nodes circle {
  stroke: #fff;
  stroke-width: 1.5px;
  fill: #B64A2A;
}

text {
  font-family: sans-serif;
  font-size: 10px;
}

svg {
  background-color: #fffafa;
}

JavaScript

var width=1000;
var height=1000;
var svg = d3.select("svg")
  .style("width",width)
  .style("height",height)
//  .style("background-color","#FFFAFA");
  ;  
var simulation = d3.forceSimulation()
    .force("link", 
        d3.forceLink()
          .id(function(d) { return d.id; })
          .distance(function(d) {
            //return d.value/10000;
            return Math.log(d.value);
            })
          .strength(0.08)
      )
    .force("charge", d3.forceManyBody())
    .force("center", d3.forceCenter(width / 2, height / 2));

d3.json("https://raw.githubusercontent.com/allenday/force-layout-from-csv/master/graph.json?1", function(error, graph) {
  if (error) throw error;

  graph.nodes = [];
  nodeMap = new Map();
  nodeVal = new Map();
  graph.links.map(link=>{ nodeMap.set(link.source, 1); nodeMap.set(link.target, 1); });
  graph.links.map(link=>{ 
      nodeVal.set(link.target,link.value+(nodeVal.get(link.target)||0));
      nodeVal.set(link.source,link.value-(nodeVal.get(link.source)||0));
    });
  for (let element of nodeMap) {
    graph.nodes.push({id:element[0],group:1});
  }

  var link = svg.append("g")
      .attr("class", "links")
    .selectAll("line")
    .data(graph.links)
    .enter().append("line")
      .attr("stroke-width", function(d) { 
          //return d.value/10000;
          return Math.log(d.value);
        });

  var node = svg.append("g")
      .attr("class", "nodes")
    .selectAll("g")
    .data(graph.nodes)
    .enter().append("g")
  var circles = node.append("circle")
//      .attr("r", 5)
      .attr("r", function(d) { return 5 + 0.1 * Math.sqrt(nodeVal.get(d.id) / 3.14159);})
//      .attr("r", function(d) { return 5+Math.log(nodeVal.get(d.id));})
//      .attr("fill", function(d) { return color(d.group); })
      .call(d3.drag()
          .on("start", dragstarted)
          .on("drag", dragged)
          .on("end", dragended)
        );

  var labels = node.append("text")
      .text(function(d) {
        return...