3 var bootstrap = (typeof exports.bootstrap === "object") ?
5 (exports.bootstrap = {});
7 bootstrap.tooltip = function() {
9 var tooltip = function(selection) {
10 selection.each(setup);
12 animation = d3.functor(false),
13 html = d3.functor(false),
15 var title = this.getAttribute("data-original-title");
19 title = this.getAttribute("title");
20 this.removeAttribute("title");
21 this.setAttribute("data-original-title", title);
25 over = "mouseenter.tooltip",
26 out = "mouseleave.tooltip",
27 placements = "top left bottom right".split(" "),
28 placement = d3.functor("top");
30 tooltip.title = function(_) {
31 if (arguments.length) {
32 title = d3.functor(_);
39 tooltip.html = function(_) {
40 if (arguments.length) {
48 tooltip.placement = function(_) {
49 if (arguments.length) {
50 placement = d3.functor(_);
57 tooltip.show = function(selection) {
61 tooltip.hide = function(selection) {
65 tooltip.toggle = function(selection) {
66 selection.each(toggle);
69 tooltip.destroy = function(selection) {
73 .attr("title", function() {
74 return this.getAttribute("data-original-title") || this.getAttribute("title");
76 .attr("data-original-title", null)
82 var root = d3.select(this),
83 animate = animation.apply(this, arguments),
84 tip = root.append("div")
85 .attr("class", "tooltip");
88 tip.classed("fade", true);
91 // TODO "inside" checks?
94 .attr("class", "tooltip-arrow");
96 .attr("class", "tooltip-inner");
98 var place = placement.apply(this, arguments);
99 tip.classed(place, true);
106 var root = d3.select(this),
107 content = title.apply(this, arguments),
108 tip = root.select(".tooltip")
109 .classed("in", true),
110 markup = html.apply(this, arguments),
111 innercontent = tip.select(".tooltip-inner")[markup ? "html" : "text"](content),
112 place = placement.apply(this, arguments),
113 outer = getPosition(root.node()),
114 inner = getPosition(tip.node()),
119 pos = {x: outer.x + (outer.w - inner.w) / 2, y: outer.y - inner.h};
122 pos = {x: outer.x + outer.w, y: outer.y + (outer.h - inner.h) / 2};
125 pos = {x: outer.x - inner.w, y: outer.y + (outer.h - inner.h) / 2};
128 pos = {x: Math.max(0, outer.x + (outer.w - inner.w) / 2), y: outer.y + outer.h};
133 {left: ~~pos.x + "px", top: ~~pos.y + "px"} :
134 {left: null, top: null});
136 this.tooltipVisible = true;
140 d3.select(this).select(".tooltip")
141 .classed("in", false);
143 this.tooltipVisible = false;
147 if (this.tooltipVisible) {
148 hide.apply(this, arguments);
150 show.apply(this, arguments);
157 function getPosition(node) {
158 var mode = d3.select(node).style('position');
159 if (mode === 'absolute' || mode === 'static') {
178 var d3 = {version: "3.3.10"}; // semver
179 d3.ascending = function(a, b) {
180 return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
182 d3.descending = function(a, b) {
183 return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
185 d3.min = function(array, f) {
190 if (arguments.length === 1) {
191 while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined;
192 while (++i < n) if ((b = array[i]) != null && a > b) a = b;
194 while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
195 while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
199 d3.max = function(array, f) {
204 if (arguments.length === 1) {
205 while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined;
206 while (++i < n) if ((b = array[i]) != null && b > a) a = b;
208 while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
209 while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
213 d3.extent = function(array, f) {
219 if (arguments.length === 1) {
220 while (++i < n && !((a = c = array[i]) != null && a <= a)) a = c = undefined;
221 while (++i < n) if ((b = array[i]) != null) {
226 while (++i < n && !((a = c = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
227 while (++i < n) if ((b = f.call(array, array[i], i)) != null) {
234 d3.sum = function(array, f) {
240 if (arguments.length === 1) {
241 while (++i < n) if (!isNaN(a = +array[i])) s += a;
243 while (++i < n) if (!isNaN(a = +f.call(array, array[i], i))) s += a;
248 function d3_number(x) {
249 return x != null && !isNaN(x);
252 d3.mean = function(array, f) {
253 var n = array.length,
258 if (arguments.length === 1) {
259 while (++i < n) if (d3_number(a = array[i])) m += (a - m) / ++j;
261 while (++i < n) if (d3_number(a = f.call(array, array[i], i))) m += (a - m) / ++j;
263 return j ? m : undefined;
265 // R-7 per <http://en.wikipedia.org/wiki/Quantile>
266 d3.quantile = function(values, p) {
267 var H = (values.length - 1) * p + 1,
271 return e ? v + e * (values[h] - v) : v;
274 d3.median = function(array, f) {
275 if (arguments.length > 1) array = array.map(f);
276 array = array.filter(d3_number);
277 return array.length ? d3.quantile(array.sort(d3.ascending), .5) : undefined;
279 d3.bisector = function(f) {
281 left: function(a, x, lo, hi) {
282 if (arguments.length < 3) lo = 0;
283 if (arguments.length < 4) hi = a.length;
285 var mid = lo + hi >>> 1;
286 if (f.call(a, a[mid], mid) < x) lo = mid + 1;
291 right: function(a, x, lo, hi) {
292 if (arguments.length < 3) lo = 0;
293 if (arguments.length < 4) hi = a.length;
295 var mid = lo + hi >>> 1;
296 if (x < f.call(a, a[mid], mid)) hi = mid;
304 var d3_bisector = d3.bisector(function(d) { return d; });
305 d3.bisectLeft = d3_bisector.left;
306 d3.bisect = d3.bisectRight = d3_bisector.right;
307 d3.shuffle = function(array) {
308 var m = array.length, t, i;
310 i = Math.random() * m-- | 0;
311 t = array[m], array[m] = array[i], array[i] = t;
315 d3.permute = function(array, indexes) {
316 var i = indexes.length, permutes = new Array(i);
317 while (i--) permutes[i] = array[indexes[i]];
320 d3.pairs = function(array) {
321 var i = 0, n = array.length - 1, p0, p1 = array[0], pairs = new Array(n < 0 ? 0 : n);
322 while (i < n) pairs[i] = [p0 = p1, p1 = array[++i]];
326 d3.zip = function() {
327 if (!(n = arguments.length)) return [];
328 for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m;) {
329 for (var j = -1, n, zip = zips[i] = new Array(n); ++j < n;) {
330 zip[j] = arguments[j][i];
336 function d3_zipLength(d) {
340 d3.transpose = function(matrix) {
341 return d3.zip.apply(d3, matrix);
343 d3.keys = function(map) {
345 for (var key in map) keys.push(key);
348 d3.values = function(map) {
350 for (var key in map) values.push(map[key]);
353 d3.entries = function(map) {
355 for (var key in map) entries.push({key: key, value: map[key]});
358 d3.merge = function(arrays) {
359 var n = arrays.length,
366 while (++i < n) j += arrays[i].length;
367 merged = new Array(j);
373 merged[--j] = array[m];
381 d3.range = function(start, stop, step) {
382 if (arguments.length < 3) {
384 if (arguments.length < 2) {
389 if ((stop - start) / step === Infinity) throw new Error("infinite range");
391 k = d3_range_integerScale(abs(step)),
394 start *= k, stop *= k, step *= k;
395 if (step < 0) while ((j = start + step * ++i) > stop) range.push(j / k);
396 else while ((j = start + step * ++i) < stop) range.push(j / k);
400 function d3_range_integerScale(x) {
402 while (x * k % 1) k *= 10;
405 function d3_class(ctor, properties) {
407 for (var key in properties) {
408 Object.defineProperty(ctor.prototype, key, {
409 value: properties[key],
414 ctor.prototype = properties;
418 d3.map = function(object) {
419 var map = new d3_Map;
420 if (object instanceof d3_Map) object.forEach(function(key, value) { map.set(key, value); });
421 else for (var key in object) map.set(key, object[key]);
429 return d3_map_prefix + key in this;
432 return this[d3_map_prefix + key];
434 set: function(key, value) {
435 return this[d3_map_prefix + key] = value;
437 remove: function(key) {
438 key = d3_map_prefix + key;
439 return key in this && delete this[key];
443 this.forEach(function(key) { keys.push(key); });
448 this.forEach(function(key, value) { values.push(value); });
451 entries: function() {
453 this.forEach(function(key, value) { entries.push({key: key, value: value}); });
456 forEach: function(f) {
457 for (var key in this) {
458 if (key.charCodeAt(0) === d3_map_prefixCode) {
459 f.call(this, key.substring(1), this[key]);
465 var d3_map_prefix = "\0", // prevent collision with built-ins
466 d3_map_prefixCode = d3_map_prefix.charCodeAt(0);
468 d3.nest = function() {
475 function map(mapType, array, depth) {
476 if (depth >= keys.length) return rollup
477 ? rollup.call(nest, array) : (sortValues
478 ? array.sort(sortValues)
487 valuesByKey = new d3_Map,
491 if (values = valuesByKey.get(keyValue = key(object = array[i]))) {
494 valuesByKey.set(keyValue, [object]);
500 setter = function(keyValue, values) {
501 object.set(keyValue, map(mapType, values, depth));
505 setter = function(keyValue, values) {
506 object[keyValue] = map(mapType, values, depth);
510 valuesByKey.forEach(setter);
514 function entries(map, depth) {
515 if (depth >= keys.length) return map;
518 sortKey = sortKeys[depth++];
520 map.forEach(function(key, keyMap) {
521 array.push({key: key, values: entries(keyMap, depth)});
525 ? array.sort(function(a, b) { return sortKey(a.key, b.key); })
529 nest.map = function(array, mapType) {
530 return map(mapType, array, 0);
533 nest.entries = function(array) {
534 return entries(map(d3.map, array, 0), 0);
537 nest.key = function(d) {
542 // Specifies the order for the most-recently specified key.
543 // Note: only applies to entries. Map keys are unordered!
544 nest.sortKeys = function(order) {
545 sortKeys[keys.length - 1] = order;
549 // Specifies the order for leaf values.
550 // Applies to both maps and entries array.
551 nest.sortValues = function(order) {
556 nest.rollup = function(f) {
564 d3.set = function(array) {
565 var set = new d3_Set;
566 if (array) for (var i = 0, n = array.length; i < n; ++i) set.add(array[i]);
573 has: function(value) {
574 return d3_map_prefix + value in this;
576 add: function(value) {
577 this[d3_map_prefix + value] = true;
580 remove: function(value) {
581 value = d3_map_prefix + value;
582 return value in this && delete this[value];
586 this.forEach(function(value) {
591 forEach: function(f) {
592 for (var value in this) {
593 if (value.charCodeAt(0) === d3_map_prefixCode) {
594 f.call(this, value.substring(1));
600 var d3_arraySlice = [].slice,
601 d3_array = function(list) { return d3_arraySlice.call(list); }; // conversion for NodeLists
603 var d3_document = document,
604 d3_documentElement = d3_document.documentElement,
607 // Redefine d3_array if the browser doesn’t support slice-based conversion.
609 d3_array(d3_documentElement.childNodes)[0].nodeType;
611 d3_array = function(list) {
612 var i = list.length, array = new Array(i);
613 while (i--) array[i] = list[i];
617 // Copies a variable number of methods from source to target.
618 d3.rebind = function(target, source) {
619 var i = 1, n = arguments.length, method;
620 while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]);
624 // Method is assumed to be a standard D3 getter-setter:
625 // If passed with no arguments, gets the value.
626 // If passed with arguments, sets the value and returns the target.
627 function d3_rebind(target, source, method) {
629 var value = method.apply(source, arguments);
630 return value === source ? target : value;
634 function d3_vendorSymbol(object, name) {
635 if (name in object) return name;
636 name = name.charAt(0).toUpperCase() + name.substring(1);
637 for (var i = 0, n = d3_vendorPrefixes.length; i < n; ++i) {
638 var prefixName = d3_vendorPrefixes[i] + name;
639 if (prefixName in object) return prefixName;
643 var d3_vendorPrefixes = ["webkit", "ms", "moz", "Moz", "o", "O"];
644 function d3_noop() {}
646 d3.dispatch = function() {
647 var dispatch = new d3_dispatch,
649 n = arguments.length;
650 while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch);
654 function d3_dispatch() {}
656 d3_dispatch.prototype.on = function(type, listener) {
657 var i = type.indexOf("."),
660 // Extract optional namespace, e.g., "click.foo"
662 name = type.substring(i + 1);
663 type = type.substring(0, i);
666 if (type) return arguments.length < 2
667 ? this[type].on(name)
668 : this[type].on(name, listener);
670 if (arguments.length === 2) {
671 if (listener == null) for (type in this) {
672 if (this.hasOwnProperty(type)) this[type].on(name, null);
678 function d3_dispatch_event(dispatch) {
680 listenerByName = new d3_Map;
683 var z = listeners, // defensive reference
687 while (++i < n) if (l = z[i].on) l.apply(this, arguments);
691 event.on = function(name, listener) {
692 var l = listenerByName.get(name),
695 // return the current listener, if any
696 if (arguments.length < 2) return l && l.on;
698 // remove the old listener, if any (with copy-on-write)
701 listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1));
702 listenerByName.remove(name);
705 // add the new listener, if any
706 if (listener) listeners.push(listenerByName.set(name, {on: listener}));
716 function d3_eventPreventDefault() {
717 d3.event.preventDefault();
720 function d3_eventCancel() {
721 d3.event.preventDefault();
722 d3.event.stopPropagation();
725 function d3_eventSource() {
727 while (s = e.sourceEvent) e = s;
731 // Like d3.dispatch, but for custom events abstracting native UI events. These
732 // events have a target component (such as a brush), a target element (such as
733 // the svg:g element containing the brush) and the standard arguments `d` (the
734 // target element's data) and `i` (the selection index of the target element).
735 function d3_eventDispatch(target) {
736 var dispatch = new d3_dispatch,
738 n = arguments.length;
740 while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch);
742 // Creates a dispatch context for the specified `thiz` (typically, the target
743 // DOM element that received the source event) and `argumentz` (typically, the
744 // data `d` and index `i` of the target element). The returned function can be
745 // used to dispatch an event to any registered listeners; the function takes a
746 // single argument as input, being the event to dispatch. The event must have
747 // a "type" attribute which corresponds to a type registered in the
748 // constructor. This context will automatically populate the "sourceEvent" and
749 // "target" attributes of the event, as well as setting the `d3.event` global
750 // for the duration of the notification.
751 dispatch.of = function(thiz, argumentz) {
752 return function(e1) {
755 e1.sourceEvent = d3.event;
758 dispatch[e1.type].apply(thiz, argumentz);
767 d3.requote = function(s) {
768 return s.replace(d3_requote_re, "\\$&");
771 var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;
772 var d3_subclass = {}.__proto__?
774 // Until ECMAScript supports array subclassing, prototype injection works well.
775 function(object, prototype) {
776 object.__proto__ = prototype;
779 // And if your browser doesn't support __proto__, we'll use direct extension.
780 function(object, prototype) {
781 for (var property in prototype) object[property] = prototype[property];
784 function d3_selection(groups) {
785 d3_subclass(groups, d3_selectionPrototype);
789 var d3_select = function(s, n) { return n.querySelector(s); },
790 d3_selectAll = function(s, n) { return n.querySelectorAll(s); },
791 d3_selectMatcher = d3_documentElement[d3_vendorSymbol(d3_documentElement, "matchesSelector")],
792 d3_selectMatches = function(n, s) { return d3_selectMatcher.call(n, s); };
794 // Prefer Sizzle, if available.
795 if (typeof Sizzle === "function") {
796 d3_select = function(s, n) { return Sizzle(s, n)[0] || null; };
797 d3_selectAll = function(s, n) { return Sizzle.uniqueSort(Sizzle(s, n)); };
798 d3_selectMatches = Sizzle.matchesSelector;
801 d3.selection = function() {
802 return d3_selectionRoot;
805 var d3_selectionPrototype = d3.selection.prototype = [];
808 d3_selectionPrototype.select = function(selector) {
815 selector = d3_selection_selector(selector);
817 for (var j = -1, m = this.length; ++j < m;) {
818 subgroups.push(subgroup = []);
819 subgroup.parentNode = (group = this[j]).parentNode;
820 for (var i = -1, n = group.length; ++i < n;) {
821 if (node = group[i]) {
822 subgroup.push(subnode = selector.call(node, node.__data__, i, j));
823 if (subnode && "__data__" in node) subnode.__data__ = node.__data__;
830 return d3_selection(subgroups);
833 function d3_selection_selector(selector) {
834 return typeof selector === "function" ? selector : function() {
835 return d3_select(selector, this);
839 d3_selectionPrototype.selectAll = function(selector) {
844 selector = d3_selection_selectorAll(selector);
846 for (var j = -1, m = this.length; ++j < m;) {
847 for (var group = this[j], i = -1, n = group.length; ++i < n;) {
848 if (node = group[i]) {
849 subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i, j)));
850 subgroup.parentNode = node;
855 return d3_selection(subgroups);
858 function d3_selection_selectorAll(selector) {
859 return typeof selector === "function" ? selector : function() {
860 return d3_selectAll(selector, this);
864 svg: "http://www.w3.org/2000/svg",
865 xhtml: "http://www.w3.org/1999/xhtml",
866 xlink: "http://www.w3.org/1999/xlink",
867 xml: "http://www.w3.org/XML/1998/namespace",
868 xmlns: "http://www.w3.org/2000/xmlns/"
873 qualify: function(name) {
874 var i = name.indexOf(":"),
877 prefix = name.substring(0, i);
878 name = name.substring(i + 1);
880 return d3_nsPrefix.hasOwnProperty(prefix)
881 ? {space: d3_nsPrefix[prefix], local: name}
886 d3_selectionPrototype.attr = function(name, value) {
887 if (arguments.length < 2) {
889 // For attr(string), return the attribute value for the first node.
890 if (typeof name === "string") {
891 var node = this.node();
892 name = d3.ns.qualify(name);
894 ? node.getAttributeNS(name.space, name.local)
895 : node.getAttribute(name);
898 // For attr(object), the object specifies the names and values of the
899 // attributes to set or remove. The values may be functions that are
900 // evaluated for each element.
901 for (value in name) this.each(d3_selection_attr(value, name[value]));
905 return this.each(d3_selection_attr(name, value));
908 function d3_selection_attr(name, value) {
909 name = d3.ns.qualify(name);
911 // For attr(string, null), remove the attribute with the specified name.
912 function attrNull() {
913 this.removeAttribute(name);
915 function attrNullNS() {
916 this.removeAttributeNS(name.space, name.local);
919 // For attr(string, string), set the attribute with the specified name.
920 function attrConstant() {
921 this.setAttribute(name, value);
923 function attrConstantNS() {
924 this.setAttributeNS(name.space, name.local, value);
927 // For attr(string, function), evaluate the function for each element, and set
928 // or remove the attribute as appropriate.
929 function attrFunction() {
930 var x = value.apply(this, arguments);
931 if (x == null) this.removeAttribute(name);
932 else this.setAttribute(name, x);
934 function attrFunctionNS() {
935 var x = value.apply(this, arguments);
936 if (x == null) this.removeAttributeNS(name.space, name.local);
937 else this.setAttributeNS(name.space, name.local, x);
941 ? (name.local ? attrNullNS : attrNull) : (typeof value === "function"
942 ? (name.local ? attrFunctionNS : attrFunction)
943 : (name.local ? attrConstantNS : attrConstant));
945 function d3_collapse(s) {
946 return s.trim().replace(/\s+/g, " ");
949 d3_selectionPrototype.classed = function(name, value) {
950 if (arguments.length < 2) {
952 // For classed(string), return true only if the first node has the specified
953 // class or classes. Note that even if the browser supports DOMTokenList, it
954 // probably doesn't support it on SVG elements (which can be animated).
955 if (typeof name === "string") {
956 var node = this.node(),
957 n = (name = name.trim().split(/^|\s+/g)).length,
959 if (value = node.classList) {
960 while (++i < n) if (!value.contains(name[i])) return false;
962 value = node.getAttribute("class");
963 while (++i < n) if (!d3_selection_classedRe(name[i]).test(value)) return false;
968 // For classed(object), the object specifies the names of classes to add or
969 // remove. The values may be functions that are evaluated for each element.
970 for (value in name) this.each(d3_selection_classed(value, name[value]));
974 // Otherwise, both a name and a value are specified, and are handled as below.
975 return this.each(d3_selection_classed(name, value));
978 function d3_selection_classedRe(name) {
979 return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g");
982 // Multiple class names are allowed (e.g., "foo bar").
983 function d3_selection_classed(name, value) {
984 name = name.trim().split(/\s+/).map(d3_selection_classedName);
987 function classedConstant() {
989 while (++i < n) name[i](this, value);
992 // When the value is a function, the function is still evaluated only once per
993 // element even if there are multiple class names.
994 function classedFunction() {
995 var i = -1, x = value.apply(this, arguments);
996 while (++i < n) name[i](this, x);
999 return typeof value === "function"
1004 function d3_selection_classedName(name) {
1005 var re = d3_selection_classedRe(name);
1006 return function(node, value) {
1007 if (c = node.classList) return value ? c.add(name) : c.remove(name);
1008 var c = node.getAttribute("class") || "";
1011 if (!re.test(c)) node.setAttribute("class", d3_collapse(c + " " + name));
1013 node.setAttribute("class", d3_collapse(c.replace(re, " ")));
1018 d3_selectionPrototype.style = function(name, value, priority) {
1019 var n = arguments.length;
1022 // For style(object) or style(object, string), the object specifies the
1023 // names and values of the attributes to set or remove. The values may be
1024 // functions that are evaluated for each element. The optional string
1025 // specifies the priority.
1026 if (typeof name !== "string") {
1027 if (n < 2) value = "";
1028 for (priority in name) this.each(d3_selection_style(priority, name[priority], value));
1032 // For style(string), return the computed style value for the first node.
1033 if (n < 2) return d3_window.getComputedStyle(this.node(), null).getPropertyValue(name);
1035 // For style(string, string) or style(string, function), use the default
1036 // priority. The priority is ignored for style(string, null).
1040 // Otherwise, a name, value and priority are specified, and handled as below.
1041 return this.each(d3_selection_style(name, value, priority));
1044 function d3_selection_style(name, value, priority) {
1046 // For style(name, null) or style(name, null, priority), remove the style
1047 // property with the specified name. The priority is ignored.
1048 function styleNull() {
1049 this.style.removeProperty(name);
1052 // For style(name, string) or style(name, string, priority), set the style
1053 // property with the specified name, using the specified priority.
1054 function styleConstant() {
1055 this.style.setProperty(name, value, priority);
1058 // For style(name, function) or style(name, function, priority), evaluate the
1059 // function for each element, and set or remove the style property as
1060 // appropriate. When setting, use the specified priority.
1061 function styleFunction() {
1062 var x = value.apply(this, arguments);
1063 if (x == null) this.style.removeProperty(name);
1064 else this.style.setProperty(name, x, priority);
1067 return value == null
1068 ? styleNull : (typeof value === "function"
1069 ? styleFunction : styleConstant);
1072 d3_selectionPrototype.property = function(name, value) {
1073 if (arguments.length < 2) {
1075 // For property(string), return the property value for the first node.
1076 if (typeof name === "string") return this.node()[name];
1078 // For property(object), the object specifies the names and values of the
1079 // properties to set or remove. The values may be functions that are
1080 // evaluated for each element.
1081 for (value in name) this.each(d3_selection_property(value, name[value]));
1085 // Otherwise, both a name and a value are specified, and are handled as below.
1086 return this.each(d3_selection_property(name, value));
1089 function d3_selection_property(name, value) {
1091 // For property(name, null), remove the property with the specified name.
1092 function propertyNull() {
1096 // For property(name, string), set the property with the specified name.
1097 function propertyConstant() {
1101 // For property(name, function), evaluate the function for each element, and
1102 // set or remove the property as appropriate.
1103 function propertyFunction() {
1104 var x = value.apply(this, arguments);
1105 if (x == null) delete this[name];
1106 else this[name] = x;
1109 return value == null
1110 ? propertyNull : (typeof value === "function"
1111 ? propertyFunction : propertyConstant);
1114 d3_selectionPrototype.text = function(value) {
1115 return arguments.length
1116 ? this.each(typeof value === "function"
1117 ? function() { var v = value.apply(this, arguments); this.textContent = v == null ? "" : v; } : value == null
1118 ? function() { if (this.textContent !== "") this.textContent = ""; }
1119 : function() { if (this.textContent !== value) this.textContent = value; })
1120 : this.node().textContent;
1123 d3_selectionPrototype.html = function(value) {
1124 return arguments.length
1125 ? this.each(typeof value === "function"
1126 ? function() { var v = value.apply(this, arguments); this.innerHTML = v == null ? "" : v; } : value == null
1127 ? function() { this.innerHTML = ""; }
1128 : function() { this.innerHTML = value; })
1129 : this.node().innerHTML;
1132 d3_selectionPrototype.append = function(name) {
1133 name = d3_selection_creator(name);
1134 return this.select(function() {
1135 return this.appendChild(name.apply(this, arguments));
1139 function d3_selection_creator(name) {
1140 return typeof name === "function" ? name
1141 : (name = d3.ns.qualify(name)).local ? function() { return this.ownerDocument.createElementNS(name.space, name.local); }
1142 : function() { return this.ownerDocument.createElementNS(this.namespaceURI, name); };
1145 d3_selectionPrototype.insert = function(name, before) {
1146 name = d3_selection_creator(name);
1147 before = d3_selection_selector(before);
1148 return this.select(function() {
1149 return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments) || null);
1153 // TODO remove(selector)?
1154 // TODO remove(node)?
1155 // TODO remove(function)?
1156 d3_selectionPrototype.remove = function() {
1157 return this.each(function() {
1158 var parent = this.parentNode;
1159 if (parent) parent.removeChild(this);
1163 d3_selectionPrototype.data = function(value, key) {
1169 // If no value is specified, return the first value.
1170 if (!arguments.length) {
1171 value = new Array(n = (group = this[0]).length);
1173 if (node = group[i]) {
1174 value[i] = node.__data__;
1180 function bind(group, groupData) {
1183 m = groupData.length,
1184 n0 = Math.min(n, m),
1185 updateNodes = new Array(m),
1186 enterNodes = new Array(m),
1187 exitNodes = new Array(n),
1192 var nodeByKeyValue = new d3_Map,
1193 dataByKeyValue = new d3_Map,
1197 for (i = -1; ++i < n;) {
1198 keyValue = key.call(node = group[i], node.__data__, i);
1199 if (nodeByKeyValue.has(keyValue)) {
1200 exitNodes[i] = node; // duplicate selection key
1202 nodeByKeyValue.set(keyValue, node);
1204 keyValues.push(keyValue);
1207 for (i = -1; ++i < m;) {
1208 keyValue = key.call(groupData, nodeData = groupData[i], i);
1209 if (node = nodeByKeyValue.get(keyValue)) {
1210 updateNodes[i] = node;
1211 node.__data__ = nodeData;
1212 } else if (!dataByKeyValue.has(keyValue)) { // no duplicate data key
1213 enterNodes[i] = d3_selection_dataNode(nodeData);
1215 dataByKeyValue.set(keyValue, nodeData);
1216 nodeByKeyValue.remove(keyValue);
1219 for (i = -1; ++i < n;) {
1220 if (nodeByKeyValue.has(keyValues[i])) {
1221 exitNodes[i] = group[i];
1225 for (i = -1; ++i < n0;) {
1227 nodeData = groupData[i];
1229 node.__data__ = nodeData;
1230 updateNodes[i] = node;
1232 enterNodes[i] = d3_selection_dataNode(nodeData);
1235 for (; i < m; ++i) {
1236 enterNodes[i] = d3_selection_dataNode(groupData[i]);
1238 for (; i < n; ++i) {
1239 exitNodes[i] = group[i];
1246 enterNodes.parentNode
1247 = updateNodes.parentNode
1248 = exitNodes.parentNode
1251 enter.push(enterNodes);
1252 update.push(updateNodes);
1253 exit.push(exitNodes);
1256 var enter = d3_selection_enter([]),
1257 update = d3_selection([]),
1258 exit = d3_selection([]);
1260 if (typeof value === "function") {
1262 bind(group = this[i], value.call(group, group.parentNode.__data__, i));
1266 bind(group = this[i], value);
1270 update.enter = function() { return enter; };
1271 update.exit = function() { return exit; };
1275 function d3_selection_dataNode(data) {
1276 return {__data__: data};
1279 d3_selectionPrototype.datum = function(value) {
1280 return arguments.length
1281 ? this.property("__data__", value)
1282 : this.property("__data__");
1285 d3_selectionPrototype.filter = function(filter) {
1291 if (typeof filter !== "function") filter = d3_selection_filter(filter);
1293 for (var j = 0, m = this.length; j < m; j++) {
1294 subgroups.push(subgroup = []);
1295 subgroup.parentNode = (group = this[j]).parentNode;
1296 for (var i = 0, n = group.length; i < n; i++) {
1297 if ((node = group[i]) && filter.call(node, node.__data__, i, j)) {
1298 subgroup.push(node);
1303 return d3_selection(subgroups);
1306 function d3_selection_filter(selector) {
1308 return d3_selectMatches(this, selector);
1312 d3_selectionPrototype.order = function() {
1313 for (var j = -1, m = this.length; ++j < m;) {
1314 for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0;) {
1315 if (node = group[i]) {
1316 if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next);
1324 d3_selectionPrototype.sort = function(comparator) {
1325 comparator = d3_selection_sortComparator.apply(this, arguments);
1326 for (var j = -1, m = this.length; ++j < m;) this[j].sort(comparator);
1327 return this.order();
1330 function d3_selection_sortComparator(comparator) {
1331 if (!arguments.length) comparator = d3.ascending;
1332 return function(a, b) {
1333 return a && b ? comparator(a.__data__, b.__data__) : !a - !b;
1337 d3_selectionPrototype.each = function(callback) {
1338 return d3_selection_each(this, function(node, i, j) {
1339 callback.call(node, node.__data__, i, j);
1343 function d3_selection_each(groups, callback) {
1344 for (var j = 0, m = groups.length; j < m; j++) {
1345 for (var group = groups[j], i = 0, n = group.length, node; i < n; i++) {
1346 if (node = group[i]) callback(node, i, j);
1352 d3_selectionPrototype.call = function(callback) {
1353 var args = d3_array(arguments);
1354 callback.apply(args[0] = this, args);
1358 d3_selectionPrototype.empty = function() {
1359 return !this.node();
1362 d3_selectionPrototype.node = function() {
1363 for (var j = 0, m = this.length; j < m; j++) {
1364 for (var group = this[j], i = 0, n = group.length; i < n; i++) {
1365 var node = group[i];
1366 if (node) return node;
1372 d3_selectionPrototype.size = function() {
1374 this.each(function() { ++n; });
1378 function d3_selection_enter(selection) {
1379 d3_subclass(selection, d3_selection_enterPrototype);
1383 var d3_selection_enterPrototype = [];
1385 d3.selection.enter = d3_selection_enter;
1386 d3.selection.enter.prototype = d3_selection_enterPrototype;
1388 d3_selection_enterPrototype.append = d3_selectionPrototype.append;
1389 d3_selection_enterPrototype.empty = d3_selectionPrototype.empty;
1390 d3_selection_enterPrototype.node = d3_selectionPrototype.node;
1391 d3_selection_enterPrototype.call = d3_selectionPrototype.call;
1392 d3_selection_enterPrototype.size = d3_selectionPrototype.size;
1395 d3_selection_enterPrototype.select = function(selector) {
1403 for (var j = -1, m = this.length; ++j < m;) {
1404 upgroup = (group = this[j]).update;
1405 subgroups.push(subgroup = []);
1406 subgroup.parentNode = group.parentNode;
1407 for (var i = -1, n = group.length; ++i < n;) {
1408 if (node = group[i]) {
1409 subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i, j));
1410 subnode.__data__ = node.__data__;
1412 subgroup.push(null);
1417 return d3_selection(subgroups);
1420 d3_selection_enterPrototype.insert = function(name, before) {
1421 if (arguments.length < 2) before = d3_selection_enterInsertBefore(this);
1422 return d3_selectionPrototype.insert.call(this, name, before);
1425 function d3_selection_enterInsertBefore(enter) {
1427 return function(d, i, j) {
1428 var group = enter[j].update,
1431 if (j != j0) j0 = j, i0 = 0;
1432 if (i >= i0) i0 = i + 1;
1433 while (!(node = group[i0]) && ++i0 < n);
1438 // import "../transition/transition";
1440 d3_selectionPrototype.transition = function() {
1441 var id = d3_transitionInheritId || ++d3_transitionId,
1445 transition = d3_transitionInherit || {time: Date.now(), ease: d3_ease_cubicInOut, delay: 0, duration: 250};
1447 for (var j = -1, m = this.length; ++j < m;) {
1448 subgroups.push(subgroup = []);
1449 for (var group = this[j], i = -1, n = group.length; ++i < n;) {
1450 if (node = group[i]) d3_transitionNode(node, i, id, transition);
1451 subgroup.push(node);
1455 return d3_transition(subgroups, id);
1457 // import "../transition/transition";
1459 d3_selectionPrototype.interrupt = function() {
1460 return this.each(d3_selection_interrupt);
1463 function d3_selection_interrupt() {
1464 var lock = this.__transition__;
1465 if (lock) ++lock.active;
1468 // TODO fast singleton implementation?
1469 d3.select = function(node) {
1470 var group = [typeof node === "string" ? d3_select(node, d3_document) : node];
1471 group.parentNode = d3_documentElement;
1472 return d3_selection([group]);
1475 d3.selectAll = function(nodes) {
1476 var group = d3_array(typeof nodes === "string" ? d3_selectAll(nodes, d3_document) : nodes);
1477 group.parentNode = d3_documentElement;
1478 return d3_selection([group]);
1481 var d3_selectionRoot = d3.select(d3_documentElement);
1483 d3_selectionPrototype.on = function(type, listener, capture) {
1484 var n = arguments.length;
1487 // For on(object) or on(object, boolean), the object specifies the event
1488 // types and listeners to add or remove. The optional boolean specifies
1489 // whether the listener captures events.
1490 if (typeof type !== "string") {
1491 if (n < 2) listener = false;
1492 for (capture in type) this.each(d3_selection_on(capture, type[capture], listener));
1496 // For on(string), return the listener for the first node.
1497 if (n < 2) return (n = this.node()["__on" + type]) && n._;
1499 // For on(string, function), use the default capture.
1503 // Otherwise, a type, listener and capture are specified, and handled as below.
1504 return this.each(d3_selection_on(type, listener, capture));
1507 function d3_selection_on(type, listener, capture) {
1508 var name = "__on" + type,
1509 i = type.indexOf("."),
1510 wrap = d3_selection_onListener;
1512 if (i > 0) type = type.substring(0, i);
1513 var filter = d3_selection_onFilters.get(type);
1514 if (filter) type = filter, wrap = d3_selection_onFilter;
1516 function onRemove() {
1519 this.removeEventListener(type, l, l.$);
1525 var l = wrap(listener, d3_array(arguments));
1526 if (typeof Raven !== 'undefined') l = Raven.wrap(l);
1527 onRemove.call(this);
1528 this.addEventListener(type, this[name] = l, l.$ = capture);
1532 function removeAll() {
1533 var re = new RegExp("^__on([^.]+)" + d3.requote(type) + "$"),
1535 for (var name in this) {
1536 if (match = name.match(re)) {
1538 this.removeEventListener(match[1], l, l.$);
1545 ? listener ? onAdd : onRemove
1546 : listener ? d3_noop : removeAll;
1549 var d3_selection_onFilters = d3.map({
1550 mouseenter: "mouseover",
1551 mouseleave: "mouseout"
1554 d3_selection_onFilters.forEach(function(k) {
1555 if ("on" + k in d3_document) d3_selection_onFilters.remove(k);
1558 function d3_selection_onListener(listener, argumentz) {
1559 return function(e) {
1560 var o = d3.event; // Events can be reentrant (e.g., focus).
1562 argumentz[0] = this.__data__;
1564 listener.apply(this, argumentz);
1571 function d3_selection_onFilter(listener, argumentz) {
1572 var l = d3_selection_onListener(listener, argumentz);
1573 return function(e) {
1574 var target = this, related = e.relatedTarget;
1575 if (!related || (related !== target && !(related.compareDocumentPosition(target) & 8))) {
1581 var d3_event_dragSelect = "onselectstart" in d3_document ? null : d3_vendorSymbol(d3_documentElement.style, "userSelect"),
1582 d3_event_dragId = 0;
1584 function d3_event_dragSuppress() {
1585 var name = ".dragsuppress-" + ++d3_event_dragId,
1586 click = "click" + name,
1587 w = d3.select(d3_window)
1588 .on("touchmove" + name, d3_eventPreventDefault)
1589 .on("dragstart" + name, d3_eventPreventDefault)
1590 .on("selectstart" + name, d3_eventPreventDefault);
1591 if (d3_event_dragSelect) {
1592 var style = d3_documentElement.style,
1593 select = style[d3_event_dragSelect];
1594 style[d3_event_dragSelect] = "none";
1596 return function(suppressClick) {
1598 if (d3_event_dragSelect) style[d3_event_dragSelect] = select;
1599 if (suppressClick) { // suppress the next click, but only if it’s immediate
1600 function off() { w.on(click, null); }
1601 w.on(click, function() { d3_eventCancel(); off(); }, true);
1607 d3.mouse = function(container) {
1608 return d3_mousePoint(container, d3_eventSource());
1611 // https://bugs.webkit.org/show_bug.cgi?id=44083
1612 var d3_mouse_bug44083 = /WebKit/.test(d3_window.navigator.userAgent) ? -1 : 0;
1614 function d3_mousePoint(container, e) {
1615 if (e.changedTouches) e = e.changedTouches[0];
1616 var svg = container.ownerSVGElement || container;
1617 if (svg.createSVGPoint) {
1618 var point = svg.createSVGPoint();
1619 if (d3_mouse_bug44083 < 0 && (d3_window.scrollX || d3_window.scrollY)) {
1620 svg = d3.select("body").append("svg").style({
1621 position: "absolute",
1628 var ctm = svg[0][0].getScreenCTM();
1629 d3_mouse_bug44083 = !(ctm.f || ctm.e);
1632 if (d3_mouse_bug44083) point.x = e.pageX, point.y = e.pageY;
1633 else point.x = e.clientX, point.y = e.clientY;
1634 point = point.matrixTransform(container.getScreenCTM().inverse());
1635 return [point.x, point.y];
1637 var rect = container.getBoundingClientRect();
1638 return [e.clientX - rect.left - container.clientLeft, e.clientY - rect.top - container.clientTop];
1641 d3.touches = function(container, touches) {
1642 if (arguments.length < 2) touches = d3_eventSource().touches;
1643 return touches ? d3_array(touches).map(function(touch) {
1644 var point = d3_mousePoint(container, touch);
1645 point.identifier = touch.identifier;
1654 d3_radians = π / 180,
1655 d3_degrees = 180 / π;
1657 function d3_sgn(x) {
1658 return x > 0 ? 1 : x < 0 ? -1 : 0;
1661 function d3_acos(x) {
1662 return x > 1 ? 0 : x < -1 ? π : Math.acos(x);
1665 function d3_asin(x) {
1666 return x > 1 ? halfπ : x < -1 ? -halfπ : Math.asin(x);
1669 function d3_sinh(x) {
1670 return ((x = Math.exp(x)) - 1 / x) / 2;
1673 function d3_cosh(x) {
1674 return ((x = Math.exp(x)) + 1 / x) / 2;
1677 function d3_tanh(x) {
1678 return ((x = Math.exp(2 * x)) - 1) / (x + 1);
1681 function d3_haversin(x) {
1682 return (x = Math.sin(x / 2)) * x;
1689 // p0 = [ux0, uy0, w0]
1690 // p1 = [ux1, uy1, w1]
1691 d3.interpolateZoom = function(p0, p1) {
1692 var ux0 = p0[0], uy0 = p0[1], w0 = p0[2],
1693 ux1 = p1[0], uy1 = p1[1], w1 = p1[2];
1697 d2 = dx * dx + dy * dy,
1699 b0 = (w1 * w1 - w0 * w0 + ρ4 * d2) / (2 * w0 * ρ2 * d1),
1700 b1 = (w1 * w1 - w0 * w0 - ρ4 * d2) / (2 * w1 * ρ2 * d1),
1701 r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0),
1702 r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1),
1704 S = (dr || Math.log(w1 / w0)) / ρ;
1706 function interpolate(t) {
1710 var coshr0 = d3_cosh(r0),
1711 u = w0 / (ρ2 * d1) * (coshr0 * d3_tanh(ρ * s + r0) - d3_sinh(r0));
1715 w0 * coshr0 / d3_cosh(ρ * s + r0)
1718 // Special case for u0 ~= u1.
1722 w0 * Math.exp(ρ * s)
1726 interpolate.duration = S * 1000;
1731 d3.behavior.zoom = function() {
1732 var view = {x: 0, y: 0, k: 1},
1733 translate0, // translate when we started zooming (to avoid drift)
1734 center, // desired position of translate0 after zooming
1735 size = [960, 500], // viewport size; required for zoom interpolation
1736 scaleExtent = d3_behavior_zoomInfinity,
1737 mousedown = "mousedown.zoom",
1738 mousemove = "mousemove.zoom",
1739 mouseup = "mouseup.zoom",
1741 touchstart = "touchstart.zoom",
1742 touchtime, // time of last touchstart (to detect double-tap)
1743 event = d3_eventDispatch(zoom, "zoomstart", "zoom", "zoomend"),
1750 g .on(mousedown, mousedowned)
1751 .on(d3_behavior_zoomWheel + ".zoom", mousewheeled)
1752 .on(mousemove, mousewheelreset)
1753 .on("dblclick.zoom", dblclicked)
1754 .on(touchstart, touchstarted);
1757 zoom.event = function(g) {
1759 var event_ = event.of(this, arguments),
1761 if (d3_transitionInheritId) {
1762 d3.select(this).transition()
1763 .each("start.zoom", function() {
1764 view = this.__chart__ || {x: 0, y: 0, k: 1}; // pre-transition state
1765 zoomstarted(event_);
1767 .tween("zoom:zoom", function() {
1772 i = d3.interpolateZoom(
1773 [(cx - view.x) / view.k, (cy - view.y) / view.k, dx / view.k],
1774 [(cx - view1.x) / view1.k, (cy - view1.y) / view1.k, dx / view1.k]
1776 return function(t) {
1777 var l = i(t), k = dx / l[2];
1778 this.__chart__ = view = {x: cx - l[0] * k, y: cy - l[1] * k, k: k};
1782 .each("end.zoom", function() {
1786 this.__chart__ = view;
1787 zoomstarted(event_);
1794 zoom.translate = function(_) {
1795 if (!arguments.length) return [view.x, view.y];
1796 view = {x: +_[0], y: +_[1], k: view.k}; // copy-on-write
1801 zoom.scale = function(_) {
1802 if (!arguments.length) return view.k;
1803 view = {x: view.x, y: view.y, k: +_}; // copy-on-write
1808 zoom.scaleExtent = function(_) {
1809 if (!arguments.length) return scaleExtent;
1810 scaleExtent = _ == null ? d3_behavior_zoomInfinity : [+_[0], +_[1]];
1814 zoom.center = function(_) {
1815 if (!arguments.length) return center;
1816 center = _ && [+_[0], +_[1]];
1820 zoom.size = function(_) {
1821 if (!arguments.length) return size;
1822 size = _ && [+_[0], +_[1]];
1826 zoom.x = function(z) {
1827 if (!arguments.length) return x1;
1830 view = {x: 0, y: 0, k: 1}; // copy-on-write
1834 zoom.y = function(z) {
1835 if (!arguments.length) return y1;
1838 view = {x: 0, y: 0, k: 1}; // copy-on-write
1842 function location(p) {
1843 return [(p[0] - view.x) / view.k, (p[1] - view.y) / view.k];
1847 return [l[0] * view.k + view.x, l[1] * view.k + view.y];
1850 function scaleTo(s) {
1851 view.k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], s));
1854 function translateTo(p, l) {
1856 view.x += p[0] - l[0];
1857 view.y += p[1] - l[1];
1860 function rescale() {
1861 if (x1) x1.domain(x0.range().map(function(x) { return (x - view.x) / view.k; }).map(x0.invert));
1862 if (y1) y1.domain(y0.range().map(function(y) { return (y - view.y) / view.k; }).map(y0.invert));
1865 function zoomstarted(event) {
1866 event({type: "zoomstart"});
1869 function zoomed(event) {
1871 event({type: "zoom", scale: view.k, translate: [view.x, view.y]});
1874 function zoomended(event) {
1875 event({type: "zoomend"});
1878 function mousedowned() {
1880 event_ = event.of(target, arguments),
1881 eventTarget = d3.event.target,
1883 w = d3.select(d3_window).on(mousemove, moved).on(mouseup, ended),
1884 l = location(d3.mouse(target)),
1885 dragRestore = d3_event_dragSuppress();
1887 d3_selection_interrupt.call(target);
1888 zoomstarted(event_);
1892 translateTo(d3.mouse(target), l);
1897 w.on(mousemove, d3_window === target ? mousewheelreset : null).on(mouseup, null);
1898 dragRestore(dragged && d3.event.target === eventTarget);
1903 // These closures persist for as long as at least one touch is active.
1904 function touchstarted() {
1906 event_ = event.of(target, arguments),
1907 locations0 = {}, // touchstart locations
1908 distance0 = 0, // distance² between initial touches
1909 scale0, // scale when we started touching
1910 eventId = d3.event.changedTouches[0].identifier,
1911 touchmove = "touchmove.zoom-" + eventId,
1912 touchend = "touchend.zoom-" + eventId,
1913 w = d3.select(d3_window).on(touchmove, moved).on(touchend, ended),
1914 t = d3.select(target).on(mousedown, null).on(touchstart, started), // prevent duplicate events
1915 dragRestore = d3_event_dragSuppress();
1917 d3_selection_interrupt.call(target);
1919 zoomstarted(event_);
1921 // Updates locations of any touches in locations0.
1922 function relocate() {
1923 var touches = d3.touches(target);
1925 touches.forEach(function(t) {
1926 if (t.identifier in locations0) locations0[t.identifier] = location(t);
1931 // Temporarily override touchstart while gesture is active.
1932 function started() {
1933 // Only track touches started on the target element.
1934 var changed = d3.event.changedTouches;
1935 for (var i = 0, n = changed.length; i < n; ++i) {
1936 locations0[changed[i].identifier] = null;
1939 var touches = relocate(),
1942 if (touches.length === 1) {
1943 if (now - touchtime < 500) { // dbltap
1944 var p = touches[0], l = locations0[p.identifier];
1945 scaleTo(view.k * 2);
1947 d3_eventPreventDefault();
1951 } else if (touches.length > 1) {
1952 var p = touches[0], q = touches[1],
1953 dx = p[0] - q[0], dy = p[1] - q[1];
1954 distance0 = dx * dx + dy * dy;
1959 var touches = d3.touches(target),
1962 for (var i = 0, n = touches.length; i < n; ++i, l1 = null) {
1964 if (l1 = locations0[p1.identifier]) {
1971 var distance1 = (distance1 = p1[0] - p0[0]) * distance1 + (distance1 = p1[1] - p0[1]) * distance1,
1972 scale1 = distance0 && Math.sqrt(distance1 / distance0);
1973 p0 = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2];
1974 l0 = [(l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2];
1975 scaleTo(scale1 * scale0);
1979 translateTo(p0, l0);
1984 // If there are any globally-active touches remaining, remove the ended
1985 // touches from locations0.
1986 if (d3.event.touches.length) {
1987 var changed = d3.event.changedTouches;
1988 for (var i = 0, n = changed.length; i < n; ++i) {
1989 delete locations0[changed[i].identifier];
1991 // If locations0 is not empty, then relocate and continue listening for
1992 // touchmove and touchend.
1993 for (var identifier in locations0) {
1994 return void relocate(); // locations may have detached due to rotation
1997 // Otherwise, remove touchmove and touchend listeners.
1998 w.on(touchmove, null).on(touchend, null);
1999 t.on(mousedown, mousedowned).on(touchstart, touchstarted);
2005 function mousewheeled() {
2006 var event_ = event.of(this, arguments);
2007 if (mousewheelTimer) clearTimeout(mousewheelTimer);
2008 else d3_selection_interrupt.call(this), zoomstarted(event_);
2009 mousewheelTimer = setTimeout(function() { mousewheelTimer = null; zoomended(event_); }, 50);
2010 d3_eventPreventDefault();
2011 var point = center || d3.mouse(this);
2012 if (!translate0) translate0 = location(point);
2013 scaleTo(Math.pow(2, d3_behavior_zoomDelta() * .002) * view.k);
2014 translateTo(point, translate0);
2018 function mousewheelreset() {
2022 function dblclicked() {
2023 var event_ = event.of(this, arguments),
2026 k = Math.log(view.k) / Math.LN2;
2027 zoomstarted(event_);
2028 scaleTo(Math.pow(2, d3.event.shiftKey ? Math.ceil(k) - 1 : Math.floor(k) + 1));
2034 return d3.rebind(zoom, event, "on");
2037 var d3_behavior_zoomInfinity = [0, Infinity]; // default scale extent
2039 // https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/wheel
2040 var d3_behavior_zoomDelta, d3_behavior_zoomWheel
2041 = "onwheel" in d3_document ? (d3_behavior_zoomDelta = function() { return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1); }, "wheel")
2042 : "onmousewheel" in d3_document ? (d3_behavior_zoomDelta = function() { return d3.event.wheelDelta; }, "mousewheel")
2043 : (d3_behavior_zoomDelta = function() { return -d3.event.detail; }, "MozMousePixelScroll");
2044 function d3_functor(v) {
2045 return typeof v === "function" ? v : function() { return v; };
2048 d3.functor = d3_functor;
2050 var d3_timer_queueHead,
2052 d3_timer_interval, // is an interval (or frame) active?
2053 d3_timer_timeout, // is a timeout active?
2054 d3_timer_active, // active timer object
2055 d3_timer_frame = d3_window[d3_vendorSymbol(d3_window, "requestAnimationFrame")] || function(callback) { setTimeout(callback, 17); };
2057 // The timer will continue to fire until callback returns true.
2058 d3.timer = function(callback, delay, then) {
2059 var n = arguments.length;
2060 if (n < 2) delay = 0;
2061 if (n < 3) then = Date.now();
2063 // Add the callback to the tail of the queue.
2064 var time = then + delay, timer = {c: callback, t: time, f: false, n: null};
2065 if (d3_timer_queueTail) d3_timer_queueTail.n = timer;
2066 else d3_timer_queueHead = timer;
2067 d3_timer_queueTail = timer;
2070 if (!d3_timer_interval) {
2071 d3_timer_timeout = clearTimeout(d3_timer_timeout);
2072 d3_timer_interval = 1;
2073 d3_timer_frame(d3_timer_step);
2077 function d3_timer_step() {
2078 var now = d3_timer_mark(),
2079 delay = d3_timer_sweep() - now;
2081 if (isFinite(delay)) {
2082 clearTimeout(d3_timer_timeout);
2083 d3_timer_timeout = setTimeout(d3_timer_step, delay);
2085 d3_timer_interval = 0;
2087 d3_timer_interval = 1;
2088 d3_timer_frame(d3_timer_step);
2092 d3.timer.flush = function() {
2097 function d3_timer_mark() {
2098 var now = Date.now();
2099 d3_timer_active = d3_timer_queueHead;
2100 while (d3_timer_active) {
2101 if (now >= d3_timer_active.t) d3_timer_active.f = d3_timer_active.c(now - d3_timer_active.t);
2102 d3_timer_active = d3_timer_active.n;
2107 // Flush after callbacks to avoid concurrent queue modification.
2108 // Returns the time of the earliest active timer, post-sweep.
2109 function d3_timer_sweep() {
2111 t1 = d3_timer_queueHead,
2115 t1 = t0 ? t0.n = t1.n : d3_timer_queueHead = t1.n;
2117 if (t1.t < time) time = t1.t;
2121 d3_timer_queueTail = t0;
2125 function d3_identity(d) {
2128 function d3_true() {
2132 function d3_geo_spherical(cartesian) {
2134 Math.atan2(cartesian[1], cartesian[0]),
2135 d3_asin(cartesian[2])
2139 function d3_geo_sphericalEqual(a, b) {
2140 return abs(a[0] - b[0]) < ε && abs(a[1] - b[1]) < ε;
2143 // General spherical polygon clipping algorithm: takes a polygon, cuts it into
2144 // visible line segments and rejoins the segments by interpolating along the
2146 function d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener) {
2150 segments.forEach(function(segment) {
2151 if ((n = segment.length - 1) <= 0) return;
2152 var n, p0 = segment[0], p1 = segment[n];
2154 // If the first and last points of a segment are coincident, then treat as
2156 // TODO if all rings are closed, then the winding order of the exterior
2157 // ring should be checked.
2158 if (d3_geo_sphericalEqual(p0, p1)) {
2159 listener.lineStart();
2160 for (var i = 0; i < n; ++i) listener.point((p0 = segment[i])[0], p0[1]);
2165 var a = new d3_geo_clipPolygonIntersection(p0, segment, null, true),
2166 b = new d3_geo_clipPolygonIntersection(p0, null, a, false);
2170 a = new d3_geo_clipPolygonIntersection(p1, segment, null, false);
2171 b = new d3_geo_clipPolygonIntersection(p1, null, a, true);
2177 d3_geo_clipPolygonLinkCircular(subject);
2178 d3_geo_clipPolygonLinkCircular(clip);
2179 if (!subject.length) return;
2181 for (var i = 0, entry = clipStartInside, n = clip.length; i < n; ++i) {
2182 clip[i].e = entry = !entry;
2185 var start = subject[0],
2189 // Find first unvisited intersection.
2190 var current = start,
2192 while (current.v) if ((current = current.n) === start) return;
2194 listener.lineStart();
2196 current.v = current.o.v = true;
2199 for (var i = 0, n = points.length; i < n; ++i) listener.point((point = points[i])[0], point[1]);
2201 interpolate(current.x, current.n.x, 1, listener);
2203 current = current.n;
2206 points = current.p.z;
2207 for (var i = points.length - 1; i >= 0; --i) listener.point((point = points[i])[0], point[1]);
2209 interpolate(current.x, current.p.x, -1, listener);
2211 current = current.p;
2213 current = current.o;
2215 isSubject = !isSubject;
2216 } while (!current.v);
2221 function d3_geo_clipPolygonLinkCircular(array) {
2222 if (!(n = array.length)) return;
2236 function d3_geo_clipPolygonIntersection(point, points, other, entry) {
2239 this.o = other; // another intersection
2240 this.e = entry; // is an entry?
2241 this.v = false; // visited
2242 this.n = this.p = null; // next & previous
2245 function d3_geo_clip(pointVisible, clipLine, interpolate, clipStart) {
2246 return function(rotate, listener) {
2247 var line = clipLine(listener),
2248 rotatedClipStart = rotate.invert(clipStart[0], clipStart[1]);
2252 lineStart: lineStart,
2254 polygonStart: function() {
2255 clip.point = pointRing;
2256 clip.lineStart = ringStart;
2257 clip.lineEnd = ringEnd;
2260 listener.polygonStart();
2262 polygonEnd: function() {
2264 clip.lineStart = lineStart;
2265 clip.lineEnd = lineEnd;
2267 segments = d3.merge(segments);
2268 var clipStartInside = d3_geo_pointInPolygon(rotatedClipStart, polygon);
2269 if (segments.length) {
2270 d3_geo_clipPolygon(segments, d3_geo_clipSort, clipStartInside, interpolate, listener);
2271 } else if (clipStartInside) {
2272 listener.lineStart();
2273 interpolate(null, null, 1, listener);
2276 listener.polygonEnd();
2277 segments = polygon = null;
2279 sphere: function() {
2280 listener.polygonStart();
2281 listener.lineStart();
2282 interpolate(null, null, 1, listener);
2284 listener.polygonEnd();
2288 function point(λ, φ) {
2289 var point = rotate(λ, φ);
2290 if (pointVisible(λ = point[0], φ = point[1])) listener.point(λ, φ);
2292 function pointLine(λ, φ) {
2293 var point = rotate(λ, φ);
2294 line.point(point[0], point[1]);
2296 function lineStart() { clip.point = pointLine; line.lineStart(); }
2297 function lineEnd() { clip.point = point; line.lineEnd(); }
2301 var buffer = d3_geo_clipBufferListener(),
2302 ringListener = clipLine(buffer),
2306 function pointRing(λ, φ) {
2308 var point = rotate(λ, φ);
2309 ringListener.point(point[0], point[1]);
2312 function ringStart() {
2313 ringListener.lineStart();
2317 function ringEnd() {
2318 pointRing(ring[0][0], ring[0][1]);
2319 ringListener.lineEnd();
2321 var clean = ringListener.clean(),
2322 ringSegments = buffer.buffer(),
2324 n = ringSegments.length;
2332 // No intersections.
2334 segment = ringSegments[0];
2335 var n = segment.length - 1,
2338 listener.lineStart();
2339 while (++i < n) listener.point((point = segment[i])[0], point[1]);
2344 // Rejoin connected segments.
2345 // TODO reuse bufferListener.rejoin()?
2346 if (n > 1 && clean & 2) ringSegments.push(ringSegments.pop().concat(ringSegments.shift()));
2348 segments.push(ringSegments.filter(d3_geo_clipSegmentLength1));
2355 function d3_geo_clipSegmentLength1(segment) {
2356 return segment.length > 1;
2359 function d3_geo_clipBufferListener() {
2363 lineStart: function() { lines.push(line = []); },
2364 point: function(λ, φ) { line.push([λ, φ]); },
2366 buffer: function() {
2372 rejoin: function() {
2373 if (lines.length > 1) lines.push(lines.pop().concat(lines.shift()));
2378 // Intersection points are sorted along the clip edge. For both antimeridian
2379 // cutting and circle clipping, the same comparison is used.
2380 function d3_geo_clipSort(a, b) {
2381 return ((a = a.x)[0] < 0 ? a[1] - halfπ - ε : halfπ - a[1])
2382 - ((b = b.x)[0] < 0 ? b[1] - halfπ - ε : halfπ - b[1]);
2384 // Adds floating point numbers with twice the normal precision.
2385 // Reference: J. R. Shewchuk, Adaptive Precision Floating-Point Arithmetic and
2386 // Fast Robust Geometric Predicates, Discrete & Computational Geometry 18(3)
2388 // Code adapted from GeographicLib by Charles F. F. Karney,
2389 // http://geographiclib.sourceforge.net/
2390 // See lib/geographiclib/LICENSE for details.
2392 function d3_adder() {}
2394 d3_adder.prototype = {
2395 s: 0, // rounded value
2396 t: 0, // exact error
2398 d3_adderSum(y, this.t, d3_adderTemp);
2399 d3_adderSum(d3_adderTemp.s, this.s, this);
2400 if (this.s) this.t += d3_adderTemp.t;
2401 else this.s = d3_adderTemp.t;
2404 this.s = this.t = 0;
2406 valueOf: function() {
2411 var d3_adderTemp = new d3_adder;
2413 function d3_adderSum(a, b, o) {
2414 var x = o.s = a + b, // a + b
2415 bv = x - a, av = x - bv; // b_virtual & a_virtual
2416 o.t = (a - av) + (b - bv); // a_roundoff + b_roundoff
2419 d3.geo.stream = function(object, listener) {
2420 if (object && d3_geo_streamObjectType.hasOwnProperty(object.type)) {
2421 d3_geo_streamObjectType[object.type](object, listener);
2423 d3_geo_streamGeometry(object, listener);
2427 function d3_geo_streamGeometry(geometry, listener) {
2428 if (geometry && d3_geo_streamGeometryType.hasOwnProperty(geometry.type)) {
2429 d3_geo_streamGeometryType[geometry.type](geometry, listener);
2433 var d3_geo_streamObjectType = {
2434 Feature: function(feature, listener) {
2435 d3_geo_streamGeometry(feature.geometry, listener);
2437 FeatureCollection: function(object, listener) {
2438 var features = object.features, i = -1, n = features.length;
2439 while (++i < n) d3_geo_streamGeometry(features[i].geometry, listener);
2443 var d3_geo_streamGeometryType = {
2444 Sphere: function(object, listener) {
2447 Point: function(object, listener) {
2448 object = object.coordinates;
2449 listener.point(object[0], object[1], object[2]);
2451 MultiPoint: function(object, listener) {
2452 var coordinates = object.coordinates, i = -1, n = coordinates.length;
2453 while (++i < n) object = coordinates[i], listener.point(object[0], object[1], object[2]);
2455 LineString: function(object, listener) {
2456 d3_geo_streamLine(object.coordinates, listener, 0);
2458 MultiLineString: function(object, listener) {
2459 var coordinates = object.coordinates, i = -1, n = coordinates.length;
2460 while (++i < n) d3_geo_streamLine(coordinates[i], listener, 0);
2462 Polygon: function(object, listener) {
2463 d3_geo_streamPolygon(object.coordinates, listener);
2465 MultiPolygon: function(object, listener) {
2466 var coordinates = object.coordinates, i = -1, n = coordinates.length;
2467 while (++i < n) d3_geo_streamPolygon(coordinates[i], listener);
2469 GeometryCollection: function(object, listener) {
2470 var geometries = object.geometries, i = -1, n = geometries.length;
2471 while (++i < n) d3_geo_streamGeometry(geometries[i], listener);
2475 function d3_geo_streamLine(coordinates, listener, closed) {
2476 var i = -1, n = coordinates.length - closed, coordinate;
2477 listener.lineStart();
2478 while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1], coordinate[2]);
2482 function d3_geo_streamPolygon(coordinates, listener) {
2483 var i = -1, n = coordinates.length;
2484 listener.polygonStart();
2485 while (++i < n) d3_geo_streamLine(coordinates[i], listener, 1);
2486 listener.polygonEnd();
2489 d3.geo.area = function(object) {
2491 d3.geo.stream(object, d3_geo_area);
2492 return d3_geo_areaSum;
2496 d3_geo_areaRingSum = new d3_adder;
2499 sphere: function() { d3_geo_areaSum += 4 * π; },
2504 // Only count area for polygon rings.
2505 polygonStart: function() {
2506 d3_geo_areaRingSum.reset();
2507 d3_geo_area.lineStart = d3_geo_areaRingStart;
2509 polygonEnd: function() {
2510 var area = 2 * d3_geo_areaRingSum;
2511 d3_geo_areaSum += area < 0 ? 4 * π + area : area;
2512 d3_geo_area.lineStart = d3_geo_area.lineEnd = d3_geo_area.point = d3_noop;
2516 function d3_geo_areaRingStart() {
2517 var λ00, φ00, λ0, cosφ0, sinφ0; // start point and previous point
2519 // For the first point, …
2520 d3_geo_area.point = function(λ, φ) {
2521 d3_geo_area.point = nextPoint;
2522 λ0 = (λ00 = λ) * d3_radians, cosφ0 = Math.cos(φ = (φ00 = φ) * d3_radians / 2 + π / 4), sinφ0 = Math.sin(φ);
2525 // For subsequent points, …
2526 function nextPoint(λ, φ) {
2528 φ = φ * d3_radians / 2 + π / 4; // half the angular distance from south pole
2530 // Spherical excess E for a spherical triangle with vertices: south pole,
2531 // previous point, current point. Uses a formula derived from Cagnoli’s
2532 // theorem. See Todhunter, Spherical Trig. (1871), Sec. 103, Eq. (2).
2537 u = cosφ0 * cosφ + k * Math.cos(dλ),
2538 v = k * Math.sin(dλ);
2539 d3_geo_areaRingSum.add(Math.atan2(v, u));
2541 // Advance the previous points.
2542 λ0 = λ, cosφ0 = cosφ, sinφ0 = sinφ;
2545 // For the last point, return to the start.
2546 d3_geo_area.lineEnd = function() {
2547 nextPoint(λ00, φ00);
2551 // cross and scale return new vectors,
2552 // whereas add and normalize operate in-place
2554 function d3_geo_cartesian(spherical) {
2555 var λ = spherical[0],
2565 function d3_geo_cartesianDot(a, b) {
2566 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
2569 function d3_geo_cartesianCross(a, b) {
2571 a[1] * b[2] - a[2] * b[1],
2572 a[2] * b[0] - a[0] * b[2],
2573 a[0] * b[1] - a[1] * b[0]
2577 function d3_geo_cartesianAdd(a, b) {
2583 function d3_geo_cartesianScale(vector, k) {
2591 function d3_geo_cartesianNormalize(d) {
2592 var l = Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
2598 function d3_geo_pointInPolygon(point, polygon) {
2599 var meridian = point[0],
2600 parallel = point[1],
2601 meridianNormal = [Math.sin(meridian), -Math.cos(meridian), 0],
2604 d3_geo_areaRingSum.reset();
2606 for (var i = 0, n = polygon.length; i < n; ++i) {
2607 var ring = polygon[i],
2610 var point0 = ring[0],
2612 φ0 = point0[1] / 2 + π / 4,
2613 sinφ0 = Math.sin(φ0),
2614 cosφ0 = Math.cos(φ0),
2621 φ = point[1] / 2 + π / 4,
2625 antimeridian = abs(dλ) > π,
2627 d3_geo_areaRingSum.add(Math.atan2(k * Math.sin(dλ), cosφ0 * cosφ + k * Math.cos(dλ)));
2629 polarAngle += antimeridian ? dλ + (dλ >= 0 ? τ : -τ): dλ;
2631 // Are the longitudes either side of the point's meridian, and are the
2632 // latitudes smaller than the parallel?
2633 if (antimeridian ^ λ0 >= meridian ^ λ >= meridian) {
2634 var arc = d3_geo_cartesianCross(d3_geo_cartesian(point0), d3_geo_cartesian(point));
2635 d3_geo_cartesianNormalize(arc);
2636 var intersection = d3_geo_cartesianCross(meridianNormal, arc);
2637 d3_geo_cartesianNormalize(intersection);
2638 var φarc = (antimeridian ^ dλ >= 0 ? -1 : 1) * d3_asin(intersection[2]);
2639 if (parallel > φarc || parallel === φarc && (arc[0] || arc[1])) {
2640 winding += antimeridian ^ dλ >= 0 ? 1 : -1;
2644 λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ, point0 = point;
2648 // First, determine whether the South pole is inside or outside:
2651 // * the polygon winds around it in a clockwise direction.
2652 // * the polygon does not (cumulatively) wind around it, but has a negative
2653 // (counter-clockwise) area.
2655 // Second, count the (signed) number of times a segment crosses a meridian
2656 // from the point to the South pole. If it is zero, then the point is the
2657 // same side as the South pole.
2659 return (polarAngle < -ε || polarAngle < ε && d3_geo_areaRingSum < 0) ^ (winding & 1);
2662 var d3_geo_clipAntimeridian = d3_geo_clip(
2664 d3_geo_clipAntimeridianLine,
2665 d3_geo_clipAntimeridianInterpolate,
2668 // Takes a line and cuts into visible segments. Return values:
2669 // 0: there were intersections or the line was empty.
2670 // 1: no intersections.
2671 // 2: there were intersections, and the first and last segments should be
2673 function d3_geo_clipAntimeridianLine(listener) {
2677 clean; // no intersections
2680 lineStart: function() {
2681 listener.lineStart();
2684 point: function(λ1, φ1) {
2685 var sλ1 = λ1 > 0 ? π : -π,
2687 if (abs(dλ - π) < ε) { // line crosses a pole
2688 listener.point(λ0, φ0 = (φ0 + φ1) / 2 > 0 ? halfπ : -halfπ);
2689 listener.point(sλ0, φ0);
2691 listener.lineStart();
2692 listener.point(sλ1, φ0);
2693 listener.point(λ1, φ0);
2695 } else if (sλ0 !== sλ1 && dλ >= π) { // line crosses antimeridian
2696 // handle degeneracies
2697 if (abs(λ0 - sλ0) < ε) λ0 -= sλ0 * ε;
2698 if (abs(λ1 - sλ1) < ε) λ1 -= sλ1 * ε;
2699 φ0 = d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1);
2700 listener.point(sλ0, φ0);
2702 listener.lineStart();
2703 listener.point(sλ1, φ0);
2706 listener.point(λ0 = λ1, φ0 = φ1);
2709 lineEnd: function() {
2713 // if there are intersections, we always rejoin the first and last segments.
2714 clean: function() { return 2 - clean; }
2718 function d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1) {
2721 sinλ0_λ1 = Math.sin(λ0 - λ1);
2722 return abs(sinλ0_λ1) > ε
2723 ? Math.atan((Math.sin(φ0) * (cosφ1 = Math.cos(φ1)) * Math.sin(λ1)
2724 - Math.sin(φ1) * (cosφ0 = Math.cos(φ0)) * Math.sin(λ0))
2725 / (cosφ0 * cosφ1 * sinλ0_λ1))
2729 function d3_geo_clipAntimeridianInterpolate(from, to, direction, listener) {
2732 φ = direction * halfπ;
2733 listener.point(-π, φ);
2734 listener.point( 0, φ);
2735 listener.point( π, φ);
2736 listener.point( π, 0);
2737 listener.point( π, -φ);
2738 listener.point( 0, -φ);
2739 listener.point(-π, -φ);
2740 listener.point(-π, 0);
2741 listener.point(-π, φ);
2742 } else if (abs(from[0] - to[0]) > ε) {
2743 var s = from[0] < to[0] ? π : -π;
2744 φ = direction * s / 2;
2745 listener.point(-s, φ);
2746 listener.point( 0, φ);
2747 listener.point( s, φ);
2749 listener.point(to[0], to[1]);
2753 function d3_geo_equirectangular(λ, φ) {
2757 (d3.geo.equirectangular = function() {
2758 return d3_geo_projection(d3_geo_equirectangular);
2759 }).raw = d3_geo_equirectangular.invert = d3_geo_equirectangular;
2761 d3.geo.rotation = function(rotate) {
2762 rotate = d3_geo_rotation(rotate[0] % 360 * d3_radians, rotate[1] * d3_radians, rotate.length > 2 ? rotate[2] * d3_radians : 0);
2764 function forward(coordinates) {
2765 coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians);
2766 return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates;
2769 forward.invert = function(coordinates) {
2770 coordinates = rotate.invert(coordinates[0] * d3_radians, coordinates[1] * d3_radians);
2771 return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates;
2777 function d3_geo_identityRotation(λ, φ) {
2778 return [λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ];
2781 d3_geo_identityRotation.invert = d3_geo_equirectangular;
2783 // Note: |δλ| must be < 2π
2784 function d3_geo_rotation(δλ, δφ, δγ) {
2785 return δλ ? (δφ || δγ ? d3_geo_compose(d3_geo_rotationλ(δλ), d3_geo_rotationφγ(δφ, δγ))
2786 : d3_geo_rotationλ(δλ))
2787 : (δφ || δγ ? d3_geo_rotationφγ(δφ, δγ)
2788 : d3_geo_identityRotation);
2791 function d3_geo_forwardRotationλ(δλ) {
2792 return function(λ, φ) {
2793 return λ += δλ, [λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ];
2797 function d3_geo_rotationλ(δλ) {
2798 var rotation = d3_geo_forwardRotationλ(δλ);
2799 rotation.invert = d3_geo_forwardRotationλ(-δλ);
2803 function d3_geo_rotationφγ(δφ, δγ) {
2804 var cosδφ = Math.cos(δφ),
2805 sinδφ = Math.sin(δφ),
2806 cosδγ = Math.cos(δγ),
2807 sinδγ = Math.sin(δγ);
2809 function rotation(λ, φ) {
2810 var cosφ = Math.cos(φ),
2811 x = Math.cos(λ) * cosφ,
2812 y = Math.sin(λ) * cosφ,
2814 k = z * cosδφ + x * sinδφ;
2816 Math.atan2(y * cosδγ - k * sinδγ, x * cosδφ - z * sinδφ),
2817 d3_asin(k * cosδγ + y * sinδγ)
2821 rotation.invert = function(λ, φ) {
2822 var cosφ = Math.cos(φ),
2823 x = Math.cos(λ) * cosφ,
2824 y = Math.sin(λ) * cosφ,
2826 k = z * cosδγ - y * sinδγ;
2828 Math.atan2(y * cosδγ + z * sinδγ, x * cosδφ + k * sinδφ),
2829 d3_asin(k * cosδφ - x * sinδφ)
2836 d3.geo.circle = function() {
2837 var origin = [0, 0],
2843 var center = typeof origin === "function" ? origin.apply(this, arguments) : origin,
2844 rotate = d3_geo_rotation(-center[0] * d3_radians, -center[1] * d3_radians, 0).invert,
2847 interpolate(null, null, 1, {
2848 point: function(x, y) {
2849 ring.push(x = rotate(x, y));
2850 x[0] *= d3_degrees, x[1] *= d3_degrees;
2854 return {type: "Polygon", coordinates: [ring]};
2857 circle.origin = function(x) {
2858 if (!arguments.length) return origin;
2863 circle.angle = function(x) {
2864 if (!arguments.length) return angle;
2865 interpolate = d3_geo_circleInterpolate((angle = +x) * d3_radians, precision * d3_radians);
2869 circle.precision = function(_) {
2870 if (!arguments.length) return precision;
2871 interpolate = d3_geo_circleInterpolate(angle * d3_radians, (precision = +_) * d3_radians);
2875 return circle.angle(90);
2878 // Interpolates along a circle centered at [0°, 0°], with a given radius and
2880 function d3_geo_circleInterpolate(radius, precision) {
2881 var cr = Math.cos(radius),
2882 sr = Math.sin(radius);
2883 return function(from, to, direction, listener) {
2884 var step = direction * precision;
2886 from = d3_geo_circleAngle(cr, from);
2887 to = d3_geo_circleAngle(cr, to);
2888 if (direction > 0 ? from < to: from > to) from += direction * τ;
2890 from = radius + direction * τ;
2891 to = radius - .5 * step;
2893 for (var point, t = from; direction > 0 ? t > to : t < to; t -= step) {
2894 listener.point((point = d3_geo_spherical([
2903 // Signed angle of a cartesian point relative to [cr, 0, 0].
2904 function d3_geo_circleAngle(cr, point) {
2905 var a = d3_geo_cartesian(point);
2907 d3_geo_cartesianNormalize(a);
2908 var angle = d3_acos(-a[1]);
2909 return ((-a[2] < 0 ? -angle : angle) + 2 * Math.PI - ε) % (2 * Math.PI);
2912 // Clip features against a small circle centered at [0°, 0°].
2913 function d3_geo_clipCircle(radius) {
2914 var cr = Math.cos(radius),
2915 smallRadius = cr > 0,
2916 notHemisphere = abs(cr) > ε, // TODO optimise for this common case
2917 interpolate = d3_geo_circleInterpolate(radius, 6 * d3_radians);
2919 return d3_geo_clip(visible, clipLine, interpolate, smallRadius ? [0, -radius] : [-π, radius - π]);
2921 function visible(λ, φ) {
2922 return Math.cos(λ) * Math.cos(φ) > cr;
2925 // Takes a line and cuts into visible segments. Return values used for
2926 // polygon clipping:
2927 // 0: there were intersections or the line was empty.
2928 // 1: no intersections.
2929 // 2: there were intersections, and the first and last segments should be
2931 function clipLine(listener) {
2932 var point0, // previous point
2933 c0, // code for previous point
2934 v0, // visibility of previous point
2935 v00, // visibility of first point
2936 clean; // no intersections
2938 lineStart: function() {
2942 point: function(λ, φ) {
2943 var point1 = [λ, φ],
2947 ? v ? 0 : code(λ, φ)
2948 : v ? code(λ + (λ < 0 ? π : -π), φ) : 0;
2949 if (!point0 && (v00 = v0 = v)) listener.lineStart();
2950 // Handle degeneracies.
2951 // TODO ignore if not clipping polygons.
2953 point2 = intersect(point0, point1);
2954 if (d3_geo_sphericalEqual(point0, point2) || d3_geo_sphericalEqual(point1, point2)) {
2957 v = visible(point1[0], point1[1]);
2964 listener.lineStart();
2965 point2 = intersect(point1, point0);
2966 listener.point(point2[0], point2[1]);
2969 point2 = intersect(point0, point1);
2970 listener.point(point2[0], point2[1]);
2974 } else if (notHemisphere && point0 && smallRadius ^ v) {
2976 // If the codes for two points are different, or are both zero,
2977 // and there this segment intersects with the small circle.
2978 if (!(c & c0) && (t = intersect(point1, point0, true))) {
2981 listener.lineStart();
2982 listener.point(t[0][0], t[0][1]);
2983 listener.point(t[1][0], t[1][1]);
2986 listener.point(t[1][0], t[1][1]);
2988 listener.lineStart();
2989 listener.point(t[0][0], t[0][1]);
2993 if (v && (!point0 || !d3_geo_sphericalEqual(point0, point1))) {
2994 listener.point(point1[0], point1[1]);
2996 point0 = point1, v0 = v, c0 = c;
2998 lineEnd: function() {
2999 if (v0) listener.lineEnd();
3002 // Rejoin first and last segments if there were intersections and the first
3003 // and last points were visible.
3004 clean: function() { return clean | ((v00 && v0) << 1); }
3008 // Intersects the great circle between a and b with the clip circle.
3009 function intersect(a, b, two) {
3010 var pa = d3_geo_cartesian(a),
3011 pb = d3_geo_cartesian(b);
3013 // We have two planes, n1.p = d1 and n2.p = d2.
3014 // Find intersection line p(t) = c1 n1 + c2 n2 + t (n1 ⨯ n2).
3015 var n1 = [1, 0, 0], // normal
3016 n2 = d3_geo_cartesianCross(pa, pb),
3017 n2n2 = d3_geo_cartesianDot(n2, n2),
3018 n1n2 = n2[0], // d3_geo_cartesianDot(n1, n2),
3019 determinant = n2n2 - n1n2 * n1n2;
3021 // Two polar points.
3022 if (!determinant) return !two && a;
3024 var c1 = cr * n2n2 / determinant,
3025 c2 = -cr * n1n2 / determinant,
3026 n1xn2 = d3_geo_cartesianCross(n1, n2),
3027 A = d3_geo_cartesianScale(n1, c1),
3028 B = d3_geo_cartesianScale(n2, c2);
3029 d3_geo_cartesianAdd(A, B);
3031 // Solve |p(t)|^2 = 1.
3033 w = d3_geo_cartesianDot(A, u),
3034 uu = d3_geo_cartesianDot(u, u),
3035 t2 = w * w - uu * (d3_geo_cartesianDot(A, A) - 1);
3039 var t = Math.sqrt(t2),
3040 q = d3_geo_cartesianScale(u, (-w - t) / uu);
3041 d3_geo_cartesianAdd(q, A);
3042 q = d3_geo_spherical(q);
3045 // Two intersection points.
3051 if (λ1 < λ0) z = λ0, λ0 = λ1, λ1 = z;
3053 polar = abs(δλ - π) < ε,
3054 meridian = polar || δλ < ε;
3056 if (!polar && φ1 < φ0) z = φ0, φ0 = φ1, φ1 = z;
3058 // Check that the first point is between a and b.
3061 ? φ0 + φ1 > 0 ^ q[1] < (abs(q[0] - λ0) < ε ? φ0 : φ1)
3062 : φ0 <= q[1] && q[1] <= φ1
3063 : δλ > π ^ (λ0 <= q[0] && q[0] <= λ1)) {
3064 var q1 = d3_geo_cartesianScale(u, (-w + t) / uu);
3065 d3_geo_cartesianAdd(q1, A);
3066 return [q, d3_geo_spherical(q1)];
3070 // Generates a 4-bit vector representing the location of a point relative to
3071 // the small circle's bounding box.
3072 function code(λ, φ) {
3073 var r = smallRadius ? radius : π - radius,
3075 if (λ < -r) code |= 1; // left
3076 else if (λ > r) code |= 2; // right
3077 if (φ < -r) code |= 4; // below
3078 else if (φ > r) code |= 8; // above
3083 // Liang–Barsky line clipping.
3084 function d3_geom_clipLine(x0, y0, x1, y1) {
3085 return function(line) {
3099 if (!dx && r > 0) return;
3104 } else if (dx > 0) {
3110 if (!dx && r < 0) return;
3115 } else if (dx > 0) {
3121 if (!dy && r > 0) return;
3126 } else if (dy > 0) {
3132 if (!dy && r < 0) return;
3137 } else if (dy > 0) {
3142 if (t0 > 0) line.a = {x: ax + t0 * dx, y: ay + t0 * dy};
3143 if (t1 < 1) line.b = {x: ax + t1 * dx, y: ay + t1 * dy};
3148 var d3_geo_clipExtentMAX = 1e9;
3150 d3.geo.clipExtent = function() {
3155 stream: function(output) {
3156 if (stream) stream.valid = false;
3157 stream = clip(output);
3158 stream.valid = true; // allow caching by d3.geo.path
3161 extent: function(_) {
3162 if (!arguments.length) return [[x0, y0], [x1, y1]];
3163 clip = d3_geo_clipExtent(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]);
3164 if (stream) stream.valid = false, stream = null;
3168 return clipExtent.extent([[0, 0], [960, 500]]);
3171 function d3_geo_clipExtent(x0, y0, x1, y1) {
3172 return function(listener) {
3173 var listener_ = listener,
3174 bufferListener = d3_geo_clipBufferListener(),
3175 clipLine = d3_geom_clipLine(x0, y0, x1, y1),
3182 lineStart: lineStart,
3184 polygonStart: function() {
3185 listener = bufferListener;
3190 polygonEnd: function() {
3191 listener = listener_;
3192 segments = d3.merge(segments);
3193 var clipStartInside = insidePolygon([x0, y1]),
3194 inside = clean && clipStartInside,
3195 visible = segments.length;
3196 if (inside || visible) {
3197 listener.polygonStart();
3199 listener.lineStart();
3200 interpolate(null, null, 1, listener);
3204 d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener);
3206 listener.polygonEnd();
3208 segments = polygon = ring = null;
3212 function insidePolygon(p) {
3213 var wn = 0, // the winding number counter
3217 for (var i = 0; i < n; ++i) {
3218 for (var j = 1, v = polygon[i], m = v.length, a = v[0], b; j < m; ++j) {
3221 if (b[1] > y && isLeft(a, b, p) > 0) ++wn;
3223 if (b[1] <= y && isLeft(a, b, p) < 0) --wn;
3231 function isLeft(a, b, c) {
3232 return (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]);
3235 function interpolate(from, to, direction, listener) {
3238 (a = corner(from, direction)) !== (a1 = corner(to, direction)) ||
3239 comparePoints(from, to) < 0 ^ direction > 0) {
3241 listener.point(a === 0 || a === 3 ? x0 : x1, a > 1 ? y1 : y0);
3242 } while ((a = (a + direction + 4) % 4) !== a1);
3244 listener.point(to[0], to[1]);
3248 function pointVisible(x, y) {
3249 return x0 <= x && x <= x1 && y0 <= y && y <= y1;
3252 function point(x, y) {
3253 if (pointVisible(x, y)) listener.point(x, y);
3256 var x__, y__, v__, // first point
3257 x_, y_, v_, // previous point
3261 function lineStart() {
3262 clip.point = linePoint;
3263 if (polygon) polygon.push(ring = []);
3269 function lineEnd() {
3270 // TODO rather than special-case polygons, simply handle them separately.
3271 // Ideally, coincident intersection points should be jittered to avoid
3274 linePoint(x__, y__);
3275 if (v__ && v_) bufferListener.rejoin();
3276 segments.push(bufferListener.buffer());
3279 if (v_) listener.lineEnd();
3282 function linePoint(x, y) {
3283 x = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, x));
3284 y = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, y));
3285 var v = pointVisible(x, y);
3286 if (polygon) ring.push([x, y]);
3288 x__ = x, y__ = y, v__ = v;
3291 listener.lineStart();
3292 listener.point(x, y);
3295 if (v && v_) listener.point(x, y);
3297 var l = {a: {x: x_, y: y_}, b: {x: x, y: y}};
3300 listener.lineStart();
3301 listener.point(l.a.x, l.a.y);
3303 listener.point(l.b.x, l.b.y);
3304 if (!v) listener.lineEnd();
3307 listener.lineStart();
3308 listener.point(x, y);
3313 x_ = x, y_ = y, v_ = v;
3319 function corner(p, direction) {
3320 return abs(p[0] - x0) < ε ? direction > 0 ? 0 : 3
3321 : abs(p[0] - x1) < ε ? direction > 0 ? 2 : 1
3322 : abs(p[1] - y0) < ε ? direction > 0 ? 1 : 0
3323 : direction > 0 ? 3 : 2; // abs(p[1] - y1) < ε
3326 function compare(a, b) {
3327 return comparePoints(a.x, b.x);
3330 function comparePoints(a, b) {
3331 var ca = corner(a, 1),
3333 return ca !== cb ? ca - cb
3334 : ca === 0 ? b[1] - a[1]
3335 : ca === 1 ? a[0] - b[0]
3336 : ca === 2 ? a[1] - b[1]
3340 function d3_geo_compose(a, b) {
3342 function compose(x, y) {
3343 return x = a(x, y), b(x[0], x[1]);
3346 if (a.invert && b.invert) compose.invert = function(x, y) {
3347 return x = b.invert(x, y), x && a.invert(x[0], x[1]);
3353 function d3_geo_conic(projectAt) {
3356 m = d3_geo_projectionMutator(projectAt),
3359 p.parallels = function(_) {
3360 if (!arguments.length) return [φ0 / π * 180, φ1 / π * 180];
3361 return m(φ0 = _[0] * π / 180, φ1 = _[1] * π / 180);
3367 function d3_geo_conicEqualArea(φ0, φ1) {
3368 var sinφ0 = Math.sin(φ0),
3369 n = (sinφ0 + Math.sin(φ1)) / 2,
3370 C = 1 + sinφ0 * (2 * n - sinφ0),
3371 ρ0 = Math.sqrt(C) / n;
3373 function forward(λ, φ) {
3374 var ρ = Math.sqrt(C - 2 * n * Math.sin(φ)) / n;
3376 ρ * Math.sin(λ *= n),
3377 ρ0 - ρ * Math.cos(λ)
3381 forward.invert = function(x, y) {
3384 Math.atan2(x, ρ0_y) / n,
3385 d3_asin((C - (x * x + ρ0_y * ρ0_y) * n * n) / (2 * n))
3392 (d3.geo.conicEqualArea = function() {
3393 return d3_geo_conic(d3_geo_conicEqualArea);
3394 }).raw = d3_geo_conicEqualArea;
3397 d3.geo.albers = function() {
3398 return d3.geo.conicEqualArea()
3400 .center([-.6, 38.7])
3401 .parallels([29.5, 45.5])
3405 // A composite projection for the United States, configured by default for
3406 // 960×500. Also works quite well at 960×600 with scale 1285. The set of
3407 // standard parallels for each region comes from USGS, which is published here:
3408 // http://egsc.usgs.gov/isb/pubs/MapProjections/projections.html#albers
3409 d3.geo.albersUsa = function() {
3410 var lower48 = d3.geo.albers();
3413 var alaska = d3.geo.conicEqualArea()
3416 .parallels([55, 65]);
3419 var hawaii = d3.geo.conicEqualArea()
3422 .parallels([8, 18]);
3425 pointStream = {point: function(x, y) { point = [x, y]; }},
3430 function albersUsa(coordinates) {
3431 var x = coordinates[0], y = coordinates[1];
3433 (lower48Point(x, y), point)
3434 || (alaskaPoint(x, y), point)
3435 || hawaiiPoint(x, y);
3439 albersUsa.invert = function(coordinates) {
3440 var k = lower48.scale(),
3441 t = lower48.translate(),
3442 x = (coordinates[0] - t[0]) / k,
3443 y = (coordinates[1] - t[1]) / k;
3444 return (y >= .120 && y < .234 && x >= -.425 && x < -.214 ? alaska
3445 : y >= .166 && y < .234 && x >= -.214 && x < -.115 ? hawaii
3446 : lower48).invert(coordinates);
3449 // A naïve multi-projection stream.
3450 // The projections must have mutually exclusive clip regions on the sphere,
3451 // as this will avoid emitting interleaving lines and polygons.
3452 albersUsa.stream = function(stream) {
3453 var lower48Stream = lower48.stream(stream),
3454 alaskaStream = alaska.stream(stream),
3455 hawaiiStream = hawaii.stream(stream);
3457 point: function(x, y) {
3458 lower48Stream.point(x, y);
3459 alaskaStream.point(x, y);
3460 hawaiiStream.point(x, y);
3462 sphere: function() {
3463 lower48Stream.sphere();
3464 alaskaStream.sphere();
3465 hawaiiStream.sphere();
3467 lineStart: function() {
3468 lower48Stream.lineStart();
3469 alaskaStream.lineStart();
3470 hawaiiStream.lineStart();
3472 lineEnd: function() {
3473 lower48Stream.lineEnd();
3474 alaskaStream.lineEnd();
3475 hawaiiStream.lineEnd();
3477 polygonStart: function() {
3478 lower48Stream.polygonStart();
3479 alaskaStream.polygonStart();
3480 hawaiiStream.polygonStart();
3482 polygonEnd: function() {
3483 lower48Stream.polygonEnd();
3484 alaskaStream.polygonEnd();
3485 hawaiiStream.polygonEnd();
3490 albersUsa.precision = function(_) {
3491 if (!arguments.length) return lower48.precision();
3492 lower48.precision(_);
3493 alaska.precision(_);
3494 hawaii.precision(_);
3498 albersUsa.scale = function(_) {
3499 if (!arguments.length) return lower48.scale();
3501 alaska.scale(_ * .35);
3503 return albersUsa.translate(lower48.translate());
3506 albersUsa.translate = function(_) {
3507 if (!arguments.length) return lower48.translate();
3508 var k = lower48.scale(), x = +_[0], y = +_[1];
3510 lower48Point = lower48
3512 .clipExtent([[x - .455 * k, y - .238 * k], [x + .455 * k, y + .238 * k]])
3513 .stream(pointStream).point;
3515 alaskaPoint = alaska
3516 .translate([x - .307 * k, y + .201 * k])
3517 .clipExtent([[x - .425 * k + ε, y + .120 * k + ε], [x - .214 * k - ε, y + .234 * k - ε]])
3518 .stream(pointStream).point;
3520 hawaiiPoint = hawaii
3521 .translate([x - .205 * k, y + .212 * k])
3522 .clipExtent([[x - .214 * k + ε, y + .166 * k + ε], [x - .115 * k - ε, y + .234 * k - ε]])
3523 .stream(pointStream).point;
3528 return albersUsa.scale(1070);
3531 d3.geo.bounds = (function() {
3532 var λ0, φ0, λ1, φ1, // bounds
3533 λ_, // previous λ-coordinate
3534 λ__, φ__, // first point
3535 p0, // previous 3D point
3542 lineStart: lineStart,
3545 polygonStart: function() {
3546 bound.point = ringPoint;
3547 bound.lineStart = ringStart;
3548 bound.lineEnd = ringEnd;
3550 d3_geo_area.polygonStart();
3552 polygonEnd: function() {
3553 d3_geo_area.polygonEnd();
3554 bound.point = point;
3555 bound.lineStart = lineStart;
3556 bound.lineEnd = lineEnd;
3557 if (d3_geo_areaRingSum < 0) λ0 = -(λ1 = 180), φ0 = -(φ1 = 90);
3558 else if (dλSum > ε) φ1 = 90;
3559 else if (dλSum < -ε) φ0 = -90;
3560 range[0] = λ0, range[1] = λ1;
3564 function point(λ, φ) {
3565 ranges.push(range = [λ0 = λ, λ1 = λ]);
3570 function linePoint(λ, φ) {
3571 var p = d3_geo_cartesian([λ * d3_radians, φ * d3_radians]);
3573 var normal = d3_geo_cartesianCross(p0, p),
3574 equatorial = [normal[1], -normal[0], 0],
3575 inflection = d3_geo_cartesianCross(equatorial, normal);
3576 d3_geo_cartesianNormalize(inflection);
3577 inflection = d3_geo_spherical(inflection);
3579 s = dλ > 0 ? 1 : -1,
3580 λi = inflection[0] * d3_degrees * s,
3581 antimeridian = abs(dλ) > 180;
3582 if (antimeridian ^ (s * λ_ < λi && λi < s * λ)) {
3583 var φi = inflection[1] * d3_degrees;
3584 if (φi > φ1) φ1 = φi;
3585 } else if (λi = (λi + 360) % 360 - 180, antimeridian ^ (s * λ_ < λi && λi < s * λ)) {
3586 var φi = -inflection[1] * d3_degrees;
3587 if (φi < φ0) φ0 = φi;
3594 if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ;
3596 if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ;
3604 if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ;
3606 if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ;
3616 function lineStart() { bound.point = linePoint; }
3617 function lineEnd() {
3618 range[0] = λ0, range[1] = λ1;
3619 bound.point = point;
3623 function ringPoint(λ, φ) {
3626 dλSum += abs(dλ) > 180 ? dλ + (dλ > 0 ? 360 : -360) : dλ;
3627 } else λ__ = λ, φ__ = φ;
3628 d3_geo_area.point(λ, φ);
3632 function ringStart() {
3633 d3_geo_area.lineStart();
3636 function ringEnd() {
3637 ringPoint(λ__, φ__);
3638 d3_geo_area.lineEnd();
3639 if (abs(dλSum) > ε) λ0 = -(λ1 = 180);
3640 range[0] = λ0, range[1] = λ1;
3644 // Finds the left-right distance between two longitudes.
3645 // This is almost the same as (λ1 - λ0 + 360°) % 360°, except that we want
3646 // the distance between ±180° to be 360°.
3647 function angle(λ0, λ1) { return (λ1 -= λ0) < 0 ? λ1 + 360 : λ1; }
3649 function compareRanges(a, b) { return a[0] - b[0]; }
3651 function withinRange(x, range) {
3652 return range[0] <= range[1] ? range[0] <= x && x <= range[1] : x < range[0] || range[1] < x;
3655 return function(feature) {
3656 φ1 = λ1 = -(λ0 = φ0 = Infinity);
3659 d3.geo.stream(feature, bound);
3661 var n = ranges.length;
3663 // First, sort ranges by their minimum longitudes.
3664 ranges.sort(compareRanges);
3666 // Then, merge any ranges that overlap.
3667 for (var i = 1, a = ranges[0], b, merged = [a]; i < n; ++i) {
3669 if (withinRange(b[0], a) || withinRange(b[1], a)) {
3670 if (angle(a[0], b[1]) > angle(a[0], a[1])) a[1] = b[1];
3671 if (angle(b[0], a[1]) > angle(a[0], a[1])) a[0] = b[0];
3677 // Finally, find the largest gap between the merged ranges.
3678 // The final bounding box will be the inverse of this gap.
3679 var best = -Infinity, dλ;
3680 for (var n = merged.length - 1, i = 0, a = merged[n], b; i <= n; a = b, ++i) {
3682 if ((dλ = angle(a[1], b[0])) > best) best = dλ, λ0 = b[0], λ1 = a[1];
3685 ranges = range = null;
3687 return λ0 === Infinity || φ0 === Infinity
3688 ? [[NaN, NaN], [NaN, NaN]]
3689 : [[λ0, φ0], [λ1, φ1]];
3693 d3.geo.centroid = function(object) {
3694 d3_geo_centroidW0 = d3_geo_centroidW1 =
3695 d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 =
3696 d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 =
3697 d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0;
3698 d3.geo.stream(object, d3_geo_centroid);
3700 var x = d3_geo_centroidX2,
3701 y = d3_geo_centroidY2,
3702 z = d3_geo_centroidZ2,
3703 m = x * x + y * y + z * z;
3705 // If the area-weighted centroid is undefined, fall back to length-weighted centroid.
3707 x = d3_geo_centroidX1, y = d3_geo_centroidY1, z = d3_geo_centroidZ1;
3708 // If the feature has zero length, fall back to arithmetic mean of point vectors.
3709 if (d3_geo_centroidW1 < ε) x = d3_geo_centroidX0, y = d3_geo_centroidY0, z = d3_geo_centroidZ0;
3710 m = x * x + y * y + z * z;
3711 // If the feature still has an undefined centroid, then return.
3712 if (m < ε2) return [NaN, NaN];
3715 return [Math.atan2(y, x) * d3_degrees, d3_asin(z / Math.sqrt(m)) * d3_degrees];
3718 var d3_geo_centroidW0,
3730 var d3_geo_centroid = {
3732 point: d3_geo_centroidPoint,
3733 lineStart: d3_geo_centroidLineStart,
3734 lineEnd: d3_geo_centroidLineEnd,
3735 polygonStart: function() {
3736 d3_geo_centroid.lineStart = d3_geo_centroidRingStart;
3738 polygonEnd: function() {
3739 d3_geo_centroid.lineStart = d3_geo_centroidLineStart;
3743 // Arithmetic mean of Cartesian vectors.
3744 function d3_geo_centroidPoint(λ, φ) {
3746 var cosφ = Math.cos(φ *= d3_radians);
3747 d3_geo_centroidPointXYZ(cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ));
3750 function d3_geo_centroidPointXYZ(x, y, z) {
3751 ++d3_geo_centroidW0;
3752 d3_geo_centroidX0 += (x - d3_geo_centroidX0) / d3_geo_centroidW0;
3753 d3_geo_centroidY0 += (y - d3_geo_centroidY0) / d3_geo_centroidW0;
3754 d3_geo_centroidZ0 += (z - d3_geo_centroidZ0) / d3_geo_centroidW0;
3757 function d3_geo_centroidLineStart() {
3758 var x0, y0, z0; // previous point
3760 d3_geo_centroid.point = function(λ, φ) {
3762 var cosφ = Math.cos(φ *= d3_radians);
3763 x0 = cosφ * Math.cos(λ);
3764 y0 = cosφ * Math.sin(λ);
3766 d3_geo_centroid.point = nextPoint;
3767 d3_geo_centroidPointXYZ(x0, y0, z0);
3770 function nextPoint(λ, φ) {
3772 var cosφ = Math.cos(φ *= d3_radians),
3773 x = cosφ * Math.cos(λ),
3774 y = cosφ * Math.sin(λ),
3777 Math.sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w),
3778 x0 * x + y0 * y + z0 * z);
3779 d3_geo_centroidW1 += w;
3780 d3_geo_centroidX1 += w * (x0 + (x0 = x));
3781 d3_geo_centroidY1 += w * (y0 + (y0 = y));
3782 d3_geo_centroidZ1 += w * (z0 + (z0 = z));
3783 d3_geo_centroidPointXYZ(x0, y0, z0);
3787 function d3_geo_centroidLineEnd() {
3788 d3_geo_centroid.point = d3_geo_centroidPoint;
3791 // See J. E. Brock, The Inertia Tensor for a Spherical Triangle,
3792 // J. Applied Mechanics 42, 239 (1975).
3793 function d3_geo_centroidRingStart() {
3794 var λ00, φ00, // first point
3795 x0, y0, z0; // previous point
3797 d3_geo_centroid.point = function(λ, φ) {
3799 d3_geo_centroid.point = nextPoint;
3801 var cosφ = Math.cos(φ *= d3_radians);
3802 x0 = cosφ * Math.cos(λ);
3803 y0 = cosφ * Math.sin(λ);
3805 d3_geo_centroidPointXYZ(x0, y0, z0);
3808 d3_geo_centroid.lineEnd = function() {
3809 nextPoint(λ00, φ00);
3810 d3_geo_centroid.lineEnd = d3_geo_centroidLineEnd;
3811 d3_geo_centroid.point = d3_geo_centroidPoint;
3814 function nextPoint(λ, φ) {
3816 var cosφ = Math.cos(φ *= d3_radians),
3817 x = cosφ * Math.cos(λ),
3818 y = cosφ * Math.sin(λ),
3820 cx = y0 * z - z0 * y,
3821 cy = z0 * x - x0 * z,
3822 cz = x0 * y - y0 * x,
3823 m = Math.sqrt(cx * cx + cy * cy + cz * cz),
3824 u = x0 * x + y0 * y + z0 * z,
3825 v = m && -d3_acos(u) / m, // area weight
3826 w = Math.atan2(m, u); // line weight
3827 d3_geo_centroidX2 += v * cx;
3828 d3_geo_centroidY2 += v * cy;
3829 d3_geo_centroidZ2 += v * cz;
3830 d3_geo_centroidW1 += w;
3831 d3_geo_centroidX1 += w * (x0 + (x0 = x));
3832 d3_geo_centroidY1 += w * (y0 + (y0 = y));
3833 d3_geo_centroidZ1 += w * (z0 + (z0 = z));
3834 d3_geo_centroidPointXYZ(x0, y0, z0);
3838 // TODO Unify this code with d3.geom.polygon area?
3840 var d3_geo_pathAreaSum, d3_geo_pathAreaPolygon, d3_geo_pathArea = {
3845 // Only count area for polygon rings.
3846 polygonStart: function() {
3847 d3_geo_pathAreaPolygon = 0;
3848 d3_geo_pathArea.lineStart = d3_geo_pathAreaRingStart;
3850 polygonEnd: function() {
3851 d3_geo_pathArea.lineStart = d3_geo_pathArea.lineEnd = d3_geo_pathArea.point = d3_noop;
3852 d3_geo_pathAreaSum += abs(d3_geo_pathAreaPolygon / 2);
3856 function d3_geo_pathAreaRingStart() {
3857 var x00, y00, x0, y0;
3859 // For the first point, …
3860 d3_geo_pathArea.point = function(x, y) {
3861 d3_geo_pathArea.point = nextPoint;
3862 x00 = x0 = x, y00 = y0 = y;
3865 // For subsequent points, …
3866 function nextPoint(x, y) {
3867 d3_geo_pathAreaPolygon += y0 * x - x0 * y;
3871 // For the last point, return to the start.
3872 d3_geo_pathArea.lineEnd = function() {
3873 nextPoint(x00, y00);
3877 var d3_geo_pathBoundsX0,
3878 d3_geo_pathBoundsY0,
3879 d3_geo_pathBoundsX1,
3880 d3_geo_pathBoundsY1;
3882 var d3_geo_pathBounds = {
3883 point: d3_geo_pathBoundsPoint,
3886 polygonStart: d3_noop,
3890 function d3_geo_pathBoundsPoint(x, y) {
3891 if (x < d3_geo_pathBoundsX0) d3_geo_pathBoundsX0 = x;
3892 if (x > d3_geo_pathBoundsX1) d3_geo_pathBoundsX1 = x;
3893 if (y < d3_geo_pathBoundsY0) d3_geo_pathBoundsY0 = y;
3894 if (y > d3_geo_pathBoundsY1) d3_geo_pathBoundsY1 = y;
3896 function d3_geo_pathBuffer() {
3897 var pointCircle = d3_geo_pathBufferCircle(4.5),
3903 // While inside a line, override point to moveTo then lineTo.
3904 lineStart: function() { stream.point = pointLineStart; },
3907 // While inside a polygon, override lineEnd to closePath.
3908 polygonStart: function() { stream.lineEnd = lineEndPolygon; },
3909 polygonEnd: function() { stream.lineEnd = lineEnd; stream.point = point; },
3911 pointRadius: function(_) {
3912 pointCircle = d3_geo_pathBufferCircle(_);
3916 result: function() {
3917 if (buffer.length) {
3918 var result = buffer.join("");
3925 function point(x, y) {
3926 buffer.push("M", x, ",", y, pointCircle);
3929 function pointLineStart(x, y) {
3930 buffer.push("M", x, ",", y);
3931 stream.point = pointLine;
3934 function pointLine(x, y) {
3935 buffer.push("L", x, ",", y);
3938 function lineEnd() {
3939 stream.point = point;
3942 function lineEndPolygon() {
3949 function d3_geo_pathBufferCircle(radius) {
3950 return "m0," + radius
3951 + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius
3952 + "a" + radius + "," + radius + " 0 1,1 0," + 2 * radius
3956 // TODO Unify this code with d3.geom.polygon centroid?
3957 // TODO Enforce positive area for exterior, negative area for interior?
3959 var d3_geo_pathCentroid = {
3960 point: d3_geo_pathCentroidPoint,
3962 // For lines, weight by length.
3963 lineStart: d3_geo_pathCentroidLineStart,
3964 lineEnd: d3_geo_pathCentroidLineEnd,
3966 // For polygons, weight by area.
3967 polygonStart: function() {
3968 d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidRingStart;
3970 polygonEnd: function() {
3971 d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint;
3972 d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidLineStart;
3973 d3_geo_pathCentroid.lineEnd = d3_geo_pathCentroidLineEnd;
3977 function d3_geo_pathCentroidPoint(x, y) {
3978 d3_geo_centroidX0 += x;
3979 d3_geo_centroidY0 += y;
3980 ++d3_geo_centroidZ0;
3983 function d3_geo_pathCentroidLineStart() {
3986 d3_geo_pathCentroid.point = function(x, y) {
3987 d3_geo_pathCentroid.point = nextPoint;
3988 d3_geo_pathCentroidPoint(x0 = x, y0 = y);
3991 function nextPoint(x, y) {
3992 var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy);
3993 d3_geo_centroidX1 += z * (x0 + x) / 2;
3994 d3_geo_centroidY1 += z * (y0 + y) / 2;
3995 d3_geo_centroidZ1 += z;
3996 d3_geo_pathCentroidPoint(x0 = x, y0 = y);
4000 function d3_geo_pathCentroidLineEnd() {
4001 d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint;
4004 function d3_geo_pathCentroidRingStart() {
4005 var x00, y00, x0, y0;
4007 // For the first point, …
4008 d3_geo_pathCentroid.point = function(x, y) {
4009 d3_geo_pathCentroid.point = nextPoint;
4010 d3_geo_pathCentroidPoint(x00 = x0 = x, y00 = y0 = y);
4013 // For subsequent points, …
4014 function nextPoint(x, y) {
4015 var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy);
4016 d3_geo_centroidX1 += z * (x0 + x) / 2;
4017 d3_geo_centroidY1 += z * (y0 + y) / 2;
4018 d3_geo_centroidZ1 += z;
4020 z = y0 * x - x0 * y;
4021 d3_geo_centroidX2 += z * (x0 + x);
4022 d3_geo_centroidY2 += z * (y0 + y);
4023 d3_geo_centroidZ2 += z * 3;
4024 d3_geo_pathCentroidPoint(x0 = x, y0 = y);
4027 // For the last point, return to the start.
4028 d3_geo_pathCentroid.lineEnd = function() {
4029 nextPoint(x00, y00);
4033 function d3_geo_pathContext(context) {
4034 var pointRadius = 4.5;
4039 // While inside a line, override point to moveTo then lineTo.
4040 lineStart: function() { stream.point = pointLineStart; },
4043 // While inside a polygon, override lineEnd to closePath.
4044 polygonStart: function() { stream.lineEnd = lineEndPolygon; },
4045 polygonEnd: function() { stream.lineEnd = lineEnd; stream.point = point; },
4047 pointRadius: function(_) {
4055 function point(x, y) {
4056 context.moveTo(x, y);
4057 context.arc(x, y, pointRadius, 0, τ);
4060 function pointLineStart(x, y) {
4061 context.moveTo(x, y);
4062 stream.point = pointLine;
4065 function pointLine(x, y) {
4066 context.lineTo(x, y);
4069 function lineEnd() {
4070 stream.point = point;
4073 function lineEndPolygon() {
4074 context.closePath();
4080 function d3_geo_resample(project) {
4081 var δ2 = .5, // precision, px²
4082 cosMinDistance = Math.cos(30 * d3_radians), // cos(minimum angular distance)
4085 function resample(stream) {
4086 return (maxDepth ? resampleRecursive : resampleNone)(stream);
4089 function resampleNone(stream) {
4090 return d3_geo_transformPoint(stream, function(x, y) {
4092 stream.point(x[0], x[1]);
4096 function resampleRecursive(stream) {
4097 var λ00, φ00, x00, y00, a00, b00, c00, // first point
4098 λ0, x0, y0, a0, b0, c0; // previous point
4102 lineStart: lineStart,
4104 polygonStart: function() { stream.polygonStart(); resample.lineStart = ringStart; },
4105 polygonEnd: function() { stream.polygonEnd(); resample.lineStart = lineStart; }
4108 function point(x, y) {
4110 stream.point(x[0], x[1]);
4113 function lineStart() {
4115 resample.point = linePoint;
4119 function linePoint(λ, φ) {
4120 var c = d3_geo_cartesian([λ, φ]), p = project(λ, φ);
4121 resampleLineTo(x0, y0, λ0, a0, b0, c0, x0 = p[0], y0 = p[1], λ0 = λ, a0 = c[0], b0 = c[1], c0 = c[2], maxDepth, stream);
4122 stream.point(x0, y0);
4125 function lineEnd() {
4126 resample.point = point;
4130 function ringStart() {
4132 resample.point = ringPoint;
4133 resample.lineEnd = ringEnd;
4136 function ringPoint(λ, φ) {
4137 linePoint(λ00 = λ, φ00 = φ), x00 = x0, y00 = y0, a00 = a0, b00 = b0, c00 = c0;
4138 resample.point = linePoint;
4141 function ringEnd() {
4142 resampleLineTo(x0, y0, λ0, a0, b0, c0, x00, y00, λ00, a00, b00, c00, maxDepth, stream);
4143 resample.lineEnd = lineEnd;
4150 function resampleLineTo(x0, y0, λ0, a0, b0, c0, x1, y1, λ1, a1, b1, c1, depth, stream) {
4153 d2 = dx * dx + dy * dy;
4154 if (d2 > 4 * δ2 && depth--) {
4158 m = Math.sqrt(a * a + b * b + c * c),
4159 φ2 = Math.asin(c /= m),
4160 λ2 = abs(abs(c) - 1) < ε || abs(λ0 - λ1) < ε ? (λ0 + λ1) / 2 : Math.atan2(b, a),
4161 p = project(λ2, φ2),
4166 dz = dy * dx2 - dx * dy2;
4167 if (dz * dz / d2 > δ2 // perpendicular projected distance
4168 || abs((dx * dx2 + dy * dy2) / d2 - .5) > .3 // midpoint close to an end
4169 || a0 * a1 + b0 * b1 + c0 * c1 < cosMinDistance) { // angular distance
4170 resampleLineTo(x0, y0, λ0, a0, b0, c0, x2, y2, λ2, a /= m, b /= m, c, depth, stream);
4171 stream.point(x2, y2);
4172 resampleLineTo(x2, y2, λ2, a, b, c, x1, y1, λ1, a1, b1, c1, depth, stream);
4177 resample.precision = function(_) {
4178 if (!arguments.length) return Math.sqrt(δ2);
4179 maxDepth = (δ2 = _ * _) > 0 && 16;
4186 d3.geo.path = function() {
4187 var pointRadius = 4.5,
4194 function path(object) {
4196 if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments));
4197 if (!cacheStream || !cacheStream.valid) cacheStream = projectStream(contextStream);
4198 d3.geo.stream(object, cacheStream);
4200 return contextStream.result();
4203 path.area = function(object) {
4204 d3_geo_pathAreaSum = 0;
4205 d3.geo.stream(object, projectStream(d3_geo_pathArea));
4206 return d3_geo_pathAreaSum;
4209 path.centroid = function(object) {
4210 d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 =
4211 d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 =
4212 d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0;
4213 d3.geo.stream(object, projectStream(d3_geo_pathCentroid));
4214 return d3_geo_centroidZ2 ? [d3_geo_centroidX2 / d3_geo_centroidZ2, d3_geo_centroidY2 / d3_geo_centroidZ2]
4215 : d3_geo_centroidZ1 ? [d3_geo_centroidX1 / d3_geo_centroidZ1, d3_geo_centroidY1 / d3_geo_centroidZ1]
4216 : d3_geo_centroidZ0 ? [d3_geo_centroidX0 / d3_geo_centroidZ0, d3_geo_centroidY0 / d3_geo_centroidZ0]
4220 path.bounds = function(object) {
4221 d3_geo_pathBoundsX1 = d3_geo_pathBoundsY1 = -(d3_geo_pathBoundsX0 = d3_geo_pathBoundsY0 = Infinity);
4222 d3.geo.stream(object, projectStream(d3_geo_pathBounds));
4223 return [[d3_geo_pathBoundsX0, d3_geo_pathBoundsY0], [d3_geo_pathBoundsX1, d3_geo_pathBoundsY1]];
4226 path.projection = function(_) {
4227 if (!arguments.length) return projection;
4228 projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity;
4232 path.context = function(_) {
4233 if (!arguments.length) return context;
4234 contextStream = (context = _) == null ? new d3_geo_pathBuffer : new d3_geo_pathContext(_);
4235 if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius);
4239 path.pointRadius = function(_) {
4240 if (!arguments.length) return pointRadius;
4241 pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_);
4250 return path.projection(d3.geo.albersUsa()).context(null);
4253 function d3_geo_pathProjectStream(project) {
4254 var resample = d3_geo_resample(function(x, y) { return project([x * d3_degrees, y * d3_degrees]); });
4255 return function(stream) { return d3_geo_projectionRadians(resample(stream)); };
4258 d3.geo.transform = function(methods) {
4260 stream: function(stream) {
4261 var transform = new d3_geo_transform(stream);
4262 for (var k in methods) transform[k] = methods[k];
4268 function d3_geo_transform(stream) {
4269 this.stream = stream;
4272 d3_geo_transform.prototype = {
4273 point: function(x, y) { this.stream.point(x, y); },
4274 sphere: function() { this.stream.sphere(); },
4275 lineStart: function() { this.stream.lineStart(); },
4276 lineEnd: function() { this.stream.lineEnd(); },
4277 polygonStart: function() { this.stream.polygonStart(); },
4278 polygonEnd: function() { this.stream.polygonEnd(); }
4281 function d3_geo_transformPoint(stream, point) {
4284 sphere: function() { stream.sphere(); },
4285 lineStart: function() { stream.lineStart(); },
4286 lineEnd: function() { stream.lineEnd(); },
4287 polygonStart: function() { stream.polygonStart(); },
4288 polygonEnd: function() { stream.polygonEnd(); },
4292 d3.geo.projection = d3_geo_projection;
4293 d3.geo.projectionMutator = d3_geo_projectionMutator;
4295 function d3_geo_projection(project) {
4296 return d3_geo_projectionMutator(function() { return project; })();
4299 function d3_geo_projectionMutator(projectAt) {
4303 projectResample = d3_geo_resample(function(x, y) { x = project(x, y); return [x[0] * k + δx, δy - x[1] * k]; }),
4305 x = 480, y = 250, // translate
4306 λ = 0, φ = 0, // center
4307 δλ = 0, δφ = 0, δγ = 0, // rotate
4309 preclip = d3_geo_clipAntimeridian,
4310 postclip = d3_identity,
4315 function projection(point) {
4316 point = projectRotate(point[0] * d3_radians, point[1] * d3_radians);
4317 return [point[0] * k + δx, δy - point[1] * k];
4320 function invert(point) {
4321 point = projectRotate.invert((point[0] - δx) / k, (δy - point[1]) / k);
4322 return point && [point[0] * d3_degrees, point[1] * d3_degrees];
4325 projection.stream = function(output) {
4326 if (stream) stream.valid = false;
4327 stream = d3_geo_projectionRadians(preclip(rotate, projectResample(postclip(output))));
4328 stream.valid = true; // allow caching by d3.geo.path
4332 projection.clipAngle = function(_) {
4333 if (!arguments.length) return clipAngle;
4334 preclip = _ == null ? (clipAngle = _, d3_geo_clipAntimeridian) : d3_geo_clipCircle((clipAngle = +_) * d3_radians);
4335 return invalidate();
4338 projection.clipExtent = function(_) {
4339 if (!arguments.length) return clipExtent;
4341 postclip = _ ? d3_geo_clipExtent(_[0][0], _[0][1], _[1][0], _[1][1]) : d3_identity;
4342 return invalidate();
4345 projection.scale = function(_) {
4346 if (!arguments.length) return k;
4351 projection.translate = function(_) {
4352 if (!arguments.length) return [x, y];
4358 projection.center = function(_) {
4359 if (!arguments.length) return [λ * d3_degrees, φ * d3_degrees];
4360 λ = _[0] % 360 * d3_radians;
4361 φ = _[1] % 360 * d3_radians;
4365 projection.rotate = function(_) {
4366 if (!arguments.length) return [δλ * d3_degrees, δφ * d3_degrees, δγ * d3_degrees];
4367 δλ = _[0] % 360 * d3_radians;
4368 δφ = _[1] % 360 * d3_radians;
4369 δγ = _.length > 2 ? _[2] % 360 * d3_radians : 0;
4373 d3.rebind(projection, projectResample, "precision");
4376 projectRotate = d3_geo_compose(rotate = d3_geo_rotation(δλ, δφ, δγ), project);
4377 var center = project(λ, φ);
4378 δx = x - center[0] * k;
4379 δy = y + center[1] * k;
4380 return invalidate();
4383 function invalidate() {
4384 if (stream) stream.valid = false, stream = null;
4389 project = projectAt.apply(this, arguments);
4390 projection.invert = project.invert && invert;
4395 function d3_geo_projectionRadians(stream) {
4396 return d3_geo_transformPoint(stream, function(x, y) {
4397 stream.point(x * d3_radians, y * d3_radians);
4401 function d3_geo_mercator(λ, φ) {
4402 return [λ, Math.log(Math.tan(π / 4 + φ / 2))];
4405 d3_geo_mercator.invert = function(x, y) {
4406 return [x, 2 * Math.atan(Math.exp(y)) - halfπ];
4409 function d3_geo_mercatorProjection(project) {
4410 var m = d3_geo_projection(project),
4412 translate = m.translate,
4413 clipExtent = m.clipExtent,
4416 m.scale = function() {
4417 var v = scale.apply(m, arguments);
4418 return v === m ? (clipAuto ? m.clipExtent(null) : m) : v;
4421 m.translate = function() {
4422 var v = translate.apply(m, arguments);
4423 return v === m ? (clipAuto ? m.clipExtent(null) : m) : v;
4426 m.clipExtent = function(_) {
4427 var v = clipExtent.apply(m, arguments);
4429 if (clipAuto = _ == null) {
4430 var k = π * scale(), t = translate();
4431 clipExtent([[t[0] - k, t[1] - k], [t[0] + k, t[1] + k]]);
4433 } else if (clipAuto) {
4439 return m.clipExtent(null);
4442 (d3.geo.mercator = function() {
4443 return d3_geo_mercatorProjection(d3_geo_mercator);
4444 }).raw = d3_geo_mercator;
4447 d3.geom.polygon = function(coordinates) {
4448 d3_subclass(coordinates, d3_geom_polygonPrototype);
4452 var d3_geom_polygonPrototype = d3.geom.polygon.prototype = [];
4454 d3_geom_polygonPrototype.area = function() {
4464 area += a[1] * b[0] - a[0] * b[1];
4470 d3_geom_polygonPrototype.centroid = function(k) {
4479 if (!arguments.length) k = -1 / (6 * this.area());
4484 c = a[0] * b[1] - b[0] * a[1];
4485 x += (a[0] + b[0]) * c;
4486 y += (a[1] + b[1]) * c;
4489 return [x * k, y * k];
4492 // The Sutherland-Hodgman clipping algorithm.
4493 // Note: requires the clip polygon to be counterclockwise and convex.
4494 d3_geom_polygonPrototype.clip = function(subject) {
4496 closed = d3_geom_polygonClosed(subject),
4498 n = this.length - d3_geom_polygonClosed(this),
4507 input = subject.slice();
4510 c = input[(m = input.length - closed) - 1];
4514 if (d3_geom_polygonInside(d, a, b)) {
4515 if (!d3_geom_polygonInside(c, a, b)) {
4516 subject.push(d3_geom_polygonIntersect(c, d, a, b));
4519 } else if (d3_geom_polygonInside(c, a, b)) {
4520 subject.push(d3_geom_polygonIntersect(c, d, a, b));
4524 if (closed) subject.push(subject[0]);
4531 function d3_geom_polygonInside(p, a, b) {
4532 return (b[0] - a[0]) * (p[1] - a[1]) < (b[1] - a[1]) * (p[0] - a[0]);
4535 // Intersect two infinite lines cd and ab.
4536 function d3_geom_polygonIntersect(c, d, a, b) {
4537 var x1 = c[0], x3 = a[0], x21 = d[0] - x1, x43 = b[0] - x3,
4538 y1 = c[1], y3 = a[1], y21 = d[1] - y1, y43 = b[1] - y3,
4539 ua = (x43 * (y1 - y3) - y43 * (x1 - x3)) / (y43 * x21 - x43 * y21);
4540 return [x1 + ua * x21, y1 + ua * y21];
4543 // Returns true if the polygon is closed.
4544 function d3_geom_polygonClosed(coordinates) {
4545 var a = coordinates[0],
4546 b = coordinates[coordinates.length - 1];
4547 return !(a[0] - b[0] || a[1] - b[1]);
4550 var d3_ease_default = function() { return d3_identity; };
4552 var d3_ease = d3.map({
4553 linear: d3_ease_default,
4555 quad: function() { return d3_ease_quad; },
4556 cubic: function() { return d3_ease_cubic; },
4557 sin: function() { return d3_ease_sin; },
4558 exp: function() { return d3_ease_exp; },
4559 circle: function() { return d3_ease_circle; },
4560 elastic: d3_ease_elastic,
4562 bounce: function() { return d3_ease_bounce; }
4565 var d3_ease_mode = d3.map({
4567 "out": d3_ease_reverse,
4568 "in-out": d3_ease_reflect,
4569 "out-in": function(f) { return d3_ease_reflect(d3_ease_reverse(f)); }
4572 d3.ease = function(name) {
4573 var i = name.indexOf("-"),
4574 t = i >= 0 ? name.substring(0, i) : name,
4575 m = i >= 0 ? name.substring(i + 1) : "in";
4576 t = d3_ease.get(t) || d3_ease_default;
4577 m = d3_ease_mode.get(m) || d3_identity;
4578 return d3_ease_clamp(m(t.apply(null, d3_arraySlice.call(arguments, 1))));
4581 function d3_ease_clamp(f) {
4582 return function(t) {
4583 return t <= 0 ? 0 : t >= 1 ? 1 : f(t);
4587 function d3_ease_reverse(f) {
4588 return function(t) {
4589 return 1 - f(1 - t);
4593 function d3_ease_reflect(f) {
4594 return function(t) {
4595 return .5 * (t < .5 ? f(2 * t) : (2 - f(2 - 2 * t)));
4599 function d3_ease_quad(t) {
4603 function d3_ease_cubic(t) {
4607 // Optimized clamp(reflect(poly(3))).
4608 function d3_ease_cubicInOut(t) {
4609 if (t <= 0) return 0;
4610 if (t >= 1) return 1;
4611 var t2 = t * t, t3 = t2 * t;
4612 return 4 * (t < .5 ? t3 : 3 * (t - t2) + t3 - .75);
4615 function d3_ease_poly(e) {
4616 return function(t) {
4617 return Math.pow(t, e);
4621 function d3_ease_sin(t) {
4622 return 1 - Math.cos(t * halfπ);
4625 function d3_ease_exp(t) {
4626 return Math.pow(2, 10 * (t - 1));
4629 function d3_ease_circle(t) {
4630 return 1 - Math.sqrt(1 - t * t);
4633 function d3_ease_elastic(a, p) {
4635 if (arguments.length < 2) p = 0.45;
4636 if (arguments.length) s = p / τ * Math.asin(1 / a);
4637 else a = 1, s = p / 4;
4638 return function(t) {
4639 return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * τ / p);
4643 function d3_ease_back(s) {
4644 if (!s) s = 1.70158;
4645 return function(t) {
4646 return t * t * ((s + 1) * t - s);
4650 function d3_ease_bounce(t) {
4651 return t < 1 / 2.75 ? 7.5625 * t * t
4652 : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75
4653 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375
4654 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375;
4657 function d3_transition(groups, id) {
4658 d3_subclass(groups, d3_transitionPrototype);
4660 groups.id = id; // Note: read-only!
4665 var d3_transitionPrototype = [],
4666 d3_transitionId = 0,
4667 d3_transitionInheritId,
4668 d3_transitionInherit;
4670 d3_transitionPrototype.call = d3_selectionPrototype.call;
4671 d3_transitionPrototype.empty = d3_selectionPrototype.empty;
4672 d3_transitionPrototype.node = d3_selectionPrototype.node;
4673 d3_transitionPrototype.size = d3_selectionPrototype.size;
4675 d3.transition = function(selection) {
4676 return arguments.length
4677 ? (d3_transitionInheritId ? selection.transition() : selection)
4678 : d3_selectionRoot.transition();
4681 d3.transition.prototype = d3_transitionPrototype;
4684 d3_transitionPrototype.select = function(selector) {
4691 selector = d3_selection_selector(selector);
4693 for (var j = -1, m = this.length; ++j < m;) {
4694 subgroups.push(subgroup = []);
4695 for (var group = this[j], i = -1, n = group.length; ++i < n;) {
4696 if ((node = group[i]) && (subnode = selector.call(node, node.__data__, i, j))) {
4697 if ("__data__" in node) subnode.__data__ = node.__data__;
4698 d3_transitionNode(subnode, i, id, node.__transition__[id]);
4699 subgroup.push(subnode);
4701 subgroup.push(null);
4706 return d3_transition(subgroups, id);
4709 d3_transitionPrototype.selectAll = function(selector) {
4718 selector = d3_selection_selectorAll(selector);
4720 for (var j = -1, m = this.length; ++j < m;) {
4721 for (var group = this[j], i = -1, n = group.length; ++i < n;) {
4722 if (node = group[i]) {
4723 transition = node.__transition__[id];
4724 subnodes = selector.call(node, node.__data__, i, j);
4725 subgroups.push(subgroup = []);
4726 for (var k = -1, o = subnodes.length; ++k < o;) {
4727 if (subnode = subnodes[k]) d3_transitionNode(subnode, k, id, transition);
4728 subgroup.push(subnode);
4734 return d3_transition(subgroups, id);
4737 d3_transitionPrototype.filter = function(filter) {
4743 if (typeof filter !== "function") filter = d3_selection_filter(filter);
4745 for (var j = 0, m = this.length; j < m; j++) {
4746 subgroups.push(subgroup = []);
4747 for (var group = this[j], i = 0, n = group.length; i < n; i++) {
4748 if ((node = group[i]) && filter.call(node, node.__data__, i, j)) {
4749 subgroup.push(node);
4754 return d3_transition(subgroups, this.id);
4756 function d3_Color() {}
4758 d3_Color.prototype.toString = function() {
4759 return this.rgb() + "";
4762 d3.hsl = function(h, s, l) {
4763 return arguments.length === 1
4764 ? (h instanceof d3_Hsl ? d3_hsl(h.h, h.s, h.l)
4765 : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl))
4766 : d3_hsl(+h, +s, +l);
4769 function d3_hsl(h, s, l) {
4770 return new d3_Hsl(h, s, l);
4773 function d3_Hsl(h, s, l) {
4779 var d3_hslPrototype = d3_Hsl.prototype = new d3_Color;
4781 d3_hslPrototype.brighter = function(k) {
4782 k = Math.pow(0.7, arguments.length ? k : 1);
4783 return d3_hsl(this.h, this.s, this.l / k);
4786 d3_hslPrototype.darker = function(k) {
4787 k = Math.pow(0.7, arguments.length ? k : 1);
4788 return d3_hsl(this.h, this.s, k * this.l);
4791 d3_hslPrototype.rgb = function() {
4792 return d3_hsl_rgb(this.h, this.s, this.l);
4795 function d3_hsl_rgb(h, s, l) {
4799 /* Some simple corrections for h, s and l. */
4800 h = isNaN(h) ? 0 : (h %= 360) < 0 ? h + 360 : h;
4801 s = isNaN(s) ? 0 : s < 0 ? 0 : s > 1 ? 1 : s;
4802 l = l < 0 ? 0 : l > 1 ? 1 : l;
4804 /* From FvD 13.37, CSS Color Module Level 3 */
4805 m2 = l <= .5 ? l * (1 + s) : l + s - l * s;
4809 if (h > 360) h -= 360;
4810 else if (h < 0) h += 360;
4811 if (h < 60) return m1 + (m2 - m1) * h / 60;
4812 if (h < 180) return m2;
4813 if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60;
4818 return Math.round(v(h) * 255);
4821 return d3_rgb(vv(h + 120), vv(h), vv(h - 120));
4824 d3.hcl = function(h, c,