1 (function(){d3 = {version: "1.8.5"}; // semver
2 if (!Date.now) Date.now = function() {
5 if (!Object.create) Object.create = function(o) {
6 /** @constructor */ function f() {}
10 var d3_array = d3_arraySlice; // conversion for NodeLists
12 function d3_arrayCopy(psuedoarray) {
13 var i = -1, n = psuedoarray.length, array = [];
14 while (++i < n) array.push(psuedoarray[i]);
18 function d3_arraySlice(psuedoarray) {
19 return Array.prototype.slice.call(psuedoarray);
23 d3_array(document.documentElement.childNodes)[0].nodeType;
25 d3_array = d3_arrayCopy;
27 function d3_functor(v) {
28 return typeof v == "function" ? v : function() { return v; };
30 // A getter-setter method that preserves the appropriate `this` context.
31 d3.rebind = function(object, method) {
33 var x = method.apply(object, arguments);
34 return arguments.length ? object : x;
37 d3.ascending = function(a, b) {
38 return a < b ? -1 : a > b ? 1 : 0;
40 d3.descending = function(a, b) {
41 return b < a ? -1 : b > a ? 1 : 0;
43 d3.min = function(array, f) {
48 if (arguments.length == 1) {
49 while (++i < n) if (a > (b = array[i])) a = b;
52 while (++i < n) if (a > (b = f(array[i]))) a = b;
56 d3.max = function(array, f) {
61 if (arguments.length == 1) {
62 while (++i < n) if (a < (b = array[i])) a = b;
65 while (++i < n) if (a < (b = f(array[i]))) a = b;
69 d3.nest = function() {
76 function map(array, depth) {
77 if (depth >= keys.length) return rollup
78 ? rollup.call(nest, array) : (sortValues
79 ? array.sort(sortValues)
90 if ((keyValue = key(object = array[i])) in o) {
91 o[keyValue].push(object);
93 o[keyValue] = [object];
98 o[keyValue] = map(o[keyValue], depth);
104 function entries(map, depth) {
105 if (depth >= keys.length) return map;
108 sortKey = sortKeys[depth++],
112 a.push({key: key, values: entries(map[key], depth)});
115 if (sortKey) a.sort(function(a, b) {
116 return sortKey(a.key, b.key);
122 nest.map = function(array) {
123 return map(array, 0);
126 nest.entries = function(array) {
127 return entries(map(array, 0), 0);
130 nest.key = function(d) {
135 // Specifies the order for the most-recently specified key.
136 // Note: only applies to entries. Map keys are unordered!
137 nest.sortKeys = function(order) {
138 sortKeys[keys.length - 1] = order;
142 // Specifies the order for leaf values.
143 // Applies to both maps and entries array.
144 nest.sortValues = function(order) {
149 nest.rollup = function(f) {
156 d3.keys = function(map) {
158 for (var key in map) keys.push(key);
161 d3.values = function(map) {
163 for (var key in map) values.push(map[key]);
166 d3.entries = function(map) {
168 for (var key in map) entries.push({key: key, value: map[key]});
171 d3.merge = function(arrays) {
172 return Array.prototype.concat.apply([], arrays);
174 d3.split = function(array, f) {
180 if (arguments.length < 2) f = d3_splitter;
182 if (f.call(values, value = array[i], i)) {
185 if (!values.length) arrays.push(values);
192 function d3_splitter(d) {
195 function d3_collapse(s) {
196 return s.replace(/(^\s+)|(\s+$)/g, "").replace(/\s+/g, " ");
199 // Note: assigning to the arguments array simultaneously changes the value of
200 // the corresponding argument!
202 // TODO The `this` argument probably shouldn't be the first argument to the
203 // callback, anyway, since it's redundant. However, that will require a major
204 // version bump due to backwards compatibility, so I'm not changing it right
207 function d3_call(callback) {
208 callback.apply(this, (arguments[0] = this, arguments));
212 * @param {number} start
213 * @param {number=} stop
214 * @param {number=} step
216 d3.range = function(start, stop, step) {
217 if (arguments.length == 1) { stop = start; start = 0; }
218 if (step == null) step = 1;
219 if ((stop - start) / step == Infinity) throw new Error("infinite range");
223 if (step < 0) while ((j = start + step * ++i) > stop) range.push(j);
224 else while ((j = start + step * ++i) < stop) range.push(j);
227 d3.requote = function(s) {
228 return s.replace(d3_requote_re, "\\$&");
231 var d3_requote_re = /[\\\^\$\*\+\?\[\]\(\)\.\{\}]/g;
232 d3.xhr = function(url, mime, callback) {
233 var req = new XMLHttpRequest();
234 if (arguments.length < 3) callback = mime;
235 else if (mime && req.overrideMimeType) req.overrideMimeType(mime);
236 req.open("GET", url, true);
237 req.onreadystatechange = function() {
238 if (req.readyState == 4) callback(req.status < 300 ? req : null);
242 d3.text = function(url, mime, callback) {
243 function ready(req) {
244 callback(req && req.responseText);
246 if (arguments.length < 3) {
250 d3.xhr(url, mime, ready);
252 d3.json = function(url, callback) {
253 d3.text(url, "application/json", function(text) {
254 callback(text ? JSON.parse(text) : null);
257 d3.html = function(url, callback) {
258 d3.text(url, "text/html", function(text) {
259 if (text != null) { // Treat empty string as valid HTML.
260 var range = document.createRange();
261 range.selectNode(document.body);
262 text = range.createContextualFragment(text);
267 d3.xml = function(url, mime, callback) {
268 function ready(req) {
269 callback(req && req.responseXML);
271 if (arguments.length < 3) {
275 d3.xhr(url, mime, ready);
280 svg: "http://www.w3.org/2000/svg",
281 xhtml: "http://www.w3.org/1999/xhtml",
282 xlink: "http://www.w3.org/1999/xlink",
283 xml: "http://www.w3.org/XML/1998/namespace",
284 xmlns: "http://www.w3.org/2000/xmlns/"
287 qualify: function(name) {
288 var i = name.indexOf(":");
289 return i < 0 ? name : {
290 space: d3.ns.prefix[name.substring(0, i)],
291 local: name.substring(i + 1)
296 /** @param {...string} types */
297 d3.dispatch = function(types) {
300 for (var i = 0, n = arguments.length; i < n; i++) {
302 dispatch[type] = d3_dispatch(type);
307 function d3_dispatch(type) {
311 dispatch.add = function(listener) {
312 for (var i = 0; i < listeners.length; i++) {
313 if (listeners[i].listener == listener) return dispatch; // already registered
315 listeners.push({listener: listener, on: true});
319 dispatch.remove = function(listener) {
320 for (var i = 0; i < listeners.length; i++) {
321 var l = listeners[i];
322 if (l.listener == listener) {
324 listeners = listeners.slice(0, i).concat(listeners.slice(i + 1));
331 dispatch.dispatch = function() {
332 var ls = listeners; // defensive reference
333 for (var i = 0, n = ls.length; i < n; i++) {
335 if (l.on) l.listener.apply(this, arguments);
342 d3.format = function(specifier) {
343 var match = d3_format_re.exec(specifier),
344 fill = match[1] || " ",
345 sign = match[3] || "",
349 precision = match[8],
351 if (precision) precision = precision.substring(1);
353 fill = "0"; // TODO align = "=";
354 if (comma) width -= Math.floor((width - 1) / 4);
356 if (type == "d") precision = "0";
357 return function(value) {
359 negative = (number < 0) && (number = -number) ? "\u2212" : sign;
361 // Return the empty string for floats formatted as ints.
362 if ((type == "d") && (number % 1)) return "";
364 // Convert the input value to the desired precision.
365 if (precision) value = number.toFixed(precision);
366 else value = "" + number;
368 // If the fill character is 0, the sign and group is applied after the fill.
370 var length = value.length + negative.length;
371 if (length < width) value = new Array(width - length + 1).join(fill) + value;
372 if (comma) value = d3_format_group(value);
373 value = negative + value;
376 // Otherwise (e.g., space-filling), the sign and group is applied before.
378 if (comma) value = d3_format_group(value);
379 value = negative + value;
380 var length = value.length;
381 if (length < width) value = new Array(width - length + 1).join(fill) + value;
388 // [[fill]align][sign][#][0][width][,][.precision][type]
389 var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/;
391 // Apply comma grouping for thousands.
392 function d3_format_group(value) {
393 var i = value.lastIndexOf("."),
394 f = i >= 0 ? value.substring(i) : (i = value.length, ""),
396 while (i > 0) t.push(value.substring(i -= 3, i + 3));
397 return t.reverse().join(",") + f;
400 * TERMS OF USE - EASING EQUATIONS
402 * Open source under the BSD License.
404 * Copyright 2001 Robert Penner
405 * All rights reserved.
407 * Redistribution and use in source and binary forms, with or without
408 * modification, are permitted provided that the following conditions are met:
410 * - Redistributions of source code must retain the above copyright notice, this
411 * list of conditions and the following disclaimer.
413 * - Redistributions in binary form must reproduce the above copyright notice,
414 * this list of conditions and the following disclaimer in the documentation
415 * and/or other materials provided with the distribution.
417 * - Neither the name of the author nor the names of contributors may be used to
418 * endorse or promote products derived from this software without specific
419 * prior written permission.
421 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
422 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
423 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
424 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
425 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
426 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
427 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
428 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
429 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
430 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
431 * POSSIBILITY OF SUCH DAMAGE.
434 var d3_ease_quad = d3_ease_poly(2),
435 d3_ease_cubic = d3_ease_poly(3);
438 linear: function() { return d3_ease_linear; },
440 quad: function() { return d3_ease_quad; },
441 cubic: function() { return d3_ease_cubic; },
442 sin: function() { return d3_ease_sin; },
443 exp: function() { return d3_ease_exp; },
444 circle: function() { return d3_ease_circle; },
445 elastic: d3_ease_elastic,
447 bounce: function() { return d3_ease_bounce; }
451 "in": function(f) { return f; },
452 "out": d3_ease_reverse,
453 "in-out": d3_ease_reflect,
454 "out-in": function(f) { return d3_ease_reflect(d3_ease_reverse(f)); }
457 d3.ease = function(name) {
458 var i = name.indexOf("-"),
459 t = i >= 0 ? name.substring(0, i) : name,
460 m = i >= 0 ? name.substring(i + 1) : "in";
461 return d3_ease_mode[m](d3_ease[t].apply(null, Array.prototype.slice.call(arguments, 1)));
464 function d3_ease_reverse(f) {
470 function d3_ease_reflect(f) {
472 return .5 * (t < .5 ? f(2 * t) : (2 - f(2 - 2 * t)));
476 function d3_ease_linear(t) {
480 function d3_ease_poly(e) {
482 return Math.pow(t, e);
486 function d3_ease_sin(t) {
487 return 1 - Math.cos(t * Math.PI / 2);
490 function d3_ease_exp(t) {
491 return t ? Math.pow(2, 10 * (t - 1)) - 1e-3 : 0;
494 function d3_ease_circle(t) {
495 return 1 - Math.sqrt(1 - t * t);
498 function d3_ease_elastic(a, p) {
500 if (arguments.length < 2) p = 0.45;
501 if (arguments.length < 1) { a = 1; s = p / 4; }
502 else s = p / (2 * Math.PI) * Math.asin(1 / a);
504 return 1 + a * Math.pow(2, 10 * -t) * Math.sin((t - s) * 2 * Math.PI / p);
508 function d3_ease_back(s) {
511 return t * t * ((s + 1) * t - s);
515 function d3_ease_bounce(t) {
516 return t < 1 / 2.75 ? 7.5625 * t * t
517 : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75
518 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375
519 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375;
522 d3.interpolate = function(a, b) {
523 if (typeof b == "number") return d3.interpolateNumber(+a, b);
524 if (typeof b == "string") {
525 return (b in d3_rgb_names) || /^(#|rgb\(|hsl\()/.test(b)
526 ? d3.interpolateRgb(String(a), b)
527 : d3.interpolateString(String(a), b);
529 if (b instanceof Array) return d3.interpolateArray(a, b);
530 return d3.interpolateObject(a, b);
533 d3.interpolateNumber = function(a, b) {
535 return function(t) { return a + b * t; };
538 d3.interpolateRound = function(a, b) {
540 return function(t) { return Math.round(a + b * t); };
543 d3.interpolateString = function(a, b) {
544 var m, // current match
546 j, // current index (for coallescing)
547 s0 = 0, // start index of current string prefix
548 s1 = 0, // end index of current string prefix
549 s = [], // string constants and placeholders
550 q = [], // number interpolators
554 // Find all numbers in b.
555 for (i = 0; m = d3_interpolate_number.exec(b); ++i) {
556 if (m.index) s.push(b.substring(s0, s1 = m.index));
557 q.push({i: s.length, x: m[0]});
559 s0 = d3_interpolate_number.lastIndex;
561 if (s0 < b.length) s.push(b.substring(s0));
563 // Find all numbers in a.
564 for (i = 0, n = q.length; (m = d3_interpolate_number.exec(a)) && i < n; ++i) {
566 if (o.x == m[0]) { // The numbers match, so coallesce.
568 if (s[o.i + 1] == null) { // This match is followed by another number.
571 for (j = i + 1; j < n; ++j) q[j].i--;
572 } else { // This match is followed by a string, so coallesce twice.
573 s[o.i - 1] += o.x + s[o.i + 1];
575 for (j = i + 1; j < n; ++j) q[j].i -= 2;
578 if (s[o.i + 1] == null) { // This match is followed by another number.
580 } else { // This match is followed by a string, so coallesce twice.
581 s[o.i] = o.x + s[o.i + 1];
582 s.splice(o.i + 1, 1);
583 for (j = i + 1; j < n; ++j) q[j].i--;
590 o.x = d3.interpolateNumber(parseFloat(m[0]), parseFloat(o.x));
594 // Remove any numbers in b not found in a.
597 if (s[o.i + 1] == null) { // This match is followed by another number.
599 } else { // This match is followed by a string, so coallesce twice.
600 s[o.i] = o.x + s[o.i + 1];
601 s.splice(o.i + 1, 1);
606 // Special optimization for only a single match.
608 return s[0] == null ? q[0].x : function() { return b; };
611 // Otherwise, interpolate each of the numbers and rejoin the string.
613 for (i = 0; i < n; ++i) s[(o = q[i]).i] = o.x(t);
618 d3.interpolateRgb = function(a, b) {
628 return "rgb(" + Math.round(ar + br * t)
629 + "," + Math.round(ag + bg * t)
630 + "," + Math.round(ab + bb * t)
635 // interpolates HSL space, but outputs RGB string (for compatibility)
636 d3.interpolateHsl = function(a, b) {
646 return d3_hsl_rgb(h0 + h1 * t, s0 + s1 * t, l0 + l1 * t).toString();
650 d3.interpolateArray = function(a, b) {
655 n0 = Math.min(a.length, b.length),
657 for (i = 0; i < n0; ++i) x.push(d3.interpolate(a[i], b[i]));
658 for (; i < na; ++i) c[i] = a[i];
659 for (; i < nb; ++i) c[i] = b[i];
661 for (i = 0; i < n0; ++i) c[i] = x[i](t);
666 d3.interpolateObject = function(a, b) {
672 i[k] = d3_interpolateByName(k)(a[k], b[k]);
683 for (k in i) c[k] = i[k](t);
688 var d3_interpolate_number = /[-+]?(?:\d+\.\d+|\d+\.|\.\d+|\d+)(?:[eE][-]?\d+)?/g,
689 d3_interpolate_digits = /[-+]?\d*\.?\d*(?:[eE][-]?\d+)?(.*)/,
690 d3_interpolate_rgb = {background: 1, fill: 1, stroke: 1};
692 function d3_interpolateByName(n) {
693 return n in d3_interpolate_rgb || /\bcolor\b/.test(n)
701 d3.rgb = function(r, g, b) {
702 return arguments.length == 1
703 ? d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb)
704 : d3_rgb(~~r, ~~g, ~~b);
707 function d3_rgb(r, g, b) {
708 return {r: r, g: g, b: b, toString: d3_rgb_format};
712 function d3_rgb_format() {
713 return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b);
716 function d3_rgb_hex(v) {
717 return v < 0x10 ? "0" + v.toString(16) : v.toString(16);
720 function d3_rgb_parse(format, rgb, hsl) {
721 var r = 0, // red channel; int in [0, 255]
722 g = 0, // green channel; int in [0, 255]
723 b = 0, // blue channel; int in [0, 255]
724 m1, // CSS color specification match
725 m2, // CSS color specification type (e.g., rgb)
728 /* Handle hsl, rgb. */
729 m1 = /([a-z]+)\((.*)\)/i.exec(format);
731 m2 = m1[2].split(",");
735 parseFloat(m2[0]), // degrees
736 parseFloat(m2[1]) / 100, // percentage
737 parseFloat(m2[2]) / 100 // percentage
742 d3_rgb_parseNumber(m2[0]),
743 d3_rgb_parseNumber(m2[1]),
744 d3_rgb_parseNumber(m2[2])
751 if (name = d3_rgb_names[format]) return rgb(name.r, name.g, name.b);
753 /* Hexadecimal colors: #rgb and #rrggbb. */
754 if (format != null && format.charAt(0) == "#") {
755 if (format.length == 4) {
756 r = format.charAt(1); r += r;
757 g = format.charAt(2); g += g;
758 b = format.charAt(3); b += b;
759 } else if (format.length == 7) {
760 r = format.substring(1, 3);
761 g = format.substring(3, 5);
762 b = format.substring(5, 7);
772 function d3_rgb_hsl(r, g, b) {
773 var min = Math.min(r /= 255, g /= 255, b /= 255),
774 max = Math.max(r, g, b),
780 s = l < .5 ? d / (max + min) : d / (2 - max - min);
781 if (r == max) h = (g - b) / d + (g < b ? 6 : 0);
782 else if (g == max) h = (b - r) / d + 2;
783 else h = (r - g) / d + 4;
788 return d3_hsl(h, s, l);
791 function d3_rgb_parseNumber(c) { // either integer or percentage
792 var f = parseFloat(c);
793 return c.charAt(c.length - 1) == "%" ? Math.round(f * 2.55) : f;
797 aliceblue: "#f0f8ff",
798 antiquewhite: "#faebd7",
800 aquamarine: "#7fffd4",
805 blanchedalmond: "#ffebcd",
807 blueviolet: "#8a2be2",
809 burlywood: "#deb887",
810 cadetblue: "#5f9ea0",
811 chartreuse: "#7fff00",
812 chocolate: "#d2691e",
814 cornflowerblue: "#6495ed",
820 darkgoldenrod: "#b8860b",
822 darkgreen: "#006400",
824 darkkhaki: "#bdb76b",
825 darkmagenta: "#8b008b",
826 darkolivegreen: "#556b2f",
827 darkorange: "#ff8c00",
828 darkorchid: "#9932cc",
830 darksalmon: "#e9967a",
831 darkseagreen: "#8fbc8f",
832 darkslateblue: "#483d8b",
833 darkslategray: "#2f4f4f",
834 darkslategrey: "#2f4f4f",
835 darkturquoise: "#00ced1",
836 darkviolet: "#9400d3",
838 deepskyblue: "#00bfff",
841 dodgerblue: "#1e90ff",
842 firebrick: "#b22222",
843 floralwhite: "#fffaf0",
844 forestgreen: "#228b22",
846 gainsboro: "#dcdcdc",
847 ghostwhite: "#f8f8ff",
849 goldenrod: "#daa520",
852 greenyellow: "#adff2f",
856 indianred: "#cd5c5c",
861 lavenderblush: "#fff0f5",
862 lawngreen: "#7cfc00",
863 lemonchiffon: "#fffacd",
864 lightblue: "#add8e6",
865 lightcoral: "#f08080",
866 lightcyan: "#e0ffff",
867 lightgoldenrodyellow: "#fafad2",
868 lightgray: "#d3d3d3",
869 lightgreen: "#90ee90",
870 lightgrey: "#d3d3d3",
871 lightpink: "#ffb6c1",
872 lightsalmon: "#ffa07a",
873 lightseagreen: "#20b2aa",
874 lightskyblue: "#87cefa",
875 lightslategray: "#778899",
876 lightslategrey: "#778899",
877 lightsteelblue: "#b0c4de",
878 lightyellow: "#ffffe0",
880 limegreen: "#32cd32",
884 mediumaquamarine: "#66cdaa",
885 mediumblue: "#0000cd",
886 mediumorchid: "#ba55d3",
887 mediumpurple: "#9370db",
888 mediumseagreen: "#3cb371",
889 mediumslateblue: "#7b68ee",
890 mediumspringgreen: "#00fa9a",
891 mediumturquoise: "#48d1cc",
892 mediumvioletred: "#c71585",
893 midnightblue: "#191970",
894 mintcream: "#f5fffa",
895 mistyrose: "#ffe4e1",
897 navajowhite: "#ffdead",
901 olivedrab: "#6b8e23",
903 orangered: "#ff4500",
905 palegoldenrod: "#eee8aa",
906 palegreen: "#98fb98",
907 paleturquoise: "#afeeee",
908 palevioletred: "#db7093",
909 papayawhip: "#ffefd5",
910 peachpuff: "#ffdab9",
914 powderblue: "#b0e0e6",
917 rosybrown: "#bc8f8f",
918 royalblue: "#4169e1",
919 saddlebrown: "#8b4513",
921 sandybrown: "#f4a460",
927 slateblue: "#6a5acd",
928 slategray: "#708090",
929 slategrey: "#708090",
931 springgreen: "#00ff7f",
932 steelblue: "#4682b4",
937 turquoise: "#40e0d0",
941 whitesmoke: "#f5f5f5",
943 yellowgreen: "#9acd32"
946 for (var d3_rgb_name in d3_rgb_names) {
947 d3_rgb_names[d3_rgb_name] = d3_rgb_parse(
948 d3_rgb_names[d3_rgb_name],
956 d3.hsl = function(h, s, l) {
957 return arguments.length == 1
958 ? d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl)
959 : d3_hsl(+h, +s, +l);
962 function d3_hsl(h, s, l) {
963 return {h: h, s: s, l: l, toString: d3_hsl_format};
967 function d3_hsl_format() {
968 return "hsl(" + this.h + "," + this.s * 100 + "%," + this.l * 100 + "%)";
971 function d3_hsl_rgb(h, s, l) {
975 /* Some simple corrections for h, s and l. */
976 h = h % 360; if (h < 0) h += 360;
977 s = s < 0 ? 0 : s > 1 ? 1 : s;
978 l = l < 0 ? 0 : l > 1 ? 1 : l;
980 /* From FvD 13.37, CSS Color Module Level 3 */
981 m2 = l <= .5 ? l * (1 + s) : l + s - l * s;
985 if (h > 360) h -= 360;
986 else if (h < 0) h += 360;
987 if (h < 60) return m1 + (m2 - m1) * h / 60;
988 if (h < 180) return m2;
989 if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60;
994 return Math.round(v(h) * 255);
997 return d3_rgb(vv(h + 120), vv(h), vv(h - 120));
999 var d3_select = function(s, n) { return n.querySelector(s); },
1000 d3_selectAll = function(s, n) { return d3_array(n.querySelectorAll(s)); };
1002 // Use Sizzle, if available.
1003 if (typeof Sizzle == "function") {
1004 d3_select = function(s, n) { return Sizzle(s, n)[0]; };
1005 d3_selectAll = function(s, n) { return Sizzle.uniqueSort(Sizzle(s, n)); };
1008 var d3_root = d3_selection([[document]]);
1009 d3_root[0].parentNode = document.documentElement;
1011 // TODO fast singleton implementation!
1012 d3.select = function(selector) {
1013 return typeof selector == "string"
1014 ? d3_root.select(selector)
1015 : d3_selection([[selector]]); // assume node
1018 d3.selectAll = function(selector) {
1019 return typeof selector == "string"
1020 ? d3_root.selectAll(selector)
1021 : d3_selection([d3_array(selector)]); // assume node[]
1024 function d3_selection(groups) {
1026 function select(select) {
1032 for (var j = 0, m = groups.length; j < m; j++) {
1034 subgroups.push(subgroup = []);
1035 subgroup.parentNode = group.parentNode;
1036 subgroup.parentData = group.parentData;
1037 for (var i = 0, n = group.length; i < n; i++) {
1038 if (node = group[i]) {
1039 subgroup.push(subnode = select(node));
1040 if (subnode && "__data__" in node) subnode.__data__ = node.__data__;
1042 subgroup.push(null);
1046 return d3_selection(subgroups);
1049 function selectAll(selectAll) {
1054 for (var j = 0, m = groups.length; j < m; j++) {
1056 for (var i = 0, n = group.length; i < n; i++) {
1057 if (node = group[i]) {
1058 subgroups.push(subgroup = selectAll(node));
1059 subgroup.parentNode = node;
1060 subgroup.parentData = node.__data__;
1064 return d3_selection(subgroups);
1067 // TODO select(function)?
1068 groups.select = function(selector) {
1069 return select(function(node) {
1070 return d3_select(selector, node);
1074 // TODO selectAll(function)?
1075 groups.selectAll = function(selector) {
1076 return selectAll(function(node) {
1077 return d3_selectAll(selector, node);
1081 // TODO preserve null elements to maintain index?
1082 groups.filter = function(filter) {
1087 for (var j = 0, m = groups.length; j < m; j++) {
1089 subgroups.push(subgroup = []);
1090 subgroup.parentNode = group.parentNode;
1091 subgroup.parentData = group.parentData;
1092 for (var i = 0, n = group.length; i < n; i++) {
1093 if ((node = group[i]) && filter.call(node, node.__data__, i)) {
1094 subgroup.push(node);
1098 return d3_selection(subgroups);
1101 groups.map = function(map) {
1104 for (var j = 0, m = groups.length; j < m; j++) {
1106 for (var i = 0, n = group.length; i < n; i++) {
1107 if (node = group[i]) node.__data__ = map.call(node, node.__data__, i);
1113 // TODO data(null) for clearing data?
1114 groups.data = function(data, join) {
1119 function bind(group, groupData) {
1122 m = groupData.length,
1123 n0 = Math.min(n, m),
1124 n1 = Math.max(n, m),
1131 function enterNode(data) {
1132 return {__data__: data};
1140 j = groupData.length;
1142 for (i = 0; i < n; i++) {
1143 key = join.call(node = group[i], node.__data__, i);
1144 if (key in nodeByKey) {
1145 exitNodes[j++] = group[i];
1147 nodeByKey[key] = node;
1152 for (i = 0; i < m; i++) {
1153 node = nodeByKey[key = join.call(groupData, nodeData = groupData[i], i)];
1155 node.__data__ = nodeData;
1156 updateNodes[i] = node;
1157 enterNodes[i] = exitNodes[i] = null;
1159 enterNodes[i] = enterNode(nodeData),
1160 updateNodes[i] = exitNodes[i] = null;
1162 delete nodeByKey[key];
1165 for (i = 0; i < n; i++) {
1166 if (keys[i] in nodeByKey) {
1167 exitNodes[i] = group[i];
1171 for (; i < n0; i++) {
1173 nodeData = groupData[i];
1175 node.__data__ = nodeData;
1176 updateNodes[i] = node;
1177 enterNodes[i] = exitNodes[i] = null;
1179 enterNodes[i] = enterNode(nodeData);
1180 updateNodes[i] = exitNodes[i] = null;
1183 for (; i < m; i++) {
1184 enterNodes[i] = enterNode(groupData[i]);
1185 updateNodes[i] = exitNodes[i] = null;
1187 for (; i < n1; i++) {
1188 exitNodes[i] = group[i];
1189 enterNodes[i] = updateNodes[i] = null;
1193 enterNodes.parentNode
1194 = updateNodes.parentNode
1195 = exitNodes.parentNode
1198 enterNodes.parentData
1199 = updateNodes.parentData
1200 = exitNodes.parentData
1203 enter.push(enterNodes);
1204 update.push(updateNodes);
1205 exit.push(exitNodes);
1211 if (typeof data == "function") {
1213 bind(group = groups[i], data.call(group, group.parentData, i));
1217 bind(group = groups[i], data);
1221 var selection = d3_selection(update);
1222 selection.enter = function() {
1223 return d3_selectionEnter(enter);
1225 selection.exit = function() {
1226 return d3_selection(exit);
1231 // TODO mask forEach? or rename for eachData?
1232 // TODO offer the same semantics for map, reduce, etc.?
1233 groups.each = function(callback) {
1234 for (var j = 0, m = groups.length; j < m; j++) {
1235 var group = groups[j];
1236 for (var i = 0, n = group.length; i < n; i++) {
1237 var node = group[i];
1238 if (node) callback.call(node, node.__data__, i);
1244 function first(callback) {
1245 for (var j = 0, m = groups.length; j < m; j++) {
1246 var group = groups[j];
1247 for (var i = 0, n = group.length; i < n; i++) {
1248 var node = group[i];
1249 if (node) return callback.call(node, node.__data__, i);
1255 groups.empty = function() {
1256 return !first(function() { return true; });
1259 groups.node = function() {
1260 return first(function() { return this; });
1263 groups.attr = function(name, value) {
1264 name = d3.ns.qualify(name);
1266 // If no value is specified, return the first value.
1267 if (arguments.length < 2) {
1268 return first(name.local
1269 ? function() { return this.getAttributeNS(name.space, name.local); }
1270 : function() { return this.getAttribute(name); });
1273 /** @this {Element} */
1274 function attrNull() {
1275 this.removeAttribute(name);
1278 /** @this {Element} */
1279 function attrNullNS() {
1280 this.removeAttributeNS(name.space, name.local);
1283 /** @this {Element} */
1284 function attrConstant() {
1285 this.setAttribute(name, value);
1288 /** @this {Element} */
1289 function attrConstantNS() {
1290 this.setAttributeNS(name.space, name.local, value);
1293 /** @this {Element} */
1294 function attrFunction() {
1295 var x = value.apply(this, arguments);
1296 if (x == null) this.removeAttribute(name);
1297 else this.setAttribute(name, x);
1300 /** @this {Element} */
1301 function attrFunctionNS() {
1302 var x = value.apply(this, arguments);
1303 if (x == null) this.removeAttributeNS(name.space, name.local);
1304 else this.setAttributeNS(name.space, name.local, x);
1307 return groups.each(value == null
1308 ? (name.local ? attrNullNS : attrNull) : (typeof value == "function"
1309 ? (name.local ? attrFunctionNS : attrFunction)
1310 : (name.local ? attrConstantNS : attrConstant)));
1313 groups.classed = function(name, value) {
1314 var re = new RegExp("(^|\\s+)" + d3.requote(name) + "(\\s+|$)", "g");
1316 // If no value is specified, return the first value.
1317 if (arguments.length < 2) {
1318 return first(function() {
1320 return re.test(this.className);
1324 /** @this {Element} */
1325 function classedAdd() {
1326 var classes = this.className;
1328 if (!re.test(classes)) {
1329 this.className = d3_collapse(classes + " " + name);
1333 /** @this {Element} */
1334 function classedRemove() {
1335 var classes = d3_collapse(this.className.replace(re, " "));
1336 this.className = classes.length ? classes : null;
1339 /** @this {Element} */
1340 function classedFunction() {
1341 (value.apply(this, arguments)
1343 : classedRemove).call(this);
1346 return groups.each(typeof value == "function"
1347 ? classedFunction : value
1352 groups.style = function(name, value, priority) {
1353 if (arguments.length < 3) priority = null;
1355 // If no value is specified, return the first value.
1356 if (arguments.length < 2) {
1357 return first(function() {
1358 return window.getComputedStyle(this, null).getPropertyValue(name);
1362 /** @this {Element} */
1363 function styleNull() {
1364 this.style.removeProperty(name);
1367 /** @this {Element} */
1368 function styleConstant() {
1369 this.style.setProperty(name, value, priority);
1372 /** @this {Element} */
1373 function styleFunction() {
1374 var x = value.apply(this, arguments);
1375 if (x == null) this.style.removeProperty(name);
1376 else this.style.setProperty(name, x, priority);
1379 return groups.each(value == null
1380 ? styleNull : (typeof value == "function"
1381 ? styleFunction : styleConstant));
1384 groups.property = function(name, value) {
1385 name = d3.ns.qualify(name);
1387 // If no value is specified, return the first value.
1388 if (arguments.length < 2) {
1389 return first(function() {
1394 /** @this {Element} */
1395 function propertyNull() {
1399 /** @this {Element} */
1400 function propertyConstant() {
1404 /** @this {Element} */
1405 function propertyFunction() {
1406 var x = value.apply(this, arguments);
1407 if (x == null) delete this[name];
1408 else this[name] = x;
1411 return groups.each(value == null
1412 ? propertyNull : (typeof value == "function"
1413 ? propertyFunction : propertyConstant));
1416 groups.text = function(value) {
1418 // If no value is specified, return the first value.
1419 if (arguments.length < 1) {
1420 return first(function() {
1421 return this.textContent;
1425 /** @this {Element} */
1426 function textNull() {
1427 while (this.lastChild) this.removeChild(this.lastChild);
1430 /** @this {Element} */
1431 function textConstant() {
1432 this.appendChild(document.createTextNode(value));
1435 /** @this {Element} */
1436 function textFunction() {
1437 var x = value.apply(this, arguments);
1438 if (x != null) this.appendChild(document.createTextNode(x));
1441 groups.each(textNull);
1442 return value == null ? groups
1443 : groups.each(typeof value == "function"
1444 ? textFunction : textConstant);
1447 groups.html = function(value) {
1449 // If no value is specified, return the first value.
1450 if (arguments.length < 1) {
1451 return first(function() {
1452 return this.innerHTML;
1456 /** @this {Element} */
1457 function htmlConstant() {
1458 this.innerHTML = value;
1461 /** @this {Element} */
1462 function htmlFunction() {
1463 this.innerHTML = value.apply(this, arguments);
1466 return groups.each(typeof value == "function"
1467 ? htmlFunction : htmlConstant);
1470 // TODO append(node)?
1471 // TODO append(function)?
1472 groups.append = function(name) {
1473 name = d3.ns.qualify(name);
1475 function append(node) {
1476 return node.appendChild(document.createElement(name));
1479 function appendNS(node) {
1480 return node.appendChild(document.createElementNS(name.space, name.local));
1483 return select(name.local ? appendNS : append);
1486 // TODO insert(node, function)?
1487 // TODO insert(function, string)?
1488 // TODO insert(function, function)?
1489 groups.insert = function(name, before) {
1490 name = d3.ns.qualify(name);
1492 function insert(node) {
1493 return node.insertBefore(
1494 document.createElement(name),
1495 d3_select(before, node));
1498 function insertNS(node) {
1499 return node.insertBefore(
1500 document.createElementNS(name.space, name.local),
1501 d3_select(before, node));
1504 return select(name.local ? insertNS : insert);
1507 // TODO remove(selector)?
1508 // TODO remove(node)?
1509 // TODO remove(function)?
1510 groups.remove = function() {
1511 return select(function(node) {
1512 var parent = node.parentNode;
1513 parent.removeChild(node);
1518 groups.sort = function(comparator) {
1519 comparator = d3_selection_comparator.apply(this, arguments);
1520 for (var j = 0, m = groups.length; j < m; j++) {
1521 var group = groups[j];
1522 group.sort(comparator);
1523 for (var i = 1, n = group.length, prev = group[0]; i < n; i++) {
1524 var node = group[i];
1526 if (prev) prev.parentNode.insertBefore(node, prev.nextSibling);
1534 // type can be namespaced, e.g., "click.foo"
1535 // listener can be null for removal
1536 groups.on = function(type, listener) {
1538 // parse the type specifier
1539 var i = type.indexOf("."),
1540 typo = i == -1 ? type : type.substring(0, i),
1541 name = "__on" + type;
1543 // remove the old event listener, and add the new event listener
1544 return groups.each(function(d, i) {
1545 if (this[name]) this.removeEventListener(typo, this[name], false);
1546 if (listener) this.addEventListener(typo, this[name] = l, false);
1548 // wrapped event listener that preserves d, i
1550 var o = d3.event; // Events can be reentrant (e.g., focus).
1553 listener.call(this, d, i);
1563 groups.transition = function() {
1564 return d3_transition(groups);
1567 groups.call = d3_call;
1572 function d3_selectionEnter(groups) {
1574 function select(select) {
1580 for (var j = 0, m = groups.length; j < m; j++) {
1582 subgroups.push(subgroup = []);
1583 subgroup.parentNode = group.parentNode;
1584 subgroup.parentData = group.parentData;
1585 for (var i = 0, n = group.length; i < n; i++) {
1586 if (node = group[i]) {
1587 subgroup.push(subnode = select(group.parentNode));
1588 subnode.__data__ = node.__data__;
1590 subgroup.push(null);
1594 return d3_selection(subgroups);
1597 // TODO append(node)?
1598 // TODO append(function)?
1599 groups.append = function(name) {
1600 name = d3.ns.qualify(name);
1602 function append(node) {
1603 return node.appendChild(document.createElement(name));
1606 function appendNS(node) {
1607 return node.appendChild(document.createElementNS(name.space, name.local));
1610 return select(name.local ? appendNS : append);
1613 // TODO insert(node, function)?
1614 // TODO insert(function, string)?
1615 // TODO insert(function, function)?
1616 groups.insert = function(name, before) {
1617 name = d3.ns.qualify(name);
1619 function insert(node) {
1620 return node.insertBefore(
1621 document.createElement(name),
1622 d3_select(before, node));
1625 function insertNS(node) {
1626 return node.insertBefore(
1627 document.createElementNS(name.space, name.local),
1628 d3_select(before, node));
1631 return select(name.local ? insertNS : insert);
1637 function d3_selection_comparator(comparator) {
1638 if (!arguments.length) comparator = d3.ascending;
1639 return function(a, b) {
1640 return comparator(a && a.__data__, b && b.__data__);
1643 d3.transition = d3_root.transition;
1645 var d3_transitionId = 0,
1646 d3_transitionInheritId = 0;
1648 function d3_transition(groups) {
1649 var transition = {},
1650 transitionId = d3_transitionInheritId || ++d3_transitionId,
1654 event = d3.dispatch("start", "end"),
1659 ease = d3.ease("cubic-in-out");
1662 // Be careful with concurrent transitions!
1664 // Say transition A causes an exit. Before A finishes, a transition B is
1665 // created, and believes it only needs to do an update, because the elements
1666 // haven't been removed yet (which happens at the very end of the exit
1669 // Even worse, what if either transition A or B has a staggered delay? Then,
1670 // some elements may be removed, while others remain. Transition B does not
1671 // know to enter the elements because they were still present at the time
1672 // the transition B was created (but not yet started).
1674 // To prevent such confusion, we only trigger end events for transitions if
1675 // the transition ending is the only one scheduled for the given element.
1676 // Similarly, we only allow one transition to be active for any given
1677 // element, so that concurrent transitions do not overwrite each other's
1680 // TODO Support transition namespaces, so that transitions can proceed
1681 // concurrently on the same element if needed. Hopefully, this is rare!
1684 groups.each(function() {
1685 (this.__transition__ || (this.__transition__ = {})).owner = transitionId;
1688 function step(elapsed) {
1691 groups.each(function() {
1692 if (stage[++k] == 2) return; // ended
1693 var t = (elapsed - delay[k]) / duration[k],
1694 tx = this.__transition__,
1697 ik = interpolators[k];
1699 // Check if the (un-eased) time is outside the range [0,1].
1707 // Determine the stage of this transition.
1708 // 0 - Not yet started.
1712 if (!tx || tx.active != transitionId) {
1716 } else if (!tx || tx.active > transitionId) {
1721 event.start.dispatch.apply(this, arguments);
1722 ik = interpolators[k] = {};
1723 tx.active = transitionId;
1724 for (tk in tweens) ik[tk] = tweens[tk].apply(this, arguments);
1727 // Apply the interpolators!
1729 for (tk in tweens) ik[tk].call(this, te);
1731 // Handle ending transitions.
1734 if (tx.active == transitionId) {
1735 var owner = tx.owner;
1736 if (owner == transitionId) {
1737 delete this.__transition__;
1738 if (remove) this.parentNode.removeChild(this);
1740 d3_transitionInheritId = transitionId;
1741 event.end.dispatch.apply(this, arguments);
1742 d3_transitionInheritId = 0;
1750 transition.delay = function(value) {
1751 var delayMin = Infinity,
1753 if (typeof value == "function") {
1754 groups.each(function(d, i) {
1755 var x = delay[++k] = +value.apply(this, arguments);
1756 if (x < delayMin) delayMin = x;
1760 groups.each(function(d, i) {
1761 delay[++k] = delayMin;
1764 d3_timer(step, delayMin);
1768 transition.duration = function(value) {
1770 if (typeof value == "function") {
1772 groups.each(function(d, i) {
1773 var x = duration[++k] = +value.apply(this, arguments);
1774 if (x > durationMax) durationMax = x;
1777 durationMax = +value;
1778 groups.each(function(d, i) {
1779 duration[++k] = durationMax;
1785 transition.ease = function(value) {
1786 ease = typeof value == "function" ? value : d3.ease.apply(d3, arguments);
1790 transition.attrTween = function(name, tween) {
1792 /** @this {Element} */
1793 function attrTween(d, i) {
1794 var f = tween.call(this, d, i, this.getAttribute(name));
1795 return function(t) {
1796 this.setAttribute(name, f(t));
1800 /** @this {Element} */
1801 function attrTweenNS(d, i) {
1802 var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local));
1803 return function(t) {
1804 this.setAttributeNS(name.space, name.local, f(t));
1808 tweens["attr." + name] = name.local ? attrTweenNS : attrTween;
1812 transition.attr = function(name, value) {
1813 return transition.attrTween(name, d3_transitionTween(value));
1816 transition.styleTween = function(name, tween, priority) {
1817 if (arguments.length < 3) priority = null;
1819 /** @this {Element} */
1820 function styleTween(d, i) {
1821 var f = tween.call(this, d, i, window.getComputedStyle(this, null).getPropertyValue(name));
1822 return function(t) {
1823 this.style.setProperty(name, f(t), priority);
1827 tweens["style." + name] = styleTween;
1831 transition.style = function(name, value, priority) {
1832 if (arguments.length < 3) priority = null;
1833 return transition.styleTween(name, d3_transitionTween(value), priority);
1836 transition.select = function(query) {
1837 var k, t = d3_transition(groups.select(query)).ease(ease);
1838 k = -1; t.delay(function(d, i) { return delay[++k]; });
1839 k = -1; t.duration(function(d, i) { return duration[++k]; });
1843 transition.selectAll = function(query) {
1844 var k, t = d3_transition(groups.selectAll(query)).ease(ease);
1845 k = -1; t.delay(function(d, i) { return delay[i ? k : ++k]; })
1846 k = -1; t.duration(function(d, i) { return duration[i ? k : ++k]; });
1850 transition.remove = function() {
1855 transition.each = function(type, listener) {
1856 event[type].add(listener);
1860 transition.call = d3_call;
1862 return transition.delay(0).duration(250);
1865 function d3_transitionTween(b) {
1866 return typeof b == "function"
1867 ? function(d, i, a) { return d3.interpolate(a, String(b.call(this, d, i))); }
1868 : (b = String(b), function(d, i, a) { return d3.interpolate(a, b); });
1870 var d3_timer_queue = null,
1871 d3_timer_timeout = 0,
1874 // The timer will continue to fire until callback returns true.
1875 d3.timer = function(callback) {
1876 d3_timer(callback, 0);
1879 function d3_timer(callback, delay) {
1880 var now = Date.now(),
1882 start = now + delay,
1884 t1 = d3_timer_queue;
1886 if (!isFinite(delay)) return;
1888 // Scan the queue for earliest callback.
1890 if (t1.callback == callback) {
1895 var x = t1.then + t1.delay;
1896 if (x < start) start = x;
1902 // Otherwise, add the callback to the queue.
1903 if (!found) d3_timer_queue = {
1907 next: d3_timer_queue
1910 if (!d3_timer_interval) {
1911 clearTimeout(d3_timer_timeout);
1912 d3_timer_timeout = setTimeout(d3_timer_start, Math.max(24, start - now));
1916 function d3_timer_start() {
1917 d3_timer_interval = 1;
1918 d3_timer_timeout = 0;
1919 d3_timer_frame(d3_timer_step);
1922 function d3_timer_step() {
1926 t1 = d3_timer_queue;
1928 elapsed = now - t1.then;
1929 if (elapsed > t1.delay) t1.flush = t1.callback(elapsed);
1930 t1 = (t0 = t1).next;
1933 if (d3_timer_interval) d3_timer_frame(d3_timer_step);
1936 // Flush after callbacks, to avoid concurrent queue modification.
1937 function d3_timer_flush() {
1939 t1 = d3_timer_queue;
1942 ? (t0 ? t0.next = t1.next : d3_timer_queue = t1.next)
1945 if (!t0) d3_timer_interval = 0;
1948 var d3_timer_frame = window.requestAnimationFrame
1949 || window.webkitRequestAnimationFrame
1950 || window.mozRequestAnimationFrame
1951 || window.oRequestAnimationFrame
1952 || window.msRequestAnimationFrame
1953 || function(callback) { setTimeout(callback, 17); };
1955 d3.scale.linear = function() {
1960 kx = 1, // 1 / (x1 - x0)
1961 ky = 1, // (x1 - x0) / (y1 - y0)
1962 interpolate = d3.interpolate,
1963 i = interpolate(y0, y1);
1966 return i((x - x0) * kx);
1969 // Note: requires range is coercible to number!
1970 scale.invert = function(y) {
1971 return (y - y0) * ky + x0;
1974 scale.domain = function(x) {
1975 if (!arguments.length) return [x0, x1];
1979 ky = (x1 - x0) / (y1 - y0);
1983 scale.range = function(x) {
1984 if (!arguments.length) return [y0, y1];
1987 ky = (x1 - x0) / (y1 - y0);
1988 i = interpolate(y0, y1);
1992 scale.rangeRound = function(x) {
1993 return scale.range(x).interpolate(d3.interpolateRound);
1996 scale.interpolate = function(x) {
1997 if (!arguments.length) return interpolate;
1998 i = (interpolate = x)(y0, y1);
2003 function tickRange(m) {
2004 var start = Math.min(x0, x1),
2005 stop = Math.max(x0, x1),
2006 span = stop - start,
2007 step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)),
2008 err = m / (span / step);
2010 // Filter ticks to get closer to the desired count.
2011 if (err <= .15) step *= 10;
2012 else if (err <= .35) step *= 5;
2013 else if (err <= .75) step *= 2;
2015 // Round start and stop values to step interval.
2017 start: Math.ceil(start / step) * step,
2018 stop: Math.floor(stop / step) * step + step * .5, // inclusive
2023 scale.ticks = function(m) {
2024 var range = tickRange(m);
2025 return d3.range(range.start, range.stop, range.step);
2028 scale.tickFormat = function(m) {
2029 var n = Math.max(0, -Math.floor(Math.log(tickRange(m).step) / Math.LN10 + .01));
2030 return d3.format(",." + n + "f");
2035 d3.scale.log = function() {
2036 var linear = d3.scale.linear(),
2041 return linear(log(x));
2044 scale.invert = function(x) {
2045 return pow(linear.invert(x));
2048 scale.domain = function(x) {
2049 if (!arguments.length) return linear.domain().map(pow);
2050 log = (x[0] || x[1]) < 0 ? d3_scale_logn : d3_scale_log;
2052 linear.domain(x.map(log));
2056 scale.range = d3.rebind(scale, linear.range);
2057 scale.rangeRound = d3.rebind(scale, linear.rangeRound);
2058 scale.interpolate = d3.rebind(scale, linear.interpolate);
2060 scale.ticks = function() {
2061 var d = linear.domain(),
2063 if (d.every(isFinite)) {
2064 var i = Math.floor(d[0]),
2065 j = Math.ceil(d[1]),
2068 if (log === d3_scale_logn) {
2070 for (; i++ < j;) for (var k = 9; k > 0; k--) ticks.push(pow(i) * k);
2072 for (; i < j; i++) for (var k = 1; k < 10; k++) ticks.push(pow(i) * k);
2075 for (i = 0; ticks[i] < u; i++) {} // strip small values
2076 for (j = ticks.length; ticks[j - 1] > v; j--) {} // strip big values
2077 ticks = ticks.slice(i, j);
2082 scale.tickFormat = function() {
2083 return function(d) { return d.toPrecision(1); };
2089 function d3_scale_log(x) {
2090 return Math.log(x) / Math.LN10;
2093 function d3_scale_logn(x) {
2094 return -Math.log(-x) / Math.LN10;
2097 d3_scale_log.pow = function(x) {
2098 return Math.pow(10, x);
2101 d3_scale_logn.pow = function(x) {
2102 return -Math.pow(10, -x);
2104 d3.scale.pow = function() {
2105 var linear = d3.scale.linear(),
2106 tick = d3.scale.linear(), // TODO better tick formatting...
2112 return linear(powp(x));
2115 scale.invert = function(x) {
2116 return powb(linear.invert(x));
2119 scale.domain = function(x) {
2120 if (!arguments.length) return linear.domain().map(powb);
2121 var pow = (x[0] || x[1]) < 0 ? d3_scale_pown : d3_scale_pow;
2122 powp = pow(exponent);
2123 powb = pow(1 / exponent);
2124 linear.domain(x.map(powp));
2129 scale.range = d3.rebind(scale, linear.range);
2130 scale.rangeRound = d3.rebind(scale, linear.rangeRound);
2131 scale.interpolate = d3.rebind(scale, linear.interpolate);
2132 scale.ticks = tick.ticks;
2133 scale.tickFormat = tick.tickFormat;
2135 scale.exponent = function(x) {
2136 if (!arguments.length) return exponent;
2137 var domain = scale.domain();
2139 return scale.domain(domain);
2145 function d3_scale_pow(e) {
2146 return function(x) {
2147 return Math.pow(x, e);
2151 function d3_scale_pown(e) {
2152 return function(x) {
2153 return -Math.pow(-x, e);
2156 d3.scale.sqrt = function() {
2157 return d3.scale.pow().exponent(.5);
2159 d3.scale.ordinal = function() {
2166 var i = x in index ? index[x] : (index[x] = domain.push(x) - 1);
2167 return range[i % range.length];
2170 scale.domain = function(x) {
2171 if (!arguments.length) return domain;
2174 var i = -1, j = -1, n = domain.length; while (++i < n) {
2176 if (!(x in index)) index[x] = ++j;
2181 scale.range = function(x) {
2182 if (!arguments.length) return range;
2187 scale.rangePoints = function(x, padding) {
2188 if (arguments.length < 2) padding = 0;
2191 step = (stop - start) / (domain.length - 1 + padding);
2192 range = domain.length == 1
2193 ? [(start + stop) / 2]
2194 : d3.range(start + step * padding / 2, stop + step / 2, step);
2199 scale.rangeBands = function(x, padding) {
2200 if (arguments.length < 2) padding = 0;
2203 step = (stop - start) / (domain.length + padding);
2204 range = d3.range(start + step * padding, stop, step);
2205 rangeBand = step * (1 - padding);
2209 scale.rangeRoundBands = function(x, padding) {
2210 if (arguments.length < 2) padding = 0;
2213 diff = stop - start,
2214 step = Math.floor(diff / (domain.length + padding)),
2215 err = diff - (domain.length - padding) * step;
2216 range = d3.range(start + Math.round(err / 2), stop, step);
2217 rangeBand = Math.round(step * (1 - padding));
2221 scale.rangeBand = function() {
2228 * This product includes color specifications and designs developed by Cynthia
2229 * Brewer (http://colorbrewer.org/). See lib/colorbrewer for more information.
2232 d3.scale.category10 = function() {
2233 return d3.scale.ordinal().range(d3_category10);
2236 d3.scale.category20 = function() {
2237 return d3.scale.ordinal().range(d3_category20);
2240 d3.scale.category20b = function() {
2241 return d3.scale.ordinal().range(d3_category20b);
2244 d3.scale.category20c = function() {
2245 return d3.scale.ordinal().range(d3_category20c);
2248 var d3_category10 = [
2249 "#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
2250 "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"
2253 var d3_category20 = [
2254 "#1f77b4", "#aec7e8",
2255 "#ff7f0e", "#ffbb78",
2256 "#2ca02c", "#98df8a",
2257 "#d62728", "#ff9896",
2258 "#9467bd", "#c5b0d5",
2259 "#8c564b", "#c49c94",
2260 "#e377c2", "#f7b6d2",
2261 "#7f7f7f", "#c7c7c7",
2262 "#bcbd22", "#dbdb8d",
2263 "#17becf", "#9edae5"
2266 var d3_category20b = [
2267 "#393b79", "#5254a3", "#6b6ecf", "#9c9ede",
2268 "#637939", "#8ca252", "#b5cf6b", "#cedb9c",
2269 "#8c6d31", "#bd9e39", "#e7ba52", "#e7cb94",
2270 "#843c39", "#ad494a", "#d6616b", "#e7969c",
2271 "#7b4173", "#a55194", "#ce6dbd", "#de9ed6"
2274 var d3_category20c = [
2275 "#3182bd", "#6baed6", "#9ecae1", "#c6dbef",
2276 "#e6550d", "#fd8d3c", "#fdae6b", "#fdd0a2",
2277 "#31a354", "#74c476", "#a1d99b", "#c7e9c0",
2278 "#756bb1", "#9e9ac8", "#bcbddc", "#dadaeb",
2279 "#636363", "#969696", "#bdbdbd", "#d9d9d9"
2281 d3.scale.quantile = function() {
2286 function rescale() {
2288 n = thresholds.length = range.length,
2289 k = domain.length / n;
2290 while (++i < n) thresholds[i] = domain[~~(i * k)];
2293 function quantile(value) {
2294 if (isNaN(value = +value)) return NaN;
2295 var low = 0, high = thresholds.length - 1;
2296 while (low <= high) {
2297 var mid = (low + high) >> 1, midValue = thresholds[mid];
2298 if (midValue < value) low = mid + 1;
2299 else if (midValue > value) high = mid - 1;
2302 return high < 0 ? 0 : high;
2306 return range[quantile(x)];
2309 scale.domain = function(x) {
2310 if (!arguments.length) return domain;
2311 domain = x.filter(function(d) { return !isNaN(d); }).sort(d3.ascending);
2316 scale.range = function(x) {
2317 if (!arguments.length) return range;
2323 scale.quantiles = function() {
2329 d3.scale.quantize = function() {
2337 return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))];
2340 scale.domain = function(x) {
2341 if (!arguments.length) return [x0, x1];
2344 kx = range.length / (x1 - x0);
2348 scale.range = function(x) {
2349 if (!arguments.length) return range;
2351 kx = range.length / (x1 - x0);
2352 i = range.length - 1;
2359 d3.svg.arc = function() {
2360 var innerRadius = d3_svg_arcInnerRadius,
2361 outerRadius = d3_svg_arcOuterRadius,
2362 startAngle = d3_svg_arcStartAngle,
2363 endAngle = d3_svg_arcEndAngle;
2366 var r0 = innerRadius.apply(this, arguments),
2367 r1 = outerRadius.apply(this, arguments),
2368 a0 = startAngle.apply(this, arguments) + d3_svg_arcOffset,
2369 a1 = endAngle.apply(this, arguments) + d3_svg_arcOffset,
2371 df = da < Math.PI ? "0" : "1",
2376 return da >= d3_svg_arcMax
2379 + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1)
2380 + "A" + r1 + "," + r1 + " 0 1,1 0," + r1
2382 + "A" + r0 + "," + r0 + " 0 1,1 0," + (-r0)
2383 + "A" + r0 + "," + r0 + " 0 1,1 0," + r0
2386 + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1)
2387 + "A" + r1 + "," + r1 + " 0 1,1 0," + r1
2390 ? "M" + r1 * c0 + "," + r1 * s0
2391 + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1
2392 + "L" + r0 * c1 + "," + r0 * s1
2393 + "A" + r0 + "," + r0 + " 0 " + df + ",0 " + r0 * c0 + "," + r0 * s0
2395 : "M" + r1 * c0 + "," + r1 * s0
2396 + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1
2401 arc.innerRadius = function(v) {
2402 if (!arguments.length) return innerRadius;
2403 innerRadius = d3_functor(v);
2407 arc.outerRadius = function(v) {
2408 if (!arguments.length) return outerRadius;
2409 outerRadius = d3_functor(v);
2413 arc.startAngle = function(v) {
2414 if (!arguments.length) return startAngle;
2415 startAngle = d3_functor(v);
2419 arc.endAngle = function(v) {
2420 if (!arguments.length) return endAngle;
2421 endAngle = d3_functor(v);
2425 arc.centroid = function() {
2426 var r = (innerRadius.apply(this, arguments)
2427 + outerRadius.apply(this, arguments)) / 2,
2428 a = (startAngle.apply(this, arguments)
2429 + endAngle.apply(this, arguments)) / 2 + d3_svg_arcOffset;
2430 return [Math.cos(a) * r, Math.sin(a) * r];
2436 var d3_svg_arcOffset = -Math.PI / 2,
2437 d3_svg_arcMax = 2 * Math.PI - 1e-6;
2439 function d3_svg_arcInnerRadius(d) {
2440 return d.innerRadius;
2443 function d3_svg_arcOuterRadius(d) {
2444 return d.outerRadius;
2447 function d3_svg_arcStartAngle(d) {
2448 return d.startAngle;
2451 function d3_svg_arcEndAngle(d) {
2454 d3.svg.line = function() {
2455 var x = d3_svg_lineX,
2457 interpolate = "linear",
2458 interpolator = d3_svg_lineInterpolators[interpolate],
2462 return d.length < 1 ? null
2463 : "M" + interpolator(d3_svg_linePoints(this, d, x, y), tension);
2466 line.x = function(v) {
2467 if (!arguments.length) return x;
2472 line.y = function(v) {
2473 if (!arguments.length) return y;
2478 line.interpolate = function(v) {
2479 if (!arguments.length) return interpolate;
2480 interpolator = d3_svg_lineInterpolators[interpolate = v];
2484 line.tension = function(v) {
2485 if (!arguments.length) return tension;
2493 // Converts the specified array of data into an array of points
2494 // (x-y tuples), by evaluating the specified `x` and `y` functions on each
2495 // data point. The `this` context of the evaluated functions is the specified
2496 // "self" object; each function is passed the current datum and index.
2497 function d3_svg_linePoints(self, d, x, y) {
2501 fx = typeof x == "function",
2502 fy = typeof y == "function",
2505 while (++i < n) points.push([
2506 x.call(self, value = d[i], i),
2507 y.call(self, value, i)
2510 while (++i < n) points.push([x.call(self, d[i], i), y]);
2512 while (++i < n) points.push([x, y.call(self, d[i], i)]);
2514 while (++i < n) points.push([x, y]);
2519 // The default `x` property, which references d[0].
2520 function d3_svg_lineX(d) {
2524 // The default `y` property, which references d[1].
2525 function d3_svg_lineY(d) {
2529 // The various interpolators supported by the `line` class.
2530 var d3_svg_lineInterpolators = {
2531 "linear": d3_svg_lineLinear,
2532 "step-before": d3_svg_lineStepBefore,
2533 "step-after": d3_svg_lineStepAfter,
2534 "basis": d3_svg_lineBasis,
2535 "basis-closed": d3_svg_lineBasisClosed,
2536 "cardinal": d3_svg_lineCardinal,
2537 "cardinal-closed": d3_svg_lineCardinalClosed
2540 // Linear interpolation; generates "L" commands.
2541 function d3_svg_lineLinear(points) {
2546 path.push(p[0], ",", p[1]);
2547 while (++i < n) path.push("L", (p = points[i])[0], ",", p[1]);
2548 return path.join("");
2551 // Step interpolation; generates "H" and "V" commands.
2552 function d3_svg_lineStepBefore(points) {
2557 path.push(p[0], ",", p[1]);
2558 while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]);
2559 return path.join("");
2562 // Step interpolation; generates "H" and "V" commands.
2563 function d3_svg_lineStepAfter(points) {
2568 path.push(p[0], ",", p[1]);
2569 while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]);
2570 return path.join("");
2573 // Closed cardinal spline interpolation; generates "C" commands.
2574 function d3_svg_lineCardinalClosed(points, tension) {
2575 return points.length < 3
2576 ? d3_svg_lineLinear(points)
2577 : points[0] + d3_svg_lineHermite(points,
2578 d3_svg_lineCardinalTangents([points[points.length - 2]]
2579 .concat(points, [points[1]]), tension));
2582 // Cardinal spline interpolation; generates "C" commands.
2583 function d3_svg_lineCardinal(points, tension, closed) {
2584 return points.length < 3
2585 ? d3_svg_lineLinear(points)
2586 : points[0] + d3_svg_lineHermite(points,
2587 d3_svg_lineCardinalTangents(points, tension));
2590 // Hermite spline construction; generates "C" commands.
2591 function d3_svg_lineHermite(points, tangents) {
2592 if (tangents.length < 1
2593 || (points.length != tangents.length
2594 && points.length != tangents.length + 2)) {
2595 return d3_svg_lineLinear(points);
2598 var quad = points.length != tangents.length,
2607 path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3)
2608 + "," + p[0] + "," + p[1];
2613 if (tangents.length > 1) {
2617 path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1])
2618 + "," + (p[0] - t[0]) + "," + (p[1] - t[1])
2619 + "," + p[0] + "," + p[1];
2620 for (var i = 2; i < tangents.length; i++, pi++) {
2623 path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1])
2624 + "," + p[0] + "," + p[1];
2629 var lp = points[pi];
2630 path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3)
2631 + "," + lp[0] + "," + lp[1];
2637 // Generates tangents for a cardinal spline.
2638 function d3_svg_lineCardinalTangents(points, tension) {
2640 a = (1 - tension) / 2,
2647 tangents.push([a * (p2[0] - p0[0]), a * (p2[1] - p0[1])]);
2652 tangents.push([a * (p2[0] - p0[0]), a * (p2[1] - p0[1])]);
2656 // Open B-spline interpolation; generates "C" commands.
2657 function d3_svg_lineBasis(points) {
2658 if (points.length < 3) return d3_svg_lineLinear(points);
2665 px = [x0, x0, x0, (pi = points[1])[0]],
2666 py = [y0, y0, y0, pi[1]];
2667 path.push(x0, ",", y0);
2668 d3_svg_lineBasisBezier(path, px, py);
2671 px.shift(); px.push(pi[0]);
2672 py.shift(); py.push(pi[1]);
2673 d3_svg_lineBasisBezier(path, px, py);
2677 px.shift(); px.push(pi[0]);
2678 py.shift(); py.push(pi[1]);
2679 d3_svg_lineBasisBezier(path, px, py);
2681 return path.join("");
2684 // Closed B-spline interpolation; generates "C" commands.
2685 function d3_svg_lineBasisClosed(points) {
2699 d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",",
2700 d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)
2702 --i; while (++i < m) {
2704 px.shift(); px.push(pi[0]);
2705 py.shift(); py.push(pi[1]);
2706 d3_svg_lineBasisBezier(path, px, py);
2708 return path.join("");
2711 // Returns the dot product of the given four-element vectors.
2712 function d3_svg_lineDot4(a, b) {
2713 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
2716 // Matrix to transform basis (b-spline) control points to bezier
2717 // control points. Derived from FvD 11.2.8.
2718 var d3_svg_lineBasisBezier1 = [0, 2/3, 1/3, 0],
2719 d3_svg_lineBasisBezier2 = [0, 1/3, 2/3, 0],
2720 d3_svg_lineBasisBezier3 = [0, 1/6, 2/3, 1/6];
2722 // Pushes a "C" Bézier curve onto the specified path array, given the
2723 // two specified four-element arrays which define the control points.
2724 function d3_svg_lineBasisBezier(path, x, y) {
2726 "C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x),
2727 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y),
2728 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x),
2729 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y),
2730 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x),
2731 ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y));
2733 d3.svg.area = function() {
2734 var x = d3_svg_lineX,
2737 interpolate = "linear",
2738 interpolator = d3_svg_lineInterpolators[interpolate],
2741 // TODO horizontal / vertical / radial orientation
2744 return d.length < 1 ? null
2745 : "M" + interpolator(d3_svg_linePoints(this, d, x, y1), tension)
2746 + "L" + interpolator(d3_svg_linePoints(this, d, x, y0).reverse(), tension)
2750 area.x = function(v) {
2751 if (!arguments.length) return x;
2756 area.y0 = function(v) {
2757 if (!arguments.length) return y0;
2762 area.y1 = function(v) {
2763 if (!arguments.length) return y1;
2768 area.interpolate = function(v) {
2769 if (!arguments.length) return interpolate;
2770 interpolator = d3_svg_lineInterpolators[interpolate = v];
2774 area.tension = function(v) {
2775 if (!arguments.length) return tension;
2783 function d3_svg_areaY0() {
2786 d3.svg.chord = function() {
2787 var source = d3_svg_chordSource,
2788 target = d3_svg_chordTarget,
2789 radius = d3_svg_chordRadius,
2790 startAngle = d3_svg_arcStartAngle,
2791 endAngle = d3_svg_arcEndAngle;
2793 // TODO Allow control point to be customized.
2795 function chord(d, i) {
2796 var s = subgroup(this, source, d, i),
2797 t = subgroup(this, target, d, i);
2799 + arc(s.r, s.p1) + (equals(s, t)
2800 ? curve(s.r, s.p1, s.r, s.p0)
2801 : curve(s.r, s.p1, t.r, t.p0)
2803 + curve(t.r, t.p1, s.r, s.p0))
2807 function subgroup(self, f, d, i) {
2808 var subgroup = f.call(self, d, i),
2809 r = radius.call(self, subgroup, i),
2810 a0 = startAngle.call(self, subgroup, i) + d3_svg_arcOffset,
2811 a1 = endAngle.call(self, subgroup, i) + d3_svg_arcOffset;
2816 p0: [r * Math.cos(a0), r * Math.sin(a0)],
2817 p1: [r * Math.cos(a1), r * Math.sin(a1)]
2821 function equals(a, b) {
2822 return a.a0 == b.a0 && a.a1 == b.a1;
2825 function arc(r, p) {
2826 return "A" + r + "," + r + " 0 0,1 " + p;
2829 function curve(r0, p0, r1, p1) {
2830 return "Q 0,0 " + p1;
2833 chord.radius = function(v) {
2834 if (!arguments.length) return radius;
2835 radius = d3_functor(v);
2839 chord.source = function(v) {
2840 if (!arguments.length) return source;
2841 source = d3_functor(v);
2845 chord.target = function(v) {
2846 if (!arguments.length) return target;
2847 target = d3_functor(v);
2851 chord.startAngle = function(v) {
2852 if (!arguments.length) return startAngle;
2853 startAngle = d3_functor(v);
2857 chord.endAngle = function(v) {
2858 if (!arguments.length) return endAngle;
2859 endAngle = d3_functor(v);
2866 function d3_svg_chordSource(d) {
2870 function d3_svg_chordTarget(d) {
2874 function d3_svg_chordRadius(d) {
2878 function d3_svg_chordStartAngle(d) {
2879 return d.startAngle;
2882 function d3_svg_chordEndAngle(d) {
2885 d3.svg.mouse = function(container) {
2886 var point = (container.ownerSVGElement || container).createSVGPoint();
2887 if ((d3_mouse_bug44083 < 0) && (window.scrollX || window.scrollY)) {
2888 var svg = d3.select(document.body)
2890 .style("position", "absolute")
2893 var ctm = svg[0][0].getScreenCTM();
2894 d3_mouse_bug44083 = !(ctm.f || ctm.e);
2897 if (d3_mouse_bug44083) {
2898 point.x = d3.event.pageX;
2899 point.y = d3.event.pageY;
2901 point.x = d3.event.clientX;
2902 point.y = d3.event.clientY;
2904 point = point.matrixTransform(container.getScreenCTM().inverse());
2905 return [point.x, point.y];
2908 // https://bugs.webkit.org/show_bug.cgi?id=44083
2909 var d3_mouse_bug44083 = /WebKit/.test(navigator.userAgent) ? -1 : 0;
2910 d3.svg.symbol = function() {
2911 var type = d3_svg_symbolType,
2912 size = d3_svg_symbolSize;
2914 function symbol(d, i) {
2915 return (d3_svg_symbols[type.call(this, d, i)]
2916 || d3_svg_symbols.circle)
2917 (size.call(this, d, i));
2920 symbol.type = function(x) {
2921 if (!arguments.length) return type;
2922 type = d3_functor(x);
2926 // size of symbol in square pixels
2927 symbol.size = function(x) {
2928 if (!arguments.length) return size;
2929 size = d3_functor(x);
2936 // TODO cross-diagonal?
2937 d3.svg.symbolTypes = [
2946 function d3_svg_symbolSize() {
2950 function d3_svg_symbolType() {
2954 var d3_svg_symbols = {
2955 "circle": function(size) {
2956 var r = Math.sqrt(size / Math.PI);
2958 + "A" + r + "," + r + " 0 1,1 0," + (-r)
2959 + "A" + r + "," + r + " 0 1,1 0," + r
2962 "cross": function(size) {
2963 var r = Math.sqrt(size / 5) / 2;
2964 return "M" + -3 * r + "," + -r
2978 "diamond": function(size) {
2979 var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)),
2980 rx = ry * d3_svg_symbolTan30;
2987 "square": function(size) {
2988 var r = Math.sqrt(size) / 2;
2989 return "M" + -r + "," + -r
2990 + "L" + r + "," + -r
2992 + " " + -r + "," + r
2995 "triangle-down": function(size) {
2996 var rx = Math.sqrt(size / d3_svg_symbolSqrt3),
2997 ry = rx * d3_svg_symbolSqrt3 / 2;
2999 + "L" + rx +"," + -ry
3000 + " " + -rx + "," + -ry
3003 "triangle-up": function(size) {
3004 var rx = Math.sqrt(size / d3_svg_symbolSqrt3),
3005 ry = rx * d3_svg_symbolSqrt3 / 2;
3007 + "L" + rx +"," + ry
3008 + " " + -rx + "," + ry
3013 var d3_svg_symbolSqrt3 = Math.sqrt(3),
3014 d3_svg_symbolTan30 = Math.tan(30 * Math.PI / 180);