Brooklyn Train Misfit Map - Communication
by dshipper
HTML
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script type="application/json" id="mis">
{
"nodes": [{
"name": "I don't want to have to walk far to get to the train",
"group": "1",
"number": "0"
}, {
"name": "I want to be able to go anywhere inside the city quickly",
"group": "1",
"number": "1"
}, {
"name": "I want to be able to go another inter-city transportation hub quickly",
"group": "1",
"number": "2"
}, {
"name": "I want to transfer as few times as possible",
"group": "1",
"number": "3"
}, {
"name": "I want to to be able to use my phone while in transit",
"group": "1",
"number": "4"
}, {
"name": "I want to be able to sit down on longer journeys",
"group": "1",
"number": "5"
}, {
"name": "During rush hour I don't want to be delayed",
"group": "1",
"number": "6"
}, {
"name": "During rush hour I want to be able to have personal space (not be crammed like a sardine)",
"group": "1",
"number": "7"
}, {
"name": "I don't want to spend more than a few dollars per trip",
"group": "1",
"number": "8"
}, {
"name": "I want to be able to track how many people are using the train",
"group": "1",
"number": "9"
}, {
"name": "I want to be able to track where people are going",
"group": "1",
"number": "10"
}, {
"name": "If I'm rushing to make a train, I don't want to be held up by having to buy a MetroCard",
"group": "1",
...
CSS
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
h3 {
color: #1ABC9C;
text-align:center;
font-style: italic;
font-size: 14px;
font-family: "Helvetica";
}
JavaScript
//Constants for the SVG
var width = 640,
height = 480;
//Set up the colour scale
var color = d3.scale.category20();
//Set up the force layout
var force = d3.layout.force()
.charge(-500)
.linkDistance(30)
.size([width, height]);
//Append a SVG to the body of the html page. Assign this SVG as an object to svg
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var node_drag = d3.behavior.drag()
.on("dragstart", dragstart)
.on("drag", dragmove)
.on("dragend", dragend);
function dragstart(d, i) {
force.stop() // stops the force auto positioning before you start dragging
}
function dragmove(d, i) {
d.px += d3.event.dx;
d.py += d3.event.dy;
d.x += d3.event.dx;
d.y += d3.event.dy;
}
function dragend(d, i) {
d.fixed = true; // of course set the node to fixed so the force doesn't include the node in its auto positioning stuff
force.resume();
}
function releasenode(d) {
d.fixed = false; // of course set the node to fixed so the force doesn't include the node in its auto positioning stuff
//force.resume();
}
//Read the data from the mis element
var mis = document.getElementById('mis').innerHTML;
graph = JSON.parse(mis);
graphRec=JSON.parse(JSON.stringify(graph)); //Add this line
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function (d) {
return d.name + "";
})
svg.call(tip);
//Creates the graph data structure out of the json data
force.nodes(graph.nodes)
.links(graph.links)
.start();
//Create all the line svgs but without locations yet
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function (d) {
return Math.sqrt(d.value);
});
//Do the same with the circles for the nodes - no
var node = svg.selectAll(".node")
.data(graph.nodes)
...