forked from ezyang/metromaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.html
240 lines (234 loc) · 7.86 KB
/
maps.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="jquery-ui.css" />
<script src="d3.v2.js"></script>
<script src="jquery-1.8.2.js"></script>
<script src="jquery-ui.js"></script>
<script src="jquery.fireEvent.js"></script> <!-- fix D3/jQuery interoperability issues -->
<script src="ultbuttons.js"></script> <!-- fix jQuery UI bug with checkbox buttons -->
<!-- our stuff -->
<script src="pforce.js"></script>
<script src="vector.js"></script>
<script src="util.js"></script>
<script src="metromap.js"></script>
<script src="slideshow.js"></script>
<script src="debug.js"></script>
<style>
body {font-family:'Times New Roman'}
#debug-container {margin:0 1em;}
#debug-container td {padding: 0 0.5em;}
#fulltext {min-width:600px;padding-left:2em;vertical-align:top;}
#svg-container {height:600px}
.ui-button {font-size:0.7em; }
.controls {height:1em}
.controls td {padding: 0 0.5em 0 0;}
.controls .ui-button span {padding:.2em .5em}
#time-controls {width:140px;text-align:right;}
#slideshow-controls {padding-top: .5em;}
#line-controls {vertical-align: top;text-align:right;padding-top:10px;}
/* See jQuery UI bug http://bugs.jqueryui.com/ticket/8827 */
.ui-buttonset .ui-button {margin:0; margin-right:-1px;}
.ui-state-hover, .ui-state-active {z-index:1;}
.text img {float:left; margin-right: 1em;}
#fulltext a {text-decoration:none; color:#000;}
.image {float: right; width:200px;margin-left:2em;}
.image h6 {margin:0;color:#999; }
.image .caption {font-size:12px;}
#svg_legend {width:20px;height:10px;margin:0;}
/* ezyang: This is BAD */
#color_blue {stroke: #1f77b4; stroke-width: 7px; fill: none;}
#color_orange {stroke: #ff7f0e; stroke-width: 7px; fill: none;}
#color_green {stroke: #2ca02c; stroke-width: 7px; fill: none;}
#color_red {stroke: #d62728; stroke-width: 7px; fill: none;}
#color_none {stroke-width:0;}
#slideshow-text h2 {margin-top:10px; margin-bottom: 7px;}
.lines label {text-align:left; width:14em;}
.img_div {width:400px; float:left; padding:0 20px 20px 0;}
.img_attr {color:grey; text-align: right; width:400px; display: block; font-family: Verdana; font-size: smaller;}
.axis path,
.axis line {
fill: none;
stroke: #CCC;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
color: #CCC;
font-size: 11px;
}
</style>
</head>
<body>
<!--<h1>Metro Maps of the News</h1>-->
<table>
<tr>
<td colspan="2" id="svg-container">
</td>
<td rowspan="3" id="fulltext">
</td>
</tr>
<tr class="controls">
<td id="slideshow-controls"></td>
<td id="time-controls"></td>
</tr>
<tr>
<td style="vertical-align:top;padding-right:1em;">
<div id="slideshow-text"></div>
</td>
<td id="line-controls"></td>
</tr>
</table>
<div id="debug-container">
<div>(Press SHIFT to delete nodes.)</div>
</div>
<script>
var metro; // for easy console access
var ss; //ditto
function initData(callback) {
d3.xml("greek2.graphml", function(doc) {
var nodetable = d3.map();
var nodes = [];
var links = [];
var format = d3.time.format("%d/%m/%Y");
// TODO: The first step is to simplify our graph by removing all
// straight line segments of the graph. The end result
// should be a new list of key nodes (nodes with adjacent edge
// count not equal to two) and edges for all of those nodes, with
// weight equal to the number of edges that they are representing.
// For example:
// A -- B -- C -- D
// reduces to:
// A -- D (with edge weight = 3)
// XXX I think with links this becomes a bit more delicate,
// since a link still wants to be represented as a polyline
// including the removed nodes. Essentially, if we have:
// A -- B -- C
// Then if I click on "B" it should be promoted to a real node,
// and if I click on the edge "A -- B", then I should insert a
// dummy node
d3.select(doc).selectAll("node").each(function () {
var e = d3.select(this);
var id = e.attr("id");
var sx = parseInt(e.select("data[key='x']").text()),
sy = parseInt(e.select("data[key='y']").text());
var rawtitle = e.select("data[key='label']").text().trim();
// XXX price would have told me to use a regex
var bits = rawtitle.split("\t");
var node = {
id: id,
label: bits[0].trim(),
date: format.parse(e.select("data[key='date']").text().trim()),
//date: format.parse(bits[1].trim()),
fixed: 0,
edges: d3.map(), // indexed by line
};
nodetable.set(id, node);
nodes.push(node);
});
// parse out the various metro lines; we'll instantiate node later
var lines = [];
var linetable = d3.map();
d3.select(doc).selectAll("key")
.filter(function() {return d3.select(this).attr("for") == "edge"})
.each(function() {
var id = d3.select(this).attr("id");
var line = { id: id, nodes: [] };
linetable.set(id, line);
lines.push(line);
});
// parse out the edges
d3.select(doc).selectAll("edge").each(function () {
var e = d3.select(this);
var n1 = nodetable.get(e.attr("source"));
var n2 = nodetable.get(e.attr("target"));
var l = {
id: e.attr("id"),
source: n1,
target: n2,
path: [],
};
e.selectAll("data").each(function() {
// XXX assuming that none of these are FALSE
var p = d3.select(this).attr("key");
l.path.push(linetable.get(p));
if (!n1.edges.has(p)) n1.edges.set(p, []);
if (!n2.edges.has(p)) n2.edges.set(p, []);
n1.edges.get(p).push(l);
n2.edges.get(p).push(l);
});
/*
if (l.path.length > 1) {
// end nodes need some special handling
n1.
}
*/
links.push(l);
});
// determine the initial stories
var initials = d3.map();
nodes.forEach(function(n) {
n.edges.forEach(function(k,v) {
var l = linetable.get(k);
if (v.length == 1 && !l.nodes.length) {
l.nodes.push(n);
}
});
});
// follow the trail (topological sort)
lines.forEach(function(l) {
var curNode = l.nodes[0];
var edges = curNode.edges.get(l.id);
var curEdge;
var first = true;
do {
curEdge = first ? edges[0] : edges[0] == curEdge ? edges[1] : edges[0];
first = false;
curNode = curEdge.source == curNode ? curEdge.target : curEdge.source;
l.nodes.push(curNode);
edges = curNode.edges.get(l.id);
} while (edges.length != 1);
});
metro
.lines(lines)
.nodes(nodes)
.links(links);
metro();
});
}
var debug = window.location.hash == "#debug";
metro = metromap(d3.select("#svg-container"), debug)
.size([1000, 600]);
function clear() {
localStorage.setItem("state", null);
initData();
}
ss = slideshow(metro);
ss(d3.select("#slideshow-controls"), d3.select("#time-controls"), d3.select("#line-controls"), d3.select("#slideshow-text"));
if (debug) {
var state = localStorage.getItem("state");
try {
var parsed = JSON.parse(state);
if (!parsed) throw "nothing";
metro.state(parsed);
metro();
metro.stop();
} catch(e) {
//throw e; // uncomment me if you want to debug a corrupt savefile
if (e == "nothing") {
clear();
}
}
debugForce(metro, d3.select("#debug-container"));
} else {
d3.select("#debug-container").style("display", "none");
d3.json("preserving.json", function(doc) {
metro.state(doc);
metro();
metro.mode(MetroMode.VIEW);
});
}
</script>
</body>
</html>