"use strict";
// badass visualization code goes here
}
</script>
</head>
<body>
<script>
d3.json("data/some_data.json", draw);
</script>
</body>
</html>
The D3 library is always included to give our visualizations access to the D3 methods.
The rest of the book will focus on this function, which we will always call draw. This
is a function with one argument; it is called once the data has been downloaded to
the client. It will contain the bulk of the code necessary to create the visualization.
All the JavaScript will satisfy the JSLint tool, available at http://www.jslint.com/. D3
encourages the use of the “cascade” JavaScript coding style, making it easy to write
well-formed JavaScript. The "use strict"; line instructs the browser to apply a strict
interpretation of the JavaScript rules which, by making us write cleaner code, allows
us to avoid confusing bugs.
The d3.json() function makes an HTTP GET request to a JSON file at the URL
described by its first argument and once the data has been downloaded, will then
call the function passed as the second argument. This second argument is a callback
function (which we will always call draw), which is passed, as its only parameter, the
contents of the JSON having been turned into an object or an array, whichever is
appropriate. Although D3 can read both XML and CSV, we remain constant
throughout the book and stick to JSON.
The approach taken in this book is to expose the reader to the process of building up
the visualizations. This means that the first few steps of the process can result in some
ugly, incomprehensible pages, which are subsequently styled into shape. As such, all
the CSS is detailed in the examples and tends to be explained after the elements of the
visualization have been specified.
The New York Metropolitan Transit Authority Data Set
New York is an incredibly large, incredibly dense city with a lot of people constantly
moving around. As such, it has evolved an intricate transport network, large parts of
which are managed by the Metropolitan Transit Authority (MTA). The MTA is re-
sponsible for the local trains, subways, buses, bridges, and tunnels that move over 11
million people a day through the five boroughs and beyond.
The MTA has made a large amount of data associated with the running of this network
publicly available. This has, in turn, generated a vibrant developer community that is
The New York Metropolitan Transit Authority Data Set | 3