michael@0: !function() { michael@0: var d3 = { michael@0: version: "3.4.2" michael@0: }; michael@0: if (!Date.now) Date.now = function() { michael@0: return +new Date(); michael@0: }; michael@0: var d3_arraySlice = [].slice, d3_array = function(list) { michael@0: return d3_arraySlice.call(list); michael@0: }; michael@0: var d3_document = document, d3_documentElement = d3_document.documentElement, d3_window = window; michael@0: try { michael@0: d3_array(d3_documentElement.childNodes)[0].nodeType; michael@0: } catch (e) { michael@0: d3_array = function(list) { michael@0: var i = list.length, array = new Array(i); michael@0: while (i--) array[i] = list[i]; michael@0: return array; michael@0: }; michael@0: } michael@0: try { michael@0: d3_document.createElement("div").style.setProperty("opacity", 0, ""); michael@0: } catch (error) { michael@0: var d3_element_prototype = d3_window.Element.prototype, d3_element_setAttribute = d3_element_prototype.setAttribute, d3_element_setAttributeNS = d3_element_prototype.setAttributeNS, d3_style_prototype = d3_window.CSSStyleDeclaration.prototype, d3_style_setProperty = d3_style_prototype.setProperty; michael@0: d3_element_prototype.setAttribute = function(name, value) { michael@0: d3_element_setAttribute.call(this, name, value + ""); michael@0: }; michael@0: d3_element_prototype.setAttributeNS = function(space, local, value) { michael@0: d3_element_setAttributeNS.call(this, space, local, value + ""); michael@0: }; michael@0: d3_style_prototype.setProperty = function(name, value, priority) { michael@0: d3_style_setProperty.call(this, name, value + "", priority); michael@0: }; michael@0: } michael@0: d3.ascending = function(a, b) { michael@0: return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN; michael@0: }; michael@0: d3.descending = function(a, b) { michael@0: return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN; michael@0: }; michael@0: d3.min = function(array, f) { michael@0: var i = -1, n = array.length, a, b; michael@0: if (arguments.length === 1) { michael@0: while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined; michael@0: while (++i < n) if ((b = array[i]) != null && a > b) a = b; michael@0: } else { michael@0: while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined; michael@0: while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b; michael@0: } michael@0: return a; michael@0: }; michael@0: d3.max = function(array, f) { michael@0: var i = -1, n = array.length, a, b; michael@0: if (arguments.length === 1) { michael@0: while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined; michael@0: while (++i < n) if ((b = array[i]) != null && b > a) a = b; michael@0: } else { michael@0: while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined; michael@0: while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b; michael@0: } michael@0: return a; michael@0: }; michael@0: d3.extent = function(array, f) { michael@0: var i = -1, n = array.length, a, b, c; michael@0: if (arguments.length === 1) { michael@0: while (++i < n && !((a = c = array[i]) != null && a <= a)) a = c = undefined; michael@0: while (++i < n) if ((b = array[i]) != null) { michael@0: if (a > b) a = b; michael@0: if (c < b) c = b; michael@0: } michael@0: } else { michael@0: while (++i < n && !((a = c = f.call(array, array[i], i)) != null && a <= a)) a = undefined; michael@0: while (++i < n) if ((b = f.call(array, array[i], i)) != null) { michael@0: if (a > b) a = b; michael@0: if (c < b) c = b; michael@0: } michael@0: } michael@0: return [ a, c ]; michael@0: }; michael@0: d3.sum = function(array, f) { michael@0: var s = 0, n = array.length, a, i = -1; michael@0: if (arguments.length === 1) { michael@0: while (++i < n) if (!isNaN(a = +array[i])) s += a; michael@0: } else { michael@0: while (++i < n) if (!isNaN(a = +f.call(array, array[i], i))) s += a; michael@0: } michael@0: return s; michael@0: }; michael@0: function d3_number(x) { michael@0: return x != null && !isNaN(x); michael@0: } michael@0: d3.mean = function(array, f) { michael@0: var n = array.length, a, m = 0, i = -1, j = 0; michael@0: if (arguments.length === 1) { michael@0: while (++i < n) if (d3_number(a = array[i])) m += (a - m) / ++j; michael@0: } else { michael@0: while (++i < n) if (d3_number(a = f.call(array, array[i], i))) m += (a - m) / ++j; michael@0: } michael@0: return j ? m : undefined; michael@0: }; michael@0: d3.quantile = function(values, p) { michael@0: var H = (values.length - 1) * p + 1, h = Math.floor(H), v = +values[h - 1], e = H - h; michael@0: return e ? v + e * (values[h] - v) : v; michael@0: }; michael@0: d3.median = function(array, f) { michael@0: if (arguments.length > 1) array = array.map(f); michael@0: array = array.filter(d3_number); michael@0: return array.length ? d3.quantile(array.sort(d3.ascending), .5) : undefined; michael@0: }; michael@0: d3.bisector = function(f) { michael@0: return { michael@0: left: function(a, x, lo, hi) { michael@0: if (arguments.length < 3) lo = 0; michael@0: if (arguments.length < 4) hi = a.length; michael@0: while (lo < hi) { michael@0: var mid = lo + hi >>> 1; michael@0: if (f.call(a, a[mid], mid) < x) lo = mid + 1; else hi = mid; michael@0: } michael@0: return lo; michael@0: }, michael@0: right: function(a, x, lo, hi) { michael@0: if (arguments.length < 3) lo = 0; michael@0: if (arguments.length < 4) hi = a.length; michael@0: while (lo < hi) { michael@0: var mid = lo + hi >>> 1; michael@0: if (x < f.call(a, a[mid], mid)) hi = mid; else lo = mid + 1; michael@0: } michael@0: return lo; michael@0: } michael@0: }; michael@0: }; michael@0: var d3_bisector = d3.bisector(function(d) { michael@0: return d; michael@0: }); michael@0: d3.bisectLeft = d3_bisector.left; michael@0: d3.bisect = d3.bisectRight = d3_bisector.right; michael@0: d3.shuffle = function(array) { michael@0: var m = array.length, t, i; michael@0: while (m) { michael@0: i = Math.random() * m-- | 0; michael@0: t = array[m], array[m] = array[i], array[i] = t; michael@0: } michael@0: return array; michael@0: }; michael@0: d3.permute = function(array, indexes) { michael@0: var i = indexes.length, permutes = new Array(i); michael@0: while (i--) permutes[i] = array[indexes[i]]; michael@0: return permutes; michael@0: }; michael@0: d3.pairs = function(array) { michael@0: var i = 0, n = array.length - 1, p0, p1 = array[0], pairs = new Array(n < 0 ? 0 : n); michael@0: while (i < n) pairs[i] = [ p0 = p1, p1 = array[++i] ]; michael@0: return pairs; michael@0: }; michael@0: d3.zip = function() { michael@0: if (!(n = arguments.length)) return []; michael@0: for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m; ) { michael@0: for (var j = -1, n, zip = zips[i] = new Array(n); ++j < n; ) { michael@0: zip[j] = arguments[j][i]; michael@0: } michael@0: } michael@0: return zips; michael@0: }; michael@0: function d3_zipLength(d) { michael@0: return d.length; michael@0: } michael@0: d3.transpose = function(matrix) { michael@0: return d3.zip.apply(d3, matrix); michael@0: }; michael@0: d3.keys = function(map) { michael@0: var keys = []; michael@0: for (var key in map) keys.push(key); michael@0: return keys; michael@0: }; michael@0: d3.values = function(map) { michael@0: var values = []; michael@0: for (var key in map) values.push(map[key]); michael@0: return values; michael@0: }; michael@0: d3.entries = function(map) { michael@0: var entries = []; michael@0: for (var key in map) entries.push({ michael@0: key: key, michael@0: value: map[key] michael@0: }); michael@0: return entries; michael@0: }; michael@0: d3.merge = function(arrays) { michael@0: var n = arrays.length, m, i = -1, j = 0, merged, array; michael@0: while (++i < n) j += arrays[i].length; michael@0: merged = new Array(j); michael@0: while (--n >= 0) { michael@0: array = arrays[n]; michael@0: m = array.length; michael@0: while (--m >= 0) { michael@0: merged[--j] = array[m]; michael@0: } michael@0: } michael@0: return merged; michael@0: }; michael@0: var abs = Math.abs; michael@0: d3.range = function(start, stop, step) { michael@0: if (arguments.length < 3) { michael@0: step = 1; michael@0: if (arguments.length < 2) { michael@0: stop = start; michael@0: start = 0; michael@0: } michael@0: } michael@0: if ((stop - start) / step === Infinity) throw new Error("infinite range"); michael@0: var range = [], k = d3_range_integerScale(abs(step)), i = -1, j; michael@0: start *= k, stop *= k, step *= k; michael@0: if (step < 0) while ((j = start + step * ++i) > stop) range.push(j / k); else while ((j = start + step * ++i) < stop) range.push(j / k); michael@0: return range; michael@0: }; michael@0: function d3_range_integerScale(x) { michael@0: var k = 1; michael@0: while (x * k % 1) k *= 10; michael@0: return k; michael@0: } michael@0: function d3_class(ctor, properties) { michael@0: try { michael@0: for (var key in properties) { michael@0: Object.defineProperty(ctor.prototype, key, { michael@0: value: properties[key], michael@0: enumerable: false michael@0: }); michael@0: } michael@0: } catch (e) { michael@0: ctor.prototype = properties; michael@0: } michael@0: } michael@0: d3.map = function(object) { michael@0: var map = new d3_Map(); michael@0: if (object instanceof d3_Map) object.forEach(function(key, value) { michael@0: map.set(key, value); michael@0: }); else for (var key in object) map.set(key, object[key]); michael@0: return map; michael@0: }; michael@0: function d3_Map() {} michael@0: d3_class(d3_Map, { michael@0: has: d3_map_has, michael@0: get: function(key) { michael@0: return this[d3_map_prefix + key]; michael@0: }, michael@0: set: function(key, value) { michael@0: return this[d3_map_prefix + key] = value; michael@0: }, michael@0: remove: d3_map_remove, michael@0: keys: d3_map_keys, michael@0: values: function() { michael@0: var values = []; michael@0: this.forEach(function(key, value) { michael@0: values.push(value); michael@0: }); michael@0: return values; michael@0: }, michael@0: entries: function() { michael@0: var entries = []; michael@0: this.forEach(function(key, value) { michael@0: entries.push({ michael@0: key: key, michael@0: value: value michael@0: }); michael@0: }); michael@0: return entries; michael@0: }, michael@0: size: d3_map_size, michael@0: empty: d3_map_empty, michael@0: forEach: function(f) { michael@0: for (var key in this) if (key.charCodeAt(0) === d3_map_prefixCode) f.call(this, key.substring(1), this[key]); michael@0: } michael@0: }); michael@0: var d3_map_prefix = "\x00", d3_map_prefixCode = d3_map_prefix.charCodeAt(0); michael@0: function d3_map_has(key) { michael@0: return d3_map_prefix + key in this; michael@0: } michael@0: function d3_map_remove(key) { michael@0: key = d3_map_prefix + key; michael@0: return key in this && delete this[key]; michael@0: } michael@0: function d3_map_keys() { michael@0: var keys = []; michael@0: this.forEach(function(key) { michael@0: keys.push(key); michael@0: }); michael@0: return keys; michael@0: } michael@0: function d3_map_size() { michael@0: var size = 0; michael@0: for (var key in this) if (key.charCodeAt(0) === d3_map_prefixCode) ++size; michael@0: return size; michael@0: } michael@0: function d3_map_empty() { michael@0: for (var key in this) if (key.charCodeAt(0) === d3_map_prefixCode) return false; michael@0: return true; michael@0: } michael@0: d3.nest = function() { michael@0: var nest = {}, keys = [], sortKeys = [], sortValues, rollup; michael@0: function map(mapType, array, depth) { michael@0: if (depth >= keys.length) return rollup ? rollup.call(nest, array) : sortValues ? array.sort(sortValues) : array; michael@0: var i = -1, n = array.length, key = keys[depth++], keyValue, object, setter, valuesByKey = new d3_Map(), values; michael@0: while (++i < n) { michael@0: if (values = valuesByKey.get(keyValue = key(object = array[i]))) { michael@0: values.push(object); michael@0: } else { michael@0: valuesByKey.set(keyValue, [ object ]); michael@0: } michael@0: } michael@0: if (mapType) { michael@0: object = mapType(); michael@0: setter = function(keyValue, values) { michael@0: object.set(keyValue, map(mapType, values, depth)); michael@0: }; michael@0: } else { michael@0: object = {}; michael@0: setter = function(keyValue, values) { michael@0: object[keyValue] = map(mapType, values, depth); michael@0: }; michael@0: } michael@0: valuesByKey.forEach(setter); michael@0: return object; michael@0: } michael@0: function entries(map, depth) { michael@0: if (depth >= keys.length) return map; michael@0: var array = [], sortKey = sortKeys[depth++]; michael@0: map.forEach(function(key, keyMap) { michael@0: array.push({ michael@0: key: key, michael@0: values: entries(keyMap, depth) michael@0: }); michael@0: }); michael@0: return sortKey ? array.sort(function(a, b) { michael@0: return sortKey(a.key, b.key); michael@0: }) : array; michael@0: } michael@0: nest.map = function(array, mapType) { michael@0: return map(mapType, array, 0); michael@0: }; michael@0: nest.entries = function(array) { michael@0: return entries(map(d3.map, array, 0), 0); michael@0: }; michael@0: nest.key = function(d) { michael@0: keys.push(d); michael@0: return nest; michael@0: }; michael@0: nest.sortKeys = function(order) { michael@0: sortKeys[keys.length - 1] = order; michael@0: return nest; michael@0: }; michael@0: nest.sortValues = function(order) { michael@0: sortValues = order; michael@0: return nest; michael@0: }; michael@0: nest.rollup = function(f) { michael@0: rollup = f; michael@0: return nest; michael@0: }; michael@0: return nest; michael@0: }; michael@0: d3.set = function(array) { michael@0: var set = new d3_Set(); michael@0: if (array) for (var i = 0, n = array.length; i < n; ++i) set.add(array[i]); michael@0: return set; michael@0: }; michael@0: function d3_Set() {} michael@0: d3_class(d3_Set, { michael@0: has: d3_map_has, michael@0: add: function(value) { michael@0: this[d3_map_prefix + value] = true; michael@0: return value; michael@0: }, michael@0: remove: function(value) { michael@0: value = d3_map_prefix + value; michael@0: return value in this && delete this[value]; michael@0: }, michael@0: values: d3_map_keys, michael@0: size: d3_map_size, michael@0: empty: d3_map_empty, michael@0: forEach: function(f) { michael@0: for (var value in this) if (value.charCodeAt(0) === d3_map_prefixCode) f.call(this, value.substring(1)); michael@0: } michael@0: }); michael@0: d3.behavior = {}; michael@0: d3.rebind = function(target, source) { michael@0: var i = 1, n = arguments.length, method; michael@0: while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]); michael@0: return target; michael@0: }; michael@0: function d3_rebind(target, source, method) { michael@0: return function() { michael@0: var value = method.apply(source, arguments); michael@0: return value === source ? target : value; michael@0: }; michael@0: } michael@0: function d3_vendorSymbol(object, name) { michael@0: if (name in object) return name; michael@0: name = name.charAt(0).toUpperCase() + name.substring(1); michael@0: for (var i = 0, n = d3_vendorPrefixes.length; i < n; ++i) { michael@0: var prefixName = d3_vendorPrefixes[i] + name; michael@0: if (prefixName in object) return prefixName; michael@0: } michael@0: } michael@0: var d3_vendorPrefixes = [ "webkit", "ms", "moz", "Moz", "o", "O" ]; michael@0: function d3_noop() {} michael@0: d3.dispatch = function() { michael@0: var dispatch = new d3_dispatch(), i = -1, n = arguments.length; michael@0: while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); michael@0: return dispatch; michael@0: }; michael@0: function d3_dispatch() {} michael@0: d3_dispatch.prototype.on = function(type, listener) { michael@0: var i = type.indexOf("."), name = ""; michael@0: if (i >= 0) { michael@0: name = type.substring(i + 1); michael@0: type = type.substring(0, i); michael@0: } michael@0: if (type) return arguments.length < 2 ? this[type].on(name) : this[type].on(name, listener); michael@0: if (arguments.length === 2) { michael@0: if (listener == null) for (type in this) { michael@0: if (this.hasOwnProperty(type)) this[type].on(name, null); michael@0: } michael@0: return this; michael@0: } michael@0: }; michael@0: function d3_dispatch_event(dispatch) { michael@0: var listeners = [], listenerByName = new d3_Map(); michael@0: function event() { michael@0: var z = listeners, i = -1, n = z.length, l; michael@0: while (++i < n) if (l = z[i].on) l.apply(this, arguments); michael@0: return dispatch; michael@0: } michael@0: event.on = function(name, listener) { michael@0: var l = listenerByName.get(name), i; michael@0: if (arguments.length < 2) return l && l.on; michael@0: if (l) { michael@0: l.on = null; michael@0: listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1)); michael@0: listenerByName.remove(name); michael@0: } michael@0: if (listener) listeners.push(listenerByName.set(name, { michael@0: on: listener michael@0: })); michael@0: return dispatch; michael@0: }; michael@0: return event; michael@0: } michael@0: d3.event = null; michael@0: function d3_eventPreventDefault() { michael@0: d3.event.preventDefault(); michael@0: } michael@0: function d3_eventSource() { michael@0: var e = d3.event, s; michael@0: while (s = e.sourceEvent) e = s; michael@0: return e; michael@0: } michael@0: function d3_eventDispatch(target) { michael@0: var dispatch = new d3_dispatch(), i = 0, n = arguments.length; michael@0: while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); michael@0: dispatch.of = function(thiz, argumentz) { michael@0: return function(e1) { michael@0: try { michael@0: var e0 = e1.sourceEvent = d3.event; michael@0: e1.target = target; michael@0: d3.event = e1; michael@0: dispatch[e1.type].apply(thiz, argumentz); michael@0: } finally { michael@0: d3.event = e0; michael@0: } michael@0: }; michael@0: }; michael@0: return dispatch; michael@0: } michael@0: d3.requote = function(s) { michael@0: return s.replace(d3_requote_re, "\\$&"); michael@0: }; michael@0: var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g; michael@0: var d3_subclass = {}.__proto__ ? function(object, prototype) { michael@0: object.__proto__ = prototype; michael@0: } : function(object, prototype) { michael@0: for (var property in prototype) object[property] = prototype[property]; michael@0: }; michael@0: function d3_selection(groups) { michael@0: d3_subclass(groups, d3_selectionPrototype); michael@0: return groups; michael@0: } michael@0: var d3_select = function(s, n) { michael@0: return n.querySelector(s); michael@0: }, d3_selectAll = function(s, n) { michael@0: return n.querySelectorAll(s); michael@0: }, d3_selectMatcher = d3_documentElement[d3_vendorSymbol(d3_documentElement, "matchesSelector")], d3_selectMatches = function(n, s) { michael@0: return d3_selectMatcher.call(n, s); michael@0: }; michael@0: if (typeof Sizzle === "function") { michael@0: d3_select = function(s, n) { michael@0: return Sizzle(s, n)[0] || null; michael@0: }; michael@0: d3_selectAll = function(s, n) { michael@0: return Sizzle.uniqueSort(Sizzle(s, n)); michael@0: }; michael@0: d3_selectMatches = Sizzle.matchesSelector; michael@0: } michael@0: d3.selection = function() { michael@0: return d3_selectionRoot; michael@0: }; michael@0: var d3_selectionPrototype = d3.selection.prototype = []; michael@0: d3_selectionPrototype.select = function(selector) { michael@0: var subgroups = [], subgroup, subnode, group, node; michael@0: selector = d3_selection_selector(selector); michael@0: for (var j = -1, m = this.length; ++j < m; ) { michael@0: subgroups.push(subgroup = []); michael@0: subgroup.parentNode = (group = this[j]).parentNode; michael@0: for (var i = -1, n = group.length; ++i < n; ) { michael@0: if (node = group[i]) { michael@0: subgroup.push(subnode = selector.call(node, node.__data__, i, j)); michael@0: if (subnode && "__data__" in node) subnode.__data__ = node.__data__; michael@0: } else { michael@0: subgroup.push(null); michael@0: } michael@0: } michael@0: } michael@0: return d3_selection(subgroups); michael@0: }; michael@0: function d3_selection_selector(selector) { michael@0: return typeof selector === "function" ? selector : function() { michael@0: return d3_select(selector, this); michael@0: }; michael@0: } michael@0: d3_selectionPrototype.selectAll = function(selector) { michael@0: var subgroups = [], subgroup, node; michael@0: selector = d3_selection_selectorAll(selector); michael@0: for (var j = -1, m = this.length; ++j < m; ) { michael@0: for (var group = this[j], i = -1, n = group.length; ++i < n; ) { michael@0: if (node = group[i]) { michael@0: subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i, j))); michael@0: subgroup.parentNode = node; michael@0: } michael@0: } michael@0: } michael@0: return d3_selection(subgroups); michael@0: }; michael@0: function d3_selection_selectorAll(selector) { michael@0: return typeof selector === "function" ? selector : function() { michael@0: return d3_selectAll(selector, this); michael@0: }; michael@0: } michael@0: var d3_nsPrefix = { michael@0: svg: "http://www.w3.org/2000/svg", michael@0: xhtml: "http://www.w3.org/1999/xhtml", michael@0: xlink: "http://www.w3.org/1999/xlink", michael@0: xml: "http://www.w3.org/XML/1998/namespace", michael@0: xmlns: "http://www.w3.org/2000/xmlns/" michael@0: }; michael@0: d3.ns = { michael@0: prefix: d3_nsPrefix, michael@0: qualify: function(name) { michael@0: var i = name.indexOf(":"), prefix = name; michael@0: if (i >= 0) { michael@0: prefix = name.substring(0, i); michael@0: name = name.substring(i + 1); michael@0: } michael@0: return d3_nsPrefix.hasOwnProperty(prefix) ? { michael@0: space: d3_nsPrefix[prefix], michael@0: local: name michael@0: } : name; michael@0: } michael@0: }; michael@0: d3_selectionPrototype.attr = function(name, value) { michael@0: if (arguments.length < 2) { michael@0: if (typeof name === "string") { michael@0: var node = this.node(); michael@0: name = d3.ns.qualify(name); michael@0: return name.local ? node.getAttributeNS(name.space, name.local) : node.getAttribute(name); michael@0: } michael@0: for (value in name) this.each(d3_selection_attr(value, name[value])); michael@0: return this; michael@0: } michael@0: return this.each(d3_selection_attr(name, value)); michael@0: }; michael@0: function d3_selection_attr(name, value) { michael@0: name = d3.ns.qualify(name); michael@0: function attrNull() { michael@0: this.removeAttribute(name); michael@0: } michael@0: function attrNullNS() { michael@0: this.removeAttributeNS(name.space, name.local); michael@0: } michael@0: function attrConstant() { michael@0: this.setAttribute(name, value); michael@0: } michael@0: function attrConstantNS() { michael@0: this.setAttributeNS(name.space, name.local, value); michael@0: } michael@0: function attrFunction() { michael@0: var x = value.apply(this, arguments); michael@0: if (x == null) this.removeAttribute(name); else this.setAttribute(name, x); michael@0: } michael@0: function attrFunctionNS() { michael@0: var x = value.apply(this, arguments); michael@0: if (x == null) this.removeAttributeNS(name.space, name.local); else this.setAttributeNS(name.space, name.local, x); michael@0: } michael@0: return value == null ? name.local ? attrNullNS : attrNull : typeof value === "function" ? name.local ? attrFunctionNS : attrFunction : name.local ? attrConstantNS : attrConstant; michael@0: } michael@0: function d3_collapse(s) { michael@0: return s.trim().replace(/\s+/g, " "); michael@0: } michael@0: d3_selectionPrototype.classed = function(name, value) { michael@0: if (arguments.length < 2) { michael@0: if (typeof name === "string") { michael@0: var node = this.node(), n = (name = d3_selection_classes(name)).length, i = -1; michael@0: if (value = node.classList) { michael@0: while (++i < n) if (!value.contains(name[i])) return false; michael@0: } else { michael@0: value = node.getAttribute("class"); michael@0: while (++i < n) if (!d3_selection_classedRe(name[i]).test(value)) return false; michael@0: } michael@0: return true; michael@0: } michael@0: for (value in name) this.each(d3_selection_classed(value, name[value])); michael@0: return this; michael@0: } michael@0: return this.each(d3_selection_classed(name, value)); michael@0: }; michael@0: function d3_selection_classedRe(name) { michael@0: return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g"); michael@0: } michael@0: function d3_selection_classes(name) { michael@0: return name.trim().split(/^|\s+/); michael@0: } michael@0: function d3_selection_classed(name, value) { michael@0: name = d3_selection_classes(name).map(d3_selection_classedName); michael@0: var n = name.length; michael@0: function classedConstant() { michael@0: var i = -1; michael@0: while (++i < n) name[i](this, value); michael@0: } michael@0: function classedFunction() { michael@0: var i = -1, x = value.apply(this, arguments); michael@0: while (++i < n) name[i](this, x); michael@0: } michael@0: return typeof value === "function" ? classedFunction : classedConstant; michael@0: } michael@0: function d3_selection_classedName(name) { michael@0: var re = d3_selection_classedRe(name); michael@0: return function(node, value) { michael@0: if (c = node.classList) return value ? c.add(name) : c.remove(name); michael@0: var c = node.getAttribute("class") || ""; michael@0: if (value) { michael@0: re.lastIndex = 0; michael@0: if (!re.test(c)) node.setAttribute("class", d3_collapse(c + " " + name)); michael@0: } else { michael@0: node.setAttribute("class", d3_collapse(c.replace(re, " "))); michael@0: } michael@0: }; michael@0: } michael@0: d3_selectionPrototype.style = function(name, value, priority) { michael@0: var n = arguments.length; michael@0: if (n < 3) { michael@0: if (typeof name !== "string") { michael@0: if (n < 2) value = ""; michael@0: for (priority in name) this.each(d3_selection_style(priority, name[priority], value)); michael@0: return this; michael@0: } michael@0: if (n < 2) return d3_window.getComputedStyle(this.node(), null).getPropertyValue(name); michael@0: priority = ""; michael@0: } michael@0: return this.each(d3_selection_style(name, value, priority)); michael@0: }; michael@0: function d3_selection_style(name, value, priority) { michael@0: function styleNull() { michael@0: this.style.removeProperty(name); michael@0: } michael@0: function styleConstant() { michael@0: this.style.setProperty(name, value, priority); michael@0: } michael@0: function styleFunction() { michael@0: var x = value.apply(this, arguments); michael@0: if (x == null) this.style.removeProperty(name); else this.style.setProperty(name, x, priority); michael@0: } michael@0: return value == null ? styleNull : typeof value === "function" ? styleFunction : styleConstant; michael@0: } michael@0: d3_selectionPrototype.property = function(name, value) { michael@0: if (arguments.length < 2) { michael@0: if (typeof name === "string") return this.node()[name]; michael@0: for (value in name) this.each(d3_selection_property(value, name[value])); michael@0: return this; michael@0: } michael@0: return this.each(d3_selection_property(name, value)); michael@0: }; michael@0: function d3_selection_property(name, value) { michael@0: function propertyNull() { michael@0: delete this[name]; michael@0: } michael@0: function propertyConstant() { michael@0: this[name] = value; michael@0: } michael@0: function propertyFunction() { michael@0: var x = value.apply(this, arguments); michael@0: if (x == null) delete this[name]; else this[name] = x; michael@0: } michael@0: return value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant; michael@0: } michael@0: d3_selectionPrototype.text = function(value) { michael@0: return arguments.length ? this.each(typeof value === "function" ? function() { michael@0: var v = value.apply(this, arguments); michael@0: this.textContent = v == null ? "" : v; michael@0: } : value == null ? function() { michael@0: this.textContent = ""; michael@0: } : function() { michael@0: this.textContent = value; michael@0: }) : this.node().textContent; michael@0: }; michael@0: d3_selectionPrototype.html = function(value) { michael@0: return arguments.length ? this.each(typeof value === "function" ? function() { michael@0: var v = value.apply(this, arguments); michael@0: this.innerHTML = v == null ? "" : v; michael@0: } : value == null ? function() { michael@0: this.innerHTML = ""; michael@0: } : function() { michael@0: this.innerHTML = value; michael@0: }) : this.node().innerHTML; michael@0: }; michael@0: d3_selectionPrototype.append = function(name) { michael@0: name = d3_selection_creator(name); michael@0: return this.select(function() { michael@0: return this.appendChild(name.apply(this, arguments)); michael@0: }); michael@0: }; michael@0: function d3_selection_creator(name) { michael@0: return typeof name === "function" ? name : (name = d3.ns.qualify(name)).local ? function() { michael@0: return this.ownerDocument.createElementNS(name.space, name.local); michael@0: } : function() { michael@0: return this.ownerDocument.createElementNS(this.namespaceURI, name); michael@0: }; michael@0: } michael@0: d3_selectionPrototype.insert = function(name, before) { michael@0: name = d3_selection_creator(name); michael@0: before = d3_selection_selector(before); michael@0: return this.select(function() { michael@0: return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments) || null); michael@0: }); michael@0: }; michael@0: d3_selectionPrototype.remove = function() { michael@0: return this.each(function() { michael@0: var parent = this.parentNode; michael@0: if (parent) parent.removeChild(this); michael@0: }); michael@0: }; michael@0: d3_selectionPrototype.data = function(value, key) { michael@0: var i = -1, n = this.length, group, node; michael@0: if (!arguments.length) { michael@0: value = new Array(n = (group = this[0]).length); michael@0: while (++i < n) { michael@0: if (node = group[i]) { michael@0: value[i] = node.__data__; michael@0: } michael@0: } michael@0: return value; michael@0: } michael@0: function bind(group, groupData) { michael@0: var i, n = group.length, m = groupData.length, n0 = Math.min(n, m), updateNodes = new Array(m), enterNodes = new Array(m), exitNodes = new Array(n), node, nodeData; michael@0: if (key) { michael@0: var nodeByKeyValue = new d3_Map(), dataByKeyValue = new d3_Map(), keyValues = [], keyValue; michael@0: for (i = -1; ++i < n; ) { michael@0: keyValue = key.call(node = group[i], node.__data__, i); michael@0: if (nodeByKeyValue.has(keyValue)) { michael@0: exitNodes[i] = node; michael@0: } else { michael@0: nodeByKeyValue.set(keyValue, node); michael@0: } michael@0: keyValues.push(keyValue); michael@0: } michael@0: for (i = -1; ++i < m; ) { michael@0: keyValue = key.call(groupData, nodeData = groupData[i], i); michael@0: if (node = nodeByKeyValue.get(keyValue)) { michael@0: updateNodes[i] = node; michael@0: node.__data__ = nodeData; michael@0: } else if (!dataByKeyValue.has(keyValue)) { michael@0: enterNodes[i] = d3_selection_dataNode(nodeData); michael@0: } michael@0: dataByKeyValue.set(keyValue, nodeData); michael@0: nodeByKeyValue.remove(keyValue); michael@0: } michael@0: for (i = -1; ++i < n; ) { michael@0: if (nodeByKeyValue.has(keyValues[i])) { michael@0: exitNodes[i] = group[i]; michael@0: } michael@0: } michael@0: } else { michael@0: for (i = -1; ++i < n0; ) { michael@0: node = group[i]; michael@0: nodeData = groupData[i]; michael@0: if (node) { michael@0: node.__data__ = nodeData; michael@0: updateNodes[i] = node; michael@0: } else { michael@0: enterNodes[i] = d3_selection_dataNode(nodeData); michael@0: } michael@0: } michael@0: for (;i < m; ++i) { michael@0: enterNodes[i] = d3_selection_dataNode(groupData[i]); michael@0: } michael@0: for (;i < n; ++i) { michael@0: exitNodes[i] = group[i]; michael@0: } michael@0: } michael@0: enterNodes.update = updateNodes; michael@0: enterNodes.parentNode = updateNodes.parentNode = exitNodes.parentNode = group.parentNode; michael@0: enter.push(enterNodes); michael@0: update.push(updateNodes); michael@0: exit.push(exitNodes); michael@0: } michael@0: var enter = d3_selection_enter([]), update = d3_selection([]), exit = d3_selection([]); michael@0: if (typeof value === "function") { michael@0: while (++i < n) { michael@0: bind(group = this[i], value.call(group, group.parentNode.__data__, i)); michael@0: } michael@0: } else { michael@0: while (++i < n) { michael@0: bind(group = this[i], value); michael@0: } michael@0: } michael@0: update.enter = function() { michael@0: return enter; michael@0: }; michael@0: update.exit = function() { michael@0: return exit; michael@0: }; michael@0: return update; michael@0: }; michael@0: function d3_selection_dataNode(data) { michael@0: return { michael@0: __data__: data michael@0: }; michael@0: } michael@0: d3_selectionPrototype.datum = function(value) { michael@0: return arguments.length ? this.property("__data__", value) : this.property("__data__"); michael@0: }; michael@0: d3_selectionPrototype.filter = function(filter) { michael@0: var subgroups = [], subgroup, group, node; michael@0: if (typeof filter !== "function") filter = d3_selection_filter(filter); michael@0: for (var j = 0, m = this.length; j < m; j++) { michael@0: subgroups.push(subgroup = []); michael@0: subgroup.parentNode = (group = this[j]).parentNode; michael@0: for (var i = 0, n = group.length; i < n; i++) { michael@0: if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { michael@0: subgroup.push(node); michael@0: } michael@0: } michael@0: } michael@0: return d3_selection(subgroups); michael@0: }; michael@0: function d3_selection_filter(selector) { michael@0: return function() { michael@0: return d3_selectMatches(this, selector); michael@0: }; michael@0: } michael@0: d3_selectionPrototype.order = function() { michael@0: for (var j = -1, m = this.length; ++j < m; ) { michael@0: for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0; ) { michael@0: if (node = group[i]) { michael@0: if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next); michael@0: next = node; michael@0: } michael@0: } michael@0: } michael@0: return this; michael@0: }; michael@0: d3_selectionPrototype.sort = function(comparator) { michael@0: comparator = d3_selection_sortComparator.apply(this, arguments); michael@0: for (var j = -1, m = this.length; ++j < m; ) this[j].sort(comparator); michael@0: return this.order(); michael@0: }; michael@0: function d3_selection_sortComparator(comparator) { michael@0: if (!arguments.length) comparator = d3.ascending; michael@0: return function(a, b) { michael@0: return a && b ? comparator(a.__data__, b.__data__) : !a - !b; michael@0: }; michael@0: } michael@0: d3_selectionPrototype.each = function(callback) { michael@0: return d3_selection_each(this, function(node, i, j) { michael@0: callback.call(node, node.__data__, i, j); michael@0: }); michael@0: }; michael@0: function d3_selection_each(groups, callback) { michael@0: for (var j = 0, m = groups.length; j < m; j++) { michael@0: for (var group = groups[j], i = 0, n = group.length, node; i < n; i++) { michael@0: if (node = group[i]) callback(node, i, j); michael@0: } michael@0: } michael@0: return groups; michael@0: } michael@0: d3_selectionPrototype.call = function(callback) { michael@0: var args = d3_array(arguments); michael@0: callback.apply(args[0] = this, args); michael@0: return this; michael@0: }; michael@0: d3_selectionPrototype.empty = function() { michael@0: return !this.node(); michael@0: }; michael@0: d3_selectionPrototype.node = function() { michael@0: for (var j = 0, m = this.length; j < m; j++) { michael@0: for (var group = this[j], i = 0, n = group.length; i < n; i++) { michael@0: var node = group[i]; michael@0: if (node) return node; michael@0: } michael@0: } michael@0: return null; michael@0: }; michael@0: d3_selectionPrototype.size = function() { michael@0: var n = 0; michael@0: this.each(function() { michael@0: ++n; michael@0: }); michael@0: return n; michael@0: }; michael@0: function d3_selection_enter(selection) { michael@0: d3_subclass(selection, d3_selection_enterPrototype); michael@0: return selection; michael@0: } michael@0: var d3_selection_enterPrototype = []; michael@0: d3.selection.enter = d3_selection_enter; michael@0: d3.selection.enter.prototype = d3_selection_enterPrototype; michael@0: d3_selection_enterPrototype.append = d3_selectionPrototype.append; michael@0: d3_selection_enterPrototype.empty = d3_selectionPrototype.empty; michael@0: d3_selection_enterPrototype.node = d3_selectionPrototype.node; michael@0: d3_selection_enterPrototype.call = d3_selectionPrototype.call; michael@0: d3_selection_enterPrototype.size = d3_selectionPrototype.size; michael@0: d3_selection_enterPrototype.select = function(selector) { michael@0: var subgroups = [], subgroup, subnode, upgroup, group, node; michael@0: for (var j = -1, m = this.length; ++j < m; ) { michael@0: upgroup = (group = this[j]).update; michael@0: subgroups.push(subgroup = []); michael@0: subgroup.parentNode = group.parentNode; michael@0: for (var i = -1, n = group.length; ++i < n; ) { michael@0: if (node = group[i]) { michael@0: subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i, j)); michael@0: subnode.__data__ = node.__data__; michael@0: } else { michael@0: subgroup.push(null); michael@0: } michael@0: } michael@0: } michael@0: return d3_selection(subgroups); michael@0: }; michael@0: d3_selection_enterPrototype.insert = function(name, before) { michael@0: if (arguments.length < 2) before = d3_selection_enterInsertBefore(this); michael@0: return d3_selectionPrototype.insert.call(this, name, before); michael@0: }; michael@0: function d3_selection_enterInsertBefore(enter) { michael@0: var i0, j0; michael@0: return function(d, i, j) { michael@0: var group = enter[j].update, n = group.length, node; michael@0: if (j != j0) j0 = j, i0 = 0; michael@0: if (i >= i0) i0 = i + 1; michael@0: while (!(node = group[i0]) && ++i0 < n) ; michael@0: return node; michael@0: }; michael@0: } michael@0: d3_selectionPrototype.transition = function() { michael@0: var id = d3_transitionInheritId || ++d3_transitionId, subgroups = [], subgroup, node, transition = d3_transitionInherit || { michael@0: time: Date.now(), michael@0: ease: d3_ease_cubicInOut, michael@0: delay: 0, michael@0: duration: 250 michael@0: }; michael@0: for (var j = -1, m = this.length; ++j < m; ) { michael@0: subgroups.push(subgroup = []); michael@0: for (var group = this[j], i = -1, n = group.length; ++i < n; ) { michael@0: if (node = group[i]) d3_transitionNode(node, i, id, transition); michael@0: subgroup.push(node); michael@0: } michael@0: } michael@0: return d3_transition(subgroups, id); michael@0: }; michael@0: d3_selectionPrototype.interrupt = function() { michael@0: return this.each(d3_selection_interrupt); michael@0: }; michael@0: function d3_selection_interrupt() { michael@0: var lock = this.__transition__; michael@0: if (lock) ++lock.active; michael@0: } michael@0: d3.select = function(node) { michael@0: var group = [ typeof node === "string" ? d3_select(node, d3_document) : node ]; michael@0: group.parentNode = d3_documentElement; michael@0: return d3_selection([ group ]); michael@0: }; michael@0: d3.selectAll = function(nodes) { michael@0: var group = d3_array(typeof nodes === "string" ? d3_selectAll(nodes, d3_document) : nodes); michael@0: group.parentNode = d3_documentElement; michael@0: return d3_selection([ group ]); michael@0: }; michael@0: var d3_selectionRoot = d3.select(d3_documentElement); michael@0: d3_selectionPrototype.on = function(type, listener, capture) { michael@0: var n = arguments.length; michael@0: if (n < 3) { michael@0: if (typeof type !== "string") { michael@0: if (n < 2) listener = false; michael@0: for (capture in type) this.each(d3_selection_on(capture, type[capture], listener)); michael@0: return this; michael@0: } michael@0: if (n < 2) return (n = this.node()["__on" + type]) && n._; michael@0: capture = false; michael@0: } michael@0: return this.each(d3_selection_on(type, listener, capture)); michael@0: }; michael@0: function d3_selection_on(type, listener, capture) { michael@0: var name = "__on" + type, i = type.indexOf("."), wrap = d3_selection_onListener; michael@0: if (i > 0) type = type.substring(0, i); michael@0: var filter = d3_selection_onFilters.get(type); michael@0: if (filter) type = filter, wrap = d3_selection_onFilter; michael@0: function onRemove() { michael@0: var l = this[name]; michael@0: if (l) { michael@0: this.removeEventListener(type, l, l.$); michael@0: delete this[name]; michael@0: } michael@0: } michael@0: function onAdd() { michael@0: var l = wrap(listener, d3_array(arguments)); michael@0: onRemove.call(this); michael@0: this.addEventListener(type, this[name] = l, l.$ = capture); michael@0: l._ = listener; michael@0: } michael@0: function removeAll() { michael@0: var re = new RegExp("^__on([^.]+)" + d3.requote(type) + "$"), match; michael@0: for (var name in this) { michael@0: if (match = name.match(re)) { michael@0: var l = this[name]; michael@0: this.removeEventListener(match[1], l, l.$); michael@0: delete this[name]; michael@0: } michael@0: } michael@0: } michael@0: return i ? listener ? onAdd : onRemove : listener ? d3_noop : removeAll; michael@0: } michael@0: var d3_selection_onFilters = d3.map({ michael@0: mouseenter: "mouseover", michael@0: mouseleave: "mouseout" michael@0: }); michael@0: d3_selection_onFilters.forEach(function(k) { michael@0: if ("on" + k in d3_document) d3_selection_onFilters.remove(k); michael@0: }); michael@0: function d3_selection_onListener(listener, argumentz) { michael@0: return function(e) { michael@0: var o = d3.event; michael@0: d3.event = e; michael@0: argumentz[0] = this.__data__; michael@0: try { michael@0: listener.apply(this, argumentz); michael@0: } finally { michael@0: d3.event = o; michael@0: } michael@0: }; michael@0: } michael@0: function d3_selection_onFilter(listener, argumentz) { michael@0: var l = d3_selection_onListener(listener, argumentz); michael@0: return function(e) { michael@0: var target = this, related = e.relatedTarget; michael@0: if (!related || related !== target && !(related.compareDocumentPosition(target) & 8)) { michael@0: l.call(target, e); michael@0: } michael@0: }; michael@0: } michael@0: var d3_event_dragSelect = "onselectstart" in d3_document ? null : d3_vendorSymbol(d3_documentElement.style, "userSelect"), d3_event_dragId = 0; michael@0: function d3_event_dragSuppress() { michael@0: var name = ".dragsuppress-" + ++d3_event_dragId, click = "click" + name, w = d3.select(d3_window).on("touchmove" + name, d3_eventPreventDefault).on("dragstart" + name, d3_eventPreventDefault).on("selectstart" + name, d3_eventPreventDefault); michael@0: if (d3_event_dragSelect) { michael@0: var style = d3_documentElement.style, select = style[d3_event_dragSelect]; michael@0: style[d3_event_dragSelect] = "none"; michael@0: } michael@0: return function(suppressClick) { michael@0: w.on(name, null); michael@0: if (d3_event_dragSelect) style[d3_event_dragSelect] = select; michael@0: if (suppressClick) { michael@0: function off() { michael@0: w.on(click, null); michael@0: } michael@0: w.on(click, function() { michael@0: d3_eventPreventDefault(); michael@0: off(); michael@0: }, true); michael@0: setTimeout(off, 0); michael@0: } michael@0: }; michael@0: } michael@0: d3.mouse = function(container) { michael@0: return d3_mousePoint(container, d3_eventSource()); michael@0: }; michael@0: var d3_mouse_bug44083 = /WebKit/.test(d3_window.navigator.userAgent) ? -1 : 0; michael@0: function d3_mousePoint(container, e) { michael@0: if (e.changedTouches) e = e.changedTouches[0]; michael@0: var svg = container.ownerSVGElement || container; michael@0: if (svg.createSVGPoint) { michael@0: var point = svg.createSVGPoint(); michael@0: if (d3_mouse_bug44083 < 0 && (d3_window.scrollX || d3_window.scrollY)) { michael@0: svg = d3.select("body").append("svg").style({ michael@0: position: "absolute", michael@0: top: 0, michael@0: left: 0, michael@0: margin: 0, michael@0: padding: 0, michael@0: border: "none" michael@0: }, "important"); michael@0: var ctm = svg[0][0].getScreenCTM(); michael@0: d3_mouse_bug44083 = !(ctm.f || ctm.e); michael@0: svg.remove(); michael@0: } michael@0: if (d3_mouse_bug44083) point.x = e.pageX, point.y = e.pageY; else point.x = e.clientX, michael@0: point.y = e.clientY; michael@0: point = point.matrixTransform(container.getScreenCTM().inverse()); michael@0: return [ point.x, point.y ]; michael@0: } michael@0: var rect = container.getBoundingClientRect(); michael@0: return [ e.clientX - rect.left - container.clientLeft, e.clientY - rect.top - container.clientTop ]; michael@0: } michael@0: d3.touches = function(container, touches) { michael@0: if (arguments.length < 2) touches = d3_eventSource().touches; michael@0: return touches ? d3_array(touches).map(function(touch) { michael@0: var point = d3_mousePoint(container, touch); michael@0: point.identifier = touch.identifier; michael@0: return point; michael@0: }) : []; michael@0: }; michael@0: d3.behavior.drag = function() { michael@0: var event = d3_eventDispatch(drag, "drag", "dragstart", "dragend"), origin = null, mousedown = dragstart(d3_noop, d3.mouse, "mousemove", "mouseup"), touchstart = dragstart(touchid, touchposition, "touchmove", "touchend"); michael@0: function drag() { michael@0: this.on("mousedown.drag", mousedown).on("touchstart.drag", touchstart); michael@0: } michael@0: function touchid() { michael@0: return d3.event.changedTouches[0].identifier; michael@0: } michael@0: function touchposition(parent, id) { michael@0: return d3.touches(parent).filter(function(p) { michael@0: return p.identifier === id; michael@0: })[0]; michael@0: } michael@0: function dragstart(id, position, move, end) { michael@0: return function() { michael@0: var target = this, parent = target.parentNode, event_ = event.of(target, arguments), eventTarget = d3.event.target, eventId = id(), drag = eventId == null ? "drag" : "drag-" + eventId, origin_ = position(parent, eventId), dragged = 0, offset, w = d3.select(d3_window).on(move + "." + drag, moved).on(end + "." + drag, ended), dragRestore = d3_event_dragSuppress(); michael@0: if (origin) { michael@0: offset = origin.apply(target, arguments); michael@0: offset = [ offset.x - origin_[0], offset.y - origin_[1] ]; michael@0: } else { michael@0: offset = [ 0, 0 ]; michael@0: } michael@0: event_({ michael@0: type: "dragstart" michael@0: }); michael@0: function moved() { michael@0: var p = position(parent, eventId), dx = p[0] - origin_[0], dy = p[1] - origin_[1]; michael@0: dragged |= dx | dy; michael@0: origin_ = p; michael@0: event_({ michael@0: type: "drag", michael@0: x: p[0] + offset[0], michael@0: y: p[1] + offset[1], michael@0: dx: dx, michael@0: dy: dy michael@0: }); michael@0: } michael@0: function ended() { michael@0: w.on(move + "." + drag, null).on(end + "." + drag, null); michael@0: dragRestore(dragged && d3.event.target === eventTarget); michael@0: event_({ michael@0: type: "dragend" michael@0: }); michael@0: } michael@0: }; michael@0: } michael@0: drag.origin = function(x) { michael@0: if (!arguments.length) return origin; michael@0: origin = x; michael@0: return drag; michael@0: }; michael@0: return d3.rebind(drag, event, "on"); michael@0: }; michael@0: var π = Math.PI, τ = 2 * π, halfπ = π / 2, ε = 1e-6, ε2 = ε * ε, d3_radians = π / 180, d3_degrees = 180 / π; michael@0: function d3_sgn(x) { michael@0: return x > 0 ? 1 : x < 0 ? -1 : 0; michael@0: } michael@0: function d3_cross2d(a, b, c) { michael@0: return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); michael@0: } michael@0: function d3_acos(x) { michael@0: return x > 1 ? 0 : x < -1 ? π : Math.acos(x); michael@0: } michael@0: function d3_asin(x) { michael@0: return x > 1 ? halfπ : x < -1 ? -halfπ : Math.asin(x); michael@0: } michael@0: function d3_sinh(x) { michael@0: return ((x = Math.exp(x)) - 1 / x) / 2; michael@0: } michael@0: function d3_cosh(x) { michael@0: return ((x = Math.exp(x)) + 1 / x) / 2; michael@0: } michael@0: function d3_tanh(x) { michael@0: return ((x = Math.exp(2 * x)) - 1) / (x + 1); michael@0: } michael@0: function d3_haversin(x) { michael@0: return (x = Math.sin(x / 2)) * x; michael@0: } michael@0: var ρ = Math.SQRT2, ρ2 = 2, ρ4 = 4; michael@0: d3.interpolateZoom = function(p0, p1) { michael@0: var ux0 = p0[0], uy0 = p0[1], w0 = p0[2], ux1 = p1[0], uy1 = p1[1], w1 = p1[2]; michael@0: var dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, d1 = Math.sqrt(d2), b0 = (w1 * w1 - w0 * w0 + ρ4 * d2) / (2 * w0 * ρ2 * d1), b1 = (w1 * w1 - w0 * w0 - ρ4 * d2) / (2 * w1 * ρ2 * d1), r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0), r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1), dr = r1 - r0, S = (dr || Math.log(w1 / w0)) / ρ; michael@0: function interpolate(t) { michael@0: var s = t * S; michael@0: if (dr) { michael@0: var coshr0 = d3_cosh(r0), u = w0 / (ρ2 * d1) * (coshr0 * d3_tanh(ρ * s + r0) - d3_sinh(r0)); michael@0: return [ ux0 + u * dx, uy0 + u * dy, w0 * coshr0 / d3_cosh(ρ * s + r0) ]; michael@0: } michael@0: return [ ux0 + t * dx, uy0 + t * dy, w0 * Math.exp(ρ * s) ]; michael@0: } michael@0: interpolate.duration = S * 1e3; michael@0: return interpolate; michael@0: }; michael@0: d3.behavior.zoom = function() { michael@0: var view = { michael@0: x: 0, michael@0: y: 0, michael@0: k: 1 michael@0: }, translate0, center, size = [ 960, 500 ], scaleExtent = d3_behavior_zoomInfinity, mousedown = "mousedown.zoom", mousemove = "mousemove.zoom", mouseup = "mouseup.zoom", mousewheelTimer, touchstart = "touchstart.zoom", touchtime, event = d3_eventDispatch(zoom, "zoomstart", "zoom", "zoomend"), x0, x1, y0, y1; michael@0: function zoom(g) { michael@0: g.on(mousedown, mousedowned).on(d3_behavior_zoomWheel + ".zoom", mousewheeled).on(mousemove, mousewheelreset).on("dblclick.zoom", dblclicked).on(touchstart, touchstarted); michael@0: } michael@0: zoom.event = function(g) { michael@0: g.each(function() { michael@0: var event_ = event.of(this, arguments), view1 = view; michael@0: if (d3_transitionInheritId) { michael@0: d3.select(this).transition().each("start.zoom", function() { michael@0: view = this.__chart__ || { michael@0: x: 0, michael@0: y: 0, michael@0: k: 1 michael@0: }; michael@0: zoomstarted(event_); michael@0: }).tween("zoom:zoom", function() { michael@0: var dx = size[0], dy = size[1], cx = dx / 2, cy = dy / 2, i = d3.interpolateZoom([ (cx - view.x) / view.k, (cy - view.y) / view.k, dx / view.k ], [ (cx - view1.x) / view1.k, (cy - view1.y) / view1.k, dx / view1.k ]); michael@0: return function(t) { michael@0: var l = i(t), k = dx / l[2]; michael@0: this.__chart__ = view = { michael@0: x: cx - l[0] * k, michael@0: y: cy - l[1] * k, michael@0: k: k michael@0: }; michael@0: zoomed(event_); michael@0: }; michael@0: }).each("end.zoom", function() { michael@0: zoomended(event_); michael@0: }); michael@0: } else { michael@0: this.__chart__ = view; michael@0: zoomstarted(event_); michael@0: zoomed(event_); michael@0: zoomended(event_); michael@0: } michael@0: }); michael@0: }; michael@0: zoom.translate = function(_) { michael@0: if (!arguments.length) return [ view.x, view.y ]; michael@0: view = { michael@0: x: +_[0], michael@0: y: +_[1], michael@0: k: view.k michael@0: }; michael@0: rescale(); michael@0: return zoom; michael@0: }; michael@0: zoom.scale = function(_) { michael@0: if (!arguments.length) return view.k; michael@0: view = { michael@0: x: view.x, michael@0: y: view.y, michael@0: k: +_ michael@0: }; michael@0: rescale(); michael@0: return zoom; michael@0: }; michael@0: zoom.scaleExtent = function(_) { michael@0: if (!arguments.length) return scaleExtent; michael@0: scaleExtent = _ == null ? d3_behavior_zoomInfinity : [ +_[0], +_[1] ]; michael@0: return zoom; michael@0: }; michael@0: zoom.center = function(_) { michael@0: if (!arguments.length) return center; michael@0: center = _ && [ +_[0], +_[1] ]; michael@0: return zoom; michael@0: }; michael@0: zoom.size = function(_) { michael@0: if (!arguments.length) return size; michael@0: size = _ && [ +_[0], +_[1] ]; michael@0: return zoom; michael@0: }; michael@0: zoom.x = function(z) { michael@0: if (!arguments.length) return x1; michael@0: x1 = z; michael@0: x0 = z.copy(); michael@0: view = { michael@0: x: 0, michael@0: y: 0, michael@0: k: 1 michael@0: }; michael@0: return zoom; michael@0: }; michael@0: zoom.y = function(z) { michael@0: if (!arguments.length) return y1; michael@0: y1 = z; michael@0: y0 = z.copy(); michael@0: view = { michael@0: x: 0, michael@0: y: 0, michael@0: k: 1 michael@0: }; michael@0: return zoom; michael@0: }; michael@0: function location(p) { michael@0: return [ (p[0] - view.x) / view.k, (p[1] - view.y) / view.k ]; michael@0: } michael@0: function point(l) { michael@0: return [ l[0] * view.k + view.x, l[1] * view.k + view.y ]; michael@0: } michael@0: function scaleTo(s) { michael@0: view.k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], s)); michael@0: } michael@0: function translateTo(p, l) { michael@0: l = point(l); michael@0: view.x += p[0] - l[0]; michael@0: view.y += p[1] - l[1]; michael@0: } michael@0: function rescale() { michael@0: if (x1) x1.domain(x0.range().map(function(x) { michael@0: return (x - view.x) / view.k; michael@0: }).map(x0.invert)); michael@0: if (y1) y1.domain(y0.range().map(function(y) { michael@0: return (y - view.y) / view.k; michael@0: }).map(y0.invert)); michael@0: } michael@0: function zoomstarted(event) { michael@0: event({ michael@0: type: "zoomstart" michael@0: }); michael@0: } michael@0: function zoomed(event) { michael@0: rescale(); michael@0: event({ michael@0: type: "zoom", michael@0: scale: view.k, michael@0: translate: [ view.x, view.y ] michael@0: }); michael@0: } michael@0: function zoomended(event) { michael@0: event({ michael@0: type: "zoomend" michael@0: }); michael@0: } michael@0: function mousedowned() { michael@0: var target = this, event_ = event.of(target, arguments), eventTarget = d3.event.target, dragged = 0, w = d3.select(d3_window).on(mousemove, moved).on(mouseup, ended), l = location(d3.mouse(target)), dragRestore = d3_event_dragSuppress(); michael@0: d3_selection_interrupt.call(target); michael@0: zoomstarted(event_); michael@0: function moved() { michael@0: dragged = 1; michael@0: translateTo(d3.mouse(target), l); michael@0: zoomed(event_); michael@0: } michael@0: function ended() { michael@0: w.on(mousemove, d3_window === target ? mousewheelreset : null).on(mouseup, null); michael@0: dragRestore(dragged && d3.event.target === eventTarget); michael@0: zoomended(event_); michael@0: } michael@0: } michael@0: function touchstarted() { michael@0: var target = this, event_ = event.of(target, arguments), locations0 = {}, distance0 = 0, scale0, eventId = d3.event.changedTouches[0].identifier, touchmove = "touchmove.zoom-" + eventId, touchend = "touchend.zoom-" + eventId, w = d3.select(d3_window).on(touchmove, moved).on(touchend, ended), t = d3.select(target).on(mousedown, null).on(touchstart, started), dragRestore = d3_event_dragSuppress(); michael@0: d3_selection_interrupt.call(target); michael@0: started(); michael@0: zoomstarted(event_); michael@0: function relocate() { michael@0: var touches = d3.touches(target); michael@0: scale0 = view.k; michael@0: touches.forEach(function(t) { michael@0: if (t.identifier in locations0) locations0[t.identifier] = location(t); michael@0: }); michael@0: return touches; michael@0: } michael@0: function started() { michael@0: var changed = d3.event.changedTouches; michael@0: for (var i = 0, n = changed.length; i < n; ++i) { michael@0: locations0[changed[i].identifier] = null; michael@0: } michael@0: var touches = relocate(), now = Date.now(); michael@0: if (touches.length === 1) { michael@0: if (now - touchtime < 500) { michael@0: var p = touches[0], l = locations0[p.identifier]; michael@0: scaleTo(view.k * 2); michael@0: translateTo(p, l); michael@0: d3_eventPreventDefault(); michael@0: zoomed(event_); michael@0: } michael@0: touchtime = now; michael@0: } else if (touches.length > 1) { michael@0: var p = touches[0], q = touches[1], dx = p[0] - q[0], dy = p[1] - q[1]; michael@0: distance0 = dx * dx + dy * dy; michael@0: } michael@0: } michael@0: function moved() { michael@0: var touches = d3.touches(target), p0, l0, p1, l1; michael@0: for (var i = 0, n = touches.length; i < n; ++i, l1 = null) { michael@0: p1 = touches[i]; michael@0: if (l1 = locations0[p1.identifier]) { michael@0: if (l0) break; michael@0: p0 = p1, l0 = l1; michael@0: } michael@0: } michael@0: if (l1) { michael@0: var distance1 = (distance1 = p1[0] - p0[0]) * distance1 + (distance1 = p1[1] - p0[1]) * distance1, scale1 = distance0 && Math.sqrt(distance1 / distance0); michael@0: p0 = [ (p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2 ]; michael@0: l0 = [ (l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2 ]; michael@0: scaleTo(scale1 * scale0); michael@0: } michael@0: touchtime = null; michael@0: translateTo(p0, l0); michael@0: zoomed(event_); michael@0: } michael@0: function ended() { michael@0: if (d3.event.touches.length) { michael@0: var changed = d3.event.changedTouches; michael@0: for (var i = 0, n = changed.length; i < n; ++i) { michael@0: delete locations0[changed[i].identifier]; michael@0: } michael@0: for (var identifier in locations0) { michael@0: return void relocate(); michael@0: } michael@0: } michael@0: w.on(touchmove, null).on(touchend, null); michael@0: t.on(mousedown, mousedowned).on(touchstart, touchstarted); michael@0: dragRestore(); michael@0: zoomended(event_); michael@0: } michael@0: } michael@0: function mousewheeled() { michael@0: var event_ = event.of(this, arguments); michael@0: if (mousewheelTimer) clearTimeout(mousewheelTimer); else d3_selection_interrupt.call(this), michael@0: zoomstarted(event_); michael@0: mousewheelTimer = setTimeout(function() { michael@0: mousewheelTimer = null; michael@0: zoomended(event_); michael@0: }, 50); michael@0: d3_eventPreventDefault(); michael@0: var point = center || d3.mouse(this); michael@0: if (!translate0) translate0 = location(point); michael@0: scaleTo(Math.pow(2, d3_behavior_zoomDelta() * .002) * view.k); michael@0: translateTo(point, translate0); michael@0: zoomed(event_); michael@0: } michael@0: function mousewheelreset() { michael@0: translate0 = null; michael@0: } michael@0: function dblclicked() { michael@0: var event_ = event.of(this, arguments), p = d3.mouse(this), l = location(p), k = Math.log(view.k) / Math.LN2; michael@0: zoomstarted(event_); michael@0: scaleTo(Math.pow(2, d3.event.shiftKey ? Math.ceil(k) - 1 : Math.floor(k) + 1)); michael@0: translateTo(p, l); michael@0: zoomed(event_); michael@0: zoomended(event_); michael@0: } michael@0: return d3.rebind(zoom, event, "on"); michael@0: }; michael@0: var d3_behavior_zoomInfinity = [ 0, Infinity ]; michael@0: var d3_behavior_zoomDelta, d3_behavior_zoomWheel = "onwheel" in d3_document ? (d3_behavior_zoomDelta = function() { michael@0: return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1); michael@0: }, "wheel") : "onmousewheel" in d3_document ? (d3_behavior_zoomDelta = function() { michael@0: return d3.event.wheelDelta; michael@0: }, "mousewheel") : (d3_behavior_zoomDelta = function() { michael@0: return -d3.event.detail; michael@0: }, "MozMousePixelScroll"); michael@0: function d3_Color() {} michael@0: d3_Color.prototype.toString = function() { michael@0: return this.rgb() + ""; michael@0: }; michael@0: d3.hsl = function(h, s, l) { michael@0: return arguments.length === 1 ? h instanceof d3_Hsl ? d3_hsl(h.h, h.s, h.l) : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl) : d3_hsl(+h, +s, +l); michael@0: }; michael@0: function d3_hsl(h, s, l) { michael@0: return new d3_Hsl(h, s, l); michael@0: } michael@0: function d3_Hsl(h, s, l) { michael@0: this.h = h; michael@0: this.s = s; michael@0: this.l = l; michael@0: } michael@0: var d3_hslPrototype = d3_Hsl.prototype = new d3_Color(); michael@0: d3_hslPrototype.brighter = function(k) { michael@0: k = Math.pow(.7, arguments.length ? k : 1); michael@0: return d3_hsl(this.h, this.s, this.l / k); michael@0: }; michael@0: d3_hslPrototype.darker = function(k) { michael@0: k = Math.pow(.7, arguments.length ? k : 1); michael@0: return d3_hsl(this.h, this.s, k * this.l); michael@0: }; michael@0: d3_hslPrototype.rgb = function() { michael@0: return d3_hsl_rgb(this.h, this.s, this.l); michael@0: }; michael@0: function d3_hsl_rgb(h, s, l) { michael@0: var m1, m2; michael@0: h = isNaN(h) ? 0 : (h %= 360) < 0 ? h + 360 : h; michael@0: s = isNaN(s) ? 0 : s < 0 ? 0 : s > 1 ? 1 : s; michael@0: l = l < 0 ? 0 : l > 1 ? 1 : l; michael@0: m2 = l <= .5 ? l * (1 + s) : l + s - l * s; michael@0: m1 = 2 * l - m2; michael@0: function v(h) { michael@0: if (h > 360) h -= 360; else if (h < 0) h += 360; michael@0: if (h < 60) return m1 + (m2 - m1) * h / 60; michael@0: if (h < 180) return m2; michael@0: if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60; michael@0: return m1; michael@0: } michael@0: function vv(h) { michael@0: return Math.round(v(h) * 255); michael@0: } michael@0: return d3_rgb(vv(h + 120), vv(h), vv(h - 120)); michael@0: } michael@0: d3.hcl = function(h, c, l) { michael@0: return arguments.length === 1 ? h instanceof d3_Hcl ? d3_hcl(h.h, h.c, h.l) : h instanceof d3_Lab ? d3_lab_hcl(h.l, h.a, h.b) : d3_lab_hcl((h = d3_rgb_lab((h = d3.rgb(h)).r, h.g, h.b)).l, h.a, h.b) : d3_hcl(+h, +c, +l); michael@0: }; michael@0: function d3_hcl(h, c, l) { michael@0: return new d3_Hcl(h, c, l); michael@0: } michael@0: function d3_Hcl(h, c, l) { michael@0: this.h = h; michael@0: this.c = c; michael@0: this.l = l; michael@0: } michael@0: var d3_hclPrototype = d3_Hcl.prototype = new d3_Color(); michael@0: d3_hclPrototype.brighter = function(k) { michael@0: return d3_hcl(this.h, this.c, Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1))); michael@0: }; michael@0: d3_hclPrototype.darker = function(k) { michael@0: return d3_hcl(this.h, this.c, Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1))); michael@0: }; michael@0: d3_hclPrototype.rgb = function() { michael@0: return d3_hcl_lab(this.h, this.c, this.l).rgb(); michael@0: }; michael@0: function d3_hcl_lab(h, c, l) { michael@0: if (isNaN(h)) h = 0; michael@0: if (isNaN(c)) c = 0; michael@0: return d3_lab(l, Math.cos(h *= d3_radians) * c, Math.sin(h) * c); michael@0: } michael@0: d3.lab = function(l, a, b) { michael@0: return arguments.length === 1 ? l instanceof d3_Lab ? d3_lab(l.l, l.a, l.b) : l instanceof d3_Hcl ? d3_hcl_lab(l.l, l.c, l.h) : d3_rgb_lab((l = d3.rgb(l)).r, l.g, l.b) : d3_lab(+l, +a, +b); michael@0: }; michael@0: function d3_lab(l, a, b) { michael@0: return new d3_Lab(l, a, b); michael@0: } michael@0: function d3_Lab(l, a, b) { michael@0: this.l = l; michael@0: this.a = a; michael@0: this.b = b; michael@0: } michael@0: var d3_lab_K = 18; michael@0: var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883; michael@0: var d3_labPrototype = d3_Lab.prototype = new d3_Color(); michael@0: d3_labPrototype.brighter = function(k) { michael@0: return d3_lab(Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); michael@0: }; michael@0: d3_labPrototype.darker = function(k) { michael@0: return d3_lab(Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); michael@0: }; michael@0: d3_labPrototype.rgb = function() { michael@0: return d3_lab_rgb(this.l, this.a, this.b); michael@0: }; michael@0: function d3_lab_rgb(l, a, b) { michael@0: var y = (l + 16) / 116, x = y + a / 500, z = y - b / 200; michael@0: x = d3_lab_xyz(x) * d3_lab_X; michael@0: y = d3_lab_xyz(y) * d3_lab_Y; michael@0: z = d3_lab_xyz(z) * d3_lab_Z; michael@0: return d3_rgb(d3_xyz_rgb(3.2404542 * x - 1.5371385 * y - .4985314 * z), d3_xyz_rgb(-.969266 * x + 1.8760108 * y + .041556 * z), d3_xyz_rgb(.0556434 * x - .2040259 * y + 1.0572252 * z)); michael@0: } michael@0: function d3_lab_hcl(l, a, b) { michael@0: return l > 0 ? d3_hcl(Math.atan2(b, a) * d3_degrees, Math.sqrt(a * a + b * b), l) : d3_hcl(NaN, NaN, l); michael@0: } michael@0: function d3_lab_xyz(x) { michael@0: return x > .206893034 ? x * x * x : (x - 4 / 29) / 7.787037; michael@0: } michael@0: function d3_xyz_lab(x) { michael@0: return x > .008856 ? Math.pow(x, 1 / 3) : 7.787037 * x + 4 / 29; michael@0: } michael@0: function d3_xyz_rgb(r) { michael@0: return Math.round(255 * (r <= .00304 ? 12.92 * r : 1.055 * Math.pow(r, 1 / 2.4) - .055)); michael@0: } michael@0: d3.rgb = function(r, g, b) { michael@0: return arguments.length === 1 ? r instanceof d3_Rgb ? d3_rgb(r.r, r.g, r.b) : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb) : d3_rgb(~~r, ~~g, ~~b); michael@0: }; michael@0: function d3_rgbNumber(value) { michael@0: return d3_rgb(value >> 16, value >> 8 & 255, value & 255); michael@0: } michael@0: function d3_rgbString(value) { michael@0: return d3_rgbNumber(value) + ""; michael@0: } michael@0: function d3_rgb(r, g, b) { michael@0: return new d3_Rgb(r, g, b); michael@0: } michael@0: function d3_Rgb(r, g, b) { michael@0: this.r = r; michael@0: this.g = g; michael@0: this.b = b; michael@0: } michael@0: var d3_rgbPrototype = d3_Rgb.prototype = new d3_Color(); michael@0: d3_rgbPrototype.brighter = function(k) { michael@0: k = Math.pow(.7, arguments.length ? k : 1); michael@0: var r = this.r, g = this.g, b = this.b, i = 30; michael@0: if (!r && !g && !b) return d3_rgb(i, i, i); michael@0: if (r && r < i) r = i; michael@0: if (g && g < i) g = i; michael@0: if (b && b < i) b = i; michael@0: return d3_rgb(Math.min(255, ~~(r / k)), Math.min(255, ~~(g / k)), Math.min(255, ~~(b / k))); michael@0: }; michael@0: d3_rgbPrototype.darker = function(k) { michael@0: k = Math.pow(.7, arguments.length ? k : 1); michael@0: return d3_rgb(~~(k * this.r), ~~(k * this.g), ~~(k * this.b)); michael@0: }; michael@0: d3_rgbPrototype.hsl = function() { michael@0: return d3_rgb_hsl(this.r, this.g, this.b); michael@0: }; michael@0: d3_rgbPrototype.toString = function() { michael@0: return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b); michael@0: }; michael@0: function d3_rgb_hex(v) { michael@0: return v < 16 ? "0" + Math.max(0, v).toString(16) : Math.min(255, v).toString(16); michael@0: } michael@0: function d3_rgb_parse(format, rgb, hsl) { michael@0: var r = 0, g = 0, b = 0, m1, m2, name; michael@0: m1 = /([a-z]+)\((.*)\)/i.exec(format); michael@0: if (m1) { michael@0: m2 = m1[2].split(","); michael@0: switch (m1[1]) { michael@0: case "hsl": michael@0: { michael@0: return hsl(parseFloat(m2[0]), parseFloat(m2[1]) / 100, parseFloat(m2[2]) / 100); michael@0: } michael@0: michael@0: case "rgb": michael@0: { michael@0: return rgb(d3_rgb_parseNumber(m2[0]), d3_rgb_parseNumber(m2[1]), d3_rgb_parseNumber(m2[2])); michael@0: } michael@0: } michael@0: } michael@0: if (name = d3_rgb_names.get(format)) return rgb(name.r, name.g, name.b); michael@0: if (format != null && format.charAt(0) === "#") { michael@0: if (format.length === 4) { michael@0: r = format.charAt(1); michael@0: r += r; michael@0: g = format.charAt(2); michael@0: g += g; michael@0: b = format.charAt(3); michael@0: b += b; michael@0: } else if (format.length === 7) { michael@0: r = format.substring(1, 3); michael@0: g = format.substring(3, 5); michael@0: b = format.substring(5, 7); michael@0: } michael@0: r = parseInt(r, 16); michael@0: g = parseInt(g, 16); michael@0: b = parseInt(b, 16); michael@0: } michael@0: return rgb(r, g, b); michael@0: } michael@0: function d3_rgb_hsl(r, g, b) { michael@0: var min = Math.min(r /= 255, g /= 255, b /= 255), max = Math.max(r, g, b), d = max - min, h, s, l = (max + min) / 2; michael@0: if (d) { michael@0: s = l < .5 ? d / (max + min) : d / (2 - max - min); michael@0: if (r == max) h = (g - b) / d + (g < b ? 6 : 0); else if (g == max) h = (b - r) / d + 2; else h = (r - g) / d + 4; michael@0: h *= 60; michael@0: } else { michael@0: h = NaN; michael@0: s = l > 0 && l < 1 ? 0 : h; michael@0: } michael@0: return d3_hsl(h, s, l); michael@0: } michael@0: function d3_rgb_lab(r, g, b) { michael@0: r = d3_rgb_xyz(r); michael@0: g = d3_rgb_xyz(g); michael@0: b = d3_rgb_xyz(b); michael@0: var x = d3_xyz_lab((.4124564 * r + .3575761 * g + .1804375 * b) / d3_lab_X), y = d3_xyz_lab((.2126729 * r + .7151522 * g + .072175 * b) / d3_lab_Y), z = d3_xyz_lab((.0193339 * r + .119192 * g + .9503041 * b) / d3_lab_Z); michael@0: return d3_lab(116 * y - 16, 500 * (x - y), 200 * (y - z)); michael@0: } michael@0: function d3_rgb_xyz(r) { michael@0: return (r /= 255) <= .04045 ? r / 12.92 : Math.pow((r + .055) / 1.055, 2.4); michael@0: } michael@0: function d3_rgb_parseNumber(c) { michael@0: var f = parseFloat(c); michael@0: return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f; michael@0: } michael@0: var d3_rgb_names = d3.map({ michael@0: aliceblue: 15792383, michael@0: antiquewhite: 16444375, michael@0: aqua: 65535, michael@0: aquamarine: 8388564, michael@0: azure: 15794175, michael@0: beige: 16119260, michael@0: bisque: 16770244, michael@0: black: 0, michael@0: blanchedalmond: 16772045, michael@0: blue: 255, michael@0: blueviolet: 9055202, michael@0: brown: 10824234, michael@0: burlywood: 14596231, michael@0: cadetblue: 6266528, michael@0: chartreuse: 8388352, michael@0: chocolate: 13789470, michael@0: coral: 16744272, michael@0: cornflowerblue: 6591981, michael@0: cornsilk: 16775388, michael@0: crimson: 14423100, michael@0: cyan: 65535, michael@0: darkblue: 139, michael@0: darkcyan: 35723, michael@0: darkgoldenrod: 12092939, michael@0: darkgray: 11119017, michael@0: darkgreen: 25600, michael@0: darkgrey: 11119017, michael@0: darkkhaki: 12433259, michael@0: darkmagenta: 9109643, michael@0: darkolivegreen: 5597999, michael@0: darkorange: 16747520, michael@0: darkorchid: 10040012, michael@0: darkred: 9109504, michael@0: darksalmon: 15308410, michael@0: darkseagreen: 9419919, michael@0: darkslateblue: 4734347, michael@0: darkslategray: 3100495, michael@0: darkslategrey: 3100495, michael@0: darkturquoise: 52945, michael@0: darkviolet: 9699539, michael@0: deeppink: 16716947, michael@0: deepskyblue: 49151, michael@0: dimgray: 6908265, michael@0: dimgrey: 6908265, michael@0: dodgerblue: 2003199, michael@0: firebrick: 11674146, michael@0: floralwhite: 16775920, michael@0: forestgreen: 2263842, michael@0: fuchsia: 16711935, michael@0: gainsboro: 14474460, michael@0: ghostwhite: 16316671, michael@0: gold: 16766720, michael@0: goldenrod: 14329120, michael@0: gray: 8421504, michael@0: green: 32768, michael@0: greenyellow: 11403055, michael@0: grey: 8421504, michael@0: honeydew: 15794160, michael@0: hotpink: 16738740, michael@0: indianred: 13458524, michael@0: indigo: 4915330, michael@0: ivory: 16777200, michael@0: khaki: 15787660, michael@0: lavender: 15132410, michael@0: lavenderblush: 16773365, michael@0: lawngreen: 8190976, michael@0: lemonchiffon: 16775885, michael@0: lightblue: 11393254, michael@0: lightcoral: 15761536, michael@0: lightcyan: 14745599, michael@0: lightgoldenrodyellow: 16448210, michael@0: lightgray: 13882323, michael@0: lightgreen: 9498256, michael@0: lightgrey: 13882323, michael@0: lightpink: 16758465, michael@0: lightsalmon: 16752762, michael@0: lightseagreen: 2142890, michael@0: lightskyblue: 8900346, michael@0: lightslategray: 7833753, michael@0: lightslategrey: 7833753, michael@0: lightsteelblue: 11584734, michael@0: lightyellow: 16777184, michael@0: lime: 65280, michael@0: limegreen: 3329330, michael@0: linen: 16445670, michael@0: magenta: 16711935, michael@0: maroon: 8388608, michael@0: mediumaquamarine: 6737322, michael@0: mediumblue: 205, michael@0: mediumorchid: 12211667, michael@0: mediumpurple: 9662683, michael@0: mediumseagreen: 3978097, michael@0: mediumslateblue: 8087790, michael@0: mediumspringgreen: 64154, michael@0: mediumturquoise: 4772300, michael@0: mediumvioletred: 13047173, michael@0: midnightblue: 1644912, michael@0: mintcream: 16121850, michael@0: mistyrose: 16770273, michael@0: moccasin: 16770229, michael@0: navajowhite: 16768685, michael@0: navy: 128, michael@0: oldlace: 16643558, michael@0: olive: 8421376, michael@0: olivedrab: 7048739, michael@0: orange: 16753920, michael@0: orangered: 16729344, michael@0: orchid: 14315734, michael@0: palegoldenrod: 15657130, michael@0: palegreen: 10025880, michael@0: paleturquoise: 11529966, michael@0: palevioletred: 14381203, michael@0: papayawhip: 16773077, michael@0: peachpuff: 16767673, michael@0: peru: 13468991, michael@0: pink: 16761035, michael@0: plum: 14524637, michael@0: powderblue: 11591910, michael@0: purple: 8388736, michael@0: red: 16711680, michael@0: rosybrown: 12357519, michael@0: royalblue: 4286945, michael@0: saddlebrown: 9127187, michael@0: salmon: 16416882, michael@0: sandybrown: 16032864, michael@0: seagreen: 3050327, michael@0: seashell: 16774638, michael@0: sienna: 10506797, michael@0: silver: 12632256, michael@0: skyblue: 8900331, michael@0: slateblue: 6970061, michael@0: slategray: 7372944, michael@0: slategrey: 7372944, michael@0: snow: 16775930, michael@0: springgreen: 65407, michael@0: steelblue: 4620980, michael@0: tan: 13808780, michael@0: teal: 32896, michael@0: thistle: 14204888, michael@0: tomato: 16737095, michael@0: turquoise: 4251856, michael@0: violet: 15631086, michael@0: wheat: 16113331, michael@0: white: 16777215, michael@0: whitesmoke: 16119285, michael@0: yellow: 16776960, michael@0: yellowgreen: 10145074 michael@0: }); michael@0: d3_rgb_names.forEach(function(key, value) { michael@0: d3_rgb_names.set(key, d3_rgbNumber(value)); michael@0: }); michael@0: function d3_functor(v) { michael@0: return typeof v === "function" ? v : function() { michael@0: return v; michael@0: }; michael@0: } michael@0: d3.functor = d3_functor; michael@0: function d3_identity(d) { michael@0: return d; michael@0: } michael@0: d3.xhr = d3_xhrType(d3_identity); michael@0: function d3_xhrType(response) { michael@0: return function(url, mimeType, callback) { michael@0: if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType, michael@0: mimeType = null; michael@0: return d3_xhr(url, mimeType, response, callback); michael@0: }; michael@0: } michael@0: function d3_xhr(url, mimeType, response, callback) { michael@0: var xhr = {}, dispatch = d3.dispatch("beforesend", "progress", "load", "error"), headers = {}, request = new XMLHttpRequest(), responseType = null; michael@0: if (d3_window.XDomainRequest && !("withCredentials" in request) && /^(http(s)?:)?\/\//.test(url)) request = new XDomainRequest(); michael@0: "onload" in request ? request.onload = request.onerror = respond : request.onreadystatechange = function() { michael@0: request.readyState > 3 && respond(); michael@0: }; michael@0: function respond() { michael@0: var status = request.status, result; michael@0: if (!status && request.responseText || status >= 200 && status < 300 || status === 304) { michael@0: try { michael@0: result = response.call(xhr, request); michael@0: } catch (e) { michael@0: dispatch.error.call(xhr, e); michael@0: return; michael@0: } michael@0: dispatch.load.call(xhr, result); michael@0: } else { michael@0: dispatch.error.call(xhr, request); michael@0: } michael@0: } michael@0: request.onprogress = function(event) { michael@0: var o = d3.event; michael@0: d3.event = event; michael@0: try { michael@0: dispatch.progress.call(xhr, request); michael@0: } finally { michael@0: d3.event = o; michael@0: } michael@0: }; michael@0: xhr.header = function(name, value) { michael@0: name = (name + "").toLowerCase(); michael@0: if (arguments.length < 2) return headers[name]; michael@0: if (value == null) delete headers[name]; else headers[name] = value + ""; michael@0: return xhr; michael@0: }; michael@0: xhr.mimeType = function(value) { michael@0: if (!arguments.length) return mimeType; michael@0: mimeType = value == null ? null : value + ""; michael@0: return xhr; michael@0: }; michael@0: xhr.responseType = function(value) { michael@0: if (!arguments.length) return responseType; michael@0: responseType = value; michael@0: return xhr; michael@0: }; michael@0: xhr.response = function(value) { michael@0: response = value; michael@0: return xhr; michael@0: }; michael@0: [ "get", "post" ].forEach(function(method) { michael@0: xhr[method] = function() { michael@0: return xhr.send.apply(xhr, [ method ].concat(d3_array(arguments))); michael@0: }; michael@0: }); michael@0: xhr.send = function(method, data, callback) { michael@0: if (arguments.length === 2 && typeof data === "function") callback = data, data = null; michael@0: request.open(method, url, true); michael@0: if (mimeType != null && !("accept" in headers)) headers["accept"] = mimeType + ",*/*"; michael@0: if (request.setRequestHeader) for (var name in headers) request.setRequestHeader(name, headers[name]); michael@0: if (mimeType != null && request.overrideMimeType) request.overrideMimeType(mimeType); michael@0: if (responseType != null) request.responseType = responseType; michael@0: if (callback != null) xhr.on("error", callback).on("load", function(request) { michael@0: callback(null, request); michael@0: }); michael@0: dispatch.beforesend.call(xhr, request); michael@0: request.send(data == null ? null : data); michael@0: return xhr; michael@0: }; michael@0: xhr.abort = function() { michael@0: request.abort(); michael@0: return xhr; michael@0: }; michael@0: d3.rebind(xhr, dispatch, "on"); michael@0: return callback == null ? xhr : xhr.get(d3_xhr_fixCallback(callback)); michael@0: } michael@0: function d3_xhr_fixCallback(callback) { michael@0: return callback.length === 1 ? function(error, request) { michael@0: callback(error == null ? request : null); michael@0: } : callback; michael@0: } michael@0: d3.dsv = function(delimiter, mimeType) { michael@0: var reFormat = new RegExp('["' + delimiter + "\n]"), delimiterCode = delimiter.charCodeAt(0); michael@0: function dsv(url, row, callback) { michael@0: if (arguments.length < 3) callback = row, row = null; michael@0: var xhr = d3_xhr(url, mimeType, row == null ? response : typedResponse(row), callback); michael@0: xhr.row = function(_) { michael@0: return arguments.length ? xhr.response((row = _) == null ? response : typedResponse(_)) : row; michael@0: }; michael@0: return xhr; michael@0: } michael@0: function response(request) { michael@0: return dsv.parse(request.responseText); michael@0: } michael@0: function typedResponse(f) { michael@0: return function(request) { michael@0: return dsv.parse(request.responseText, f); michael@0: }; michael@0: } michael@0: dsv.parse = function(text, f) { michael@0: var o; michael@0: return dsv.parseRows(text, function(row, i) { michael@0: if (o) return o(row, i - 1); michael@0: var a = new Function("d", "return {" + row.map(function(name, i) { michael@0: return JSON.stringify(name) + ": d[" + i + "]"; michael@0: }).join(",") + "}"); michael@0: o = f ? function(row, i) { michael@0: return f(a(row), i); michael@0: } : a; michael@0: }); michael@0: }; michael@0: dsv.parseRows = function(text, f) { michael@0: var EOL = {}, EOF = {}, rows = [], N = text.length, I = 0, n = 0, t, eol; michael@0: function token() { michael@0: if (I >= N) return EOF; michael@0: if (eol) return eol = false, EOL; michael@0: var j = I; michael@0: if (text.charCodeAt(j) === 34) { michael@0: var i = j; michael@0: while (i++ < N) { michael@0: if (text.charCodeAt(i) === 34) { michael@0: if (text.charCodeAt(i + 1) !== 34) break; michael@0: ++i; michael@0: } michael@0: } michael@0: I = i + 2; michael@0: var c = text.charCodeAt(i + 1); michael@0: if (c === 13) { michael@0: eol = true; michael@0: if (text.charCodeAt(i + 2) === 10) ++I; michael@0: } else if (c === 10) { michael@0: eol = true; michael@0: } michael@0: return text.substring(j + 1, i).replace(/""/g, '"'); michael@0: } michael@0: while (I < N) { michael@0: var c = text.charCodeAt(I++), k = 1; michael@0: if (c === 10) eol = true; else if (c === 13) { michael@0: eol = true; michael@0: if (text.charCodeAt(I) === 10) ++I, ++k; michael@0: } else if (c !== delimiterCode) continue; michael@0: return text.substring(j, I - k); michael@0: } michael@0: return text.substring(j); michael@0: } michael@0: while ((t = token()) !== EOF) { michael@0: var a = []; michael@0: while (t !== EOL && t !== EOF) { michael@0: a.push(t); michael@0: t = token(); michael@0: } michael@0: if (f && !(a = f(a, n++))) continue; michael@0: rows.push(a); michael@0: } michael@0: return rows; michael@0: }; michael@0: dsv.format = function(rows) { michael@0: if (Array.isArray(rows[0])) return dsv.formatRows(rows); michael@0: var fieldSet = new d3_Set(), fields = []; michael@0: rows.forEach(function(row) { michael@0: for (var field in row) { michael@0: if (!fieldSet.has(field)) { michael@0: fields.push(fieldSet.add(field)); michael@0: } michael@0: } michael@0: }); michael@0: return [ fields.map(formatValue).join(delimiter) ].concat(rows.map(function(row) { michael@0: return fields.map(function(field) { michael@0: return formatValue(row[field]); michael@0: }).join(delimiter); michael@0: })).join("\n"); michael@0: }; michael@0: dsv.formatRows = function(rows) { michael@0: return rows.map(formatRow).join("\n"); michael@0: }; michael@0: function formatRow(row) { michael@0: return row.map(formatValue).join(delimiter); michael@0: } michael@0: function formatValue(text) { michael@0: return reFormat.test(text) ? '"' + text.replace(/\"/g, '""') + '"' : text; michael@0: } michael@0: return dsv; michael@0: }; michael@0: d3.csv = d3.dsv(",", "text/csv"); michael@0: d3.tsv = d3.dsv(" ", "text/tab-separated-values"); michael@0: var d3_timer_queueHead, d3_timer_queueTail, d3_timer_interval, d3_timer_timeout, d3_timer_active, d3_timer_frame = d3_window[d3_vendorSymbol(d3_window, "requestAnimationFrame")] || function(callback) { michael@0: setTimeout(callback, 17); michael@0: }; michael@0: d3.timer = function(callback, delay, then) { michael@0: var n = arguments.length; michael@0: if (n < 2) delay = 0; michael@0: if (n < 3) then = Date.now(); michael@0: var time = then + delay, timer = { michael@0: c: callback, michael@0: t: time, michael@0: f: false, michael@0: n: null michael@0: }; michael@0: if (d3_timer_queueTail) d3_timer_queueTail.n = timer; else d3_timer_queueHead = timer; michael@0: d3_timer_queueTail = timer; michael@0: if (!d3_timer_interval) { michael@0: d3_timer_timeout = clearTimeout(d3_timer_timeout); michael@0: d3_timer_interval = 1; michael@0: d3_timer_frame(d3_timer_step); michael@0: } michael@0: }; michael@0: function d3_timer_step() { michael@0: var now = d3_timer_mark(), delay = d3_timer_sweep() - now; michael@0: if (delay > 24) { michael@0: if (isFinite(delay)) { michael@0: clearTimeout(d3_timer_timeout); michael@0: d3_timer_timeout = setTimeout(d3_timer_step, delay); michael@0: } michael@0: d3_timer_interval = 0; michael@0: } else { michael@0: d3_timer_interval = 1; michael@0: d3_timer_frame(d3_timer_step); michael@0: } michael@0: } michael@0: d3.timer.flush = function() { michael@0: d3_timer_mark(); michael@0: d3_timer_sweep(); michael@0: }; michael@0: function d3_timer_mark() { michael@0: var now = Date.now(); michael@0: d3_timer_active = d3_timer_queueHead; michael@0: while (d3_timer_active) { michael@0: if (now >= d3_timer_active.t) d3_timer_active.f = d3_timer_active.c(now - d3_timer_active.t); michael@0: d3_timer_active = d3_timer_active.n; michael@0: } michael@0: return now; michael@0: } michael@0: function d3_timer_sweep() { michael@0: var t0, t1 = d3_timer_queueHead, time = Infinity; michael@0: while (t1) { michael@0: if (t1.f) { michael@0: t1 = t0 ? t0.n = t1.n : d3_timer_queueHead = t1.n; michael@0: } else { michael@0: if (t1.t < time) time = t1.t; michael@0: t1 = (t0 = t1).n; michael@0: } michael@0: } michael@0: d3_timer_queueTail = t0; michael@0: return time; michael@0: } michael@0: function d3_format_precision(x, p) { michael@0: return p - (x ? Math.ceil(Math.log(x) / Math.LN10) : 1); michael@0: } michael@0: d3.round = function(x, n) { michael@0: return n ? Math.round(x * (n = Math.pow(10, n))) / n : Math.round(x); michael@0: }; michael@0: var d3_formatPrefixes = [ "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" ].map(d3_formatPrefix); michael@0: d3.formatPrefix = function(value, precision) { michael@0: var i = 0; michael@0: if (value) { michael@0: if (value < 0) value *= -1; michael@0: if (precision) value = d3.round(value, d3_format_precision(value, precision)); michael@0: i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10); michael@0: i = Math.max(-24, Math.min(24, Math.floor((i <= 0 ? i + 1 : i - 1) / 3) * 3)); michael@0: } michael@0: return d3_formatPrefixes[8 + i / 3]; michael@0: }; michael@0: function d3_formatPrefix(d, i) { michael@0: var k = Math.pow(10, abs(8 - i) * 3); michael@0: return { michael@0: scale: i > 8 ? function(d) { michael@0: return d / k; michael@0: } : function(d) { michael@0: return d * k; michael@0: }, michael@0: symbol: d michael@0: }; michael@0: } michael@0: function d3_locale_numberFormat(locale) { michael@0: var locale_decimal = locale.decimal, locale_thousands = locale.thousands, locale_grouping = locale.grouping, locale_currency = locale.currency, formatGroup = locale_grouping ? function(value) { michael@0: var i = value.length, t = [], j = 0, g = locale_grouping[0]; michael@0: while (i > 0 && g > 0) { michael@0: t.push(value.substring(i -= g, i + g)); michael@0: g = locale_grouping[j = (j + 1) % locale_grouping.length]; michael@0: } michael@0: return t.reverse().join(locale_thousands); michael@0: } : d3_identity; michael@0: return function(specifier) { michael@0: var match = d3_format_re.exec(specifier), fill = match[1] || " ", align = match[2] || ">", sign = match[3] || "", symbol = match[4] || "", zfill = match[5], width = +match[6], comma = match[7], precision = match[8], type = match[9], scale = 1, prefix = "", suffix = "", integer = false; michael@0: if (precision) precision = +precision.substring(1); michael@0: if (zfill || fill === "0" && align === "=") { michael@0: zfill = fill = "0"; michael@0: align = "="; michael@0: if (comma) width -= Math.floor((width - 1) / 4); michael@0: } michael@0: switch (type) { michael@0: case "n": michael@0: comma = true; michael@0: type = "g"; michael@0: break; michael@0: michael@0: case "%": michael@0: scale = 100; michael@0: suffix = "%"; michael@0: type = "f"; michael@0: break; michael@0: michael@0: case "p": michael@0: scale = 100; michael@0: suffix = "%"; michael@0: type = "r"; michael@0: break; michael@0: michael@0: case "b": michael@0: case "o": michael@0: case "x": michael@0: case "X": michael@0: if (symbol === "#") prefix = "0" + type.toLowerCase(); michael@0: michael@0: case "c": michael@0: case "d": michael@0: integer = true; michael@0: precision = 0; michael@0: break; michael@0: michael@0: case "s": michael@0: scale = -1; michael@0: type = "r"; michael@0: break; michael@0: } michael@0: if (symbol === "$") prefix = locale_currency[0], suffix = locale_currency[1]; michael@0: if (type == "r" && !precision) type = "g"; michael@0: if (precision != null) { michael@0: if (type == "g") precision = Math.max(1, Math.min(21, precision)); else if (type == "e" || type == "f") precision = Math.max(0, Math.min(20, precision)); michael@0: } michael@0: type = d3_format_types.get(type) || d3_format_typeDefault; michael@0: var zcomma = zfill && comma; michael@0: return function(value) { michael@0: var fullSuffix = suffix; michael@0: if (integer && value % 1) return ""; michael@0: var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign; michael@0: if (scale < 0) { michael@0: var unit = d3.formatPrefix(value, precision); michael@0: value = unit.scale(value); michael@0: fullSuffix = unit.symbol + suffix; michael@0: } else { michael@0: value *= scale; michael@0: } michael@0: value = type(value, precision); michael@0: var i = value.lastIndexOf("."), before = i < 0 ? value : value.substring(0, i), after = i < 0 ? "" : locale_decimal + value.substring(i + 1); michael@0: if (!zfill && comma) before = formatGroup(before); michael@0: var length = prefix.length + before.length + after.length + (zcomma ? 0 : negative.length), padding = length < width ? new Array(length = width - length + 1).join(fill) : ""; michael@0: if (zcomma) before = formatGroup(padding + before); michael@0: negative += prefix; michael@0: value = before + after; michael@0: return (align === "<" ? negative + value + padding : align === ">" ? padding + negative + value : align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length) : negative + (zcomma ? value : padding + value)) + fullSuffix; michael@0: }; michael@0: }; michael@0: } michael@0: var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?([$#])?(0)?(\d+)?(,)?(\.-?\d+)?([a-z%])?/i; michael@0: var d3_format_types = d3.map({ michael@0: b: function(x) { michael@0: return x.toString(2); michael@0: }, michael@0: c: function(x) { michael@0: return String.fromCharCode(x); michael@0: }, michael@0: o: function(x) { michael@0: return x.toString(8); michael@0: }, michael@0: x: function(x) { michael@0: return x.toString(16); michael@0: }, michael@0: X: function(x) { michael@0: return x.toString(16).toUpperCase(); michael@0: }, michael@0: g: function(x, p) { michael@0: return x.toPrecision(p); michael@0: }, michael@0: e: function(x, p) { michael@0: return x.toExponential(p); michael@0: }, michael@0: f: function(x, p) { michael@0: return x.toFixed(p); michael@0: }, michael@0: r: function(x, p) { michael@0: return (x = d3.round(x, d3_format_precision(x, p))).toFixed(Math.max(0, Math.min(20, d3_format_precision(x * (1 + 1e-15), p)))); michael@0: } michael@0: }); michael@0: function d3_format_typeDefault(x) { michael@0: return x + ""; michael@0: } michael@0: var d3_time = d3.time = {}, d3_date = Date; michael@0: function d3_date_utc() { michael@0: this._ = new Date(arguments.length > 1 ? Date.UTC.apply(this, arguments) : arguments[0]); michael@0: } michael@0: d3_date_utc.prototype = { michael@0: getDate: function() { michael@0: return this._.getUTCDate(); michael@0: }, michael@0: getDay: function() { michael@0: return this._.getUTCDay(); michael@0: }, michael@0: getFullYear: function() { michael@0: return this._.getUTCFullYear(); michael@0: }, michael@0: getHours: function() { michael@0: return this._.getUTCHours(); michael@0: }, michael@0: getMilliseconds: function() { michael@0: return this._.getUTCMilliseconds(); michael@0: }, michael@0: getMinutes: function() { michael@0: return this._.getUTCMinutes(); michael@0: }, michael@0: getMonth: function() { michael@0: return this._.getUTCMonth(); michael@0: }, michael@0: getSeconds: function() { michael@0: return this._.getUTCSeconds(); michael@0: }, michael@0: getTime: function() { michael@0: return this._.getTime(); michael@0: }, michael@0: getTimezoneOffset: function() { michael@0: return 0; michael@0: }, michael@0: valueOf: function() { michael@0: return this._.valueOf(); michael@0: }, michael@0: setDate: function() { michael@0: d3_time_prototype.setUTCDate.apply(this._, arguments); michael@0: }, michael@0: setDay: function() { michael@0: d3_time_prototype.setUTCDay.apply(this._, arguments); michael@0: }, michael@0: setFullYear: function() { michael@0: d3_time_prototype.setUTCFullYear.apply(this._, arguments); michael@0: }, michael@0: setHours: function() { michael@0: d3_time_prototype.setUTCHours.apply(this._, arguments); michael@0: }, michael@0: setMilliseconds: function() { michael@0: d3_time_prototype.setUTCMilliseconds.apply(this._, arguments); michael@0: }, michael@0: setMinutes: function() { michael@0: d3_time_prototype.setUTCMinutes.apply(this._, arguments); michael@0: }, michael@0: setMonth: function() { michael@0: d3_time_prototype.setUTCMonth.apply(this._, arguments); michael@0: }, michael@0: setSeconds: function() { michael@0: d3_time_prototype.setUTCSeconds.apply(this._, arguments); michael@0: }, michael@0: setTime: function() { michael@0: d3_time_prototype.setTime.apply(this._, arguments); michael@0: } michael@0: }; michael@0: var d3_time_prototype = Date.prototype; michael@0: function d3_time_interval(local, step, number) { michael@0: function round(date) { michael@0: var d0 = local(date), d1 = offset(d0, 1); michael@0: return date - d0 < d1 - date ? d0 : d1; michael@0: } michael@0: function ceil(date) { michael@0: step(date = local(new d3_date(date - 1)), 1); michael@0: return date; michael@0: } michael@0: function offset(date, k) { michael@0: step(date = new d3_date(+date), k); michael@0: return date; michael@0: } michael@0: function range(t0, t1, dt) { michael@0: var time = ceil(t0), times = []; michael@0: if (dt > 1) { michael@0: while (time < t1) { michael@0: if (!(number(time) % dt)) times.push(new Date(+time)); michael@0: step(time, 1); michael@0: } michael@0: } else { michael@0: while (time < t1) times.push(new Date(+time)), step(time, 1); michael@0: } michael@0: return times; michael@0: } michael@0: function range_utc(t0, t1, dt) { michael@0: try { michael@0: d3_date = d3_date_utc; michael@0: var utc = new d3_date_utc(); michael@0: utc._ = t0; michael@0: return range(utc, t1, dt); michael@0: } finally { michael@0: d3_date = Date; michael@0: } michael@0: } michael@0: local.floor = local; michael@0: local.round = round; michael@0: local.ceil = ceil; michael@0: local.offset = offset; michael@0: local.range = range; michael@0: var utc = local.utc = d3_time_interval_utc(local); michael@0: utc.floor = utc; michael@0: utc.round = d3_time_interval_utc(round); michael@0: utc.ceil = d3_time_interval_utc(ceil); michael@0: utc.offset = d3_time_interval_utc(offset); michael@0: utc.range = range_utc; michael@0: return local; michael@0: } michael@0: function d3_time_interval_utc(method) { michael@0: return function(date, k) { michael@0: try { michael@0: d3_date = d3_date_utc; michael@0: var utc = new d3_date_utc(); michael@0: utc._ = date; michael@0: return method(utc, k)._; michael@0: } finally { michael@0: d3_date = Date; michael@0: } michael@0: }; michael@0: } michael@0: d3_time.year = d3_time_interval(function(date) { michael@0: date = d3_time.day(date); michael@0: date.setMonth(0, 1); michael@0: return date; michael@0: }, function(date, offset) { michael@0: date.setFullYear(date.getFullYear() + offset); michael@0: }, function(date) { michael@0: return date.getFullYear(); michael@0: }); michael@0: d3_time.years = d3_time.year.range; michael@0: d3_time.years.utc = d3_time.year.utc.range; michael@0: d3_time.day = d3_time_interval(function(date) { michael@0: var day = new d3_date(2e3, 0); michael@0: day.setFullYear(date.getFullYear(), date.getMonth(), date.getDate()); michael@0: return day; michael@0: }, function(date, offset) { michael@0: date.setDate(date.getDate() + offset); michael@0: }, function(date) { michael@0: return date.getDate() - 1; michael@0: }); michael@0: d3_time.days = d3_time.day.range; michael@0: d3_time.days.utc = d3_time.day.utc.range; michael@0: d3_time.dayOfYear = function(date) { michael@0: var year = d3_time.year(date); michael@0: return Math.floor((date - year - (date.getTimezoneOffset() - year.getTimezoneOffset()) * 6e4) / 864e5); michael@0: }; michael@0: [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ].forEach(function(day, i) { michael@0: i = 7 - i; michael@0: var interval = d3_time[day] = d3_time_interval(function(date) { michael@0: (date = d3_time.day(date)).setDate(date.getDate() - (date.getDay() + i) % 7); michael@0: return date; michael@0: }, function(date, offset) { michael@0: date.setDate(date.getDate() + Math.floor(offset) * 7); michael@0: }, function(date) { michael@0: var day = d3_time.year(date).getDay(); michael@0: return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7) - (day !== i); michael@0: }); michael@0: d3_time[day + "s"] = interval.range; michael@0: d3_time[day + "s"].utc = interval.utc.range; michael@0: d3_time[day + "OfYear"] = function(date) { michael@0: var day = d3_time.year(date).getDay(); michael@0: return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7); michael@0: }; michael@0: }); michael@0: d3_time.week = d3_time.sunday; michael@0: d3_time.weeks = d3_time.sunday.range; michael@0: d3_time.weeks.utc = d3_time.sunday.utc.range; michael@0: d3_time.weekOfYear = d3_time.sundayOfYear; michael@0: function d3_locale_timeFormat(locale) { michael@0: var locale_dateTime = locale.dateTime, locale_date = locale.date, locale_time = locale.time, locale_periods = locale.periods, locale_days = locale.days, locale_shortDays = locale.shortDays, locale_months = locale.months, locale_shortMonths = locale.shortMonths; michael@0: function d3_time_format(template) { michael@0: var n = template.length; michael@0: function format(date) { michael@0: var string = [], i = -1, j = 0, c, p, f; michael@0: while (++i < n) { michael@0: if (template.charCodeAt(i) === 37) { michael@0: string.push(template.substring(j, i)); michael@0: if ((p = d3_time_formatPads[c = template.charAt(++i)]) != null) c = template.charAt(++i); michael@0: if (f = d3_time_formats[c]) c = f(date, p == null ? c === "e" ? " " : "0" : p); michael@0: string.push(c); michael@0: j = i + 1; michael@0: } michael@0: } michael@0: string.push(template.substring(j, i)); michael@0: return string.join(""); michael@0: } michael@0: format.parse = function(string) { michael@0: var d = { michael@0: y: 1900, michael@0: m: 0, michael@0: d: 1, michael@0: H: 0, michael@0: M: 0, michael@0: S: 0, michael@0: L: 0, michael@0: Z: null michael@0: }, i = d3_time_parse(d, template, string, 0); michael@0: if (i != string.length) return null; michael@0: if ("p" in d) d.H = d.H % 12 + d.p * 12; michael@0: var localZ = d.Z != null && d3_date !== d3_date_utc, date = new (localZ ? d3_date_utc : d3_date)(); michael@0: if ("j" in d) date.setFullYear(d.y, 0, d.j); else if ("w" in d && ("W" in d || "U" in d)) { michael@0: date.setFullYear(d.y, 0, 1); michael@0: date.setFullYear(d.y, 0, "W" in d ? (d.w + 6) % 7 + d.W * 7 - (date.getDay() + 5) % 7 : d.w + d.U * 7 - (date.getDay() + 6) % 7); michael@0: } else date.setFullYear(d.y, d.m, d.d); michael@0: date.setHours(d.H + Math.floor(d.Z / 100), d.M + d.Z % 100, d.S, d.L); michael@0: return localZ ? date._ : date; michael@0: }; michael@0: format.toString = function() { michael@0: return template; michael@0: }; michael@0: return format; michael@0: } michael@0: function d3_time_parse(date, template, string, j) { michael@0: var c, p, t, i = 0, n = template.length, m = string.length; michael@0: while (i < n) { michael@0: if (j >= m) return -1; michael@0: c = template.charCodeAt(i++); michael@0: if (c === 37) { michael@0: t = template.charAt(i++); michael@0: p = d3_time_parsers[t in d3_time_formatPads ? template.charAt(i++) : t]; michael@0: if (!p || (j = p(date, string, j)) < 0) return -1; michael@0: } else if (c != string.charCodeAt(j++)) { michael@0: return -1; michael@0: } michael@0: } michael@0: return j; michael@0: } michael@0: d3_time_format.utc = function(template) { michael@0: var local = d3_time_format(template); michael@0: function format(date) { michael@0: try { michael@0: d3_date = d3_date_utc; michael@0: var utc = new d3_date(); michael@0: utc._ = date; michael@0: return local(utc); michael@0: } finally { michael@0: d3_date = Date; michael@0: } michael@0: } michael@0: format.parse = function(string) { michael@0: try { michael@0: d3_date = d3_date_utc; michael@0: var date = local.parse(string); michael@0: return date && date._; michael@0: } finally { michael@0: d3_date = Date; michael@0: } michael@0: }; michael@0: format.toString = local.toString; michael@0: return format; michael@0: }; michael@0: d3_time_format.multi = d3_time_format.utc.multi = d3_time_formatMulti; michael@0: var d3_time_periodLookup = d3.map(), d3_time_dayRe = d3_time_formatRe(locale_days), d3_time_dayLookup = d3_time_formatLookup(locale_days), d3_time_dayAbbrevRe = d3_time_formatRe(locale_shortDays), d3_time_dayAbbrevLookup = d3_time_formatLookup(locale_shortDays), d3_time_monthRe = d3_time_formatRe(locale_months), d3_time_monthLookup = d3_time_formatLookup(locale_months), d3_time_monthAbbrevRe = d3_time_formatRe(locale_shortMonths), d3_time_monthAbbrevLookup = d3_time_formatLookup(locale_shortMonths); michael@0: locale_periods.forEach(function(p, i) { michael@0: d3_time_periodLookup.set(p.toLowerCase(), i); michael@0: }); michael@0: var d3_time_formats = { michael@0: a: function(d) { michael@0: return locale_shortDays[d.getDay()]; michael@0: }, michael@0: A: function(d) { michael@0: return locale_days[d.getDay()]; michael@0: }, michael@0: b: function(d) { michael@0: return locale_shortMonths[d.getMonth()]; michael@0: }, michael@0: B: function(d) { michael@0: return locale_months[d.getMonth()]; michael@0: }, michael@0: c: d3_time_format(locale_dateTime), michael@0: d: function(d, p) { michael@0: return d3_time_formatPad(d.getDate(), p, 2); michael@0: }, michael@0: e: function(d, p) { michael@0: return d3_time_formatPad(d.getDate(), p, 2); michael@0: }, michael@0: H: function(d, p) { michael@0: return d3_time_formatPad(d.getHours(), p, 2); michael@0: }, michael@0: I: function(d, p) { michael@0: return d3_time_formatPad(d.getHours() % 12 || 12, p, 2); michael@0: }, michael@0: j: function(d, p) { michael@0: return d3_time_formatPad(1 + d3_time.dayOfYear(d), p, 3); michael@0: }, michael@0: L: function(d, p) { michael@0: return d3_time_formatPad(d.getMilliseconds(), p, 3); michael@0: }, michael@0: m: function(d, p) { michael@0: return d3_time_formatPad(d.getMonth() + 1, p, 2); michael@0: }, michael@0: M: function(d, p) { michael@0: return d3_time_formatPad(d.getMinutes(), p, 2); michael@0: }, michael@0: p: function(d) { michael@0: return locale_periods[+(d.getHours() >= 12)]; michael@0: }, michael@0: S: function(d, p) { michael@0: return d3_time_formatPad(d.getSeconds(), p, 2); michael@0: }, michael@0: U: function(d, p) { michael@0: return d3_time_formatPad(d3_time.sundayOfYear(d), p, 2); michael@0: }, michael@0: w: function(d) { michael@0: return d.getDay(); michael@0: }, michael@0: W: function(d, p) { michael@0: return d3_time_formatPad(d3_time.mondayOfYear(d), p, 2); michael@0: }, michael@0: x: d3_time_format(locale_date), michael@0: X: d3_time_format(locale_time), michael@0: y: function(d, p) { michael@0: return d3_time_formatPad(d.getFullYear() % 100, p, 2); michael@0: }, michael@0: Y: function(d, p) { michael@0: return d3_time_formatPad(d.getFullYear() % 1e4, p, 4); michael@0: }, michael@0: Z: d3_time_zone, michael@0: "%": function() { michael@0: return "%"; michael@0: } michael@0: }; michael@0: var d3_time_parsers = { michael@0: a: d3_time_parseWeekdayAbbrev, michael@0: A: d3_time_parseWeekday, michael@0: b: d3_time_parseMonthAbbrev, michael@0: B: d3_time_parseMonth, michael@0: c: d3_time_parseLocaleFull, michael@0: d: d3_time_parseDay, michael@0: e: d3_time_parseDay, michael@0: H: d3_time_parseHour24, michael@0: I: d3_time_parseHour24, michael@0: j: d3_time_parseDayOfYear, michael@0: L: d3_time_parseMilliseconds, michael@0: m: d3_time_parseMonthNumber, michael@0: M: d3_time_parseMinutes, michael@0: p: d3_time_parseAmPm, michael@0: S: d3_time_parseSeconds, michael@0: U: d3_time_parseWeekNumberSunday, michael@0: w: d3_time_parseWeekdayNumber, michael@0: W: d3_time_parseWeekNumberMonday, michael@0: x: d3_time_parseLocaleDate, michael@0: X: d3_time_parseLocaleTime, michael@0: y: d3_time_parseYear, michael@0: Y: d3_time_parseFullYear, michael@0: Z: d3_time_parseZone, michael@0: "%": d3_time_parseLiteralPercent michael@0: }; michael@0: function d3_time_parseWeekdayAbbrev(date, string, i) { michael@0: d3_time_dayAbbrevRe.lastIndex = 0; michael@0: var n = d3_time_dayAbbrevRe.exec(string.substring(i)); michael@0: return n ? (date.w = d3_time_dayAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseWeekday(date, string, i) { michael@0: d3_time_dayRe.lastIndex = 0; michael@0: var n = d3_time_dayRe.exec(string.substring(i)); michael@0: return n ? (date.w = d3_time_dayLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseMonthAbbrev(date, string, i) { michael@0: d3_time_monthAbbrevRe.lastIndex = 0; michael@0: var n = d3_time_monthAbbrevRe.exec(string.substring(i)); michael@0: return n ? (date.m = d3_time_monthAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseMonth(date, string, i) { michael@0: d3_time_monthRe.lastIndex = 0; michael@0: var n = d3_time_monthRe.exec(string.substring(i)); michael@0: return n ? (date.m = d3_time_monthLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseLocaleFull(date, string, i) { michael@0: return d3_time_parse(date, d3_time_formats.c.toString(), string, i); michael@0: } michael@0: function d3_time_parseLocaleDate(date, string, i) { michael@0: return d3_time_parse(date, d3_time_formats.x.toString(), string, i); michael@0: } michael@0: function d3_time_parseLocaleTime(date, string, i) { michael@0: return d3_time_parse(date, d3_time_formats.X.toString(), string, i); michael@0: } michael@0: function d3_time_parseAmPm(date, string, i) { michael@0: var n = d3_time_periodLookup.get(string.substring(i, i += 2).toLowerCase()); michael@0: return n == null ? -1 : (date.p = n, i); michael@0: } michael@0: return d3_time_format; michael@0: } michael@0: var d3_time_formatPads = { michael@0: "-": "", michael@0: _: " ", michael@0: "0": "0" michael@0: }, d3_time_numberRe = /^\s*\d+/, d3_time_percentRe = /^%/; michael@0: function d3_time_formatPad(value, fill, width) { michael@0: var sign = value < 0 ? "-" : "", string = (sign ? -value : value) + "", length = string.length; michael@0: return sign + (length < width ? new Array(width - length + 1).join(fill) + string : string); michael@0: } michael@0: function d3_time_formatRe(names) { michael@0: return new RegExp("^(?:" + names.map(d3.requote).join("|") + ")", "i"); michael@0: } michael@0: function d3_time_formatLookup(names) { michael@0: var map = new d3_Map(), i = -1, n = names.length; michael@0: while (++i < n) map.set(names[i].toLowerCase(), i); michael@0: return map; michael@0: } michael@0: function d3_time_parseWeekdayNumber(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 1)); michael@0: return n ? (date.w = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseWeekNumberSunday(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i)); michael@0: return n ? (date.U = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseWeekNumberMonday(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i)); michael@0: return n ? (date.W = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseFullYear(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 4)); michael@0: return n ? (date.y = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseYear(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 2)); michael@0: return n ? (date.y = d3_time_expandYear(+n[0]), i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseZone(date, string, i) { michael@0: return /^[+-]\d{4}$/.test(string = string.substring(i, i + 5)) ? (date.Z = +string, michael@0: i + 5) : -1; michael@0: } michael@0: function d3_time_expandYear(d) { michael@0: return d + (d > 68 ? 1900 : 2e3); michael@0: } michael@0: function d3_time_parseMonthNumber(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 2)); michael@0: return n ? (date.m = n[0] - 1, i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseDay(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 2)); michael@0: return n ? (date.d = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseDayOfYear(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 3)); michael@0: return n ? (date.j = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseHour24(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 2)); michael@0: return n ? (date.H = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseMinutes(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 2)); michael@0: return n ? (date.M = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseSeconds(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 2)); michael@0: return n ? (date.S = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_parseMilliseconds(date, string, i) { michael@0: d3_time_numberRe.lastIndex = 0; michael@0: var n = d3_time_numberRe.exec(string.substring(i, i + 3)); michael@0: return n ? (date.L = +n[0], i + n[0].length) : -1; michael@0: } michael@0: function d3_time_zone(d) { michael@0: var z = d.getTimezoneOffset(), zs = z > 0 ? "-" : "+", zh = ~~(abs(z) / 60), zm = abs(z) % 60; michael@0: return zs + d3_time_formatPad(zh, "0", 2) + d3_time_formatPad(zm, "0", 2); michael@0: } michael@0: function d3_time_parseLiteralPercent(date, string, i) { michael@0: d3_time_percentRe.lastIndex = 0; michael@0: var n = d3_time_percentRe.exec(string.substring(i, i + 1)); michael@0: return n ? i + n[0].length : -1; michael@0: } michael@0: function d3_time_formatMulti(formats) { michael@0: var n = formats.length, i = -1; michael@0: while (++i < n) formats[i][0] = this(formats[i][0]); michael@0: return function(date) { michael@0: var i = 0, f = formats[i]; michael@0: while (!f[1](date)) f = formats[++i]; michael@0: return f[0](date); michael@0: }; michael@0: } michael@0: d3.locale = function(locale) { michael@0: return { michael@0: numberFormat: d3_locale_numberFormat(locale), michael@0: timeFormat: d3_locale_timeFormat(locale) michael@0: }; michael@0: }; michael@0: var d3_locale_enUS = d3.locale({ michael@0: decimal: ".", michael@0: thousands: ",", michael@0: grouping: [ 3 ], michael@0: currency: [ "$", "" ], michael@0: dateTime: "%a %b %e %X %Y", michael@0: date: "%m/%d/%Y", michael@0: time: "%H:%M:%S", michael@0: periods: [ "AM", "PM" ], michael@0: days: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], michael@0: shortDays: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], michael@0: months: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], michael@0: shortMonths: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] michael@0: }); michael@0: d3.format = d3_locale_enUS.numberFormat; michael@0: d3.geo = {}; michael@0: function d3_adder() {} michael@0: d3_adder.prototype = { michael@0: s: 0, michael@0: t: 0, michael@0: add: function(y) { michael@0: d3_adderSum(y, this.t, d3_adderTemp); michael@0: d3_adderSum(d3_adderTemp.s, this.s, this); michael@0: if (this.s) this.t += d3_adderTemp.t; else this.s = d3_adderTemp.t; michael@0: }, michael@0: reset: function() { michael@0: this.s = this.t = 0; michael@0: }, michael@0: valueOf: function() { michael@0: return this.s; michael@0: } michael@0: }; michael@0: var d3_adderTemp = new d3_adder(); michael@0: function d3_adderSum(a, b, o) { michael@0: var x = o.s = a + b, bv = x - a, av = x - bv; michael@0: o.t = a - av + (b - bv); michael@0: } michael@0: d3.geo.stream = function(object, listener) { michael@0: if (object && d3_geo_streamObjectType.hasOwnProperty(object.type)) { michael@0: d3_geo_streamObjectType[object.type](object, listener); michael@0: } else { michael@0: d3_geo_streamGeometry(object, listener); michael@0: } michael@0: }; michael@0: function d3_geo_streamGeometry(geometry, listener) { michael@0: if (geometry && d3_geo_streamGeometryType.hasOwnProperty(geometry.type)) { michael@0: d3_geo_streamGeometryType[geometry.type](geometry, listener); michael@0: } michael@0: } michael@0: var d3_geo_streamObjectType = { michael@0: Feature: function(feature, listener) { michael@0: d3_geo_streamGeometry(feature.geometry, listener); michael@0: }, michael@0: FeatureCollection: function(object, listener) { michael@0: var features = object.features, i = -1, n = features.length; michael@0: while (++i < n) d3_geo_streamGeometry(features[i].geometry, listener); michael@0: } michael@0: }; michael@0: var d3_geo_streamGeometryType = { michael@0: Sphere: function(object, listener) { michael@0: listener.sphere(); michael@0: }, michael@0: Point: function(object, listener) { michael@0: object = object.coordinates; michael@0: listener.point(object[0], object[1], object[2]); michael@0: }, michael@0: MultiPoint: function(object, listener) { michael@0: var coordinates = object.coordinates, i = -1, n = coordinates.length; michael@0: while (++i < n) object = coordinates[i], listener.point(object[0], object[1], object[2]); michael@0: }, michael@0: LineString: function(object, listener) { michael@0: d3_geo_streamLine(object.coordinates, listener, 0); michael@0: }, michael@0: MultiLineString: function(object, listener) { michael@0: var coordinates = object.coordinates, i = -1, n = coordinates.length; michael@0: while (++i < n) d3_geo_streamLine(coordinates[i], listener, 0); michael@0: }, michael@0: Polygon: function(object, listener) { michael@0: d3_geo_streamPolygon(object.coordinates, listener); michael@0: }, michael@0: MultiPolygon: function(object, listener) { michael@0: var coordinates = object.coordinates, i = -1, n = coordinates.length; michael@0: while (++i < n) d3_geo_streamPolygon(coordinates[i], listener); michael@0: }, michael@0: GeometryCollection: function(object, listener) { michael@0: var geometries = object.geometries, i = -1, n = geometries.length; michael@0: while (++i < n) d3_geo_streamGeometry(geometries[i], listener); michael@0: } michael@0: }; michael@0: function d3_geo_streamLine(coordinates, listener, closed) { michael@0: var i = -1, n = coordinates.length - closed, coordinate; michael@0: listener.lineStart(); michael@0: while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1], coordinate[2]); michael@0: listener.lineEnd(); michael@0: } michael@0: function d3_geo_streamPolygon(coordinates, listener) { michael@0: var i = -1, n = coordinates.length; michael@0: listener.polygonStart(); michael@0: while (++i < n) d3_geo_streamLine(coordinates[i], listener, 1); michael@0: listener.polygonEnd(); michael@0: } michael@0: d3.geo.area = function(object) { michael@0: d3_geo_areaSum = 0; michael@0: d3.geo.stream(object, d3_geo_area); michael@0: return d3_geo_areaSum; michael@0: }; michael@0: var d3_geo_areaSum, d3_geo_areaRingSum = new d3_adder(); michael@0: var d3_geo_area = { michael@0: sphere: function() { michael@0: d3_geo_areaSum += 4 * π; michael@0: }, michael@0: point: d3_noop, michael@0: lineStart: d3_noop, michael@0: lineEnd: d3_noop, michael@0: polygonStart: function() { michael@0: d3_geo_areaRingSum.reset(); michael@0: d3_geo_area.lineStart = d3_geo_areaRingStart; michael@0: }, michael@0: polygonEnd: function() { michael@0: var area = 2 * d3_geo_areaRingSum; michael@0: d3_geo_areaSum += area < 0 ? 4 * π + area : area; michael@0: d3_geo_area.lineStart = d3_geo_area.lineEnd = d3_geo_area.point = d3_noop; michael@0: } michael@0: }; michael@0: function d3_geo_areaRingStart() { michael@0: var λ00, φ00, λ0, cosφ0, sinφ0; michael@0: d3_geo_area.point = function(λ, φ) { michael@0: d3_geo_area.point = nextPoint; michael@0: λ0 = (λ00 = λ) * d3_radians, cosφ0 = Math.cos(φ = (φ00 = φ) * d3_radians / 2 + π / 4), michael@0: sinφ0 = Math.sin(φ); michael@0: }; michael@0: function nextPoint(λ, φ) { michael@0: λ *= d3_radians; michael@0: φ = φ * d3_radians / 2 + π / 4; michael@0: var dλ = λ - λ0, cosφ = Math.cos(φ), sinφ = Math.sin(φ), k = sinφ0 * sinφ, u = cosφ0 * cosφ + k * Math.cos(dλ), v = k * Math.sin(dλ); michael@0: d3_geo_areaRingSum.add(Math.atan2(v, u)); michael@0: λ0 = λ, cosφ0 = cosφ, sinφ0 = sinφ; michael@0: } michael@0: d3_geo_area.lineEnd = function() { michael@0: nextPoint(λ00, φ00); michael@0: }; michael@0: } michael@0: function d3_geo_cartesian(spherical) { michael@0: var λ = spherical[0], φ = spherical[1], cosφ = Math.cos(φ); michael@0: return [ cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ) ]; michael@0: } michael@0: function d3_geo_cartesianDot(a, b) { michael@0: return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; michael@0: } michael@0: function d3_geo_cartesianCross(a, b) { michael@0: return [ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ]; michael@0: } michael@0: function d3_geo_cartesianAdd(a, b) { michael@0: a[0] += b[0]; michael@0: a[1] += b[1]; michael@0: a[2] += b[2]; michael@0: } michael@0: function d3_geo_cartesianScale(vector, k) { michael@0: return [ vector[0] * k, vector[1] * k, vector[2] * k ]; michael@0: } michael@0: function d3_geo_cartesianNormalize(d) { michael@0: var l = Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); michael@0: d[0] /= l; michael@0: d[1] /= l; michael@0: d[2] /= l; michael@0: } michael@0: function d3_geo_spherical(cartesian) { michael@0: return [ Math.atan2(cartesian[1], cartesian[0]), d3_asin(cartesian[2]) ]; michael@0: } michael@0: function d3_geo_sphericalEqual(a, b) { michael@0: return abs(a[0] - b[0]) < ε && abs(a[1] - b[1]) < ε; michael@0: } michael@0: d3.geo.bounds = function() { michael@0: var λ0, φ0, λ1, φ1, λ_, λ__, φ__, p0, dλSum, ranges, range; michael@0: var bound = { michael@0: point: point, michael@0: lineStart: lineStart, michael@0: lineEnd: lineEnd, michael@0: polygonStart: function() { michael@0: bound.point = ringPoint; michael@0: bound.lineStart = ringStart; michael@0: bound.lineEnd = ringEnd; michael@0: dλSum = 0; michael@0: d3_geo_area.polygonStart(); michael@0: }, michael@0: polygonEnd: function() { michael@0: d3_geo_area.polygonEnd(); michael@0: bound.point = point; michael@0: bound.lineStart = lineStart; michael@0: bound.lineEnd = lineEnd; michael@0: if (d3_geo_areaRingSum < 0) λ0 = -(λ1 = 180), φ0 = -(φ1 = 90); else if (dλSum > ε) φ1 = 90; else if (dλSum < -ε) φ0 = -90; michael@0: range[0] = λ0, range[1] = λ1; michael@0: } michael@0: }; michael@0: function point(λ, φ) { michael@0: ranges.push(range = [ λ0 = λ, λ1 = λ ]); michael@0: if (φ < φ0) φ0 = φ; michael@0: if (φ > φ1) φ1 = φ; michael@0: } michael@0: function linePoint(λ, φ) { michael@0: var p = d3_geo_cartesian([ λ * d3_radians, φ * d3_radians ]); michael@0: if (p0) { michael@0: var normal = d3_geo_cartesianCross(p0, p), equatorial = [ normal[1], -normal[0], 0 ], inflection = d3_geo_cartesianCross(equatorial, normal); michael@0: d3_geo_cartesianNormalize(inflection); michael@0: inflection = d3_geo_spherical(inflection); michael@0: var dλ = λ - λ_, s = dλ > 0 ? 1 : -1, λi = inflection[0] * d3_degrees * s, antimeridian = abs(dλ) > 180; michael@0: if (antimeridian ^ (s * λ_ < λi && λi < s * λ)) { michael@0: var φi = inflection[1] * d3_degrees; michael@0: if (φi > φ1) φ1 = φi; michael@0: } else if (λi = (λi + 360) % 360 - 180, antimeridian ^ (s * λ_ < λi && λi < s * λ)) { michael@0: var φi = -inflection[1] * d3_degrees; michael@0: if (φi < φ0) φ0 = φi; michael@0: } else { michael@0: if (φ < φ0) φ0 = φ; michael@0: if (φ > φ1) φ1 = φ; michael@0: } michael@0: if (antimeridian) { michael@0: if (λ < λ_) { michael@0: if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; michael@0: } else { michael@0: if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; michael@0: } michael@0: } else { michael@0: if (λ1 >= λ0) { michael@0: if (λ < λ0) λ0 = λ; michael@0: if (λ > λ1) λ1 = λ; michael@0: } else { michael@0: if (λ > λ_) { michael@0: if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; michael@0: } else { michael@0: if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: point(λ, φ); michael@0: } michael@0: p0 = p, λ_ = λ; michael@0: } michael@0: function lineStart() { michael@0: bound.point = linePoint; michael@0: } michael@0: function lineEnd() { michael@0: range[0] = λ0, range[1] = λ1; michael@0: bound.point = point; michael@0: p0 = null; michael@0: } michael@0: function ringPoint(λ, φ) { michael@0: if (p0) { michael@0: var dλ = λ - λ_; michael@0: dλSum += abs(dλ) > 180 ? dλ + (dλ > 0 ? 360 : -360) : dλ; michael@0: } else λ__ = λ, φ__ = φ; michael@0: d3_geo_area.point(λ, φ); michael@0: linePoint(λ, φ); michael@0: } michael@0: function ringStart() { michael@0: d3_geo_area.lineStart(); michael@0: } michael@0: function ringEnd() { michael@0: ringPoint(λ__, φ__); michael@0: d3_geo_area.lineEnd(); michael@0: if (abs(dλSum) > ε) λ0 = -(λ1 = 180); michael@0: range[0] = λ0, range[1] = λ1; michael@0: p0 = null; michael@0: } michael@0: function angle(λ0, λ1) { michael@0: return (λ1 -= λ0) < 0 ? λ1 + 360 : λ1; michael@0: } michael@0: function compareRanges(a, b) { michael@0: return a[0] - b[0]; michael@0: } michael@0: function withinRange(x, range) { michael@0: return range[0] <= range[1] ? range[0] <= x && x <= range[1] : x < range[0] || range[1] < x; michael@0: } michael@0: return function(feature) { michael@0: φ1 = λ1 = -(λ0 = φ0 = Infinity); michael@0: ranges = []; michael@0: d3.geo.stream(feature, bound); michael@0: var n = ranges.length; michael@0: if (n) { michael@0: ranges.sort(compareRanges); michael@0: for (var i = 1, a = ranges[0], b, merged = [ a ]; i < n; ++i) { michael@0: b = ranges[i]; michael@0: if (withinRange(b[0], a) || withinRange(b[1], a)) { michael@0: if (angle(a[0], b[1]) > angle(a[0], a[1])) a[1] = b[1]; michael@0: if (angle(b[0], a[1]) > angle(a[0], a[1])) a[0] = b[0]; michael@0: } else { michael@0: merged.push(a = b); michael@0: } michael@0: } michael@0: var best = -Infinity, dλ; michael@0: for (var n = merged.length - 1, i = 0, a = merged[n], b; i <= n; a = b, ++i) { michael@0: b = merged[i]; michael@0: if ((dλ = angle(a[1], b[0])) > best) best = dλ, λ0 = b[0], λ1 = a[1]; michael@0: } michael@0: } michael@0: ranges = range = null; michael@0: return λ0 === Infinity || φ0 === Infinity ? [ [ NaN, NaN ], [ NaN, NaN ] ] : [ [ λ0, φ0 ], [ λ1, φ1 ] ]; michael@0: }; michael@0: }(); michael@0: d3.geo.centroid = function(object) { michael@0: d3_geo_centroidW0 = d3_geo_centroidW1 = d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; michael@0: d3.geo.stream(object, d3_geo_centroid); michael@0: var x = d3_geo_centroidX2, y = d3_geo_centroidY2, z = d3_geo_centroidZ2, m = x * x + y * y + z * z; michael@0: if (m < ε2) { michael@0: x = d3_geo_centroidX1, y = d3_geo_centroidY1, z = d3_geo_centroidZ1; michael@0: if (d3_geo_centroidW1 < ε) x = d3_geo_centroidX0, y = d3_geo_centroidY0, z = d3_geo_centroidZ0; michael@0: m = x * x + y * y + z * z; michael@0: if (m < ε2) return [ NaN, NaN ]; michael@0: } michael@0: return [ Math.atan2(y, x) * d3_degrees, d3_asin(z / Math.sqrt(m)) * d3_degrees ]; michael@0: }; michael@0: var d3_geo_centroidW0, d3_geo_centroidW1, d3_geo_centroidX0, d3_geo_centroidY0, d3_geo_centroidZ0, d3_geo_centroidX1, d3_geo_centroidY1, d3_geo_centroidZ1, d3_geo_centroidX2, d3_geo_centroidY2, d3_geo_centroidZ2; michael@0: var d3_geo_centroid = { michael@0: sphere: d3_noop, michael@0: point: d3_geo_centroidPoint, michael@0: lineStart: d3_geo_centroidLineStart, michael@0: lineEnd: d3_geo_centroidLineEnd, michael@0: polygonStart: function() { michael@0: d3_geo_centroid.lineStart = d3_geo_centroidRingStart; michael@0: }, michael@0: polygonEnd: function() { michael@0: d3_geo_centroid.lineStart = d3_geo_centroidLineStart; michael@0: } michael@0: }; michael@0: function d3_geo_centroidPoint(λ, φ) { michael@0: λ *= d3_radians; michael@0: var cosφ = Math.cos(φ *= d3_radians); michael@0: d3_geo_centroidPointXYZ(cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ)); michael@0: } michael@0: function d3_geo_centroidPointXYZ(x, y, z) { michael@0: ++d3_geo_centroidW0; michael@0: d3_geo_centroidX0 += (x - d3_geo_centroidX0) / d3_geo_centroidW0; michael@0: d3_geo_centroidY0 += (y - d3_geo_centroidY0) / d3_geo_centroidW0; michael@0: d3_geo_centroidZ0 += (z - d3_geo_centroidZ0) / d3_geo_centroidW0; michael@0: } michael@0: function d3_geo_centroidLineStart() { michael@0: var x0, y0, z0; michael@0: d3_geo_centroid.point = function(λ, φ) { michael@0: λ *= d3_radians; michael@0: var cosφ = Math.cos(φ *= d3_radians); michael@0: x0 = cosφ * Math.cos(λ); michael@0: y0 = cosφ * Math.sin(λ); michael@0: z0 = Math.sin(φ); michael@0: d3_geo_centroid.point = nextPoint; michael@0: d3_geo_centroidPointXYZ(x0, y0, z0); michael@0: }; michael@0: function nextPoint(λ, φ) { michael@0: λ *= d3_radians; michael@0: var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), w = Math.atan2(Math.sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z); michael@0: d3_geo_centroidW1 += w; michael@0: d3_geo_centroidX1 += w * (x0 + (x0 = x)); michael@0: d3_geo_centroidY1 += w * (y0 + (y0 = y)); michael@0: d3_geo_centroidZ1 += w * (z0 + (z0 = z)); michael@0: d3_geo_centroidPointXYZ(x0, y0, z0); michael@0: } michael@0: } michael@0: function d3_geo_centroidLineEnd() { michael@0: d3_geo_centroid.point = d3_geo_centroidPoint; michael@0: } michael@0: function d3_geo_centroidRingStart() { michael@0: var λ00, φ00, x0, y0, z0; michael@0: d3_geo_centroid.point = function(λ, φ) { michael@0: λ00 = λ, φ00 = φ; michael@0: d3_geo_centroid.point = nextPoint; michael@0: λ *= d3_radians; michael@0: var cosφ = Math.cos(φ *= d3_radians); michael@0: x0 = cosφ * Math.cos(λ); michael@0: y0 = cosφ * Math.sin(λ); michael@0: z0 = Math.sin(φ); michael@0: d3_geo_centroidPointXYZ(x0, y0, z0); michael@0: }; michael@0: d3_geo_centroid.lineEnd = function() { michael@0: nextPoint(λ00, φ00); michael@0: d3_geo_centroid.lineEnd = d3_geo_centroidLineEnd; michael@0: d3_geo_centroid.point = d3_geo_centroidPoint; michael@0: }; michael@0: function nextPoint(λ, φ) { michael@0: λ *= d3_radians; michael@0: var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), cx = y0 * z - z0 * y, cy = z0 * x - x0 * z, cz = x0 * y - y0 * x, m = Math.sqrt(cx * cx + cy * cy + cz * cz), u = x0 * x + y0 * y + z0 * z, v = m && -d3_acos(u) / m, w = Math.atan2(m, u); michael@0: d3_geo_centroidX2 += v * cx; michael@0: d3_geo_centroidY2 += v * cy; michael@0: d3_geo_centroidZ2 += v * cz; michael@0: d3_geo_centroidW1 += w; michael@0: d3_geo_centroidX1 += w * (x0 + (x0 = x)); michael@0: d3_geo_centroidY1 += w * (y0 + (y0 = y)); michael@0: d3_geo_centroidZ1 += w * (z0 + (z0 = z)); michael@0: d3_geo_centroidPointXYZ(x0, y0, z0); michael@0: } michael@0: } michael@0: function d3_true() { michael@0: return true; michael@0: } michael@0: function d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener) { michael@0: var subject = [], clip = []; michael@0: segments.forEach(function(segment) { michael@0: if ((n = segment.length - 1) <= 0) return; michael@0: var n, p0 = segment[0], p1 = segment[n]; michael@0: if (d3_geo_sphericalEqual(p0, p1)) { michael@0: listener.lineStart(); michael@0: for (var i = 0; i < n; ++i) listener.point((p0 = segment[i])[0], p0[1]); michael@0: listener.lineEnd(); michael@0: return; michael@0: } michael@0: var a = new d3_geo_clipPolygonIntersection(p0, segment, null, true), b = new d3_geo_clipPolygonIntersection(p0, null, a, false); michael@0: a.o = b; michael@0: subject.push(a); michael@0: clip.push(b); michael@0: a = new d3_geo_clipPolygonIntersection(p1, segment, null, false); michael@0: b = new d3_geo_clipPolygonIntersection(p1, null, a, true); michael@0: a.o = b; michael@0: subject.push(a); michael@0: clip.push(b); michael@0: }); michael@0: clip.sort(compare); michael@0: d3_geo_clipPolygonLinkCircular(subject); michael@0: d3_geo_clipPolygonLinkCircular(clip); michael@0: if (!subject.length) return; michael@0: for (var i = 0, entry = clipStartInside, n = clip.length; i < n; ++i) { michael@0: clip[i].e = entry = !entry; michael@0: } michael@0: var start = subject[0], points, point; michael@0: while (1) { michael@0: var current = start, isSubject = true; michael@0: while (current.v) if ((current = current.n) === start) return; michael@0: points = current.z; michael@0: listener.lineStart(); michael@0: do { michael@0: current.v = current.o.v = true; michael@0: if (current.e) { michael@0: if (isSubject) { michael@0: for (var i = 0, n = points.length; i < n; ++i) listener.point((point = points[i])[0], point[1]); michael@0: } else { michael@0: interpolate(current.x, current.n.x, 1, listener); michael@0: } michael@0: current = current.n; michael@0: } else { michael@0: if (isSubject) { michael@0: points = current.p.z; michael@0: for (var i = points.length - 1; i >= 0; --i) listener.point((point = points[i])[0], point[1]); michael@0: } else { michael@0: interpolate(current.x, current.p.x, -1, listener); michael@0: } michael@0: current = current.p; michael@0: } michael@0: current = current.o; michael@0: points = current.z; michael@0: isSubject = !isSubject; michael@0: } while (!current.v); michael@0: listener.lineEnd(); michael@0: } michael@0: } michael@0: function d3_geo_clipPolygonLinkCircular(array) { michael@0: if (!(n = array.length)) return; michael@0: var n, i = 0, a = array[0], b; michael@0: while (++i < n) { michael@0: a.n = b = array[i]; michael@0: b.p = a; michael@0: a = b; michael@0: } michael@0: a.n = b = array[0]; michael@0: b.p = a; michael@0: } michael@0: function d3_geo_clipPolygonIntersection(point, points, other, entry) { michael@0: this.x = point; michael@0: this.z = points; michael@0: this.o = other; michael@0: this.e = entry; michael@0: this.v = false; michael@0: this.n = this.p = null; michael@0: } michael@0: function d3_geo_clip(pointVisible, clipLine, interpolate, clipStart) { michael@0: return function(rotate, listener) { michael@0: var line = clipLine(listener), rotatedClipStart = rotate.invert(clipStart[0], clipStart[1]); michael@0: var clip = { michael@0: point: point, michael@0: lineStart: lineStart, michael@0: lineEnd: lineEnd, michael@0: polygonStart: function() { michael@0: clip.point = pointRing; michael@0: clip.lineStart = ringStart; michael@0: clip.lineEnd = ringEnd; michael@0: segments = []; michael@0: polygon = []; michael@0: listener.polygonStart(); michael@0: }, michael@0: polygonEnd: function() { michael@0: clip.point = point; michael@0: clip.lineStart = lineStart; michael@0: clip.lineEnd = lineEnd; michael@0: segments = d3.merge(segments); michael@0: var clipStartInside = d3_geo_pointInPolygon(rotatedClipStart, polygon); michael@0: if (segments.length) { michael@0: d3_geo_clipPolygon(segments, d3_geo_clipSort, clipStartInside, interpolate, listener); michael@0: } else if (clipStartInside) { michael@0: listener.lineStart(); michael@0: interpolate(null, null, 1, listener); michael@0: listener.lineEnd(); michael@0: } michael@0: listener.polygonEnd(); michael@0: segments = polygon = null; michael@0: }, michael@0: sphere: function() { michael@0: listener.polygonStart(); michael@0: listener.lineStart(); michael@0: interpolate(null, null, 1, listener); michael@0: listener.lineEnd(); michael@0: listener.polygonEnd(); michael@0: } michael@0: }; michael@0: function point(λ, φ) { michael@0: var point = rotate(λ, φ); michael@0: if (pointVisible(λ = point[0], φ = point[1])) listener.point(λ, φ); michael@0: } michael@0: function pointLine(λ, φ) { michael@0: var point = rotate(λ, φ); michael@0: line.point(point[0], point[1]); michael@0: } michael@0: function lineStart() { michael@0: clip.point = pointLine; michael@0: line.lineStart(); michael@0: } michael@0: function lineEnd() { michael@0: clip.point = point; michael@0: line.lineEnd(); michael@0: } michael@0: var segments; michael@0: var buffer = d3_geo_clipBufferListener(), ringListener = clipLine(buffer), polygon, ring; michael@0: function pointRing(λ, φ) { michael@0: ring.push([ λ, φ ]); michael@0: var point = rotate(λ, φ); michael@0: ringListener.point(point[0], point[1]); michael@0: } michael@0: function ringStart() { michael@0: ringListener.lineStart(); michael@0: ring = []; michael@0: } michael@0: function ringEnd() { michael@0: pointRing(ring[0][0], ring[0][1]); michael@0: ringListener.lineEnd(); michael@0: var clean = ringListener.clean(), ringSegments = buffer.buffer(), segment, n = ringSegments.length; michael@0: ring.pop(); michael@0: polygon.push(ring); michael@0: ring = null; michael@0: if (!n) return; michael@0: if (clean & 1) { michael@0: segment = ringSegments[0]; michael@0: var n = segment.length - 1, i = -1, point; michael@0: listener.lineStart(); michael@0: while (++i < n) listener.point((point = segment[i])[0], point[1]); michael@0: listener.lineEnd(); michael@0: return; michael@0: } michael@0: if (n > 1 && clean & 2) ringSegments.push(ringSegments.pop().concat(ringSegments.shift())); michael@0: segments.push(ringSegments.filter(d3_geo_clipSegmentLength1)); michael@0: } michael@0: return clip; michael@0: }; michael@0: } michael@0: function d3_geo_clipSegmentLength1(segment) { michael@0: return segment.length > 1; michael@0: } michael@0: function d3_geo_clipBufferListener() { michael@0: var lines = [], line; michael@0: return { michael@0: lineStart: function() { michael@0: lines.push(line = []); michael@0: }, michael@0: point: function(λ, φ) { michael@0: line.push([ λ, φ ]); michael@0: }, michael@0: lineEnd: d3_noop, michael@0: buffer: function() { michael@0: var buffer = lines; michael@0: lines = []; michael@0: line = null; michael@0: return buffer; michael@0: }, michael@0: rejoin: function() { michael@0: if (lines.length > 1) lines.push(lines.pop().concat(lines.shift())); michael@0: } michael@0: }; michael@0: } michael@0: function d3_geo_clipSort(a, b) { michael@0: return ((a = a.x)[0] < 0 ? a[1] - halfπ - ε : halfπ - a[1]) - ((b = b.x)[0] < 0 ? b[1] - halfπ - ε : halfπ - b[1]); michael@0: } michael@0: function d3_geo_pointInPolygon(point, polygon) { michael@0: var meridian = point[0], parallel = point[1], meridianNormal = [ Math.sin(meridian), -Math.cos(meridian), 0 ], polarAngle = 0, winding = 0; michael@0: d3_geo_areaRingSum.reset(); michael@0: for (var i = 0, n = polygon.length; i < n; ++i) { michael@0: var ring = polygon[i], m = ring.length; michael@0: if (!m) continue; michael@0: var point0 = ring[0], λ0 = point0[0], φ0 = point0[1] / 2 + π / 4, sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), j = 1; michael@0: while (true) { michael@0: if (j === m) j = 0; michael@0: point = ring[j]; michael@0: var λ = point[0], φ = point[1] / 2 + π / 4, sinφ = Math.sin(φ), cosφ = Math.cos(φ), dλ = λ - λ0, antimeridian = abs(dλ) > π, k = sinφ0 * sinφ; michael@0: d3_geo_areaRingSum.add(Math.atan2(k * Math.sin(dλ), cosφ0 * cosφ + k * Math.cos(dλ))); michael@0: polarAngle += antimeridian ? dλ + (dλ >= 0 ? τ : -τ) : dλ; michael@0: if (antimeridian ^ λ0 >= meridian ^ λ >= meridian) { michael@0: var arc = d3_geo_cartesianCross(d3_geo_cartesian(point0), d3_geo_cartesian(point)); michael@0: d3_geo_cartesianNormalize(arc); michael@0: var intersection = d3_geo_cartesianCross(meridianNormal, arc); michael@0: d3_geo_cartesianNormalize(intersection); michael@0: var φarc = (antimeridian ^ dλ >= 0 ? -1 : 1) * d3_asin(intersection[2]); michael@0: if (parallel > φarc || parallel === φarc && (arc[0] || arc[1])) { michael@0: winding += antimeridian ^ dλ >= 0 ? 1 : -1; michael@0: } michael@0: } michael@0: if (!j++) break; michael@0: λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ, point0 = point; michael@0: } michael@0: } michael@0: return (polarAngle < -ε || polarAngle < ε && d3_geo_areaRingSum < 0) ^ winding & 1; michael@0: } michael@0: var d3_geo_clipAntimeridian = d3_geo_clip(d3_true, d3_geo_clipAntimeridianLine, d3_geo_clipAntimeridianInterpolate, [ -π, -π / 2 ]); michael@0: function d3_geo_clipAntimeridianLine(listener) { michael@0: var λ0 = NaN, φ0 = NaN, sλ0 = NaN, clean; michael@0: return { michael@0: lineStart: function() { michael@0: listener.lineStart(); michael@0: clean = 1; michael@0: }, michael@0: point: function(λ1, φ1) { michael@0: var sλ1 = λ1 > 0 ? π : -π, dλ = abs(λ1 - λ0); michael@0: if (abs(dλ - π) < ε) { michael@0: listener.point(λ0, φ0 = (φ0 + φ1) / 2 > 0 ? halfπ : -halfπ); michael@0: listener.point(sλ0, φ0); michael@0: listener.lineEnd(); michael@0: listener.lineStart(); michael@0: listener.point(sλ1, φ0); michael@0: listener.point(λ1, φ0); michael@0: clean = 0; michael@0: } else if (sλ0 !== sλ1 && dλ >= π) { michael@0: if (abs(λ0 - sλ0) < ε) λ0 -= sλ0 * ε; michael@0: if (abs(λ1 - sλ1) < ε) λ1 -= sλ1 * ε; michael@0: φ0 = d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1); michael@0: listener.point(sλ0, φ0); michael@0: listener.lineEnd(); michael@0: listener.lineStart(); michael@0: listener.point(sλ1, φ0); michael@0: clean = 0; michael@0: } michael@0: listener.point(λ0 = λ1, φ0 = φ1); michael@0: sλ0 = sλ1; michael@0: }, michael@0: lineEnd: function() { michael@0: listener.lineEnd(); michael@0: λ0 = φ0 = NaN; michael@0: }, michael@0: clean: function() { michael@0: return 2 - clean; michael@0: } michael@0: }; michael@0: } michael@0: function d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1) { michael@0: var cosφ0, cosφ1, sinλ0_λ1 = Math.sin(λ0 - λ1); michael@0: return abs(sinλ0_λ1) > ε ? Math.atan((Math.sin(φ0) * (cosφ1 = Math.cos(φ1)) * Math.sin(λ1) - Math.sin(φ1) * (cosφ0 = Math.cos(φ0)) * Math.sin(λ0)) / (cosφ0 * cosφ1 * sinλ0_λ1)) : (φ0 + φ1) / 2; michael@0: } michael@0: function d3_geo_clipAntimeridianInterpolate(from, to, direction, listener) { michael@0: var φ; michael@0: if (from == null) { michael@0: φ = direction * halfπ; michael@0: listener.point(-π, φ); michael@0: listener.point(0, φ); michael@0: listener.point(π, φ); michael@0: listener.point(π, 0); michael@0: listener.point(π, -φ); michael@0: listener.point(0, -φ); michael@0: listener.point(-π, -φ); michael@0: listener.point(-π, 0); michael@0: listener.point(-π, φ); michael@0: } else if (abs(from[0] - to[0]) > ε) { michael@0: var s = from[0] < to[0] ? π : -π; michael@0: φ = direction * s / 2; michael@0: listener.point(-s, φ); michael@0: listener.point(0, φ); michael@0: listener.point(s, φ); michael@0: } else { michael@0: listener.point(to[0], to[1]); michael@0: } michael@0: } michael@0: function d3_geo_clipCircle(radius) { michael@0: var cr = Math.cos(radius), smallRadius = cr > 0, notHemisphere = abs(cr) > ε, interpolate = d3_geo_circleInterpolate(radius, 6 * d3_radians); michael@0: return d3_geo_clip(visible, clipLine, interpolate, smallRadius ? [ 0, -radius ] : [ -π, radius - π ]); michael@0: function visible(λ, φ) { michael@0: return Math.cos(λ) * Math.cos(φ) > cr; michael@0: } michael@0: function clipLine(listener) { michael@0: var point0, c0, v0, v00, clean; michael@0: return { michael@0: lineStart: function() { michael@0: v00 = v0 = false; michael@0: clean = 1; michael@0: }, michael@0: point: function(λ, φ) { michael@0: var point1 = [ λ, φ ], point2, v = visible(λ, φ), c = smallRadius ? v ? 0 : code(λ, φ) : v ? code(λ + (λ < 0 ? π : -π), φ) : 0; michael@0: if (!point0 && (v00 = v0 = v)) listener.lineStart(); michael@0: if (v !== v0) { michael@0: point2 = intersect(point0, point1); michael@0: if (d3_geo_sphericalEqual(point0, point2) || d3_geo_sphericalEqual(point1, point2)) { michael@0: point1[0] += ε; michael@0: point1[1] += ε; michael@0: v = visible(point1[0], point1[1]); michael@0: } michael@0: } michael@0: if (v !== v0) { michael@0: clean = 0; michael@0: if (v) { michael@0: listener.lineStart(); michael@0: point2 = intersect(point1, point0); michael@0: listener.point(point2[0], point2[1]); michael@0: } else { michael@0: point2 = intersect(point0, point1); michael@0: listener.point(point2[0], point2[1]); michael@0: listener.lineEnd(); michael@0: } michael@0: point0 = point2; michael@0: } else if (notHemisphere && point0 && smallRadius ^ v) { michael@0: var t; michael@0: if (!(c & c0) && (t = intersect(point1, point0, true))) { michael@0: clean = 0; michael@0: if (smallRadius) { michael@0: listener.lineStart(); michael@0: listener.point(t[0][0], t[0][1]); michael@0: listener.point(t[1][0], t[1][1]); michael@0: listener.lineEnd(); michael@0: } else { michael@0: listener.point(t[1][0], t[1][1]); michael@0: listener.lineEnd(); michael@0: listener.lineStart(); michael@0: listener.point(t[0][0], t[0][1]); michael@0: } michael@0: } michael@0: } michael@0: if (v && (!point0 || !d3_geo_sphericalEqual(point0, point1))) { michael@0: listener.point(point1[0], point1[1]); michael@0: } michael@0: point0 = point1, v0 = v, c0 = c; michael@0: }, michael@0: lineEnd: function() { michael@0: if (v0) listener.lineEnd(); michael@0: point0 = null; michael@0: }, michael@0: clean: function() { michael@0: return clean | (v00 && v0) << 1; michael@0: } michael@0: }; michael@0: } michael@0: function intersect(a, b, two) { michael@0: var pa = d3_geo_cartesian(a), pb = d3_geo_cartesian(b); michael@0: var n1 = [ 1, 0, 0 ], n2 = d3_geo_cartesianCross(pa, pb), n2n2 = d3_geo_cartesianDot(n2, n2), n1n2 = n2[0], determinant = n2n2 - n1n2 * n1n2; michael@0: if (!determinant) return !two && a; michael@0: var c1 = cr * n2n2 / determinant, c2 = -cr * n1n2 / determinant, n1xn2 = d3_geo_cartesianCross(n1, n2), A = d3_geo_cartesianScale(n1, c1), B = d3_geo_cartesianScale(n2, c2); michael@0: d3_geo_cartesianAdd(A, B); michael@0: var u = n1xn2, w = d3_geo_cartesianDot(A, u), uu = d3_geo_cartesianDot(u, u), t2 = w * w - uu * (d3_geo_cartesianDot(A, A) - 1); michael@0: if (t2 < 0) return; michael@0: var t = Math.sqrt(t2), q = d3_geo_cartesianScale(u, (-w - t) / uu); michael@0: d3_geo_cartesianAdd(q, A); michael@0: q = d3_geo_spherical(q); michael@0: if (!two) return q; michael@0: var λ0 = a[0], λ1 = b[0], φ0 = a[1], φ1 = b[1], z; michael@0: if (λ1 < λ0) z = λ0, λ0 = λ1, λ1 = z; michael@0: var δλ = λ1 - λ0, polar = abs(δλ - π) < ε, meridian = polar || δλ < ε; michael@0: if (!polar && φ1 < φ0) z = φ0, φ0 = φ1, φ1 = z; michael@0: if (meridian ? polar ? φ0 + φ1 > 0 ^ q[1] < (abs(q[0] - λ0) < ε ? φ0 : φ1) : φ0 <= q[1] && q[1] <= φ1 : δλ > π ^ (λ0 <= q[0] && q[0] <= λ1)) { michael@0: var q1 = d3_geo_cartesianScale(u, (-w + t) / uu); michael@0: d3_geo_cartesianAdd(q1, A); michael@0: return [ q, d3_geo_spherical(q1) ]; michael@0: } michael@0: } michael@0: function code(λ, φ) { michael@0: var r = smallRadius ? radius : π - radius, code = 0; michael@0: if (λ < -r) code |= 1; else if (λ > r) code |= 2; michael@0: if (φ < -r) code |= 4; else if (φ > r) code |= 8; michael@0: return code; michael@0: } michael@0: } michael@0: function d3_geom_clipLine(x0, y0, x1, y1) { michael@0: return function(line) { michael@0: var a = line.a, b = line.b, ax = a.x, ay = a.y, bx = b.x, by = b.y, t0 = 0, t1 = 1, dx = bx - ax, dy = by - ay, r; michael@0: r = x0 - ax; michael@0: if (!dx && r > 0) return; michael@0: r /= dx; michael@0: if (dx < 0) { michael@0: if (r < t0) return; michael@0: if (r < t1) t1 = r; michael@0: } else if (dx > 0) { michael@0: if (r > t1) return; michael@0: if (r > t0) t0 = r; michael@0: } michael@0: r = x1 - ax; michael@0: if (!dx && r < 0) return; michael@0: r /= dx; michael@0: if (dx < 0) { michael@0: if (r > t1) return; michael@0: if (r > t0) t0 = r; michael@0: } else if (dx > 0) { michael@0: if (r < t0) return; michael@0: if (r < t1) t1 = r; michael@0: } michael@0: r = y0 - ay; michael@0: if (!dy && r > 0) return; michael@0: r /= dy; michael@0: if (dy < 0) { michael@0: if (r < t0) return; michael@0: if (r < t1) t1 = r; michael@0: } else if (dy > 0) { michael@0: if (r > t1) return; michael@0: if (r > t0) t0 = r; michael@0: } michael@0: r = y1 - ay; michael@0: if (!dy && r < 0) return; michael@0: r /= dy; michael@0: if (dy < 0) { michael@0: if (r > t1) return; michael@0: if (r > t0) t0 = r; michael@0: } else if (dy > 0) { michael@0: if (r < t0) return; michael@0: if (r < t1) t1 = r; michael@0: } michael@0: if (t0 > 0) line.a = { michael@0: x: ax + t0 * dx, michael@0: y: ay + t0 * dy michael@0: }; michael@0: if (t1 < 1) line.b = { michael@0: x: ax + t1 * dx, michael@0: y: ay + t1 * dy michael@0: }; michael@0: return line; michael@0: }; michael@0: } michael@0: var d3_geo_clipExtentMAX = 1e9; michael@0: d3.geo.clipExtent = function() { michael@0: var x0, y0, x1, y1, stream, clip, clipExtent = { michael@0: stream: function(output) { michael@0: if (stream) stream.valid = false; michael@0: stream = clip(output); michael@0: stream.valid = true; michael@0: return stream; michael@0: }, michael@0: extent: function(_) { michael@0: if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; michael@0: clip = d3_geo_clipExtent(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]); michael@0: if (stream) stream.valid = false, stream = null; michael@0: return clipExtent; michael@0: } michael@0: }; michael@0: return clipExtent.extent([ [ 0, 0 ], [ 960, 500 ] ]); michael@0: }; michael@0: function d3_geo_clipExtent(x0, y0, x1, y1) { michael@0: return function(listener) { michael@0: var listener_ = listener, bufferListener = d3_geo_clipBufferListener(), clipLine = d3_geom_clipLine(x0, y0, x1, y1), segments, polygon, ring; michael@0: var clip = { michael@0: point: point, michael@0: lineStart: lineStart, michael@0: lineEnd: lineEnd, michael@0: polygonStart: function() { michael@0: listener = bufferListener; michael@0: segments = []; michael@0: polygon = []; michael@0: clean = true; michael@0: }, michael@0: polygonEnd: function() { michael@0: listener = listener_; michael@0: segments = d3.merge(segments); michael@0: var clipStartInside = insidePolygon([ x0, y1 ]), inside = clean && clipStartInside, visible = segments.length; michael@0: if (inside || visible) { michael@0: listener.polygonStart(); michael@0: if (inside) { michael@0: listener.lineStart(); michael@0: interpolate(null, null, 1, listener); michael@0: listener.lineEnd(); michael@0: } michael@0: if (visible) { michael@0: d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener); michael@0: } michael@0: listener.polygonEnd(); michael@0: } michael@0: segments = polygon = ring = null; michael@0: } michael@0: }; michael@0: function insidePolygon(p) { michael@0: var wn = 0, n = polygon.length, y = p[1]; michael@0: for (var i = 0; i < n; ++i) { michael@0: for (var j = 1, v = polygon[i], m = v.length, a = v[0], b; j < m; ++j) { michael@0: b = v[j]; michael@0: if (a[1] <= y) { michael@0: if (b[1] > y && d3_cross2d(a, b, p) > 0) ++wn; michael@0: } else { michael@0: if (b[1] <= y && d3_cross2d(a, b, p) < 0) --wn; michael@0: } michael@0: a = b; michael@0: } michael@0: } michael@0: return wn !== 0; michael@0: } michael@0: function interpolate(from, to, direction, listener) { michael@0: var a = 0, a1 = 0; michael@0: if (from == null || (a = corner(from, direction)) !== (a1 = corner(to, direction)) || comparePoints(from, to) < 0 ^ direction > 0) { michael@0: do { michael@0: listener.point(a === 0 || a === 3 ? x0 : x1, a > 1 ? y1 : y0); michael@0: } while ((a = (a + direction + 4) % 4) !== a1); michael@0: } else { michael@0: listener.point(to[0], to[1]); michael@0: } michael@0: } michael@0: function pointVisible(x, y) { michael@0: return x0 <= x && x <= x1 && y0 <= y && y <= y1; michael@0: } michael@0: function point(x, y) { michael@0: if (pointVisible(x, y)) listener.point(x, y); michael@0: } michael@0: var x__, y__, v__, x_, y_, v_, first, clean; michael@0: function lineStart() { michael@0: clip.point = linePoint; michael@0: if (polygon) polygon.push(ring = []); michael@0: first = true; michael@0: v_ = false; michael@0: x_ = y_ = NaN; michael@0: } michael@0: function lineEnd() { michael@0: if (segments) { michael@0: linePoint(x__, y__); michael@0: if (v__ && v_) bufferListener.rejoin(); michael@0: segments.push(bufferListener.buffer()); michael@0: } michael@0: clip.point = point; michael@0: if (v_) listener.lineEnd(); michael@0: } michael@0: function linePoint(x, y) { michael@0: x = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, x)); michael@0: y = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, y)); michael@0: var v = pointVisible(x, y); michael@0: if (polygon) ring.push([ x, y ]); michael@0: if (first) { michael@0: x__ = x, y__ = y, v__ = v; michael@0: first = false; michael@0: if (v) { michael@0: listener.lineStart(); michael@0: listener.point(x, y); michael@0: } michael@0: } else { michael@0: if (v && v_) listener.point(x, y); else { michael@0: var l = { michael@0: a: { michael@0: x: x_, michael@0: y: y_ michael@0: }, michael@0: b: { michael@0: x: x, michael@0: y: y michael@0: } michael@0: }; michael@0: if (clipLine(l)) { michael@0: if (!v_) { michael@0: listener.lineStart(); michael@0: listener.point(l.a.x, l.a.y); michael@0: } michael@0: listener.point(l.b.x, l.b.y); michael@0: if (!v) listener.lineEnd(); michael@0: clean = false; michael@0: } else if (v) { michael@0: listener.lineStart(); michael@0: listener.point(x, y); michael@0: clean = false; michael@0: } michael@0: } michael@0: } michael@0: x_ = x, y_ = y, v_ = v; michael@0: } michael@0: return clip; michael@0: }; michael@0: function corner(p, direction) { michael@0: return abs(p[0] - x0) < ε ? direction > 0 ? 0 : 3 : abs(p[0] - x1) < ε ? direction > 0 ? 2 : 1 : abs(p[1] - y0) < ε ? direction > 0 ? 1 : 0 : direction > 0 ? 3 : 2; michael@0: } michael@0: function compare(a, b) { michael@0: return comparePoints(a.x, b.x); michael@0: } michael@0: function comparePoints(a, b) { michael@0: var ca = corner(a, 1), cb = corner(b, 1); michael@0: return ca !== cb ? ca - cb : ca === 0 ? b[1] - a[1] : ca === 1 ? a[0] - b[0] : ca === 2 ? a[1] - b[1] : b[0] - a[0]; michael@0: } michael@0: } michael@0: function d3_geo_compose(a, b) { michael@0: function compose(x, y) { michael@0: return x = a(x, y), b(x[0], x[1]); michael@0: } michael@0: if (a.invert && b.invert) compose.invert = function(x, y) { michael@0: return x = b.invert(x, y), x && a.invert(x[0], x[1]); michael@0: }; michael@0: return compose; michael@0: } michael@0: function d3_geo_conic(projectAt) { michael@0: var φ0 = 0, φ1 = π / 3, m = d3_geo_projectionMutator(projectAt), p = m(φ0, φ1); michael@0: p.parallels = function(_) { michael@0: if (!arguments.length) return [ φ0 / π * 180, φ1 / π * 180 ]; michael@0: return m(φ0 = _[0] * π / 180, φ1 = _[1] * π / 180); michael@0: }; michael@0: return p; michael@0: } michael@0: function d3_geo_conicEqualArea(φ0, φ1) { michael@0: var sinφ0 = Math.sin(φ0), n = (sinφ0 + Math.sin(φ1)) / 2, C = 1 + sinφ0 * (2 * n - sinφ0), ρ0 = Math.sqrt(C) / n; michael@0: function forward(λ, φ) { michael@0: var ρ = Math.sqrt(C - 2 * n * Math.sin(φ)) / n; michael@0: return [ ρ * Math.sin(λ *= n), ρ0 - ρ * Math.cos(λ) ]; michael@0: } michael@0: forward.invert = function(x, y) { michael@0: var ρ0_y = ρ0 - y; michael@0: return [ Math.atan2(x, ρ0_y) / n, d3_asin((C - (x * x + ρ0_y * ρ0_y) * n * n) / (2 * n)) ]; michael@0: }; michael@0: return forward; michael@0: } michael@0: (d3.geo.conicEqualArea = function() { michael@0: return d3_geo_conic(d3_geo_conicEqualArea); michael@0: }).raw = d3_geo_conicEqualArea; michael@0: d3.geo.albers = function() { michael@0: return d3.geo.conicEqualArea().rotate([ 96, 0 ]).center([ -.6, 38.7 ]).parallels([ 29.5, 45.5 ]).scale(1070); michael@0: }; michael@0: d3.geo.albersUsa = function() { michael@0: var lower48 = d3.geo.albers(); michael@0: var alaska = d3.geo.conicEqualArea().rotate([ 154, 0 ]).center([ -2, 58.5 ]).parallels([ 55, 65 ]); michael@0: var hawaii = d3.geo.conicEqualArea().rotate([ 157, 0 ]).center([ -3, 19.9 ]).parallels([ 8, 18 ]); michael@0: var point, pointStream = { michael@0: point: function(x, y) { michael@0: point = [ x, y ]; michael@0: } michael@0: }, lower48Point, alaskaPoint, hawaiiPoint; michael@0: function albersUsa(coordinates) { michael@0: var x = coordinates[0], y = coordinates[1]; michael@0: point = null; michael@0: (lower48Point(x, y), point) || (alaskaPoint(x, y), point) || hawaiiPoint(x, y); michael@0: return point; michael@0: } michael@0: albersUsa.invert = function(coordinates) { michael@0: var k = lower48.scale(), t = lower48.translate(), x = (coordinates[0] - t[0]) / k, y = (coordinates[1] - t[1]) / k; michael@0: return (y >= .12 && y < .234 && x >= -.425 && x < -.214 ? alaska : y >= .166 && y < .234 && x >= -.214 && x < -.115 ? hawaii : lower48).invert(coordinates); michael@0: }; michael@0: albersUsa.stream = function(stream) { michael@0: var lower48Stream = lower48.stream(stream), alaskaStream = alaska.stream(stream), hawaiiStream = hawaii.stream(stream); michael@0: return { michael@0: point: function(x, y) { michael@0: lower48Stream.point(x, y); michael@0: alaskaStream.point(x, y); michael@0: hawaiiStream.point(x, y); michael@0: }, michael@0: sphere: function() { michael@0: lower48Stream.sphere(); michael@0: alaskaStream.sphere(); michael@0: hawaiiStream.sphere(); michael@0: }, michael@0: lineStart: function() { michael@0: lower48Stream.lineStart(); michael@0: alaskaStream.lineStart(); michael@0: hawaiiStream.lineStart(); michael@0: }, michael@0: lineEnd: function() { michael@0: lower48Stream.lineEnd(); michael@0: alaskaStream.lineEnd(); michael@0: hawaiiStream.lineEnd(); michael@0: }, michael@0: polygonStart: function() { michael@0: lower48Stream.polygonStart(); michael@0: alaskaStream.polygonStart(); michael@0: hawaiiStream.polygonStart(); michael@0: }, michael@0: polygonEnd: function() { michael@0: lower48Stream.polygonEnd(); michael@0: alaskaStream.polygonEnd(); michael@0: hawaiiStream.polygonEnd(); michael@0: } michael@0: }; michael@0: }; michael@0: albersUsa.precision = function(_) { michael@0: if (!arguments.length) return lower48.precision(); michael@0: lower48.precision(_); michael@0: alaska.precision(_); michael@0: hawaii.precision(_); michael@0: return albersUsa; michael@0: }; michael@0: albersUsa.scale = function(_) { michael@0: if (!arguments.length) return lower48.scale(); michael@0: lower48.scale(_); michael@0: alaska.scale(_ * .35); michael@0: hawaii.scale(_); michael@0: return albersUsa.translate(lower48.translate()); michael@0: }; michael@0: albersUsa.translate = function(_) { michael@0: if (!arguments.length) return lower48.translate(); michael@0: var k = lower48.scale(), x = +_[0], y = +_[1]; michael@0: lower48Point = lower48.translate(_).clipExtent([ [ x - .455 * k, y - .238 * k ], [ x + .455 * k, y + .238 * k ] ]).stream(pointStream).point; michael@0: alaskaPoint = alaska.translate([ x - .307 * k, y + .201 * k ]).clipExtent([ [ x - .425 * k + ε, y + .12 * k + ε ], [ x - .214 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; michael@0: hawaiiPoint = hawaii.translate([ x - .205 * k, y + .212 * k ]).clipExtent([ [ x - .214 * k + ε, y + .166 * k + ε ], [ x - .115 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; michael@0: return albersUsa; michael@0: }; michael@0: return albersUsa.scale(1070); michael@0: }; michael@0: var d3_geo_pathAreaSum, d3_geo_pathAreaPolygon, d3_geo_pathArea = { michael@0: point: d3_noop, michael@0: lineStart: d3_noop, michael@0: lineEnd: d3_noop, michael@0: polygonStart: function() { michael@0: d3_geo_pathAreaPolygon = 0; michael@0: d3_geo_pathArea.lineStart = d3_geo_pathAreaRingStart; michael@0: }, michael@0: polygonEnd: function() { michael@0: d3_geo_pathArea.lineStart = d3_geo_pathArea.lineEnd = d3_geo_pathArea.point = d3_noop; michael@0: d3_geo_pathAreaSum += abs(d3_geo_pathAreaPolygon / 2); michael@0: } michael@0: }; michael@0: function d3_geo_pathAreaRingStart() { michael@0: var x00, y00, x0, y0; michael@0: d3_geo_pathArea.point = function(x, y) { michael@0: d3_geo_pathArea.point = nextPoint; michael@0: x00 = x0 = x, y00 = y0 = y; michael@0: }; michael@0: function nextPoint(x, y) { michael@0: d3_geo_pathAreaPolygon += y0 * x - x0 * y; michael@0: x0 = x, y0 = y; michael@0: } michael@0: d3_geo_pathArea.lineEnd = function() { michael@0: nextPoint(x00, y00); michael@0: }; michael@0: } michael@0: var d3_geo_pathBoundsX0, d3_geo_pathBoundsY0, d3_geo_pathBoundsX1, d3_geo_pathBoundsY1; michael@0: var d3_geo_pathBounds = { michael@0: point: d3_geo_pathBoundsPoint, michael@0: lineStart: d3_noop, michael@0: lineEnd: d3_noop, michael@0: polygonStart: d3_noop, michael@0: polygonEnd: d3_noop michael@0: }; michael@0: function d3_geo_pathBoundsPoint(x, y) { michael@0: if (x < d3_geo_pathBoundsX0) d3_geo_pathBoundsX0 = x; michael@0: if (x > d3_geo_pathBoundsX1) d3_geo_pathBoundsX1 = x; michael@0: if (y < d3_geo_pathBoundsY0) d3_geo_pathBoundsY0 = y; michael@0: if (y > d3_geo_pathBoundsY1) d3_geo_pathBoundsY1 = y; michael@0: } michael@0: function d3_geo_pathBuffer() { michael@0: var pointCircle = d3_geo_pathBufferCircle(4.5), buffer = []; michael@0: var stream = { michael@0: point: point, michael@0: lineStart: function() { michael@0: stream.point = pointLineStart; michael@0: }, michael@0: lineEnd: lineEnd, michael@0: polygonStart: function() { michael@0: stream.lineEnd = lineEndPolygon; michael@0: }, michael@0: polygonEnd: function() { michael@0: stream.lineEnd = lineEnd; michael@0: stream.point = point; michael@0: }, michael@0: pointRadius: function(_) { michael@0: pointCircle = d3_geo_pathBufferCircle(_); michael@0: return stream; michael@0: }, michael@0: result: function() { michael@0: if (buffer.length) { michael@0: var result = buffer.join(""); michael@0: buffer = []; michael@0: return result; michael@0: } michael@0: } michael@0: }; michael@0: function point(x, y) { michael@0: buffer.push("M", x, ",", y, pointCircle); michael@0: } michael@0: function pointLineStart(x, y) { michael@0: buffer.push("M", x, ",", y); michael@0: stream.point = pointLine; michael@0: } michael@0: function pointLine(x, y) { michael@0: buffer.push("L", x, ",", y); michael@0: } michael@0: function lineEnd() { michael@0: stream.point = point; michael@0: } michael@0: function lineEndPolygon() { michael@0: buffer.push("Z"); michael@0: } michael@0: return stream; michael@0: } michael@0: function d3_geo_pathBufferCircle(radius) { michael@0: return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + 2 * radius + "z"; michael@0: } michael@0: var d3_geo_pathCentroid = { michael@0: point: d3_geo_pathCentroidPoint, michael@0: lineStart: d3_geo_pathCentroidLineStart, michael@0: lineEnd: d3_geo_pathCentroidLineEnd, michael@0: polygonStart: function() { michael@0: d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidRingStart; michael@0: }, michael@0: polygonEnd: function() { michael@0: d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; michael@0: d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidLineStart; michael@0: d3_geo_pathCentroid.lineEnd = d3_geo_pathCentroidLineEnd; michael@0: } michael@0: }; michael@0: function d3_geo_pathCentroidPoint(x, y) { michael@0: d3_geo_centroidX0 += x; michael@0: d3_geo_centroidY0 += y; michael@0: ++d3_geo_centroidZ0; michael@0: } michael@0: function d3_geo_pathCentroidLineStart() { michael@0: var x0, y0; michael@0: d3_geo_pathCentroid.point = function(x, y) { michael@0: d3_geo_pathCentroid.point = nextPoint; michael@0: d3_geo_pathCentroidPoint(x0 = x, y0 = y); michael@0: }; michael@0: function nextPoint(x, y) { michael@0: var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); michael@0: d3_geo_centroidX1 += z * (x0 + x) / 2; michael@0: d3_geo_centroidY1 += z * (y0 + y) / 2; michael@0: d3_geo_centroidZ1 += z; michael@0: d3_geo_pathCentroidPoint(x0 = x, y0 = y); michael@0: } michael@0: } michael@0: function d3_geo_pathCentroidLineEnd() { michael@0: d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; michael@0: } michael@0: function d3_geo_pathCentroidRingStart() { michael@0: var x00, y00, x0, y0; michael@0: d3_geo_pathCentroid.point = function(x, y) { michael@0: d3_geo_pathCentroid.point = nextPoint; michael@0: d3_geo_pathCentroidPoint(x00 = x0 = x, y00 = y0 = y); michael@0: }; michael@0: function nextPoint(x, y) { michael@0: var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); michael@0: d3_geo_centroidX1 += z * (x0 + x) / 2; michael@0: d3_geo_centroidY1 += z * (y0 + y) / 2; michael@0: d3_geo_centroidZ1 += z; michael@0: z = y0 * x - x0 * y; michael@0: d3_geo_centroidX2 += z * (x0 + x); michael@0: d3_geo_centroidY2 += z * (y0 + y); michael@0: d3_geo_centroidZ2 += z * 3; michael@0: d3_geo_pathCentroidPoint(x0 = x, y0 = y); michael@0: } michael@0: d3_geo_pathCentroid.lineEnd = function() { michael@0: nextPoint(x00, y00); michael@0: }; michael@0: } michael@0: function d3_geo_pathContext(context) { michael@0: var pointRadius = 4.5; michael@0: var stream = { michael@0: point: point, michael@0: lineStart: function() { michael@0: stream.point = pointLineStart; michael@0: }, michael@0: lineEnd: lineEnd, michael@0: polygonStart: function() { michael@0: stream.lineEnd = lineEndPolygon; michael@0: }, michael@0: polygonEnd: function() { michael@0: stream.lineEnd = lineEnd; michael@0: stream.point = point; michael@0: }, michael@0: pointRadius: function(_) { michael@0: pointRadius = _; michael@0: return stream; michael@0: }, michael@0: result: d3_noop michael@0: }; michael@0: function point(x, y) { michael@0: context.moveTo(x, y); michael@0: context.arc(x, y, pointRadius, 0, τ); michael@0: } michael@0: function pointLineStart(x, y) { michael@0: context.moveTo(x, y); michael@0: stream.point = pointLine; michael@0: } michael@0: function pointLine(x, y) { michael@0: context.lineTo(x, y); michael@0: } michael@0: function lineEnd() { michael@0: stream.point = point; michael@0: } michael@0: function lineEndPolygon() { michael@0: context.closePath(); michael@0: } michael@0: return stream; michael@0: } michael@0: function d3_geo_resample(project) { michael@0: var δ2 = .5, cosMinDistance = Math.cos(30 * d3_radians), maxDepth = 16; michael@0: function resample(stream) { michael@0: return (maxDepth ? resampleRecursive : resampleNone)(stream); michael@0: } michael@0: function resampleNone(stream) { michael@0: return d3_geo_transformPoint(stream, function(x, y) { michael@0: x = project(x, y); michael@0: stream.point(x[0], x[1]); michael@0: }); michael@0: } michael@0: function resampleRecursive(stream) { michael@0: var λ00, φ00, x00, y00, a00, b00, c00, λ0, x0, y0, a0, b0, c0; michael@0: var resample = { michael@0: point: point, michael@0: lineStart: lineStart, michael@0: lineEnd: lineEnd, michael@0: polygonStart: function() { michael@0: stream.polygonStart(); michael@0: resample.lineStart = ringStart; michael@0: }, michael@0: polygonEnd: function() { michael@0: stream.polygonEnd(); michael@0: resample.lineStart = lineStart; michael@0: } michael@0: }; michael@0: function point(x, y) { michael@0: x = project(x, y); michael@0: stream.point(x[0], x[1]); michael@0: } michael@0: function lineStart() { michael@0: x0 = NaN; michael@0: resample.point = linePoint; michael@0: stream.lineStart(); michael@0: } michael@0: function linePoint(λ, φ) { michael@0: var c = d3_geo_cartesian([ λ, φ ]), p = project(λ, φ); michael@0: 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); michael@0: stream.point(x0, y0); michael@0: } michael@0: function lineEnd() { michael@0: resample.point = point; michael@0: stream.lineEnd(); michael@0: } michael@0: function ringStart() { michael@0: lineStart(); michael@0: resample.point = ringPoint; michael@0: resample.lineEnd = ringEnd; michael@0: } michael@0: function ringPoint(λ, φ) { michael@0: linePoint(λ00 = λ, φ00 = φ), x00 = x0, y00 = y0, a00 = a0, b00 = b0, c00 = c0; michael@0: resample.point = linePoint; michael@0: } michael@0: function ringEnd() { michael@0: resampleLineTo(x0, y0, λ0, a0, b0, c0, x00, y00, λ00, a00, b00, c00, maxDepth, stream); michael@0: resample.lineEnd = lineEnd; michael@0: lineEnd(); michael@0: } michael@0: return resample; michael@0: } michael@0: function resampleLineTo(x0, y0, λ0, a0, b0, c0, x1, y1, λ1, a1, b1, c1, depth, stream) { michael@0: var dx = x1 - x0, dy = y1 - y0, d2 = dx * dx + dy * dy; michael@0: if (d2 > 4 * δ2 && depth--) { michael@0: var a = a0 + a1, b = b0 + b1, c = c0 + c1, m = Math.sqrt(a * a + b * b + c * c), φ2 = Math.asin(c /= m), λ2 = abs(abs(c) - 1) < ε || abs(λ0 - λ1) < ε ? (λ0 + λ1) / 2 : Math.atan2(b, a), p = project(λ2, φ2), x2 = p[0], y2 = p[1], dx2 = x2 - x0, dy2 = y2 - y0, dz = dy * dx2 - dx * dy2; michael@0: if (dz * dz / d2 > δ2 || abs((dx * dx2 + dy * dy2) / d2 - .5) > .3 || a0 * a1 + b0 * b1 + c0 * c1 < cosMinDistance) { michael@0: resampleLineTo(x0, y0, λ0, a0, b0, c0, x2, y2, λ2, a /= m, b /= m, c, depth, stream); michael@0: stream.point(x2, y2); michael@0: resampleLineTo(x2, y2, λ2, a, b, c, x1, y1, λ1, a1, b1, c1, depth, stream); michael@0: } michael@0: } michael@0: } michael@0: resample.precision = function(_) { michael@0: if (!arguments.length) return Math.sqrt(δ2); michael@0: maxDepth = (δ2 = _ * _) > 0 && 16; michael@0: return resample; michael@0: }; michael@0: return resample; michael@0: } michael@0: d3.geo.path = function() { michael@0: var pointRadius = 4.5, projection, context, projectStream, contextStream, cacheStream; michael@0: function path(object) { michael@0: if (object) { michael@0: if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments)); michael@0: if (!cacheStream || !cacheStream.valid) cacheStream = projectStream(contextStream); michael@0: d3.geo.stream(object, cacheStream); michael@0: } michael@0: return contextStream.result(); michael@0: } michael@0: path.area = function(object) { michael@0: d3_geo_pathAreaSum = 0; michael@0: d3.geo.stream(object, projectStream(d3_geo_pathArea)); michael@0: return d3_geo_pathAreaSum; michael@0: }; michael@0: path.centroid = function(object) { michael@0: d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; michael@0: d3.geo.stream(object, projectStream(d3_geo_pathCentroid)); michael@0: return d3_geo_centroidZ2 ? [ d3_geo_centroidX2 / d3_geo_centroidZ2, d3_geo_centroidY2 / d3_geo_centroidZ2 ] : d3_geo_centroidZ1 ? [ d3_geo_centroidX1 / d3_geo_centroidZ1, d3_geo_centroidY1 / d3_geo_centroidZ1 ] : d3_geo_centroidZ0 ? [ d3_geo_centroidX0 / d3_geo_centroidZ0, d3_geo_centroidY0 / d3_geo_centroidZ0 ] : [ NaN, NaN ]; michael@0: }; michael@0: path.bounds = function(object) { michael@0: d3_geo_pathBoundsX1 = d3_geo_pathBoundsY1 = -(d3_geo_pathBoundsX0 = d3_geo_pathBoundsY0 = Infinity); michael@0: d3.geo.stream(object, projectStream(d3_geo_pathBounds)); michael@0: return [ [ d3_geo_pathBoundsX0, d3_geo_pathBoundsY0 ], [ d3_geo_pathBoundsX1, d3_geo_pathBoundsY1 ] ]; michael@0: }; michael@0: path.projection = function(_) { michael@0: if (!arguments.length) return projection; michael@0: projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity; michael@0: return reset(); michael@0: }; michael@0: path.context = function(_) { michael@0: if (!arguments.length) return context; michael@0: contextStream = (context = _) == null ? new d3_geo_pathBuffer() : new d3_geo_pathContext(_); michael@0: if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius); michael@0: return reset(); michael@0: }; michael@0: path.pointRadius = function(_) { michael@0: if (!arguments.length) return pointRadius; michael@0: pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_); michael@0: return path; michael@0: }; michael@0: function reset() { michael@0: cacheStream = null; michael@0: return path; michael@0: } michael@0: return path.projection(d3.geo.albersUsa()).context(null); michael@0: }; michael@0: function d3_geo_pathProjectStream(project) { michael@0: var resample = d3_geo_resample(function(x, y) { michael@0: return project([ x * d3_degrees, y * d3_degrees ]); michael@0: }); michael@0: return function(stream) { michael@0: return d3_geo_projectionRadians(resample(stream)); michael@0: }; michael@0: } michael@0: d3.geo.transform = function(methods) { michael@0: return { michael@0: stream: function(stream) { michael@0: var transform = new d3_geo_transform(stream); michael@0: for (var k in methods) transform[k] = methods[k]; michael@0: return transform; michael@0: } michael@0: }; michael@0: }; michael@0: function d3_geo_transform(stream) { michael@0: this.stream = stream; michael@0: } michael@0: d3_geo_transform.prototype = { michael@0: point: function(x, y) { michael@0: this.stream.point(x, y); michael@0: }, michael@0: sphere: function() { michael@0: this.stream.sphere(); michael@0: }, michael@0: lineStart: function() { michael@0: this.stream.lineStart(); michael@0: }, michael@0: lineEnd: function() { michael@0: this.stream.lineEnd(); michael@0: }, michael@0: polygonStart: function() { michael@0: this.stream.polygonStart(); michael@0: }, michael@0: polygonEnd: function() { michael@0: this.stream.polygonEnd(); michael@0: } michael@0: }; michael@0: function d3_geo_transformPoint(stream, point) { michael@0: return { michael@0: point: point, michael@0: sphere: function() { michael@0: stream.sphere(); michael@0: }, michael@0: lineStart: function() { michael@0: stream.lineStart(); michael@0: }, michael@0: lineEnd: function() { michael@0: stream.lineEnd(); michael@0: }, michael@0: polygonStart: function() { michael@0: stream.polygonStart(); michael@0: }, michael@0: polygonEnd: function() { michael@0: stream.polygonEnd(); michael@0: } michael@0: }; michael@0: } michael@0: d3.geo.projection = d3_geo_projection; michael@0: d3.geo.projectionMutator = d3_geo_projectionMutator; michael@0: function d3_geo_projection(project) { michael@0: return d3_geo_projectionMutator(function() { michael@0: return project; michael@0: })(); michael@0: } michael@0: function d3_geo_projectionMutator(projectAt) { michael@0: var project, rotate, projectRotate, projectResample = d3_geo_resample(function(x, y) { michael@0: x = project(x, y); michael@0: return [ x[0] * k + δx, δy - x[1] * k ]; michael@0: }), k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx, δy, preclip = d3_geo_clipAntimeridian, postclip = d3_identity, clipAngle = null, clipExtent = null, stream; michael@0: function projection(point) { michael@0: point = projectRotate(point[0] * d3_radians, point[1] * d3_radians); michael@0: return [ point[0] * k + δx, δy - point[1] * k ]; michael@0: } michael@0: function invert(point) { michael@0: point = projectRotate.invert((point[0] - δx) / k, (δy - point[1]) / k); michael@0: return point && [ point[0] * d3_degrees, point[1] * d3_degrees ]; michael@0: } michael@0: projection.stream = function(output) { michael@0: if (stream) stream.valid = false; michael@0: stream = d3_geo_projectionRadians(preclip(rotate, projectResample(postclip(output)))); michael@0: stream.valid = true; michael@0: return stream; michael@0: }; michael@0: projection.clipAngle = function(_) { michael@0: if (!arguments.length) return clipAngle; michael@0: preclip = _ == null ? (clipAngle = _, d3_geo_clipAntimeridian) : d3_geo_clipCircle((clipAngle = +_) * d3_radians); michael@0: return invalidate(); michael@0: }; michael@0: projection.clipExtent = function(_) { michael@0: if (!arguments.length) return clipExtent; michael@0: clipExtent = _; michael@0: postclip = _ ? d3_geo_clipExtent(_[0][0], _[0][1], _[1][0], _[1][1]) : d3_identity; michael@0: return invalidate(); michael@0: }; michael@0: projection.scale = function(_) { michael@0: if (!arguments.length) return k; michael@0: k = +_; michael@0: return reset(); michael@0: }; michael@0: projection.translate = function(_) { michael@0: if (!arguments.length) return [ x, y ]; michael@0: x = +_[0]; michael@0: y = +_[1]; michael@0: return reset(); michael@0: }; michael@0: projection.center = function(_) { michael@0: if (!arguments.length) return [ λ * d3_degrees, φ * d3_degrees ]; michael@0: λ = _[0] % 360 * d3_radians; michael@0: φ = _[1] % 360 * d3_radians; michael@0: return reset(); michael@0: }; michael@0: projection.rotate = function(_) { michael@0: if (!arguments.length) return [ δλ * d3_degrees, δφ * d3_degrees, δγ * d3_degrees ]; michael@0: δλ = _[0] % 360 * d3_radians; michael@0: δφ = _[1] % 360 * d3_radians; michael@0: δγ = _.length > 2 ? _[2] % 360 * d3_radians : 0; michael@0: return reset(); michael@0: }; michael@0: d3.rebind(projection, projectResample, "precision"); michael@0: function reset() { michael@0: projectRotate = d3_geo_compose(rotate = d3_geo_rotation(δλ, δφ, δγ), project); michael@0: var center = project(λ, φ); michael@0: δx = x - center[0] * k; michael@0: δy = y + center[1] * k; michael@0: return invalidate(); michael@0: } michael@0: function invalidate() { michael@0: if (stream) stream.valid = false, stream = null; michael@0: return projection; michael@0: } michael@0: return function() { michael@0: project = projectAt.apply(this, arguments); michael@0: projection.invert = project.invert && invert; michael@0: return reset(); michael@0: }; michael@0: } michael@0: function d3_geo_projectionRadians(stream) { michael@0: return d3_geo_transformPoint(stream, function(x, y) { michael@0: stream.point(x * d3_radians, y * d3_radians); michael@0: }); michael@0: } michael@0: function d3_geo_equirectangular(λ, φ) { michael@0: return [ λ, φ ]; michael@0: } michael@0: (d3.geo.equirectangular = function() { michael@0: return d3_geo_projection(d3_geo_equirectangular); michael@0: }).raw = d3_geo_equirectangular.invert = d3_geo_equirectangular; michael@0: d3.geo.rotation = function(rotate) { michael@0: rotate = d3_geo_rotation(rotate[0] % 360 * d3_radians, rotate[1] * d3_radians, rotate.length > 2 ? rotate[2] * d3_radians : 0); michael@0: function forward(coordinates) { michael@0: coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians); michael@0: return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; michael@0: } michael@0: forward.invert = function(coordinates) { michael@0: coordinates = rotate.invert(coordinates[0] * d3_radians, coordinates[1] * d3_radians); michael@0: return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; michael@0: }; michael@0: return forward; michael@0: }; michael@0: function d3_geo_identityRotation(λ, φ) { michael@0: return [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; michael@0: } michael@0: d3_geo_identityRotation.invert = d3_geo_equirectangular; michael@0: function d3_geo_rotation(δλ, δφ, δγ) { michael@0: return δλ ? δφ || δγ ? d3_geo_compose(d3_geo_rotationλ(δλ), d3_geo_rotationφγ(δφ, δγ)) : d3_geo_rotationλ(δλ) : δφ || δγ ? d3_geo_rotationφγ(δφ, δγ) : d3_geo_identityRotation; michael@0: } michael@0: function d3_geo_forwardRotationλ(δλ) { michael@0: return function(λ, φ) { michael@0: return λ += δλ, [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; michael@0: }; michael@0: } michael@0: function d3_geo_rotationλ(δλ) { michael@0: var rotation = d3_geo_forwardRotationλ(δλ); michael@0: rotation.invert = d3_geo_forwardRotationλ(-δλ); michael@0: return rotation; michael@0: } michael@0: function d3_geo_rotationφγ(δφ, δγ) { michael@0: var cosδφ = Math.cos(δφ), sinδφ = Math.sin(δφ), cosδγ = Math.cos(δγ), sinδγ = Math.sin(δγ); michael@0: function rotation(λ, φ) { michael@0: var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδφ + x * sinδφ; michael@0: return [ Math.atan2(y * cosδγ - k * sinδγ, x * cosδφ - z * sinδφ), d3_asin(k * cosδγ + y * sinδγ) ]; michael@0: } michael@0: rotation.invert = function(λ, φ) { michael@0: var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδγ - y * sinδγ; michael@0: return [ Math.atan2(y * cosδγ + z * sinδγ, x * cosδφ + k * sinδφ), d3_asin(k * cosδφ - x * sinδφ) ]; michael@0: }; michael@0: return rotation; michael@0: } michael@0: d3.geo.circle = function() { michael@0: var origin = [ 0, 0 ], angle, precision = 6, interpolate; michael@0: function circle() { michael@0: var center = typeof origin === "function" ? origin.apply(this, arguments) : origin, rotate = d3_geo_rotation(-center[0] * d3_radians, -center[1] * d3_radians, 0).invert, ring = []; michael@0: interpolate(null, null, 1, { michael@0: point: function(x, y) { michael@0: ring.push(x = rotate(x, y)); michael@0: x[0] *= d3_degrees, x[1] *= d3_degrees; michael@0: } michael@0: }); michael@0: return { michael@0: type: "Polygon", michael@0: coordinates: [ ring ] michael@0: }; michael@0: } michael@0: circle.origin = function(x) { michael@0: if (!arguments.length) return origin; michael@0: origin = x; michael@0: return circle; michael@0: }; michael@0: circle.angle = function(x) { michael@0: if (!arguments.length) return angle; michael@0: interpolate = d3_geo_circleInterpolate((angle = +x) * d3_radians, precision * d3_radians); michael@0: return circle; michael@0: }; michael@0: circle.precision = function(_) { michael@0: if (!arguments.length) return precision; michael@0: interpolate = d3_geo_circleInterpolate(angle * d3_radians, (precision = +_) * d3_radians); michael@0: return circle; michael@0: }; michael@0: return circle.angle(90); michael@0: }; michael@0: function d3_geo_circleInterpolate(radius, precision) { michael@0: var cr = Math.cos(radius), sr = Math.sin(radius); michael@0: return function(from, to, direction, listener) { michael@0: var step = direction * precision; michael@0: if (from != null) { michael@0: from = d3_geo_circleAngle(cr, from); michael@0: to = d3_geo_circleAngle(cr, to); michael@0: if (direction > 0 ? from < to : from > to) from += direction * τ; michael@0: } else { michael@0: from = radius + direction * τ; michael@0: to = radius - .5 * step; michael@0: } michael@0: for (var point, t = from; direction > 0 ? t > to : t < to; t -= step) { michael@0: listener.point((point = d3_geo_spherical([ cr, -sr * Math.cos(t), -sr * Math.sin(t) ]))[0], point[1]); michael@0: } michael@0: }; michael@0: } michael@0: function d3_geo_circleAngle(cr, point) { michael@0: var a = d3_geo_cartesian(point); michael@0: a[0] -= cr; michael@0: d3_geo_cartesianNormalize(a); michael@0: var angle = d3_acos(-a[1]); michael@0: return ((-a[2] < 0 ? -angle : angle) + 2 * Math.PI - ε) % (2 * Math.PI); michael@0: } michael@0: d3.geo.distance = function(a, b) { michael@0: var Δλ = (b[0] - a[0]) * d3_radians, φ0 = a[1] * d3_radians, φ1 = b[1] * d3_radians, sinΔλ = Math.sin(Δλ), cosΔλ = Math.cos(Δλ), sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), sinφ1 = Math.sin(φ1), cosφ1 = Math.cos(φ1), t; michael@0: return Math.atan2(Math.sqrt((t = cosφ1 * sinΔλ) * t + (t = cosφ0 * sinφ1 - sinφ0 * cosφ1 * cosΔλ) * t), sinφ0 * sinφ1 + cosφ0 * cosφ1 * cosΔλ); michael@0: }; michael@0: d3.geo.graticule = function() { michael@0: var x1, x0, X1, X0, y1, y0, Y1, Y0, dx = 10, dy = dx, DX = 90, DY = 360, x, y, X, Y, precision = 2.5; michael@0: function graticule() { michael@0: return { michael@0: type: "MultiLineString", michael@0: coordinates: lines() michael@0: }; michael@0: } michael@0: function lines() { michael@0: return d3.range(Math.ceil(X0 / DX) * DX, X1, DX).map(X).concat(d3.range(Math.ceil(Y0 / DY) * DY, Y1, DY).map(Y)).concat(d3.range(Math.ceil(x0 / dx) * dx, x1, dx).filter(function(x) { michael@0: return abs(x % DX) > ε; michael@0: }).map(x)).concat(d3.range(Math.ceil(y0 / dy) * dy, y1, dy).filter(function(y) { michael@0: return abs(y % DY) > ε; michael@0: }).map(y)); michael@0: } michael@0: graticule.lines = function() { michael@0: return lines().map(function(coordinates) { michael@0: return { michael@0: type: "LineString", michael@0: coordinates: coordinates michael@0: }; michael@0: }); michael@0: }; michael@0: graticule.outline = function() { michael@0: return { michael@0: type: "Polygon", michael@0: coordinates: [ X(X0).concat(Y(Y1).slice(1), X(X1).reverse().slice(1), Y(Y0).reverse().slice(1)) ] michael@0: }; michael@0: }; michael@0: graticule.extent = function(_) { michael@0: if (!arguments.length) return graticule.minorExtent(); michael@0: return graticule.majorExtent(_).minorExtent(_); michael@0: }; michael@0: graticule.majorExtent = function(_) { michael@0: if (!arguments.length) return [ [ X0, Y0 ], [ X1, Y1 ] ]; michael@0: X0 = +_[0][0], X1 = +_[1][0]; michael@0: Y0 = +_[0][1], Y1 = +_[1][1]; michael@0: if (X0 > X1) _ = X0, X0 = X1, X1 = _; michael@0: if (Y0 > Y1) _ = Y0, Y0 = Y1, Y1 = _; michael@0: return graticule.precision(precision); michael@0: }; michael@0: graticule.minorExtent = function(_) { michael@0: if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; michael@0: x0 = +_[0][0], x1 = +_[1][0]; michael@0: y0 = +_[0][1], y1 = +_[1][1]; michael@0: if (x0 > x1) _ = x0, x0 = x1, x1 = _; michael@0: if (y0 > y1) _ = y0, y0 = y1, y1 = _; michael@0: return graticule.precision(precision); michael@0: }; michael@0: graticule.step = function(_) { michael@0: if (!arguments.length) return graticule.minorStep(); michael@0: return graticule.majorStep(_).minorStep(_); michael@0: }; michael@0: graticule.majorStep = function(_) { michael@0: if (!arguments.length) return [ DX, DY ]; michael@0: DX = +_[0], DY = +_[1]; michael@0: return graticule; michael@0: }; michael@0: graticule.minorStep = function(_) { michael@0: if (!arguments.length) return [ dx, dy ]; michael@0: dx = +_[0], dy = +_[1]; michael@0: return graticule; michael@0: }; michael@0: graticule.precision = function(_) { michael@0: if (!arguments.length) return precision; michael@0: precision = +_; michael@0: x = d3_geo_graticuleX(y0, y1, 90); michael@0: y = d3_geo_graticuleY(x0, x1, precision); michael@0: X = d3_geo_graticuleX(Y0, Y1, 90); michael@0: Y = d3_geo_graticuleY(X0, X1, precision); michael@0: return graticule; michael@0: }; michael@0: return graticule.majorExtent([ [ -180, -90 + ε ], [ 180, 90 - ε ] ]).minorExtent([ [ -180, -80 - ε ], [ 180, 80 + ε ] ]); michael@0: }; michael@0: function d3_geo_graticuleX(y0, y1, dy) { michael@0: var y = d3.range(y0, y1 - ε, dy).concat(y1); michael@0: return function(x) { michael@0: return y.map(function(y) { michael@0: return [ x, y ]; michael@0: }); michael@0: }; michael@0: } michael@0: function d3_geo_graticuleY(x0, x1, dx) { michael@0: var x = d3.range(x0, x1 - ε, dx).concat(x1); michael@0: return function(y) { michael@0: return x.map(function(x) { michael@0: return [ x, y ]; michael@0: }); michael@0: }; michael@0: } michael@0: function d3_source(d) { michael@0: return d.source; michael@0: } michael@0: function d3_target(d) { michael@0: return d.target; michael@0: } michael@0: d3.geo.greatArc = function() { michael@0: var source = d3_source, source_, target = d3_target, target_; michael@0: function greatArc() { michael@0: return { michael@0: type: "LineString", michael@0: coordinates: [ source_ || source.apply(this, arguments), target_ || target.apply(this, arguments) ] michael@0: }; michael@0: } michael@0: greatArc.distance = function() { michael@0: return d3.geo.distance(source_ || source.apply(this, arguments), target_ || target.apply(this, arguments)); michael@0: }; michael@0: greatArc.source = function(_) { michael@0: if (!arguments.length) return source; michael@0: source = _, source_ = typeof _ === "function" ? null : _; michael@0: return greatArc; michael@0: }; michael@0: greatArc.target = function(_) { michael@0: if (!arguments.length) return target; michael@0: target = _, target_ = typeof _ === "function" ? null : _; michael@0: return greatArc; michael@0: }; michael@0: greatArc.precision = function() { michael@0: return arguments.length ? greatArc : 0; michael@0: }; michael@0: return greatArc; michael@0: }; michael@0: d3.geo.interpolate = function(source, target) { michael@0: return d3_geo_interpolate(source[0] * d3_radians, source[1] * d3_radians, target[0] * d3_radians, target[1] * d3_radians); michael@0: }; michael@0: function d3_geo_interpolate(x0, y0, x1, y1) { michael@0: var cy0 = Math.cos(y0), sy0 = Math.sin(y0), cy1 = Math.cos(y1), sy1 = Math.sin(y1), kx0 = cy0 * Math.cos(x0), ky0 = cy0 * Math.sin(x0), kx1 = cy1 * Math.cos(x1), ky1 = cy1 * Math.sin(x1), d = 2 * Math.asin(Math.sqrt(d3_haversin(y1 - y0) + cy0 * cy1 * d3_haversin(x1 - x0))), k = 1 / Math.sin(d); michael@0: var interpolate = d ? function(t) { michael@0: var B = Math.sin(t *= d) * k, A = Math.sin(d - t) * k, x = A * kx0 + B * kx1, y = A * ky0 + B * ky1, z = A * sy0 + B * sy1; michael@0: return [ Math.atan2(y, x) * d3_degrees, Math.atan2(z, Math.sqrt(x * x + y * y)) * d3_degrees ]; michael@0: } : function() { michael@0: return [ x0 * d3_degrees, y0 * d3_degrees ]; michael@0: }; michael@0: interpolate.distance = d; michael@0: return interpolate; michael@0: } michael@0: d3.geo.length = function(object) { michael@0: d3_geo_lengthSum = 0; michael@0: d3.geo.stream(object, d3_geo_length); michael@0: return d3_geo_lengthSum; michael@0: }; michael@0: var d3_geo_lengthSum; michael@0: var d3_geo_length = { michael@0: sphere: d3_noop, michael@0: point: d3_noop, michael@0: lineStart: d3_geo_lengthLineStart, michael@0: lineEnd: d3_noop, michael@0: polygonStart: d3_noop, michael@0: polygonEnd: d3_noop michael@0: }; michael@0: function d3_geo_lengthLineStart() { michael@0: var λ0, sinφ0, cosφ0; michael@0: d3_geo_length.point = function(λ, φ) { michael@0: λ0 = λ * d3_radians, sinφ0 = Math.sin(φ *= d3_radians), cosφ0 = Math.cos(φ); michael@0: d3_geo_length.point = nextPoint; michael@0: }; michael@0: d3_geo_length.lineEnd = function() { michael@0: d3_geo_length.point = d3_geo_length.lineEnd = d3_noop; michael@0: }; michael@0: function nextPoint(λ, φ) { michael@0: var sinφ = Math.sin(φ *= d3_radians), cosφ = Math.cos(φ), t = abs((λ *= d3_radians) - λ0), cosΔλ = Math.cos(t); michael@0: d3_geo_lengthSum += Math.atan2(Math.sqrt((t = cosφ * Math.sin(t)) * t + (t = cosφ0 * sinφ - sinφ0 * cosφ * cosΔλ) * t), sinφ0 * sinφ + cosφ0 * cosφ * cosΔλ); michael@0: λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ; michael@0: } michael@0: } michael@0: function d3_geo_azimuthal(scale, angle) { michael@0: function azimuthal(λ, φ) { michael@0: var cosλ = Math.cos(λ), cosφ = Math.cos(φ), k = scale(cosλ * cosφ); michael@0: return [ k * cosφ * Math.sin(λ), k * Math.sin(φ) ]; michael@0: } michael@0: azimuthal.invert = function(x, y) { michael@0: var ρ = Math.sqrt(x * x + y * y), c = angle(ρ), sinc = Math.sin(c), cosc = Math.cos(c); michael@0: return [ Math.atan2(x * sinc, ρ * cosc), Math.asin(ρ && y * sinc / ρ) ]; michael@0: }; michael@0: return azimuthal; michael@0: } michael@0: var d3_geo_azimuthalEqualArea = d3_geo_azimuthal(function(cosλcosφ) { michael@0: return Math.sqrt(2 / (1 + cosλcosφ)); michael@0: }, function(ρ) { michael@0: return 2 * Math.asin(ρ / 2); michael@0: }); michael@0: (d3.geo.azimuthalEqualArea = function() { michael@0: return d3_geo_projection(d3_geo_azimuthalEqualArea); michael@0: }).raw = d3_geo_azimuthalEqualArea; michael@0: var d3_geo_azimuthalEquidistant = d3_geo_azimuthal(function(cosλcosφ) { michael@0: var c = Math.acos(cosλcosφ); michael@0: return c && c / Math.sin(c); michael@0: }, d3_identity); michael@0: (d3.geo.azimuthalEquidistant = function() { michael@0: return d3_geo_projection(d3_geo_azimuthalEquidistant); michael@0: }).raw = d3_geo_azimuthalEquidistant; michael@0: function d3_geo_conicConformal(φ0, φ1) { michael@0: var cosφ0 = Math.cos(φ0), t = function(φ) { michael@0: return Math.tan(π / 4 + φ / 2); michael@0: }, n = φ0 === φ1 ? Math.sin(φ0) : Math.log(cosφ0 / Math.cos(φ1)) / Math.log(t(φ1) / t(φ0)), F = cosφ0 * Math.pow(t(φ0), n) / n; michael@0: if (!n) return d3_geo_mercator; michael@0: function forward(λ, φ) { michael@0: var ρ = abs(abs(φ) - halfπ) < ε ? 0 : F / Math.pow(t(φ), n); michael@0: return [ ρ * Math.sin(n * λ), F - ρ * Math.cos(n * λ) ]; michael@0: } michael@0: forward.invert = function(x, y) { michael@0: var ρ0_y = F - y, ρ = d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y); michael@0: return [ Math.atan2(x, ρ0_y) / n, 2 * Math.atan(Math.pow(F / ρ, 1 / n)) - halfπ ]; michael@0: }; michael@0: return forward; michael@0: } michael@0: (d3.geo.conicConformal = function() { michael@0: return d3_geo_conic(d3_geo_conicConformal); michael@0: }).raw = d3_geo_conicConformal; michael@0: function d3_geo_conicEquidistant(φ0, φ1) { michael@0: var cosφ0 = Math.cos(φ0), n = φ0 === φ1 ? Math.sin(φ0) : (cosφ0 - Math.cos(φ1)) / (φ1 - φ0), G = cosφ0 / n + φ0; michael@0: if (abs(n) < ε) return d3_geo_equirectangular; michael@0: function forward(λ, φ) { michael@0: var ρ = G - φ; michael@0: return [ ρ * Math.sin(n * λ), G - ρ * Math.cos(n * λ) ]; michael@0: } michael@0: forward.invert = function(x, y) { michael@0: var ρ0_y = G - y; michael@0: return [ Math.atan2(x, ρ0_y) / n, G - d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y) ]; michael@0: }; michael@0: return forward; michael@0: } michael@0: (d3.geo.conicEquidistant = function() { michael@0: return d3_geo_conic(d3_geo_conicEquidistant); michael@0: }).raw = d3_geo_conicEquidistant; michael@0: var d3_geo_gnomonic = d3_geo_azimuthal(function(cosλcosφ) { michael@0: return 1 / cosλcosφ; michael@0: }, Math.atan); michael@0: (d3.geo.gnomonic = function() { michael@0: return d3_geo_projection(d3_geo_gnomonic); michael@0: }).raw = d3_geo_gnomonic; michael@0: function d3_geo_mercator(λ, φ) { michael@0: return [ λ, Math.log(Math.tan(π / 4 + φ / 2)) ]; michael@0: } michael@0: d3_geo_mercator.invert = function(x, y) { michael@0: return [ x, 2 * Math.atan(Math.exp(y)) - halfπ ]; michael@0: }; michael@0: function d3_geo_mercatorProjection(project) { michael@0: var m = d3_geo_projection(project), scale = m.scale, translate = m.translate, clipExtent = m.clipExtent, clipAuto; michael@0: m.scale = function() { michael@0: var v = scale.apply(m, arguments); michael@0: return v === m ? clipAuto ? m.clipExtent(null) : m : v; michael@0: }; michael@0: m.translate = function() { michael@0: var v = translate.apply(m, arguments); michael@0: return v === m ? clipAuto ? m.clipExtent(null) : m : v; michael@0: }; michael@0: m.clipExtent = function(_) { michael@0: var v = clipExtent.apply(m, arguments); michael@0: if (v === m) { michael@0: if (clipAuto = _ == null) { michael@0: var k = π * scale(), t = translate(); michael@0: clipExtent([ [ t[0] - k, t[1] - k ], [ t[0] + k, t[1] + k ] ]); michael@0: } michael@0: } else if (clipAuto) { michael@0: v = null; michael@0: } michael@0: return v; michael@0: }; michael@0: return m.clipExtent(null); michael@0: } michael@0: (d3.geo.mercator = function() { michael@0: return d3_geo_mercatorProjection(d3_geo_mercator); michael@0: }).raw = d3_geo_mercator; michael@0: var d3_geo_orthographic = d3_geo_azimuthal(function() { michael@0: return 1; michael@0: }, Math.asin); michael@0: (d3.geo.orthographic = function() { michael@0: return d3_geo_projection(d3_geo_orthographic); michael@0: }).raw = d3_geo_orthographic; michael@0: var d3_geo_stereographic = d3_geo_azimuthal(function(cosλcosφ) { michael@0: return 1 / (1 + cosλcosφ); michael@0: }, function(ρ) { michael@0: return 2 * Math.atan(ρ); michael@0: }); michael@0: (d3.geo.stereographic = function() { michael@0: return d3_geo_projection(d3_geo_stereographic); michael@0: }).raw = d3_geo_stereographic; michael@0: function d3_geo_transverseMercator(λ, φ) { michael@0: return [ Math.log(Math.tan(π / 4 + φ / 2)), -λ ]; michael@0: } michael@0: d3_geo_transverseMercator.invert = function(x, y) { michael@0: return [ -y, 2 * Math.atan(Math.exp(x)) - halfπ ]; michael@0: }; michael@0: (d3.geo.transverseMercator = function() { michael@0: var projection = d3_geo_mercatorProjection(d3_geo_transverseMercator), center = projection.center, rotate = projection.rotate; michael@0: projection.center = function(_) { michael@0: return _ ? center([ -_[1], _[0] ]) : (_ = center(), [ -_[1], _[0] ]); michael@0: }; michael@0: projection.rotate = function(_) { michael@0: return _ ? rotate([ _[0], _[1], _.length > 2 ? _[2] + 90 : 90 ]) : (_ = rotate(), michael@0: [ _[0], _[1], _[2] - 90 ]); michael@0: }; michael@0: return projection.rotate([ 0, 0 ]); michael@0: }).raw = d3_geo_transverseMercator; michael@0: d3.geom = {}; michael@0: function d3_geom_pointX(d) { michael@0: return d[0]; michael@0: } michael@0: function d3_geom_pointY(d) { michael@0: return d[1]; michael@0: } michael@0: d3.geom.hull = function(vertices) { michael@0: var x = d3_geom_pointX, y = d3_geom_pointY; michael@0: if (arguments.length) return hull(vertices); michael@0: function hull(data) { michael@0: if (data.length < 3) return []; michael@0: var fx = d3_functor(x), fy = d3_functor(y), i, n = data.length, points = [], flippedPoints = []; michael@0: for (i = 0; i < n; i++) { michael@0: points.push([ +fx.call(this, data[i], i), +fy.call(this, data[i], i), i ]); michael@0: } michael@0: points.sort(d3_geom_hullOrder); michael@0: for (i = 0; i < n; i++) flippedPoints.push([ points[i][0], -points[i][1] ]); michael@0: var upper = d3_geom_hullUpper(points), lower = d3_geom_hullUpper(flippedPoints); michael@0: var skipLeft = lower[0] === upper[0], skipRight = lower[lower.length - 1] === upper[upper.length - 1], polygon = []; michael@0: for (i = upper.length - 1; i >= 0; --i) polygon.push(data[points[upper[i]][2]]); michael@0: for (i = +skipLeft; i < lower.length - skipRight; ++i) polygon.push(data[points[lower[i]][2]]); michael@0: return polygon; michael@0: } michael@0: hull.x = function(_) { michael@0: return arguments.length ? (x = _, hull) : x; michael@0: }; michael@0: hull.y = function(_) { michael@0: return arguments.length ? (y = _, hull) : y; michael@0: }; michael@0: return hull; michael@0: }; michael@0: function d3_geom_hullUpper(points) { michael@0: var n = points.length, hull = [ 0, 1 ], hs = 2; michael@0: for (var i = 2; i < n; i++) { michael@0: while (hs > 1 && d3_cross2d(points[hull[hs - 2]], points[hull[hs - 1]], points[i]) <= 0) --hs; michael@0: hull[hs++] = i; michael@0: } michael@0: return hull.slice(0, hs); michael@0: } michael@0: function d3_geom_hullOrder(a, b) { michael@0: return a[0] - b[0] || a[1] - b[1]; michael@0: } michael@0: d3.geom.polygon = function(coordinates) { michael@0: d3_subclass(coordinates, d3_geom_polygonPrototype); michael@0: return coordinates; michael@0: }; michael@0: var d3_geom_polygonPrototype = d3.geom.polygon.prototype = []; michael@0: d3_geom_polygonPrototype.area = function() { michael@0: var i = -1, n = this.length, a, b = this[n - 1], area = 0; michael@0: while (++i < n) { michael@0: a = b; michael@0: b = this[i]; michael@0: area += a[1] * b[0] - a[0] * b[1]; michael@0: } michael@0: return area * .5; michael@0: }; michael@0: d3_geom_polygonPrototype.centroid = function(k) { michael@0: var i = -1, n = this.length, x = 0, y = 0, a, b = this[n - 1], c; michael@0: if (!arguments.length) k = -1 / (6 * this.area()); michael@0: while (++i < n) { michael@0: a = b; michael@0: b = this[i]; michael@0: c = a[0] * b[1] - b[0] * a[1]; michael@0: x += (a[0] + b[0]) * c; michael@0: y += (a[1] + b[1]) * c; michael@0: } michael@0: return [ x * k, y * k ]; michael@0: }; michael@0: d3_geom_polygonPrototype.clip = function(subject) { michael@0: var input, closed = d3_geom_polygonClosed(subject), i = -1, n = this.length - d3_geom_polygonClosed(this), j, m, a = this[n - 1], b, c, d; michael@0: while (++i < n) { michael@0: input = subject.slice(); michael@0: subject.length = 0; michael@0: b = this[i]; michael@0: c = input[(m = input.length - closed) - 1]; michael@0: j = -1; michael@0: while (++j < m) { michael@0: d = input[j]; michael@0: if (d3_geom_polygonInside(d, a, b)) { michael@0: if (!d3_geom_polygonInside(c, a, b)) { michael@0: subject.push(d3_geom_polygonIntersect(c, d, a, b)); michael@0: } michael@0: subject.push(d); michael@0: } else if (d3_geom_polygonInside(c, a, b)) { michael@0: subject.push(d3_geom_polygonIntersect(c, d, a, b)); michael@0: } michael@0: c = d; michael@0: } michael@0: if (closed) subject.push(subject[0]); michael@0: a = b; michael@0: } michael@0: return subject; michael@0: }; michael@0: function d3_geom_polygonInside(p, a, b) { michael@0: return (b[0] - a[0]) * (p[1] - a[1]) < (b[1] - a[1]) * (p[0] - a[0]); michael@0: } michael@0: function d3_geom_polygonIntersect(c, d, a, b) { michael@0: var x1 = c[0], x3 = a[0], x21 = d[0] - x1, x43 = b[0] - x3, y1 = c[1], y3 = a[1], y21 = d[1] - y1, y43 = b[1] - y3, ua = (x43 * (y1 - y3) - y43 * (x1 - x3)) / (y43 * x21 - x43 * y21); michael@0: return [ x1 + ua * x21, y1 + ua * y21 ]; michael@0: } michael@0: function d3_geom_polygonClosed(coordinates) { michael@0: var a = coordinates[0], b = coordinates[coordinates.length - 1]; michael@0: return !(a[0] - b[0] || a[1] - b[1]); michael@0: } michael@0: var d3_geom_voronoiEdges, d3_geom_voronoiCells, d3_geom_voronoiBeaches, d3_geom_voronoiBeachPool = [], d3_geom_voronoiFirstCircle, d3_geom_voronoiCircles, d3_geom_voronoiCirclePool = []; michael@0: function d3_geom_voronoiBeach() { michael@0: d3_geom_voronoiRedBlackNode(this); michael@0: this.edge = this.site = this.circle = null; michael@0: } michael@0: function d3_geom_voronoiCreateBeach(site) { michael@0: var beach = d3_geom_voronoiBeachPool.pop() || new d3_geom_voronoiBeach(); michael@0: beach.site = site; michael@0: return beach; michael@0: } michael@0: function d3_geom_voronoiDetachBeach(beach) { michael@0: d3_geom_voronoiDetachCircle(beach); michael@0: d3_geom_voronoiBeaches.remove(beach); michael@0: d3_geom_voronoiBeachPool.push(beach); michael@0: d3_geom_voronoiRedBlackNode(beach); michael@0: } michael@0: function d3_geom_voronoiRemoveBeach(beach) { michael@0: var circle = beach.circle, x = circle.x, y = circle.cy, vertex = { michael@0: x: x, michael@0: y: y michael@0: }, previous = beach.P, next = beach.N, disappearing = [ beach ]; michael@0: d3_geom_voronoiDetachBeach(beach); michael@0: var lArc = previous; michael@0: while (lArc.circle && abs(x - lArc.circle.x) < ε && abs(y - lArc.circle.cy) < ε) { michael@0: previous = lArc.P; michael@0: disappearing.unshift(lArc); michael@0: d3_geom_voronoiDetachBeach(lArc); michael@0: lArc = previous; michael@0: } michael@0: disappearing.unshift(lArc); michael@0: d3_geom_voronoiDetachCircle(lArc); michael@0: var rArc = next; michael@0: while (rArc.circle && abs(x - rArc.circle.x) < ε && abs(y - rArc.circle.cy) < ε) { michael@0: next = rArc.N; michael@0: disappearing.push(rArc); michael@0: d3_geom_voronoiDetachBeach(rArc); michael@0: rArc = next; michael@0: } michael@0: disappearing.push(rArc); michael@0: d3_geom_voronoiDetachCircle(rArc); michael@0: var nArcs = disappearing.length, iArc; michael@0: for (iArc = 1; iArc < nArcs; ++iArc) { michael@0: rArc = disappearing[iArc]; michael@0: lArc = disappearing[iArc - 1]; michael@0: d3_geom_voronoiSetEdgeEnd(rArc.edge, lArc.site, rArc.site, vertex); michael@0: } michael@0: lArc = disappearing[0]; michael@0: rArc = disappearing[nArcs - 1]; michael@0: rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, rArc.site, null, vertex); michael@0: d3_geom_voronoiAttachCircle(lArc); michael@0: d3_geom_voronoiAttachCircle(rArc); michael@0: } michael@0: function d3_geom_voronoiAddBeach(site) { michael@0: var x = site.x, directrix = site.y, lArc, rArc, dxl, dxr, node = d3_geom_voronoiBeaches._; michael@0: while (node) { michael@0: dxl = d3_geom_voronoiLeftBreakPoint(node, directrix) - x; michael@0: if (dxl > ε) node = node.L; else { michael@0: dxr = x - d3_geom_voronoiRightBreakPoint(node, directrix); michael@0: if (dxr > ε) { michael@0: if (!node.R) { michael@0: lArc = node; michael@0: break; michael@0: } michael@0: node = node.R; michael@0: } else { michael@0: if (dxl > -ε) { michael@0: lArc = node.P; michael@0: rArc = node; michael@0: } else if (dxr > -ε) { michael@0: lArc = node; michael@0: rArc = node.N; michael@0: } else { michael@0: lArc = rArc = node; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: var newArc = d3_geom_voronoiCreateBeach(site); michael@0: d3_geom_voronoiBeaches.insert(lArc, newArc); michael@0: if (!lArc && !rArc) return; michael@0: if (lArc === rArc) { michael@0: d3_geom_voronoiDetachCircle(lArc); michael@0: rArc = d3_geom_voronoiCreateBeach(lArc.site); michael@0: d3_geom_voronoiBeaches.insert(newArc, rArc); michael@0: newArc.edge = rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); michael@0: d3_geom_voronoiAttachCircle(lArc); michael@0: d3_geom_voronoiAttachCircle(rArc); michael@0: return; michael@0: } michael@0: if (!rArc) { michael@0: newArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); michael@0: return; michael@0: } michael@0: d3_geom_voronoiDetachCircle(lArc); michael@0: d3_geom_voronoiDetachCircle(rArc); michael@0: var lSite = lArc.site, ax = lSite.x, ay = lSite.y, bx = site.x - ax, by = site.y - ay, rSite = rArc.site, cx = rSite.x - ax, cy = rSite.y - ay, d = 2 * (bx * cy - by * cx), hb = bx * bx + by * by, hc = cx * cx + cy * cy, vertex = { michael@0: x: (cy * hb - by * hc) / d + ax, michael@0: y: (bx * hc - cx * hb) / d + ay michael@0: }; michael@0: d3_geom_voronoiSetEdgeEnd(rArc.edge, lSite, rSite, vertex); michael@0: newArc.edge = d3_geom_voronoiCreateEdge(lSite, site, null, vertex); michael@0: rArc.edge = d3_geom_voronoiCreateEdge(site, rSite, null, vertex); michael@0: d3_geom_voronoiAttachCircle(lArc); michael@0: d3_geom_voronoiAttachCircle(rArc); michael@0: } michael@0: function d3_geom_voronoiLeftBreakPoint(arc, directrix) { michael@0: var site = arc.site, rfocx = site.x, rfocy = site.y, pby2 = rfocy - directrix; michael@0: if (!pby2) return rfocx; michael@0: var lArc = arc.P; michael@0: if (!lArc) return -Infinity; michael@0: site = lArc.site; michael@0: var lfocx = site.x, lfocy = site.y, plby2 = lfocy - directrix; michael@0: if (!plby2) return lfocx; michael@0: var hl = lfocx - rfocx, aby2 = 1 / pby2 - 1 / plby2, b = hl / plby2; michael@0: if (aby2) return (-b + Math.sqrt(b * b - 2 * aby2 * (hl * hl / (-2 * plby2) - lfocy + plby2 / 2 + rfocy - pby2 / 2))) / aby2 + rfocx; michael@0: return (rfocx + lfocx) / 2; michael@0: } michael@0: function d3_geom_voronoiRightBreakPoint(arc, directrix) { michael@0: var rArc = arc.N; michael@0: if (rArc) return d3_geom_voronoiLeftBreakPoint(rArc, directrix); michael@0: var site = arc.site; michael@0: return site.y === directrix ? site.x : Infinity; michael@0: } michael@0: function d3_geom_voronoiCell(site) { michael@0: this.site = site; michael@0: this.edges = []; michael@0: } michael@0: d3_geom_voronoiCell.prototype.prepare = function() { michael@0: var halfEdges = this.edges, iHalfEdge = halfEdges.length, edge; michael@0: while (iHalfEdge--) { michael@0: edge = halfEdges[iHalfEdge].edge; michael@0: if (!edge.b || !edge.a) halfEdges.splice(iHalfEdge, 1); michael@0: } michael@0: halfEdges.sort(d3_geom_voronoiHalfEdgeOrder); michael@0: return halfEdges.length; michael@0: }; michael@0: function d3_geom_voronoiCloseCells(extent) { michael@0: var x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], x2, y2, x3, y3, cells = d3_geom_voronoiCells, iCell = cells.length, cell, iHalfEdge, halfEdges, nHalfEdges, start, end; michael@0: while (iCell--) { michael@0: cell = cells[iCell]; michael@0: if (!cell || !cell.prepare()) continue; michael@0: halfEdges = cell.edges; michael@0: nHalfEdges = halfEdges.length; michael@0: iHalfEdge = 0; michael@0: while (iHalfEdge < nHalfEdges) { michael@0: end = halfEdges[iHalfEdge].end(), x3 = end.x, y3 = end.y; michael@0: start = halfEdges[++iHalfEdge % nHalfEdges].start(), x2 = start.x, y2 = start.y; michael@0: if (abs(x3 - x2) > ε || abs(y3 - y2) > ε) { michael@0: halfEdges.splice(iHalfEdge, 0, new d3_geom_voronoiHalfEdge(d3_geom_voronoiCreateBorderEdge(cell.site, end, abs(x3 - x0) < ε && y1 - y3 > ε ? { michael@0: x: x0, michael@0: y: abs(x2 - x0) < ε ? y2 : y1 michael@0: } : abs(y3 - y1) < ε && x1 - x3 > ε ? { michael@0: x: abs(y2 - y1) < ε ? x2 : x1, michael@0: y: y1 michael@0: } : abs(x3 - x1) < ε && y3 - y0 > ε ? { michael@0: x: x1, michael@0: y: abs(x2 - x1) < ε ? y2 : y0 michael@0: } : abs(y3 - y0) < ε && x3 - x0 > ε ? { michael@0: x: abs(y2 - y0) < ε ? x2 : x0, michael@0: y: y0 michael@0: } : null), cell.site, null)); michael@0: ++nHalfEdges; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: function d3_geom_voronoiHalfEdgeOrder(a, b) { michael@0: return b.angle - a.angle; michael@0: } michael@0: function d3_geom_voronoiCircle() { michael@0: d3_geom_voronoiRedBlackNode(this); michael@0: this.x = this.y = this.arc = this.site = this.cy = null; michael@0: } michael@0: function d3_geom_voronoiAttachCircle(arc) { michael@0: var lArc = arc.P, rArc = arc.N; michael@0: if (!lArc || !rArc) return; michael@0: var lSite = lArc.site, cSite = arc.site, rSite = rArc.site; michael@0: if (lSite === rSite) return; michael@0: var bx = cSite.x, by = cSite.y, ax = lSite.x - bx, ay = lSite.y - by, cx = rSite.x - bx, cy = rSite.y - by; michael@0: var d = 2 * (ax * cy - ay * cx); michael@0: if (d >= -ε2) return; michael@0: var ha = ax * ax + ay * ay, hc = cx * cx + cy * cy, x = (cy * ha - ay * hc) / d, y = (ax * hc - cx * ha) / d, cy = y + by; michael@0: var circle = d3_geom_voronoiCirclePool.pop() || new d3_geom_voronoiCircle(); michael@0: circle.arc = arc; michael@0: circle.site = cSite; michael@0: circle.x = x + bx; michael@0: circle.y = cy + Math.sqrt(x * x + y * y); michael@0: circle.cy = cy; michael@0: arc.circle = circle; michael@0: var before = null, node = d3_geom_voronoiCircles._; michael@0: while (node) { michael@0: if (circle.y < node.y || circle.y === node.y && circle.x <= node.x) { michael@0: if (node.L) node = node.L; else { michael@0: before = node.P; michael@0: break; michael@0: } michael@0: } else { michael@0: if (node.R) node = node.R; else { michael@0: before = node; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: d3_geom_voronoiCircles.insert(before, circle); michael@0: if (!before) d3_geom_voronoiFirstCircle = circle; michael@0: } michael@0: function d3_geom_voronoiDetachCircle(arc) { michael@0: var circle = arc.circle; michael@0: if (circle) { michael@0: if (!circle.P) d3_geom_voronoiFirstCircle = circle.N; michael@0: d3_geom_voronoiCircles.remove(circle); michael@0: d3_geom_voronoiCirclePool.push(circle); michael@0: d3_geom_voronoiRedBlackNode(circle); michael@0: arc.circle = null; michael@0: } michael@0: } michael@0: function d3_geom_voronoiClipEdges(extent) { michael@0: var edges = d3_geom_voronoiEdges, clip = d3_geom_clipLine(extent[0][0], extent[0][1], extent[1][0], extent[1][1]), i = edges.length, e; michael@0: while (i--) { michael@0: e = edges[i]; michael@0: if (!d3_geom_voronoiConnectEdge(e, extent) || !clip(e) || abs(e.a.x - e.b.x) < ε && abs(e.a.y - e.b.y) < ε) { michael@0: e.a = e.b = null; michael@0: edges.splice(i, 1); michael@0: } michael@0: } michael@0: } michael@0: function d3_geom_voronoiConnectEdge(edge, extent) { michael@0: var vb = edge.b; michael@0: if (vb) return true; michael@0: var va = edge.a, x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], lSite = edge.l, rSite = edge.r, lx = lSite.x, ly = lSite.y, rx = rSite.x, ry = rSite.y, fx = (lx + rx) / 2, fy = (ly + ry) / 2, fm, fb; michael@0: if (ry === ly) { michael@0: if (fx < x0 || fx >= x1) return; michael@0: if (lx > rx) { michael@0: if (!va) va = { michael@0: x: fx, michael@0: y: y0 michael@0: }; else if (va.y >= y1) return; michael@0: vb = { michael@0: x: fx, michael@0: y: y1 michael@0: }; michael@0: } else { michael@0: if (!va) va = { michael@0: x: fx, michael@0: y: y1 michael@0: }; else if (va.y < y0) return; michael@0: vb = { michael@0: x: fx, michael@0: y: y0 michael@0: }; michael@0: } michael@0: } else { michael@0: fm = (lx - rx) / (ry - ly); michael@0: fb = fy - fm * fx; michael@0: if (fm < -1 || fm > 1) { michael@0: if (lx > rx) { michael@0: if (!va) va = { michael@0: x: (y0 - fb) / fm, michael@0: y: y0 michael@0: }; else if (va.y >= y1) return; michael@0: vb = { michael@0: x: (y1 - fb) / fm, michael@0: y: y1 michael@0: }; michael@0: } else { michael@0: if (!va) va = { michael@0: x: (y1 - fb) / fm, michael@0: y: y1 michael@0: }; else if (va.y < y0) return; michael@0: vb = { michael@0: x: (y0 - fb) / fm, michael@0: y: y0 michael@0: }; michael@0: } michael@0: } else { michael@0: if (ly < ry) { michael@0: if (!va) va = { michael@0: x: x0, michael@0: y: fm * x0 + fb michael@0: }; else if (va.x >= x1) return; michael@0: vb = { michael@0: x: x1, michael@0: y: fm * x1 + fb michael@0: }; michael@0: } else { michael@0: if (!va) va = { michael@0: x: x1, michael@0: y: fm * x1 + fb michael@0: }; else if (va.x < x0) return; michael@0: vb = { michael@0: x: x0, michael@0: y: fm * x0 + fb michael@0: }; michael@0: } michael@0: } michael@0: } michael@0: edge.a = va; michael@0: edge.b = vb; michael@0: return true; michael@0: } michael@0: function d3_geom_voronoiEdge(lSite, rSite) { michael@0: this.l = lSite; michael@0: this.r = rSite; michael@0: this.a = this.b = null; michael@0: } michael@0: function d3_geom_voronoiCreateEdge(lSite, rSite, va, vb) { michael@0: var edge = new d3_geom_voronoiEdge(lSite, rSite); michael@0: d3_geom_voronoiEdges.push(edge); michael@0: if (va) d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, va); michael@0: if (vb) d3_geom_voronoiSetEdgeEnd(edge, rSite, lSite, vb); michael@0: d3_geom_voronoiCells[lSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, lSite, rSite)); michael@0: d3_geom_voronoiCells[rSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, rSite, lSite)); michael@0: return edge; michael@0: } michael@0: function d3_geom_voronoiCreateBorderEdge(lSite, va, vb) { michael@0: var edge = new d3_geom_voronoiEdge(lSite, null); michael@0: edge.a = va; michael@0: edge.b = vb; michael@0: d3_geom_voronoiEdges.push(edge); michael@0: return edge; michael@0: } michael@0: function d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, vertex) { michael@0: if (!edge.a && !edge.b) { michael@0: edge.a = vertex; michael@0: edge.l = lSite; michael@0: edge.r = rSite; michael@0: } else if (edge.l === rSite) { michael@0: edge.b = vertex; michael@0: } else { michael@0: edge.a = vertex; michael@0: } michael@0: } michael@0: function d3_geom_voronoiHalfEdge(edge, lSite, rSite) { michael@0: var va = edge.a, vb = edge.b; michael@0: this.edge = edge; michael@0: this.site = lSite; michael@0: this.angle = rSite ? Math.atan2(rSite.y - lSite.y, rSite.x - lSite.x) : edge.l === lSite ? Math.atan2(vb.x - va.x, va.y - vb.y) : Math.atan2(va.x - vb.x, vb.y - va.y); michael@0: } michael@0: d3_geom_voronoiHalfEdge.prototype = { michael@0: start: function() { michael@0: return this.edge.l === this.site ? this.edge.a : this.edge.b; michael@0: }, michael@0: end: function() { michael@0: return this.edge.l === this.site ? this.edge.b : this.edge.a; michael@0: } michael@0: }; michael@0: function d3_geom_voronoiRedBlackTree() { michael@0: this._ = null; michael@0: } michael@0: function d3_geom_voronoiRedBlackNode(node) { michael@0: node.U = node.C = node.L = node.R = node.P = node.N = null; michael@0: } michael@0: d3_geom_voronoiRedBlackTree.prototype = { michael@0: insert: function(after, node) { michael@0: var parent, grandpa, uncle; michael@0: if (after) { michael@0: node.P = after; michael@0: node.N = after.N; michael@0: if (after.N) after.N.P = node; michael@0: after.N = node; michael@0: if (after.R) { michael@0: after = after.R; michael@0: while (after.L) after = after.L; michael@0: after.L = node; michael@0: } else { michael@0: after.R = node; michael@0: } michael@0: parent = after; michael@0: } else if (this._) { michael@0: after = d3_geom_voronoiRedBlackFirst(this._); michael@0: node.P = null; michael@0: node.N = after; michael@0: after.P = after.L = node; michael@0: parent = after; michael@0: } else { michael@0: node.P = node.N = null; michael@0: this._ = node; michael@0: parent = null; michael@0: } michael@0: node.L = node.R = null; michael@0: node.U = parent; michael@0: node.C = true; michael@0: after = node; michael@0: while (parent && parent.C) { michael@0: grandpa = parent.U; michael@0: if (parent === grandpa.L) { michael@0: uncle = grandpa.R; michael@0: if (uncle && uncle.C) { michael@0: parent.C = uncle.C = false; michael@0: grandpa.C = true; michael@0: after = grandpa; michael@0: } else { michael@0: if (after === parent.R) { michael@0: d3_geom_voronoiRedBlackRotateLeft(this, parent); michael@0: after = parent; michael@0: parent = after.U; michael@0: } michael@0: parent.C = false; michael@0: grandpa.C = true; michael@0: d3_geom_voronoiRedBlackRotateRight(this, grandpa); michael@0: } michael@0: } else { michael@0: uncle = grandpa.L; michael@0: if (uncle && uncle.C) { michael@0: parent.C = uncle.C = false; michael@0: grandpa.C = true; michael@0: after = grandpa; michael@0: } else { michael@0: if (after === parent.L) { michael@0: d3_geom_voronoiRedBlackRotateRight(this, parent); michael@0: after = parent; michael@0: parent = after.U; michael@0: } michael@0: parent.C = false; michael@0: grandpa.C = true; michael@0: d3_geom_voronoiRedBlackRotateLeft(this, grandpa); michael@0: } michael@0: } michael@0: parent = after.U; michael@0: } michael@0: this._.C = false; michael@0: }, michael@0: remove: function(node) { michael@0: if (node.N) node.N.P = node.P; michael@0: if (node.P) node.P.N = node.N; michael@0: node.N = node.P = null; michael@0: var parent = node.U, sibling, left = node.L, right = node.R, next, red; michael@0: if (!left) next = right; else if (!right) next = left; else next = d3_geom_voronoiRedBlackFirst(right); michael@0: if (parent) { michael@0: if (parent.L === node) parent.L = next; else parent.R = next; michael@0: } else { michael@0: this._ = next; michael@0: } michael@0: if (left && right) { michael@0: red = next.C; michael@0: next.C = node.C; michael@0: next.L = left; michael@0: left.U = next; michael@0: if (next !== right) { michael@0: parent = next.U; michael@0: next.U = node.U; michael@0: node = next.R; michael@0: parent.L = node; michael@0: next.R = right; michael@0: right.U = next; michael@0: } else { michael@0: next.U = parent; michael@0: parent = next; michael@0: node = next.R; michael@0: } michael@0: } else { michael@0: red = node.C; michael@0: node = next; michael@0: } michael@0: if (node) node.U = parent; michael@0: if (red) return; michael@0: if (node && node.C) { michael@0: node.C = false; michael@0: return; michael@0: } michael@0: do { michael@0: if (node === this._) break; michael@0: if (node === parent.L) { michael@0: sibling = parent.R; michael@0: if (sibling.C) { michael@0: sibling.C = false; michael@0: parent.C = true; michael@0: d3_geom_voronoiRedBlackRotateLeft(this, parent); michael@0: sibling = parent.R; michael@0: } michael@0: if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { michael@0: if (!sibling.R || !sibling.R.C) { michael@0: sibling.L.C = false; michael@0: sibling.C = true; michael@0: d3_geom_voronoiRedBlackRotateRight(this, sibling); michael@0: sibling = parent.R; michael@0: } michael@0: sibling.C = parent.C; michael@0: parent.C = sibling.R.C = false; michael@0: d3_geom_voronoiRedBlackRotateLeft(this, parent); michael@0: node = this._; michael@0: break; michael@0: } michael@0: } else { michael@0: sibling = parent.L; michael@0: if (sibling.C) { michael@0: sibling.C = false; michael@0: parent.C = true; michael@0: d3_geom_voronoiRedBlackRotateRight(this, parent); michael@0: sibling = parent.L; michael@0: } michael@0: if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { michael@0: if (!sibling.L || !sibling.L.C) { michael@0: sibling.R.C = false; michael@0: sibling.C = true; michael@0: d3_geom_voronoiRedBlackRotateLeft(this, sibling); michael@0: sibling = parent.L; michael@0: } michael@0: sibling.C = parent.C; michael@0: parent.C = sibling.L.C = false; michael@0: d3_geom_voronoiRedBlackRotateRight(this, parent); michael@0: node = this._; michael@0: break; michael@0: } michael@0: } michael@0: sibling.C = true; michael@0: node = parent; michael@0: parent = parent.U; michael@0: } while (!node.C); michael@0: if (node) node.C = false; michael@0: } michael@0: }; michael@0: function d3_geom_voronoiRedBlackRotateLeft(tree, node) { michael@0: var p = node, q = node.R, parent = p.U; michael@0: if (parent) { michael@0: if (parent.L === p) parent.L = q; else parent.R = q; michael@0: } else { michael@0: tree._ = q; michael@0: } michael@0: q.U = parent; michael@0: p.U = q; michael@0: p.R = q.L; michael@0: if (p.R) p.R.U = p; michael@0: q.L = p; michael@0: } michael@0: function d3_geom_voronoiRedBlackRotateRight(tree, node) { michael@0: var p = node, q = node.L, parent = p.U; michael@0: if (parent) { michael@0: if (parent.L === p) parent.L = q; else parent.R = q; michael@0: } else { michael@0: tree._ = q; michael@0: } michael@0: q.U = parent; michael@0: p.U = q; michael@0: p.L = q.R; michael@0: if (p.L) p.L.U = p; michael@0: q.R = p; michael@0: } michael@0: function d3_geom_voronoiRedBlackFirst(node) { michael@0: while (node.L) node = node.L; michael@0: return node; michael@0: } michael@0: function d3_geom_voronoi(sites, bbox) { michael@0: var site = sites.sort(d3_geom_voronoiVertexOrder).pop(), x0, y0, circle; michael@0: d3_geom_voronoiEdges = []; michael@0: d3_geom_voronoiCells = new Array(sites.length); michael@0: d3_geom_voronoiBeaches = new d3_geom_voronoiRedBlackTree(); michael@0: d3_geom_voronoiCircles = new d3_geom_voronoiRedBlackTree(); michael@0: while (true) { michael@0: circle = d3_geom_voronoiFirstCircle; michael@0: if (site && (!circle || site.y < circle.y || site.y === circle.y && site.x < circle.x)) { michael@0: if (site.x !== x0 || site.y !== y0) { michael@0: d3_geom_voronoiCells[site.i] = new d3_geom_voronoiCell(site); michael@0: d3_geom_voronoiAddBeach(site); michael@0: x0 = site.x, y0 = site.y; michael@0: } michael@0: site = sites.pop(); michael@0: } else if (circle) { michael@0: d3_geom_voronoiRemoveBeach(circle.arc); michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: if (bbox) d3_geom_voronoiClipEdges(bbox), d3_geom_voronoiCloseCells(bbox); michael@0: var diagram = { michael@0: cells: d3_geom_voronoiCells, michael@0: edges: d3_geom_voronoiEdges michael@0: }; michael@0: d3_geom_voronoiBeaches = d3_geom_voronoiCircles = d3_geom_voronoiEdges = d3_geom_voronoiCells = null; michael@0: return diagram; michael@0: } michael@0: function d3_geom_voronoiVertexOrder(a, b) { michael@0: return b.y - a.y || b.x - a.x; michael@0: } michael@0: d3.geom.voronoi = function(points) { michael@0: var x = d3_geom_pointX, y = d3_geom_pointY, fx = x, fy = y, clipExtent = d3_geom_voronoiClipExtent; michael@0: if (points) return voronoi(points); michael@0: function voronoi(data) { michael@0: var polygons = new Array(data.length), x0 = clipExtent[0][0], y0 = clipExtent[0][1], x1 = clipExtent[1][0], y1 = clipExtent[1][1]; michael@0: d3_geom_voronoi(sites(data), clipExtent).cells.forEach(function(cell, i) { michael@0: var edges = cell.edges, site = cell.site, polygon = polygons[i] = edges.length ? edges.map(function(e) { michael@0: var s = e.start(); michael@0: return [ s.x, s.y ]; michael@0: }) : site.x >= x0 && site.x <= x1 && site.y >= y0 && site.y <= y1 ? [ [ x0, y1 ], [ x1, y1 ], [ x1, y0 ], [ x0, y0 ] ] : []; michael@0: polygon.point = data[i]; michael@0: }); michael@0: return polygons; michael@0: } michael@0: function sites(data) { michael@0: return data.map(function(d, i) { michael@0: return { michael@0: x: Math.round(fx(d, i) / ε) * ε, michael@0: y: Math.round(fy(d, i) / ε) * ε, michael@0: i: i michael@0: }; michael@0: }); michael@0: } michael@0: voronoi.links = function(data) { michael@0: return d3_geom_voronoi(sites(data)).edges.filter(function(edge) { michael@0: return edge.l && edge.r; michael@0: }).map(function(edge) { michael@0: return { michael@0: source: data[edge.l.i], michael@0: target: data[edge.r.i] michael@0: }; michael@0: }); michael@0: }; michael@0: voronoi.triangles = function(data) { michael@0: var triangles = []; michael@0: d3_geom_voronoi(sites(data)).cells.forEach(function(cell, i) { michael@0: var site = cell.site, edges = cell.edges.sort(d3_geom_voronoiHalfEdgeOrder), j = -1, m = edges.length, e0, s0, e1 = edges[m - 1].edge, s1 = e1.l === site ? e1.r : e1.l; michael@0: while (++j < m) { michael@0: e0 = e1; michael@0: s0 = s1; michael@0: e1 = edges[j].edge; michael@0: s1 = e1.l === site ? e1.r : e1.l; michael@0: if (i < s0.i && i < s1.i && d3_geom_voronoiTriangleArea(site, s0, s1) < 0) { michael@0: triangles.push([ data[i], data[s0.i], data[s1.i] ]); michael@0: } michael@0: } michael@0: }); michael@0: return triangles; michael@0: }; michael@0: voronoi.x = function(_) { michael@0: return arguments.length ? (fx = d3_functor(x = _), voronoi) : x; michael@0: }; michael@0: voronoi.y = function(_) { michael@0: return arguments.length ? (fy = d3_functor(y = _), voronoi) : y; michael@0: }; michael@0: voronoi.clipExtent = function(_) { michael@0: if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent; michael@0: clipExtent = _ == null ? d3_geom_voronoiClipExtent : _; michael@0: return voronoi; michael@0: }; michael@0: voronoi.size = function(_) { michael@0: if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent && clipExtent[1]; michael@0: return voronoi.clipExtent(_ && [ [ 0, 0 ], _ ]); michael@0: }; michael@0: return voronoi; michael@0: }; michael@0: var d3_geom_voronoiClipExtent = [ [ -1e6, -1e6 ], [ 1e6, 1e6 ] ]; michael@0: function d3_geom_voronoiTriangleArea(a, b, c) { michael@0: return (a.x - c.x) * (b.y - a.y) - (a.x - b.x) * (c.y - a.y); michael@0: } michael@0: d3.geom.delaunay = function(vertices) { michael@0: return d3.geom.voronoi().triangles(vertices); michael@0: }; michael@0: d3.geom.quadtree = function(points, x1, y1, x2, y2) { michael@0: var x = d3_geom_pointX, y = d3_geom_pointY, compat; michael@0: if (compat = arguments.length) { michael@0: x = d3_geom_quadtreeCompatX; michael@0: y = d3_geom_quadtreeCompatY; michael@0: if (compat === 3) { michael@0: y2 = y1; michael@0: x2 = x1; michael@0: y1 = x1 = 0; michael@0: } michael@0: return quadtree(points); michael@0: } michael@0: function quadtree(data) { michael@0: var d, fx = d3_functor(x), fy = d3_functor(y), xs, ys, i, n, x1_, y1_, x2_, y2_; michael@0: if (x1 != null) { michael@0: x1_ = x1, y1_ = y1, x2_ = x2, y2_ = y2; michael@0: } else { michael@0: x2_ = y2_ = -(x1_ = y1_ = Infinity); michael@0: xs = [], ys = []; michael@0: n = data.length; michael@0: if (compat) for (i = 0; i < n; ++i) { michael@0: d = data[i]; michael@0: if (d.x < x1_) x1_ = d.x; michael@0: if (d.y < y1_) y1_ = d.y; michael@0: if (d.x > x2_) x2_ = d.x; michael@0: if (d.y > y2_) y2_ = d.y; michael@0: xs.push(d.x); michael@0: ys.push(d.y); michael@0: } else for (i = 0; i < n; ++i) { michael@0: var x_ = +fx(d = data[i], i), y_ = +fy(d, i); michael@0: if (x_ < x1_) x1_ = x_; michael@0: if (y_ < y1_) y1_ = y_; michael@0: if (x_ > x2_) x2_ = x_; michael@0: if (y_ > y2_) y2_ = y_; michael@0: xs.push(x_); michael@0: ys.push(y_); michael@0: } michael@0: } michael@0: var dx = x2_ - x1_, dy = y2_ - y1_; michael@0: if (dx > dy) y2_ = y1_ + dx; else x2_ = x1_ + dy; michael@0: function insert(n, d, x, y, x1, y1, x2, y2) { michael@0: if (isNaN(x) || isNaN(y)) return; michael@0: if (n.leaf) { michael@0: var nx = n.x, ny = n.y; michael@0: if (nx != null) { michael@0: if (abs(nx - x) + abs(ny - y) < .01) { michael@0: insertChild(n, d, x, y, x1, y1, x2, y2); michael@0: } else { michael@0: var nPoint = n.point; michael@0: n.x = n.y = n.point = null; michael@0: insertChild(n, nPoint, nx, ny, x1, y1, x2, y2); michael@0: insertChild(n, d, x, y, x1, y1, x2, y2); michael@0: } michael@0: } else { michael@0: n.x = x, n.y = y, n.point = d; michael@0: } michael@0: } else { michael@0: insertChild(n, d, x, y, x1, y1, x2, y2); michael@0: } michael@0: } michael@0: function insertChild(n, d, x, y, x1, y1, x2, y2) { michael@0: var sx = (x1 + x2) * .5, sy = (y1 + y2) * .5, right = x >= sx, bottom = y >= sy, i = (bottom << 1) + right; michael@0: n.leaf = false; michael@0: n = n.nodes[i] || (n.nodes[i] = d3_geom_quadtreeNode()); michael@0: if (right) x1 = sx; else x2 = sx; michael@0: if (bottom) y1 = sy; else y2 = sy; michael@0: insert(n, d, x, y, x1, y1, x2, y2); michael@0: } michael@0: var root = d3_geom_quadtreeNode(); michael@0: root.add = function(d) { michael@0: insert(root, d, +fx(d, ++i), +fy(d, i), x1_, y1_, x2_, y2_); michael@0: }; michael@0: root.visit = function(f) { michael@0: d3_geom_quadtreeVisit(f, root, x1_, y1_, x2_, y2_); michael@0: }; michael@0: i = -1; michael@0: if (x1 == null) { michael@0: while (++i < n) { michael@0: insert(root, data[i], xs[i], ys[i], x1_, y1_, x2_, y2_); michael@0: } michael@0: --i; michael@0: } else data.forEach(root.add); michael@0: xs = ys = data = d = null; michael@0: return root; michael@0: } michael@0: quadtree.x = function(_) { michael@0: return arguments.length ? (x = _, quadtree) : x; michael@0: }; michael@0: quadtree.y = function(_) { michael@0: return arguments.length ? (y = _, quadtree) : y; michael@0: }; michael@0: quadtree.extent = function(_) { michael@0: if (!arguments.length) return x1 == null ? null : [ [ x1, y1 ], [ x2, y2 ] ]; michael@0: if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = +_[0][0], y1 = +_[0][1], x2 = +_[1][0], michael@0: y2 = +_[1][1]; michael@0: return quadtree; michael@0: }; michael@0: quadtree.size = function(_) { michael@0: if (!arguments.length) return x1 == null ? null : [ x2 - x1, y2 - y1 ]; michael@0: if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = y1 = 0, x2 = +_[0], y2 = +_[1]; michael@0: return quadtree; michael@0: }; michael@0: return quadtree; michael@0: }; michael@0: function d3_geom_quadtreeCompatX(d) { michael@0: return d.x; michael@0: } michael@0: function d3_geom_quadtreeCompatY(d) { michael@0: return d.y; michael@0: } michael@0: function d3_geom_quadtreeNode() { michael@0: return { michael@0: leaf: true, michael@0: nodes: [], michael@0: point: null, michael@0: x: null, michael@0: y: null michael@0: }; michael@0: } michael@0: function d3_geom_quadtreeVisit(f, node, x1, y1, x2, y2) { michael@0: if (!f(node, x1, y1, x2, y2)) { michael@0: var sx = (x1 + x2) * .5, sy = (y1 + y2) * .5, children = node.nodes; michael@0: if (children[0]) d3_geom_quadtreeVisit(f, children[0], x1, y1, sx, sy); michael@0: if (children[1]) d3_geom_quadtreeVisit(f, children[1], sx, y1, x2, sy); michael@0: if (children[2]) d3_geom_quadtreeVisit(f, children[2], x1, sy, sx, y2); michael@0: if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2); michael@0: } michael@0: } michael@0: d3.interpolateRgb = d3_interpolateRgb; michael@0: function d3_interpolateRgb(a, b) { michael@0: a = d3.rgb(a); michael@0: b = d3.rgb(b); michael@0: var ar = a.r, ag = a.g, ab = a.b, br = b.r - ar, bg = b.g - ag, bb = b.b - ab; michael@0: return function(t) { michael@0: return "#" + d3_rgb_hex(Math.round(ar + br * t)) + d3_rgb_hex(Math.round(ag + bg * t)) + d3_rgb_hex(Math.round(ab + bb * t)); michael@0: }; michael@0: } michael@0: d3.interpolateObject = d3_interpolateObject; michael@0: function d3_interpolateObject(a, b) { michael@0: var i = {}, c = {}, k; michael@0: for (k in a) { michael@0: if (k in b) { michael@0: i[k] = d3_interpolate(a[k], b[k]); michael@0: } else { michael@0: c[k] = a[k]; michael@0: } michael@0: } michael@0: for (k in b) { michael@0: if (!(k in a)) { michael@0: c[k] = b[k]; michael@0: } michael@0: } michael@0: return function(t) { michael@0: for (k in i) c[k] = i[k](t); michael@0: return c; michael@0: }; michael@0: } michael@0: d3.interpolateNumber = d3_interpolateNumber; michael@0: function d3_interpolateNumber(a, b) { michael@0: b -= a = +a; michael@0: return function(t) { michael@0: return a + b * t; michael@0: }; michael@0: } michael@0: d3.interpolateString = d3_interpolateString; michael@0: function d3_interpolateString(a, b) { michael@0: var m, i, j, s0 = 0, s1 = 0, s = [], q = [], n, o; michael@0: a = a + "", b = b + ""; michael@0: d3_interpolate_number.lastIndex = 0; michael@0: for (i = 0; m = d3_interpolate_number.exec(b); ++i) { michael@0: if (m.index) s.push(b.substring(s0, s1 = m.index)); michael@0: q.push({ michael@0: i: s.length, michael@0: x: m[0] michael@0: }); michael@0: s.push(null); michael@0: s0 = d3_interpolate_number.lastIndex; michael@0: } michael@0: if (s0 < b.length) s.push(b.substring(s0)); michael@0: for (i = 0, n = q.length; (m = d3_interpolate_number.exec(a)) && i < n; ++i) { michael@0: o = q[i]; michael@0: if (o.x == m[0]) { michael@0: if (o.i) { michael@0: if (s[o.i + 1] == null) { michael@0: s[o.i - 1] += o.x; michael@0: s.splice(o.i, 1); michael@0: for (j = i + 1; j < n; ++j) q[j].i--; michael@0: } else { michael@0: s[o.i - 1] += o.x + s[o.i + 1]; michael@0: s.splice(o.i, 2); michael@0: for (j = i + 1; j < n; ++j) q[j].i -= 2; michael@0: } michael@0: } else { michael@0: if (s[o.i + 1] == null) { michael@0: s[o.i] = o.x; michael@0: } else { michael@0: s[o.i] = o.x + s[o.i + 1]; michael@0: s.splice(o.i + 1, 1); michael@0: for (j = i + 1; j < n; ++j) q[j].i--; michael@0: } michael@0: } michael@0: q.splice(i, 1); michael@0: n--; michael@0: i--; michael@0: } else { michael@0: o.x = d3_interpolateNumber(parseFloat(m[0]), parseFloat(o.x)); michael@0: } michael@0: } michael@0: while (i < n) { michael@0: o = q.pop(); michael@0: if (s[o.i + 1] == null) { michael@0: s[o.i] = o.x; michael@0: } else { michael@0: s[o.i] = o.x + s[o.i + 1]; michael@0: s.splice(o.i + 1, 1); michael@0: } michael@0: n--; michael@0: } michael@0: if (s.length === 1) { michael@0: return s[0] == null ? (o = q[0].x, function(t) { michael@0: return o(t) + ""; michael@0: }) : function() { michael@0: return b; michael@0: }; michael@0: } michael@0: return function(t) { michael@0: for (i = 0; i < n; ++i) s[(o = q[i]).i] = o.x(t); michael@0: return s.join(""); michael@0: }; michael@0: } michael@0: var d3_interpolate_number = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g; michael@0: d3.interpolate = d3_interpolate; michael@0: function d3_interpolate(a, b) { michael@0: var i = d3.interpolators.length, f; michael@0: while (--i >= 0 && !(f = d3.interpolators[i](a, b))) ; michael@0: return f; michael@0: } michael@0: d3.interpolators = [ function(a, b) { michael@0: var t = typeof b; michael@0: return (t === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_Color ? d3_interpolateRgb : t === "object" ? Array.isArray(b) ? d3_interpolateArray : d3_interpolateObject : d3_interpolateNumber)(a, b); michael@0: } ]; michael@0: d3.interpolateArray = d3_interpolateArray; michael@0: function d3_interpolateArray(a, b) { michael@0: var x = [], c = [], na = a.length, nb = b.length, n0 = Math.min(a.length, b.length), i; michael@0: for (i = 0; i < n0; ++i) x.push(d3_interpolate(a[i], b[i])); michael@0: for (;i < na; ++i) c[i] = a[i]; michael@0: for (;i < nb; ++i) c[i] = b[i]; michael@0: return function(t) { michael@0: for (i = 0; i < n0; ++i) c[i] = x[i](t); michael@0: return c; michael@0: }; michael@0: } michael@0: var d3_ease_default = function() { michael@0: return d3_identity; michael@0: }; michael@0: var d3_ease = d3.map({ michael@0: linear: d3_ease_default, michael@0: poly: d3_ease_poly, michael@0: quad: function() { michael@0: return d3_ease_quad; michael@0: }, michael@0: cubic: function() { michael@0: return d3_ease_cubic; michael@0: }, michael@0: sin: function() { michael@0: return d3_ease_sin; michael@0: }, michael@0: exp: function() { michael@0: return d3_ease_exp; michael@0: }, michael@0: circle: function() { michael@0: return d3_ease_circle; michael@0: }, michael@0: elastic: d3_ease_elastic, michael@0: back: d3_ease_back, michael@0: bounce: function() { michael@0: return d3_ease_bounce; michael@0: } michael@0: }); michael@0: var d3_ease_mode = d3.map({ michael@0: "in": d3_identity, michael@0: out: d3_ease_reverse, michael@0: "in-out": d3_ease_reflect, michael@0: "out-in": function(f) { michael@0: return d3_ease_reflect(d3_ease_reverse(f)); michael@0: } michael@0: }); michael@0: d3.ease = function(name) { michael@0: var i = name.indexOf("-"), t = i >= 0 ? name.substring(0, i) : name, m = i >= 0 ? name.substring(i + 1) : "in"; michael@0: t = d3_ease.get(t) || d3_ease_default; michael@0: m = d3_ease_mode.get(m) || d3_identity; michael@0: return d3_ease_clamp(m(t.apply(null, d3_arraySlice.call(arguments, 1)))); michael@0: }; michael@0: function d3_ease_clamp(f) { michael@0: return function(t) { michael@0: return t <= 0 ? 0 : t >= 1 ? 1 : f(t); michael@0: }; michael@0: } michael@0: function d3_ease_reverse(f) { michael@0: return function(t) { michael@0: return 1 - f(1 - t); michael@0: }; michael@0: } michael@0: function d3_ease_reflect(f) { michael@0: return function(t) { michael@0: return .5 * (t < .5 ? f(2 * t) : 2 - f(2 - 2 * t)); michael@0: }; michael@0: } michael@0: function d3_ease_quad(t) { michael@0: return t * t; michael@0: } michael@0: function d3_ease_cubic(t) { michael@0: return t * t * t; michael@0: } michael@0: function d3_ease_cubicInOut(t) { michael@0: if (t <= 0) return 0; michael@0: if (t >= 1) return 1; michael@0: var t2 = t * t, t3 = t2 * t; michael@0: return 4 * (t < .5 ? t3 : 3 * (t - t2) + t3 - .75); michael@0: } michael@0: function d3_ease_poly(e) { michael@0: return function(t) { michael@0: return Math.pow(t, e); michael@0: }; michael@0: } michael@0: function d3_ease_sin(t) { michael@0: return 1 - Math.cos(t * halfπ); michael@0: } michael@0: function d3_ease_exp(t) { michael@0: return Math.pow(2, 10 * (t - 1)); michael@0: } michael@0: function d3_ease_circle(t) { michael@0: return 1 - Math.sqrt(1 - t * t); michael@0: } michael@0: function d3_ease_elastic(a, p) { michael@0: var s; michael@0: if (arguments.length < 2) p = .45; michael@0: if (arguments.length) s = p / τ * Math.asin(1 / a); else a = 1, s = p / 4; michael@0: return function(t) { michael@0: return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * τ / p); michael@0: }; michael@0: } michael@0: function d3_ease_back(s) { michael@0: if (!s) s = 1.70158; michael@0: return function(t) { michael@0: return t * t * ((s + 1) * t - s); michael@0: }; michael@0: } michael@0: function d3_ease_bounce(t) { michael@0: return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; michael@0: } michael@0: d3.interpolateHcl = d3_interpolateHcl; michael@0: function d3_interpolateHcl(a, b) { michael@0: a = d3.hcl(a); michael@0: b = d3.hcl(b); michael@0: var ah = a.h, ac = a.c, al = a.l, bh = b.h - ah, bc = b.c - ac, bl = b.l - al; michael@0: if (isNaN(bc)) bc = 0, ac = isNaN(ac) ? b.c : ac; michael@0: if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; michael@0: return function(t) { michael@0: return d3_hcl_lab(ah + bh * t, ac + bc * t, al + bl * t) + ""; michael@0: }; michael@0: } michael@0: d3.interpolateHsl = d3_interpolateHsl; michael@0: function d3_interpolateHsl(a, b) { michael@0: a = d3.hsl(a); michael@0: b = d3.hsl(b); michael@0: var ah = a.h, as = a.s, al = a.l, bh = b.h - ah, bs = b.s - as, bl = b.l - al; michael@0: if (isNaN(bs)) bs = 0, as = isNaN(as) ? b.s : as; michael@0: if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; michael@0: return function(t) { michael@0: return d3_hsl_rgb(ah + bh * t, as + bs * t, al + bl * t) + ""; michael@0: }; michael@0: } michael@0: d3.interpolateLab = d3_interpolateLab; michael@0: function d3_interpolateLab(a, b) { michael@0: a = d3.lab(a); michael@0: b = d3.lab(b); michael@0: var al = a.l, aa = a.a, ab = a.b, bl = b.l - al, ba = b.a - aa, bb = b.b - ab; michael@0: return function(t) { michael@0: return d3_lab_rgb(al + bl * t, aa + ba * t, ab + bb * t) + ""; michael@0: }; michael@0: } michael@0: d3.interpolateRound = d3_interpolateRound; michael@0: function d3_interpolateRound(a, b) { michael@0: b -= a; michael@0: return function(t) { michael@0: return Math.round(a + b * t); michael@0: }; michael@0: } michael@0: d3.transform = function(string) { michael@0: var g = d3_document.createElementNS(d3.ns.prefix.svg, "g"); michael@0: return (d3.transform = function(string) { michael@0: if (string != null) { michael@0: g.setAttribute("transform", string); michael@0: var t = g.transform.baseVal.consolidate(); michael@0: } michael@0: return new d3_transform(t ? t.matrix : d3_transformIdentity); michael@0: })(string); michael@0: }; michael@0: function d3_transform(m) { michael@0: var r0 = [ m.a, m.b ], r1 = [ m.c, m.d ], kx = d3_transformNormalize(r0), kz = d3_transformDot(r0, r1), ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0; michael@0: if (r0[0] * r1[1] < r1[0] * r0[1]) { michael@0: r0[0] *= -1; michael@0: r0[1] *= -1; michael@0: kx *= -1; michael@0: kz *= -1; michael@0: } michael@0: this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_degrees; michael@0: this.translate = [ m.e, m.f ]; michael@0: this.scale = [ kx, ky ]; michael@0: this.skew = ky ? Math.atan2(kz, ky) * d3_degrees : 0; michael@0: } michael@0: d3_transform.prototype.toString = function() { michael@0: return "translate(" + this.translate + ")rotate(" + this.rotate + ")skewX(" + this.skew + ")scale(" + this.scale + ")"; michael@0: }; michael@0: function d3_transformDot(a, b) { michael@0: return a[0] * b[0] + a[1] * b[1]; michael@0: } michael@0: function d3_transformNormalize(a) { michael@0: var k = Math.sqrt(d3_transformDot(a, a)); michael@0: if (k) { michael@0: a[0] /= k; michael@0: a[1] /= k; michael@0: } michael@0: return k; michael@0: } michael@0: function d3_transformCombine(a, b, k) { michael@0: a[0] += k * b[0]; michael@0: a[1] += k * b[1]; michael@0: return a; michael@0: } michael@0: var d3_transformIdentity = { michael@0: a: 1, michael@0: b: 0, michael@0: c: 0, michael@0: d: 1, michael@0: e: 0, michael@0: f: 0 michael@0: }; michael@0: d3.interpolateTransform = d3_interpolateTransform; michael@0: function d3_interpolateTransform(a, b) { michael@0: var s = [], q = [], n, A = d3.transform(a), B = d3.transform(b), ta = A.translate, tb = B.translate, ra = A.rotate, rb = B.rotate, wa = A.skew, wb = B.skew, ka = A.scale, kb = B.scale; michael@0: if (ta[0] != tb[0] || ta[1] != tb[1]) { michael@0: s.push("translate(", null, ",", null, ")"); michael@0: q.push({ michael@0: i: 1, michael@0: x: d3_interpolateNumber(ta[0], tb[0]) michael@0: }, { michael@0: i: 3, michael@0: x: d3_interpolateNumber(ta[1], tb[1]) michael@0: }); michael@0: } else if (tb[0] || tb[1]) { michael@0: s.push("translate(" + tb + ")"); michael@0: } else { michael@0: s.push(""); michael@0: } michael@0: if (ra != rb) { michael@0: if (ra - rb > 180) rb += 360; else if (rb - ra > 180) ra += 360; michael@0: q.push({ michael@0: i: s.push(s.pop() + "rotate(", null, ")") - 2, michael@0: x: d3_interpolateNumber(ra, rb) michael@0: }); michael@0: } else if (rb) { michael@0: s.push(s.pop() + "rotate(" + rb + ")"); michael@0: } michael@0: if (wa != wb) { michael@0: q.push({ michael@0: i: s.push(s.pop() + "skewX(", null, ")") - 2, michael@0: x: d3_interpolateNumber(wa, wb) michael@0: }); michael@0: } else if (wb) { michael@0: s.push(s.pop() + "skewX(" + wb + ")"); michael@0: } michael@0: if (ka[0] != kb[0] || ka[1] != kb[1]) { michael@0: n = s.push(s.pop() + "scale(", null, ",", null, ")"); michael@0: q.push({ michael@0: i: n - 4, michael@0: x: d3_interpolateNumber(ka[0], kb[0]) michael@0: }, { michael@0: i: n - 2, michael@0: x: d3_interpolateNumber(ka[1], kb[1]) michael@0: }); michael@0: } else if (kb[0] != 1 || kb[1] != 1) { michael@0: s.push(s.pop() + "scale(" + kb + ")"); michael@0: } michael@0: n = q.length; michael@0: return function(t) { michael@0: var i = -1, o; michael@0: while (++i < n) s[(o = q[i]).i] = o.x(t); michael@0: return s.join(""); michael@0: }; michael@0: } michael@0: function d3_uninterpolateNumber(a, b) { michael@0: b = b - (a = +a) ? 1 / (b - a) : 0; michael@0: return function(x) { michael@0: return (x - a) * b; michael@0: }; michael@0: } michael@0: function d3_uninterpolateClamp(a, b) { michael@0: b = b - (a = +a) ? 1 / (b - a) : 0; michael@0: return function(x) { michael@0: return Math.max(0, Math.min(1, (x - a) * b)); michael@0: }; michael@0: } michael@0: d3.layout = {}; michael@0: d3.layout.bundle = function() { michael@0: return function(links) { michael@0: var paths = [], i = -1, n = links.length; michael@0: while (++i < n) paths.push(d3_layout_bundlePath(links[i])); michael@0: return paths; michael@0: }; michael@0: }; michael@0: function d3_layout_bundlePath(link) { michael@0: var start = link.source, end = link.target, lca = d3_layout_bundleLeastCommonAncestor(start, end), points = [ start ]; michael@0: while (start !== lca) { michael@0: start = start.parent; michael@0: points.push(start); michael@0: } michael@0: var k = points.length; michael@0: while (end !== lca) { michael@0: points.splice(k, 0, end); michael@0: end = end.parent; michael@0: } michael@0: return points; michael@0: } michael@0: function d3_layout_bundleAncestors(node) { michael@0: var ancestors = [], parent = node.parent; michael@0: while (parent != null) { michael@0: ancestors.push(node); michael@0: node = parent; michael@0: parent = parent.parent; michael@0: } michael@0: ancestors.push(node); michael@0: return ancestors; michael@0: } michael@0: function d3_layout_bundleLeastCommonAncestor(a, b) { michael@0: if (a === b) return a; michael@0: var aNodes = d3_layout_bundleAncestors(a), bNodes = d3_layout_bundleAncestors(b), aNode = aNodes.pop(), bNode = bNodes.pop(), sharedNode = null; michael@0: while (aNode === bNode) { michael@0: sharedNode = aNode; michael@0: aNode = aNodes.pop(); michael@0: bNode = bNodes.pop(); michael@0: } michael@0: return sharedNode; michael@0: } michael@0: d3.layout.chord = function() { michael@0: var chord = {}, chords, groups, matrix, n, padding = 0, sortGroups, sortSubgroups, sortChords; michael@0: function relayout() { michael@0: var subgroups = {}, groupSums = [], groupIndex = d3.range(n), subgroupIndex = [], k, x, x0, i, j; michael@0: chords = []; michael@0: groups = []; michael@0: k = 0, i = -1; michael@0: while (++i < n) { michael@0: x = 0, j = -1; michael@0: while (++j < n) { michael@0: x += matrix[i][j]; michael@0: } michael@0: groupSums.push(x); michael@0: subgroupIndex.push(d3.range(n)); michael@0: k += x; michael@0: } michael@0: if (sortGroups) { michael@0: groupIndex.sort(function(a, b) { michael@0: return sortGroups(groupSums[a], groupSums[b]); michael@0: }); michael@0: } michael@0: if (sortSubgroups) { michael@0: subgroupIndex.forEach(function(d, i) { michael@0: d.sort(function(a, b) { michael@0: return sortSubgroups(matrix[i][a], matrix[i][b]); michael@0: }); michael@0: }); michael@0: } michael@0: k = (τ - padding * n) / k; michael@0: x = 0, i = -1; michael@0: while (++i < n) { michael@0: x0 = x, j = -1; michael@0: while (++j < n) { michael@0: var di = groupIndex[i], dj = subgroupIndex[di][j], v = matrix[di][dj], a0 = x, a1 = x += v * k; michael@0: subgroups[di + "-" + dj] = { michael@0: index: di, michael@0: subindex: dj, michael@0: startAngle: a0, michael@0: endAngle: a1, michael@0: value: v michael@0: }; michael@0: } michael@0: groups[di] = { michael@0: index: di, michael@0: startAngle: x0, michael@0: endAngle: x, michael@0: value: (x - x0) / k michael@0: }; michael@0: x += padding; michael@0: } michael@0: i = -1; michael@0: while (++i < n) { michael@0: j = i - 1; michael@0: while (++j < n) { michael@0: var source = subgroups[i + "-" + j], target = subgroups[j + "-" + i]; michael@0: if (source.value || target.value) { michael@0: chords.push(source.value < target.value ? { michael@0: source: target, michael@0: target: source michael@0: } : { michael@0: source: source, michael@0: target: target michael@0: }); michael@0: } michael@0: } michael@0: } michael@0: if (sortChords) resort(); michael@0: } michael@0: function resort() { michael@0: chords.sort(function(a, b) { michael@0: return sortChords((a.source.value + a.target.value) / 2, (b.source.value + b.target.value) / 2); michael@0: }); michael@0: } michael@0: chord.matrix = function(x) { michael@0: if (!arguments.length) return matrix; michael@0: n = (matrix = x) && matrix.length; michael@0: chords = groups = null; michael@0: return chord; michael@0: }; michael@0: chord.padding = function(x) { michael@0: if (!arguments.length) return padding; michael@0: padding = x; michael@0: chords = groups = null; michael@0: return chord; michael@0: }; michael@0: chord.sortGroups = function(x) { michael@0: if (!arguments.length) return sortGroups; michael@0: sortGroups = x; michael@0: chords = groups = null; michael@0: return chord; michael@0: }; michael@0: chord.sortSubgroups = function(x) { michael@0: if (!arguments.length) return sortSubgroups; michael@0: sortSubgroups = x; michael@0: chords = null; michael@0: return chord; michael@0: }; michael@0: chord.sortChords = function(x) { michael@0: if (!arguments.length) return sortChords; michael@0: sortChords = x; michael@0: if (chords) resort(); michael@0: return chord; michael@0: }; michael@0: chord.chords = function() { michael@0: if (!chords) relayout(); michael@0: return chords; michael@0: }; michael@0: chord.groups = function() { michael@0: if (!groups) relayout(); michael@0: return groups; michael@0: }; michael@0: return chord; michael@0: }; michael@0: d3.layout.force = function() { michael@0: var force = {}, event = d3.dispatch("start", "tick", "end"), size = [ 1, 1 ], drag, alpha, friction = .9, linkDistance = d3_layout_forceLinkDistance, linkStrength = d3_layout_forceLinkStrength, charge = -30, chargeDistance2 = d3_layout_forceChargeDistance2, gravity = .1, theta2 = .64, nodes = [], links = [], distances, strengths, charges; michael@0: function repulse(node) { michael@0: return function(quad, x1, _, x2) { michael@0: if (quad.point !== node) { michael@0: var dx = quad.cx - node.x, dy = quad.cy - node.y, dw = x2 - x1, dn = dx * dx + dy * dy; michael@0: if (dw * dw / theta2 < dn) { michael@0: if (dn < chargeDistance2) { michael@0: var k = quad.charge / dn; michael@0: node.px -= dx * k; michael@0: node.py -= dy * k; michael@0: } michael@0: return true; michael@0: } michael@0: if (quad.point && dn && dn < chargeDistance2) { michael@0: var k = quad.pointCharge / dn; michael@0: node.px -= dx * k; michael@0: node.py -= dy * k; michael@0: } michael@0: } michael@0: return !quad.charge; michael@0: }; michael@0: } michael@0: force.tick = function() { michael@0: if ((alpha *= .99) < .005) { michael@0: event.end({ michael@0: type: "end", michael@0: alpha: alpha = 0 michael@0: }); michael@0: return true; michael@0: } michael@0: var n = nodes.length, m = links.length, q, i, o, s, t, l, k, x, y; michael@0: for (i = 0; i < m; ++i) { michael@0: o = links[i]; michael@0: s = o.source; michael@0: t = o.target; michael@0: x = t.x - s.x; michael@0: y = t.y - s.y; michael@0: if (l = x * x + y * y) { michael@0: l = alpha * strengths[i] * ((l = Math.sqrt(l)) - distances[i]) / l; michael@0: x *= l; michael@0: y *= l; michael@0: t.x -= x * (k = s.weight / (t.weight + s.weight)); michael@0: t.y -= y * k; michael@0: s.x += x * (k = 1 - k); michael@0: s.y += y * k; michael@0: } michael@0: } michael@0: if (k = alpha * gravity) { michael@0: x = size[0] / 2; michael@0: y = size[1] / 2; michael@0: i = -1; michael@0: if (k) while (++i < n) { michael@0: o = nodes[i]; michael@0: o.x += (x - o.x) * k; michael@0: o.y += (y - o.y) * k; michael@0: } michael@0: } michael@0: if (charge) { michael@0: d3_layout_forceAccumulate(q = d3.geom.quadtree(nodes), alpha, charges); michael@0: i = -1; michael@0: while (++i < n) { michael@0: if (!(o = nodes[i]).fixed) { michael@0: q.visit(repulse(o)); michael@0: } michael@0: } michael@0: } michael@0: i = -1; michael@0: while (++i < n) { michael@0: o = nodes[i]; michael@0: if (o.fixed) { michael@0: o.x = o.px; michael@0: o.y = o.py; michael@0: } else { michael@0: o.x -= (o.px - (o.px = o.x)) * friction; michael@0: o.y -= (o.py - (o.py = o.y)) * friction; michael@0: } michael@0: } michael@0: event.tick({ michael@0: type: "tick", michael@0: alpha: alpha michael@0: }); michael@0: }; michael@0: force.nodes = function(x) { michael@0: if (!arguments.length) return nodes; michael@0: nodes = x; michael@0: return force; michael@0: }; michael@0: force.links = function(x) { michael@0: if (!arguments.length) return links; michael@0: links = x; michael@0: return force; michael@0: }; michael@0: force.size = function(x) { michael@0: if (!arguments.length) return size; michael@0: size = x; michael@0: return force; michael@0: }; michael@0: force.linkDistance = function(x) { michael@0: if (!arguments.length) return linkDistance; michael@0: linkDistance = typeof x === "function" ? x : +x; michael@0: return force; michael@0: }; michael@0: force.distance = force.linkDistance; michael@0: force.linkStrength = function(x) { michael@0: if (!arguments.length) return linkStrength; michael@0: linkStrength = typeof x === "function" ? x : +x; michael@0: return force; michael@0: }; michael@0: force.friction = function(x) { michael@0: if (!arguments.length) return friction; michael@0: friction = +x; michael@0: return force; michael@0: }; michael@0: force.charge = function(x) { michael@0: if (!arguments.length) return charge; michael@0: charge = typeof x === "function" ? x : +x; michael@0: return force; michael@0: }; michael@0: force.chargeDistance = function(x) { michael@0: if (!arguments.length) return Math.sqrt(chargeDistance2); michael@0: chargeDistance2 = x * x; michael@0: return force; michael@0: }; michael@0: force.gravity = function(x) { michael@0: if (!arguments.length) return gravity; michael@0: gravity = +x; michael@0: return force; michael@0: }; michael@0: force.theta = function(x) { michael@0: if (!arguments.length) return Math.sqrt(theta2); michael@0: theta2 = x * x; michael@0: return force; michael@0: }; michael@0: force.alpha = function(x) { michael@0: if (!arguments.length) return alpha; michael@0: x = +x; michael@0: if (alpha) { michael@0: if (x > 0) alpha = x; else alpha = 0; michael@0: } else if (x > 0) { michael@0: event.start({ michael@0: type: "start", michael@0: alpha: alpha = x michael@0: }); michael@0: d3.timer(force.tick); michael@0: } michael@0: return force; michael@0: }; michael@0: force.start = function() { michael@0: var i, n = nodes.length, m = links.length, w = size[0], h = size[1], neighbors, o; michael@0: for (i = 0; i < n; ++i) { michael@0: (o = nodes[i]).index = i; michael@0: o.weight = 0; michael@0: } michael@0: for (i = 0; i < m; ++i) { michael@0: o = links[i]; michael@0: if (typeof o.source == "number") o.source = nodes[o.source]; michael@0: if (typeof o.target == "number") o.target = nodes[o.target]; michael@0: ++o.source.weight; michael@0: ++o.target.weight; michael@0: } michael@0: for (i = 0; i < n; ++i) { michael@0: o = nodes[i]; michael@0: if (isNaN(o.x)) o.x = position("x", w); michael@0: if (isNaN(o.y)) o.y = position("y", h); michael@0: if (isNaN(o.px)) o.px = o.x; michael@0: if (isNaN(o.py)) o.py = o.y; michael@0: } michael@0: distances = []; michael@0: if (typeof linkDistance === "function") for (i = 0; i < m; ++i) distances[i] = +linkDistance.call(this, links[i], i); else for (i = 0; i < m; ++i) distances[i] = linkDistance; michael@0: strengths = []; michael@0: if (typeof linkStrength === "function") for (i = 0; i < m; ++i) strengths[i] = +linkStrength.call(this, links[i], i); else for (i = 0; i < m; ++i) strengths[i] = linkStrength; michael@0: charges = []; michael@0: if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i); else for (i = 0; i < n; ++i) charges[i] = charge; michael@0: function position(dimension, size) { michael@0: if (!neighbors) { michael@0: neighbors = new Array(n); michael@0: for (j = 0; j < n; ++j) { michael@0: neighbors[j] = []; michael@0: } michael@0: for (j = 0; j < m; ++j) { michael@0: var o = links[j]; michael@0: neighbors[o.source.index].push(o.target); michael@0: neighbors[o.target.index].push(o.source); michael@0: } michael@0: } michael@0: var candidates = neighbors[i], j = -1, m = candidates.length, x; michael@0: while (++j < m) if (!isNaN(x = candidates[j][dimension])) return x; michael@0: return Math.random() * size; michael@0: } michael@0: return force.resume(); michael@0: }; michael@0: force.resume = function() { michael@0: return force.alpha(.1); michael@0: }; michael@0: force.stop = function() { michael@0: return force.alpha(0); michael@0: }; michael@0: force.drag = function() { michael@0: if (!drag) drag = d3.behavior.drag().origin(d3_identity).on("dragstart.force", d3_layout_forceDragstart).on("drag.force", dragmove).on("dragend.force", d3_layout_forceDragend); michael@0: if (!arguments.length) return drag; michael@0: this.on("mouseover.force", d3_layout_forceMouseover).on("mouseout.force", d3_layout_forceMouseout).call(drag); michael@0: }; michael@0: function dragmove(d) { michael@0: d.px = d3.event.x, d.py = d3.event.y; michael@0: force.resume(); michael@0: } michael@0: return d3.rebind(force, event, "on"); michael@0: }; michael@0: function d3_layout_forceDragstart(d) { michael@0: d.fixed |= 2; michael@0: } michael@0: function d3_layout_forceDragend(d) { michael@0: d.fixed &= ~6; michael@0: } michael@0: function d3_layout_forceMouseover(d) { michael@0: d.fixed |= 4; michael@0: d.px = d.x, d.py = d.y; michael@0: } michael@0: function d3_layout_forceMouseout(d) { michael@0: d.fixed &= ~4; michael@0: } michael@0: function d3_layout_forceAccumulate(quad, alpha, charges) { michael@0: var cx = 0, cy = 0; michael@0: quad.charge = 0; michael@0: if (!quad.leaf) { michael@0: var nodes = quad.nodes, n = nodes.length, i = -1, c; michael@0: while (++i < n) { michael@0: c = nodes[i]; michael@0: if (c == null) continue; michael@0: d3_layout_forceAccumulate(c, alpha, charges); michael@0: quad.charge += c.charge; michael@0: cx += c.charge * c.cx; michael@0: cy += c.charge * c.cy; michael@0: } michael@0: } michael@0: if (quad.point) { michael@0: if (!quad.leaf) { michael@0: quad.point.x += Math.random() - .5; michael@0: quad.point.y += Math.random() - .5; michael@0: } michael@0: var k = alpha * charges[quad.point.index]; michael@0: quad.charge += quad.pointCharge = k; michael@0: cx += k * quad.point.x; michael@0: cy += k * quad.point.y; michael@0: } michael@0: quad.cx = cx / quad.charge; michael@0: quad.cy = cy / quad.charge; michael@0: } michael@0: var d3_layout_forceLinkDistance = 20, d3_layout_forceLinkStrength = 1, d3_layout_forceChargeDistance2 = Infinity; michael@0: d3.layout.hierarchy = function() { michael@0: var sort = d3_layout_hierarchySort, children = d3_layout_hierarchyChildren, value = d3_layout_hierarchyValue; michael@0: function recurse(node, depth, nodes) { michael@0: var childs = children.call(hierarchy, node, depth); michael@0: node.depth = depth; michael@0: nodes.push(node); michael@0: if (childs && (n = childs.length)) { michael@0: var i = -1, n, c = node.children = new Array(n), v = 0, j = depth + 1, d; michael@0: while (++i < n) { michael@0: d = c[i] = recurse(childs[i], j, nodes); michael@0: d.parent = node; michael@0: v += d.value; michael@0: } michael@0: if (sort) c.sort(sort); michael@0: if (value) node.value = v; michael@0: } else { michael@0: delete node.children; michael@0: if (value) { michael@0: node.value = +value.call(hierarchy, node, depth) || 0; michael@0: } michael@0: } michael@0: return node; michael@0: } michael@0: function revalue(node, depth) { michael@0: var children = node.children, v = 0; michael@0: if (children && (n = children.length)) { michael@0: var i = -1, n, j = depth + 1; michael@0: while (++i < n) v += revalue(children[i], j); michael@0: } else if (value) { michael@0: v = +value.call(hierarchy, node, depth) || 0; michael@0: } michael@0: if (value) node.value = v; michael@0: return v; michael@0: } michael@0: function hierarchy(d) { michael@0: var nodes = []; michael@0: recurse(d, 0, nodes); michael@0: return nodes; michael@0: } michael@0: hierarchy.sort = function(x) { michael@0: if (!arguments.length) return sort; michael@0: sort = x; michael@0: return hierarchy; michael@0: }; michael@0: hierarchy.children = function(x) { michael@0: if (!arguments.length) return children; michael@0: children = x; michael@0: return hierarchy; michael@0: }; michael@0: hierarchy.value = function(x) { michael@0: if (!arguments.length) return value; michael@0: value = x; michael@0: return hierarchy; michael@0: }; michael@0: hierarchy.revalue = function(root) { michael@0: revalue(root, 0); michael@0: return root; michael@0: }; michael@0: return hierarchy; michael@0: }; michael@0: function d3_layout_hierarchyRebind(object, hierarchy) { michael@0: d3.rebind(object, hierarchy, "sort", "children", "value"); michael@0: object.nodes = object; michael@0: object.links = d3_layout_hierarchyLinks; michael@0: return object; michael@0: } michael@0: function d3_layout_hierarchyChildren(d) { michael@0: return d.children; michael@0: } michael@0: function d3_layout_hierarchyValue(d) { michael@0: return d.value; michael@0: } michael@0: function d3_layout_hierarchySort(a, b) { michael@0: return b.value - a.value; michael@0: } michael@0: function d3_layout_hierarchyLinks(nodes) { michael@0: return d3.merge(nodes.map(function(parent) { michael@0: return (parent.children || []).map(function(child) { michael@0: return { michael@0: source: parent, michael@0: target: child michael@0: }; michael@0: }); michael@0: })); michael@0: } michael@0: d3.layout.partition = function() { michael@0: var hierarchy = d3.layout.hierarchy(), size = [ 1, 1 ]; michael@0: function position(node, x, dx, dy) { michael@0: var children = node.children; michael@0: node.x = x; michael@0: node.y = node.depth * dy; michael@0: node.dx = dx; michael@0: node.dy = dy; michael@0: if (children && (n = children.length)) { michael@0: var i = -1, n, c, d; michael@0: dx = node.value ? dx / node.value : 0; michael@0: while (++i < n) { michael@0: position(c = children[i], x, d = c.value * dx, dy); michael@0: x += d; michael@0: } michael@0: } michael@0: } michael@0: function depth(node) { michael@0: var children = node.children, d = 0; michael@0: if (children && (n = children.length)) { michael@0: var i = -1, n; michael@0: while (++i < n) d = Math.max(d, depth(children[i])); michael@0: } michael@0: return 1 + d; michael@0: } michael@0: function partition(d, i) { michael@0: var nodes = hierarchy.call(this, d, i); michael@0: position(nodes[0], 0, size[0], size[1] / depth(nodes[0])); michael@0: return nodes; michael@0: } michael@0: partition.size = function(x) { michael@0: if (!arguments.length) return size; michael@0: size = x; michael@0: return partition; michael@0: }; michael@0: return d3_layout_hierarchyRebind(partition, hierarchy); michael@0: }; michael@0: d3.layout.pie = function() { michael@0: var value = Number, sort = d3_layout_pieSortByValue, startAngle = 0, endAngle = τ; michael@0: function pie(data) { michael@0: var values = data.map(function(d, i) { michael@0: return +value.call(pie, d, i); michael@0: }); michael@0: var a = +(typeof startAngle === "function" ? startAngle.apply(this, arguments) : startAngle); michael@0: var k = ((typeof endAngle === "function" ? endAngle.apply(this, arguments) : endAngle) - a) / d3.sum(values); michael@0: var index = d3.range(data.length); michael@0: if (sort != null) index.sort(sort === d3_layout_pieSortByValue ? function(i, j) { michael@0: return values[j] - values[i]; michael@0: } : function(i, j) { michael@0: return sort(data[i], data[j]); michael@0: }); michael@0: var arcs = []; michael@0: index.forEach(function(i) { michael@0: var d; michael@0: arcs[i] = { michael@0: data: data[i], michael@0: value: d = values[i], michael@0: startAngle: a, michael@0: endAngle: a += d * k michael@0: }; michael@0: }); michael@0: return arcs; michael@0: } michael@0: pie.value = function(x) { michael@0: if (!arguments.length) return value; michael@0: value = x; michael@0: return pie; michael@0: }; michael@0: pie.sort = function(x) { michael@0: if (!arguments.length) return sort; michael@0: sort = x; michael@0: return pie; michael@0: }; michael@0: pie.startAngle = function(x) { michael@0: if (!arguments.length) return startAngle; michael@0: startAngle = x; michael@0: return pie; michael@0: }; michael@0: pie.endAngle = function(x) { michael@0: if (!arguments.length) return endAngle; michael@0: endAngle = x; michael@0: return pie; michael@0: }; michael@0: return pie; michael@0: }; michael@0: var d3_layout_pieSortByValue = {}; michael@0: d3.layout.stack = function() { michael@0: var values = d3_identity, order = d3_layout_stackOrderDefault, offset = d3_layout_stackOffsetZero, out = d3_layout_stackOut, x = d3_layout_stackX, y = d3_layout_stackY; michael@0: function stack(data, index) { michael@0: var series = data.map(function(d, i) { michael@0: return values.call(stack, d, i); michael@0: }); michael@0: var points = series.map(function(d) { michael@0: return d.map(function(v, i) { michael@0: return [ x.call(stack, v, i), y.call(stack, v, i) ]; michael@0: }); michael@0: }); michael@0: var orders = order.call(stack, points, index); michael@0: series = d3.permute(series, orders); michael@0: points = d3.permute(points, orders); michael@0: var offsets = offset.call(stack, points, index); michael@0: var n = series.length, m = series[0].length, i, j, o; michael@0: for (j = 0; j < m; ++j) { michael@0: out.call(stack, series[0][j], o = offsets[j], points[0][j][1]); michael@0: for (i = 1; i < n; ++i) { michael@0: out.call(stack, series[i][j], o += points[i - 1][j][1], points[i][j][1]); michael@0: } michael@0: } michael@0: return data; michael@0: } michael@0: stack.values = function(x) { michael@0: if (!arguments.length) return values; michael@0: values = x; michael@0: return stack; michael@0: }; michael@0: stack.order = function(x) { michael@0: if (!arguments.length) return order; michael@0: order = typeof x === "function" ? x : d3_layout_stackOrders.get(x) || d3_layout_stackOrderDefault; michael@0: return stack; michael@0: }; michael@0: stack.offset = function(x) { michael@0: if (!arguments.length) return offset; michael@0: offset = typeof x === "function" ? x : d3_layout_stackOffsets.get(x) || d3_layout_stackOffsetZero; michael@0: return stack; michael@0: }; michael@0: stack.x = function(z) { michael@0: if (!arguments.length) return x; michael@0: x = z; michael@0: return stack; michael@0: }; michael@0: stack.y = function(z) { michael@0: if (!arguments.length) return y; michael@0: y = z; michael@0: return stack; michael@0: }; michael@0: stack.out = function(z) { michael@0: if (!arguments.length) return out; michael@0: out = z; michael@0: return stack; michael@0: }; michael@0: return stack; michael@0: }; michael@0: function d3_layout_stackX(d) { michael@0: return d.x; michael@0: } michael@0: function d3_layout_stackY(d) { michael@0: return d.y; michael@0: } michael@0: function d3_layout_stackOut(d, y0, y) { michael@0: d.y0 = y0; michael@0: d.y = y; michael@0: } michael@0: var d3_layout_stackOrders = d3.map({ michael@0: "inside-out": function(data) { michael@0: var n = data.length, i, j, max = data.map(d3_layout_stackMaxIndex), sums = data.map(d3_layout_stackReduceSum), index = d3.range(n).sort(function(a, b) { michael@0: return max[a] - max[b]; michael@0: }), top = 0, bottom = 0, tops = [], bottoms = []; michael@0: for (i = 0; i < n; ++i) { michael@0: j = index[i]; michael@0: if (top < bottom) { michael@0: top += sums[j]; michael@0: tops.push(j); michael@0: } else { michael@0: bottom += sums[j]; michael@0: bottoms.push(j); michael@0: } michael@0: } michael@0: return bottoms.reverse().concat(tops); michael@0: }, michael@0: reverse: function(data) { michael@0: return d3.range(data.length).reverse(); michael@0: }, michael@0: "default": d3_layout_stackOrderDefault michael@0: }); michael@0: var d3_layout_stackOffsets = d3.map({ michael@0: silhouette: function(data) { michael@0: var n = data.length, m = data[0].length, sums = [], max = 0, i, j, o, y0 = []; michael@0: for (j = 0; j < m; ++j) { michael@0: for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; michael@0: if (o > max) max = o; michael@0: sums.push(o); michael@0: } michael@0: for (j = 0; j < m; ++j) { michael@0: y0[j] = (max - sums[j]) / 2; michael@0: } michael@0: return y0; michael@0: }, michael@0: wiggle: function(data) { michael@0: var n = data.length, x = data[0], m = x.length, i, j, k, s1, s2, s3, dx, o, o0, y0 = []; michael@0: y0[0] = o = o0 = 0; michael@0: for (j = 1; j < m; ++j) { michael@0: for (i = 0, s1 = 0; i < n; ++i) s1 += data[i][j][1]; michael@0: for (i = 0, s2 = 0, dx = x[j][0] - x[j - 1][0]; i < n; ++i) { michael@0: for (k = 0, s3 = (data[i][j][1] - data[i][j - 1][1]) / (2 * dx); k < i; ++k) { michael@0: s3 += (data[k][j][1] - data[k][j - 1][1]) / dx; michael@0: } michael@0: s2 += s3 * data[i][j][1]; michael@0: } michael@0: y0[j] = o -= s1 ? s2 / s1 * dx : 0; michael@0: if (o < o0) o0 = o; michael@0: } michael@0: for (j = 0; j < m; ++j) y0[j] -= o0; michael@0: return y0; michael@0: }, michael@0: expand: function(data) { michael@0: var n = data.length, m = data[0].length, k = 1 / n, i, j, o, y0 = []; michael@0: for (j = 0; j < m; ++j) { michael@0: for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; michael@0: if (o) for (i = 0; i < n; i++) data[i][j][1] /= o; else for (i = 0; i < n; i++) data[i][j][1] = k; michael@0: } michael@0: for (j = 0; j < m; ++j) y0[j] = 0; michael@0: return y0; michael@0: }, michael@0: zero: d3_layout_stackOffsetZero michael@0: }); michael@0: function d3_layout_stackOrderDefault(data) { michael@0: return d3.range(data.length); michael@0: } michael@0: function d3_layout_stackOffsetZero(data) { michael@0: var j = -1, m = data[0].length, y0 = []; michael@0: while (++j < m) y0[j] = 0; michael@0: return y0; michael@0: } michael@0: function d3_layout_stackMaxIndex(array) { michael@0: var i = 1, j = 0, v = array[0][1], k, n = array.length; michael@0: for (;i < n; ++i) { michael@0: if ((k = array[i][1]) > v) { michael@0: j = i; michael@0: v = k; michael@0: } michael@0: } michael@0: return j; michael@0: } michael@0: function d3_layout_stackReduceSum(d) { michael@0: return d.reduce(d3_layout_stackSum, 0); michael@0: } michael@0: function d3_layout_stackSum(p, d) { michael@0: return p + d[1]; michael@0: } michael@0: d3.layout.histogram = function() { michael@0: var frequency = true, valuer = Number, ranger = d3_layout_histogramRange, binner = d3_layout_histogramBinSturges; michael@0: function histogram(data, i) { michael@0: var bins = [], values = data.map(valuer, this), range = ranger.call(this, values, i), thresholds = binner.call(this, range, values, i), bin, i = -1, n = values.length, m = thresholds.length - 1, k = frequency ? 1 : 1 / n, x; michael@0: while (++i < m) { michael@0: bin = bins[i] = []; michael@0: bin.dx = thresholds[i + 1] - (bin.x = thresholds[i]); michael@0: bin.y = 0; michael@0: } michael@0: if (m > 0) { michael@0: i = -1; michael@0: while (++i < n) { michael@0: x = values[i]; michael@0: if (x >= range[0] && x <= range[1]) { michael@0: bin = bins[d3.bisect(thresholds, x, 1, m) - 1]; michael@0: bin.y += k; michael@0: bin.push(data[i]); michael@0: } michael@0: } michael@0: } michael@0: return bins; michael@0: } michael@0: histogram.value = function(x) { michael@0: if (!arguments.length) return valuer; michael@0: valuer = x; michael@0: return histogram; michael@0: }; michael@0: histogram.range = function(x) { michael@0: if (!arguments.length) return ranger; michael@0: ranger = d3_functor(x); michael@0: return histogram; michael@0: }; michael@0: histogram.bins = function(x) { michael@0: if (!arguments.length) return binner; michael@0: binner = typeof x === "number" ? function(range) { michael@0: return d3_layout_histogramBinFixed(range, x); michael@0: } : d3_functor(x); michael@0: return histogram; michael@0: }; michael@0: histogram.frequency = function(x) { michael@0: if (!arguments.length) return frequency; michael@0: frequency = !!x; michael@0: return histogram; michael@0: }; michael@0: return histogram; michael@0: }; michael@0: function d3_layout_histogramBinSturges(range, values) { michael@0: return d3_layout_histogramBinFixed(range, Math.ceil(Math.log(values.length) / Math.LN2 + 1)); michael@0: } michael@0: function d3_layout_histogramBinFixed(range, n) { michael@0: var x = -1, b = +range[0], m = (range[1] - b) / n, f = []; michael@0: while (++x <= n) f[x] = m * x + b; michael@0: return f; michael@0: } michael@0: function d3_layout_histogramRange(values) { michael@0: return [ d3.min(values), d3.max(values) ]; michael@0: } michael@0: d3.layout.tree = function() { michael@0: var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false; michael@0: function tree(d, i) { michael@0: var nodes = hierarchy.call(this, d, i), root = nodes[0]; michael@0: function firstWalk(node, previousSibling) { michael@0: var children = node.children, layout = node._tree; michael@0: if (children && (n = children.length)) { michael@0: var n, firstChild = children[0], previousChild, ancestor = firstChild, child, i = -1; michael@0: while (++i < n) { michael@0: child = children[i]; michael@0: firstWalk(child, previousChild); michael@0: ancestor = apportion(child, previousChild, ancestor); michael@0: previousChild = child; michael@0: } michael@0: d3_layout_treeShift(node); michael@0: var midpoint = .5 * (firstChild._tree.prelim + child._tree.prelim); michael@0: if (previousSibling) { michael@0: layout.prelim = previousSibling._tree.prelim + separation(node, previousSibling); michael@0: layout.mod = layout.prelim - midpoint; michael@0: } else { michael@0: layout.prelim = midpoint; michael@0: } michael@0: } else { michael@0: if (previousSibling) { michael@0: layout.prelim = previousSibling._tree.prelim + separation(node, previousSibling); michael@0: } michael@0: } michael@0: } michael@0: function secondWalk(node, x) { michael@0: node.x = node._tree.prelim + x; michael@0: var children = node.children; michael@0: if (children && (n = children.length)) { michael@0: var i = -1, n; michael@0: x += node._tree.mod; michael@0: while (++i < n) { michael@0: secondWalk(children[i], x); michael@0: } michael@0: } michael@0: } michael@0: function apportion(node, previousSibling, ancestor) { michael@0: if (previousSibling) { michael@0: var vip = node, vop = node, vim = previousSibling, vom = node.parent.children[0], sip = vip._tree.mod, sop = vop._tree.mod, sim = vim._tree.mod, som = vom._tree.mod, shift; michael@0: while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) { michael@0: vom = d3_layout_treeLeft(vom); michael@0: vop = d3_layout_treeRight(vop); michael@0: vop._tree.ancestor = node; michael@0: shift = vim._tree.prelim + sim - vip._tree.prelim - sip + separation(vim, vip); michael@0: if (shift > 0) { michael@0: d3_layout_treeMove(d3_layout_treeAncestor(vim, node, ancestor), node, shift); michael@0: sip += shift; michael@0: sop += shift; michael@0: } michael@0: sim += vim._tree.mod; michael@0: sip += vip._tree.mod; michael@0: som += vom._tree.mod; michael@0: sop += vop._tree.mod; michael@0: } michael@0: if (vim && !d3_layout_treeRight(vop)) { michael@0: vop._tree.thread = vim; michael@0: vop._tree.mod += sim - sop; michael@0: } michael@0: if (vip && !d3_layout_treeLeft(vom)) { michael@0: vom._tree.thread = vip; michael@0: vom._tree.mod += sip - som; michael@0: ancestor = node; michael@0: } michael@0: } michael@0: return ancestor; michael@0: } michael@0: d3_layout_treeVisitAfter(root, function(node, previousSibling) { michael@0: node._tree = { michael@0: ancestor: node, michael@0: prelim: 0, michael@0: mod: 0, michael@0: change: 0, michael@0: shift: 0, michael@0: number: previousSibling ? previousSibling._tree.number + 1 : 0 michael@0: }; michael@0: }); michael@0: firstWalk(root); michael@0: secondWalk(root, -root._tree.prelim); michael@0: var left = d3_layout_treeSearch(root, d3_layout_treeLeftmost), right = d3_layout_treeSearch(root, d3_layout_treeRightmost), deep = d3_layout_treeSearch(root, d3_layout_treeDeepest), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2, y1 = deep.depth || 1; michael@0: d3_layout_treeVisitAfter(root, nodeSize ? function(node) { michael@0: node.x *= size[0]; michael@0: node.y = node.depth * size[1]; michael@0: delete node._tree; michael@0: } : function(node) { michael@0: node.x = (node.x - x0) / (x1 - x0) * size[0]; michael@0: node.y = node.depth / y1 * size[1]; michael@0: delete node._tree; michael@0: }); michael@0: return nodes; michael@0: } michael@0: tree.separation = function(x) { michael@0: if (!arguments.length) return separation; michael@0: separation = x; michael@0: return tree; michael@0: }; michael@0: tree.size = function(x) { michael@0: if (!arguments.length) return nodeSize ? null : size; michael@0: nodeSize = (size = x) == null; michael@0: return tree; michael@0: }; michael@0: tree.nodeSize = function(x) { michael@0: if (!arguments.length) return nodeSize ? size : null; michael@0: nodeSize = (size = x) != null; michael@0: return tree; michael@0: }; michael@0: return d3_layout_hierarchyRebind(tree, hierarchy); michael@0: }; michael@0: function d3_layout_treeSeparation(a, b) { michael@0: return a.parent == b.parent ? 1 : 2; michael@0: } michael@0: function d3_layout_treeLeft(node) { michael@0: var children = node.children; michael@0: return children && children.length ? children[0] : node._tree.thread; michael@0: } michael@0: function d3_layout_treeRight(node) { michael@0: var children = node.children, n; michael@0: return children && (n = children.length) ? children[n - 1] : node._tree.thread; michael@0: } michael@0: function d3_layout_treeSearch(node, compare) { michael@0: var children = node.children; michael@0: if (children && (n = children.length)) { michael@0: var child, n, i = -1; michael@0: while (++i < n) { michael@0: if (compare(child = d3_layout_treeSearch(children[i], compare), node) > 0) { michael@0: node = child; michael@0: } michael@0: } michael@0: } michael@0: return node; michael@0: } michael@0: function d3_layout_treeRightmost(a, b) { michael@0: return a.x - b.x; michael@0: } michael@0: function d3_layout_treeLeftmost(a, b) { michael@0: return b.x - a.x; michael@0: } michael@0: function d3_layout_treeDeepest(a, b) { michael@0: return a.depth - b.depth; michael@0: } michael@0: function d3_layout_treeVisitAfter(node, callback) { michael@0: function visit(node, previousSibling) { michael@0: var children = node.children; michael@0: if (children && (n = children.length)) { michael@0: var child, previousChild = null, i = -1, n; michael@0: while (++i < n) { michael@0: child = children[i]; michael@0: visit(child, previousChild); michael@0: previousChild = child; michael@0: } michael@0: } michael@0: callback(node, previousSibling); michael@0: } michael@0: visit(node, null); michael@0: } michael@0: function d3_layout_treeShift(node) { michael@0: var shift = 0, change = 0, children = node.children, i = children.length, child; michael@0: while (--i >= 0) { michael@0: child = children[i]._tree; michael@0: child.prelim += shift; michael@0: child.mod += shift; michael@0: shift += child.shift + (change += child.change); michael@0: } michael@0: } michael@0: function d3_layout_treeMove(ancestor, node, shift) { michael@0: ancestor = ancestor._tree; michael@0: node = node._tree; michael@0: var change = shift / (node.number - ancestor.number); michael@0: ancestor.change += change; michael@0: node.change -= change; michael@0: node.shift += shift; michael@0: node.prelim += shift; michael@0: node.mod += shift; michael@0: } michael@0: function d3_layout_treeAncestor(vim, node, ancestor) { michael@0: return vim._tree.ancestor.parent == node.parent ? vim._tree.ancestor : ancestor; michael@0: } michael@0: d3.layout.pack = function() { michael@0: var hierarchy = d3.layout.hierarchy().sort(d3_layout_packSort), padding = 0, size = [ 1, 1 ], radius; michael@0: function pack(d, i) { michael@0: var nodes = hierarchy.call(this, d, i), root = nodes[0], w = size[0], h = size[1], r = radius == null ? Math.sqrt : typeof radius === "function" ? radius : function() { michael@0: return radius; michael@0: }; michael@0: root.x = root.y = 0; michael@0: d3_layout_treeVisitAfter(root, function(d) { michael@0: d.r = +r(d.value); michael@0: }); michael@0: d3_layout_treeVisitAfter(root, d3_layout_packSiblings); michael@0: if (padding) { michael@0: var dr = padding * (radius ? 1 : Math.max(2 * root.r / w, 2 * root.r / h)) / 2; michael@0: d3_layout_treeVisitAfter(root, function(d) { michael@0: d.r += dr; michael@0: }); michael@0: d3_layout_treeVisitAfter(root, d3_layout_packSiblings); michael@0: d3_layout_treeVisitAfter(root, function(d) { michael@0: d.r -= dr; michael@0: }); michael@0: } michael@0: d3_layout_packTransform(root, w / 2, h / 2, radius ? 1 : 1 / Math.max(2 * root.r / w, 2 * root.r / h)); michael@0: return nodes; michael@0: } michael@0: pack.size = function(_) { michael@0: if (!arguments.length) return size; michael@0: size = _; michael@0: return pack; michael@0: }; michael@0: pack.radius = function(_) { michael@0: if (!arguments.length) return radius; michael@0: radius = _ == null || typeof _ === "function" ? _ : +_; michael@0: return pack; michael@0: }; michael@0: pack.padding = function(_) { michael@0: if (!arguments.length) return padding; michael@0: padding = +_; michael@0: return pack; michael@0: }; michael@0: return d3_layout_hierarchyRebind(pack, hierarchy); michael@0: }; michael@0: function d3_layout_packSort(a, b) { michael@0: return a.value - b.value; michael@0: } michael@0: function d3_layout_packInsert(a, b) { michael@0: var c = a._pack_next; michael@0: a._pack_next = b; michael@0: b._pack_prev = a; michael@0: b._pack_next = c; michael@0: c._pack_prev = b; michael@0: } michael@0: function d3_layout_packSplice(a, b) { michael@0: a._pack_next = b; michael@0: b._pack_prev = a; michael@0: } michael@0: function d3_layout_packIntersects(a, b) { michael@0: var dx = b.x - a.x, dy = b.y - a.y, dr = a.r + b.r; michael@0: return .999 * dr * dr > dx * dx + dy * dy; michael@0: } michael@0: function d3_layout_packSiblings(node) { michael@0: if (!(nodes = node.children) || !(n = nodes.length)) return; michael@0: var nodes, xMin = Infinity, xMax = -Infinity, yMin = Infinity, yMax = -Infinity, a, b, c, i, j, k, n; michael@0: function bound(node) { michael@0: xMin = Math.min(node.x - node.r, xMin); michael@0: xMax = Math.max(node.x + node.r, xMax); michael@0: yMin = Math.min(node.y - node.r, yMin); michael@0: yMax = Math.max(node.y + node.r, yMax); michael@0: } michael@0: nodes.forEach(d3_layout_packLink); michael@0: a = nodes[0]; michael@0: a.x = -a.r; michael@0: a.y = 0; michael@0: bound(a); michael@0: if (n > 1) { michael@0: b = nodes[1]; michael@0: b.x = b.r; michael@0: b.y = 0; michael@0: bound(b); michael@0: if (n > 2) { michael@0: c = nodes[2]; michael@0: d3_layout_packPlace(a, b, c); michael@0: bound(c); michael@0: d3_layout_packInsert(a, c); michael@0: a._pack_prev = c; michael@0: d3_layout_packInsert(c, b); michael@0: b = a._pack_next; michael@0: for (i = 3; i < n; i++) { michael@0: d3_layout_packPlace(a, b, c = nodes[i]); michael@0: var isect = 0, s1 = 1, s2 = 1; michael@0: for (j = b._pack_next; j !== b; j = j._pack_next, s1++) { michael@0: if (d3_layout_packIntersects(j, c)) { michael@0: isect = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (isect == 1) { michael@0: for (k = a._pack_prev; k !== j._pack_prev; k = k._pack_prev, s2++) { michael@0: if (d3_layout_packIntersects(k, c)) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (isect) { michael@0: if (s1 < s2 || s1 == s2 && b.r < a.r) d3_layout_packSplice(a, b = j); else d3_layout_packSplice(a = k, b); michael@0: i--; michael@0: } else { michael@0: d3_layout_packInsert(a, c); michael@0: b = c; michael@0: bound(c); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: var cx = (xMin + xMax) / 2, cy = (yMin + yMax) / 2, cr = 0; michael@0: for (i = 0; i < n; i++) { michael@0: c = nodes[i]; michael@0: c.x -= cx; michael@0: c.y -= cy; michael@0: cr = Math.max(cr, c.r + Math.sqrt(c.x * c.x + c.y * c.y)); michael@0: } michael@0: node.r = cr; michael@0: nodes.forEach(d3_layout_packUnlink); michael@0: } michael@0: function d3_layout_packLink(node) { michael@0: node._pack_next = node._pack_prev = node; michael@0: } michael@0: function d3_layout_packUnlink(node) { michael@0: delete node._pack_next; michael@0: delete node._pack_prev; michael@0: } michael@0: function d3_layout_packTransform(node, x, y, k) { michael@0: var children = node.children; michael@0: node.x = x += k * node.x; michael@0: node.y = y += k * node.y; michael@0: node.r *= k; michael@0: if (children) { michael@0: var i = -1, n = children.length; michael@0: while (++i < n) d3_layout_packTransform(children[i], x, y, k); michael@0: } michael@0: } michael@0: function d3_layout_packPlace(a, b, c) { michael@0: var db = a.r + c.r, dx = b.x - a.x, dy = b.y - a.y; michael@0: if (db && (dx || dy)) { michael@0: var da = b.r + c.r, dc = dx * dx + dy * dy; michael@0: da *= da; michael@0: db *= db; michael@0: var x = .5 + (db - da) / (2 * dc), y = Math.sqrt(Math.max(0, 2 * da * (db + dc) - (db -= dc) * db - da * da)) / (2 * dc); michael@0: c.x = a.x + x * dx + y * dy; michael@0: c.y = a.y + x * dy - y * dx; michael@0: } else { michael@0: c.x = a.x + db; michael@0: c.y = a.y; michael@0: } michael@0: } michael@0: d3.layout.cluster = function() { michael@0: var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false; michael@0: function cluster(d, i) { michael@0: var nodes = hierarchy.call(this, d, i), root = nodes[0], previousNode, x = 0; michael@0: d3_layout_treeVisitAfter(root, function(node) { michael@0: var children = node.children; michael@0: if (children && children.length) { michael@0: node.x = d3_layout_clusterX(children); michael@0: node.y = d3_layout_clusterY(children); michael@0: } else { michael@0: node.x = previousNode ? x += separation(node, previousNode) : 0; michael@0: node.y = 0; michael@0: previousNode = node; michael@0: } michael@0: }); michael@0: var left = d3_layout_clusterLeft(root), right = d3_layout_clusterRight(root), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2; michael@0: d3_layout_treeVisitAfter(root, nodeSize ? function(node) { michael@0: node.x = (node.x - root.x) * size[0]; michael@0: node.y = (root.y - node.y) * size[1]; michael@0: } : function(node) { michael@0: node.x = (node.x - x0) / (x1 - x0) * size[0]; michael@0: node.y = (1 - (root.y ? node.y / root.y : 1)) * size[1]; michael@0: }); michael@0: return nodes; michael@0: } michael@0: cluster.separation = function(x) { michael@0: if (!arguments.length) return separation; michael@0: separation = x; michael@0: return cluster; michael@0: }; michael@0: cluster.size = function(x) { michael@0: if (!arguments.length) return nodeSize ? null : size; michael@0: nodeSize = (size = x) == null; michael@0: return cluster; michael@0: }; michael@0: cluster.nodeSize = function(x) { michael@0: if (!arguments.length) return nodeSize ? size : null; michael@0: nodeSize = (size = x) != null; michael@0: return cluster; michael@0: }; michael@0: return d3_layout_hierarchyRebind(cluster, hierarchy); michael@0: }; michael@0: function d3_layout_clusterY(children) { michael@0: return 1 + d3.max(children, function(child) { michael@0: return child.y; michael@0: }); michael@0: } michael@0: function d3_layout_clusterX(children) { michael@0: return children.reduce(function(x, child) { michael@0: return x + child.x; michael@0: }, 0) / children.length; michael@0: } michael@0: function d3_layout_clusterLeft(node) { michael@0: var children = node.children; michael@0: return children && children.length ? d3_layout_clusterLeft(children[0]) : node; michael@0: } michael@0: function d3_layout_clusterRight(node) { michael@0: var children = node.children, n; michael@0: return children && (n = children.length) ? d3_layout_clusterRight(children[n - 1]) : node; michael@0: } michael@0: d3.layout.treemap = function() { michael@0: var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [ 1, 1 ], padding = null, pad = d3_layout_treemapPadNull, sticky = false, stickies, mode = "squarify", ratio = .5 * (1 + Math.sqrt(5)); michael@0: function scale(children, k) { michael@0: var i = -1, n = children.length, child, area; michael@0: while (++i < n) { michael@0: area = (child = children[i]).value * (k < 0 ? 0 : k); michael@0: child.area = isNaN(area) || area <= 0 ? 0 : area; michael@0: } michael@0: } michael@0: function squarify(node) { michael@0: var children = node.children; michael@0: if (children && children.length) { michael@0: var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = mode === "slice" ? rect.dx : mode === "dice" ? rect.dy : mode === "slice-dice" ? node.depth & 1 ? rect.dy : rect.dx : Math.min(rect.dx, rect.dy), n; michael@0: scale(remaining, rect.dx * rect.dy / node.value); michael@0: row.area = 0; michael@0: while ((n = remaining.length) > 0) { michael@0: row.push(child = remaining[n - 1]); michael@0: row.area += child.area; michael@0: if (mode !== "squarify" || (score = worst(row, u)) <= best) { michael@0: remaining.pop(); michael@0: best = score; michael@0: } else { michael@0: row.area -= row.pop().area; michael@0: position(row, u, rect, false); michael@0: u = Math.min(rect.dx, rect.dy); michael@0: row.length = row.area = 0; michael@0: best = Infinity; michael@0: } michael@0: } michael@0: if (row.length) { michael@0: position(row, u, rect, true); michael@0: row.length = row.area = 0; michael@0: } michael@0: children.forEach(squarify); michael@0: } michael@0: } michael@0: function stickify(node) { michael@0: var children = node.children; michael@0: if (children && children.length) { michael@0: var rect = pad(node), remaining = children.slice(), child, row = []; michael@0: scale(remaining, rect.dx * rect.dy / node.value); michael@0: row.area = 0; michael@0: while (child = remaining.pop()) { michael@0: row.push(child); michael@0: row.area += child.area; michael@0: if (child.z != null) { michael@0: position(row, child.z ? rect.dx : rect.dy, rect, !remaining.length); michael@0: row.length = row.area = 0; michael@0: } michael@0: } michael@0: children.forEach(stickify); michael@0: } michael@0: } michael@0: function worst(row, u) { michael@0: var s = row.area, r, rmax = 0, rmin = Infinity, i = -1, n = row.length; michael@0: while (++i < n) { michael@0: if (!(r = row[i].area)) continue; michael@0: if (r < rmin) rmin = r; michael@0: if (r > rmax) rmax = r; michael@0: } michael@0: s *= s; michael@0: u *= u; michael@0: return s ? Math.max(u * rmax * ratio / s, s / (u * rmin * ratio)) : Infinity; michael@0: } michael@0: function position(row, u, rect, flush) { michael@0: var i = -1, n = row.length, x = rect.x, y = rect.y, v = u ? round(row.area / u) : 0, o; michael@0: if (u == rect.dx) { michael@0: if (flush || v > rect.dy) v = rect.dy; michael@0: while (++i < n) { michael@0: o = row[i]; michael@0: o.x = x; michael@0: o.y = y; michael@0: o.dy = v; michael@0: x += o.dx = Math.min(rect.x + rect.dx - x, v ? round(o.area / v) : 0); michael@0: } michael@0: o.z = true; michael@0: o.dx += rect.x + rect.dx - x; michael@0: rect.y += v; michael@0: rect.dy -= v; michael@0: } else { michael@0: if (flush || v > rect.dx) v = rect.dx; michael@0: while (++i < n) { michael@0: o = row[i]; michael@0: o.x = x; michael@0: o.y = y; michael@0: o.dx = v; michael@0: y += o.dy = Math.min(rect.y + rect.dy - y, v ? round(o.area / v) : 0); michael@0: } michael@0: o.z = false; michael@0: o.dy += rect.y + rect.dy - y; michael@0: rect.x += v; michael@0: rect.dx -= v; michael@0: } michael@0: } michael@0: function treemap(d) { michael@0: var nodes = stickies || hierarchy(d), root = nodes[0]; michael@0: root.x = 0; michael@0: root.y = 0; michael@0: root.dx = size[0]; michael@0: root.dy = size[1]; michael@0: if (stickies) hierarchy.revalue(root); michael@0: scale([ root ], root.dx * root.dy / root.value); michael@0: (stickies ? stickify : squarify)(root); michael@0: if (sticky) stickies = nodes; michael@0: return nodes; michael@0: } michael@0: treemap.size = function(x) { michael@0: if (!arguments.length) return size; michael@0: size = x; michael@0: return treemap; michael@0: }; michael@0: treemap.padding = function(x) { michael@0: if (!arguments.length) return padding; michael@0: function padFunction(node) { michael@0: var p = x.call(treemap, node, node.depth); michael@0: return p == null ? d3_layout_treemapPadNull(node) : d3_layout_treemapPad(node, typeof p === "number" ? [ p, p, p, p ] : p); michael@0: } michael@0: function padConstant(node) { michael@0: return d3_layout_treemapPad(node, x); michael@0: } michael@0: var type; michael@0: pad = (padding = x) == null ? d3_layout_treemapPadNull : (type = typeof x) === "function" ? padFunction : type === "number" ? (x = [ x, x, x, x ], michael@0: padConstant) : padConstant; michael@0: return treemap; michael@0: }; michael@0: treemap.round = function(x) { michael@0: if (!arguments.length) return round != Number; michael@0: round = x ? Math.round : Number; michael@0: return treemap; michael@0: }; michael@0: treemap.sticky = function(x) { michael@0: if (!arguments.length) return sticky; michael@0: sticky = x; michael@0: stickies = null; michael@0: return treemap; michael@0: }; michael@0: treemap.ratio = function(x) { michael@0: if (!arguments.length) return ratio; michael@0: ratio = x; michael@0: return treemap; michael@0: }; michael@0: treemap.mode = function(x) { michael@0: if (!arguments.length) return mode; michael@0: mode = x + ""; michael@0: return treemap; michael@0: }; michael@0: return d3_layout_hierarchyRebind(treemap, hierarchy); michael@0: }; michael@0: function d3_layout_treemapPadNull(node) { michael@0: return { michael@0: x: node.x, michael@0: y: node.y, michael@0: dx: node.dx, michael@0: dy: node.dy michael@0: }; michael@0: } michael@0: function d3_layout_treemapPad(node, padding) { michael@0: var x = node.x + padding[3], y = node.y + padding[0], dx = node.dx - padding[1] - padding[3], dy = node.dy - padding[0] - padding[2]; michael@0: if (dx < 0) { michael@0: x += dx / 2; michael@0: dx = 0; michael@0: } michael@0: if (dy < 0) { michael@0: y += dy / 2; michael@0: dy = 0; michael@0: } michael@0: return { michael@0: x: x, michael@0: y: y, michael@0: dx: dx, michael@0: dy: dy michael@0: }; michael@0: } michael@0: d3.random = { michael@0: normal: function(µ, σ) { michael@0: var n = arguments.length; michael@0: if (n < 2) σ = 1; michael@0: if (n < 1) µ = 0; michael@0: return function() { michael@0: var x, y, r; michael@0: do { michael@0: x = Math.random() * 2 - 1; michael@0: y = Math.random() * 2 - 1; michael@0: r = x * x + y * y; michael@0: } while (!r || r > 1); michael@0: return µ + σ * x * Math.sqrt(-2 * Math.log(r) / r); michael@0: }; michael@0: }, michael@0: logNormal: function() { michael@0: var random = d3.random.normal.apply(d3, arguments); michael@0: return function() { michael@0: return Math.exp(random()); michael@0: }; michael@0: }, michael@0: bates: function(m) { michael@0: var random = d3.random.irwinHall(m); michael@0: return function() { michael@0: return random() / m; michael@0: }; michael@0: }, michael@0: irwinHall: function(m) { michael@0: return function() { michael@0: for (var s = 0, j = 0; j < m; j++) s += Math.random(); michael@0: return s; michael@0: }; michael@0: } michael@0: }; michael@0: d3.scale = {}; michael@0: function d3_scaleExtent(domain) { michael@0: var start = domain[0], stop = domain[domain.length - 1]; michael@0: return start < stop ? [ start, stop ] : [ stop, start ]; michael@0: } michael@0: function d3_scaleRange(scale) { michael@0: return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range()); michael@0: } michael@0: function d3_scale_bilinear(domain, range, uninterpolate, interpolate) { michael@0: var u = uninterpolate(domain[0], domain[1]), i = interpolate(range[0], range[1]); michael@0: return function(x) { michael@0: return i(u(x)); michael@0: }; michael@0: } michael@0: function d3_scale_nice(domain, nice) { michael@0: var i0 = 0, i1 = domain.length - 1, x0 = domain[i0], x1 = domain[i1], dx; michael@0: if (x1 < x0) { michael@0: dx = i0, i0 = i1, i1 = dx; michael@0: dx = x0, x0 = x1, x1 = dx; michael@0: } michael@0: domain[i0] = nice.floor(x0); michael@0: domain[i1] = nice.ceil(x1); michael@0: return domain; michael@0: } michael@0: function d3_scale_niceStep(step) { michael@0: return step ? { michael@0: floor: function(x) { michael@0: return Math.floor(x / step) * step; michael@0: }, michael@0: ceil: function(x) { michael@0: return Math.ceil(x / step) * step; michael@0: } michael@0: } : d3_scale_niceIdentity; michael@0: } michael@0: var d3_scale_niceIdentity = { michael@0: floor: d3_identity, michael@0: ceil: d3_identity michael@0: }; michael@0: function d3_scale_polylinear(domain, range, uninterpolate, interpolate) { michael@0: var u = [], i = [], j = 0, k = Math.min(domain.length, range.length) - 1; michael@0: if (domain[k] < domain[0]) { michael@0: domain = domain.slice().reverse(); michael@0: range = range.slice().reverse(); michael@0: } michael@0: while (++j <= k) { michael@0: u.push(uninterpolate(domain[j - 1], domain[j])); michael@0: i.push(interpolate(range[j - 1], range[j])); michael@0: } michael@0: return function(x) { michael@0: var j = d3.bisect(domain, x, 1, k) - 1; michael@0: return i[j](u[j](x)); michael@0: }; michael@0: } michael@0: d3.scale.linear = function() { michael@0: return d3_scale_linear([ 0, 1 ], [ 0, 1 ], d3_interpolate, false); michael@0: }; michael@0: function d3_scale_linear(domain, range, interpolate, clamp) { michael@0: var output, input; michael@0: function rescale() { michael@0: var linear = Math.min(domain.length, range.length) > 2 ? d3_scale_polylinear : d3_scale_bilinear, uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber; michael@0: output = linear(domain, range, uninterpolate, interpolate); michael@0: input = linear(range, domain, uninterpolate, d3_interpolate); michael@0: return scale; michael@0: } michael@0: function scale(x) { michael@0: return output(x); michael@0: } michael@0: scale.invert = function(y) { michael@0: return input(y); michael@0: }; michael@0: scale.domain = function(x) { michael@0: if (!arguments.length) return domain; michael@0: domain = x.map(Number); michael@0: return rescale(); michael@0: }; michael@0: scale.range = function(x) { michael@0: if (!arguments.length) return range; michael@0: range = x; michael@0: return rescale(); michael@0: }; michael@0: scale.rangeRound = function(x) { michael@0: return scale.range(x).interpolate(d3_interpolateRound); michael@0: }; michael@0: scale.clamp = function(x) { michael@0: if (!arguments.length) return clamp; michael@0: clamp = x; michael@0: return rescale(); michael@0: }; michael@0: scale.interpolate = function(x) { michael@0: if (!arguments.length) return interpolate; michael@0: interpolate = x; michael@0: return rescale(); michael@0: }; michael@0: scale.ticks = function(m) { michael@0: return d3_scale_linearTicks(domain, m); michael@0: }; michael@0: scale.tickFormat = function(m, format) { michael@0: return d3_scale_linearTickFormat(domain, m, format); michael@0: }; michael@0: scale.nice = function(m) { michael@0: d3_scale_linearNice(domain, m); michael@0: return rescale(); michael@0: }; michael@0: scale.copy = function() { michael@0: return d3_scale_linear(domain, range, interpolate, clamp); michael@0: }; michael@0: return rescale(); michael@0: } michael@0: function d3_scale_linearRebind(scale, linear) { michael@0: return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp"); michael@0: } michael@0: function d3_scale_linearNice(domain, m) { michael@0: return d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2])); michael@0: } michael@0: function d3_scale_linearTickRange(domain, m) { michael@0: if (m == null) m = 10; michael@0: var extent = d3_scaleExtent(domain), span = extent[1] - extent[0], step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)), err = m / span * step; michael@0: if (err <= .15) step *= 10; else if (err <= .35) step *= 5; else if (err <= .75) step *= 2; michael@0: extent[0] = Math.ceil(extent[0] / step) * step; michael@0: extent[1] = Math.floor(extent[1] / step) * step + step * .5; michael@0: extent[2] = step; michael@0: return extent; michael@0: } michael@0: function d3_scale_linearTicks(domain, m) { michael@0: return d3.range.apply(d3, d3_scale_linearTickRange(domain, m)); michael@0: } michael@0: function d3_scale_linearTickFormat(domain, m, format) { michael@0: var range = d3_scale_linearTickRange(domain, m); michael@0: return d3.format(format ? format.replace(d3_format_re, function(a, b, c, d, e, f, g, h, i, j) { michael@0: return [ b, c, d, e, f, g, h, i || "." + d3_scale_linearFormatPrecision(j, range), j ].join(""); michael@0: }) : ",." + d3_scale_linearPrecision(range[2]) + "f"); michael@0: } michael@0: var d3_scale_linearFormatSignificant = { michael@0: s: 1, michael@0: g: 1, michael@0: p: 1, michael@0: r: 1, michael@0: e: 1 michael@0: }; michael@0: function d3_scale_linearPrecision(value) { michael@0: return -Math.floor(Math.log(value) / Math.LN10 + .01); michael@0: } michael@0: function d3_scale_linearFormatPrecision(type, range) { michael@0: var p = d3_scale_linearPrecision(range[2]); michael@0: return type in d3_scale_linearFormatSignificant ? Math.abs(p - d3_scale_linearPrecision(Math.max(Math.abs(range[0]), Math.abs(range[1])))) + +(type !== "e") : p - (type === "%") * 2; michael@0: } michael@0: d3.scale.log = function() { michael@0: return d3_scale_log(d3.scale.linear().domain([ 0, 1 ]), 10, true, [ 1, 10 ]); michael@0: }; michael@0: function d3_scale_log(linear, base, positive, domain) { michael@0: function log(x) { michael@0: return (positive ? Math.log(x < 0 ? 0 : x) : -Math.log(x > 0 ? 0 : -x)) / Math.log(base); michael@0: } michael@0: function pow(x) { michael@0: return positive ? Math.pow(base, x) : -Math.pow(base, -x); michael@0: } michael@0: function scale(x) { michael@0: return linear(log(x)); michael@0: } michael@0: scale.invert = function(x) { michael@0: return pow(linear.invert(x)); michael@0: }; michael@0: scale.domain = function(x) { michael@0: if (!arguments.length) return domain; michael@0: positive = x[0] >= 0; michael@0: linear.domain((domain = x.map(Number)).map(log)); michael@0: return scale; michael@0: }; michael@0: scale.base = function(_) { michael@0: if (!arguments.length) return base; michael@0: base = +_; michael@0: linear.domain(domain.map(log)); michael@0: return scale; michael@0: }; michael@0: scale.nice = function() { michael@0: var niced = d3_scale_nice(domain.map(log), positive ? Math : d3_scale_logNiceNegative); michael@0: linear.domain(niced); michael@0: domain = niced.map(pow); michael@0: return scale; michael@0: }; michael@0: scale.ticks = function() { michael@0: var extent = d3_scaleExtent(domain), ticks = [], u = extent[0], v = extent[1], i = Math.floor(log(u)), j = Math.ceil(log(v)), n = base % 1 ? 2 : base; michael@0: if (isFinite(j - i)) { michael@0: if (positive) { michael@0: for (;i < j; i++) for (var k = 1; k < n; k++) ticks.push(pow(i) * k); michael@0: ticks.push(pow(i)); michael@0: } else { michael@0: ticks.push(pow(i)); michael@0: for (;i++ < j; ) for (var k = n - 1; k > 0; k--) ticks.push(pow(i) * k); michael@0: } michael@0: for (i = 0; ticks[i] < u; i++) {} michael@0: for (j = ticks.length; ticks[j - 1] > v; j--) {} michael@0: ticks = ticks.slice(i, j); michael@0: } michael@0: return ticks; michael@0: }; michael@0: scale.tickFormat = function(n, format) { michael@0: if (!arguments.length) return d3_scale_logFormat; michael@0: if (arguments.length < 2) format = d3_scale_logFormat; else if (typeof format !== "function") format = d3.format(format); michael@0: var k = Math.max(.1, n / scale.ticks().length), f = positive ? (e = 1e-12, Math.ceil) : (e = -1e-12, michael@0: Math.floor), e; michael@0: return function(d) { michael@0: return d / pow(f(log(d) + e)) <= k ? format(d) : ""; michael@0: }; michael@0: }; michael@0: scale.copy = function() { michael@0: return d3_scale_log(linear.copy(), base, positive, domain); michael@0: }; michael@0: return d3_scale_linearRebind(scale, linear); michael@0: } michael@0: var d3_scale_logFormat = d3.format(".0e"), d3_scale_logNiceNegative = { michael@0: floor: function(x) { michael@0: return -Math.ceil(-x); michael@0: }, michael@0: ceil: function(x) { michael@0: return -Math.floor(-x); michael@0: } michael@0: }; michael@0: d3.scale.pow = function() { michael@0: return d3_scale_pow(d3.scale.linear(), 1, [ 0, 1 ]); michael@0: }; michael@0: function d3_scale_pow(linear, exponent, domain) { michael@0: var powp = d3_scale_powPow(exponent), powb = d3_scale_powPow(1 / exponent); michael@0: function scale(x) { michael@0: return linear(powp(x)); michael@0: } michael@0: scale.invert = function(x) { michael@0: return powb(linear.invert(x)); michael@0: }; michael@0: scale.domain = function(x) { michael@0: if (!arguments.length) return domain; michael@0: linear.domain((domain = x.map(Number)).map(powp)); michael@0: return scale; michael@0: }; michael@0: scale.ticks = function(m) { michael@0: return d3_scale_linearTicks(domain, m); michael@0: }; michael@0: scale.tickFormat = function(m, format) { michael@0: return d3_scale_linearTickFormat(domain, m, format); michael@0: }; michael@0: scale.nice = function(m) { michael@0: return scale.domain(d3_scale_linearNice(domain, m)); michael@0: }; michael@0: scale.exponent = function(x) { michael@0: if (!arguments.length) return exponent; michael@0: powp = d3_scale_powPow(exponent = x); michael@0: powb = d3_scale_powPow(1 / exponent); michael@0: linear.domain(domain.map(powp)); michael@0: return scale; michael@0: }; michael@0: scale.copy = function() { michael@0: return d3_scale_pow(linear.copy(), exponent, domain); michael@0: }; michael@0: return d3_scale_linearRebind(scale, linear); michael@0: } michael@0: function d3_scale_powPow(e) { michael@0: return function(x) { michael@0: return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e); michael@0: }; michael@0: } michael@0: d3.scale.sqrt = function() { michael@0: return d3.scale.pow().exponent(.5); michael@0: }; michael@0: d3.scale.ordinal = function() { michael@0: return d3_scale_ordinal([], { michael@0: t: "range", michael@0: a: [ [] ] michael@0: }); michael@0: }; michael@0: function d3_scale_ordinal(domain, ranger) { michael@0: var index, range, rangeBand; michael@0: function scale(x) { michael@0: return range[((index.get(x) || ranger.t === "range" && index.set(x, domain.push(x))) - 1) % range.length]; michael@0: } michael@0: function steps(start, step) { michael@0: return d3.range(domain.length).map(function(i) { michael@0: return start + step * i; michael@0: }); michael@0: } michael@0: scale.domain = function(x) { michael@0: if (!arguments.length) return domain; michael@0: domain = []; michael@0: index = new d3_Map(); michael@0: var i = -1, n = x.length, xi; michael@0: while (++i < n) if (!index.has(xi = x[i])) index.set(xi, domain.push(xi)); michael@0: return scale[ranger.t].apply(scale, ranger.a); michael@0: }; michael@0: scale.range = function(x) { michael@0: if (!arguments.length) return range; michael@0: range = x; michael@0: rangeBand = 0; michael@0: ranger = { michael@0: t: "range", michael@0: a: arguments michael@0: }; michael@0: return scale; michael@0: }; michael@0: scale.rangePoints = function(x, padding) { michael@0: if (arguments.length < 2) padding = 0; michael@0: var start = x[0], stop = x[1], step = (stop - start) / (Math.max(1, domain.length - 1) + padding); michael@0: range = steps(domain.length < 2 ? (start + stop) / 2 : start + step * padding / 2, step); michael@0: rangeBand = 0; michael@0: ranger = { michael@0: t: "rangePoints", michael@0: a: arguments michael@0: }; michael@0: return scale; michael@0: }; michael@0: scale.rangeBands = function(x, padding, outerPadding) { michael@0: if (arguments.length < 2) padding = 0; michael@0: if (arguments.length < 3) outerPadding = padding; michael@0: var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = (stop - start) / (domain.length - padding + 2 * outerPadding); michael@0: range = steps(start + step * outerPadding, step); michael@0: if (reverse) range.reverse(); michael@0: rangeBand = step * (1 - padding); michael@0: ranger = { michael@0: t: "rangeBands", michael@0: a: arguments michael@0: }; michael@0: return scale; michael@0: }; michael@0: scale.rangeRoundBands = function(x, padding, outerPadding) { michael@0: if (arguments.length < 2) padding = 0; michael@0: if (arguments.length < 3) outerPadding = padding; michael@0: var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = Math.floor((stop - start) / (domain.length - padding + 2 * outerPadding)), error = stop - start - (domain.length - padding) * step; michael@0: range = steps(start + Math.round(error / 2), step); michael@0: if (reverse) range.reverse(); michael@0: rangeBand = Math.round(step * (1 - padding)); michael@0: ranger = { michael@0: t: "rangeRoundBands", michael@0: a: arguments michael@0: }; michael@0: return scale; michael@0: }; michael@0: scale.rangeBand = function() { michael@0: return rangeBand; michael@0: }; michael@0: scale.rangeExtent = function() { michael@0: return d3_scaleExtent(ranger.a[0]); michael@0: }; michael@0: scale.copy = function() { michael@0: return d3_scale_ordinal(domain, ranger); michael@0: }; michael@0: return scale.domain(domain); michael@0: } michael@0: d3.scale.category10 = function() { michael@0: return d3.scale.ordinal().range(d3_category10); michael@0: }; michael@0: d3.scale.category20 = function() { michael@0: return d3.scale.ordinal().range(d3_category20); michael@0: }; michael@0: d3.scale.category20b = function() { michael@0: return d3.scale.ordinal().range(d3_category20b); michael@0: }; michael@0: d3.scale.category20c = function() { michael@0: return d3.scale.ordinal().range(d3_category20c); michael@0: }; michael@0: var d3_category10 = [ 2062260, 16744206, 2924588, 14034728, 9725885, 9197131, 14907330, 8355711, 12369186, 1556175 ].map(d3_rgbString); michael@0: var d3_category20 = [ 2062260, 11454440, 16744206, 16759672, 2924588, 10018698, 14034728, 16750742, 9725885, 12955861, 9197131, 12885140, 14907330, 16234194, 8355711, 13092807, 12369186, 14408589, 1556175, 10410725 ].map(d3_rgbString); michael@0: var d3_category20b = [ 3750777, 5395619, 7040719, 10264286, 6519097, 9216594, 11915115, 13556636, 9202993, 12426809, 15186514, 15190932, 8666169, 11356490, 14049643, 15177372, 8077683, 10834324, 13528509, 14589654 ].map(d3_rgbString); michael@0: var d3_category20c = [ 3244733, 7057110, 10406625, 13032431, 15095053, 16616764, 16625259, 16634018, 3253076, 7652470, 10607003, 13101504, 7695281, 10394312, 12369372, 14342891, 6513507, 9868950, 12434877, 14277081 ].map(d3_rgbString); michael@0: d3.scale.quantile = function() { michael@0: return d3_scale_quantile([], []); michael@0: }; michael@0: function d3_scale_quantile(domain, range) { michael@0: var thresholds; michael@0: function rescale() { michael@0: var k = 0, q = range.length; michael@0: thresholds = []; michael@0: while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q); michael@0: return scale; michael@0: } michael@0: function scale(x) { michael@0: if (!isNaN(x = +x)) return range[d3.bisect(thresholds, x)]; michael@0: } michael@0: scale.domain = function(x) { michael@0: if (!arguments.length) return domain; michael@0: domain = x.filter(function(d) { michael@0: return !isNaN(d); michael@0: }).sort(d3.ascending); michael@0: return rescale(); michael@0: }; michael@0: scale.range = function(x) { michael@0: if (!arguments.length) return range; michael@0: range = x; michael@0: return rescale(); michael@0: }; michael@0: scale.quantiles = function() { michael@0: return thresholds; michael@0: }; michael@0: scale.invertExtent = function(y) { michael@0: y = range.indexOf(y); michael@0: return y < 0 ? [ NaN, NaN ] : [ y > 0 ? thresholds[y - 1] : domain[0], y < thresholds.length ? thresholds[y] : domain[domain.length - 1] ]; michael@0: }; michael@0: scale.copy = function() { michael@0: return d3_scale_quantile(domain, range); michael@0: }; michael@0: return rescale(); michael@0: } michael@0: d3.scale.quantize = function() { michael@0: return d3_scale_quantize(0, 1, [ 0, 1 ]); michael@0: }; michael@0: function d3_scale_quantize(x0, x1, range) { michael@0: var kx, i; michael@0: function scale(x) { michael@0: return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))]; michael@0: } michael@0: function rescale() { michael@0: kx = range.length / (x1 - x0); michael@0: i = range.length - 1; michael@0: return scale; michael@0: } michael@0: scale.domain = function(x) { michael@0: if (!arguments.length) return [ x0, x1 ]; michael@0: x0 = +x[0]; michael@0: x1 = +x[x.length - 1]; michael@0: return rescale(); michael@0: }; michael@0: scale.range = function(x) { michael@0: if (!arguments.length) return range; michael@0: range = x; michael@0: return rescale(); michael@0: }; michael@0: scale.invertExtent = function(y) { michael@0: y = range.indexOf(y); michael@0: y = y < 0 ? NaN : y / kx + x0; michael@0: return [ y, y + 1 / kx ]; michael@0: }; michael@0: scale.copy = function() { michael@0: return d3_scale_quantize(x0, x1, range); michael@0: }; michael@0: return rescale(); michael@0: } michael@0: d3.scale.threshold = function() { michael@0: return d3_scale_threshold([ .5 ], [ 0, 1 ]); michael@0: }; michael@0: function d3_scale_threshold(domain, range) { michael@0: function scale(x) { michael@0: if (x <= x) return range[d3.bisect(domain, x)]; michael@0: } michael@0: scale.domain = function(_) { michael@0: if (!arguments.length) return domain; michael@0: domain = _; michael@0: return scale; michael@0: }; michael@0: scale.range = function(_) { michael@0: if (!arguments.length) return range; michael@0: range = _; michael@0: return scale; michael@0: }; michael@0: scale.invertExtent = function(y) { michael@0: y = range.indexOf(y); michael@0: return [ domain[y - 1], domain[y] ]; michael@0: }; michael@0: scale.copy = function() { michael@0: return d3_scale_threshold(domain, range); michael@0: }; michael@0: return scale; michael@0: } michael@0: d3.scale.identity = function() { michael@0: return d3_scale_identity([ 0, 1 ]); michael@0: }; michael@0: function d3_scale_identity(domain) { michael@0: function identity(x) { michael@0: return +x; michael@0: } michael@0: identity.invert = identity; michael@0: identity.domain = identity.range = function(x) { michael@0: if (!arguments.length) return domain; michael@0: domain = x.map(identity); michael@0: return identity; michael@0: }; michael@0: identity.ticks = function(m) { michael@0: return d3_scale_linearTicks(domain, m); michael@0: }; michael@0: identity.tickFormat = function(m, format) { michael@0: return d3_scale_linearTickFormat(domain, m, format); michael@0: }; michael@0: identity.copy = function() { michael@0: return d3_scale_identity(domain); michael@0: }; michael@0: return identity; michael@0: } michael@0: d3.svg = {}; michael@0: d3.svg.arc = function() { michael@0: var innerRadius = d3_svg_arcInnerRadius, outerRadius = d3_svg_arcOuterRadius, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle; michael@0: function arc() { michael@0: var r0 = innerRadius.apply(this, arguments), r1 = outerRadius.apply(this, arguments), a0 = startAngle.apply(this, arguments) + d3_svg_arcOffset, a1 = endAngle.apply(this, arguments) + d3_svg_arcOffset, da = (a1 < a0 && (da = a0, michael@0: a0 = a1, a1 = da), a1 - a0), df = da < π ? "0" : "1", c0 = Math.cos(a0), s0 = Math.sin(a0), c1 = Math.cos(a1), s1 = Math.sin(a1); michael@0: return da >= d3_svg_arcMax ? r0 ? "M0," + r1 + "A" + r1 + "," + r1 + " 0 1,1 0," + -r1 + "A" + r1 + "," + r1 + " 0 1,1 0," + r1 + "M0," + r0 + "A" + r0 + "," + r0 + " 0 1,0 0," + -r0 + "A" + r0 + "," + r0 + " 0 1,0 0," + r0 + "Z" : "M0," + r1 + "A" + r1 + "," + r1 + " 0 1,1 0," + -r1 + "A" + r1 + "," + r1 + " 0 1,1 0," + r1 + "Z" : r0 ? "M" + r1 * c0 + "," + r1 * s0 + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1 + "L" + r0 * c1 + "," + r0 * s1 + "A" + r0 + "," + r0 + " 0 " + df + ",0 " + r0 * c0 + "," + r0 * s0 + "Z" : "M" + r1 * c0 + "," + r1 * s0 + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1 + "L0,0" + "Z"; michael@0: } michael@0: arc.innerRadius = function(v) { michael@0: if (!arguments.length) return innerRadius; michael@0: innerRadius = d3_functor(v); michael@0: return arc; michael@0: }; michael@0: arc.outerRadius = function(v) { michael@0: if (!arguments.length) return outerRadius; michael@0: outerRadius = d3_functor(v); michael@0: return arc; michael@0: }; michael@0: arc.startAngle = function(v) { michael@0: if (!arguments.length) return startAngle; michael@0: startAngle = d3_functor(v); michael@0: return arc; michael@0: }; michael@0: arc.endAngle = function(v) { michael@0: if (!arguments.length) return endAngle; michael@0: endAngle = d3_functor(v); michael@0: return arc; michael@0: }; michael@0: arc.centroid = function() { michael@0: var r = (innerRadius.apply(this, arguments) + outerRadius.apply(this, arguments)) / 2, a = (startAngle.apply(this, arguments) + endAngle.apply(this, arguments)) / 2 + d3_svg_arcOffset; michael@0: return [ Math.cos(a) * r, Math.sin(a) * r ]; michael@0: }; michael@0: return arc; michael@0: }; michael@0: var d3_svg_arcOffset = -halfπ, d3_svg_arcMax = τ - ε; michael@0: function d3_svg_arcInnerRadius(d) { michael@0: return d.innerRadius; michael@0: } michael@0: function d3_svg_arcOuterRadius(d) { michael@0: return d.outerRadius; michael@0: } michael@0: function d3_svg_arcStartAngle(d) { michael@0: return d.startAngle; michael@0: } michael@0: function d3_svg_arcEndAngle(d) { michael@0: return d.endAngle; michael@0: } michael@0: function d3_svg_line(projection) { michael@0: var x = d3_geom_pointX, y = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, tension = .7; michael@0: function line(data) { michael@0: var segments = [], points = [], i = -1, n = data.length, d, fx = d3_functor(x), fy = d3_functor(y); michael@0: function segment() { michael@0: segments.push("M", interpolate(projection(points), tension)); michael@0: } michael@0: while (++i < n) { michael@0: if (defined.call(this, d = data[i], i)) { michael@0: points.push([ +fx.call(this, d, i), +fy.call(this, d, i) ]); michael@0: } else if (points.length) { michael@0: segment(); michael@0: points = []; michael@0: } michael@0: } michael@0: if (points.length) segment(); michael@0: return segments.length ? segments.join("") : null; michael@0: } michael@0: line.x = function(_) { michael@0: if (!arguments.length) return x; michael@0: x = _; michael@0: return line; michael@0: }; michael@0: line.y = function(_) { michael@0: if (!arguments.length) return y; michael@0: y = _; michael@0: return line; michael@0: }; michael@0: line.defined = function(_) { michael@0: if (!arguments.length) return defined; michael@0: defined = _; michael@0: return line; michael@0: }; michael@0: line.interpolate = function(_) { michael@0: if (!arguments.length) return interpolateKey; michael@0: if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; michael@0: return line; michael@0: }; michael@0: line.tension = function(_) { michael@0: if (!arguments.length) return tension; michael@0: tension = _; michael@0: return line; michael@0: }; michael@0: return line; michael@0: } michael@0: d3.svg.line = function() { michael@0: return d3_svg_line(d3_identity); michael@0: }; michael@0: var d3_svg_lineInterpolators = d3.map({ michael@0: linear: d3_svg_lineLinear, michael@0: "linear-closed": d3_svg_lineLinearClosed, michael@0: step: d3_svg_lineStep, michael@0: "step-before": d3_svg_lineStepBefore, michael@0: "step-after": d3_svg_lineStepAfter, michael@0: basis: d3_svg_lineBasis, michael@0: "basis-open": d3_svg_lineBasisOpen, michael@0: "basis-closed": d3_svg_lineBasisClosed, michael@0: bundle: d3_svg_lineBundle, michael@0: cardinal: d3_svg_lineCardinal, michael@0: "cardinal-open": d3_svg_lineCardinalOpen, michael@0: "cardinal-closed": d3_svg_lineCardinalClosed, michael@0: monotone: d3_svg_lineMonotone michael@0: }); michael@0: d3_svg_lineInterpolators.forEach(function(key, value) { michael@0: value.key = key; michael@0: value.closed = /-closed$/.test(key); michael@0: }); michael@0: function d3_svg_lineLinear(points) { michael@0: return points.join("L"); michael@0: } michael@0: function d3_svg_lineLinearClosed(points) { michael@0: return d3_svg_lineLinear(points) + "Z"; michael@0: } michael@0: function d3_svg_lineStep(points) { michael@0: var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; michael@0: while (++i < n) path.push("H", (p[0] + (p = points[i])[0]) / 2, "V", p[1]); michael@0: if (n > 1) path.push("H", p[0]); michael@0: return path.join(""); michael@0: } michael@0: function d3_svg_lineStepBefore(points) { michael@0: var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; michael@0: while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]); michael@0: return path.join(""); michael@0: } michael@0: function d3_svg_lineStepAfter(points) { michael@0: var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; michael@0: while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]); michael@0: return path.join(""); michael@0: } michael@0: function d3_svg_lineCardinalOpen(points, tension) { michael@0: return points.length < 4 ? d3_svg_lineLinear(points) : points[1] + d3_svg_lineHermite(points.slice(1, points.length - 1), d3_svg_lineCardinalTangents(points, tension)); michael@0: } michael@0: function d3_svg_lineCardinalClosed(points, tension) { michael@0: return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite((points.push(points[0]), michael@0: points), d3_svg_lineCardinalTangents([ points[points.length - 2] ].concat(points, [ points[1] ]), tension)); michael@0: } michael@0: function d3_svg_lineCardinal(points, tension) { michael@0: return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineCardinalTangents(points, tension)); michael@0: } michael@0: function d3_svg_lineHermite(points, tangents) { michael@0: if (tangents.length < 1 || points.length != tangents.length && points.length != tangents.length + 2) { michael@0: return d3_svg_lineLinear(points); michael@0: } michael@0: var quad = points.length != tangents.length, path = "", p0 = points[0], p = points[1], t0 = tangents[0], t = t0, pi = 1; michael@0: if (quad) { michael@0: path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3) + "," + p[0] + "," + p[1]; michael@0: p0 = points[1]; michael@0: pi = 2; michael@0: } michael@0: if (tangents.length > 1) { michael@0: t = tangents[1]; michael@0: p = points[pi]; michael@0: pi++; michael@0: path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1]) + "," + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; michael@0: for (var i = 2; i < tangents.length; i++, pi++) { michael@0: p = points[pi]; michael@0: t = tangents[i]; michael@0: path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; michael@0: } michael@0: } michael@0: if (quad) { michael@0: var lp = points[pi]; michael@0: path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3) + "," + lp[0] + "," + lp[1]; michael@0: } michael@0: return path; michael@0: } michael@0: function d3_svg_lineCardinalTangents(points, tension) { michael@0: var tangents = [], a = (1 - tension) / 2, p0, p1 = points[0], p2 = points[1], i = 1, n = points.length; michael@0: while (++i < n) { michael@0: p0 = p1; michael@0: p1 = p2; michael@0: p2 = points[i]; michael@0: tangents.push([ a * (p2[0] - p0[0]), a * (p2[1] - p0[1]) ]); michael@0: } michael@0: return tangents; michael@0: } michael@0: function d3_svg_lineBasis(points) { michael@0: if (points.length < 3) return d3_svg_lineLinear(points); michael@0: var i = 1, n = points.length, pi = points[0], x0 = pi[0], y0 = pi[1], px = [ x0, x0, x0, (pi = points[1])[0] ], py = [ y0, y0, y0, pi[1] ], path = [ x0, ",", y0, "L", d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; michael@0: points.push(points[n - 1]); michael@0: while (++i <= n) { michael@0: pi = points[i]; michael@0: px.shift(); michael@0: px.push(pi[0]); michael@0: py.shift(); michael@0: py.push(pi[1]); michael@0: d3_svg_lineBasisBezier(path, px, py); michael@0: } michael@0: points.pop(); michael@0: path.push("L", pi); michael@0: return path.join(""); michael@0: } michael@0: function d3_svg_lineBasisOpen(points) { michael@0: if (points.length < 4) return d3_svg_lineLinear(points); michael@0: var path = [], i = -1, n = points.length, pi, px = [ 0 ], py = [ 0 ]; michael@0: while (++i < 3) { michael@0: pi = points[i]; michael@0: px.push(pi[0]); michael@0: py.push(pi[1]); michael@0: } michael@0: path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px) + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)); michael@0: --i; michael@0: while (++i < n) { michael@0: pi = points[i]; michael@0: px.shift(); michael@0: px.push(pi[0]); michael@0: py.shift(); michael@0: py.push(pi[1]); michael@0: d3_svg_lineBasisBezier(path, px, py); michael@0: } michael@0: return path.join(""); michael@0: } michael@0: function d3_svg_lineBasisClosed(points) { michael@0: var path, i = -1, n = points.length, m = n + 4, pi, px = [], py = []; michael@0: while (++i < 4) { michael@0: pi = points[i % n]; michael@0: px.push(pi[0]); michael@0: py.push(pi[1]); michael@0: } michael@0: path = [ d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; michael@0: --i; michael@0: while (++i < m) { michael@0: pi = points[i % n]; michael@0: px.shift(); michael@0: px.push(pi[0]); michael@0: py.shift(); michael@0: py.push(pi[1]); michael@0: d3_svg_lineBasisBezier(path, px, py); michael@0: } michael@0: return path.join(""); michael@0: } michael@0: function d3_svg_lineBundle(points, tension) { michael@0: var n = points.length - 1; michael@0: if (n) { michael@0: var x0 = points[0][0], y0 = points[0][1], dx = points[n][0] - x0, dy = points[n][1] - y0, i = -1, p, t; michael@0: while (++i <= n) { michael@0: p = points[i]; michael@0: t = i / n; michael@0: p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx); michael@0: p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy); michael@0: } michael@0: } michael@0: return d3_svg_lineBasis(points); michael@0: } michael@0: function d3_svg_lineDot4(a, b) { michael@0: return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; michael@0: } michael@0: var d3_svg_lineBasisBezier1 = [ 0, 2 / 3, 1 / 3, 0 ], d3_svg_lineBasisBezier2 = [ 0, 1 / 3, 2 / 3, 0 ], d3_svg_lineBasisBezier3 = [ 0, 1 / 6, 2 / 3, 1 / 6 ]; michael@0: function d3_svg_lineBasisBezier(path, x, y) { michael@0: path.push("C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y)); michael@0: } michael@0: function d3_svg_lineSlope(p0, p1) { michael@0: return (p1[1] - p0[1]) / (p1[0] - p0[0]); michael@0: } michael@0: function d3_svg_lineFiniteDifferences(points) { michael@0: var i = 0, j = points.length - 1, m = [], p0 = points[0], p1 = points[1], d = m[0] = d3_svg_lineSlope(p0, p1); michael@0: while (++i < j) { michael@0: m[i] = (d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1]))) / 2; michael@0: } michael@0: m[i] = d; michael@0: return m; michael@0: } michael@0: function d3_svg_lineMonotoneTangents(points) { michael@0: var tangents = [], d, a, b, s, m = d3_svg_lineFiniteDifferences(points), i = -1, j = points.length - 1; michael@0: while (++i < j) { michael@0: d = d3_svg_lineSlope(points[i], points[i + 1]); michael@0: if (abs(d) < ε) { michael@0: m[i] = m[i + 1] = 0; michael@0: } else { michael@0: a = m[i] / d; michael@0: b = m[i + 1] / d; michael@0: s = a * a + b * b; michael@0: if (s > 9) { michael@0: s = d * 3 / Math.sqrt(s); michael@0: m[i] = s * a; michael@0: m[i + 1] = s * b; michael@0: } michael@0: } michael@0: } michael@0: i = -1; michael@0: while (++i <= j) { michael@0: s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0]) / (6 * (1 + m[i] * m[i])); michael@0: tangents.push([ s || 0, m[i] * s || 0 ]); michael@0: } michael@0: return tangents; michael@0: } michael@0: function d3_svg_lineMonotone(points) { michael@0: return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points)); michael@0: } michael@0: d3.svg.line.radial = function() { michael@0: var line = d3_svg_line(d3_svg_lineRadial); michael@0: line.radius = line.x, delete line.x; michael@0: line.angle = line.y, delete line.y; michael@0: return line; michael@0: }; michael@0: function d3_svg_lineRadial(points) { michael@0: var point, i = -1, n = points.length, r, a; michael@0: while (++i < n) { michael@0: point = points[i]; michael@0: r = point[0]; michael@0: a = point[1] + d3_svg_arcOffset; michael@0: point[0] = r * Math.cos(a); michael@0: point[1] = r * Math.sin(a); michael@0: } michael@0: return points; michael@0: } michael@0: function d3_svg_area(projection) { michael@0: var x0 = d3_geom_pointX, x1 = d3_geom_pointX, y0 = 0, y1 = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, interpolateReverse = interpolate, L = "L", tension = .7; michael@0: function area(data) { michael@0: var segments = [], points0 = [], points1 = [], i = -1, n = data.length, d, fx0 = d3_functor(x0), fy0 = d3_functor(y0), fx1 = x0 === x1 ? function() { michael@0: return x; michael@0: } : d3_functor(x1), fy1 = y0 === y1 ? function() { michael@0: return y; michael@0: } : d3_functor(y1), x, y; michael@0: function segment() { michael@0: segments.push("M", interpolate(projection(points1), tension), L, interpolateReverse(projection(points0.reverse()), tension), "Z"); michael@0: } michael@0: while (++i < n) { michael@0: if (defined.call(this, d = data[i], i)) { michael@0: points0.push([ x = +fx0.call(this, d, i), y = +fy0.call(this, d, i) ]); michael@0: points1.push([ +fx1.call(this, d, i), +fy1.call(this, d, i) ]); michael@0: } else if (points0.length) { michael@0: segment(); michael@0: points0 = []; michael@0: points1 = []; michael@0: } michael@0: } michael@0: if (points0.length) segment(); michael@0: return segments.length ? segments.join("") : null; michael@0: } michael@0: area.x = function(_) { michael@0: if (!arguments.length) return x1; michael@0: x0 = x1 = _; michael@0: return area; michael@0: }; michael@0: area.x0 = function(_) { michael@0: if (!arguments.length) return x0; michael@0: x0 = _; michael@0: return area; michael@0: }; michael@0: area.x1 = function(_) { michael@0: if (!arguments.length) return x1; michael@0: x1 = _; michael@0: return area; michael@0: }; michael@0: area.y = function(_) { michael@0: if (!arguments.length) return y1; michael@0: y0 = y1 = _; michael@0: return area; michael@0: }; michael@0: area.y0 = function(_) { michael@0: if (!arguments.length) return y0; michael@0: y0 = _; michael@0: return area; michael@0: }; michael@0: area.y1 = function(_) { michael@0: if (!arguments.length) return y1; michael@0: y1 = _; michael@0: return area; michael@0: }; michael@0: area.defined = function(_) { michael@0: if (!arguments.length) return defined; michael@0: defined = _; michael@0: return area; michael@0: }; michael@0: area.interpolate = function(_) { michael@0: if (!arguments.length) return interpolateKey; michael@0: if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; michael@0: interpolateReverse = interpolate.reverse || interpolate; michael@0: L = interpolate.closed ? "M" : "L"; michael@0: return area; michael@0: }; michael@0: area.tension = function(_) { michael@0: if (!arguments.length) return tension; michael@0: tension = _; michael@0: return area; michael@0: }; michael@0: return area; michael@0: } michael@0: d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter; michael@0: d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore; michael@0: d3.svg.area = function() { michael@0: return d3_svg_area(d3_identity); michael@0: }; michael@0: d3.svg.area.radial = function() { michael@0: var area = d3_svg_area(d3_svg_lineRadial); michael@0: area.radius = area.x, delete area.x; michael@0: area.innerRadius = area.x0, delete area.x0; michael@0: area.outerRadius = area.x1, delete area.x1; michael@0: area.angle = area.y, delete area.y; michael@0: area.startAngle = area.y0, delete area.y0; michael@0: area.endAngle = area.y1, delete area.y1; michael@0: return area; michael@0: }; michael@0: d3.svg.chord = function() { michael@0: var source = d3_source, target = d3_target, radius = d3_svg_chordRadius, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle; michael@0: function chord(d, i) { michael@0: var s = subgroup(this, source, d, i), t = subgroup(this, target, d, i); michael@0: return "M" + s.p0 + arc(s.r, s.p1, s.a1 - s.a0) + (equals(s, t) ? curve(s.r, s.p1, s.r, s.p0) : curve(s.r, s.p1, t.r, t.p0) + arc(t.r, t.p1, t.a1 - t.a0) + curve(t.r, t.p1, s.r, s.p0)) + "Z"; michael@0: } michael@0: function subgroup(self, f, d, i) { michael@0: var subgroup = f.call(self, d, i), r = radius.call(self, subgroup, i), a0 = startAngle.call(self, subgroup, i) + d3_svg_arcOffset, a1 = endAngle.call(self, subgroup, i) + d3_svg_arcOffset; michael@0: return { michael@0: r: r, michael@0: a0: a0, michael@0: a1: a1, michael@0: p0: [ r * Math.cos(a0), r * Math.sin(a0) ], michael@0: p1: [ r * Math.cos(a1), r * Math.sin(a1) ] michael@0: }; michael@0: } michael@0: function equals(a, b) { michael@0: return a.a0 == b.a0 && a.a1 == b.a1; michael@0: } michael@0: function arc(r, p, a) { michael@0: return "A" + r + "," + r + " 0 " + +(a > π) + ",1 " + p; michael@0: } michael@0: function curve(r0, p0, r1, p1) { michael@0: return "Q 0,0 " + p1; michael@0: } michael@0: chord.radius = function(v) { michael@0: if (!arguments.length) return radius; michael@0: radius = d3_functor(v); michael@0: return chord; michael@0: }; michael@0: chord.source = function(v) { michael@0: if (!arguments.length) return source; michael@0: source = d3_functor(v); michael@0: return chord; michael@0: }; michael@0: chord.target = function(v) { michael@0: if (!arguments.length) return target; michael@0: target = d3_functor(v); michael@0: return chord; michael@0: }; michael@0: chord.startAngle = function(v) { michael@0: if (!arguments.length) return startAngle; michael@0: startAngle = d3_functor(v); michael@0: return chord; michael@0: }; michael@0: chord.endAngle = function(v) { michael@0: if (!arguments.length) return endAngle; michael@0: endAngle = d3_functor(v); michael@0: return chord; michael@0: }; michael@0: return chord; michael@0: }; michael@0: function d3_svg_chordRadius(d) { michael@0: return d.radius; michael@0: } michael@0: d3.svg.diagonal = function() { michael@0: var source = d3_source, target = d3_target, projection = d3_svg_diagonalProjection; michael@0: function diagonal(d, i) { michael@0: var p0 = source.call(this, d, i), p3 = target.call(this, d, i), m = (p0.y + p3.y) / 2, p = [ p0, { michael@0: x: p0.x, michael@0: y: m michael@0: }, { michael@0: x: p3.x, michael@0: y: m michael@0: }, p3 ]; michael@0: p = p.map(projection); michael@0: return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3]; michael@0: } michael@0: diagonal.source = function(x) { michael@0: if (!arguments.length) return source; michael@0: source = d3_functor(x); michael@0: return diagonal; michael@0: }; michael@0: diagonal.target = function(x) { michael@0: if (!arguments.length) return target; michael@0: target = d3_functor(x); michael@0: return diagonal; michael@0: }; michael@0: diagonal.projection = function(x) { michael@0: if (!arguments.length) return projection; michael@0: projection = x; michael@0: return diagonal; michael@0: }; michael@0: return diagonal; michael@0: }; michael@0: function d3_svg_diagonalProjection(d) { michael@0: return [ d.x, d.y ]; michael@0: } michael@0: d3.svg.diagonal.radial = function() { michael@0: var diagonal = d3.svg.diagonal(), projection = d3_svg_diagonalProjection, projection_ = diagonal.projection; michael@0: diagonal.projection = function(x) { michael@0: return arguments.length ? projection_(d3_svg_diagonalRadialProjection(projection = x)) : projection; michael@0: }; michael@0: return diagonal; michael@0: }; michael@0: function d3_svg_diagonalRadialProjection(projection) { michael@0: return function() { michael@0: var d = projection.apply(this, arguments), r = d[0], a = d[1] + d3_svg_arcOffset; michael@0: return [ r * Math.cos(a), r * Math.sin(a) ]; michael@0: }; michael@0: } michael@0: d3.svg.symbol = function() { michael@0: var type = d3_svg_symbolType, size = d3_svg_symbolSize; michael@0: function symbol(d, i) { michael@0: return (d3_svg_symbols.get(type.call(this, d, i)) || d3_svg_symbolCircle)(size.call(this, d, i)); michael@0: } michael@0: symbol.type = function(x) { michael@0: if (!arguments.length) return type; michael@0: type = d3_functor(x); michael@0: return symbol; michael@0: }; michael@0: symbol.size = function(x) { michael@0: if (!arguments.length) return size; michael@0: size = d3_functor(x); michael@0: return symbol; michael@0: }; michael@0: return symbol; michael@0: }; michael@0: function d3_svg_symbolSize() { michael@0: return 64; michael@0: } michael@0: function d3_svg_symbolType() { michael@0: return "circle"; michael@0: } michael@0: function d3_svg_symbolCircle(size) { michael@0: var r = Math.sqrt(size / π); michael@0: return "M0," + r + "A" + r + "," + r + " 0 1,1 0," + -r + "A" + r + "," + r + " 0 1,1 0," + r + "Z"; michael@0: } michael@0: var d3_svg_symbols = d3.map({ michael@0: circle: d3_svg_symbolCircle, michael@0: cross: function(size) { michael@0: var r = Math.sqrt(size / 5) / 2; michael@0: return "M" + -3 * r + "," + -r + "H" + -r + "V" + -3 * r + "H" + r + "V" + -r + "H" + 3 * r + "V" + r + "H" + r + "V" + 3 * r + "H" + -r + "V" + r + "H" + -3 * r + "Z"; michael@0: }, michael@0: diamond: function(size) { michael@0: var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)), rx = ry * d3_svg_symbolTan30; michael@0: return "M0," + -ry + "L" + rx + ",0" + " 0," + ry + " " + -rx + ",0" + "Z"; michael@0: }, michael@0: square: function(size) { michael@0: var r = Math.sqrt(size) / 2; michael@0: return "M" + -r + "," + -r + "L" + r + "," + -r + " " + r + "," + r + " " + -r + "," + r + "Z"; michael@0: }, michael@0: "triangle-down": function(size) { michael@0: var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; michael@0: return "M0," + ry + "L" + rx + "," + -ry + " " + -rx + "," + -ry + "Z"; michael@0: }, michael@0: "triangle-up": function(size) { michael@0: var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; michael@0: return "M0," + -ry + "L" + rx + "," + ry + " " + -rx + "," + ry + "Z"; michael@0: } michael@0: }); michael@0: d3.svg.symbolTypes = d3_svg_symbols.keys(); michael@0: var d3_svg_symbolSqrt3 = Math.sqrt(3), d3_svg_symbolTan30 = Math.tan(30 * d3_radians); michael@0: function d3_transition(groups, id) { michael@0: d3_subclass(groups, d3_transitionPrototype); michael@0: groups.id = id; michael@0: return groups; michael@0: } michael@0: var d3_transitionPrototype = [], d3_transitionId = 0, d3_transitionInheritId, d3_transitionInherit; michael@0: d3_transitionPrototype.call = d3_selectionPrototype.call; michael@0: d3_transitionPrototype.empty = d3_selectionPrototype.empty; michael@0: d3_transitionPrototype.node = d3_selectionPrototype.node; michael@0: d3_transitionPrototype.size = d3_selectionPrototype.size; michael@0: d3.transition = function(selection) { michael@0: return arguments.length ? d3_transitionInheritId ? selection.transition() : selection : d3_selectionRoot.transition(); michael@0: }; michael@0: d3.transition.prototype = d3_transitionPrototype; michael@0: d3_transitionPrototype.select = function(selector) { michael@0: var id = this.id, subgroups = [], subgroup, subnode, node; michael@0: selector = d3_selection_selector(selector); michael@0: for (var j = -1, m = this.length; ++j < m; ) { michael@0: subgroups.push(subgroup = []); michael@0: for (var group = this[j], i = -1, n = group.length; ++i < n; ) { michael@0: if ((node = group[i]) && (subnode = selector.call(node, node.__data__, i, j))) { michael@0: if ("__data__" in node) subnode.__data__ = node.__data__; michael@0: d3_transitionNode(subnode, i, id, node.__transition__[id]); michael@0: subgroup.push(subnode); michael@0: } else { michael@0: subgroup.push(null); michael@0: } michael@0: } michael@0: } michael@0: return d3_transition(subgroups, id); michael@0: }; michael@0: d3_transitionPrototype.selectAll = function(selector) { michael@0: var id = this.id, subgroups = [], subgroup, subnodes, node, subnode, transition; michael@0: selector = d3_selection_selectorAll(selector); michael@0: for (var j = -1, m = this.length; ++j < m; ) { michael@0: for (var group = this[j], i = -1, n = group.length; ++i < n; ) { michael@0: if (node = group[i]) { michael@0: transition = node.__transition__[id]; michael@0: subnodes = selector.call(node, node.__data__, i, j); michael@0: subgroups.push(subgroup = []); michael@0: for (var k = -1, o = subnodes.length; ++k < o; ) { michael@0: if (subnode = subnodes[k]) d3_transitionNode(subnode, k, id, transition); michael@0: subgroup.push(subnode); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return d3_transition(subgroups, id); michael@0: }; michael@0: d3_transitionPrototype.filter = function(filter) { michael@0: var subgroups = [], subgroup, group, node; michael@0: if (typeof filter !== "function") filter = d3_selection_filter(filter); michael@0: for (var j = 0, m = this.length; j < m; j++) { michael@0: subgroups.push(subgroup = []); michael@0: for (var group = this[j], i = 0, n = group.length; i < n; i++) { michael@0: if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { michael@0: subgroup.push(node); michael@0: } michael@0: } michael@0: } michael@0: return d3_transition(subgroups, this.id); michael@0: }; michael@0: d3_transitionPrototype.tween = function(name, tween) { michael@0: var id = this.id; michael@0: if (arguments.length < 2) return this.node().__transition__[id].tween.get(name); michael@0: return d3_selection_each(this, tween == null ? function(node) { michael@0: node.__transition__[id].tween.remove(name); michael@0: } : function(node) { michael@0: node.__transition__[id].tween.set(name, tween); michael@0: }); michael@0: }; michael@0: function d3_transition_tween(groups, name, value, tween) { michael@0: var id = groups.id; michael@0: return d3_selection_each(groups, typeof value === "function" ? function(node, i, j) { michael@0: node.__transition__[id].tween.set(name, tween(value.call(node, node.__data__, i, j))); michael@0: } : (value = tween(value), function(node) { michael@0: node.__transition__[id].tween.set(name, value); michael@0: })); michael@0: } michael@0: d3_transitionPrototype.attr = function(nameNS, value) { michael@0: if (arguments.length < 2) { michael@0: for (value in nameNS) this.attr(value, nameNS[value]); michael@0: return this; michael@0: } michael@0: var interpolate = nameNS == "transform" ? d3_interpolateTransform : d3_interpolate, name = d3.ns.qualify(nameNS); michael@0: function attrNull() { michael@0: this.removeAttribute(name); michael@0: } michael@0: function attrNullNS() { michael@0: this.removeAttributeNS(name.space, name.local); michael@0: } michael@0: function attrTween(b) { michael@0: return b == null ? attrNull : (b += "", function() { michael@0: var a = this.getAttribute(name), i; michael@0: return a !== b && (i = interpolate(a, b), function(t) { michael@0: this.setAttribute(name, i(t)); michael@0: }); michael@0: }); michael@0: } michael@0: function attrTweenNS(b) { michael@0: return b == null ? attrNullNS : (b += "", function() { michael@0: var a = this.getAttributeNS(name.space, name.local), i; michael@0: return a !== b && (i = interpolate(a, b), function(t) { michael@0: this.setAttributeNS(name.space, name.local, i(t)); michael@0: }); michael@0: }); michael@0: } michael@0: return d3_transition_tween(this, "attr." + nameNS, value, name.local ? attrTweenNS : attrTween); michael@0: }; michael@0: d3_transitionPrototype.attrTween = function(nameNS, tween) { michael@0: var name = d3.ns.qualify(nameNS); michael@0: function attrTween(d, i) { michael@0: var f = tween.call(this, d, i, this.getAttribute(name)); michael@0: return f && function(t) { michael@0: this.setAttribute(name, f(t)); michael@0: }; michael@0: } michael@0: function attrTweenNS(d, i) { michael@0: var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local)); michael@0: return f && function(t) { michael@0: this.setAttributeNS(name.space, name.local, f(t)); michael@0: }; michael@0: } michael@0: return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween); michael@0: }; michael@0: d3_transitionPrototype.style = function(name, value, priority) { michael@0: var n = arguments.length; michael@0: if (n < 3) { michael@0: if (typeof name !== "string") { michael@0: if (n < 2) value = ""; michael@0: for (priority in name) this.style(priority, name[priority], value); michael@0: return this; michael@0: } michael@0: priority = ""; michael@0: } michael@0: function styleNull() { michael@0: this.style.removeProperty(name); michael@0: } michael@0: function styleString(b) { michael@0: return b == null ? styleNull : (b += "", function() { michael@0: var a = d3_window.getComputedStyle(this, null).getPropertyValue(name), i; michael@0: return a !== b && (i = d3_interpolate(a, b), function(t) { michael@0: this.style.setProperty(name, i(t), priority); michael@0: }); michael@0: }); michael@0: } michael@0: return d3_transition_tween(this, "style." + name, value, styleString); michael@0: }; michael@0: d3_transitionPrototype.styleTween = function(name, tween, priority) { michael@0: if (arguments.length < 3) priority = ""; michael@0: function styleTween(d, i) { michael@0: var f = tween.call(this, d, i, d3_window.getComputedStyle(this, null).getPropertyValue(name)); michael@0: return f && function(t) { michael@0: this.style.setProperty(name, f(t), priority); michael@0: }; michael@0: } michael@0: return this.tween("style." + name, styleTween); michael@0: }; michael@0: d3_transitionPrototype.text = function(value) { michael@0: return d3_transition_tween(this, "text", value, d3_transition_text); michael@0: }; michael@0: function d3_transition_text(b) { michael@0: if (b == null) b = ""; michael@0: return function() { michael@0: this.textContent = b; michael@0: }; michael@0: } michael@0: d3_transitionPrototype.remove = function() { michael@0: return this.each("end.transition", function() { michael@0: var p; michael@0: if (this.__transition__.count < 2 && (p = this.parentNode)) p.removeChild(this); michael@0: }); michael@0: }; michael@0: d3_transitionPrototype.ease = function(value) { michael@0: var id = this.id; michael@0: if (arguments.length < 1) return this.node().__transition__[id].ease; michael@0: if (typeof value !== "function") value = d3.ease.apply(d3, arguments); michael@0: return d3_selection_each(this, function(node) { michael@0: node.__transition__[id].ease = value; michael@0: }); michael@0: }; michael@0: d3_transitionPrototype.delay = function(value) { michael@0: var id = this.id; michael@0: return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { michael@0: node.__transition__[id].delay = +value.call(node, node.__data__, i, j); michael@0: } : (value = +value, function(node) { michael@0: node.__transition__[id].delay = value; michael@0: })); michael@0: }; michael@0: d3_transitionPrototype.duration = function(value) { michael@0: var id = this.id; michael@0: return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { michael@0: node.__transition__[id].duration = Math.max(1, value.call(node, node.__data__, i, j)); michael@0: } : (value = Math.max(1, value), function(node) { michael@0: node.__transition__[id].duration = value; michael@0: })); michael@0: }; michael@0: d3_transitionPrototype.each = function(type, listener) { michael@0: var id = this.id; michael@0: if (arguments.length < 2) { michael@0: var inherit = d3_transitionInherit, inheritId = d3_transitionInheritId; michael@0: d3_transitionInheritId = id; michael@0: d3_selection_each(this, function(node, i, j) { michael@0: d3_transitionInherit = node.__transition__[id]; michael@0: type.call(node, node.__data__, i, j); michael@0: }); michael@0: d3_transitionInherit = inherit; michael@0: d3_transitionInheritId = inheritId; michael@0: } else { michael@0: d3_selection_each(this, function(node) { michael@0: var transition = node.__transition__[id]; michael@0: (transition.event || (transition.event = d3.dispatch("start", "end"))).on(type, listener); michael@0: }); michael@0: } michael@0: return this; michael@0: }; michael@0: d3_transitionPrototype.transition = function() { michael@0: var id0 = this.id, id1 = ++d3_transitionId, subgroups = [], subgroup, group, node, transition; michael@0: for (var j = 0, m = this.length; j < m; j++) { michael@0: subgroups.push(subgroup = []); michael@0: for (var group = this[j], i = 0, n = group.length; i < n; i++) { michael@0: if (node = group[i]) { michael@0: transition = Object.create(node.__transition__[id0]); michael@0: transition.delay += transition.duration; michael@0: d3_transitionNode(node, i, id1, transition); michael@0: } michael@0: subgroup.push(node); michael@0: } michael@0: } michael@0: return d3_transition(subgroups, id1); michael@0: }; michael@0: function d3_transitionNode(node, i, id, inherit) { michael@0: var lock = node.__transition__ || (node.__transition__ = { michael@0: active: 0, michael@0: count: 0 michael@0: }), transition = lock[id]; michael@0: if (!transition) { michael@0: var time = inherit.time; michael@0: transition = lock[id] = { michael@0: tween: new d3_Map(), michael@0: time: time, michael@0: ease: inherit.ease, michael@0: delay: inherit.delay, michael@0: duration: inherit.duration michael@0: }; michael@0: ++lock.count; michael@0: d3.timer(function(elapsed) { michael@0: var d = node.__data__, ease = transition.ease, delay = transition.delay, duration = transition.duration, timer = d3_timer_active, tweened = []; michael@0: timer.t = delay + time; michael@0: if (delay <= elapsed) return start(elapsed - delay); michael@0: timer.c = start; michael@0: function start(elapsed) { michael@0: if (lock.active > id) return stop(); michael@0: lock.active = id; michael@0: transition.event && transition.event.start.call(node, d, i); michael@0: transition.tween.forEach(function(key, value) { michael@0: if (value = value.call(node, d, i)) { michael@0: tweened.push(value); michael@0: } michael@0: }); michael@0: d3.timer(function() { michael@0: timer.c = tick(elapsed || 1) ? d3_true : tick; michael@0: return 1; michael@0: }, 0, time); michael@0: } michael@0: function tick(elapsed) { michael@0: if (lock.active !== id) return stop(); michael@0: var t = elapsed / duration, e = ease(t), n = tweened.length; michael@0: while (n > 0) { michael@0: tweened[--n].call(node, e); michael@0: } michael@0: if (t >= 1) { michael@0: transition.event && transition.event.end.call(node, d, i); michael@0: return stop(); michael@0: } michael@0: } michael@0: function stop() { michael@0: if (--lock.count) delete lock[id]; else delete node.__transition__; michael@0: return 1; michael@0: } michael@0: }, 0, time); michael@0: } michael@0: } michael@0: d3.svg.axis = function() { michael@0: var scale = d3.scale.linear(), orient = d3_svg_axisDefaultOrient, innerTickSize = 6, outerTickSize = 6, tickPadding = 3, tickArguments_ = [ 10 ], tickValues = null, tickFormat_; michael@0: function axis(g) { michael@0: g.each(function() { michael@0: var g = d3.select(this); michael@0: var scale0 = this.__chart__ || scale, scale1 = this.__chart__ = scale.copy(); michael@0: var ticks = tickValues == null ? scale1.ticks ? scale1.ticks.apply(scale1, tickArguments_) : scale1.domain() : tickValues, tickFormat = tickFormat_ == null ? scale1.tickFormat ? scale1.tickFormat.apply(scale1, tickArguments_) : d3_identity : tickFormat_, tick = g.selectAll(".tick").data(ticks, scale1), tickEnter = tick.enter().insert("g", ".domain").attr("class", "tick").style("opacity", ε), tickExit = d3.transition(tick.exit()).style("opacity", ε).remove(), tickUpdate = d3.transition(tick).style("opacity", 1), tickTransform; michael@0: var range = d3_scaleRange(scale1), path = g.selectAll(".domain").data([ 0 ]), pathUpdate = (path.enter().append("path").attr("class", "domain"), michael@0: d3.transition(path)); michael@0: tickEnter.append("line"); michael@0: tickEnter.append("text"); michael@0: var lineEnter = tickEnter.select("line"), lineUpdate = tickUpdate.select("line"), text = tick.select("text").text(tickFormat), textEnter = tickEnter.select("text"), textUpdate = tickUpdate.select("text"); michael@0: switch (orient) { michael@0: case "bottom": michael@0: { michael@0: tickTransform = d3_svg_axisX; michael@0: lineEnter.attr("y2", innerTickSize); michael@0: textEnter.attr("y", Math.max(innerTickSize, 0) + tickPadding); michael@0: lineUpdate.attr("x2", 0).attr("y2", innerTickSize); michael@0: textUpdate.attr("x", 0).attr("y", Math.max(innerTickSize, 0) + tickPadding); michael@0: text.attr("dy", ".71em").style("text-anchor", "middle"); michael@0: pathUpdate.attr("d", "M" + range[0] + "," + outerTickSize + "V0H" + range[1] + "V" + outerTickSize); michael@0: break; michael@0: } michael@0: michael@0: case "top": michael@0: { michael@0: tickTransform = d3_svg_axisX; michael@0: lineEnter.attr("y2", -innerTickSize); michael@0: textEnter.attr("y", -(Math.max(innerTickSize, 0) + tickPadding)); michael@0: lineUpdate.attr("x2", 0).attr("y2", -innerTickSize); michael@0: textUpdate.attr("x", 0).attr("y", -(Math.max(innerTickSize, 0) + tickPadding)); michael@0: text.attr("dy", "0em").style("text-anchor", "middle"); michael@0: pathUpdate.attr("d", "M" + range[0] + "," + -outerTickSize + "V0H" + range[1] + "V" + -outerTickSize); michael@0: break; michael@0: } michael@0: michael@0: case "left": michael@0: { michael@0: tickTransform = d3_svg_axisY; michael@0: lineEnter.attr("x2", -innerTickSize); michael@0: textEnter.attr("x", -(Math.max(innerTickSize, 0) + tickPadding)); michael@0: lineUpdate.attr("x2", -innerTickSize).attr("y2", 0); michael@0: textUpdate.attr("x", -(Math.max(innerTickSize, 0) + tickPadding)).attr("y", 0); michael@0: text.attr("dy", ".32em").style("text-anchor", "end"); michael@0: pathUpdate.attr("d", "M" + -outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + -outerTickSize); michael@0: break; michael@0: } michael@0: michael@0: case "right": michael@0: { michael@0: tickTransform = d3_svg_axisY; michael@0: lineEnter.attr("x2", innerTickSize); michael@0: textEnter.attr("x", Math.max(innerTickSize, 0) + tickPadding); michael@0: lineUpdate.attr("x2", innerTickSize).attr("y2", 0); michael@0: textUpdate.attr("x", Math.max(innerTickSize, 0) + tickPadding).attr("y", 0); michael@0: text.attr("dy", ".32em").style("text-anchor", "start"); michael@0: pathUpdate.attr("d", "M" + outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + outerTickSize); michael@0: break; michael@0: } michael@0: } michael@0: if (scale1.rangeBand) { michael@0: var x = scale1, dx = x.rangeBand() / 2; michael@0: scale0 = scale1 = function(d) { michael@0: return x(d) + dx; michael@0: }; michael@0: } else if (scale0.rangeBand) { michael@0: scale0 = scale1; michael@0: } else { michael@0: tickExit.call(tickTransform, scale1); michael@0: } michael@0: tickEnter.call(tickTransform, scale0); michael@0: tickUpdate.call(tickTransform, scale1); michael@0: }); michael@0: } michael@0: axis.scale = function(x) { michael@0: if (!arguments.length) return scale; michael@0: scale = x; michael@0: return axis; michael@0: }; michael@0: axis.orient = function(x) { michael@0: if (!arguments.length) return orient; michael@0: orient = x in d3_svg_axisOrients ? x + "" : d3_svg_axisDefaultOrient; michael@0: return axis; michael@0: }; michael@0: axis.ticks = function() { michael@0: if (!arguments.length) return tickArguments_; michael@0: tickArguments_ = arguments; michael@0: return axis; michael@0: }; michael@0: axis.tickValues = function(x) { michael@0: if (!arguments.length) return tickValues; michael@0: tickValues = x; michael@0: return axis; michael@0: }; michael@0: axis.tickFormat = function(x) { michael@0: if (!arguments.length) return tickFormat_; michael@0: tickFormat_ = x; michael@0: return axis; michael@0: }; michael@0: axis.tickSize = function(x) { michael@0: var n = arguments.length; michael@0: if (!n) return innerTickSize; michael@0: innerTickSize = +x; michael@0: outerTickSize = +arguments[n - 1]; michael@0: return axis; michael@0: }; michael@0: axis.innerTickSize = function(x) { michael@0: if (!arguments.length) return innerTickSize; michael@0: innerTickSize = +x; michael@0: return axis; michael@0: }; michael@0: axis.outerTickSize = function(x) { michael@0: if (!arguments.length) return outerTickSize; michael@0: outerTickSize = +x; michael@0: return axis; michael@0: }; michael@0: axis.tickPadding = function(x) { michael@0: if (!arguments.length) return tickPadding; michael@0: tickPadding = +x; michael@0: return axis; michael@0: }; michael@0: axis.tickSubdivide = function() { michael@0: return arguments.length && axis; michael@0: }; michael@0: return axis; michael@0: }; michael@0: var d3_svg_axisDefaultOrient = "bottom", d3_svg_axisOrients = { michael@0: top: 1, michael@0: right: 1, michael@0: bottom: 1, michael@0: left: 1 michael@0: }; michael@0: function d3_svg_axisX(selection, x) { michael@0: selection.attr("transform", function(d) { michael@0: return "translate(" + x(d) + ",0)"; michael@0: }); michael@0: } michael@0: function d3_svg_axisY(selection, y) { michael@0: selection.attr("transform", function(d) { michael@0: return "translate(0," + y(d) + ")"; michael@0: }); michael@0: } michael@0: d3.svg.brush = function() { michael@0: var event = d3_eventDispatch(brush, "brushstart", "brush", "brushend"), x = null, y = null, xExtent = [ 0, 0 ], yExtent = [ 0, 0 ], xExtentDomain, yExtentDomain, xClamp = true, yClamp = true, resizes = d3_svg_brushResizes[0]; michael@0: function brush(g) { michael@0: g.each(function() { michael@0: var g = d3.select(this).style("pointer-events", "all").style("-webkit-tap-highlight-color", "rgba(0,0,0,0)").on("mousedown.brush", brushstart).on("touchstart.brush", brushstart); michael@0: var background = g.selectAll(".background").data([ 0 ]); michael@0: background.enter().append("rect").attr("class", "background").style("visibility", "hidden").style("cursor", "crosshair"); michael@0: g.selectAll(".extent").data([ 0 ]).enter().append("rect").attr("class", "extent").style("cursor", "move"); michael@0: var resize = g.selectAll(".resize").data(resizes, d3_identity); michael@0: resize.exit().remove(); michael@0: resize.enter().append("g").attr("class", function(d) { michael@0: return "resize " + d; michael@0: }).style("cursor", function(d) { michael@0: return d3_svg_brushCursor[d]; michael@0: }).append("rect").attr("x", function(d) { michael@0: return /[ew]$/.test(d) ? -3 : null; michael@0: }).attr("y", function(d) { michael@0: return /^[ns]/.test(d) ? -3 : null; michael@0: }).attr("width", 6).attr("height", 6).style("visibility", "hidden"); michael@0: resize.style("display", brush.empty() ? "none" : null); michael@0: var gUpdate = d3.transition(g), backgroundUpdate = d3.transition(background), range; michael@0: if (x) { michael@0: range = d3_scaleRange(x); michael@0: backgroundUpdate.attr("x", range[0]).attr("width", range[1] - range[0]); michael@0: redrawX(gUpdate); michael@0: } michael@0: if (y) { michael@0: range = d3_scaleRange(y); michael@0: backgroundUpdate.attr("y", range[0]).attr("height", range[1] - range[0]); michael@0: redrawY(gUpdate); michael@0: } michael@0: redraw(gUpdate); michael@0: }); michael@0: } michael@0: brush.event = function(g) { michael@0: g.each(function() { michael@0: var event_ = event.of(this, arguments), extent1 = { michael@0: x: xExtent, michael@0: y: yExtent, michael@0: i: xExtentDomain, michael@0: j: yExtentDomain michael@0: }, extent0 = this.__chart__ || extent1; michael@0: this.__chart__ = extent1; michael@0: if (d3_transitionInheritId) { michael@0: d3.select(this).transition().each("start.brush", function() { michael@0: xExtentDomain = extent0.i; michael@0: yExtentDomain = extent0.j; michael@0: xExtent = extent0.x; michael@0: yExtent = extent0.y; michael@0: event_({ michael@0: type: "brushstart" michael@0: }); michael@0: }).tween("brush:brush", function() { michael@0: var xi = d3_interpolateArray(xExtent, extent1.x), yi = d3_interpolateArray(yExtent, extent1.y); michael@0: xExtentDomain = yExtentDomain = null; michael@0: return function(t) { michael@0: xExtent = extent1.x = xi(t); michael@0: yExtent = extent1.y = yi(t); michael@0: event_({ michael@0: type: "brush", michael@0: mode: "resize" michael@0: }); michael@0: }; michael@0: }).each("end.brush", function() { michael@0: xExtentDomain = extent1.i; michael@0: yExtentDomain = extent1.j; michael@0: event_({ michael@0: type: "brush", michael@0: mode: "resize" michael@0: }); michael@0: event_({ michael@0: type: "brushend" michael@0: }); michael@0: }); michael@0: } else { michael@0: event_({ michael@0: type: "brushstart" michael@0: }); michael@0: event_({ michael@0: type: "brush", michael@0: mode: "resize" michael@0: }); michael@0: event_({ michael@0: type: "brushend" michael@0: }); michael@0: } michael@0: }); michael@0: }; michael@0: function redraw(g) { michael@0: g.selectAll(".resize").attr("transform", function(d) { michael@0: return "translate(" + xExtent[+/e$/.test(d)] + "," + yExtent[+/^s/.test(d)] + ")"; michael@0: }); michael@0: } michael@0: function redrawX(g) { michael@0: g.select(".extent").attr("x", xExtent[0]); michael@0: g.selectAll(".extent,.n>rect,.s>rect").attr("width", xExtent[1] - xExtent[0]); michael@0: } michael@0: function redrawY(g) { michael@0: g.select(".extent").attr("y", yExtent[0]); michael@0: g.selectAll(".extent,.e>rect,.w>rect").attr("height", yExtent[1] - yExtent[0]); michael@0: } michael@0: function brushstart() { michael@0: var target = this, eventTarget = d3.select(d3.event.target), event_ = event.of(target, arguments), g = d3.select(target), resizing = eventTarget.datum(), resizingX = !/^(n|s)$/.test(resizing) && x, resizingY = !/^(e|w)$/.test(resizing) && y, dragging = eventTarget.classed("extent"), dragRestore = d3_event_dragSuppress(), center, origin = d3.mouse(target), offset; michael@0: var w = d3.select(d3_window).on("keydown.brush", keydown).on("keyup.brush", keyup); michael@0: if (d3.event.changedTouches) { michael@0: w.on("touchmove.brush", brushmove).on("touchend.brush", brushend); michael@0: } else { michael@0: w.on("mousemove.brush", brushmove).on("mouseup.brush", brushend); michael@0: } michael@0: g.interrupt().selectAll("*").interrupt(); michael@0: if (dragging) { michael@0: origin[0] = xExtent[0] - origin[0]; michael@0: origin[1] = yExtent[0] - origin[1]; michael@0: } else if (resizing) { michael@0: var ex = +/w$/.test(resizing), ey = +/^n/.test(resizing); michael@0: offset = [ xExtent[1 - ex] - origin[0], yExtent[1 - ey] - origin[1] ]; michael@0: origin[0] = xExtent[ex]; michael@0: origin[1] = yExtent[ey]; michael@0: } else if (d3.event.altKey) center = origin.slice(); michael@0: g.style("pointer-events", "none").selectAll(".resize").style("display", null); michael@0: d3.select("body").style("cursor", eventTarget.style("cursor")); michael@0: event_({ michael@0: type: "brushstart" michael@0: }); michael@0: brushmove(); michael@0: function keydown() { michael@0: if (d3.event.keyCode == 32) { michael@0: if (!dragging) { michael@0: center = null; michael@0: origin[0] -= xExtent[1]; michael@0: origin[1] -= yExtent[1]; michael@0: dragging = 2; michael@0: } michael@0: d3_eventPreventDefault(); michael@0: } michael@0: } michael@0: function keyup() { michael@0: if (d3.event.keyCode == 32 && dragging == 2) { michael@0: origin[0] += xExtent[1]; michael@0: origin[1] += yExtent[1]; michael@0: dragging = 0; michael@0: d3_eventPreventDefault(); michael@0: } michael@0: } michael@0: function brushmove() { michael@0: var point = d3.mouse(target), moved = false; michael@0: if (offset) { michael@0: point[0] += offset[0]; michael@0: point[1] += offset[1]; michael@0: } michael@0: if (!dragging) { michael@0: if (d3.event.altKey) { michael@0: if (!center) center = [ (xExtent[0] + xExtent[1]) / 2, (yExtent[0] + yExtent[1]) / 2 ]; michael@0: origin[0] = xExtent[+(point[0] < center[0])]; michael@0: origin[1] = yExtent[+(point[1] < center[1])]; michael@0: } else center = null; michael@0: } michael@0: if (resizingX && move1(point, x, 0)) { michael@0: redrawX(g); michael@0: moved = true; michael@0: } michael@0: if (resizingY && move1(point, y, 1)) { michael@0: redrawY(g); michael@0: moved = true; michael@0: } michael@0: if (moved) { michael@0: redraw(g); michael@0: event_({ michael@0: type: "brush", michael@0: mode: dragging ? "move" : "resize" michael@0: }); michael@0: } michael@0: } michael@0: function move1(point, scale, i) { michael@0: var range = d3_scaleRange(scale), r0 = range[0], r1 = range[1], position = origin[i], extent = i ? yExtent : xExtent, size = extent[1] - extent[0], min, max; michael@0: if (dragging) { michael@0: r0 -= position; michael@0: r1 -= size + position; michael@0: } michael@0: min = (i ? yClamp : xClamp) ? Math.max(r0, Math.min(r1, point[i])) : point[i]; michael@0: if (dragging) { michael@0: max = (min += position) + size; michael@0: } else { michael@0: if (center) position = Math.max(r0, Math.min(r1, 2 * center[i] - min)); michael@0: if (position < min) { michael@0: max = min; michael@0: min = position; michael@0: } else { michael@0: max = position; michael@0: } michael@0: } michael@0: if (extent[0] != min || extent[1] != max) { michael@0: if (i) yExtentDomain = null; else xExtentDomain = null; michael@0: extent[0] = min; michael@0: extent[1] = max; michael@0: return true; michael@0: } michael@0: } michael@0: function brushend() { michael@0: brushmove(); michael@0: g.style("pointer-events", "all").selectAll(".resize").style("display", brush.empty() ? "none" : null); michael@0: d3.select("body").style("cursor", null); michael@0: w.on("mousemove.brush", null).on("mouseup.brush", null).on("touchmove.brush", null).on("touchend.brush", null).on("keydown.brush", null).on("keyup.brush", null); michael@0: dragRestore(); michael@0: event_({ michael@0: type: "brushend" michael@0: }); michael@0: } michael@0: } michael@0: brush.x = function(z) { michael@0: if (!arguments.length) return x; michael@0: x = z; michael@0: resizes = d3_svg_brushResizes[!x << 1 | !y]; michael@0: return brush; michael@0: }; michael@0: brush.y = function(z) { michael@0: if (!arguments.length) return y; michael@0: y = z; michael@0: resizes = d3_svg_brushResizes[!x << 1 | !y]; michael@0: return brush; michael@0: }; michael@0: brush.clamp = function(z) { michael@0: if (!arguments.length) return x && y ? [ xClamp, yClamp ] : x ? xClamp : y ? yClamp : null; michael@0: if (x && y) xClamp = !!z[0], yClamp = !!z[1]; else if (x) xClamp = !!z; else if (y) yClamp = !!z; michael@0: return brush; michael@0: }; michael@0: brush.extent = function(z) { michael@0: var x0, x1, y0, y1, t; michael@0: if (!arguments.length) { michael@0: if (x) { michael@0: if (xExtentDomain) { michael@0: x0 = xExtentDomain[0], x1 = xExtentDomain[1]; michael@0: } else { michael@0: x0 = xExtent[0], x1 = xExtent[1]; michael@0: if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1); michael@0: if (x1 < x0) t = x0, x0 = x1, x1 = t; michael@0: } michael@0: } michael@0: if (y) { michael@0: if (yExtentDomain) { michael@0: y0 = yExtentDomain[0], y1 = yExtentDomain[1]; michael@0: } else { michael@0: y0 = yExtent[0], y1 = yExtent[1]; michael@0: if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1); michael@0: if (y1 < y0) t = y0, y0 = y1, y1 = t; michael@0: } michael@0: } michael@0: return x && y ? [ [ x0, y0 ], [ x1, y1 ] ] : x ? [ x0, x1 ] : y && [ y0, y1 ]; michael@0: } michael@0: if (x) { michael@0: x0 = z[0], x1 = z[1]; michael@0: if (y) x0 = x0[0], x1 = x1[0]; michael@0: xExtentDomain = [ x0, x1 ]; michael@0: if (x.invert) x0 = x(x0), x1 = x(x1); michael@0: if (x1 < x0) t = x0, x0 = x1, x1 = t; michael@0: if (x0 != xExtent[0] || x1 != xExtent[1]) xExtent = [ x0, x1 ]; michael@0: } michael@0: if (y) { michael@0: y0 = z[0], y1 = z[1]; michael@0: if (x) y0 = y0[1], y1 = y1[1]; michael@0: yExtentDomain = [ y0, y1 ]; michael@0: if (y.invert) y0 = y(y0), y1 = y(y1); michael@0: if (y1 < y0) t = y0, y0 = y1, y1 = t; michael@0: if (y0 != yExtent[0] || y1 != yExtent[1]) yExtent = [ y0, y1 ]; michael@0: } michael@0: return brush; michael@0: }; michael@0: brush.clear = function() { michael@0: if (!brush.empty()) { michael@0: xExtent = [ 0, 0 ], yExtent = [ 0, 0 ]; michael@0: xExtentDomain = yExtentDomain = null; michael@0: } michael@0: return brush; michael@0: }; michael@0: brush.empty = function() { michael@0: return !!x && xExtent[0] == xExtent[1] || !!y && yExtent[0] == yExtent[1]; michael@0: }; michael@0: return d3.rebind(brush, event, "on"); michael@0: }; michael@0: var d3_svg_brushCursor = { michael@0: n: "ns-resize", michael@0: e: "ew-resize", michael@0: s: "ns-resize", michael@0: w: "ew-resize", michael@0: nw: "nwse-resize", michael@0: ne: "nesw-resize", michael@0: se: "nwse-resize", michael@0: sw: "nesw-resize" michael@0: }; michael@0: var d3_svg_brushResizes = [ [ "n", "e", "s", "w", "nw", "ne", "se", "sw" ], [ "e", "w" ], [ "n", "s" ], [] ]; michael@0: var d3_time_format = d3_time.format = d3_locale_enUS.timeFormat; michael@0: var d3_time_formatUtc = d3_time_format.utc; michael@0: var d3_time_formatIso = d3_time_formatUtc("%Y-%m-%dT%H:%M:%S.%LZ"); michael@0: d3_time_format.iso = Date.prototype.toISOString && +new Date("2000-01-01T00:00:00.000Z") ? d3_time_formatIsoNative : d3_time_formatIso; michael@0: function d3_time_formatIsoNative(date) { michael@0: return date.toISOString(); michael@0: } michael@0: d3_time_formatIsoNative.parse = function(string) { michael@0: var date = new Date(string); michael@0: return isNaN(date) ? null : date; michael@0: }; michael@0: d3_time_formatIsoNative.toString = d3_time_formatIso.toString; michael@0: d3_time.second = d3_time_interval(function(date) { michael@0: return new d3_date(Math.floor(date / 1e3) * 1e3); michael@0: }, function(date, offset) { michael@0: date.setTime(date.getTime() + Math.floor(offset) * 1e3); michael@0: }, function(date) { michael@0: return date.getSeconds(); michael@0: }); michael@0: d3_time.seconds = d3_time.second.range; michael@0: d3_time.seconds.utc = d3_time.second.utc.range; michael@0: d3_time.minute = d3_time_interval(function(date) { michael@0: return new d3_date(Math.floor(date / 6e4) * 6e4); michael@0: }, function(date, offset) { michael@0: date.setTime(date.getTime() + Math.floor(offset) * 6e4); michael@0: }, function(date) { michael@0: return date.getMinutes(); michael@0: }); michael@0: d3_time.minutes = d3_time.minute.range; michael@0: d3_time.minutes.utc = d3_time.minute.utc.range; michael@0: d3_time.hour = d3_time_interval(function(date) { michael@0: var timezone = date.getTimezoneOffset() / 60; michael@0: return new d3_date((Math.floor(date / 36e5 - timezone) + timezone) * 36e5); michael@0: }, function(date, offset) { michael@0: date.setTime(date.getTime() + Math.floor(offset) * 36e5); michael@0: }, function(date) { michael@0: return date.getHours(); michael@0: }); michael@0: d3_time.hours = d3_time.hour.range; michael@0: d3_time.hours.utc = d3_time.hour.utc.range; michael@0: d3_time.month = d3_time_interval(function(date) { michael@0: date = d3_time.day(date); michael@0: date.setDate(1); michael@0: return date; michael@0: }, function(date, offset) { michael@0: date.setMonth(date.getMonth() + offset); michael@0: }, function(date) { michael@0: return date.getMonth(); michael@0: }); michael@0: d3_time.months = d3_time.month.range; michael@0: d3_time.months.utc = d3_time.month.utc.range; michael@0: function d3_time_scale(linear, methods, format) { michael@0: function scale(x) { michael@0: return linear(x); michael@0: } michael@0: scale.invert = function(x) { michael@0: return d3_time_scaleDate(linear.invert(x)); michael@0: }; michael@0: scale.domain = function(x) { michael@0: if (!arguments.length) return linear.domain().map(d3_time_scaleDate); michael@0: linear.domain(x); michael@0: return scale; michael@0: }; michael@0: function tickMethod(extent, count) { michael@0: var span = extent[1] - extent[0], target = span / count, i = d3.bisect(d3_time_scaleSteps, target); michael@0: return i == d3_time_scaleSteps.length ? [ methods.year, d3_scale_linearTickRange(extent.map(function(d) { michael@0: return d / 31536e6; michael@0: }), count)[2] ] : !i ? [ d3_time_scaleMilliseconds, d3_scale_linearTickRange(extent, count)[2] ] : methods[target / d3_time_scaleSteps[i - 1] < d3_time_scaleSteps[i] / target ? i - 1 : i]; michael@0: } michael@0: scale.nice = function(interval, skip) { michael@0: var domain = scale.domain(), extent = d3_scaleExtent(domain), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" && tickMethod(extent, interval); michael@0: if (method) interval = method[0], skip = method[1]; michael@0: function skipped(date) { michael@0: return !isNaN(date) && !interval.range(date, d3_time_scaleDate(+date + 1), skip).length; michael@0: } michael@0: return scale.domain(d3_scale_nice(domain, skip > 1 ? { michael@0: floor: function(date) { michael@0: while (skipped(date = interval.floor(date))) date = d3_time_scaleDate(date - 1); michael@0: return date; michael@0: }, michael@0: ceil: function(date) { michael@0: while (skipped(date = interval.ceil(date))) date = d3_time_scaleDate(+date + 1); michael@0: return date; michael@0: } michael@0: } : interval)); michael@0: }; michael@0: scale.ticks = function(interval, skip) { michael@0: var extent = d3_scaleExtent(scale.domain()), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" ? tickMethod(extent, interval) : !interval.range && [ { michael@0: range: interval michael@0: }, skip ]; michael@0: if (method) interval = method[0], skip = method[1]; michael@0: return interval.range(extent[0], d3_time_scaleDate(+extent[1] + 1), skip < 1 ? 1 : skip); michael@0: }; michael@0: scale.tickFormat = function() { michael@0: return format; michael@0: }; michael@0: scale.copy = function() { michael@0: return d3_time_scale(linear.copy(), methods, format); michael@0: }; michael@0: return d3_scale_linearRebind(scale, linear); michael@0: } michael@0: function d3_time_scaleDate(t) { michael@0: return new Date(t); michael@0: } michael@0: var d3_time_scaleSteps = [ 1e3, 5e3, 15e3, 3e4, 6e4, 3e5, 9e5, 18e5, 36e5, 108e5, 216e5, 432e5, 864e5, 1728e5, 6048e5, 2592e6, 7776e6, 31536e6 ]; michael@0: var d3_time_scaleLocalMethods = [ [ d3_time.second, 1 ], [ d3_time.second, 5 ], [ d3_time.second, 15 ], [ d3_time.second, 30 ], [ d3_time.minute, 1 ], [ d3_time.minute, 5 ], [ d3_time.minute, 15 ], [ d3_time.minute, 30 ], [ d3_time.hour, 1 ], [ d3_time.hour, 3 ], [ d3_time.hour, 6 ], [ d3_time.hour, 12 ], [ d3_time.day, 1 ], [ d3_time.day, 2 ], [ d3_time.week, 1 ], [ d3_time.month, 1 ], [ d3_time.month, 3 ], [ d3_time.year, 1 ] ]; michael@0: var d3_time_scaleLocalFormat = d3_time_format.multi([ [ ".%L", function(d) { michael@0: return d.getMilliseconds(); michael@0: } ], [ ":%S", function(d) { michael@0: return d.getSeconds(); michael@0: } ], [ "%I:%M", function(d) { michael@0: return d.getMinutes(); michael@0: } ], [ "%I %p", function(d) { michael@0: return d.getHours(); michael@0: } ], [ "%a %d", function(d) { michael@0: return d.getDay() && d.getDate() != 1; michael@0: } ], [ "%b %d", function(d) { michael@0: return d.getDate() != 1; michael@0: } ], [ "%B", function(d) { michael@0: return d.getMonth(); michael@0: } ], [ "%Y", d3_true ] ]); michael@0: var d3_time_scaleMilliseconds = { michael@0: range: function(start, stop, step) { michael@0: return d3.range(+start, +stop, step).map(d3_time_scaleDate); michael@0: }, michael@0: floor: d3_identity, michael@0: ceil: d3_identity michael@0: }; michael@0: d3_time_scaleLocalMethods.year = d3_time.year; michael@0: d3_time.scale = function() { michael@0: return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat); michael@0: }; michael@0: var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) { michael@0: return [ m[0].utc, m[1] ]; michael@0: }); michael@0: var d3_time_scaleUtcFormat = d3_time_formatUtc.multi([ [ ".%L", function(d) { michael@0: return d.getUTCMilliseconds(); michael@0: } ], [ ":%S", function(d) { michael@0: return d.getUTCSeconds(); michael@0: } ], [ "%I:%M", function(d) { michael@0: return d.getUTCMinutes(); michael@0: } ], [ "%I %p", function(d) { michael@0: return d.getUTCHours(); michael@0: } ], [ "%a %d", function(d) { michael@0: return d.getUTCDay() && d.getUTCDate() != 1; michael@0: } ], [ "%b %d", function(d) { michael@0: return d.getUTCDate() != 1; michael@0: } ], [ "%B", function(d) { michael@0: return d.getUTCMonth(); michael@0: } ], [ "%Y", d3_true ] ]); michael@0: d3_time_scaleUtcMethods.year = d3_time.year.utc; michael@0: d3_time.scale.utc = function() { michael@0: return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_time_scaleUtcFormat); michael@0: }; michael@0: d3.text = d3_xhrType(function(request) { michael@0: return request.responseText; michael@0: }); michael@0: d3.json = function(url, callback) { michael@0: return d3_xhr(url, "application/json", d3_json, callback); michael@0: }; michael@0: function d3_json(request) { michael@0: return JSON.parse(request.responseText); michael@0: } michael@0: d3.html = function(url, callback) { michael@0: return d3_xhr(url, "text/html", d3_html, callback); michael@0: }; michael@0: function d3_html(request) { michael@0: var range = d3_document.createRange(); michael@0: range.selectNode(d3_document.body); michael@0: return range.createContextualFragment(request.responseText); michael@0: } michael@0: d3.xml = d3_xhrType(function(request) { michael@0: return request.responseXML; michael@0: }); michael@0: if (typeof define === "function" && define.amd) { michael@0: define(d3); michael@0: } else if (typeof module === "object" && module.exports) { michael@0: module.exports = d3; michael@0: } else { michael@0: this.d3 = d3; michael@0: } michael@0: }();