annotate gtc/d3.js @ 124:9b57b90aea31

initial add for pics3
author paulo
date Thu, 25 Mar 2021 00:33:42 -0700
parents
children
rev   line source
paulo@89 1 !function() {
paulo@89 2 var d3 = {
paulo@89 3 version: "3.5.17"
paulo@89 4 };
paulo@89 5 var d3_arraySlice = [].slice, d3_array = function(list) {
paulo@89 6 return d3_arraySlice.call(list);
paulo@89 7 };
paulo@89 8 var d3_document = this.document;
paulo@89 9 function d3_documentElement(node) {
paulo@89 10 return node && (node.ownerDocument || node.document || node).documentElement;
paulo@89 11 }
paulo@89 12 function d3_window(node) {
paulo@89 13 return node && (node.ownerDocument && node.ownerDocument.defaultView || node.document && node || node.defaultView);
paulo@89 14 }
paulo@89 15 if (d3_document) {
paulo@89 16 try {
paulo@89 17 d3_array(d3_document.documentElement.childNodes)[0].nodeType;
paulo@89 18 } catch (e) {
paulo@89 19 d3_array = function(list) {
paulo@89 20 var i = list.length, array = new Array(i);
paulo@89 21 while (i--) array[i] = list[i];
paulo@89 22 return array;
paulo@89 23 };
paulo@89 24 }
paulo@89 25 }
paulo@89 26 if (!Date.now) Date.now = function() {
paulo@89 27 return +new Date();
paulo@89 28 };
paulo@89 29 if (d3_document) {
paulo@89 30 try {
paulo@89 31 d3_document.createElement("DIV").style.setProperty("opacity", 0, "");
paulo@89 32 } catch (error) {
paulo@89 33 var d3_element_prototype = this.Element.prototype, d3_element_setAttribute = d3_element_prototype.setAttribute, d3_element_setAttributeNS = d3_element_prototype.setAttributeNS, d3_style_prototype = this.CSSStyleDeclaration.prototype, d3_style_setProperty = d3_style_prototype.setProperty;
paulo@89 34 d3_element_prototype.setAttribute = function(name, value) {
paulo@89 35 d3_element_setAttribute.call(this, name, value + "");
paulo@89 36 };
paulo@89 37 d3_element_prototype.setAttributeNS = function(space, local, value) {
paulo@89 38 d3_element_setAttributeNS.call(this, space, local, value + "");
paulo@89 39 };
paulo@89 40 d3_style_prototype.setProperty = function(name, value, priority) {
paulo@89 41 d3_style_setProperty.call(this, name, value + "", priority);
paulo@89 42 };
paulo@89 43 }
paulo@89 44 }
paulo@89 45 d3.ascending = d3_ascending;
paulo@89 46 function d3_ascending(a, b) {
paulo@89 47 return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
paulo@89 48 }
paulo@89 49 d3.descending = function(a, b) {
paulo@89 50 return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
paulo@89 51 };
paulo@89 52 d3.min = function(array, f) {
paulo@89 53 var i = -1, n = array.length, a, b;
paulo@89 54 if (arguments.length === 1) {
paulo@89 55 while (++i < n) if ((b = array[i]) != null && b >= b) {
paulo@89 56 a = b;
paulo@89 57 break;
paulo@89 58 }
paulo@89 59 while (++i < n) if ((b = array[i]) != null && a > b) a = b;
paulo@89 60 } else {
paulo@89 61 while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
paulo@89 62 a = b;
paulo@89 63 break;
paulo@89 64 }
paulo@89 65 while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
paulo@89 66 }
paulo@89 67 return a;
paulo@89 68 };
paulo@89 69 d3.max = function(array, f) {
paulo@89 70 var i = -1, n = array.length, a, b;
paulo@89 71 if (arguments.length === 1) {
paulo@89 72 while (++i < n) if ((b = array[i]) != null && b >= b) {
paulo@89 73 a = b;
paulo@89 74 break;
paulo@89 75 }
paulo@89 76 while (++i < n) if ((b = array[i]) != null && b > a) a = b;
paulo@89 77 } else {
paulo@89 78 while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
paulo@89 79 a = b;
paulo@89 80 break;
paulo@89 81 }
paulo@89 82 while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
paulo@89 83 }
paulo@89 84 return a;
paulo@89 85 };
paulo@89 86 d3.extent = function(array, f) {
paulo@89 87 var i = -1, n = array.length, a, b, c;
paulo@89 88 if (arguments.length === 1) {
paulo@89 89 while (++i < n) if ((b = array[i]) != null && b >= b) {
paulo@89 90 a = c = b;
paulo@89 91 break;
paulo@89 92 }
paulo@89 93 while (++i < n) if ((b = array[i]) != null) {
paulo@89 94 if (a > b) a = b;
paulo@89 95 if (c < b) c = b;
paulo@89 96 }
paulo@89 97 } else {
paulo@89 98 while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
paulo@89 99 a = c = b;
paulo@89 100 break;
paulo@89 101 }
paulo@89 102 while (++i < n) if ((b = f.call(array, array[i], i)) != null) {
paulo@89 103 if (a > b) a = b;
paulo@89 104 if (c < b) c = b;
paulo@89 105 }
paulo@89 106 }
paulo@89 107 return [ a, c ];
paulo@89 108 };
paulo@89 109 function d3_number(x) {
paulo@89 110 return x === null ? NaN : +x;
paulo@89 111 }
paulo@89 112 function d3_numeric(x) {
paulo@89 113 return !isNaN(x);
paulo@89 114 }
paulo@89 115 d3.sum = function(array, f) {
paulo@89 116 var s = 0, n = array.length, a, i = -1;
paulo@89 117 if (arguments.length === 1) {
paulo@89 118 while (++i < n) if (d3_numeric(a = +array[i])) s += a;
paulo@89 119 } else {
paulo@89 120 while (++i < n) if (d3_numeric(a = +f.call(array, array[i], i))) s += a;
paulo@89 121 }
paulo@89 122 return s;
paulo@89 123 };
paulo@89 124 d3.mean = function(array, f) {
paulo@89 125 var s = 0, n = array.length, a, i = -1, j = n;
paulo@89 126 if (arguments.length === 1) {
paulo@89 127 while (++i < n) if (d3_numeric(a = d3_number(array[i]))) s += a; else --j;
paulo@89 128 } else {
paulo@89 129 while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) s += a; else --j;
paulo@89 130 }
paulo@89 131 if (j) return s / j;
paulo@89 132 };
paulo@89 133 d3.quantile = function(values, p) {
paulo@89 134 var H = (values.length - 1) * p + 1, h = Math.floor(H), v = +values[h - 1], e = H - h;
paulo@89 135 return e ? v + e * (values[h] - v) : v;
paulo@89 136 };
paulo@89 137 d3.median = function(array, f) {
paulo@89 138 var numbers = [], n = array.length, a, i = -1;
paulo@89 139 if (arguments.length === 1) {
paulo@89 140 while (++i < n) if (d3_numeric(a = d3_number(array[i]))) numbers.push(a);
paulo@89 141 } else {
paulo@89 142 while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) numbers.push(a);
paulo@89 143 }
paulo@89 144 if (numbers.length) return d3.quantile(numbers.sort(d3_ascending), .5);
paulo@89 145 };
paulo@89 146 d3.variance = function(array, f) {
paulo@89 147 var n = array.length, m = 0, a, d, s = 0, i = -1, j = 0;
paulo@89 148 if (arguments.length === 1) {
paulo@89 149 while (++i < n) {
paulo@89 150 if (d3_numeric(a = d3_number(array[i]))) {
paulo@89 151 d = a - m;
paulo@89 152 m += d / ++j;
paulo@89 153 s += d * (a - m);
paulo@89 154 }
paulo@89 155 }
paulo@89 156 } else {
paulo@89 157 while (++i < n) {
paulo@89 158 if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) {
paulo@89 159 d = a - m;
paulo@89 160 m += d / ++j;
paulo@89 161 s += d * (a - m);
paulo@89 162 }
paulo@89 163 }
paulo@89 164 }
paulo@89 165 if (j > 1) return s / (j - 1);
paulo@89 166 };
paulo@89 167 d3.deviation = function() {
paulo@89 168 var v = d3.variance.apply(this, arguments);
paulo@89 169 return v ? Math.sqrt(v) : v;
paulo@89 170 };
paulo@89 171 function d3_bisector(compare) {
paulo@89 172 return {
paulo@89 173 left: function(a, x, lo, hi) {
paulo@89 174 if (arguments.length < 3) lo = 0;
paulo@89 175 if (arguments.length < 4) hi = a.length;
paulo@89 176 while (lo < hi) {
paulo@89 177 var mid = lo + hi >>> 1;
paulo@89 178 if (compare(a[mid], x) < 0) lo = mid + 1; else hi = mid;
paulo@89 179 }
paulo@89 180 return lo;
paulo@89 181 },
paulo@89 182 right: function(a, x, lo, hi) {
paulo@89 183 if (arguments.length < 3) lo = 0;
paulo@89 184 if (arguments.length < 4) hi = a.length;
paulo@89 185 while (lo < hi) {
paulo@89 186 var mid = lo + hi >>> 1;
paulo@89 187 if (compare(a[mid], x) > 0) hi = mid; else lo = mid + 1;
paulo@89 188 }
paulo@89 189 return lo;
paulo@89 190 }
paulo@89 191 };
paulo@89 192 }
paulo@89 193 var d3_bisect = d3_bisector(d3_ascending);
paulo@89 194 d3.bisectLeft = d3_bisect.left;
paulo@89 195 d3.bisect = d3.bisectRight = d3_bisect.right;
paulo@89 196 d3.bisector = function(f) {
paulo@89 197 return d3_bisector(f.length === 1 ? function(d, x) {
paulo@89 198 return d3_ascending(f(d), x);
paulo@89 199 } : f);
paulo@89 200 };
paulo@89 201 d3.shuffle = function(array, i0, i1) {
paulo@89 202 if ((m = arguments.length) < 3) {
paulo@89 203 i1 = array.length;
paulo@89 204 if (m < 2) i0 = 0;
paulo@89 205 }
paulo@89 206 var m = i1 - i0, t, i;
paulo@89 207 while (m) {
paulo@89 208 i = Math.random() * m-- | 0;
paulo@89 209 t = array[m + i0], array[m + i0] = array[i + i0], array[i + i0] = t;
paulo@89 210 }
paulo@89 211 return array;
paulo@89 212 };
paulo@89 213 d3.permute = function(array, indexes) {
paulo@89 214 var i = indexes.length, permutes = new Array(i);
paulo@89 215 while (i--) permutes[i] = array[indexes[i]];
paulo@89 216 return permutes;
paulo@89 217 };
paulo@89 218 d3.pairs = function(array) {
paulo@89 219 var i = 0, n = array.length - 1, p0, p1 = array[0], pairs = new Array(n < 0 ? 0 : n);
paulo@89 220 while (i < n) pairs[i] = [ p0 = p1, p1 = array[++i] ];
paulo@89 221 return pairs;
paulo@89 222 };
paulo@89 223 d3.transpose = function(matrix) {
paulo@89 224 if (!(n = matrix.length)) return [];
paulo@89 225 for (var i = -1, m = d3.min(matrix, d3_transposeLength), transpose = new Array(m); ++i < m; ) {
paulo@89 226 for (var j = -1, n, row = transpose[i] = new Array(n); ++j < n; ) {
paulo@89 227 row[j] = matrix[j][i];
paulo@89 228 }
paulo@89 229 }
paulo@89 230 return transpose;
paulo@89 231 };
paulo@89 232 function d3_transposeLength(d) {
paulo@89 233 return d.length;
paulo@89 234 }
paulo@89 235 d3.zip = function() {
paulo@89 236 return d3.transpose(arguments);
paulo@89 237 };
paulo@89 238 d3.keys = function(map) {
paulo@89 239 var keys = [];
paulo@89 240 for (var key in map) keys.push(key);
paulo@89 241 return keys;
paulo@89 242 };
paulo@89 243 d3.values = function(map) {
paulo@89 244 var values = [];
paulo@89 245 for (var key in map) values.push(map[key]);
paulo@89 246 return values;
paulo@89 247 };
paulo@89 248 d3.entries = function(map) {
paulo@89 249 var entries = [];
paulo@89 250 for (var key in map) entries.push({
paulo@89 251 key: key,
paulo@89 252 value: map[key]
paulo@89 253 });
paulo@89 254 return entries;
paulo@89 255 };
paulo@89 256 d3.merge = function(arrays) {
paulo@89 257 var n = arrays.length, m, i = -1, j = 0, merged, array;
paulo@89 258 while (++i < n) j += arrays[i].length;
paulo@89 259 merged = new Array(j);
paulo@89 260 while (--n >= 0) {
paulo@89 261 array = arrays[n];
paulo@89 262 m = array.length;
paulo@89 263 while (--m >= 0) {
paulo@89 264 merged[--j] = array[m];
paulo@89 265 }
paulo@89 266 }
paulo@89 267 return merged;
paulo@89 268 };
paulo@89 269 var abs = Math.abs;
paulo@89 270 d3.range = function(start, stop, step) {
paulo@89 271 if (arguments.length < 3) {
paulo@89 272 step = 1;
paulo@89 273 if (arguments.length < 2) {
paulo@89 274 stop = start;
paulo@89 275 start = 0;
paulo@89 276 }
paulo@89 277 }
paulo@89 278 if ((stop - start) / step === Infinity) throw new Error("infinite range");
paulo@89 279 var range = [], k = d3_range_integerScale(abs(step)), i = -1, j;
paulo@89 280 start *= k, stop *= k, step *= k;
paulo@89 281 if (step < 0) while ((j = start + step * ++i) > stop) range.push(j / k); else while ((j = start + step * ++i) < stop) range.push(j / k);
paulo@89 282 return range;
paulo@89 283 };
paulo@89 284 function d3_range_integerScale(x) {
paulo@89 285 var k = 1;
paulo@89 286 while (x * k % 1) k *= 10;
paulo@89 287 return k;
paulo@89 288 }
paulo@89 289 function d3_class(ctor, properties) {
paulo@89 290 for (var key in properties) {
paulo@89 291 Object.defineProperty(ctor.prototype, key, {
paulo@89 292 value: properties[key],
paulo@89 293 enumerable: false
paulo@89 294 });
paulo@89 295 }
paulo@89 296 }
paulo@89 297 d3.map = function(object, f) {
paulo@89 298 var map = new d3_Map();
paulo@89 299 if (object instanceof d3_Map) {
paulo@89 300 object.forEach(function(key, value) {
paulo@89 301 map.set(key, value);
paulo@89 302 });
paulo@89 303 } else if (Array.isArray(object)) {
paulo@89 304 var i = -1, n = object.length, o;
paulo@89 305 if (arguments.length === 1) while (++i < n) map.set(i, object[i]); else while (++i < n) map.set(f.call(object, o = object[i], i), o);
paulo@89 306 } else {
paulo@89 307 for (var key in object) map.set(key, object[key]);
paulo@89 308 }
paulo@89 309 return map;
paulo@89 310 };
paulo@89 311 function d3_Map() {
paulo@89 312 this._ = Object.create(null);
paulo@89 313 }
paulo@89 314 var d3_map_proto = "__proto__", d3_map_zero = "\x00";
paulo@89 315 d3_class(d3_Map, {
paulo@89 316 has: d3_map_has,
paulo@89 317 get: function(key) {
paulo@89 318 return this._[d3_map_escape(key)];
paulo@89 319 },
paulo@89 320 set: function(key, value) {
paulo@89 321 return this._[d3_map_escape(key)] = value;
paulo@89 322 },
paulo@89 323 remove: d3_map_remove,
paulo@89 324 keys: d3_map_keys,
paulo@89 325 values: function() {
paulo@89 326 var values = [];
paulo@89 327 for (var key in this._) values.push(this._[key]);
paulo@89 328 return values;
paulo@89 329 },
paulo@89 330 entries: function() {
paulo@89 331 var entries = [];
paulo@89 332 for (var key in this._) entries.push({
paulo@89 333 key: d3_map_unescape(key),
paulo@89 334 value: this._[key]
paulo@89 335 });
paulo@89 336 return entries;
paulo@89 337 },
paulo@89 338 size: d3_map_size,
paulo@89 339 empty: d3_map_empty,
paulo@89 340 forEach: function(f) {
paulo@89 341 for (var key in this._) f.call(this, d3_map_unescape(key), this._[key]);
paulo@89 342 }
paulo@89 343 });
paulo@89 344 function d3_map_escape(key) {
paulo@89 345 return (key += "") === d3_map_proto || key[0] === d3_map_zero ? d3_map_zero + key : key;
paulo@89 346 }
paulo@89 347 function d3_map_unescape(key) {
paulo@89 348 return (key += "")[0] === d3_map_zero ? key.slice(1) : key;
paulo@89 349 }
paulo@89 350 function d3_map_has(key) {
paulo@89 351 return d3_map_escape(key) in this._;
paulo@89 352 }
paulo@89 353 function d3_map_remove(key) {
paulo@89 354 return (key = d3_map_escape(key)) in this._ && delete this._[key];
paulo@89 355 }
paulo@89 356 function d3_map_keys() {
paulo@89 357 var keys = [];
paulo@89 358 for (var key in this._) keys.push(d3_map_unescape(key));
paulo@89 359 return keys;
paulo@89 360 }
paulo@89 361 function d3_map_size() {
paulo@89 362 var size = 0;
paulo@89 363 for (var key in this._) ++size;
paulo@89 364 return size;
paulo@89 365 }
paulo@89 366 function d3_map_empty() {
paulo@89 367 for (var key in this._) return false;
paulo@89 368 return true;
paulo@89 369 }
paulo@89 370 d3.nest = function() {
paulo@89 371 var nest = {}, keys = [], sortKeys = [], sortValues, rollup;
paulo@89 372 function map(mapType, array, depth) {
paulo@89 373 if (depth >= keys.length) return rollup ? rollup.call(nest, array) : sortValues ? array.sort(sortValues) : array;
paulo@89 374 var i = -1, n = array.length, key = keys[depth++], keyValue, object, setter, valuesByKey = new d3_Map(), values;
paulo@89 375 while (++i < n) {
paulo@89 376 if (values = valuesByKey.get(keyValue = key(object = array[i]))) {
paulo@89 377 values.push(object);
paulo@89 378 } else {
paulo@89 379 valuesByKey.set(keyValue, [ object ]);
paulo@89 380 }
paulo@89 381 }
paulo@89 382 if (mapType) {
paulo@89 383 object = mapType();
paulo@89 384 setter = function(keyValue, values) {
paulo@89 385 object.set(keyValue, map(mapType, values, depth));
paulo@89 386 };
paulo@89 387 } else {
paulo@89 388 object = {};
paulo@89 389 setter = function(keyValue, values) {
paulo@89 390 object[keyValue] = map(mapType, values, depth);
paulo@89 391 };
paulo@89 392 }
paulo@89 393 valuesByKey.forEach(setter);
paulo@89 394 return object;
paulo@89 395 }
paulo@89 396 function entries(map, depth) {
paulo@89 397 if (depth >= keys.length) return map;
paulo@89 398 var array = [], sortKey = sortKeys[depth++];
paulo@89 399 map.forEach(function(key, keyMap) {
paulo@89 400 array.push({
paulo@89 401 key: key,
paulo@89 402 values: entries(keyMap, depth)
paulo@89 403 });
paulo@89 404 });
paulo@89 405 return sortKey ? array.sort(function(a, b) {
paulo@89 406 return sortKey(a.key, b.key);
paulo@89 407 }) : array;
paulo@89 408 }
paulo@89 409 nest.map = function(array, mapType) {
paulo@89 410 return map(mapType, array, 0);
paulo@89 411 };
paulo@89 412 nest.entries = function(array) {
paulo@89 413 return entries(map(d3.map, array, 0), 0);
paulo@89 414 };
paulo@89 415 nest.key = function(d) {
paulo@89 416 keys.push(d);
paulo@89 417 return nest;
paulo@89 418 };
paulo@89 419 nest.sortKeys = function(order) {
paulo@89 420 sortKeys[keys.length - 1] = order;
paulo@89 421 return nest;
paulo@89 422 };
paulo@89 423 nest.sortValues = function(order) {
paulo@89 424 sortValues = order;
paulo@89 425 return nest;
paulo@89 426 };
paulo@89 427 nest.rollup = function(f) {
paulo@89 428 rollup = f;
paulo@89 429 return nest;
paulo@89 430 };
paulo@89 431 return nest;
paulo@89 432 };
paulo@89 433 d3.set = function(array) {
paulo@89 434 var set = new d3_Set();
paulo@89 435 if (array) for (var i = 0, n = array.length; i < n; ++i) set.add(array[i]);
paulo@89 436 return set;
paulo@89 437 };
paulo@89 438 function d3_Set() {
paulo@89 439 this._ = Object.create(null);
paulo@89 440 }
paulo@89 441 d3_class(d3_Set, {
paulo@89 442 has: d3_map_has,
paulo@89 443 add: function(key) {
paulo@89 444 this._[d3_map_escape(key += "")] = true;
paulo@89 445 return key;
paulo@89 446 },
paulo@89 447 remove: d3_map_remove,
paulo@89 448 values: d3_map_keys,
paulo@89 449 size: d3_map_size,
paulo@89 450 empty: d3_map_empty,
paulo@89 451 forEach: function(f) {
paulo@89 452 for (var key in this._) f.call(this, d3_map_unescape(key));
paulo@89 453 }
paulo@89 454 });
paulo@89 455 d3.behavior = {};
paulo@89 456 function d3_identity(d) {
paulo@89 457 return d;
paulo@89 458 }
paulo@89 459 d3.rebind = function(target, source) {
paulo@89 460 var i = 1, n = arguments.length, method;
paulo@89 461 while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]);
paulo@89 462 return target;
paulo@89 463 };
paulo@89 464 function d3_rebind(target, source, method) {
paulo@89 465 return function() {
paulo@89 466 var value = method.apply(source, arguments);
paulo@89 467 return value === source ? target : value;
paulo@89 468 };
paulo@89 469 }
paulo@89 470 function d3_vendorSymbol(object, name) {
paulo@89 471 if (name in object) return name;
paulo@89 472 name = name.charAt(0).toUpperCase() + name.slice(1);
paulo@89 473 for (var i = 0, n = d3_vendorPrefixes.length; i < n; ++i) {
paulo@89 474 var prefixName = d3_vendorPrefixes[i] + name;
paulo@89 475 if (prefixName in object) return prefixName;
paulo@89 476 }
paulo@89 477 }
paulo@89 478 var d3_vendorPrefixes = [ "webkit", "ms", "moz", "Moz", "o", "O" ];
paulo@89 479 function d3_noop() {}
paulo@89 480 d3.dispatch = function() {
paulo@89 481 var dispatch = new d3_dispatch(), i = -1, n = arguments.length;
paulo@89 482 while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch);
paulo@89 483 return dispatch;
paulo@89 484 };
paulo@89 485 function d3_dispatch() {}
paulo@89 486 d3_dispatch.prototype.on = function(type, listener) {
paulo@89 487 var i = type.indexOf("."), name = "";
paulo@89 488 if (i >= 0) {
paulo@89 489 name = type.slice(i + 1);
paulo@89 490 type = type.slice(0, i);
paulo@89 491 }
paulo@89 492 if (type) return arguments.length < 2 ? this[type].on(name) : this[type].on(name, listener);
paulo@89 493 if (arguments.length === 2) {
paulo@89 494 if (listener == null) for (type in this) {
paulo@89 495 if (this.hasOwnProperty(type)) this[type].on(name, null);
paulo@89 496 }
paulo@89 497 return this;
paulo@89 498 }
paulo@89 499 };
paulo@89 500 function d3_dispatch_event(dispatch) {
paulo@89 501 var listeners = [], listenerByName = new d3_Map();
paulo@89 502 function event() {
paulo@89 503 var z = listeners, i = -1, n = z.length, l;
paulo@89 504 while (++i < n) if (l = z[i].on) l.apply(this, arguments);
paulo@89 505 return dispatch;
paulo@89 506 }
paulo@89 507 event.on = function(name, listener) {
paulo@89 508 var l = listenerByName.get(name), i;
paulo@89 509 if (arguments.length < 2) return l && l.on;
paulo@89 510 if (l) {
paulo@89 511 l.on = null;
paulo@89 512 listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1));
paulo@89 513 listenerByName.remove(name);
paulo@89 514 }
paulo@89 515 if (listener) listeners.push(listenerByName.set(name, {
paulo@89 516 on: listener
paulo@89 517 }));
paulo@89 518 return dispatch;
paulo@89 519 };
paulo@89 520 return event;
paulo@89 521 }
paulo@89 522 d3.event = null;
paulo@89 523 function d3_eventPreventDefault() {
paulo@89 524 d3.event.preventDefault();
paulo@89 525 }
paulo@89 526 function d3_eventSource() {
paulo@89 527 var e = d3.event, s;
paulo@89 528 while (s = e.sourceEvent) e = s;
paulo@89 529 return e;
paulo@89 530 }
paulo@89 531 function d3_eventDispatch(target) {
paulo@89 532 var dispatch = new d3_dispatch(), i = 0, n = arguments.length;
paulo@89 533 while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch);
paulo@89 534 dispatch.of = function(thiz, argumentz) {
paulo@89 535 return function(e1) {
paulo@89 536 try {
paulo@89 537 var e0 = e1.sourceEvent = d3.event;
paulo@89 538 e1.target = target;
paulo@89 539 d3.event = e1;
paulo@89 540 dispatch[e1.type].apply(thiz, argumentz);
paulo@89 541 } finally {
paulo@89 542 d3.event = e0;
paulo@89 543 }
paulo@89 544 };
paulo@89 545 };
paulo@89 546 return dispatch;
paulo@89 547 }
paulo@89 548 d3.requote = function(s) {
paulo@89 549 return s.replace(d3_requote_re, "\\$&");
paulo@89 550 };
paulo@89 551 var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;
paulo@89 552 var d3_subclass = {}.__proto__ ? function(object, prototype) {
paulo@89 553 object.__proto__ = prototype;
paulo@89 554 } : function(object, prototype) {
paulo@89 555 for (var property in prototype) object[property] = prototype[property];
paulo@89 556 };
paulo@89 557 function d3_selection(groups) {
paulo@89 558 d3_subclass(groups, d3_selectionPrototype);
paulo@89 559 return groups;
paulo@89 560 }
paulo@89 561 var d3_select = function(s, n) {
paulo@89 562 return n.querySelector(s);
paulo@89 563 }, d3_selectAll = function(s, n) {
paulo@89 564 return n.querySelectorAll(s);
paulo@89 565 }, d3_selectMatches = function(n, s) {
paulo@89 566 var d3_selectMatcher = n.matches || n[d3_vendorSymbol(n, "matchesSelector")];
paulo@89 567 d3_selectMatches = function(n, s) {
paulo@89 568 return d3_selectMatcher.call(n, s);
paulo@89 569 };
paulo@89 570 return d3_selectMatches(n, s);
paulo@89 571 };
paulo@89 572 if (typeof Sizzle === "function") {
paulo@89 573 d3_select = function(s, n) {
paulo@89 574 return Sizzle(s, n)[0] || null;
paulo@89 575 };
paulo@89 576 d3_selectAll = Sizzle;
paulo@89 577 d3_selectMatches = Sizzle.matchesSelector;
paulo@89 578 }
paulo@89 579 d3.selection = function() {
paulo@89 580 return d3.select(d3_document.documentElement);
paulo@89 581 };
paulo@89 582 var d3_selectionPrototype = d3.selection.prototype = [];
paulo@89 583 d3_selectionPrototype.select = function(selector) {
paulo@89 584 var subgroups = [], subgroup, subnode, group, node;
paulo@89 585 selector = d3_selection_selector(selector);
paulo@89 586 for (var j = -1, m = this.length; ++j < m; ) {
paulo@89 587 subgroups.push(subgroup = []);
paulo@89 588 subgroup.parentNode = (group = this[j]).parentNode;
paulo@89 589 for (var i = -1, n = group.length; ++i < n; ) {
paulo@89 590 if (node = group[i]) {
paulo@89 591 subgroup.push(subnode = selector.call(node, node.__data__, i, j));
paulo@89 592 if (subnode && "__data__" in node) subnode.__data__ = node.__data__;
paulo@89 593 } else {
paulo@89 594 subgroup.push(null);
paulo@89 595 }
paulo@89 596 }
paulo@89 597 }
paulo@89 598 return d3_selection(subgroups);
paulo@89 599 };
paulo@89 600 function d3_selection_selector(selector) {
paulo@89 601 return typeof selector === "function" ? selector : function() {
paulo@89 602 return d3_select(selector, this);
paulo@89 603 };
paulo@89 604 }
paulo@89 605 d3_selectionPrototype.selectAll = function(selector) {
paulo@89 606 var subgroups = [], subgroup, node;
paulo@89 607 selector = d3_selection_selectorAll(selector);
paulo@89 608 for (var j = -1, m = this.length; ++j < m; ) {
paulo@89 609 for (var group = this[j], i = -1, n = group.length; ++i < n; ) {
paulo@89 610 if (node = group[i]) {
paulo@89 611 subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i, j)));
paulo@89 612 subgroup.parentNode = node;
paulo@89 613 }
paulo@89 614 }
paulo@89 615 }
paulo@89 616 return d3_selection(subgroups);
paulo@89 617 };
paulo@89 618 function d3_selection_selectorAll(selector) {
paulo@89 619 return typeof selector === "function" ? selector : function() {
paulo@89 620 return d3_selectAll(selector, this);
paulo@89 621 };
paulo@89 622 }
paulo@89 623 var d3_nsXhtml = "http://www.w3.org/1999/xhtml";
paulo@89 624 var d3_nsPrefix = {
paulo@89 625 svg: "http://www.w3.org/2000/svg",
paulo@89 626 xhtml: d3_nsXhtml,
paulo@89 627 xlink: "http://www.w3.org/1999/xlink",
paulo@89 628 xml: "http://www.w3.org/XML/1998/namespace",
paulo@89 629 xmlns: "http://www.w3.org/2000/xmlns/"
paulo@89 630 };
paulo@89 631 d3.ns = {
paulo@89 632 prefix: d3_nsPrefix,
paulo@89 633 qualify: function(name) {
paulo@89 634 var i = name.indexOf(":"), prefix = name;
paulo@89 635 if (i >= 0 && (prefix = name.slice(0, i)) !== "xmlns") name = name.slice(i + 1);
paulo@89 636 return d3_nsPrefix.hasOwnProperty(prefix) ? {
paulo@89 637 space: d3_nsPrefix[prefix],
paulo@89 638 local: name
paulo@89 639 } : name;
paulo@89 640 }
paulo@89 641 };
paulo@89 642 d3_selectionPrototype.attr = function(name, value) {
paulo@89 643 if (arguments.length < 2) {
paulo@89 644 if (typeof name === "string") {
paulo@89 645 var node = this.node();
paulo@89 646 name = d3.ns.qualify(name);
paulo@89 647 return name.local ? node.getAttributeNS(name.space, name.local) : node.getAttribute(name);
paulo@89 648 }
paulo@89 649 for (value in name) this.each(d3_selection_attr(value, name[value]));
paulo@89 650 return this;
paulo@89 651 }
paulo@89 652 return this.each(d3_selection_attr(name, value));
paulo@89 653 };
paulo@89 654 function d3_selection_attr(name, value) {
paulo@89 655 name = d3.ns.qualify(name);
paulo@89 656 function attrNull() {
paulo@89 657 this.removeAttribute(name);
paulo@89 658 }
paulo@89 659 function attrNullNS() {
paulo@89 660 this.removeAttributeNS(name.space, name.local);
paulo@89 661 }
paulo@89 662 function attrConstant() {
paulo@89 663 this.setAttribute(name, value);
paulo@89 664 }
paulo@89 665 function attrConstantNS() {
paulo@89 666 this.setAttributeNS(name.space, name.local, value);
paulo@89 667 }
paulo@89 668 function attrFunction() {
paulo@89 669 var x = value.apply(this, arguments);
paulo@89 670 if (x == null) this.removeAttribute(name); else this.setAttribute(name, x);
paulo@89 671 }
paulo@89 672 function attrFunctionNS() {
paulo@89 673 var x = value.apply(this, arguments);
paulo@89 674 if (x == null) this.removeAttributeNS(name.space, name.local); else this.setAttributeNS(name.space, name.local, x);
paulo@89 675 }
paulo@89 676 return value == null ? name.local ? attrNullNS : attrNull : typeof value === "function" ? name.local ? attrFunctionNS : attrFunction : name.local ? attrConstantNS : attrConstant;
paulo@89 677 }
paulo@89 678 function d3_collapse(s) {
paulo@89 679 return s.trim().replace(/\s+/g, " ");
paulo@89 680 }
paulo@89 681 d3_selectionPrototype.classed = function(name, value) {
paulo@89 682 if (arguments.length < 2) {
paulo@89 683 if (typeof name === "string") {
paulo@89 684 var node = this.node(), n = (name = d3_selection_classes(name)).length, i = -1;
paulo@89 685 if (value = node.classList) {
paulo@89 686 while (++i < n) if (!value.contains(name[i])) return false;
paulo@89 687 } else {
paulo@89 688 value = node.getAttribute("class");
paulo@89 689 while (++i < n) if (!d3_selection_classedRe(name[i]).test(value)) return false;
paulo@89 690 }
paulo@89 691 return true;
paulo@89 692 }
paulo@89 693 for (value in name) this.each(d3_selection_classed(value, name[value]));
paulo@89 694 return this;
paulo@89 695 }
paulo@89 696 return this.each(d3_selection_classed(name, value));
paulo@89 697 };
paulo@89 698 function d3_selection_classedRe(name) {
paulo@89 699 return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g");
paulo@89 700 }
paulo@89 701 function d3_selection_classes(name) {
paulo@89 702 return (name + "").trim().split(/^|\s+/);
paulo@89 703 }
paulo@89 704 function d3_selection_classed(name, value) {
paulo@89 705 name = d3_selection_classes(name).map(d3_selection_classedName);
paulo@89 706 var n = name.length;
paulo@89 707 function classedConstant() {
paulo@89 708 var i = -1;
paulo@89 709 while (++i < n) name[i](this, value);
paulo@89 710 }
paulo@89 711 function classedFunction() {
paulo@89 712 var i = -1, x = value.apply(this, arguments);
paulo@89 713 while (++i < n) name[i](this, x);
paulo@89 714 }
paulo@89 715 return typeof value === "function" ? classedFunction : classedConstant;
paulo@89 716 }
paulo@89 717 function d3_selection_classedName(name) {
paulo@89 718 var re = d3_selection_classedRe(name);
paulo@89 719 return function(node, value) {
paulo@89 720 if (c = node.classList) return value ? c.add(name) : c.remove(name);
paulo@89 721 var c = node.getAttribute("class") || "";
paulo@89 722 if (value) {
paulo@89 723 re.lastIndex = 0;
paulo@89 724 if (!re.test(c)) node.setAttribute("class", d3_collapse(c + " " + name));
paulo@89 725 } else {
paulo@89 726 node.setAttribute("class", d3_collapse(c.replace(re, " ")));
paulo@89 727 }
paulo@89 728 };
paulo@89 729 }
paulo@89 730 d3_selectionPrototype.style = function(name, value, priority) {
paulo@89 731 var n = arguments.length;
paulo@89 732 if (n < 3) {
paulo@89 733 if (typeof name !== "string") {
paulo@89 734 if (n < 2) value = "";
paulo@89 735 for (priority in name) this.each(d3_selection_style(priority, name[priority], value));
paulo@89 736 return this;
paulo@89 737 }
paulo@89 738 if (n < 2) {
paulo@89 739 var node = this.node();
paulo@89 740 return d3_window(node).getComputedStyle(node, null).getPropertyValue(name);
paulo@89 741 }
paulo@89 742 priority = "";
paulo@89 743 }
paulo@89 744 return this.each(d3_selection_style(name, value, priority));
paulo@89 745 };
paulo@89 746 function d3_selection_style(name, value, priority) {
paulo@89 747 function styleNull() {
paulo@89 748 this.style.removeProperty(name);
paulo@89 749 }
paulo@89 750 function styleConstant() {
paulo@89 751 this.style.setProperty(name, value, priority);
paulo@89 752 }
paulo@89 753 function styleFunction() {
paulo@89 754 var x = value.apply(this, arguments);
paulo@89 755 if (x == null) this.style.removeProperty(name); else this.style.setProperty(name, x, priority);
paulo@89 756 }
paulo@89 757 return value == null ? styleNull : typeof value === "function" ? styleFunction : styleConstant;
paulo@89 758 }
paulo@89 759 d3_selectionPrototype.property = function(name, value) {
paulo@89 760 if (arguments.length < 2) {
paulo@89 761 if (typeof name === "string") return this.node()[name];
paulo@89 762 for (value in name) this.each(d3_selection_property(value, name[value]));
paulo@89 763 return this;
paulo@89 764 }
paulo@89 765 return this.each(d3_selection_property(name, value));
paulo@89 766 };
paulo@89 767 function d3_selection_property(name, value) {
paulo@89 768 function propertyNull() {
paulo@89 769 delete this[name];
paulo@89 770 }
paulo@89 771 function propertyConstant() {
paulo@89 772 this[name] = value;
paulo@89 773 }
paulo@89 774 function propertyFunction() {
paulo@89 775 var x = value.apply(this, arguments);
paulo@89 776 if (x == null) delete this[name]; else this[name] = x;
paulo@89 777 }
paulo@89 778 return value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant;
paulo@89 779 }
paulo@89 780 d3_selectionPrototype.text = function(value) {
paulo@89 781 return arguments.length ? this.each(typeof value === "function" ? function() {
paulo@89 782 var v = value.apply(this, arguments);
paulo@89 783 this.textContent = v == null ? "" : v;
paulo@89 784 } : value == null ? function() {
paulo@89 785 this.textContent = "";
paulo@89 786 } : function() {
paulo@89 787 this.textContent = value;
paulo@89 788 }) : this.node().textContent;
paulo@89 789 };
paulo@89 790 d3_selectionPrototype.html = function(value) {
paulo@89 791 return arguments.length ? this.each(typeof value === "function" ? function() {
paulo@89 792 var v = value.apply(this, arguments);
paulo@89 793 this.innerHTML = v == null ? "" : v;
paulo@89 794 } : value == null ? function() {
paulo@89 795 this.innerHTML = "";
paulo@89 796 } : function() {
paulo@89 797 this.innerHTML = value;
paulo@89 798 }) : this.node().innerHTML;
paulo@89 799 };
paulo@89 800 d3_selectionPrototype.append = function(name) {
paulo@89 801 name = d3_selection_creator(name);
paulo@89 802 return this.select(function() {
paulo@89 803 return this.appendChild(name.apply(this, arguments));
paulo@89 804 });
paulo@89 805 };
paulo@89 806 function d3_selection_creator(name) {
paulo@89 807 function create() {
paulo@89 808 var document = this.ownerDocument, namespace = this.namespaceURI;
paulo@89 809 return namespace === d3_nsXhtml && document.documentElement.namespaceURI === d3_nsXhtml ? document.createElement(name) : document.createElementNS(namespace, name);
paulo@89 810 }
paulo@89 811 function createNS() {
paulo@89 812 return this.ownerDocument.createElementNS(name.space, name.local);
paulo@89 813 }
paulo@89 814 return typeof name === "function" ? name : (name = d3.ns.qualify(name)).local ? createNS : create;
paulo@89 815 }
paulo@89 816 d3_selectionPrototype.insert = function(name, before) {
paulo@89 817 name = d3_selection_creator(name);
paulo@89 818 before = d3_selection_selector(before);
paulo@89 819 return this.select(function() {
paulo@89 820 return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments) || null);
paulo@89 821 });
paulo@89 822 };
paulo@89 823 d3_selectionPrototype.remove = function() {
paulo@89 824 return this.each(d3_selectionRemove);
paulo@89 825 };
paulo@89 826 function d3_selectionRemove() {
paulo@89 827 var parent = this.parentNode;
paulo@89 828 if (parent) parent.removeChild(this);
paulo@89 829 }
paulo@89 830 d3_selectionPrototype.data = function(value, key) {
paulo@89 831 var i = -1, n = this.length, group, node;
paulo@89 832 if (!arguments.length) {
paulo@89 833 value = new Array(n = (group = this[0]).length);
paulo@89 834 while (++i < n) {
paulo@89 835 if (node = group[i]) {
paulo@89 836 value[i] = node.__data__;
paulo@89 837 }
paulo@89 838 }
paulo@89 839 return value;
paulo@89 840 }
paulo@89 841 function bind(group, groupData) {
paulo@89 842 var i, n = group.length, m = groupData.length, n0 = Math.min(n, m), updateNodes = new Array(m), enterNodes = new Array(m), exitNodes = new Array(n), node, nodeData;
paulo@89 843 if (key) {
paulo@89 844 var nodeByKeyValue = new d3_Map(), keyValues = new Array(n), keyValue;
paulo@89 845 for (i = -1; ++i < n; ) {
paulo@89 846 if (node = group[i]) {
paulo@89 847 if (nodeByKeyValue.has(keyValue = key.call(node, node.__data__, i))) {
paulo@89 848 exitNodes[i] = node;
paulo@89 849 } else {
paulo@89 850 nodeByKeyValue.set(keyValue, node);
paulo@89 851 }
paulo@89 852 keyValues[i] = keyValue;
paulo@89 853 }
paulo@89 854 }
paulo@89 855 for (i = -1; ++i < m; ) {
paulo@89 856 if (!(node = nodeByKeyValue.get(keyValue = key.call(groupData, nodeData = groupData[i], i)))) {
paulo@89 857 enterNodes[i] = d3_selection_dataNode(nodeData);
paulo@89 858 } else if (node !== true) {
paulo@89 859 updateNodes[i] = node;
paulo@89 860 node.__data__ = nodeData;
paulo@89 861 }
paulo@89 862 nodeByKeyValue.set(keyValue, true);
paulo@89 863 }
paulo@89 864 for (i = -1; ++i < n; ) {
paulo@89 865 if (i in keyValues && nodeByKeyValue.get(keyValues[i]) !== true) {
paulo@89 866 exitNodes[i] = group[i];
paulo@89 867 }
paulo@89 868 }
paulo@89 869 } else {
paulo@89 870 for (i = -1; ++i < n0; ) {
paulo@89 871 node = group[i];
paulo@89 872 nodeData = groupData[i];
paulo@89 873 if (node) {
paulo@89 874 node.__data__ = nodeData;
paulo@89 875 updateNodes[i] = node;
paulo@89 876 } else {
paulo@89 877 enterNodes[i] = d3_selection_dataNode(nodeData);
paulo@89 878 }
paulo@89 879 }
paulo@89 880 for (;i < m; ++i) {
paulo@89 881 enterNodes[i] = d3_selection_dataNode(groupData[i]);
paulo@89 882 }
paulo@89 883 for (;i < n; ++i) {
paulo@89 884 exitNodes[i] = group[i];
paulo@89 885 }
paulo@89 886 }
paulo@89 887 enterNodes.update = updateNodes;
paulo@89 888 enterNodes.parentNode = updateNodes.parentNode = exitNodes.parentNode = group.parentNode;
paulo@89 889 enter.push(enterNodes);
paulo@89 890 update.push(updateNodes);
paulo@89 891 exit.push(exitNodes);
paulo@89 892 }
paulo@89 893 var enter = d3_selection_enter([]), update = d3_selection([]), exit = d3_selection([]);
paulo@89 894 if (typeof value === "function") {
paulo@89 895 while (++i < n) {
paulo@89 896 bind(group = this[i], value.call(group, group.parentNode.__data__, i));
paulo@89 897 }
paulo@89 898 } else {
paulo@89 899 while (++i < n) {
paulo@89 900 bind(group = this[i], value);
paulo@89 901 }
paulo@89 902 }
paulo@89 903 update.enter = function() {
paulo@89 904 return enter;
paulo@89 905 };
paulo@89 906 update.exit = function() {
paulo@89 907 return exit;
paulo@89 908 };
paulo@89 909 return update;
paulo@89 910 };
paulo@89 911 function d3_selection_dataNode(data) {
paulo@89 912 return {
paulo@89 913 __data__: data
paulo@89 914 };
paulo@89 915 }
paulo@89 916 d3_selectionPrototype.datum = function(value) {
paulo@89 917 return arguments.length ? this.property("__data__", value) : this.property("__data__");
paulo@89 918 };
paulo@89 919 d3_selectionPrototype.filter = function(filter) {
paulo@89 920 var subgroups = [], subgroup, group, node;
paulo@89 921 if (typeof filter !== "function") filter = d3_selection_filter(filter);
paulo@89 922 for (var j = 0, m = this.length; j < m; j++) {
paulo@89 923 subgroups.push(subgroup = []);
paulo@89 924 subgroup.parentNode = (group = this[j]).parentNode;
paulo@89 925 for (var i = 0, n = group.length; i < n; i++) {
paulo@89 926 if ((node = group[i]) && filter.call(node, node.__data__, i, j)) {
paulo@89 927 subgroup.push(node);
paulo@89 928 }
paulo@89 929 }
paulo@89 930 }
paulo@89 931 return d3_selection(subgroups);
paulo@89 932 };
paulo@89 933 function d3_selection_filter(selector) {
paulo@89 934 return function() {
paulo@89 935 return d3_selectMatches(this, selector);
paulo@89 936 };
paulo@89 937 }
paulo@89 938 d3_selectionPrototype.order = function() {
paulo@89 939 for (var j = -1, m = this.length; ++j < m; ) {
paulo@89 940 for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0; ) {
paulo@89 941 if (node = group[i]) {
paulo@89 942 if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next);
paulo@89 943 next = node;
paulo@89 944 }
paulo@89 945 }
paulo@89 946 }
paulo@89 947 return this;
paulo@89 948 };
paulo@89 949 d3_selectionPrototype.sort = function(comparator) {
paulo@89 950 comparator = d3_selection_sortComparator.apply(this, arguments);
paulo@89 951 for (var j = -1, m = this.length; ++j < m; ) this[j].sort(comparator);
paulo@89 952 return this.order();
paulo@89 953 };
paulo@89 954 function d3_selection_sortComparator(comparator) {
paulo@89 955 if (!arguments.length) comparator = d3_ascending;
paulo@89 956 return function(a, b) {
paulo@89 957 return a && b ? comparator(a.__data__, b.__data__) : !a - !b;
paulo@89 958 };
paulo@89 959 }
paulo@89 960 d3_selectionPrototype.each = function(callback) {
paulo@89 961 return d3_selection_each(this, function(node, i, j) {
paulo@89 962 callback.call(node, node.__data__, i, j);
paulo@89 963 });
paulo@89 964 };
paulo@89 965 function d3_selection_each(groups, callback) {
paulo@89 966 for (var j = 0, m = groups.length; j < m; j++) {
paulo@89 967 for (var group = groups[j], i = 0, n = group.length, node; i < n; i++) {
paulo@89 968 if (node = group[i]) callback(node, i, j);
paulo@89 969 }
paulo@89 970 }
paulo@89 971 return groups;
paulo@89 972 }
paulo@89 973 d3_selectionPrototype.call = function(callback) {
paulo@89 974 var args = d3_array(arguments);
paulo@89 975 callback.apply(args[0] = this, args);
paulo@89 976 return this;
paulo@89 977 };
paulo@89 978 d3_selectionPrototype.empty = function() {
paulo@89 979 return !this.node();
paulo@89 980 };
paulo@89 981 d3_selectionPrototype.node = function() {
paulo@89 982 for (var j = 0, m = this.length; j < m; j++) {
paulo@89 983 for (var group = this[j], i = 0, n = group.length; i < n; i++) {
paulo@89 984 var node = group[i];
paulo@89 985 if (node) return node;
paulo@89 986 }
paulo@89 987 }
paulo@89 988 return null;
paulo@89 989 };
paulo@89 990 d3_selectionPrototype.size = function() {
paulo@89 991 var n = 0;
paulo@89 992 d3_selection_each(this, function() {
paulo@89 993 ++n;
paulo@89 994 });
paulo@89 995 return n;
paulo@89 996 };
paulo@89 997 function d3_selection_enter(selection) {
paulo@89 998 d3_subclass(selection, d3_selection_enterPrototype);
paulo@89 999 return selection;
paulo@89 1000 }
paulo@89 1001 var d3_selection_enterPrototype = [];
paulo@89 1002 d3.selection.enter = d3_selection_enter;
paulo@89 1003 d3.selection.enter.prototype = d3_selection_enterPrototype;
paulo@89 1004 d3_selection_enterPrototype.append = d3_selectionPrototype.append;
paulo@89 1005 d3_selection_enterPrototype.empty = d3_selectionPrototype.empty;
paulo@89 1006 d3_selection_enterPrototype.node = d3_selectionPrototype.node;
paulo@89 1007 d3_selection_enterPrototype.call = d3_selectionPrototype.call;
paulo@89 1008 d3_selection_enterPrototype.size = d3_selectionPrototype.size;
paulo@89 1009 d3_selection_enterPrototype.select = function(selector) {
paulo@89 1010 var subgroups = [], subgroup, subnode, upgroup, group, node;
paulo@89 1011 for (var j = -1, m = this.length; ++j < m; ) {
paulo@89 1012 upgroup = (group = this[j]).update;
paulo@89 1013 subgroups.push(subgroup = []);
paulo@89 1014 subgroup.parentNode = group.parentNode;
paulo@89 1015 for (var i = -1, n = group.length; ++i < n; ) {
paulo@89 1016 if (node = group[i]) {
paulo@89 1017 subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i, j));
paulo@89 1018 subnode.__data__ = node.__data__;
paulo@89 1019 } else {
paulo@89 1020 subgroup.push(null);
paulo@89 1021 }
paulo@89 1022 }
paulo@89 1023 }
paulo@89 1024 return d3_selection(subgroups);
paulo@89 1025 };
paulo@89 1026 d3_selection_enterPrototype.insert = function(name, before) {
paulo@89 1027 if (arguments.length < 2) before = d3_selection_enterInsertBefore(this);
paulo@89 1028 return d3_selectionPrototype.insert.call(this, name, before);
paulo@89 1029 };
paulo@89 1030 function d3_selection_enterInsertBefore(enter) {
paulo@89 1031 var i0, j0;
paulo@89 1032 return function(d, i, j) {
paulo@89 1033 var group = enter[j].update, n = group.length, node;
paulo@89 1034 if (j != j0) j0 = j, i0 = 0;
paulo@89 1035 if (i >= i0) i0 = i + 1;
paulo@89 1036 while (!(node = group[i0]) && ++i0 < n) ;
paulo@89 1037 return node;
paulo@89 1038 };
paulo@89 1039 }
paulo@89 1040 d3.select = function(node) {
paulo@89 1041 var group;
paulo@89 1042 if (typeof node === "string") {
paulo@89 1043 group = [ d3_select(node, d3_document) ];
paulo@89 1044 group.parentNode = d3_document.documentElement;
paulo@89 1045 } else {
paulo@89 1046 group = [ node ];
paulo@89 1047 group.parentNode = d3_documentElement(node);
paulo@89 1048 }
paulo@89 1049 return d3_selection([ group ]);
paulo@89 1050 };
paulo@89 1051 d3.selectAll = function(nodes) {
paulo@89 1052 var group;
paulo@89 1053 if (typeof nodes === "string") {
paulo@89 1054 group = d3_array(d3_selectAll(nodes, d3_document));
paulo@89 1055 group.parentNode = d3_document.documentElement;
paulo@89 1056 } else {
paulo@89 1057 group = d3_array(nodes);
paulo@89 1058 group.parentNode = null;
paulo@89 1059 }
paulo@89 1060 return d3_selection([ group ]);
paulo@89 1061 };
paulo@89 1062 d3_selectionPrototype.on = function(type, listener, capture) {
paulo@89 1063 var n = arguments.length;
paulo@89 1064 if (n < 3) {
paulo@89 1065 if (typeof type !== "string") {
paulo@89 1066 if (n < 2) listener = false;
paulo@89 1067 for (capture in type) this.each(d3_selection_on(capture, type[capture], listener));
paulo@89 1068 return this;
paulo@89 1069 }
paulo@89 1070 if (n < 2) return (n = this.node()["__on" + type]) && n._;
paulo@89 1071 capture = false;
paulo@89 1072 }
paulo@89 1073 return this.each(d3_selection_on(type, listener, capture));
paulo@89 1074 };
paulo@89 1075 function d3_selection_on(type, listener, capture) {
paulo@89 1076 var name = "__on" + type, i = type.indexOf("."), wrap = d3_selection_onListener;
paulo@89 1077 if (i > 0) type = type.slice(0, i);
paulo@89 1078 var filter = d3_selection_onFilters.get(type);
paulo@89 1079 if (filter) type = filter, wrap = d3_selection_onFilter;
paulo@89 1080 function onRemove() {
paulo@89 1081 var l = this[name];
paulo@89 1082 if (l) {
paulo@89 1083 this.removeEventListener(type, l, l.$);
paulo@89 1084 delete this[name];
paulo@89 1085 }
paulo@89 1086 }
paulo@89 1087 function onAdd() {
paulo@89 1088 var l = wrap(listener, d3_array(arguments));
paulo@89 1089 onRemove.call(this);
paulo@89 1090 this.addEventListener(type, this[name] = l, l.$ = capture);
paulo@89 1091 l._ = listener;
paulo@89 1092 }
paulo@89 1093 function removeAll() {
paulo@89 1094 var re = new RegExp("^__on([^.]+)" + d3.requote(type) + "$"), match;
paulo@89 1095 for (var name in this) {
paulo@89 1096 if (match = name.match(re)) {
paulo@89 1097 var l = this[name];
paulo@89 1098 this.removeEventListener(match[1], l, l.$);
paulo@89 1099 delete this[name];
paulo@89 1100 }
paulo@89 1101 }
paulo@89 1102 }
paulo@89 1103 return i ? listener ? onAdd : onRemove : listener ? d3_noop : removeAll;
paulo@89 1104 }
paulo@89 1105 var d3_selection_onFilters = d3.map({
paulo@89 1106 mouseenter: "mouseover",
paulo@89 1107 mouseleave: "mouseout"
paulo@89 1108 });
paulo@89 1109 if (d3_document) {
paulo@89 1110 d3_selection_onFilters.forEach(function(k) {
paulo@89 1111 if ("on" + k in d3_document) d3_selection_onFilters.remove(k);
paulo@89 1112 });
paulo@89 1113 }
paulo@89 1114 function d3_selection_onListener(listener, argumentz) {
paulo@89 1115 return function(e) {
paulo@89 1116 var o = d3.event;
paulo@89 1117 d3.event = e;
paulo@89 1118 argumentz[0] = this.__data__;
paulo@89 1119 try {
paulo@89 1120 listener.apply(this, argumentz);
paulo@89 1121 } finally {
paulo@89 1122 d3.event = o;
paulo@89 1123 }
paulo@89 1124 };
paulo@89 1125 }
paulo@89 1126 function d3_selection_onFilter(listener, argumentz) {
paulo@89 1127 var l = d3_selection_onListener(listener, argumentz);
paulo@89 1128 return function(e) {
paulo@89 1129 var target = this, related = e.relatedTarget;
paulo@89 1130 if (!related || related !== target && !(related.compareDocumentPosition(target) & 8)) {
paulo@89 1131 l.call(target, e);
paulo@89 1132 }
paulo@89 1133 };
paulo@89 1134 }
paulo@89 1135 var d3_event_dragSelect, d3_event_dragId = 0;
paulo@89 1136 function d3_event_dragSuppress(node) {
paulo@89 1137 var name = ".dragsuppress-" + ++d3_event_dragId, click = "click" + name, w = d3.select(d3_window(node)).on("touchmove" + name, d3_eventPreventDefault).on("dragstart" + name, d3_eventPreventDefault).on("selectstart" + name, d3_eventPreventDefault);
paulo@89 1138 if (d3_event_dragSelect == null) {
paulo@89 1139 d3_event_dragSelect = "onselectstart" in node ? false : d3_vendorSymbol(node.style, "userSelect");
paulo@89 1140 }
paulo@89 1141 if (d3_event_dragSelect) {
paulo@89 1142 var style = d3_documentElement(node).style, select = style[d3_event_dragSelect];
paulo@89 1143 style[d3_event_dragSelect] = "none";
paulo@89 1144 }
paulo@89 1145 return function(suppressClick) {
paulo@89 1146 w.on(name, null);
paulo@89 1147 if (d3_event_dragSelect) style[d3_event_dragSelect] = select;
paulo@89 1148 if (suppressClick) {
paulo@89 1149 var off = function() {
paulo@89 1150 w.on(click, null);
paulo@89 1151 };
paulo@89 1152 w.on(click, function() {
paulo@89 1153 d3_eventPreventDefault();
paulo@89 1154 off();
paulo@89 1155 }, true);
paulo@89 1156 setTimeout(off, 0);
paulo@89 1157 }
paulo@89 1158 };
paulo@89 1159 }
paulo@89 1160 d3.mouse = function(container) {
paulo@89 1161 return d3_mousePoint(container, d3_eventSource());
paulo@89 1162 };
paulo@89 1163 var d3_mouse_bug44083 = this.navigator && /WebKit/.test(this.navigator.userAgent) ? -1 : 0;
paulo@89 1164 function d3_mousePoint(container, e) {
paulo@89 1165 if (e.changedTouches) e = e.changedTouches[0];
paulo@89 1166 var svg = container.ownerSVGElement || container;
paulo@89 1167 if (svg.createSVGPoint) {
paulo@89 1168 var point = svg.createSVGPoint();
paulo@89 1169 if (d3_mouse_bug44083 < 0) {
paulo@89 1170 var window = d3_window(container);
paulo@89 1171 if (window.scrollX || window.scrollY) {
paulo@89 1172 svg = d3.select("body").append("svg").style({
paulo@89 1173 position: "absolute",
paulo@89 1174 top: 0,
paulo@89 1175 left: 0,
paulo@89 1176 margin: 0,
paulo@89 1177 padding: 0,
paulo@89 1178 border: "none"
paulo@89 1179 }, "important");
paulo@89 1180 var ctm = svg[0][0].getScreenCTM();
paulo@89 1181 d3_mouse_bug44083 = !(ctm.f || ctm.e);
paulo@89 1182 svg.remove();
paulo@89 1183 }
paulo@89 1184 }
paulo@89 1185 if (d3_mouse_bug44083) point.x = e.pageX, point.y = e.pageY; else point.x = e.clientX,
paulo@89 1186 point.y = e.clientY;
paulo@89 1187 point = point.matrixTransform(container.getScreenCTM().inverse());
paulo@89 1188 return [ point.x, point.y ];
paulo@89 1189 }
paulo@89 1190 var rect = container.getBoundingClientRect();
paulo@89 1191 return [ e.clientX - rect.left - container.clientLeft, e.clientY - rect.top - container.clientTop ];
paulo@89 1192 }
paulo@89 1193 d3.touch = function(container, touches, identifier) {
paulo@89 1194 if (arguments.length < 3) identifier = touches, touches = d3_eventSource().changedTouches;
paulo@89 1195 if (touches) for (var i = 0, n = touches.length, touch; i < n; ++i) {
paulo@89 1196 if ((touch = touches[i]).identifier === identifier) {
paulo@89 1197 return d3_mousePoint(container, touch);
paulo@89 1198 }
paulo@89 1199 }
paulo@89 1200 };
paulo@89 1201 d3.behavior.drag = function() {
paulo@89 1202 var event = d3_eventDispatch(drag, "drag", "dragstart", "dragend"), origin = null, mousedown = dragstart(d3_noop, d3.mouse, d3_window, "mousemove", "mouseup"), touchstart = dragstart(d3_behavior_dragTouchId, d3.touch, d3_identity, "touchmove", "touchend");
paulo@89 1203 function drag() {
paulo@89 1204 this.on("mousedown.drag", mousedown).on("touchstart.drag", touchstart);
paulo@89 1205 }
paulo@89 1206 function dragstart(id, position, subject, move, end) {
paulo@89 1207 return function() {
paulo@89 1208 var that = this, target = d3.event.target.correspondingElement || d3.event.target, parent = that.parentNode, dispatch = event.of(that, arguments), dragged = 0, dragId = id(), dragName = ".drag" + (dragId == null ? "" : "-" + dragId), dragOffset, dragSubject = d3.select(subject(target)).on(move + dragName, moved).on(end + dragName, ended), dragRestore = d3_event_dragSuppress(target), position0 = position(parent, dragId);
paulo@89 1209 if (origin) {
paulo@89 1210 dragOffset = origin.apply(that, arguments);
paulo@89 1211 dragOffset = [ dragOffset.x - position0[0], dragOffset.y - position0[1] ];
paulo@89 1212 } else {
paulo@89 1213 dragOffset = [ 0, 0 ];
paulo@89 1214 }
paulo@89 1215 dispatch({
paulo@89 1216 type: "dragstart"
paulo@89 1217 });
paulo@89 1218 function moved() {
paulo@89 1219 var position1 = position(parent, dragId), dx, dy;
paulo@89 1220 if (!position1) return;
paulo@89 1221 dx = position1[0] - position0[0];
paulo@89 1222 dy = position1[1] - position0[1];
paulo@89 1223 dragged |= dx | dy;
paulo@89 1224 position0 = position1;
paulo@89 1225 dispatch({
paulo@89 1226 type: "drag",
paulo@89 1227 x: position1[0] + dragOffset[0],
paulo@89 1228 y: position1[1] + dragOffset[1],
paulo@89 1229 dx: dx,
paulo@89 1230 dy: dy
paulo@89 1231 });
paulo@89 1232 }
paulo@89 1233 function ended() {
paulo@89 1234 if (!position(parent, dragId)) return;
paulo@89 1235 dragSubject.on(move + dragName, null).on(end + dragName, null);
paulo@89 1236 dragRestore(dragged);
paulo@89 1237 dispatch({
paulo@89 1238 type: "dragend"
paulo@89 1239 });
paulo@89 1240 }
paulo@89 1241 };
paulo@89 1242 }
paulo@89 1243 drag.origin = function(x) {
paulo@89 1244 if (!arguments.length) return origin;
paulo@89 1245 origin = x;
paulo@89 1246 return drag;
paulo@89 1247 };
paulo@89 1248 return d3.rebind(drag, event, "on");
paulo@89 1249 };
paulo@89 1250 function d3_behavior_dragTouchId() {
paulo@89 1251 return d3.event.changedTouches[0].identifier;
paulo@89 1252 }
paulo@89 1253 d3.touches = function(container, touches) {
paulo@89 1254 if (arguments.length < 2) touches = d3_eventSource().touches;
paulo@89 1255 return touches ? d3_array(touches).map(function(touch) {
paulo@89 1256 var point = d3_mousePoint(container, touch);
paulo@89 1257 point.identifier = touch.identifier;
paulo@89 1258 return point;
paulo@89 1259 }) : [];
paulo@89 1260 };
paulo@89 1261 var ε = 1e-6, ε2 = ε * ε, π = Math.PI, τ = 2 * π, τε = τ - ε, halfπ = π / 2, d3_radians = π / 180, d3_degrees = 180 / π;
paulo@89 1262 function d3_sgn(x) {
paulo@89 1263 return x > 0 ? 1 : x < 0 ? -1 : 0;
paulo@89 1264 }
paulo@89 1265 function d3_cross2d(a, b, c) {
paulo@89 1266 return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
paulo@89 1267 }
paulo@89 1268 function d3_acos(x) {
paulo@89 1269 return x > 1 ? 0 : x < -1 ? π : Math.acos(x);
paulo@89 1270 }
paulo@89 1271 function d3_asin(x) {
paulo@89 1272 return x > 1 ? halfπ : x < -1 ? -halfπ : Math.asin(x);
paulo@89 1273 }
paulo@89 1274 function d3_sinh(x) {
paulo@89 1275 return ((x = Math.exp(x)) - 1 / x) / 2;
paulo@89 1276 }
paulo@89 1277 function d3_cosh(x) {
paulo@89 1278 return ((x = Math.exp(x)) + 1 / x) / 2;
paulo@89 1279 }
paulo@89 1280 function d3_tanh(x) {
paulo@89 1281 return ((x = Math.exp(2 * x)) - 1) / (x + 1);
paulo@89 1282 }
paulo@89 1283 function d3_haversin(x) {
paulo@89 1284 return (x = Math.sin(x / 2)) * x;
paulo@89 1285 }
paulo@89 1286 var ρ = Math.SQRT2, ρ2 = 2, ρ4 = 4;
paulo@89 1287 d3.interpolateZoom = function(p0, p1) {
paulo@89 1288 var ux0 = p0[0], uy0 = p0[1], w0 = p0[2], ux1 = p1[0], uy1 = p1[1], w1 = p1[2], dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, i, S;
paulo@89 1289 if (d2 < ε2) {
paulo@89 1290 S = Math.log(w1 / w0) / ρ;
paulo@89 1291 i = function(t) {
paulo@89 1292 return [ ux0 + t * dx, uy0 + t * dy, w0 * Math.exp(ρ * t * S) ];
paulo@89 1293 };
paulo@89 1294 } else {
paulo@89 1295 var d1 = Math.sqrt(d2), b0 = (w1 * w1 - w0 * w0 + ρ4 * d2) / (2 * w0 * ρ2 * d1), b1 = (w1 * w1 - w0 * w0 - ρ4 * d2) / (2 * w1 * ρ2 * d1), r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0), r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1);
paulo@89 1296 S = (r1 - r0) / ρ;
paulo@89 1297 i = function(t) {
paulo@89 1298 var s = t * S, coshr0 = d3_cosh(r0), u = w0 / (ρ2 * d1) * (coshr0 * d3_tanh(ρ * s + r0) - d3_sinh(r0));
paulo@89 1299 return [ ux0 + u * dx, uy0 + u * dy, w0 * coshr0 / d3_cosh(ρ * s + r0) ];
paulo@89 1300 };
paulo@89 1301 }
paulo@89 1302 i.duration = S * 1e3;
paulo@89 1303 return i;
paulo@89 1304 };
paulo@89 1305 d3.behavior.zoom = function() {
paulo@89 1306 var view = {
paulo@89 1307 x: 0,
paulo@89 1308 y: 0,
paulo@89 1309 k: 1
paulo@89 1310 }, translate0, center0, center, size = [ 960, 500 ], scaleExtent = d3_behavior_zoomInfinity, duration = 250, zooming = 0, mousedown = "mousedown.zoom", mousemove = "mousemove.zoom", mouseup = "mouseup.zoom", mousewheelTimer, touchstart = "touchstart.zoom", touchtime, event = d3_eventDispatch(zoom, "zoomstart", "zoom", "zoomend"), x0, x1, y0, y1;
paulo@89 1311 if (!d3_behavior_zoomWheel) {
paulo@89 1312 d3_behavior_zoomWheel = "onwheel" in d3_document ? (d3_behavior_zoomDelta = function() {
paulo@89 1313 return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1);
paulo@89 1314 }, "wheel") : "onmousewheel" in d3_document ? (d3_behavior_zoomDelta = function() {
paulo@89 1315 return d3.event.wheelDelta;
paulo@89 1316 }, "mousewheel") : (d3_behavior_zoomDelta = function() {
paulo@89 1317 return -d3.event.detail;
paulo@89 1318 }, "MozMousePixelScroll");
paulo@89 1319 }
paulo@89 1320 function zoom(g) {
paulo@89 1321 g.on(mousedown, mousedowned).on(d3_behavior_zoomWheel + ".zoom", mousewheeled).on("dblclick.zoom", dblclicked).on(touchstart, touchstarted);
paulo@89 1322 }
paulo@89 1323 zoom.event = function(g) {
paulo@89 1324 g.each(function() {
paulo@89 1325 var dispatch = event.of(this, arguments), view1 = view;
paulo@89 1326 if (d3_transitionInheritId) {
paulo@89 1327 d3.select(this).transition().each("start.zoom", function() {
paulo@89 1328 view = this.__chart__ || {
paulo@89 1329 x: 0,
paulo@89 1330 y: 0,
paulo@89 1331 k: 1
paulo@89 1332 };
paulo@89 1333 zoomstarted(dispatch);
paulo@89 1334 }).tween("zoom:zoom", function() {
paulo@89 1335 var dx = size[0], dy = size[1], cx = center0 ? center0[0] : dx / 2, cy = center0 ? center0[1] : dy / 2, i = d3.interpolateZoom([ (cx - view.x) / view.k, (cy - view.y) / view.k, dx / view.k ], [ (cx - view1.x) / view1.k, (cy - view1.y) / view1.k, dx / view1.k ]);
paulo@89 1336 return function(t) {
paulo@89 1337 var l = i(t), k = dx / l[2];
paulo@89 1338 this.__chart__ = view = {
paulo@89 1339 x: cx - l[0] * k,
paulo@89 1340 y: cy - l[1] * k,
paulo@89 1341 k: k
paulo@89 1342 };
paulo@89 1343 zoomed(dispatch);
paulo@89 1344 };
paulo@89 1345 }).each("interrupt.zoom", function() {
paulo@89 1346 zoomended(dispatch);
paulo@89 1347 }).each("end.zoom", function() {
paulo@89 1348 zoomended(dispatch);
paulo@89 1349 });
paulo@89 1350 } else {
paulo@89 1351 this.__chart__ = view;
paulo@89 1352 zoomstarted(dispatch);
paulo@89 1353 zoomed(dispatch);
paulo@89 1354 zoomended(dispatch);
paulo@89 1355 }
paulo@89 1356 });
paulo@89 1357 };
paulo@89 1358 zoom.translate = function(_) {
paulo@89 1359 if (!arguments.length) return [ view.x, view.y ];
paulo@89 1360 view = {
paulo@89 1361 x: +_[0],
paulo@89 1362 y: +_[1],
paulo@89 1363 k: view.k
paulo@89 1364 };
paulo@89 1365 rescale();
paulo@89 1366 return zoom;
paulo@89 1367 };
paulo@89 1368 zoom.scale = function(_) {
paulo@89 1369 if (!arguments.length) return view.k;
paulo@89 1370 view = {
paulo@89 1371 x: view.x,
paulo@89 1372 y: view.y,
paulo@89 1373 k: null
paulo@89 1374 };
paulo@89 1375 scaleTo(+_);
paulo@89 1376 rescale();
paulo@89 1377 return zoom;
paulo@89 1378 };
paulo@89 1379 zoom.scaleExtent = function(_) {
paulo@89 1380 if (!arguments.length) return scaleExtent;
paulo@89 1381 scaleExtent = _ == null ? d3_behavior_zoomInfinity : [ +_[0], +_[1] ];
paulo@89 1382 return zoom;
paulo@89 1383 };
paulo@89 1384 zoom.center = function(_) {
paulo@89 1385 if (!arguments.length) return center;
paulo@89 1386 center = _ && [ +_[0], +_[1] ];
paulo@89 1387 return zoom;
paulo@89 1388 };
paulo@89 1389 zoom.size = function(_) {
paulo@89 1390 if (!arguments.length) return size;
paulo@89 1391 size = _ && [ +_[0], +_[1] ];
paulo@89 1392 return zoom;
paulo@89 1393 };
paulo@89 1394 zoom.duration = function(_) {
paulo@89 1395 if (!arguments.length) return duration;
paulo@89 1396 duration = +_;
paulo@89 1397 return zoom;
paulo@89 1398 };
paulo@89 1399 zoom.x = function(z) {
paulo@89 1400 if (!arguments.length) return x1;
paulo@89 1401 x1 = z;
paulo@89 1402 x0 = z.copy();
paulo@89 1403 view = {
paulo@89 1404 x: 0,
paulo@89 1405 y: 0,
paulo@89 1406 k: 1
paulo@89 1407 };
paulo@89 1408 return zoom;
paulo@89 1409 };
paulo@89 1410 zoom.y = function(z) {
paulo@89 1411 if (!arguments.length) return y1;
paulo@89 1412 y1 = z;
paulo@89 1413 y0 = z.copy();
paulo@89 1414 view = {
paulo@89 1415 x: 0,
paulo@89 1416 y: 0,
paulo@89 1417 k: 1
paulo@89 1418 };
paulo@89 1419 return zoom;
paulo@89 1420 };
paulo@89 1421 function location(p) {
paulo@89 1422 return [ (p[0] - view.x) / view.k, (p[1] - view.y) / view.k ];
paulo@89 1423 }
paulo@89 1424 function point(l) {
paulo@89 1425 return [ l[0] * view.k + view.x, l[1] * view.k + view.y ];
paulo@89 1426 }
paulo@89 1427 function scaleTo(s) {
paulo@89 1428 view.k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], s));
paulo@89 1429 }
paulo@89 1430 function translateTo(p, l) {
paulo@89 1431 l = point(l);
paulo@89 1432 view.x += p[0] - l[0];
paulo@89 1433 view.y += p[1] - l[1];
paulo@89 1434 }
paulo@89 1435 function zoomTo(that, p, l, k) {
paulo@89 1436 that.__chart__ = {
paulo@89 1437 x: view.x,
paulo@89 1438 y: view.y,
paulo@89 1439 k: view.k
paulo@89 1440 };
paulo@89 1441 scaleTo(Math.pow(2, k));
paulo@89 1442 translateTo(center0 = p, l);
paulo@89 1443 that = d3.select(that);
paulo@89 1444 if (duration > 0) that = that.transition().duration(duration);
paulo@89 1445 that.call(zoom.event);
paulo@89 1446 }
paulo@89 1447 function rescale() {
paulo@89 1448 if (x1) x1.domain(x0.range().map(function(x) {
paulo@89 1449 return (x - view.x) / view.k;
paulo@89 1450 }).map(x0.invert));
paulo@89 1451 if (y1) y1.domain(y0.range().map(function(y) {
paulo@89 1452 return (y - view.y) / view.k;
paulo@89 1453 }).map(y0.invert));
paulo@89 1454 }
paulo@89 1455 function zoomstarted(dispatch) {
paulo@89 1456 if (!zooming++) dispatch({
paulo@89 1457 type: "zoomstart"
paulo@89 1458 });
paulo@89 1459 }
paulo@89 1460 function zoomed(dispatch) {
paulo@89 1461 rescale();
paulo@89 1462 dispatch({
paulo@89 1463 type: "zoom",
paulo@89 1464 scale: view.k,
paulo@89 1465 translate: [ view.x, view.y ]
paulo@89 1466 });
paulo@89 1467 }
paulo@89 1468 function zoomended(dispatch) {
paulo@89 1469 if (!--zooming) dispatch({
paulo@89 1470 type: "zoomend"
paulo@89 1471 }), center0 = null;
paulo@89 1472 }
paulo@89 1473 function mousedowned() {
paulo@89 1474 var that = this, dispatch = event.of(that, arguments), dragged = 0, subject = d3.select(d3_window(that)).on(mousemove, moved).on(mouseup, ended), location0 = location(d3.mouse(that)), dragRestore = d3_event_dragSuppress(that);
paulo@89 1475 d3_selection_interrupt.call(that);
paulo@89 1476 zoomstarted(dispatch);
paulo@89 1477 function moved() {
paulo@89 1478 dragged = 1;
paulo@89 1479 translateTo(d3.mouse(that), location0);
paulo@89 1480 zoomed(dispatch);
paulo@89 1481 }
paulo@89 1482 function ended() {
paulo@89 1483 subject.on(mousemove, null).on(mouseup, null);
paulo@89 1484 dragRestore(dragged);
paulo@89 1485 zoomended(dispatch);
paulo@89 1486 }
paulo@89 1487 }
paulo@89 1488 function touchstarted() {
paulo@89 1489 var that = this, dispatch = event.of(that, arguments), locations0 = {}, distance0 = 0, scale0, zoomName = ".zoom-" + d3.event.changedTouches[0].identifier, touchmove = "touchmove" + zoomName, touchend = "touchend" + zoomName, targets = [], subject = d3.select(that), dragRestore = d3_event_dragSuppress(that);
paulo@89 1490 started();
paulo@89 1491 zoomstarted(dispatch);
paulo@89 1492 subject.on(mousedown, null).on(touchstart, started);
paulo@89 1493 function relocate() {
paulo@89 1494 var touches = d3.touches(that);
paulo@89 1495 scale0 = view.k;
paulo@89 1496 touches.forEach(function(t) {
paulo@89 1497 if (t.identifier in locations0) locations0[t.identifier] = location(t);
paulo@89 1498 });
paulo@89 1499 return touches;
paulo@89 1500 }
paulo@89 1501 function started() {
paulo@89 1502 var target = d3.event.target;
paulo@89 1503 d3.select(target).on(touchmove, moved).on(touchend, ended);
paulo@89 1504 targets.push(target);
paulo@89 1505 var changed = d3.event.changedTouches;
paulo@89 1506 for (var i = 0, n = changed.length; i < n; ++i) {
paulo@89 1507 locations0[changed[i].identifier] = null;
paulo@89 1508 }
paulo@89 1509 var touches = relocate(), now = Date.now();
paulo@89 1510 if (touches.length === 1) {
paulo@89 1511 if (now - touchtime < 500) {
paulo@89 1512 var p = touches[0];
paulo@89 1513 zoomTo(that, p, locations0[p.identifier], Math.floor(Math.log(view.k) / Math.LN2) + 1);
paulo@89 1514 d3_eventPreventDefault();
paulo@89 1515 }
paulo@89 1516 touchtime = now;
paulo@89 1517 } else if (touches.length > 1) {
paulo@89 1518 var p = touches[0], q = touches[1], dx = p[0] - q[0], dy = p[1] - q[1];
paulo@89 1519 distance0 = dx * dx + dy * dy;
paulo@89 1520 }
paulo@89 1521 }
paulo@89 1522 function moved() {
paulo@89 1523 var touches = d3.touches(that), p0, l0, p1, l1;
paulo@89 1524 d3_selection_interrupt.call(that);
paulo@89 1525 for (var i = 0, n = touches.length; i < n; ++i, l1 = null) {
paulo@89 1526 p1 = touches[i];
paulo@89 1527 if (l1 = locations0[p1.identifier]) {
paulo@89 1528 if (l0) break;
paulo@89 1529 p0 = p1, l0 = l1;
paulo@89 1530 }
paulo@89 1531 }
paulo@89 1532 if (l1) {
paulo@89 1533 var distance1 = (distance1 = p1[0] - p0[0]) * distance1 + (distance1 = p1[1] - p0[1]) * distance1, scale1 = distance0 && Math.sqrt(distance1 / distance0);
paulo@89 1534 p0 = [ (p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2 ];
paulo@89 1535 l0 = [ (l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2 ];
paulo@89 1536 scaleTo(scale1 * scale0);
paulo@89 1537 }
paulo@89 1538 touchtime = null;
paulo@89 1539 translateTo(p0, l0);
paulo@89 1540 zoomed(dispatch);
paulo@89 1541 }
paulo@89 1542 function ended() {
paulo@89 1543 if (d3.event.touches.length) {
paulo@89 1544 var changed = d3.event.changedTouches;
paulo@89 1545 for (var i = 0, n = changed.length; i < n; ++i) {
paulo@89 1546 delete locations0[changed[i].identifier];
paulo@89 1547 }
paulo@89 1548 for (var identifier in locations0) {
paulo@89 1549 return void relocate();
paulo@89 1550 }
paulo@89 1551 }
paulo@89 1552 d3.selectAll(targets).on(zoomName, null);
paulo@89 1553 subject.on(mousedown, mousedowned).on(touchstart, touchstarted);
paulo@89 1554 dragRestore();
paulo@89 1555 zoomended(dispatch);
paulo@89 1556 }
paulo@89 1557 }
paulo@89 1558 function mousewheeled() {
paulo@89 1559 var dispatch = event.of(this, arguments);
paulo@89 1560 if (mousewheelTimer) clearTimeout(mousewheelTimer); else d3_selection_interrupt.call(this),
paulo@89 1561 translate0 = location(center0 = center || d3.mouse(this)), zoomstarted(dispatch);
paulo@89 1562 mousewheelTimer = setTimeout(function() {
paulo@89 1563 mousewheelTimer = null;
paulo@89 1564 zoomended(dispatch);
paulo@89 1565 }, 50);
paulo@89 1566 d3_eventPreventDefault();
paulo@89 1567 scaleTo(Math.pow(2, d3_behavior_zoomDelta() * .002) * view.k);
paulo@89 1568 translateTo(center0, translate0);
paulo@89 1569 zoomed(dispatch);
paulo@89 1570 }
paulo@89 1571 function dblclicked() {
paulo@89 1572 var p = d3.mouse(this), k = Math.log(view.k) / Math.LN2;
paulo@89 1573 zoomTo(this, p, location(p), d3.event.shiftKey ? Math.ceil(k) - 1 : Math.floor(k) + 1);
paulo@89 1574 }
paulo@89 1575 return d3.rebind(zoom, event, "on");
paulo@89 1576 };
paulo@89 1577 var d3_behavior_zoomInfinity = [ 0, Infinity ], d3_behavior_zoomDelta, d3_behavior_zoomWheel;
paulo@89 1578 d3.color = d3_color;
paulo@89 1579 function d3_color() {}
paulo@89 1580 d3_color.prototype.toString = function() {
paulo@89 1581 return this.rgb() + "";
paulo@89 1582 };
paulo@89 1583 d3.hsl = d3_hsl;
paulo@89 1584 function d3_hsl(h, s, l) {
paulo@89 1585 return this instanceof d3_hsl ? void (this.h = +h, this.s = +s, this.l = +l) : arguments.length < 2 ? h instanceof d3_hsl ? new d3_hsl(h.h, h.s, h.l) : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl) : new d3_hsl(h, s, l);
paulo@89 1586 }
paulo@89 1587 var d3_hslPrototype = d3_hsl.prototype = new d3_color();
paulo@89 1588 d3_hslPrototype.brighter = function(k) {
paulo@89 1589 k = Math.pow(.7, arguments.length ? k : 1);
paulo@89 1590 return new d3_hsl(this.h, this.s, this.l / k);
paulo@89 1591 };
paulo@89 1592 d3_hslPrototype.darker = function(k) {
paulo@89 1593 k = Math.pow(.7, arguments.length ? k : 1);
paulo@89 1594 return new d3_hsl(this.h, this.s, k * this.l);
paulo@89 1595 };
paulo@89 1596 d3_hslPrototype.rgb = function() {
paulo@89 1597 return d3_hsl_rgb(this.h, this.s, this.l);
paulo@89 1598 };
paulo@89 1599 function d3_hsl_rgb(h, s, l) {
paulo@89 1600 var m1, m2;
paulo@89 1601 h = isNaN(h) ? 0 : (h %= 360) < 0 ? h + 360 : h;
paulo@89 1602 s = isNaN(s) ? 0 : s < 0 ? 0 : s > 1 ? 1 : s;
paulo@89 1603 l = l < 0 ? 0 : l > 1 ? 1 : l;
paulo@89 1604 m2 = l <= .5 ? l * (1 + s) : l + s - l * s;
paulo@89 1605 m1 = 2 * l - m2;
paulo@89 1606 function v(h) {
paulo@89 1607 if (h > 360) h -= 360; else if (h < 0) h += 360;
paulo@89 1608 if (h < 60) return m1 + (m2 - m1) * h / 60;
paulo@89 1609 if (h < 180) return m2;
paulo@89 1610 if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60;
paulo@89 1611 return m1;
paulo@89 1612 }
paulo@89 1613 function vv(h) {
paulo@89 1614 return Math.round(v(h) * 255);
paulo@89 1615 }
paulo@89 1616 return new d3_rgb(vv(h + 120), vv(h), vv(h - 120));
paulo@89 1617 }
paulo@89 1618 d3.hcl = d3_hcl;
paulo@89 1619 function d3_hcl(h, c, l) {
paulo@89 1620 return this instanceof d3_hcl ? void (this.h = +h, this.c = +c, this.l = +l) : arguments.length < 2 ? h instanceof d3_hcl ? new d3_hcl(h.h, h.c, h.l) : h instanceof d3_lab ? d3_lab_hcl(h.l, h.a, h.b) : d3_lab_hcl((h = d3_rgb_lab((h = d3.rgb(h)).r, h.g, h.b)).l, h.a, h.b) : new d3_hcl(h, c, l);
paulo@89 1621 }
paulo@89 1622 var d3_hclPrototype = d3_hcl.prototype = new d3_color();
paulo@89 1623 d3_hclPrototype.brighter = function(k) {
paulo@89 1624 return new d3_hcl(this.h, this.c, Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)));
paulo@89 1625 };
paulo@89 1626 d3_hclPrototype.darker = function(k) {
paulo@89 1627 return new d3_hcl(this.h, this.c, Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)));
paulo@89 1628 };
paulo@89 1629 d3_hclPrototype.rgb = function() {
paulo@89 1630 return d3_hcl_lab(this.h, this.c, this.l).rgb();
paulo@89 1631 };
paulo@89 1632 function d3_hcl_lab(h, c, l) {
paulo@89 1633 if (isNaN(h)) h = 0;
paulo@89 1634 if (isNaN(c)) c = 0;
paulo@89 1635 return new d3_lab(l, Math.cos(h *= d3_radians) * c, Math.sin(h) * c);
paulo@89 1636 }
paulo@89 1637 d3.lab = d3_lab;
paulo@89 1638 function d3_lab(l, a, b) {
paulo@89 1639 return this instanceof d3_lab ? void (this.l = +l, this.a = +a, this.b = +b) : arguments.length < 2 ? l instanceof d3_lab ? new d3_lab(l.l, l.a, l.b) : l instanceof d3_hcl ? d3_hcl_lab(l.h, l.c, l.l) : d3_rgb_lab((l = d3_rgb(l)).r, l.g, l.b) : new d3_lab(l, a, b);
paulo@89 1640 }
paulo@89 1641 var d3_lab_K = 18;
paulo@89 1642 var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883;
paulo@89 1643 var d3_labPrototype = d3_lab.prototype = new d3_color();
paulo@89 1644 d3_labPrototype.brighter = function(k) {
paulo@89 1645 return new d3_lab(Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)), this.a, this.b);
paulo@89 1646 };
paulo@89 1647 d3_labPrototype.darker = function(k) {
paulo@89 1648 return new d3_lab(Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)), this.a, this.b);
paulo@89 1649 };
paulo@89 1650 d3_labPrototype.rgb = function() {
paulo@89 1651 return d3_lab_rgb(this.l, this.a, this.b);
paulo@89 1652 };
paulo@89 1653 function d3_lab_rgb(l, a, b) {
paulo@89 1654 var y = (l + 16) / 116, x = y + a / 500, z = y - b / 200;
paulo@89 1655 x = d3_lab_xyz(x) * d3_lab_X;
paulo@89 1656 y = d3_lab_xyz(y) * d3_lab_Y;
paulo@89 1657 z = d3_lab_xyz(z) * d3_lab_Z;
paulo@89 1658 return new d3_rgb(d3_xyz_rgb(3.2404542 * x - 1.5371385 * y - .4985314 * z), d3_xyz_rgb(-.969266 * x + 1.8760108 * y + .041556 * z), d3_xyz_rgb(.0556434 * x - .2040259 * y + 1.0572252 * z));
paulo@89 1659 }
paulo@89 1660 function d3_lab_hcl(l, a, b) {
paulo@89 1661 return l > 0 ? new d3_hcl(Math.atan2(b, a) * d3_degrees, Math.sqrt(a * a + b * b), l) : new d3_hcl(NaN, NaN, l);
paulo@89 1662 }
paulo@89 1663 function d3_lab_xyz(x) {
paulo@89 1664 return x > .206893034 ? x * x * x : (x - 4 / 29) / 7.787037;
paulo@89 1665 }
paulo@89 1666 function d3_xyz_lab(x) {
paulo@89 1667 return x > .008856 ? Math.pow(x, 1 / 3) : 7.787037 * x + 4 / 29;
paulo@89 1668 }
paulo@89 1669 function d3_xyz_rgb(r) {
paulo@89 1670 return Math.round(255 * (r <= .00304 ? 12.92 * r : 1.055 * Math.pow(r, 1 / 2.4) - .055));
paulo@89 1671 }
paulo@89 1672 d3.rgb = d3_rgb;
paulo@89 1673 function d3_rgb(r, g, b) {
paulo@89 1674 return this instanceof d3_rgb ? void (this.r = ~~r, this.g = ~~g, this.b = ~~b) : arguments.length < 2 ? r instanceof d3_rgb ? new d3_rgb(r.r, r.g, r.b) : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb) : new d3_rgb(r, g, b);
paulo@89 1675 }
paulo@89 1676 function d3_rgbNumber(value) {
paulo@89 1677 return new d3_rgb(value >> 16, value >> 8 & 255, value & 255);
paulo@89 1678 }
paulo@89 1679 function d3_rgbString(value) {
paulo@89 1680 return d3_rgbNumber(value) + "";
paulo@89 1681 }
paulo@89 1682 var d3_rgbPrototype = d3_rgb.prototype = new d3_color();
paulo@89 1683 d3_rgbPrototype.brighter = function(k) {
paulo@89 1684 k = Math.pow(.7, arguments.length ? k : 1);
paulo@89 1685 var r = this.r, g = this.g, b = this.b, i = 30;
paulo@89 1686 if (!r && !g && !b) return new d3_rgb(i, i, i);
paulo@89 1687 if (r && r < i) r = i;
paulo@89 1688 if (g && g < i) g = i;
paulo@89 1689 if (b && b < i) b = i;
paulo@89 1690 return new d3_rgb(Math.min(255, r / k), Math.min(255, g / k), Math.min(255, b / k));
paulo@89 1691 };
paulo@89 1692 d3_rgbPrototype.darker = function(k) {
paulo@89 1693 k = Math.pow(.7, arguments.length ? k : 1);
paulo@89 1694 return new d3_rgb(k * this.r, k * this.g, k * this.b);
paulo@89 1695 };
paulo@89 1696 d3_rgbPrototype.hsl = function() {
paulo@89 1697 return d3_rgb_hsl(this.r, this.g, this.b);
paulo@89 1698 };
paulo@89 1699 d3_rgbPrototype.toString = function() {
paulo@89 1700 return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b);
paulo@89 1701 };
paulo@89 1702 function d3_rgb_hex(v) {
paulo@89 1703 return v < 16 ? "0" + Math.max(0, v).toString(16) : Math.min(255, v).toString(16);
paulo@89 1704 }
paulo@89 1705 function d3_rgb_parse(format, rgb, hsl) {
paulo@89 1706 var r = 0, g = 0, b = 0, m1, m2, color;
paulo@89 1707 m1 = /([a-z]+)\((.*)\)/.exec(format = format.toLowerCase());
paulo@89 1708 if (m1) {
paulo@89 1709 m2 = m1[2].split(",");
paulo@89 1710 switch (m1[1]) {
paulo@89 1711 case "hsl":
paulo@89 1712 {
paulo@89 1713 return hsl(parseFloat(m2[0]), parseFloat(m2[1]) / 100, parseFloat(m2[2]) / 100);
paulo@89 1714 }
paulo@89 1715
paulo@89 1716 case "rgb":
paulo@89 1717 {
paulo@89 1718 return rgb(d3_rgb_parseNumber(m2[0]), d3_rgb_parseNumber(m2[1]), d3_rgb_parseNumber(m2[2]));
paulo@89 1719 }
paulo@89 1720 }
paulo@89 1721 }
paulo@89 1722 if (color = d3_rgb_names.get(format)) {
paulo@89 1723 return rgb(color.r, color.g, color.b);
paulo@89 1724 }
paulo@89 1725 if (format != null && format.charAt(0) === "#" && !isNaN(color = parseInt(format.slice(1), 16))) {
paulo@89 1726 if (format.length === 4) {
paulo@89 1727 r = (color & 3840) >> 4;
paulo@89 1728 r = r >> 4 | r;
paulo@89 1729 g = color & 240;
paulo@89 1730 g = g >> 4 | g;
paulo@89 1731 b = color & 15;
paulo@89 1732 b = b << 4 | b;
paulo@89 1733 } else if (format.length === 7) {
paulo@89 1734 r = (color & 16711680) >> 16;
paulo@89 1735 g = (color & 65280) >> 8;
paulo@89 1736 b = color & 255;
paulo@89 1737 }
paulo@89 1738 }
paulo@89 1739 return rgb(r, g, b);
paulo@89 1740 }
paulo@89 1741 function d3_rgb_hsl(r, g, b) {
paulo@89 1742 var min = Math.min(r /= 255, g /= 255, b /= 255), max = Math.max(r, g, b), d = max - min, h, s, l = (max + min) / 2;
paulo@89 1743 if (d) {
paulo@89 1744 s = l < .5 ? d / (max + min) : d / (2 - max - min);
paulo@89 1745 if (r == max) h = (g - b) / d + (g < b ? 6 : 0); else if (g == max) h = (b - r) / d + 2; else h = (r - g) / d + 4;
paulo@89 1746 h *= 60;
paulo@89 1747 } else {
paulo@89 1748 h = NaN;
paulo@89 1749 s = l > 0 && l < 1 ? 0 : h;
paulo@89 1750 }
paulo@89 1751 return new d3_hsl(h, s, l);
paulo@89 1752 }
paulo@89 1753 function d3_rgb_lab(r, g, b) {
paulo@89 1754 r = d3_rgb_xyz(r);
paulo@89 1755 g = d3_rgb_xyz(g);
paulo@89 1756 b = d3_rgb_xyz(b);
paulo@89 1757 var x = d3_xyz_lab((.4124564 * r + .3575761 * g + .1804375 * b) / d3_lab_X), y = d3_xyz_lab((.2126729 * r + .7151522 * g + .072175 * b) / d3_lab_Y), z = d3_xyz_lab((.0193339 * r + .119192 * g + .9503041 * b) / d3_lab_Z);
paulo@89 1758 return d3_lab(116 * y - 16, 500 * (x - y), 200 * (y - z));
paulo@89 1759 }
paulo@89 1760 function d3_rgb_xyz(r) {
paulo@89 1761 return (r /= 255) <= .04045 ? r / 12.92 : Math.pow((r + .055) / 1.055, 2.4);
paulo@89 1762 }
paulo@89 1763 function d3_rgb_parseNumber(c) {
paulo@89 1764 var f = parseFloat(c);
paulo@89 1765 return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f;
paulo@89 1766 }
paulo@89 1767 var d3_rgb_names = d3.map({
paulo@89 1768 aliceblue: 15792383,
paulo@89 1769 antiquewhite: 16444375,
paulo@89 1770 aqua: 65535,
paulo@89 1771 aquamarine: 8388564,
paulo@89 1772 azure: 15794175,
paulo@89 1773 beige: 16119260,
paulo@89 1774 bisque: 16770244,
paulo@89 1775 black: 0,
paulo@89 1776 blanchedalmond: 16772045,
paulo@89 1777 blue: 255,
paulo@89 1778 blueviolet: 9055202,
paulo@89 1779 brown: 10824234,
paulo@89 1780 burlywood: 14596231,
paulo@89 1781 cadetblue: 6266528,
paulo@89 1782 chartreuse: 8388352,
paulo@89 1783 chocolate: 13789470,
paulo@89 1784 coral: 16744272,
paulo@89 1785 cornflowerblue: 6591981,
paulo@89 1786 cornsilk: 16775388,
paulo@89 1787 crimson: 14423100,
paulo@89 1788 cyan: 65535,
paulo@89 1789 darkblue: 139,
paulo@89 1790 darkcyan: 35723,
paulo@89 1791 darkgoldenrod: 12092939,
paulo@89 1792 darkgray: 11119017,
paulo@89 1793 darkgreen: 25600,
paulo@89 1794 darkgrey: 11119017,
paulo@89 1795 darkkhaki: 12433259,
paulo@89 1796 darkmagenta: 9109643,
paulo@89 1797 darkolivegreen: 5597999,
paulo@89 1798 darkorange: 16747520,
paulo@89 1799 darkorchid: 10040012,
paulo@89 1800 darkred: 9109504,
paulo@89 1801 darksalmon: 15308410,
paulo@89 1802 darkseagreen: 9419919,
paulo@89 1803 darkslateblue: 4734347,
paulo@89 1804 darkslategray: 3100495,
paulo@89 1805 darkslategrey: 3100495,
paulo@89 1806 darkturquoise: 52945,
paulo@89 1807 darkviolet: 9699539,
paulo@89 1808 deeppink: 16716947,
paulo@89 1809 deepskyblue: 49151,
paulo@89 1810 dimgray: 6908265,
paulo@89 1811 dimgrey: 6908265,
paulo@89 1812 dodgerblue: 2003199,
paulo@89 1813 firebrick: 11674146,
paulo@89 1814 floralwhite: 16775920,
paulo@89 1815 forestgreen: 2263842,
paulo@89 1816 fuchsia: 16711935,
paulo@89 1817 gainsboro: 14474460,
paulo@89 1818 ghostwhite: 16316671,
paulo@89 1819 gold: 16766720,
paulo@89 1820 goldenrod: 14329120,
paulo@89 1821 gray: 8421504,
paulo@89 1822 green: 32768,
paulo@89 1823 greenyellow: 11403055,
paulo@89 1824 grey: 8421504,
paulo@89 1825 honeydew: 15794160,
paulo@89 1826 hotpink: 16738740,
paulo@89 1827 indianred: 13458524,
paulo@89 1828 indigo: 4915330,
paulo@89 1829 ivory: 16777200,
paulo@89 1830 khaki: 15787660,
paulo@89 1831 lavender: 15132410,
paulo@89 1832 lavenderblush: 16773365,
paulo@89 1833 lawngreen: 8190976,
paulo@89 1834 lemonchiffon: 16775885,
paulo@89 1835 lightblue: 11393254,
paulo@89 1836 lightcoral: 15761536,
paulo@89 1837 lightcyan: 14745599,
paulo@89 1838 lightgoldenrodyellow: 16448210,
paulo@89 1839 lightgray: 13882323,
paulo@89 1840 lightgreen: 9498256,
paulo@89 1841 lightgrey: 13882323,
paulo@89 1842 lightpink: 16758465,
paulo@89 1843 lightsalmon: 16752762,
paulo@89 1844 lightseagreen: 2142890,
paulo@89 1845 lightskyblue: 8900346,
paulo@89 1846 lightslategray: 7833753,
paulo@89 1847 lightslategrey: 7833753,
paulo@89 1848 lightsteelblue: 11584734,
paulo@89 1849 lightyellow: 16777184,
paulo@89 1850 lime: 65280,
paulo@89 1851 limegreen: 3329330,
paulo@89 1852 linen: 16445670,
paulo@89 1853 magenta: 16711935,
paulo@89 1854 maroon: 8388608,
paulo@89 1855 mediumaquamarine: 6737322,
paulo@89 1856 mediumblue: 205,
paulo@89 1857 mediumorchid: 12211667,
paulo@89 1858 mediumpurple: 9662683,
paulo@89 1859 mediumseagreen: 3978097,
paulo@89 1860 mediumslateblue: 8087790,
paulo@89 1861 mediumspringgreen: 64154,
paulo@89 1862 mediumturquoise: 4772300,
paulo@89 1863 mediumvioletred: 13047173,
paulo@89 1864 midnightblue: 1644912,
paulo@89 1865 mintcream: 16121850,
paulo@89 1866 mistyrose: 16770273,
paulo@89 1867 moccasin: 16770229,
paulo@89 1868 navajowhite: 16768685,
paulo@89 1869 navy: 128,
paulo@89 1870 oldlace: 16643558,
paulo@89 1871 olive: 8421376,
paulo@89 1872 olivedrab: 7048739,
paulo@89 1873 orange: 16753920,
paulo@89 1874 orangered: 16729344,
paulo@89 1875 orchid: 14315734,
paulo@89 1876 palegoldenrod: 15657130,
paulo@89 1877 palegreen: 10025880,
paulo@89 1878 paleturquoise: 11529966,
paulo@89 1879 palevioletred: 14381203,
paulo@89 1880 papayawhip: 16773077,
paulo@89 1881 peachpuff: 16767673,
paulo@89 1882 peru: 13468991,
paulo@89 1883 pink: 16761035,
paulo@89 1884 plum: 14524637,
paulo@89 1885 powderblue: 11591910,
paulo@89 1886 purple: 8388736,
paulo@89 1887 rebeccapurple: 6697881,
paulo@89 1888 red: 16711680,
paulo@89 1889 rosybrown: 12357519,
paulo@89 1890 royalblue: 4286945,
paulo@89 1891 saddlebrown: 9127187,
paulo@89 1892 salmon: 16416882,
paulo@89 1893 sandybrown: 16032864,
paulo@89 1894 seagreen: 3050327,
paulo@89 1895 seashell: 16774638,
paulo@89 1896 sienna: 10506797,
paulo@89 1897 silver: 12632256,
paulo@89 1898 skyblue: 8900331,
paulo@89 1899 slateblue: 6970061,
paulo@89 1900 slategray: 7372944,
paulo@89 1901 slategrey: 7372944,
paulo@89 1902 snow: 16775930,
paulo@89 1903 springgreen: 65407,
paulo@89 1904 steelblue: 4620980,
paulo@89 1905 tan: 13808780,
paulo@89 1906 teal: 32896,
paulo@89 1907 thistle: 14204888,
paulo@89 1908 tomato: 16737095,
paulo@89 1909 turquoise: 4251856,
paulo@89 1910 violet: 15631086,
paulo@89 1911 wheat: 16113331,
paulo@89 1912 white: 16777215,
paulo@89 1913 whitesmoke: 16119285,
paulo@89 1914 yellow: 16776960,
paulo@89 1915 yellowgreen: 10145074
paulo@89 1916 });
paulo@89 1917 d3_rgb_names.forEach(function(key, value) {
paulo@89 1918 d3_rgb_names.set(key, d3_rgbNumber(value));
paulo@89 1919 });
paulo@89 1920 function d3_functor(v) {
paulo@89 1921 return typeof v === "function" ? v : function() {
paulo@89 1922 return v;
paulo@89 1923 };
paulo@89 1924 }
paulo@89 1925 d3.functor = d3_functor;
paulo@89 1926 d3.xhr = d3_xhrType(d3_identity);
paulo@89 1927 function d3_xhrType(response) {
paulo@89 1928 return function(url, mimeType, callback) {
paulo@89 1929 if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType,
paulo@89 1930 mimeType = null;
paulo@89 1931 return d3_xhr(url, mimeType, response, callback);
paulo@89 1932 };
paulo@89 1933 }
paulo@89 1934 function d3_xhr(url, mimeType, response, callback) {
paulo@89 1935 var xhr = {}, dispatch = d3.dispatch("beforesend", "progress", "load", "error"), headers = {}, request = new XMLHttpRequest(), responseType = null;
paulo@89 1936 if (this.XDomainRequest && !("withCredentials" in request) && /^(http(s)?:)?\/\//.test(url)) request = new XDomainRequest();
paulo@89 1937 "onload" in request ? request.onload = request.onerror = respond : request.onreadystatechange = function() {
paulo@89 1938 request.readyState > 3 && respond();
paulo@89 1939 };
paulo@89 1940 function respond() {
paulo@89 1941 var status = request.status, result;
paulo@89 1942 if (!status && d3_xhrHasResponse(request) || status >= 200 && status < 300 || status === 304) {
paulo@89 1943 try {
paulo@89 1944 result = response.call(xhr, request);
paulo@89 1945 } catch (e) {
paulo@89 1946 dispatch.error.call(xhr, e);
paulo@89 1947 return;
paulo@89 1948 }
paulo@89 1949 dispatch.load.call(xhr, result);
paulo@89 1950 } else {
paulo@89 1951 dispatch.error.call(xhr, request);
paulo@89 1952 }
paulo@89 1953 }
paulo@89 1954 request.onprogress = function(event) {
paulo@89 1955 var o = d3.event;
paulo@89 1956 d3.event = event;
paulo@89 1957 try {
paulo@89 1958 dispatch.progress.call(xhr, request);
paulo@89 1959 } finally {
paulo@89 1960 d3.event = o;
paulo@89 1961 }
paulo@89 1962 };
paulo@89 1963 xhr.header = function(name, value) {
paulo@89 1964 name = (name + "").toLowerCase();
paulo@89 1965 if (arguments.length < 2) return headers[name];
paulo@89 1966 if (value == null) delete headers[name]; else headers[name] = value + "";
paulo@89 1967 return xhr;
paulo@89 1968 };
paulo@89 1969 xhr.mimeType = function(value) {
paulo@89 1970 if (!arguments.length) return mimeType;
paulo@89 1971 mimeType = value == null ? null : value + "";
paulo@89 1972 return xhr;
paulo@89 1973 };
paulo@89 1974 xhr.responseType = function(value) {
paulo@89 1975 if (!arguments.length) return responseType;
paulo@89 1976 responseType = value;
paulo@89 1977 return xhr;
paulo@89 1978 };
paulo@89 1979 xhr.response = function(value) {
paulo@89 1980 response = value;
paulo@89 1981 return xhr;
paulo@89 1982 };
paulo@89 1983 [ "get", "post" ].forEach(function(method) {
paulo@89 1984 xhr[method] = function() {
paulo@89 1985 return xhr.send.apply(xhr, [ method ].concat(d3_array(arguments)));
paulo@89 1986 };
paulo@89 1987 });
paulo@89 1988 xhr.send = function(method, data, callback) {
paulo@89 1989 if (arguments.length === 2 && typeof data === "function") callback = data, data = null;
paulo@89 1990 request.open(method, url, true);
paulo@89 1991 if (mimeType != null && !("accept" in headers)) headers["accept"] = mimeType + ",*/*";
paulo@89 1992 if (request.setRequestHeader) for (var name in headers) request.setRequestHeader(name, headers[name]);
paulo@89 1993 if (mimeType != null && request.overrideMimeType) request.overrideMimeType(mimeType);
paulo@89 1994 if (responseType != null) request.responseType = responseType;
paulo@89 1995 if (callback != null) xhr.on("error", callback).on("load", function(request) {
paulo@89 1996 callback(null, request);
paulo@89 1997 });
paulo@89 1998 dispatch.beforesend.call(xhr, request);
paulo@89 1999 request.send(data == null ? null : data);
paulo@89 2000 return xhr;
paulo@89 2001 };
paulo@89 2002 xhr.abort = function() {
paulo@89 2003 request.abort();
paulo@89 2004 return xhr;
paulo@89 2005 };
paulo@89 2006 d3.rebind(xhr, dispatch, "on");
paulo@89 2007 return callback == null ? xhr : xhr.get(d3_xhr_fixCallback(callback));
paulo@89 2008 }
paulo@89 2009 function d3_xhr_fixCallback(callback) {
paulo@89 2010 return callback.length === 1 ? function(error, request) {
paulo@89 2011 callback(error == null ? request : null);
paulo@89 2012 } : callback;
paulo@89 2013 }
paulo@89 2014 function d3_xhrHasResponse(request) {
paulo@89 2015 var type = request.responseType;
paulo@89 2016 return type && type !== "text" ? request.response : request.responseText;
paulo@89 2017 }
paulo@89 2018 d3.dsv = function(delimiter, mimeType) {
paulo@89 2019 var reFormat = new RegExp('["' + delimiter + "\n]"), delimiterCode = delimiter.charCodeAt(0);
paulo@89 2020 function dsv(url, row, callback) {
paulo@89 2021 if (arguments.length < 3) callback = row, row = null;
paulo@89 2022 var xhr = d3_xhr(url, mimeType, row == null ? response : typedResponse(row), callback);
paulo@89 2023 xhr.row = function(_) {
paulo@89 2024 return arguments.length ? xhr.response((row = _) == null ? response : typedResponse(_)) : row;
paulo@89 2025 };
paulo@89 2026 return xhr;
paulo@89 2027 }
paulo@89 2028 function response(request) {
paulo@89 2029 return dsv.parse(request.responseText);
paulo@89 2030 }
paulo@89 2031 function typedResponse(f) {
paulo@89 2032 return function(request) {
paulo@89 2033 return dsv.parse(request.responseText, f);
paulo@89 2034 };
paulo@89 2035 }
paulo@89 2036 dsv.parse = function(text, f) {
paulo@89 2037 var o;
paulo@89 2038 return dsv.parseRows(text, function(row, i) {
paulo@89 2039 if (o) return o(row, i - 1);
paulo@89 2040 var a = new Function("d", "return {" + row.map(function(name, i) {
paulo@89 2041 return JSON.stringify(name) + ": d[" + i + "]";
paulo@89 2042 }).join(",") + "}");
paulo@89 2043 o = f ? function(row, i) {
paulo@89 2044 return f(a(row), i);
paulo@89 2045 } : a;
paulo@89 2046 });
paulo@89 2047 };
paulo@89 2048 dsv.parseRows = function(text, f) {
paulo@89 2049 var EOL = {}, EOF = {}, rows = [], N = text.length, I = 0, n = 0, t, eol;
paulo@89 2050 function token() {
paulo@89 2051 if (I >= N) return EOF;
paulo@89 2052 if (eol) return eol = false, EOL;
paulo@89 2053 var j = I;
paulo@89 2054 if (text.charCodeAt(j) === 34) {
paulo@89 2055 var i = j;
paulo@89 2056 while (i++ < N) {
paulo@89 2057 if (text.charCodeAt(i) === 34) {
paulo@89 2058 if (text.charCodeAt(i + 1) !== 34) break;
paulo@89 2059 ++i;
paulo@89 2060 }
paulo@89 2061 }
paulo@89 2062 I = i + 2;
paulo@89 2063 var c = text.charCodeAt(i + 1);
paulo@89 2064 if (c === 13) {
paulo@89 2065 eol = true;
paulo@89 2066 if (text.charCodeAt(i + 2) === 10) ++I;
paulo@89 2067 } else if (c === 10) {
paulo@89 2068 eol = true;
paulo@89 2069 }
paulo@89 2070 return text.slice(j + 1, i).replace(/""/g, '"');
paulo@89 2071 }
paulo@89 2072 while (I < N) {
paulo@89 2073 var c = text.charCodeAt(I++), k = 1;
paulo@89 2074 if (c === 10) eol = true; else if (c === 13) {
paulo@89 2075 eol = true;
paulo@89 2076 if (text.charCodeAt(I) === 10) ++I, ++k;
paulo@89 2077 } else if (c !== delimiterCode) continue;
paulo@89 2078 return text.slice(j, I - k);
paulo@89 2079 }
paulo@89 2080 return text.slice(j);
paulo@89 2081 }
paulo@89 2082 while ((t = token()) !== EOF) {
paulo@89 2083 var a = [];
paulo@89 2084 while (t !== EOL && t !== EOF) {
paulo@89 2085 a.push(t);
paulo@89 2086 t = token();
paulo@89 2087 }
paulo@89 2088 if (f && (a = f(a, n++)) == null) continue;
paulo@89 2089 rows.push(a);
paulo@89 2090 }
paulo@89 2091 return rows;
paulo@89 2092 };
paulo@89 2093 dsv.format = function(rows) {
paulo@89 2094 if (Array.isArray(rows[0])) return dsv.formatRows(rows);
paulo@89 2095 var fieldSet = new d3_Set(), fields = [];
paulo@89 2096 rows.forEach(function(row) {
paulo@89 2097 for (var field in row) {
paulo@89 2098 if (!fieldSet.has(field)) {
paulo@89 2099 fields.push(fieldSet.add(field));
paulo@89 2100 }
paulo@89 2101 }
paulo@89 2102 });
paulo@89 2103 return [ fields.map(formatValue).join(delimiter) ].concat(rows.map(function(row) {
paulo@89 2104 return fields.map(function(field) {
paulo@89 2105 return formatValue(row[field]);
paulo@89 2106 }).join(delimiter);
paulo@89 2107 })).join("\n");
paulo@89 2108 };
paulo@89 2109 dsv.formatRows = function(rows) {
paulo@89 2110 return rows.map(formatRow).join("\n");
paulo@89 2111 };
paulo@89 2112 function formatRow(row) {
paulo@89 2113 return row.map(formatValue).join(delimiter);
paulo@89 2114 }
paulo@89 2115 function formatValue(text) {
paulo@89 2116 return reFormat.test(text) ? '"' + text.replace(/\"/g, '""') + '"' : text;
paulo@89 2117 }
paulo@89 2118 return dsv;
paulo@89 2119 };
paulo@89 2120 d3.csv = d3.dsv(",", "text/csv");
paulo@89 2121 d3.tsv = d3.dsv(" ", "text/tab-separated-values");
paulo@89 2122 var d3_timer_queueHead, d3_timer_queueTail, d3_timer_interval, d3_timer_timeout, d3_timer_frame = this[d3_vendorSymbol(this, "requestAnimationFrame")] || function(callback) {
paulo@89 2123 setTimeout(callback, 17);
paulo@89 2124 };
paulo@89 2125 d3.timer = function() {
paulo@89 2126 d3_timer.apply(this, arguments);
paulo@89 2127 };
paulo@89 2128 function d3_timer(callback, delay, then) {
paulo@89 2129 var n = arguments.length;
paulo@89 2130 if (n < 2) delay = 0;
paulo@89 2131 if (n < 3) then = Date.now();
paulo@89 2132 var time = then + delay, timer = {
paulo@89 2133 c: callback,
paulo@89 2134 t: time,
paulo@89 2135 n: null
paulo@89 2136 };
paulo@89 2137 if (d3_timer_queueTail) d3_timer_queueTail.n = timer; else d3_timer_queueHead = timer;
paulo@89 2138 d3_timer_queueTail = timer;
paulo@89 2139 if (!d3_timer_interval) {
paulo@89 2140 d3_timer_timeout = clearTimeout(d3_timer_timeout);
paulo@89 2141 d3_timer_interval = 1;
paulo@89 2142 d3_timer_frame(d3_timer_step);
paulo@89 2143 }
paulo@89 2144 return timer;
paulo@89 2145 }
paulo@89 2146 function d3_timer_step() {
paulo@89 2147 var now = d3_timer_mark(), delay = d3_timer_sweep() - now;
paulo@89 2148 if (delay > 24) {
paulo@89 2149 if (isFinite(delay)) {
paulo@89 2150 clearTimeout(d3_timer_timeout);
paulo@89 2151 d3_timer_timeout = setTimeout(d3_timer_step, delay);
paulo@89 2152 }
paulo@89 2153 d3_timer_interval = 0;
paulo@89 2154 } else {
paulo@89 2155 d3_timer_interval = 1;
paulo@89 2156 d3_timer_frame(d3_timer_step);
paulo@89 2157 }
paulo@89 2158 }
paulo@89 2159 d3.timer.flush = function() {
paulo@89 2160 d3_timer_mark();
paulo@89 2161 d3_timer_sweep();
paulo@89 2162 };
paulo@89 2163 function d3_timer_mark() {
paulo@89 2164 var now = Date.now(), timer = d3_timer_queueHead;
paulo@89 2165 while (timer) {
paulo@89 2166 if (now >= timer.t && timer.c(now - timer.t)) timer.c = null;
paulo@89 2167 timer = timer.n;
paulo@89 2168 }
paulo@89 2169 return now;
paulo@89 2170 }
paulo@89 2171 function d3_timer_sweep() {
paulo@89 2172 var t0, t1 = d3_timer_queueHead, time = Infinity;
paulo@89 2173 while (t1) {
paulo@89 2174 if (t1.c) {
paulo@89 2175 if (t1.t < time) time = t1.t;
paulo@89 2176 t1 = (t0 = t1).n;
paulo@89 2177 } else {
paulo@89 2178 t1 = t0 ? t0.n = t1.n : d3_timer_queueHead = t1.n;
paulo@89 2179 }
paulo@89 2180 }
paulo@89 2181 d3_timer_queueTail = t0;
paulo@89 2182 return time;
paulo@89 2183 }
paulo@89 2184 function d3_format_precision(x, p) {
paulo@89 2185 return p - (x ? Math.ceil(Math.log(x) / Math.LN10) : 1);
paulo@89 2186 }
paulo@89 2187 d3.round = function(x, n) {
paulo@89 2188 return n ? Math.round(x * (n = Math.pow(10, n))) / n : Math.round(x);
paulo@89 2189 };
paulo@89 2190 var d3_formatPrefixes = [ "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" ].map(d3_formatPrefix);
paulo@89 2191 d3.formatPrefix = function(value, precision) {
paulo@89 2192 var i = 0;
paulo@89 2193 if (value = +value) {
paulo@89 2194 if (value < 0) value *= -1;
paulo@89 2195 if (precision) value = d3.round(value, d3_format_precision(value, precision));
paulo@89 2196 i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10);
paulo@89 2197 i = Math.max(-24, Math.min(24, Math.floor((i - 1) / 3) * 3));
paulo@89 2198 }
paulo@89 2199 return d3_formatPrefixes[8 + i / 3];
paulo@89 2200 };
paulo@89 2201 function d3_formatPrefix(d, i) {
paulo@89 2202 var k = Math.pow(10, abs(8 - i) * 3);
paulo@89 2203 return {
paulo@89 2204 scale: i > 8 ? function(d) {
paulo@89 2205 return d / k;
paulo@89 2206 } : function(d) {
paulo@89 2207 return d * k;
paulo@89 2208 },
paulo@89 2209 symbol: d
paulo@89 2210 };
paulo@89 2211 }
paulo@89 2212 function d3_locale_numberFormat(locale) {
paulo@89 2213 var locale_decimal = locale.decimal, locale_thousands = locale.thousands, locale_grouping = locale.grouping, locale_currency = locale.currency, formatGroup = locale_grouping && locale_thousands ? function(value, width) {
paulo@89 2214 var i = value.length, t = [], j = 0, g = locale_grouping[0], length = 0;
paulo@89 2215 while (i > 0 && g > 0) {
paulo@89 2216 if (length + g + 1 > width) g = Math.max(1, width - length);
paulo@89 2217 t.push(value.substring(i -= g, i + g));
paulo@89 2218 if ((length += g + 1) > width) break;
paulo@89 2219 g = locale_grouping[j = (j + 1) % locale_grouping.length];
paulo@89 2220 }
paulo@89 2221 return t.reverse().join(locale_thousands);
paulo@89 2222 } : d3_identity;
paulo@89 2223 return function(specifier) {
paulo@89 2224 var match = d3_format_re.exec(specifier), fill = match[1] || " ", align = match[2] || ">", sign = match[3] || "-", symbol = match[4] || "", zfill = match[5], width = +match[6], comma = match[7], precision = match[8], type = match[9], scale = 1, prefix = "", suffix = "", integer = false, exponent = true;
paulo@89 2225 if (precision) precision = +precision.substring(1);
paulo@89 2226 if (zfill || fill === "0" && align === "=") {
paulo@89 2227 zfill = fill = "0";
paulo@89 2228 align = "=";
paulo@89 2229 }
paulo@89 2230 switch (type) {
paulo@89 2231 case "n":
paulo@89 2232 comma = true;
paulo@89 2233 type = "g";
paulo@89 2234 break;
paulo@89 2235
paulo@89 2236 case "%":
paulo@89 2237 scale = 100;
paulo@89 2238 suffix = "%";
paulo@89 2239 type = "f";
paulo@89 2240 break;
paulo@89 2241
paulo@89 2242 case "p":
paulo@89 2243 scale = 100;
paulo@89 2244 suffix = "%";
paulo@89 2245 type = "r";
paulo@89 2246 break;
paulo@89 2247
paulo@89 2248 case "b":
paulo@89 2249 case "o":
paulo@89 2250 case "x":
paulo@89 2251 case "X":
paulo@89 2252 if (symbol === "#") prefix = "0" + type.toLowerCase();
paulo@89 2253
paulo@89 2254 case "c":
paulo@89 2255 exponent = false;
paulo@89 2256
paulo@89 2257 case "d":
paulo@89 2258 integer = true;
paulo@89 2259 precision = 0;
paulo@89 2260 break;
paulo@89 2261
paulo@89 2262 case "s":
paulo@89 2263 scale = -1;
paulo@89 2264 type = "r";
paulo@89 2265 break;
paulo@89 2266 }
paulo@89 2267 if (symbol === "$") prefix = locale_currency[0], suffix = locale_currency[1];
paulo@89 2268 if (type == "r" && !precision) type = "g";
paulo@89 2269 if (precision != null) {
paulo@89 2270 if (type == "g") precision = Math.max(1, Math.min(21, precision)); else if (type == "e" || type == "f") precision = Math.max(0, Math.min(20, precision));
paulo@89 2271 }
paulo@89 2272 type = d3_format_types.get(type) || d3_format_typeDefault;
paulo@89 2273 var zcomma = zfill && comma;
paulo@89 2274 return function(value) {
paulo@89 2275 var fullSuffix = suffix;
paulo@89 2276 if (integer && value % 1) return "";
paulo@89 2277 var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign === "-" ? "" : sign;
paulo@89 2278 if (scale < 0) {
paulo@89 2279 var unit = d3.formatPrefix(value, precision);
paulo@89 2280 value = unit.scale(value);
paulo@89 2281 fullSuffix = unit.symbol + suffix;
paulo@89 2282 } else {
paulo@89 2283 value *= scale;
paulo@89 2284 }
paulo@89 2285 value = type(value, precision);
paulo@89 2286 var i = value.lastIndexOf("."), before, after;
paulo@89 2287 if (i < 0) {
paulo@89 2288 var j = exponent ? value.lastIndexOf("e") : -1;
paulo@89 2289 if (j < 0) before = value, after = ""; else before = value.substring(0, j), after = value.substring(j);
paulo@89 2290 } else {
paulo@89 2291 before = value.substring(0, i);
paulo@89 2292 after = locale_decimal + value.substring(i + 1);
paulo@89 2293 }
paulo@89 2294 if (!zfill && comma) before = formatGroup(before, Infinity);
paulo@89 2295 var length = prefix.length + before.length + after.length + (zcomma ? 0 : negative.length), padding = length < width ? new Array(length = width - length + 1).join(fill) : "";
paulo@89 2296 if (zcomma) before = formatGroup(padding + before, padding.length ? width - after.length : Infinity);
paulo@89 2297 negative += prefix;
paulo@89 2298 value = before + after;
paulo@89 2299 return (align === "<" ? negative + value + padding : align === ">" ? padding + negative + value : align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length) : negative + (zcomma ? value : padding + value)) + fullSuffix;
paulo@89 2300 };
paulo@89 2301 };
paulo@89 2302 }
paulo@89 2303 var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?([$#])?(0)?(\d+)?(,)?(\.-?\d+)?([a-z%])?/i;
paulo@89 2304 var d3_format_types = d3.map({
paulo@89 2305 b: function(x) {
paulo@89 2306 return x.toString(2);
paulo@89 2307 },
paulo@89 2308 c: function(x) {
paulo@89 2309 return String.fromCharCode(x);
paulo@89 2310 },
paulo@89 2311 o: function(x) {
paulo@89 2312 return x.toString(8);
paulo@89 2313 },
paulo@89 2314 x: function(x) {
paulo@89 2315 return x.toString(16);
paulo@89 2316 },
paulo@89 2317 X: function(x) {
paulo@89 2318 return x.toString(16).toUpperCase();
paulo@89 2319 },
paulo@89 2320 g: function(x, p) {
paulo@89 2321 return x.toPrecision(p);
paulo@89 2322 },
paulo@89 2323 e: function(x, p) {
paulo@89 2324 return x.toExponential(p);
paulo@89 2325 },
paulo@89 2326 f: function(x, p) {
paulo@89 2327 return x.toFixed(p);
paulo@89 2328 },
paulo@89 2329 r: function(x, p) {
paulo@89 2330 return (x = d3.round(x, d3_format_precision(x, p))).toFixed(Math.max(0, Math.min(20, d3_format_precision(x * (1 + 1e-15), p))));
paulo@89 2331 }
paulo@89 2332 });
paulo@89 2333 function d3_format_typeDefault(x) {
paulo@89 2334 return x + "";
paulo@89 2335 }
paulo@89 2336 var d3_time = d3.time = {}, d3_date = Date;
paulo@89 2337 function d3_date_utc() {
paulo@89 2338 this._ = new Date(arguments.length > 1 ? Date.UTC.apply(this, arguments) : arguments[0]);
paulo@89 2339 }
paulo@89 2340 d3_date_utc.prototype = {
paulo@89 2341 getDate: function() {
paulo@89 2342 return this._.getUTCDate();
paulo@89 2343 },
paulo@89 2344 getDay: function() {
paulo@89 2345 return this._.getUTCDay();
paulo@89 2346 },
paulo@89 2347 getFullYear: function() {
paulo@89 2348 return this._.getUTCFullYear();
paulo@89 2349 },
paulo@89 2350 getHours: function() {
paulo@89 2351 return this._.getUTCHours();
paulo@89 2352 },
paulo@89 2353 getMilliseconds: function() {
paulo@89 2354 return this._.getUTCMilliseconds();
paulo@89 2355 },
paulo@89 2356 getMinutes: function() {
paulo@89 2357 return this._.getUTCMinutes();
paulo@89 2358 },
paulo@89 2359 getMonth: function() {
paulo@89 2360 return this._.getUTCMonth();
paulo@89 2361 },
paulo@89 2362 getSeconds: function() {
paulo@89 2363 return this._.getUTCSeconds();
paulo@89 2364 },
paulo@89 2365 getTime: function() {
paulo@89 2366 return this._.getTime();
paulo@89 2367 },
paulo@89 2368 getTimezoneOffset: function() {
paulo@89 2369 return 0;
paulo@89 2370 },
paulo@89 2371 valueOf: function() {
paulo@89 2372 return this._.valueOf();
paulo@89 2373 },
paulo@89 2374 setDate: function() {
paulo@89 2375 d3_time_prototype.setUTCDate.apply(this._, arguments);
paulo@89 2376 },
paulo@89 2377 setDay: function() {
paulo@89 2378 d3_time_prototype.setUTCDay.apply(this._, arguments);
paulo@89 2379 },
paulo@89 2380 setFullYear: function() {
paulo@89 2381 d3_time_prototype.setUTCFullYear.apply(this._, arguments);
paulo@89 2382 },
paulo@89 2383 setHours: function() {
paulo@89 2384 d3_time_prototype.setUTCHours.apply(this._, arguments);
paulo@89 2385 },
paulo@89 2386 setMilliseconds: function() {
paulo@89 2387 d3_time_prototype.setUTCMilliseconds.apply(this._, arguments);
paulo@89 2388 },
paulo@89 2389 setMinutes: function() {
paulo@89 2390 d3_time_prototype.setUTCMinutes.apply(this._, arguments);
paulo@89 2391 },
paulo@89 2392 setMonth: function() {
paulo@89 2393 d3_time_prototype.setUTCMonth.apply(this._, arguments);
paulo@89 2394 },
paulo@89 2395 setSeconds: function() {
paulo@89 2396 d3_time_prototype.setUTCSeconds.apply(this._, arguments);
paulo@89 2397 },
paulo@89 2398 setTime: function() {
paulo@89 2399 d3_time_prototype.setTime.apply(this._, arguments);
paulo@89 2400 }
paulo@89 2401 };
paulo@89 2402 var d3_time_prototype = Date.prototype;
paulo@89 2403 function d3_time_interval(local, step, number) {
paulo@89 2404 function round(date) {
paulo@89 2405 var d0 = local(date), d1 = offset(d0, 1);
paulo@89 2406 return date - d0 < d1 - date ? d0 : d1;
paulo@89 2407 }
paulo@89 2408 function ceil(date) {
paulo@89 2409 step(date = local(new d3_date(date - 1)), 1);
paulo@89 2410 return date;
paulo@89 2411 }
paulo@89 2412 function offset(date, k) {
paulo@89 2413 step(date = new d3_date(+date), k);
paulo@89 2414 return date;
paulo@89 2415 }
paulo@89 2416 function range(t0, t1, dt) {
paulo@89 2417 var time = ceil(t0), times = [];
paulo@89 2418 if (dt > 1) {
paulo@89 2419 while (time < t1) {
paulo@89 2420 if (!(number(time) % dt)) times.push(new Date(+time));
paulo@89 2421 step(time, 1);
paulo@89 2422 }
paulo@89 2423 } else {
paulo@89 2424 while (time < t1) times.push(new Date(+time)), step(time, 1);
paulo@89 2425 }
paulo@89 2426 return times;
paulo@89 2427 }
paulo@89 2428 function range_utc(t0, t1, dt) {
paulo@89 2429 try {
paulo@89 2430 d3_date = d3_date_utc;
paulo@89 2431 var utc = new d3_date_utc();
paulo@89 2432 utc._ = t0;
paulo@89 2433 return range(utc, t1, dt);
paulo@89 2434 } finally {
paulo@89 2435 d3_date = Date;
paulo@89 2436 }
paulo@89 2437 }
paulo@89 2438 local.floor = local;
paulo@89 2439 local.round = round;
paulo@89 2440 local.ceil = ceil;
paulo@89 2441 local.offset = offset;
paulo@89 2442 local.range = range;
paulo@89 2443 var utc = local.utc = d3_time_interval_utc(local);
paulo@89 2444 utc.floor = utc;
paulo@89 2445 utc.round = d3_time_interval_utc(round);
paulo@89 2446 utc.ceil = d3_time_interval_utc(ceil);
paulo@89 2447 utc.offset = d3_time_interval_utc(offset);
paulo@89 2448 utc.range = range_utc;
paulo@89 2449 return local;
paulo@89 2450 }
paulo@89 2451 function d3_time_interval_utc(method) {
paulo@89 2452 return function(date, k) {
paulo@89 2453 try {
paulo@89 2454 d3_date = d3_date_utc;
paulo@89 2455 var utc = new d3_date_utc();
paulo@89 2456 utc._ = date;
paulo@89 2457 return method(utc, k)._;
paulo@89 2458 } finally {
paulo@89 2459 d3_date = Date;
paulo@89 2460 }
paulo@89 2461 };
paulo@89 2462 }
paulo@89 2463 d3_time.year = d3_time_interval(function(date) {
paulo@89 2464 date = d3_time.day(date);
paulo@89 2465 date.setMonth(0, 1);
paulo@89 2466 return date;
paulo@89 2467 }, function(date, offset) {
paulo@89 2468 date.setFullYear(date.getFullYear() + offset);
paulo@89 2469 }, function(date) {
paulo@89 2470 return date.getFullYear();
paulo@89 2471 });
paulo@89 2472 d3_time.years = d3_time.year.range;
paulo@89 2473 d3_time.years.utc = d3_time.year.utc.range;
paulo@89 2474 d3_time.day = d3_time_interval(function(date) {
paulo@89 2475 var day = new d3_date(2e3, 0);
paulo@89 2476 day.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
paulo@89 2477 return day;
paulo@89 2478 }, function(date, offset) {
paulo@89 2479 date.setDate(date.getDate() + offset);
paulo@89 2480 }, function(date) {
paulo@89 2481 return date.getDate() - 1;
paulo@89 2482 });
paulo@89 2483 d3_time.days = d3_time.day.range;
paulo@89 2484 d3_time.days.utc = d3_time.day.utc.range;
paulo@89 2485 d3_time.dayOfYear = function(date) {
paulo@89 2486 var year = d3_time.year(date);
paulo@89 2487 return Math.floor((date - year - (date.getTimezoneOffset() - year.getTimezoneOffset()) * 6e4) / 864e5);
paulo@89 2488 };
paulo@89 2489 [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ].forEach(function(day, i) {
paulo@89 2490 i = 7 - i;
paulo@89 2491 var interval = d3_time[day] = d3_time_interval(function(date) {
paulo@89 2492 (date = d3_time.day(date)).setDate(date.getDate() - (date.getDay() + i) % 7);
paulo@89 2493 return date;
paulo@89 2494 }, function(date, offset) {
paulo@89 2495 date.setDate(date.getDate() + Math.floor(offset) * 7);
paulo@89 2496 }, function(date) {
paulo@89 2497 var day = d3_time.year(date).getDay();
paulo@89 2498 return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7) - (day !== i);
paulo@89 2499 });
paulo@89 2500 d3_time[day + "s"] = interval.range;
paulo@89 2501 d3_time[day + "s"].utc = interval.utc.range;
paulo@89 2502 d3_time[day + "OfYear"] = function(date) {
paulo@89 2503 var day = d3_time.year(date).getDay();
paulo@89 2504 return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7);
paulo@89 2505 };
paulo@89 2506 });
paulo@89 2507 d3_time.week = d3_time.sunday;
paulo@89 2508 d3_time.weeks = d3_time.sunday.range;
paulo@89 2509 d3_time.weeks.utc = d3_time.sunday.utc.range;
paulo@89 2510 d3_time.weekOfYear = d3_time.sundayOfYear;
paulo@89 2511 function d3_locale_timeFormat(locale) {
paulo@89 2512 var locale_dateTime = locale.dateTime, locale_date = locale.date, locale_time = locale.time, locale_periods = locale.periods, locale_days = locale.days, locale_shortDays = locale.shortDays, locale_months = locale.months, locale_shortMonths = locale.shortMonths;
paulo@89 2513 function d3_time_format(template) {
paulo@89 2514 var n = template.length;
paulo@89 2515 function format(date) {
paulo@89 2516 var string = [], i = -1, j = 0, c, p, f;
paulo@89 2517 while (++i < n) {
paulo@89 2518 if (template.charCodeAt(i) === 37) {
paulo@89 2519 string.push(template.slice(j, i));
paulo@89 2520 if ((p = d3_time_formatPads[c = template.charAt(++i)]) != null) c = template.charAt(++i);
paulo@89 2521 if (f = d3_time_formats[c]) c = f(date, p == null ? c === "e" ? " " : "0" : p);
paulo@89 2522 string.push(c);
paulo@89 2523 j = i + 1;
paulo@89 2524 }
paulo@89 2525 }
paulo@89 2526 string.push(template.slice(j, i));
paulo@89 2527 return string.join("");
paulo@89 2528 }
paulo@89 2529 format.parse = function(string) {
paulo@89 2530 var d = {
paulo@89 2531 y: 1900,
paulo@89 2532 m: 0,
paulo@89 2533 d: 1,
paulo@89 2534 H: 0,
paulo@89 2535 M: 0,
paulo@89 2536 S: 0,
paulo@89 2537 L: 0,
paulo@89 2538 Z: null
paulo@89 2539 }, i = d3_time_parse(d, template, string, 0);
paulo@89 2540 if (i != string.length) return null;
paulo@89 2541 if ("p" in d) d.H = d.H % 12 + d.p * 12;
paulo@89 2542 var localZ = d.Z != null && d3_date !== d3_date_utc, date = new (localZ ? d3_date_utc : d3_date)();
paulo@89 2543 if ("j" in d) date.setFullYear(d.y, 0, d.j); else if ("W" in d || "U" in d) {
paulo@89 2544 if (!("w" in d)) d.w = "W" in d ? 1 : 0;
paulo@89 2545 date.setFullYear(d.y, 0, 1);
paulo@89 2546 date.setFullYear(d.y, 0, "W" in d ? (d.w + 6) % 7 + d.W * 7 - (date.getDay() + 5) % 7 : d.w + d.U * 7 - (date.getDay() + 6) % 7);
paulo@89 2547 } else date.setFullYear(d.y, d.m, d.d);
paulo@89 2548 date.setHours(d.H + (d.Z / 100 | 0), d.M + d.Z % 100, d.S, d.L);
paulo@89 2549 return localZ ? date._ : date;
paulo@89 2550 };
paulo@89 2551 format.toString = function() {
paulo@89 2552 return template;
paulo@89 2553 };
paulo@89 2554 return format;
paulo@89 2555 }
paulo@89 2556 function d3_time_parse(date, template, string, j) {
paulo@89 2557 var c, p, t, i = 0, n = template.length, m = string.length;
paulo@89 2558 while (i < n) {
paulo@89 2559 if (j >= m) return -1;
paulo@89 2560 c = template.charCodeAt(i++);
paulo@89 2561 if (c === 37) {
paulo@89 2562 t = template.charAt(i++);
paulo@89 2563 p = d3_time_parsers[t in d3_time_formatPads ? template.charAt(i++) : t];
paulo@89 2564 if (!p || (j = p(date, string, j)) < 0) return -1;
paulo@89 2565 } else if (c != string.charCodeAt(j++)) {
paulo@89 2566 return -1;
paulo@89 2567 }
paulo@89 2568 }
paulo@89 2569 return j;
paulo@89 2570 }
paulo@89 2571 d3_time_format.utc = function(template) {
paulo@89 2572 var local = d3_time_format(template);
paulo@89 2573 function format(date) {
paulo@89 2574 try {
paulo@89 2575 d3_date = d3_date_utc;
paulo@89 2576 var utc = new d3_date();
paulo@89 2577 utc._ = date;
paulo@89 2578 return local(utc);
paulo@89 2579 } finally {
paulo@89 2580 d3_date = Date;
paulo@89 2581 }
paulo@89 2582 }
paulo@89 2583 format.parse = function(string) {
paulo@89 2584 try {
paulo@89 2585 d3_date = d3_date_utc;
paulo@89 2586 var date = local.parse(string);
paulo@89 2587 return date && date._;
paulo@89 2588 } finally {
paulo@89 2589 d3_date = Date;
paulo@89 2590 }
paulo@89 2591 };
paulo@89 2592 format.toString = local.toString;
paulo@89 2593 return format;
paulo@89 2594 };
paulo@89 2595 d3_time_format.multi = d3_time_format.utc.multi = d3_time_formatMulti;
paulo@89 2596 var d3_time_periodLookup = d3.map(), d3_time_dayRe = d3_time_formatRe(locale_days), d3_time_dayLookup = d3_time_formatLookup(locale_days), d3_time_dayAbbrevRe = d3_time_formatRe(locale_shortDays), d3_time_dayAbbrevLookup = d3_time_formatLookup(locale_shortDays), d3_time_monthRe = d3_time_formatRe(locale_months), d3_time_monthLookup = d3_time_formatLookup(locale_months), d3_time_monthAbbrevRe = d3_time_formatRe(locale_shortMonths), d3_time_monthAbbrevLookup = d3_time_formatLookup(locale_shortMonths);
paulo@89 2597 locale_periods.forEach(function(p, i) {
paulo@89 2598 d3_time_periodLookup.set(p.toLowerCase(), i);
paulo@89 2599 });
paulo@89 2600 var d3_time_formats = {
paulo@89 2601 a: function(d) {
paulo@89 2602 return locale_shortDays[d.getDay()];
paulo@89 2603 },
paulo@89 2604 A: function(d) {
paulo@89 2605 return locale_days[d.getDay()];
paulo@89 2606 },
paulo@89 2607 b: function(d) {
paulo@89 2608 return locale_shortMonths[d.getMonth()];
paulo@89 2609 },
paulo@89 2610 B: function(d) {
paulo@89 2611 return locale_months[d.getMonth()];
paulo@89 2612 },
paulo@89 2613 c: d3_time_format(locale_dateTime),
paulo@89 2614 d: function(d, p) {
paulo@89 2615 return d3_time_formatPad(d.getDate(), p, 2);
paulo@89 2616 },
paulo@89 2617 e: function(d, p) {
paulo@89 2618 return d3_time_formatPad(d.getDate(), p, 2);
paulo@89 2619 },
paulo@89 2620 H: function(d, p) {
paulo@89 2621 return d3_time_formatPad(d.getHours(), p, 2);
paulo@89 2622 },
paulo@89 2623 I: function(d, p) {
paulo@89 2624 return d3_time_formatPad(d.getHours() % 12 || 12, p, 2);
paulo@89 2625 },
paulo@89 2626 j: function(d, p) {
paulo@89 2627 return d3_time_formatPad(1 + d3_time.dayOfYear(d), p, 3);
paulo@89 2628 },
paulo@89 2629 L: function(d, p) {
paulo@89 2630 return d3_time_formatPad(d.getMilliseconds(), p, 3);
paulo@89 2631 },
paulo@89 2632 m: function(d, p) {
paulo@89 2633 return d3_time_formatPad(d.getMonth() + 1, p, 2);
paulo@89 2634 },
paulo@89 2635 M: function(d, p) {
paulo@89 2636 return d3_time_formatPad(d.getMinutes(), p, 2);
paulo@89 2637 },
paulo@89 2638 p: function(d) {
paulo@89 2639 return locale_periods[+(d.getHours() >= 12)];
paulo@89 2640 },
paulo@89 2641 S: function(d, p) {
paulo@89 2642 return d3_time_formatPad(d.getSeconds(), p, 2);
paulo@89 2643 },
paulo@89 2644 U: function(d, p) {
paulo@89 2645 return d3_time_formatPad(d3_time.sundayOfYear(d), p, 2);
paulo@89 2646 },
paulo@89 2647 w: function(d) {
paulo@89 2648 return d.getDay();
paulo@89 2649 },
paulo@89 2650 W: function(d, p) {
paulo@89 2651 return d3_time_formatPad(d3_time.mondayOfYear(d), p, 2);
paulo@89 2652 },
paulo@89 2653 x: d3_time_format(locale_date),
paulo@89 2654 X: d3_time_format(locale_time),
paulo@89 2655 y: function(d, p) {
paulo@89 2656 return d3_time_formatPad(d.getFullYear() % 100, p, 2);
paulo@89 2657 },
paulo@89 2658 Y: function(d, p) {
paulo@89 2659 return d3_time_formatPad(d.getFullYear() % 1e4, p, 4);
paulo@89 2660 },
paulo@89 2661 Z: d3_time_zone,
paulo@89 2662 "%": function() {
paulo@89 2663 return "%";
paulo@89 2664 }
paulo@89 2665 };
paulo@89 2666 var d3_time_parsers = {
paulo@89 2667 a: d3_time_parseWeekdayAbbrev,
paulo@89 2668 A: d3_time_parseWeekday,
paulo@89 2669 b: d3_time_parseMonthAbbrev,
paulo@89 2670 B: d3_time_parseMonth,
paulo@89 2671 c: d3_time_parseLocaleFull,
paulo@89 2672 d: d3_time_parseDay,
paulo@89 2673 e: d3_time_parseDay,
paulo@89 2674 H: d3_time_parseHour24,
paulo@89 2675 I: d3_time_parseHour24,
paulo@89 2676 j: d3_time_parseDayOfYear,
paulo@89 2677 L: d3_time_parseMilliseconds,
paulo@89 2678 m: d3_time_parseMonthNumber,
paulo@89 2679 M: d3_time_parseMinutes,
paulo@89 2680 p: d3_time_parseAmPm,
paulo@89 2681 S: d3_time_parseSeconds,
paulo@89 2682 U: d3_time_parseWeekNumberSunday,
paulo@89 2683 w: d3_time_parseWeekdayNumber,
paulo@89 2684 W: d3_time_parseWeekNumberMonday,
paulo@89 2685 x: d3_time_parseLocaleDate,
paulo@89 2686 X: d3_time_parseLocaleTime,
paulo@89 2687 y: d3_time_parseYear,
paulo@89 2688 Y: d3_time_parseFullYear,
paulo@89 2689 Z: d3_time_parseZone,
paulo@89 2690 "%": d3_time_parseLiteralPercent
paulo@89 2691 };
paulo@89 2692 function d3_time_parseWeekdayAbbrev(date, string, i) {
paulo@89 2693 d3_time_dayAbbrevRe.lastIndex = 0;
paulo@89 2694 var n = d3_time_dayAbbrevRe.exec(string.slice(i));
paulo@89 2695 return n ? (date.w = d3_time_dayAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1;
paulo@89 2696 }
paulo@89 2697 function d3_time_parseWeekday(date, string, i) {
paulo@89 2698 d3_time_dayRe.lastIndex = 0;
paulo@89 2699 var n = d3_time_dayRe.exec(string.slice(i));
paulo@89 2700 return n ? (date.w = d3_time_dayLookup.get(n[0].toLowerCase()), i + n[0].length) : -1;
paulo@89 2701 }
paulo@89 2702 function d3_time_parseMonthAbbrev(date, string, i) {
paulo@89 2703 d3_time_monthAbbrevRe.lastIndex = 0;
paulo@89 2704 var n = d3_time_monthAbbrevRe.exec(string.slice(i));
paulo@89 2705 return n ? (date.m = d3_time_monthAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1;
paulo@89 2706 }
paulo@89 2707 function d3_time_parseMonth(date, string, i) {
paulo@89 2708 d3_time_monthRe.lastIndex = 0;
paulo@89 2709 var n = d3_time_monthRe.exec(string.slice(i));
paulo@89 2710 return n ? (date.m = d3_time_monthLookup.get(n[0].toLowerCase()), i + n[0].length) : -1;
paulo@89 2711 }
paulo@89 2712 function d3_time_parseLocaleFull(date, string, i) {
paulo@89 2713 return d3_time_parse(date, d3_time_formats.c.toString(), string, i);
paulo@89 2714 }
paulo@89 2715 function d3_time_parseLocaleDate(date, string, i) {
paulo@89 2716 return d3_time_parse(date, d3_time_formats.x.toString(), string, i);
paulo@89 2717 }
paulo@89 2718 function d3_time_parseLocaleTime(date, string, i) {
paulo@89 2719 return d3_time_parse(date, d3_time_formats.X.toString(), string, i);
paulo@89 2720 }
paulo@89 2721 function d3_time_parseAmPm(date, string, i) {
paulo@89 2722 var n = d3_time_periodLookup.get(string.slice(i, i += 2).toLowerCase());
paulo@89 2723 return n == null ? -1 : (date.p = n, i);
paulo@89 2724 }
paulo@89 2725 return d3_time_format;
paulo@89 2726 }
paulo@89 2727 var d3_time_formatPads = {
paulo@89 2728 "-": "",
paulo@89 2729 _: " ",
paulo@89 2730 "0": "0"
paulo@89 2731 }, d3_time_numberRe = /^\s*\d+/, d3_time_percentRe = /^%/;
paulo@89 2732 function d3_time_formatPad(value, fill, width) {
paulo@89 2733 var sign = value < 0 ? "-" : "", string = (sign ? -value : value) + "", length = string.length;
paulo@89 2734 return sign + (length < width ? new Array(width - length + 1).join(fill) + string : string);
paulo@89 2735 }
paulo@89 2736 function d3_time_formatRe(names) {
paulo@89 2737 return new RegExp("^(?:" + names.map(d3.requote).join("|") + ")", "i");
paulo@89 2738 }
paulo@89 2739 function d3_time_formatLookup(names) {
paulo@89 2740 var map = new d3_Map(), i = -1, n = names.length;
paulo@89 2741 while (++i < n) map.set(names[i].toLowerCase(), i);
paulo@89 2742 return map;
paulo@89 2743 }
paulo@89 2744 function d3_time_parseWeekdayNumber(date, string, i) {
paulo@89 2745 d3_time_numberRe.lastIndex = 0;
paulo@89 2746 var n = d3_time_numberRe.exec(string.slice(i, i + 1));
paulo@89 2747 return n ? (date.w = +n[0], i + n[0].length) : -1;
paulo@89 2748 }
paulo@89 2749 function d3_time_parseWeekNumberSunday(date, string, i) {
paulo@89 2750 d3_time_numberRe.lastIndex = 0;
paulo@89 2751 var n = d3_time_numberRe.exec(string.slice(i));
paulo@89 2752 return n ? (date.U = +n[0], i + n[0].length) : -1;
paulo@89 2753 }
paulo@89 2754 function d3_time_parseWeekNumberMonday(date, string, i) {
paulo@89 2755 d3_time_numberRe.lastIndex = 0;
paulo@89 2756 var n = d3_time_numberRe.exec(string.slice(i));
paulo@89 2757 return n ? (date.W = +n[0], i + n[0].length) : -1;
paulo@89 2758 }
paulo@89 2759 function d3_time_parseFullYear(date, string, i) {
paulo@89 2760 d3_time_numberRe.lastIndex = 0;
paulo@89 2761 var n = d3_time_numberRe.exec(string.slice(i, i + 4));
paulo@89 2762 return n ? (date.y = +n[0], i + n[0].length) : -1;
paulo@89 2763 }
paulo@89 2764 function d3_time_parseYear(date, string, i) {
paulo@89 2765 d3_time_numberRe.lastIndex = 0;
paulo@89 2766 var n = d3_time_numberRe.exec(string.slice(i, i + 2));
paulo@89 2767 return n ? (date.y = d3_time_expandYear(+n[0]), i + n[0].length) : -1;
paulo@89 2768 }
paulo@89 2769 function d3_time_parseZone(date, string, i) {
paulo@89 2770 return /^[+-]\d{4}$/.test(string = string.slice(i, i + 5)) ? (date.Z = -string,
paulo@89 2771 i + 5) : -1;
paulo@89 2772 }
paulo@89 2773 function d3_time_expandYear(d) {
paulo@89 2774 return d + (d > 68 ? 1900 : 2e3);
paulo@89 2775 }
paulo@89 2776 function d3_time_parseMonthNumber(date, string, i) {
paulo@89 2777 d3_time_numberRe.lastIndex = 0;
paulo@89 2778 var n = d3_time_numberRe.exec(string.slice(i, i + 2));
paulo@89 2779 return n ? (date.m = n[0] - 1, i + n[0].length) : -1;
paulo@89 2780 }
paulo@89 2781 function d3_time_parseDay(date, string, i) {
paulo@89 2782 d3_time_numberRe.lastIndex = 0;
paulo@89 2783 var n = d3_time_numberRe.exec(string.slice(i, i + 2));
paulo@89 2784 return n ? (date.d = +n[0], i + n[0].length) : -1;
paulo@89 2785 }
paulo@89 2786 function d3_time_parseDayOfYear(date, string, i) {
paulo@89 2787 d3_time_numberRe.lastIndex = 0;
paulo@89 2788 var n = d3_time_numberRe.exec(string.slice(i, i + 3));
paulo@89 2789 return n ? (date.j = +n[0], i + n[0].length) : -1;
paulo@89 2790 }
paulo@89 2791 function d3_time_parseHour24(date, string, i) {
paulo@89 2792 d3_time_numberRe.lastIndex = 0;
paulo@89 2793 var n = d3_time_numberRe.exec(string.slice(i, i + 2));
paulo@89 2794 return n ? (date.H = +n[0], i + n[0].length) : -1;
paulo@89 2795 }
paulo@89 2796 function d3_time_parseMinutes(date, string, i) {
paulo@89 2797 d3_time_numberRe.lastIndex = 0;
paulo@89 2798 var n = d3_time_numberRe.exec(string.slice(i, i + 2));
paulo@89 2799 return n ? (date.M = +n[0], i + n[0].length) : -1;
paulo@89 2800 }
paulo@89 2801 function d3_time_parseSeconds(date, string, i) {
paulo@89 2802 d3_time_numberRe.lastIndex = 0;
paulo@89 2803 var n = d3_time_numberRe.exec(string.slice(i, i + 2));
paulo@89 2804 return n ? (date.S = +n[0], i + n[0].length) : -1;
paulo@89 2805 }
paulo@89 2806 function d3_time_parseMilliseconds(date, string, i) {
paulo@89 2807 d3_time_numberRe.lastIndex = 0;
paulo@89 2808 var n = d3_time_numberRe.exec(string.slice(i, i + 3));
paulo@89 2809 return n ? (date.L = +n[0], i + n[0].length) : -1;
paulo@89 2810 }
paulo@89 2811 function d3_time_zone(d) {
paulo@89 2812 var z = d.getTimezoneOffset(), zs = z > 0 ? "-" : "+", zh = abs(z) / 60 | 0, zm = abs(z) % 60;
paulo@89 2813 return zs + d3_time_formatPad(zh, "0", 2) + d3_time_formatPad(zm, "0", 2);
paulo@89 2814 }
paulo@89 2815 function d3_time_parseLiteralPercent(date, string, i) {
paulo@89 2816 d3_time_percentRe.lastIndex = 0;
paulo@89 2817 var n = d3_time_percentRe.exec(string.slice(i, i + 1));
paulo@89 2818 return n ? i + n[0].length : -1;
paulo@89 2819 }
paulo@89 2820 function d3_time_formatMulti(formats) {
paulo@89 2821 var n = formats.length, i = -1;
paulo@89 2822 while (++i < n) formats[i][0] = this(formats[i][0]);
paulo@89 2823 return function(date) {
paulo@89 2824 var i = 0, f = formats[i];
paulo@89 2825 while (!f[1](date)) f = formats[++i];
paulo@89 2826 return f[0](date);
paulo@89 2827 };
paulo@89 2828 }
paulo@89 2829 d3.locale = function(locale) {
paulo@89 2830 return {
paulo@89 2831 numberFormat: d3_locale_numberFormat(locale),
paulo@89 2832 timeFormat: d3_locale_timeFormat(locale)
paulo@89 2833 };
paulo@89 2834 };
paulo@89 2835 var d3_locale_enUS = d3.locale({
paulo@89 2836 decimal: ".",
paulo@89 2837 thousands: ",",
paulo@89 2838 grouping: [ 3 ],
paulo@89 2839 currency: [ "$", "" ],
paulo@89 2840 dateTime: "%a %b %e %X %Y",
paulo@89 2841 date: "%m/%d/%Y",
paulo@89 2842 time: "%H:%M:%S",
paulo@89 2843 periods: [ "AM", "PM" ],
paulo@89 2844 days: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
paulo@89 2845 shortDays: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ],
paulo@89 2846 months: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ],
paulo@89 2847 shortMonths: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
paulo@89 2848 });
paulo@89 2849 d3.format = d3_locale_enUS.numberFormat;
paulo@89 2850 d3.geo = {};
paulo@89 2851 function d3_adder() {}
paulo@89 2852 d3_adder.prototype = {
paulo@89 2853 s: 0,
paulo@89 2854 t: 0,
paulo@89 2855 add: function(y) {
paulo@89 2856 d3_adderSum(y, this.t, d3_adderTemp);
paulo@89 2857 d3_adderSum(d3_adderTemp.s, this.s, this);
paulo@89 2858 if (this.s) this.t += d3_adderTemp.t; else this.s = d3_adderTemp.t;
paulo@89 2859 },
paulo@89 2860 reset: function() {
paulo@89 2861 this.s = this.t = 0;
paulo@89 2862 },
paulo@89 2863 valueOf: function() {
paulo@89 2864 return this.s;
paulo@89 2865 }
paulo@89 2866 };
paulo@89 2867 var d3_adderTemp = new d3_adder();
paulo@89 2868 function d3_adderSum(a, b, o) {
paulo@89 2869 var x = o.s = a + b, bv = x - a, av = x - bv;
paulo@89 2870 o.t = a - av + (b - bv);
paulo@89 2871 }
paulo@89 2872 d3.geo.stream = function(object, listener) {
paulo@89 2873 if (object && d3_geo_streamObjectType.hasOwnProperty(object.type)) {
paulo@89 2874 d3_geo_streamObjectType[object.type](object, listener);
paulo@89 2875 } else {
paulo@89 2876 d3_geo_streamGeometry(object, listener);
paulo@89 2877 }
paulo@89 2878 };
paulo@89 2879 function d3_geo_streamGeometry(geometry, listener) {
paulo@89 2880 if (geometry && d3_geo_streamGeometryType.hasOwnProperty(geometry.type)) {
paulo@89 2881 d3_geo_streamGeometryType[geometry.type](geometry, listener);
paulo@89 2882 }
paulo@89 2883 }
paulo@89 2884 var d3_geo_streamObjectType = {
paulo@89 2885 Feature: function(feature, listener) {
paulo@89 2886 d3_geo_streamGeometry(feature.geometry, listener);
paulo@89 2887 },
paulo@89 2888 FeatureCollection: function(object, listener) {
paulo@89 2889 var features = object.features, i = -1, n = features.length;
paulo@89 2890 while (++i < n) d3_geo_streamGeometry(features[i].geometry, listener);
paulo@89 2891 }
paulo@89 2892 };
paulo@89 2893 var d3_geo_streamGeometryType = {
paulo@89 2894 Sphere: function(object, listener) {
paulo@89 2895 listener.sphere();
paulo@89 2896 },
paulo@89 2897 Point: function(object, listener) {
paulo@89 2898 object = object.coordinates;
paulo@89 2899 listener.point(object[0], object[1], object[2]);
paulo@89 2900 },
paulo@89 2901 MultiPoint: function(object, listener) {
paulo@89 2902 var coordinates = object.coordinates, i = -1, n = coordinates.length;
paulo@89 2903 while (++i < n) object = coordinates[i], listener.point(object[0], object[1], object[2]);
paulo@89 2904 },
paulo@89 2905 LineString: function(object, listener) {
paulo@89 2906 d3_geo_streamLine(object.coordinates, listener, 0);
paulo@89 2907 },
paulo@89 2908 MultiLineString: function(object, listener) {
paulo@89 2909 var coordinates = object.coordinates, i = -1, n = coordinates.length;
paulo@89 2910 while (++i < n) d3_geo_streamLine(coordinates[i], listener, 0);
paulo@89 2911 },
paulo@89 2912 Polygon: function(object, listener) {
paulo@89 2913 d3_geo_streamPolygon(object.coordinates, listener);
paulo@89 2914 },
paulo@89 2915 MultiPolygon: function(object, listener) {
paulo@89 2916 var coordinates = object.coordinates, i = -1, n = coordinates.length;
paulo@89 2917 while (++i < n) d3_geo_streamPolygon(coordinates[i], listener);
paulo@89 2918 },
paulo@89 2919 GeometryCollection: function(object, listener) {
paulo@89 2920 var geometries = object.geometries, i = -1, n = geometries.length;
paulo@89 2921 while (++i < n) d3_geo_streamGeometry(geometries[i], listener);
paulo@89 2922 }
paulo@89 2923 };
paulo@89 2924 function d3_geo_streamLine(coordinates, listener, closed) {
paulo@89 2925 var i = -1, n = coordinates.length - closed, coordinate;
paulo@89 2926 listener.lineStart();
paulo@89 2927 while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1], coordinate[2]);
paulo@89 2928 listener.lineEnd();
paulo@89 2929 }
paulo@89 2930 function d3_geo_streamPolygon(coordinates, listener) {
paulo@89 2931 var i = -1, n = coordinates.length;
paulo@89 2932 listener.polygonStart();
paulo@89 2933 while (++i < n) d3_geo_streamLine(coordinates[i], listener, 1);
paulo@89 2934 listener.polygonEnd();
paulo@89 2935 }
paulo@89 2936 d3.geo.area = function(object) {
paulo@89 2937 d3_geo_areaSum = 0;
paulo@89 2938 d3.geo.stream(object, d3_geo_area);
paulo@89 2939 return d3_geo_areaSum;
paulo@89 2940 };
paulo@89 2941 var d3_geo_areaSum, d3_geo_areaRingSum = new d3_adder();
paulo@89 2942 var d3_geo_area = {
paulo@89 2943 sphere: function() {
paulo@89 2944 d3_geo_areaSum += 4 * π;
paulo@89 2945 },
paulo@89 2946 point: d3_noop,
paulo@89 2947 lineStart: d3_noop,
paulo@89 2948 lineEnd: d3_noop,
paulo@89 2949 polygonStart: function() {
paulo@89 2950 d3_geo_areaRingSum.reset();
paulo@89 2951 d3_geo_area.lineStart = d3_geo_areaRingStart;
paulo@89 2952 },
paulo@89 2953 polygonEnd: function() {
paulo@89 2954 var area = 2 * d3_geo_areaRingSum;
paulo@89 2955 d3_geo_areaSum += area < 0 ? 4 * π + area : area;
paulo@89 2956 d3_geo_area.lineStart = d3_geo_area.lineEnd = d3_geo_area.point = d3_noop;
paulo@89 2957 }
paulo@89 2958 };
paulo@89 2959 function d3_geo_areaRingStart() {
paulo@89 2960 var λ00, φ00, λ0, cosφ0, sinφ0;
paulo@89 2961 d3_geo_area.point = function(λ, φ) {
paulo@89 2962 d3_geo_area.point = nextPoint;
paulo@89 2963 λ0 = (λ00 = λ) * d3_radians, cosφ0 = Math.cos(φ = (φ00 = φ) * d3_radians / 2 + π / 4),
paulo@89 2964 sinφ0 = Math.sin(φ);
paulo@89 2965 };
paulo@89 2966 function nextPoint(λ, φ) {
paulo@89 2967 λ *= d3_radians;
paulo@89 2968 φ = φ * d3_radians / 2 + π / 4;
paulo@89 2969 var dλ = λ - λ0, sdλ = dλ >= 0 ? 1 : -1, adλ = sdλ * dλ, cosφ = Math.cos(φ), sinφ = Math.sin(φ), k = sinφ0 * sinφ, u = cosφ0 * cosφ + k * Math.cos(adλ), v = k * sdλ * Math.sin(adλ);
paulo@89 2970 d3_geo_areaRingSum.add(Math.atan2(v, u));
paulo@89 2971 λ0 = λ, cosφ0 = cosφ, sinφ0 = sinφ;
paulo@89 2972 }
paulo@89 2973 d3_geo_area.lineEnd = function() {
paulo@89 2974 nextPoint(λ00, φ00);
paulo@89 2975 };
paulo@89 2976 }
paulo@89 2977 function d3_geo_cartesian(spherical) {
paulo@89 2978 var λ = spherical[0], φ = spherical[1], cosφ = Math.cos(φ);
paulo@89 2979 return [ cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ) ];
paulo@89 2980 }
paulo@89 2981 function d3_geo_cartesianDot(a, b) {
paulo@89 2982 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
paulo@89 2983 }
paulo@89 2984 function d3_geo_cartesianCross(a, b) {
paulo@89 2985 return [ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ];
paulo@89 2986 }
paulo@89 2987 function d3_geo_cartesianAdd(a, b) {
paulo@89 2988 a[0] += b[0];
paulo@89 2989 a[1] += b[1];
paulo@89 2990 a[2] += b[2];
paulo@89 2991 }
paulo@89 2992 function d3_geo_cartesianScale(vector, k) {
paulo@89 2993 return [ vector[0] * k, vector[1] * k, vector[2] * k ];
paulo@89 2994 }
paulo@89 2995 function d3_geo_cartesianNormalize(d) {
paulo@89 2996 var l = Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
paulo@89 2997 d[0] /= l;
paulo@89 2998 d[1] /= l;
paulo@89 2999 d[2] /= l;
paulo@89 3000 }
paulo@89 3001 function d3_geo_spherical(cartesian) {
paulo@89 3002 return [ Math.atan2(cartesian[1], cartesian[0]), d3_asin(cartesian[2]) ];
paulo@89 3003 }
paulo@89 3004 function d3_geo_sphericalEqual(a, b) {
paulo@89 3005 return abs(a[0] - b[0]) < ε && abs(a[1] - b[1]) < ε;
paulo@89 3006 }
paulo@89 3007 d3.geo.bounds = function() {
paulo@89 3008 var λ0, φ0, λ1, φ1, λ_, λ__, φ__, p0, dλSum, ranges, range;
paulo@89 3009 var bound = {
paulo@89 3010 point: point,
paulo@89 3011 lineStart: lineStart,
paulo@89 3012 lineEnd: lineEnd,
paulo@89 3013 polygonStart: function() {
paulo@89 3014 bound.point = ringPoint;
paulo@89 3015 bound.lineStart = ringStart;
paulo@89 3016 bound.lineEnd = ringEnd;
paulo@89 3017 dλSum = 0;
paulo@89 3018 d3_geo_area.polygonStart();
paulo@89 3019 },
paulo@89 3020 polygonEnd: function() {
paulo@89 3021 d3_geo_area.polygonEnd();
paulo@89 3022 bound.point = point;
paulo@89 3023 bound.lineStart = lineStart;
paulo@89 3024 bound.lineEnd = lineEnd;
paulo@89 3025 if (d3_geo_areaRingSum < 0) λ0 = -(λ1 = 180), φ0 = -(φ1 = 90); else if (dλSum > ε) φ1 = 90; else if (dλSum < -ε) φ0 = -90;
paulo@89 3026 range[0] = λ0, range[1] = λ1;
paulo@89 3027 }
paulo@89 3028 };
paulo@89 3029 function point(λ, φ) {
paulo@89 3030 ranges.push(range = [ λ0 = λ, λ1 = λ ]);
paulo@89 3031 if (φ < φ0) φ0 = φ;
paulo@89 3032 if (φ > φ1) φ1 = φ;
paulo@89 3033 }
paulo@89 3034 function linePoint(λ, φ) {
paulo@89 3035 var p = d3_geo_cartesian([ λ * d3_radians, φ * d3_radians ]);
paulo@89 3036 if (p0) {
paulo@89 3037 var normal = d3_geo_cartesianCross(p0, p), equatorial = [ normal[1], -normal[0], 0 ], inflection = d3_geo_cartesianCross(equatorial, normal);
paulo@89 3038 d3_geo_cartesianNormalize(inflection);
paulo@89 3039 inflection = d3_geo_spherical(inflection);
paulo@89 3040 var dλ = λ - λ_, s = dλ > 0 ? 1 : -1, λi = inflection[0] * d3_degrees * s, antimeridian = abs(dλ) > 180;
paulo@89 3041 if (antimeridian ^ (s * λ_ < λi && λi < s * λ)) {
paulo@89 3042 var φi = inflection[1] * d3_degrees;
paulo@89 3043 if (φi > φ1) φ1 = φi;
paulo@89 3044 } else if (λi = (λi + 360) % 360 - 180, antimeridian ^ (s * λ_ < λi && λi < s * λ)) {
paulo@89 3045 var φi = -inflection[1] * d3_degrees;
paulo@89 3046 if (φi < φ0) φ0 = φi;
paulo@89 3047 } else {
paulo@89 3048 if (φ < φ0) φ0 = φ;
paulo@89 3049 if (φ > φ1) φ1 = φ;
paulo@89 3050 }
paulo@89 3051 if (antimeridian) {
paulo@89 3052 if (λ < λ_) {
paulo@89 3053 if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ;
paulo@89 3054 } else {
paulo@89 3055 if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ;
paulo@89 3056 }
paulo@89 3057 } else {
paulo@89 3058 if (λ1 >= λ0) {
paulo@89 3059 if (λ < λ0) λ0 = λ;
paulo@89 3060 if (λ > λ1) λ1 = λ;
paulo@89 3061 } else {
paulo@89 3062 if (λ > λ_) {
paulo@89 3063 if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ;
paulo@89 3064 } else {
paulo@89 3065 if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ;
paulo@89 3066 }
paulo@89 3067 }
paulo@89 3068 }
paulo@89 3069 } else {
paulo@89 3070 point(λ, φ);
paulo@89 3071 }
paulo@89 3072 p0 = p, λ_ = λ;
paulo@89 3073 }
paulo@89 3074 function lineStart() {
paulo@89 3075 bound.point = linePoint;
paulo@89 3076 }
paulo@89 3077 function lineEnd() {
paulo@89 3078 range[0] = λ0, range[1] = λ1;
paulo@89 3079 bound.point = point;
paulo@89 3080 p0 = null;
paulo@89 3081 }
paulo@89 3082 function ringPoint(λ, φ) {
paulo@89 3083 if (p0) {
paulo@89 3084 var dλ = λ - λ_;
paulo@89 3085 dλSum += abs(dλ) > 180 ? dλ + (dλ > 0 ? 360 : -360) : dλ;
paulo@89 3086 } else λ__ = λ, φ__ = φ;
paulo@89 3087 d3_geo_area.point(λ, φ);
paulo@89 3088 linePoint(λ, φ);
paulo@89 3089 }
paulo@89 3090 function ringStart() {
paulo@89 3091 d3_geo_area.lineStart();
paulo@89 3092 }
paulo@89 3093 function ringEnd() {
paulo@89 3094 ringPoint(λ__, φ__);
paulo@89 3095 d3_geo_area.lineEnd();
paulo@89 3096 if (abs(dλSum) > ε) λ0 = -(λ1 = 180);
paulo@89 3097 range[0] = λ0, range[1] = λ1;
paulo@89 3098 p0 = null;
paulo@89 3099 }
paulo@89 3100 function angle(λ0, λ1) {
paulo@89 3101 return (λ1 -= λ0) < 0 ? λ1 + 360 : λ1;
paulo@89 3102 }
paulo@89 3103 function compareRanges(a, b) {
paulo@89 3104 return a[0] - b[0];
paulo@89 3105 }
paulo@89 3106 function withinRange(x, range) {
paulo@89 3107 return range[0] <= range[1] ? range[0] <= x && x <= range[1] : x < range[0] || range[1] < x;
paulo@89 3108 }
paulo@89 3109 return function(feature) {
paulo@89 3110 φ1 = λ1 = -(λ0 = φ0 = Infinity);
paulo@89 3111 ranges = [];
paulo@89 3112 d3.geo.stream(feature, bound);
paulo@89 3113 var n = ranges.length;
paulo@89 3114 if (n) {
paulo@89 3115 ranges.sort(compareRanges);
paulo@89 3116 for (var i = 1, a = ranges[0], b, merged = [ a ]; i < n; ++i) {
paulo@89 3117 b = ranges[i];
paulo@89 3118 if (withinRange(b[0], a) || withinRange(b[1], a)) {
paulo@89 3119 if (angle(a[0], b[1]) > angle(a[0], a[1])) a[1] = b[1];
paulo@89 3120 if (angle(b[0], a[1]) > angle(a[0], a[1])) a[0] = b[0];
paulo@89 3121 } else {
paulo@89 3122 merged.push(a = b);
paulo@89 3123 }
paulo@89 3124 }
paulo@89 3125 var best = -Infinity, dλ;
paulo@89 3126 for (var n = merged.length - 1, i = 0, a = merged[n], b; i <= n; a = b, ++i) {
paulo@89 3127 b = merged[i];
paulo@89 3128 if ((dλ = angle(a[1], b[0])) > best) best = dλ, λ0 = b[0], λ1 = a[1];
paulo@89 3129 }
paulo@89 3130 }
paulo@89 3131 ranges = range = null;
paulo@89 3132 return λ0 === Infinity || φ0 === Infinity ? [ [ NaN, NaN ], [ NaN, NaN ] ] : [ [ λ0, φ0 ], [ λ1, φ1 ] ];
paulo@89 3133 };
paulo@89 3134 }();
paulo@89 3135 d3.geo.centroid = function(object) {
paulo@89 3136 d3_geo_centroidW0 = d3_geo_centroidW1 = d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0;
paulo@89 3137 d3.geo.stream(object, d3_geo_centroid);
paulo@89 3138 var x = d3_geo_centroidX2, y = d3_geo_centroidY2, z = d3_geo_centroidZ2, m = x * x + y * y + z * z;
paulo@89 3139 if (m < ε2) {
paulo@89 3140 x = d3_geo_centroidX1, y = d3_geo_centroidY1, z = d3_geo_centroidZ1;
paulo@89 3141 if (d3_geo_centroidW1 < ε) x = d3_geo_centroidX0, y = d3_geo_centroidY0, z = d3_geo_centroidZ0;
paulo@89 3142 m = x * x + y * y + z * z;
paulo@89 3143 if (m < ε2) return [ NaN, NaN ];
paulo@89 3144 }
paulo@89 3145 return [ Math.atan2(y, x) * d3_degrees, d3_asin(z / Math.sqrt(m)) * d3_degrees ];
paulo@89 3146 };
paulo@89 3147 var d3_geo_centroidW0, d3_geo_centroidW1, d3_geo_centroidX0, d3_geo_centroidY0, d3_geo_centroidZ0, d3_geo_centroidX1, d3_geo_centroidY1, d3_geo_centroidZ1, d3_geo_centroidX2, d3_geo_centroidY2, d3_geo_centroidZ2;
paulo@89 3148 var d3_geo_centroid = {
paulo@89 3149 sphere: d3_noop,
paulo@89 3150 point: d3_geo_centroidPoint,
paulo@89 3151 lineStart: d3_geo_centroidLineStart,
paulo@89 3152 lineEnd: d3_geo_centroidLineEnd,
paulo@89 3153 polygonStart: function() {
paulo@89 3154 d3_geo_centroid.lineStart = d3_geo_centroidRingStart;
paulo@89 3155 },
paulo@89 3156 polygonEnd: function() {
paulo@89 3157 d3_geo_centroid.lineStart = d3_geo_centroidLineStart;
paulo@89 3158 }
paulo@89 3159 };
paulo@89 3160 function d3_geo_centroidPoint(λ, φ) {
paulo@89 3161 λ *= d3_radians;
paulo@89 3162 var cosφ = Math.cos(φ *= d3_radians);
paulo@89 3163 d3_geo_centroidPointXYZ(cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ));
paulo@89 3164 }
paulo@89 3165 function d3_geo_centroidPointXYZ(x, y, z) {
paulo@89 3166 ++d3_geo_centroidW0;
paulo@89 3167 d3_geo_centroidX0 += (x - d3_geo_centroidX0) / d3_geo_centroidW0;
paulo@89 3168 d3_geo_centroidY0 += (y - d3_geo_centroidY0) / d3_geo_centroidW0;
paulo@89 3169 d3_geo_centroidZ0 += (z - d3_geo_centroidZ0) / d3_geo_centroidW0;
paulo@89 3170 }
paulo@89 3171 function d3_geo_centroidLineStart() {
paulo@89 3172 var x0, y0, z0;
paulo@89 3173 d3_geo_centroid.point = function(λ, φ) {
paulo@89 3174 λ *= d3_radians;
paulo@89 3175 var cosφ = Math.cos(φ *= d3_radians);
paulo@89 3176 x0 = cosφ * Math.cos(λ);
paulo@89 3177 y0 = cosφ * Math.sin(λ);
paulo@89 3178 z0 = Math.sin(φ);
paulo@89 3179 d3_geo_centroid.point = nextPoint;
paulo@89 3180 d3_geo_centroidPointXYZ(x0, y0, z0);
paulo@89 3181 };
paulo@89 3182 function nextPoint(λ, φ) {
paulo@89 3183 λ *= d3_radians;
paulo@89 3184 var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), w = Math.atan2(Math.sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
paulo@89 3185 d3_geo_centroidW1 += w;
paulo@89 3186 d3_geo_centroidX1 += w * (x0 + (x0 = x));
paulo@89 3187 d3_geo_centroidY1 += w * (y0 + (y0 = y));
paulo@89 3188 d3_geo_centroidZ1 += w * (z0 + (z0 = z));
paulo@89 3189 d3_geo_centroidPointXYZ(x0, y0, z0);
paulo@89 3190 }
paulo@89 3191 }
paulo@89 3192 function d3_geo_centroidLineEnd() {
paulo@89 3193 d3_geo_centroid.point = d3_geo_centroidPoint;
paulo@89 3194 }
paulo@89 3195 function d3_geo_centroidRingStart() {
paulo@89 3196 var λ00, φ00, x0, y0, z0;
paulo@89 3197 d3_geo_centroid.point = function(λ, φ) {
paulo@89 3198 λ00 = λ, φ00 = φ;
paulo@89 3199 d3_geo_centroid.point = nextPoint;
paulo@89 3200 λ *= d3_radians;
paulo@89 3201 var cosφ = Math.cos(φ *= d3_radians);
paulo@89 3202 x0 = cosφ * Math.cos(λ);
paulo@89 3203 y0 = cosφ * Math.sin(λ);
paulo@89 3204 z0 = Math.sin(φ);
paulo@89 3205 d3_geo_centroidPointXYZ(x0, y0, z0);
paulo@89 3206 };
paulo@89 3207 d3_geo_centroid.lineEnd = function() {
paulo@89 3208 nextPoint(λ00, φ00);
paulo@89 3209 d3_geo_centroid.lineEnd = d3_geo_centroidLineEnd;
paulo@89 3210 d3_geo_centroid.point = d3_geo_centroidPoint;
paulo@89 3211 };
paulo@89 3212 function nextPoint(λ, φ) {
paulo@89 3213 λ *= d3_radians;
paulo@89 3214 var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), cx = y0 * z - z0 * y, cy = z0 * x - x0 * z, cz = x0 * y - y0 * x, m = Math.sqrt(cx * cx + cy * cy + cz * cz), u = x0 * x + y0 * y + z0 * z, v = m && -d3_acos(u) / m, w = Math.atan2(m, u);
paulo@89 3215 d3_geo_centroidX2 += v * cx;
paulo@89 3216 d3_geo_centroidY2 += v * cy;
paulo@89 3217 d3_geo_centroidZ2 += v * cz;
paulo@89 3218 d3_geo_centroidW1 += w;
paulo@89 3219 d3_geo_centroidX1 += w * (x0 + (x0 = x));
paulo@89 3220 d3_geo_centroidY1 += w * (y0 + (y0 = y));
paulo@89 3221 d3_geo_centroidZ1 += w * (z0 + (z0 = z));
paulo@89 3222 d3_geo_centroidPointXYZ(x0, y0, z0);
paulo@89 3223 }
paulo@89 3224 }
paulo@89 3225 function d3_geo_compose(a, b) {
paulo@89 3226 function compose(x, y) {
paulo@89 3227 return x = a(x, y), b(x[0], x[1]);
paulo@89 3228 }
paulo@89 3229 if (a.invert && b.invert) compose.invert = function(x, y) {
paulo@89 3230 return x = b.invert(x, y), x && a.invert(x[0], x[1]);
paulo@89 3231 };
paulo@89 3232 return compose;
paulo@89 3233 }
paulo@89 3234 function d3_true() {
paulo@89 3235 return true;
paulo@89 3236 }
paulo@89 3237 function d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener) {
paulo@89 3238 var subject = [], clip = [];
paulo@89 3239 segments.forEach(function(segment) {
paulo@89 3240 if ((n = segment.length - 1) <= 0) return;
paulo@89 3241 var n, p0 = segment[0], p1 = segment[n];
paulo@89 3242 if (d3_geo_sphericalEqual(p0, p1)) {
paulo@89 3243 listener.lineStart();
paulo@89 3244 for (var i = 0; i < n; ++i) listener.point((p0 = segment[i])[0], p0[1]);
paulo@89 3245 listener.lineEnd();
paulo@89 3246 return;
paulo@89 3247 }
paulo@89 3248 var a = new d3_geo_clipPolygonIntersection(p0, segment, null, true), b = new d3_geo_clipPolygonIntersection(p0, null, a, false);
paulo@89 3249 a.o = b;
paulo@89 3250 subject.push(a);
paulo@89 3251 clip.push(b);
paulo@89 3252 a = new d3_geo_clipPolygonIntersection(p1, segment, null, false);
paulo@89 3253 b = new d3_geo_clipPolygonIntersection(p1, null, a, true);
paulo@89 3254 a.o = b;
paulo@89 3255 subject.push(a);
paulo@89 3256 clip.push(b);
paulo@89 3257 });
paulo@89 3258 clip.sort(compare);
paulo@89 3259 d3_geo_clipPolygonLinkCircular(subject);
paulo@89 3260 d3_geo_clipPolygonLinkCircular(clip);
paulo@89 3261 if (!subject.length) return;
paulo@89 3262 for (var i = 0, entry = clipStartInside, n = clip.length; i < n; ++i) {
paulo@89 3263 clip[i].e = entry = !entry;
paulo@89 3264 }
paulo@89 3265 var start = subject[0], points, point;
paulo@89 3266 while (1) {
paulo@89 3267 var current = start, isSubject = true;
paulo@89 3268 while (current.v) if ((current = current.n) === start) return;
paulo@89 3269 points = current.z;
paulo@89 3270 listener.lineStart();
paulo@89 3271 do {
paulo@89 3272 current.v = current.o.v = true;
paulo@89 3273 if (current.e) {
paulo@89 3274 if (isSubject) {
paulo@89 3275 for (var i = 0, n = points.length; i < n; ++i) listener.point((point = points[i])[0], point[1]);
paulo@89 3276 } else {
paulo@89 3277 interpolate(current.x, current.n.x, 1, listener);
paulo@89 3278 }
paulo@89 3279 current = current.n;
paulo@89 3280 } else {
paulo@89 3281 if (isSubject) {
paulo@89 3282 points = current.p.z;
paulo@89 3283 for (var i = points.length - 1; i >= 0; --i) listener.point((point = points[i])[0], point[1]);
paulo@89 3284 } else {
paulo@89 3285 interpolate(current.x, current.p.x, -1, listener);
paulo@89 3286 }
paulo@89 3287 current = current.p;
paulo@89 3288 }
paulo@89 3289 current = current.o;
paulo@89 3290 points = current.z;
paulo@89 3291 isSubject = !isSubject;
paulo@89 3292 } while (!current.v);
paulo@89 3293 listener.lineEnd();
paulo@89 3294 }
paulo@89 3295 }
paulo@89 3296 function d3_geo_clipPolygonLinkCircular(array) {
paulo@89 3297 if (!(n = array.length)) return;
paulo@89 3298 var n, i = 0, a = array[0], b;
paulo@89 3299 while (++i < n) {
paulo@89 3300 a.n = b = array[i];
paulo@89 3301 b.p = a;
paulo@89 3302 a = b;
paulo@89 3303 }
paulo@89 3304 a.n = b = array[0];
paulo@89 3305 b.p = a;
paulo@89 3306 }
paulo@89 3307 function d3_geo_clipPolygonIntersection(point, points, other, entry) {
paulo@89 3308 this.x = point;
paulo@89 3309 this.z = points;
paulo@89 3310 this.o = other;
paulo@89 3311 this.e = entry;
paulo@89 3312 this.v = false;
paulo@89 3313 this.n = this.p = null;
paulo@89 3314 }
paulo@89 3315 function d3_geo_clip(pointVisible, clipLine, interpolate, clipStart) {
paulo@89 3316 return function(rotate, listener) {
paulo@89 3317 var line = clipLine(listener), rotatedClipStart = rotate.invert(clipStart[0], clipStart[1]);
paulo@89 3318 var clip = {
paulo@89 3319 point: point,
paulo@89 3320 lineStart: lineStart,
paulo@89 3321 lineEnd: lineEnd,
paulo@89 3322 polygonStart: function() {
paulo@89 3323 clip.point = pointRing;
paulo@89 3324 clip.lineStart = ringStart;
paulo@89 3325 clip.lineEnd = ringEnd;
paulo@89 3326 segments = [];
paulo@89 3327 polygon = [];
paulo@89 3328 },
paulo@89 3329 polygonEnd: function() {
paulo@89 3330 clip.point = point;
paulo@89 3331 clip.lineStart = lineStart;
paulo@89 3332 clip.lineEnd = lineEnd;
paulo@89 3333 segments = d3.merge(segments);
paulo@89 3334 var clipStartInside = d3_geo_pointInPolygon(rotatedClipStart, polygon);
paulo@89 3335 if (segments.length) {
paulo@89 3336 if (!polygonStarted) listener.polygonStart(), polygonStarted = true;
paulo@89 3337 d3_geo_clipPolygon(segments, d3_geo_clipSort, clipStartInside, interpolate, listener);
paulo@89 3338 } else if (clipStartInside) {
paulo@89 3339 if (!polygonStarted) listener.polygonStart(), polygonStarted = true;
paulo@89 3340 listener.lineStart();
paulo@89 3341 interpolate(null, null, 1, listener);
paulo@89 3342 listener.lineEnd();
paulo@89 3343 }
paulo@89 3344 if (polygonStarted) listener.polygonEnd(), polygonStarted = false;
paulo@89 3345 segments = polygon = null;
paulo@89 3346 },
paulo@89 3347 sphere: function() {
paulo@89 3348 listener.polygonStart();
paulo@89 3349 listener.lineStart();
paulo@89 3350 interpolate(null, null, 1, listener);
paulo@89 3351 listener.lineEnd();
paulo@89 3352 listener.polygonEnd();
paulo@89 3353 }
paulo@89 3354 };
paulo@89 3355 function point(λ, φ) {
paulo@89 3356 var point = rotate(λ, φ);
paulo@89 3357 if (pointVisible(λ = point[0], φ = point[1])) listener.point(λ, φ);
paulo@89 3358 }
paulo@89 3359 function pointLine(λ, φ) {
paulo@89 3360 var point = rotate(λ, φ);
paulo@89 3361 line.point(point[0], point[1]);
paulo@89 3362 }
paulo@89 3363 function lineStart() {
paulo@89 3364 clip.point = pointLine;
paulo@89 3365 line.lineStart();
paulo@89 3366 }
paulo@89 3367 function lineEnd() {
paulo@89 3368 clip.point = point;
paulo@89 3369 line.lineEnd();
paulo@89 3370 }
paulo@89 3371 var segments;
paulo@89 3372 var buffer = d3_geo_clipBufferListener(), ringListener = clipLine(buffer), polygonStarted = false, polygon, ring;
paulo@89 3373 function pointRing(λ, φ) {
paulo@89 3374 ring.push([ λ, φ ]);
paulo@89 3375 var point = rotate(λ, φ);
paulo@89 3376 ringListener.point(point[0], point[1]);
paulo@89 3377 }
paulo@89 3378 function ringStart() {
paulo@89 3379 ringListener.lineStart();
paulo@89 3380 ring = [];
paulo@89 3381 }
paulo@89 3382 function ringEnd() {
paulo@89 3383 pointRing(ring[0][0], ring[0][1]);
paulo@89 3384 ringListener.lineEnd();
paulo@89 3385 var clean = ringListener.clean(), ringSegments = buffer.buffer(), segment, n = ringSegments.length;
paulo@89 3386 ring.pop();
paulo@89 3387 polygon.push(ring);
paulo@89 3388 ring = null;
paulo@89 3389 if (!n) return;
paulo@89 3390 if (clean & 1) {
paulo@89 3391 segment = ringSegments[0];
paulo@89 3392 var n = segment.length - 1, i = -1, point;
paulo@89 3393 if (n > 0) {
paulo@89 3394 if (!polygonStarted) listener.polygonStart(), polygonStarted = true;
paulo@89 3395 listener.lineStart();
paulo@89 3396 while (++i < n) listener.point((point = segment[i])[0], point[1]);
paulo@89 3397 listener.lineEnd();
paulo@89 3398 }
paulo@89 3399 return;
paulo@89 3400 }
paulo@89 3401 if (n > 1 && clean & 2) ringSegments.push(ringSegments.pop().concat(ringSegments.shift()));
paulo@89 3402 segments.push(ringSegments.filter(d3_geo_clipSegmentLength1));
paulo@89 3403 }
paulo@89 3404 return clip;
paulo@89 3405 };
paulo@89 3406 }
paulo@89 3407 function d3_geo_clipSegmentLength1(segment) {
paulo@89 3408 return segment.length > 1;
paulo@89 3409 }
paulo@89 3410 function d3_geo_clipBufferListener() {
paulo@89 3411 var lines = [], line;
paulo@89 3412 return {
paulo@89 3413 lineStart: function() {
paulo@89 3414 lines.push(line = []);
paulo@89 3415 },
paulo@89 3416 point: function(λ, φ) {
paulo@89 3417 line.push([ λ, φ ]);
paulo@89 3418 },
paulo@89 3419 lineEnd: d3_noop,
paulo@89 3420 buffer: function() {
paulo@89 3421 var buffer = lines;
paulo@89 3422 lines = [];
paulo@89 3423 line = null;
paulo@89 3424 return buffer;
paulo@89 3425 },
paulo@89 3426 rejoin: function() {
paulo@89 3427 if (lines.length > 1) lines.push(lines.pop().concat(lines.shift()));
paulo@89 3428 }
paulo@89 3429 };
paulo@89 3430 }
paulo@89 3431 function d3_geo_clipSort(a, b) {
paulo@89 3432 return ((a = a.x)[0] < 0 ? a[1] - halfπ - ε : halfπ - a[1]) - ((b = b.x)[0] < 0 ? b[1] - halfπ - ε : halfπ - b[1]);
paulo@89 3433 }
paulo@89 3434 var d3_geo_clipAntimeridian = d3_geo_clip(d3_true, d3_geo_clipAntimeridianLine, d3_geo_clipAntimeridianInterpolate, [ -π, -π / 2 ]);
paulo@89 3435 function d3_geo_clipAntimeridianLine(listener) {
paulo@89 3436 var λ0 = NaN, φ0 = NaN, sλ0 = NaN, clean;
paulo@89 3437 return {
paulo@89 3438 lineStart: function() {
paulo@89 3439 listener.lineStart();
paulo@89 3440 clean = 1;
paulo@89 3441 },
paulo@89 3442 point: function(λ1, φ1) {
paulo@89 3443 var sλ1 = λ1 > 0 ? π : -π, dλ = abs(λ1 - λ0);
paulo@89 3444 if (abs(dλ - π) < ε) {
paulo@89 3445 listener.point(λ0, φ0 = (φ0 + φ1) / 2 > 0 ? halfπ : -halfπ);
paulo@89 3446 listener.point(sλ0, φ0);
paulo@89 3447 listener.lineEnd();
paulo@89 3448 listener.lineStart();
paulo@89 3449 listener.point(sλ1, φ0);
paulo@89 3450 listener.point(λ1, φ0);
paulo@89 3451 clean = 0;
paulo@89 3452 } else if (sλ0 !== sλ1 && dλ >= π) {
paulo@89 3453 if (abs(λ0 - sλ0) < ε) λ0 -= sλ0 * ε;
paulo@89 3454 if (abs(λ1 - sλ1) < ε) λ1 -= sλ1 * ε;
paulo@89 3455 φ0 = d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1);
paulo@89 3456 listener.point(sλ0, φ0);
paulo@89 3457 listener.lineEnd();
paulo@89 3458 listener.lineStart();
paulo@89 3459 listener.point(sλ1, φ0);
paulo@89 3460 clean = 0;
paulo@89 3461 }
paulo@89 3462 listener.point(λ0 = λ1, φ0 = φ1);
paulo@89 3463 sλ0 = sλ1;
paulo@89 3464 },
paulo@89 3465 lineEnd: function() {
paulo@89 3466 listener.lineEnd();
paulo@89 3467 λ0 = φ0 = NaN;
paulo@89 3468 },
paulo@89 3469 clean: function() {
paulo@89 3470 return 2 - clean;
paulo@89 3471 }
paulo@89 3472 };
paulo@89 3473 }
paulo@89 3474 function d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1) {
paulo@89 3475 var cosφ0, cosφ1, sinλ0_λ1 = Math.sin(λ0 - λ1);
paulo@89 3476 return abs(sinλ0_λ1) > ε ? Math.atan((Math.sin(φ0) * (cosφ1 = Math.cos(φ1)) * Math.sin(λ1) - Math.sin(φ1) * (cosφ0 = Math.cos(φ0)) * Math.sin(λ0)) / (cosφ0 * cosφ1 * sinλ0_λ1)) : (φ0 + φ1) / 2;
paulo@89 3477 }
paulo@89 3478 function d3_geo_clipAntimeridianInterpolate(from, to, direction, listener) {
paulo@89 3479 var φ;
paulo@89 3480 if (from == null) {
paulo@89 3481 φ = direction * halfπ;
paulo@89 3482 listener.point(-π, φ);
paulo@89 3483 listener.point(0, φ);
paulo@89 3484 listener.point(π, φ);
paulo@89 3485 listener.point(π, 0);
paulo@89 3486 listener.point(π, -φ);
paulo@89 3487 listener.point(0, -φ);
paulo@89 3488 listener.point(-π, -φ);
paulo@89 3489 listener.point(-π, 0);
paulo@89 3490 listener.point(-π, φ);
paulo@89 3491 } else if (abs(from[0] - to[0]) > ε) {
paulo@89 3492 var s = from[0] < to[0] ? π : -π;
paulo@89 3493 φ = direction * s / 2;
paulo@89 3494 listener.point(-s, φ);
paulo@89 3495 listener.point(0, φ);
paulo@89 3496 listener.point(s, φ);
paulo@89 3497 } else {
paulo@89 3498 listener.point(to[0], to[1]);
paulo@89 3499 }
paulo@89 3500 }
paulo@89 3501 function d3_geo_pointInPolygon(point, polygon) {
paulo@89 3502 var meridian = point[0], parallel = point[1], meridianNormal = [ Math.sin(meridian), -Math.cos(meridian), 0 ], polarAngle = 0, winding = 0;
paulo@89 3503 d3_geo_areaRingSum.reset();
paulo@89 3504 for (var i = 0, n = polygon.length; i < n; ++i) {
paulo@89 3505 var ring = polygon[i], m = ring.length;
paulo@89 3506 if (!m) continue;
paulo@89 3507 var point0 = ring[0], λ0 = point0[0], φ0 = point0[1] / 2 + π / 4, sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), j = 1;
paulo@89 3508 while (true) {
paulo@89 3509 if (j === m) j = 0;
paulo@89 3510 point = ring[j];
paulo@89 3511 var λ = point[0], φ = point[1] / 2 + π / 4, sinφ = Math.sin(φ), cosφ = Math.cos(φ), dλ = λ - λ0, sdλ = dλ >= 0 ? 1 : -1, adλ = sdλ * dλ, antimeridian = adλ > π, k = sinφ0 * sinφ;
paulo@89 3512 d3_geo_areaRingSum.add(Math.atan2(k * sdλ * Math.sin(adλ), cosφ0 * cosφ + k * Math.cos(adλ)));
paulo@89 3513 polarAngle += antimeridian ? dλ + sdλ * τ : dλ;
paulo@89 3514 if (antimeridian ^ λ0 >= meridian ^ λ >= meridian) {
paulo@89 3515 var arc = d3_geo_cartesianCross(d3_geo_cartesian(point0), d3_geo_cartesian(point));
paulo@89 3516 d3_geo_cartesianNormalize(arc);
paulo@89 3517 var intersection = d3_geo_cartesianCross(meridianNormal, arc);
paulo@89 3518 d3_geo_cartesianNormalize(intersection);
paulo@89 3519 var φarc = (antimeridian ^ dλ >= 0 ? -1 : 1) * d3_asin(intersection[2]);
paulo@89 3520 if (parallel > φarc || parallel === φarc && (arc[0] || arc[1])) {
paulo@89 3521 winding += antimeridian ^ dλ >= 0 ? 1 : -1;
paulo@89 3522 }
paulo@89 3523 }
paulo@89 3524 if (!j++) break;
paulo@89 3525 λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ, point0 = point;
paulo@89 3526 }
paulo@89 3527 }
paulo@89 3528 return (polarAngle < -ε || polarAngle < ε && d3_geo_areaRingSum < -ε) ^ winding & 1;
paulo@89 3529 }
paulo@89 3530 function d3_geo_clipCircle(radius) {
paulo@89 3531 var cr = Math.cos(radius), smallRadius = cr > 0, notHemisphere = abs(cr) > ε, interpolate = d3_geo_circleInterpolate(radius, 6 * d3_radians);
paulo@89 3532 return d3_geo_clip(visible, clipLine, interpolate, smallRadius ? [ 0, -radius ] : [ -π, radius - π ]);
paulo@89 3533 function visible(λ, φ) {
paulo@89 3534 return Math.cos(λ) * Math.cos(φ) > cr;
paulo@89 3535 }
paulo@89 3536 function clipLine(listener) {
paulo@89 3537 var point0, c0, v0, v00, clean;
paulo@89 3538 return {
paulo@89 3539 lineStart: function() {
paulo@89 3540 v00 = v0 = false;
paulo@89 3541 clean = 1;
paulo@89 3542 },
paulo@89 3543 point: function(λ, φ) {
paulo@89 3544 var point1 = [ λ, φ ], point2, v = visible(λ, φ), c = smallRadius ? v ? 0 : code(λ, φ) : v ? code(λ + (λ < 0 ? π : -π), φ) : 0;
paulo@89 3545 if (!point0 && (v00 = v0 = v)) listener.lineStart();
paulo@89 3546 if (v !== v0) {
paulo@89 3547 point2 = intersect(point0, point1);
paulo@89 3548 if (d3_geo_sphericalEqual(point0, point2) || d3_geo_sphericalEqual(point1, point2)) {
paulo@89 3549 point1[0] += ε;
paulo@89 3550 point1[1] += ε;
paulo@89 3551 v = visible(point1[0], point1[1]);
paulo@89 3552 }
paulo@89 3553 }
paulo@89 3554 if (v !== v0) {
paulo@89 3555 clean = 0;
paulo@89 3556 if (v) {
paulo@89 3557 listener.lineStart();
paulo@89 3558 point2 = intersect(point1, point0);
paulo@89 3559 listener.point(point2[0], point2[1]);
paulo@89 3560 } else {
paulo@89 3561 point2 = intersect(point0, point1);
paulo@89 3562 listener.point(point2[0], point2[1]);
paulo@89 3563 listener.lineEnd();
paulo@89 3564 }
paulo@89 3565 point0 = point2;
paulo@89 3566 } else if (notHemisphere && point0 && smallRadius ^ v) {
paulo@89 3567 var t;
paulo@89 3568 if (!(c & c0) && (t = intersect(point1, point0, true))) {
paulo@89 3569 clean = 0;
paulo@89 3570 if (smallRadius) {
paulo@89 3571 listener.lineStart();
paulo@89 3572 listener.point(t[0][0], t[0][1]);
paulo@89 3573 listener.point(t[1][0], t[1][1]);
paulo@89 3574 listener.lineEnd();
paulo@89 3575 } else {
paulo@89 3576 listener.point(t[1][0], t[1][1]);
paulo@89 3577 listener.lineEnd();
paulo@89 3578 listener.lineStart();
paulo@89 3579 listener.point(t[0][0], t[0][1]);
paulo@89 3580 }
paulo@89 3581 }
paulo@89 3582 }
paulo@89 3583 if (v && (!point0 || !d3_geo_sphericalEqual(point0, point1))) {
paulo@89 3584 listener.point(point1[0], point1[1]);
paulo@89 3585 }
paulo@89 3586 point0 = point1, v0 = v, c0 = c;
paulo@89 3587 },
paulo@89 3588 lineEnd: function() {
paulo@89 3589 if (v0) listener.lineEnd();
paulo@89 3590 point0 = null;
paulo@89 3591 },
paulo@89 3592 clean: function() {
paulo@89 3593 return clean | (v00 && v0) << 1;
paulo@89 3594 }
paulo@89 3595 };
paulo@89 3596 }
paulo@89 3597 function intersect(a, b, two) {
paulo@89 3598 var pa = d3_geo_cartesian(a), pb = d3_geo_cartesian(b);
paulo@89 3599 var n1 = [ 1, 0, 0 ], n2 = d3_geo_cartesianCross(pa, pb), n2n2 = d3_geo_cartesianDot(n2, n2), n1n2 = n2[0], determinant = n2n2 - n1n2 * n1n2;
paulo@89 3600 if (!determinant) return !two && a;
paulo@89 3601 var c1 = cr * n2n2 / determinant, c2 = -cr * n1n2 / determinant, n1xn2 = d3_geo_cartesianCross(n1, n2), A = d3_geo_cartesianScale(n1, c1), B = d3_geo_cartesianScale(n2, c2);
paulo@89 3602 d3_geo_cartesianAdd(A, B);
paulo@89 3603 var u = n1xn2, w = d3_geo_cartesianDot(A, u), uu = d3_geo_cartesianDot(u, u), t2 = w * w - uu * (d3_geo_cartesianDot(A, A) - 1);
paulo@89 3604 if (t2 < 0) return;
paulo@89 3605 var t = Math.sqrt(t2), q = d3_geo_cartesianScale(u, (-w - t) / uu);
paulo@89 3606 d3_geo_cartesianAdd(q, A);
paulo@89 3607 q = d3_geo_spherical(q);
paulo@89 3608 if (!two) return q;
paulo@89 3609 var λ0 = a[0], λ1 = b[0], φ0 = a[1], φ1 = b[1], z;
paulo@89 3610 if (λ1 < λ0) z = λ0, λ0 = λ1, λ1 = z;
paulo@89 3611 var δλ = λ1 - λ0, polar = abs(δλ - π) < ε, meridian = polar || δλ < ε;
paulo@89 3612 if (!polar && φ1 < φ0) z = φ0, φ0 = φ1, φ1 = z;
paulo@89 3613 if (meridian ? polar ? φ0 + φ1 > 0 ^ q[1] < (abs(q[0] - λ0) < ε ? φ0 : φ1) : φ0 <= q[1] && q[1] <= φ1 : δλ > π ^ (λ0 <= q[0] && q[0] <= λ1)) {
paulo@89 3614 var q1 = d3_geo_cartesianScale(u, (-w + t) / uu);
paulo@89 3615 d3_geo_cartesianAdd(q1, A);
paulo@89 3616 return [ q, d3_geo_spherical(q1) ];
paulo@89 3617 }
paulo@89 3618 }
paulo@89 3619 function code(λ, φ) {
paulo@89 3620 var r = smallRadius ? radius : π - radius, code = 0;
paulo@89 3621 if (λ < -r) code |= 1; else if (λ > r) code |= 2;
paulo@89 3622 if (φ < -r) code |= 4; else if (φ > r) code |= 8;
paulo@89 3623 return code;
paulo@89 3624 }
paulo@89 3625 }
paulo@89 3626 function d3_geom_clipLine(x0, y0, x1, y1) {
paulo@89 3627 return function(line) {
paulo@89 3628 var a = line.a, b = line.b, ax = a.x, ay = a.y, bx = b.x, by = b.y, t0 = 0, t1 = 1, dx = bx - ax, dy = by - ay, r;
paulo@89 3629 r = x0 - ax;
paulo@89 3630 if (!dx && r > 0) return;
paulo@89 3631 r /= dx;
paulo@89 3632 if (dx < 0) {
paulo@89 3633 if (r < t0) return;
paulo@89 3634 if (r < t1) t1 = r;
paulo@89 3635 } else if (dx > 0) {
paulo@89 3636 if (r > t1) return;
paulo@89 3637 if (r > t0) t0 = r;
paulo@89 3638 }
paulo@89 3639 r = x1 - ax;
paulo@89 3640 if (!dx && r < 0) return;
paulo@89 3641 r /= dx;
paulo@89 3642 if (dx < 0) {
paulo@89 3643 if (r > t1) return;
paulo@89 3644 if (r > t0) t0 = r;
paulo@89 3645 } else if (dx > 0) {
paulo@89 3646 if (r < t0) return;
paulo@89 3647 if (r < t1) t1 = r;
paulo@89 3648 }
paulo@89 3649 r = y0 - ay;
paulo@89 3650 if (!dy && r > 0) return;
paulo@89 3651 r /= dy;
paulo@89 3652 if (dy < 0) {
paulo@89 3653 if (r < t0) return;
paulo@89 3654 if (r < t1) t1 = r;
paulo@89 3655 } else if (dy > 0) {
paulo@89 3656 if (r > t1) return;
paulo@89 3657 if (r > t0) t0 = r;
paulo@89 3658 }
paulo@89 3659 r = y1 - ay;
paulo@89 3660 if (!dy && r < 0) return;
paulo@89 3661 r /= dy;
paulo@89 3662 if (dy < 0) {
paulo@89 3663 if (r > t1) return;
paulo@89 3664 if (r > t0) t0 = r;
paulo@89 3665 } else if (dy > 0) {
paulo@89 3666 if (r < t0) return;
paulo@89 3667 if (r < t1) t1 = r;
paulo@89 3668 }
paulo@89 3669 if (t0 > 0) line.a = {
paulo@89 3670 x: ax + t0 * dx,
paulo@89 3671 y: ay + t0 * dy
paulo@89 3672 };
paulo@89 3673 if (t1 < 1) line.b = {
paulo@89 3674 x: ax + t1 * dx,
paulo@89 3675 y: ay + t1 * dy
paulo@89 3676 };
paulo@89 3677 return line;
paulo@89 3678 };
paulo@89 3679 }
paulo@89 3680 var d3_geo_clipExtentMAX = 1e9;
paulo@89 3681 d3.geo.clipExtent = function() {
paulo@89 3682 var x0, y0, x1, y1, stream, clip, clipExtent = {
paulo@89 3683 stream: function(output) {
paulo@89 3684 if (stream) stream.valid = false;
paulo@89 3685 stream = clip(output);
paulo@89 3686 stream.valid = true;
paulo@89 3687 return stream;
paulo@89 3688 },
paulo@89 3689 extent: function(_) {
paulo@89 3690 if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ];
paulo@89 3691 clip = d3_geo_clipExtent(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]);
paulo@89 3692 if (stream) stream.valid = false, stream = null;
paulo@89 3693 return clipExtent;
paulo@89 3694 }
paulo@89 3695 };
paulo@89 3696 return clipExtent.extent([ [ 0, 0 ], [ 960, 500 ] ]);
paulo@89 3697 };
paulo@89 3698 function d3_geo_clipExtent(x0, y0, x1, y1) {
paulo@89 3699 return function(listener) {
paulo@89 3700 var listener_ = listener, bufferListener = d3_geo_clipBufferListener(), clipLine = d3_geom_clipLine(x0, y0, x1, y1), segments, polygon, ring;
paulo@89 3701 var clip = {
paulo@89 3702 point: point,
paulo@89 3703 lineStart: lineStart,
paulo@89 3704 lineEnd: lineEnd,
paulo@89 3705 polygonStart: function() {
paulo@89 3706 listener = bufferListener;
paulo@89 3707 segments = [];
paulo@89 3708 polygon = [];
paulo@89 3709 clean = true;
paulo@89 3710 },
paulo@89 3711 polygonEnd: function() {
paulo@89 3712 listener = listener_;
paulo@89 3713 segments = d3.merge(segments);
paulo@89 3714 var clipStartInside = insidePolygon([ x0, y1 ]), inside = clean && clipStartInside, visible = segments.length;
paulo@89 3715 if (inside || visible) {
paulo@89 3716 listener.polygonStart();
paulo@89 3717 if (inside) {
paulo@89 3718 listener.lineStart();
paulo@89 3719 interpolate(null, null, 1, listener);
paulo@89 3720 listener.lineEnd();
paulo@89 3721 }
paulo@89 3722 if (visible) {
paulo@89 3723 d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener);
paulo@89 3724 }
paulo@89 3725 listener.polygonEnd();
paulo@89 3726 }
paulo@89 3727 segments = polygon = ring = null;
paulo@89 3728 }
paulo@89 3729 };
paulo@89 3730 function insidePolygon(p) {
paulo@89 3731 var wn = 0, n = polygon.length, y = p[1];
paulo@89 3732 for (var i = 0; i < n; ++i) {
paulo@89 3733 for (var j = 1, v = polygon[i], m = v.length, a = v[0], b; j < m; ++j) {
paulo@89 3734 b = v[j];
paulo@89 3735 if (a[1] <= y) {
paulo@89 3736 if (b[1] > y && d3_cross2d(a, b, p) > 0) ++wn;
paulo@89 3737 } else {
paulo@89 3738 if (b[1] <= y && d3_cross2d(a, b, p) < 0) --wn;
paulo@89 3739 }
paulo@89 3740 a = b;
paulo@89 3741 }
paulo@89 3742 }
paulo@89 3743 return wn !== 0;
paulo@89 3744 }
paulo@89 3745 function interpolate(from, to, direction, listener) {
paulo@89 3746 var a = 0, a1 = 0;
paulo@89 3747 if (from == null || (a = corner(from, direction)) !== (a1 = corner(to, direction)) || comparePoints(from, to) < 0 ^ direction > 0) {
paulo@89 3748 do {
paulo@89 3749 listener.point(a === 0 || a === 3 ? x0 : x1, a > 1 ? y1 : y0);
paulo@89 3750 } while ((a = (a + direction + 4) % 4) !== a1);
paulo@89 3751 } else {
paulo@89 3752 listener.point(to[0], to[1]);
paulo@89 3753 }
paulo@89 3754 }
paulo@89 3755 function pointVisible(x, y) {
paulo@89 3756 return x0 <= x && x <= x1 && y0 <= y && y <= y1;
paulo@89 3757 }
paulo@89 3758 function point(x, y) {
paulo@89 3759 if (pointVisible(x, y)) listener.point(x, y);
paulo@89 3760 }
paulo@89 3761 var x__, y__, v__, x_, y_, v_, first, clean;
paulo@89 3762 function lineStart() {
paulo@89 3763 clip.point = linePoint;
paulo@89 3764 if (polygon) polygon.push(ring = []);
paulo@89 3765 first = true;
paulo@89 3766 v_ = false;
paulo@89 3767 x_ = y_ = NaN;
paulo@89 3768 }
paulo@89 3769 function lineEnd() {
paulo@89 3770 if (segments) {
paulo@89 3771 linePoint(x__, y__);
paulo@89 3772 if (v__ && v_) bufferListener.rejoin();
paulo@89 3773 segments.push(bufferListener.buffer());
paulo@89 3774 }
paulo@89 3775 clip.point = point;
paulo@89 3776 if (v_) listener.lineEnd();
paulo@89 3777 }
paulo@89 3778 function linePoint(x, y) {
paulo@89 3779 x = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, x));
paulo@89 3780 y = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, y));
paulo@89 3781 var v = pointVisible(x, y);
paulo@89 3782 if (polygon) ring.push([ x, y ]);
paulo@89 3783 if (first) {
paulo@89 3784 x__ = x, y__ = y, v__ = v;
paulo@89 3785 first = false;
paulo@89 3786 if (v) {
paulo@89 3787 listener.lineStart();
paulo@89 3788 listener.point(x, y);
paulo@89 3789 }
paulo@89 3790 } else {
paulo@89 3791 if (v && v_) listener.point(x, y); else {
paulo@89 3792 var l = {
paulo@89 3793 a: {
paulo@89 3794 x: x_,
paulo@89 3795 y: y_
paulo@89 3796 },
paulo@89 3797 b: {
paulo@89 3798 x: x,
paulo@89 3799 y: y
paulo@89 3800 }
paulo@89 3801 };
paulo@89 3802 if (clipLine(l)) {
paulo@89 3803 if (!v_) {
paulo@89 3804 listener.lineStart();
paulo@89 3805 listener.point(l.a.x, l.a.y);
paulo@89 3806 }
paulo@89 3807 listener.point(l.b.x, l.b.y);
paulo@89 3808 if (!v) listener.lineEnd();
paulo@89 3809 clean = false;
paulo@89 3810 } else if (v) {
paulo@89 3811 listener.lineStart();
paulo@89 3812 listener.point(x, y);
paulo@89 3813 clean = false;
paulo@89 3814 }
paulo@89 3815 }
paulo@89 3816 }
paulo@89 3817 x_ = x, y_ = y, v_ = v;
paulo@89 3818 }
paulo@89 3819 return clip;
paulo@89 3820 };
paulo@89 3821 function corner(p, direction) {
paulo@89 3822 return abs(p[0] - x0) < ε ? direction > 0 ? 0 : 3 : abs(p[0] - x1) < ε ? direction > 0 ? 2 : 1 : abs(p[1] - y0) < ε ? direction > 0 ? 1 : 0 : direction > 0 ? 3 : 2;
paulo@89 3823 }
paulo@89 3824 function compare(a, b) {
paulo@89 3825 return comparePoints(a.x, b.x);
paulo@89 3826 }
paulo@89 3827 function comparePoints(a, b) {
paulo@89 3828 var ca = corner(a, 1), cb = corner(b, 1);
paulo@89 3829 return ca !== cb ? ca - cb : ca === 0 ? b[1] - a[1] : ca === 1 ? a[0] - b[0] : ca === 2 ? a[1] - b[1] : b[0] - a[0];
paulo@89 3830 }
paulo@89 3831 }
paulo@89 3832 function d3_geo_conic(projectAt) {
paulo@89 3833 var φ0 = 0, φ1 = π / 3, m = d3_geo_projectionMutator(projectAt), p = m(φ0, φ1);
paulo@89 3834 p.parallels = function(_) {
paulo@89 3835 if (!arguments.length) return [ φ0 / π * 180, φ1 / π * 180 ];
paulo@89 3836 return m(φ0 = _[0] * π / 180, φ1 = _[1] * π / 180);
paulo@89 3837 };
paulo@89 3838 return p;
paulo@89 3839 }
paulo@89 3840 function d3_geo_conicEqualArea(φ0, φ1) {
paulo@89 3841 var sinφ0 = Math.sin(φ0), n = (sinφ0 + Math.sin(φ1)) / 2, C = 1 + sinφ0 * (2 * n - sinφ0), ρ0 = Math.sqrt(C) / n;
paulo@89 3842 function forward(λ, φ) {
paulo@89 3843 var ρ = Math.sqrt(C - 2 * n * Math.sin(φ)) / n;
paulo@89 3844 return [ ρ * Math.sin(λ *= n), ρ0 - ρ * Math.cos(λ) ];
paulo@89 3845 }
paulo@89 3846 forward.invert = function(x, y) {
paulo@89 3847 var ρ0_y = ρ0 - y;
paulo@89 3848 return [ Math.atan2(x, ρ0_y) / n, d3_asin((C - (x * x + ρ0_y * ρ0_y) * n * n) / (2 * n)) ];
paulo@89 3849 };
paulo@89 3850 return forward;
paulo@89 3851 }
paulo@89 3852 (d3.geo.conicEqualArea = function() {
paulo@89 3853 return d3_geo_conic(d3_geo_conicEqualArea);
paulo@89 3854 }).raw = d3_geo_conicEqualArea;
paulo@89 3855 d3.geo.albers = function() {
paulo@89 3856 return d3.geo.conicEqualArea().rotate([ 96, 0 ]).center([ -.6, 38.7 ]).parallels([ 29.5, 45.5 ]).scale(1070);
paulo@89 3857 };
paulo@89 3858 d3.geo.albersUsa = function() {
paulo@89 3859 var lower48 = d3.geo.albers();
paulo@89 3860 var alaska = d3.geo.conicEqualArea().rotate([ 154, 0 ]).center([ -2, 58.5 ]).parallels([ 55, 65 ]);
paulo@89 3861 var hawaii = d3.geo.conicEqualArea().rotate([ 157, 0 ]).center([ -3, 19.9 ]).parallels([ 8, 18 ]);
paulo@89 3862 var point, pointStream = {
paulo@89 3863 point: function(x, y) {
paulo@89 3864 point = [ x, y ];
paulo@89 3865 }
paulo@89 3866 }, lower48Point, alaskaPoint, hawaiiPoint;
paulo@89 3867 function albersUsa(coordinates) {
paulo@89 3868 var x = coordinates[0], y = coordinates[1];
paulo@89 3869 point = null;
paulo@89 3870 (lower48Point(x, y), point) || (alaskaPoint(x, y), point) || hawaiiPoint(x, y);
paulo@89 3871 return point;
paulo@89 3872 }
paulo@89 3873 albersUsa.invert = function(coordinates) {
paulo@89 3874 var k = lower48.scale(), t = lower48.translate(), x = (coordinates[0] - t[0]) / k, y = (coordinates[1] - t[1]) / k;
paulo@89 3875 return (y >= .12 && y < .234 && x >= -.425 && x < -.214 ? alaska : y >= .166 && y < .234 && x >= -.214 && x < -.115 ? hawaii : lower48).invert(coordinates);
paulo@89 3876 };
paulo@89 3877 albersUsa.stream = function(stream) {
paulo@89 3878 var lower48Stream = lower48.stream(stream), alaskaStream = alaska.stream(stream), hawaiiStream = hawaii.stream(stream);
paulo@89 3879 return {
paulo@89 3880 point: function(x, y) {
paulo@89 3881 lower48Stream.point(x, y);
paulo@89 3882 alaskaStream.point(x, y);
paulo@89 3883 hawaiiStream.point(x, y);
paulo@89 3884 },
paulo@89 3885 sphere: function() {
paulo@89 3886 lower48Stream.sphere();
paulo@89 3887 alaskaStream.sphere();
paulo@89 3888 hawaiiStream.sphere();
paulo@89 3889 },
paulo@89 3890 lineStart: function() {
paulo@89 3891 lower48Stream.lineStart();
paulo@89 3892 alaskaStream.lineStart();
paulo@89 3893 hawaiiStream.lineStart();
paulo@89 3894 },
paulo@89 3895 lineEnd: function() {
paulo@89 3896 lower48Stream.lineEnd();
paulo@89 3897 alaskaStream.lineEnd();
paulo@89 3898 hawaiiStream.lineEnd();
paulo@89 3899 },
paulo@89 3900 polygonStart: function() {
paulo@89 3901 lower48Stream.polygonStart();
paulo@89 3902 alaskaStream.polygonStart();
paulo@89 3903 hawaiiStream.polygonStart();
paulo@89 3904 },
paulo@89 3905 polygonEnd: function() {
paulo@89 3906 lower48Stream.polygonEnd();
paulo@89 3907 alaskaStream.polygonEnd();
paulo@89 3908 hawaiiStream.polygonEnd();
paulo@89 3909 }
paulo@89 3910 };
paulo@89 3911 };
paulo@89 3912 albersUsa.precision = function(_) {
paulo@89 3913 if (!arguments.length) return lower48.precision();
paulo@89 3914 lower48.precision(_);
paulo@89 3915 alaska.precision(_);
paulo@89 3916 hawaii.precision(_);
paulo@89 3917 return albersUsa;
paulo@89 3918 };
paulo@89 3919 albersUsa.scale = function(_) {
paulo@89 3920 if (!arguments.length) return lower48.scale();
paulo@89 3921 lower48.scale(_);
paulo@89 3922 alaska.scale(_ * .35);
paulo@89 3923 hawaii.scale(_);
paulo@89 3924 return albersUsa.translate(lower48.translate());
paulo@89 3925 };
paulo@89 3926 albersUsa.translate = function(_) {
paulo@89 3927 if (!arguments.length) return lower48.translate();
paulo@89 3928 var k = lower48.scale(), x = +_[0], y = +_[1];
paulo@89 3929 lower48Point = lower48.translate(_).clipExtent([ [ x - .455 * k, y - .238 * k ], [ x + .455 * k, y + .238 * k ] ]).stream(pointStream).point;
paulo@89 3930 alaskaPoint = alaska.translate([ x - .307 * k, y + .201 * k ]).clipExtent([ [ x - .425 * k + ε, y + .12 * k + ε ], [ x - .214 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point;
paulo@89 3931 hawaiiPoint = hawaii.translate([ x - .205 * k, y + .212 * k ]).clipExtent([ [ x - .214 * k + ε, y + .166 * k + ε ], [ x - .115 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point;
paulo@89 3932 return albersUsa;
paulo@89 3933 };
paulo@89 3934 return albersUsa.scale(1070);
paulo@89 3935 };
paulo@89 3936 var d3_geo_pathAreaSum, d3_geo_pathAreaPolygon, d3_geo_pathArea = {
paulo@89 3937 point: d3_noop,
paulo@89 3938 lineStart: d3_noop,
paulo@89 3939 lineEnd: d3_noop,
paulo@89 3940 polygonStart: function() {
paulo@89 3941 d3_geo_pathAreaPolygon = 0;
paulo@89 3942 d3_geo_pathArea.lineStart = d3_geo_pathAreaRingStart;
paulo@89 3943 },
paulo@89 3944 polygonEnd: function() {
paulo@89 3945 d3_geo_pathArea.lineStart = d3_geo_pathArea.lineEnd = d3_geo_pathArea.point = d3_noop;
paulo@89 3946 d3_geo_pathAreaSum += abs(d3_geo_pathAreaPolygon / 2);
paulo@89 3947 }
paulo@89 3948 };
paulo@89 3949 function d3_geo_pathAreaRingStart() {
paulo@89 3950 var x00, y00, x0, y0;
paulo@89 3951 d3_geo_pathArea.point = function(x, y) {
paulo@89 3952 d3_geo_pathArea.point = nextPoint;
paulo@89 3953 x00 = x0 = x, y00 = y0 = y;
paulo@89 3954 };
paulo@89 3955 function nextPoint(x, y) {
paulo@89 3956 d3_geo_pathAreaPolygon += y0 * x - x0 * y;
paulo@89 3957 x0 = x, y0 = y;
paulo@89 3958 }
paulo@89 3959 d3_geo_pathArea.lineEnd = function() {
paulo@89 3960 nextPoint(x00, y00);
paulo@89 3961 };
paulo@89 3962 }
paulo@89 3963 var d3_geo_pathBoundsX0, d3_geo_pathBoundsY0, d3_geo_pathBoundsX1, d3_geo_pathBoundsY1;
paulo@89 3964 var d3_geo_pathBounds = {
paulo@89 3965 point: d3_geo_pathBoundsPoint,
paulo@89 3966 lineStart: d3_noop,
paulo@89 3967 lineEnd: d3_noop,
paulo@89 3968 polygonStart: d3_noop,
paulo@89 3969 polygonEnd: d3_noop
paulo@89 3970 };
paulo@89 3971 function d3_geo_pathBoundsPoint(x, y) {
paulo@89 3972 if (x < d3_geo_pathBoundsX0) d3_geo_pathBoundsX0 = x;
paulo@89 3973 if (x > d3_geo_pathBoundsX1) d3_geo_pathBoundsX1 = x;
paulo@89 3974 if (y < d3_geo_pathBoundsY0) d3_geo_pathBoundsY0 = y;
paulo@89 3975 if (y > d3_geo_pathBoundsY1) d3_geo_pathBoundsY1 = y;
paulo@89 3976 }
paulo@89 3977 function d3_geo_pathBuffer() {
paulo@89 3978 var pointCircle = d3_geo_pathBufferCircle(4.5), buffer = [];
paulo@89 3979 var stream = {
paulo@89 3980 point: point,
paulo@89 3981 lineStart: function() {
paulo@89 3982 stream.point = pointLineStart;
paulo@89 3983 },
paulo@89 3984 lineEnd: lineEnd,
paulo@89 3985 polygonStart: function() {
paulo@89 3986 stream.lineEnd = lineEndPolygon;
paulo@89 3987 },
paulo@89 3988 polygonEnd: function() {
paulo@89 3989 stream.lineEnd = lineEnd;
paulo@89 3990 stream.point = point;
paulo@89 3991 },
paulo@89 3992 pointRadius: function(_) {
paulo@89 3993 pointCircle = d3_geo_pathBufferCircle(_);
paulo@89 3994 return stream;
paulo@89 3995 },
paulo@89 3996 result: function() {
paulo@89 3997 if (buffer.length) {
paulo@89 3998 var result = buffer.join("");
paulo@89 3999 buffer = [];
paulo@89 4000 return result;
paulo@89 4001 }
paulo@89 4002 }
paulo@89 4003 };
paulo@89 4004 function point(x, y) {
paulo@89 4005 buffer.push("M", x, ",", y, pointCircle);
paulo@89 4006 }
paulo@89 4007 function pointLineStart(x, y) {
paulo@89 4008 buffer.push("M", x, ",", y);
paulo@89 4009 stream.point = pointLine;
paulo@89 4010 }
paulo@89 4011 function pointLine(x, y) {
paulo@89 4012 buffer.push("L", x, ",", y);
paulo@89 4013 }
paulo@89 4014 function lineEnd() {
paulo@89 4015 stream.point = point;
paulo@89 4016 }
paulo@89 4017 function lineEndPolygon() {
paulo@89 4018 buffer.push("Z");
paulo@89 4019 }
paulo@89 4020 return stream;
paulo@89 4021 }
paulo@89 4022 function d3_geo_pathBufferCircle(radius) {
paulo@89 4023 return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + 2 * radius + "z";
paulo@89 4024 }
paulo@89 4025 var d3_geo_pathCentroid = {
paulo@89 4026 point: d3_geo_pathCentroidPoint,
paulo@89 4027 lineStart: d3_geo_pathCentroidLineStart,
paulo@89 4028 lineEnd: d3_geo_pathCentroidLineEnd,
paulo@89 4029 polygonStart: function() {
paulo@89 4030 d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidRingStart;
paulo@89 4031 },
paulo@89 4032 polygonEnd: function() {
paulo@89 4033 d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint;
paulo@89 4034 d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidLineStart;
paulo@89 4035 d3_geo_pathCentroid.lineEnd = d3_geo_pathCentroidLineEnd;
paulo@89 4036 }
paulo@89 4037 };
paulo@89 4038 function d3_geo_pathCentroidPoint(x, y) {
paulo@89 4039 d3_geo_centroidX0 += x;
paulo@89 4040 d3_geo_centroidY0 += y;
paulo@89 4041 ++d3_geo_centroidZ0;
paulo@89 4042 }
paulo@89 4043 function d3_geo_pathCentroidLineStart() {
paulo@89 4044 var x0, y0;
paulo@89 4045 d3_geo_pathCentroid.point = function(x, y) {
paulo@89 4046 d3_geo_pathCentroid.point = nextPoint;
paulo@89 4047 d3_geo_pathCentroidPoint(x0 = x, y0 = y);
paulo@89 4048 };
paulo@89 4049 function nextPoint(x, y) {
paulo@89 4050 var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy);
paulo@89 4051 d3_geo_centroidX1 += z * (x0 + x) / 2;
paulo@89 4052 d3_geo_centroidY1 += z * (y0 + y) / 2;
paulo@89 4053 d3_geo_centroidZ1 += z;
paulo@89 4054 d3_geo_pathCentroidPoint(x0 = x, y0 = y);
paulo@89 4055 }
paulo@89 4056 }
paulo@89 4057 function d3_geo_pathCentroidLineEnd() {
paulo@89 4058 d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint;
paulo@89 4059 }
paulo@89 4060 function d3_geo_pathCentroidRingStart() {
paulo@89 4061 var x00, y00, x0, y0;
paulo@89 4062 d3_geo_pathCentroid.point = function(x, y) {
paulo@89 4063 d3_geo_pathCentroid.point = nextPoint;
paulo@89 4064 d3_geo_pathCentroidPoint(x00 = x0 = x, y00 = y0 = y);
paulo@89 4065 };
paulo@89 4066 function nextPoint(x, y) {
paulo@89 4067 var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy);
paulo@89 4068 d3_geo_centroidX1 += z * (x0 + x) / 2;
paulo@89 4069 d3_geo_centroidY1 += z * (y0 + y) / 2;
paulo@89 4070 d3_geo_centroidZ1 += z;
paulo@89 4071 z = y0 * x - x0 * y;
paulo@89 4072 d3_geo_centroidX2 += z * (x0 + x);
paulo@89 4073 d3_geo_centroidY2 += z * (y0 + y);
paulo@89 4074 d3_geo_centroidZ2 += z * 3;
paulo@89 4075 d3_geo_pathCentroidPoint(x0 = x, y0 = y);
paulo@89 4076 }
paulo@89 4077 d3_geo_pathCentroid.lineEnd = function() {
paulo@89 4078 nextPoint(x00, y00);
paulo@89 4079 };
paulo@89 4080 }
paulo@89 4081 function d3_geo_pathContext(context) {
paulo@89 4082 var pointRadius = 4.5;
paulo@89 4083 var stream = {
paulo@89 4084 point: point,
paulo@89 4085 lineStart: function() {
paulo@89 4086 stream.point = pointLineStart;
paulo@89 4087 },
paulo@89 4088 lineEnd: lineEnd,
paulo@89 4089 polygonStart: function() {
paulo@89 4090 stream.lineEnd = lineEndPolygon;
paulo@89 4091 },
paulo@89 4092 polygonEnd: function() {
paulo@89 4093 stream.lineEnd = lineEnd;
paulo@89 4094 stream.point = point;
paulo@89 4095 },
paulo@89 4096 pointRadius: function(_) {
paulo@89 4097 pointRadius = _;
paulo@89 4098 return stream;
paulo@89 4099 },
paulo@89 4100 result: d3_noop
paulo@89 4101 };
paulo@89 4102 function point(x, y) {
paulo@89 4103 context.moveTo(x + pointRadius, y);
paulo@89 4104 context.arc(x, y, pointRadius, 0, τ);
paulo@89 4105 }
paulo@89 4106 function pointLineStart(x, y) {
paulo@89 4107 context.moveTo(x, y);
paulo@89 4108 stream.point = pointLine;
paulo@89 4109 }
paulo@89 4110 function pointLine(x, y) {
paulo@89 4111 context.lineTo(x, y);
paulo@89 4112 }
paulo@89 4113 function lineEnd() {
paulo@89 4114 stream.point = point;
paulo@89 4115 }
paulo@89 4116 function lineEndPolygon() {
paulo@89 4117 context.closePath();
paulo@89 4118 }
paulo@89 4119 return stream;
paulo@89 4120 }
paulo@89 4121 function d3_geo_resample(project) {
paulo@89 4122 var δ2 = .5, cosMinDistance = Math.cos(30 * d3_radians), maxDepth = 16;
paulo@89 4123 function resample(stream) {
paulo@89 4124 return (maxDepth ? resampleRecursive : resampleNone)(stream);
paulo@89 4125 }
paulo@89 4126 function resampleNone(stream) {
paulo@89 4127 return d3_geo_transformPoint(stream, function(x, y) {
paulo@89 4128 x = project(x, y);
paulo@89 4129 stream.point(x[0], x[1]);
paulo@89 4130 });
paulo@89 4131 }
paulo@89 4132 function resampleRecursive(stream) {
paulo@89 4133 var λ00, φ00, x00, y00, a00, b00, c00, λ0, x0, y0, a0, b0, c0;
paulo@89 4134 var resample = {
paulo@89 4135 point: point,
paulo@89 4136 lineStart: lineStart,
paulo@89 4137 lineEnd: lineEnd,
paulo@89 4138 polygonStart: function() {
paulo@89 4139 stream.polygonStart();
paulo@89 4140 resample.lineStart = ringStart;
paulo@89 4141 },
paulo@89 4142 polygonEnd: function() {
paulo@89 4143 stream.polygonEnd();
paulo@89 4144 resample.lineStart = lineStart;
paulo@89 4145 }
paulo@89 4146 };
paulo@89 4147 function point(x, y) {
paulo@89 4148 x = project(x, y);
paulo@89 4149 stream.point(x[0], x[1]);
paulo@89 4150 }
paulo@89 4151 function lineStart() {
paulo@89 4152 x0 = NaN;
paulo@89 4153 resample.point = linePoint;
paulo@89 4154 stream.lineStart();
paulo@89 4155 }
paulo@89 4156 function linePoint(λ, φ) {
paulo@89 4157 var c = d3_geo_cartesian([ λ, φ ]), p = project(λ, φ);
paulo@89 4158 resampleLineTo(x0, y0, λ0, a0, b0, c0, x0 = p[0], y0 = p[1], λ0 = λ, a0 = c[0], b0 = c[1], c0 = c[2], maxDepth, stream);
paulo@89 4159 stream.point(x0, y0);
paulo@89 4160 }
paulo@89 4161 function lineEnd() {
paulo@89 4162 resample.point = point;
paulo@89 4163 stream.lineEnd();
paulo@89 4164 }
paulo@89 4165 function ringStart() {
paulo@89 4166 lineStart();
paulo@89 4167 resample.point = ringPoint;
paulo@89 4168 resample.lineEnd = ringEnd;
paulo@89 4169 }
paulo@89 4170 function ringPoint(λ, φ) {
paulo@89 4171 linePoint(λ00 = λ, φ00 = φ), x00 = x0, y00 = y0, a00 = a0, b00 = b0, c00 = c0;
paulo@89 4172 resample.point = linePoint;
paulo@89 4173 }
paulo@89 4174 function ringEnd() {
paulo@89 4175 resampleLineTo(x0, y0, λ0, a0, b0, c0, x00, y00, λ00, a00, b00, c00, maxDepth, stream);
paulo@89 4176 resample.lineEnd = lineEnd;
paulo@89 4177 lineEnd();
paulo@89 4178 }
paulo@89 4179 return resample;
paulo@89 4180 }
paulo@89 4181 function resampleLineTo(x0, y0, λ0, a0, b0, c0, x1, y1, λ1, a1, b1, c1, depth, stream) {
paulo@89 4182 var dx = x1 - x0, dy = y1 - y0, d2 = dx * dx + dy * dy;
paulo@89 4183 if (d2 > 4 * δ2 && depth--) {
paulo@89 4184 var a = a0 + a1, b = b0 + b1, c = c0 + c1, m = Math.sqrt(a * a + b * b + c * c), φ2 = Math.asin(c /= m), λ2 = abs(abs(c) - 1) < ε || abs(λ0 - λ1) < ε ? (λ0 + λ1) / 2 : Math.atan2(b, a), p = project(λ2, φ2), x2 = p[0], y2 = p[1], dx2 = x2 - x0, dy2 = y2 - y0, dz = dy * dx2 - dx * dy2;
paulo@89 4185 if (dz * dz / d2 > δ2 || abs((dx * dx2 + dy * dy2) / d2 - .5) > .3 || a0 * a1 + b0 * b1 + c0 * c1 < cosMinDistance) {
paulo@89 4186 resampleLineTo(x0, y0, λ0, a0, b0, c0, x2, y2, λ2, a /= m, b /= m, c, depth, stream);
paulo@89 4187 stream.point(x2, y2);
paulo@89 4188 resampleLineTo(x2, y2, λ2, a, b, c, x1, y1, λ1, a1, b1, c1, depth, stream);
paulo@89 4189 }
paulo@89 4190 }
paulo@89 4191 }
paulo@89 4192 resample.precision = function(_) {
paulo@89 4193 if (!arguments.length) return Math.sqrt(δ2);
paulo@89 4194 maxDepth = (δ2 = _ * _) > 0 && 16;
paulo@89 4195 return resample;
paulo@89 4196 };
paulo@89 4197 return resample;
paulo@89 4198 }
paulo@89 4199 d3.geo.path = function() {
paulo@89 4200 var pointRadius = 4.5, projection, context, projectStream, contextStream, cacheStream;
paulo@89 4201 function path(object) {
paulo@89 4202 if (object) {
paulo@89 4203 if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments));
paulo@89 4204 if (!cacheStream || !cacheStream.valid) cacheStream = projectStream(contextStream);
paulo@89 4205 d3.geo.stream(object, cacheStream);
paulo@89 4206 }
paulo@89 4207 return contextStream.result();
paulo@89 4208 }
paulo@89 4209 path.area = function(object) {
paulo@89 4210 d3_geo_pathAreaSum = 0;
paulo@89 4211 d3.geo.stream(object, projectStream(d3_geo_pathArea));
paulo@89 4212 return d3_geo_pathAreaSum;
paulo@89 4213 };
paulo@89 4214 path.centroid = function(object) {
paulo@89 4215 d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0;
paulo@89 4216 d3.geo.stream(object, projectStream(d3_geo_pathCentroid));
paulo@89 4217 return d3_geo_centroidZ2 ? [ d3_geo_centroidX2 / d3_geo_centroidZ2, d3_geo_centroidY2 / d3_geo_centroidZ2 ] : d3_geo_centroidZ1 ? [ d3_geo_centroidX1 / d3_geo_centroidZ1, d3_geo_centroidY1 / d3_geo_centroidZ1 ] : d3_geo_centroidZ0 ? [ d3_geo_centroidX0 / d3_geo_centroidZ0, d3_geo_centroidY0 / d3_geo_centroidZ0 ] : [ NaN, NaN ];
paulo@89 4218 };
paulo@89 4219 path.bounds = function(object) {
paulo@89 4220 d3_geo_pathBoundsX1 = d3_geo_pathBoundsY1 = -(d3_geo_pathBoundsX0 = d3_geo_pathBoundsY0 = Infinity);
paulo@89 4221 d3.geo.stream(object, projectStream(d3_geo_pathBounds));
paulo@89 4222 return [ [ d3_geo_pathBoundsX0, d3_geo_pathBoundsY0 ], [ d3_geo_pathBoundsX1, d3_geo_pathBoundsY1 ] ];
paulo@89 4223 };
paulo@89 4224 path.projection = function(_) {
paulo@89 4225 if (!arguments.length) return projection;
paulo@89 4226 projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity;
paulo@89 4227 return reset();
paulo@89 4228 };
paulo@89 4229 path.context = function(_) {
paulo@89 4230 if (!arguments.length) return context;
paulo@89 4231 contextStream = (context = _) == null ? new d3_geo_pathBuffer() : new d3_geo_pathContext(_);
paulo@89 4232 if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius);
paulo@89 4233 return reset();
paulo@89 4234 };
paulo@89 4235 path.pointRadius = function(_) {
paulo@89 4236 if (!arguments.length) return pointRadius;
paulo@89 4237 pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_);
paulo@89 4238 return path;
paulo@89 4239 };
paulo@89 4240 function reset() {
paulo@89 4241 cacheStream = null;
paulo@89 4242 return path;
paulo@89 4243 }
paulo@89 4244 return path.projection(d3.geo.albersUsa()).context(null);
paulo@89 4245 };
paulo@89 4246 function d3_geo_pathProjectStream(project) {
paulo@89 4247 var resample = d3_geo_resample(function(x, y) {
paulo@89 4248 return project([ x * d3_degrees, y * d3_degrees ]);
paulo@89 4249 });
paulo@89 4250 return function(stream) {
paulo@89 4251 return d3_geo_projectionRadians(resample(stream));
paulo@89 4252 };
paulo@89 4253 }
paulo@89 4254 d3.geo.transform = function(methods) {
paulo@89 4255 return {
paulo@89 4256 stream: function(stream) {
paulo@89 4257 var transform = new d3_geo_transform(stream);
paulo@89 4258 for (var k in methods) transform[k] = methods[k];
paulo@89 4259 return transform;
paulo@89 4260 }
paulo@89 4261 };
paulo@89 4262 };
paulo@89 4263 function d3_geo_transform(stream) {
paulo@89 4264 this.stream = stream;
paulo@89 4265 }
paulo@89 4266 d3_geo_transform.prototype = {
paulo@89 4267 point: function(x, y) {
paulo@89 4268 this.stream.point(x, y);
paulo@89 4269 },
paulo@89 4270 sphere: function() {
paulo@89 4271 this.stream.sphere();
paulo@89 4272 },
paulo@89 4273 lineStart: function() {
paulo@89 4274 this.stream.lineStart();
paulo@89 4275 },
paulo@89 4276 lineEnd: function() {
paulo@89 4277 this.stream.lineEnd();
paulo@89 4278 },
paulo@89 4279 polygonStart: function() {
paulo@89 4280 this.stream.polygonStart();
paulo@89 4281 },
paulo@89 4282 polygonEnd: function() {
paulo@89 4283 this.stream.polygonEnd();
paulo@89 4284 }
paulo@89 4285 };
paulo@89 4286 function d3_geo_transformPoint(stream, point) {
paulo@89 4287 return {
paulo@89 4288 point: point,
paulo@89 4289 sphere: function() {
paulo@89 4290 stream.sphere();
paulo@89 4291 },
paulo@89 4292 lineStart: function() {
paulo@89 4293 stream.lineStart();
paulo@89 4294 },
paulo@89 4295 lineEnd: function() {
paulo@89 4296 stream.lineEnd();
paulo@89 4297 },
paulo@89 4298 polygonStart: function() {
paulo@89 4299 stream.polygonStart();
paulo@89 4300 },
paulo@89 4301 polygonEnd: function() {
paulo@89 4302 stream.polygonEnd();
paulo@89 4303 }
paulo@89 4304 };
paulo@89 4305 }
paulo@89 4306 d3.geo.projection = d3_geo_projection;
paulo@89 4307 d3.geo.projectionMutator = d3_geo_projectionMutator;
paulo@89 4308 function d3_geo_projection(project) {
paulo@89 4309 return d3_geo_projectionMutator(function() {
paulo@89 4310 return project;
paulo@89 4311 })();
paulo@89 4312 }
paulo@89 4313 function d3_geo_projectionMutator(projectAt) {
paulo@89 4314 var project, rotate, projectRotate, projectResample = d3_geo_resample(function(x, y) {
paulo@89 4315 x = project(x, y);
paulo@89 4316 return [ x[0] * k + δx, δy - x[1] * k ];
paulo@89 4317 }), k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx, δy, preclip = d3_geo_clipAntimeridian, postclip = d3_identity, clipAngle = null, clipExtent = null, stream;
paulo@89 4318 function projection(point) {
paulo@89 4319 point = projectRotate(point[0] * d3_radians, point[1] * d3_radians);
paulo@89 4320 return [ point[0] * k + δx, δy - point[1] * k ];
paulo@89 4321 }
paulo@89 4322 function invert(point) {
paulo@89 4323 point = projectRotate.invert((point[0] - δx) / k, (δy - point[1]) / k);
paulo@89 4324 return point && [ point[0] * d3_degrees, point[1] * d3_degrees ];
paulo@89 4325 }
paulo@89 4326 projection.stream = function(output) {
paulo@89 4327 if (stream) stream.valid = false;
paulo@89 4328 stream = d3_geo_projectionRadians(preclip(rotate, projectResample(postclip(output))));
paulo@89 4329 stream.valid = true;
paulo@89 4330 return stream;
paulo@89 4331 };
paulo@89 4332 projection.clipAngle = function(_) {
paulo@89 4333 if (!arguments.length) return clipAngle;
paulo@89 4334 preclip = _ == null ? (clipAngle = _, d3_geo_clipAntimeridian) : d3_geo_clipCircle((clipAngle = +_) * d3_radians);
paulo@89 4335 return invalidate();
paulo@89 4336 };
paulo@89 4337 projection.clipExtent = function(_) {
paulo@89 4338 if (!arguments.length) return clipExtent;
paulo@89 4339 clipExtent = _;
paulo@89 4340 postclip = _ ? d3_geo_clipExtent(_[0][0], _[0][1], _[1][0], _[1][1]) : d3_identity;
paulo@89 4341 return invalidate();
paulo@89 4342 };
paulo@89 4343 projection.scale = function(_) {
paulo@89 4344 if (!arguments.length) return k;
paulo@89 4345 k = +_;
paulo@89 4346 return reset();
paulo@89 4347 };
paulo@89 4348 projection.translate = function(_) {
paulo@89 4349 if (!arguments.length) return [ x, y ];
paulo@89 4350 x = +_[0];
paulo@89 4351 y = +_[1];
paulo@89 4352 return reset();
paulo@89 4353 };
paulo@89 4354 projection.center = function(_) {
paulo@89 4355 if (!arguments.length) return [ λ * d3_degrees, φ * d3_degrees ];
paulo@89 4356 λ = _[0] % 360 * d3_radians;
paulo@89 4357 φ = _[1] % 360 * d3_radians;
paulo@89 4358 return reset();
paulo@89 4359 };
paulo@89 4360 projection.rotate = function(_) {
paulo@89 4361 if (!arguments.length) return [ δλ * d3_degrees, δφ * d3_degrees, δγ * d3_degrees ];
paulo@89 4362 δλ = _[0] % 360 * d3_radians;
paulo@89 4363 δφ = _[1] % 360 * d3_radians;
paulo@89 4364 δγ = _.length > 2 ? _[2] % 360 * d3_radians : 0;
paulo@89 4365 return reset();
paulo@89 4366 };
paulo@89 4367 d3.rebind(projection, projectResample, "precision");
paulo@89 4368 function reset() {
paulo@89 4369 projectRotate = d3_geo_compose(rotate = d3_geo_rotation(δλ, δφ, δγ), project);
paulo@89 4370 var center = project(λ, φ);
paulo@89 4371 δx = x - center[0] * k;
paulo@89 4372 δy = y + center[1] * k;
paulo@89 4373 return invalidate();
paulo@89 4374 }
paulo@89 4375 function invalidate() {
paulo@89 4376 if (stream) stream.valid = false, stream = null;
paulo@89 4377 return projection;
paulo@89 4378 }
paulo@89 4379 return function() {
paulo@89 4380 project = projectAt.apply(this, arguments);
paulo@89 4381 projection.invert = project.invert && invert;
paulo@89 4382 return reset();
paulo@89 4383 };
paulo@89 4384 }
paulo@89 4385 function d3_geo_projectionRadians(stream) {
paulo@89 4386 return d3_geo_transformPoint(stream, function(x, y) {
paulo@89 4387 stream.point(x * d3_radians, y * d3_radians);
paulo@89 4388 });
paulo@89 4389 }
paulo@89 4390 function d3_geo_equirectangular(λ, φ) {
paulo@89 4391 return [ λ, φ ];
paulo@89 4392 }
paulo@89 4393 (d3.geo.equirectangular = function() {
paulo@89 4394 return d3_geo_projection(d3_geo_equirectangular);
paulo@89 4395 }).raw = d3_geo_equirectangular.invert = d3_geo_equirectangular;
paulo@89 4396 d3.geo.rotation = function(rotate) {
paulo@89 4397 rotate = d3_geo_rotation(rotate[0] % 360 * d3_radians, rotate[1] * d3_radians, rotate.length > 2 ? rotate[2] * d3_radians : 0);
paulo@89 4398 function forward(coordinates) {
paulo@89 4399 coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians);
paulo@89 4400 return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates;
paulo@89 4401 }
paulo@89 4402 forward.invert = function(coordinates) {
paulo@89 4403 coordinates = rotate.invert(coordinates[0] * d3_radians, coordinates[1] * d3_radians);
paulo@89 4404 return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates;
paulo@89 4405 };
paulo@89 4406 return forward;
paulo@89 4407 };
paulo@89 4408 function d3_geo_identityRotation(λ, φ) {
paulo@89 4409 return [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ];
paulo@89 4410 }
paulo@89 4411 d3_geo_identityRotation.invert = d3_geo_equirectangular;
paulo@89 4412 function d3_geo_rotation(δλ, δφ, δγ) {
paulo@89 4413 return δλ ? δφ || δγ ? d3_geo_compose(d3_geo_rotationλ(δλ), d3_geo_rotationφγ(δφ, δγ)) : d3_geo_rotationλ(δλ) : δφ || δγ ? d3_geo_rotationφγ(δφ, δγ) : d3_geo_identityRotation;
paulo@89 4414 }
paulo@89 4415 function d3_geo_forwardRotationλ(δλ) {
paulo@89 4416 return function(λ, φ) {
paulo@89 4417 return λ += δλ, [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ];
paulo@89 4418 };
paulo@89 4419 }
paulo@89 4420 function d3_geo_rotationλ(δλ) {
paulo@89 4421 var rotation = d3_geo_forwardRotationλ(δλ);
paulo@89 4422 rotation.invert = d3_geo_forwardRotationλ(-δλ);
paulo@89 4423 return rotation;
paulo@89 4424 }
paulo@89 4425 function d3_geo_rotationφγ(δφ, δγ) {
paulo@89 4426 var cosδφ = Math.cos(δφ), sinδφ = Math.sin(δφ), cosδγ = Math.cos(δγ), sinδγ = Math.sin(δγ);
paulo@89 4427 function rotation(λ, φ) {
paulo@89 4428 var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδφ + x * sinδφ;
paulo@89 4429 return [ Math.atan2(y * cosδγ - k * sinδγ, x * cosδφ - z * sinδφ), d3_asin(k * cosδγ + y * sinδγ) ];
paulo@89 4430 }
paulo@89 4431 rotation.invert = function(λ, φ) {
paulo@89 4432 var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδγ - y * sinδγ;
paulo@89 4433 return [ Math.atan2(y * cosδγ + z * sinδγ, x * cosδφ + k * sinδφ), d3_asin(k * cosδφ - x * sinδφ) ];
paulo@89 4434 };
paulo@89 4435 return rotation;
paulo@89 4436 }
paulo@89 4437 d3.geo.circle = function() {
paulo@89 4438 var origin = [ 0, 0 ], angle, precision = 6, interpolate;
paulo@89 4439 function circle() {
paulo@89 4440 var center = typeof origin === "function" ? origin.apply(this, arguments) : origin, rotate = d3_geo_rotation(-center[0] * d3_radians, -center[1] * d3_radians, 0).invert, ring = [];
paulo@89 4441 interpolate(null, null, 1, {
paulo@89 4442 point: function(x, y) {
paulo@89 4443 ring.push(x = rotate(x, y));
paulo@89 4444 x[0] *= d3_degrees, x[1] *= d3_degrees;
paulo@89 4445 }
paulo@89 4446 });
paulo@89 4447 return {
paulo@89 4448 type: "Polygon",
paulo@89 4449 coordinates: [ ring ]
paulo@89 4450 };
paulo@89 4451 }
paulo@89 4452 circle.origin = function(x) {
paulo@89 4453 if (!arguments.length) return origin;
paulo@89 4454 origin = x;
paulo@89 4455 return circle;
paulo@89 4456 };
paulo@89 4457 circle.angle = function(x) {
paulo@89 4458 if (!arguments.length) return angle;
paulo@89 4459 interpolate = d3_geo_circleInterpolate((angle = +x) * d3_radians, precision * d3_radians);
paulo@89 4460 return circle;
paulo@89 4461 };
paulo@89 4462 circle.precision = function(_) {
paulo@89 4463 if (!arguments.length) return precision;
paulo@89 4464 interpolate = d3_geo_circleInterpolate(angle * d3_radians, (precision = +_) * d3_radians);
paulo@89 4465 return circle;
paulo@89 4466 };
paulo@89 4467 return circle.angle(90);
paulo@89 4468 };
paulo@89 4469 function d3_geo_circleInterpolate(radius, precision) {
paulo@89 4470 var cr = Math.cos(radius), sr = Math.sin(radius);
paulo@89 4471 return function(from, to, direction, listener) {
paulo@89 4472 var step = direction * precision;
paulo@89 4473 if (from != null) {
paulo@89 4474 from = d3_geo_circleAngle(cr, from);
paulo@89 4475 to = d3_geo_circleAngle(cr, to);
paulo@89 4476 if (direction > 0 ? from < to : from > to) from += direction * τ;
paulo@89 4477 } else {
paulo@89 4478 from = radius + direction * τ;
paulo@89 4479 to = radius - .5 * step;
paulo@89 4480 }
paulo@89 4481 for (var point, t = from; direction > 0 ? t > to : t < to; t -= step) {
paulo@89 4482 listener.point((point = d3_geo_spherical([ cr, -sr * Math.cos(t), -sr * Math.sin(t) ]))[0], point[1]);
paulo@89 4483 }
paulo@89 4484 };
paulo@89 4485 }
paulo@89 4486 function d3_geo_circleAngle(cr, point) {
paulo@89 4487 var a = d3_geo_cartesian(point);
paulo@89 4488 a[0] -= cr;
paulo@89 4489 d3_geo_cartesianNormalize(a);
paulo@89 4490 var angle = d3_acos(-a[1]);
paulo@89 4491 return ((-a[2] < 0 ? -angle : angle) + 2 * Math.PI - ε) % (2 * Math.PI);
paulo@89 4492 }
paulo@89 4493 d3.geo.distance = function(a, b) {
paulo@89 4494 var Δλ = (b[0] - a[0]) * d3_radians, φ0 = a[1] * d3_radians, φ1 = b[1] * d3_radians, sinΔλ = Math.sin(Δλ), cosΔλ = Math.cos(Δλ), sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), sinφ1 = Math.sin(φ1), cosφ1 = Math.cos(φ1), t;
paulo@89 4495 return Math.atan2(Math.sqrt((t = cosφ1 * sinΔλ) * t + (t = cosφ0 * sinφ1 - sinφ0 * cosφ1 * cosΔλ) * t), sinφ0 * sinφ1 + cosφ0 * cosφ1 * cosΔλ);
paulo@89 4496 };
paulo@89 4497 d3.geo.graticule = function() {
paulo@89 4498 var x1, x0, X1, X0, y1, y0, Y1, Y0, dx = 10, dy = dx, DX = 90, DY = 360, x, y, X, Y, precision = 2.5;
paulo@89 4499 function graticule() {
paulo@89 4500 return {
paulo@89 4501 type: "MultiLineString",
paulo@89 4502 coordinates: lines()
paulo@89 4503 };
paulo@89 4504 }
paulo@89 4505 function lines() {
paulo@89 4506 return d3.range(Math.ceil(X0 / DX) * DX, X1, DX).map(X).concat(d3.range(Math.ceil(Y0 / DY) * DY, Y1, DY).map(Y)).concat(d3.range(Math.ceil(x0 / dx) * dx, x1, dx).filter(function(x) {
paulo@89 4507 return abs(x % DX) > ε;
paulo@89 4508 }).map(x)).concat(d3.range(Math.ceil(y0 / dy) * dy, y1, dy).filter(function(y) {
paulo@89 4509 return abs(y % DY) > ε;
paulo@89 4510 }).map(y));
paulo@89 4511 }
paulo@89 4512 graticule.lines = function() {
paulo@89 4513 return lines().map(function(coordinates) {
paulo@89 4514 return {
paulo@89 4515 type: "LineString",
paulo@89 4516 coordinates: coordinates
paulo@89 4517 };
paulo@89 4518 });
paulo@89 4519 };
paulo@89 4520 graticule.outline = function() {
paulo@89 4521 return {
paulo@89 4522 type: "Polygon",
paulo@89 4523 coordinates: [ X(X0).concat(Y(Y1).slice(1), X(X1).reverse().slice(1), Y(Y0).reverse().slice(1)) ]
paulo@89 4524 };
paulo@89 4525 };
paulo@89 4526 graticule.extent = function(_) {
paulo@89 4527 if (!arguments.length) return graticule.minorExtent();
paulo@89 4528 return graticule.majorExtent(_).minorExtent(_);
paulo@89 4529 };
paulo@89 4530 graticule.majorExtent = function(_) {
paulo@89 4531 if (!arguments.length) return [ [ X0, Y0 ], [ X1, Y1 ] ];
paulo@89 4532 X0 = +_[0][0], X1 = +_[1][0];
paulo@89 4533 Y0 = +_[0][1], Y1 = +_[1][1];
paulo@89 4534 if (X0 > X1) _ = X0, X0 = X1, X1 = _;
paulo@89 4535 if (Y0 > Y1) _ = Y0, Y0 = Y1, Y1 = _;
paulo@89 4536 return graticule.precision(precision);
paulo@89 4537 };
paulo@89 4538 graticule.minorExtent = function(_) {
paulo@89 4539 if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ];
paulo@89 4540 x0 = +_[0][0], x1 = +_[1][0];
paulo@89 4541 y0 = +_[0][1], y1 = +_[1][1];
paulo@89 4542 if (x0 > x1) _ = x0, x0 = x1, x1 = _;
paulo@89 4543 if (y0 > y1) _ = y0, y0 = y1, y1 = _;
paulo@89 4544 return graticule.precision(precision);
paulo@89 4545 };
paulo@89 4546 graticule.step = function(_) {
paulo@89 4547 if (!arguments.length) return graticule.minorStep();
paulo@89 4548 return graticule.majorStep(_).minorStep(_);
paulo@89 4549 };
paulo@89 4550 graticule.majorStep = function(_) {
paulo@89 4551 if (!arguments.length) return [ DX, DY ];
paulo@89 4552 DX = +_[0], DY = +_[1];
paulo@89 4553 return graticule;
paulo@89 4554 };
paulo@89 4555 graticule.minorStep = function(_) {
paulo@89 4556 if (!arguments.length) return [ dx, dy ];
paulo@89 4557 dx = +_[0], dy = +_[1];
paulo@89 4558 return graticule;
paulo@89 4559 };
paulo@89 4560 graticule.precision = function(_) {
paulo@89 4561 if (!arguments.length) return precision;
paulo@89 4562 precision = +_;
paulo@89 4563 x = d3_geo_graticuleX(y0, y1, 90);
paulo@89 4564 y = d3_geo_graticuleY(x0, x1, precision);
paulo@89 4565 X = d3_geo_graticuleX(Y0, Y1, 90);
paulo@89 4566 Y = d3_geo_graticuleY(X0, X1, precision);
paulo@89 4567 return graticule;
paulo@89 4568 };
paulo@89 4569 return graticule.majorExtent([ [ -180, -90 + ε ], [ 180, 90 - ε ] ]).minorExtent([ [ -180, -80 - ε ], [ 180, 80 + ε ] ]);
paulo@89 4570 };
paulo@89 4571 function d3_geo_graticuleX(y0, y1, dy) {
paulo@89 4572 var y = d3.range(y0, y1 - ε, dy).concat(y1);
paulo@89 4573 return function(x) {
paulo@89 4574 return y.map(function(y) {
paulo@89 4575 return [ x, y ];
paulo@89 4576 });
paulo@89 4577 };
paulo@89 4578 }
paulo@89 4579 function d3_geo_graticuleY(x0, x1, dx) {
paulo@89 4580 var x = d3.range(x0, x1 - ε, dx).concat(x1);
paulo@89 4581 return function(y) {
paulo@89 4582 return x.map(function(x) {
paulo@89 4583 return [ x, y ];
paulo@89 4584 });
paulo@89 4585 };
paulo@89 4586 }
paulo@89 4587 function d3_source(d) {
paulo@89 4588 return d.source;
paulo@89 4589 }
paulo@89 4590 function d3_target(d) {
paulo@89 4591 return d.target;
paulo@89 4592 }
paulo@89 4593 d3.geo.greatArc = function() {
paulo@89 4594 var source = d3_source, source_, target = d3_target, target_;
paulo@89 4595 function greatArc() {
paulo@89 4596 return {
paulo@89 4597 type: "LineString",
paulo@89 4598 coordinates: [ source_ || source.apply(this, arguments), target_ || target.apply(this, arguments) ]
paulo@89 4599 };
paulo@89 4600 }
paulo@89 4601 greatArc.distance = function() {
paulo@89 4602 return d3.geo.distance(source_ || source.apply(this, arguments), target_ || target.apply(this, arguments));
paulo@89 4603 };
paulo@89 4604 greatArc.source = function(_) {
paulo@89 4605 if (!arguments.length) return source;
paulo@89 4606 source = _, source_ = typeof _ === "function" ? null : _;
paulo@89 4607 return greatArc;
paulo@89 4608 };
paulo@89 4609 greatArc.target = function(_) {
paulo@89 4610 if (!arguments.length) return target;
paulo@89 4611 target = _, target_ = typeof _ === "function" ? null : _;
paulo@89 4612 return greatArc;
paulo@89 4613 };
paulo@89 4614 greatArc.precision = function() {
paulo@89 4615 return arguments.length ? greatArc : 0;
paulo@89 4616 };
paulo@89 4617 return greatArc;
paulo@89 4618 };
paulo@89 4619 d3.geo.interpolate = function(source, target) {
paulo@89 4620 return d3_geo_interpolate(source[0] * d3_radians, source[1] * d3_radians, target[0] * d3_radians, target[1] * d3_radians);
paulo@89 4621 };
paulo@89 4622 function d3_geo_interpolate(x0, y0, x1, y1) {
paulo@89 4623 var cy0 = Math.cos(y0), sy0 = Math.sin(y0), cy1 = Math.cos(y1), sy1 = Math.sin(y1), kx0 = cy0 * Math.cos(x0), ky0 = cy0 * Math.sin(x0), kx1 = cy1 * Math.cos(x1), ky1 = cy1 * Math.sin(x1), d = 2 * Math.asin(Math.sqrt(d3_haversin(y1 - y0) + cy0 * cy1 * d3_haversin(x1 - x0))), k = 1 / Math.sin(d);
paulo@89 4624 var interpolate = d ? function(t) {
paulo@89 4625 var B = Math.sin(t *= d) * k, A = Math.sin(d - t) * k, x = A * kx0 + B * kx1, y = A * ky0 + B * ky1, z = A * sy0 + B * sy1;
paulo@89 4626 return [ Math.atan2(y, x) * d3_degrees, Math.atan2(z, Math.sqrt(x * x + y * y)) * d3_degrees ];
paulo@89 4627 } : function() {
paulo@89 4628 return [ x0 * d3_degrees, y0 * d3_degrees ];
paulo@89 4629 };
paulo@89 4630 interpolate.distance = d;
paulo@89 4631 return interpolate;
paulo@89 4632 }
paulo@89 4633 d3.geo.length = function(object) {
paulo@89 4634 d3_geo_lengthSum = 0;
paulo@89 4635 d3.geo.stream(object, d3_geo_length);
paulo@89 4636 return d3_geo_lengthSum;
paulo@89 4637 };
paulo@89 4638 var d3_geo_lengthSum;
paulo@89 4639 var d3_geo_length = {
paulo@89 4640 sphere: d3_noop,
paulo@89 4641 point: d3_noop,
paulo@89 4642 lineStart: d3_geo_lengthLineStart,
paulo@89 4643 lineEnd: d3_noop,
paulo@89 4644 polygonStart: d3_noop,
paulo@89 4645 polygonEnd: d3_noop
paulo@89 4646 };
paulo@89 4647 function d3_geo_lengthLineStart() {
paulo@89 4648 var λ0, sinφ0, cosφ0;
paulo@89 4649 d3_geo_length.point = function(λ, φ) {
paulo@89 4650 λ0 = λ * d3_radians, sinφ0 = Math.sin(φ *= d3_radians), cosφ0 = Math.cos(φ);
paulo@89 4651 d3_geo_length.point = nextPoint;
paulo@89 4652 };
paulo@89 4653 d3_geo_length.lineEnd = function() {
paulo@89 4654 d3_geo_length.point = d3_geo_length.lineEnd = d3_noop;
paulo@89 4655 };
paulo@89 4656 function nextPoint(λ, φ) {
paulo@89 4657 var sinφ = Math.sin(φ *= d3_radians), cosφ = Math.cos(φ), t = abs((λ *= d3_radians) - λ0), cosΔλ = Math.cos(t);
paulo@89 4658 d3_geo_lengthSum += Math.atan2(Math.sqrt((t = cosφ * Math.sin(t)) * t + (t = cosφ0 * sinφ - sinφ0 * cosφ * cosΔλ) * t), sinφ0 * sinφ + cosφ0 * cosφ * cosΔλ);
paulo@89 4659 λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ;
paulo@89 4660 }
paulo@89 4661 }
paulo@89 4662 function d3_geo_azimuthal(scale, angle) {
paulo@89 4663 function azimuthal(λ, φ) {
paulo@89 4664 var cosλ = Math.cos(λ), cosφ = Math.cos(φ), k = scale(cosλ * cosφ);
paulo@89 4665 return [ k * cosφ * Math.sin(λ), k * Math.sin(φ) ];
paulo@89 4666 }
paulo@89 4667 azimuthal.invert = function(x, y) {
paulo@89 4668 var ρ = Math.sqrt(x * x + y * y), c = angle(ρ), sinc = Math.sin(c), cosc = Math.cos(c);
paulo@89 4669 return [ Math.atan2(x * sinc, ρ * cosc), Math.asin(ρ && y * sinc / ρ) ];
paulo@89 4670 };
paulo@89 4671 return azimuthal;
paulo@89 4672 }
paulo@89 4673 var d3_geo_azimuthalEqualArea = d3_geo_azimuthal(function(cosλcosφ) {
paulo@89 4674 return Math.sqrt(2 / (1 + cosλcosφ));
paulo@89 4675 }, function(ρ) {
paulo@89 4676 return 2 * Math.asin(ρ / 2);
paulo@89 4677 });
paulo@89 4678 (d3.geo.azimuthalEqualArea = function() {
paulo@89 4679 return d3_geo_projection(d3_geo_azimuthalEqualArea);
paulo@89 4680 }).raw = d3_geo_azimuthalEqualArea;
paulo@89 4681 var d3_geo_azimuthalEquidistant = d3_geo_azimuthal(function(cosλcosφ) {
paulo@89 4682 var c = Math.acos(cosλcosφ);
paulo@89 4683 return c && c / Math.sin(c);
paulo@89 4684 }, d3_identity);
paulo@89 4685 (d3.geo.azimuthalEquidistant = function() {
paulo@89 4686 return d3_geo_projection(d3_geo_azimuthalEquidistant);
paulo@89 4687 }).raw = d3_geo_azimuthalEquidistant;
paulo@89 4688 function d3_geo_conicConformal(φ0, φ1) {
paulo@89 4689 var cosφ0 = Math.cos(φ0), t = function(φ) {
paulo@89 4690 return Math.tan(π / 4 + φ / 2);
paulo@89 4691 }, n = φ0 === φ1 ? Math.sin(φ0) : Math.log(cosφ0 / Math.cos(φ1)) / Math.log(t(φ1) / t(φ0)), F = cosφ0 * Math.pow(t(φ0), n) / n;
paulo@89 4692 if (!n) return d3_geo_mercator;
paulo@89 4693 function forward(λ, φ) {
paulo@89 4694 if (F > 0) {
paulo@89 4695 if (φ < -halfπ + ε) φ = -halfπ + ε;
paulo@89 4696 } else {
paulo@89 4697 if (φ > halfπ - ε) φ = halfπ - ε;
paulo@89 4698 }
paulo@89 4699 var ρ = F / Math.pow(t(φ), n);
paulo@89 4700 return [ ρ * Math.sin(n * λ), F - ρ * Math.cos(n * λ) ];
paulo@89 4701 }
paulo@89 4702 forward.invert = function(x, y) {
paulo@89 4703 var ρ0_y = F - y, ρ = d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y);
paulo@89 4704 return [ Math.atan2(x, ρ0_y) / n, 2 * Math.atan(Math.pow(F / ρ, 1 / n)) - halfπ ];
paulo@89 4705 };
paulo@89 4706 return forward;
paulo@89 4707 }
paulo@89 4708 (d3.geo.conicConformal = function() {
paulo@89 4709 return d3_geo_conic(d3_geo_conicConformal);
paulo@89 4710 }).raw = d3_geo_conicConformal;
paulo@89 4711 function d3_geo_conicEquidistant(φ0, φ1) {
paulo@89 4712 var cosφ0 = Math.cos(φ0), n = φ0 === φ1 ? Math.sin(φ0) : (cosφ0 - Math.cos(φ1)) / (φ1 - φ0), G = cosφ0 / n + φ0;
paulo@89 4713 if (abs(n) < ε) return d3_geo_equirectangular;
paulo@89 4714 function forward(λ, φ) {
paulo@89 4715 var ρ = G - φ;
paulo@89 4716 return [ ρ * Math.sin(n * λ), G - ρ * Math.cos(n * λ) ];
paulo@89 4717 }
paulo@89 4718 forward.invert = function(x, y) {
paulo@89 4719 var ρ0_y = G - y;
paulo@89 4720 return [ Math.atan2(x, ρ0_y) / n, G - d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y) ];
paulo@89 4721 };
paulo@89 4722 return forward;
paulo@89 4723 }
paulo@89 4724 (d3.geo.conicEquidistant = function() {
paulo@89 4725 return d3_geo_conic(d3_geo_conicEquidistant);
paulo@89 4726 }).raw = d3_geo_conicEquidistant;
paulo@89 4727 var d3_geo_gnomonic = d3_geo_azimuthal(function(cosλcosφ) {
paulo@89 4728 return 1 / cosλcosφ;
paulo@89 4729 }, Math.atan);
paulo@89 4730 (d3.geo.gnomonic = function() {
paulo@89 4731 return d3_geo_projection(d3_geo_gnomonic);
paulo@89 4732 }).raw = d3_geo_gnomonic;
paulo@89 4733 function d3_geo_mercator(λ, φ) {
paulo@89 4734 return [ λ, Math.log(Math.tan(π / 4 + φ / 2)) ];
paulo@89 4735 }
paulo@89 4736 d3_geo_mercator.invert = function(x, y) {
paulo@89 4737 return [ x, 2 * Math.atan(Math.exp(y)) - halfπ ];
paulo@89 4738 };
paulo@89 4739 function d3_geo_mercatorProjection(project) {
paulo@89 4740 var m = d3_geo_projection(project), scale = m.scale, translate = m.translate, clipExtent = m.clipExtent, clipAuto;
paulo@89 4741 m.scale = function() {
paulo@89 4742 var v = scale.apply(m, arguments);
paulo@89 4743 return v === m ? clipAuto ? m.clipExtent(null) : m : v;
paulo@89 4744 };
paulo@89 4745 m.translate = function() {
paulo@89 4746 var v = translate.apply(m, arguments);
paulo@89 4747 return v === m ? clipAuto ? m.clipExtent(null) : m : v;
paulo@89 4748 };
paulo@89 4749 m.clipExtent = function(_) {
paulo@89 4750 var v = clipExtent.apply(m, arguments);
paulo@89 4751 if (v === m) {
paulo@89 4752 if (clipAuto = _ == null) {
paulo@89 4753 var k = π * scale(), t = translate();
paulo@89 4754 clipExtent([ [ t[0] - k, t[1] - k ], [ t[0] + k, t[1] + k ] ]);
paulo@89 4755 }
paulo@89 4756 } else if (clipAuto) {
paulo@89 4757 v = null;
paulo@89 4758 }
paulo@89 4759 return v;
paulo@89 4760 };
paulo@89 4761 return m.clipExtent(null);
paulo@89 4762 }
paulo@89 4763 (d3.geo.mercator = function() {
paulo@89 4764 return d3_geo_mercatorProjection(d3_geo_mercator);
paulo@89 4765 }).raw = d3_geo_mercator;
paulo@89 4766 var d3_geo_orthographic = d3_geo_azimuthal(function() {
paulo@89 4767 return 1;
paulo@89 4768 }, Math.asin);
paulo@89 4769 (d3.geo.orthographic = function() {
paulo@89 4770 return d3_geo_projection(d3_geo_orthographic);
paulo@89 4771 }).raw = d3_geo_orthographic;
paulo@89 4772 var d3_geo_stereographic = d3_geo_azimuthal(function(cosλcosφ) {
paulo@89 4773 return 1 / (1 + cosλcosφ);
paulo@89 4774 }, function(ρ) {
paulo@89 4775 return 2 * Math.atan(ρ);
paulo@89 4776 });
paulo@89 4777 (d3.geo.stereographic = function() {
paulo@89 4778 return d3_geo_projection(d3_geo_stereographic);
paulo@89 4779 }).raw = d3_geo_stereographic;
paulo@89 4780 function d3_geo_transverseMercator(λ, φ) {
paulo@89 4781 return [ Math.log(Math.tan(π / 4 + φ / 2)), -λ ];
paulo@89 4782 }
paulo@89 4783 d3_geo_transverseMercator.invert = function(x, y) {
paulo@89 4784 return [ -y, 2 * Math.atan(Math.exp(x)) - halfπ ];
paulo@89 4785 };
paulo@89 4786 (d3.geo.transverseMercator = function() {
paulo@89 4787 var projection = d3_geo_mercatorProjection(d3_geo_transverseMercator), center = projection.center, rotate = projection.rotate;
paulo@89 4788 projection.center = function(_) {
paulo@89 4789 return _ ? center([ -_[1], _[0] ]) : (_ = center(), [ _[1], -_[0] ]);
paulo@89 4790 };
paulo@89 4791 projection.rotate = function(_) {
paulo@89 4792 return _ ? rotate([ _[0], _[1], _.length > 2 ? _[2] + 90 : 90 ]) : (_ = rotate(),
paulo@89 4793 [ _[0], _[1], _[2] - 90 ]);
paulo@89 4794 };
paulo@89 4795 return rotate([ 0, 0, 90 ]);
paulo@89 4796 }).raw = d3_geo_transverseMercator;
paulo@89 4797 d3.geom = {};
paulo@89 4798 function d3_geom_pointX(d) {
paulo@89 4799 return d[0];
paulo@89 4800 }
paulo@89 4801 function d3_geom_pointY(d) {
paulo@89 4802 return d[1];
paulo@89 4803 }
paulo@89 4804 d3.geom.hull = function(vertices) {
paulo@89 4805 var x = d3_geom_pointX, y = d3_geom_pointY;
paulo@89 4806 if (arguments.length) return hull(vertices);
paulo@89 4807 function hull(data) {
paulo@89 4808 if (data.length < 3) return [];
paulo@89 4809 var fx = d3_functor(x), fy = d3_functor(y), i, n = data.length, points = [], flippedPoints = [];
paulo@89 4810 for (i = 0; i < n; i++) {
paulo@89 4811 points.push([ +fx.call(this, data[i], i), +fy.call(this, data[i], i), i ]);
paulo@89 4812 }
paulo@89 4813 points.sort(d3_geom_hullOrder);
paulo@89 4814 for (i = 0; i < n; i++) flippedPoints.push([ points[i][0], -points[i][1] ]);
paulo@89 4815 var upper = d3_geom_hullUpper(points), lower = d3_geom_hullUpper(flippedPoints);
paulo@89 4816 var skipLeft = lower[0] === upper[0], skipRight = lower[lower.length - 1] === upper[upper.length - 1], polygon = [];
paulo@89 4817 for (i = upper.length - 1; i >= 0; --i) polygon.push(data[points[upper[i]][2]]);
paulo@89 4818 for (i = +skipLeft; i < lower.length - skipRight; ++i) polygon.push(data[points[lower[i]][2]]);
paulo@89 4819 return polygon;
paulo@89 4820 }
paulo@89 4821 hull.x = function(_) {
paulo@89 4822 return arguments.length ? (x = _, hull) : x;
paulo@89 4823 };
paulo@89 4824 hull.y = function(_) {
paulo@89 4825 return arguments.length ? (y = _, hull) : y;
paulo@89 4826 };
paulo@89 4827 return hull;
paulo@89 4828 };
paulo@89 4829 function d3_geom_hullUpper(points) {
paulo@89 4830 var n = points.length, hull = [ 0, 1 ], hs = 2;
paulo@89 4831 for (var i = 2; i < n; i++) {
paulo@89 4832 while (hs > 1 && d3_cross2d(points[hull[hs - 2]], points[hull[hs - 1]], points[i]) <= 0) --hs;
paulo@89 4833 hull[hs++] = i;
paulo@89 4834 }
paulo@89 4835 return hull.slice(0, hs);
paulo@89 4836 }
paulo@89 4837 function d3_geom_hullOrder(a, b) {
paulo@89 4838 return a[0] - b[0] || a[1] - b[1];
paulo@89 4839 }
paulo@89 4840 d3.geom.polygon = function(coordinates) {
paulo@89 4841 d3_subclass(coordinates, d3_geom_polygonPrototype);
paulo@89 4842 return coordinates;
paulo@89 4843 };
paulo@89 4844 var d3_geom_polygonPrototype = d3.geom.polygon.prototype = [];
paulo@89 4845 d3_geom_polygonPrototype.area = function() {
paulo@89 4846 var i = -1, n = this.length, a, b = this[n - 1], area = 0;
paulo@89 4847 while (++i < n) {
paulo@89 4848 a = b;
paulo@89 4849 b = this[i];
paulo@89 4850 area += a[1] * b[0] - a[0] * b[1];
paulo@89 4851 }
paulo@89 4852 return area * .5;
paulo@89 4853 };
paulo@89 4854 d3_geom_polygonPrototype.centroid = function(k) {
paulo@89 4855 var i = -1, n = this.length, x = 0, y = 0, a, b = this[n - 1], c;
paulo@89 4856 if (!arguments.length) k = -1 / (6 * this.area());
paulo@89 4857 while (++i < n) {
paulo@89 4858 a = b;
paulo@89 4859 b = this[i];
paulo@89 4860 c = a[0] * b[1] - b[0] * a[1];
paulo@89 4861 x += (a[0] + b[0]) * c;
paulo@89 4862 y += (a[1] + b[1]) * c;
paulo@89 4863 }
paulo@89 4864 return [ x * k, y * k ];
paulo@89 4865 };
paulo@89 4866 d3_geom_polygonPrototype.clip = function(subject) {
paulo@89 4867 var input, closed = d3_geom_polygonClosed(subject), i = -1, n = this.length - d3_geom_polygonClosed(this), j, m, a = this[n - 1], b, c, d;
paulo@89 4868 while (++i < n) {
paulo@89 4869 input = subject.slice();
paulo@89 4870 subject.length = 0;
paulo@89 4871 b = this[i];
paulo@89 4872 c = input[(m = input.length - closed) - 1];
paulo@89 4873 j = -1;
paulo@89 4874 while (++j < m) {
paulo@89 4875 d = input[j];
paulo@89 4876 if (d3_geom_polygonInside(d, a, b)) {
paulo@89 4877 if (!d3_geom_polygonInside(c, a, b)) {
paulo@89 4878 subject.push(d3_geom_polygonIntersect(c, d, a, b));
paulo@89 4879 }
paulo@89 4880 subject.push(d);
paulo@89 4881 } else if (d3_geom_polygonInside(c, a, b)) {
paulo@89 4882 subject.push(d3_geom_polygonIntersect(c, d, a, b));
paulo@89 4883 }
paulo@89 4884 c = d;
paulo@89 4885 }
paulo@89 4886 if (closed) subject.push(subject[0]);
paulo@89 4887 a = b;
paulo@89 4888 }
paulo@89 4889 return subject;
paulo@89 4890 };
paulo@89 4891 function d3_geom_polygonInside(p, a, b) {
paulo@89 4892 return (b[0] - a[0]) * (p[1] - a[1]) < (b[1] - a[1]) * (p[0] - a[0]);
paulo@89 4893 }
paulo@89 4894 function d3_geom_polygonIntersect(c, d, a, b) {
paulo@89 4895 var x1 = c[0], x3 = a[0], x21 = d[0] - x1, x43 = b[0] - x3, y1 = c[1], y3 = a[1], y21 = d[1] - y1, y43 = b[1] - y3, ua = (x43 * (y1 - y3) - y43 * (x1 - x3)) / (y43 * x21 - x43 * y21);
paulo@89 4896 return [ x1 + ua * x21, y1 + ua * y21 ];
paulo@89 4897 }
paulo@89 4898 function d3_geom_polygonClosed(coordinates) {
paulo@89 4899 var a = coordinates[0], b = coordinates[coordinates.length - 1];
paulo@89 4900 return !(a[0] - b[0] || a[1] - b[1]);
paulo@89 4901 }
paulo@89 4902 var d3_geom_voronoiEdges, d3_geom_voronoiCells, d3_geom_voronoiBeaches, d3_geom_voronoiBeachPool = [], d3_geom_voronoiFirstCircle, d3_geom_voronoiCircles, d3_geom_voronoiCirclePool = [];
paulo@89 4903 function d3_geom_voronoiBeach() {
paulo@89 4904 d3_geom_voronoiRedBlackNode(this);
paulo@89 4905 this.edge = this.site = this.circle = null;
paulo@89 4906 }
paulo@89 4907 function d3_geom_voronoiCreateBeach(site) {
paulo@89 4908 var beach = d3_geom_voronoiBeachPool.pop() || new d3_geom_voronoiBeach();
paulo@89 4909 beach.site = site;
paulo@89 4910 return beach;
paulo@89 4911 }
paulo@89 4912 function d3_geom_voronoiDetachBeach(beach) {
paulo@89 4913 d3_geom_voronoiDetachCircle(beach);
paulo@89 4914 d3_geom_voronoiBeaches.remove(beach);
paulo@89 4915 d3_geom_voronoiBeachPool.push(beach);
paulo@89 4916 d3_geom_voronoiRedBlackNode(beach);
paulo@89 4917 }
paulo@89 4918 function d3_geom_voronoiRemoveBeach(beach) {
paulo@89 4919 var circle = beach.circle, x = circle.x, y = circle.cy, vertex = {
paulo@89 4920 x: x,
paulo@89 4921 y: y
paulo@89 4922 }, previous = beach.P, next = beach.N, disappearing = [ beach ];
paulo@89 4923 d3_geom_voronoiDetachBeach(beach);
paulo@89 4924 var lArc = previous;
paulo@89 4925 while (lArc.circle && abs(x - lArc.circle.x) < ε && abs(y - lArc.circle.cy) < ε) {
paulo@89 4926 previous = lArc.P;
paulo@89 4927 disappearing.unshift(lArc);
paulo@89 4928 d3_geom_voronoiDetachBeach(lArc);
paulo@89 4929 lArc = previous;
paulo@89 4930 }
paulo@89 4931 disappearing.unshift(lArc);
paulo@89 4932 d3_geom_voronoiDetachCircle(lArc);
paulo@89 4933 var rArc = next;
paulo@89 4934 while (rArc.circle && abs(x - rArc.circle.x) < ε && abs(y - rArc.circle.cy) < ε) {
paulo@89 4935 next = rArc.N;
paulo@89 4936 disappearing.push(rArc);
paulo@89 4937 d3_geom_voronoiDetachBeach(rArc);
paulo@89 4938 rArc = next;
paulo@89 4939 }
paulo@89 4940 disappearing.push(rArc);
paulo@89 4941 d3_geom_voronoiDetachCircle(rArc);
paulo@89 4942 var nArcs = disappearing.length, iArc;
paulo@89 4943 for (iArc = 1; iArc < nArcs; ++iArc) {
paulo@89 4944 rArc = disappearing[iArc];
paulo@89 4945 lArc = disappearing[iArc - 1];
paulo@89 4946 d3_geom_voronoiSetEdgeEnd(rArc.edge, lArc.site, rArc.site, vertex);
paulo@89 4947 }
paulo@89 4948 lArc = disappearing[0];
paulo@89 4949 rArc = disappearing[nArcs - 1];
paulo@89 4950 rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, rArc.site, null, vertex);
paulo@89 4951 d3_geom_voronoiAttachCircle(lArc);
paulo@89 4952 d3_geom_voronoiAttachCircle(rArc);
paulo@89 4953 }
paulo@89 4954 function d3_geom_voronoiAddBeach(site) {
paulo@89 4955 var x = site.x, directrix = site.y, lArc, rArc, dxl, dxr, node = d3_geom_voronoiBeaches._;
paulo@89 4956 while (node) {
paulo@89 4957 dxl = d3_geom_voronoiLeftBreakPoint(node, directrix) - x;
paulo@89 4958 if (dxl > ε) node = node.L; else {
paulo@89 4959 dxr = x - d3_geom_voronoiRightBreakPoint(node, directrix);
paulo@89 4960 if (dxr > ε) {
paulo@89 4961 if (!node.R) {
paulo@89 4962 lArc = node;
paulo@89 4963 break;
paulo@89 4964 }
paulo@89 4965 node = node.R;
paulo@89 4966 } else {
paulo@89 4967 if (dxl > -ε) {
paulo@89 4968 lArc = node.P;
paulo@89 4969 rArc = node;
paulo@89 4970 } else if (dxr > -ε) {
paulo@89 4971 lArc = node;
paulo@89 4972 rArc = node.N;
paulo@89 4973 } else {
paulo@89 4974 lArc = rArc = node;
paulo@89 4975 }
paulo@89 4976 break;
paulo@89 4977 }
paulo@89 4978 }
paulo@89 4979 }
paulo@89 4980 var newArc = d3_geom_voronoiCreateBeach(site);
paulo@89 4981 d3_geom_voronoiBeaches.insert(lArc, newArc);
paulo@89 4982 if (!lArc && !rArc) return;
paulo@89 4983 if (lArc === rArc) {
paulo@89 4984 d3_geom_voronoiDetachCircle(lArc);
paulo@89 4985 rArc = d3_geom_voronoiCreateBeach(lArc.site);
paulo@89 4986 d3_geom_voronoiBeaches.insert(newArc, rArc);
paulo@89 4987 newArc.edge = rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site);
paulo@89 4988 d3_geom_voronoiAttachCircle(lArc);
paulo@89 4989 d3_geom_voronoiAttachCircle(rArc);
paulo@89 4990 return;
paulo@89 4991 }
paulo@89 4992 if (!rArc) {
paulo@89 4993 newArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site);
paulo@89 4994 return;
paulo@89 4995 }
paulo@89 4996 d3_geom_voronoiDetachCircle(lArc);
paulo@89 4997 d3_geom_voronoiDetachCircle(rArc);
paulo@89 4998 var lSite = lArc.site, ax = lSite.x, ay = lSite.y, bx = site.x - ax, by = site.y - ay, rSite = rArc.site, cx = rSite.x - ax, cy = rSite.y - ay, d = 2 * (bx * cy - by * cx), hb = bx * bx + by * by, hc = cx * cx + cy * cy, vertex = {
paulo@89 4999 x: (cy * hb - by * hc) / d + ax,
paulo@89 5000 y: (bx * hc - cx * hb) / d + ay
paulo@89 5001 };
paulo@89 5002 d3_geom_voronoiSetEdgeEnd(rArc.edge, lSite, rSite, vertex);
paulo@89 5003 newArc.edge = d3_geom_voronoiCreateEdge(lSite, site, null, vertex);
paulo@89 5004 rArc.edge = d3_geom_voronoiCreateEdge(site, rSite, null, vertex);
paulo@89 5005 d3_geom_voronoiAttachCircle(lArc);
paulo@89 5006 d3_geom_voronoiAttachCircle(rArc);
paulo@89 5007 }
paulo@89 5008 function d3_geom_voronoiLeftBreakPoint(arc, directrix) {
paulo@89 5009 var site = arc.site, rfocx = site.x, rfocy = site.y, pby2 = rfocy - directrix;
paulo@89 5010 if (!pby2) return rfocx;
paulo@89 5011 var lArc = arc.P;
paulo@89 5012 if (!lArc) return -Infinity;
paulo@89 5013 site = lArc.site;
paulo@89 5014 var lfocx = site.x, lfocy = site.y, plby2 = lfocy - directrix;
paulo@89 5015 if (!plby2) return lfocx;
paulo@89 5016 var hl = lfocx - rfocx, aby2 = 1 / pby2 - 1 / plby2, b = hl / plby2;
paulo@89 5017 if (aby2) return (-b + Math.sqrt(b * b - 2 * aby2 * (hl * hl / (-2 * plby2) - lfocy + plby2 / 2 + rfocy - pby2 / 2))) / aby2 + rfocx;
paulo@89 5018 return (rfocx + lfocx) / 2;
paulo@89 5019 }
paulo@89 5020 function d3_geom_voronoiRightBreakPoint(arc, directrix) {
paulo@89 5021 var rArc = arc.N;
paulo@89 5022 if (rArc) return d3_geom_voronoiLeftBreakPoint(rArc, directrix);
paulo@89 5023 var site = arc.site;
paulo@89 5024 return site.y === directrix ? site.x : Infinity;
paulo@89 5025 }
paulo@89 5026 function d3_geom_voronoiCell(site) {
paulo@89 5027 this.site = site;
paulo@89 5028 this.edges = [];
paulo@89 5029 }
paulo@89 5030 d3_geom_voronoiCell.prototype.prepare = function() {
paulo@89 5031 var halfEdges = this.edges, iHalfEdge = halfEdges.length, edge;
paulo@89 5032 while (iHalfEdge--) {
paulo@89 5033 edge = halfEdges[iHalfEdge].edge;
paulo@89 5034 if (!edge.b || !edge.a) halfEdges.splice(iHalfEdge, 1);
paulo@89 5035 }
paulo@89 5036 halfEdges.sort(d3_geom_voronoiHalfEdgeOrder);
paulo@89 5037 return halfEdges.length;
paulo@89 5038 };
paulo@89 5039 function d3_geom_voronoiCloseCells(extent) {
paulo@89 5040 var x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], x2, y2, x3, y3, cells = d3_geom_voronoiCells, iCell = cells.length, cell, iHalfEdge, halfEdges, nHalfEdges, start, end;
paulo@89 5041 while (iCell--) {
paulo@89 5042 cell = cells[iCell];
paulo@89 5043 if (!cell || !cell.prepare()) continue;
paulo@89 5044 halfEdges = cell.edges;
paulo@89 5045 nHalfEdges = halfEdges.length;
paulo@89 5046 iHalfEdge = 0;
paulo@89 5047 while (iHalfEdge < nHalfEdges) {
paulo@89 5048 end = halfEdges[iHalfEdge].end(), x3 = end.x, y3 = end.y;
paulo@89 5049 start = halfEdges[++iHalfEdge % nHalfEdges].start(), x2 = start.x, y2 = start.y;
paulo@89 5050 if (abs(x3 - x2) > ε || abs(y3 - y2) > ε) {
paulo@89 5051 halfEdges.splice(iHalfEdge, 0, new d3_geom_voronoiHalfEdge(d3_geom_voronoiCreateBorderEdge(cell.site, end, abs(x3 - x0) < ε && y1 - y3 > ε ? {
paulo@89 5052 x: x0,
paulo@89 5053 y: abs(x2 - x0) < ε ? y2 : y1
paulo@89 5054 } : abs(y3 - y1) < ε && x1 - x3 > ε ? {
paulo@89 5055 x: abs(y2 - y1) < ε ? x2 : x1,
paulo@89 5056 y: y1
paulo@89 5057 } : abs(x3 - x1) < ε && y3 - y0 > ε ? {
paulo@89 5058 x: x1,
paulo@89 5059 y: abs(x2 - x1) < ε ? y2 : y0
paulo@89 5060 } : abs(y3 - y0) < ε && x3 - x0 > ε ? {
paulo@89 5061 x: abs(y2 - y0) < ε ? x2 : x0,
paulo@89 5062 y: y0
paulo@89 5063 } : null), cell.site, null));
paulo@89 5064 ++nHalfEdges;
paulo@89 5065 }
paulo@89 5066 }
paulo@89 5067 }
paulo@89 5068 }
paulo@89 5069 function d3_geom_voronoiHalfEdgeOrder(a, b) {
paulo@89 5070 return b.angle - a.angle;
paulo@89 5071 }
paulo@89 5072 function d3_geom_voronoiCircle() {
paulo@89 5073 d3_geom_voronoiRedBlackNode(this);
paulo@89 5074 this.x = this.y = this.arc = this.site = this.cy = null;
paulo@89 5075 }
paulo@89 5076 function d3_geom_voronoiAttachCircle(arc) {
paulo@89 5077 var lArc = arc.P, rArc = arc.N;
paulo@89 5078 if (!lArc || !rArc) return;
paulo@89 5079 var lSite = lArc.site, cSite = arc.site, rSite = rArc.site;
paulo@89 5080 if (lSite === rSite) return;
paulo@89 5081 var bx = cSite.x, by = cSite.y, ax = lSite.x - bx, ay = lSite.y - by, cx = rSite.x - bx, cy = rSite.y - by;
paulo@89 5082 var d = 2 * (ax * cy - ay * cx);
paulo@89 5083 if (d >= -ε2) return;
paulo@89 5084 var ha = ax * ax + ay * ay, hc = cx * cx + cy * cy, x = (cy * ha - ay * hc) / d, y = (ax * hc - cx * ha) / d, cy = y + by;
paulo@89 5085 var circle = d3_geom_voronoiCirclePool.pop() || new d3_geom_voronoiCircle();
paulo@89 5086 circle.arc = arc;
paulo@89 5087 circle.site = cSite;
paulo@89 5088 circle.x = x + bx;
paulo@89 5089 circle.y = cy + Math.sqrt(x * x + y * y);
paulo@89 5090 circle.cy = cy;
paulo@89 5091 arc.circle = circle;
paulo@89 5092 var before = null, node = d3_geom_voronoiCircles._;
paulo@89 5093 while (node) {
paulo@89 5094 if (circle.y < node.y || circle.y === node.y && circle.x <= node.x) {
paulo@89 5095 if (node.L) node = node.L; else {
paulo@89 5096 before = node.P;
paulo@89 5097 break;
paulo@89 5098 }
paulo@89 5099 } else {
paulo@89 5100 if (node.R) node = node.R; else {
paulo@89 5101 before = node;
paulo@89 5102 break;
paulo@89 5103 }
paulo@89 5104 }
paulo@89 5105 }
paulo@89 5106 d3_geom_voronoiCircles.insert(before, circle);
paulo@89 5107 if (!before) d3_geom_voronoiFirstCircle = circle;
paulo@89 5108 }
paulo@89 5109 function d3_geom_voronoiDetachCircle(arc) {
paulo@89 5110 var circle = arc.circle;
paulo@89 5111 if (circle) {
paulo@89 5112 if (!circle.P) d3_geom_voronoiFirstCircle = circle.N;
paulo@89 5113 d3_geom_voronoiCircles.remove(circle);
paulo@89 5114 d3_geom_voronoiCirclePool.push(circle);
paulo@89 5115 d3_geom_voronoiRedBlackNode(circle);
paulo@89 5116 arc.circle = null;
paulo@89 5117 }
paulo@89 5118 }
paulo@89 5119 function d3_geom_voronoiClipEdges(extent) {
paulo@89 5120 var edges = d3_geom_voronoiEdges, clip = d3_geom_clipLine(extent[0][0], extent[0][1], extent[1][0], extent[1][1]), i = edges.length, e;
paulo@89 5121 while (i--) {
paulo@89 5122 e = edges[i];
paulo@89 5123 if (!d3_geom_voronoiConnectEdge(e, extent) || !clip(e) || abs(e.a.x - e.b.x) < ε && abs(e.a.y - e.b.y) < ε) {
paulo@89 5124 e.a = e.b = null;
paulo@89 5125 edges.splice(i, 1);
paulo@89 5126 }
paulo@89 5127 }
paulo@89 5128 }
paulo@89 5129 function d3_geom_voronoiConnectEdge(edge, extent) {
paulo@89 5130 var vb = edge.b;
paulo@89 5131 if (vb) return true;
paulo@89 5132 var va = edge.a, x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], lSite = edge.l, rSite = edge.r, lx = lSite.x, ly = lSite.y, rx = rSite.x, ry = rSite.y, fx = (lx + rx) / 2, fy = (ly + ry) / 2, fm, fb;
paulo@89 5133 if (ry === ly) {
paulo@89 5134 if (fx < x0 || fx >= x1) return;
paulo@89 5135 if (lx > rx) {
paulo@89 5136 if (!va) va = {
paulo@89 5137 x: fx,
paulo@89 5138 y: y0
paulo@89 5139 }; else if (va.y >= y1) return;
paulo@89 5140 vb = {
paulo@89 5141 x: fx,
paulo@89 5142 y: y1
paulo@89 5143 };
paulo@89 5144 } else {
paulo@89 5145 if (!va) va = {
paulo@89 5146 x: fx,
paulo@89 5147 y: y1
paulo@89 5148 }; else if (va.y < y0) return;
paulo@89 5149 vb = {
paulo@89 5150 x: fx,
paulo@89 5151 y: y0
paulo@89 5152 };
paulo@89 5153 }
paulo@89 5154 } else {
paulo@89 5155 fm = (lx - rx) / (ry - ly);
paulo@89 5156 fb = fy - fm * fx;
paulo@89 5157 if (fm < -1 || fm > 1) {
paulo@89 5158 if (lx > rx) {
paulo@89 5159 if (!va) va = {
paulo@89 5160 x: (y0 - fb) / fm,
paulo@89 5161 y: y0
paulo@89 5162 }; else if (va.y >= y1) return;
paulo@89 5163 vb = {
paulo@89 5164 x: (y1 - fb) / fm,
paulo@89 5165 y: y1
paulo@89 5166 };
paulo@89 5167 } else {
paulo@89 5168 if (!va) va = {
paulo@89 5169 x: (y1 - fb) / fm,
paulo@89 5170 y: y1
paulo@89 5171 }; else if (va.y < y0) return;
paulo@89 5172 vb = {
paulo@89 5173 x: (y0 - fb) / fm,
paulo@89 5174 y: y0
paulo@89 5175 };
paulo@89 5176 }
paulo@89 5177 } else {
paulo@89 5178 if (ly < ry) {
paulo@89 5179 if (!va) va = {
paulo@89 5180 x: x0,
paulo@89 5181 y: fm * x0 + fb
paulo@89 5182 }; else if (va.x >= x1) return;
paulo@89 5183 vb = {
paulo@89 5184 x: x1,
paulo@89 5185 y: fm * x1 + fb
paulo@89 5186 };
paulo@89 5187 } else {
paulo@89 5188 if (!va) va = {
paulo@89 5189 x: x1,
paulo@89 5190 y: fm * x1 + fb
paulo@89 5191 }; else if (va.x < x0) return;
paulo@89 5192 vb = {
paulo@89 5193 x: x0,
paulo@89 5194 y: fm * x0 + fb
paulo@89 5195 };
paulo@89 5196 }
paulo@89 5197 }
paulo@89 5198 }
paulo@89 5199 edge.a = va;
paulo@89 5200 edge.b = vb;
paulo@89 5201 return true;
paulo@89 5202 }
paulo@89 5203 function d3_geom_voronoiEdge(lSite, rSite) {
paulo@89 5204 this.l = lSite;
paulo@89 5205 this.r = rSite;
paulo@89 5206 this.a = this.b = null;
paulo@89 5207 }
paulo@89 5208 function d3_geom_voronoiCreateEdge(lSite, rSite, va, vb) {
paulo@89 5209 var edge = new d3_geom_voronoiEdge(lSite, rSite);
paulo@89 5210 d3_geom_voronoiEdges.push(edge);
paulo@89 5211 if (va) d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, va);
paulo@89 5212 if (vb) d3_geom_voronoiSetEdgeEnd(edge, rSite, lSite, vb);
paulo@89 5213 d3_geom_voronoiCells[lSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, lSite, rSite));
paulo@89 5214 d3_geom_voronoiCells[rSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, rSite, lSite));
paulo@89 5215 return edge;
paulo@89 5216 }
paulo@89 5217 function d3_geom_voronoiCreateBorderEdge(lSite, va, vb) {
paulo@89 5218 var edge = new d3_geom_voronoiEdge(lSite, null);
paulo@89 5219 edge.a = va;
paulo@89 5220 edge.b = vb;
paulo@89 5221 d3_geom_voronoiEdges.push(edge);
paulo@89 5222 return edge;
paulo@89 5223 }
paulo@89 5224 function d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, vertex) {
paulo@89 5225 if (!edge.a && !edge.b) {
paulo@89 5226 edge.a = vertex;
paulo@89 5227 edge.l = lSite;
paulo@89 5228 edge.r = rSite;
paulo@89 5229 } else if (edge.l === rSite) {
paulo@89 5230 edge.b = vertex;
paulo@89 5231 } else {
paulo@89 5232 edge.a = vertex;
paulo@89 5233 }
paulo@89 5234 }
paulo@89 5235 function d3_geom_voronoiHalfEdge(edge, lSite, rSite) {
paulo@89 5236 var va = edge.a, vb = edge.b;
paulo@89 5237 this.edge = edge;
paulo@89 5238 this.site = lSite;
paulo@89 5239 this.angle = rSite ? Math.atan2(rSite.y - lSite.y, rSite.x - lSite.x) : edge.l === lSite ? Math.atan2(vb.x - va.x, va.y - vb.y) : Math.atan2(va.x - vb.x, vb.y - va.y);
paulo@89 5240 }
paulo@89 5241 d3_geom_voronoiHalfEdge.prototype = {
paulo@89 5242 start: function() {
paulo@89 5243 return this.edge.l === this.site ? this.edge.a : this.edge.b;
paulo@89 5244 },
paulo@89 5245 end: function() {
paulo@89 5246 return this.edge.l === this.site ? this.edge.b : this.edge.a;
paulo@89 5247 }
paulo@89 5248 };
paulo@89 5249 function d3_geom_voronoiRedBlackTree() {
paulo@89 5250 this._ = null;
paulo@89 5251 }
paulo@89 5252 function d3_geom_voronoiRedBlackNode(node) {
paulo@89 5253 node.U = node.C = node.L = node.R = node.P = node.N = null;
paulo@89 5254 }
paulo@89 5255 d3_geom_voronoiRedBlackTree.prototype = {
paulo@89 5256 insert: function(after, node) {
paulo@89 5257 var parent, grandpa, uncle;
paulo@89 5258 if (after) {
paulo@89 5259 node.P = after;
paulo@89 5260 node.N = after.N;
paulo@89 5261 if (after.N) after.N.P = node;
paulo@89 5262 after.N = node;
paulo@89 5263 if (after.R) {
paulo@89 5264 after = after.R;
paulo@89 5265 while (after.L) after = after.L;
paulo@89 5266 after.L = node;
paulo@89 5267 } else {
paulo@89 5268 after.R = node;
paulo@89 5269 }
paulo@89 5270 parent = after;
paulo@89 5271 } else if (this._) {
paulo@89 5272 after = d3_geom_voronoiRedBlackFirst(this._);
paulo@89 5273 node.P = null;
paulo@89 5274 node.N = after;
paulo@89 5275 after.P = after.L = node;
paulo@89 5276 parent = after;
paulo@89 5277 } else {
paulo@89 5278 node.P = node.N = null;
paulo@89 5279 this._ = node;
paulo@89 5280 parent = null;
paulo@89 5281 }
paulo@89 5282 node.L = node.R = null;
paulo@89 5283 node.U = parent;
paulo@89 5284 node.C = true;
paulo@89 5285 after = node;
paulo@89 5286 while (parent && parent.C) {
paulo@89 5287 grandpa = parent.U;
paulo@89 5288 if (parent === grandpa.L) {
paulo@89 5289 uncle = grandpa.R;
paulo@89 5290 if (uncle && uncle.C) {
paulo@89 5291 parent.C = uncle.C = false;
paulo@89 5292 grandpa.C = true;
paulo@89 5293 after = grandpa;
paulo@89 5294 } else {
paulo@89 5295 if (after === parent.R) {
paulo@89 5296 d3_geom_voronoiRedBlackRotateLeft(this, parent);
paulo@89 5297 after = parent;
paulo@89 5298 parent = after.U;
paulo@89 5299 }
paulo@89 5300 parent.C = false;
paulo@89 5301 grandpa.C = true;
paulo@89 5302 d3_geom_voronoiRedBlackRotateRight(this, grandpa);
paulo@89 5303 }
paulo@89 5304 } else {
paulo@89 5305 uncle = grandpa.L;
paulo@89 5306 if (uncle && uncle.C) {
paulo@89 5307 parent.C = uncle.C = false;
paulo@89 5308 grandpa.C = true;
paulo@89 5309 after = grandpa;
paulo@89 5310 } else {
paulo@89 5311 if (after === parent.L) {
paulo@89 5312 d3_geom_voronoiRedBlackRotateRight(this, parent);
paulo@89 5313 after = parent;
paulo@89 5314 parent = after.U;
paulo@89 5315 }
paulo@89 5316 parent.C = false;
paulo@89 5317 grandpa.C = true;
paulo@89 5318 d3_geom_voronoiRedBlackRotateLeft(this, grandpa);
paulo@89 5319 }
paulo@89 5320 }
paulo@89 5321 parent = after.U;
paulo@89 5322 }
paulo@89 5323 this._.C = false;
paulo@89 5324 },
paulo@89 5325 remove: function(node) {
paulo@89 5326 if (node.N) node.N.P = node.P;
paulo@89 5327 if (node.P) node.P.N = node.N;
paulo@89 5328 node.N = node.P = null;
paulo@89 5329 var parent = node.U, sibling, left = node.L, right = node.R, next, red;
paulo@89 5330 if (!left) next = right; else if (!right) next = left; else next = d3_geom_voronoiRedBlackFirst(right);
paulo@89 5331 if (parent) {
paulo@89 5332 if (parent.L === node) parent.L = next; else parent.R = next;
paulo@89 5333 } else {
paulo@89 5334 this._ = next;
paulo@89 5335 }
paulo@89 5336 if (left && right) {
paulo@89 5337 red = next.C;
paulo@89 5338 next.C = node.C;
paulo@89 5339 next.L = left;
paulo@89 5340 left.U = next;
paulo@89 5341 if (next !== right) {
paulo@89 5342 parent = next.U;
paulo@89 5343 next.U = node.U;
paulo@89 5344 node = next.R;
paulo@89 5345 parent.L = node;
paulo@89 5346 next.R = right;
paulo@89 5347 right.U = next;
paulo@89 5348 } else {
paulo@89 5349 next.U = parent;
paulo@89 5350 parent = next;
paulo@89 5351 node = next.R;
paulo@89 5352 }
paulo@89 5353 } else {
paulo@89 5354 red = node.C;
paulo@89 5355 node = next;
paulo@89 5356 }
paulo@89 5357 if (node) node.U = parent;
paulo@89 5358 if (red) return;
paulo@89 5359 if (node && node.C) {
paulo@89 5360 node.C = false;
paulo@89 5361 return;
paulo@89 5362 }
paulo@89 5363 do {
paulo@89 5364 if (node === this._) break;
paulo@89 5365 if (node === parent.L) {
paulo@89 5366 sibling = parent.R;
paulo@89 5367 if (sibling.C) {
paulo@89 5368 sibling.C = false;
paulo@89 5369 parent.C = true;
paulo@89 5370 d3_geom_voronoiRedBlackRotateLeft(this, parent);
paulo@89 5371 sibling = parent.R;
paulo@89 5372 }
paulo@89 5373 if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) {
paulo@89 5374 if (!sibling.R || !sibling.R.C) {
paulo@89 5375 sibling.L.C = false;
paulo@89 5376 sibling.C = true;
paulo@89 5377 d3_geom_voronoiRedBlackRotateRight(this, sibling);
paulo@89 5378 sibling = parent.R;
paulo@89 5379 }
paulo@89 5380 sibling.C = parent.C;
paulo@89 5381 parent.C = sibling.R.C = false;
paulo@89 5382 d3_geom_voronoiRedBlackRotateLeft(this, parent);
paulo@89 5383 node = this._;
paulo@89 5384 break;
paulo@89 5385 }
paulo@89 5386 } else {
paulo@89 5387 sibling = parent.L;
paulo@89 5388 if (sibling.C) {
paulo@89 5389 sibling.C = false;
paulo@89 5390 parent.C = true;
paulo@89 5391 d3_geom_voronoiRedBlackRotateRight(this, parent);
paulo@89 5392 sibling = parent.L;
paulo@89 5393 }
paulo@89 5394 if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) {
paulo@89 5395 if (!sibling.L || !sibling.L.C) {
paulo@89 5396 sibling.R.C = false;
paulo@89 5397 sibling.C = true;
paulo@89 5398 d3_geom_voronoiRedBlackRotateLeft(this, sibling);
paulo@89 5399 sibling = parent.L;
paulo@89 5400 }
paulo@89 5401 sibling.C = parent.C;
paulo@89 5402 parent.C = sibling.L.C = false;
paulo@89 5403 d3_geom_voronoiRedBlackRotateRight(this, parent);
paulo@89 5404 node = this._;
paulo@89 5405 break;
paulo@89 5406 }
paulo@89 5407 }
paulo@89 5408 sibling.C = true;
paulo@89 5409 node = parent;
paulo@89 5410 parent = parent.U;
paulo@89 5411 } while (!node.C);
paulo@89 5412 if (node) node.C = false;
paulo@89 5413 }
paulo@89 5414 };
paulo@89 5415 function d3_geom_voronoiRedBlackRotateLeft(tree, node) {
paulo@89 5416 var p = node, q = node.R, parent = p.U;
paulo@89 5417 if (parent) {
paulo@89 5418 if (parent.L === p) parent.L = q; else parent.R = q;
paulo@89 5419 } else {
paulo@89 5420 tree._ = q;
paulo@89 5421 }
paulo@89 5422 q.U = parent;
paulo@89 5423 p.U = q;
paulo@89 5424 p.R = q.L;
paulo@89 5425 if (p.R) p.R.U = p;
paulo@89 5426 q.L = p;
paulo@89 5427 }
paulo@89 5428 function d3_geom_voronoiRedBlackRotateRight(tree, node) {
paulo@89 5429 var p = node, q = node.L, parent = p.U;
paulo@89 5430 if (parent) {
paulo@89 5431 if (parent.L === p) parent.L = q; else parent.R = q;
paulo@89 5432 } else {
paulo@89 5433 tree._ = q;
paulo@89 5434 }
paulo@89 5435 q.U = parent;
paulo@89 5436 p.U = q;
paulo@89 5437 p.L = q.R;
paulo@89 5438 if (p.L) p.L.U = p;
paulo@89 5439 q.R = p;
paulo@89 5440 }
paulo@89 5441 function d3_geom_voronoiRedBlackFirst(node) {
paulo@89 5442 while (node.L) node = node.L;
paulo@89 5443 return node;
paulo@89 5444 }
paulo@89 5445 function d3_geom_voronoi(sites, bbox) {
paulo@89 5446 var site = sites.sort(d3_geom_voronoiVertexOrder).pop(), x0, y0, circle;
paulo@89 5447 d3_geom_voronoiEdges = [];
paulo@89 5448 d3_geom_voronoiCells = new Array(sites.length);
paulo@89 5449 d3_geom_voronoiBeaches = new d3_geom_voronoiRedBlackTree();
paulo@89 5450 d3_geom_voronoiCircles = new d3_geom_voronoiRedBlackTree();
paulo@89 5451 while (true) {
paulo@89 5452 circle = d3_geom_voronoiFirstCircle;
paulo@89 5453 if (site && (!circle || site.y < circle.y || site.y === circle.y && site.x < circle.x)) {
paulo@89 5454 if (site.x !== x0 || site.y !== y0) {
paulo@89 5455 d3_geom_voronoiCells[site.i] = new d3_geom_voronoiCell(site);
paulo@89 5456 d3_geom_voronoiAddBeach(site);
paulo@89 5457 x0 = site.x, y0 = site.y;
paulo@89 5458 }
paulo@89 5459 site = sites.pop();
paulo@89 5460 } else if (circle) {
paulo@89 5461 d3_geom_voronoiRemoveBeach(circle.arc);
paulo@89 5462 } else {
paulo@89 5463 break;
paulo@89 5464 }
paulo@89 5465 }
paulo@89 5466 if (bbox) d3_geom_voronoiClipEdges(bbox), d3_geom_voronoiCloseCells(bbox);
paulo@89 5467 var diagram = {
paulo@89 5468 cells: d3_geom_voronoiCells,
paulo@89 5469 edges: d3_geom_voronoiEdges
paulo@89 5470 };
paulo@89 5471 d3_geom_voronoiBeaches = d3_geom_voronoiCircles = d3_geom_voronoiEdges = d3_geom_voronoiCells = null;
paulo@89 5472 return diagram;
paulo@89 5473 }
paulo@89 5474 function d3_geom_voronoiVertexOrder(a, b) {
paulo@89 5475 return b.y - a.y || b.x - a.x;
paulo@89 5476 }
paulo@89 5477 d3.geom.voronoi = function(points) {
paulo@89 5478 var x = d3_geom_pointX, y = d3_geom_pointY, fx = x, fy = y, clipExtent = d3_geom_voronoiClipExtent;
paulo@89 5479 if (points) return voronoi(points);
paulo@89 5480 function voronoi(data) {
paulo@89 5481 var polygons = new Array(data.length), x0 = clipExtent[0][0], y0 = clipExtent[0][1], x1 = clipExtent[1][0], y1 = clipExtent[1][1];
paulo@89 5482 d3_geom_voronoi(sites(data), clipExtent).cells.forEach(function(cell, i) {
paulo@89 5483 var edges = cell.edges, site = cell.site, polygon = polygons[i] = edges.length ? edges.map(function(e) {
paulo@89 5484 var s = e.start();
paulo@89 5485 return [ s.x, s.y ];
paulo@89 5486 }) : site.x >= x0 && site.x <= x1 && site.y >= y0 && site.y <= y1 ? [ [ x0, y1 ], [ x1, y1 ], [ x1, y0 ], [ x0, y0 ] ] : [];
paulo@89 5487 polygon.point = data[i];
paulo@89 5488 });
paulo@89 5489 return polygons;
paulo@89 5490 }
paulo@89 5491 function sites(data) {
paulo@89 5492 return data.map(function(d, i) {
paulo@89 5493 return {
paulo@89 5494 x: Math.round(fx(d, i) / ε) * ε,
paulo@89 5495 y: Math.round(fy(d, i) / ε) * ε,
paulo@89 5496 i: i
paulo@89 5497 };
paulo@89 5498 });
paulo@89 5499 }
paulo@89 5500 voronoi.links = function(data) {
paulo@89 5501 return d3_geom_voronoi(sites(data)).edges.filter(function(edge) {
paulo@89 5502 return edge.l && edge.r;
paulo@89 5503 }).map(function(edge) {
paulo@89 5504 return {
paulo@89 5505 source: data[edge.l.i],
paulo@89 5506 target: data[edge.r.i]
paulo@89 5507 };
paulo@89 5508 });
paulo@89 5509 };
paulo@89 5510 voronoi.triangles = function(data) {
paulo@89 5511 var triangles = [];
paulo@89 5512 d3_geom_voronoi(sites(data)).cells.forEach(function(cell, i) {
paulo@89 5513 var site = cell.site, edges = cell.edges.sort(d3_geom_voronoiHalfEdgeOrder), j = -1, m = edges.length, e0, s0, e1 = edges[m - 1].edge, s1 = e1.l === site ? e1.r : e1.l;
paulo@89 5514 while (++j < m) {
paulo@89 5515 e0 = e1;
paulo@89 5516 s0 = s1;
paulo@89 5517 e1 = edges[j].edge;
paulo@89 5518 s1 = e1.l === site ? e1.r : e1.l;
paulo@89 5519 if (i < s0.i && i < s1.i && d3_geom_voronoiTriangleArea(site, s0, s1) < 0) {
paulo@89 5520 triangles.push([ data[i], data[s0.i], data[s1.i] ]);
paulo@89 5521 }
paulo@89 5522 }
paulo@89 5523 });
paulo@89 5524 return triangles;
paulo@89 5525 };
paulo@89 5526 voronoi.x = function(_) {
paulo@89 5527 return arguments.length ? (fx = d3_functor(x = _), voronoi) : x;
paulo@89 5528 };
paulo@89 5529 voronoi.y = function(_) {
paulo@89 5530 return arguments.length ? (fy = d3_functor(y = _), voronoi) : y;
paulo@89 5531 };
paulo@89 5532 voronoi.clipExtent = function(_) {
paulo@89 5533 if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent;
paulo@89 5534 clipExtent = _ == null ? d3_geom_voronoiClipExtent : _;
paulo@89 5535 return voronoi;
paulo@89 5536 };
paulo@89 5537 voronoi.size = function(_) {
paulo@89 5538 if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent && clipExtent[1];
paulo@89 5539 return voronoi.clipExtent(_ && [ [ 0, 0 ], _ ]);
paulo@89 5540 };
paulo@89 5541 return voronoi;
paulo@89 5542 };
paulo@89 5543 var d3_geom_voronoiClipExtent = [ [ -1e6, -1e6 ], [ 1e6, 1e6 ] ];
paulo@89 5544 function d3_geom_voronoiTriangleArea(a, b, c) {
paulo@89 5545 return (a.x - c.x) * (b.y - a.y) - (a.x - b.x) * (c.y - a.y);
paulo@89 5546 }
paulo@89 5547 d3.geom.delaunay = function(vertices) {
paulo@89 5548 return d3.geom.voronoi().triangles(vertices);
paulo@89 5549 };
paulo@89 5550 d3.geom.quadtree = function(points, x1, y1, x2, y2) {
paulo@89 5551 var x = d3_geom_pointX, y = d3_geom_pointY, compat;
paulo@89 5552 if (compat = arguments.length) {
paulo@89 5553 x = d3_geom_quadtreeCompatX;
paulo@89 5554 y = d3_geom_quadtreeCompatY;
paulo@89 5555 if (compat === 3) {
paulo@89 5556 y2 = y1;
paulo@89 5557 x2 = x1;
paulo@89 5558 y1 = x1 = 0;
paulo@89 5559 }
paulo@89 5560 return quadtree(points);
paulo@89 5561 }
paulo@89 5562 function quadtree(data) {
paulo@89 5563 var d, fx = d3_functor(x), fy = d3_functor(y), xs, ys, i, n, x1_, y1_, x2_, y2_;
paulo@89 5564 if (x1 != null) {
paulo@89 5565 x1_ = x1, y1_ = y1, x2_ = x2, y2_ = y2;
paulo@89 5566 } else {
paulo@89 5567 x2_ = y2_ = -(x1_ = y1_ = Infinity);
paulo@89 5568 xs = [], ys = [];
paulo@89 5569 n = data.length;
paulo@89 5570 if (compat) for (i = 0; i < n; ++i) {
paulo@89 5571 d = data[i];
paulo@89 5572 if (d.x < x1_) x1_ = d.x;
paulo@89 5573 if (d.y < y1_) y1_ = d.y;
paulo@89 5574 if (d.x > x2_) x2_ = d.x;
paulo@89 5575 if (d.y > y2_) y2_ = d.y;
paulo@89 5576 xs.push(d.x);
paulo@89 5577 ys.push(d.y);
paulo@89 5578 } else for (i = 0; i < n; ++i) {
paulo@89 5579 var x_ = +fx(d = data[i], i), y_ = +fy(d, i);
paulo@89 5580 if (x_ < x1_) x1_ = x_;
paulo@89 5581 if (y_ < y1_) y1_ = y_;
paulo@89 5582 if (x_ > x2_) x2_ = x_;
paulo@89 5583 if (y_ > y2_) y2_ = y_;
paulo@89 5584 xs.push(x_);
paulo@89 5585 ys.push(y_);
paulo@89 5586 }
paulo@89 5587 }
paulo@89 5588 var dx = x2_ - x1_, dy = y2_ - y1_;
paulo@89 5589 if (dx > dy) y2_ = y1_ + dx; else x2_ = x1_ + dy;
paulo@89 5590 function insert(n, d, x, y, x1, y1, x2, y2) {
paulo@89 5591 if (isNaN(x) || isNaN(y)) return;
paulo@89 5592 if (n.leaf) {
paulo@89 5593 var nx = n.x, ny = n.y;
paulo@89 5594 if (nx != null) {
paulo@89 5595 if (abs(nx - x) + abs(ny - y) < .01) {
paulo@89 5596 insertChild(n, d, x, y, x1, y1, x2, y2);
paulo@89 5597 } else {
paulo@89 5598 var nPoint = n.point;
paulo@89 5599 n.x = n.y = n.point = null;
paulo@89 5600 insertChild(n, nPoint, nx, ny, x1, y1, x2, y2);
paulo@89 5601 insertChild(n, d, x, y, x1, y1, x2, y2);
paulo@89 5602 }
paulo@89 5603 } else {
paulo@89 5604 n.x = x, n.y = y, n.point = d;
paulo@89 5605 }
paulo@89 5606 } else {
paulo@89 5607 insertChild(n, d, x, y, x1, y1, x2, y2);
paulo@89 5608 }
paulo@89 5609 }
paulo@89 5610 function insertChild(n, d, x, y, x1, y1, x2, y2) {
paulo@89 5611 var xm = (x1 + x2) * .5, ym = (y1 + y2) * .5, right = x >= xm, below = y >= ym, i = below << 1 | right;
paulo@89 5612 n.leaf = false;
paulo@89 5613 n = n.nodes[i] || (n.nodes[i] = d3_geom_quadtreeNode());
paulo@89 5614 if (right) x1 = xm; else x2 = xm;
paulo@89 5615 if (below) y1 = ym; else y2 = ym;
paulo@89 5616 insert(n, d, x, y, x1, y1, x2, y2);
paulo@89 5617 }
paulo@89 5618 var root = d3_geom_quadtreeNode();
paulo@89 5619 root.add = function(d) {
paulo@89 5620 insert(root, d, +fx(d, ++i), +fy(d, i), x1_, y1_, x2_, y2_);
paulo@89 5621 };
paulo@89 5622 root.visit = function(f) {
paulo@89 5623 d3_geom_quadtreeVisit(f, root, x1_, y1_, x2_, y2_);
paulo@89 5624 };
paulo@89 5625 root.find = function(point) {
paulo@89 5626 return d3_geom_quadtreeFind(root, point[0], point[1], x1_, y1_, x2_, y2_);
paulo@89 5627 };
paulo@89 5628 i = -1;
paulo@89 5629 if (x1 == null) {
paulo@89 5630 while (++i < n) {
paulo@89 5631 insert(root, data[i], xs[i], ys[i], x1_, y1_, x2_, y2_);
paulo@89 5632 }
paulo@89 5633 --i;
paulo@89 5634 } else data.forEach(root.add);
paulo@89 5635 xs = ys = data = d = null;
paulo@89 5636 return root;
paulo@89 5637 }
paulo@89 5638 quadtree.x = function(_) {
paulo@89 5639 return arguments.length ? (x = _, quadtree) : x;
paulo@89 5640 };
paulo@89 5641 quadtree.y = function(_) {
paulo@89 5642 return arguments.length ? (y = _, quadtree) : y;
paulo@89 5643 };
paulo@89 5644 quadtree.extent = function(_) {
paulo@89 5645 if (!arguments.length) return x1 == null ? null : [ [ x1, y1 ], [ x2, y2 ] ];
paulo@89 5646 if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = +_[0][0], y1 = +_[0][1], x2 = +_[1][0],
paulo@89 5647 y2 = +_[1][1];
paulo@89 5648 return quadtree;
paulo@89 5649 };
paulo@89 5650 quadtree.size = function(_) {
paulo@89 5651 if (!arguments.length) return x1 == null ? null : [ x2 - x1, y2 - y1 ];
paulo@89 5652 if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = y1 = 0, x2 = +_[0], y2 = +_[1];
paulo@89 5653 return quadtree;
paulo@89 5654 };
paulo@89 5655 return quadtree;
paulo@89 5656 };
paulo@89 5657 function d3_geom_quadtreeCompatX(d) {
paulo@89 5658 return d.x;
paulo@89 5659 }
paulo@89 5660 function d3_geom_quadtreeCompatY(d) {
paulo@89 5661 return d.y;
paulo@89 5662 }
paulo@89 5663 function d3_geom_quadtreeNode() {
paulo@89 5664 return {
paulo@89 5665 leaf: true,
paulo@89 5666 nodes: [],
paulo@89 5667 point: null,
paulo@89 5668 x: null,
paulo@89 5669 y: null
paulo@89 5670 };
paulo@89 5671 }
paulo@89 5672 function d3_geom_quadtreeVisit(f, node, x1, y1, x2, y2) {
paulo@89 5673 if (!f(node, x1, y1, x2, y2)) {
paulo@89 5674 var sx = (x1 + x2) * .5, sy = (y1 + y2) * .5, children = node.nodes;
paulo@89 5675 if (children[0]) d3_geom_quadtreeVisit(f, children[0], x1, y1, sx, sy);
paulo@89 5676 if (children[1]) d3_geom_quadtreeVisit(f, children[1], sx, y1, x2, sy);
paulo@89 5677 if (children[2]) d3_geom_quadtreeVisit(f, children[2], x1, sy, sx, y2);
paulo@89 5678 if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2);
paulo@89 5679 }
paulo@89 5680 }
paulo@89 5681 function d3_geom_quadtreeFind(root, x, y, x0, y0, x3, y3) {
paulo@89 5682 var minDistance2 = Infinity, closestPoint;
paulo@89 5683 (function find(node, x1, y1, x2, y2) {
paulo@89 5684 if (x1 > x3 || y1 > y3 || x2 < x0 || y2 < y0) return;
paulo@89 5685 if (point = node.point) {
paulo@89 5686 var point, dx = x - node.x, dy = y - node.y, distance2 = dx * dx + dy * dy;
paulo@89 5687 if (distance2 < minDistance2) {
paulo@89 5688 var distance = Math.sqrt(minDistance2 = distance2);
paulo@89 5689 x0 = x - distance, y0 = y - distance;
paulo@89 5690 x3 = x + distance, y3 = y + distance;
paulo@89 5691 closestPoint = point;
paulo@89 5692 }
paulo@89 5693 }
paulo@89 5694 var children = node.nodes, xm = (x1 + x2) * .5, ym = (y1 + y2) * .5, right = x >= xm, below = y >= ym;
paulo@89 5695 for (var i = below << 1 | right, j = i + 4; i < j; ++i) {
paulo@89 5696 if (node = children[i & 3]) switch (i & 3) {
paulo@89 5697 case 0:
paulo@89 5698 find(node, x1, y1, xm, ym);
paulo@89 5699 break;
paulo@89 5700
paulo@89 5701 case 1:
paulo@89 5702 find(node, xm, y1, x2, ym);
paulo@89 5703 break;
paulo@89 5704
paulo@89 5705 case 2:
paulo@89 5706 find(node, x1, ym, xm, y2);
paulo@89 5707 break;
paulo@89 5708
paulo@89 5709 case 3:
paulo@89 5710 find(node, xm, ym, x2, y2);
paulo@89 5711 break;
paulo@89 5712 }
paulo@89 5713 }
paulo@89 5714 })(root, x0, y0, x3, y3);
paulo@89 5715 return closestPoint;
paulo@89 5716 }
paulo@89 5717 d3.interpolateRgb = d3_interpolateRgb;
paulo@89 5718 function d3_interpolateRgb(a, b) {
paulo@89 5719 a = d3.rgb(a);
paulo@89 5720 b = d3.rgb(b);
paulo@89 5721 var ar = a.r, ag = a.g, ab = a.b, br = b.r - ar, bg = b.g - ag, bb = b.b - ab;
paulo@89 5722 return function(t) {
paulo@89 5723 return "#" + d3_rgb_hex(Math.round(ar + br * t)) + d3_rgb_hex(Math.round(ag + bg * t)) + d3_rgb_hex(Math.round(ab + bb * t));
paulo@89 5724 };
paulo@89 5725 }
paulo@89 5726 d3.interpolateObject = d3_interpolateObject;
paulo@89 5727 function d3_interpolateObject(a, b) {
paulo@89 5728 var i = {}, c = {}, k;
paulo@89 5729 for (k in a) {
paulo@89 5730 if (k in b) {
paulo@89 5731 i[k] = d3_interpolate(a[k], b[k]);
paulo@89 5732 } else {
paulo@89 5733 c[k] = a[k];
paulo@89 5734 }
paulo@89 5735 }
paulo@89 5736 for (k in b) {
paulo@89 5737 if (!(k in a)) {
paulo@89 5738 c[k] = b[k];
paulo@89 5739 }
paulo@89 5740 }
paulo@89 5741 return function(t) {
paulo@89 5742 for (k in i) c[k] = i[k](t);
paulo@89 5743 return c;
paulo@89 5744 };
paulo@89 5745 }
paulo@89 5746 d3.interpolateNumber = d3_interpolateNumber;
paulo@89 5747 function d3_interpolateNumber(a, b) {
paulo@89 5748 a = +a, b = +b;
paulo@89 5749 return function(t) {
paulo@89 5750 return a * (1 - t) + b * t;
paulo@89 5751 };
paulo@89 5752 }
paulo@89 5753 d3.interpolateString = d3_interpolateString;
paulo@89 5754 function d3_interpolateString(a, b) {
paulo@89 5755 var bi = d3_interpolate_numberA.lastIndex = d3_interpolate_numberB.lastIndex = 0, am, bm, bs, i = -1, s = [], q = [];
paulo@89 5756 a = a + "", b = b + "";
paulo@89 5757 while ((am = d3_interpolate_numberA.exec(a)) && (bm = d3_interpolate_numberB.exec(b))) {
paulo@89 5758 if ((bs = bm.index) > bi) {
paulo@89 5759 bs = b.slice(bi, bs);
paulo@89 5760 if (s[i]) s[i] += bs; else s[++i] = bs;
paulo@89 5761 }
paulo@89 5762 if ((am = am[0]) === (bm = bm[0])) {
paulo@89 5763 if (s[i]) s[i] += bm; else s[++i] = bm;
paulo@89 5764 } else {
paulo@89 5765 s[++i] = null;
paulo@89 5766 q.push({
paulo@89 5767 i: i,
paulo@89 5768 x: d3_interpolateNumber(am, bm)
paulo@89 5769 });
paulo@89 5770 }
paulo@89 5771 bi = d3_interpolate_numberB.lastIndex;
paulo@89 5772 }
paulo@89 5773 if (bi < b.length) {
paulo@89 5774 bs = b.slice(bi);
paulo@89 5775 if (s[i]) s[i] += bs; else s[++i] = bs;
paulo@89 5776 }
paulo@89 5777 return s.length < 2 ? q[0] ? (b = q[0].x, function(t) {
paulo@89 5778 return b(t) + "";
paulo@89 5779 }) : function() {
paulo@89 5780 return b;
paulo@89 5781 } : (b = q.length, function(t) {
paulo@89 5782 for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t);
paulo@89 5783 return s.join("");
paulo@89 5784 });
paulo@89 5785 }
paulo@89 5786 var d3_interpolate_numberA = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g, d3_interpolate_numberB = new RegExp(d3_interpolate_numberA.source, "g");
paulo@89 5787 d3.interpolate = d3_interpolate;
paulo@89 5788 function d3_interpolate(a, b) {
paulo@89 5789 var i = d3.interpolators.length, f;
paulo@89 5790 while (--i >= 0 && !(f = d3.interpolators[i](a, b))) ;
paulo@89 5791 return f;
paulo@89 5792 }
paulo@89 5793 d3.interpolators = [ function(a, b) {
paulo@89 5794 var t = typeof b;
paulo@89 5795 return (t === "string" ? d3_rgb_names.has(b.toLowerCase()) || /^(#|rgb\(|hsl\()/i.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_color ? d3_interpolateRgb : Array.isArray(b) ? d3_interpolateArray : t === "object" && isNaN(b) ? d3_interpolateObject : d3_interpolateNumber)(a, b);
paulo@89 5796 } ];
paulo@89 5797 d3.interpolateArray = d3_interpolateArray;
paulo@89 5798 function d3_interpolateArray(a, b) {
paulo@89 5799 var x = [], c = [], na = a.length, nb = b.length, n0 = Math.min(a.length, b.length), i;
paulo@89 5800 for (i = 0; i < n0; ++i) x.push(d3_interpolate(a[i], b[i]));
paulo@89 5801 for (;i < na; ++i) c[i] = a[i];
paulo@89 5802 for (;i < nb; ++i) c[i] = b[i];
paulo@89 5803 return function(t) {
paulo@89 5804 for (i = 0; i < n0; ++i) c[i] = x[i](t);
paulo@89 5805 return c;
paulo@89 5806 };
paulo@89 5807 }
paulo@89 5808 var d3_ease_default = function() {
paulo@89 5809 return d3_identity;
paulo@89 5810 };
paulo@89 5811 var d3_ease = d3.map({
paulo@89 5812 linear: d3_ease_default,
paulo@89 5813 poly: d3_ease_poly,
paulo@89 5814 quad: function() {
paulo@89 5815 return d3_ease_quad;
paulo@89 5816 },
paulo@89 5817 cubic: function() {
paulo@89 5818 return d3_ease_cubic;
paulo@89 5819 },
paulo@89 5820 sin: function() {
paulo@89 5821 return d3_ease_sin;
paulo@89 5822 },
paulo@89 5823 exp: function() {
paulo@89 5824 return d3_ease_exp;
paulo@89 5825 },
paulo@89 5826 circle: function() {
paulo@89 5827 return d3_ease_circle;
paulo@89 5828 },
paulo@89 5829 elastic: d3_ease_elastic,
paulo@89 5830 back: d3_ease_back,
paulo@89 5831 bounce: function() {
paulo@89 5832 return d3_ease_bounce;
paulo@89 5833 }
paulo@89 5834 });
paulo@89 5835 var d3_ease_mode = d3.map({
paulo@89 5836 "in": d3_identity,
paulo@89 5837 out: d3_ease_reverse,
paulo@89 5838 "in-out": d3_ease_reflect,
paulo@89 5839 "out-in": function(f) {
paulo@89 5840 return d3_ease_reflect(d3_ease_reverse(f));
paulo@89 5841 }
paulo@89 5842 });
paulo@89 5843 d3.ease = function(name) {
paulo@89 5844 var i = name.indexOf("-"), t = i >= 0 ? name.slice(0, i) : name, m = i >= 0 ? name.slice(i + 1) : "in";
paulo@89 5845 t = d3_ease.get(t) || d3_ease_default;
paulo@89 5846 m = d3_ease_mode.get(m) || d3_identity;
paulo@89 5847 return d3_ease_clamp(m(t.apply(null, d3_arraySlice.call(arguments, 1))));
paulo@89 5848 };
paulo@89 5849 function d3_ease_clamp(f) {
paulo@89 5850 return function(t) {
paulo@89 5851 return t <= 0 ? 0 : t >= 1 ? 1 : f(t);
paulo@89 5852 };
paulo@89 5853 }
paulo@89 5854 function d3_ease_reverse(f) {
paulo@89 5855 return function(t) {
paulo@89 5856 return 1 - f(1 - t);
paulo@89 5857 };
paulo@89 5858 }
paulo@89 5859 function d3_ease_reflect(f) {
paulo@89 5860 return function(t) {
paulo@89 5861 return .5 * (t < .5 ? f(2 * t) : 2 - f(2 - 2 * t));
paulo@89 5862 };
paulo@89 5863 }
paulo@89 5864 function d3_ease_quad(t) {
paulo@89 5865 return t * t;
paulo@89 5866 }
paulo@89 5867 function d3_ease_cubic(t) {
paulo@89 5868 return t * t * t;
paulo@89 5869 }
paulo@89 5870 function d3_ease_cubicInOut(t) {
paulo@89 5871 if (t <= 0) return 0;
paulo@89 5872 if (t >= 1) return 1;
paulo@89 5873 var t2 = t * t, t3 = t2 * t;
paulo@89 5874 return 4 * (t < .5 ? t3 : 3 * (t - t2) + t3 - .75);
paulo@89 5875 }
paulo@89 5876 function d3_ease_poly(e) {
paulo@89 5877 return function(t) {
paulo@89 5878 return Math.pow(t, e);
paulo@89 5879 };
paulo@89 5880 }
paulo@89 5881 function d3_ease_sin(t) {
paulo@89 5882 return 1 - Math.cos(t * halfπ);
paulo@89 5883 }
paulo@89 5884 function d3_ease_exp(t) {
paulo@89 5885 return Math.pow(2, 10 * (t - 1));
paulo@89 5886 }
paulo@89 5887 function d3_ease_circle(t) {
paulo@89 5888 return 1 - Math.sqrt(1 - t * t);
paulo@89 5889 }
paulo@89 5890 function d3_ease_elastic(a, p) {
paulo@89 5891 var s;
paulo@89 5892 if (arguments.length < 2) p = .45;
paulo@89 5893 if (arguments.length) s = p / τ * Math.asin(1 / a); else a = 1, s = p / 4;
paulo@89 5894 return function(t) {
paulo@89 5895 return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * τ / p);
paulo@89 5896 };
paulo@89 5897 }
paulo@89 5898 function d3_ease_back(s) {
paulo@89 5899 if (!s) s = 1.70158;
paulo@89 5900 return function(t) {
paulo@89 5901 return t * t * ((s + 1) * t - s);
paulo@89 5902 };
paulo@89 5903 }
paulo@89 5904 function d3_ease_bounce(t) {
paulo@89 5905 return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375;
paulo@89 5906 }
paulo@89 5907 d3.interpolateHcl = d3_interpolateHcl;
paulo@89 5908 function d3_interpolateHcl(a, b) {
paulo@89 5909 a = d3.hcl(a);
paulo@89 5910 b = d3.hcl(b);
paulo@89 5911 var ah = a.h, ac = a.c, al = a.l, bh = b.h - ah, bc = b.c - ac, bl = b.l - al;
paulo@89 5912 if (isNaN(bc)) bc = 0, ac = isNaN(ac) ? b.c : ac;
paulo@89 5913 if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360;
paulo@89 5914 return function(t) {
paulo@89 5915 return d3_hcl_lab(ah + bh * t, ac + bc * t, al + bl * t) + "";
paulo@89 5916 };
paulo@89 5917 }
paulo@89 5918 d3.interpolateHsl = d3_interpolateHsl;
paulo@89 5919 function d3_interpolateHsl(a, b) {
paulo@89 5920 a = d3.hsl(a);
paulo@89 5921 b = d3.hsl(b);
paulo@89 5922 var ah = a.h, as = a.s, al = a.l, bh = b.h - ah, bs = b.s - as, bl = b.l - al;
paulo@89 5923 if (isNaN(bs)) bs = 0, as = isNaN(as) ? b.s : as;
paulo@89 5924 if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360;
paulo@89 5925 return function(t) {
paulo@89 5926 return d3_hsl_rgb(ah + bh * t, as + bs * t, al + bl * t) + "";
paulo@89 5927 };
paulo@89 5928 }
paulo@89 5929 d3.interpolateLab = d3_interpolateLab;
paulo@89 5930 function d3_interpolateLab(a, b) {
paulo@89 5931 a = d3.lab(a);
paulo@89 5932 b = d3.lab(b);
paulo@89 5933 var al = a.l, aa = a.a, ab = a.b, bl = b.l - al, ba = b.a - aa, bb = b.b - ab;
paulo@89 5934 return function(t) {
paulo@89 5935 return d3_lab_rgb(al + bl * t, aa + ba * t, ab + bb * t) + "";
paulo@89 5936 };
paulo@89 5937 }
paulo@89 5938 d3.interpolateRound = d3_interpolateRound;
paulo@89 5939 function d3_interpolateRound(a, b) {
paulo@89 5940 b -= a;
paulo@89 5941 return function(t) {
paulo@89 5942 return Math.round(a + b * t);
paulo@89 5943 };
paulo@89 5944 }
paulo@89 5945 d3.transform = function(string) {
paulo@89 5946 var g = d3_document.createElementNS(d3.ns.prefix.svg, "g");
paulo@89 5947 return (d3.transform = function(string) {
paulo@89 5948 if (string != null) {
paulo@89 5949 g.setAttribute("transform", string);
paulo@89 5950 var t = g.transform.baseVal.consolidate();
paulo@89 5951 }
paulo@89 5952 return new d3_transform(t ? t.matrix : d3_transformIdentity);
paulo@89 5953 })(string);
paulo@89 5954 };
paulo@89 5955 function d3_transform(m) {
paulo@89 5956 var r0 = [ m.a, m.b ], r1 = [ m.c, m.d ], kx = d3_transformNormalize(r0), kz = d3_transformDot(r0, r1), ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0;
paulo@89 5957 if (r0[0] * r1[1] < r1[0] * r0[1]) {
paulo@89 5958 r0[0] *= -1;
paulo@89 5959 r0[1] *= -1;
paulo@89 5960 kx *= -1;
paulo@89 5961 kz *= -1;
paulo@89 5962 }
paulo@89 5963 this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_degrees;
paulo@89 5964 this.translate = [ m.e, m.f ];
paulo@89 5965 this.scale = [ kx, ky ];
paulo@89 5966 this.skew = ky ? Math.atan2(kz, ky) * d3_degrees : 0;
paulo@89 5967 }
paulo@89 5968 d3_transform.prototype.toString = function() {
paulo@89 5969 return "translate(" + this.translate + ")rotate(" + this.rotate + ")skewX(" + this.skew + ")scale(" + this.scale + ")";
paulo@89 5970 };
paulo@89 5971 function d3_transformDot(a, b) {
paulo@89 5972 return a[0] * b[0] + a[1] * b[1];
paulo@89 5973 }
paulo@89 5974 function d3_transformNormalize(a) {
paulo@89 5975 var k = Math.sqrt(d3_transformDot(a, a));
paulo@89 5976 if (k) {
paulo@89 5977 a[0] /= k;
paulo@89 5978 a[1] /= k;
paulo@89 5979 }
paulo@89 5980 return k;
paulo@89 5981 }
paulo@89 5982 function d3_transformCombine(a, b, k) {
paulo@89 5983 a[0] += k * b[0];
paulo@89 5984 a[1] += k * b[1];
paulo@89 5985 return a;
paulo@89 5986 }
paulo@89 5987 var d3_transformIdentity = {
paulo@89 5988 a: 1,
paulo@89 5989 b: 0,
paulo@89 5990 c: 0,
paulo@89 5991 d: 1,
paulo@89 5992 e: 0,
paulo@89 5993 f: 0
paulo@89 5994 };
paulo@89 5995 d3.interpolateTransform = d3_interpolateTransform;
paulo@89 5996 function d3_interpolateTransformPop(s) {
paulo@89 5997 return s.length ? s.pop() + "," : "";
paulo@89 5998 }
paulo@89 5999 function d3_interpolateTranslate(ta, tb, s, q) {
paulo@89 6000 if (ta[0] !== tb[0] || ta[1] !== tb[1]) {
paulo@89 6001 var i = s.push("translate(", null, ",", null, ")");
paulo@89 6002 q.push({
paulo@89 6003 i: i - 4,
paulo@89 6004 x: d3_interpolateNumber(ta[0], tb[0])
paulo@89 6005 }, {
paulo@89 6006 i: i - 2,
paulo@89 6007 x: d3_interpolateNumber(ta[1], tb[1])
paulo@89 6008 });
paulo@89 6009 } else if (tb[0] || tb[1]) {
paulo@89 6010 s.push("translate(" + tb + ")");
paulo@89 6011 }
paulo@89 6012 }
paulo@89 6013 function d3_interpolateRotate(ra, rb, s, q) {
paulo@89 6014 if (ra !== rb) {
paulo@89 6015 if (ra - rb > 180) rb += 360; else if (rb - ra > 180) ra += 360;
paulo@89 6016 q.push({
paulo@89 6017 i: s.push(d3_interpolateTransformPop(s) + "rotate(", null, ")") - 2,
paulo@89 6018 x: d3_interpolateNumber(ra, rb)
paulo@89 6019 });
paulo@89 6020 } else if (rb) {
paulo@89 6021 s.push(d3_interpolateTransformPop(s) + "rotate(" + rb + ")");
paulo@89 6022 }
paulo@89 6023 }
paulo@89 6024 function d3_interpolateSkew(wa, wb, s, q) {
paulo@89 6025 if (wa !== wb) {
paulo@89 6026 q.push({
paulo@89 6027 i: s.push(d3_interpolateTransformPop(s) + "skewX(", null, ")") - 2,
paulo@89 6028 x: d3_interpolateNumber(wa, wb)
paulo@89 6029 });
paulo@89 6030 } else if (wb) {
paulo@89 6031 s.push(d3_interpolateTransformPop(s) + "skewX(" + wb + ")");
paulo@89 6032 }
paulo@89 6033 }
paulo@89 6034 function d3_interpolateScale(ka, kb, s, q) {
paulo@89 6035 if (ka[0] !== kb[0] || ka[1] !== kb[1]) {
paulo@89 6036 var i = s.push(d3_interpolateTransformPop(s) + "scale(", null, ",", null, ")");
paulo@89 6037 q.push({
paulo@89 6038 i: i - 4,
paulo@89 6039 x: d3_interpolateNumber(ka[0], kb[0])
paulo@89 6040 }, {
paulo@89 6041 i: i - 2,
paulo@89 6042 x: d3_interpolateNumber(ka[1], kb[1])
paulo@89 6043 });
paulo@89 6044 } else if (kb[0] !== 1 || kb[1] !== 1) {
paulo@89 6045 s.push(d3_interpolateTransformPop(s) + "scale(" + kb + ")");
paulo@89 6046 }
paulo@89 6047 }
paulo@89 6048 function d3_interpolateTransform(a, b) {
paulo@89 6049 var s = [], q = [];
paulo@89 6050 a = d3.transform(a), b = d3.transform(b);
paulo@89 6051 d3_interpolateTranslate(a.translate, b.translate, s, q);
paulo@89 6052 d3_interpolateRotate(a.rotate, b.rotate, s, q);
paulo@89 6053 d3_interpolateSkew(a.skew, b.skew, s, q);
paulo@89 6054 d3_interpolateScale(a.scale, b.scale, s, q);
paulo@89 6055 a = b = null;
paulo@89 6056 return function(t) {
paulo@89 6057 var i = -1, n = q.length, o;
paulo@89 6058 while (++i < n) s[(o = q[i]).i] = o.x(t);
paulo@89 6059 return s.join("");
paulo@89 6060 };
paulo@89 6061 }
paulo@89 6062 function d3_uninterpolateNumber(a, b) {
paulo@89 6063 b = (b -= a = +a) || 1 / b;
paulo@89 6064 return function(x) {
paulo@89 6065 return (x - a) / b;
paulo@89 6066 };
paulo@89 6067 }
paulo@89 6068 function d3_uninterpolateClamp(a, b) {
paulo@89 6069 b = (b -= a = +a) || 1 / b;
paulo@89 6070 return function(x) {
paulo@89 6071 return Math.max(0, Math.min(1, (x - a) / b));
paulo@89 6072 };
paulo@89 6073 }
paulo@89 6074 d3.layout = {};
paulo@89 6075 d3.layout.bundle = function() {
paulo@89 6076 return function(links) {
paulo@89 6077 var paths = [], i = -1, n = links.length;
paulo@89 6078 while (++i < n) paths.push(d3_layout_bundlePath(links[i]));
paulo@89 6079 return paths;
paulo@89 6080 };
paulo@89 6081 };
paulo@89 6082 function d3_layout_bundlePath(link) {
paulo@89 6083 var start = link.source, end = link.target, lca = d3_layout_bundleLeastCommonAncestor(start, end), points = [ start ];
paulo@89 6084 while (start !== lca) {
paulo@89 6085 start = start.parent;
paulo@89 6086 points.push(start);
paulo@89 6087 }
paulo@89 6088 var k = points.length;
paulo@89 6089 while (end !== lca) {
paulo@89 6090 points.splice(k, 0, end);
paulo@89 6091 end = end.parent;
paulo@89 6092 }
paulo@89 6093 return points;
paulo@89 6094 }
paulo@89 6095 function d3_layout_bundleAncestors(node) {
paulo@89 6096 var ancestors = [], parent = node.parent;
paulo@89 6097 while (parent != null) {
paulo@89 6098 ancestors.push(node);
paulo@89 6099 node = parent;
paulo@89 6100 parent = parent.parent;
paulo@89 6101 }
paulo@89 6102 ancestors.push(node);
paulo@89 6103 return ancestors;
paulo@89 6104 }
paulo@89 6105 function d3_layout_bundleLeastCommonAncestor(a, b) {
paulo@89 6106 if (a === b) return a;
paulo@89 6107 var aNodes = d3_layout_bundleAncestors(a), bNodes = d3_layout_bundleAncestors(b), aNode = aNodes.pop(), bNode = bNodes.pop(), sharedNode = null;
paulo@89 6108 while (aNode === bNode) {
paulo@89 6109 sharedNode = aNode;
paulo@89 6110 aNode = aNodes.pop();
paulo@89 6111 bNode = bNodes.pop();
paulo@89 6112 }
paulo@89 6113 return sharedNode;
paulo@89 6114 }
paulo@89 6115 d3.layout.chord = function() {
paulo@89 6116 var chord = {}, chords, groups, matrix, n, padding = 0, sortGroups, sortSubgroups, sortChords;
paulo@89 6117 function relayout() {
paulo@89 6118 var subgroups = {}, groupSums = [], groupIndex = d3.range(n), subgroupIndex = [], k, x, x0, i, j;
paulo@89 6119 chords = [];
paulo@89 6120 groups = [];
paulo@89 6121 k = 0, i = -1;
paulo@89 6122 while (++i < n) {
paulo@89 6123 x = 0, j = -1;
paulo@89 6124 while (++j < n) {
paulo@89 6125 x += matrix[i][j];
paulo@89 6126 }
paulo@89 6127 groupSums.push(x);
paulo@89 6128 subgroupIndex.push(d3.range(n));
paulo@89 6129 k += x;
paulo@89 6130 }
paulo@89 6131 if (sortGroups) {
paulo@89 6132 groupIndex.sort(function(a, b) {
paulo@89 6133 return sortGroups(groupSums[a], groupSums[b]);
paulo@89 6134 });
paulo@89 6135 }
paulo@89 6136 if (sortSubgroups) {
paulo@89 6137 subgroupIndex.forEach(function(d, i) {
paulo@89 6138 d.sort(function(a, b) {
paulo@89 6139 return sortSubgroups(matrix[i][a], matrix[i][b]);
paulo@89 6140 });
paulo@89 6141 });
paulo@89 6142 }
paulo@89 6143 k = (τ - padding * n) / k;
paulo@89 6144 x = 0, i = -1;
paulo@89 6145 while (++i < n) {
paulo@89 6146 x0 = x, j = -1;
paulo@89 6147 while (++j < n) {
paulo@89 6148 var di = groupIndex[i], dj = subgroupIndex[di][j], v = matrix[di][dj], a0 = x, a1 = x += v * k;
paulo@89 6149 subgroups[di + "-" + dj] = {
paulo@89 6150 index: di,
paulo@89 6151 subindex: dj,
paulo@89 6152 startAngle: a0,
paulo@89 6153 endAngle: a1,
paulo@89 6154 value: v
paulo@89 6155 };
paulo@89 6156 }
paulo@89 6157 groups[di] = {
paulo@89 6158 index: di,
paulo@89 6159 startAngle: x0,
paulo@89 6160 endAngle: x,
paulo@89 6161 value: groupSums[di]
paulo@89 6162 };
paulo@89 6163 x += padding;
paulo@89 6164 }
paulo@89 6165 i = -1;
paulo@89 6166 while (++i < n) {
paulo@89 6167 j = i - 1;
paulo@89 6168 while (++j < n) {
paulo@89 6169 var source = subgroups[i + "-" + j], target = subgroups[j + "-" + i];
paulo@89 6170 if (source.value || target.value) {
paulo@89 6171 chords.push(source.value < target.value ? {
paulo@89 6172 source: target,
paulo@89 6173 target: source
paulo@89 6174 } : {
paulo@89 6175 source: source,
paulo@89 6176 target: target
paulo@89 6177 });
paulo@89 6178 }
paulo@89 6179 }
paulo@89 6180 }
paulo@89 6181 if (sortChords) resort();
paulo@89 6182 }
paulo@89 6183 function resort() {
paulo@89 6184 chords.sort(function(a, b) {
paulo@89 6185 return sortChords((a.source.value + a.target.value) / 2, (b.source.value + b.target.value) / 2);
paulo@89 6186 });
paulo@89 6187 }
paulo@89 6188 chord.matrix = function(x) {
paulo@89 6189 if (!arguments.length) return matrix;
paulo@89 6190 n = (matrix = x) && matrix.length;
paulo@89 6191 chords = groups = null;
paulo@89 6192 return chord;
paulo@89 6193 };
paulo@89 6194 chord.padding = function(x) {
paulo@89 6195 if (!arguments.length) return padding;
paulo@89 6196 padding = x;
paulo@89 6197 chords = groups = null;
paulo@89 6198 return chord;
paulo@89 6199 };
paulo@89 6200 chord.sortGroups = function(x) {
paulo@89 6201 if (!arguments.length) return sortGroups;
paulo@89 6202 sortGroups = x;
paulo@89 6203 chords = groups = null;
paulo@89 6204 return chord;
paulo@89 6205 };
paulo@89 6206 chord.sortSubgroups = function(x) {
paulo@89 6207 if (!arguments.length) return sortSubgroups;
paulo@89 6208 sortSubgroups = x;
paulo@89 6209 chords = null;
paulo@89 6210 return chord;
paulo@89 6211 };
paulo@89 6212 chord.sortChords = function(x) {
paulo@89 6213 if (!arguments.length) return sortChords;
paulo@89 6214 sortChords = x;
paulo@89 6215 if (chords) resort();
paulo@89 6216 return chord;
paulo@89 6217 };
paulo@89 6218 chord.chords = function() {
paulo@89 6219 if (!chords) relayout();
paulo@89 6220 return chords;
paulo@89 6221 };
paulo@89 6222 chord.groups = function() {
paulo@89 6223 if (!groups) relayout();
paulo@89 6224 return groups;
paulo@89 6225 };
paulo@89 6226 return chord;
paulo@89 6227 };
paulo@89 6228 d3.layout.force = function() {
paulo@89 6229 var force = {}, event = d3.dispatch("start", "tick", "end"), timer, size = [ 1, 1 ], drag, alpha, friction = .9, linkDistance = d3_layout_forceLinkDistance, linkStrength = d3_layout_forceLinkStrength, charge = -30, chargeDistance2 = d3_layout_forceChargeDistance2, gravity = .1, theta2 = .64, nodes = [], links = [], distances, strengths, charges;
paulo@89 6230 function repulse(node) {
paulo@89 6231 return function(quad, x1, _, x2) {
paulo@89 6232 if (quad.point !== node) {
paulo@89 6233 var dx = quad.cx - node.x, dy = quad.cy - node.y, dw = x2 - x1, dn = dx * dx + dy * dy;
paulo@89 6234 if (dw * dw / theta2 < dn) {
paulo@89 6235 if (dn < chargeDistance2) {
paulo@89 6236 var k = quad.charge / dn;
paulo@89 6237 node.px -= dx * k;
paulo@89 6238 node.py -= dy * k;
paulo@89 6239 }
paulo@89 6240 return true;
paulo@89 6241 }
paulo@89 6242 if (quad.point && dn && dn < chargeDistance2) {
paulo@89 6243 var k = quad.pointCharge / dn;
paulo@89 6244 node.px -= dx * k;
paulo@89 6245 node.py -= dy * k;
paulo@89 6246 }
paulo@89 6247 }
paulo@89 6248 return !quad.charge;
paulo@89 6249 };
paulo@89 6250 }
paulo@89 6251 force.tick = function() {
paulo@89 6252 if ((alpha *= .99) < .005) {
paulo@89 6253 timer = null;
paulo@89 6254 event.end({
paulo@89 6255 type: "end",
paulo@89 6256 alpha: alpha = 0
paulo@89 6257 });
paulo@89 6258 return true;
paulo@89 6259 }
paulo@89 6260 var n = nodes.length, m = links.length, q, i, o, s, t, l, k, x, y;
paulo@89 6261 for (i = 0; i < m; ++i) {
paulo@89 6262 o = links[i];
paulo@89 6263 s = o.source;
paulo@89 6264 t = o.target;
paulo@89 6265 x = t.x - s.x;
paulo@89 6266 y = t.y - s.y;
paulo@89 6267 if (l = x * x + y * y) {
paulo@89 6268 l = alpha * strengths[i] * ((l = Math.sqrt(l)) - distances[i]) / l;
paulo@89 6269 x *= l;
paulo@89 6270 y *= l;
paulo@89 6271 t.x -= x * (k = s.weight + t.weight ? s.weight / (s.weight + t.weight) : .5);
paulo@89 6272 t.y -= y * k;
paulo@89 6273 s.x += x * (k = 1 - k);
paulo@89 6274 s.y += y * k;
paulo@89 6275 }
paulo@89 6276 }
paulo@89 6277 if (k = alpha * gravity) {
paulo@89 6278 x = size[0] / 2;
paulo@89 6279 y = size[1] / 2;
paulo@89 6280 i = -1;
paulo@89 6281 if (k) while (++i < n) {
paulo@89 6282 o = nodes[i];
paulo@89 6283 o.x += (x - o.x) * k;
paulo@89 6284 o.y += (y - o.y) * k;
paulo@89 6285 }
paulo@89 6286 }
paulo@89 6287 if (charge) {
paulo@89 6288 d3_layout_forceAccumulate(q = d3.geom.quadtree(nodes), alpha, charges);
paulo@89 6289 i = -1;
paulo@89 6290 while (++i < n) {
paulo@89 6291 if (!(o = nodes[i]).fixed) {
paulo@89 6292 q.visit(repulse(o));
paulo@89 6293 }
paulo@89 6294 }
paulo@89 6295 }
paulo@89 6296 i = -1;
paulo@89 6297 while (++i < n) {
paulo@89 6298 o = nodes[i];
paulo@89 6299 if (o.fixed) {
paulo@89 6300 o.x = o.px;
paulo@89 6301 o.y = o.py;
paulo@89 6302 } else {
paulo@89 6303 o.x -= (o.px - (o.px = o.x)) * friction;
paulo@89 6304 o.y -= (o.py - (o.py = o.y)) * friction;
paulo@89 6305 }
paulo@89 6306 }
paulo@89 6307 event.tick({
paulo@89 6308 type: "tick",
paulo@89 6309 alpha: alpha
paulo@89 6310 });
paulo@89 6311 };
paulo@89 6312 force.nodes = function(x) {
paulo@89 6313 if (!arguments.length) return nodes;
paulo@89 6314 nodes = x;
paulo@89 6315 return force;
paulo@89 6316 };
paulo@89 6317 force.links = function(x) {
paulo@89 6318 if (!arguments.length) return links;
paulo@89 6319 links = x;
paulo@89 6320 return force;
paulo@89 6321 };
paulo@89 6322 force.size = function(x) {
paulo@89 6323 if (!arguments.length) return size;
paulo@89 6324 size = x;
paulo@89 6325 return force;
paulo@89 6326 };
paulo@89 6327 force.linkDistance = function(x) {
paulo@89 6328 if (!arguments.length) return linkDistance;
paulo@89 6329 linkDistance = typeof x === "function" ? x : +x;
paulo@89 6330 return force;
paulo@89 6331 };
paulo@89 6332 force.distance = force.linkDistance;
paulo@89 6333 force.linkStrength = function(x) {
paulo@89 6334 if (!arguments.length) return linkStrength;
paulo@89 6335 linkStrength = typeof x === "function" ? x : +x;
paulo@89 6336 return force;
paulo@89 6337 };
paulo@89 6338 force.friction = function(x) {
paulo@89 6339 if (!arguments.length) return friction;
paulo@89 6340 friction = +x;
paulo@89 6341 return force;
paulo@89 6342 };
paulo@89 6343 force.charge = function(x) {
paulo@89 6344 if (!arguments.length) return charge;
paulo@89 6345 charge = typeof x === "function" ? x : +x;
paulo@89 6346 return force;
paulo@89 6347 };
paulo@89 6348 force.chargeDistance = function(x) {
paulo@89 6349 if (!arguments.length) return Math.sqrt(chargeDistance2);
paulo@89 6350 chargeDistance2 = x * x;
paulo@89 6351 return force;
paulo@89 6352 };
paulo@89 6353 force.gravity = function(x) {
paulo@89 6354 if (!arguments.length) return gravity;
paulo@89 6355 gravity = +x;
paulo@89 6356 return force;
paulo@89 6357 };
paulo@89 6358 force.theta = function(x) {
paulo@89 6359 if (!arguments.length) return Math.sqrt(theta2);
paulo@89 6360 theta2 = x * x;
paulo@89 6361 return force;
paulo@89 6362 };
paulo@89 6363 force.alpha = function(x) {
paulo@89 6364 if (!arguments.length) return alpha;
paulo@89 6365 x = +x;
paulo@89 6366 if (alpha) {
paulo@89 6367 if (x > 0) {
paulo@89 6368 alpha = x;
paulo@89 6369 } else {
paulo@89 6370 timer.c = null, timer.t = NaN, timer = null;
paulo@89 6371 event.end({
paulo@89 6372 type: "end",
paulo@89 6373 alpha: alpha = 0
paulo@89 6374 });
paulo@89 6375 }
paulo@89 6376 } else if (x > 0) {
paulo@89 6377 event.start({
paulo@89 6378 type: "start",
paulo@89 6379 alpha: alpha = x
paulo@89 6380 });
paulo@89 6381 timer = d3_timer(force.tick);
paulo@89 6382 }
paulo@89 6383 return force;
paulo@89 6384 };
paulo@89 6385 force.start = function() {
paulo@89 6386 var i, n = nodes.length, m = links.length, w = size[0], h = size[1], neighbors, o;
paulo@89 6387 for (i = 0; i < n; ++i) {
paulo@89 6388 (o = nodes[i]).index = i;
paulo@89 6389 o.weight = 0;
paulo@89 6390 }
paulo@89 6391 for (i = 0; i < m; ++i) {
paulo@89 6392 o = links[i];
paulo@89 6393 if (typeof o.source == "number") o.source = nodes[o.source];
paulo@89 6394 if (typeof o.target == "number") o.target = nodes[o.target];
paulo@89 6395 ++o.source.weight;
paulo@89 6396 ++o.target.weight;
paulo@89 6397 }
paulo@89 6398 for (i = 0; i < n; ++i) {
paulo@89 6399 o = nodes[i];
paulo@89 6400 if (isNaN(o.x)) o.x = position("x", w);
paulo@89 6401 if (isNaN(o.y)) o.y = position("y", h);
paulo@89 6402 if (isNaN(o.px)) o.px = o.x;
paulo@89 6403 if (isNaN(o.py)) o.py = o.y;
paulo@89 6404 }
paulo@89 6405 distances = [];
paulo@89 6406 if (typeof linkDistance === "function") for (i = 0; i < m; ++i) distances[i] = +linkDistance.call(this, links[i], i); else for (i = 0; i < m; ++i) distances[i] = linkDistance;
paulo@89 6407 strengths = [];
paulo@89 6408 if (typeof linkStrength === "function") for (i = 0; i < m; ++i) strengths[i] = +linkStrength.call(this, links[i], i); else for (i = 0; i < m; ++i) strengths[i] = linkStrength;
paulo@89 6409 charges = [];
paulo@89 6410 if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i); else for (i = 0; i < n; ++i) charges[i] = charge;
paulo@89 6411 function position(dimension, size) {
paulo@89 6412 if (!neighbors) {
paulo@89 6413 neighbors = new Array(n);
paulo@89 6414 for (j = 0; j < n; ++j) {
paulo@89 6415 neighbors[j] = [];
paulo@89 6416 }
paulo@89 6417 for (j = 0; j < m; ++j) {
paulo@89 6418 var o = links[j];
paulo@89 6419 neighbors[o.source.index].push(o.target);
paulo@89 6420 neighbors[o.target.index].push(o.source);
paulo@89 6421 }
paulo@89 6422 }
paulo@89 6423 var candidates = neighbors[i], j = -1, l = candidates.length, x;
paulo@89 6424 while (++j < l) if (!isNaN(x = candidates[j][dimension])) return x;
paulo@89 6425 return Math.random() * size;
paulo@89 6426 }
paulo@89 6427 return force.resume();
paulo@89 6428 };
paulo@89 6429 force.resume = function() {
paulo@89 6430 return force.alpha(.1);
paulo@89 6431 };
paulo@89 6432 force.stop = function() {
paulo@89 6433 return force.alpha(0);
paulo@89 6434 };
paulo@89 6435 force.drag = function() {
paulo@89 6436 if (!drag) drag = d3.behavior.drag().origin(d3_identity).on("dragstart.force", d3_layout_forceDragstart).on("drag.force", dragmove).on("dragend.force", d3_layout_forceDragend);
paulo@89 6437 if (!arguments.length) return drag;
paulo@89 6438 this.on("mouseover.force", d3_layout_forceMouseover).on("mouseout.force", d3_layout_forceMouseout).call(drag);
paulo@89 6439 };
paulo@89 6440 function dragmove(d) {
paulo@89 6441 d.px = d3.event.x, d.py = d3.event.y;
paulo@89 6442 force.resume();
paulo@89 6443 }
paulo@89 6444 return d3.rebind(force, event, "on");
paulo@89 6445 };
paulo@89 6446 function d3_layout_forceDragstart(d) {
paulo@89 6447 d.fixed |= 2;
paulo@89 6448 }
paulo@89 6449 function d3_layout_forceDragend(d) {
paulo@89 6450 d.fixed &= ~6;
paulo@89 6451 }
paulo@89 6452 function d3_layout_forceMouseover(d) {
paulo@89 6453 d.fixed |= 4;
paulo@89 6454 d.px = d.x, d.py = d.y;
paulo@89 6455 }
paulo@89 6456 function d3_layout_forceMouseout(d) {
paulo@89 6457 d.fixed &= ~4;
paulo@89 6458 }
paulo@89 6459 function d3_layout_forceAccumulate(quad, alpha, charges) {
paulo@89 6460 var cx = 0, cy = 0;
paulo@89 6461 quad.charge = 0;
paulo@89 6462 if (!quad.leaf) {
paulo@89 6463 var nodes = quad.nodes, n = nodes.length, i = -1, c;
paulo@89 6464 while (++i < n) {
paulo@89 6465 c = nodes[i];
paulo@89 6466 if (c == null) continue;
paulo@89 6467 d3_layout_forceAccumulate(c, alpha, charges);
paulo@89 6468 quad.charge += c.charge;
paulo@89 6469 cx += c.charge * c.cx;
paulo@89 6470 cy += c.charge * c.cy;
paulo@89 6471 }
paulo@89 6472 }
paulo@89 6473 if (quad.point) {
paulo@89 6474 if (!quad.leaf) {
paulo@89 6475 quad.point.x += Math.random() - .5;
paulo@89 6476 quad.point.y += Math.random() - .5;
paulo@89 6477 }
paulo@89 6478 var k = alpha * charges[quad.point.index];
paulo@89 6479 quad.charge += quad.pointCharge = k;
paulo@89 6480 cx += k * quad.point.x;
paulo@89 6481 cy += k * quad.point.y;
paulo@89 6482 }
paulo@89 6483 quad.cx = cx / quad.charge;
paulo@89 6484 quad.cy = cy / quad.charge;
paulo@89 6485 }
paulo@89 6486 var d3_layout_forceLinkDistance = 20, d3_layout_forceLinkStrength = 1, d3_layout_forceChargeDistance2 = Infinity;
paulo@89 6487 d3.layout.hierarchy = function() {
paulo@89 6488 var sort = d3_layout_hierarchySort, children = d3_layout_hierarchyChildren, value = d3_layout_hierarchyValue;
paulo@89 6489 function hierarchy(root) {
paulo@89 6490 var stack = [ root ], nodes = [], node;
paulo@89 6491 root.depth = 0;
paulo@89 6492 while ((node = stack.pop()) != null) {
paulo@89 6493 nodes.push(node);
paulo@89 6494 if ((childs = children.call(hierarchy, node, node.depth)) && (n = childs.length)) {
paulo@89 6495 var n, childs, child;
paulo@89 6496 while (--n >= 0) {
paulo@89 6497 stack.push(child = childs[n]);
paulo@89 6498 child.parent = node;
paulo@89 6499 child.depth = node.depth + 1;
paulo@89 6500 }
paulo@89 6501 if (value) node.value = 0;
paulo@89 6502 node.children = childs;
paulo@89 6503 } else {
paulo@89 6504 if (value) node.value = +value.call(hierarchy, node, node.depth) || 0;
paulo@89 6505 delete node.children;
paulo@89 6506 }
paulo@89 6507 }
paulo@89 6508 d3_layout_hierarchyVisitAfter(root, function(node) {
paulo@89 6509 var childs, parent;
paulo@89 6510 if (sort && (childs = node.children)) childs.sort(sort);
paulo@89 6511 if (value && (parent = node.parent)) parent.value += node.value;
paulo@89 6512 });
paulo@89 6513 return nodes;
paulo@89 6514 }
paulo@89 6515 hierarchy.sort = function(x) {
paulo@89 6516 if (!arguments.length) return sort;
paulo@89 6517 sort = x;
paulo@89 6518 return hierarchy;
paulo@89 6519 };
paulo@89 6520 hierarchy.children = function(x) {
paulo@89 6521 if (!arguments.length) return children;
paulo@89 6522 children = x;
paulo@89 6523 return hierarchy;
paulo@89 6524 };
paulo@89 6525 hierarchy.value = function(x) {
paulo@89 6526 if (!arguments.length) return value;
paulo@89 6527 value = x;
paulo@89 6528 return hierarchy;
paulo@89 6529 };
paulo@89 6530 hierarchy.revalue = function(root) {
paulo@89 6531 if (value) {
paulo@89 6532 d3_layout_hierarchyVisitBefore(root, function(node) {
paulo@89 6533 if (node.children) node.value = 0;
paulo@89 6534 });
paulo@89 6535 d3_layout_hierarchyVisitAfter(root, function(node) {
paulo@89 6536 var parent;
paulo@89 6537 if (!node.children) node.value = +value.call(hierarchy, node, node.depth) || 0;
paulo@89 6538 if (parent = node.parent) parent.value += node.value;
paulo@89 6539 });
paulo@89 6540 }
paulo@89 6541 return root;
paulo@89 6542 };
paulo@89 6543 return hierarchy;
paulo@89 6544 };
paulo@89 6545 function d3_layout_hierarchyRebind(object, hierarchy) {
paulo@89 6546 d3.rebind(object, hierarchy, "sort", "children", "value");
paulo@89 6547 object.nodes = object;
paulo@89 6548 object.links = d3_layout_hierarchyLinks;
paulo@89 6549 return object;
paulo@89 6550 }
paulo@89 6551 function d3_layout_hierarchyVisitBefore(node, callback) {
paulo@89 6552 var nodes = [ node ];
paulo@89 6553 while ((node = nodes.pop()) != null) {
paulo@89 6554 callback(node);
paulo@89 6555 if ((children = node.children) && (n = children.length)) {
paulo@89 6556 var n, children;
paulo@89 6557 while (--n >= 0) nodes.push(children[n]);
paulo@89 6558 }
paulo@89 6559 }
paulo@89 6560 }
paulo@89 6561 function d3_layout_hierarchyVisitAfter(node, callback) {
paulo@89 6562 var nodes = [ node ], nodes2 = [];
paulo@89 6563 while ((node = nodes.pop()) != null) {
paulo@89 6564 nodes2.push(node);
paulo@89 6565 if ((children = node.children) && (n = children.length)) {
paulo@89 6566 var i = -1, n, children;
paulo@89 6567 while (++i < n) nodes.push(children[i]);
paulo@89 6568 }
paulo@89 6569 }
paulo@89 6570 while ((node = nodes2.pop()) != null) {
paulo@89 6571 callback(node);
paulo@89 6572 }
paulo@89 6573 }
paulo@89 6574 function d3_layout_hierarchyChildren(d) {
paulo@89 6575 return d.children;
paulo@89 6576 }
paulo@89 6577 function d3_layout_hierarchyValue(d) {
paulo@89 6578 return d.value;
paulo@89 6579 }
paulo@89 6580 function d3_layout_hierarchySort(a, b) {
paulo@89 6581 return b.value - a.value;
paulo@89 6582 }
paulo@89 6583 function d3_layout_hierarchyLinks(nodes) {
paulo@89 6584 return d3.merge(nodes.map(function(parent) {
paulo@89 6585 return (parent.children || []).map(function(child) {
paulo@89 6586 return {
paulo@89 6587 source: parent,
paulo@89 6588 target: child
paulo@89 6589 };
paulo@89 6590 });
paulo@89 6591 }));
paulo@89 6592 }
paulo@89 6593 d3.layout.partition = function() {
paulo@89 6594 var hierarchy = d3.layout.hierarchy(), size = [ 1, 1 ];
paulo@89 6595 function position(node, x, dx, dy) {
paulo@89 6596 var children = node.children;
paulo@89 6597 node.x = x;
paulo@89 6598 node.y = node.depth * dy;
paulo@89 6599 node.dx = dx;
paulo@89 6600 node.dy = dy;
paulo@89 6601 if (children && (n = children.length)) {
paulo@89 6602 var i = -1, n, c, d;
paulo@89 6603 dx = node.value ? dx / node.value : 0;
paulo@89 6604 while (++i < n) {
paulo@89 6605 position(c = children[i], x, d = c.value * dx, dy);
paulo@89 6606 x += d;
paulo@89 6607 }
paulo@89 6608 }
paulo@89 6609 }
paulo@89 6610 function depth(node) {
paulo@89 6611 var children = node.children, d = 0;
paulo@89 6612 if (children && (n = children.length)) {
paulo@89 6613 var i = -1, n;
paulo@89 6614 while (++i < n) d = Math.max(d, depth(children[i]));
paulo@89 6615 }
paulo@89 6616 return 1 + d;
paulo@89 6617 }
paulo@89 6618 function partition(d, i) {
paulo@89 6619 var nodes = hierarchy.call(this, d, i);
paulo@89 6620 position(nodes[0], 0, size[0], size[1] / depth(nodes[0]));
paulo@89 6621 return nodes;
paulo@89 6622 }
paulo@89 6623 partition.size = function(x) {
paulo@89 6624 if (!arguments.length) return size;
paulo@89 6625 size = x;
paulo@89 6626 return partition;
paulo@89 6627 };
paulo@89 6628 return d3_layout_hierarchyRebind(partition, hierarchy);
paulo@89 6629 };
paulo@89 6630 d3.layout.pie = function() {
paulo@89 6631 var value = Number, sort = d3_layout_pieSortByValue, startAngle = 0, endAngle = τ, padAngle = 0;
paulo@89 6632 function pie(data) {
paulo@89 6633 var n = data.length, values = data.map(function(d, i) {
paulo@89 6634 return +value.call(pie, d, i);
paulo@89 6635 }), a = +(typeof startAngle === "function" ? startAngle.apply(this, arguments) : startAngle), da = (typeof endAngle === "function" ? endAngle.apply(this, arguments) : endAngle) - a, p = Math.min(Math.abs(da) / n, +(typeof padAngle === "function" ? padAngle.apply(this, arguments) : padAngle)), pa = p * (da < 0 ? -1 : 1), sum = d3.sum(values), k = sum ? (da - n * pa) / sum : 0, index = d3.range(n), arcs = [], v;
paulo@89 6636 if (sort != null) index.sort(sort === d3_layout_pieSortByValue ? function(i, j) {
paulo@89 6637 return values[j] - values[i];
paulo@89 6638 } : function(i, j) {
paulo@89 6639 return sort(data[i], data[j]);
paulo@89 6640 });
paulo@89 6641 index.forEach(function(i) {
paulo@89 6642 arcs[i] = {
paulo@89 6643 data: data[i],
paulo@89 6644 value: v = values[i],
paulo@89 6645 startAngle: a,
paulo@89 6646 endAngle: a += v * k + pa,
paulo@89 6647 padAngle: p
paulo@89 6648 };
paulo@89 6649 });
paulo@89 6650 return arcs;
paulo@89 6651 }
paulo@89 6652 pie.value = function(_) {
paulo@89 6653 if (!arguments.length) return value;
paulo@89 6654 value = _;
paulo@89 6655 return pie;
paulo@89 6656 };
paulo@89 6657 pie.sort = function(_) {
paulo@89 6658 if (!arguments.length) return sort;
paulo@89 6659 sort = _;
paulo@89 6660 return pie;
paulo@89 6661 };
paulo@89 6662 pie.startAngle = function(_) {
paulo@89 6663 if (!arguments.length) return startAngle;
paulo@89 6664 startAngle = _;
paulo@89 6665 return pie;
paulo@89 6666 };
paulo@89 6667 pie.endAngle = function(_) {
paulo@89 6668 if (!arguments.length) return endAngle;
paulo@89 6669 endAngle = _;
paulo@89 6670 return pie;
paulo@89 6671 };
paulo@89 6672 pie.padAngle = function(_) {
paulo@89 6673 if (!arguments.length) return padAngle;
paulo@89 6674 padAngle = _;
paulo@89 6675 return pie;
paulo@89 6676 };
paulo@89 6677 return pie;
paulo@89 6678 };
paulo@89 6679 var d3_layout_pieSortByValue = {};
paulo@89 6680 d3.layout.stack = function() {
paulo@89 6681 var values = d3_identity, order = d3_layout_stackOrderDefault, offset = d3_layout_stackOffsetZero, out = d3_layout_stackOut, x = d3_layout_stackX, y = d3_layout_stackY;
paulo@89 6682 function stack(data, index) {
paulo@89 6683 if (!(n = data.length)) return data;
paulo@89 6684 var series = data.map(function(d, i) {
paulo@89 6685 return values.call(stack, d, i);
paulo@89 6686 });
paulo@89 6687 var points = series.map(function(d) {
paulo@89 6688 return d.map(function(v, i) {
paulo@89 6689 return [ x.call(stack, v, i), y.call(stack, v, i) ];
paulo@89 6690 });
paulo@89 6691 });
paulo@89 6692 var orders = order.call(stack, points, index);
paulo@89 6693 series = d3.permute(series, orders);
paulo@89 6694 points = d3.permute(points, orders);
paulo@89 6695 var offsets = offset.call(stack, points, index);
paulo@89 6696 var m = series[0].length, n, i, j, o;
paulo@89 6697 for (j = 0; j < m; ++j) {
paulo@89 6698 out.call(stack, series[0][j], o = offsets[j], points[0][j][1]);
paulo@89 6699 for (i = 1; i < n; ++i) {
paulo@89 6700 out.call(stack, series[i][j], o += points[i - 1][j][1], points[i][j][1]);
paulo@89 6701 }
paulo@89 6702 }
paulo@89 6703 return data;
paulo@89 6704 }
paulo@89 6705 stack.values = function(x) {
paulo@89 6706 if (!arguments.length) return values;
paulo@89 6707 values = x;
paulo@89 6708 return stack;
paulo@89 6709 };
paulo@89 6710 stack.order = function(x) {
paulo@89 6711 if (!arguments.length) return order;
paulo@89 6712 order = typeof x === "function" ? x : d3_layout_stackOrders.get(x) || d3_layout_stackOrderDefault;
paulo@89 6713 return stack;
paulo@89 6714 };
paulo@89 6715 stack.offset = function(x) {
paulo@89 6716 if (!arguments.length) return offset;
paulo@89 6717 offset = typeof x === "function" ? x : d3_layout_stackOffsets.get(x) || d3_layout_stackOffsetZero;
paulo@89 6718 return stack;
paulo@89 6719 };
paulo@89 6720 stack.x = function(z) {
paulo@89 6721 if (!arguments.length) return x;
paulo@89 6722 x = z;
paulo@89 6723 return stack;
paulo@89 6724 };
paulo@89 6725 stack.y = function(z) {
paulo@89 6726 if (!arguments.length) return y;
paulo@89 6727 y = z;
paulo@89 6728 return stack;
paulo@89 6729 };
paulo@89 6730 stack.out = function(z) {
paulo@89 6731 if (!arguments.length) return out;
paulo@89 6732 out = z;
paulo@89 6733 return stack;
paulo@89 6734 };
paulo@89 6735 return stack;
paulo@89 6736 };
paulo@89 6737 function d3_layout_stackX(d) {
paulo@89 6738 return d.x;
paulo@89 6739 }
paulo@89 6740 function d3_layout_stackY(d) {
paulo@89 6741 return d.y;
paulo@89 6742 }
paulo@89 6743 function d3_layout_stackOut(d, y0, y) {
paulo@89 6744 d.y0 = y0;
paulo@89 6745 d.y = y;
paulo@89 6746 }
paulo@89 6747 var d3_layout_stackOrders = d3.map({
paulo@89 6748 "inside-out": function(data) {
paulo@89 6749 var n = data.length, i, j, max = data.map(d3_layout_stackMaxIndex), sums = data.map(d3_layout_stackReduceSum), index = d3.range(n).sort(function(a, b) {
paulo@89 6750 return max[a] - max[b];
paulo@89 6751 }), top = 0, bottom = 0, tops = [], bottoms = [];
paulo@89 6752 for (i = 0; i < n; ++i) {
paulo@89 6753 j = index[i];
paulo@89 6754 if (top < bottom) {
paulo@89 6755 top += sums[j];
paulo@89 6756 tops.push(j);
paulo@89 6757 } else {
paulo@89 6758 bottom += sums[j];
paulo@89 6759 bottoms.push(j);
paulo@89 6760 }
paulo@89 6761 }
paulo@89 6762 return bottoms.reverse().concat(tops);
paulo@89 6763 },
paulo@89 6764 reverse: function(data) {
paulo@89 6765 return d3.range(data.length).reverse();
paulo@89 6766 },
paulo@89 6767 "default": d3_layout_stackOrderDefault
paulo@89 6768 });
paulo@89 6769 var d3_layout_stackOffsets = d3.map({
paulo@89 6770 silhouette: function(data) {
paulo@89 6771 var n = data.length, m = data[0].length, sums = [], max = 0, i, j, o, y0 = [];
paulo@89 6772 for (j = 0; j < m; ++j) {
paulo@89 6773 for (i = 0, o = 0; i < n; i++) o += data[i][j][1];
paulo@89 6774 if (o > max) max = o;
paulo@89 6775 sums.push(o);
paulo@89 6776 }
paulo@89 6777 for (j = 0; j < m; ++j) {
paulo@89 6778 y0[j] = (max - sums[j]) / 2;
paulo@89 6779 }
paulo@89 6780 return y0;
paulo@89 6781 },
paulo@89 6782 wiggle: function(data) {
paulo@89 6783 var n = data.length, x = data[0], m = x.length, i, j, k, s1, s2, s3, dx, o, o0, y0 = [];
paulo@89 6784 y0[0] = o = o0 = 0;
paulo@89 6785 for (j = 1; j < m; ++j) {
paulo@89 6786 for (i = 0, s1 = 0; i < n; ++i) s1 += data[i][j][1];
paulo@89 6787 for (i = 0, s2 = 0, dx = x[j][0] - x[j - 1][0]; i < n; ++i) {
paulo@89 6788 for (k = 0, s3 = (data[i][j][1] - data[i][j - 1][1]) / (2 * dx); k < i; ++k) {
paulo@89 6789 s3 += (data[k][j][1] - data[k][j - 1][1]) / dx;
paulo@89 6790 }
paulo@89 6791 s2 += s3 * data[i][j][1];
paulo@89 6792 }
paulo@89 6793 y0[j] = o -= s1 ? s2 / s1 * dx : 0;
paulo@89 6794 if (o < o0) o0 = o;
paulo@89 6795 }
paulo@89 6796 for (j = 0; j < m; ++j) y0[j] -= o0;
paulo@89 6797 return y0;
paulo@89 6798 },
paulo@89 6799 expand: function(data) {
paulo@89 6800 var n = data.length, m = data[0].length, k = 1 / n, i, j, o, y0 = [];
paulo@89 6801 for (j = 0; j < m; ++j) {
paulo@89 6802 for (i = 0, o = 0; i < n; i++) o += data[i][j][1];
paulo@89 6803 if (o) for (i = 0; i < n; i++) data[i][j][1] /= o; else for (i = 0; i < n; i++) data[i][j][1] = k;
paulo@89 6804 }
paulo@89 6805 for (j = 0; j < m; ++j) y0[j] = 0;
paulo@89 6806 return y0;
paulo@89 6807 },
paulo@89 6808 zero: d3_layout_stackOffsetZero
paulo@89 6809 });
paulo@89 6810 function d3_layout_stackOrderDefault(data) {
paulo@89 6811 return d3.range(data.length);
paulo@89 6812 }
paulo@89 6813 function d3_layout_stackOffsetZero(data) {
paulo@89 6814 var j = -1, m = data[0].length, y0 = [];
paulo@89 6815 while (++j < m) y0[j] = 0;
paulo@89 6816 return y0;
paulo@89 6817 }
paulo@89 6818 function d3_layout_stackMaxIndex(array) {
paulo@89 6819 var i = 1, j = 0, v = array[0][1], k, n = array.length;
paulo@89 6820 for (;i < n; ++i) {
paulo@89 6821 if ((k = array[i][1]) > v) {
paulo@89 6822 j = i;
paulo@89 6823 v = k;
paulo@89 6824 }
paulo@89 6825 }
paulo@89 6826 return j;
paulo@89 6827 }
paulo@89 6828 function d3_layout_stackReduceSum(d) {
paulo@89 6829 return d.reduce(d3_layout_stackSum, 0);
paulo@89 6830 }
paulo@89 6831 function d3_layout_stackSum(p, d) {
paulo@89 6832 return p + d[1];
paulo@89 6833 }
paulo@89 6834 d3.layout.histogram = function() {
paulo@89 6835 var frequency = true, valuer = Number, ranger = d3_layout_histogramRange, binner = d3_layout_histogramBinSturges;
paulo@89 6836 function histogram(data, i) {
paulo@89 6837 var bins = [], values = data.map(valuer, this), range = ranger.call(this, values, i), thresholds = binner.call(this, range, values, i), bin, i = -1, n = values.length, m = thresholds.length - 1, k = frequency ? 1 : 1 / n, x;
paulo@89 6838 while (++i < m) {
paulo@89 6839 bin = bins[i] = [];
paulo@89 6840 bin.dx = thresholds[i + 1] - (bin.x = thresholds[i]);
paulo@89 6841 bin.y = 0;
paulo@89 6842 }
paulo@89 6843 if (m > 0) {
paulo@89 6844 i = -1;
paulo@89 6845 while (++i < n) {
paulo@89 6846 x = values[i];
paulo@89 6847 if (x >= range[0] && x <= range[1]) {
paulo@89 6848 bin = bins[d3.bisect(thresholds, x, 1, m) - 1];
paulo@89 6849 bin.y += k;
paulo@89 6850 bin.push(data[i]);
paulo@89 6851 }
paulo@89 6852 }
paulo@89 6853 }
paulo@89 6854 return bins;
paulo@89 6855 }
paulo@89 6856 histogram.value = function(x) {
paulo@89 6857 if (!arguments.length) return valuer;
paulo@89 6858 valuer = x;
paulo@89 6859 return histogram;
paulo@89 6860 };
paulo@89 6861 histogram.range = function(x) {
paulo@89 6862 if (!arguments.length) return ranger;
paulo@89 6863 ranger = d3_functor(x);
paulo@89 6864 return histogram;
paulo@89 6865 };
paulo@89 6866 histogram.bins = function(x) {
paulo@89 6867 if (!arguments.length) return binner;
paulo@89 6868 binner = typeof x === "number" ? function(range) {
paulo@89 6869 return d3_layout_histogramBinFixed(range, x);
paulo@89 6870 } : d3_functor(x);
paulo@89 6871 return histogram;
paulo@89 6872 };
paulo@89 6873 histogram.frequency = function(x) {
paulo@89 6874 if (!arguments.length) return frequency;
paulo@89 6875 frequency = !!x;
paulo@89 6876 return histogram;
paulo@89 6877 };
paulo@89 6878 return histogram;
paulo@89 6879 };
paulo@89 6880 function d3_layout_histogramBinSturges(range, values) {
paulo@89 6881 return d3_layout_histogramBinFixed(range, Math.ceil(Math.log(values.length) / Math.LN2 + 1));
paulo@89 6882 }
paulo@89 6883 function d3_layout_histogramBinFixed(range, n) {
paulo@89 6884 var x = -1, b = +range[0], m = (range[1] - b) / n, f = [];
paulo@89 6885 while (++x <= n) f[x] = m * x + b;
paulo@89 6886 return f;
paulo@89 6887 }
paulo@89 6888 function d3_layout_histogramRange(values) {
paulo@89 6889 return [ d3.min(values), d3.max(values) ];
paulo@89 6890 }
paulo@89 6891 d3.layout.pack = function() {
paulo@89 6892 var hierarchy = d3.layout.hierarchy().sort(d3_layout_packSort), padding = 0, size = [ 1, 1 ], radius;
paulo@89 6893 function pack(d, i) {
paulo@89 6894 var nodes = hierarchy.call(this, d, i), root = nodes[0], w = size[0], h = size[1], r = radius == null ? Math.sqrt : typeof radius === "function" ? radius : function() {
paulo@89 6895 return radius;
paulo@89 6896 };
paulo@89 6897 root.x = root.y = 0;
paulo@89 6898 d3_layout_hierarchyVisitAfter(root, function(d) {
paulo@89 6899 d.r = +r(d.value);
paulo@89 6900 });
paulo@89 6901 d3_layout_hierarchyVisitAfter(root, d3_layout_packSiblings);
paulo@89 6902 if (padding) {
paulo@89 6903 var dr = padding * (radius ? 1 : Math.max(2 * root.r / w, 2 * root.r / h)) / 2;
paulo@89 6904 d3_layout_hierarchyVisitAfter(root, function(d) {
paulo@89 6905 d.r += dr;
paulo@89 6906 });
paulo@89 6907 d3_layout_hierarchyVisitAfter(root, d3_layout_packSiblings);
paulo@89 6908 d3_layout_hierarchyVisitAfter(root, function(d) {
paulo@89 6909 d.r -= dr;
paulo@89 6910 });
paulo@89 6911 }
paulo@89 6912 d3_layout_packTransform(root, w / 2, h / 2, radius ? 1 : 1 / Math.max(2 * root.r / w, 2 * root.r / h));
paulo@89 6913 return nodes;
paulo@89 6914 }
paulo@89 6915 pack.size = function(_) {
paulo@89 6916 if (!arguments.length) return size;
paulo@89 6917 size = _;
paulo@89 6918 return pack;
paulo@89 6919 };
paulo@89 6920 pack.radius = function(_) {
paulo@89 6921 if (!arguments.length) return radius;
paulo@89 6922 radius = _ == null || typeof _ === "function" ? _ : +_;
paulo@89 6923 return pack;
paulo@89 6924 };
paulo@89 6925 pack.padding = function(_) {
paulo@89 6926 if (!arguments.length) return padding;
paulo@89 6927 padding = +_;
paulo@89 6928 return pack;
paulo@89 6929 };
paulo@89 6930 return d3_layout_hierarchyRebind(pack, hierarchy);
paulo@89 6931 };
paulo@89 6932 function d3_layout_packSort(a, b) {
paulo@89 6933 return a.value - b.value;
paulo@89 6934 }
paulo@89 6935 function d3_layout_packInsert(a, b) {
paulo@89 6936 var c = a._pack_next;
paulo@89 6937 a._pack_next = b;
paulo@89 6938 b._pack_prev = a;
paulo@89 6939 b._pack_next = c;
paulo@89 6940 c._pack_prev = b;
paulo@89 6941 }
paulo@89 6942 function d3_layout_packSplice(a, b) {
paulo@89 6943 a._pack_next = b;
paulo@89 6944 b._pack_prev = a;
paulo@89 6945 }
paulo@89 6946 function d3_layout_packIntersects(a, b) {
paulo@89 6947 var dx = b.x - a.x, dy = b.y - a.y, dr = a.r + b.r;
paulo@89 6948 return .999 * dr * dr > dx * dx + dy * dy;
paulo@89 6949 }
paulo@89 6950 function d3_layout_packSiblings(node) {
paulo@89 6951 if (!(nodes = node.children) || !(n = nodes.length)) return;
paulo@89 6952 var nodes, xMin = Infinity, xMax = -Infinity, yMin = Infinity, yMax = -Infinity, a, b, c, i, j, k, n;
paulo@89 6953 function bound(node) {
paulo@89 6954 xMin = Math.min(node.x - node.r, xMin);
paulo@89 6955 xMax = Math.max(node.x + node.r, xMax);
paulo@89 6956 yMin = Math.min(node.y - node.r, yMin);
paulo@89 6957 yMax = Math.max(node.y + node.r, yMax);
paulo@89 6958 }
paulo@89 6959 nodes.forEach(d3_layout_packLink);
paulo@89 6960 a = nodes[0];
paulo@89 6961 a.x = -a.r;
paulo@89 6962 a.y = 0;
paulo@89 6963 bound(a);
paulo@89 6964 if (n > 1) {
paulo@89 6965 b = nodes[1];
paulo@89 6966 b.x = b.r;
paulo@89 6967 b.y = 0;
paulo@89 6968 bound(b);
paulo@89 6969 if (n > 2) {
paulo@89 6970 c = nodes[2];
paulo@89 6971 d3_layout_packPlace(a, b, c);
paulo@89 6972 bound(c);
paulo@89 6973 d3_layout_packInsert(a, c);
paulo@89 6974 a._pack_prev = c;
paulo@89 6975 d3_layout_packInsert(c, b);
paulo@89 6976 b = a._pack_next;
paulo@89 6977 for (i = 3; i < n; i++) {
paulo@89 6978 d3_layout_packPlace(a, b, c = nodes[i]);
paulo@89 6979 var isect = 0, s1 = 1, s2 = 1;
paulo@89 6980 for (j = b._pack_next; j !== b; j = j._pack_next, s1++) {
paulo@89 6981 if (d3_layout_packIntersects(j, c)) {
paulo@89 6982 isect = 1;
paulo@89 6983 break;
paulo@89 6984 }
paulo@89 6985 }
paulo@89 6986 if (isect == 1) {
paulo@89 6987 for (k = a._pack_prev; k !== j._pack_prev; k = k._pack_prev, s2++) {
paulo@89 6988 if (d3_layout_packIntersects(k, c)) {
paulo@89 6989 break;
paulo@89 6990 }
paulo@89 6991 }
paulo@89 6992 }
paulo@89 6993 if (isect) {
paulo@89 6994 if (s1 < s2 || s1 == s2 && b.r < a.r) d3_layout_packSplice(a, b = j); else d3_layout_packSplice(a = k, b);
paulo@89 6995 i--;
paulo@89 6996 } else {
paulo@89 6997 d3_layout_packInsert(a, c);
paulo@89 6998 b = c;
paulo@89 6999 bound(c);
paulo@89 7000 }
paulo@89 7001 }
paulo@89 7002 }
paulo@89 7003 }
paulo@89 7004 var cx = (xMin + xMax) / 2, cy = (yMin + yMax) / 2, cr = 0;
paulo@89 7005 for (i = 0; i < n; i++) {
paulo@89 7006 c = nodes[i];
paulo@89 7007 c.x -= cx;
paulo@89 7008 c.y -= cy;
paulo@89 7009 cr = Math.max(cr, c.r + Math.sqrt(c.x * c.x + c.y * c.y));
paulo@89 7010 }
paulo@89 7011 node.r = cr;
paulo@89 7012 nodes.forEach(d3_layout_packUnlink);
paulo@89 7013 }
paulo@89 7014 function d3_layout_packLink(node) {
paulo@89 7015 node._pack_next = node._pack_prev = node;
paulo@89 7016 }
paulo@89 7017 function d3_layout_packUnlink(node) {
paulo@89 7018 delete node._pack_next;
paulo@89 7019 delete node._pack_prev;
paulo@89 7020 }
paulo@89 7021 function d3_layout_packTransform(node, x, y, k) {
paulo@89 7022 var children = node.children;
paulo@89 7023 node.x = x += k * node.x;
paulo@89 7024 node.y = y += k * node.y;
paulo@89 7025 node.r *= k;
paulo@89 7026 if (children) {
paulo@89 7027 var i = -1, n = children.length;
paulo@89 7028 while (++i < n) d3_layout_packTransform(children[i], x, y, k);
paulo@89 7029 }
paulo@89 7030 }
paulo@89 7031 function d3_layout_packPlace(a, b, c) {
paulo@89 7032 var db = a.r + c.r, dx = b.x - a.x, dy = b.y - a.y;
paulo@89 7033 if (db && (dx || dy)) {
paulo@89 7034 var da = b.r + c.r, dc = dx * dx + dy * dy;
paulo@89 7035 da *= da;
paulo@89 7036 db *= db;
paulo@89 7037 var x = .5 + (db - da) / (2 * dc), y = Math.sqrt(Math.max(0, 2 * da * (db + dc) - (db -= dc) * db - da * da)) / (2 * dc);
paulo@89 7038 c.x = a.x + x * dx + y * dy;
paulo@89 7039 c.y = a.y + x * dy - y * dx;
paulo@89 7040 } else {
paulo@89 7041 c.x = a.x + db;
paulo@89 7042 c.y = a.y;
paulo@89 7043 }
paulo@89 7044 }
paulo@89 7045 d3.layout.tree = function() {
paulo@89 7046 var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = null;
paulo@89 7047 function tree(d, i) {
paulo@89 7048 var nodes = hierarchy.call(this, d, i), root0 = nodes[0], root1 = wrapTree(root0);
paulo@89 7049 d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.m = -root1.z;
paulo@89 7050 d3_layout_hierarchyVisitBefore(root1, secondWalk);
paulo@89 7051 if (nodeSize) d3_layout_hierarchyVisitBefore(root0, sizeNode); else {
paulo@89 7052 var left = root0, right = root0, bottom = root0;
paulo@89 7053 d3_layout_hierarchyVisitBefore(root0, function(node) {
paulo@89 7054 if (node.x < left.x) left = node;
paulo@89 7055 if (node.x > right.x) right = node;
paulo@89 7056 if (node.depth > bottom.depth) bottom = node;
paulo@89 7057 });
paulo@89 7058 var tx = separation(left, right) / 2 - left.x, kx = size[0] / (right.x + separation(right, left) / 2 + tx), ky = size[1] / (bottom.depth || 1);
paulo@89 7059 d3_layout_hierarchyVisitBefore(root0, function(node) {
paulo@89 7060 node.x = (node.x + tx) * kx;
paulo@89 7061 node.y = node.depth * ky;
paulo@89 7062 });
paulo@89 7063 }
paulo@89 7064 return nodes;
paulo@89 7065 }
paulo@89 7066 function wrapTree(root0) {
paulo@89 7067 var root1 = {
paulo@89 7068 A: null,
paulo@89 7069 children: [ root0 ]
paulo@89 7070 }, queue = [ root1 ], node1;
paulo@89 7071 while ((node1 = queue.pop()) != null) {
paulo@89 7072 for (var children = node1.children, child, i = 0, n = children.length; i < n; ++i) {
paulo@89 7073 queue.push((children[i] = child = {
paulo@89 7074 _: children[i],
paulo@89 7075 parent: node1,
paulo@89 7076 children: (child = children[i].children) && child.slice() || [],
paulo@89 7077 A: null,
paulo@89 7078 a: null,
paulo@89 7079 z: 0,
paulo@89 7080 m: 0,
paulo@89 7081 c: 0,
paulo@89 7082 s: 0,
paulo@89 7083 t: null,
paulo@89 7084 i: i
paulo@89 7085 }).a = child);
paulo@89 7086 }
paulo@89 7087 }
paulo@89 7088 return root1.children[0];
paulo@89 7089 }
paulo@89 7090 function firstWalk(v) {
paulo@89 7091 var children = v.children, siblings = v.parent.children, w = v.i ? siblings[v.i - 1] : null;
paulo@89 7092 if (children.length) {
paulo@89 7093 d3_layout_treeShift(v);
paulo@89 7094 var midpoint = (children[0].z + children[children.length - 1].z) / 2;
paulo@89 7095 if (w) {
paulo@89 7096 v.z = w.z + separation(v._, w._);
paulo@89 7097 v.m = v.z - midpoint;
paulo@89 7098 } else {
paulo@89 7099 v.z = midpoint;
paulo@89 7100 }
paulo@89 7101 } else if (w) {
paulo@89 7102 v.z = w.z + separation(v._, w._);
paulo@89 7103 }
paulo@89 7104 v.parent.A = apportion(v, w, v.parent.A || siblings[0]);
paulo@89 7105 }
paulo@89 7106 function secondWalk(v) {
paulo@89 7107 v._.x = v.z + v.parent.m;
paulo@89 7108 v.m += v.parent.m;
paulo@89 7109 }
paulo@89 7110 function apportion(v, w, ancestor) {
paulo@89 7111 if (w) {
paulo@89 7112 var vip = v, vop = v, vim = w, vom = vip.parent.children[0], sip = vip.m, sop = vop.m, sim = vim.m, som = vom.m, shift;
paulo@89 7113 while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) {
paulo@89 7114 vom = d3_layout_treeLeft(vom);
paulo@89 7115 vop = d3_layout_treeRight(vop);
paulo@89 7116 vop.a = v;
paulo@89 7117 shift = vim.z + sim - vip.z - sip + separation(vim._, vip._);
paulo@89 7118 if (shift > 0) {
paulo@89 7119 d3_layout_treeMove(d3_layout_treeAncestor(vim, v, ancestor), v, shift);
paulo@89 7120 sip += shift;
paulo@89 7121 sop += shift;
paulo@89 7122 }
paulo@89 7123 sim += vim.m;
paulo@89 7124 sip += vip.m;
paulo@89 7125 som += vom.m;
paulo@89 7126 sop += vop.m;
paulo@89 7127 }
paulo@89 7128 if (vim && !d3_layout_treeRight(vop)) {
paulo@89 7129 vop.t = vim;
paulo@89 7130 vop.m += sim - sop;
paulo@89 7131 }
paulo@89 7132 if (vip && !d3_layout_treeLeft(vom)) {
paulo@89 7133 vom.t = vip;
paulo@89 7134 vom.m += sip - som;
paulo@89 7135 ancestor = v;
paulo@89 7136 }
paulo@89 7137 }
paulo@89 7138 return ancestor;
paulo@89 7139 }
paulo@89 7140 function sizeNode(node) {
paulo@89 7141 node.x *= size[0];
paulo@89 7142 node.y = node.depth * size[1];
paulo@89 7143 }
paulo@89 7144 tree.separation = function(x) {
paulo@89 7145 if (!arguments.length) return separation;
paulo@89 7146 separation = x;
paulo@89 7147 return tree;
paulo@89 7148 };
paulo@89 7149 tree.size = function(x) {
paulo@89 7150 if (!arguments.length) return nodeSize ? null : size;
paulo@89 7151 nodeSize = (size = x) == null ? sizeNode : null;
paulo@89 7152 return tree;
paulo@89 7153 };
paulo@89 7154 tree.nodeSize = function(x) {
paulo@89 7155 if (!arguments.length) return nodeSize ? size : null;
paulo@89 7156 nodeSize = (size = x) == null ? null : sizeNode;
paulo@89 7157 return tree;
paulo@89 7158 };
paulo@89 7159 return d3_layout_hierarchyRebind(tree, hierarchy);
paulo@89 7160 };
paulo@89 7161 function d3_layout_treeSeparation(a, b) {
paulo@89 7162 return a.parent == b.parent ? 1 : 2;
paulo@89 7163 }
paulo@89 7164 function d3_layout_treeLeft(v) {
paulo@89 7165 var children = v.children;
paulo@89 7166 return children.length ? children[0] : v.t;
paulo@89 7167 }
paulo@89 7168 function d3_layout_treeRight(v) {
paulo@89 7169 var children = v.children, n;
paulo@89 7170 return (n = children.length) ? children[n - 1] : v.t;
paulo@89 7171 }
paulo@89 7172 function d3_layout_treeMove(wm, wp, shift) {
paulo@89 7173 var change = shift / (wp.i - wm.i);
paulo@89 7174 wp.c -= change;
paulo@89 7175 wp.s += shift;
paulo@89 7176 wm.c += change;
paulo@89 7177 wp.z += shift;
paulo@89 7178 wp.m += shift;
paulo@89 7179 }
paulo@89 7180 function d3_layout_treeShift(v) {
paulo@89 7181 var shift = 0, change = 0, children = v.children, i = children.length, w;
paulo@89 7182 while (--i >= 0) {
paulo@89 7183 w = children[i];
paulo@89 7184 w.z += shift;
paulo@89 7185 w.m += shift;
paulo@89 7186 shift += w.s + (change += w.c);
paulo@89 7187 }
paulo@89 7188 }
paulo@89 7189 function d3_layout_treeAncestor(vim, v, ancestor) {
paulo@89 7190 return vim.a.parent === v.parent ? vim.a : ancestor;
paulo@89 7191 }
paulo@89 7192 d3.layout.cluster = function() {
paulo@89 7193 var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false;
paulo@89 7194 function cluster(d, i) {
paulo@89 7195 var nodes = hierarchy.call(this, d, i), root = nodes[0], previousNode, x = 0;
paulo@89 7196 d3_layout_hierarchyVisitAfter(root, function(node) {
paulo@89 7197 var children = node.children;
paulo@89 7198 if (children && children.length) {
paulo@89 7199 node.x = d3_layout_clusterX(children);
paulo@89 7200 node.y = d3_layout_clusterY(children);
paulo@89 7201 } else {
paulo@89 7202 node.x = previousNode ? x += separation(node, previousNode) : 0;
paulo@89 7203 node.y = 0;
paulo@89 7204 previousNode = node;
paulo@89 7205 }
paulo@89 7206 });
paulo@89 7207 var left = d3_layout_clusterLeft(root), right = d3_layout_clusterRight(root), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2;
paulo@89 7208 d3_layout_hierarchyVisitAfter(root, nodeSize ? function(node) {
paulo@89 7209 node.x = (node.x - root.x) * size[0];
paulo@89 7210 node.y = (root.y - node.y) * size[1];
paulo@89 7211 } : function(node) {
paulo@89 7212 node.x = (node.x - x0) / (x1 - x0) * size[0];
paulo@89 7213 node.y = (1 - (root.y ? node.y / root.y : 1)) * size[1];
paulo@89 7214 });
paulo@89 7215 return nodes;
paulo@89 7216 }
paulo@89 7217 cluster.separation = function(x) {
paulo@89 7218 if (!arguments.length) return separation;
paulo@89 7219 separation = x;
paulo@89 7220 return cluster;
paulo@89 7221 };
paulo@89 7222 cluster.size = function(x) {
paulo@89 7223 if (!arguments.length) return nodeSize ? null : size;
paulo@89 7224 nodeSize = (size = x) == null;
paulo@89 7225 return cluster;
paulo@89 7226 };
paulo@89 7227 cluster.nodeSize = function(x) {
paulo@89 7228 if (!arguments.length) return nodeSize ? size : null;
paulo@89 7229 nodeSize = (size = x) != null;
paulo@89 7230 return cluster;
paulo@89 7231 };
paulo@89 7232 return d3_layout_hierarchyRebind(cluster, hierarchy);
paulo@89 7233 };
paulo@89 7234 function d3_layout_clusterY(children) {
paulo@89 7235 return 1 + d3.max(children, function(child) {
paulo@89 7236 return child.y;
paulo@89 7237 });
paulo@89 7238 }
paulo@89 7239 function d3_layout_clusterX(children) {
paulo@89 7240 return children.reduce(function(x, child) {
paulo@89 7241 return x + child.x;
paulo@89 7242 }, 0) / children.length;
paulo@89 7243 }
paulo@89 7244 function d3_layout_clusterLeft(node) {
paulo@89 7245 var children = node.children;
paulo@89 7246 return children && children.length ? d3_layout_clusterLeft(children[0]) : node;
paulo@89 7247 }
paulo@89 7248 function d3_layout_clusterRight(node) {
paulo@89 7249 var children = node.children, n;
paulo@89 7250 return children && (n = children.length) ? d3_layout_clusterRight(children[n - 1]) : node;
paulo@89 7251 }
paulo@89 7252 d3.layout.treemap = function() {
paulo@89 7253 var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [ 1, 1 ], padding = null, pad = d3_layout_treemapPadNull, sticky = false, stickies, mode = "squarify", ratio = .5 * (1 + Math.sqrt(5));
paulo@89 7254 function scale(children, k) {
paulo@89 7255 var i = -1, n = children.length, child, area;
paulo@89 7256 while (++i < n) {
paulo@89 7257 area = (child = children[i]).value * (k < 0 ? 0 : k);
paulo@89 7258 child.area = isNaN(area) || area <= 0 ? 0 : area;
paulo@89 7259 }
paulo@89 7260 }
paulo@89 7261 function squarify(node) {
paulo@89 7262 var children = node.children;
paulo@89 7263 if (children && children.length) {
paulo@89 7264 var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = mode === "slice" ? rect.dx : mode === "dice" ? rect.dy : mode === "slice-dice" ? node.depth & 1 ? rect.dy : rect.dx : Math.min(rect.dx, rect.dy), n;
paulo@89 7265 scale(remaining, rect.dx * rect.dy / node.value);
paulo@89 7266 row.area = 0;
paulo@89 7267 while ((n = remaining.length) > 0) {
paulo@89 7268 row.push(child = remaining[n - 1]);
paulo@89 7269 row.area += child.area;
paulo@89 7270 if (mode !== "squarify" || (score = worst(row, u)) <= best) {
paulo@89 7271 remaining.pop();
paulo@89 7272 best = score;
paulo@89 7273 } else {
paulo@89 7274 row.area -= row.pop().area;
paulo@89 7275 position(row, u, rect, false);
paulo@89 7276 u = Math.min(rect.dx, rect.dy);
paulo@89 7277 row.length = row.area = 0;
paulo@89 7278 best = Infinity;
paulo@89 7279 }
paulo@89 7280 }
paulo@89 7281 if (row.length) {
paulo@89 7282 position(row, u, rect, true);
paulo@89 7283 row.length = row.area = 0;
paulo@89 7284 }
paulo@89 7285 children.forEach(squarify);
paulo@89 7286 }
paulo@89 7287 }
paulo@89 7288 function stickify(node) {
paulo@89 7289 var children = node.children;
paulo@89 7290 if (children && children.length) {
paulo@89 7291 var rect = pad(node), remaining = children.slice(), child, row = [];
paulo@89 7292 scale(remaining, rect.dx * rect.dy / node.value);
paulo@89 7293 row.area = 0;
paulo@89 7294 while (child = remaining.pop()) {
paulo@89 7295 row.push(child);
paulo@89 7296 row.area += child.area;
paulo@89 7297 if (child.z != null) {
paulo@89 7298 position(row, child.z ? rect.dx : rect.dy, rect, !remaining.length);
paulo@89 7299 row.length = row.area = 0;
paulo@89 7300 }
paulo@89 7301 }
paulo@89 7302 children.forEach(stickify);
paulo@89 7303 }
paulo@89 7304 }
paulo@89 7305 function worst(row, u) {
paulo@89 7306 var s = row.area, r, rmax = 0, rmin = Infinity, i = -1, n = row.length;
paulo@89 7307 while (++i < n) {
paulo@89 7308 if (!(r = row[i].area)) continue;
paulo@89 7309 if (r < rmin) rmin = r;
paulo@89 7310 if (r > rmax) rmax = r;
paulo@89 7311 }
paulo@89 7312 s *= s;
paulo@89 7313 u *= u;
paulo@89 7314 return s ? Math.max(u * rmax * ratio / s, s / (u * rmin * ratio)) : Infinity;
paulo@89 7315 }
paulo@89 7316 function position(row, u, rect, flush) {
paulo@89 7317 var i = -1, n = row.length, x = rect.x, y = rect.y, v = u ? round(row.area / u) : 0, o;
paulo@89 7318 if (u == rect.dx) {
paulo@89 7319 if (flush || v > rect.dy) v = rect.dy;
paulo@89 7320 while (++i < n) {
paulo@89 7321 o = row[i];
paulo@89 7322 o.x = x;
paulo@89 7323 o.y = y;
paulo@89 7324 o.dy = v;
paulo@89 7325 x += o.dx = Math.min(rect.x + rect.dx - x, v ? round(o.area / v) : 0);
paulo@89 7326 }
paulo@89 7327 o.z = true;
paulo@89 7328 o.dx += rect.x + rect.dx - x;
paulo@89 7329 rect.y += v;
paulo@89 7330 rect.dy -= v;
paulo@89 7331 } else {
paulo@89 7332 if (flush || v > rect.dx) v = rect.dx;
paulo@89 7333 while (++i < n) {
paulo@89 7334 o = row[i];
paulo@89 7335 o.x = x;
paulo@89 7336 o.y = y;
paulo@89 7337 o.dx = v;
paulo@89 7338 y += o.dy = Math.min(rect.y + rect.dy - y, v ? round(o.area / v) : 0);
paulo@89 7339 }
paulo@89 7340 o.z = false;
paulo@89 7341 o.dy += rect.y + rect.dy - y;
paulo@89 7342 rect.x += v;
paulo@89 7343 rect.dx -= v;
paulo@89 7344 }
paulo@89 7345 }
paulo@89 7346 function treemap(d) {
paulo@89 7347 var nodes = stickies || hierarchy(d), root = nodes[0];
paulo@89 7348 root.x = root.y = 0;
paulo@89 7349 if (root.value) root.dx = size[0], root.dy = size[1]; else root.dx = root.dy = 0;
paulo@89 7350 if (stickies) hierarchy.revalue(root);
paulo@89 7351 scale([ root ], root.dx * root.dy / root.value);
paulo@89 7352 (stickies ? stickify : squarify)(root);
paulo@89 7353 if (sticky) stickies = nodes;
paulo@89 7354 return nodes;
paulo@89 7355 }
paulo@89 7356 treemap.size = function(x) {
paulo@89 7357 if (!arguments.length) return size;
paulo@89 7358 size = x;
paulo@89 7359 return treemap;
paulo@89 7360 };
paulo@89 7361 treemap.padding = function(x) {
paulo@89 7362 if (!arguments.length) return padding;
paulo@89 7363 function padFunction(node) {
paulo@89 7364 var p = x.call(treemap, node, node.depth);
paulo@89 7365 return p == null ? d3_layout_treemapPadNull(node) : d3_layout_treemapPad(node, typeof p === "number" ? [ p, p, p, p ] : p);
paulo@89 7366 }
paulo@89 7367 function padConstant(node) {
paulo@89 7368 return d3_layout_treemapPad(node, x);
paulo@89 7369 }
paulo@89 7370 var type;
paulo@89 7371 pad = (padding = x) == null ? d3_layout_treemapPadNull : (type = typeof x) === "function" ? padFunction : type === "number" ? (x = [ x, x, x, x ],
paulo@89 7372 padConstant) : padConstant;
paulo@89 7373 return treemap;
paulo@89 7374 };
paulo@89 7375 treemap.round = function(x) {
paulo@89 7376 if (!arguments.length) return round != Number;
paulo@89 7377 round = x ? Math.round : Number;
paulo@89 7378 return treemap;
paulo@89 7379 };
paulo@89 7380 treemap.sticky = function(x) {
paulo@89 7381 if (!arguments.length) return sticky;
paulo@89 7382 sticky = x;
paulo@89 7383 stickies = null;
paulo@89 7384 return treemap;
paulo@89 7385 };
paulo@89 7386 treemap.ratio = function(x) {
paulo@89 7387 if (!arguments.length) return ratio;
paulo@89 7388 ratio = x;
paulo@89 7389 return treemap;
paulo@89 7390 };
paulo@89 7391 treemap.mode = function(x) {
paulo@89 7392 if (!arguments.length) return mode;
paulo@89 7393 mode = x + "";
paulo@89 7394 return treemap;
paulo@89 7395 };
paulo@89 7396 return d3_layout_hierarchyRebind(treemap, hierarchy);
paulo@89 7397 };
paulo@89 7398 function d3_layout_treemapPadNull(node) {
paulo@89 7399 return {
paulo@89 7400 x: node.x,
paulo@89 7401 y: node.y,
paulo@89 7402 dx: node.dx,
paulo@89 7403 dy: node.dy
paulo@89 7404 };
paulo@89 7405 }
paulo@89 7406 function d3_layout_treemapPad(node, padding) {
paulo@89 7407 var x = node.x + padding[3], y = node.y + padding[0], dx = node.dx - padding[1] - padding[3], dy = node.dy - padding[0] - padding[2];
paulo@89 7408 if (dx < 0) {
paulo@89 7409 x += dx / 2;
paulo@89 7410 dx = 0;
paulo@89 7411 }
paulo@89 7412 if (dy < 0) {
paulo@89 7413 y += dy / 2;
paulo@89 7414 dy = 0;
paulo@89 7415 }
paulo@89 7416 return {
paulo@89 7417 x: x,
paulo@89 7418 y: y,
paulo@89 7419 dx: dx,
paulo@89 7420 dy: dy
paulo@89 7421 };
paulo@89 7422 }
paulo@89 7423 d3.random = {
paulo@89 7424 normal: function(µ, σ) {
paulo@89 7425 var n = arguments.length;
paulo@89 7426 if (n < 2) σ = 1;
paulo@89 7427 if (n < 1) µ = 0;
paulo@89 7428 return function() {
paulo@89 7429 var x, y, r;
paulo@89 7430 do {
paulo@89 7431 x = Math.random() * 2 - 1;
paulo@89 7432 y = Math.random() * 2 - 1;
paulo@89 7433 r = x * x + y * y;
paulo@89 7434 } while (!r || r > 1);
paulo@89 7435 return µ + σ * x * Math.sqrt(-2 * Math.log(r) / r);
paulo@89 7436 };
paulo@89 7437 },
paulo@89 7438 logNormal: function() {
paulo@89 7439 var random = d3.random.normal.apply(d3, arguments);
paulo@89 7440 return function() {
paulo@89 7441 return Math.exp(random());
paulo@89 7442 };
paulo@89 7443 },
paulo@89 7444 bates: function(m) {
paulo@89 7445 var random = d3.random.irwinHall(m);
paulo@89 7446 return function() {
paulo@89 7447 return random() / m;
paulo@89 7448 };
paulo@89 7449 },
paulo@89 7450 irwinHall: function(m) {
paulo@89 7451 return function() {
paulo@89 7452 for (var s = 0, j = 0; j < m; j++) s += Math.random();
paulo@89 7453 return s;
paulo@89 7454 };
paulo@89 7455 }
paulo@89 7456 };
paulo@89 7457 d3.scale = {};
paulo@89 7458 function d3_scaleExtent(domain) {
paulo@89 7459 var start = domain[0], stop = domain[domain.length - 1];
paulo@89 7460 return start < stop ? [ start, stop ] : [ stop, start ];
paulo@89 7461 }
paulo@89 7462 function d3_scaleRange(scale) {
paulo@89 7463 return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range());
paulo@89 7464 }
paulo@89 7465 function d3_scale_bilinear(domain, range, uninterpolate, interpolate) {
paulo@89 7466 var u = uninterpolate(domain[0], domain[1]), i = interpolate(range[0], range[1]);
paulo@89 7467 return function(x) {
paulo@89 7468 return i(u(x));
paulo@89 7469 };
paulo@89 7470 }
paulo@89 7471 function d3_scale_nice(domain, nice) {
paulo@89 7472 var i0 = 0, i1 = domain.length - 1, x0 = domain[i0], x1 = domain[i1], dx;
paulo@89 7473 if (x1 < x0) {
paulo@89 7474 dx = i0, i0 = i1, i1 = dx;
paulo@89 7475 dx = x0, x0 = x1, x1 = dx;
paulo@89 7476 }
paulo@89 7477 domain[i0] = nice.floor(x0);
paulo@89 7478 domain[i1] = nice.ceil(x1);
paulo@89 7479 return domain;
paulo@89 7480 }
paulo@89 7481 function d3_scale_niceStep(step) {
paulo@89 7482 return step ? {
paulo@89 7483 floor: function(x) {
paulo@89 7484 return Math.floor(x / step) * step;
paulo@89 7485 },
paulo@89 7486 ceil: function(x) {
paulo@89 7487 return Math.ceil(x / step) * step;
paulo@89 7488 }
paulo@89 7489 } : d3_scale_niceIdentity;
paulo@89 7490 }
paulo@89 7491 var d3_scale_niceIdentity = {
paulo@89 7492 floor: d3_identity,
paulo@89 7493 ceil: d3_identity
paulo@89 7494 };
paulo@89 7495 function d3_scale_polylinear(domain, range, uninterpolate, interpolate) {
paulo@89 7496 var u = [], i = [], j = 0, k = Math.min(domain.length, range.length) - 1;
paulo@89 7497 if (domain[k] < domain[0]) {
paulo@89 7498 domain = domain.slice().reverse();
paulo@89 7499 range = range.slice().reverse();
paulo@89 7500 }
paulo@89 7501 while (++j <= k) {
paulo@89 7502 u.push(uninterpolate(domain[j - 1], domain[j]));
paulo@89 7503 i.push(interpolate(range[j - 1], range[j]));
paulo@89 7504 }
paulo@89 7505 return function(x) {
paulo@89 7506 var j = d3.bisect(domain, x, 1, k) - 1;
paulo@89 7507 return i[j](u[j](x));
paulo@89 7508 };
paulo@89 7509 }
paulo@89 7510 d3.scale.linear = function() {
paulo@89 7511 return d3_scale_linear([ 0, 1 ], [ 0, 1 ], d3_interpolate, false);
paulo@89 7512 };
paulo@89 7513 function d3_scale_linear(domain, range, interpolate, clamp) {
paulo@89 7514 var output, input;
paulo@89 7515 function rescale() {
paulo@89 7516 var linear = Math.min(domain.length, range.length) > 2 ? d3_scale_polylinear : d3_scale_bilinear, uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber;
paulo@89 7517 output = linear(domain, range, uninterpolate, interpolate);
paulo@89 7518 input = linear(range, domain, uninterpolate, d3_interpolate);
paulo@89 7519 return scale;
paulo@89 7520 }
paulo@89 7521 function scale(x) {
paulo@89 7522 return output(x);
paulo@89 7523 }
paulo@89 7524 scale.invert = function(y) {
paulo@89 7525 return input(y);
paulo@89 7526 };
paulo@89 7527 scale.domain = function(x) {
paulo@89 7528 if (!arguments.length) return domain;
paulo@89 7529 domain = x.map(Number);
paulo@89 7530 return rescale();
paulo@89 7531 };
paulo@89 7532 scale.range = function(x) {
paulo@89 7533 if (!arguments.length) return range;
paulo@89 7534 range = x;
paulo@89 7535 return rescale();
paulo@89 7536 };
paulo@89 7537 scale.rangeRound = function(x) {
paulo@89 7538 return scale.range(x).interpolate(d3_interpolateRound);
paulo@89 7539 };
paulo@89 7540 scale.clamp = function(x) {
paulo@89 7541 if (!arguments.length) return clamp;
paulo@89 7542 clamp = x;
paulo@89 7543 return rescale();
paulo@89 7544 };
paulo@89 7545 scale.interpolate = function(x) {
paulo@89 7546 if (!arguments.length) return interpolate;
paulo@89 7547 interpolate = x;
paulo@89 7548 return rescale();
paulo@89 7549 };
paulo@89 7550 scale.ticks = function(m) {
paulo@89 7551 return d3_scale_linearTicks(domain, m);
paulo@89 7552 };
paulo@89 7553 scale.tickFormat = function(m, format) {
paulo@89 7554 return d3_scale_linearTickFormat(domain, m, format);
paulo@89 7555 };
paulo@89 7556 scale.nice = function(m) {
paulo@89 7557 d3_scale_linearNice(domain, m);
paulo@89 7558 return rescale();
paulo@89 7559 };
paulo@89 7560 scale.copy = function() {
paulo@89 7561 return d3_scale_linear(domain, range, interpolate, clamp);
paulo@89 7562 };
paulo@89 7563 return rescale();
paulo@89 7564 }
paulo@89 7565 function d3_scale_linearRebind(scale, linear) {
paulo@89 7566 return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
paulo@89 7567 }
paulo@89 7568 function d3_scale_linearNice(domain, m) {
paulo@89 7569 d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2]));
paulo@89 7570 d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2]));
paulo@89 7571 return domain;
paulo@89 7572 }
paulo@89 7573 function d3_scale_linearTickRange(domain, m) {
paulo@89 7574 if (m == null) m = 10;
paulo@89 7575 var extent = d3_scaleExtent(domain), span = extent[1] - extent[0], step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)), err = m / span * step;
paulo@89 7576 if (err <= .15) step *= 10; else if (err <= .35) step *= 5; else if (err <= .75) step *= 2;
paulo@89 7577 extent[0] = Math.ceil(extent[0] / step) * step;
paulo@89 7578 extent[1] = Math.floor(extent[1] / step) * step + step * .5;
paulo@89 7579 extent[2] = step;
paulo@89 7580 return extent;
paulo@89 7581 }
paulo@89 7582 function d3_scale_linearTicks(domain, m) {
paulo@89 7583 return d3.range.apply(d3, d3_scale_linearTickRange(domain, m));
paulo@89 7584 }
paulo@89 7585 function d3_scale_linearTickFormat(domain, m, format) {
paulo@89 7586 var range = d3_scale_linearTickRange(domain, m);
paulo@89 7587 if (format) {
paulo@89 7588 var match = d3_format_re.exec(format);
paulo@89 7589 match.shift();
paulo@89 7590 if (match[8] === "s") {
paulo@89 7591 var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1])));
paulo@89 7592 if (!match[7]) match[7] = "." + d3_scale_linearPrecision(prefix.scale(range[2]));
paulo@89 7593 match[8] = "f";
paulo@89 7594 format = d3.format(match.join(""));
paulo@89 7595 return function(d) {
paulo@89 7596 return format(prefix.scale(d)) + prefix.symbol;
paulo@89 7597 };
paulo@89 7598 }
paulo@89 7599 if (!match[7]) match[7] = "." + d3_scale_linearFormatPrecision(match[8], range);
paulo@89 7600 format = match.join("");
paulo@89 7601 } else {
paulo@89 7602 format = ",." + d3_scale_linearPrecision(range[2]) + "f";
paulo@89 7603 }
paulo@89 7604 return d3.format(format);
paulo@89 7605 }
paulo@89 7606 var d3_scale_linearFormatSignificant = {
paulo@89 7607 s: 1,
paulo@89 7608 g: 1,
paulo@89 7609 p: 1,
paulo@89 7610 r: 1,
paulo@89 7611 e: 1
paulo@89 7612 };
paulo@89 7613 function d3_scale_linearPrecision(value) {
paulo@89 7614 return -Math.floor(Math.log(value) / Math.LN10 + .01);
paulo@89 7615 }
paulo@89 7616 function d3_scale_linearFormatPrecision(type, range) {
paulo@89 7617 var p = d3_scale_linearPrecision(range[2]);
paulo@89 7618 return type in d3_scale_linearFormatSignificant ? Math.abs(p - d3_scale_linearPrecision(Math.max(abs(range[0]), abs(range[1])))) + +(type !== "e") : p - (type === "%") * 2;
paulo@89 7619 }
paulo@89 7620 d3.scale.log = function() {
paulo@89 7621 return d3_scale_log(d3.scale.linear().domain([ 0, 1 ]), 10, true, [ 1, 10 ]);
paulo@89 7622 };
paulo@89 7623 function d3_scale_log(linear, base, positive, domain) {
paulo@89 7624 function log(x) {
paulo@89 7625 return (positive ? Math.log(x < 0 ? 0 : x) : -Math.log(x > 0 ? 0 : -x)) / Math.log(base);
paulo@89 7626 }
paulo@89 7627 function pow(x) {
paulo@89 7628 return positive ? Math.pow(base, x) : -Math.pow(base, -x);
paulo@89 7629 }
paulo@89 7630 function scale(x) {
paulo@89 7631 return linear(log(x));
paulo@89 7632 }
paulo@89 7633 scale.invert = function(x) {
paulo@89 7634 return pow(linear.invert(x));
paulo@89 7635 };
paulo@89 7636 scale.domain = function(x) {
paulo@89 7637 if (!arguments.length) return domain;
paulo@89 7638 positive = x[0] >= 0;
paulo@89 7639 linear.domain((domain = x.map(Number)).map(log));
paulo@89 7640 return scale;
paulo@89 7641 };
paulo@89 7642 scale.base = function(_) {
paulo@89 7643 if (!arguments.length) return base;
paulo@89 7644 base = +_;
paulo@89 7645 linear.domain(domain.map(log));
paulo@89 7646 return scale;
paulo@89 7647 };
paulo@89 7648 scale.nice = function() {
paulo@89 7649 var niced = d3_scale_nice(domain.map(log), positive ? Math : d3_scale_logNiceNegative);
paulo@89 7650 linear.domain(niced);
paulo@89 7651 domain = niced.map(pow);
paulo@89 7652 return scale;
paulo@89 7653 };
paulo@89 7654 scale.ticks = function() {
paulo@89 7655 var extent = d3_scaleExtent(domain), ticks = [], u = extent[0], v = extent[1], i = Math.floor(log(u)), j = Math.ceil(log(v)), n = base % 1 ? 2 : base;
paulo@89 7656 if (isFinite(j - i)) {
paulo@89 7657 if (positive) {
paulo@89 7658 for (;i < j; i++) for (var k = 1; k < n; k++) ticks.push(pow(i) * k);
paulo@89 7659 ticks.push(pow(i));
paulo@89 7660 } else {
paulo@89 7661 ticks.push(pow(i));
paulo@89 7662 for (;i++ < j; ) for (var k = n - 1; k > 0; k--) ticks.push(pow(i) * k);
paulo@89 7663 }
paulo@89 7664 for (i = 0; ticks[i] < u; i++) {}
paulo@89 7665 for (j = ticks.length; ticks[j - 1] > v; j--) {}
paulo@89 7666 ticks = ticks.slice(i, j);
paulo@89 7667 }
paulo@89 7668 return ticks;
paulo@89 7669 };
paulo@89 7670 scale.tickFormat = function(n, format) {
paulo@89 7671 if (!arguments.length) return d3_scale_logFormat;
paulo@89 7672 if (arguments.length < 2) format = d3_scale_logFormat; else if (typeof format !== "function") format = d3.format(format);
paulo@89 7673 var k = Math.max(1, base * n / scale.ticks().length);
paulo@89 7674 return function(d) {
paulo@89 7675 var i = d / pow(Math.round(log(d)));
paulo@89 7676 if (i * base < base - .5) i *= base;
paulo@89 7677 return i <= k ? format(d) : "";
paulo@89 7678 };
paulo@89 7679 };
paulo@89 7680 scale.copy = function() {
paulo@89 7681 return d3_scale_log(linear.copy(), base, positive, domain);
paulo@89 7682 };
paulo@89 7683 return d3_scale_linearRebind(scale, linear);
paulo@89 7684 }
paulo@89 7685 var d3_scale_logFormat = d3.format(".0e"), d3_scale_logNiceNegative = {
paulo@89 7686 floor: function(x) {
paulo@89 7687 return -Math.ceil(-x);
paulo@89 7688 },
paulo@89 7689 ceil: function(x) {
paulo@89 7690 return -Math.floor(-x);
paulo@89 7691 }
paulo@89 7692 };
paulo@89 7693 d3.scale.pow = function() {
paulo@89 7694 return d3_scale_pow(d3.scale.linear(), 1, [ 0, 1 ]);
paulo@89 7695 };
paulo@89 7696 function d3_scale_pow(linear, exponent, domain) {
paulo@89 7697 var powp = d3_scale_powPow(exponent), powb = d3_scale_powPow(1 / exponent);
paulo@89 7698 function scale(x) {
paulo@89 7699 return linear(powp(x));
paulo@89 7700 }
paulo@89 7701 scale.invert = function(x) {
paulo@89 7702 return powb(linear.invert(x));
paulo@89 7703 };
paulo@89 7704 scale.domain = function(x) {
paulo@89 7705 if (!arguments.length) return domain;
paulo@89 7706 linear.domain((domain = x.map(Number)).map(powp));
paulo@89 7707 return scale;
paulo@89 7708 };
paulo@89 7709 scale.ticks = function(m) {
paulo@89 7710 return d3_scale_linearTicks(domain, m);
paulo@89 7711 };
paulo@89 7712 scale.tickFormat = function(m, format) {
paulo@89 7713 return d3_scale_linearTickFormat(domain, m, format);
paulo@89 7714 };
paulo@89 7715 scale.nice = function(m) {
paulo@89 7716 return scale.domain(d3_scale_linearNice(domain, m));
paulo@89 7717 };
paulo@89 7718 scale.exponent = function(x) {
paulo@89 7719 if (!arguments.length) return exponent;
paulo@89 7720 powp = d3_scale_powPow(exponent = x);
paulo@89 7721 powb = d3_scale_powPow(1 / exponent);
paulo@89 7722 linear.domain(domain.map(powp));
paulo@89 7723 return scale;
paulo@89 7724 };
paulo@89 7725 scale.copy = function() {
paulo@89 7726 return d3_scale_pow(linear.copy(), exponent, domain);
paulo@89 7727 };
paulo@89 7728 return d3_scale_linearRebind(scale, linear);
paulo@89 7729 }
paulo@89 7730 function d3_scale_powPow(e) {
paulo@89 7731 return function(x) {
paulo@89 7732 return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e);
paulo@89 7733 };
paulo@89 7734 }
paulo@89 7735 d3.scale.sqrt = function() {
paulo@89 7736 return d3.scale.pow().exponent(.5);
paulo@89 7737 };
paulo@89 7738 d3.scale.ordinal = function() {
paulo@89 7739 return d3_scale_ordinal([], {
paulo@89 7740 t: "range",
paulo@89 7741 a: [ [] ]
paulo@89 7742 });
paulo@89 7743 };
paulo@89 7744 function d3_scale_ordinal(domain, ranger) {
paulo@89 7745 var index, range, rangeBand;
paulo@89 7746 function scale(x) {
paulo@89 7747 return range[((index.get(x) || (ranger.t === "range" ? index.set(x, domain.push(x)) : NaN)) - 1) % range.length];
paulo@89 7748 }
paulo@89 7749 function steps(start, step) {
paulo@89 7750 return d3.range(domain.length).map(function(i) {
paulo@89 7751 return start + step * i;
paulo@89 7752 });
paulo@89 7753 }
paulo@89 7754 scale.domain = function(x) {
paulo@89 7755 if (!arguments.length) return domain;
paulo@89 7756 domain = [];
paulo@89 7757 index = new d3_Map();
paulo@89 7758 var i = -1, n = x.length, xi;
paulo@89 7759 while (++i < n) if (!index.has(xi = x[i])) index.set(xi, domain.push(xi));
paulo@89 7760 return scale[ranger.t].apply(scale, ranger.a);
paulo@89 7761 };
paulo@89 7762 scale.range = function(x) {
paulo@89 7763 if (!arguments.length) return range;
paulo@89 7764 range = x;
paulo@89 7765 rangeBand = 0;
paulo@89 7766 ranger = {
paulo@89 7767 t: "range",
paulo@89 7768 a: arguments
paulo@89 7769 };
paulo@89 7770 return scale;
paulo@89 7771 };
paulo@89 7772 scale.rangePoints = function(x, padding) {
paulo@89 7773 if (arguments.length < 2) padding = 0;
paulo@89 7774 var start = x[0], stop = x[1], step = domain.length < 2 ? (start = (start + stop) / 2,
paulo@89 7775 0) : (stop - start) / (domain.length - 1 + padding);
paulo@89 7776 range = steps(start + step * padding / 2, step);
paulo@89 7777 rangeBand = 0;
paulo@89 7778 ranger = {
paulo@89 7779 t: "rangePoints",
paulo@89 7780 a: arguments
paulo@89 7781 };
paulo@89 7782 return scale;
paulo@89 7783 };
paulo@89 7784 scale.rangeRoundPoints = function(x, padding) {
paulo@89 7785 if (arguments.length < 2) padding = 0;
paulo@89 7786 var start = x[0], stop = x[1], step = domain.length < 2 ? (start = stop = Math.round((start + stop) / 2),
paulo@89 7787 0) : (stop - start) / (domain.length - 1 + padding) | 0;
paulo@89 7788 range = steps(start + Math.round(step * padding / 2 + (stop - start - (domain.length - 1 + padding) * step) / 2), step);
paulo@89 7789 rangeBand = 0;
paulo@89 7790 ranger = {
paulo@89 7791 t: "rangeRoundPoints",
paulo@89 7792 a: arguments
paulo@89 7793 };
paulo@89 7794 return scale;
paulo@89 7795 };
paulo@89 7796 scale.rangeBands = function(x, padding, outerPadding) {
paulo@89 7797 if (arguments.length < 2) padding = 0;
paulo@89 7798 if (arguments.length < 3) outerPadding = padding;
paulo@89 7799 var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = (stop - start) / (domain.length - padding + 2 * outerPadding);
paulo@89 7800 range = steps(start + step * outerPadding, step);
paulo@89 7801 if (reverse) range.reverse();
paulo@89 7802 rangeBand = step * (1 - padding);
paulo@89 7803 ranger = {
paulo@89 7804 t: "rangeBands",
paulo@89 7805 a: arguments
paulo@89 7806 };
paulo@89 7807 return scale;
paulo@89 7808 };
paulo@89 7809 scale.rangeRoundBands = function(x, padding, outerPadding) {
paulo@89 7810 if (arguments.length < 2) padding = 0;
paulo@89 7811 if (arguments.length < 3) outerPadding = padding;
paulo@89 7812 var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = Math.floor((stop - start) / (domain.length - padding + 2 * outerPadding));
paulo@89 7813 range = steps(start + Math.round((stop - start - (domain.length - padding) * step) / 2), step);
paulo@89 7814 if (reverse) range.reverse();
paulo@89 7815 rangeBand = Math.round(step * (1 - padding));
paulo@89 7816 ranger = {
paulo@89 7817 t: "rangeRoundBands",
paulo@89 7818 a: arguments
paulo@89 7819 };
paulo@89 7820 return scale;
paulo@89 7821 };
paulo@89 7822 scale.rangeBand = function() {
paulo@89 7823 return rangeBand;
paulo@89 7824 };
paulo@89 7825 scale.rangeExtent = function() {
paulo@89 7826 return d3_scaleExtent(ranger.a[0]);
paulo@89 7827 };
paulo@89 7828 scale.copy = function() {
paulo@89 7829 return d3_scale_ordinal(domain, ranger);
paulo@89 7830 };
paulo@89 7831 return scale.domain(domain);
paulo@89 7832 }
paulo@89 7833 d3.scale.category10 = function() {
paulo@89 7834 return d3.scale.ordinal().range(d3_category10);
paulo@89 7835 };
paulo@89 7836 d3.scale.category20 = function() {
paulo@89 7837 return d3.scale.ordinal().range(d3_category20);
paulo@89 7838 };
paulo@89 7839 d3.scale.category20b = function() {
paulo@89 7840 return d3.scale.ordinal().range(d3_category20b);
paulo@89 7841 };
paulo@89 7842 d3.scale.category20c = function() {
paulo@89 7843 return d3.scale.ordinal().range(d3_category20c);
paulo@89 7844 };
paulo@89 7845 var d3_category10 = [ 2062260, 16744206, 2924588, 14034728, 9725885, 9197131, 14907330, 8355711, 12369186, 1556175 ].map(d3_rgbString);
paulo@89 7846 var d3_category20 = [ 2062260, 11454440, 16744206, 16759672, 2924588, 10018698, 14034728, 16750742, 9725885, 12955861, 9197131, 12885140, 14907330, 16234194, 8355711, 13092807, 12369186, 14408589, 1556175, 10410725 ].map(d3_rgbString);
paulo@89 7847 var d3_category20b = [ 3750777, 5395619, 7040719, 10264286, 6519097, 9216594, 11915115, 13556636, 9202993, 12426809, 15186514, 15190932, 8666169, 11356490, 14049643, 15177372, 8077683, 10834324, 13528509, 14589654 ].map(d3_rgbString);
paulo@89 7848 var d3_category20c = [ 3244733, 7057110, 10406625, 13032431, 15095053, 16616764, 16625259, 16634018, 3253076, 7652470, 10607003, 13101504, 7695281, 10394312, 12369372, 14342891, 6513507, 9868950, 12434877, 14277081 ].map(d3_rgbString);
paulo@89 7849 d3.scale.quantile = function() {
paulo@89 7850 return d3_scale_quantile([], []);
paulo@89 7851 };
paulo@89 7852 function d3_scale_quantile(domain, range) {
paulo@89 7853 var thresholds;
paulo@89 7854 function rescale() {
paulo@89 7855 var k = 0, q = range.length;
paulo@89 7856 thresholds = [];
paulo@89 7857 while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q);
paulo@89 7858 return scale;
paulo@89 7859 }
paulo@89 7860 function scale(x) {
paulo@89 7861 if (!isNaN(x = +x)) return range[d3.bisect(thresholds, x)];
paulo@89 7862 }
paulo@89 7863 scale.domain = function(x) {
paulo@89 7864 if (!arguments.length) return domain;
paulo@89 7865 domain = x.map(d3_number).filter(d3_numeric).sort(d3_ascending);
paulo@89 7866 return rescale();
paulo@89 7867 };
paulo@89 7868 scale.range = function(x) {
paulo@89 7869 if (!arguments.length) return range;
paulo@89 7870 range = x;
paulo@89 7871 return rescale();
paulo@89 7872 };
paulo@89 7873 scale.quantiles = function() {
paulo@89 7874 return thresholds;
paulo@89 7875 };
paulo@89 7876 scale.invertExtent = function(y) {
paulo@89 7877 y = range.indexOf(y);
paulo@89 7878 return y < 0 ? [ NaN, NaN ] : [ y > 0 ? thresholds[y - 1] : domain[0], y < thresholds.length ? thresholds[y] : domain[domain.length - 1] ];
paulo@89 7879 };
paulo@89 7880 scale.copy = function() {
paulo@89 7881 return d3_scale_quantile(domain, range);
paulo@89 7882 };
paulo@89 7883 return rescale();
paulo@89 7884 }
paulo@89 7885 d3.scale.quantize = function() {
paulo@89 7886 return d3_scale_quantize(0, 1, [ 0, 1 ]);
paulo@89 7887 };
paulo@89 7888 function d3_scale_quantize(x0, x1, range) {
paulo@89 7889 var kx, i;
paulo@89 7890 function scale(x) {
paulo@89 7891 return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))];
paulo@89 7892 }
paulo@89 7893 function rescale() {
paulo@89 7894 kx = range.length / (x1 - x0);
paulo@89 7895 i = range.length - 1;
paulo@89 7896 return scale;
paulo@89 7897 }
paulo@89 7898 scale.domain = function(x) {
paulo@89 7899 if (!arguments.length) return [ x0, x1 ];
paulo@89 7900 x0 = +x[0];
paulo@89 7901 x1 = +x[x.length - 1];
paulo@89 7902 return rescale();
paulo@89 7903 };
paulo@89 7904 scale.range = function(x) {
paulo@89 7905 if (!arguments.length) return range;
paulo@89 7906 range = x;
paulo@89 7907 return rescale();
paulo@89 7908 };
paulo@89 7909 scale.invertExtent = function(y) {
paulo@89 7910 y = range.indexOf(y);
paulo@89 7911 y = y < 0 ? NaN : y / kx + x0;
paulo@89 7912 return [ y, y + 1 / kx ];
paulo@89 7913 };
paulo@89 7914 scale.copy = function() {
paulo@89 7915 return d3_scale_quantize(x0, x1, range);
paulo@89 7916 };
paulo@89 7917 return rescale();
paulo@89 7918 }
paulo@89 7919 d3.scale.threshold = function() {
paulo@89 7920 return d3_scale_threshold([ .5 ], [ 0, 1 ]);
paulo@89 7921 };
paulo@89 7922 function d3_scale_threshold(domain, range) {
paulo@89 7923 function scale(x) {
paulo@89 7924 if (x <= x) return range[d3.bisect(domain, x)];
paulo@89 7925 }
paulo@89 7926 scale.domain = function(_) {
paulo@89 7927 if (!arguments.length) return domain;
paulo@89 7928 domain = _;
paulo@89 7929 return scale;
paulo@89 7930 };
paulo@89 7931 scale.range = function(_) {
paulo@89 7932 if (!arguments.length) return range;
paulo@89 7933 range = _;
paulo@89 7934 return scale;
paulo@89 7935 };
paulo@89 7936 scale.invertExtent = function(y) {
paulo@89 7937 y = range.indexOf(y);
paulo@89 7938 return [ domain[y - 1], domain[y] ];
paulo@89 7939 };
paulo@89 7940 scale.copy = function() {
paulo@89 7941 return d3_scale_threshold(domain, range);
paulo@89 7942 };
paulo@89 7943 return scale;
paulo@89 7944 }
paulo@89 7945 d3.scale.identity = function() {
paulo@89 7946 return d3_scale_identity([ 0, 1 ]);
paulo@89 7947 };
paulo@89 7948 function d3_scale_identity(domain) {
paulo@89 7949 function identity(x) {
paulo@89 7950 return +x;
paulo@89 7951 }
paulo@89 7952 identity.invert = identity;
paulo@89 7953 identity.domain = identity.range = function(x) {
paulo@89 7954 if (!arguments.length) return domain;
paulo@89 7955 domain = x.map(identity);
paulo@89 7956 return identity;
paulo@89 7957 };
paulo@89 7958 identity.ticks = function(m) {
paulo@89 7959 return d3_scale_linearTicks(domain, m);
paulo@89 7960 };
paulo@89 7961 identity.tickFormat = function(m, format) {
paulo@89 7962 return d3_scale_linearTickFormat(domain, m, format);
paulo@89 7963 };
paulo@89 7964 identity.copy = function() {
paulo@89 7965 return d3_scale_identity(domain);
paulo@89 7966 };
paulo@89 7967 return identity;
paulo@89 7968 }
paulo@89 7969 d3.svg = {};
paulo@89 7970 function d3_zero() {
paulo@89 7971 return 0;
paulo@89 7972 }
paulo@89 7973 d3.svg.arc = function() {
paulo@89 7974 var innerRadius = d3_svg_arcInnerRadius, outerRadius = d3_svg_arcOuterRadius, cornerRadius = d3_zero, padRadius = d3_svg_arcAuto, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle, padAngle = d3_svg_arcPadAngle;
paulo@89 7975 function arc() {
paulo@89 7976 var r0 = Math.max(0, +innerRadius.apply(this, arguments)), r1 = Math.max(0, +outerRadius.apply(this, arguments)), a0 = startAngle.apply(this, arguments) - halfπ, a1 = endAngle.apply(this, arguments) - halfπ, da = Math.abs(a1 - a0), cw = a0 > a1 ? 0 : 1;
paulo@89 7977 if (r1 < r0) rc = r1, r1 = r0, r0 = rc;
paulo@89 7978 if (da >= τε) return circleSegment(r1, cw) + (r0 ? circleSegment(r0, 1 - cw) : "") + "Z";
paulo@89 7979 var rc, cr, rp, ap, p0 = 0, p1 = 0, x0, y0, x1, y1, x2, y2, x3, y3, path = [];
paulo@89 7980 if (ap = (+padAngle.apply(this, arguments) || 0) / 2) {
paulo@89 7981 rp = padRadius === d3_svg_arcAuto ? Math.sqrt(r0 * r0 + r1 * r1) : +padRadius.apply(this, arguments);
paulo@89 7982 if (!cw) p1 *= -1;
paulo@89 7983 if (r1) p1 = d3_asin(rp / r1 * Math.sin(ap));
paulo@89 7984 if (r0) p0 = d3_asin(rp / r0 * Math.sin(ap));
paulo@89 7985 }
paulo@89 7986 if (r1) {
paulo@89 7987 x0 = r1 * Math.cos(a0 + p1);
paulo@89 7988 y0 = r1 * Math.sin(a0 + p1);
paulo@89 7989 x1 = r1 * Math.cos(a1 - p1);
paulo@89 7990 y1 = r1 * Math.sin(a1 - p1);
paulo@89 7991 var l1 = Math.abs(a1 - a0 - 2 * p1) <= π ? 0 : 1;
paulo@89 7992 if (p1 && d3_svg_arcSweep(x0, y0, x1, y1) === cw ^ l1) {
paulo@89 7993 var h1 = (a0 + a1) / 2;
paulo@89 7994 x0 = r1 * Math.cos(h1);
paulo@89 7995 y0 = r1 * Math.sin(h1);
paulo@89 7996 x1 = y1 = null;
paulo@89 7997 }
paulo@89 7998 } else {
paulo@89 7999 x0 = y0 = 0;
paulo@89 8000 }
paulo@89 8001 if (r0) {
paulo@89 8002 x2 = r0 * Math.cos(a1 - p0);
paulo@89 8003 y2 = r0 * Math.sin(a1 - p0);
paulo@89 8004 x3 = r0 * Math.cos(a0 + p0);
paulo@89 8005 y3 = r0 * Math.sin(a0 + p0);
paulo@89 8006 var l0 = Math.abs(a0 - a1 + 2 * p0) <= π ? 0 : 1;
paulo@89 8007 if (p0 && d3_svg_arcSweep(x2, y2, x3, y3) === 1 - cw ^ l0) {
paulo@89 8008 var h0 = (a0 + a1) / 2;
paulo@89 8009 x2 = r0 * Math.cos(h0);
paulo@89 8010 y2 = r0 * Math.sin(h0);
paulo@89 8011 x3 = y3 = null;
paulo@89 8012 }
paulo@89 8013 } else {
paulo@89 8014 x2 = y2 = 0;
paulo@89 8015 }
paulo@89 8016 if (da > ε && (rc = Math.min(Math.abs(r1 - r0) / 2, +cornerRadius.apply(this, arguments))) > .001) {
paulo@89 8017 cr = r0 < r1 ^ cw ? 0 : 1;
paulo@89 8018 var rc1 = rc, rc0 = rc;
paulo@89 8019 if (da < π) {
paulo@89 8020 var oc = x3 == null ? [ x2, y2 ] : x1 == null ? [ x0, y0 ] : d3_geom_polygonIntersect([ x0, y0 ], [ x3, y3 ], [ x1, y1 ], [ x2, y2 ]), ax = x0 - oc[0], ay = y0 - oc[1], bx = x1 - oc[0], by = y1 - oc[1], kc = 1 / Math.sin(Math.acos((ax * bx + ay * by) / (Math.sqrt(ax * ax + ay * ay) * Math.sqrt(bx * bx + by * by))) / 2), lc = Math.sqrt(oc[0] * oc[0] + oc[1] * oc[1]);
paulo@89 8021 rc0 = Math.min(rc, (r0 - lc) / (kc - 1));
paulo@89 8022 rc1 = Math.min(rc, (r1 - lc) / (kc + 1));
paulo@89 8023 }
paulo@89 8024 if (x1 != null) {
paulo@89 8025 var t30 = d3_svg_arcCornerTangents(x3 == null ? [ x2, y2 ] : [ x3, y3 ], [ x0, y0 ], r1, rc1, cw), t12 = d3_svg_arcCornerTangents([ x1, y1 ], [ x2, y2 ], r1, rc1, cw);
paulo@89 8026 if (rc === rc1) {
paulo@89 8027 path.push("M", t30[0], "A", rc1, ",", rc1, " 0 0,", cr, " ", t30[1], "A", r1, ",", r1, " 0 ", 1 - cw ^ d3_svg_arcSweep(t30[1][0], t30[1][1], t12[1][0], t12[1][1]), ",", cw, " ", t12[1], "A", rc1, ",", rc1, " 0 0,", cr, " ", t12[0]);
paulo@89 8028 } else {
paulo@89 8029 path.push("M", t30[0], "A", rc1, ",", rc1, " 0 1,", cr, " ", t12[0]);
paulo@89 8030 }
paulo@89 8031 } else {
paulo@89 8032 path.push("M", x0, ",", y0);
paulo@89 8033 }
paulo@89 8034 if (x3 != null) {
paulo@89 8035 var t03 = d3_svg_arcCornerTangents([ x0, y0 ], [ x3, y3 ], r0, -rc0, cw), t21 = d3_svg_arcCornerTangents([ x2, y2 ], x1 == null ? [ x0, y0 ] : [ x1, y1 ], r0, -rc0, cw);
paulo@89 8036 if (rc === rc0) {
paulo@89 8037 path.push("L", t21[0], "A", rc0, ",", rc0, " 0 0,", cr, " ", t21[1], "A", r0, ",", r0, " 0 ", cw ^ d3_svg_arcSweep(t21[1][0], t21[1][1], t03[1][0], t03[1][1]), ",", 1 - cw, " ", t03[1], "A", rc0, ",", rc0, " 0 0,", cr, " ", t03[0]);
paulo@89 8038 } else {
paulo@89 8039 path.push("L", t21[0], "A", rc0, ",", rc0, " 0 0,", cr, " ", t03[0]);
paulo@89 8040 }
paulo@89 8041 } else {
paulo@89 8042 path.push("L", x2, ",", y2);
paulo@89 8043 }
paulo@89 8044 } else {
paulo@89 8045 path.push("M", x0, ",", y0);
paulo@89 8046 if (x1 != null) path.push("A", r1, ",", r1, " 0 ", l1, ",", cw, " ", x1, ",", y1);
paulo@89 8047 path.push("L", x2, ",", y2);
paulo@89 8048 if (x3 != null) path.push("A", r0, ",", r0, " 0 ", l0, ",", 1 - cw, " ", x3, ",", y3);
paulo@89 8049 }
paulo@89 8050 path.push("Z");
paulo@89 8051 return path.join("");
paulo@89 8052 }
paulo@89 8053 function circleSegment(r1, cw) {
paulo@89 8054 return "M0," + r1 + "A" + r1 + "," + r1 + " 0 1," + cw + " 0," + -r1 + "A" + r1 + "," + r1 + " 0 1," + cw + " 0," + r1;
paulo@89 8055 }
paulo@89 8056 arc.innerRadius = function(v) {
paulo@89 8057 if (!arguments.length) return innerRadius;
paulo@89 8058 innerRadius = d3_functor(v);
paulo@89 8059 return arc;
paulo@89 8060 };
paulo@89 8061 arc.outerRadius = function(v) {
paulo@89 8062 if (!arguments.length) return outerRadius;
paulo@89 8063 outerRadius = d3_functor(v);
paulo@89 8064 return arc;
paulo@89 8065 };
paulo@89 8066 arc.cornerRadius = function(v) {
paulo@89 8067 if (!arguments.length) return cornerRadius;
paulo@89 8068 cornerRadius = d3_functor(v);
paulo@89 8069 return arc;
paulo@89 8070 };
paulo@89 8071 arc.padRadius = function(v) {
paulo@89 8072 if (!arguments.length) return padRadius;
paulo@89 8073 padRadius = v == d3_svg_arcAuto ? d3_svg_arcAuto : d3_functor(v);
paulo@89 8074 return arc;
paulo@89 8075 };
paulo@89 8076 arc.startAngle = function(v) {
paulo@89 8077 if (!arguments.length) return startAngle;
paulo@89 8078 startAngle = d3_functor(v);
paulo@89 8079 return arc;
paulo@89 8080 };
paulo@89 8081 arc.endAngle = function(v) {
paulo@89 8082 if (!arguments.length) return endAngle;
paulo@89 8083 endAngle = d3_functor(v);
paulo@89 8084 return arc;
paulo@89 8085 };
paulo@89 8086 arc.padAngle = function(v) {
paulo@89 8087 if (!arguments.length) return padAngle;
paulo@89 8088 padAngle = d3_functor(v);
paulo@89 8089 return arc;
paulo@89 8090 };
paulo@89 8091 arc.centroid = function() {
paulo@89 8092 var r = (+innerRadius.apply(this, arguments) + +outerRadius.apply(this, arguments)) / 2, a = (+startAngle.apply(this, arguments) + +endAngle.apply(this, arguments)) / 2 - halfπ;
paulo@89 8093 return [ Math.cos(a) * r, Math.sin(a) * r ];
paulo@89 8094 };
paulo@89 8095 return arc;
paulo@89 8096 };
paulo@89 8097 var d3_svg_arcAuto = "auto";
paulo@89 8098 function d3_svg_arcInnerRadius(d) {
paulo@89 8099 return d.innerRadius;
paulo@89 8100 }
paulo@89 8101 function d3_svg_arcOuterRadius(d) {
paulo@89 8102 return d.outerRadius;
paulo@89 8103 }
paulo@89 8104 function d3_svg_arcStartAngle(d) {
paulo@89 8105 return d.startAngle;
paulo@89 8106 }
paulo@89 8107 function d3_svg_arcEndAngle(d) {
paulo@89 8108 return d.endAngle;
paulo@89 8109 }
paulo@89 8110 function d3_svg_arcPadAngle(d) {
paulo@89 8111 return d && d.padAngle;
paulo@89 8112 }
paulo@89 8113 function d3_svg_arcSweep(x0, y0, x1, y1) {
paulo@89 8114 return (x0 - x1) * y0 - (y0 - y1) * x0 > 0 ? 0 : 1;
paulo@89 8115 }
paulo@89 8116 function d3_svg_arcCornerTangents(p0, p1, r1, rc, cw) {
paulo@89 8117 var x01 = p0[0] - p1[0], y01 = p0[1] - p1[1], lo = (cw ? rc : -rc) / Math.sqrt(x01 * x01 + y01 * y01), ox = lo * y01, oy = -lo * x01, x1 = p0[0] + ox, y1 = p0[1] + oy, x2 = p1[0] + ox, y2 = p1[1] + oy, x3 = (x1 + x2) / 2, y3 = (y1 + y2) / 2, dx = x2 - x1, dy = y2 - y1, d2 = dx * dx + dy * dy, r = r1 - rc, D = x1 * y2 - x2 * y1, d = (dy < 0 ? -1 : 1) * Math.sqrt(Math.max(0, r * r * d2 - D * D)), cx0 = (D * dy - dx * d) / d2, cy0 = (-D * dx - dy * d) / d2, cx1 = (D * dy + dx * d) / d2, cy1 = (-D * dx + dy * d) / d2, dx0 = cx0 - x3, dy0 = cy0 - y3, dx1 = cx1 - x3, dy1 = cy1 - y3;
paulo@89 8118 if (dx0 * dx0 + dy0 * dy0 > dx1 * dx1 + dy1 * dy1) cx0 = cx1, cy0 = cy1;
paulo@89 8119 return [ [ cx0 - ox, cy0 - oy ], [ cx0 * r1 / r, cy0 * r1 / r ] ];
paulo@89 8120 }
paulo@89 8121 function d3_svg_line(projection) {
paulo@89 8122 var x = d3_geom_pointX, y = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, tension = .7;
paulo@89 8123 function line(data) {
paulo@89 8124 var segments = [], points = [], i = -1, n = data.length, d, fx = d3_functor(x), fy = d3_functor(y);
paulo@89 8125 function segment() {
paulo@89 8126 segments.push("M", interpolate(projection(points), tension));
paulo@89 8127 }
paulo@89 8128 while (++i < n) {
paulo@89 8129 if (defined.call(this, d = data[i], i)) {
paulo@89 8130 points.push([ +fx.call(this, d, i), +fy.call(this, d, i) ]);
paulo@89 8131 } else if (points.length) {
paulo@89 8132 segment();
paulo@89 8133 points = [];
paulo@89 8134 }
paulo@89 8135 }
paulo@89 8136 if (points.length) segment();
paulo@89 8137 return segments.length ? segments.join("") : null;
paulo@89 8138 }
paulo@89 8139 line.x = function(_) {
paulo@89 8140 if (!arguments.length) return x;
paulo@89 8141 x = _;
paulo@89 8142 return line;
paulo@89 8143 };
paulo@89 8144 line.y = function(_) {
paulo@89 8145 if (!arguments.length) return y;
paulo@89 8146 y = _;
paulo@89 8147 return line;
paulo@89 8148 };
paulo@89 8149 line.defined = function(_) {
paulo@89 8150 if (!arguments.length) return defined;
paulo@89 8151 defined = _;
paulo@89 8152 return line;
paulo@89 8153 };
paulo@89 8154 line.interpolate = function(_) {
paulo@89 8155 if (!arguments.length) return interpolateKey;
paulo@89 8156 if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key;
paulo@89 8157 return line;
paulo@89 8158 };
paulo@89 8159 line.tension = function(_) {
paulo@89 8160 if (!arguments.length) return tension;
paulo@89 8161 tension = _;
paulo@89 8162 return line;
paulo@89 8163 };
paulo@89 8164 return line;
paulo@89 8165 }
paulo@89 8166 d3.svg.line = function() {
paulo@89 8167 return d3_svg_line(d3_identity);
paulo@89 8168 };
paulo@89 8169 var d3_svg_lineInterpolators = d3.map({
paulo@89 8170 linear: d3_svg_lineLinear,
paulo@89 8171 "linear-closed": d3_svg_lineLinearClosed,
paulo@89 8172 step: d3_svg_lineStep,
paulo@89 8173 "step-before": d3_svg_lineStepBefore,
paulo@89 8174 "step-after": d3_svg_lineStepAfter,
paulo@89 8175 basis: d3_svg_lineBasis,
paulo@89 8176 "basis-open": d3_svg_lineBasisOpen,
paulo@89 8177 "basis-closed": d3_svg_lineBasisClosed,
paulo@89 8178 bundle: d3_svg_lineBundle,
paulo@89 8179 cardinal: d3_svg_lineCardinal,
paulo@89 8180 "cardinal-open": d3_svg_lineCardinalOpen,
paulo@89 8181 "cardinal-closed": d3_svg_lineCardinalClosed,
paulo@89 8182 monotone: d3_svg_lineMonotone
paulo@89 8183 });
paulo@89 8184 d3_svg_lineInterpolators.forEach(function(key, value) {
paulo@89 8185 value.key = key;
paulo@89 8186 value.closed = /-closed$/.test(key);
paulo@89 8187 });
paulo@89 8188 function d3_svg_lineLinear(points) {
paulo@89 8189 return points.length > 1 ? points.join("L") : points + "Z";
paulo@89 8190 }
paulo@89 8191 function d3_svg_lineLinearClosed(points) {
paulo@89 8192 return points.join("L") + "Z";
paulo@89 8193 }
paulo@89 8194 function d3_svg_lineStep(points) {
paulo@89 8195 var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ];
paulo@89 8196 while (++i < n) path.push("H", (p[0] + (p = points[i])[0]) / 2, "V", p[1]);
paulo@89 8197 if (n > 1) path.push("H", p[0]);
paulo@89 8198 return path.join("");
paulo@89 8199 }
paulo@89 8200 function d3_svg_lineStepBefore(points) {
paulo@89 8201 var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ];
paulo@89 8202 while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]);
paulo@89 8203 return path.join("");
paulo@89 8204 }
paulo@89 8205 function d3_svg_lineStepAfter(points) {
paulo@89 8206 var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ];
paulo@89 8207 while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]);
paulo@89 8208 return path.join("");
paulo@89 8209 }
paulo@89 8210 function d3_svg_lineCardinalOpen(points, tension) {
paulo@89 8211 return points.length < 4 ? d3_svg_lineLinear(points) : points[1] + d3_svg_lineHermite(points.slice(1, -1), d3_svg_lineCardinalTangents(points, tension));
paulo@89 8212 }
paulo@89 8213 function d3_svg_lineCardinalClosed(points, tension) {
paulo@89 8214 return points.length < 3 ? d3_svg_lineLinearClosed(points) : points[0] + d3_svg_lineHermite((points.push(points[0]),
paulo@89 8215 points), d3_svg_lineCardinalTangents([ points[points.length - 2] ].concat(points, [ points[1] ]), tension));
paulo@89 8216 }
paulo@89 8217 function d3_svg_lineCardinal(points, tension) {
paulo@89 8218 return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineCardinalTangents(points, tension));
paulo@89 8219 }
paulo@89 8220 function d3_svg_lineHermite(points, tangents) {
paulo@89 8221 if (tangents.length < 1 || points.length != tangents.length && points.length != tangents.length + 2) {
paulo@89 8222 return d3_svg_lineLinear(points);
paulo@89 8223 }
paulo@89 8224 var quad = points.length != tangents.length, path = "", p0 = points[0], p = points[1], t0 = tangents[0], t = t0, pi = 1;
paulo@89 8225 if (quad) {
paulo@89 8226 path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3) + "," + p[0] + "," + p[1];
paulo@89 8227 p0 = points[1];
paulo@89 8228 pi = 2;
paulo@89 8229 }
paulo@89 8230 if (tangents.length > 1) {
paulo@89 8231 t = tangents[1];
paulo@89 8232 p = points[pi];
paulo@89 8233 pi++;
paulo@89 8234 path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1]) + "," + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1];
paulo@89 8235 for (var i = 2; i < tangents.length; i++, pi++) {
paulo@89 8236 p = points[pi];
paulo@89 8237 t = tangents[i];
paulo@89 8238 path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1];
paulo@89 8239 }
paulo@89 8240 }
paulo@89 8241 if (quad) {
paulo@89 8242 var lp = points[pi];
paulo@89 8243 path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3) + "," + lp[0] + "," + lp[1];
paulo@89 8244 }
paulo@89 8245 return path;
paulo@89 8246 }
paulo@89 8247 function d3_svg_lineCardinalTangents(points, tension) {
paulo@89 8248 var tangents = [], a = (1 - tension) / 2, p0, p1 = points[0], p2 = points[1], i = 1, n = points.length;
paulo@89 8249 while (++i < n) {
paulo@89 8250 p0 = p1;
paulo@89 8251 p1 = p2;
paulo@89 8252 p2 = points[i];
paulo@89 8253 tangents.push([ a * (p2[0] - p0[0]), a * (p2[1] - p0[1]) ]);
paulo@89 8254 }
paulo@89 8255 return tangents;
paulo@89 8256 }
paulo@89 8257 function d3_svg_lineBasis(points) {
paulo@89 8258 if (points.length < 3) return d3_svg_lineLinear(points);
paulo@89 8259 var i = 1, n = points.length, pi = points[0], x0 = pi[0], y0 = pi[1], px = [ x0, x0, x0, (pi = points[1])[0] ], py = [ y0, y0, y0, pi[1] ], path = [ x0, ",", y0, "L", d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ];
paulo@89 8260 points.push(points[n - 1]);
paulo@89 8261 while (++i <= n) {
paulo@89 8262 pi = points[i];
paulo@89 8263 px.shift();
paulo@89 8264 px.push(pi[0]);
paulo@89 8265 py.shift();
paulo@89 8266 py.push(pi[1]);
paulo@89 8267 d3_svg_lineBasisBezier(path, px, py);
paulo@89 8268 }
paulo@89 8269 points.pop();
paulo@89 8270 path.push("L", pi);
paulo@89 8271 return path.join("");
paulo@89 8272 }
paulo@89 8273 function d3_svg_lineBasisOpen(points) {
paulo@89 8274 if (points.length < 4) return d3_svg_lineLinear(points);
paulo@89 8275 var path = [], i = -1, n = points.length, pi, px = [ 0 ], py = [ 0 ];
paulo@89 8276 while (++i < 3) {
paulo@89 8277 pi = points[i];
paulo@89 8278 px.push(pi[0]);
paulo@89 8279 py.push(pi[1]);
paulo@89 8280 }
paulo@89 8281 path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px) + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py));
paulo@89 8282 --i;
paulo@89 8283 while (++i < n) {
paulo@89 8284 pi = points[i];
paulo@89 8285 px.shift();
paulo@89 8286 px.push(pi[0]);
paulo@89 8287 py.shift();
paulo@89 8288 py.push(pi[1]);
paulo@89 8289 d3_svg_lineBasisBezier(path, px, py);
paulo@89 8290 }
paulo@89 8291 return path.join("");
paulo@89 8292 }
paulo@89 8293 function d3_svg_lineBasisClosed(points) {
paulo@89 8294 var path, i = -1, n = points.length, m = n + 4, pi, px = [], py = [];
paulo@89 8295 while (++i < 4) {
paulo@89 8296 pi = points[i % n];
paulo@89 8297 px.push(pi[0]);
paulo@89 8298 py.push(pi[1]);
paulo@89 8299 }
paulo@89 8300 path = [ d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ];
paulo@89 8301 --i;
paulo@89 8302 while (++i < m) {
paulo@89 8303 pi = points[i % n];
paulo@89 8304 px.shift();
paulo@89 8305 px.push(pi[0]);
paulo@89 8306 py.shift();
paulo@89 8307 py.push(pi[1]);
paulo@89 8308 d3_svg_lineBasisBezier(path, px, py);
paulo@89 8309 }
paulo@89 8310 return path.join("");
paulo@89 8311 }
paulo@89 8312 function d3_svg_lineBundle(points, tension) {
paulo@89 8313 var n = points.length - 1;
paulo@89 8314 if (n) {
paulo@89 8315 var x0 = points[0][0], y0 = points[0][1], dx = points[n][0] - x0, dy = points[n][1] - y0, i = -1, p, t;
paulo@89 8316 while (++i <= n) {
paulo@89 8317 p = points[i];
paulo@89 8318 t = i / n;
paulo@89 8319 p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx);
paulo@89 8320 p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy);
paulo@89 8321 }
paulo@89 8322 }
paulo@89 8323 return d3_svg_lineBasis(points);
paulo@89 8324 }
paulo@89 8325 function d3_svg_lineDot4(a, b) {
paulo@89 8326 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
paulo@89 8327 }
paulo@89 8328 var d3_svg_lineBasisBezier1 = [ 0, 2 / 3, 1 / 3, 0 ], d3_svg_lineBasisBezier2 = [ 0, 1 / 3, 2 / 3, 0 ], d3_svg_lineBasisBezier3 = [ 0, 1 / 6, 2 / 3, 1 / 6 ];
paulo@89 8329 function d3_svg_lineBasisBezier(path, x, y) {
paulo@89 8330 path.push("C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y));
paulo@89 8331 }
paulo@89 8332 function d3_svg_lineSlope(p0, p1) {
paulo@89 8333 return (p1[1] - p0[1]) / (p1[0] - p0[0]);
paulo@89 8334 }
paulo@89 8335 function d3_svg_lineFiniteDifferences(points) {
paulo@89 8336 var i = 0, j = points.length - 1, m = [], p0 = points[0], p1 = points[1], d = m[0] = d3_svg_lineSlope(p0, p1);
paulo@89 8337 while (++i < j) {
paulo@89 8338 m[i] = (d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1]))) / 2;
paulo@89 8339 }
paulo@89 8340 m[i] = d;
paulo@89 8341 return m;
paulo@89 8342 }
paulo@89 8343 function d3_svg_lineMonotoneTangents(points) {
paulo@89 8344 var tangents = [], d, a, b, s, m = d3_svg_lineFiniteDifferences(points), i = -1, j = points.length - 1;
paulo@89 8345 while (++i < j) {
paulo@89 8346 d = d3_svg_lineSlope(points[i], points[i + 1]);
paulo@89 8347 if (abs(d) < ε) {
paulo@89 8348 m[i] = m[i + 1] = 0;
paulo@89 8349 } else {
paulo@89 8350 a = m[i] / d;
paulo@89 8351 b = m[i + 1] / d;
paulo@89 8352 s = a * a + b * b;
paulo@89 8353 if (s > 9) {
paulo@89 8354 s = d * 3 / Math.sqrt(s);
paulo@89 8355 m[i] = s * a;
paulo@89 8356 m[i + 1] = s * b;
paulo@89 8357 }
paulo@89 8358 }
paulo@89 8359 }
paulo@89 8360 i = -1;
paulo@89 8361 while (++i <= j) {
paulo@89 8362 s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0]) / (6 * (1 + m[i] * m[i]));
paulo@89 8363 tangents.push([ s || 0, m[i] * s || 0 ]);
paulo@89 8364 }
paulo@89 8365 return tangents;
paulo@89 8366 }
paulo@89 8367 function d3_svg_lineMonotone(points) {
paulo@89 8368 return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points));
paulo@89 8369 }
paulo@89 8370 d3.svg.line.radial = function() {
paulo@89 8371 var line = d3_svg_line(d3_svg_lineRadial);
paulo@89 8372 line.radius = line.x, delete line.x;
paulo@89 8373 line.angle = line.y, delete line.y;
paulo@89 8374 return line;
paulo@89 8375 };
paulo@89 8376 function d3_svg_lineRadial(points) {
paulo@89 8377 var point, i = -1, n = points.length, r, a;
paulo@89 8378 while (++i < n) {
paulo@89 8379 point = points[i];
paulo@89 8380 r = point[0];
paulo@89 8381 a = point[1] - halfπ;
paulo@89 8382 point[0] = r * Math.cos(a);
paulo@89 8383 point[1] = r * Math.sin(a);
paulo@89 8384 }
paulo@89 8385 return points;
paulo@89 8386 }
paulo@89 8387 function d3_svg_area(projection) {
paulo@89 8388 var x0 = d3_geom_pointX, x1 = d3_geom_pointX, y0 = 0, y1 = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, interpolateReverse = interpolate, L = "L", tension = .7;
paulo@89 8389 function area(data) {
paulo@89 8390 var segments = [], points0 = [], points1 = [], i = -1, n = data.length, d, fx0 = d3_functor(x0), fy0 = d3_functor(y0), fx1 = x0 === x1 ? function() {
paulo@89 8391 return x;
paulo@89 8392 } : d3_functor(x1), fy1 = y0 === y1 ? function() {
paulo@89 8393 return y;
paulo@89 8394 } : d3_functor(y1), x, y;
paulo@89 8395 function segment() {
paulo@89 8396 segments.push("M", interpolate(projection(points1), tension), L, interpolateReverse(projection(points0.reverse()), tension), "Z");
paulo@89 8397 }
paulo@89 8398 while (++i < n) {
paulo@89 8399 if (defined.call(this, d = data[i], i)) {
paulo@89 8400 points0.push([ x = +fx0.call(this, d, i), y = +fy0.call(this, d, i) ]);
paulo@89 8401 points1.push([ +fx1.call(this, d, i), +fy1.call(this, d, i) ]);
paulo@89 8402 } else if (points0.length) {
paulo@89 8403 segment();
paulo@89 8404 points0 = [];
paulo@89 8405 points1 = [];
paulo@89 8406 }
paulo@89 8407 }
paulo@89 8408 if (points0.length) segment();
paulo@89 8409 return segments.length ? segments.join("") : null;
paulo@89 8410 }
paulo@89 8411 area.x = function(_) {
paulo@89 8412 if (!arguments.length) return x1;
paulo@89 8413 x0 = x1 = _;
paulo@89 8414 return area;
paulo@89 8415 };
paulo@89 8416 area.x0 = function(_) {
paulo@89 8417 if (!arguments.length) return x0;
paulo@89 8418 x0 = _;
paulo@89 8419 return area;
paulo@89 8420 };
paulo@89 8421 area.x1 = function(_) {
paulo@89 8422 if (!arguments.length) return x1;
paulo@89 8423 x1 = _;
paulo@89 8424 return area;
paulo@89 8425 };
paulo@89 8426 area.y = function(_) {
paulo@89 8427 if (!arguments.length) return y1;
paulo@89 8428 y0 = y1 = _;
paulo@89 8429 return area;
paulo@89 8430 };
paulo@89 8431 area.y0 = function(_) {
paulo@89 8432 if (!arguments.length) return y0;
paulo@89 8433 y0 = _;
paulo@89 8434 return area;
paulo@89 8435 };
paulo@89 8436 area.y1 = function(_) {
paulo@89 8437 if (!arguments.length) return y1;
paulo@89 8438 y1 = _;
paulo@89 8439 return area;
paulo@89 8440 };
paulo@89 8441 area.defined = function(_) {
paulo@89 8442 if (!arguments.length) return defined;
paulo@89 8443 defined = _;
paulo@89 8444 return area;
paulo@89 8445 };
paulo@89 8446 area.interpolate = function(_) {
paulo@89 8447 if (!arguments.length) return interpolateKey;
paulo@89 8448 if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key;
paulo@89 8449 interpolateReverse = interpolate.reverse || interpolate;
paulo@89 8450 L = interpolate.closed ? "M" : "L";
paulo@89 8451 return area;
paulo@89 8452 };
paulo@89 8453 area.tension = function(_) {
paulo@89 8454 if (!arguments.length) return tension;
paulo@89 8455 tension = _;
paulo@89 8456 return area;
paulo@89 8457 };
paulo@89 8458 return area;
paulo@89 8459 }
paulo@89 8460 d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter;
paulo@89 8461 d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore;
paulo@89 8462 d3.svg.area = function() {
paulo@89 8463 return d3_svg_area(d3_identity);
paulo@89 8464 };
paulo@89 8465 d3.svg.area.radial = function() {
paulo@89 8466 var area = d3_svg_area(d3_svg_lineRadial);
paulo@89 8467 area.radius = area.x, delete area.x;
paulo@89 8468 area.innerRadius = area.x0, delete area.x0;
paulo@89 8469 area.outerRadius = area.x1, delete area.x1;
paulo@89 8470 area.angle = area.y, delete area.y;
paulo@89 8471 area.startAngle = area.y0, delete area.y0;
paulo@89 8472 area.endAngle = area.y1, delete area.y1;
paulo@89 8473 return area;
paulo@89 8474 };
paulo@89 8475 d3.svg.chord = function() {
paulo@89 8476 var source = d3_source, target = d3_target, radius = d3_svg_chordRadius, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle;
paulo@89 8477 function chord(d, i) {
paulo@89 8478 var s = subgroup(this, source, d, i), t = subgroup(this, target, d, i);
paulo@89 8479 return "M" + s.p0 + arc(s.r, s.p1, s.a1 - s.a0) + (equals(s, t) ? curve(s.r, s.p1, s.r, s.p0) : curve(s.r, s.p1, t.r, t.p0) + arc(t.r, t.p1, t.a1 - t.a0) + curve(t.r, t.p1, s.r, s.p0)) + "Z";
paulo@89 8480 }
paulo@89 8481 function subgroup(self, f, d, i) {
paulo@89 8482 var subgroup = f.call(self, d, i), r = radius.call(self, subgroup, i), a0 = startAngle.call(self, subgroup, i) - halfπ, a1 = endAngle.call(self, subgroup, i) - halfπ;
paulo@89 8483 return {
paulo@89 8484 r: r,
paulo@89 8485 a0: a0,
paulo@89 8486 a1: a1,
paulo@89 8487 p0: [ r * Math.cos(a0), r * Math.sin(a0) ],
paulo@89 8488 p1: [ r * Math.cos(a1), r * Math.sin(a1) ]
paulo@89 8489 };
paulo@89 8490 }
paulo@89 8491 function equals(a, b) {
paulo@89 8492 return a.a0 == b.a0 && a.a1 == b.a1;
paulo@89 8493 }
paulo@89 8494 function arc(r, p, a) {
paulo@89 8495 return "A" + r + "," + r + " 0 " + +(a > π) + ",1 " + p;
paulo@89 8496 }
paulo@89 8497 function curve(r0, p0, r1, p1) {
paulo@89 8498 return "Q 0,0 " + p1;
paulo@89 8499 }
paulo@89 8500 chord.radius = function(v) {
paulo@89 8501 if (!arguments.length) return radius;
paulo@89 8502 radius = d3_functor(v);
paulo@89 8503 return chord;
paulo@89 8504 };
paulo@89 8505 chord.source = function(v) {
paulo@89 8506 if (!arguments.length) return source;
paulo@89 8507 source = d3_functor(v);
paulo@89 8508 return chord;
paulo@89 8509 };
paulo@89 8510 chord.target = function(v) {
paulo@89 8511 if (!arguments.length) return target;
paulo@89 8512 target = d3_functor(v);
paulo@89 8513 return chord;
paulo@89 8514 };
paulo@89 8515 chord.startAngle = function(v) {
paulo@89 8516 if (!arguments.length) return startAngle;
paulo@89 8517 startAngle = d3_functor(v);
paulo@89 8518 return chord;
paulo@89 8519 };
paulo@89 8520 chord.endAngle = function(v) {
paulo@89 8521 if (!arguments.length) return endAngle;
paulo@89 8522 endAngle = d3_functor(v);
paulo@89 8523 return chord;
paulo@89 8524 };
paulo@89 8525 return chord;
paulo@89 8526 };
paulo@89 8527 function d3_svg_chordRadius(d) {
paulo@89 8528 return d.radius;
paulo@89 8529 }
paulo@89 8530 d3.svg.diagonal = function() {
paulo@89 8531 var source = d3_source, target = d3_target, projection = d3_svg_diagonalProjection;
paulo@89 8532 function diagonal(d, i) {
paulo@89 8533 var p0 = source.call(this, d, i), p3 = target.call(this, d, i), m = (p0.y + p3.y) / 2, p = [ p0, {
paulo@89 8534 x: p0.x,
paulo@89 8535 y: m
paulo@89 8536 }, {
paulo@89 8537 x: p3.x,
paulo@89 8538 y: m
paulo@89 8539 }, p3 ];
paulo@89 8540 p = p.map(projection);
paulo@89 8541 return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3];
paulo@89 8542 }
paulo@89 8543 diagonal.source = function(x) {
paulo@89 8544 if (!arguments.length) return source;
paulo@89 8545 source = d3_functor(x);
paulo@89 8546 return diagonal;
paulo@89 8547 };
paulo@89 8548 diagonal.target = function(x) {
paulo@89 8549 if (!arguments.length) return target;
paulo@89 8550 target = d3_functor(x);
paulo@89 8551 return diagonal;
paulo@89 8552 };
paulo@89 8553 diagonal.projection = function(x) {
paulo@89 8554 if (!arguments.length) return projection;
paulo@89 8555 projection = x;
paulo@89 8556 return diagonal;
paulo@89 8557 };
paulo@89 8558 return diagonal;
paulo@89 8559 };
paulo@89 8560 function d3_svg_diagonalProjection(d) {
paulo@89 8561 return [ d.x, d.y ];
paulo@89 8562 }
paulo@89 8563 d3.svg.diagonal.radial = function() {
paulo@89 8564 var diagonal = d3.svg.diagonal(), projection = d3_svg_diagonalProjection, projection_ = diagonal.projection;
paulo@89 8565 diagonal.projection = function(x) {
paulo@89 8566 return arguments.length ? projection_(d3_svg_diagonalRadialProjection(projection = x)) : projection;
paulo@89 8567 };
paulo@89 8568 return diagonal;
paulo@89 8569 };
paulo@89 8570 function d3_svg_diagonalRadialProjection(projection) {
paulo@89 8571 return function() {
paulo@89 8572 var d = projection.apply(this, arguments), r = d[0], a = d[1] - halfπ;
paulo@89 8573 return [ r * Math.cos(a), r * Math.sin(a) ];
paulo@89 8574 };
paulo@89 8575 }
paulo@89 8576 d3.svg.symbol = function() {
paulo@89 8577 var type = d3_svg_symbolType, size = d3_svg_symbolSize;
paulo@89 8578 function symbol(d, i) {
paulo@89 8579 return (d3_svg_symbols.get(type.call(this, d, i)) || d3_svg_symbolCircle)(size.call(this, d, i));
paulo@89 8580 }
paulo@89 8581 symbol.type = function(x) {
paulo@89 8582 if (!arguments.length) return type;
paulo@89 8583 type = d3_functor(x);
paulo@89 8584 return symbol;
paulo@89 8585 };
paulo@89 8586 symbol.size = function(x) {
paulo@89 8587 if (!arguments.length) return size;
paulo@89 8588 size = d3_functor(x);
paulo@89 8589 return symbol;
paulo@89 8590 };
paulo@89 8591 return symbol;
paulo@89 8592 };
paulo@89 8593 function d3_svg_symbolSize() {
paulo@89 8594 return 64;
paulo@89 8595 }
paulo@89 8596 function d3_svg_symbolType() {
paulo@89 8597 return "circle";
paulo@89 8598 }
paulo@89 8599 function d3_svg_symbolCircle(size) {
paulo@89 8600 var r = Math.sqrt(size / π);
paulo@89 8601 return "M0," + r + "A" + r + "," + r + " 0 1,1 0," + -r + "A" + r + "," + r + " 0 1,1 0," + r + "Z";
paulo@89 8602 }
paulo@89 8603 var d3_svg_symbols = d3.map({
paulo@89 8604 circle: d3_svg_symbolCircle,
paulo@89 8605 cross: function(size) {
paulo@89 8606 var r = Math.sqrt(size / 5) / 2;
paulo@89 8607 return "M" + -3 * r + "," + -r + "H" + -r + "V" + -3 * r + "H" + r + "V" + -r + "H" + 3 * r + "V" + r + "H" + r + "V" + 3 * r + "H" + -r + "V" + r + "H" + -3 * r + "Z";
paulo@89 8608 },
paulo@89 8609 diamond: function(size) {
paulo@89 8610 var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)), rx = ry * d3_svg_symbolTan30;
paulo@89 8611 return "M0," + -ry + "L" + rx + ",0" + " 0," + ry + " " + -rx + ",0" + "Z";
paulo@89 8612 },
paulo@89 8613 square: function(size) {
paulo@89 8614 var r = Math.sqrt(size) / 2;
paulo@89 8615 return "M" + -r + "," + -r + "L" + r + "," + -r + " " + r + "," + r + " " + -r + "," + r + "Z";
paulo@89 8616 },
paulo@89 8617 "triangle-down": function(size) {
paulo@89 8618 var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2;
paulo@89 8619 return "M0," + ry + "L" + rx + "," + -ry + " " + -rx + "," + -ry + "Z";
paulo@89 8620 },
paulo@89 8621 "triangle-up": function(size) {
paulo@89 8622 var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2;
paulo@89 8623 return "M0," + -ry + "L" + rx + "," + ry + " " + -rx + "," + ry + "Z";
paulo@89 8624 }
paulo@89 8625 });
paulo@89 8626 d3.svg.symbolTypes = d3_svg_symbols.keys();
paulo@89 8627 var d3_svg_symbolSqrt3 = Math.sqrt(3), d3_svg_symbolTan30 = Math.tan(30 * d3_radians);
paulo@89 8628 d3_selectionPrototype.transition = function(name) {
paulo@89 8629 var id = d3_transitionInheritId || ++d3_transitionId, ns = d3_transitionNamespace(name), subgroups = [], subgroup, node, transition = d3_transitionInherit || {
paulo@89 8630 time: Date.now(),
paulo@89 8631 ease: d3_ease_cubicInOut,
paulo@89 8632 delay: 0,
paulo@89 8633 duration: 250
paulo@89 8634 };
paulo@89 8635 for (var j = -1, m = this.length; ++j < m; ) {
paulo@89 8636 subgroups.push(subgroup = []);
paulo@89 8637 for (var group = this[j], i = -1, n = group.length; ++i < n; ) {
paulo@89 8638 if (node = group[i]) d3_transitionNode(node, i, ns, id, transition);
paulo@89 8639 subgroup.push(node);
paulo@89 8640 }
paulo@89 8641 }
paulo@89 8642 return d3_transition(subgroups, ns, id);
paulo@89 8643 };
paulo@89 8644 d3_selectionPrototype.interrupt = function(name) {
paulo@89 8645 return this.each(name == null ? d3_selection_interrupt : d3_selection_interruptNS(d3_transitionNamespace(name)));
paulo@89 8646 };
paulo@89 8647 var d3_selection_interrupt = d3_selection_interruptNS(d3_transitionNamespace());
paulo@89 8648 function d3_selection_interruptNS(ns) {
paulo@89 8649 return function() {
paulo@89 8650 var lock, activeId, active;
paulo@89 8651 if ((lock = this[ns]) && (active = lock[activeId = lock.active])) {
paulo@89 8652 active.timer.c = null;
paulo@89 8653 active.timer.t = NaN;
paulo@89 8654 if (--lock.count) delete lock[activeId]; else delete this[ns];
paulo@89 8655 lock.active += .5;
paulo@89 8656 active.event && active.event.interrupt.call(this, this.__data__, active.index);
paulo@89 8657 }
paulo@89 8658 };
paulo@89 8659 }
paulo@89 8660 function d3_transition(groups, ns, id) {
paulo@89 8661 d3_subclass(groups, d3_transitionPrototype);
paulo@89 8662 groups.namespace = ns;
paulo@89 8663 groups.id = id;
paulo@89 8664 return groups;
paulo@89 8665 }
paulo@89 8666 var d3_transitionPrototype = [], d3_transitionId = 0, d3_transitionInheritId, d3_transitionInherit;
paulo@89 8667 d3_transitionPrototype.call = d3_selectionPrototype.call;
paulo@89 8668 d3_transitionPrototype.empty = d3_selectionPrototype.empty;
paulo@89 8669 d3_transitionPrototype.node = d3_selectionPrototype.node;
paulo@89 8670 d3_transitionPrototype.size = d3_selectionPrototype.size;
paulo@89 8671 d3.transition = function(selection, name) {
paulo@89 8672 return selection && selection.transition ? d3_transitionInheritId ? selection.transition(name) : selection : d3.selection().transition(selection);
paulo@89 8673 };
paulo@89 8674 d3.transition.prototype = d3_transitionPrototype;
paulo@89 8675 d3_transitionPrototype.select = function(selector) {
paulo@89 8676 var id = this.id, ns = this.namespace, subgroups = [], subgroup, subnode, node;
paulo@89 8677 selector = d3_selection_selector(selector);
paulo@89 8678 for (var j = -1, m = this.length; ++j < m; ) {
paulo@89 8679 subgroups.push(subgroup = []);
paulo@89 8680 for (var group = this[j], i = -1, n = group.length; ++i < n; ) {
paulo@89 8681 if ((node = group[i]) && (subnode = selector.call(node, node.__data__, i, j))) {
paulo@89 8682 if ("__data__" in node) subnode.__data__ = node.__data__;
paulo@89 8683 d3_transitionNode(subnode, i, ns, id, node[ns][id]);
paulo@89 8684 subgroup.push(subnode);
paulo@89 8685 } else {
paulo@89 8686 subgroup.push(null);
paulo@89 8687 }
paulo@89 8688 }
paulo@89 8689 }
paulo@89 8690 return d3_transition(subgroups, ns, id);
paulo@89 8691 };
paulo@89 8692 d3_transitionPrototype.selectAll = function(selector) {
paulo@89 8693 var id = this.id, ns = this.namespace, subgroups = [], subgroup, subnodes, node, subnode, transition;
paulo@89 8694 selector = d3_selection_selectorAll(selector);
paulo@89 8695 for (var j = -1, m = this.length; ++j < m; ) {
paulo@89 8696 for (var group = this[j], i = -1, n = group.length; ++i < n; ) {
paulo@89 8697 if (node = group[i]) {
paulo@89 8698 transition = node[ns][id];
paulo@89 8699 subnodes = selector.call(node, node.__data__, i, j);
paulo@89 8700 subgroups.push(subgroup = []);
paulo@89 8701 for (var k = -1, o = subnodes.length; ++k < o; ) {
paulo@89 8702 if (subnode = subnodes[k]) d3_transitionNode(subnode, k, ns, id, transition);
paulo@89 8703 subgroup.push(subnode);
paulo@89 8704 }
paulo@89 8705 }
paulo@89 8706 }
paulo@89 8707 }
paulo@89 8708 return d3_transition(subgroups, ns, id);
paulo@89 8709 };
paulo@89 8710 d3_transitionPrototype.filter = function(filter) {
paulo@89 8711 var subgroups = [], subgroup, group, node;
paulo@89 8712 if (typeof filter !== "function") filter = d3_selection_filter(filter);
paulo@89 8713 for (var j = 0, m = this.length; j < m; j++) {
paulo@89 8714 subgroups.push(subgroup = []);
paulo@89 8715 for (var group = this[j], i = 0, n = group.length; i < n; i++) {
paulo@89 8716 if ((node = group[i]) && filter.call(node, node.__data__, i, j)) {
paulo@89 8717 subgroup.push(node);
paulo@89 8718 }
paulo@89 8719 }
paulo@89 8720 }
paulo@89 8721 return d3_transition(subgroups, this.namespace, this.id);
paulo@89 8722 };
paulo@89 8723 d3_transitionPrototype.tween = function(name, tween) {
paulo@89 8724 var id = this.id, ns = this.namespace;
paulo@89 8725 if (arguments.length < 2) return this.node()[ns][id].tween.get(name);
paulo@89 8726 return d3_selection_each(this, tween == null ? function(node) {
paulo@89 8727 node[ns][id].tween.remove(name);
paulo@89 8728 } : function(node) {
paulo@89 8729 node[ns][id].tween.set(name, tween);
paulo@89 8730 });
paulo@89 8731 };
paulo@89 8732 function d3_transition_tween(groups, name, value, tween) {
paulo@89 8733 var id = groups.id, ns = groups.namespace;
paulo@89 8734 return d3_selection_each(groups, typeof value === "function" ? function(node, i, j) {
paulo@89 8735 node[ns][id].tween.set(name, tween(value.call(node, node.__data__, i, j)));
paulo@89 8736 } : (value = tween(value), function(node) {
paulo@89 8737 node[ns][id].tween.set(name, value);
paulo@89 8738 }));
paulo@89 8739 }
paulo@89 8740 d3_transitionPrototype.attr = function(nameNS, value) {
paulo@89 8741 if (arguments.length < 2) {
paulo@89 8742 for (value in nameNS) this.attr(value, nameNS[value]);
paulo@89 8743 return this;
paulo@89 8744 }
paulo@89 8745 var interpolate = nameNS == "transform" ? d3_interpolateTransform : d3_interpolate, name = d3.ns.qualify(nameNS);
paulo@89 8746 function attrNull() {
paulo@89 8747 this.removeAttribute(name);
paulo@89 8748 }
paulo@89 8749 function attrNullNS() {
paulo@89 8750 this.removeAttributeNS(name.space, name.local);
paulo@89 8751 }
paulo@89 8752 function attrTween(b) {
paulo@89 8753 return b == null ? attrNull : (b += "", function() {
paulo@89 8754 var a = this.getAttribute(name), i;
paulo@89 8755 return a !== b && (i = interpolate(a, b), function(t) {
paulo@89 8756 this.setAttribute(name, i(t));
paulo@89 8757 });
paulo@89 8758 });
paulo@89 8759 }
paulo@89 8760 function attrTweenNS(b) {
paulo@89 8761 return b == null ? attrNullNS : (b += "", function() {
paulo@89 8762 var a = this.getAttributeNS(name.space, name.local), i;
paulo@89 8763 return a !== b && (i = interpolate(a, b), function(t) {
paulo@89 8764 this.setAttributeNS(name.space, name.local, i(t));
paulo@89 8765 });
paulo@89 8766 });
paulo@89 8767 }
paulo@89 8768 return d3_transition_tween(this, "attr." + nameNS, value, name.local ? attrTweenNS : attrTween);
paulo@89 8769 };
paulo@89 8770 d3_transitionPrototype.attrTween = function(nameNS, tween) {
paulo@89 8771 var name = d3.ns.qualify(nameNS);
paulo@89 8772 function attrTween(d, i) {
paulo@89 8773 var f = tween.call(this, d, i, this.getAttribute(name));
paulo@89 8774 return f && function(t) {
paulo@89 8775 this.setAttribute(name, f(t));
paulo@89 8776 };
paulo@89 8777 }
paulo@89 8778 function attrTweenNS(d, i) {
paulo@89 8779 var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local));
paulo@89 8780 return f && function(t) {
paulo@89 8781 this.setAttributeNS(name.space, name.local, f(t));
paulo@89 8782 };
paulo@89 8783 }
paulo@89 8784 return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween);
paulo@89 8785 };
paulo@89 8786 d3_transitionPrototype.style = function(name, value, priority) {
paulo@89 8787 var n = arguments.length;
paulo@89 8788 if (n < 3) {
paulo@89 8789 if (typeof name !== "string") {
paulo@89 8790 if (n < 2) value = "";
paulo@89 8791 for (priority in name) this.style(priority, name[priority], value);
paulo@89 8792 return this;
paulo@89 8793 }
paulo@89 8794 priority = "";
paulo@89 8795 }
paulo@89 8796 function styleNull() {
paulo@89 8797 this.style.removeProperty(name);
paulo@89 8798 }
paulo@89 8799 function styleString(b) {
paulo@89 8800 return b == null ? styleNull : (b += "", function() {
paulo@89 8801 var a = d3_window(this).getComputedStyle(this, null).getPropertyValue(name), i;
paulo@89 8802 return a !== b && (i = d3_interpolate(a, b), function(t) {
paulo@89 8803 this.style.setProperty(name, i(t), priority);
paulo@89 8804 });
paulo@89 8805 });
paulo@89 8806 }
paulo@89 8807 return d3_transition_tween(this, "style." + name, value, styleString);
paulo@89 8808 };
paulo@89 8809 d3_transitionPrototype.styleTween = function(name, tween, priority) {
paulo@89 8810 if (arguments.length < 3) priority = "";
paulo@89 8811 function styleTween(d, i) {
paulo@89 8812 var f = tween.call(this, d, i, d3_window(this).getComputedStyle(this, null).getPropertyValue(name));
paulo@89 8813 return f && function(t) {
paulo@89 8814 this.style.setProperty(name, f(t), priority);
paulo@89 8815 };
paulo@89 8816 }
paulo@89 8817 return this.tween("style." + name, styleTween);
paulo@89 8818 };
paulo@89 8819 d3_transitionPrototype.text = function(value) {
paulo@89 8820 return d3_transition_tween(this, "text", value, d3_transition_text);
paulo@89 8821 };
paulo@89 8822 function d3_transition_text(b) {
paulo@89 8823 if (b == null) b = "";
paulo@89 8824 return function() {
paulo@89 8825 this.textContent = b;
paulo@89 8826 };
paulo@89 8827 }
paulo@89 8828 d3_transitionPrototype.remove = function() {
paulo@89 8829 var ns = this.namespace;
paulo@89 8830 return this.each("end.transition", function() {
paulo@89 8831 var p;
paulo@89 8832 if (this[ns].count < 2 && (p = this.parentNode)) p.removeChild(this);
paulo@89 8833 });
paulo@89 8834 };
paulo@89 8835 d3_transitionPrototype.ease = function(value) {
paulo@89 8836 var id = this.id, ns = this.namespace;
paulo@89 8837 if (arguments.length < 1) return this.node()[ns][id].ease;
paulo@89 8838 if (typeof value !== "function") value = d3.ease.apply(d3, arguments);
paulo@89 8839 return d3_selection_each(this, function(node) {
paulo@89 8840 node[ns][id].ease = value;
paulo@89 8841 });
paulo@89 8842 };
paulo@89 8843 d3_transitionPrototype.delay = function(value) {
paulo@89 8844 var id = this.id, ns = this.namespace;
paulo@89 8845 if (arguments.length < 1) return this.node()[ns][id].delay;
paulo@89 8846 return d3_selection_each(this, typeof value === "function" ? function(node, i, j) {
paulo@89 8847 node[ns][id].delay = +value.call(node, node.__data__, i, j);
paulo@89 8848 } : (value = +value, function(node) {
paulo@89 8849 node[ns][id].delay = value;
paulo@89 8850 }));
paulo@89 8851 };
paulo@89 8852 d3_transitionPrototype.duration = function(value) {
paulo@89 8853 var id = this.id, ns = this.namespace;
paulo@89 8854 if (arguments.length < 1) return this.node()[ns][id].duration;
paulo@89 8855 return d3_selection_each(this, typeof value === "function" ? function(node, i, j) {
paulo@89 8856 node[ns][id].duration = Math.max(1, value.call(node, node.__data__, i, j));
paulo@89 8857 } : (value = Math.max(1, value), function(node) {
paulo@89 8858 node[ns][id].duration = value;
paulo@89 8859 }));
paulo@89 8860 };
paulo@89 8861 d3_transitionPrototype.each = function(type, listener) {
paulo@89 8862 var id = this.id, ns = this.namespace;
paulo@89 8863 if (arguments.length < 2) {
paulo@89 8864 var inherit = d3_transitionInherit, inheritId = d3_transitionInheritId;
paulo@89 8865 try {
paulo@89 8866 d3_transitionInheritId = id;
paulo@89 8867 d3_selection_each(this, function(node, i, j) {
paulo@89 8868 d3_transitionInherit = node[ns][id];
paulo@89 8869 type.call(node, node.__data__, i, j);
paulo@89 8870 });
paulo@89 8871 } finally {
paulo@89 8872 d3_transitionInherit = inherit;
paulo@89 8873 d3_transitionInheritId = inheritId;
paulo@89 8874 }
paulo@89 8875 } else {
paulo@89 8876 d3_selection_each(this, function(node) {
paulo@89 8877 var transition = node[ns][id];
paulo@89 8878 (transition.event || (transition.event = d3.dispatch("start", "end", "interrupt"))).on(type, listener);
paulo@89 8879 });
paulo@89 8880 }
paulo@89 8881 return this;
paulo@89 8882 };
paulo@89 8883 d3_transitionPrototype.transition = function() {
paulo@89 8884 var id0 = this.id, id1 = ++d3_transitionId, ns = this.namespace, subgroups = [], subgroup, group, node, transition;
paulo@89 8885 for (var j = 0, m = this.length; j < m; j++) {
paulo@89 8886 subgroups.push(subgroup = []);
paulo@89 8887 for (var group = this[j], i = 0, n = group.length; i < n; i++) {
paulo@89 8888 if (node = group[i]) {
paulo@89 8889 transition = node[ns][id0];
paulo@89 8890 d3_transitionNode(node, i, ns, id1, {
paulo@89 8891 time: transition.time,
paulo@89 8892 ease: transition.ease,
paulo@89 8893 delay: transition.delay + transition.duration,
paulo@89 8894 duration: transition.duration
paulo@89 8895 });
paulo@89 8896 }
paulo@89 8897 subgroup.push(node);
paulo@89 8898 }
paulo@89 8899 }
paulo@89 8900 return d3_transition(subgroups, ns, id1);
paulo@89 8901 };
paulo@89 8902 function d3_transitionNamespace(name) {
paulo@89 8903 return name == null ? "__transition__" : "__transition_" + name + "__";
paulo@89 8904 }
paulo@89 8905 function d3_transitionNode(node, i, ns, id, inherit) {
paulo@89 8906 var lock = node[ns] || (node[ns] = {
paulo@89 8907 active: 0,
paulo@89 8908 count: 0
paulo@89 8909 }), transition = lock[id], time, timer, duration, ease, tweens;
paulo@89 8910 function schedule(elapsed) {
paulo@89 8911 var delay = transition.delay;
paulo@89 8912 timer.t = delay + time;
paulo@89 8913 if (delay <= elapsed) return start(elapsed - delay);
paulo@89 8914 timer.c = start;
paulo@89 8915 }
paulo@89 8916 function start(elapsed) {
paulo@89 8917 var activeId = lock.active, active = lock[activeId];
paulo@89 8918 if (active) {
paulo@89 8919 active.timer.c = null;
paulo@89 8920 active.timer.t = NaN;
paulo@89 8921 --lock.count;
paulo@89 8922 delete lock[activeId];
paulo@89 8923 active.event && active.event.interrupt.call(node, node.__data__, active.index);
paulo@89 8924 }
paulo@89 8925 for (var cancelId in lock) {
paulo@89 8926 if (+cancelId < id) {
paulo@89 8927 var cancel = lock[cancelId];
paulo@89 8928 cancel.timer.c = null;
paulo@89 8929 cancel.timer.t = NaN;
paulo@89 8930 --lock.count;
paulo@89 8931 delete lock[cancelId];
paulo@89 8932 }
paulo@89 8933 }
paulo@89 8934 timer.c = tick;
paulo@89 8935 d3_timer(function() {
paulo@89 8936 if (timer.c && tick(elapsed || 1)) {
paulo@89 8937 timer.c = null;
paulo@89 8938 timer.t = NaN;
paulo@89 8939 }
paulo@89 8940 return 1;
paulo@89 8941 }, 0, time);
paulo@89 8942 lock.active = id;
paulo@89 8943 transition.event && transition.event.start.call(node, node.__data__, i);
paulo@89 8944 tweens = [];
paulo@89 8945 transition.tween.forEach(function(key, value) {
paulo@89 8946 if (value = value.call(node, node.__data__, i)) {
paulo@89 8947 tweens.push(value);
paulo@89 8948 }
paulo@89 8949 });
paulo@89 8950 ease = transition.ease;
paulo@89 8951 duration = transition.duration;
paulo@89 8952 }
paulo@89 8953 function tick(elapsed) {
paulo@89 8954 var t = elapsed / duration, e = ease(t), n = tweens.length;
paulo@89 8955 while (n > 0) {
paulo@89 8956 tweens[--n].call(node, e);
paulo@89 8957 }
paulo@89 8958 if (t >= 1) {
paulo@89 8959 transition.event && transition.event.end.call(node, node.__data__, i);
paulo@89 8960 if (--lock.count) delete lock[id]; else delete node[ns];
paulo@89 8961 return 1;
paulo@89 8962 }
paulo@89 8963 }
paulo@89 8964 if (!transition) {
paulo@89 8965 time = inherit.time;
paulo@89 8966 timer = d3_timer(schedule, 0, time);
paulo@89 8967 transition = lock[id] = {
paulo@89 8968 tween: new d3_Map(),
paulo@89 8969 time: time,
paulo@89 8970 timer: timer,
paulo@89 8971 delay: inherit.delay,
paulo@89 8972 duration: inherit.duration,
paulo@89 8973 ease: inherit.ease,
paulo@89 8974 index: i
paulo@89 8975 };
paulo@89 8976 inherit = null;
paulo@89 8977 ++lock.count;
paulo@89 8978 }
paulo@89 8979 }
paulo@89 8980 d3.svg.axis = function() {
paulo@89 8981 var scale = d3.scale.linear(), orient = d3_svg_axisDefaultOrient, innerTickSize = 6, outerTickSize = 6, tickPadding = 3, tickArguments_ = [ 10 ], tickValues = null, tickFormat_;
paulo@89 8982 function axis(g) {
paulo@89 8983 g.each(function() {
paulo@89 8984 var g = d3.select(this);
paulo@89 8985 var scale0 = this.__chart__ || scale, scale1 = this.__chart__ = scale.copy();
paulo@89 8986 var ticks = tickValues == null ? scale1.ticks ? scale1.ticks.apply(scale1, tickArguments_) : scale1.domain() : tickValues, tickFormat = tickFormat_ == null ? scale1.tickFormat ? scale1.tickFormat.apply(scale1, tickArguments_) : d3_identity : tickFormat_, tick = g.selectAll(".tick").data(ticks, scale1), tickEnter = tick.enter().insert("g", ".domain").attr("class", "tick").style("opacity", ε), tickExit = d3.transition(tick.exit()).style("opacity", ε).remove(), tickUpdate = d3.transition(tick.order()).style("opacity", 1), tickSpacing = Math.max(innerTickSize, 0) + tickPadding, tickTransform;
paulo@89 8987 var range = d3_scaleRange(scale1), path = g.selectAll(".domain").data([ 0 ]), pathUpdate = (path.enter().append("path").attr("class", "domain"),
paulo@89 8988 d3.transition(path));
paulo@89 8989 tickEnter.append("line");
paulo@89 8990 tickEnter.append("text");
paulo@89 8991 var lineEnter = tickEnter.select("line"), lineUpdate = tickUpdate.select("line"), text = tick.select("text").text(tickFormat), textEnter = tickEnter.select("text"), textUpdate = tickUpdate.select("text"), sign = orient === "top" || orient === "left" ? -1 : 1, x1, x2, y1, y2;
paulo@89 8992 if (orient === "bottom" || orient === "top") {
paulo@89 8993 tickTransform = d3_svg_axisX, x1 = "x", y1 = "y", x2 = "x2", y2 = "y2";
paulo@89 8994 text.attr("dy", sign < 0 ? "0em" : ".71em").style("text-anchor", "middle");
paulo@89 8995 pathUpdate.attr("d", "M" + range[0] + "," + sign * outerTickSize + "V0H" + range[1] + "V" + sign * outerTickSize);
paulo@89 8996 } else {
paulo@89 8997 tickTransform = d3_svg_axisY, x1 = "y", y1 = "x", x2 = "y2", y2 = "x2";
paulo@89 8998 text.attr("dy", ".32em").style("text-anchor", sign < 0 ? "end" : "start");
paulo@89 8999 pathUpdate.attr("d", "M" + sign * outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + sign * outerTickSize);
paulo@89 9000 }
paulo@89 9001 lineEnter.attr(y2, sign * innerTickSize);
paulo@89 9002 textEnter.attr(y1, sign * tickSpacing);
paulo@89 9003 lineUpdate.attr(x2, 0).attr(y2, sign * innerTickSize);
paulo@89 9004 textUpdate.attr(x1, 0).attr(y1, sign * tickSpacing);
paulo@89 9005 if (scale1.rangeBand) {
paulo@89 9006 var x = scale1, dx = x.rangeBand() / 2;
paulo@89 9007 scale0 = scale1 = function(d) {
paulo@89 9008 return x(d) + dx;
paulo@89 9009 };
paulo@89 9010 } else if (scale0.rangeBand) {
paulo@89 9011 scale0 = scale1;
paulo@89 9012 } else {
paulo@89 9013 tickExit.call(tickTransform, scale1, scale0);
paulo@89 9014 }
paulo@89 9015 tickEnter.call(tickTransform, scale0, scale1);
paulo@89 9016 tickUpdate.call(tickTransform, scale1, scale1);
paulo@89 9017 });
paulo@89 9018 }
paulo@89 9019 axis.scale = function(x) {
paulo@89 9020 if (!arguments.length) return scale;
paulo@89 9021 scale = x;
paulo@89 9022 return axis;
paulo@89 9023 };
paulo@89 9024 axis.orient = function(x) {
paulo@89 9025 if (!arguments.length) return orient;
paulo@89 9026 orient = x in d3_svg_axisOrients ? x + "" : d3_svg_axisDefaultOrient;
paulo@89 9027 return axis;
paulo@89 9028 };
paulo@89 9029 axis.ticks = function() {
paulo@89 9030 if (!arguments.length) return tickArguments_;
paulo@89 9031 tickArguments_ = d3_array(arguments);
paulo@89 9032 return axis;
paulo@89 9033 };
paulo@89 9034 axis.tickValues = function(x) {
paulo@89 9035 if (!arguments.length) return tickValues;
paulo@89 9036 tickValues = x;
paulo@89 9037 return axis;
paulo@89 9038 };
paulo@89 9039 axis.tickFormat = function(x) {
paulo@89 9040 if (!arguments.length) return tickFormat_;
paulo@89 9041 tickFormat_ = x;
paulo@89 9042 return axis;
paulo@89 9043 };
paulo@89 9044 axis.tickSize = function(x) {
paulo@89 9045 var n = arguments.length;
paulo@89 9046 if (!n) return innerTickSize;
paulo@89 9047 innerTickSize = +x;
paulo@89 9048 outerTickSize = +arguments[n - 1];
paulo@89 9049 return axis;
paulo@89 9050 };
paulo@89 9051 axis.innerTickSize = function(x) {
paulo@89 9052 if (!arguments.length) return innerTickSize;
paulo@89 9053 innerTickSize = +x;
paulo@89 9054 return axis;
paulo@89 9055 };
paulo@89 9056 axis.outerTickSize = function(x) {
paulo@89 9057 if (!arguments.length) return outerTickSize;
paulo@89 9058 outerTickSize = +x;
paulo@89 9059 return axis;
paulo@89 9060 };
paulo@89 9061 axis.tickPadding = function(x) {
paulo@89 9062 if (!arguments.length) return tickPadding;
paulo@89 9063 tickPadding = +x;
paulo@89 9064 return axis;
paulo@89 9065 };
paulo@89 9066 axis.tickSubdivide = function() {
paulo@89 9067 return arguments.length && axis;
paulo@89 9068 };
paulo@89 9069 return axis;
paulo@89 9070 };
paulo@89 9071 var d3_svg_axisDefaultOrient = "bottom", d3_svg_axisOrients = {
paulo@89 9072 top: 1,
paulo@89 9073 right: 1,
paulo@89 9074 bottom: 1,
paulo@89 9075 left: 1
paulo@89 9076 };
paulo@89 9077 function d3_svg_axisX(selection, x0, x1) {
paulo@89 9078 selection.attr("transform", function(d) {
paulo@89 9079 var v0 = x0(d);
paulo@89 9080 return "translate(" + (isFinite(v0) ? v0 : x1(d)) + ",0)";
paulo@89 9081 });
paulo@89 9082 }
paulo@89 9083 function d3_svg_axisY(selection, y0, y1) {
paulo@89 9084 selection.attr("transform", function(d) {
paulo@89 9085 var v0 = y0(d);
paulo@89 9086 return "translate(0," + (isFinite(v0) ? v0 : y1(d)) + ")";
paulo@89 9087 });
paulo@89 9088 }
paulo@89 9089 d3.svg.brush = function() {
paulo@89 9090 var event = d3_eventDispatch(brush, "brushstart", "brush", "brushend"), x = null, y = null, xExtent = [ 0, 0 ], yExtent = [ 0, 0 ], xExtentDomain, yExtentDomain, xClamp = true, yClamp = true, resizes = d3_svg_brushResizes[0];
paulo@89 9091 function brush(g) {
paulo@89 9092 g.each(function() {
paulo@89 9093 var g = d3.select(this).style("pointer-events", "all").style("-webkit-tap-highlight-color", "rgba(0,0,0,0)").on("mousedown.brush", brushstart).on("touchstart.brush", brushstart);
paulo@89 9094 var background = g.selectAll(".background").data([ 0 ]);
paulo@89 9095 background.enter().append("rect").attr("class", "background").style("visibility", "hidden").style("cursor", "crosshair");
paulo@89 9096 g.selectAll(".extent").data([ 0 ]).enter().append("rect").attr("class", "extent").style("cursor", "move");
paulo@89 9097 var resize = g.selectAll(".resize").data(resizes, d3_identity);
paulo@89 9098 resize.exit().remove();
paulo@89 9099 resize.enter().append("g").attr("class", function(d) {
paulo@89 9100 return "resize " + d;
paulo@89 9101 }).style("cursor", function(d) {
paulo@89 9102 return d3_svg_brushCursor[d];
paulo@89 9103 }).append("rect").attr("x", function(d) {
paulo@89 9104 return /[ew]$/.test(d) ? -3 : null;
paulo@89 9105 }).attr("y", function(d) {
paulo@89 9106 return /^[ns]/.test(d) ? -3 : null;
paulo@89 9107 }).attr("width", 6).attr("height", 6).style("visibility", "hidden");
paulo@89 9108 resize.style("display", brush.empty() ? "none" : null);
paulo@89 9109 var gUpdate = d3.transition(g), backgroundUpdate = d3.transition(background), range;
paulo@89 9110 if (x) {
paulo@89 9111 range = d3_scaleRange(x);
paulo@89 9112 backgroundUpdate.attr("x", range[0]).attr("width", range[1] - range[0]);
paulo@89 9113 redrawX(gUpdate);
paulo@89 9114 }
paulo@89 9115 if (y) {
paulo@89 9116 range = d3_scaleRange(y);
paulo@89 9117 backgroundUpdate.attr("y", range[0]).attr("height", range[1] - range[0]);
paulo@89 9118 redrawY(gUpdate);
paulo@89 9119 }
paulo@89 9120 redraw(gUpdate);
paulo@89 9121 });
paulo@89 9122 }
paulo@89 9123 brush.event = function(g) {
paulo@89 9124 g.each(function() {
paulo@89 9125 var event_ = event.of(this, arguments), extent1 = {
paulo@89 9126 x: xExtent,
paulo@89 9127 y: yExtent,
paulo@89 9128 i: xExtentDomain,
paulo@89 9129 j: yExtentDomain
paulo@89 9130 }, extent0 = this.__chart__ || extent1;
paulo@89 9131 this.__chart__ = extent1;
paulo@89 9132 if (d3_transitionInheritId) {
paulo@89 9133 d3.select(this).transition().each("start.brush", function() {
paulo@89 9134 xExtentDomain = extent0.i;
paulo@89 9135 yExtentDomain = extent0.j;
paulo@89 9136 xExtent = extent0.x;
paulo@89 9137 yExtent = extent0.y;
paulo@89 9138 event_({
paulo@89 9139 type: "brushstart"
paulo@89 9140 });
paulo@89 9141 }).tween("brush:brush", function() {
paulo@89 9142 var xi = d3_interpolateArray(xExtent, extent1.x), yi = d3_interpolateArray(yExtent, extent1.y);
paulo@89 9143 xExtentDomain = yExtentDomain = null;
paulo@89 9144 return function(t) {
paulo@89 9145 xExtent = extent1.x = xi(t);
paulo@89 9146 yExtent = extent1.y = yi(t);
paulo@89 9147 event_({
paulo@89 9148 type: "brush",
paulo@89 9149 mode: "resize"
paulo@89 9150 });
paulo@89 9151 };
paulo@89 9152 }).each("end.brush", function() {
paulo@89 9153 xExtentDomain = extent1.i;
paulo@89 9154 yExtentDomain = extent1.j;
paulo@89 9155 event_({
paulo@89 9156 type: "brush",
paulo@89 9157 mode: "resize"
paulo@89 9158 });
paulo@89 9159 event_({
paulo@89 9160 type: "brushend"
paulo@89 9161 });
paulo@89 9162 });
paulo@89 9163 } else {
paulo@89 9164 event_({
paulo@89 9165 type: "brushstart"
paulo@89 9166 });
paulo@89 9167 event_({
paulo@89 9168 type: "brush",
paulo@89 9169 mode: "resize"
paulo@89 9170 });
paulo@89 9171 event_({
paulo@89 9172 type: "brushend"
paulo@89 9173 });
paulo@89 9174 }
paulo@89 9175 });
paulo@89 9176 };
paulo@89 9177 function redraw(g) {
paulo@89 9178 g.selectAll(".resize").attr("transform", function(d) {
paulo@89 9179 return "translate(" + xExtent[+/e$/.test(d)] + "," + yExtent[+/^s/.test(d)] + ")";
paulo@89 9180 });
paulo@89 9181 }
paulo@89 9182 function redrawX(g) {
paulo@89 9183 g.select(".extent").attr("x", xExtent[0]);
paulo@89 9184 g.selectAll(".extent,.n>rect,.s>rect").attr("width", xExtent[1] - xExtent[0]);
paulo@89 9185 }
paulo@89 9186 function redrawY(g) {
paulo@89 9187 g.select(".extent").attr("y", yExtent[0]);
paulo@89 9188 g.selectAll(".extent,.e>rect,.w>rect").attr("height", yExtent[1] - yExtent[0]);
paulo@89 9189 }
paulo@89 9190 function brushstart() {
paulo@89 9191 var target = this, eventTarget = d3.select(d3.event.target), event_ = event.of(target, arguments), g = d3.select(target), resizing = eventTarget.datum(), resizingX = !/^(n|s)$/.test(resizing) && x, resizingY = !/^(e|w)$/.test(resizing) && y, dragging = eventTarget.classed("extent"), dragRestore = d3_event_dragSuppress(target), center, origin = d3.mouse(target), offset;
paulo@89 9192 var w = d3.select(d3_window(target)).on("keydown.brush", keydown).on("keyup.brush", keyup);
paulo@89 9193 if (d3.event.changedTouches) {
paulo@89 9194 w.on("touchmove.brush", brushmove).on("touchend.brush", brushend);
paulo@89 9195 } else {
paulo@89 9196 w.on("mousemove.brush", brushmove).on("mouseup.brush", brushend);
paulo@89 9197 }
paulo@89 9198 g.interrupt().selectAll("*").interrupt();
paulo@89 9199 if (dragging) {
paulo@89 9200 origin[0] = xExtent[0] - origin[0];
paulo@89 9201 origin[1] = yExtent[0] - origin[1];
paulo@89 9202 } else if (resizing) {
paulo@89 9203 var ex = +/w$/.test(resizing), ey = +/^n/.test(resizing);
paulo@89 9204 offset = [ xExtent[1 - ex] - origin[0], yExtent[1 - ey] - origin[1] ];
paulo@89 9205 origin[0] = xExtent[ex];
paulo@89 9206 origin[1] = yExtent[ey];
paulo@89 9207 } else if (d3.event.altKey) center = origin.slice();
paulo@89 9208 g.style("pointer-events", "none").selectAll(".resize").style("display", null);
paulo@89 9209 d3.select("body").style("cursor", eventTarget.style("cursor"));
paulo@89 9210 event_({
paulo@89 9211 type: "brushstart"
paulo@89 9212 });
paulo@89 9213 brushmove();
paulo@89 9214 function keydown() {
paulo@89 9215 if (d3.event.keyCode == 32) {
paulo@89 9216 if (!dragging) {
paulo@89 9217 center = null;
paulo@89 9218 origin[0] -= xExtent[1];
paulo@89 9219 origin[1] -= yExtent[1];
paulo@89 9220 dragging = 2;
paulo@89 9221 }
paulo@89 9222 d3_eventPreventDefault();
paulo@89 9223 }
paulo@89 9224 }
paulo@89 9225 function keyup() {
paulo@89 9226 if (d3.event.keyCode == 32 && dragging == 2) {
paulo@89 9227 origin[0] += xExtent[1];
paulo@89 9228 origin[1] += yExtent[1];
paulo@89 9229 dragging = 0;
paulo@89 9230 d3_eventPreventDefault();
paulo@89 9231 }
paulo@89 9232 }
paulo@89 9233 function brushmove() {
paulo@89 9234 var point = d3.mouse(target), moved = false;
paulo@89 9235 if (offset) {
paulo@89 9236 point[0] += offset[0];
paulo@89 9237 point[1] += offset[1];
paulo@89 9238 }
paulo@89 9239 if (!dragging) {
paulo@89 9240 if (d3.event.altKey) {
paulo@89 9241 if (!center) center = [ (xExtent[0] + xExtent[1]) / 2, (yExtent[0] + yExtent[1]) / 2 ];
paulo@89 9242 origin[0] = xExtent[+(point[0] < center[0])];
paulo@89 9243 origin[1] = yExtent[+(point[1] < center[1])];
paulo@89 9244 } else center = null;
paulo@89 9245 }
paulo@89 9246 if (resizingX && move1(point, x, 0)) {
paulo@89 9247 redrawX(g);
paulo@89 9248 moved = true;
paulo@89 9249 }
paulo@89 9250 if (resizingY && move1(point, y, 1)) {
paulo@89 9251 redrawY(g);
paulo@89 9252 moved = true;
paulo@89 9253 }
paulo@89 9254 if (moved) {
paulo@89 9255 redraw(g);
paulo@89 9256 event_({
paulo@89 9257 type: "brush",
paulo@89 9258 mode: dragging ? "move" : "resize"
paulo@89 9259 });
paulo@89 9260 }
paulo@89 9261 }
paulo@89 9262 function move1(point, scale, i) {
paulo@89 9263 var range = d3_scaleRange(scale), r0 = range[0], r1 = range[1], position = origin[i], extent = i ? yExtent : xExtent, size = extent[1] - extent[0], min, max;
paulo@89 9264 if (dragging) {
paulo@89 9265 r0 -= position;
paulo@89 9266 r1 -= size + position;
paulo@89 9267 }
paulo@89 9268 min = (i ? yClamp : xClamp) ? Math.max(r0, Math.min(r1, point[i])) : point[i];
paulo@89 9269 if (dragging) {
paulo@89 9270 max = (min += position) + size;
paulo@89 9271 } else {
paulo@89 9272 if (center) position = Math.max(r0, Math.min(r1, 2 * center[i] - min));
paulo@89 9273 if (position < min) {
paulo@89 9274 max = min;
paulo@89 9275 min = position;
paulo@89 9276 } else {
paulo@89 9277 max = position;
paulo@89 9278 }
paulo@89 9279 }
paulo@89 9280 if (extent[0] != min || extent[1] != max) {
paulo@89 9281 if (i) yExtentDomain = null; else xExtentDomain = null;
paulo@89 9282 extent[0] = min;
paulo@89 9283 extent[1] = max;
paulo@89 9284 return true;
paulo@89 9285 }
paulo@89 9286 }
paulo@89 9287 function brushend() {
paulo@89 9288 brushmove();
paulo@89 9289 g.style("pointer-events", "all").selectAll(".resize").style("display", brush.empty() ? "none" : null);
paulo@89 9290 d3.select("body").style("cursor", null);
paulo@89 9291 w.on("mousemove.brush", null).on("mouseup.brush", null).on("touchmove.brush", null).on("touchend.brush", null).on("keydown.brush", null).on("keyup.brush", null);
paulo@89 9292 dragRestore();
paulo@89 9293 event_({
paulo@89 9294 type: "brushend"
paulo@89 9295 });
paulo@89 9296 }
paulo@89 9297 }
paulo@89 9298 brush.x = function(z) {
paulo@89 9299 if (!arguments.length) return x;
paulo@89 9300 x = z;
paulo@89 9301 resizes = d3_svg_brushResizes[!x << 1 | !y];
paulo@89 9302 return brush;
paulo@89 9303 };
paulo@89 9304 brush.y = function(z) {
paulo@89 9305 if (!arguments.length) return y;
paulo@89 9306 y = z;
paulo@89 9307 resizes = d3_svg_brushResizes[!x << 1 | !y];
paulo@89 9308 return brush;
paulo@89 9309 };
paulo@89 9310 brush.clamp = function(z) {
paulo@89 9311 if (!arguments.length) return x && y ? [ xClamp, yClamp ] : x ? xClamp : y ? yClamp : null;
paulo@89 9312 if (x && y) xClamp = !!z[0], yClamp = !!z[1]; else if (x) xClamp = !!z; else if (y) yClamp = !!z;
paulo@89 9313 return brush;
paulo@89 9314 };
paulo@89 9315 brush.extent = function(z) {
paulo@89 9316 var x0, x1, y0, y1, t;
paulo@89 9317 if (!arguments.length) {
paulo@89 9318 if (x) {
paulo@89 9319 if (xExtentDomain) {
paulo@89 9320 x0 = xExtentDomain[0], x1 = xExtentDomain[1];
paulo@89 9321 } else {
paulo@89 9322 x0 = xExtent[0], x1 = xExtent[1];
paulo@89 9323 if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1);
paulo@89 9324 if (x1 < x0) t = x0, x0 = x1, x1 = t;
paulo@89 9325 }
paulo@89 9326 }
paulo@89 9327 if (y) {
paulo@89 9328 if (yExtentDomain) {
paulo@89 9329 y0 = yExtentDomain[0], y1 = yExtentDomain[1];
paulo@89 9330 } else {
paulo@89 9331 y0 = yExtent[0], y1 = yExtent[1];
paulo@89 9332 if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1);
paulo@89 9333 if (y1 < y0) t = y0, y0 = y1, y1 = t;
paulo@89 9334 }
paulo@89 9335 }
paulo@89 9336 return x && y ? [ [ x0, y0 ], [ x1, y1 ] ] : x ? [ x0, x1 ] : y && [ y0, y1 ];
paulo@89 9337 }
paulo@89 9338 if (x) {
paulo@89 9339 x0 = z[0], x1 = z[1];
paulo@89 9340 if (y) x0 = x0[0], x1 = x1[0];
paulo@89 9341 xExtentDomain = [ x0, x1 ];
paulo@89 9342 if (x.invert) x0 = x(x0), x1 = x(x1);
paulo@89 9343 if (x1 < x0) t = x0, x0 = x1, x1 = t;
paulo@89 9344 if (x0 != xExtent[0] || x1 != xExtent[1]) xExtent = [ x0, x1 ];
paulo@89 9345 }
paulo@89 9346 if (y) {
paulo@89 9347 y0 = z[0], y1 = z[1];
paulo@89 9348 if (x) y0 = y0[1], y1 = y1[1];
paulo@89 9349 yExtentDomain = [ y0, y1 ];
paulo@89 9350 if (y.invert) y0 = y(y0), y1 = y(y1);
paulo@89 9351 if (y1 < y0) t = y0, y0 = y1, y1 = t;
paulo@89 9352 if (y0 != yExtent[0] || y1 != yExtent[1]) yExtent = [ y0, y1 ];
paulo@89 9353 }
paulo@89 9354 return brush;
paulo@89 9355 };
paulo@89 9356 brush.clear = function() {
paulo@89 9357 if (!brush.empty()) {
paulo@89 9358 xExtent = [ 0, 0 ], yExtent = [ 0, 0 ];
paulo@89 9359 xExtentDomain = yExtentDomain = null;
paulo@89 9360 }
paulo@89 9361 return brush;
paulo@89 9362 };
paulo@89 9363 brush.empty = function() {
paulo@89 9364 return !!x && xExtent[0] == xExtent[1] || !!y && yExtent[0] == yExtent[1];
paulo@89 9365 };
paulo@89 9366 return d3.rebind(brush, event, "on");
paulo@89 9367 };
paulo@89 9368 var d3_svg_brushCursor = {
paulo@89 9369 n: "ns-resize",
paulo@89 9370 e: "ew-resize",
paulo@89 9371 s: "ns-resize",
paulo@89 9372 w: "ew-resize",
paulo@89 9373 nw: "nwse-resize",
paulo@89 9374 ne: "nesw-resize",
paulo@89 9375 se: "nwse-resize",
paulo@89 9376 sw: "nesw-resize"
paulo@89 9377 };
paulo@89 9378 var d3_svg_brushResizes = [ [ "n", "e", "s", "w", "nw", "ne", "se", "sw" ], [ "e", "w" ], [ "n", "s" ], [] ];
paulo@89 9379 var d3_time_format = d3_time.format = d3_locale_enUS.timeFormat;
paulo@89 9380 var d3_time_formatUtc = d3_time_format.utc;
paulo@89 9381 var d3_time_formatIso = d3_time_formatUtc("%Y-%m-%dT%H:%M:%S.%LZ");
paulo@89 9382 d3_time_format.iso = Date.prototype.toISOString && +new Date("2000-01-01T00:00:00.000Z") ? d3_time_formatIsoNative : d3_time_formatIso;
paulo@89 9383 function d3_time_formatIsoNative(date) {
paulo@89 9384 return date.toISOString();
paulo@89 9385 }
paulo@89 9386 d3_time_formatIsoNative.parse = function(string) {
paulo@89 9387 var date = new Date(string);
paulo@89 9388 return isNaN(date) ? null : date;
paulo@89 9389 };
paulo@89 9390 d3_time_formatIsoNative.toString = d3_time_formatIso.toString;
paulo@89 9391 d3_time.second = d3_time_interval(function(date) {
paulo@89 9392 return new d3_date(Math.floor(date / 1e3) * 1e3);
paulo@89 9393 }, function(date, offset) {
paulo@89 9394 date.setTime(date.getTime() + Math.floor(offset) * 1e3);
paulo@89 9395 }, function(date) {
paulo@89 9396 return date.getSeconds();
paulo@89 9397 });
paulo@89 9398 d3_time.seconds = d3_time.second.range;
paulo@89 9399 d3_time.seconds.utc = d3_time.second.utc.range;
paulo@89 9400 d3_time.minute = d3_time_interval(function(date) {
paulo@89 9401 return new d3_date(Math.floor(date / 6e4) * 6e4);
paulo@89 9402 }, function(date, offset) {
paulo@89 9403 date.setTime(date.getTime() + Math.floor(offset) * 6e4);
paulo@89 9404 }, function(date) {
paulo@89 9405 return date.getMinutes();
paulo@89 9406 });
paulo@89 9407 d3_time.minutes = d3_time.minute.range;
paulo@89 9408 d3_time.minutes.utc = d3_time.minute.utc.range;
paulo@89 9409 d3_time.hour = d3_time_interval(function(date) {
paulo@89 9410 var timezone = date.getTimezoneOffset() / 60;
paulo@89 9411 return new d3_date((Math.floor(date / 36e5 - timezone) + timezone) * 36e5);
paulo@89 9412 }, function(date, offset) {
paulo@89 9413 date.setTime(date.getTime() + Math.floor(offset) * 36e5);
paulo@89 9414 }, function(date) {
paulo@89 9415 return date.getHours();
paulo@89 9416 });
paulo@89 9417 d3_time.hours = d3_time.hour.range;
paulo@89 9418 d3_time.hours.utc = d3_time.hour.utc.range;
paulo@89 9419 d3_time.month = d3_time_interval(function(date) {
paulo@89 9420 date = d3_time.day(date);
paulo@89 9421 date.setDate(1);
paulo@89 9422 return date;
paulo@89 9423 }, function(date, offset) {
paulo@89 9424 date.setMonth(date.getMonth() + offset);
paulo@89 9425 }, function(date) {
paulo@89 9426 return date.getMonth();
paulo@89 9427 });
paulo@89 9428 d3_time.months = d3_time.month.range;
paulo@89 9429 d3_time.months.utc = d3_time.month.utc.range;
paulo@89 9430 function d3_time_scale(linear, methods, format) {
paulo@89 9431 function scale(x) {
paulo@89 9432 return linear(x);
paulo@89 9433 }
paulo@89 9434 scale.invert = function(x) {
paulo@89 9435 return d3_time_scaleDate(linear.invert(x));
paulo@89 9436 };
paulo@89 9437 scale.domain = function(x) {
paulo@89 9438 if (!arguments.length) return linear.domain().map(d3_time_scaleDate);
paulo@89 9439 linear.domain(x);
paulo@89 9440 return scale;
paulo@89 9441 };
paulo@89 9442 function tickMethod(extent, count) {
paulo@89 9443 var span = extent[1] - extent[0], target = span / count, i = d3.bisect(d3_time_scaleSteps, target);
paulo@89 9444 return i == d3_time_scaleSteps.length ? [ methods.year, d3_scale_linearTickRange(extent.map(function(d) {
paulo@89 9445 return d / 31536e6;
paulo@89 9446 }), count)[2] ] : !i ? [ d3_time_scaleMilliseconds, d3_scale_linearTickRange(extent, count)[2] ] : methods[target / d3_time_scaleSteps[i - 1] < d3_time_scaleSteps[i] / target ? i - 1 : i];
paulo@89 9447 }
paulo@89 9448 scale.nice = function(interval, skip) {
paulo@89 9449 var domain = scale.domain(), extent = d3_scaleExtent(domain), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" && tickMethod(extent, interval);
paulo@89 9450 if (method) interval = method[0], skip = method[1];
paulo@89 9451 function skipped(date) {
paulo@89 9452 return !isNaN(date) && !interval.range(date, d3_time_scaleDate(+date + 1), skip).length;
paulo@89 9453 }
paulo@89 9454 return scale.domain(d3_scale_nice(domain, skip > 1 ? {
paulo@89 9455 floor: function(date) {
paulo@89 9456 while (skipped(date = interval.floor(date))) date = d3_time_scaleDate(date - 1);
paulo@89 9457 return date;
paulo@89 9458 },
paulo@89 9459 ceil: function(date) {
paulo@89 9460 while (skipped(date = interval.ceil(date))) date = d3_time_scaleDate(+date + 1);
paulo@89 9461 return date;
paulo@89 9462 }
paulo@89 9463 } : interval));
paulo@89 9464 };
paulo@89 9465 scale.ticks = function(interval, skip) {
paulo@89 9466 var extent = d3_scaleExtent(scale.domain()), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" ? tickMethod(extent, interval) : !interval.range && [ {
paulo@89 9467 range: interval
paulo@89 9468 }, skip ];
paulo@89 9469 if (method) interval = method[0], skip = method[1];
paulo@89 9470 return interval.range(extent[0], d3_time_scaleDate(+extent[1] + 1), skip < 1 ? 1 : skip);
paulo@89 9471 };
paulo@89 9472 scale.tickFormat = function() {
paulo@89 9473 return format;
paulo@89 9474 };
paulo@89 9475 scale.copy = function() {
paulo@89 9476 return d3_time_scale(linear.copy(), methods, format);
paulo@89 9477 };
paulo@89 9478 return d3_scale_linearRebind(scale, linear);
paulo@89 9479 }
paulo@89 9480 function d3_time_scaleDate(t) {
paulo@89 9481 return new Date(t);
paulo@89 9482 }
paulo@89 9483 var d3_time_scaleSteps = [ 1e3, 5e3, 15e3, 3e4, 6e4, 3e5, 9e5, 18e5, 36e5, 108e5, 216e5, 432e5, 864e5, 1728e5, 6048e5, 2592e6, 7776e6, 31536e6 ];
paulo@89 9484 var d3_time_scaleLocalMethods = [ [ d3_time.second, 1 ], [ d3_time.second, 5 ], [ d3_time.second, 15 ], [ d3_time.second, 30 ], [ d3_time.minute, 1 ], [ d3_time.minute, 5 ], [ d3_time.minute, 15 ], [ d3_time.minute, 30 ], [ d3_time.hour, 1 ], [ d3_time.hour, 3 ], [ d3_time.hour, 6 ], [ d3_time.hour, 12 ], [ d3_time.day, 1 ], [ d3_time.day, 2 ], [ d3_time.week, 1 ], [ d3_time.month, 1 ], [ d3_time.month, 3 ], [ d3_time.year, 1 ] ];
paulo@89 9485 var d3_time_scaleLocalFormat = d3_time_format.multi([ [ ".%L", function(d) {
paulo@89 9486 return d.getMilliseconds();
paulo@89 9487 } ], [ ":%S", function(d) {
paulo@89 9488 return d.getSeconds();
paulo@89 9489 } ], [ "%I:%M", function(d) {
paulo@89 9490 return d.getMinutes();
paulo@89 9491 } ], [ "%I %p", function(d) {
paulo@89 9492 return d.getHours();
paulo@89 9493 } ], [ "%a %d", function(d) {
paulo@89 9494 return d.getDay() && d.getDate() != 1;
paulo@89 9495 } ], [ "%b %d", function(d) {
paulo@89 9496 return d.getDate() != 1;
paulo@89 9497 } ], [ "%B", function(d) {
paulo@89 9498 return d.getMonth();
paulo@89 9499 } ], [ "%Y", d3_true ] ]);
paulo@89 9500 var d3_time_scaleMilliseconds = {
paulo@89 9501 range: function(start, stop, step) {
paulo@89 9502 return d3.range(Math.ceil(start / step) * step, +stop, step).map(d3_time_scaleDate);
paulo@89 9503 },
paulo@89 9504 floor: d3_identity,
paulo@89 9505 ceil: d3_identity
paulo@89 9506 };
paulo@89 9507 d3_time_scaleLocalMethods.year = d3_time.year;
paulo@89 9508 d3_time.scale = function() {
paulo@89 9509 return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat);
paulo@89 9510 };
paulo@89 9511 var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) {
paulo@89 9512 return [ m[0].utc, m[1] ];
paulo@89 9513 });
paulo@89 9514 var d3_time_scaleUtcFormat = d3_time_formatUtc.multi([ [ ".%L", function(d) {
paulo@89 9515 return d.getUTCMilliseconds();
paulo@89 9516 } ], [ ":%S", function(d) {
paulo@89 9517 return d.getUTCSeconds();
paulo@89 9518 } ], [ "%I:%M", function(d) {
paulo@89 9519 return d.getUTCMinutes();
paulo@89 9520 } ], [ "%I %p", function(d) {
paulo@89 9521 return d.getUTCHours();
paulo@89 9522 } ], [ "%a %d", function(d) {
paulo@89 9523 return d.getUTCDay() && d.getUTCDate() != 1;
paulo@89 9524 } ], [ "%b %d", function(d) {
paulo@89 9525 return d.getUTCDate() != 1;
paulo@89 9526 } ], [ "%B", function(d) {
paulo@89 9527 return d.getUTCMonth();
paulo@89 9528 } ], [ "%Y", d3_true ] ]);
paulo@89 9529 d3_time_scaleUtcMethods.year = d3_time.year.utc;
paulo@89 9530 d3_time.scale.utc = function() {
paulo@89 9531 return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_time_scaleUtcFormat);
paulo@89 9532 };
paulo@89 9533 d3.text = d3_xhrType(function(request) {
paulo@89 9534 return request.responseText;
paulo@89 9535 });
paulo@89 9536 d3.json = function(url, callback) {
paulo@89 9537 return d3_xhr(url, "application/json", d3_json, callback);
paulo@89 9538 };
paulo@89 9539 function d3_json(request) {
paulo@89 9540 return JSON.parse(request.responseText);
paulo@89 9541 }
paulo@89 9542 d3.html = function(url, callback) {
paulo@89 9543 return d3_xhr(url, "text/html", d3_html, callback);
paulo@89 9544 };
paulo@89 9545 function d3_html(request) {
paulo@89 9546 var range = d3_document.createRange();
paulo@89 9547 range.selectNode(d3_document.body);
paulo@89 9548 return range.createContextualFragment(request.responseText);
paulo@89 9549 }
paulo@89 9550 d3.xml = d3_xhrType(function(request) {
paulo@89 9551 return request.responseXML;
paulo@89 9552 });
paulo@89 9553 if (typeof define === "function" && define.amd) this.d3 = d3, define(d3); else if (typeof module === "object" && module.exports) module.exports = d3; else this.d3 = d3;
paulo@89 9554 }();