Skip to content

Instantly share code, notes, and snippets.

@pdiazq
Last active October 24, 2017 00:10
Show Gist options
  • Save pdiazq/a55f66289cfea578607de7b9ef1d7f25 to your computer and use it in GitHub Desktop.
Save pdiazq/a55f66289cfea578607de7b9ef1d7f25 to your computer and use it in GitHub Desktop.
TemperatureAnalysis
license: mit
Cities1 B/quilla Bogotá B/manga B/tura Cali C/gena Cúcuta Ibagué M/zales Medellín Neiva Pasto Pereira Sta.Marta V/cencio Yopal
Armenia 103.87 151.02 109.95 145.83 162.25 107.53 110.21 290.16 280.20 160.17 140.05 121.00 502.75 94.88 150.73 111.43
B/quilla 96.49 108.19 101.65 100.86 186.62 101.06 93.16 103.49 107.20 93.39 100.93 103.92 226.37 100.01 100.11
B/meja 106.37 112.02 205.19 104.85 104.56 98.77 146.47 108.79 113.77 117.83 101.67 112.31 113.44 101.67 119.65 120.01
Bogotá 97.05 123.38 123.75 125.97 94.11 118.91 165.30 156.40 133.70 136.68 114.77 146.20 96.15 284.64 126.04
B/manga 105.31 121.15 105.49 104.73 99.99 180.84 112.54 115.45 119.38 107.98 104.79 113.54 105.21 126.61 118.91
B/tura 98.96 123.75 105.49 164.12 101.62 105.79 138.75 137.32 126.89 111.09 122.29 141.46 93.85 126.31 109.90
Cali 97.92 125.97 104.73 164.12 103.68 105.57 147.80 141.95 127.60 111.07 140.50 148.60 93.23 129.50 105.99
C/gena 186.62 93.43 99.99 101.62 103.68 95.26 94.52 107.51 113.05 93.16 102.95 107.70 142.93 98.33 90.69
Cartago 102.74 153.80 113.18 144.50 152.82 107.39 111.73 210.59 313.57 161.56 133.43 117.85 848.39 94.29 144.12 108.99
Cúcuta 98.15 117.44 180.84 105.79 105.57 95.26 111.24 114.26 119.03 108.36 105.43 112.12 100.98 122.11 119.12
Sogamoso 102.08 155.20 144.18 112.48 107.59 94.00 133.43 122.21 123.56 114.17 114.88 108.62 113.89 105.36 129.16 186.53
Ibagué 90.10 165.30 112.54 135.77 147.80 94.52 111.24 203.04 153.33 152.74 120.55 238.02 92.85 159.12 115.76
Ipiales 98.55 114.20 105.00 120.52 134.90 103.25 105.64 119.17 119.65 115.56 104.04 270.77 121.35 98.60 116.99 107.20
M/zales 109.64 156.40 115.45 137.32 141.95 107.51 114.26 203.04 180.90 129.64 121.31 393.81 93.26 134.14 117.95
Medellín 103.47 133.70 119.38 126.89 127.60 113.05 119.03 153.33 180.90 110.22 116.16 165.32 101.52 136.76 113.42
Neiva 90.58 136.68 109.20 111.09 111.07 93.16 109.31 148.81 129.64 110.22 103.70 137.31 90.20 139.01 110.94
Pasto 98.39 114.77 104.79 122.29 140.50 106.61 105.43 120.56 121.31 116.16 103.70 118.60 98.14 115.25 104.70
Pereira 101.07 146.20 113.54 141.46 148.60 107.70 110.37 238.02 393.81 168.07 137.31 118.60 94.24 147.11 111.15
Sta.Marta 226.37 96.15 105.21 94.39 93.23 142.93 100.98 92.85 93.26 101.52 90.20 98.14 94.24 101.05 100.28
V/cencio 101.63 284.64 128.39 127.51 129.50 97.87 123.37 159.12 134.14 136.76 139.01 115.25 147.11 101.05 123.68
Yopal 98.81 125.35 118.14 109.62 105.68 90.69 118.47 115.31 117.57 113.10 110.57 104.49 110.77 100.28 123.68
d3.dijkstra = function () {
var dijkstra = {}, nodes, edges, source, dispatch = d3.dispatch("start", "tick", "step", "end");
dijkstra.run = function (src) {
source = src;
var unvisited = [];
nodes.forEach(function (d) {
if (d != src) {
d.distance = Infinity;
unvisited.push(d);
d.visited = false;
}
});
var current = src;
current.distance = 0;
function tick() {
current.visited = true;
current.links.forEach(function(link) {
var tar = link.target;
if (!tar.visited) {
var dist = current.distance + link.value;
tar.distance = Math.min(dist, tar.distance);
}
});
if (unvisited.length == 0 || current.distance == Infinity) {
dispatch.end()
return true;
}
unvisited.sort(function(a, b) {
return b.distance - a.distance
});
current = unvisited.pop()
dispatch.tick();
return false;
}
d3.timer(tick);
};
dijkstra.nodes = function (_) {
if (!arguments.length)
return nodes;
else {
nodes = _;
return dijkstra;
}
};
dijkstra.edges = function (_) {
if (!arguments.length)
return edges;
else {
edges = _;
return dijkstra;
}
};
dijkstra.source = function(_) {
if (!arguments.length)
return source;
else {
source = _;
return dijkstra;
}
};
dispatch.on("start.code", dijkstra.run);
return d3.rebind(dijkstra, dispatch, "on", "end", "start", "tick");
};
-<h1>Programas impartidos en Casas de Cultura Barranquilla</h1>
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="dijkstra.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(250)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.tsv("cities.tsv", function(error, data) {
var nodes = [], nodesByName = {}, links = [];
function addNodeByName(fullname) {
var name = fullname.split(',')[0];
if (!nodesByName[name]) {
var node = {"name":name, "links":[]}
nodesByName[name] = node;
nodes.push(node);
return node;
}
else
return nodesByName[name];
}
data.forEach(function(d) {
for (k in d) {
if (d.hasOwnProperty(k) && k != "Cities1" && d[k] < 750) {
links.push({"source": addNodeByName(d["Cities1"]), "target": addNodeByName(k), "value": parseInt(d[k])})
}
}
});
force
.nodes(nodes)
.links(links)
.start();
var link = svg.selectAll(".link")
.data(links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", 1);
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", "grey")
.call(force.drag);
link.each(function(d) {
d.source.links.push(d);
d.selection = d3.select(this);
});
node.each(function(d) {
d.selection = d3.select(this);
});
node.append("title")
.text(function(d) { return d.name; });
link.append("title")
.text(function(d) { return d.source.name + " → " + d.target.name + "\n" + d.value + " mi" });
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; });
});
node.on("mouseover", function(d) {
d3.select(this)
.attr("r", 10)
d.links.forEach(function(l) {
l.selection
.style("stroke-width", 10)
l.target.selection
.attr("r", 7);
});
});
node.on("mouseout", function(d) {
node.attr("r", 5)
link.style("stroke-width", 1);
});
link.on("mouseover", function() {
d3.select(this)
.style("stroke-width", 10);
});
link.on("mouseout", function() {
d3.select(this)
.style("stroke-width", 1);
});
var dijkstra = d3.dijkstra()
.nodes(nodes)
.edges(links);
var color = d3.scale.linear()
.domain([0, 100, 200])
.range(["green", "yellow", "red"]);
dijkstra.on("tick", function() {
node.style("fill", function(d) { return color(d.distance); });
});
dijkstra.on("end", function() {
var name = dijkstra.source().name;
node.select("title")
.text(function(d) { return d.name + "\n(" + d.distance + " Costo promedio de tonelada transportada por kilómetro desde " + name + ")" });
});
node.on("click", dijkstra.start);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment