Sankey Inmigración

tarea

by jwaddington

HTML

<p id="chart"></p>
<script>
    var ddata = {
        "nodes": [
    {"node": 0,"name": "Santiago"}, 
    {"node": 1,"name": "Estación Central"}, 
    {"node": 2,"name": "Recoleta"}, 
    {"node": 3,"name": "Iquique"}, 
    {"node": 4,"name": "Antofagasta"}, 
    {"node": 5,"name": "Calama"}, 
    {"node": 6,"name": "Quinta Normal"}, 
    {"node": 7,"name": "Providencia"}, 
    {"node": 8,"name": "Alto Hospicio"},
    {"node": 9,"name": "Las Condes"},    
    {"node": 10,"name": "Otras Comunas"},
    {"node": 11,"name": "Perú"},
    {"node": 12,"name": "Colombia"},
    {"node": 13,"name": "Venezuela"}, 
    {"node": 14,"name": "Bolivia"}, 
    {"node": 15,"name": "Argentina"}, 
    {"node": 16,"name": "Haití"}, 
    {"node": 17,"name": "Resto"}, 
    ],
        "links": [
    {"source": 0,"target": 11, "value": 37}, 
    {"source": 0,"target": 12, "value": 23},
    {"source": 0,"target": 13, "value": 19},
    {"source": 0,"target": 14, "value": 3},
    {"source": 0,"target": 15, "value": 1},
    {"source": 0,"target": 16, "value": 8},
    {"source": 0,"target": 17, "value": 10},
    
    {"source": 1,"target": 11, "value": 22}, 
    {"source": 1,"target": 12, "value": 19},
    {"source": 1,"target": 13, "value": 38},
    {"source": 1,"target": 14, "value": 2},
    {"source": 1,"target": 15, "value": 1},
    {"source": 1,"target": 16, "value": 4},
    {"source": 1,"target": 17, "value": 13},
 
 		{"source": 2,"target": 11, "value": 23}, 
    {"source": 2,"target": 12, "value": 13},
    {"source": 2,"target": 13, "value": 17},
    {"source": 2,"target": 14, "value": 3},
    {"source": 2,"target": 15, "value": 1},
    {"source": 2,"target": 16, "value": 32},
    {"source": 2,"target": 17, "value": 10},
    
    {"source": 3,"target": 11, "value": 51}, 
    {"source": 3,"target": 12, "value": 9},
    {"source": 3,"target": 13, "value": 5},
    {"source": 3,"target": 14, "value": 8},
    {"source": 3,"target": 15, "value": 1},
    {"source": 3,"target": 16, "value":...

CSS

.node rect {
	  cursor: move;
	  fill-opacity: .9;
	  shape-rendering: crispEdges;
	}
	.node text {
	  pointer-events: none;
	  text-shadow: 0 1px 0 #fff;
	}
	.node:hover {
		stroke-opacity: .2;
	}
	.link {
	  fill: none;
	  stroke: #000;
	  stroke-opacity: .2;
	}
	.link:hover {
	  stroke-opacity: .5;
	}

JavaScript

var margin = {top: 10, right: 10, bottom: 10, left: 10},
    width = 700 - margin.left - margin.right,
    height = 300 - margin.top - margin.bottom;

var formatNumber = d3.format(",.0f"),
    format = function(d) { return formatNumber(d) + " TWh"; },
    color = d3.scale.category20();

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(" + margin.left + "," + margin.top + ")");

var sankey = d3sankey()
    .nodeWidth(25)
    .nodePadding(15)
    .size([width, height]);

var path = sankey.link();

var energy = getData();

  sankey
      .nodes(energy.nodes)
      .links(energy.links)
      .layout(32);

  var link = svg.append("g").selectAll(".link")
      .data(energy.links)
    .enter().append("path")
      .attr("class", "link")
      .attr("d", path)
      .style("stroke-width", function(d) { return Math.max(1, d.dy); })
      .sort(function(a, b) { return b.dy - a.dy; });

  link.append("title")
      .text(function(d) {
          return d.source.name + " → " + d.target.name + "\n" +
                 format(d.value);
      });

  var node = svg.append("g").selectAll(".node")
      .data(energy.nodes)
    .enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) {
          return "translate(" + d.x + "," + d.y + ")";
      })
    .call(d3.behavior.drag()
      .origin(function(d) { return d; })
      .on("dragstart", function() { this.parentNode.appendChild(this); })
      .on("drag", dragmove));

  node.append("rect")
      .attr("height", function(d) { return d.dy; })
      .attr("width", sankey.nodeWidth())
      .style("fill", function(d) {
          return d.color = color(d.name.replace(/ .*/, ""));
      })
      .style("stroke", function(d) { return d3.rgb(d.color).darker(2); });

  node.append("text")
      .attr("class","nodeValue")
      .text(function(d) { return...