Skip to content

Instantly share code, notes, and snippets.

View mbostock's full-sized avatar
📊
Plottin’

Mike Bostock mbostock

📊
Plottin’
View GitHub Profile
@mbostock
mbostock / sample-client.js
Created January 4, 2012 01:40
Cube Metrics Client (Node.js + WebSockets)
var websocket = require("websocket");
var client = new websocket.client();
client.on("connect", function(connection) {
var remaining = 762; // based on start, stop, and step
connection.on("message", function(message) {
if (message.type === "utf8") {
if (!--remaining) connection.close();
@mbostock
mbostock / get.js
Created July 11, 2012 18:45
Read File or HTTP
var fs = require("fs"),
http = require("http");
function get(url, callback) {
if (/^http:/.test(url)) {
http.get(url, function(response) {
var body = [];
response
.on("data", function(data) { body.push(data); })
.on("end", function() { callback(null, body.join("")); })
@mbostock
mbostock / index.js
Created August 7, 2012 18:21
Google Hurdles
function key(type, code) {
var e = document.createEvent("Event");
e.initEvent(type, true, true);
e.keyCode = code;
document.getElementById("hplogo").dispatchEvent(e);
}
setInterval(function() {
key("keydown", 37);
key("keypress", 37);
@mbostock
mbostock / CSV.tmLanguage
Created August 13, 2012 22:40
CSV Syntax Definition
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>fileTypes</key>
<array>
<string>csv</string>
</array>
<key>name</key>
<string>Comma-Separated Values</string>
@mbostock
mbostock / index.js
Created August 16, 2012 16:32
Git-backed Node Blob Server
var express = require("express"),
gitteh = require("gitteh"),
path = require("path");
var server = express();
server.get("/", function(request, response) {
gitteh.openRepository(path.join(__dirname, "repository", ".git"), function(error, repository) {
if (error) return response.end(error.toString());
repository.getBlob("9b4b40c2bca67e781930105fa190b9b90235cfe5", function(error, blob) {
@mbostock
mbostock / index.js
Created August 29, 2012 19:11
Lazy Scale Domain
// Create a scale, but don't set the domain until the data is known.
var x = d3.scale.linear()
.range([0, width]);
// Load the data.
d3.json("data.json", function(data) {
// Now we can compute the domain and update the scale.
x.domain([0, d3.max(data)]);
@mbostock
mbostock / cleanup.js
Created September 9, 2012 06:30
Clean Up for Natural Earth GeoJSON
var fs = require("fs");
var roundPrecision = 1e6;
fs.readFile("/dev/stdin", "utf-8", function(error, input) {
var collection = JSON.parse(input);
switch (collection.type) {
case "FeatureCollection": return cleanupFeatureCollection(collection);
case "GeometryCollection": return cleanupGeometryCollection(collection);
default: throw "unknown type: " + collection.type;
@mbostock
mbostock / us-boundary.json
Created September 19, 2012 17:20
Shared Data
{"type":"MultiPolygon","coordinates":[[[[-155.59,20.14],[-155.58,20.13],[-155.57,20.13],[-155.56,20.14],[-155.55,20.14],[-155.53,20.14],[-155.49,20.11],[-155.45,20.11],[-155.34,20.06],[-155.21,20],[-155.17,19.97],[-155.09,19.88],[-155.08,19.87],[-155.08,19.84],[-155.08,19.77],[-155.07,19.74],[-155.06,19.74],[-155.02,19.75],[-154.99,19.74],[-154.98,19.73],[-154.98,19.72],[-154.97,19.67],[-154.96,19.65],[-154.92,19.61],[-154.87,19.59],[-154.79,19.54],[-154.8,19.52],[-154.81,19.5],[-154.82,19.48],[-154.83,19.46],[-154.91,19.42],[-154.92,19.41],[-155.01,19.33],[-155.02,19.33],[-155.05,19.32],[-155.07,19.32],[-155.1,19.3],[-155.17,19.28],[-155.19,19.28],[-155.27,19.28],[-155.29,19.27],[-155.3,19.27],[-155.32,19.25],[-155.35,19.22],[-155.37,19.22],[-155.38,19.21],[-155.4,19.2],[-155.42,19.18],[-155.44,19.17],[-155.49,19.14],[-155.5,19.14],[-155.55,19.1],[-155.55,19.08],[-155.57,19.03],[-155.61,18.97],[-155.63,18.96],[-155.64,18.95],[-155.66,18.93],[-155.67,18.93],[-155.67,18.94],[-155.69,18.96],[-155.71,18.99],[-15
@mbostock
mbostock / projection.js
Created September 29, 2012 18:16
Projection Contexts
projection.line = function(coordinates, context) {
if (!(n = coordinates.length)) return;
context = radiansAndRotateContext(projectAndTransformContext(context));
var i = 0,
n = coordinates.length,
p = coordinates[0];
context.moveTo(p[0], p[1]);
while (++i < n) context.lineTo((p = coordinates[i])[0], p[1]);
};
@mbostock
mbostock / index.js
Last active October 13, 2015 03:47
Stitching States from Counties
d3.json("us-counties.json", function(error, topology) {
if (error) throw error;
var infoByArc = {},
arcsByState = {};
topology.objects[0].geometries.forEach(function(object) {
object.arcs.forEach(object.type === "MultiPolygon"
? function(polygon) { polygon.forEach(function(ring) { ring.forEach(info); }); }
: function(ring) { ring.forEach(info); });