1 (function(){if (!Date.now) Date.now = function() {
5 document.createElement("div").style.setProperty("opacity", 0, "");
7 var d3_style_prototype = CSSStyleDeclaration.prototype,
8 d3_style_setProperty = d3_style_prototype.setProperty;
9 d3_style_prototype.setProperty = function(name, value, priority) {
10 d3_style_setProperty.call(this, name, value + "", priority);
13 d3 = {version: "2.6.0"}; // semver
14 var d3_array = d3_arraySlice; // conversion for NodeLists
16 function d3_arrayCopy(pseudoarray) {
17 var i = -1, n = pseudoarray.length, array = [];
18 while (++i < n) array.push(pseudoarray[i]);
22 function d3_arraySlice(pseudoarray) {
23 return Array.prototype.slice.call(pseudoarray);
27 d3_array(document.documentElement.childNodes)[0].nodeType;
29 d3_array = d3_arrayCopy;
32 var d3_arraySubclass = [].__proto__?
34 // Until ECMAScript supports array subclassing, prototype injection works well.
35 function(array, prototype) {
36 array.__proto__ = prototype;
39 // And if your browser doesn't support __proto__, we'll use direct extension.
40 function(array, prototype) {
41 for (var property in prototype) array[property] = prototype[property];
46 d3.functor = function(v) {
47 return typeof v === "function" ? v : function() { return v; };
49 // Copies a variable number of methods from source to target.
50 d3.rebind = function(target, source) {
51 var i = 1, n = arguments.length, method;
52 while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]);
56 // Method is assumed to be a standard D3 getter-setter:
57 // If passed with no arguments, gets the value.
58 // If passed with arguments, sets the value and returns the target.
59 function d3_rebind(target, source, method) {
61 var value = method.apply(source, arguments);
62 return arguments.length ? target : value;
65 d3.ascending = function(a, b) {
66 return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
68 d3.descending = function(a, b) {
69 return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
71 d3.mean = function(array, f) {
77 if (arguments.length === 1) {
78 while (++i < n) if (d3_number(a = array[i])) m += (a - m) / ++j;
80 while (++i < n) if (d3_number(a = f.call(array, array[i], i))) m += (a - m) / ++j;
82 return j ? m : undefined;
84 d3.median = function(array, f) {
85 if (arguments.length > 1) array = array.map(f);
86 array = array.filter(d3_number);
87 return array.length ? d3.quantile(array.sort(d3.ascending), .5) : undefined;
89 d3.min = function(array, f) {
94 if (arguments.length === 1) {
95 while (++i < n && ((a = array[i]) == null || a != a)) a = undefined;
96 while (++i < n) if ((b = array[i]) != null && a > b) a = b;
98 while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined;
99 while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
103 d3.max = function(array, f) {
108 if (arguments.length === 1) {
109 while (++i < n && ((a = array[i]) == null || a != a)) a = undefined;
110 while (++i < n) if ((b = array[i]) != null && b > a) a = b;
112 while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined;
113 while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
117 d3.extent = function(array, f) {
123 if (arguments.length === 1) {
124 while (++i < n && ((a = c = array[i]) == null || a != a)) a = c = undefined;
125 while (++i < n) if ((b = array[i]) != null) {
130 while (++i < n && ((a = c = f.call(array, array[i], i)) == null || a != a)) a = undefined;
131 while (++i < n) if ((b = f.call(array, array[i], i)) != null) {
139 normal: function(mean, deviation) {
140 if (arguments.length < 2) deviation = 1;
141 if (arguments.length < 1) mean = 0;
145 x = Math.random() * 2 - 1;
146 y = Math.random() * 2 - 1;
148 } while (!r || r > 1);
149 return mean + deviation * x * Math.sqrt(-2 * Math.log(r) / r);
153 function d3_number(x) {
154 return x != null && !isNaN(x);
156 d3.sum = function(array, f) {
162 if (arguments.length === 1) {
163 while (++i < n) if (!isNaN(a = +array[i])) s += a;
165 while (++i < n) if (!isNaN(a = +f.call(array, array[i], i))) s += a;
170 // R-7 per <http://en.wikipedia.org/wiki/Quantile>
171 d3.quantile = function(values, p) {
172 var H = (values.length - 1) * p + 1,
176 return e ? v + e * (values[h] - v) : v;
178 d3.transpose = function(matrix) {
179 return d3.zip.apply(d3, matrix);
181 d3.zip = function() {
182 if (!(n = arguments.length)) return [];
183 for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m;) {
184 for (var j = -1, n, zip = zips[i] = new Array(n); ++j < n;) {
185 zip[j] = arguments[j][i];
191 function d3_zipLength(d) {
194 // Locate the insertion point for x in a to maintain sorted order. The
195 // arguments lo and hi may be used to specify a subset of the array which should
196 // be considered; by default the entire array is used. If x is already present
197 // in a, the insertion point will be before (to the left of) any existing
198 // entries. The return value is suitable for use as the first argument to
199 // `array.splice` assuming that a is already sorted.
201 // The returned insertion point i partitions the array a into two halves so that
202 // all v < x for v in a[lo:i] for the left side and all v >= x for v in a[i:hi]
203 // for the right side.
204 d3.bisectLeft = function(a, x, lo, hi) {
205 if (arguments.length < 3) lo = 0;
206 if (arguments.length < 4) hi = a.length;
208 var mid = (lo + hi) >> 1;
209 if (a[mid] < x) lo = mid + 1;
215 // Similar to bisectLeft, but returns an insertion point which comes after (to
216 // the right of) any existing entries of x in a.
218 // The returned insertion point i partitions the array into two halves so that
219 // all v <= x for v in a[lo:i] for the left side and all v > x for v in a[i:hi]
220 // for the right side.
222 d3.bisectRight = function(a, x, lo, hi) {
223 if (arguments.length < 3) lo = 0;
224 if (arguments.length < 4) hi = a.length;
226 var mid = (lo + hi) >> 1;
227 if (x < a[mid]) hi = mid;
232 d3.first = function(array, f) {
237 if (arguments.length === 1) f = d3.ascending;
239 if (f.call(array, a, b = array[i]) > 0) {
245 d3.last = function(array, f) {
250 if (arguments.length === 1) f = d3.ascending;
252 if (f.call(array, a, b = array[i]) <= 0) {
258 d3.nest = function() {
265 function map(array, depth) {
266 if (depth >= keys.length) return rollup
267 ? rollup.call(nest, array) : (sortValues
268 ? array.sort(sortValues)
279 if ((keyValue = key(object = array[i])) in o) {
280 o[keyValue].push(object);
282 o[keyValue] = [object];
286 for (keyValue in o) {
287 o[keyValue] = map(o[keyValue], depth);
293 function entries(map, depth) {
294 if (depth >= keys.length) return map;
297 sortKey = sortKeys[depth++],
301 a.push({key: key, values: entries(map[key], depth)});
304 if (sortKey) a.sort(function(a, b) {
305 return sortKey(a.key, b.key);
311 nest.map = function(array) {
312 return map(array, 0);
315 nest.entries = function(array) {
316 return entries(map(array, 0), 0);
319 nest.key = function(d) {
324 // Specifies the order for the most-recently specified key.
325 // Note: only applies to entries. Map keys are unordered!
326 nest.sortKeys = function(order) {
327 sortKeys[keys.length - 1] = order;
331 // Specifies the order for leaf values.
332 // Applies to both maps and entries array.
333 nest.sortValues = function(order) {
338 nest.rollup = function(f) {
345 d3.keys = function(map) {
347 for (var key in map) keys.push(key);
350 d3.values = function(map) {
352 for (var key in map) values.push(map[key]);
355 d3.entries = function(map) {
357 for (var key in map) entries.push({key: key, value: map[key]});
360 d3.permute = function(array, indexes) {
364 while (++i < n) permutes[i] = array[indexes[i]];
367 d3.merge = function(arrays) {
368 return Array.prototype.concat.apply([], arrays);
370 d3.split = function(array, f) {
376 if (arguments.length < 2) f = d3_splitter;
378 if (f.call(values, value = array[i], i)) {
381 if (!values.length) arrays.push(values);
388 function d3_splitter(d) {
391 function d3_collapse(s) {
392 return s.replace(/(^\s+)|(\s+$)/g, "").replace(/\s+/g, " ");
395 * @param {number} start
396 * @param {number=} stop
397 * @param {number=} step
399 d3.range = function(start, stop, step) {
400 if (arguments.length < 3) {
402 if (arguments.length < 2) {
407 if ((stop - start) / step == Infinity) throw new Error("infinite range");
411 if (step < 0) while ((j = start + step * ++i) > stop) range.push(j);
412 else while ((j = start + step * ++i) < stop) range.push(j);
415 d3.requote = function(s) {
416 return s.replace(d3_requote_re, "\\$&");
419 var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;
420 d3.round = function(x, n) {
422 ? Math.round(x * Math.pow(10, n)) * Math.pow(10, -n)
425 d3.xhr = function(url, mime, callback) {
426 var req = new XMLHttpRequest;
427 if (arguments.length < 3) callback = mime;
428 else if (mime && req.overrideMimeType) req.overrideMimeType(mime);
429 req.open("GET", url, true);
430 req.onreadystatechange = function() {
431 if (req.readyState === 4) callback(req.status < 300 ? req : null);
435 d3.text = function(url, mime, callback) {
436 function ready(req) {
437 callback(req && req.responseText);
439 if (arguments.length < 3) {
443 d3.xhr(url, mime, ready);
445 d3.json = function(url, callback) {
446 d3.text(url, "application/json", function(text) {
447 callback(text ? JSON.parse(text) : null);
450 d3.html = function(url, callback) {
451 d3.text(url, "text/html", function(text) {
452 if (text != null) { // Treat empty string as valid HTML.
453 var range = document.createRange();
454 range.selectNode(document.body);
455 text = range.createContextualFragment(text);
460 d3.xml = function(url, mime, callback) {
461 function ready(req) {
462 callback(req && req.responseXML);
464 if (arguments.length < 3) {
468 d3.xhr(url, mime, ready);
471 svg: "http://www.w3.org/2000/svg",
472 xhtml: "http://www.w3.org/1999/xhtml",
473 xlink: "http://www.w3.org/1999/xlink",
474 xml: "http://www.w3.org/XML/1998/namespace",
475 xmlns: "http://www.w3.org/2000/xmlns/"
480 qualify: function(name) {
481 var i = name.indexOf(":");
482 return i < 0 ? (name in d3_nsPrefix
483 ? {space: d3_nsPrefix[name], local: name} : name)
484 : {space: d3_nsPrefix[name.substring(0, i)], local: name.substring(i + 1)};
487 d3.dispatch = function() {
488 var dispatch = new d3_dispatch(),
490 n = arguments.length;
491 while (++i < n) dispatch[arguments[i]] = d3_dispatch_event();
495 function d3_dispatch() {}
497 d3_dispatch.prototype.on = function(type, listener) {
498 var i = type.indexOf("."),
501 // Extract optional namespace, e.g., "click.foo"
503 name = type.substring(i + 1);
504 type = type.substring(0, i);
507 return arguments.length < 2
508 ? this[type].on(name)
509 : (this[type].on(name, listener), this);
512 function d3_dispatch_event() {
516 function dispatch() {
517 var z = listeners, // defensive reference
521 while (++i < n) if (l = z[i].on) l.apply(this, arguments);
524 dispatch.on = function(name, listener) {
527 // return the current listener, if any
528 if (arguments.length < 2) return (l = listenerByName[name]) && l.on;
530 // remove the old listener, if any (with copy-on-write)
531 if (l = listenerByName[name]) {
533 listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1));
534 delete listenerByName[name];
537 // add the new listener, if any
539 listeners.push(listenerByName[name] = {on: listener});
548 d3.format = function(specifier) {
549 var match = d3_format_re.exec(specifier),
550 fill = match[1] || " ",
551 sign = match[3] || "",
555 precision = match[8],
561 if (precision) precision = +precision.substring(1);
564 fill = "0"; // TODO align = "=";
565 if (comma) width -= Math.floor((width - 1) / 4);
569 case "n": comma = true; type = "g"; break;
570 case "%": scale = 100; suffix = "%"; type = "f"; break;
571 case "p": scale = 100; suffix = "%"; type = "r"; break;
572 case "d": integer = true; precision = 0; break;
573 case "s": scale = -1; type = "r"; break;
576 // If no precision is specified for r, fallback to general notation.
577 if (type == "r" && !precision) type = "g";
579 type = d3_format_types[type] || d3_format_typeDefault;
581 return function(value) {
583 // Return the empty string for floats formatted as ints.
584 if (integer && (value % 1)) return "";
586 // Convert negative to positive, and record the sign prefix.
587 var negative = (value < 0) && (value = -value) ? "\u2212" : sign;
589 // Apply the scale, computing it from the value's exponent for si format.
591 var prefix = d3.formatPrefix(value, precision);
592 value *= prefix.scale;
593 suffix = prefix.symbol;
598 // Convert to the desired precision.
599 value = type(value, precision);
601 // If the fill character is 0, the sign and group is applied after the fill.
603 var length = value.length + negative.length;
604 if (length < width) value = new Array(width - length + 1).join(fill) + value;
605 if (comma) value = d3_format_group(value);
606 value = negative + value;
609 // Otherwise (e.g., space-filling), the sign and group is applied before.
611 if (comma) value = d3_format_group(value);
612 value = negative + value;
613 var length = value.length;
614 if (length < width) value = new Array(width - length + 1).join(fill) + value;
617 return value + suffix;
621 // [[fill]align][sign][#][0][width][,][.precision][type]
622 var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/;
624 var d3_format_types = {
625 g: function(x, p) { return x.toPrecision(p); },
626 e: function(x, p) { return x.toExponential(p); },
627 f: function(x, p) { return x.toFixed(p); },
628 r: function(x, p) { return d3.round(x, p = d3_format_precision(x, p)).toFixed(Math.max(0, Math.min(20, p))); }
631 function d3_format_precision(x, p) {
632 return p - (x ? 1 + Math.floor(Math.log(x + Math.pow(10, 1 + Math.floor(Math.log(x) / Math.LN10) - p)) / Math.LN10) : 1);
635 function d3_format_typeDefault(x) {
639 // Apply comma grouping for thousands.
640 function d3_format_group(value) {
641 var i = value.lastIndexOf("."),
642 f = i >= 0 ? value.substring(i) : (i = value.length, ""),
644 while (i > 0) t.push(value.substring(i -= 3, i + 3));
645 return t.reverse().join(",") + f;
647 var d3_formatPrefixes = ["y","z","a","f","p","n","μ","m","","k","M","G","T","P","E","Z","Y"].map(d3_formatPrefix);
649 d3.formatPrefix = function(value, precision) {
652 if (value < 0) value *= -1;
653 if (precision) value = d3.round(value, d3_format_precision(value, precision));
654 i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10);
655 i = Math.max(-24, Math.min(24, Math.floor((i <= 0 ? i + 1 : i - 1) / 3) * 3));
657 return d3_formatPrefixes[8 + i / 3];
660 function d3_formatPrefix(d, i) {
662 scale: Math.pow(10, (8 - i) * 3),
668 * TERMS OF USE - EASING EQUATIONS
670 * Open source under the BSD License.
672 * Copyright 2001 Robert Penner
673 * All rights reserved.
675 * Redistribution and use in source and binary forms, with or without
676 * modification, are permitted provided that the following conditions are met:
678 * - Redistributions of source code must retain the above copyright notice, this
679 * list of conditions and the following disclaimer.
681 * - Redistributions in binary form must reproduce the above copyright notice,
682 * this list of conditions and the following disclaimer in the documentation
683 * and/or other materials provided with the distribution.
685 * - Neither the name of the author nor the names of contributors may be used to
686 * endorse or promote products derived from this software without specific
687 * prior written permission.
689 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
690 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
691 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
692 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
693 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
694 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
695 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
696 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
697 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
698 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
699 * POSSIBILITY OF SUCH DAMAGE.
702 var d3_ease_quad = d3_ease_poly(2),
703 d3_ease_cubic = d3_ease_poly(3);
706 linear: function() { return d3_ease_linear; },
708 quad: function() { return d3_ease_quad; },
709 cubic: function() { return d3_ease_cubic; },
710 sin: function() { return d3_ease_sin; },
711 exp: function() { return d3_ease_exp; },
712 circle: function() { return d3_ease_circle; },
713 elastic: d3_ease_elastic,
715 bounce: function() { return d3_ease_bounce; }
719 "in": function(f) { return f; },
720 "out": d3_ease_reverse,
721 "in-out": d3_ease_reflect,
722 "out-in": function(f) { return d3_ease_reflect(d3_ease_reverse(f)); }
725 d3.ease = function(name) {
726 var i = name.indexOf("-"),
727 t = i >= 0 ? name.substring(0, i) : name,
728 m = i >= 0 ? name.substring(i + 1) : "in";
729 return d3_ease_clamp(d3_ease_mode[m](d3_ease[t].apply(null, Array.prototype.slice.call(arguments, 1))));
732 function d3_ease_clamp(f) {
734 return t <= 0 ? 0 : t >= 1 ? 1 : f(t);
738 function d3_ease_reverse(f) {
744 function d3_ease_reflect(f) {
746 return .5 * (t < .5 ? f(2 * t) : (2 - f(2 - 2 * t)));
750 function d3_ease_linear(t) {
754 function d3_ease_poly(e) {
756 return Math.pow(t, e);
760 function d3_ease_sin(t) {
761 return 1 - Math.cos(t * Math.PI / 2);
764 function d3_ease_exp(t) {
765 return Math.pow(2, 10 * (t - 1));
768 function d3_ease_circle(t) {
769 return 1 - Math.sqrt(1 - t * t);
772 function d3_ease_elastic(a, p) {
774 if (arguments.length < 2) p = 0.45;
775 if (arguments.length < 1) { a = 1; s = p / 4; }
776 else s = p / (2 * Math.PI) * Math.asin(1 / a);
778 return 1 + a * Math.pow(2, 10 * -t) * Math.sin((t - s) * 2 * Math.PI / p);
782 function d3_ease_back(s) {
785 return t * t * ((s + 1) * t - s);
789 function d3_ease_bounce(t) {
790 return t < 1 / 2.75 ? 7.5625 * t * t
791 : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75
792 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375
793 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375;
797 function d3_eventCancel() {
798 d3.event.stopPropagation();
799 d3.event.preventDefault();
801 d3.interpolate = function(a, b) {
802 var i = d3.interpolators.length, f;
803 while (--i >= 0 && !(f = d3.interpolators[i](a, b)));
807 d3.interpolateNumber = function(a, b) {
809 return function(t) { return a + b * t; };
812 d3.interpolateRound = function(a, b) {
814 return function(t) { return Math.round(a + b * t); };
817 d3.interpolateString = function(a, b) {
818 var m, // current match
820 j, // current index (for coallescing)
821 s0 = 0, // start index of current string prefix
822 s1 = 0, // end index of current string prefix
823 s = [], // string constants and placeholders
824 q = [], // number interpolators
828 // Reset our regular expression!
829 d3_interpolate_number.lastIndex = 0;
831 // Find all numbers in b.
832 for (i = 0; m = d3_interpolate_number.exec(b); ++i) {
833 if (m.index) s.push(b.substring(s0, s1 = m.index));
834 q.push({i: s.length, x: m[0]});
836 s0 = d3_interpolate_number.lastIndex;
838 if (s0 < b.length) s.push(b.substring(s0));
840 // Find all numbers in a.
841 for (i = 0, n = q.length; (m = d3_interpolate_number.exec(a)) && i < n; ++i) {
843 if (o.x == m[0]) { // The numbers match, so coallesce.
845 if (s[o.i + 1] == null) { // This match is followed by another number.
848 for (j = i + 1; j < n; ++j) q[j].i--;
849 } else { // This match is followed by a string, so coallesce twice.
850 s[o.i - 1] += o.x + s[o.i + 1];
852 for (j = i + 1; j < n; ++j) q[j].i -= 2;
855 if (s[o.i + 1] == null) { // This match is followed by another number.
857 } else { // This match is followed by a string, so coallesce twice.
858 s[o.i] = o.x + s[o.i + 1];
859 s.splice(o.i + 1, 1);
860 for (j = i + 1; j < n; ++j) q[j].i--;
867 o.x = d3.interpolateNumber(parseFloat(m[0]), parseFloat(o.x));
871 // Remove any numbers in b not found in a.
874 if (s[o.i + 1] == null) { // This match is followed by another number.
876 } else { // This match is followed by a string, so coallesce twice.
877 s[o.i] = o.x + s[o.i + 1];
878 s.splice(o.i + 1, 1);
883 // Special optimization for only a single match.
884 if (s.length === 1) {
885 return s[0] == null ? q[0].x : function() { return b; };
888 // Otherwise, interpolate each of the numbers and rejoin the string.
890 for (i = 0; i < n; ++i) s[(o = q[i]).i] = o.x(t);
895 d3.interpolateTransform = function(a, b) {
896 return d3.interpolateString(d3.transform(a) + "", d3.transform(b) + "");
899 d3.interpolateRgb = function(a, b) {
910 + d3_rgb_hex(Math.round(ar + br * t))
911 + d3_rgb_hex(Math.round(ag + bg * t))
912 + d3_rgb_hex(Math.round(ab + bb * t));
916 // interpolates HSL space, but outputs RGB string (for compatibility)
917 d3.interpolateHsl = function(a, b) {
927 return d3_hsl_rgb(h0 + h1 * t, s0 + s1 * t, l0 + l1 * t).toString();
931 d3.interpolateArray = function(a, b) {
936 n0 = Math.min(a.length, b.length),
938 for (i = 0; i < n0; ++i) x.push(d3.interpolate(a[i], b[i]));
939 for (; i < na; ++i) c[i] = a[i];
940 for (; i < nb; ++i) c[i] = b[i];
942 for (i = 0; i < n0; ++i) c[i] = x[i](t);
947 d3.interpolateObject = function(a, b) {
953 i[k] = d3_interpolateByName(k)(a[k], b[k]);
964 for (k in i) c[k] = i[k](t);
969 var d3_interpolate_number = /[-+]?(?:\d*\.?\d+)(?:[eE][-+]?\d+)?/g;
971 function d3_interpolateByName(n) {
972 return n == "transform"
973 ? d3.interpolateTransform
978 d3.interpolateObject,
979 function(a, b) { return (b instanceof Array) && d3.interpolateArray(a, b); },
980 function(a, b) { return (typeof b === "string") && d3.interpolateString(a + "", b); },
981 function(a, b) { return (typeof b === "string" ? b in d3_rgb_names || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a + "", b); },
982 function(a, b) { return (typeof b === "number") && d3.interpolateNumber(+a, b); }
984 function d3_uninterpolateNumber(a, b) {
985 b = b - (a = +a) ? 1 / (b - a) : 0;
986 return function(x) { return (x - a) * b; };
989 function d3_uninterpolateClamp(a, b) {
990 b = b - (a = +a) ? 1 / (b - a) : 0;
991 return function(x) { return Math.max(0, Math.min(1, (x - a) * b)); };
993 d3.rgb = function(r, g, b) {
994 return arguments.length === 1
995 ? (r instanceof d3_Rgb ? d3_rgb(r.r, r.g, r.b)
996 : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb))
997 : d3_rgb(~~r, ~~g, ~~b);
1000 function d3_rgb(r, g, b) {
1001 return new d3_Rgb(r, g, b);
1004 function d3_Rgb(r, g, b) {
1010 d3_Rgb.prototype.brighter = function(k) {
1011 k = Math.pow(0.7, arguments.length ? k : 1);
1016 if (!r && !g && !b) return d3_rgb(i, i, i);
1017 if (r && r < i) r = i;
1018 if (g && g < i) g = i;
1019 if (b && b < i) b = i;
1021 Math.min(255, Math.floor(r / k)),
1022 Math.min(255, Math.floor(g / k)),
1023 Math.min(255, Math.floor(b / k)));
1026 d3_Rgb.prototype.darker = function(k) {
1027 k = Math.pow(0.7, arguments.length ? k : 1);
1029 Math.floor(k * this.r),
1030 Math.floor(k * this.g),
1031 Math.floor(k * this.b));
1034 d3_Rgb.prototype.hsl = function() {
1035 return d3_rgb_hsl(this.r, this.g, this.b);
1038 d3_Rgb.prototype.toString = function() {
1039 return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b);
1042 function d3_rgb_hex(v) {
1044 ? "0" + Math.max(0, v).toString(16)
1045 : Math.min(255, v).toString(16);
1048 function d3_rgb_parse(format, rgb, hsl) {
1049 var r = 0, // red channel; int in [0, 255]
1050 g = 0, // green channel; int in [0, 255]
1051 b = 0, // blue channel; int in [0, 255]
1052 m1, // CSS color specification match
1053 m2, // CSS color specification type (e.g., rgb)
1056 /* Handle hsl, rgb. */
1057 m1 = /([a-z]+)\((.*)\)/i.exec(format);
1059 m2 = m1[2].split(",");
1063 parseFloat(m2[0]), // degrees
1064 parseFloat(m2[1]) / 100, // percentage
1065 parseFloat(m2[2]) / 100 // percentage
1070 d3_rgb_parseNumber(m2[0]),
1071 d3_rgb_parseNumber(m2[1]),
1072 d3_rgb_parseNumber(m2[2])
1079 if (name = d3_rgb_names[format]) return rgb(name.r, name.g, name.b);
1081 /* Hexadecimal colors: #rgb and #rrggbb. */
1082 if (format != null && format.charAt(0) === "#") {
1083 if (format.length === 4) {
1084 r = format.charAt(1); r += r;
1085 g = format.charAt(2); g += g;
1086 b = format.charAt(3); b += b;
1087 } else if (format.length === 7) {
1088 r = format.substring(1, 3);
1089 g = format.substring(3, 5);
1090 b = format.substring(5, 7);
1092 r = parseInt(r, 16);
1093 g = parseInt(g, 16);
1094 b = parseInt(b, 16);
1097 return rgb(r, g, b);
1100 function d3_rgb_hsl(r, g, b) {
1101 var min = Math.min(r /= 255, g /= 255, b /= 255),
1102 max = Math.max(r, g, b),
1106 l = (max + min) / 2;
1108 s = l < .5 ? d / (max + min) : d / (2 - max - min);
1109 if (r == max) h = (g - b) / d + (g < b ? 6 : 0);
1110 else if (g == max) h = (b - r) / d + 2;
1111 else h = (r - g) / d + 4;
1116 return d3_hsl(h, s, l);
1119 function d3_rgb_parseNumber(c) { // either integer or percentage
1120 var f = parseFloat(c);
1121 return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f;
1124 var d3_rgb_names = {
1125 aliceblue: "#f0f8ff",
1126 antiquewhite: "#faebd7",
1128 aquamarine: "#7fffd4",
1133 blanchedalmond: "#ffebcd",
1135 blueviolet: "#8a2be2",
1137 burlywood: "#deb887",
1138 cadetblue: "#5f9ea0",
1139 chartreuse: "#7fff00",
1140 chocolate: "#d2691e",
1142 cornflowerblue: "#6495ed",
1143 cornsilk: "#fff8dc",
1146 darkblue: "#00008b",
1147 darkcyan: "#008b8b",
1148 darkgoldenrod: "#b8860b",
1149 darkgray: "#a9a9a9",
1150 darkgreen: "#006400",
1151 darkgrey: "#a9a9a9",
1152 darkkhaki: "#bdb76b",
1153 darkmagenta: "#8b008b",
1154 darkolivegreen: "#556b2f",
1155 darkorange: "#ff8c00",
1156 darkorchid: "#9932cc",
1158 darksalmon: "#e9967a",
1159 darkseagreen: "#8fbc8f",
1160 darkslateblue: "#483d8b",
1161 darkslategray: "#2f4f4f",
1162 darkslategrey: "#2f4f4f",
1163 darkturquoise: "#00ced1",
1164 darkviolet: "#9400d3",
1165 deeppink: "#ff1493",
1166 deepskyblue: "#00bfff",
1169 dodgerblue: "#1e90ff",
1170 firebrick: "#b22222",
1171 floralwhite: "#fffaf0",
1172 forestgreen: "#228b22",
1174 gainsboro: "#dcdcdc",
1175 ghostwhite: "#f8f8ff",
1177 goldenrod: "#daa520",
1180 greenyellow: "#adff2f",
1182 honeydew: "#f0fff0",
1184 indianred: "#cd5c5c",
1188 lavender: "#e6e6fa",
1189 lavenderblush: "#fff0f5",
1190 lawngreen: "#7cfc00",
1191 lemonchiffon: "#fffacd",
1192 lightblue: "#add8e6",
1193 lightcoral: "#f08080",
1194 lightcyan: "#e0ffff",
1195 lightgoldenrodyellow: "#fafad2",
1196 lightgray: "#d3d3d3",
1197 lightgreen: "#90ee90",
1198 lightgrey: "#d3d3d3",
1199 lightpink: "#ffb6c1",
1200 lightsalmon: "#ffa07a",
1201 lightseagreen: "#20b2aa",
1202 lightskyblue: "#87cefa",
1203 lightslategray: "#778899",
1204 lightslategrey: "#778899",
1205 lightsteelblue: "#b0c4de",
1206 lightyellow: "#ffffe0",
1208 limegreen: "#32cd32",
1212 mediumaquamarine: "#66cdaa",
1213 mediumblue: "#0000cd",
1214 mediumorchid: "#ba55d3",
1215 mediumpurple: "#9370db",
1216 mediumseagreen: "#3cb371",
1217 mediumslateblue: "#7b68ee",
1218 mediumspringgreen: "#00fa9a",
1219 mediumturquoise: "#48d1cc",
1220 mediumvioletred: "#c71585",
1221 midnightblue: "#191970",
1222 mintcream: "#f5fffa",
1223 mistyrose: "#ffe4e1",
1224 moccasin: "#ffe4b5",
1225 navajowhite: "#ffdead",
1229 olivedrab: "#6b8e23",
1231 orangered: "#ff4500",
1233 palegoldenrod: "#eee8aa",
1234 palegreen: "#98fb98",
1235 paleturquoise: "#afeeee",
1236 palevioletred: "#db7093",
1237 papayawhip: "#ffefd5",
1238 peachpuff: "#ffdab9",
1242 powderblue: "#b0e0e6",
1245 rosybrown: "#bc8f8f",
1246 royalblue: "#4169e1",
1247 saddlebrown: "#8b4513",
1249 sandybrown: "#f4a460",
1250 seagreen: "#2e8b57",
1251 seashell: "#fff5ee",
1255 slateblue: "#6a5acd",
1256 slategray: "#708090",
1257 slategrey: "#708090",
1259 springgreen: "#00ff7f",
1260 steelblue: "#4682b4",
1265 turquoise: "#40e0d0",
1269 whitesmoke: "#f5f5f5",
1271 yellowgreen: "#9acd32"
1274 for (var d3_rgb_name in d3_rgb_names) {
1275 d3_rgb_names[d3_rgb_name] = d3_rgb_parse(
1276 d3_rgb_names[d3_rgb_name],
1280 d3.hsl = function(h, s, l) {
1281 return arguments.length === 1
1282 ? (h instanceof d3_Hsl ? d3_hsl(h.h, h.s, h.l)
1283 : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl))
1284 : d3_hsl(+h, +s, +l);
1287 function d3_hsl(h, s, l) {
1288 return new d3_Hsl(h, s, l);
1291 function d3_Hsl(h, s, l) {
1297 d3_Hsl.prototype.brighter = function(k) {
1298 k = Math.pow(0.7, arguments.length ? k : 1);
1299 return d3_hsl(this.h, this.s, this.l / k);
1302 d3_Hsl.prototype.darker = function(k) {
1303 k = Math.pow(0.7, arguments.length ? k : 1);
1304 return d3_hsl(this.h, this.s, k * this.l);
1307 d3_Hsl.prototype.rgb = function() {
1308 return d3_hsl_rgb(this.h, this.s, this.l);
1311 d3_Hsl.prototype.toString = function() {
1312 return this.rgb().toString();
1315 function d3_hsl_rgb(h, s, l) {
1319 /* Some simple corrections for h, s and l. */
1320 h = h % 360; if (h < 0) h += 360;
1321 s = s < 0 ? 0 : s > 1 ? 1 : s;
1322 l = l < 0 ? 0 : l > 1 ? 1 : l;
1324 /* From FvD 13.37, CSS Color Module Level 3 */
1325 m2 = l <= .5 ? l * (1 + s) : l + s - l * s;
1329 if (h > 360) h -= 360;
1330 else if (h < 0) h += 360;
1331 if (h < 60) return m1 + (m2 - m1) * h / 60;
1332 if (h < 180) return m2;
1333 if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60;
1338 return Math.round(v(h) * 255);
1341 return d3_rgb(vv(h + 120), vv(h), vv(h - 120));
1343 function d3_selection(groups) {
1344 d3_arraySubclass(groups, d3_selectionPrototype);
1348 var d3_select = function(s, n) { return n.querySelector(s); },
1349 d3_selectAll = function(s, n) { return n.querySelectorAll(s); };
1351 // Prefer Sizzle, if available.
1352 if (typeof Sizzle === "function") {
1353 d3_select = function(s, n) { return Sizzle(s, n)[0]; };
1354 d3_selectAll = function(s, n) { return Sizzle.uniqueSort(Sizzle(s, n)); };
1357 var d3_selectionPrototype = [];
1359 d3.selection = function() {
1360 return d3_selectionRoot;
1363 d3.selection.prototype = d3_selectionPrototype;
1364 d3_selectionPrototype.select = function(selector) {
1371 if (typeof selector !== "function") selector = d3_selection_selector(selector);
1373 for (var j = -1, m = this.length; ++j < m;) {
1374 subgroups.push(subgroup = []);
1375 subgroup.parentNode = (group = this[j]).parentNode;
1376 for (var i = -1, n = group.length; ++i < n;) {
1377 if (node = group[i]) {
1378 subgroup.push(subnode = selector.call(node, node.__data__, i));
1379 if (subnode && "__data__" in node) subnode.__data__ = node.__data__;
1381 subgroup.push(null);
1386 return d3_selection(subgroups);
1389 function d3_selection_selector(selector) {
1391 return d3_select(selector, this);
1394 d3_selectionPrototype.selectAll = function(selector) {
1399 if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
1401 for (var j = -1, m = this.length; ++j < m;) {
1402 for (var group = this[j], i = -1, n = group.length; ++i < n;) {
1403 if (node = group[i]) {
1404 subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i)));
1405 subgroup.parentNode = node;
1410 return d3_selection(subgroups);
1413 function d3_selection_selectorAll(selector) {
1415 return d3_selectAll(selector, this);
1418 d3_selectionPrototype.attr = function(name, value) {
1419 name = d3.ns.qualify(name);
1421 // If no value is specified, return the first value.
1422 if (arguments.length < 2) {
1423 var node = this.node();
1425 ? node.getAttributeNS(name.space, name.local)
1426 : node.getAttribute(name);
1429 function attrNull() {
1430 this.removeAttribute(name);
1433 function attrNullNS() {
1434 this.removeAttributeNS(name.space, name.local);
1437 function attrConstant() {
1438 this.setAttribute(name, value);
1441 function attrConstantNS() {
1442 this.setAttributeNS(name.space, name.local, value);
1445 function attrFunction() {
1446 var x = value.apply(this, arguments);
1447 if (x == null) this.removeAttribute(name);
1448 else this.setAttribute(name, x);
1451 function attrFunctionNS() {
1452 var x = value.apply(this, arguments);
1453 if (x == null) this.removeAttributeNS(name.space, name.local);
1454 else this.setAttributeNS(name.space, name.local, x);
1457 return this.each(value == null
1458 ? (name.local ? attrNullNS : attrNull) : (typeof value === "function"
1459 ? (name.local ? attrFunctionNS : attrFunction)
1460 : (name.local ? attrConstantNS : attrConstant)));
1462 d3_selectionPrototype.classed = function(name, value) {
1463 var names = name.split(d3_selection_classedWhitespace),
1466 if (arguments.length > 1) {
1467 while (++i < n) d3_selection_classed.call(this, names[i], value);
1470 while (++i < n) if (!d3_selection_classed.call(this, names[i])) return false;
1475 var d3_selection_classedWhitespace = /\s+/g;
1477 function d3_selection_classed(name, value) {
1478 var re = new RegExp("(^|\\s+)" + d3.requote(name) + "(\\s+|$)", "g");
1480 // If no value is specified, return the first value.
1481 if (arguments.length < 2) {
1482 var node = this.node();
1483 if (c = node.classList) return c.contains(name);
1484 var c = node.className;
1486 return re.test(c.baseVal != null ? c.baseVal : c);
1489 function classedAdd() {
1490 if (c = this.classList) return c.add(name);
1491 var c = this.className,
1492 cb = c.baseVal != null,
1493 cv = cb ? c.baseVal : c;
1496 cv = d3_collapse(cv + " " + name);
1497 if (cb) c.baseVal = cv;
1498 else this.className = cv;
1502 function classedRemove() {
1503 if (c = this.classList) return c.remove(name);
1504 var c = this.className,
1505 cb = c.baseVal != null,
1506 cv = cb ? c.baseVal : c;
1507 cv = d3_collapse(cv.replace(re, " "));
1508 if (cb) c.baseVal = cv;
1509 else this.className = cv;
1512 function classedFunction() {
1513 (value.apply(this, arguments)
1515 : classedRemove).call(this);
1518 return this.each(typeof value === "function"
1519 ? classedFunction : value
1523 d3_selectionPrototype.style = function(name, value, priority) {
1524 if (arguments.length < 3) priority = "";
1526 // If no value is specified, return the first value.
1527 if (arguments.length < 2) return window
1528 .getComputedStyle(this.node(), null)
1529 .getPropertyValue(name);
1531 function styleNull() {
1532 this.style.removeProperty(name);
1535 function styleConstant() {
1536 this.style.setProperty(name, value, priority);
1539 function styleFunction() {
1540 var x = value.apply(this, arguments);
1541 if (x == null) this.style.removeProperty(name);
1542 else this.style.setProperty(name, x, priority);
1545 return this.each(value == null
1546 ? styleNull : (typeof value === "function"
1547 ? styleFunction : styleConstant));
1549 d3_selectionPrototype.property = function(name, value) {
1551 // If no value is specified, return the first value.
1552 if (arguments.length < 2) return this.node()[name];
1554 function propertyNull() {
1558 function propertyConstant() {
1562 function propertyFunction() {
1563 var x = value.apply(this, arguments);
1564 if (x == null) delete this[name];
1565 else this[name] = x;
1568 return this.each(value == null
1569 ? propertyNull : (typeof value === "function"
1570 ? propertyFunction : propertyConstant));
1572 d3_selectionPrototype.text = function(value) {
1573 return arguments.length < 1 ? this.node().textContent
1574 : (this.each(typeof value === "function"
1575 ? function() { this.textContent = value.apply(this, arguments); }
1576 : function() { this.textContent = value; }));
1578 d3_selectionPrototype.html = function(value) {
1579 return arguments.length < 1 ? this.node().innerHTML
1580 : (this.each(typeof value === "function"
1581 ? function() { this.innerHTML = value.apply(this, arguments); }
1582 : function() { this.innerHTML = value; }));
1584 // TODO append(node)?
1585 // TODO append(function)?
1586 d3_selectionPrototype.append = function(name) {
1587 name = d3.ns.qualify(name);
1590 return this.appendChild(document.createElementNS(this.namespaceURI, name));
1593 function appendNS() {
1594 return this.appendChild(document.createElementNS(name.space, name.local));
1597 return this.select(name.local ? appendNS : append);
1599 // TODO insert(node, function)?
1600 // TODO insert(function, string)?
1601 // TODO insert(function, function)?
1602 d3_selectionPrototype.insert = function(name, before) {
1603 name = d3.ns.qualify(name);
1606 return this.insertBefore(
1607 document.createElementNS(this.namespaceURI, name),
1608 d3_select(before, this));
1611 function insertNS() {
1612 return this.insertBefore(
1613 document.createElementNS(name.space, name.local),
1614 d3_select(before, this));
1617 return this.select(name.local ? insertNS : insert);
1619 // TODO remove(selector)?
1620 // TODO remove(node)?
1621 // TODO remove(function)?
1622 d3_selectionPrototype.remove = function() {
1623 return this.each(function() {
1624 var parent = this.parentNode;
1625 if (parent) parent.removeChild(this);
1628 // TODO data(null) for clearing data?
1629 d3_selectionPrototype.data = function(data, join) {
1634 function bind(group, groupData) {
1637 m = groupData.length,
1638 n0 = Math.min(n, m),
1639 n1 = Math.max(n, m),
1650 j = groupData.length;
1652 for (i = -1; ++i < n;) {
1653 key = join.call(node = group[i], node.__data__, i);
1654 if (key in nodeByKey) {
1655 exitNodes[j++] = node; // duplicate key
1657 nodeByKey[key] = node;
1662 for (i = -1; ++i < m;) {
1663 node = nodeByKey[key = join.call(groupData, nodeData = groupData[i], i)];
1665 node.__data__ = nodeData;
1666 updateNodes[i] = node;
1667 enterNodes[i] = exitNodes[i] = null;
1669 enterNodes[i] = d3_selection_dataNode(nodeData);
1670 updateNodes[i] = exitNodes[i] = null;
1672 delete nodeByKey[key];
1675 for (i = -1; ++i < n;) {
1676 if (keys[i] in nodeByKey) {
1677 exitNodes[i] = group[i];
1681 for (i = -1; ++i < n0;) {
1683 nodeData = groupData[i];
1685 node.__data__ = nodeData;
1686 updateNodes[i] = node;
1687 enterNodes[i] = exitNodes[i] = null;
1689 enterNodes[i] = d3_selection_dataNode(nodeData);
1690 updateNodes[i] = exitNodes[i] = null;
1693 for (; i < m; ++i) {
1694 enterNodes[i] = d3_selection_dataNode(groupData[i]);
1695 updateNodes[i] = exitNodes[i] = null;
1697 for (; i < n1; ++i) {
1698 exitNodes[i] = group[i];
1699 enterNodes[i] = updateNodes[i] = null;
1706 enterNodes.parentNode
1707 = updateNodes.parentNode
1708 = exitNodes.parentNode
1711 enter.push(enterNodes);
1712 update.push(updateNodes);
1713 exit.push(exitNodes);
1719 if (typeof data === "function") {
1721 bind(group = this[i], data.call(group, group.parentNode.__data__, i));
1725 bind(group = this[i], data);
1729 var selection = d3_selection(update);
1730 selection.enter = function() { return d3_selection_enter(enter); };
1731 selection.exit = function() { return d3_selection(exit); };
1735 function d3_selection_dataNode(data) {
1736 return {__data__: data};
1738 // TODO preserve null elements to maintain index?
1739 d3_selectionPrototype.filter = function(filter) {
1745 for (var j = 0, m = this.length; j < m; j++) {
1746 subgroups.push(subgroup = []);
1747 subgroup.parentNode = (group = this[j]).parentNode;
1748 for (var i = 0, n = group.length; i < n; i++) {
1749 if ((node = group[i]) && filter.call(node, node.__data__, i)) {
1750 subgroup.push(node);
1755 return d3_selection(subgroups);
1757 d3_selectionPrototype.map = function(map) {
1758 return this.each(function() {
1759 this.__data__ = map.apply(this, arguments);
1762 d3_selectionPrototype.sort = function(comparator) {
1763 comparator = d3_selection_sortComparator.apply(this, arguments);
1764 for (var j = 0, m = this.length; j < m; j++) {
1765 for (var group = this[j].sort(comparator), i = 1, n = group.length, prev = group[0]; i < n; i++) {
1766 var node = group[i];
1768 if (prev) prev.parentNode.insertBefore(node, prev.nextSibling);
1776 function d3_selection_sortComparator(comparator) {
1777 if (!arguments.length) comparator = d3.ascending;
1778 return function(a, b) {
1779 return comparator(a && a.__data__, b && b.__data__);
1782 // type can be namespaced, e.g., "click.foo"
1783 // listener can be null for removal
1784 d3_selectionPrototype.on = function(type, listener, capture) {
1785 if (arguments.length < 3) capture = false;
1787 // parse the type specifier
1788 var name = "__on" + type, i = type.indexOf(".");
1789 if (i > 0) type = type.substring(0, i);
1791 // if called with only one argument, return the current listener
1792 if (arguments.length < 2) return (i = this.node()[name]) && i._;
1794 // remove the old event listener, and add the new event listener
1795 return this.each(function(d, i) {
1798 if (node[name]) node.removeEventListener(type, node[name], capture);
1799 if (listener) node.addEventListener(type, node[name] = l, capture);
1801 // wrapped event listener that preserves i
1803 var o = d3.event; // Events can be reentrant (e.g., focus).
1806 listener.call(node, node.__data__, i);
1812 // stash the unwrapped listener for retrieval
1816 d3_selectionPrototype.each = function(callback) {
1817 for (var j = -1, m = this.length; ++j < m;) {
1818 for (var group = this[j], i = -1, n = group.length; ++i < n;) {
1819 var node = group[i];
1820 if (node) callback.call(node, node.__data__, i, j);
1826 // Note: assigning to the arguments array simultaneously changes the value of
1827 // the corresponding argument!
1829 // TODO The `this` argument probably shouldn't be the first argument to the
1830 // callback, anyway, since it's redundant. However, that will require a major
1831 // version bump due to backwards compatibility, so I'm not changing it right
1834 d3_selectionPrototype.call = function(callback) {
1835 callback.apply(this, (arguments[0] = this, arguments));
1838 d3_selectionPrototype.empty = function() {
1839 return !this.node();
1841 d3_selectionPrototype.node = function(callback) {
1842 for (var j = 0, m = this.length; j < m; j++) {
1843 for (var group = this[j], i = 0, n = group.length; i < n; i++) {
1844 var node = group[i];
1845 if (node) return node;
1850 d3_selectionPrototype.transition = function() {
1855 for (var j = -1, m = this.length; ++j < m;) {
1856 subgroups.push(subgroup = []);
1857 for (var group = this[j], i = -1, n = group.length; ++i < n;) {
1858 subgroup.push((node = group[i]) ? {node: node, delay: 0, duration: 250} : null);
1862 return d3_transition(subgroups, d3_transitionInheritId || ++d3_transitionId, Date.now());
1864 var d3_selectionRoot = d3_selection([[document]]);
1866 d3_selectionRoot[0].parentNode = document.documentElement;
1868 // TODO fast singleton implementation!
1869 // TODO select(function)
1870 d3.select = function(selector) {
1871 return typeof selector === "string"
1872 ? d3_selectionRoot.select(selector)
1873 : d3_selection([[selector]]); // assume node
1876 // TODO selectAll(function)
1877 d3.selectAll = function(selector) {
1878 return typeof selector === "string"
1879 ? d3_selectionRoot.selectAll(selector)
1880 : d3_selection([d3_array(selector)]); // assume node[]
1882 function d3_selection_enter(selection) {
1883 d3_arraySubclass(selection, d3_selection_enterPrototype);
1887 var d3_selection_enterPrototype = [];
1889 d3_selection_enterPrototype.append = d3_selectionPrototype.append;
1890 d3_selection_enterPrototype.insert = d3_selectionPrototype.insert;
1891 d3_selection_enterPrototype.empty = d3_selectionPrototype.empty;
1892 d3_selection_enterPrototype.node = d3_selectionPrototype.node;
1893 d3_selection_enterPrototype.select = function(selector) {
1901 for (var j = -1, m = this.length; ++j < m;) {
1902 upgroup = (group = this[j]).update;
1903 subgroups.push(subgroup = []);
1904 subgroup.parentNode = group.parentNode;
1905 for (var i = -1, n = group.length; ++i < n;) {
1906 if (node = group[i]) {
1907 subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i));
1908 subnode.__data__ = node.__data__;
1910 subgroup.push(null);
1915 return d3_selection(subgroups);
1917 function d3_transition(groups, id, time) {
1918 d3_arraySubclass(groups, d3_transitionPrototype);
1921 event = d3.dispatch("start", "end"),
1922 ease = d3_transitionEase;
1928 groups.tween = function(name, tween) {
1929 if (arguments.length < 2) return tweens[name];
1930 if (tween == null) delete tweens[name];
1931 else tweens[name] = tween;
1935 groups.ease = function(value) {
1936 if (!arguments.length) return ease;
1937 ease = typeof value === "function" ? value : d3.ease.apply(d3, arguments);
1941 groups.each = function(type, listener) {
1942 if (arguments.length < 2) return d3_transition_each.call(groups, type);
1943 event.on(type, listener);
1947 d3.timer(function(elapsed) {
1948 groups.each(function(d, i, j) {
1951 delay = groups[j][i].delay,
1952 duration = groups[j][i].duration,
1953 lock = node.__transition__ || (node.__transition__ = {active: 0, count: 0});
1957 delay <= elapsed ? start(elapsed) : d3.timer(start, delay, time);
1959 function start(elapsed) {
1960 if (lock.active > id) return stop();
1963 for (var tween in tweens) {
1964 if (tween = tweens[tween].call(node, d, i)) {
1965 tweened.push(tween);
1969 event.start.call(node, d, i);
1970 if (!tick(elapsed)) d3.timer(tick, 0, time);
1974 function tick(elapsed) {
1975 if (lock.active !== id) return stop();
1977 var t = (elapsed - delay) / duration,
1982 tweened[--n].call(node, e);
1987 d3_transitionInheritId = id;
1988 event.end.call(node, d, i);
1989 d3_transitionInheritId = 0;
1995 if (!--lock.count) delete node.__transition__;
2005 var d3_transitionRemove = {};
2007 function d3_transitionNull(d, i, a) {
2008 return a != "" && d3_transitionRemove;
2011 function d3_transitionTween(name, b) {
2012 var interpolate = d3_interpolateByName(name);
2014 function transitionFunction(d, i, a) {
2015 var v = b.call(this, d, i);
2017 ? a != "" && d3_transitionRemove
2018 : a != v && interpolate(a, v);
2021 function transitionString(d, i, a) {
2022 return a != b && interpolate(a, b);
2025 return typeof b === "function" ? transitionFunction
2026 : b == null ? d3_transitionNull
2027 : (b += "", transitionString);
2030 var d3_transitionPrototype = [],
2031 d3_transitionId = 0,
2032 d3_transitionInheritId = 0,
2033 d3_transitionEase = d3.ease("cubic-in-out");
2035 d3_transitionPrototype.call = d3_selectionPrototype.call;
2037 d3.transition = function() {
2038 return d3_selectionRoot.transition();
2041 d3.transition.prototype = d3_transitionPrototype;
2042 d3_transitionPrototype.select = function(selector) {
2048 if (typeof selector !== "function") selector = d3_selection_selector(selector);
2050 for (var j = -1, m = this.length; ++j < m;) {
2051 subgroups.push(subgroup = []);
2052 for (var group = this[j], i = -1, n = group.length; ++i < n;) {
2053 if ((node = group[i]) && (subnode = selector.call(node.node, node.node.__data__, i))) {
2054 if ("__data__" in node.node) subnode.__data__ = node.node.__data__;
2055 subgroup.push({node: subnode, delay: node.delay, duration: node.duration});
2057 subgroup.push(null);
2062 return d3_transition(subgroups, this.id, this.time).ease(this.ease());
2064 d3_transitionPrototype.selectAll = function(selector) {
2070 if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
2072 for (var j = -1, m = this.length; ++j < m;) {
2073 for (var group = this[j], i = -1, n = group.length; ++i < n;) {
2074 if (node = group[i]) {
2075 subnodes = selector.call(node.node, node.node.__data__, i);
2076 subgroups.push(subgroup = []);
2077 for (var k = -1, o = subnodes.length; ++k < o;) {
2078 subgroup.push({node: subnodes[k], delay: node.delay, duration: node.duration});
2084 return d3_transition(subgroups, this.id, this.time).ease(this.ease());
2086 d3_transitionPrototype.attr = function(name, value) {
2087 return this.attrTween(name, d3_transitionTween(name, value));
2090 d3_transitionPrototype.attrTween = function(nameNS, tween) {
2091 var name = d3.ns.qualify(nameNS);
2093 function attrTween(d, i) {
2094 var f = tween.call(this, d, i, this.getAttribute(name));
2095 return f === d3_transitionRemove
2096 ? (this.removeAttribute(name), null)
2097 : f && function(t) { this.setAttribute(name, f(t)); };
2100 function attrTweenNS(d, i) {
2101 var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local));
2102 return f === d3_transitionRemove
2103 ? (this.removeAttributeNS(name.space, name.local), null)
2104 : f && function(t) { this.setAttributeNS(name.space, name.local, f(t)); };
2107 return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween);
2109 d3_transitionPrototype.style = function(name, value, priority) {
2110 if (arguments.length < 3) priority = "";
2111 return this.styleTween(name, d3_transitionTween(name, value), priority);
2114 d3_transitionPrototype.styleTween = function(name, tween, priority) {
2115 if (arguments.length < 3) priority = "";
2116 return this.tween("style." + name, function(d, i) {
2117 var f = tween.call(this, d, i, window.getComputedStyle(this, null).getPropertyValue(name));
2118 return f === d3_transitionRemove
2119 ? (this.style.removeProperty(name), null)
2120 : f && function(t) { this.style.setProperty(name, f(t), priority); };
2123 d3_transitionPrototype.text = function(value) {
2124 return this.tween("text", function(d, i) {
2125 this.textContent = typeof value === "function"
2126 ? value.call(this, d, i)
2130 d3_transitionPrototype.remove = function() {
2131 return this.each("end", function() {
2133 if (!this.__transition__ && (p = this.parentNode)) p.removeChild(this);
2136 d3_transitionPrototype.delay = function(value) {
2138 return groups.each(typeof value === "function"
2139 ? function(d, i, j) { groups[j][i].delay = +value.apply(this, arguments); }
2140 : (value = +value, function(d, i, j) { groups[j][i].delay = value; }));
2142 d3_transitionPrototype.duration = function(value) {
2144 return groups.each(typeof value === "function"
2145 ? function(d, i, j) { groups[j][i].duration = +value.apply(this, arguments); }
2146 : (value = +value, function(d, i, j) { groups[j][i].duration = value; }));
2148 function d3_transition_each(callback) {
2149 for (var j = 0, m = this.length; j < m; j++) {
2150 for (var group = this[j], i = 0, n = group.length; i < n; i++) {
2151 var node = group[i];
2152 if (node) callback.call(node = node.node, node.__data__, i, j);
2157 d3_transitionPrototype.transition = function() {
2158 return this.select(d3_this);
2160 var d3_timer_queue = null,
2161 d3_timer_interval, // is an interval (or frame) active?
2162 d3_timer_timeout; // is a timeout active?
2164 // The timer will continue to fire until callback returns true.
2165 d3.timer = function(callback, delay, then) {
2168 t1 = d3_timer_queue;
2170 if (arguments.length < 3) {
2171 if (arguments.length < 2) delay = 0;
2172 else if (!isFinite(delay)) return;
2176 // See if the callback's already in the queue.
2178 if (t1.callback === callback) {
2188 // Otherwise, add the callback to the queue.
2189 if (!found) d3_timer_queue = {
2193 next: d3_timer_queue
2197 if (!d3_timer_interval) {
2198 d3_timer_timeout = clearTimeout(d3_timer_timeout);
2199 d3_timer_interval = 1;
2200 d3_timer_frame(d3_timer_step);
2204 function d3_timer_step() {
2207 t1 = d3_timer_queue;
2210 elapsed = now - t1.then;
2211 if (elapsed >= t1.delay) t1.flush = t1.callback(elapsed);
2215 var delay = d3_timer_flush() - now;
2217 if (isFinite(delay)) {
2218 clearTimeout(d3_timer_timeout);
2219 d3_timer_timeout = setTimeout(d3_timer_step, delay);
2221 d3_timer_interval = 0;
2223 d3_timer_interval = 1;
2224 d3_timer_frame(d3_timer_step);
2228 d3.timer.flush = function() {
2231 t1 = d3_timer_queue;
2234 elapsed = now - t1.then;
2235 if (!t1.delay) t1.flush = t1.callback(elapsed);
2242 // Flush after callbacks, to avoid concurrent queue modification.
2243 function d3_timer_flush() {
2245 t1 = d3_timer_queue,
2249 t1 = t0 ? t0.next = t1.next : d3_timer_queue = t1.next;
2251 then = Math.min(then, t1.then + t1.delay);
2252 t1 = (t0 = t1).next;
2258 var d3_timer_frame = window.requestAnimationFrame
2259 || window.webkitRequestAnimationFrame
2260 || window.mozRequestAnimationFrame
2261 || window.oRequestAnimationFrame
2262 || window.msRequestAnimationFrame
2263 || function(callback) { setTimeout(callback, 17); };
2264 d3.transform = function(string) {
2265 d3_transformG.setAttribute("transform", string);
2266 var t = d3_transformG.transform.baseVal.consolidate();
2267 return new d3_transform(t ? t.matrix : d3_transformIdentity);
2270 // Compute x-scale and normalize the first row.
2271 // Compute shear and make second row orthogonal to first.
2272 // Compute y-scale and normalize the second row.
2273 // Finally, compute the rotation.
2274 function d3_transform(m) {
2275 var r0 = [m.a, m.b],
2277 kx = d3_transformNormalize(r0),
2278 kz = d3_transformDot(r0, r1),
2279 ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0;
2280 if (r0[0] * r1[1] < r1[0] * r0[1]) {
2286 this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_transformDegrees;
2287 this.translate = [m.e, m.f];
2288 this.scale = [kx, ky];
2289 this.skew = ky ? Math.atan2(kz, ky) * d3_transformDegrees : 0;
2292 d3_transform.prototype.toString = function() {
2293 return "translate(" + this.translate
2294 + ")rotate(" + this.rotate
2295 + ")skewX(" + this.skew
2296 + ")scale(" + this.scale
2300 function d3_transformDot(a, b) {
2301 return a[0] * b[0] + a[1] * b[1];
2304 function d3_transformNormalize(a) {
2305 var k = Math.sqrt(d3_transformDot(a, a));
2313 function d3_transformCombine(a, b, k) {
2319 var d3_transformG = document.createElementNS(d3.ns.prefix.svg, "g"),
2320 d3_transformIdentity = {a: 1, b: 0, c: 0, d: 1, e: 0, f: 0},
2321 d3_transformDegrees = 180 / Math.PI;
2322 function d3_noop() {}
2325 function d3_scaleExtent(domain) {
2326 var start = domain[0], stop = domain[domain.length - 1];
2327 return start < stop ? [start, stop] : [stop, start];
2330 function d3_scaleRange(scale) {
2331 return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range());
2333 function d3_scale_nice(domain, nice) {
2335 i1 = domain.length - 1,
2341 dx = i0; i0 = i1; i1 = dx;
2342 dx = x0; x0 = x1; x1 = dx;
2347 domain[i0] = nice.floor(x0);
2348 domain[i1] = nice.ceil(x1);
2354 function d3_scale_niceDefault() {
2357 d3.scale.linear = function() {
2358 return d3_scale_linear([0, 1], [0, 1], d3.interpolate, false);
2361 function d3_scale_linear(domain, range, interpolate, clamp) {
2365 function rescale() {
2366 var linear = domain.length == 2 ? d3_scale_bilinear : d3_scale_polylinear,
2367 uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber;
2368 output = linear(domain, range, uninterpolate, interpolate);
2369 input = linear(range, domain, uninterpolate, d3.interpolate);
2377 // Note: requires range is coercible to number!
2378 scale.invert = function(y) {
2382 scale.domain = function(x) {
2383 if (!arguments.length) return domain;
2384 domain = x.map(Number);
2388 scale.range = function(x) {
2389 if (!arguments.length) return range;
2394 scale.rangeRound = function(x) {
2395 return scale.range(x).interpolate(d3.interpolateRound);
2398 scale.clamp = function(x) {
2399 if (!arguments.length) return clamp;
2404 scale.interpolate = function(x) {
2405 if (!arguments.length) return interpolate;
2410 scale.ticks = function(m) {
2411 return d3_scale_linearTicks(domain, m);
2414 scale.tickFormat = function(m) {
2415 return d3_scale_linearTickFormat(domain, m);
2418 scale.nice = function() {
2419 d3_scale_nice(domain, d3_scale_linearNice);
2423 scale.copy = function() {
2424 return d3_scale_linear(domain, range, interpolate, clamp);
2430 function d3_scale_linearRebind(scale, linear) {
2431 return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
2434 function d3_scale_linearNice(dx) {
2435 dx = Math.pow(10, Math.round(Math.log(dx) / Math.LN10) - 1);
2437 floor: function(x) { return Math.floor(x / dx) * dx; },
2438 ceil: function(x) { return Math.ceil(x / dx) * dx; }
2443 function d3_scale_linearTickRange(domain, m) {
2444 var extent = d3_scaleExtent(domain),
2445 span = extent[1] - extent[0],
2446 step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)),
2447 err = m / span * step;
2449 // Filter ticks to get closer to the desired count.
2450 if (err <= .15) step *= 10;
2451 else if (err <= .35) step *= 5;
2452 else if (err <= .75) step *= 2;
2454 // Round start and stop values to step interval.
2455 extent[0] = Math.ceil(extent[0] / step) * step;
2456 extent[1] = Math.floor(extent[1] / step) * step + step * .5; // inclusive
2461 function d3_scale_linearTicks(domain, m) {
2462 return d3.range.apply(d3, d3_scale_linearTickRange(domain, m));
2465 function d3_scale_linearTickFormat(domain, m) {
2466 return d3.format(",." + Math.max(0, -Math.floor(Math.log(d3_scale_linearTickRange(domain, m)[2]) / Math.LN10 + .01)) + "f");
2468 function d3_scale_bilinear(domain, range, uninterpolate, interpolate) {
2469 var u = uninterpolate(domain[0], domain[1]),
2470 i = interpolate(range[0], range[1]);
2471 return function(x) {
2475 function d3_scale_polylinear(domain, range, uninterpolate, interpolate) {
2482 u.push(uninterpolate(domain[j - 1], domain[j]));
2483 i.push(interpolate(range[j - 1], range[j]));
2486 return function(x) {
2487 var j = d3.bisect(domain, x, 1, domain.length - 1) - 1;
2488 return i[j](u[j](x));
2491 d3.scale.log = function() {
2492 return d3_scale_log(d3.scale.linear(), d3_scale_logp);
2495 function d3_scale_log(linear, log) {
2499 return linear(log(x));
2502 scale.invert = function(x) {
2503 return pow(linear.invert(x));
2506 scale.domain = function(x) {
2507 if (!arguments.length) return linear.domain().map(pow);
2508 log = x[0] < 0 ? d3_scale_logn : d3_scale_logp;
2510 linear.domain(x.map(log));
2514 scale.nice = function() {
2515 linear.domain(d3_scale_nice(linear.domain(), d3_scale_niceDefault));
2519 scale.ticks = function() {
2520 var extent = d3_scaleExtent(linear.domain()),
2522 if (extent.every(isFinite)) {
2523 var i = Math.floor(extent[0]),
2524 j = Math.ceil(extent[1]),
2527 if (log === d3_scale_logn) {
2529 for (; i++ < j;) for (var k = 9; k > 0; k--) ticks.push(pow(i) * k);
2531 for (; i < j; i++) for (var k = 1; k < 10; k++) ticks.push(pow(i) * k);
2534 for (i = 0; ticks[i] < u; i++) {} // strip small values
2535 for (j = ticks.length; ticks[j - 1] > v; j--) {} // strip big values
2536 ticks = ticks.slice(i, j);
2541 scale.tickFormat = function(n, format) {
2542 if (arguments.length < 2) format = d3_scale_logFormat;
2543 if (arguments.length < 1) return format;
2544 var k = n / scale.ticks().length,
2545 f = log === d3_scale_logn ? (e = -1e-15, Math.floor) : (e = 1e-15, Math.ceil),
2547 return function(d) {
2548 return d / pow(f(log(d) + e)) < k ? format(d) : "";
2552 scale.copy = function() {
2553 return d3_scale_log(linear.copy(), log);
2556 return d3_scale_linearRebind(scale, linear);
2559 var d3_scale_logFormat = d3.format(".0e");
2561 function d3_scale_logp(x) {
2562 return Math.log(x) / Math.LN10;
2565 function d3_scale_logn(x) {
2566 return -Math.log(-x) / Math.LN10;
2569 d3_scale_logp.pow = function(x) {
2570 return Math.pow(10, x);
2573 d3_scale_logn.pow = function(x) {
2574 return -Math.pow(10, -x);
2576 d3.scale.pow = function() {
2577 return d3_scale_pow(d3.scale.linear(), 1);
2580 function d3_scale_pow(linear, exponent) {
2581 var powp = d3_scale_powPow(exponent),
2582 powb = d3_scale_powPow(1 / exponent);
2585 return linear(powp(x));
2588 scale.invert = function(x) {
2589 return powb(linear.invert(x));
2592 scale.domain = function(x) {
2593 if (!arguments.length) return linear.domain().map(powb);
2594 linear.domain(x.map(powp));
2598 scale.ticks = function(m) {
2599 return d3_scale_linearTicks(scale.domain(), m);
2602 scale.tickFormat = function(m) {
2603 return d3_scale_linearTickFormat(scale.domain(), m);
2606 scale.nice = function() {
2607 return scale.domain(d3_scale_nice(scale.domain(), d3_scale_linearNice));
2610 scale.exponent = function(x) {
2611 if (!arguments.length) return exponent;
2612 var domain = scale.domain();
2613 powp = d3_scale_powPow(exponent = x);
2614 powb = d3_scale_powPow(1 / exponent);
2615 return scale.domain(domain);
2618 scale.copy = function() {
2619 return d3_scale_pow(linear.copy(), exponent);
2622 return d3_scale_linearRebind(scale, linear);
2625 function d3_scale_powPow(e) {
2626 return function(x) {
2627 return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e);
2630 d3.scale.sqrt = function() {
2631 return d3.scale.pow().exponent(.5);
2633 d3.scale.ordinal = function() {
2634 return d3_scale_ordinal([], {t: "range", x: []});
2637 function d3_scale_ordinal(domain, ranger) {
2643 return range[((index[x] || (index[x] = domain.push(x))) - 1) % range.length];
2646 function steps(start, step) {
2647 return d3.range(domain.length).map(function(i) { return start + step * i; });
2650 scale.domain = function(x) {
2651 if (!arguments.length) return domain;
2654 var i = -1, n = x.length, xi;
2655 while (++i < n) if (!index[xi = x[i]]) index[xi] = domain.push(xi);
2656 return scale[ranger.t](ranger.x, ranger.p);
2659 scale.range = function(x) {
2660 if (!arguments.length) return range;
2663 ranger = {t: "range", x: x};
2667 scale.rangePoints = function(x, padding) {
2668 if (arguments.length < 2) padding = 0;
2671 step = (stop - start) / (domain.length - 1 + padding);
2672 range = steps(domain.length < 2 ? (start + stop) / 2 : start + step * padding / 2, step);
2674 ranger = {t: "rangePoints", x: x, p: padding};
2678 scale.rangeBands = function(x, padding) {
2679 if (arguments.length < 2) padding = 0;
2682 step = (stop - start) / (domain.length + padding);
2683 range = steps(start + step * padding, step);
2684 rangeBand = step * (1 - padding);
2685 ranger = {t: "rangeBands", x: x, p: padding};
2689 scale.rangeRoundBands = function(x, padding) {
2690 if (arguments.length < 2) padding = 0;
2693 step = Math.floor((stop - start) / (domain.length + padding));
2694 range = steps(start + Math.round((stop - start - (domain.length - padding) * step) / 2), step);
2695 rangeBand = Math.round(step * (1 - padding));
2696 ranger = {t: "rangeRoundBands", x: x, p: padding};
2700 scale.rangeBand = function() {
2704 scale.rangeExtent = function() {
2708 scale.copy = function() {
2709 return d3_scale_ordinal(domain, ranger);
2712 return scale.domain(domain);
2715 * This product includes color specifications and designs developed by Cynthia
2716 * Brewer (http://colorbrewer.org/). See lib/colorbrewer for more information.
2719 d3.scale.category10 = function() {
2720 return d3.scale.ordinal().range(d3_category10);
2723 d3.scale.category20 = function() {
2724 return d3.scale.ordinal().range(d3_category20);
2727 d3.scale.category20b = function() {
2728 return d3.scale.ordinal().range(d3_category20b);
2731 d3.scale.category20c = function() {
2732 return d3.scale.ordinal().range(d3_category20c);
2735 var d3_category10 = [
2736 "#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
2737 "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"
2740 var d3_category20 = [
2741 "#1f77b4", "#aec7e8",
2742 "#ff7f0e", "#ffbb78",
2743 "#2ca02c", "#98df8a",
2744 "#d62728", "#ff9896",
2745 "#9467bd", "#c5b0d5",
2746 "#8c564b", "#c49c94",
2747 "#e377c2", "#f7b6d2",
2748 "#7f7f7f", "#c7c7c7",
2749 "#bcbd22", "#dbdb8d",
2750 "#17becf", "#9edae5"
2753 var d3_category20b = [
2754 "#393b79", "#5254a3", "#6b6ecf", "#9c9ede",
2755 "#637939", "#8ca252", "#b5cf6b", "#cedb9c",
2756 "#8c6d31", "#bd9e39", "#e7ba52", "#e7cb94",
2757 "#843c39", "#ad494a", "#d6616b", "#e7969c",
2758 "#7b4173", "#a55194", "#ce6dbd", "#de9ed6"
2761 var d3_category20c = [
2762 "#3182bd", "#6baed6", "#9ecae1", "#c6dbef",
2763 "#e6550d", "#fd8d3c", "#fdae6b", "#fdd0a2",
2764 "#31a354", "#74c476", "#a1d99b", "#c7e9c0",
2765 "#756bb1", "#9e9ac8", "#bcbddc", "#dadaeb",
2766 "#636363", "#969696", "#bdbdbd", "#d9d9d9"
2768 d3.scale.quantile = function() {
2769 return d3_scale_quantile([], []);
2772 function d3_scale_quantile(domain, range) {
2775 function rescale() {
2780 while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q);
2785 if (isNaN(x = +x)) return NaN;
2786 return range[d3.bisect(thresholds, x)];
2789 scale.domain = function(x) {
2790 if (!arguments.length) return domain;
2791 domain = x.filter(function(d) { return !isNaN(d); }).sort(d3.ascending);
2795 scale.range = function(x) {
2796 if (!arguments.length) return range;
2801 scale.quantiles = function() {
2805 scale.copy = function() {
2806 return d3_scale_quantile(domain, range); // copy on write!
2811 d3.scale.quantize = function() {
2812 return d3_scale_quantize(0, 1, [0, 1]);
2815 function d3_scale_quantize(x0, x1, range) {
2819 return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))];
2822 function rescale() {
2823 kx = range.length / (x1 - x0);
2824 i = range.length - 1;
2828 scale.domain = function(x) {
2829 if (!arguments.length) return [x0, x1];
2831 x1 = +x[x.length - 1];
2835 scale.range = function(x) {
2836 if (!arguments.length) return range;
2841 scale.copy = function() {
2842 return d3_scale_quantize(x0, x1, range); // copy on write
2848 d3.svg.arc = function() {
2849 var innerRadius = d3_svg_arcInnerRadius,
2850 outerRadius = d3_svg_arcOuterRadius,
2851 startAngle = d3_svg_arcStartAngle,
2852 endAngle = d3_svg_arcEndAngle;
2855 var r0 = innerRadius.apply(this, arguments),
2856 r1 = outerRadius.apply(this, arguments),
2857 a0 = startAngle.apply(this, arguments) + d3_svg_arcOffset,
2858 a1 = endAngle.apply(this, arguments) + d3_svg_arcOffset,
2859 da = (a1 < a0 && (da = a0, a0 = a1, a1 = da), a1 - a0),
2860 df = da < Math.PI ? "0" : "1",
2865 return da >= d3_svg_arcMax
2868 + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1)
2869 + "A" + r1 + "," + r1 + " 0 1,1 0," + r1
2871 + "A" + r0 + "," + r0 + " 0 1,0 0," + (-r0)
2872 + "A" + r0 + "," + r0 + " 0 1,0 0," + r0
2875 + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1)
2876 + "A" + r1 + "," + r1 + " 0 1,1 0," + r1
2879 ? "M" + r1 * c0 + "," + r1 * s0
2880 + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1
2881 + "L" + r0 * c1 + "," + r0 * s1
2882 + "A" + r0 + "," + r0 + " 0 " + df + ",0 " + r0 * c0 + "," + r0 * s0
2884 : "M" + r1 * c0 + "," + r1 * s0
2885 + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1
2890 arc.innerRadius = function(v) {
2891 if (!arguments.length) return innerRadius;
2892 innerRadius = d3.functor(v);
2896 arc.outerRadius = function(v) {
2897 if (!arguments.length) return outerRadius;
2898 outerRadius = d3.functor(v);
2902 arc.startAngle = function(v) {
2903 if (!arguments.length) return startAngle;
2904 startAngle = d3.functor(v);
2908 arc.endAngle = function(v) {
2909 if (!arguments.length) return endAngle;
2910 endAngle = d3.functor(v);
2914 arc.centroid = function() {
2915 var r = (innerRadius.apply(this, arguments)
2916 + outerRadius.apply(this, arguments)) / 2,
2917 a = (startAngle.apply(this, arguments)
2918 + endAngle.apply(this, arguments)) / 2 + d3_svg_arcOffset;
2919 return [Math.cos(a) * r, Math.sin(a) * r];
2925 var d3_svg_arcOffset = -Math.PI / 2,
2926 d3_svg_arcMax = 2 * Math.PI - 1e-6;
2928 function d3_svg_arcInnerRadius(d) {
2929 return d.innerRadius;
2932 function d3_svg_arcOuterRadius(d) {
2933 return d.outerRadius;
2936 function d3_svg_arcStartAngle(d) {
2937 return d.startAngle;
2940 function d3_svg_arcEndAngle(d) {
2943 function d3_svg_line(projection) {
2944 var x = d3_svg_lineX,
2946 interpolate = "linear",
2947 interpolator = d3_svg_lineInterpolators[interpolate],
2951 return d.length < 1 ? null : "M" + interpolator(projection(d3_svg_linePoints(this, d, x, y)), tension);
2954 line.x = function(v) {
2955 if (!arguments.length) return x;
2960 line.y = function(v) {
2961 if (!arguments.length) return y;
2966 line.interpolate = function(v) {
2967 if (!arguments.length) return interpolate;
2968 interpolator = d3_svg_lineInterpolators[interpolate = v];
2972 line.tension = function(v) {
2973 if (!arguments.length) return tension;
2981 d3.svg.line = function() {
2982 return d3_svg_line(Object);
2985 // Converts the specified array of data into an array of points
2986 // (x-y tuples), by evaluating the specified `x` and `y` functions on each
2987 // data point. The `this` context of the evaluated functions is the specified
2988 // "self" object; each function is passed the current datum and index.
2989 function d3_svg_linePoints(self, d, x, y) {
2993 fx = typeof x === "function",
2994 fy = typeof y === "function",
2997 while (++i < n) points.push([
2998 x.call(self, value = d[i], i),
2999 y.call(self, value, i)
3002 while (++i < n) points.push([x.call(self, d[i], i), y]);
3004 while (++i < n) points.push([x, y.call(self, d[i], i)]);
3006 while (++i < n) points.push([x, y]);
3011 // The default `x` property, which references d[0].
3012 function d3_svg_lineX(d) {
3016 // The default `y` property, which references d[1].
3017 function d3_svg_lineY(d) {
3021 // The various interpolators supported by the `line` class.
3022 var d3_svg_lineInterpolators = {
3023 "linear": d3_svg_lineLinear,
3024 "step-before": d3_svg_lineStepBefore,
3025 "step-after": d3_svg_lineStepAfter,
3026 "basis": d3_svg_lineBasis,
3027 "basis-open": d3_svg_lineBasisOpen,
3028 "basis-closed": d3_svg_lineBasisClosed,
3029 "bundle": d3_svg_lineBundle,
3030 "cardinal": d3_svg_lineCardinal,
3031 "cardinal-open": d3_svg_lineCardinalOpen,
3032 "cardinal-closed": d3_svg_lineCardinalClosed,
3033 "monotone": d3_svg_lineMonotone
3036 // Linear interpolation; generates "L" commands.
3037 function d3_svg_lineLinear(points) {
3041 path = [p[0], ",", p[1]];
3042 while (++i < n) path.push("L", (p = points[i])[0], ",", p[1]);
3043 return path.join("");
3046 // Step interpolation; generates "H" and "V" commands.
3047 function d3_svg_lineStepBefore(points) {
3051 path = [p[0], ",", p[1]];
3052 while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]);
3053 return path.join("");
3056 // Step interpolation; generates "H" and "V" commands.
3057 function d3_svg_lineStepAfter(points) {
3061 path = [p[0], ",", p[1]];
3062 while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]);
3063 return path.join("");
3066 // Open cardinal spline interpolation; generates "C" commands.
3067 function d3_svg_lineCardinalOpen(points, tension) {
3068 return points.length < 4
3069 ? d3_svg_lineLinear(points)
3070 : points[1] + d3_svg_lineHermite(points.slice(1, points.length - 1),
3071 d3_svg_lineCardinalTangents(points, tension));
3074 // Closed cardinal spline interpolation; generates "C" commands.
3075 function d3_svg_lineCardinalClosed(points, tension) {
3076 return points.length < 3
3077 ? d3_svg_lineLinear(points)
3078 : points[0] + d3_svg_lineHermite((points.push(points[0]), points),
3079 d3_svg_lineCardinalTangents([points[points.length - 2]]
3080 .concat(points, [points[1]]), tension));
3083 // Cardinal spline interpolation; generates "C" commands.
3084 function d3_svg_lineCardinal(points, tension, closed) {
3085 return points.length < 3
3086 ? d3_svg_lineLinear(points)
3087 : points[0] + d3_svg_lineHermite(points,
3088 d3_svg_lineCardinalTangents(points, tension));
3091 // Hermite spline construction; generates "C" commands.
3092 function d3_svg_lineHermite(points, tangents) {
3093 if (tangents.length < 1
3094 || (points.length != tangents.length
3095 && points.length != tangents.length + 2)) {
3096 return d3_svg_lineLinear(points);
3099 var quad = points.length != tangents.length,
3108 path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3)
3109 + "," + p[0] + "," + p[1];
3114 if (tangents.length > 1) {
3118 path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1])
3119 + "," + (p[0] - t[0]) + "," + (p[1] - t[1])
3120 + "," + p[0] + "," + p[1];
3121 for (var i = 2; i < tangents.length; i++, pi++) {
3124 path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1])
3125 + "," + p[0] + "," + p[1];
3130 var lp = points[pi];
3131 path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3)
3132 + "," + lp[0] + "," + lp[1];
3138 // Generates tangents for a cardinal spline.
3139 function d3_svg_lineCardinalTangents(points, tension) {
3141 a = (1 - tension) / 2,
3151 tangents.push([a * (p2[0] - p0[0]), a * (p2[1] - p0[1])]);
3156 // B-spline interpolation; generates "C" commands.
3157 function d3_svg_lineBasis(points) {
3158 if (points.length < 3) return d3_svg_lineLinear(points);
3164 px = [x0, x0, x0, (pi = points[1])[0]],
3165 py = [y0, y0, y0, pi[1]],
3166 path = [x0, ",", y0];
3167 d3_svg_lineBasisBezier(path, px, py);
3170 px.shift(); px.push(pi[0]);
3171 py.shift(); py.push(pi[1]);
3172 d3_svg_lineBasisBezier(path, px, py);
3176 px.shift(); px.push(pi[0]);
3177 py.shift(); py.push(pi[1]);
3178 d3_svg_lineBasisBezier(path, px, py);
3180 return path.join("");
3183 // Open B-spline interpolation; generates "C" commands.
3184 function d3_svg_lineBasisOpen(points) {
3185 if (points.length < 4) return d3_svg_lineLinear(points);
3197 path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px)
3198 + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py));
3199 --i; while (++i < n) {
3201 px.shift(); px.push(pi[0]);
3202 py.shift(); py.push(pi[1]);
3203 d3_svg_lineBasisBezier(path, px, py);
3205 return path.join("");
3208 // Closed B-spline interpolation; generates "C" commands.
3209 function d3_svg_lineBasisClosed(points) {
3223 d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",",
3224 d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)
3226 --i; while (++i < m) {
3228 px.shift(); px.push(pi[0]);
3229 py.shift(); py.push(pi[1]);
3230 d3_svg_lineBasisBezier(path, px, py);
3232 return path.join("");
3235 function d3_svg_lineBundle(points, tension) {
3236 var n = points.length - 1,
3239 dx = points[n][0] - x0,
3240 dy = points[n][1] - y0,
3247 p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx);
3248 p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy);
3250 return d3_svg_lineBasis(points);
3253 // Returns the dot product of the given four-element vectors.
3254 function d3_svg_lineDot4(a, b) {
3255 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
3258 // Matrix to transform basis (b-spline) control points to bezier
3259 // control points. Derived from FvD 11.2.8.
3260 var d3_svg_lineBasisBezier1 = [0, 2/3, 1/3, 0],
3261 d3_svg_lineBasisBezier2 = [0, 1/3, 2/3, 0],
3262 d3_svg_lineBasisBezier3 = [0, 1/6, 2/3, 1/6];
3264 // Pushes a "C" Bézier curve onto the specified path array, given the
3265 // two specified four-element arrays which define the control points.
3266 function d3_svg_lineBasisBezier(path, x, y) {
3268 "C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x),
3269 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y),
3270 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x),
3271 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y),
3272 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x),
3273 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y));
3276 // Computes the slope from points p0 to p1.
3277 function d3_svg_lineSlope(p0, p1) {
3278 return (p1[1] - p0[1]) / (p1[0] - p0[0]);
3281 // Compute three-point differences for the given points.
3282 // http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Finite_difference
3283 function d3_svg_lineFiniteDifferences(points) {
3285 j = points.length - 1,
3289 d = m[0] = d3_svg_lineSlope(p0, p1);
3291 m[i] = d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1]));
3297 // Interpolates the given points using Fritsch-Carlson Monotone cubic Hermite
3298 // interpolation. Returns an array of tangent vectors. For details, see
3299 // http://en.wikipedia.org/wiki/Monotone_cubic_interpolation
3300 function d3_svg_lineMonotoneTangents(points) {
3306 m = d3_svg_lineFiniteDifferences(points),
3308 j = points.length - 1;
3310 // The first two steps are done by computing finite-differences:
3311 // 1. Compute the slopes of the secant lines between successive points.
3312 // 2. Initialize the tangents at every point as the average of the secants.
3314 // Then, for each segment…
3316 d = d3_svg_lineSlope(points[i], points[i + 1]);
3318 // 3. If two successive yk = y{k + 1} are equal (i.e., d is zero), then set
3319 // mk = m{k + 1} = 0 as the spline connecting these points must be flat to
3320 // preserve monotonicity. Ignore step 4 and 5 for those k.
3322 if (Math.abs(d) < 1e-6) {
3323 m[i] = m[i + 1] = 0;
3325 // 4. Let ak = mk / dk and bk = m{k + 1} / dk.
3329 // 5. Prevent overshoot and ensure monotonicity by restricting the
3330 // magnitude of vector <ak, bk> to a circle of radius 3.
3333 s = d * 3 / Math.sqrt(s);
3340 // Compute the normalized tangent vector from the slopes. Note that if x is
3341 // not monotonic, it's possible that the slope will be infinite, so we protect
3342 // against NaN by setting the coordinate to zero.
3343 i = -1; while (++i <= j) {
3344 s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0])
3345 / (6 * (1 + m[i] * m[i]));
3346 tangents.push([s || 0, m[i] * s || 0]);
3352 function d3_svg_lineMonotone(points) {
3353 return points.length < 3
3354 ? d3_svg_lineLinear(points)
3356 d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points));
3358 d3.svg.line.radial = function() {
3359 var line = d3_svg_line(d3_svg_lineRadial);
3360 line.radius = line.x, delete line.x;
3361 line.angle = line.y, delete line.y;
3365 function d3_svg_lineRadial(points) {
3374 a = point[1] + d3_svg_arcOffset;
3375 point[0] = r * Math.cos(a);
3376 point[1] = r * Math.sin(a);
3380 function d3_svg_area(projection) {
3381 var x0 = d3_svg_lineX,
3391 if (d.length < 1) return null;
3392 var points0 = d3_svg_linePoints(this, d, x0, y0),
3393 points1 = d3_svg_linePoints(this, d, x0 === x1 ? d3_svg_areaX(points0) : x1, y0 === y1 ? d3_svg_areaY(points0) : y1);
3394 return "M" + i0(projection(points1), tension)
3395 + "L" + i1(projection(points0.reverse()), tension)
3399 area.x = function(x) {
3400 if (!arguments.length) return x1;
3405 area.x0 = function(x) {
3406 if (!arguments.length) return x0;
3411 area.x1 = function(x) {
3412 if (!arguments.length) return x1;
3417 area.y = function(y) {
3418 if (!arguments.length) return y1;
3423 area.y0 = function(y) {
3424 if (!arguments.length) return y0;
3429 area.y1 = function(y) {
3430 if (!arguments.length) return y1;
3435 area.interpolate = function(x) {
3436 if (!arguments.length) return interpolate;
3437 i0 = d3_svg_lineInterpolators[interpolate = x];
3438 i1 = i0.reverse || i0;
3442 area.tension = function(x) {
3443 if (!arguments.length) return tension;
3448 return area.interpolate("linear");
3451 d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter;
3452 d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore;
3454 d3.svg.area = function() {
3455 return d3_svg_area(Object);
3458 function d3_svg_areaX(points) {
3459 return function(d, i) {
3460 return points[i][0];
3464 function d3_svg_areaY(points) {
3465 return function(d, i) {
3466 return points[i][1];
3469 d3.svg.area.radial = function() {
3470 var area = d3_svg_area(d3_svg_lineRadial);
3471 area.radius = area.x, delete area.x;
3472 area.innerRadius = area.x0, delete area.x0;
3473 area.outerRadius = area.x1, delete area.x1;
3474 area.angle = area.y, delete area.y;
3475 area.startAngle = area.y0, delete area.y0;
3476 area.endAngle = area.y1, delete area.y1;
3479 d3.svg.chord = function() {
3480 var source = d3_svg_chordSource,
3481 target = d3_svg_chordTarget,
3482 radius = d3_svg_chordRadius,
3483 startAngle = d3_svg_arcStartAngle,
3484 endAngle = d3_svg_arcEndAngle;
3486 // TODO Allow control point to be customized.
3488 function chord(d, i) {
3489 var s = subgroup(this, source, d, i),
3490 t = subgroup(this, target, d, i);
3492 + arc(s.r, s.p1) + (equals(s, t)
3493 ? curve(s.r, s.p1, s.r, s.p0)
3494 : curve(s.r, s.p1, t.r, t.p0)
3496 + curve(t.r, t.p1, s.r, s.p0))
3500 function subgroup(self, f, d, i) {
3501 var subgroup = f.call(self, d, i),
3502 r = radius.call(self, subgroup, i),
3503 a0 = startAngle.call(self, subgroup, i) + d3_svg_arcOffset,
3504 a1 = endAngle.call(self, subgroup, i) + d3_svg_arcOffset;
3509 p0: [r * Math.cos(a0), r * Math.sin(a0)],
3510 p1: [r * Math.cos(a1), r * Math.sin(a1)]
3514 function equals(a, b) {
3515 return a.a0 == b.a0 && a.a1 == b.a1;
3518 function arc(r, p) {
3519 return "A" + r + "," + r + " 0 0,1 " + p;
3522 function curve(r0, p0, r1, p1) {
3523 return "Q 0,0 " + p1;
3526 chord.radius = function(v) {
3527 if (!arguments.length) return radius;
3528 radius = d3.functor(v);
3532 chord.source = function(v) {
3533 if (!arguments.length) return source;
3534 source = d3.functor(v);
3538 chord.target = function(v) {
3539 if (!arguments.length) return target;
3540 target = d3.functor(v);
3544 chord.startAngle = function(v) {
3545 if (!arguments.length) return startAngle;
3546 startAngle = d3.functor(v);
3550 chord.endAngle = function(v) {
3551 if (!arguments.length) return endAngle;
3552 endAngle = d3.functor(v);
3559 function d3_svg_chordSource(d) {
3563 function d3_svg_chordTarget(d) {
3567 function d3_svg_chordRadius(d) {
3571 function d3_svg_chordStartAngle(d) {
3572 return d.startAngle;
3575 function d3_svg_chordEndAngle(d) {
3578 d3.svg.diagonal = function() {
3579 var source = d3_svg_chordSource,
3580 target = d3_svg_chordTarget,
3581 projection = d3_svg_diagonalProjection;
3583 function diagonal(d, i) {
3584 var p0 = source.call(this, d, i),
3585 p3 = target.call(this, d, i),
3586 m = (p0.y + p3.y) / 2,
3587 p = [p0, {x: p0.x, y: m}, {x: p3.x, y: m}, p3];
3588 p = p.map(projection);
3589 return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3];
3592 diagonal.source = function(x) {
3593 if (!arguments.length) return source;
3594 source = d3.functor(x);
3598 diagonal.target = function(x) {
3599 if (!arguments.length) return target;
3600 target = d3.functor(x);
3604 diagonal.projection = function(x) {
3605 if (!arguments.length) return projection;
3613 function d3_svg_diagonalProjection(d) {
3616 d3.svg.diagonal.radial = function() {
3617 var diagonal = d3.svg.diagonal(),
3618 projection = d3_svg_diagonalProjection,
3619 projection_ = diagonal.projection;
3621 diagonal.projection = function(x) {
3622 return arguments.length
3623 ? projection_(d3_svg_diagonalRadialProjection(projection = x))
3630 function d3_svg_diagonalRadialProjection(projection) {
3632 var d = projection.apply(this, arguments),
3634 a = d[1] + d3_svg_arcOffset;
3635 return [r * Math.cos(a), r * Math.sin(a)];
3638 d3.svg.mouse = function(container) {
3639 return d3_svg_mousePoint(container, d3.event);
3642 // https://bugs.webkit.org/show_bug.cgi?id=44083
3643 var d3_mouse_bug44083 = /WebKit/.test(navigator.userAgent) ? -1 : 0;
3645 function d3_svg_mousePoint(container, e) {
3646 var point = (container.ownerSVGElement || container).createSVGPoint();
3647 if ((d3_mouse_bug44083 < 0) && (window.scrollX || window.scrollY)) {
3648 var svg = d3.select(document.body)
3650 .style("position", "absolute")
3653 var ctm = svg[0][0].getScreenCTM();
3654 d3_mouse_bug44083 = !(ctm.f || ctm.e);
3657 if (d3_mouse_bug44083) {
3661 point.x = e.clientX;
3662 point.y = e.clientY;
3664 point = point.matrixTransform(container.getScreenCTM().inverse());
3665 return [point.x, point.y];
3667 d3.svg.touches = function(container, touches) {
3668 if (arguments.length < 2) touches = d3.event.touches;
3670 return touches ? d3_array(touches).map(function(touch) {
3671 var point = d3_svg_mousePoint(container, touch);
3672 point.identifier = touch.identifier;
3676 d3.svg.symbol = function() {
3677 var type = d3_svg_symbolType,
3678 size = d3_svg_symbolSize;
3680 function symbol(d, i) {
3681 return (d3_svg_symbols[type.call(this, d, i)]
3682 || d3_svg_symbols.circle)
3683 (size.call(this, d, i));
3686 symbol.type = function(x) {
3687 if (!arguments.length) return type;
3688 type = d3.functor(x);
3692 // size of symbol in square pixels
3693 symbol.size = function(x) {
3694 if (!arguments.length) return size;
3695 size = d3.functor(x);
3702 function d3_svg_symbolSize() {
3706 function d3_svg_symbolType() {
3710 // TODO cross-diagonal?
3711 var d3_svg_symbols = {
3712 "circle": function(size) {
3713 var r = Math.sqrt(size / Math.PI);
3715 + "A" + r + "," + r + " 0 1,1 0," + (-r)
3716 + "A" + r + "," + r + " 0 1,1 0," + r
3719 "cross": function(size) {
3720 var r = Math.sqrt(size / 5) / 2;
3721 return "M" + -3 * r + "," + -r
3735 "diamond": function(size) {
3736 var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)),
3737 rx = ry * d3_svg_symbolTan30;
3744 "square": function(size) {
3745 var r = Math.sqrt(size) / 2;
3746 return "M" + -r + "," + -r
3747 + "L" + r + "," + -r
3749 + " " + -r + "," + r
3752 "triangle-down": function(size) {
3753 var rx = Math.sqrt(size / d3_svg_symbolSqrt3),
3754 ry = rx * d3_svg_symbolSqrt3 / 2;
3756 + "L" + rx +"," + -ry
3757 + " " + -rx + "," + -ry
3760 "triangle-up": function(size) {
3761 var rx = Math.sqrt(size / d3_svg_symbolSqrt3),
3762 ry = rx * d3_svg_symbolSqrt3 / 2;
3764 + "L" + rx +"," + ry
3765 + " " + -rx + "," + ry
3770 d3.svg.symbolTypes = d3.keys(d3_svg_symbols);
3772 var d3_svg_symbolSqrt3 = Math.sqrt(3),
3773 d3_svg_symbolTan30 = Math.tan(30 * Math.PI / 180);
3774 d3.svg.axis = function() {
3775 var scale = d3.scale.linear(),
3781 tickArguments_ = [10],
3785 function axis(selection) {
3786 selection.each(function(d, i, j) {
3787 var g = d3.select(this);
3789 // If selection is a transition, create subtransitions.
3790 var transition = selection.delay ? function(o) {
3791 var id = d3_transitionInheritId;
3793 d3_transitionInheritId = selection.id;
3794 return o.transition()
3795 .delay(selection[j][i].delay)
3796 .duration(selection[j][i].duration)
3797 .ease(selection.ease());
3799 d3_transitionInheritId = id;
3803 // Ticks, or domain values for ordinal scales.
3804 var ticks = scale.ticks ? scale.ticks.apply(scale, tickArguments_) : scale.domain(),
3805 tickFormat = tickFormat_ == null ? (scale.tickFormat ? scale.tickFormat.apply(scale, tickArguments_) : String) : tickFormat_;
3808 var subticks = d3_svg_axisSubdivide(scale, ticks, tickSubdivide),
3809 subtick = g.selectAll(".minor").data(subticks, String),
3810 subtickEnter = subtick.enter().insert("svg:line", "g").attr("class", "tick minor").style("opacity", 1e-6),
3811 subtickExit = transition(subtick.exit()).style("opacity", 1e-6).remove(),
3812 subtickUpdate = transition(subtick).style("opacity", 1);
3815 var tick = g.selectAll("g").data(ticks, String),
3816 tickEnter = tick.enter().insert("svg:g", "path").style("opacity", 1e-6),
3817 tickExit = transition(tick.exit()).style("opacity", 1e-6).remove(),
3818 tickUpdate = transition(tick).style("opacity", 1),
3822 var range = d3_scaleRange(scale),
3823 path = g.selectAll(".domain").data([0]),
3824 pathEnter = path.enter().append("svg:path").attr("class", "domain"),
3825 pathUpdate = transition(path);
3827 // Stash a snapshot of the new scale, and retrieve the old snapshot.
3828 var scale1 = scale.copy(),
3829 scale0 = this.__chart__ || scale1;
3830 this.__chart__ = scale1;
3832 tickEnter.append("svg:line").attr("class", "tick");
3833 tickEnter.append("svg:text");
3834 tickUpdate.select("text").text(tickFormat);
3838 tickTransform = d3_svg_axisX;
3839 subtickUpdate.attr("x2", 0).attr("y2", tickMinorSize);
3840 tickUpdate.select("line").attr("x2", 0).attr("y2", tickMajorSize);
3841 tickUpdate.select("text").attr("x", 0).attr("y", Math.max(tickMajorSize, 0) + tickPadding).attr("dy", ".71em").attr("text-anchor", "middle");
3842 pathUpdate.attr("d", "M" + range[0] + "," + tickEndSize + "V0H" + range[1] + "V" + tickEndSize);
3846 tickTransform = d3_svg_axisX;
3847 subtickUpdate.attr("x2", 0).attr("y2", -tickMinorSize);
3848 tickUpdate.select("line").attr("x2", 0).attr("y2", -tickMajorSize);
3849 tickUpdate.select("text").attr("x", 0).attr("y", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("dy", "0em").attr("text-anchor", "middle");
3850 pathUpdate.attr("d", "M" + range[0] + "," + -tickEndSize + "V0H" + range[1] + "V" + -tickEndSize);
3854 tickTransform = d3_svg_axisY;
3855 subtickUpdate.attr("x2", -tickMinorSize).attr("y2", 0);
3856 tickUpdate.select("line").attr("x2", -tickMajorSize).attr("y2", 0);
3857 tickUpdate.select("text").attr("x", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("y", 0).attr("dy", ".32em").attr("text-anchor", "end");
3858 pathUpdate.attr("d", "M" + -tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + -tickEndSize);
3862 tickTransform = d3_svg_axisY;
3863 subtickUpdate.attr("x2", tickMinorSize).attr("y2", 0);
3864 tickUpdate.select("line").attr("x2", tickMajorSize).attr("y2", 0);
3865 tickUpdate.select("text").attr("x", Math.max(tickMajorSize, 0) + tickPadding).attr("y", 0).attr("dy", ".32em").attr("text-anchor", "start");
3866 pathUpdate.attr("d", "M" + tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + tickEndSize);
3871 // For quantitative scales:
3872 // - enter new ticks from the old scale
3873 // - exit old ticks to the new scale
3875 tickEnter.call(tickTransform, scale0);
3876 tickUpdate.call(tickTransform, scale1);
3877 tickExit.call(tickTransform, scale1);
3878 subtickEnter.call(tickTransform, scale0);
3879 subtickUpdate.call(tickTransform, scale1);
3880 subtickExit.call(tickTransform, scale1);
3883 // For ordinal scales:
3884 // - any entering ticks are undefined in the old scale
3885 // - any exiting ticks are undefined in the new scale
3886 // Therefore, we only need to transition updating ticks.
3888 var dx = scale1.rangeBand() / 2, x = function(d) { return scale1(d) + dx; };
3889 tickEnter.call(tickTransform, x);
3890 tickUpdate.call(tickTransform, x);
3895 axis.scale = function(x) {
3896 if (!arguments.length) return scale;
3901 axis.orient = function(x) {
3902 if (!arguments.length) return orient;
3907 axis.ticks = function() {
3908 if (!arguments.length) return tickArguments_;
3909 tickArguments_ = arguments;
3913 axis.tickFormat = function(x) {
3914 if (!arguments.length) return tickFormat_;
3919 axis.tickSize = function(x, y, z) {
3920 if (!arguments.length) return tickMajorSize;
3921 var n = arguments.length - 1;
3923 tickMinorSize = n > 1 ? +y : tickMajorSize;
3924 tickEndSize = n > 0 ? +arguments[n] : tickMajorSize;
3928 axis.tickPadding = function(x) {
3929 if (!arguments.length) return tickPadding;
3934 axis.tickSubdivide = function(x) {
3935 if (!arguments.length) return tickSubdivide;
3943 function d3_svg_axisX(selection, x) {
3944 selection.attr("transform", function(d) { return "translate(" + x(d) + ",0)"; });
3947 function d3_svg_axisY(selection, y) {
3948 selection.attr("transform", function(d) { return "translate(0," + y(d) + ")"; });
3951 function d3_svg_axisSubdivide(scale, ticks, m) {
3953 if (m && ticks.length > 1) {
3954 var extent = d3_scaleExtent(scale.domain()),
3958 d = (ticks[1] - ticks[0]) / ++m,
3962 for (j = m; --j > 0;) {
3963 if ((v = +ticks[i] - j * d) >= extent[0]) {
3968 for (--i, j = 0; ++j < m && (v = +ticks[i] + j * d) < extent[1];) {
3974 d3.svg.brush = function() {
3975 var event = d3.dispatch("brushstart", "brush", "brushend"),
3976 x, // x-scale, optional
3977 y, // y-scale, optional
3978 extent = [[0, 0], [0, 0]]; // [x0, y0], [x1, y1]
3981 var resizes = x && y ? ["n", "e", "s", "w", "nw", "ne", "se", "sw"]
3987 var g = d3.select(this).on("mousedown.brush", down),
3988 bg = g.selectAll(".background").data([,]),
3989 fg = g.selectAll(".extent").data([,]),
3990 tz = g.selectAll(".resize").data(resizes, String),
3993 // An invisible, mouseable area for starting a new brush.
3994 bg.enter().append("svg:rect")
3995 .attr("class", "background")
3996 .style("visibility", "hidden")
3997 .style("pointer-events", "all")
3998 .style("cursor", "crosshair");
4000 // The visible brush extent; style this as you like!
4001 fg.enter().append("svg:rect")
4002 .attr("class", "extent")
4003 .style("cursor", "move");
4005 // More invisible rects for resizing the extent.
4006 tz.enter().append("svg:rect")
4007 .attr("class", function(d) { return "resize " + d; })
4010 .style("visibility", "hidden")
4011 .style("pointer-events", brush.empty() ? "none" : "all")
4012 .style("cursor", function(d) { return d3_svg_brushCursor[d]; });
4014 // Remove any superfluous resizers.
4017 // Initialize the background to fill the defined range.
4018 // If the range isn't defined, you can post-process.
4020 e = d3_scaleRange(x);
4021 bg.attr("x", e[0]).attr("width", e[1] - e[0]);
4022 d3_svg_brushRedrawX(g, extent);
4025 e = d3_scaleRange(y);
4026 bg.attr("y", e[0]).attr("height", e[1] - e[0]);
4027 d3_svg_brushRedrawY(g, extent);
4033 var target = d3.select(d3.event.target);
4035 // Store some global state for the duration of the brush gesture.
4036 d3_svg_brush = brush;
4037 d3_svg_brushTarget = this;
4038 d3_svg_brushExtent = extent;
4039 d3_svg_brushOffset = d3.svg.mouse(d3_svg_brushTarget);
4041 // If the extent was clicked on, drag rather than brush;
4042 // store the offset between the mouse and extent origin instead.
4043 if (d3_svg_brushDrag = target.classed("extent")) {
4044 d3_svg_brushOffset[0] = extent[0][0] - d3_svg_brushOffset[0];
4045 d3_svg_brushOffset[1] = extent[0][1] - d3_svg_brushOffset[1];
4048 // If a resizer was clicked on, record which side is to be resized.
4049 // Also, set the offset to the opposite side.
4050 else if (target.classed("resize")) {
4051 d3_svg_brushResize = d3.event.target.__data__;
4052 d3_svg_brushOffset[0] = extent[+/w$/.test(d3_svg_brushResize)][0];
4053 d3_svg_brushOffset[1] = extent[+/^n/.test(d3_svg_brushResize)][1];
4056 // If the ALT key is down when starting a brush, the center is at the mouse.
4057 else if (d3.event.altKey) {
4058 d3_svg_brushCenter = d3_svg_brushOffset.slice();
4061 // Restrict which dimensions are resized.
4062 d3_svg_brushX = !/^(n|s)$/.test(d3_svg_brushResize) && x;
4063 d3_svg_brushY = !/^(e|w)$/.test(d3_svg_brushResize) && y;
4065 // Notify listeners.
4066 d3_svg_brushDispatch = dispatcher(this, arguments);
4067 d3_svg_brushDispatch("brushstart");
4072 function dispatcher(that, argumentz) {
4073 return function(type) {
4076 d3.event = {type: type, target: brush};
4077 event[type].apply(that, argumentz);
4084 brush.x = function(z) {
4085 if (!arguments.length) return x;
4090 brush.y = function(z) {
4091 if (!arguments.length) return y;
4096 brush.extent = function(z) {
4097 var x0, x1, y0, y1, t;
4099 // Invert the pixel extent to data-space.
4100 if (!arguments.length) {
4102 x0 = extent[0][0], x1 = extent[1][0];
4103 if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1);
4104 if (x1 < x0) t = x0, x0 = x1, x1 = t;
4107 y0 = extent[0][1], y1 = extent[1][1];
4108 if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1);
4109 if (y1 < y0) t = y0, y0 = y1, y1 = t;
4111 return x && y ? [[x0, y0], [x1, y1]] : x ? [x0, x1] : y && [y0, y1];
4114 // Scale the data-space extent to pixels.
4116 x0 = z[0], x1 = z[1];
4117 if (y) x0 = x0[0], x1 = x1[0];
4118 if (x.invert) x0 = x(x0), x1 = x(x1);
4119 if (x1 < x0) t = x0, x0 = x1, x1 = t;
4120 extent[0][0] = x0, extent[1][0] = x1;
4123 y0 = z[0], y1 = z[1];
4124 if (x) y0 = y0[1], y1 = y1[1];
4125 if (y.invert) y0 = y(y0), y1 = y(y1);
4126 if (y1 < y0) t = y0, y0 = y1, y1 = t;
4127 extent[0][1] = y0, extent[1][1] = y1;
4133 brush.clear = function() {
4141 brush.empty = function() {
4142 return (x && extent[0][0] === extent[1][0])
4143 || (y && extent[0][1] === extent[1][1]);
4147 .on("mousemove.brush", d3_svg_brushMove)
4148 .on("mouseup.brush", d3_svg_brushUp)
4149 .on("keydown.brush", d3_svg_brushKeydown)
4150 .on("keyup.brush", d3_svg_brushKeyup);
4152 return d3.rebind(brush, event, "on");
4156 d3_svg_brushDispatch,
4166 function d3_svg_brushRedrawX(g, extent) {
4167 g.select(".extent").attr("x", extent[0][0]);
4168 g.selectAll(".n,.s,.w,.nw,.sw").attr("x", extent[0][0] - 2);
4169 g.selectAll(".e,.ne,.se").attr("x", extent[1][0] - 3);
4170 g.selectAll(".extent,.n,.s").attr("width", extent[1][0] - extent[0][0]);
4173 function d3_svg_brushRedrawY(g, extent) {
4174 g.select(".extent").attr("y", extent[0][1]);
4175 g.selectAll(".n,.e,.w,.nw,.ne").attr("y", extent[0][1] - 3);
4176 g.selectAll(".s,.se,.sw").attr("y", extent[1][1] - 4);
4177 g.selectAll(".extent,.e,.w").attr("height", extent[1][1] - extent[0][1]);
4180 function d3_svg_brushKeydown() {
4181 if (d3.event.keyCode == 32 && d3_svg_brushTarget && !d3_svg_brushDrag) {
4182 d3_svg_brushCenter = null;
4183 d3_svg_brushOffset[0] -= d3_svg_brushExtent[1][0];
4184 d3_svg_brushOffset[1] -= d3_svg_brushExtent[1][1];
4185 d3_svg_brushDrag = 2;
4190 function d3_svg_brushKeyup() {
4191 if (d3.event.keyCode == 32 && d3_svg_brushDrag == 2) {
4192 d3_svg_brushOffset[0] += d3_svg_brushExtent[1][0];
4193 d3_svg_brushOffset[1] += d3_svg_brushExtent[1][1];
4194 d3_svg_brushDrag = 0;
4199 function d3_svg_brushMove() {
4200 if (d3_svg_brushOffset) {
4201 var mouse = d3.svg.mouse(d3_svg_brushTarget),
4202 g = d3.select(d3_svg_brushTarget);
4204 if (!d3_svg_brushDrag) {
4206 // If needed, determine the center from the current extent.
4207 if (d3.event.altKey) {
4208 if (!d3_svg_brushCenter) {
4209 d3_svg_brushCenter = [
4210 (d3_svg_brushExtent[0][0] + d3_svg_brushExtent[1][0]) / 2,
4211 (d3_svg_brushExtent[0][1] + d3_svg_brushExtent[1][1]) / 2
4215 // Update the offset, for when the ALT key is released.
4216 d3_svg_brushOffset[0] = d3_svg_brushExtent[+(mouse[0] < d3_svg_brushCenter[0])][0];
4217 d3_svg_brushOffset[1] = d3_svg_brushExtent[+(mouse[1] < d3_svg_brushCenter[1])][1];
4220 // When the ALT key is released, we clear the center.
4221 else d3_svg_brushCenter = null;
4224 // Update the brush extent for each dimension.
4225 if (d3_svg_brushX) {
4226 d3_svg_brushMove1(mouse, d3_svg_brushX, 0);
4227 d3_svg_brushRedrawX(g, d3_svg_brushExtent);
4229 if (d3_svg_brushY) {
4230 d3_svg_brushMove1(mouse, d3_svg_brushY, 1);
4231 d3_svg_brushRedrawY(g, d3_svg_brushExtent);
4234 // Notify listeners.
4235 d3_svg_brushDispatch("brush");
4239 function d3_svg_brushMove1(mouse, scale, i) {
4240 var range = d3_scaleRange(scale),
4243 offset = d3_svg_brushOffset[i],
4244 size = d3_svg_brushExtent[1][i] - d3_svg_brushExtent[0][i],
4248 // When dragging, reduce the range by the extent size and offset.
4249 if (d3_svg_brushDrag) {
4251 r1 -= size + offset;
4254 // Clamp the mouse so that the extent fits within the range extent.
4255 min = Math.max(r0, Math.min(r1, mouse[i]));
4257 // Compute the new extent bounds.
4258 if (d3_svg_brushDrag) {
4259 max = (min += offset) + size;
4262 // If the ALT key is pressed, then preserve the center of the extent.
4263 if (d3_svg_brushCenter) offset = Math.max(r0, Math.min(r1, 2 * d3_svg_brushCenter[i] - min));
4265 // Compute the min and max of the offset and mouse.
4274 // Update the stored bounds.
4275 d3_svg_brushExtent[0][i] = min;
4276 d3_svg_brushExtent[1][i] = max;
4279 function d3_svg_brushUp() {
4280 if (d3_svg_brushOffset) {
4282 d3.select(d3_svg_brushTarget).selectAll(".resize").style("pointer-events", d3_svg_brush.empty() ? "none" : "all");
4283 d3_svg_brushDispatch("brushend");
4285 d3_svg_brushDispatch =
4286 d3_svg_brushTarget =
4289 d3_svg_brushExtent =
4291 d3_svg_brushResize =
4292 d3_svg_brushCenter =
4293 d3_svg_brushOffset = null;
4298 var d3_svg_brushCursor = {
4309 // TODO Track touch points by identifier.
4311 d3.behavior.drag = function() {
4312 var event = d3.dispatch("drag", "dragstart", "dragend"),
4317 .on("mousedown.drag", mousedown)
4318 .on("touchstart.drag", mousedown);
4321 .on("mousemove.drag", d3_behavior_dragMove)
4322 .on("touchmove.drag", d3_behavior_dragMove)
4323 .on("mouseup.drag", d3_behavior_dragUp, true)
4324 .on("touchend.drag", d3_behavior_dragUp, true)
4325 .on("click.drag", d3_behavior_dragClick, true);
4328 // snapshot the local context for subsequent dispatch
4330 d3_behavior_dragEvent = event;
4331 d3_behavior_dragEventTarget = d3.event.target;
4332 d3_behavior_dragTarget = this;
4333 d3_behavior_dragArguments = arguments;
4334 d3_behavior_dragOrigin = d3_behavior_dragPoint();
4336 d3_behavior_dragOffset = origin.apply(d3_behavior_dragTarget, d3_behavior_dragArguments);
4337 d3_behavior_dragOffset = [d3_behavior_dragOffset.x - d3_behavior_dragOrigin[0], d3_behavior_dragOffset.y - d3_behavior_dragOrigin[1]];
4339 d3_behavior_dragOffset = [0, 0];
4341 d3_behavior_dragMoved = 0;
4344 function mousedown() {
4345 start.apply(this, arguments);
4346 d3_behavior_dragDispatch("dragstart");
4349 drag.origin = function(x) {
4350 if (!arguments.length) return origin;
4355 return d3.rebind(drag, event, "on");
4358 var d3_behavior_dragEvent,
4359 d3_behavior_dragEventTarget,
4360 d3_behavior_dragTarget,
4361 d3_behavior_dragArguments,
4362 d3_behavior_dragOffset,
4363 d3_behavior_dragOrigin,
4364 d3_behavior_dragMoved;
4366 function d3_behavior_dragDispatch(type) {
4367 var p = d3_behavior_dragPoint(),
4369 e = d3.event = {type: type};
4372 e.x = p[0] + d3_behavior_dragOffset[0];
4373 e.y = p[1] + d3_behavior_dragOffset[1];
4374 e.dx = p[0] - d3_behavior_dragOrigin[0];
4375 e.dy = p[1] - d3_behavior_dragOrigin[1];
4376 d3_behavior_dragMoved |= e.dx | e.dy;
4377 d3_behavior_dragOrigin = p;
4381 d3_behavior_dragEvent[type].apply(d3_behavior_dragTarget, d3_behavior_dragArguments);
4386 o.stopPropagation();
4390 function d3_behavior_dragPoint() {
4391 var p = d3_behavior_dragTarget.parentNode,
4392 t = d3.event.changedTouches;
4394 ? d3.svg.touches(p, t)[0]
4398 function d3_behavior_dragMove() {
4399 if (!d3_behavior_dragTarget) return;
4400 var parent = d3_behavior_dragTarget.parentNode;
4402 // O NOES! The drag element was removed from the DOM.
4403 if (!parent) return d3_behavior_dragUp();
4405 d3_behavior_dragDispatch("drag");
4409 function d3_behavior_dragUp() {
4410 if (!d3_behavior_dragTarget) return;
4411 d3_behavior_dragDispatch("dragend");
4413 // If the node was moved, prevent the mouseup from propagating.
4414 // Also prevent the subsequent click from propagating (e.g., for anchors).
4415 if (d3_behavior_dragMoved) {
4417 d3_behavior_dragMoved = d3.event.target === d3_behavior_dragEventTarget;
4420 d3_behavior_dragEvent =
4421 d3_behavior_dragEventTarget =
4422 d3_behavior_dragTarget =
4423 d3_behavior_dragArguments =
4424 d3_behavior_dragOffset =
4425 d3_behavior_dragOrigin = null;
4428 function d3_behavior_dragClick() {
4429 if (d3_behavior_dragMoved) {
4431 d3_behavior_dragMoved = 0;
4434 // TODO unbind zoom behavior?
4435 d3.behavior.zoom = function() {
4436 var xyz = [0, 0, 0],
4437 event = d3.dispatch("zoom"),
4438 extent = d3_behavior_zoomInfiniteExtent;
4442 .on("mousedown.zoom", mousedown)
4443 .on("mousewheel.zoom", mousewheel)
4444 .on("DOMMouseScroll.zoom", mousewheel)
4445 .on("dblclick.zoom", dblclick)
4446 .on("touchstart.zoom", touchstart);
4449 .on("mousemove.zoom", d3_behavior_zoomMousemove)
4450 .on("mouseup.zoom", d3_behavior_zoomMouseup)
4451 .on("touchmove.zoom", d3_behavior_zoomTouchmove)
4452 .on("touchend.zoom", d3_behavior_zoomTouchup)
4453 .on("click.zoom", d3_behavior_zoomClick, true);
4456 // snapshot the local context for subsequent dispatch
4458 d3_behavior_zoomXyz = xyz;
4459 d3_behavior_zoomExtent = extent;
4460 d3_behavior_zoomDispatch = event.zoom;
4461 d3_behavior_zoomEventTarget = d3.event.target;
4462 d3_behavior_zoomTarget = this;
4463 d3_behavior_zoomArguments = arguments;
4466 function mousedown() {
4467 start.apply(this, arguments);
4468 d3_behavior_zoomPanning = d3_behavior_zoomLocation(d3.svg.mouse(d3_behavior_zoomTarget));
4469 d3_behavior_zoomMoved = 0;
4470 d3.event.preventDefault();
4474 // store starting mouse location
4475 function mousewheel() {
4476 start.apply(this, arguments);
4477 if (!d3_behavior_zoomZooming) d3_behavior_zoomZooming = d3_behavior_zoomLocation(d3.svg.mouse(d3_behavior_zoomTarget));
4478 d3_behavior_zoomTo(d3_behavior_zoomDelta() + xyz[2], d3.svg.mouse(d3_behavior_zoomTarget), d3_behavior_zoomZooming);
4481 function dblclick() {
4482 start.apply(this, arguments);
4483 var mouse = d3.svg.mouse(d3_behavior_zoomTarget);
4484 d3_behavior_zoomTo(d3.event.shiftKey ? Math.ceil(xyz[2] - 1) : Math.floor(xyz[2] + 1), mouse, d3_behavior_zoomLocation(mouse));
4487 // doubletap detection
4488 function touchstart() {
4489 start.apply(this, arguments);
4490 var touches = d3_behavior_zoomTouchup(),
4493 if ((touches.length === 1) && (now - d3_behavior_zoomLast < 300)) {
4494 d3_behavior_zoomTo(1 + Math.floor(xyz[2]), touch = touches[0], d3_behavior_zoomLocations[touch.identifier]);
4496 d3_behavior_zoomLast = now;
4499 zoom.extent = function(x) {
4500 if (!arguments.length) return extent;
4501 extent = x == null ? d3_behavior_zoomInfiniteExtent : x;
4505 return d3.rebind(zoom, event, "on");
4508 var d3_behavior_zoomDiv,
4509 d3_behavior_zoomPanning,
4510 d3_behavior_zoomZooming,
4511 d3_behavior_zoomLocations = {}, // identifier -> location
4512 d3_behavior_zoomLast = 0,
4513 d3_behavior_zoomXyz,
4514 d3_behavior_zoomExtent,
4515 d3_behavior_zoomDispatch,
4516 d3_behavior_zoomEventTarget,
4517 d3_behavior_zoomTarget,
4518 d3_behavior_zoomArguments,
4519 d3_behavior_zoomMoved;
4521 function d3_behavior_zoomLocation(point) {
4523 point[0] - d3_behavior_zoomXyz[0],
4524 point[1] - d3_behavior_zoomXyz[1],
4525 d3_behavior_zoomXyz[2]
4529 // detect the pixels that would be scrolled by this wheel event
4530 function d3_behavior_zoomDelta() {
4532 // mousewheel events are totally broken!
4533 // https://bugs.webkit.org/show_bug.cgi?id=40441
4534 // not only that, but Chrome and Safari differ in re. to acceleration!
4535 if (!d3_behavior_zoomDiv) {
4536 d3_behavior_zoomDiv = d3.select("body").append("div")
4537 .style("visibility", "hidden")
4541 .style("overflow-y", "scroll")
4543 .style("height", "2000px")
4547 var e = d3.event, delta;
4549 d3_behavior_zoomDiv.scrollTop = 1000;
4550 d3_behavior_zoomDiv.dispatchEvent(e);
4551 delta = 1000 - d3_behavior_zoomDiv.scrollTop;
4553 delta = e.wheelDelta || (-e.detail * 5);
4556 return delta * .005;
4559 // Note: Since we don't rotate, it's possible for the touches to become
4560 // slightly detached from their original positions. Thus, we recompute the
4561 // touch points on touchend as well as touchstart!
4562 function d3_behavior_zoomTouchup() {
4563 var touches = d3.svg.touches(d3_behavior_zoomTarget),
4567 while (++i < n) d3_behavior_zoomLocations[(touch = touches[i]).identifier] = d3_behavior_zoomLocation(touch);
4571 function d3_behavior_zoomTouchmove() {
4572 var touches = d3.svg.touches(d3_behavior_zoomTarget);
4573 switch (touches.length) {
4577 var touch = touches[0];
4578 d3_behavior_zoomTo(d3_behavior_zoomXyz[2], touch, d3_behavior_zoomLocations[touch.identifier]);
4582 // double-touch pan + zoom
4584 var p0 = touches[0],
4586 p2 = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2],
4587 l0 = d3_behavior_zoomLocations[p0.identifier],
4588 l1 = d3_behavior_zoomLocations[p1.identifier],
4589 l2 = [(l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2, l0[2]];
4590 d3_behavior_zoomTo(Math.log(d3.event.scale) / Math.LN2 + l0[2], p2, l2);
4596 function d3_behavior_zoomMousemove() {
4597 d3_behavior_zoomZooming = null;
4598 if (d3_behavior_zoomPanning) {
4599 d3_behavior_zoomMoved = 1;
4600 d3_behavior_zoomTo(d3_behavior_zoomXyz[2], d3.svg.mouse(d3_behavior_zoomTarget), d3_behavior_zoomPanning);
4604 function d3_behavior_zoomMouseup() {
4605 if (d3_behavior_zoomPanning) {
4606 if (d3_behavior_zoomMoved) {
4608 d3_behavior_zoomMoved = d3_behavior_zoomEventTarget === d3.event.target;
4611 d3_behavior_zoomXyz =
4612 d3_behavior_zoomExtent =
4613 d3_behavior_zoomDispatch =
4614 d3_behavior_zoomEventTarget =
4615 d3_behavior_zoomTarget =
4616 d3_behavior_zoomArguments =
4617 d3_behavior_zoomPanning = null;
4621 function d3_behavior_zoomClick() {
4622 if (d3_behavior_zoomMoved) {
4624 d3_behavior_zoomMoved = 0;
4628 function d3_behavior_zoomTo(z, x0, x1) {
4629 z = d3_behavior_zoomExtentClamp(z, 2);
4630 var j = Math.pow(2, d3_behavior_zoomXyz[2]),
4632 K = Math.pow(2, (d3_behavior_zoomXyz[2] = z) - x1[2]),
4633 x_ = d3_behavior_zoomXyz[0],
4634 y_ = d3_behavior_zoomXyz[1],
4635 x = d3_behavior_zoomXyz[0] = d3_behavior_zoomExtentClamp((x0[0] - x1[0] * K), 0, k),
4636 y = d3_behavior_zoomXyz[1] = d3_behavior_zoomExtentClamp((x0[1] - x1[1] * K), 1, k),
4637 o = d3.event; // Events can be reentrant (e.g., focus).
4642 transform: function(sx, sy) {
4643 if (sx) transform(sx, x_, x);
4644 if (sy) transform(sy, y_, y);
4648 function transform(scale, a, b) {
4649 scale.domain(scale.range().map(function(v) { return scale.invert(((v - b) * j) / k + a); }));
4653 d3_behavior_zoomDispatch.apply(d3_behavior_zoomTarget, d3_behavior_zoomArguments);
4661 var d3_behavior_zoomInfiniteExtent = [
4662 [-Infinity, Infinity],
4663 [-Infinity, Infinity],
4664 [-Infinity, Infinity]
4667 function d3_behavior_zoomExtentClamp(x, i, k) {
4668 var range = d3_behavior_zoomExtent[i],
4671 return arguments.length === 3
4672 ? Math.max(r1 * (r1 === Infinity ? -Infinity : 1 / k - 1),
4673 Math.min(r0 === -Infinity ? Infinity : r0, x / k)) * k
4674 : Math.max(r0, Math.min(r1, x));