paulo@89: !function() { paulo@89: var d3 = { paulo@89: version: "3.5.17" paulo@89: }; paulo@89: var d3_arraySlice = [].slice, d3_array = function(list) { paulo@89: return d3_arraySlice.call(list); paulo@89: }; paulo@89: var d3_document = this.document; paulo@89: function d3_documentElement(node) { paulo@89: return node && (node.ownerDocument || node.document || node).documentElement; paulo@89: } paulo@89: function d3_window(node) { paulo@89: return node && (node.ownerDocument && node.ownerDocument.defaultView || node.document && node || node.defaultView); paulo@89: } paulo@89: if (d3_document) { paulo@89: try { paulo@89: d3_array(d3_document.documentElement.childNodes)[0].nodeType; paulo@89: } catch (e) { paulo@89: d3_array = function(list) { paulo@89: var i = list.length, array = new Array(i); paulo@89: while (i--) array[i] = list[i]; paulo@89: return array; paulo@89: }; paulo@89: } paulo@89: } paulo@89: if (!Date.now) Date.now = function() { paulo@89: return +new Date(); paulo@89: }; paulo@89: if (d3_document) { paulo@89: try { paulo@89: d3_document.createElement("DIV").style.setProperty("opacity", 0, ""); paulo@89: } catch (error) { paulo@89: var d3_element_prototype = this.Element.prototype, d3_element_setAttribute = d3_element_prototype.setAttribute, d3_element_setAttributeNS = d3_element_prototype.setAttributeNS, d3_style_prototype = this.CSSStyleDeclaration.prototype, d3_style_setProperty = d3_style_prototype.setProperty; paulo@89: d3_element_prototype.setAttribute = function(name, value) { paulo@89: d3_element_setAttribute.call(this, name, value + ""); paulo@89: }; paulo@89: d3_element_prototype.setAttributeNS = function(space, local, value) { paulo@89: d3_element_setAttributeNS.call(this, space, local, value + ""); paulo@89: }; paulo@89: d3_style_prototype.setProperty = function(name, value, priority) { paulo@89: d3_style_setProperty.call(this, name, value + "", priority); paulo@89: }; paulo@89: } paulo@89: } paulo@89: d3.ascending = d3_ascending; paulo@89: function d3_ascending(a, b) { paulo@89: return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN; paulo@89: } paulo@89: d3.descending = function(a, b) { paulo@89: return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN; paulo@89: }; paulo@89: d3.min = function(array, f) { paulo@89: var i = -1, n = array.length, a, b; paulo@89: if (arguments.length === 1) { paulo@89: while (++i < n) if ((b = array[i]) != null && b >= b) { paulo@89: a = b; paulo@89: break; paulo@89: } paulo@89: while (++i < n) if ((b = array[i]) != null && a > b) a = b; paulo@89: } else { paulo@89: while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { paulo@89: a = b; paulo@89: break; paulo@89: } paulo@89: while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b; paulo@89: } paulo@89: return a; paulo@89: }; paulo@89: d3.max = function(array, f) { paulo@89: var i = -1, n = array.length, a, b; paulo@89: if (arguments.length === 1) { paulo@89: while (++i < n) if ((b = array[i]) != null && b >= b) { paulo@89: a = b; paulo@89: break; paulo@89: } paulo@89: while (++i < n) if ((b = array[i]) != null && b > a) a = b; paulo@89: } else { paulo@89: while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { paulo@89: a = b; paulo@89: break; paulo@89: } paulo@89: while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b; paulo@89: } paulo@89: return a; paulo@89: }; paulo@89: d3.extent = function(array, f) { paulo@89: var i = -1, n = array.length, a, b, c; paulo@89: if (arguments.length === 1) { paulo@89: while (++i < n) if ((b = array[i]) != null && b >= b) { paulo@89: a = c = b; paulo@89: break; paulo@89: } paulo@89: while (++i < n) if ((b = array[i]) != null) { paulo@89: if (a > b) a = b; paulo@89: if (c < b) c = b; paulo@89: } paulo@89: } else { paulo@89: while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { paulo@89: a = c = b; paulo@89: break; paulo@89: } paulo@89: while (++i < n) if ((b = f.call(array, array[i], i)) != null) { paulo@89: if (a > b) a = b; paulo@89: if (c < b) c = b; paulo@89: } paulo@89: } paulo@89: return [ a, c ]; paulo@89: }; paulo@89: function d3_number(x) { paulo@89: return x === null ? NaN : +x; paulo@89: } paulo@89: function d3_numeric(x) { paulo@89: return !isNaN(x); paulo@89: } paulo@89: d3.sum = function(array, f) { paulo@89: var s = 0, n = array.length, a, i = -1; paulo@89: if (arguments.length === 1) { paulo@89: while (++i < n) if (d3_numeric(a = +array[i])) s += a; paulo@89: } else { paulo@89: while (++i < n) if (d3_numeric(a = +f.call(array, array[i], i))) s += a; paulo@89: } paulo@89: return s; paulo@89: }; paulo@89: d3.mean = function(array, f) { paulo@89: var s = 0, n = array.length, a, i = -1, j = n; paulo@89: if (arguments.length === 1) { paulo@89: while (++i < n) if (d3_numeric(a = d3_number(array[i]))) s += a; else --j; paulo@89: } else { paulo@89: while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) s += a; else --j; paulo@89: } paulo@89: if (j) return s / j; paulo@89: }; paulo@89: d3.quantile = function(values, p) { paulo@89: var H = (values.length - 1) * p + 1, h = Math.floor(H), v = +values[h - 1], e = H - h; paulo@89: return e ? v + e * (values[h] - v) : v; paulo@89: }; paulo@89: d3.median = function(array, f) { paulo@89: var numbers = [], n = array.length, a, i = -1; paulo@89: if (arguments.length === 1) { paulo@89: while (++i < n) if (d3_numeric(a = d3_number(array[i]))) numbers.push(a); paulo@89: } else { paulo@89: while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) numbers.push(a); paulo@89: } paulo@89: if (numbers.length) return d3.quantile(numbers.sort(d3_ascending), .5); paulo@89: }; paulo@89: d3.variance = function(array, f) { paulo@89: var n = array.length, m = 0, a, d, s = 0, i = -1, j = 0; paulo@89: if (arguments.length === 1) { paulo@89: while (++i < n) { paulo@89: if (d3_numeric(a = d3_number(array[i]))) { paulo@89: d = a - m; paulo@89: m += d / ++j; paulo@89: s += d * (a - m); paulo@89: } paulo@89: } paulo@89: } else { paulo@89: while (++i < n) { paulo@89: if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) { paulo@89: d = a - m; paulo@89: m += d / ++j; paulo@89: s += d * (a - m); paulo@89: } paulo@89: } paulo@89: } paulo@89: if (j > 1) return s / (j - 1); paulo@89: }; paulo@89: d3.deviation = function() { paulo@89: var v = d3.variance.apply(this, arguments); paulo@89: return v ? Math.sqrt(v) : v; paulo@89: }; paulo@89: function d3_bisector(compare) { paulo@89: return { paulo@89: left: function(a, x, lo, hi) { paulo@89: if (arguments.length < 3) lo = 0; paulo@89: if (arguments.length < 4) hi = a.length; paulo@89: while (lo < hi) { paulo@89: var mid = lo + hi >>> 1; paulo@89: if (compare(a[mid], x) < 0) lo = mid + 1; else hi = mid; paulo@89: } paulo@89: return lo; paulo@89: }, paulo@89: right: function(a, x, lo, hi) { paulo@89: if (arguments.length < 3) lo = 0; paulo@89: if (arguments.length < 4) hi = a.length; paulo@89: while (lo < hi) { paulo@89: var mid = lo + hi >>> 1; paulo@89: if (compare(a[mid], x) > 0) hi = mid; else lo = mid + 1; paulo@89: } paulo@89: return lo; paulo@89: } paulo@89: }; paulo@89: } paulo@89: var d3_bisect = d3_bisector(d3_ascending); paulo@89: d3.bisectLeft = d3_bisect.left; paulo@89: d3.bisect = d3.bisectRight = d3_bisect.right; paulo@89: d3.bisector = function(f) { paulo@89: return d3_bisector(f.length === 1 ? function(d, x) { paulo@89: return d3_ascending(f(d), x); paulo@89: } : f); paulo@89: }; paulo@89: d3.shuffle = function(array, i0, i1) { paulo@89: if ((m = arguments.length) < 3) { paulo@89: i1 = array.length; paulo@89: if (m < 2) i0 = 0; paulo@89: } paulo@89: var m = i1 - i0, t, i; paulo@89: while (m) { paulo@89: i = Math.random() * m-- | 0; paulo@89: t = array[m + i0], array[m + i0] = array[i + i0], array[i + i0] = t; paulo@89: } paulo@89: return array; paulo@89: }; paulo@89: d3.permute = function(array, indexes) { paulo@89: var i = indexes.length, permutes = new Array(i); paulo@89: while (i--) permutes[i] = array[indexes[i]]; paulo@89: return permutes; paulo@89: }; paulo@89: d3.pairs = function(array) { paulo@89: var i = 0, n = array.length - 1, p0, p1 = array[0], pairs = new Array(n < 0 ? 0 : n); paulo@89: while (i < n) pairs[i] = [ p0 = p1, p1 = array[++i] ]; paulo@89: return pairs; paulo@89: }; paulo@89: d3.transpose = function(matrix) { paulo@89: if (!(n = matrix.length)) return []; paulo@89: for (var i = -1, m = d3.min(matrix, d3_transposeLength), transpose = new Array(m); ++i < m; ) { paulo@89: for (var j = -1, n, row = transpose[i] = new Array(n); ++j < n; ) { paulo@89: row[j] = matrix[j][i]; paulo@89: } paulo@89: } paulo@89: return transpose; paulo@89: }; paulo@89: function d3_transposeLength(d) { paulo@89: return d.length; paulo@89: } paulo@89: d3.zip = function() { paulo@89: return d3.transpose(arguments); paulo@89: }; paulo@89: d3.keys = function(map) { paulo@89: var keys = []; paulo@89: for (var key in map) keys.push(key); paulo@89: return keys; paulo@89: }; paulo@89: d3.values = function(map) { paulo@89: var values = []; paulo@89: for (var key in map) values.push(map[key]); paulo@89: return values; paulo@89: }; paulo@89: d3.entries = function(map) { paulo@89: var entries = []; paulo@89: for (var key in map) entries.push({ paulo@89: key: key, paulo@89: value: map[key] paulo@89: }); paulo@89: return entries; paulo@89: }; paulo@89: d3.merge = function(arrays) { paulo@89: var n = arrays.length, m, i = -1, j = 0, merged, array; paulo@89: while (++i < n) j += arrays[i].length; paulo@89: merged = new Array(j); paulo@89: while (--n >= 0) { paulo@89: array = arrays[n]; paulo@89: m = array.length; paulo@89: while (--m >= 0) { paulo@89: merged[--j] = array[m]; paulo@89: } paulo@89: } paulo@89: return merged; paulo@89: }; paulo@89: var abs = Math.abs; paulo@89: d3.range = function(start, stop, step) { paulo@89: if (arguments.length < 3) { paulo@89: step = 1; paulo@89: if (arguments.length < 2) { paulo@89: stop = start; paulo@89: start = 0; paulo@89: } paulo@89: } paulo@89: if ((stop - start) / step === Infinity) throw new Error("infinite range"); paulo@89: var range = [], k = d3_range_integerScale(abs(step)), i = -1, j; paulo@89: start *= k, stop *= k, step *= k; paulo@89: if (step < 0) while ((j = start + step * ++i) > stop) range.push(j / k); else while ((j = start + step * ++i) < stop) range.push(j / k); paulo@89: return range; paulo@89: }; paulo@89: function d3_range_integerScale(x) { paulo@89: var k = 1; paulo@89: while (x * k % 1) k *= 10; paulo@89: return k; paulo@89: } paulo@89: function d3_class(ctor, properties) { paulo@89: for (var key in properties) { paulo@89: Object.defineProperty(ctor.prototype, key, { paulo@89: value: properties[key], paulo@89: enumerable: false paulo@89: }); paulo@89: } paulo@89: } paulo@89: d3.map = function(object, f) { paulo@89: var map = new d3_Map(); paulo@89: if (object instanceof d3_Map) { paulo@89: object.forEach(function(key, value) { paulo@89: map.set(key, value); paulo@89: }); paulo@89: } else if (Array.isArray(object)) { paulo@89: var i = -1, n = object.length, o; paulo@89: if (arguments.length === 1) while (++i < n) map.set(i, object[i]); else while (++i < n) map.set(f.call(object, o = object[i], i), o); paulo@89: } else { paulo@89: for (var key in object) map.set(key, object[key]); paulo@89: } paulo@89: return map; paulo@89: }; paulo@89: function d3_Map() { paulo@89: this._ = Object.create(null); paulo@89: } paulo@89: var d3_map_proto = "__proto__", d3_map_zero = "\x00"; paulo@89: d3_class(d3_Map, { paulo@89: has: d3_map_has, paulo@89: get: function(key) { paulo@89: return this._[d3_map_escape(key)]; paulo@89: }, paulo@89: set: function(key, value) { paulo@89: return this._[d3_map_escape(key)] = value; paulo@89: }, paulo@89: remove: d3_map_remove, paulo@89: keys: d3_map_keys, paulo@89: values: function() { paulo@89: var values = []; paulo@89: for (var key in this._) values.push(this._[key]); paulo@89: return values; paulo@89: }, paulo@89: entries: function() { paulo@89: var entries = []; paulo@89: for (var key in this._) entries.push({ paulo@89: key: d3_map_unescape(key), paulo@89: value: this._[key] paulo@89: }); paulo@89: return entries; paulo@89: }, paulo@89: size: d3_map_size, paulo@89: empty: d3_map_empty, paulo@89: forEach: function(f) { paulo@89: for (var key in this._) f.call(this, d3_map_unescape(key), this._[key]); paulo@89: } paulo@89: }); paulo@89: function d3_map_escape(key) { paulo@89: return (key += "") === d3_map_proto || key[0] === d3_map_zero ? d3_map_zero + key : key; paulo@89: } paulo@89: function d3_map_unescape(key) { paulo@89: return (key += "")[0] === d3_map_zero ? key.slice(1) : key; paulo@89: } paulo@89: function d3_map_has(key) { paulo@89: return d3_map_escape(key) in this._; paulo@89: } paulo@89: function d3_map_remove(key) { paulo@89: return (key = d3_map_escape(key)) in this._ && delete this._[key]; paulo@89: } paulo@89: function d3_map_keys() { paulo@89: var keys = []; paulo@89: for (var key in this._) keys.push(d3_map_unescape(key)); paulo@89: return keys; paulo@89: } paulo@89: function d3_map_size() { paulo@89: var size = 0; paulo@89: for (var key in this._) ++size; paulo@89: return size; paulo@89: } paulo@89: function d3_map_empty() { paulo@89: for (var key in this._) return false; paulo@89: return true; paulo@89: } paulo@89: d3.nest = function() { paulo@89: var nest = {}, keys = [], sortKeys = [], sortValues, rollup; paulo@89: function map(mapType, array, depth) { paulo@89: if (depth >= keys.length) return rollup ? rollup.call(nest, array) : sortValues ? array.sort(sortValues) : array; paulo@89: var i = -1, n = array.length, key = keys[depth++], keyValue, object, setter, valuesByKey = new d3_Map(), values; paulo@89: while (++i < n) { paulo@89: if (values = valuesByKey.get(keyValue = key(object = array[i]))) { paulo@89: values.push(object); paulo@89: } else { paulo@89: valuesByKey.set(keyValue, [ object ]); paulo@89: } paulo@89: } paulo@89: if (mapType) { paulo@89: object = mapType(); paulo@89: setter = function(keyValue, values) { paulo@89: object.set(keyValue, map(mapType, values, depth)); paulo@89: }; paulo@89: } else { paulo@89: object = {}; paulo@89: setter = function(keyValue, values) { paulo@89: object[keyValue] = map(mapType, values, depth); paulo@89: }; paulo@89: } paulo@89: valuesByKey.forEach(setter); paulo@89: return object; paulo@89: } paulo@89: function entries(map, depth) { paulo@89: if (depth >= keys.length) return map; paulo@89: var array = [], sortKey = sortKeys[depth++]; paulo@89: map.forEach(function(key, keyMap) { paulo@89: array.push({ paulo@89: key: key, paulo@89: values: entries(keyMap, depth) paulo@89: }); paulo@89: }); paulo@89: return sortKey ? array.sort(function(a, b) { paulo@89: return sortKey(a.key, b.key); paulo@89: }) : array; paulo@89: } paulo@89: nest.map = function(array, mapType) { paulo@89: return map(mapType, array, 0); paulo@89: }; paulo@89: nest.entries = function(array) { paulo@89: return entries(map(d3.map, array, 0), 0); paulo@89: }; paulo@89: nest.key = function(d) { paulo@89: keys.push(d); paulo@89: return nest; paulo@89: }; paulo@89: nest.sortKeys = function(order) { paulo@89: sortKeys[keys.length - 1] = order; paulo@89: return nest; paulo@89: }; paulo@89: nest.sortValues = function(order) { paulo@89: sortValues = order; paulo@89: return nest; paulo@89: }; paulo@89: nest.rollup = function(f) { paulo@89: rollup = f; paulo@89: return nest; paulo@89: }; paulo@89: return nest; paulo@89: }; paulo@89: d3.set = function(array) { paulo@89: var set = new d3_Set(); paulo@89: if (array) for (var i = 0, n = array.length; i < n; ++i) set.add(array[i]); paulo@89: return set; paulo@89: }; paulo@89: function d3_Set() { paulo@89: this._ = Object.create(null); paulo@89: } paulo@89: d3_class(d3_Set, { paulo@89: has: d3_map_has, paulo@89: add: function(key) { paulo@89: this._[d3_map_escape(key += "")] = true; paulo@89: return key; paulo@89: }, paulo@89: remove: d3_map_remove, paulo@89: values: d3_map_keys, paulo@89: size: d3_map_size, paulo@89: empty: d3_map_empty, paulo@89: forEach: function(f) { paulo@89: for (var key in this._) f.call(this, d3_map_unescape(key)); paulo@89: } paulo@89: }); paulo@89: d3.behavior = {}; paulo@89: function d3_identity(d) { paulo@89: return d; paulo@89: } paulo@89: d3.rebind = function(target, source) { paulo@89: var i = 1, n = arguments.length, method; paulo@89: while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]); paulo@89: return target; paulo@89: }; paulo@89: function d3_rebind(target, source, method) { paulo@89: return function() { paulo@89: var value = method.apply(source, arguments); paulo@89: return value === source ? target : value; paulo@89: }; paulo@89: } paulo@89: function d3_vendorSymbol(object, name) { paulo@89: if (name in object) return name; paulo@89: name = name.charAt(0).toUpperCase() + name.slice(1); paulo@89: for (var i = 0, n = d3_vendorPrefixes.length; i < n; ++i) { paulo@89: var prefixName = d3_vendorPrefixes[i] + name; paulo@89: if (prefixName in object) return prefixName; paulo@89: } paulo@89: } paulo@89: var d3_vendorPrefixes = [ "webkit", "ms", "moz", "Moz", "o", "O" ]; paulo@89: function d3_noop() {} paulo@89: d3.dispatch = function() { paulo@89: var dispatch = new d3_dispatch(), i = -1, n = arguments.length; paulo@89: while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); paulo@89: return dispatch; paulo@89: }; paulo@89: function d3_dispatch() {} paulo@89: d3_dispatch.prototype.on = function(type, listener) { paulo@89: var i = type.indexOf("."), name = ""; paulo@89: if (i >= 0) { paulo@89: name = type.slice(i + 1); paulo@89: type = type.slice(0, i); paulo@89: } paulo@89: if (type) return arguments.length < 2 ? this[type].on(name) : this[type].on(name, listener); paulo@89: if (arguments.length === 2) { paulo@89: if (listener == null) for (type in this) { paulo@89: if (this.hasOwnProperty(type)) this[type].on(name, null); paulo@89: } paulo@89: return this; paulo@89: } paulo@89: }; paulo@89: function d3_dispatch_event(dispatch) { paulo@89: var listeners = [], listenerByName = new d3_Map(); paulo@89: function event() { paulo@89: var z = listeners, i = -1, n = z.length, l; paulo@89: while (++i < n) if (l = z[i].on) l.apply(this, arguments); paulo@89: return dispatch; paulo@89: } paulo@89: event.on = function(name, listener) { paulo@89: var l = listenerByName.get(name), i; paulo@89: if (arguments.length < 2) return l && l.on; paulo@89: if (l) { paulo@89: l.on = null; paulo@89: listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1)); paulo@89: listenerByName.remove(name); paulo@89: } paulo@89: if (listener) listeners.push(listenerByName.set(name, { paulo@89: on: listener paulo@89: })); paulo@89: return dispatch; paulo@89: }; paulo@89: return event; paulo@89: } paulo@89: d3.event = null; paulo@89: function d3_eventPreventDefault() { paulo@89: d3.event.preventDefault(); paulo@89: } paulo@89: function d3_eventSource() { paulo@89: var e = d3.event, s; paulo@89: while (s = e.sourceEvent) e = s; paulo@89: return e; paulo@89: } paulo@89: function d3_eventDispatch(target) { paulo@89: var dispatch = new d3_dispatch(), i = 0, n = arguments.length; paulo@89: while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); paulo@89: dispatch.of = function(thiz, argumentz) { paulo@89: return function(e1) { paulo@89: try { paulo@89: var e0 = e1.sourceEvent = d3.event; paulo@89: e1.target = target; paulo@89: d3.event = e1; paulo@89: dispatch[e1.type].apply(thiz, argumentz); paulo@89: } finally { paulo@89: d3.event = e0; paulo@89: } paulo@89: }; paulo@89: }; paulo@89: return dispatch; paulo@89: } paulo@89: d3.requote = function(s) { paulo@89: return s.replace(d3_requote_re, "\\$&"); paulo@89: }; paulo@89: var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g; paulo@89: var d3_subclass = {}.__proto__ ? function(object, prototype) { paulo@89: object.__proto__ = prototype; paulo@89: } : function(object, prototype) { paulo@89: for (var property in prototype) object[property] = prototype[property]; paulo@89: }; paulo@89: function d3_selection(groups) { paulo@89: d3_subclass(groups, d3_selectionPrototype); paulo@89: return groups; paulo@89: } paulo@89: var d3_select = function(s, n) { paulo@89: return n.querySelector(s); paulo@89: }, d3_selectAll = function(s, n) { paulo@89: return n.querySelectorAll(s); paulo@89: }, d3_selectMatches = function(n, s) { paulo@89: var d3_selectMatcher = n.matches || n[d3_vendorSymbol(n, "matchesSelector")]; paulo@89: d3_selectMatches = function(n, s) { paulo@89: return d3_selectMatcher.call(n, s); paulo@89: }; paulo@89: return d3_selectMatches(n, s); paulo@89: }; paulo@89: if (typeof Sizzle === "function") { paulo@89: d3_select = function(s, n) { paulo@89: return Sizzle(s, n)[0] || null; paulo@89: }; paulo@89: d3_selectAll = Sizzle; paulo@89: d3_selectMatches = Sizzle.matchesSelector; paulo@89: } paulo@89: d3.selection = function() { paulo@89: return d3.select(d3_document.documentElement); paulo@89: }; paulo@89: var d3_selectionPrototype = d3.selection.prototype = []; paulo@89: d3_selectionPrototype.select = function(selector) { paulo@89: var subgroups = [], subgroup, subnode, group, node; paulo@89: selector = d3_selection_selector(selector); paulo@89: for (var j = -1, m = this.length; ++j < m; ) { paulo@89: subgroups.push(subgroup = []); paulo@89: subgroup.parentNode = (group = this[j]).parentNode; paulo@89: for (var i = -1, n = group.length; ++i < n; ) { paulo@89: if (node = group[i]) { paulo@89: subgroup.push(subnode = selector.call(node, node.__data__, i, j)); paulo@89: if (subnode && "__data__" in node) subnode.__data__ = node.__data__; paulo@89: } else { paulo@89: subgroup.push(null); paulo@89: } paulo@89: } paulo@89: } paulo@89: return d3_selection(subgroups); paulo@89: }; paulo@89: function d3_selection_selector(selector) { paulo@89: return typeof selector === "function" ? selector : function() { paulo@89: return d3_select(selector, this); paulo@89: }; paulo@89: } paulo@89: d3_selectionPrototype.selectAll = function(selector) { paulo@89: var subgroups = [], subgroup, node; paulo@89: selector = d3_selection_selectorAll(selector); paulo@89: for (var j = -1, m = this.length; ++j < m; ) { paulo@89: for (var group = this[j], i = -1, n = group.length; ++i < n; ) { paulo@89: if (node = group[i]) { paulo@89: subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i, j))); paulo@89: subgroup.parentNode = node; paulo@89: } paulo@89: } paulo@89: } paulo@89: return d3_selection(subgroups); paulo@89: }; paulo@89: function d3_selection_selectorAll(selector) { paulo@89: return typeof selector === "function" ? selector : function() { paulo@89: return d3_selectAll(selector, this); paulo@89: }; paulo@89: } paulo@89: var d3_nsXhtml = "http://www.w3.org/1999/xhtml"; paulo@89: var d3_nsPrefix = { paulo@89: svg: "http://www.w3.org/2000/svg", paulo@89: xhtml: d3_nsXhtml, paulo@89: xlink: "http://www.w3.org/1999/xlink", paulo@89: xml: "http://www.w3.org/XML/1998/namespace", paulo@89: xmlns: "http://www.w3.org/2000/xmlns/" paulo@89: }; paulo@89: d3.ns = { paulo@89: prefix: d3_nsPrefix, paulo@89: qualify: function(name) { paulo@89: var i = name.indexOf(":"), prefix = name; paulo@89: if (i >= 0 && (prefix = name.slice(0, i)) !== "xmlns") name = name.slice(i + 1); paulo@89: return d3_nsPrefix.hasOwnProperty(prefix) ? { paulo@89: space: d3_nsPrefix[prefix], paulo@89: local: name paulo@89: } : name; paulo@89: } paulo@89: }; paulo@89: d3_selectionPrototype.attr = function(name, value) { paulo@89: if (arguments.length < 2) { paulo@89: if (typeof name === "string") { paulo@89: var node = this.node(); paulo@89: name = d3.ns.qualify(name); paulo@89: return name.local ? node.getAttributeNS(name.space, name.local) : node.getAttribute(name); paulo@89: } paulo@89: for (value in name) this.each(d3_selection_attr(value, name[value])); paulo@89: return this; paulo@89: } paulo@89: return this.each(d3_selection_attr(name, value)); paulo@89: }; paulo@89: function d3_selection_attr(name, value) { paulo@89: name = d3.ns.qualify(name); paulo@89: function attrNull() { paulo@89: this.removeAttribute(name); paulo@89: } paulo@89: function attrNullNS() { paulo@89: this.removeAttributeNS(name.space, name.local); paulo@89: } paulo@89: function attrConstant() { paulo@89: this.setAttribute(name, value); paulo@89: } paulo@89: function attrConstantNS() { paulo@89: this.setAttributeNS(name.space, name.local, value); paulo@89: } paulo@89: function attrFunction() { paulo@89: var x = value.apply(this, arguments); paulo@89: if (x == null) this.removeAttribute(name); else this.setAttribute(name, x); paulo@89: } paulo@89: function attrFunctionNS() { paulo@89: var x = value.apply(this, arguments); paulo@89: if (x == null) this.removeAttributeNS(name.space, name.local); else this.setAttributeNS(name.space, name.local, x); paulo@89: } paulo@89: return value == null ? name.local ? attrNullNS : attrNull : typeof value === "function" ? name.local ? attrFunctionNS : attrFunction : name.local ? attrConstantNS : attrConstant; paulo@89: } paulo@89: function d3_collapse(s) { paulo@89: return s.trim().replace(/\s+/g, " "); paulo@89: } paulo@89: d3_selectionPrototype.classed = function(name, value) { paulo@89: if (arguments.length < 2) { paulo@89: if (typeof name === "string") { paulo@89: var node = this.node(), n = (name = d3_selection_classes(name)).length, i = -1; paulo@89: if (value = node.classList) { paulo@89: while (++i < n) if (!value.contains(name[i])) return false; paulo@89: } else { paulo@89: value = node.getAttribute("class"); paulo@89: while (++i < n) if (!d3_selection_classedRe(name[i]).test(value)) return false; paulo@89: } paulo@89: return true; paulo@89: } paulo@89: for (value in name) this.each(d3_selection_classed(value, name[value])); paulo@89: return this; paulo@89: } paulo@89: return this.each(d3_selection_classed(name, value)); paulo@89: }; paulo@89: function d3_selection_classedRe(name) { paulo@89: return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g"); paulo@89: } paulo@89: function d3_selection_classes(name) { paulo@89: return (name + "").trim().split(/^|\s+/); paulo@89: } paulo@89: function d3_selection_classed(name, value) { paulo@89: name = d3_selection_classes(name).map(d3_selection_classedName); paulo@89: var n = name.length; paulo@89: function classedConstant() { paulo@89: var i = -1; paulo@89: while (++i < n) name[i](this, value); paulo@89: } paulo@89: function classedFunction() { paulo@89: var i = -1, x = value.apply(this, arguments); paulo@89: while (++i < n) name[i](this, x); paulo@89: } paulo@89: return typeof value === "function" ? classedFunction : classedConstant; paulo@89: } paulo@89: function d3_selection_classedName(name) { paulo@89: var re = d3_selection_classedRe(name); paulo@89: return function(node, value) { paulo@89: if (c = node.classList) return value ? c.add(name) : c.remove(name); paulo@89: var c = node.getAttribute("class") || ""; paulo@89: if (value) { paulo@89: re.lastIndex = 0; paulo@89: if (!re.test(c)) node.setAttribute("class", d3_collapse(c + " " + name)); paulo@89: } else { paulo@89: node.setAttribute("class", d3_collapse(c.replace(re, " "))); paulo@89: } paulo@89: }; paulo@89: } paulo@89: d3_selectionPrototype.style = function(name, value, priority) { paulo@89: var n = arguments.length; paulo@89: if (n < 3) { paulo@89: if (typeof name !== "string") { paulo@89: if (n < 2) value = ""; paulo@89: for (priority in name) this.each(d3_selection_style(priority, name[priority], value)); paulo@89: return this; paulo@89: } paulo@89: if (n < 2) { paulo@89: var node = this.node(); paulo@89: return d3_window(node).getComputedStyle(node, null).getPropertyValue(name); paulo@89: } paulo@89: priority = ""; paulo@89: } paulo@89: return this.each(d3_selection_style(name, value, priority)); paulo@89: }; paulo@89: function d3_selection_style(name, value, priority) { paulo@89: function styleNull() { paulo@89: this.style.removeProperty(name); paulo@89: } paulo@89: function styleConstant() { paulo@89: this.style.setProperty(name, value, priority); paulo@89: } paulo@89: function styleFunction() { paulo@89: var x = value.apply(this, arguments); paulo@89: if (x == null) this.style.removeProperty(name); else this.style.setProperty(name, x, priority); paulo@89: } paulo@89: return value == null ? styleNull : typeof value === "function" ? styleFunction : styleConstant; paulo@89: } paulo@89: d3_selectionPrototype.property = function(name, value) { paulo@89: if (arguments.length < 2) { paulo@89: if (typeof name === "string") return this.node()[name]; paulo@89: for (value in name) this.each(d3_selection_property(value, name[value])); paulo@89: return this; paulo@89: } paulo@89: return this.each(d3_selection_property(name, value)); paulo@89: }; paulo@89: function d3_selection_property(name, value) { paulo@89: function propertyNull() { paulo@89: delete this[name]; paulo@89: } paulo@89: function propertyConstant() { paulo@89: this[name] = value; paulo@89: } paulo@89: function propertyFunction() { paulo@89: var x = value.apply(this, arguments); paulo@89: if (x == null) delete this[name]; else this[name] = x; paulo@89: } paulo@89: return value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant; paulo@89: } paulo@89: d3_selectionPrototype.text = function(value) { paulo@89: return arguments.length ? this.each(typeof value === "function" ? function() { paulo@89: var v = value.apply(this, arguments); paulo@89: this.textContent = v == null ? "" : v; paulo@89: } : value == null ? function() { paulo@89: this.textContent = ""; paulo@89: } : function() { paulo@89: this.textContent = value; paulo@89: }) : this.node().textContent; paulo@89: }; paulo@89: d3_selectionPrototype.html = function(value) { paulo@89: return arguments.length ? this.each(typeof value === "function" ? function() { paulo@89: var v = value.apply(this, arguments); paulo@89: this.innerHTML = v == null ? "" : v; paulo@89: } : value == null ? function() { paulo@89: this.innerHTML = ""; paulo@89: } : function() { paulo@89: this.innerHTML = value; paulo@89: }) : this.node().innerHTML; paulo@89: }; paulo@89: d3_selectionPrototype.append = function(name) { paulo@89: name = d3_selection_creator(name); paulo@89: return this.select(function() { paulo@89: return this.appendChild(name.apply(this, arguments)); paulo@89: }); paulo@89: }; paulo@89: function d3_selection_creator(name) { paulo@89: function create() { paulo@89: var document = this.ownerDocument, namespace = this.namespaceURI; paulo@89: return namespace === d3_nsXhtml && document.documentElement.namespaceURI === d3_nsXhtml ? document.createElement(name) : document.createElementNS(namespace, name); paulo@89: } paulo@89: function createNS() { paulo@89: return this.ownerDocument.createElementNS(name.space, name.local); paulo@89: } paulo@89: return typeof name === "function" ? name : (name = d3.ns.qualify(name)).local ? createNS : create; paulo@89: } paulo@89: d3_selectionPrototype.insert = function(name, before) { paulo@89: name = d3_selection_creator(name); paulo@89: before = d3_selection_selector(before); paulo@89: return this.select(function() { paulo@89: return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments) || null); paulo@89: }); paulo@89: }; paulo@89: d3_selectionPrototype.remove = function() { paulo@89: return this.each(d3_selectionRemove); paulo@89: }; paulo@89: function d3_selectionRemove() { paulo@89: var parent = this.parentNode; paulo@89: if (parent) parent.removeChild(this); paulo@89: } paulo@89: d3_selectionPrototype.data = function(value, key) { paulo@89: var i = -1, n = this.length, group, node; paulo@89: if (!arguments.length) { paulo@89: value = new Array(n = (group = this[0]).length); paulo@89: while (++i < n) { paulo@89: if (node = group[i]) { paulo@89: value[i] = node.__data__; paulo@89: } paulo@89: } paulo@89: return value; paulo@89: } paulo@89: function bind(group, groupData) { paulo@89: var i, n = group.length, m = groupData.length, n0 = Math.min(n, m), updateNodes = new Array(m), enterNodes = new Array(m), exitNodes = new Array(n), node, nodeData; paulo@89: if (key) { paulo@89: var nodeByKeyValue = new d3_Map(), keyValues = new Array(n), keyValue; paulo@89: for (i = -1; ++i < n; ) { paulo@89: if (node = group[i]) { paulo@89: if (nodeByKeyValue.has(keyValue = key.call(node, node.__data__, i))) { paulo@89: exitNodes[i] = node; paulo@89: } else { paulo@89: nodeByKeyValue.set(keyValue, node); paulo@89: } paulo@89: keyValues[i] = keyValue; paulo@89: } paulo@89: } paulo@89: for (i = -1; ++i < m; ) { paulo@89: if (!(node = nodeByKeyValue.get(keyValue = key.call(groupData, nodeData = groupData[i], i)))) { paulo@89: enterNodes[i] = d3_selection_dataNode(nodeData); paulo@89: } else if (node !== true) { paulo@89: updateNodes[i] = node; paulo@89: node.__data__ = nodeData; paulo@89: } paulo@89: nodeByKeyValue.set(keyValue, true); paulo@89: } paulo@89: for (i = -1; ++i < n; ) { paulo@89: if (i in keyValues && nodeByKeyValue.get(keyValues[i]) !== true) { paulo@89: exitNodes[i] = group[i]; paulo@89: } paulo@89: } paulo@89: } else { paulo@89: for (i = -1; ++i < n0; ) { paulo@89: node = group[i]; paulo@89: nodeData = groupData[i]; paulo@89: if (node) { paulo@89: node.__data__ = nodeData; paulo@89: updateNodes[i] = node; paulo@89: } else { paulo@89: enterNodes[i] = d3_selection_dataNode(nodeData); paulo@89: } paulo@89: } paulo@89: for (;i < m; ++i) { paulo@89: enterNodes[i] = d3_selection_dataNode(groupData[i]); paulo@89: } paulo@89: for (;i < n; ++i) { paulo@89: exitNodes[i] = group[i]; paulo@89: } paulo@89: } paulo@89: enterNodes.update = updateNodes; paulo@89: enterNodes.parentNode = updateNodes.parentNode = exitNodes.parentNode = group.parentNode; paulo@89: enter.push(enterNodes); paulo@89: update.push(updateNodes); paulo@89: exit.push(exitNodes); paulo@89: } paulo@89: var enter = d3_selection_enter([]), update = d3_selection([]), exit = d3_selection([]); paulo@89: if (typeof value === "function") { paulo@89: while (++i < n) { paulo@89: bind(group = this[i], value.call(group, group.parentNode.__data__, i)); paulo@89: } paulo@89: } else { paulo@89: while (++i < n) { paulo@89: bind(group = this[i], value); paulo@89: } paulo@89: } paulo@89: update.enter = function() { paulo@89: return enter; paulo@89: }; paulo@89: update.exit = function() { paulo@89: return exit; paulo@89: }; paulo@89: return update; paulo@89: }; paulo@89: function d3_selection_dataNode(data) { paulo@89: return { paulo@89: __data__: data paulo@89: }; paulo@89: } paulo@89: d3_selectionPrototype.datum = function(value) { paulo@89: return arguments.length ? this.property("__data__", value) : this.property("__data__"); paulo@89: }; paulo@89: d3_selectionPrototype.filter = function(filter) { paulo@89: var subgroups = [], subgroup, group, node; paulo@89: if (typeof filter !== "function") filter = d3_selection_filter(filter); paulo@89: for (var j = 0, m = this.length; j < m; j++) { paulo@89: subgroups.push(subgroup = []); paulo@89: subgroup.parentNode = (group = this[j]).parentNode; paulo@89: for (var i = 0, n = group.length; i < n; i++) { paulo@89: if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { paulo@89: subgroup.push(node); paulo@89: } paulo@89: } paulo@89: } paulo@89: return d3_selection(subgroups); paulo@89: }; paulo@89: function d3_selection_filter(selector) { paulo@89: return function() { paulo@89: return d3_selectMatches(this, selector); paulo@89: }; paulo@89: } paulo@89: d3_selectionPrototype.order = function() { paulo@89: for (var j = -1, m = this.length; ++j < m; ) { paulo@89: for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0; ) { paulo@89: if (node = group[i]) { paulo@89: if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next); paulo@89: next = node; paulo@89: } paulo@89: } paulo@89: } paulo@89: return this; paulo@89: }; paulo@89: d3_selectionPrototype.sort = function(comparator) { paulo@89: comparator = d3_selection_sortComparator.apply(this, arguments); paulo@89: for (var j = -1, m = this.length; ++j < m; ) this[j].sort(comparator); paulo@89: return this.order(); paulo@89: }; paulo@89: function d3_selection_sortComparator(comparator) { paulo@89: if (!arguments.length) comparator = d3_ascending; paulo@89: return function(a, b) { paulo@89: return a && b ? comparator(a.__data__, b.__data__) : !a - !b; paulo@89: }; paulo@89: } paulo@89: d3_selectionPrototype.each = function(callback) { paulo@89: return d3_selection_each(this, function(node, i, j) { paulo@89: callback.call(node, node.__data__, i, j); paulo@89: }); paulo@89: }; paulo@89: function d3_selection_each(groups, callback) { paulo@89: for (var j = 0, m = groups.length; j < m; j++) { paulo@89: for (var group = groups[j], i = 0, n = group.length, node; i < n; i++) { paulo@89: if (node = group[i]) callback(node, i, j); paulo@89: } paulo@89: } paulo@89: return groups; paulo@89: } paulo@89: d3_selectionPrototype.call = function(callback) { paulo@89: var args = d3_array(arguments); paulo@89: callback.apply(args[0] = this, args); paulo@89: return this; paulo@89: }; paulo@89: d3_selectionPrototype.empty = function() { paulo@89: return !this.node(); paulo@89: }; paulo@89: d3_selectionPrototype.node = function() { paulo@89: for (var j = 0, m = this.length; j < m; j++) { paulo@89: for (var group = this[j], i = 0, n = group.length; i < n; i++) { paulo@89: var node = group[i]; paulo@89: if (node) return node; paulo@89: } paulo@89: } paulo@89: return null; paulo@89: }; paulo@89: d3_selectionPrototype.size = function() { paulo@89: var n = 0; paulo@89: d3_selection_each(this, function() { paulo@89: ++n; paulo@89: }); paulo@89: return n; paulo@89: }; paulo@89: function d3_selection_enter(selection) { paulo@89: d3_subclass(selection, d3_selection_enterPrototype); paulo@89: return selection; paulo@89: } paulo@89: var d3_selection_enterPrototype = []; paulo@89: d3.selection.enter = d3_selection_enter; paulo@89: d3.selection.enter.prototype = d3_selection_enterPrototype; paulo@89: d3_selection_enterPrototype.append = d3_selectionPrototype.append; paulo@89: d3_selection_enterPrototype.empty = d3_selectionPrototype.empty; paulo@89: d3_selection_enterPrototype.node = d3_selectionPrototype.node; paulo@89: d3_selection_enterPrototype.call = d3_selectionPrototype.call; paulo@89: d3_selection_enterPrototype.size = d3_selectionPrototype.size; paulo@89: d3_selection_enterPrototype.select = function(selector) { paulo@89: var subgroups = [], subgroup, subnode, upgroup, group, node; paulo@89: for (var j = -1, m = this.length; ++j < m; ) { paulo@89: upgroup = (group = this[j]).update; paulo@89: subgroups.push(subgroup = []); paulo@89: subgroup.parentNode = group.parentNode; paulo@89: for (var i = -1, n = group.length; ++i < n; ) { paulo@89: if (node = group[i]) { paulo@89: subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i, j)); paulo@89: subnode.__data__ = node.__data__; paulo@89: } else { paulo@89: subgroup.push(null); paulo@89: } paulo@89: } paulo@89: } paulo@89: return d3_selection(subgroups); paulo@89: }; paulo@89: d3_selection_enterPrototype.insert = function(name, before) { paulo@89: if (arguments.length < 2) before = d3_selection_enterInsertBefore(this); paulo@89: return d3_selectionPrototype.insert.call(this, name, before); paulo@89: }; paulo@89: function d3_selection_enterInsertBefore(enter) { paulo@89: var i0, j0; paulo@89: return function(d, i, j) { paulo@89: var group = enter[j].update, n = group.length, node; paulo@89: if (j != j0) j0 = j, i0 = 0; paulo@89: if (i >= i0) i0 = i + 1; paulo@89: while (!(node = group[i0]) && ++i0 < n) ; paulo@89: return node; paulo@89: }; paulo@89: } paulo@89: d3.select = function(node) { paulo@89: var group; paulo@89: if (typeof node === "string") { paulo@89: group = [ d3_select(node, d3_document) ]; paulo@89: group.parentNode = d3_document.documentElement; paulo@89: } else { paulo@89: group = [ node ]; paulo@89: group.parentNode = d3_documentElement(node); paulo@89: } paulo@89: return d3_selection([ group ]); paulo@89: }; paulo@89: d3.selectAll = function(nodes) { paulo@89: var group; paulo@89: if (typeof nodes === "string") { paulo@89: group = d3_array(d3_selectAll(nodes, d3_document)); paulo@89: group.parentNode = d3_document.documentElement; paulo@89: } else { paulo@89: group = d3_array(nodes); paulo@89: group.parentNode = null; paulo@89: } paulo@89: return d3_selection([ group ]); paulo@89: }; paulo@89: d3_selectionPrototype.on = function(type, listener, capture) { paulo@89: var n = arguments.length; paulo@89: if (n < 3) { paulo@89: if (typeof type !== "string") { paulo@89: if (n < 2) listener = false; paulo@89: for (capture in type) this.each(d3_selection_on(capture, type[capture], listener)); paulo@89: return this; paulo@89: } paulo@89: if (n < 2) return (n = this.node()["__on" + type]) && n._; paulo@89: capture = false; paulo@89: } paulo@89: return this.each(d3_selection_on(type, listener, capture)); paulo@89: }; paulo@89: function d3_selection_on(type, listener, capture) { paulo@89: var name = "__on" + type, i = type.indexOf("."), wrap = d3_selection_onListener; paulo@89: if (i > 0) type = type.slice(0, i); paulo@89: var filter = d3_selection_onFilters.get(type); paulo@89: if (filter) type = filter, wrap = d3_selection_onFilter; paulo@89: function onRemove() { paulo@89: var l = this[name]; paulo@89: if (l) { paulo@89: this.removeEventListener(type, l, l.$); paulo@89: delete this[name]; paulo@89: } paulo@89: } paulo@89: function onAdd() { paulo@89: var l = wrap(listener, d3_array(arguments)); paulo@89: onRemove.call(this); paulo@89: this.addEventListener(type, this[name] = l, l.$ = capture); paulo@89: l._ = listener; paulo@89: } paulo@89: function removeAll() { paulo@89: var re = new RegExp("^__on([^.]+)" + d3.requote(type) + "$"), match; paulo@89: for (var name in this) { paulo@89: if (match = name.match(re)) { paulo@89: var l = this[name]; paulo@89: this.removeEventListener(match[1], l, l.$); paulo@89: delete this[name]; paulo@89: } paulo@89: } paulo@89: } paulo@89: return i ? listener ? onAdd : onRemove : listener ? d3_noop : removeAll; paulo@89: } paulo@89: var d3_selection_onFilters = d3.map({ paulo@89: mouseenter: "mouseover", paulo@89: mouseleave: "mouseout" paulo@89: }); paulo@89: if (d3_document) { paulo@89: d3_selection_onFilters.forEach(function(k) { paulo@89: if ("on" + k in d3_document) d3_selection_onFilters.remove(k); paulo@89: }); paulo@89: } paulo@89: function d3_selection_onListener(listener, argumentz) { paulo@89: return function(e) { paulo@89: var o = d3.event; paulo@89: d3.event = e; paulo@89: argumentz[0] = this.__data__; paulo@89: try { paulo@89: listener.apply(this, argumentz); paulo@89: } finally { paulo@89: d3.event = o; paulo@89: } paulo@89: }; paulo@89: } paulo@89: function d3_selection_onFilter(listener, argumentz) { paulo@89: var l = d3_selection_onListener(listener, argumentz); paulo@89: return function(e) { paulo@89: var target = this, related = e.relatedTarget; paulo@89: if (!related || related !== target && !(related.compareDocumentPosition(target) & 8)) { paulo@89: l.call(target, e); paulo@89: } paulo@89: }; paulo@89: } paulo@89: var d3_event_dragSelect, d3_event_dragId = 0; paulo@89: function d3_event_dragSuppress(node) { paulo@89: var name = ".dragsuppress-" + ++d3_event_dragId, click = "click" + name, w = d3.select(d3_window(node)).on("touchmove" + name, d3_eventPreventDefault).on("dragstart" + name, d3_eventPreventDefault).on("selectstart" + name, d3_eventPreventDefault); paulo@89: if (d3_event_dragSelect == null) { paulo@89: d3_event_dragSelect = "onselectstart" in node ? false : d3_vendorSymbol(node.style, "userSelect"); paulo@89: } paulo@89: if (d3_event_dragSelect) { paulo@89: var style = d3_documentElement(node).style, select = style[d3_event_dragSelect]; paulo@89: style[d3_event_dragSelect] = "none"; paulo@89: } paulo@89: return function(suppressClick) { paulo@89: w.on(name, null); paulo@89: if (d3_event_dragSelect) style[d3_event_dragSelect] = select; paulo@89: if (suppressClick) { paulo@89: var off = function() { paulo@89: w.on(click, null); paulo@89: }; paulo@89: w.on(click, function() { paulo@89: d3_eventPreventDefault(); paulo@89: off(); paulo@89: }, true); paulo@89: setTimeout(off, 0); paulo@89: } paulo@89: }; paulo@89: } paulo@89: d3.mouse = function(container) { paulo@89: return d3_mousePoint(container, d3_eventSource()); paulo@89: }; paulo@89: var d3_mouse_bug44083 = this.navigator && /WebKit/.test(this.navigator.userAgent) ? -1 : 0; paulo@89: function d3_mousePoint(container, e) { paulo@89: if (e.changedTouches) e = e.changedTouches[0]; paulo@89: var svg = container.ownerSVGElement || container; paulo@89: if (svg.createSVGPoint) { paulo@89: var point = svg.createSVGPoint(); paulo@89: if (d3_mouse_bug44083 < 0) { paulo@89: var window = d3_window(container); paulo@89: if (window.scrollX || window.scrollY) { paulo@89: svg = d3.select("body").append("svg").style({ paulo@89: position: "absolute", paulo@89: top: 0, paulo@89: left: 0, paulo@89: margin: 0, paulo@89: padding: 0, paulo@89: border: "none" paulo@89: }, "important"); paulo@89: var ctm = svg[0][0].getScreenCTM(); paulo@89: d3_mouse_bug44083 = !(ctm.f || ctm.e); paulo@89: svg.remove(); paulo@89: } paulo@89: } paulo@89: if (d3_mouse_bug44083) point.x = e.pageX, point.y = e.pageY; else point.x = e.clientX, paulo@89: point.y = e.clientY; paulo@89: point = point.matrixTransform(container.getScreenCTM().inverse()); paulo@89: return [ point.x, point.y ]; paulo@89: } paulo@89: var rect = container.getBoundingClientRect(); paulo@89: return [ e.clientX - rect.left - container.clientLeft, e.clientY - rect.top - container.clientTop ]; paulo@89: } paulo@89: d3.touch = function(container, touches, identifier) { paulo@89: if (arguments.length < 3) identifier = touches, touches = d3_eventSource().changedTouches; paulo@89: if (touches) for (var i = 0, n = touches.length, touch; i < n; ++i) { paulo@89: if ((touch = touches[i]).identifier === identifier) { paulo@89: return d3_mousePoint(container, touch); paulo@89: } paulo@89: } paulo@89: }; paulo@89: d3.behavior.drag = function() { paulo@89: var event = d3_eventDispatch(drag, "drag", "dragstart", "dragend"), origin = null, mousedown = dragstart(d3_noop, d3.mouse, d3_window, "mousemove", "mouseup"), touchstart = dragstart(d3_behavior_dragTouchId, d3.touch, d3_identity, "touchmove", "touchend"); paulo@89: function drag() { paulo@89: this.on("mousedown.drag", mousedown).on("touchstart.drag", touchstart); paulo@89: } paulo@89: function dragstart(id, position, subject, move, end) { paulo@89: return function() { paulo@89: var that = this, target = d3.event.target.correspondingElement || d3.event.target, parent = that.parentNode, dispatch = event.of(that, arguments), dragged = 0, dragId = id(), dragName = ".drag" + (dragId == null ? "" : "-" + dragId), dragOffset, dragSubject = d3.select(subject(target)).on(move + dragName, moved).on(end + dragName, ended), dragRestore = d3_event_dragSuppress(target), position0 = position(parent, dragId); paulo@89: if (origin) { paulo@89: dragOffset = origin.apply(that, arguments); paulo@89: dragOffset = [ dragOffset.x - position0[0], dragOffset.y - position0[1] ]; paulo@89: } else { paulo@89: dragOffset = [ 0, 0 ]; paulo@89: } paulo@89: dispatch({ paulo@89: type: "dragstart" paulo@89: }); paulo@89: function moved() { paulo@89: var position1 = position(parent, dragId), dx, dy; paulo@89: if (!position1) return; paulo@89: dx = position1[0] - position0[0]; paulo@89: dy = position1[1] - position0[1]; paulo@89: dragged |= dx | dy; paulo@89: position0 = position1; paulo@89: dispatch({ paulo@89: type: "drag", paulo@89: x: position1[0] + dragOffset[0], paulo@89: y: position1[1] + dragOffset[1], paulo@89: dx: dx, paulo@89: dy: dy paulo@89: }); paulo@89: } paulo@89: function ended() { paulo@89: if (!position(parent, dragId)) return; paulo@89: dragSubject.on(move + dragName, null).on(end + dragName, null); paulo@89: dragRestore(dragged); paulo@89: dispatch({ paulo@89: type: "dragend" paulo@89: }); paulo@89: } paulo@89: }; paulo@89: } paulo@89: drag.origin = function(x) { paulo@89: if (!arguments.length) return origin; paulo@89: origin = x; paulo@89: return drag; paulo@89: }; paulo@89: return d3.rebind(drag, event, "on"); paulo@89: }; paulo@89: function d3_behavior_dragTouchId() { paulo@89: return d3.event.changedTouches[0].identifier; paulo@89: } paulo@89: d3.touches = function(container, touches) { paulo@89: if (arguments.length < 2) touches = d3_eventSource().touches; paulo@89: return touches ? d3_array(touches).map(function(touch) { paulo@89: var point = d3_mousePoint(container, touch); paulo@89: point.identifier = touch.identifier; paulo@89: return point; paulo@89: }) : []; paulo@89: }; paulo@89: var ε = 1e-6, ε2 = ε * ε, π = Math.PI, τ = 2 * π, τε = τ - ε, halfπ = π / 2, d3_radians = π / 180, d3_degrees = 180 / π; paulo@89: function d3_sgn(x) { paulo@89: return x > 0 ? 1 : x < 0 ? -1 : 0; paulo@89: } paulo@89: function d3_cross2d(a, b, c) { paulo@89: return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); paulo@89: } paulo@89: function d3_acos(x) { paulo@89: return x > 1 ? 0 : x < -1 ? π : Math.acos(x); paulo@89: } paulo@89: function d3_asin(x) { paulo@89: return x > 1 ? halfπ : x < -1 ? -halfπ : Math.asin(x); paulo@89: } paulo@89: function d3_sinh(x) { paulo@89: return ((x = Math.exp(x)) - 1 / x) / 2; paulo@89: } paulo@89: function d3_cosh(x) { paulo@89: return ((x = Math.exp(x)) + 1 / x) / 2; paulo@89: } paulo@89: function d3_tanh(x) { paulo@89: return ((x = Math.exp(2 * x)) - 1) / (x + 1); paulo@89: } paulo@89: function d3_haversin(x) { paulo@89: return (x = Math.sin(x / 2)) * x; paulo@89: } paulo@89: var ρ = Math.SQRT2, ρ2 = 2, ρ4 = 4; paulo@89: d3.interpolateZoom = function(p0, p1) { paulo@89: var ux0 = p0[0], uy0 = p0[1], w0 = p0[2], ux1 = p1[0], uy1 = p1[1], w1 = p1[2], dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, i, S; paulo@89: if (d2 < ε2) { paulo@89: S = Math.log(w1 / w0) / ρ; paulo@89: i = function(t) { paulo@89: return [ ux0 + t * dx, uy0 + t * dy, w0 * Math.exp(ρ * t * S) ]; paulo@89: }; paulo@89: } else { paulo@89: var d1 = Math.sqrt(d2), b0 = (w1 * w1 - w0 * w0 + ρ4 * d2) / (2 * w0 * ρ2 * d1), b1 = (w1 * w1 - w0 * w0 - ρ4 * d2) / (2 * w1 * ρ2 * d1), r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0), r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1); paulo@89: S = (r1 - r0) / ρ; paulo@89: i = function(t) { paulo@89: var s = t * S, coshr0 = d3_cosh(r0), u = w0 / (ρ2 * d1) * (coshr0 * d3_tanh(ρ * s + r0) - d3_sinh(r0)); paulo@89: return [ ux0 + u * dx, uy0 + u * dy, w0 * coshr0 / d3_cosh(ρ * s + r0) ]; paulo@89: }; paulo@89: } paulo@89: i.duration = S * 1e3; paulo@89: return i; paulo@89: }; paulo@89: d3.behavior.zoom = function() { paulo@89: var view = { paulo@89: x: 0, paulo@89: y: 0, paulo@89: k: 1 paulo@89: }, translate0, center0, center, size = [ 960, 500 ], scaleExtent = d3_behavior_zoomInfinity, duration = 250, zooming = 0, mousedown = "mousedown.zoom", mousemove = "mousemove.zoom", mouseup = "mouseup.zoom", mousewheelTimer, touchstart = "touchstart.zoom", touchtime, event = d3_eventDispatch(zoom, "zoomstart", "zoom", "zoomend"), x0, x1, y0, y1; paulo@89: if (!d3_behavior_zoomWheel) { paulo@89: d3_behavior_zoomWheel = "onwheel" in d3_document ? (d3_behavior_zoomDelta = function() { paulo@89: return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1); paulo@89: }, "wheel") : "onmousewheel" in d3_document ? (d3_behavior_zoomDelta = function() { paulo@89: return d3.event.wheelDelta; paulo@89: }, "mousewheel") : (d3_behavior_zoomDelta = function() { paulo@89: return -d3.event.detail; paulo@89: }, "MozMousePixelScroll"); paulo@89: } paulo@89: function zoom(g) { paulo@89: g.on(mousedown, mousedowned).on(d3_behavior_zoomWheel + ".zoom", mousewheeled).on("dblclick.zoom", dblclicked).on(touchstart, touchstarted); paulo@89: } paulo@89: zoom.event = function(g) { paulo@89: g.each(function() { paulo@89: var dispatch = event.of(this, arguments), view1 = view; paulo@89: if (d3_transitionInheritId) { paulo@89: d3.select(this).transition().each("start.zoom", function() { paulo@89: view = this.__chart__ || { paulo@89: x: 0, paulo@89: y: 0, paulo@89: k: 1 paulo@89: }; paulo@89: zoomstarted(dispatch); paulo@89: }).tween("zoom:zoom", function() { paulo@89: var dx = size[0], dy = size[1], cx = center0 ? center0[0] : dx / 2, cy = center0 ? center0[1] : dy / 2, i = d3.interpolateZoom([ (cx - view.x) / view.k, (cy - view.y) / view.k, dx / view.k ], [ (cx - view1.x) / view1.k, (cy - view1.y) / view1.k, dx / view1.k ]); paulo@89: return function(t) { paulo@89: var l = i(t), k = dx / l[2]; paulo@89: this.__chart__ = view = { paulo@89: x: cx - l[0] * k, paulo@89: y: cy - l[1] * k, paulo@89: k: k paulo@89: }; paulo@89: zoomed(dispatch); paulo@89: }; paulo@89: }).each("interrupt.zoom", function() { paulo@89: zoomended(dispatch); paulo@89: }).each("end.zoom", function() { paulo@89: zoomended(dispatch); paulo@89: }); paulo@89: } else { paulo@89: this.__chart__ = view; paulo@89: zoomstarted(dispatch); paulo@89: zoomed(dispatch); paulo@89: zoomended(dispatch); paulo@89: } paulo@89: }); paulo@89: }; paulo@89: zoom.translate = function(_) { paulo@89: if (!arguments.length) return [ view.x, view.y ]; paulo@89: view = { paulo@89: x: +_[0], paulo@89: y: +_[1], paulo@89: k: view.k paulo@89: }; paulo@89: rescale(); paulo@89: return zoom; paulo@89: }; paulo@89: zoom.scale = function(_) { paulo@89: if (!arguments.length) return view.k; paulo@89: view = { paulo@89: x: view.x, paulo@89: y: view.y, paulo@89: k: null paulo@89: }; paulo@89: scaleTo(+_); paulo@89: rescale(); paulo@89: return zoom; paulo@89: }; paulo@89: zoom.scaleExtent = function(_) { paulo@89: if (!arguments.length) return scaleExtent; paulo@89: scaleExtent = _ == null ? d3_behavior_zoomInfinity : [ +_[0], +_[1] ]; paulo@89: return zoom; paulo@89: }; paulo@89: zoom.center = function(_) { paulo@89: if (!arguments.length) return center; paulo@89: center = _ && [ +_[0], +_[1] ]; paulo@89: return zoom; paulo@89: }; paulo@89: zoom.size = function(_) { paulo@89: if (!arguments.length) return size; paulo@89: size = _ && [ +_[0], +_[1] ]; paulo@89: return zoom; paulo@89: }; paulo@89: zoom.duration = function(_) { paulo@89: if (!arguments.length) return duration; paulo@89: duration = +_; paulo@89: return zoom; paulo@89: }; paulo@89: zoom.x = function(z) { paulo@89: if (!arguments.length) return x1; paulo@89: x1 = z; paulo@89: x0 = z.copy(); paulo@89: view = { paulo@89: x: 0, paulo@89: y: 0, paulo@89: k: 1 paulo@89: }; paulo@89: return zoom; paulo@89: }; paulo@89: zoom.y = function(z) { paulo@89: if (!arguments.length) return y1; paulo@89: y1 = z; paulo@89: y0 = z.copy(); paulo@89: view = { paulo@89: x: 0, paulo@89: y: 0, paulo@89: k: 1 paulo@89: }; paulo@89: return zoom; paulo@89: }; paulo@89: function location(p) { paulo@89: return [ (p[0] - view.x) / view.k, (p[1] - view.y) / view.k ]; paulo@89: } paulo@89: function point(l) { paulo@89: return [ l[0] * view.k + view.x, l[1] * view.k + view.y ]; paulo@89: } paulo@89: function scaleTo(s) { paulo@89: view.k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], s)); paulo@89: } paulo@89: function translateTo(p, l) { paulo@89: l = point(l); paulo@89: view.x += p[0] - l[0]; paulo@89: view.y += p[1] - l[1]; paulo@89: } paulo@89: function zoomTo(that, p, l, k) { paulo@89: that.__chart__ = { paulo@89: x: view.x, paulo@89: y: view.y, paulo@89: k: view.k paulo@89: }; paulo@89: scaleTo(Math.pow(2, k)); paulo@89: translateTo(center0 = p, l); paulo@89: that = d3.select(that); paulo@89: if (duration > 0) that = that.transition().duration(duration); paulo@89: that.call(zoom.event); paulo@89: } paulo@89: function rescale() { paulo@89: if (x1) x1.domain(x0.range().map(function(x) { paulo@89: return (x - view.x) / view.k; paulo@89: }).map(x0.invert)); paulo@89: if (y1) y1.domain(y0.range().map(function(y) { paulo@89: return (y - view.y) / view.k; paulo@89: }).map(y0.invert)); paulo@89: } paulo@89: function zoomstarted(dispatch) { paulo@89: if (!zooming++) dispatch({ paulo@89: type: "zoomstart" paulo@89: }); paulo@89: } paulo@89: function zoomed(dispatch) { paulo@89: rescale(); paulo@89: dispatch({ paulo@89: type: "zoom", paulo@89: scale: view.k, paulo@89: translate: [ view.x, view.y ] paulo@89: }); paulo@89: } paulo@89: function zoomended(dispatch) { paulo@89: if (!--zooming) dispatch({ paulo@89: type: "zoomend" paulo@89: }), center0 = null; paulo@89: } paulo@89: function mousedowned() { paulo@89: var that = this, dispatch = event.of(that, arguments), dragged = 0, subject = d3.select(d3_window(that)).on(mousemove, moved).on(mouseup, ended), location0 = location(d3.mouse(that)), dragRestore = d3_event_dragSuppress(that); paulo@89: d3_selection_interrupt.call(that); paulo@89: zoomstarted(dispatch); paulo@89: function moved() { paulo@89: dragged = 1; paulo@89: translateTo(d3.mouse(that), location0); paulo@89: zoomed(dispatch); paulo@89: } paulo@89: function ended() { paulo@89: subject.on(mousemove, null).on(mouseup, null); paulo@89: dragRestore(dragged); paulo@89: zoomended(dispatch); paulo@89: } paulo@89: } paulo@89: function touchstarted() { paulo@89: var that = this, dispatch = event.of(that, arguments), locations0 = {}, distance0 = 0, scale0, zoomName = ".zoom-" + d3.event.changedTouches[0].identifier, touchmove = "touchmove" + zoomName, touchend = "touchend" + zoomName, targets = [], subject = d3.select(that), dragRestore = d3_event_dragSuppress(that); paulo@89: started(); paulo@89: zoomstarted(dispatch); paulo@89: subject.on(mousedown, null).on(touchstart, started); paulo@89: function relocate() { paulo@89: var touches = d3.touches(that); paulo@89: scale0 = view.k; paulo@89: touches.forEach(function(t) { paulo@89: if (t.identifier in locations0) locations0[t.identifier] = location(t); paulo@89: }); paulo@89: return touches; paulo@89: } paulo@89: function started() { paulo@89: var target = d3.event.target; paulo@89: d3.select(target).on(touchmove, moved).on(touchend, ended); paulo@89: targets.push(target); paulo@89: var changed = d3.event.changedTouches; paulo@89: for (var i = 0, n = changed.length; i < n; ++i) { paulo@89: locations0[changed[i].identifier] = null; paulo@89: } paulo@89: var touches = relocate(), now = Date.now(); paulo@89: if (touches.length === 1) { paulo@89: if (now - touchtime < 500) { paulo@89: var p = touches[0]; paulo@89: zoomTo(that, p, locations0[p.identifier], Math.floor(Math.log(view.k) / Math.LN2) + 1); paulo@89: d3_eventPreventDefault(); paulo@89: } paulo@89: touchtime = now; paulo@89: } else if (touches.length > 1) { paulo@89: var p = touches[0], q = touches[1], dx = p[0] - q[0], dy = p[1] - q[1]; paulo@89: distance0 = dx * dx + dy * dy; paulo@89: } paulo@89: } paulo@89: function moved() { paulo@89: var touches = d3.touches(that), p0, l0, p1, l1; paulo@89: d3_selection_interrupt.call(that); paulo@89: for (var i = 0, n = touches.length; i < n; ++i, l1 = null) { paulo@89: p1 = touches[i]; paulo@89: if (l1 = locations0[p1.identifier]) { paulo@89: if (l0) break; paulo@89: p0 = p1, l0 = l1; paulo@89: } paulo@89: } paulo@89: if (l1) { paulo@89: var distance1 = (distance1 = p1[0] - p0[0]) * distance1 + (distance1 = p1[1] - p0[1]) * distance1, scale1 = distance0 && Math.sqrt(distance1 / distance0); paulo@89: p0 = [ (p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2 ]; paulo@89: l0 = [ (l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2 ]; paulo@89: scaleTo(scale1 * scale0); paulo@89: } paulo@89: touchtime = null; paulo@89: translateTo(p0, l0); paulo@89: zoomed(dispatch); paulo@89: } paulo@89: function ended() { paulo@89: if (d3.event.touches.length) { paulo@89: var changed = d3.event.changedTouches; paulo@89: for (var i = 0, n = changed.length; i < n; ++i) { paulo@89: delete locations0[changed[i].identifier]; paulo@89: } paulo@89: for (var identifier in locations0) { paulo@89: return void relocate(); paulo@89: } paulo@89: } paulo@89: d3.selectAll(targets).on(zoomName, null); paulo@89: subject.on(mousedown, mousedowned).on(touchstart, touchstarted); paulo@89: dragRestore(); paulo@89: zoomended(dispatch); paulo@89: } paulo@89: } paulo@89: function mousewheeled() { paulo@89: var dispatch = event.of(this, arguments); paulo@89: if (mousewheelTimer) clearTimeout(mousewheelTimer); else d3_selection_interrupt.call(this), paulo@89: translate0 = location(center0 = center || d3.mouse(this)), zoomstarted(dispatch); paulo@89: mousewheelTimer = setTimeout(function() { paulo@89: mousewheelTimer = null; paulo@89: zoomended(dispatch); paulo@89: }, 50); paulo@89: d3_eventPreventDefault(); paulo@89: scaleTo(Math.pow(2, d3_behavior_zoomDelta() * .002) * view.k); paulo@89: translateTo(center0, translate0); paulo@89: zoomed(dispatch); paulo@89: } paulo@89: function dblclicked() { paulo@89: var p = d3.mouse(this), k = Math.log(view.k) / Math.LN2; paulo@89: zoomTo(this, p, location(p), d3.event.shiftKey ? Math.ceil(k) - 1 : Math.floor(k) + 1); paulo@89: } paulo@89: return d3.rebind(zoom, event, "on"); paulo@89: }; paulo@89: var d3_behavior_zoomInfinity = [ 0, Infinity ], d3_behavior_zoomDelta, d3_behavior_zoomWheel; paulo@89: d3.color = d3_color; paulo@89: function d3_color() {} paulo@89: d3_color.prototype.toString = function() { paulo@89: return this.rgb() + ""; paulo@89: }; paulo@89: d3.hsl = d3_hsl; paulo@89: function d3_hsl(h, s, l) { paulo@89: return this instanceof d3_hsl ? void (this.h = +h, this.s = +s, this.l = +l) : arguments.length < 2 ? h instanceof d3_hsl ? new d3_hsl(h.h, h.s, h.l) : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl) : new d3_hsl(h, s, l); paulo@89: } paulo@89: var d3_hslPrototype = d3_hsl.prototype = new d3_color(); paulo@89: d3_hslPrototype.brighter = function(k) { paulo@89: k = Math.pow(.7, arguments.length ? k : 1); paulo@89: return new d3_hsl(this.h, this.s, this.l / k); paulo@89: }; paulo@89: d3_hslPrototype.darker = function(k) { paulo@89: k = Math.pow(.7, arguments.length ? k : 1); paulo@89: return new d3_hsl(this.h, this.s, k * this.l); paulo@89: }; paulo@89: d3_hslPrototype.rgb = function() { paulo@89: return d3_hsl_rgb(this.h, this.s, this.l); paulo@89: }; paulo@89: function d3_hsl_rgb(h, s, l) { paulo@89: var m1, m2; paulo@89: h = isNaN(h) ? 0 : (h %= 360) < 0 ? h + 360 : h; paulo@89: s = isNaN(s) ? 0 : s < 0 ? 0 : s > 1 ? 1 : s; paulo@89: l = l < 0 ? 0 : l > 1 ? 1 : l; paulo@89: m2 = l <= .5 ? l * (1 + s) : l + s - l * s; paulo@89: m1 = 2 * l - m2; paulo@89: function v(h) { paulo@89: if (h > 360) h -= 360; else if (h < 0) h += 360; paulo@89: if (h < 60) return m1 + (m2 - m1) * h / 60; paulo@89: if (h < 180) return m2; paulo@89: if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60; paulo@89: return m1; paulo@89: } paulo@89: function vv(h) { paulo@89: return Math.round(v(h) * 255); paulo@89: } paulo@89: return new d3_rgb(vv(h + 120), vv(h), vv(h - 120)); paulo@89: } paulo@89: d3.hcl = d3_hcl; paulo@89: function d3_hcl(h, c, l) { paulo@89: return this instanceof d3_hcl ? void (this.h = +h, this.c = +c, this.l = +l) : arguments.length < 2 ? h instanceof d3_hcl ? new d3_hcl(h.h, h.c, h.l) : h instanceof d3_lab ? d3_lab_hcl(h.l, h.a, h.b) : d3_lab_hcl((h = d3_rgb_lab((h = d3.rgb(h)).r, h.g, h.b)).l, h.a, h.b) : new d3_hcl(h, c, l); paulo@89: } paulo@89: var d3_hclPrototype = d3_hcl.prototype = new d3_color(); paulo@89: d3_hclPrototype.brighter = function(k) { paulo@89: return new d3_hcl(this.h, this.c, Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1))); paulo@89: }; paulo@89: d3_hclPrototype.darker = function(k) { paulo@89: return new d3_hcl(this.h, this.c, Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1))); paulo@89: }; paulo@89: d3_hclPrototype.rgb = function() { paulo@89: return d3_hcl_lab(this.h, this.c, this.l).rgb(); paulo@89: }; paulo@89: function d3_hcl_lab(h, c, l) { paulo@89: if (isNaN(h)) h = 0; paulo@89: if (isNaN(c)) c = 0; paulo@89: return new d3_lab(l, Math.cos(h *= d3_radians) * c, Math.sin(h) * c); paulo@89: } paulo@89: d3.lab = d3_lab; paulo@89: function d3_lab(l, a, b) { paulo@89: return this instanceof d3_lab ? void (this.l = +l, this.a = +a, this.b = +b) : arguments.length < 2 ? l instanceof d3_lab ? new d3_lab(l.l, l.a, l.b) : l instanceof d3_hcl ? d3_hcl_lab(l.h, l.c, l.l) : d3_rgb_lab((l = d3_rgb(l)).r, l.g, l.b) : new d3_lab(l, a, b); paulo@89: } paulo@89: var d3_lab_K = 18; paulo@89: var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883; paulo@89: var d3_labPrototype = d3_lab.prototype = new d3_color(); paulo@89: d3_labPrototype.brighter = function(k) { paulo@89: return new d3_lab(Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); paulo@89: }; paulo@89: d3_labPrototype.darker = function(k) { paulo@89: return new d3_lab(Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); paulo@89: }; paulo@89: d3_labPrototype.rgb = function() { paulo@89: return d3_lab_rgb(this.l, this.a, this.b); paulo@89: }; paulo@89: function d3_lab_rgb(l, a, b) { paulo@89: var y = (l + 16) / 116, x = y + a / 500, z = y - b / 200; paulo@89: x = d3_lab_xyz(x) * d3_lab_X; paulo@89: y = d3_lab_xyz(y) * d3_lab_Y; paulo@89: z = d3_lab_xyz(z) * d3_lab_Z; paulo@89: return new d3_rgb(d3_xyz_rgb(3.2404542 * x - 1.5371385 * y - .4985314 * z), d3_xyz_rgb(-.969266 * x + 1.8760108 * y + .041556 * z), d3_xyz_rgb(.0556434 * x - .2040259 * y + 1.0572252 * z)); paulo@89: } paulo@89: function d3_lab_hcl(l, a, b) { paulo@89: return l > 0 ? new d3_hcl(Math.atan2(b, a) * d3_degrees, Math.sqrt(a * a + b * b), l) : new d3_hcl(NaN, NaN, l); paulo@89: } paulo@89: function d3_lab_xyz(x) { paulo@89: return x > .206893034 ? x * x * x : (x - 4 / 29) / 7.787037; paulo@89: } paulo@89: function d3_xyz_lab(x) { paulo@89: return x > .008856 ? Math.pow(x, 1 / 3) : 7.787037 * x + 4 / 29; paulo@89: } paulo@89: function d3_xyz_rgb(r) { paulo@89: return Math.round(255 * (r <= .00304 ? 12.92 * r : 1.055 * Math.pow(r, 1 / 2.4) - .055)); paulo@89: } paulo@89: d3.rgb = d3_rgb; paulo@89: function d3_rgb(r, g, b) { paulo@89: return this instanceof d3_rgb ? void (this.r = ~~r, this.g = ~~g, this.b = ~~b) : arguments.length < 2 ? r instanceof d3_rgb ? new d3_rgb(r.r, r.g, r.b) : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb) : new d3_rgb(r, g, b); paulo@89: } paulo@89: function d3_rgbNumber(value) { paulo@89: return new d3_rgb(value >> 16, value >> 8 & 255, value & 255); paulo@89: } paulo@89: function d3_rgbString(value) { paulo@89: return d3_rgbNumber(value) + ""; paulo@89: } paulo@89: var d3_rgbPrototype = d3_rgb.prototype = new d3_color(); paulo@89: d3_rgbPrototype.brighter = function(k) { paulo@89: k = Math.pow(.7, arguments.length ? k : 1); paulo@89: var r = this.r, g = this.g, b = this.b, i = 30; paulo@89: if (!r && !g && !b) return new d3_rgb(i, i, i); paulo@89: if (r && r < i) r = i; paulo@89: if (g && g < i) g = i; paulo@89: if (b && b < i) b = i; paulo@89: return new d3_rgb(Math.min(255, r / k), Math.min(255, g / k), Math.min(255, b / k)); paulo@89: }; paulo@89: d3_rgbPrototype.darker = function(k) { paulo@89: k = Math.pow(.7, arguments.length ? k : 1); paulo@89: return new d3_rgb(k * this.r, k * this.g, k * this.b); paulo@89: }; paulo@89: d3_rgbPrototype.hsl = function() { paulo@89: return d3_rgb_hsl(this.r, this.g, this.b); paulo@89: }; paulo@89: d3_rgbPrototype.toString = function() { paulo@89: return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b); paulo@89: }; paulo@89: function d3_rgb_hex(v) { paulo@89: return v < 16 ? "0" + Math.max(0, v).toString(16) : Math.min(255, v).toString(16); paulo@89: } paulo@89: function d3_rgb_parse(format, rgb, hsl) { paulo@89: var r = 0, g = 0, b = 0, m1, m2, color; paulo@89: m1 = /([a-z]+)\((.*)\)/.exec(format = format.toLowerCase()); paulo@89: if (m1) { paulo@89: m2 = m1[2].split(","); paulo@89: switch (m1[1]) { paulo@89: case "hsl": paulo@89: { paulo@89: return hsl(parseFloat(m2[0]), parseFloat(m2[1]) / 100, parseFloat(m2[2]) / 100); paulo@89: } paulo@89: paulo@89: case "rgb": paulo@89: { paulo@89: return rgb(d3_rgb_parseNumber(m2[0]), d3_rgb_parseNumber(m2[1]), d3_rgb_parseNumber(m2[2])); paulo@89: } paulo@89: } paulo@89: } paulo@89: if (color = d3_rgb_names.get(format)) { paulo@89: return rgb(color.r, color.g, color.b); paulo@89: } paulo@89: if (format != null && format.charAt(0) === "#" && !isNaN(color = parseInt(format.slice(1), 16))) { paulo@89: if (format.length === 4) { paulo@89: r = (color & 3840) >> 4; paulo@89: r = r >> 4 | r; paulo@89: g = color & 240; paulo@89: g = g >> 4 | g; paulo@89: b = color & 15; paulo@89: b = b << 4 | b; paulo@89: } else if (format.length === 7) { paulo@89: r = (color & 16711680) >> 16; paulo@89: g = (color & 65280) >> 8; paulo@89: b = color & 255; paulo@89: } paulo@89: } paulo@89: return rgb(r, g, b); paulo@89: } paulo@89: function d3_rgb_hsl(r, g, b) { paulo@89: var min = Math.min(r /= 255, g /= 255, b /= 255), max = Math.max(r, g, b), d = max - min, h, s, l = (max + min) / 2; paulo@89: if (d) { paulo@89: s = l < .5 ? d / (max + min) : d / (2 - max - min); paulo@89: if (r == max) h = (g - b) / d + (g < b ? 6 : 0); else if (g == max) h = (b - r) / d + 2; else h = (r - g) / d + 4; paulo@89: h *= 60; paulo@89: } else { paulo@89: h = NaN; paulo@89: s = l > 0 && l < 1 ? 0 : h; paulo@89: } paulo@89: return new d3_hsl(h, s, l); paulo@89: } paulo@89: function d3_rgb_lab(r, g, b) { paulo@89: r = d3_rgb_xyz(r); paulo@89: g = d3_rgb_xyz(g); paulo@89: b = d3_rgb_xyz(b); paulo@89: var x = d3_xyz_lab((.4124564 * r + .3575761 * g + .1804375 * b) / d3_lab_X), y = d3_xyz_lab((.2126729 * r + .7151522 * g + .072175 * b) / d3_lab_Y), z = d3_xyz_lab((.0193339 * r + .119192 * g + .9503041 * b) / d3_lab_Z); paulo@89: return d3_lab(116 * y - 16, 500 * (x - y), 200 * (y - z)); paulo@89: } paulo@89: function d3_rgb_xyz(r) { paulo@89: return (r /= 255) <= .04045 ? r / 12.92 : Math.pow((r + .055) / 1.055, 2.4); paulo@89: } paulo@89: function d3_rgb_parseNumber(c) { paulo@89: var f = parseFloat(c); paulo@89: return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f; paulo@89: } paulo@89: var d3_rgb_names = d3.map({ paulo@89: aliceblue: 15792383, paulo@89: antiquewhite: 16444375, paulo@89: aqua: 65535, paulo@89: aquamarine: 8388564, paulo@89: azure: 15794175, paulo@89: beige: 16119260, paulo@89: bisque: 16770244, paulo@89: black: 0, paulo@89: blanchedalmond: 16772045, paulo@89: blue: 255, paulo@89: blueviolet: 9055202, paulo@89: brown: 10824234, paulo@89: burlywood: 14596231, paulo@89: cadetblue: 6266528, paulo@89: chartreuse: 8388352, paulo@89: chocolate: 13789470, paulo@89: coral: 16744272, paulo@89: cornflowerblue: 6591981, paulo@89: cornsilk: 16775388, paulo@89: crimson: 14423100, paulo@89: cyan: 65535, paulo@89: darkblue: 139, paulo@89: darkcyan: 35723, paulo@89: darkgoldenrod: 12092939, paulo@89: darkgray: 11119017, paulo@89: darkgreen: 25600, paulo@89: darkgrey: 11119017, paulo@89: darkkhaki: 12433259, paulo@89: darkmagenta: 9109643, paulo@89: darkolivegreen: 5597999, paulo@89: darkorange: 16747520, paulo@89: darkorchid: 10040012, paulo@89: darkred: 9109504, paulo@89: darksalmon: 15308410, paulo@89: darkseagreen: 9419919, paulo@89: darkslateblue: 4734347, paulo@89: darkslategray: 3100495, paulo@89: darkslategrey: 3100495, paulo@89: darkturquoise: 52945, paulo@89: darkviolet: 9699539, paulo@89: deeppink: 16716947, paulo@89: deepskyblue: 49151, paulo@89: dimgray: 6908265, paulo@89: dimgrey: 6908265, paulo@89: dodgerblue: 2003199, paulo@89: firebrick: 11674146, paulo@89: floralwhite: 16775920, paulo@89: forestgreen: 2263842, paulo@89: fuchsia: 16711935, paulo@89: gainsboro: 14474460, paulo@89: ghostwhite: 16316671, paulo@89: gold: 16766720, paulo@89: goldenrod: 14329120, paulo@89: gray: 8421504, paulo@89: green: 32768, paulo@89: greenyellow: 11403055, paulo@89: grey: 8421504, paulo@89: honeydew: 15794160, paulo@89: hotpink: 16738740, paulo@89: indianred: 13458524, paulo@89: indigo: 4915330, paulo@89: ivory: 16777200, paulo@89: khaki: 15787660, paulo@89: lavender: 15132410, paulo@89: lavenderblush: 16773365, paulo@89: lawngreen: 8190976, paulo@89: lemonchiffon: 16775885, paulo@89: lightblue: 11393254, paulo@89: lightcoral: 15761536, paulo@89: lightcyan: 14745599, paulo@89: lightgoldenrodyellow: 16448210, paulo@89: lightgray: 13882323, paulo@89: lightgreen: 9498256, paulo@89: lightgrey: 13882323, paulo@89: lightpink: 16758465, paulo@89: lightsalmon: 16752762, paulo@89: lightseagreen: 2142890, paulo@89: lightskyblue: 8900346, paulo@89: lightslategray: 7833753, paulo@89: lightslategrey: 7833753, paulo@89: lightsteelblue: 11584734, paulo@89: lightyellow: 16777184, paulo@89: lime: 65280, paulo@89: limegreen: 3329330, paulo@89: linen: 16445670, paulo@89: magenta: 16711935, paulo@89: maroon: 8388608, paulo@89: mediumaquamarine: 6737322, paulo@89: mediumblue: 205, paulo@89: mediumorchid: 12211667, paulo@89: mediumpurple: 9662683, paulo@89: mediumseagreen: 3978097, paulo@89: mediumslateblue: 8087790, paulo@89: mediumspringgreen: 64154, paulo@89: mediumturquoise: 4772300, paulo@89: mediumvioletred: 13047173, paulo@89: midnightblue: 1644912, paulo@89: mintcream: 16121850, paulo@89: mistyrose: 16770273, paulo@89: moccasin: 16770229, paulo@89: navajowhite: 16768685, paulo@89: navy: 128, paulo@89: oldlace: 16643558, paulo@89: olive: 8421376, paulo@89: olivedrab: 7048739, paulo@89: orange: 16753920, paulo@89: orangered: 16729344, paulo@89: orchid: 14315734, paulo@89: palegoldenrod: 15657130, paulo@89: palegreen: 10025880, paulo@89: paleturquoise: 11529966, paulo@89: palevioletred: 14381203, paulo@89: papayawhip: 16773077, paulo@89: peachpuff: 16767673, paulo@89: peru: 13468991, paulo@89: pink: 16761035, paulo@89: plum: 14524637, paulo@89: powderblue: 11591910, paulo@89: purple: 8388736, paulo@89: rebeccapurple: 6697881, paulo@89: red: 16711680, paulo@89: rosybrown: 12357519, paulo@89: royalblue: 4286945, paulo@89: saddlebrown: 9127187, paulo@89: salmon: 16416882, paulo@89: sandybrown: 16032864, paulo@89: seagreen: 3050327, paulo@89: seashell: 16774638, paulo@89: sienna: 10506797, paulo@89: silver: 12632256, paulo@89: skyblue: 8900331, paulo@89: slateblue: 6970061, paulo@89: slategray: 7372944, paulo@89: slategrey: 7372944, paulo@89: snow: 16775930, paulo@89: springgreen: 65407, paulo@89: steelblue: 4620980, paulo@89: tan: 13808780, paulo@89: teal: 32896, paulo@89: thistle: 14204888, paulo@89: tomato: 16737095, paulo@89: turquoise: 4251856, paulo@89: violet: 15631086, paulo@89: wheat: 16113331, paulo@89: white: 16777215, paulo@89: whitesmoke: 16119285, paulo@89: yellow: 16776960, paulo@89: yellowgreen: 10145074 paulo@89: }); paulo@89: d3_rgb_names.forEach(function(key, value) { paulo@89: d3_rgb_names.set(key, d3_rgbNumber(value)); paulo@89: }); paulo@89: function d3_functor(v) { paulo@89: return typeof v === "function" ? v : function() { paulo@89: return v; paulo@89: }; paulo@89: } paulo@89: d3.functor = d3_functor; paulo@89: d3.xhr = d3_xhrType(d3_identity); paulo@89: function d3_xhrType(response) { paulo@89: return function(url, mimeType, callback) { paulo@89: if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType, paulo@89: mimeType = null; paulo@89: return d3_xhr(url, mimeType, response, callback); paulo@89: }; paulo@89: } paulo@89: function d3_xhr(url, mimeType, response, callback) { paulo@89: var xhr = {}, dispatch = d3.dispatch("beforesend", "progress", "load", "error"), headers = {}, request = new XMLHttpRequest(), responseType = null; paulo@89: if (this.XDomainRequest && !("withCredentials" in request) && /^(http(s)?:)?\/\//.test(url)) request = new XDomainRequest(); paulo@89: "onload" in request ? request.onload = request.onerror = respond : request.onreadystatechange = function() { paulo@89: request.readyState > 3 && respond(); paulo@89: }; paulo@89: function respond() { paulo@89: var status = request.status, result; paulo@89: if (!status && d3_xhrHasResponse(request) || status >= 200 && status < 300 || status === 304) { paulo@89: try { paulo@89: result = response.call(xhr, request); paulo@89: } catch (e) { paulo@89: dispatch.error.call(xhr, e); paulo@89: return; paulo@89: } paulo@89: dispatch.load.call(xhr, result); paulo@89: } else { paulo@89: dispatch.error.call(xhr, request); paulo@89: } paulo@89: } paulo@89: request.onprogress = function(event) { paulo@89: var o = d3.event; paulo@89: d3.event = event; paulo@89: try { paulo@89: dispatch.progress.call(xhr, request); paulo@89: } finally { paulo@89: d3.event = o; paulo@89: } paulo@89: }; paulo@89: xhr.header = function(name, value) { paulo@89: name = (name + "").toLowerCase(); paulo@89: if (arguments.length < 2) return headers[name]; paulo@89: if (value == null) delete headers[name]; else headers[name] = value + ""; paulo@89: return xhr; paulo@89: }; paulo@89: xhr.mimeType = function(value) { paulo@89: if (!arguments.length) return mimeType; paulo@89: mimeType = value == null ? null : value + ""; paulo@89: return xhr; paulo@89: }; paulo@89: xhr.responseType = function(value) { paulo@89: if (!arguments.length) return responseType; paulo@89: responseType = value; paulo@89: return xhr; paulo@89: }; paulo@89: xhr.response = function(value) { paulo@89: response = value; paulo@89: return xhr; paulo@89: }; paulo@89: [ "get", "post" ].forEach(function(method) { paulo@89: xhr[method] = function() { paulo@89: return xhr.send.apply(xhr, [ method ].concat(d3_array(arguments))); paulo@89: }; paulo@89: }); paulo@89: xhr.send = function(method, data, callback) { paulo@89: if (arguments.length === 2 && typeof data === "function") callback = data, data = null; paulo@89: request.open(method, url, true); paulo@89: if (mimeType != null && !("accept" in headers)) headers["accept"] = mimeType + ",*/*"; paulo@89: if (request.setRequestHeader) for (var name in headers) request.setRequestHeader(name, headers[name]); paulo@89: if (mimeType != null && request.overrideMimeType) request.overrideMimeType(mimeType); paulo@89: if (responseType != null) request.responseType = responseType; paulo@89: if (callback != null) xhr.on("error", callback).on("load", function(request) { paulo@89: callback(null, request); paulo@89: }); paulo@89: dispatch.beforesend.call(xhr, request); paulo@89: request.send(data == null ? null : data); paulo@89: return xhr; paulo@89: }; paulo@89: xhr.abort = function() { paulo@89: request.abort(); paulo@89: return xhr; paulo@89: }; paulo@89: d3.rebind(xhr, dispatch, "on"); paulo@89: return callback == null ? xhr : xhr.get(d3_xhr_fixCallback(callback)); paulo@89: } paulo@89: function d3_xhr_fixCallback(callback) { paulo@89: return callback.length === 1 ? function(error, request) { paulo@89: callback(error == null ? request : null); paulo@89: } : callback; paulo@89: } paulo@89: function d3_xhrHasResponse(request) { paulo@89: var type = request.responseType; paulo@89: return type && type !== "text" ? request.response : request.responseText; paulo@89: } paulo@89: d3.dsv = function(delimiter, mimeType) { paulo@89: var reFormat = new RegExp('["' + delimiter + "\n]"), delimiterCode = delimiter.charCodeAt(0); paulo@89: function dsv(url, row, callback) { paulo@89: if (arguments.length < 3) callback = row, row = null; paulo@89: var xhr = d3_xhr(url, mimeType, row == null ? response : typedResponse(row), callback); paulo@89: xhr.row = function(_) { paulo@89: return arguments.length ? xhr.response((row = _) == null ? response : typedResponse(_)) : row; paulo@89: }; paulo@89: return xhr; paulo@89: } paulo@89: function response(request) { paulo@89: return dsv.parse(request.responseText); paulo@89: } paulo@89: function typedResponse(f) { paulo@89: return function(request) { paulo@89: return dsv.parse(request.responseText, f); paulo@89: }; paulo@89: } paulo@89: dsv.parse = function(text, f) { paulo@89: var o; paulo@89: return dsv.parseRows(text, function(row, i) { paulo@89: if (o) return o(row, i - 1); paulo@89: var a = new Function("d", "return {" + row.map(function(name, i) { paulo@89: return JSON.stringify(name) + ": d[" + i + "]"; paulo@89: }).join(",") + "}"); paulo@89: o = f ? function(row, i) { paulo@89: return f(a(row), i); paulo@89: } : a; paulo@89: }); paulo@89: }; paulo@89: dsv.parseRows = function(text, f) { paulo@89: var EOL = {}, EOF = {}, rows = [], N = text.length, I = 0, n = 0, t, eol; paulo@89: function token() { paulo@89: if (I >= N) return EOF; paulo@89: if (eol) return eol = false, EOL; paulo@89: var j = I; paulo@89: if (text.charCodeAt(j) === 34) { paulo@89: var i = j; paulo@89: while (i++ < N) { paulo@89: if (text.charCodeAt(i) === 34) { paulo@89: if (text.charCodeAt(i + 1) !== 34) break; paulo@89: ++i; paulo@89: } paulo@89: } paulo@89: I = i + 2; paulo@89: var c = text.charCodeAt(i + 1); paulo@89: if (c === 13) { paulo@89: eol = true; paulo@89: if (text.charCodeAt(i + 2) === 10) ++I; paulo@89: } else if (c === 10) { paulo@89: eol = true; paulo@89: } paulo@89: return text.slice(j + 1, i).replace(/""/g, '"'); paulo@89: } paulo@89: while (I < N) { paulo@89: var c = text.charCodeAt(I++), k = 1; paulo@89: if (c === 10) eol = true; else if (c === 13) { paulo@89: eol = true; paulo@89: if (text.charCodeAt(I) === 10) ++I, ++k; paulo@89: } else if (c !== delimiterCode) continue; paulo@89: return text.slice(j, I - k); paulo@89: } paulo@89: return text.slice(j); paulo@89: } paulo@89: while ((t = token()) !== EOF) { paulo@89: var a = []; paulo@89: while (t !== EOL && t !== EOF) { paulo@89: a.push(t); paulo@89: t = token(); paulo@89: } paulo@89: if (f && (a = f(a, n++)) == null) continue; paulo@89: rows.push(a); paulo@89: } paulo@89: return rows; paulo@89: }; paulo@89: dsv.format = function(rows) { paulo@89: if (Array.isArray(rows[0])) return dsv.formatRows(rows); paulo@89: var fieldSet = new d3_Set(), fields = []; paulo@89: rows.forEach(function(row) { paulo@89: for (var field in row) { paulo@89: if (!fieldSet.has(field)) { paulo@89: fields.push(fieldSet.add(field)); paulo@89: } paulo@89: } paulo@89: }); paulo@89: return [ fields.map(formatValue).join(delimiter) ].concat(rows.map(function(row) { paulo@89: return fields.map(function(field) { paulo@89: return formatValue(row[field]); paulo@89: }).join(delimiter); paulo@89: })).join("\n"); paulo@89: }; paulo@89: dsv.formatRows = function(rows) { paulo@89: return rows.map(formatRow).join("\n"); paulo@89: }; paulo@89: function formatRow(row) { paulo@89: return row.map(formatValue).join(delimiter); paulo@89: } paulo@89: function formatValue(text) { paulo@89: return reFormat.test(text) ? '"' + text.replace(/\"/g, '""') + '"' : text; paulo@89: } paulo@89: return dsv; paulo@89: }; paulo@89: d3.csv = d3.dsv(",", "text/csv"); paulo@89: d3.tsv = d3.dsv(" ", "text/tab-separated-values"); paulo@89: var d3_timer_queueHead, d3_timer_queueTail, d3_timer_interval, d3_timer_timeout, d3_timer_frame = this[d3_vendorSymbol(this, "requestAnimationFrame")] || function(callback) { paulo@89: setTimeout(callback, 17); paulo@89: }; paulo@89: d3.timer = function() { paulo@89: d3_timer.apply(this, arguments); paulo@89: }; paulo@89: function d3_timer(callback, delay, then) { paulo@89: var n = arguments.length; paulo@89: if (n < 2) delay = 0; paulo@89: if (n < 3) then = Date.now(); paulo@89: var time = then + delay, timer = { paulo@89: c: callback, paulo@89: t: time, paulo@89: n: null paulo@89: }; paulo@89: if (d3_timer_queueTail) d3_timer_queueTail.n = timer; else d3_timer_queueHead = timer; paulo@89: d3_timer_queueTail = timer; paulo@89: if (!d3_timer_interval) { paulo@89: d3_timer_timeout = clearTimeout(d3_timer_timeout); paulo@89: d3_timer_interval = 1; paulo@89: d3_timer_frame(d3_timer_step); paulo@89: } paulo@89: return timer; paulo@89: } paulo@89: function d3_timer_step() { paulo@89: var now = d3_timer_mark(), delay = d3_timer_sweep() - now; paulo@89: if (delay > 24) { paulo@89: if (isFinite(delay)) { paulo@89: clearTimeout(d3_timer_timeout); paulo@89: d3_timer_timeout = setTimeout(d3_timer_step, delay); paulo@89: } paulo@89: d3_timer_interval = 0; paulo@89: } else { paulo@89: d3_timer_interval = 1; paulo@89: d3_timer_frame(d3_timer_step); paulo@89: } paulo@89: } paulo@89: d3.timer.flush = function() { paulo@89: d3_timer_mark(); paulo@89: d3_timer_sweep(); paulo@89: }; paulo@89: function d3_timer_mark() { paulo@89: var now = Date.now(), timer = d3_timer_queueHead; paulo@89: while (timer) { paulo@89: if (now >= timer.t && timer.c(now - timer.t)) timer.c = null; paulo@89: timer = timer.n; paulo@89: } paulo@89: return now; paulo@89: } paulo@89: function d3_timer_sweep() { paulo@89: var t0, t1 = d3_timer_queueHead, time = Infinity; paulo@89: while (t1) { paulo@89: if (t1.c) { paulo@89: if (t1.t < time) time = t1.t; paulo@89: t1 = (t0 = t1).n; paulo@89: } else { paulo@89: t1 = t0 ? t0.n = t1.n : d3_timer_queueHead = t1.n; paulo@89: } paulo@89: } paulo@89: d3_timer_queueTail = t0; paulo@89: return time; paulo@89: } paulo@89: function d3_format_precision(x, p) { paulo@89: return p - (x ? Math.ceil(Math.log(x) / Math.LN10) : 1); paulo@89: } paulo@89: d3.round = function(x, n) { paulo@89: return n ? Math.round(x * (n = Math.pow(10, n))) / n : Math.round(x); paulo@89: }; paulo@89: var d3_formatPrefixes = [ "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" ].map(d3_formatPrefix); paulo@89: d3.formatPrefix = function(value, precision) { paulo@89: var i = 0; paulo@89: if (value = +value) { paulo@89: if (value < 0) value *= -1; paulo@89: if (precision) value = d3.round(value, d3_format_precision(value, precision)); paulo@89: i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10); paulo@89: i = Math.max(-24, Math.min(24, Math.floor((i - 1) / 3) * 3)); paulo@89: } paulo@89: return d3_formatPrefixes[8 + i / 3]; paulo@89: }; paulo@89: function d3_formatPrefix(d, i) { paulo@89: var k = Math.pow(10, abs(8 - i) * 3); paulo@89: return { paulo@89: scale: i > 8 ? function(d) { paulo@89: return d / k; paulo@89: } : function(d) { paulo@89: return d * k; paulo@89: }, paulo@89: symbol: d paulo@89: }; paulo@89: } paulo@89: function d3_locale_numberFormat(locale) { paulo@89: var locale_decimal = locale.decimal, locale_thousands = locale.thousands, locale_grouping = locale.grouping, locale_currency = locale.currency, formatGroup = locale_grouping && locale_thousands ? function(value, width) { paulo@89: var i = value.length, t = [], j = 0, g = locale_grouping[0], length = 0; paulo@89: while (i > 0 && g > 0) { paulo@89: if (length + g + 1 > width) g = Math.max(1, width - length); paulo@89: t.push(value.substring(i -= g, i + g)); paulo@89: if ((length += g + 1) > width) break; paulo@89: g = locale_grouping[j = (j + 1) % locale_grouping.length]; paulo@89: } paulo@89: return t.reverse().join(locale_thousands); paulo@89: } : d3_identity; paulo@89: return function(specifier) { paulo@89: var match = d3_format_re.exec(specifier), fill = match[1] || " ", align = match[2] || ">", sign = match[3] || "-", symbol = match[4] || "", zfill = match[5], width = +match[6], comma = match[7], precision = match[8], type = match[9], scale = 1, prefix = "", suffix = "", integer = false, exponent = true; paulo@89: if (precision) precision = +precision.substring(1); paulo@89: if (zfill || fill === "0" && align === "=") { paulo@89: zfill = fill = "0"; paulo@89: align = "="; paulo@89: } paulo@89: switch (type) { paulo@89: case "n": paulo@89: comma = true; paulo@89: type = "g"; paulo@89: break; paulo@89: paulo@89: case "%": paulo@89: scale = 100; paulo@89: suffix = "%"; paulo@89: type = "f"; paulo@89: break; paulo@89: paulo@89: case "p": paulo@89: scale = 100; paulo@89: suffix = "%"; paulo@89: type = "r"; paulo@89: break; paulo@89: paulo@89: case "b": paulo@89: case "o": paulo@89: case "x": paulo@89: case "X": paulo@89: if (symbol === "#") prefix = "0" + type.toLowerCase(); paulo@89: paulo@89: case "c": paulo@89: exponent = false; paulo@89: paulo@89: case "d": paulo@89: integer = true; paulo@89: precision = 0; paulo@89: break; paulo@89: paulo@89: case "s": paulo@89: scale = -1; paulo@89: type = "r"; paulo@89: break; paulo@89: } paulo@89: if (symbol === "$") prefix = locale_currency[0], suffix = locale_currency[1]; paulo@89: if (type == "r" && !precision) type = "g"; paulo@89: if (precision != null) { paulo@89: if (type == "g") precision = Math.max(1, Math.min(21, precision)); else if (type == "e" || type == "f") precision = Math.max(0, Math.min(20, precision)); paulo@89: } paulo@89: type = d3_format_types.get(type) || d3_format_typeDefault; paulo@89: var zcomma = zfill && comma; paulo@89: return function(value) { paulo@89: var fullSuffix = suffix; paulo@89: if (integer && value % 1) return ""; paulo@89: var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign === "-" ? "" : sign; paulo@89: if (scale < 0) { paulo@89: var unit = d3.formatPrefix(value, precision); paulo@89: value = unit.scale(value); paulo@89: fullSuffix = unit.symbol + suffix; paulo@89: } else { paulo@89: value *= scale; paulo@89: } paulo@89: value = type(value, precision); paulo@89: var i = value.lastIndexOf("."), before, after; paulo@89: if (i < 0) { paulo@89: var j = exponent ? value.lastIndexOf("e") : -1; paulo@89: if (j < 0) before = value, after = ""; else before = value.substring(0, j), after = value.substring(j); paulo@89: } else { paulo@89: before = value.substring(0, i); paulo@89: after = locale_decimal + value.substring(i + 1); paulo@89: } paulo@89: if (!zfill && comma) before = formatGroup(before, Infinity); paulo@89: var length = prefix.length + before.length + after.length + (zcomma ? 0 : negative.length), padding = length < width ? new Array(length = width - length + 1).join(fill) : ""; paulo@89: if (zcomma) before = formatGroup(padding + before, padding.length ? width - after.length : Infinity); paulo@89: negative += prefix; paulo@89: value = before + after; paulo@89: return (align === "<" ? negative + value + padding : align === ">" ? padding + negative + value : align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length) : negative + (zcomma ? value : padding + value)) + fullSuffix; paulo@89: }; paulo@89: }; paulo@89: } paulo@89: var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?([$#])?(0)?(\d+)?(,)?(\.-?\d+)?([a-z%])?/i; paulo@89: var d3_format_types = d3.map({ paulo@89: b: function(x) { paulo@89: return x.toString(2); paulo@89: }, paulo@89: c: function(x) { paulo@89: return String.fromCharCode(x); paulo@89: }, paulo@89: o: function(x) { paulo@89: return x.toString(8); paulo@89: }, paulo@89: x: function(x) { paulo@89: return x.toString(16); paulo@89: }, paulo@89: X: function(x) { paulo@89: return x.toString(16).toUpperCase(); paulo@89: }, paulo@89: g: function(x, p) { paulo@89: return x.toPrecision(p); paulo@89: }, paulo@89: e: function(x, p) { paulo@89: return x.toExponential(p); paulo@89: }, paulo@89: f: function(x, p) { paulo@89: return x.toFixed(p); paulo@89: }, paulo@89: r: function(x, p) { paulo@89: return (x = d3.round(x, d3_format_precision(x, p))).toFixed(Math.max(0, Math.min(20, d3_format_precision(x * (1 + 1e-15), p)))); paulo@89: } paulo@89: }); paulo@89: function d3_format_typeDefault(x) { paulo@89: return x + ""; paulo@89: } paulo@89: var d3_time = d3.time = {}, d3_date = Date; paulo@89: function d3_date_utc() { paulo@89: this._ = new Date(arguments.length > 1 ? Date.UTC.apply(this, arguments) : arguments[0]); paulo@89: } paulo@89: d3_date_utc.prototype = { paulo@89: getDate: function() { paulo@89: return this._.getUTCDate(); paulo@89: }, paulo@89: getDay: function() { paulo@89: return this._.getUTCDay(); paulo@89: }, paulo@89: getFullYear: function() { paulo@89: return this._.getUTCFullYear(); paulo@89: }, paulo@89: getHours: function() { paulo@89: return this._.getUTCHours(); paulo@89: }, paulo@89: getMilliseconds: function() { paulo@89: return this._.getUTCMilliseconds(); paulo@89: }, paulo@89: getMinutes: function() { paulo@89: return this._.getUTCMinutes(); paulo@89: }, paulo@89: getMonth: function() { paulo@89: return this._.getUTCMonth(); paulo@89: }, paulo@89: getSeconds: function() { paulo@89: return this._.getUTCSeconds(); paulo@89: }, paulo@89: getTime: function() { paulo@89: return this._.getTime(); paulo@89: }, paulo@89: getTimezoneOffset: function() { paulo@89: return 0; paulo@89: }, paulo@89: valueOf: function() { paulo@89: return this._.valueOf(); paulo@89: }, paulo@89: setDate: function() { paulo@89: d3_time_prototype.setUTCDate.apply(this._, arguments); paulo@89: }, paulo@89: setDay: function() { paulo@89: d3_time_prototype.setUTCDay.apply(this._, arguments); paulo@89: }, paulo@89: setFullYear: function() { paulo@89: d3_time_prototype.setUTCFullYear.apply(this._, arguments); paulo@89: }, paulo@89: setHours: function() { paulo@89: d3_time_prototype.setUTCHours.apply(this._, arguments); paulo@89: }, paulo@89: setMilliseconds: function() { paulo@89: d3_time_prototype.setUTCMilliseconds.apply(this._, arguments); paulo@89: }, paulo@89: setMinutes: function() { paulo@89: d3_time_prototype.setUTCMinutes.apply(this._, arguments); paulo@89: }, paulo@89: setMonth: function() { paulo@89: d3_time_prototype.setUTCMonth.apply(this._, arguments); paulo@89: }, paulo@89: setSeconds: function() { paulo@89: d3_time_prototype.setUTCSeconds.apply(this._, arguments); paulo@89: }, paulo@89: setTime: function() { paulo@89: d3_time_prototype.setTime.apply(this._, arguments); paulo@89: } paulo@89: }; paulo@89: var d3_time_prototype = Date.prototype; paulo@89: function d3_time_interval(local, step, number) { paulo@89: function round(date) { paulo@89: var d0 = local(date), d1 = offset(d0, 1); paulo@89: return date - d0 < d1 - date ? d0 : d1; paulo@89: } paulo@89: function ceil(date) { paulo@89: step(date = local(new d3_date(date - 1)), 1); paulo@89: return date; paulo@89: } paulo@89: function offset(date, k) { paulo@89: step(date = new d3_date(+date), k); paulo@89: return date; paulo@89: } paulo@89: function range(t0, t1, dt) { paulo@89: var time = ceil(t0), times = []; paulo@89: if (dt > 1) { paulo@89: while (time < t1) { paulo@89: if (!(number(time) % dt)) times.push(new Date(+time)); paulo@89: step(time, 1); paulo@89: } paulo@89: } else { paulo@89: while (time < t1) times.push(new Date(+time)), step(time, 1); paulo@89: } paulo@89: return times; paulo@89: } paulo@89: function range_utc(t0, t1, dt) { paulo@89: try { paulo@89: d3_date = d3_date_utc; paulo@89: var utc = new d3_date_utc(); paulo@89: utc._ = t0; paulo@89: return range(utc, t1, dt); paulo@89: } finally { paulo@89: d3_date = Date; paulo@89: } paulo@89: } paulo@89: local.floor = local; paulo@89: local.round = round; paulo@89: local.ceil = ceil; paulo@89: local.offset = offset; paulo@89: local.range = range; paulo@89: var utc = local.utc = d3_time_interval_utc(local); paulo@89: utc.floor = utc; paulo@89: utc.round = d3_time_interval_utc(round); paulo@89: utc.ceil = d3_time_interval_utc(ceil); paulo@89: utc.offset = d3_time_interval_utc(offset); paulo@89: utc.range = range_utc; paulo@89: return local; paulo@89: } paulo@89: function d3_time_interval_utc(method) { paulo@89: return function(date, k) { paulo@89: try { paulo@89: d3_date = d3_date_utc; paulo@89: var utc = new d3_date_utc(); paulo@89: utc._ = date; paulo@89: return method(utc, k)._; paulo@89: } finally { paulo@89: d3_date = Date; paulo@89: } paulo@89: }; paulo@89: } paulo@89: d3_time.year = d3_time_interval(function(date) { paulo@89: date = d3_time.day(date); paulo@89: date.setMonth(0, 1); paulo@89: return date; paulo@89: }, function(date, offset) { paulo@89: date.setFullYear(date.getFullYear() + offset); paulo@89: }, function(date) { paulo@89: return date.getFullYear(); paulo@89: }); paulo@89: d3_time.years = d3_time.year.range; paulo@89: d3_time.years.utc = d3_time.year.utc.range; paulo@89: d3_time.day = d3_time_interval(function(date) { paulo@89: var day = new d3_date(2e3, 0); paulo@89: day.setFullYear(date.getFullYear(), date.getMonth(), date.getDate()); paulo@89: return day; paulo@89: }, function(date, offset) { paulo@89: date.setDate(date.getDate() + offset); paulo@89: }, function(date) { paulo@89: return date.getDate() - 1; paulo@89: }); paulo@89: d3_time.days = d3_time.day.range; paulo@89: d3_time.days.utc = d3_time.day.utc.range; paulo@89: d3_time.dayOfYear = function(date) { paulo@89: var year = d3_time.year(date); paulo@89: return Math.floor((date - year - (date.getTimezoneOffset() - year.getTimezoneOffset()) * 6e4) / 864e5); paulo@89: }; paulo@89: [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ].forEach(function(day, i) { paulo@89: i = 7 - i; paulo@89: var interval = d3_time[day] = d3_time_interval(function(date) { paulo@89: (date = d3_time.day(date)).setDate(date.getDate() - (date.getDay() + i) % 7); paulo@89: return date; paulo@89: }, function(date, offset) { paulo@89: date.setDate(date.getDate() + Math.floor(offset) * 7); paulo@89: }, function(date) { paulo@89: var day = d3_time.year(date).getDay(); paulo@89: return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7) - (day !== i); paulo@89: }); paulo@89: d3_time[day + "s"] = interval.range; paulo@89: d3_time[day + "s"].utc = interval.utc.range; paulo@89: d3_time[day + "OfYear"] = function(date) { paulo@89: var day = d3_time.year(date).getDay(); paulo@89: return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7); paulo@89: }; paulo@89: }); paulo@89: d3_time.week = d3_time.sunday; paulo@89: d3_time.weeks = d3_time.sunday.range; paulo@89: d3_time.weeks.utc = d3_time.sunday.utc.range; paulo@89: d3_time.weekOfYear = d3_time.sundayOfYear; paulo@89: function d3_locale_timeFormat(locale) { paulo@89: var locale_dateTime = locale.dateTime, locale_date = locale.date, locale_time = locale.time, locale_periods = locale.periods, locale_days = locale.days, locale_shortDays = locale.shortDays, locale_months = locale.months, locale_shortMonths = locale.shortMonths; paulo@89: function d3_time_format(template) { paulo@89: var n = template.length; paulo@89: function format(date) { paulo@89: var string = [], i = -1, j = 0, c, p, f; paulo@89: while (++i < n) { paulo@89: if (template.charCodeAt(i) === 37) { paulo@89: string.push(template.slice(j, i)); paulo@89: if ((p = d3_time_formatPads[c = template.charAt(++i)]) != null) c = template.charAt(++i); paulo@89: if (f = d3_time_formats[c]) c = f(date, p == null ? c === "e" ? " " : "0" : p); paulo@89: string.push(c); paulo@89: j = i + 1; paulo@89: } paulo@89: } paulo@89: string.push(template.slice(j, i)); paulo@89: return string.join(""); paulo@89: } paulo@89: format.parse = function(string) { paulo@89: var d = { paulo@89: y: 1900, paulo@89: m: 0, paulo@89: d: 1, paulo@89: H: 0, paulo@89: M: 0, paulo@89: S: 0, paulo@89: L: 0, paulo@89: Z: null paulo@89: }, i = d3_time_parse(d, template, string, 0); paulo@89: if (i != string.length) return null; paulo@89: if ("p" in d) d.H = d.H % 12 + d.p * 12; paulo@89: var localZ = d.Z != null && d3_date !== d3_date_utc, date = new (localZ ? d3_date_utc : d3_date)(); paulo@89: if ("j" in d) date.setFullYear(d.y, 0, d.j); else if ("W" in d || "U" in d) { paulo@89: if (!("w" in d)) d.w = "W" in d ? 1 : 0; paulo@89: date.setFullYear(d.y, 0, 1); paulo@89: date.setFullYear(d.y, 0, "W" in d ? (d.w + 6) % 7 + d.W * 7 - (date.getDay() + 5) % 7 : d.w + d.U * 7 - (date.getDay() + 6) % 7); paulo@89: } else date.setFullYear(d.y, d.m, d.d); paulo@89: date.setHours(d.H + (d.Z / 100 | 0), d.M + d.Z % 100, d.S, d.L); paulo@89: return localZ ? date._ : date; paulo@89: }; paulo@89: format.toString = function() { paulo@89: return template; paulo@89: }; paulo@89: return format; paulo@89: } paulo@89: function d3_time_parse(date, template, string, j) { paulo@89: var c, p, t, i = 0, n = template.length, m = string.length; paulo@89: while (i < n) { paulo@89: if (j >= m) return -1; paulo@89: c = template.charCodeAt(i++); paulo@89: if (c === 37) { paulo@89: t = template.charAt(i++); paulo@89: p = d3_time_parsers[t in d3_time_formatPads ? template.charAt(i++) : t]; paulo@89: if (!p || (j = p(date, string, j)) < 0) return -1; paulo@89: } else if (c != string.charCodeAt(j++)) { paulo@89: return -1; paulo@89: } paulo@89: } paulo@89: return j; paulo@89: } paulo@89: d3_time_format.utc = function(template) { paulo@89: var local = d3_time_format(template); paulo@89: function format(date) { paulo@89: try { paulo@89: d3_date = d3_date_utc; paulo@89: var utc = new d3_date(); paulo@89: utc._ = date; paulo@89: return local(utc); paulo@89: } finally { paulo@89: d3_date = Date; paulo@89: } paulo@89: } paulo@89: format.parse = function(string) { paulo@89: try { paulo@89: d3_date = d3_date_utc; paulo@89: var date = local.parse(string); paulo@89: return date && date._; paulo@89: } finally { paulo@89: d3_date = Date; paulo@89: } paulo@89: }; paulo@89: format.toString = local.toString; paulo@89: return format; paulo@89: }; paulo@89: d3_time_format.multi = d3_time_format.utc.multi = d3_time_formatMulti; paulo@89: var d3_time_periodLookup = d3.map(), d3_time_dayRe = d3_time_formatRe(locale_days), d3_time_dayLookup = d3_time_formatLookup(locale_days), d3_time_dayAbbrevRe = d3_time_formatRe(locale_shortDays), d3_time_dayAbbrevLookup = d3_time_formatLookup(locale_shortDays), d3_time_monthRe = d3_time_formatRe(locale_months), d3_time_monthLookup = d3_time_formatLookup(locale_months), d3_time_monthAbbrevRe = d3_time_formatRe(locale_shortMonths), d3_time_monthAbbrevLookup = d3_time_formatLookup(locale_shortMonths); paulo@89: locale_periods.forEach(function(p, i) { paulo@89: d3_time_periodLookup.set(p.toLowerCase(), i); paulo@89: }); paulo@89: var d3_time_formats = { paulo@89: a: function(d) { paulo@89: return locale_shortDays[d.getDay()]; paulo@89: }, paulo@89: A: function(d) { paulo@89: return locale_days[d.getDay()]; paulo@89: }, paulo@89: b: function(d) { paulo@89: return locale_shortMonths[d.getMonth()]; paulo@89: }, paulo@89: B: function(d) { paulo@89: return locale_months[d.getMonth()]; paulo@89: }, paulo@89: c: d3_time_format(locale_dateTime), paulo@89: d: function(d, p) { paulo@89: return d3_time_formatPad(d.getDate(), p, 2); paulo@89: }, paulo@89: e: function(d, p) { paulo@89: return d3_time_formatPad(d.getDate(), p, 2); paulo@89: }, paulo@89: H: function(d, p) { paulo@89: return d3_time_formatPad(d.getHours(), p, 2); paulo@89: }, paulo@89: I: function(d, p) { paulo@89: return d3_time_formatPad(d.getHours() % 12 || 12, p, 2); paulo@89: }, paulo@89: j: function(d, p) { paulo@89: return d3_time_formatPad(1 + d3_time.dayOfYear(d), p, 3); paulo@89: }, paulo@89: L: function(d, p) { paulo@89: return d3_time_formatPad(d.getMilliseconds(), p, 3); paulo@89: }, paulo@89: m: function(d, p) { paulo@89: return d3_time_formatPad(d.getMonth() + 1, p, 2); paulo@89: }, paulo@89: M: function(d, p) { paulo@89: return d3_time_formatPad(d.getMinutes(), p, 2); paulo@89: }, paulo@89: p: function(d) { paulo@89: return locale_periods[+(d.getHours() >= 12)]; paulo@89: }, paulo@89: S: function(d, p) { paulo@89: return d3_time_formatPad(d.getSeconds(), p, 2); paulo@89: }, paulo@89: U: function(d, p) { paulo@89: return d3_time_formatPad(d3_time.sundayOfYear(d), p, 2); paulo@89: }, paulo@89: w: function(d) { paulo@89: return d.getDay(); paulo@89: }, paulo@89: W: function(d, p) { paulo@89: return d3_time_formatPad(d3_time.mondayOfYear(d), p, 2); paulo@89: }, paulo@89: x: d3_time_format(locale_date), paulo@89: X: d3_time_format(locale_time), paulo@89: y: function(d, p) { paulo@89: return d3_time_formatPad(d.getFullYear() % 100, p, 2); paulo@89: }, paulo@89: Y: function(d, p) { paulo@89: return d3_time_formatPad(d.getFullYear() % 1e4, p, 4); paulo@89: }, paulo@89: Z: d3_time_zone, paulo@89: "%": function() { paulo@89: return "%"; paulo@89: } paulo@89: }; paulo@89: var d3_time_parsers = { paulo@89: a: d3_time_parseWeekdayAbbrev, paulo@89: A: d3_time_parseWeekday, paulo@89: b: d3_time_parseMonthAbbrev, paulo@89: B: d3_time_parseMonth, paulo@89: c: d3_time_parseLocaleFull, paulo@89: d: d3_time_parseDay, paulo@89: e: d3_time_parseDay, paulo@89: H: d3_time_parseHour24, paulo@89: I: d3_time_parseHour24, paulo@89: j: d3_time_parseDayOfYear, paulo@89: L: d3_time_parseMilliseconds, paulo@89: m: d3_time_parseMonthNumber, paulo@89: M: d3_time_parseMinutes, paulo@89: p: d3_time_parseAmPm, paulo@89: S: d3_time_parseSeconds, paulo@89: U: d3_time_parseWeekNumberSunday, paulo@89: w: d3_time_parseWeekdayNumber, paulo@89: W: d3_time_parseWeekNumberMonday, paulo@89: x: d3_time_parseLocaleDate, paulo@89: X: d3_time_parseLocaleTime, paulo@89: y: d3_time_parseYear, paulo@89: Y: d3_time_parseFullYear, paulo@89: Z: d3_time_parseZone, paulo@89: "%": d3_time_parseLiteralPercent paulo@89: }; paulo@89: function d3_time_parseWeekdayAbbrev(date, string, i) { paulo@89: d3_time_dayAbbrevRe.lastIndex = 0; paulo@89: var n = d3_time_dayAbbrevRe.exec(string.slice(i)); paulo@89: return n ? (date.w = d3_time_dayAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseWeekday(date, string, i) { paulo@89: d3_time_dayRe.lastIndex = 0; paulo@89: var n = d3_time_dayRe.exec(string.slice(i)); paulo@89: return n ? (date.w = d3_time_dayLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseMonthAbbrev(date, string, i) { paulo@89: d3_time_monthAbbrevRe.lastIndex = 0; paulo@89: var n = d3_time_monthAbbrevRe.exec(string.slice(i)); paulo@89: return n ? (date.m = d3_time_monthAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseMonth(date, string, i) { paulo@89: d3_time_monthRe.lastIndex = 0; paulo@89: var n = d3_time_monthRe.exec(string.slice(i)); paulo@89: return n ? (date.m = d3_time_monthLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseLocaleFull(date, string, i) { paulo@89: return d3_time_parse(date, d3_time_formats.c.toString(), string, i); paulo@89: } paulo@89: function d3_time_parseLocaleDate(date, string, i) { paulo@89: return d3_time_parse(date, d3_time_formats.x.toString(), string, i); paulo@89: } paulo@89: function d3_time_parseLocaleTime(date, string, i) { paulo@89: return d3_time_parse(date, d3_time_formats.X.toString(), string, i); paulo@89: } paulo@89: function d3_time_parseAmPm(date, string, i) { paulo@89: var n = d3_time_periodLookup.get(string.slice(i, i += 2).toLowerCase()); paulo@89: return n == null ? -1 : (date.p = n, i); paulo@89: } paulo@89: return d3_time_format; paulo@89: } paulo@89: var d3_time_formatPads = { paulo@89: "-": "", paulo@89: _: " ", paulo@89: "0": "0" paulo@89: }, d3_time_numberRe = /^\s*\d+/, d3_time_percentRe = /^%/; paulo@89: function d3_time_formatPad(value, fill, width) { paulo@89: var sign = value < 0 ? "-" : "", string = (sign ? -value : value) + "", length = string.length; paulo@89: return sign + (length < width ? new Array(width - length + 1).join(fill) + string : string); paulo@89: } paulo@89: function d3_time_formatRe(names) { paulo@89: return new RegExp("^(?:" + names.map(d3.requote).join("|") + ")", "i"); paulo@89: } paulo@89: function d3_time_formatLookup(names) { paulo@89: var map = new d3_Map(), i = -1, n = names.length; paulo@89: while (++i < n) map.set(names[i].toLowerCase(), i); paulo@89: return map; paulo@89: } paulo@89: function d3_time_parseWeekdayNumber(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 1)); paulo@89: return n ? (date.w = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseWeekNumberSunday(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i)); paulo@89: return n ? (date.U = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseWeekNumberMonday(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i)); paulo@89: return n ? (date.W = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseFullYear(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 4)); paulo@89: return n ? (date.y = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseYear(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 2)); paulo@89: return n ? (date.y = d3_time_expandYear(+n[0]), i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseZone(date, string, i) { paulo@89: return /^[+-]\d{4}$/.test(string = string.slice(i, i + 5)) ? (date.Z = -string, paulo@89: i + 5) : -1; paulo@89: } paulo@89: function d3_time_expandYear(d) { paulo@89: return d + (d > 68 ? 1900 : 2e3); paulo@89: } paulo@89: function d3_time_parseMonthNumber(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 2)); paulo@89: return n ? (date.m = n[0] - 1, i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseDay(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 2)); paulo@89: return n ? (date.d = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseDayOfYear(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 3)); paulo@89: return n ? (date.j = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseHour24(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 2)); paulo@89: return n ? (date.H = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseMinutes(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 2)); paulo@89: return n ? (date.M = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseSeconds(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 2)); paulo@89: return n ? (date.S = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_parseMilliseconds(date, string, i) { paulo@89: d3_time_numberRe.lastIndex = 0; paulo@89: var n = d3_time_numberRe.exec(string.slice(i, i + 3)); paulo@89: return n ? (date.L = +n[0], i + n[0].length) : -1; paulo@89: } paulo@89: function d3_time_zone(d) { paulo@89: var z = d.getTimezoneOffset(), zs = z > 0 ? "-" : "+", zh = abs(z) / 60 | 0, zm = abs(z) % 60; paulo@89: return zs + d3_time_formatPad(zh, "0", 2) + d3_time_formatPad(zm, "0", 2); paulo@89: } paulo@89: function d3_time_parseLiteralPercent(date, string, i) { paulo@89: d3_time_percentRe.lastIndex = 0; paulo@89: var n = d3_time_percentRe.exec(string.slice(i, i + 1)); paulo@89: return n ? i + n[0].length : -1; paulo@89: } paulo@89: function d3_time_formatMulti(formats) { paulo@89: var n = formats.length, i = -1; paulo@89: while (++i < n) formats[i][0] = this(formats[i][0]); paulo@89: return function(date) { paulo@89: var i = 0, f = formats[i]; paulo@89: while (!f[1](date)) f = formats[++i]; paulo@89: return f[0](date); paulo@89: }; paulo@89: } paulo@89: d3.locale = function(locale) { paulo@89: return { paulo@89: numberFormat: d3_locale_numberFormat(locale), paulo@89: timeFormat: d3_locale_timeFormat(locale) paulo@89: }; paulo@89: }; paulo@89: var d3_locale_enUS = d3.locale({ paulo@89: decimal: ".", paulo@89: thousands: ",", paulo@89: grouping: [ 3 ], paulo@89: currency: [ "$", "" ], paulo@89: dateTime: "%a %b %e %X %Y", paulo@89: date: "%m/%d/%Y", paulo@89: time: "%H:%M:%S", paulo@89: periods: [ "AM", "PM" ], paulo@89: days: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], paulo@89: shortDays: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], paulo@89: months: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], paulo@89: shortMonths: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] paulo@89: }); paulo@89: d3.format = d3_locale_enUS.numberFormat; paulo@89: d3.geo = {}; paulo@89: function d3_adder() {} paulo@89: d3_adder.prototype = { paulo@89: s: 0, paulo@89: t: 0, paulo@89: add: function(y) { paulo@89: d3_adderSum(y, this.t, d3_adderTemp); paulo@89: d3_adderSum(d3_adderTemp.s, this.s, this); paulo@89: if (this.s) this.t += d3_adderTemp.t; else this.s = d3_adderTemp.t; paulo@89: }, paulo@89: reset: function() { paulo@89: this.s = this.t = 0; paulo@89: }, paulo@89: valueOf: function() { paulo@89: return this.s; paulo@89: } paulo@89: }; paulo@89: var d3_adderTemp = new d3_adder(); paulo@89: function d3_adderSum(a, b, o) { paulo@89: var x = o.s = a + b, bv = x - a, av = x - bv; paulo@89: o.t = a - av + (b - bv); paulo@89: } paulo@89: d3.geo.stream = function(object, listener) { paulo@89: if (object && d3_geo_streamObjectType.hasOwnProperty(object.type)) { paulo@89: d3_geo_streamObjectType[object.type](object, listener); paulo@89: } else { paulo@89: d3_geo_streamGeometry(object, listener); paulo@89: } paulo@89: }; paulo@89: function d3_geo_streamGeometry(geometry, listener) { paulo@89: if (geometry && d3_geo_streamGeometryType.hasOwnProperty(geometry.type)) { paulo@89: d3_geo_streamGeometryType[geometry.type](geometry, listener); paulo@89: } paulo@89: } paulo@89: var d3_geo_streamObjectType = { paulo@89: Feature: function(feature, listener) { paulo@89: d3_geo_streamGeometry(feature.geometry, listener); paulo@89: }, paulo@89: FeatureCollection: function(object, listener) { paulo@89: var features = object.features, i = -1, n = features.length; paulo@89: while (++i < n) d3_geo_streamGeometry(features[i].geometry, listener); paulo@89: } paulo@89: }; paulo@89: var d3_geo_streamGeometryType = { paulo@89: Sphere: function(object, listener) { paulo@89: listener.sphere(); paulo@89: }, paulo@89: Point: function(object, listener) { paulo@89: object = object.coordinates; paulo@89: listener.point(object[0], object[1], object[2]); paulo@89: }, paulo@89: MultiPoint: function(object, listener) { paulo@89: var coordinates = object.coordinates, i = -1, n = coordinates.length; paulo@89: while (++i < n) object = coordinates[i], listener.point(object[0], object[1], object[2]); paulo@89: }, paulo@89: LineString: function(object, listener) { paulo@89: d3_geo_streamLine(object.coordinates, listener, 0); paulo@89: }, paulo@89: MultiLineString: function(object, listener) { paulo@89: var coordinates = object.coordinates, i = -1, n = coordinates.length; paulo@89: while (++i < n) d3_geo_streamLine(coordinates[i], listener, 0); paulo@89: }, paulo@89: Polygon: function(object, listener) { paulo@89: d3_geo_streamPolygon(object.coordinates, listener); paulo@89: }, paulo@89: MultiPolygon: function(object, listener) { paulo@89: var coordinates = object.coordinates, i = -1, n = coordinates.length; paulo@89: while (++i < n) d3_geo_streamPolygon(coordinates[i], listener); paulo@89: }, paulo@89: GeometryCollection: function(object, listener) { paulo@89: var geometries = object.geometries, i = -1, n = geometries.length; paulo@89: while (++i < n) d3_geo_streamGeometry(geometries[i], listener); paulo@89: } paulo@89: }; paulo@89: function d3_geo_streamLine(coordinates, listener, closed) { paulo@89: var i = -1, n = coordinates.length - closed, coordinate; paulo@89: listener.lineStart(); paulo@89: while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1], coordinate[2]); paulo@89: listener.lineEnd(); paulo@89: } paulo@89: function d3_geo_streamPolygon(coordinates, listener) { paulo@89: var i = -1, n = coordinates.length; paulo@89: listener.polygonStart(); paulo@89: while (++i < n) d3_geo_streamLine(coordinates[i], listener, 1); paulo@89: listener.polygonEnd(); paulo@89: } paulo@89: d3.geo.area = function(object) { paulo@89: d3_geo_areaSum = 0; paulo@89: d3.geo.stream(object, d3_geo_area); paulo@89: return d3_geo_areaSum; paulo@89: }; paulo@89: var d3_geo_areaSum, d3_geo_areaRingSum = new d3_adder(); paulo@89: var d3_geo_area = { paulo@89: sphere: function() { paulo@89: d3_geo_areaSum += 4 * π; paulo@89: }, paulo@89: point: d3_noop, paulo@89: lineStart: d3_noop, paulo@89: lineEnd: d3_noop, paulo@89: polygonStart: function() { paulo@89: d3_geo_areaRingSum.reset(); paulo@89: d3_geo_area.lineStart = d3_geo_areaRingStart; paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: var area = 2 * d3_geo_areaRingSum; paulo@89: d3_geo_areaSum += area < 0 ? 4 * π + area : area; paulo@89: d3_geo_area.lineStart = d3_geo_area.lineEnd = d3_geo_area.point = d3_noop; paulo@89: } paulo@89: }; paulo@89: function d3_geo_areaRingStart() { paulo@89: var λ00, φ00, λ0, cosφ0, sinφ0; paulo@89: d3_geo_area.point = function(λ, φ) { paulo@89: d3_geo_area.point = nextPoint; paulo@89: λ0 = (λ00 = λ) * d3_radians, cosφ0 = Math.cos(φ = (φ00 = φ) * d3_radians / 2 + π / 4), paulo@89: sinφ0 = Math.sin(φ); paulo@89: }; paulo@89: function nextPoint(λ, φ) { paulo@89: λ *= d3_radians; paulo@89: φ = φ * d3_radians / 2 + π / 4; paulo@89: var dλ = λ - λ0, sdλ = dλ >= 0 ? 1 : -1, adλ = sdλ * dλ, cosφ = Math.cos(φ), sinφ = Math.sin(φ), k = sinφ0 * sinφ, u = cosφ0 * cosφ + k * Math.cos(adλ), v = k * sdλ * Math.sin(adλ); paulo@89: d3_geo_areaRingSum.add(Math.atan2(v, u)); paulo@89: λ0 = λ, cosφ0 = cosφ, sinφ0 = sinφ; paulo@89: } paulo@89: d3_geo_area.lineEnd = function() { paulo@89: nextPoint(λ00, φ00); paulo@89: }; paulo@89: } paulo@89: function d3_geo_cartesian(spherical) { paulo@89: var λ = spherical[0], φ = spherical[1], cosφ = Math.cos(φ); paulo@89: return [ cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ) ]; paulo@89: } paulo@89: function d3_geo_cartesianDot(a, b) { paulo@89: return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; paulo@89: } paulo@89: function d3_geo_cartesianCross(a, b) { paulo@89: return [ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ]; paulo@89: } paulo@89: function d3_geo_cartesianAdd(a, b) { paulo@89: a[0] += b[0]; paulo@89: a[1] += b[1]; paulo@89: a[2] += b[2]; paulo@89: } paulo@89: function d3_geo_cartesianScale(vector, k) { paulo@89: return [ vector[0] * k, vector[1] * k, vector[2] * k ]; paulo@89: } paulo@89: function d3_geo_cartesianNormalize(d) { paulo@89: var l = Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); paulo@89: d[0] /= l; paulo@89: d[1] /= l; paulo@89: d[2] /= l; paulo@89: } paulo@89: function d3_geo_spherical(cartesian) { paulo@89: return [ Math.atan2(cartesian[1], cartesian[0]), d3_asin(cartesian[2]) ]; paulo@89: } paulo@89: function d3_geo_sphericalEqual(a, b) { paulo@89: return abs(a[0] - b[0]) < ε && abs(a[1] - b[1]) < ε; paulo@89: } paulo@89: d3.geo.bounds = function() { paulo@89: var λ0, φ0, λ1, φ1, λ_, λ__, φ__, p0, dλSum, ranges, range; paulo@89: var bound = { paulo@89: point: point, paulo@89: lineStart: lineStart, paulo@89: lineEnd: lineEnd, paulo@89: polygonStart: function() { paulo@89: bound.point = ringPoint; paulo@89: bound.lineStart = ringStart; paulo@89: bound.lineEnd = ringEnd; paulo@89: dλSum = 0; paulo@89: d3_geo_area.polygonStart(); paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: d3_geo_area.polygonEnd(); paulo@89: bound.point = point; paulo@89: bound.lineStart = lineStart; paulo@89: bound.lineEnd = lineEnd; paulo@89: if (d3_geo_areaRingSum < 0) λ0 = -(λ1 = 180), φ0 = -(φ1 = 90); else if (dλSum > ε) φ1 = 90; else if (dλSum < -ε) φ0 = -90; paulo@89: range[0] = λ0, range[1] = λ1; paulo@89: } paulo@89: }; paulo@89: function point(λ, φ) { paulo@89: ranges.push(range = [ λ0 = λ, λ1 = λ ]); paulo@89: if (φ < φ0) φ0 = φ; paulo@89: if (φ > φ1) φ1 = φ; paulo@89: } paulo@89: function linePoint(λ, φ) { paulo@89: var p = d3_geo_cartesian([ λ * d3_radians, φ * d3_radians ]); paulo@89: if (p0) { paulo@89: var normal = d3_geo_cartesianCross(p0, p), equatorial = [ normal[1], -normal[0], 0 ], inflection = d3_geo_cartesianCross(equatorial, normal); paulo@89: d3_geo_cartesianNormalize(inflection); paulo@89: inflection = d3_geo_spherical(inflection); paulo@89: var dλ = λ - λ_, s = dλ > 0 ? 1 : -1, λi = inflection[0] * d3_degrees * s, antimeridian = abs(dλ) > 180; paulo@89: if (antimeridian ^ (s * λ_ < λi && λi < s * λ)) { paulo@89: var φi = inflection[1] * d3_degrees; paulo@89: if (φi > φ1) φ1 = φi; paulo@89: } else if (λi = (λi + 360) % 360 - 180, antimeridian ^ (s * λ_ < λi && λi < s * λ)) { paulo@89: var φi = -inflection[1] * d3_degrees; paulo@89: if (φi < φ0) φ0 = φi; paulo@89: } else { paulo@89: if (φ < φ0) φ0 = φ; paulo@89: if (φ > φ1) φ1 = φ; paulo@89: } paulo@89: if (antimeridian) { paulo@89: if (λ < λ_) { paulo@89: if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; paulo@89: } else { paulo@89: if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; paulo@89: } paulo@89: } else { paulo@89: if (λ1 >= λ0) { paulo@89: if (λ < λ0) λ0 = λ; paulo@89: if (λ > λ1) λ1 = λ; paulo@89: } else { paulo@89: if (λ > λ_) { paulo@89: if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; paulo@89: } else { paulo@89: if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; paulo@89: } paulo@89: } paulo@89: } paulo@89: } else { paulo@89: point(λ, φ); paulo@89: } paulo@89: p0 = p, λ_ = λ; paulo@89: } paulo@89: function lineStart() { paulo@89: bound.point = linePoint; paulo@89: } paulo@89: function lineEnd() { paulo@89: range[0] = λ0, range[1] = λ1; paulo@89: bound.point = point; paulo@89: p0 = null; paulo@89: } paulo@89: function ringPoint(λ, φ) { paulo@89: if (p0) { paulo@89: var dλ = λ - λ_; paulo@89: dλSum += abs(dλ) > 180 ? dλ + (dλ > 0 ? 360 : -360) : dλ; paulo@89: } else λ__ = λ, φ__ = φ; paulo@89: d3_geo_area.point(λ, φ); paulo@89: linePoint(λ, φ); paulo@89: } paulo@89: function ringStart() { paulo@89: d3_geo_area.lineStart(); paulo@89: } paulo@89: function ringEnd() { paulo@89: ringPoint(λ__, φ__); paulo@89: d3_geo_area.lineEnd(); paulo@89: if (abs(dλSum) > ε) λ0 = -(λ1 = 180); paulo@89: range[0] = λ0, range[1] = λ1; paulo@89: p0 = null; paulo@89: } paulo@89: function angle(λ0, λ1) { paulo@89: return (λ1 -= λ0) < 0 ? λ1 + 360 : λ1; paulo@89: } paulo@89: function compareRanges(a, b) { paulo@89: return a[0] - b[0]; paulo@89: } paulo@89: function withinRange(x, range) { paulo@89: return range[0] <= range[1] ? range[0] <= x && x <= range[1] : x < range[0] || range[1] < x; paulo@89: } paulo@89: return function(feature) { paulo@89: φ1 = λ1 = -(λ0 = φ0 = Infinity); paulo@89: ranges = []; paulo@89: d3.geo.stream(feature, bound); paulo@89: var n = ranges.length; paulo@89: if (n) { paulo@89: ranges.sort(compareRanges); paulo@89: for (var i = 1, a = ranges[0], b, merged = [ a ]; i < n; ++i) { paulo@89: b = ranges[i]; paulo@89: if (withinRange(b[0], a) || withinRange(b[1], a)) { paulo@89: if (angle(a[0], b[1]) > angle(a[0], a[1])) a[1] = b[1]; paulo@89: if (angle(b[0], a[1]) > angle(a[0], a[1])) a[0] = b[0]; paulo@89: } else { paulo@89: merged.push(a = b); paulo@89: } paulo@89: } paulo@89: var best = -Infinity, dλ; paulo@89: for (var n = merged.length - 1, i = 0, a = merged[n], b; i <= n; a = b, ++i) { paulo@89: b = merged[i]; paulo@89: if ((dλ = angle(a[1], b[0])) > best) best = dλ, λ0 = b[0], λ1 = a[1]; paulo@89: } paulo@89: } paulo@89: ranges = range = null; paulo@89: return λ0 === Infinity || φ0 === Infinity ? [ [ NaN, NaN ], [ NaN, NaN ] ] : [ [ λ0, φ0 ], [ λ1, φ1 ] ]; paulo@89: }; paulo@89: }(); paulo@89: d3.geo.centroid = function(object) { paulo@89: d3_geo_centroidW0 = d3_geo_centroidW1 = d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; paulo@89: d3.geo.stream(object, d3_geo_centroid); paulo@89: var x = d3_geo_centroidX2, y = d3_geo_centroidY2, z = d3_geo_centroidZ2, m = x * x + y * y + z * z; paulo@89: if (m < ε2) { paulo@89: x = d3_geo_centroidX1, y = d3_geo_centroidY1, z = d3_geo_centroidZ1; paulo@89: if (d3_geo_centroidW1 < ε) x = d3_geo_centroidX0, y = d3_geo_centroidY0, z = d3_geo_centroidZ0; paulo@89: m = x * x + y * y + z * z; paulo@89: if (m < ε2) return [ NaN, NaN ]; paulo@89: } paulo@89: return [ Math.atan2(y, x) * d3_degrees, d3_asin(z / Math.sqrt(m)) * d3_degrees ]; paulo@89: }; paulo@89: var d3_geo_centroidW0, d3_geo_centroidW1, d3_geo_centroidX0, d3_geo_centroidY0, d3_geo_centroidZ0, d3_geo_centroidX1, d3_geo_centroidY1, d3_geo_centroidZ1, d3_geo_centroidX2, d3_geo_centroidY2, d3_geo_centroidZ2; paulo@89: var d3_geo_centroid = { paulo@89: sphere: d3_noop, paulo@89: point: d3_geo_centroidPoint, paulo@89: lineStart: d3_geo_centroidLineStart, paulo@89: lineEnd: d3_geo_centroidLineEnd, paulo@89: polygonStart: function() { paulo@89: d3_geo_centroid.lineStart = d3_geo_centroidRingStart; paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: d3_geo_centroid.lineStart = d3_geo_centroidLineStart; paulo@89: } paulo@89: }; paulo@89: function d3_geo_centroidPoint(λ, φ) { paulo@89: λ *= d3_radians; paulo@89: var cosφ = Math.cos(φ *= d3_radians); paulo@89: d3_geo_centroidPointXYZ(cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ)); paulo@89: } paulo@89: function d3_geo_centroidPointXYZ(x, y, z) { paulo@89: ++d3_geo_centroidW0; paulo@89: d3_geo_centroidX0 += (x - d3_geo_centroidX0) / d3_geo_centroidW0; paulo@89: d3_geo_centroidY0 += (y - d3_geo_centroidY0) / d3_geo_centroidW0; paulo@89: d3_geo_centroidZ0 += (z - d3_geo_centroidZ0) / d3_geo_centroidW0; paulo@89: } paulo@89: function d3_geo_centroidLineStart() { paulo@89: var x0, y0, z0; paulo@89: d3_geo_centroid.point = function(λ, φ) { paulo@89: λ *= d3_radians; paulo@89: var cosφ = Math.cos(φ *= d3_radians); paulo@89: x0 = cosφ * Math.cos(λ); paulo@89: y0 = cosφ * Math.sin(λ); paulo@89: z0 = Math.sin(φ); paulo@89: d3_geo_centroid.point = nextPoint; paulo@89: d3_geo_centroidPointXYZ(x0, y0, z0); paulo@89: }; paulo@89: function nextPoint(λ, φ) { paulo@89: λ *= d3_radians; paulo@89: var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), w = Math.atan2(Math.sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z); paulo@89: d3_geo_centroidW1 += w; paulo@89: d3_geo_centroidX1 += w * (x0 + (x0 = x)); paulo@89: d3_geo_centroidY1 += w * (y0 + (y0 = y)); paulo@89: d3_geo_centroidZ1 += w * (z0 + (z0 = z)); paulo@89: d3_geo_centroidPointXYZ(x0, y0, z0); paulo@89: } paulo@89: } paulo@89: function d3_geo_centroidLineEnd() { paulo@89: d3_geo_centroid.point = d3_geo_centroidPoint; paulo@89: } paulo@89: function d3_geo_centroidRingStart() { paulo@89: var λ00, φ00, x0, y0, z0; paulo@89: d3_geo_centroid.point = function(λ, φ) { paulo@89: λ00 = λ, φ00 = φ; paulo@89: d3_geo_centroid.point = nextPoint; paulo@89: λ *= d3_radians; paulo@89: var cosφ = Math.cos(φ *= d3_radians); paulo@89: x0 = cosφ * Math.cos(λ); paulo@89: y0 = cosφ * Math.sin(λ); paulo@89: z0 = Math.sin(φ); paulo@89: d3_geo_centroidPointXYZ(x0, y0, z0); paulo@89: }; paulo@89: d3_geo_centroid.lineEnd = function() { paulo@89: nextPoint(λ00, φ00); paulo@89: d3_geo_centroid.lineEnd = d3_geo_centroidLineEnd; paulo@89: d3_geo_centroid.point = d3_geo_centroidPoint; paulo@89: }; paulo@89: function nextPoint(λ, φ) { paulo@89: λ *= d3_radians; paulo@89: var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), cx = y0 * z - z0 * y, cy = z0 * x - x0 * z, cz = x0 * y - y0 * x, m = Math.sqrt(cx * cx + cy * cy + cz * cz), u = x0 * x + y0 * y + z0 * z, v = m && -d3_acos(u) / m, w = Math.atan2(m, u); paulo@89: d3_geo_centroidX2 += v * cx; paulo@89: d3_geo_centroidY2 += v * cy; paulo@89: d3_geo_centroidZ2 += v * cz; paulo@89: d3_geo_centroidW1 += w; paulo@89: d3_geo_centroidX1 += w * (x0 + (x0 = x)); paulo@89: d3_geo_centroidY1 += w * (y0 + (y0 = y)); paulo@89: d3_geo_centroidZ1 += w * (z0 + (z0 = z)); paulo@89: d3_geo_centroidPointXYZ(x0, y0, z0); paulo@89: } paulo@89: } paulo@89: function d3_geo_compose(a, b) { paulo@89: function compose(x, y) { paulo@89: return x = a(x, y), b(x[0], x[1]); paulo@89: } paulo@89: if (a.invert && b.invert) compose.invert = function(x, y) { paulo@89: return x = b.invert(x, y), x && a.invert(x[0], x[1]); paulo@89: }; paulo@89: return compose; paulo@89: } paulo@89: function d3_true() { paulo@89: return true; paulo@89: } paulo@89: function d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener) { paulo@89: var subject = [], clip = []; paulo@89: segments.forEach(function(segment) { paulo@89: if ((n = segment.length - 1) <= 0) return; paulo@89: var n, p0 = segment[0], p1 = segment[n]; paulo@89: if (d3_geo_sphericalEqual(p0, p1)) { paulo@89: listener.lineStart(); paulo@89: for (var i = 0; i < n; ++i) listener.point((p0 = segment[i])[0], p0[1]); paulo@89: listener.lineEnd(); paulo@89: return; paulo@89: } paulo@89: var a = new d3_geo_clipPolygonIntersection(p0, segment, null, true), b = new d3_geo_clipPolygonIntersection(p0, null, a, false); paulo@89: a.o = b; paulo@89: subject.push(a); paulo@89: clip.push(b); paulo@89: a = new d3_geo_clipPolygonIntersection(p1, segment, null, false); paulo@89: b = new d3_geo_clipPolygonIntersection(p1, null, a, true); paulo@89: a.o = b; paulo@89: subject.push(a); paulo@89: clip.push(b); paulo@89: }); paulo@89: clip.sort(compare); paulo@89: d3_geo_clipPolygonLinkCircular(subject); paulo@89: d3_geo_clipPolygonLinkCircular(clip); paulo@89: if (!subject.length) return; paulo@89: for (var i = 0, entry = clipStartInside, n = clip.length; i < n; ++i) { paulo@89: clip[i].e = entry = !entry; paulo@89: } paulo@89: var start = subject[0], points, point; paulo@89: while (1) { paulo@89: var current = start, isSubject = true; paulo@89: while (current.v) if ((current = current.n) === start) return; paulo@89: points = current.z; paulo@89: listener.lineStart(); paulo@89: do { paulo@89: current.v = current.o.v = true; paulo@89: if (current.e) { paulo@89: if (isSubject) { paulo@89: for (var i = 0, n = points.length; i < n; ++i) listener.point((point = points[i])[0], point[1]); paulo@89: } else { paulo@89: interpolate(current.x, current.n.x, 1, listener); paulo@89: } paulo@89: current = current.n; paulo@89: } else { paulo@89: if (isSubject) { paulo@89: points = current.p.z; paulo@89: for (var i = points.length - 1; i >= 0; --i) listener.point((point = points[i])[0], point[1]); paulo@89: } else { paulo@89: interpolate(current.x, current.p.x, -1, listener); paulo@89: } paulo@89: current = current.p; paulo@89: } paulo@89: current = current.o; paulo@89: points = current.z; paulo@89: isSubject = !isSubject; paulo@89: } while (!current.v); paulo@89: listener.lineEnd(); paulo@89: } paulo@89: } paulo@89: function d3_geo_clipPolygonLinkCircular(array) { paulo@89: if (!(n = array.length)) return; paulo@89: var n, i = 0, a = array[0], b; paulo@89: while (++i < n) { paulo@89: a.n = b = array[i]; paulo@89: b.p = a; paulo@89: a = b; paulo@89: } paulo@89: a.n = b = array[0]; paulo@89: b.p = a; paulo@89: } paulo@89: function d3_geo_clipPolygonIntersection(point, points, other, entry) { paulo@89: this.x = point; paulo@89: this.z = points; paulo@89: this.o = other; paulo@89: this.e = entry; paulo@89: this.v = false; paulo@89: this.n = this.p = null; paulo@89: } paulo@89: function d3_geo_clip(pointVisible, clipLine, interpolate, clipStart) { paulo@89: return function(rotate, listener) { paulo@89: var line = clipLine(listener), rotatedClipStart = rotate.invert(clipStart[0], clipStart[1]); paulo@89: var clip = { paulo@89: point: point, paulo@89: lineStart: lineStart, paulo@89: lineEnd: lineEnd, paulo@89: polygonStart: function() { paulo@89: clip.point = pointRing; paulo@89: clip.lineStart = ringStart; paulo@89: clip.lineEnd = ringEnd; paulo@89: segments = []; paulo@89: polygon = []; paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: clip.point = point; paulo@89: clip.lineStart = lineStart; paulo@89: clip.lineEnd = lineEnd; paulo@89: segments = d3.merge(segments); paulo@89: var clipStartInside = d3_geo_pointInPolygon(rotatedClipStart, polygon); paulo@89: if (segments.length) { paulo@89: if (!polygonStarted) listener.polygonStart(), polygonStarted = true; paulo@89: d3_geo_clipPolygon(segments, d3_geo_clipSort, clipStartInside, interpolate, listener); paulo@89: } else if (clipStartInside) { paulo@89: if (!polygonStarted) listener.polygonStart(), polygonStarted = true; paulo@89: listener.lineStart(); paulo@89: interpolate(null, null, 1, listener); paulo@89: listener.lineEnd(); paulo@89: } paulo@89: if (polygonStarted) listener.polygonEnd(), polygonStarted = false; paulo@89: segments = polygon = null; paulo@89: }, paulo@89: sphere: function() { paulo@89: listener.polygonStart(); paulo@89: listener.lineStart(); paulo@89: interpolate(null, null, 1, listener); paulo@89: listener.lineEnd(); paulo@89: listener.polygonEnd(); paulo@89: } paulo@89: }; paulo@89: function point(λ, φ) { paulo@89: var point = rotate(λ, φ); paulo@89: if (pointVisible(λ = point[0], φ = point[1])) listener.point(λ, φ); paulo@89: } paulo@89: function pointLine(λ, φ) { paulo@89: var point = rotate(λ, φ); paulo@89: line.point(point[0], point[1]); paulo@89: } paulo@89: function lineStart() { paulo@89: clip.point = pointLine; paulo@89: line.lineStart(); paulo@89: } paulo@89: function lineEnd() { paulo@89: clip.point = point; paulo@89: line.lineEnd(); paulo@89: } paulo@89: var segments; paulo@89: var buffer = d3_geo_clipBufferListener(), ringListener = clipLine(buffer), polygonStarted = false, polygon, ring; paulo@89: function pointRing(λ, φ) { paulo@89: ring.push([ λ, φ ]); paulo@89: var point = rotate(λ, φ); paulo@89: ringListener.point(point[0], point[1]); paulo@89: } paulo@89: function ringStart() { paulo@89: ringListener.lineStart(); paulo@89: ring = []; paulo@89: } paulo@89: function ringEnd() { paulo@89: pointRing(ring[0][0], ring[0][1]); paulo@89: ringListener.lineEnd(); paulo@89: var clean = ringListener.clean(), ringSegments = buffer.buffer(), segment, n = ringSegments.length; paulo@89: ring.pop(); paulo@89: polygon.push(ring); paulo@89: ring = null; paulo@89: if (!n) return; paulo@89: if (clean & 1) { paulo@89: segment = ringSegments[0]; paulo@89: var n = segment.length - 1, i = -1, point; paulo@89: if (n > 0) { paulo@89: if (!polygonStarted) listener.polygonStart(), polygonStarted = true; paulo@89: listener.lineStart(); paulo@89: while (++i < n) listener.point((point = segment[i])[0], point[1]); paulo@89: listener.lineEnd(); paulo@89: } paulo@89: return; paulo@89: } paulo@89: if (n > 1 && clean & 2) ringSegments.push(ringSegments.pop().concat(ringSegments.shift())); paulo@89: segments.push(ringSegments.filter(d3_geo_clipSegmentLength1)); paulo@89: } paulo@89: return clip; paulo@89: }; paulo@89: } paulo@89: function d3_geo_clipSegmentLength1(segment) { paulo@89: return segment.length > 1; paulo@89: } paulo@89: function d3_geo_clipBufferListener() { paulo@89: var lines = [], line; paulo@89: return { paulo@89: lineStart: function() { paulo@89: lines.push(line = []); paulo@89: }, paulo@89: point: function(λ, φ) { paulo@89: line.push([ λ, φ ]); paulo@89: }, paulo@89: lineEnd: d3_noop, paulo@89: buffer: function() { paulo@89: var buffer = lines; paulo@89: lines = []; paulo@89: line = null; paulo@89: return buffer; paulo@89: }, paulo@89: rejoin: function() { paulo@89: if (lines.length > 1) lines.push(lines.pop().concat(lines.shift())); paulo@89: } paulo@89: }; paulo@89: } paulo@89: function d3_geo_clipSort(a, b) { paulo@89: return ((a = a.x)[0] < 0 ? a[1] - halfπ - ε : halfπ - a[1]) - ((b = b.x)[0] < 0 ? b[1] - halfπ - ε : halfπ - b[1]); paulo@89: } paulo@89: var d3_geo_clipAntimeridian = d3_geo_clip(d3_true, d3_geo_clipAntimeridianLine, d3_geo_clipAntimeridianInterpolate, [ -π, -π / 2 ]); paulo@89: function d3_geo_clipAntimeridianLine(listener) { paulo@89: var λ0 = NaN, φ0 = NaN, sλ0 = NaN, clean; paulo@89: return { paulo@89: lineStart: function() { paulo@89: listener.lineStart(); paulo@89: clean = 1; paulo@89: }, paulo@89: point: function(λ1, φ1) { paulo@89: var sλ1 = λ1 > 0 ? π : -π, dλ = abs(λ1 - λ0); paulo@89: if (abs(dλ - π) < ε) { paulo@89: listener.point(λ0, φ0 = (φ0 + φ1) / 2 > 0 ? halfπ : -halfπ); paulo@89: listener.point(sλ0, φ0); paulo@89: listener.lineEnd(); paulo@89: listener.lineStart(); paulo@89: listener.point(sλ1, φ0); paulo@89: listener.point(λ1, φ0); paulo@89: clean = 0; paulo@89: } else if (sλ0 !== sλ1 && dλ >= π) { paulo@89: if (abs(λ0 - sλ0) < ε) λ0 -= sλ0 * ε; paulo@89: if (abs(λ1 - sλ1) < ε) λ1 -= sλ1 * ε; paulo@89: φ0 = d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1); paulo@89: listener.point(sλ0, φ0); paulo@89: listener.lineEnd(); paulo@89: listener.lineStart(); paulo@89: listener.point(sλ1, φ0); paulo@89: clean = 0; paulo@89: } paulo@89: listener.point(λ0 = λ1, φ0 = φ1); paulo@89: sλ0 = sλ1; paulo@89: }, paulo@89: lineEnd: function() { paulo@89: listener.lineEnd(); paulo@89: λ0 = φ0 = NaN; paulo@89: }, paulo@89: clean: function() { paulo@89: return 2 - clean; paulo@89: } paulo@89: }; paulo@89: } paulo@89: function d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1) { paulo@89: var cosφ0, cosφ1, sinλ0_λ1 = Math.sin(λ0 - λ1); paulo@89: return abs(sinλ0_λ1) > ε ? Math.atan((Math.sin(φ0) * (cosφ1 = Math.cos(φ1)) * Math.sin(λ1) - Math.sin(φ1) * (cosφ0 = Math.cos(φ0)) * Math.sin(λ0)) / (cosφ0 * cosφ1 * sinλ0_λ1)) : (φ0 + φ1) / 2; paulo@89: } paulo@89: function d3_geo_clipAntimeridianInterpolate(from, to, direction, listener) { paulo@89: var φ; paulo@89: if (from == null) { paulo@89: φ = direction * halfπ; paulo@89: listener.point(-π, φ); paulo@89: listener.point(0, φ); paulo@89: listener.point(π, φ); paulo@89: listener.point(π, 0); paulo@89: listener.point(π, -φ); paulo@89: listener.point(0, -φ); paulo@89: listener.point(-π, -φ); paulo@89: listener.point(-π, 0); paulo@89: listener.point(-π, φ); paulo@89: } else if (abs(from[0] - to[0]) > ε) { paulo@89: var s = from[0] < to[0] ? π : -π; paulo@89: φ = direction * s / 2; paulo@89: listener.point(-s, φ); paulo@89: listener.point(0, φ); paulo@89: listener.point(s, φ); paulo@89: } else { paulo@89: listener.point(to[0], to[1]); paulo@89: } paulo@89: } paulo@89: function d3_geo_pointInPolygon(point, polygon) { paulo@89: var meridian = point[0], parallel = point[1], meridianNormal = [ Math.sin(meridian), -Math.cos(meridian), 0 ], polarAngle = 0, winding = 0; paulo@89: d3_geo_areaRingSum.reset(); paulo@89: for (var i = 0, n = polygon.length; i < n; ++i) { paulo@89: var ring = polygon[i], m = ring.length; paulo@89: if (!m) continue; paulo@89: var point0 = ring[0], λ0 = point0[0], φ0 = point0[1] / 2 + π / 4, sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), j = 1; paulo@89: while (true) { paulo@89: if (j === m) j = 0; paulo@89: point = ring[j]; paulo@89: var λ = point[0], φ = point[1] / 2 + π / 4, sinφ = Math.sin(φ), cosφ = Math.cos(φ), dλ = λ - λ0, sdλ = dλ >= 0 ? 1 : -1, adλ = sdλ * dλ, antimeridian = adλ > π, k = sinφ0 * sinφ; paulo@89: d3_geo_areaRingSum.add(Math.atan2(k * sdλ * Math.sin(adλ), cosφ0 * cosφ + k * Math.cos(adλ))); paulo@89: polarAngle += antimeridian ? dλ + sdλ * τ : dλ; paulo@89: if (antimeridian ^ λ0 >= meridian ^ λ >= meridian) { paulo@89: var arc = d3_geo_cartesianCross(d3_geo_cartesian(point0), d3_geo_cartesian(point)); paulo@89: d3_geo_cartesianNormalize(arc); paulo@89: var intersection = d3_geo_cartesianCross(meridianNormal, arc); paulo@89: d3_geo_cartesianNormalize(intersection); paulo@89: var φarc = (antimeridian ^ dλ >= 0 ? -1 : 1) * d3_asin(intersection[2]); paulo@89: if (parallel > φarc || parallel === φarc && (arc[0] || arc[1])) { paulo@89: winding += antimeridian ^ dλ >= 0 ? 1 : -1; paulo@89: } paulo@89: } paulo@89: if (!j++) break; paulo@89: λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ, point0 = point; paulo@89: } paulo@89: } paulo@89: return (polarAngle < -ε || polarAngle < ε && d3_geo_areaRingSum < -ε) ^ winding & 1; paulo@89: } paulo@89: function d3_geo_clipCircle(radius) { paulo@89: var cr = Math.cos(radius), smallRadius = cr > 0, notHemisphere = abs(cr) > ε, interpolate = d3_geo_circleInterpolate(radius, 6 * d3_radians); paulo@89: return d3_geo_clip(visible, clipLine, interpolate, smallRadius ? [ 0, -radius ] : [ -π, radius - π ]); paulo@89: function visible(λ, φ) { paulo@89: return Math.cos(λ) * Math.cos(φ) > cr; paulo@89: } paulo@89: function clipLine(listener) { paulo@89: var point0, c0, v0, v00, clean; paulo@89: return { paulo@89: lineStart: function() { paulo@89: v00 = v0 = false; paulo@89: clean = 1; paulo@89: }, paulo@89: point: function(λ, φ) { paulo@89: var point1 = [ λ, φ ], point2, v = visible(λ, φ), c = smallRadius ? v ? 0 : code(λ, φ) : v ? code(λ + (λ < 0 ? π : -π), φ) : 0; paulo@89: if (!point0 && (v00 = v0 = v)) listener.lineStart(); paulo@89: if (v !== v0) { paulo@89: point2 = intersect(point0, point1); paulo@89: if (d3_geo_sphericalEqual(point0, point2) || d3_geo_sphericalEqual(point1, point2)) { paulo@89: point1[0] += ε; paulo@89: point1[1] += ε; paulo@89: v = visible(point1[0], point1[1]); paulo@89: } paulo@89: } paulo@89: if (v !== v0) { paulo@89: clean = 0; paulo@89: if (v) { paulo@89: listener.lineStart(); paulo@89: point2 = intersect(point1, point0); paulo@89: listener.point(point2[0], point2[1]); paulo@89: } else { paulo@89: point2 = intersect(point0, point1); paulo@89: listener.point(point2[0], point2[1]); paulo@89: listener.lineEnd(); paulo@89: } paulo@89: point0 = point2; paulo@89: } else if (notHemisphere && point0 && smallRadius ^ v) { paulo@89: var t; paulo@89: if (!(c & c0) && (t = intersect(point1, point0, true))) { paulo@89: clean = 0; paulo@89: if (smallRadius) { paulo@89: listener.lineStart(); paulo@89: listener.point(t[0][0], t[0][1]); paulo@89: listener.point(t[1][0], t[1][1]); paulo@89: listener.lineEnd(); paulo@89: } else { paulo@89: listener.point(t[1][0], t[1][1]); paulo@89: listener.lineEnd(); paulo@89: listener.lineStart(); paulo@89: listener.point(t[0][0], t[0][1]); paulo@89: } paulo@89: } paulo@89: } paulo@89: if (v && (!point0 || !d3_geo_sphericalEqual(point0, point1))) { paulo@89: listener.point(point1[0], point1[1]); paulo@89: } paulo@89: point0 = point1, v0 = v, c0 = c; paulo@89: }, paulo@89: lineEnd: function() { paulo@89: if (v0) listener.lineEnd(); paulo@89: point0 = null; paulo@89: }, paulo@89: clean: function() { paulo@89: return clean | (v00 && v0) << 1; paulo@89: } paulo@89: }; paulo@89: } paulo@89: function intersect(a, b, two) { paulo@89: var pa = d3_geo_cartesian(a), pb = d3_geo_cartesian(b); paulo@89: var n1 = [ 1, 0, 0 ], n2 = d3_geo_cartesianCross(pa, pb), n2n2 = d3_geo_cartesianDot(n2, n2), n1n2 = n2[0], determinant = n2n2 - n1n2 * n1n2; paulo@89: if (!determinant) return !two && a; paulo@89: var c1 = cr * n2n2 / determinant, c2 = -cr * n1n2 / determinant, n1xn2 = d3_geo_cartesianCross(n1, n2), A = d3_geo_cartesianScale(n1, c1), B = d3_geo_cartesianScale(n2, c2); paulo@89: d3_geo_cartesianAdd(A, B); paulo@89: var u = n1xn2, w = d3_geo_cartesianDot(A, u), uu = d3_geo_cartesianDot(u, u), t2 = w * w - uu * (d3_geo_cartesianDot(A, A) - 1); paulo@89: if (t2 < 0) return; paulo@89: var t = Math.sqrt(t2), q = d3_geo_cartesianScale(u, (-w - t) / uu); paulo@89: d3_geo_cartesianAdd(q, A); paulo@89: q = d3_geo_spherical(q); paulo@89: if (!two) return q; paulo@89: var λ0 = a[0], λ1 = b[0], φ0 = a[1], φ1 = b[1], z; paulo@89: if (λ1 < λ0) z = λ0, λ0 = λ1, λ1 = z; paulo@89: var δλ = λ1 - λ0, polar = abs(δλ - π) < ε, meridian = polar || δλ < ε; paulo@89: if (!polar && φ1 < φ0) z = φ0, φ0 = φ1, φ1 = z; paulo@89: if (meridian ? polar ? φ0 + φ1 > 0 ^ q[1] < (abs(q[0] - λ0) < ε ? φ0 : φ1) : φ0 <= q[1] && q[1] <= φ1 : δλ > π ^ (λ0 <= q[0] && q[0] <= λ1)) { paulo@89: var q1 = d3_geo_cartesianScale(u, (-w + t) / uu); paulo@89: d3_geo_cartesianAdd(q1, A); paulo@89: return [ q, d3_geo_spherical(q1) ]; paulo@89: } paulo@89: } paulo@89: function code(λ, φ) { paulo@89: var r = smallRadius ? radius : π - radius, code = 0; paulo@89: if (λ < -r) code |= 1; else if (λ > r) code |= 2; paulo@89: if (φ < -r) code |= 4; else if (φ > r) code |= 8; paulo@89: return code; paulo@89: } paulo@89: } paulo@89: function d3_geom_clipLine(x0, y0, x1, y1) { paulo@89: return function(line) { paulo@89: var a = line.a, b = line.b, ax = a.x, ay = a.y, bx = b.x, by = b.y, t0 = 0, t1 = 1, dx = bx - ax, dy = by - ay, r; paulo@89: r = x0 - ax; paulo@89: if (!dx && r > 0) return; paulo@89: r /= dx; paulo@89: if (dx < 0) { paulo@89: if (r < t0) return; paulo@89: if (r < t1) t1 = r; paulo@89: } else if (dx > 0) { paulo@89: if (r > t1) return; paulo@89: if (r > t0) t0 = r; paulo@89: } paulo@89: r = x1 - ax; paulo@89: if (!dx && r < 0) return; paulo@89: r /= dx; paulo@89: if (dx < 0) { paulo@89: if (r > t1) return; paulo@89: if (r > t0) t0 = r; paulo@89: } else if (dx > 0) { paulo@89: if (r < t0) return; paulo@89: if (r < t1) t1 = r; paulo@89: } paulo@89: r = y0 - ay; paulo@89: if (!dy && r > 0) return; paulo@89: r /= dy; paulo@89: if (dy < 0) { paulo@89: if (r < t0) return; paulo@89: if (r < t1) t1 = r; paulo@89: } else if (dy > 0) { paulo@89: if (r > t1) return; paulo@89: if (r > t0) t0 = r; paulo@89: } paulo@89: r = y1 - ay; paulo@89: if (!dy && r < 0) return; paulo@89: r /= dy; paulo@89: if (dy < 0) { paulo@89: if (r > t1) return; paulo@89: if (r > t0) t0 = r; paulo@89: } else if (dy > 0) { paulo@89: if (r < t0) return; paulo@89: if (r < t1) t1 = r; paulo@89: } paulo@89: if (t0 > 0) line.a = { paulo@89: x: ax + t0 * dx, paulo@89: y: ay + t0 * dy paulo@89: }; paulo@89: if (t1 < 1) line.b = { paulo@89: x: ax + t1 * dx, paulo@89: y: ay + t1 * dy paulo@89: }; paulo@89: return line; paulo@89: }; paulo@89: } paulo@89: var d3_geo_clipExtentMAX = 1e9; paulo@89: d3.geo.clipExtent = function() { paulo@89: var x0, y0, x1, y1, stream, clip, clipExtent = { paulo@89: stream: function(output) { paulo@89: if (stream) stream.valid = false; paulo@89: stream = clip(output); paulo@89: stream.valid = true; paulo@89: return stream; paulo@89: }, paulo@89: extent: function(_) { paulo@89: if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; paulo@89: clip = d3_geo_clipExtent(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]); paulo@89: if (stream) stream.valid = false, stream = null; paulo@89: return clipExtent; paulo@89: } paulo@89: }; paulo@89: return clipExtent.extent([ [ 0, 0 ], [ 960, 500 ] ]); paulo@89: }; paulo@89: function d3_geo_clipExtent(x0, y0, x1, y1) { paulo@89: return function(listener) { paulo@89: var listener_ = listener, bufferListener = d3_geo_clipBufferListener(), clipLine = d3_geom_clipLine(x0, y0, x1, y1), segments, polygon, ring; paulo@89: var clip = { paulo@89: point: point, paulo@89: lineStart: lineStart, paulo@89: lineEnd: lineEnd, paulo@89: polygonStart: function() { paulo@89: listener = bufferListener; paulo@89: segments = []; paulo@89: polygon = []; paulo@89: clean = true; paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: listener = listener_; paulo@89: segments = d3.merge(segments); paulo@89: var clipStartInside = insidePolygon([ x0, y1 ]), inside = clean && clipStartInside, visible = segments.length; paulo@89: if (inside || visible) { paulo@89: listener.polygonStart(); paulo@89: if (inside) { paulo@89: listener.lineStart(); paulo@89: interpolate(null, null, 1, listener); paulo@89: listener.lineEnd(); paulo@89: } paulo@89: if (visible) { paulo@89: d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener); paulo@89: } paulo@89: listener.polygonEnd(); paulo@89: } paulo@89: segments = polygon = ring = null; paulo@89: } paulo@89: }; paulo@89: function insidePolygon(p) { paulo@89: var wn = 0, n = polygon.length, y = p[1]; paulo@89: for (var i = 0; i < n; ++i) { paulo@89: for (var j = 1, v = polygon[i], m = v.length, a = v[0], b; j < m; ++j) { paulo@89: b = v[j]; paulo@89: if (a[1] <= y) { paulo@89: if (b[1] > y && d3_cross2d(a, b, p) > 0) ++wn; paulo@89: } else { paulo@89: if (b[1] <= y && d3_cross2d(a, b, p) < 0) --wn; paulo@89: } paulo@89: a = b; paulo@89: } paulo@89: } paulo@89: return wn !== 0; paulo@89: } paulo@89: function interpolate(from, to, direction, listener) { paulo@89: var a = 0, a1 = 0; paulo@89: if (from == null || (a = corner(from, direction)) !== (a1 = corner(to, direction)) || comparePoints(from, to) < 0 ^ direction > 0) { paulo@89: do { paulo@89: listener.point(a === 0 || a === 3 ? x0 : x1, a > 1 ? y1 : y0); paulo@89: } while ((a = (a + direction + 4) % 4) !== a1); paulo@89: } else { paulo@89: listener.point(to[0], to[1]); paulo@89: } paulo@89: } paulo@89: function pointVisible(x, y) { paulo@89: return x0 <= x && x <= x1 && y0 <= y && y <= y1; paulo@89: } paulo@89: function point(x, y) { paulo@89: if (pointVisible(x, y)) listener.point(x, y); paulo@89: } paulo@89: var x__, y__, v__, x_, y_, v_, first, clean; paulo@89: function lineStart() { paulo@89: clip.point = linePoint; paulo@89: if (polygon) polygon.push(ring = []); paulo@89: first = true; paulo@89: v_ = false; paulo@89: x_ = y_ = NaN; paulo@89: } paulo@89: function lineEnd() { paulo@89: if (segments) { paulo@89: linePoint(x__, y__); paulo@89: if (v__ && v_) bufferListener.rejoin(); paulo@89: segments.push(bufferListener.buffer()); paulo@89: } paulo@89: clip.point = point; paulo@89: if (v_) listener.lineEnd(); paulo@89: } paulo@89: function linePoint(x, y) { paulo@89: x = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, x)); paulo@89: y = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, y)); paulo@89: var v = pointVisible(x, y); paulo@89: if (polygon) ring.push([ x, y ]); paulo@89: if (first) { paulo@89: x__ = x, y__ = y, v__ = v; paulo@89: first = false; paulo@89: if (v) { paulo@89: listener.lineStart(); paulo@89: listener.point(x, y); paulo@89: } paulo@89: } else { paulo@89: if (v && v_) listener.point(x, y); else { paulo@89: var l = { paulo@89: a: { paulo@89: x: x_, paulo@89: y: y_ paulo@89: }, paulo@89: b: { paulo@89: x: x, paulo@89: y: y paulo@89: } paulo@89: }; paulo@89: if (clipLine(l)) { paulo@89: if (!v_) { paulo@89: listener.lineStart(); paulo@89: listener.point(l.a.x, l.a.y); paulo@89: } paulo@89: listener.point(l.b.x, l.b.y); paulo@89: if (!v) listener.lineEnd(); paulo@89: clean = false; paulo@89: } else if (v) { paulo@89: listener.lineStart(); paulo@89: listener.point(x, y); paulo@89: clean = false; paulo@89: } paulo@89: } paulo@89: } paulo@89: x_ = x, y_ = y, v_ = v; paulo@89: } paulo@89: return clip; paulo@89: }; paulo@89: function corner(p, direction) { paulo@89: return abs(p[0] - x0) < ε ? direction > 0 ? 0 : 3 : abs(p[0] - x1) < ε ? direction > 0 ? 2 : 1 : abs(p[1] - y0) < ε ? direction > 0 ? 1 : 0 : direction > 0 ? 3 : 2; paulo@89: } paulo@89: function compare(a, b) { paulo@89: return comparePoints(a.x, b.x); paulo@89: } paulo@89: function comparePoints(a, b) { paulo@89: var ca = corner(a, 1), cb = corner(b, 1); paulo@89: return ca !== cb ? ca - cb : ca === 0 ? b[1] - a[1] : ca === 1 ? a[0] - b[0] : ca === 2 ? a[1] - b[1] : b[0] - a[0]; paulo@89: } paulo@89: } paulo@89: function d3_geo_conic(projectAt) { paulo@89: var φ0 = 0, φ1 = π / 3, m = d3_geo_projectionMutator(projectAt), p = m(φ0, φ1); paulo@89: p.parallels = function(_) { paulo@89: if (!arguments.length) return [ φ0 / π * 180, φ1 / π * 180 ]; paulo@89: return m(φ0 = _[0] * π / 180, φ1 = _[1] * π / 180); paulo@89: }; paulo@89: return p; paulo@89: } paulo@89: function d3_geo_conicEqualArea(φ0, φ1) { paulo@89: var sinφ0 = Math.sin(φ0), n = (sinφ0 + Math.sin(φ1)) / 2, C = 1 + sinφ0 * (2 * n - sinφ0), ρ0 = Math.sqrt(C) / n; paulo@89: function forward(λ, φ) { paulo@89: var ρ = Math.sqrt(C - 2 * n * Math.sin(φ)) / n; paulo@89: return [ ρ * Math.sin(λ *= n), ρ0 - ρ * Math.cos(λ) ]; paulo@89: } paulo@89: forward.invert = function(x, y) { paulo@89: var ρ0_y = ρ0 - y; paulo@89: return [ Math.atan2(x, ρ0_y) / n, d3_asin((C - (x * x + ρ0_y * ρ0_y) * n * n) / (2 * n)) ]; paulo@89: }; paulo@89: return forward; paulo@89: } paulo@89: (d3.geo.conicEqualArea = function() { paulo@89: return d3_geo_conic(d3_geo_conicEqualArea); paulo@89: }).raw = d3_geo_conicEqualArea; paulo@89: d3.geo.albers = function() { paulo@89: return d3.geo.conicEqualArea().rotate([ 96, 0 ]).center([ -.6, 38.7 ]).parallels([ 29.5, 45.5 ]).scale(1070); paulo@89: }; paulo@89: d3.geo.albersUsa = function() { paulo@89: var lower48 = d3.geo.albers(); paulo@89: var alaska = d3.geo.conicEqualArea().rotate([ 154, 0 ]).center([ -2, 58.5 ]).parallels([ 55, 65 ]); paulo@89: var hawaii = d3.geo.conicEqualArea().rotate([ 157, 0 ]).center([ -3, 19.9 ]).parallels([ 8, 18 ]); paulo@89: var point, pointStream = { paulo@89: point: function(x, y) { paulo@89: point = [ x, y ]; paulo@89: } paulo@89: }, lower48Point, alaskaPoint, hawaiiPoint; paulo@89: function albersUsa(coordinates) { paulo@89: var x = coordinates[0], y = coordinates[1]; paulo@89: point = null; paulo@89: (lower48Point(x, y), point) || (alaskaPoint(x, y), point) || hawaiiPoint(x, y); paulo@89: return point; paulo@89: } paulo@89: albersUsa.invert = function(coordinates) { paulo@89: var k = lower48.scale(), t = lower48.translate(), x = (coordinates[0] - t[0]) / k, y = (coordinates[1] - t[1]) / k; paulo@89: return (y >= .12 && y < .234 && x >= -.425 && x < -.214 ? alaska : y >= .166 && y < .234 && x >= -.214 && x < -.115 ? hawaii : lower48).invert(coordinates); paulo@89: }; paulo@89: albersUsa.stream = function(stream) { paulo@89: var lower48Stream = lower48.stream(stream), alaskaStream = alaska.stream(stream), hawaiiStream = hawaii.stream(stream); paulo@89: return { paulo@89: point: function(x, y) { paulo@89: lower48Stream.point(x, y); paulo@89: alaskaStream.point(x, y); paulo@89: hawaiiStream.point(x, y); paulo@89: }, paulo@89: sphere: function() { paulo@89: lower48Stream.sphere(); paulo@89: alaskaStream.sphere(); paulo@89: hawaiiStream.sphere(); paulo@89: }, paulo@89: lineStart: function() { paulo@89: lower48Stream.lineStart(); paulo@89: alaskaStream.lineStart(); paulo@89: hawaiiStream.lineStart(); paulo@89: }, paulo@89: lineEnd: function() { paulo@89: lower48Stream.lineEnd(); paulo@89: alaskaStream.lineEnd(); paulo@89: hawaiiStream.lineEnd(); paulo@89: }, paulo@89: polygonStart: function() { paulo@89: lower48Stream.polygonStart(); paulo@89: alaskaStream.polygonStart(); paulo@89: hawaiiStream.polygonStart(); paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: lower48Stream.polygonEnd(); paulo@89: alaskaStream.polygonEnd(); paulo@89: hawaiiStream.polygonEnd(); paulo@89: } paulo@89: }; paulo@89: }; paulo@89: albersUsa.precision = function(_) { paulo@89: if (!arguments.length) return lower48.precision(); paulo@89: lower48.precision(_); paulo@89: alaska.precision(_); paulo@89: hawaii.precision(_); paulo@89: return albersUsa; paulo@89: }; paulo@89: albersUsa.scale = function(_) { paulo@89: if (!arguments.length) return lower48.scale(); paulo@89: lower48.scale(_); paulo@89: alaska.scale(_ * .35); paulo@89: hawaii.scale(_); paulo@89: return albersUsa.translate(lower48.translate()); paulo@89: }; paulo@89: albersUsa.translate = function(_) { paulo@89: if (!arguments.length) return lower48.translate(); paulo@89: var k = lower48.scale(), x = +_[0], y = +_[1]; paulo@89: lower48Point = lower48.translate(_).clipExtent([ [ x - .455 * k, y - .238 * k ], [ x + .455 * k, y + .238 * k ] ]).stream(pointStream).point; paulo@89: alaskaPoint = alaska.translate([ x - .307 * k, y + .201 * k ]).clipExtent([ [ x - .425 * k + ε, y + .12 * k + ε ], [ x - .214 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; paulo@89: hawaiiPoint = hawaii.translate([ x - .205 * k, y + .212 * k ]).clipExtent([ [ x - .214 * k + ε, y + .166 * k + ε ], [ x - .115 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; paulo@89: return albersUsa; paulo@89: }; paulo@89: return albersUsa.scale(1070); paulo@89: }; paulo@89: var d3_geo_pathAreaSum, d3_geo_pathAreaPolygon, d3_geo_pathArea = { paulo@89: point: d3_noop, paulo@89: lineStart: d3_noop, paulo@89: lineEnd: d3_noop, paulo@89: polygonStart: function() { paulo@89: d3_geo_pathAreaPolygon = 0; paulo@89: d3_geo_pathArea.lineStart = d3_geo_pathAreaRingStart; paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: d3_geo_pathArea.lineStart = d3_geo_pathArea.lineEnd = d3_geo_pathArea.point = d3_noop; paulo@89: d3_geo_pathAreaSum += abs(d3_geo_pathAreaPolygon / 2); paulo@89: } paulo@89: }; paulo@89: function d3_geo_pathAreaRingStart() { paulo@89: var x00, y00, x0, y0; paulo@89: d3_geo_pathArea.point = function(x, y) { paulo@89: d3_geo_pathArea.point = nextPoint; paulo@89: x00 = x0 = x, y00 = y0 = y; paulo@89: }; paulo@89: function nextPoint(x, y) { paulo@89: d3_geo_pathAreaPolygon += y0 * x - x0 * y; paulo@89: x0 = x, y0 = y; paulo@89: } paulo@89: d3_geo_pathArea.lineEnd = function() { paulo@89: nextPoint(x00, y00); paulo@89: }; paulo@89: } paulo@89: var d3_geo_pathBoundsX0, d3_geo_pathBoundsY0, d3_geo_pathBoundsX1, d3_geo_pathBoundsY1; paulo@89: var d3_geo_pathBounds = { paulo@89: point: d3_geo_pathBoundsPoint, paulo@89: lineStart: d3_noop, paulo@89: lineEnd: d3_noop, paulo@89: polygonStart: d3_noop, paulo@89: polygonEnd: d3_noop paulo@89: }; paulo@89: function d3_geo_pathBoundsPoint(x, y) { paulo@89: if (x < d3_geo_pathBoundsX0) d3_geo_pathBoundsX0 = x; paulo@89: if (x > d3_geo_pathBoundsX1) d3_geo_pathBoundsX1 = x; paulo@89: if (y < d3_geo_pathBoundsY0) d3_geo_pathBoundsY0 = y; paulo@89: if (y > d3_geo_pathBoundsY1) d3_geo_pathBoundsY1 = y; paulo@89: } paulo@89: function d3_geo_pathBuffer() { paulo@89: var pointCircle = d3_geo_pathBufferCircle(4.5), buffer = []; paulo@89: var stream = { paulo@89: point: point, paulo@89: lineStart: function() { paulo@89: stream.point = pointLineStart; paulo@89: }, paulo@89: lineEnd: lineEnd, paulo@89: polygonStart: function() { paulo@89: stream.lineEnd = lineEndPolygon; paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: stream.lineEnd = lineEnd; paulo@89: stream.point = point; paulo@89: }, paulo@89: pointRadius: function(_) { paulo@89: pointCircle = d3_geo_pathBufferCircle(_); paulo@89: return stream; paulo@89: }, paulo@89: result: function() { paulo@89: if (buffer.length) { paulo@89: var result = buffer.join(""); paulo@89: buffer = []; paulo@89: return result; paulo@89: } paulo@89: } paulo@89: }; paulo@89: function point(x, y) { paulo@89: buffer.push("M", x, ",", y, pointCircle); paulo@89: } paulo@89: function pointLineStart(x, y) { paulo@89: buffer.push("M", x, ",", y); paulo@89: stream.point = pointLine; paulo@89: } paulo@89: function pointLine(x, y) { paulo@89: buffer.push("L", x, ",", y); paulo@89: } paulo@89: function lineEnd() { paulo@89: stream.point = point; paulo@89: } paulo@89: function lineEndPolygon() { paulo@89: buffer.push("Z"); paulo@89: } paulo@89: return stream; paulo@89: } paulo@89: function d3_geo_pathBufferCircle(radius) { paulo@89: return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + 2 * radius + "z"; paulo@89: } paulo@89: var d3_geo_pathCentroid = { paulo@89: point: d3_geo_pathCentroidPoint, paulo@89: lineStart: d3_geo_pathCentroidLineStart, paulo@89: lineEnd: d3_geo_pathCentroidLineEnd, paulo@89: polygonStart: function() { paulo@89: d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidRingStart; paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; paulo@89: d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidLineStart; paulo@89: d3_geo_pathCentroid.lineEnd = d3_geo_pathCentroidLineEnd; paulo@89: } paulo@89: }; paulo@89: function d3_geo_pathCentroidPoint(x, y) { paulo@89: d3_geo_centroidX0 += x; paulo@89: d3_geo_centroidY0 += y; paulo@89: ++d3_geo_centroidZ0; paulo@89: } paulo@89: function d3_geo_pathCentroidLineStart() { paulo@89: var x0, y0; paulo@89: d3_geo_pathCentroid.point = function(x, y) { paulo@89: d3_geo_pathCentroid.point = nextPoint; paulo@89: d3_geo_pathCentroidPoint(x0 = x, y0 = y); paulo@89: }; paulo@89: function nextPoint(x, y) { paulo@89: var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); paulo@89: d3_geo_centroidX1 += z * (x0 + x) / 2; paulo@89: d3_geo_centroidY1 += z * (y0 + y) / 2; paulo@89: d3_geo_centroidZ1 += z; paulo@89: d3_geo_pathCentroidPoint(x0 = x, y0 = y); paulo@89: } paulo@89: } paulo@89: function d3_geo_pathCentroidLineEnd() { paulo@89: d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; paulo@89: } paulo@89: function d3_geo_pathCentroidRingStart() { paulo@89: var x00, y00, x0, y0; paulo@89: d3_geo_pathCentroid.point = function(x, y) { paulo@89: d3_geo_pathCentroid.point = nextPoint; paulo@89: d3_geo_pathCentroidPoint(x00 = x0 = x, y00 = y0 = y); paulo@89: }; paulo@89: function nextPoint(x, y) { paulo@89: var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); paulo@89: d3_geo_centroidX1 += z * (x0 + x) / 2; paulo@89: d3_geo_centroidY1 += z * (y0 + y) / 2; paulo@89: d3_geo_centroidZ1 += z; paulo@89: z = y0 * x - x0 * y; paulo@89: d3_geo_centroidX2 += z * (x0 + x); paulo@89: d3_geo_centroidY2 += z * (y0 + y); paulo@89: d3_geo_centroidZ2 += z * 3; paulo@89: d3_geo_pathCentroidPoint(x0 = x, y0 = y); paulo@89: } paulo@89: d3_geo_pathCentroid.lineEnd = function() { paulo@89: nextPoint(x00, y00); paulo@89: }; paulo@89: } paulo@89: function d3_geo_pathContext(context) { paulo@89: var pointRadius = 4.5; paulo@89: var stream = { paulo@89: point: point, paulo@89: lineStart: function() { paulo@89: stream.point = pointLineStart; paulo@89: }, paulo@89: lineEnd: lineEnd, paulo@89: polygonStart: function() { paulo@89: stream.lineEnd = lineEndPolygon; paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: stream.lineEnd = lineEnd; paulo@89: stream.point = point; paulo@89: }, paulo@89: pointRadius: function(_) { paulo@89: pointRadius = _; paulo@89: return stream; paulo@89: }, paulo@89: result: d3_noop paulo@89: }; paulo@89: function point(x, y) { paulo@89: context.moveTo(x + pointRadius, y); paulo@89: context.arc(x, y, pointRadius, 0, τ); paulo@89: } paulo@89: function pointLineStart(x, y) { paulo@89: context.moveTo(x, y); paulo@89: stream.point = pointLine; paulo@89: } paulo@89: function pointLine(x, y) { paulo@89: context.lineTo(x, y); paulo@89: } paulo@89: function lineEnd() { paulo@89: stream.point = point; paulo@89: } paulo@89: function lineEndPolygon() { paulo@89: context.closePath(); paulo@89: } paulo@89: return stream; paulo@89: } paulo@89: function d3_geo_resample(project) { paulo@89: var δ2 = .5, cosMinDistance = Math.cos(30 * d3_radians), maxDepth = 16; paulo@89: function resample(stream) { paulo@89: return (maxDepth ? resampleRecursive : resampleNone)(stream); paulo@89: } paulo@89: function resampleNone(stream) { paulo@89: return d3_geo_transformPoint(stream, function(x, y) { paulo@89: x = project(x, y); paulo@89: stream.point(x[0], x[1]); paulo@89: }); paulo@89: } paulo@89: function resampleRecursive(stream) { paulo@89: var λ00, φ00, x00, y00, a00, b00, c00, λ0, x0, y0, a0, b0, c0; paulo@89: var resample = { paulo@89: point: point, paulo@89: lineStart: lineStart, paulo@89: lineEnd: lineEnd, paulo@89: polygonStart: function() { paulo@89: stream.polygonStart(); paulo@89: resample.lineStart = ringStart; paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: stream.polygonEnd(); paulo@89: resample.lineStart = lineStart; paulo@89: } paulo@89: }; paulo@89: function point(x, y) { paulo@89: x = project(x, y); paulo@89: stream.point(x[0], x[1]); paulo@89: } paulo@89: function lineStart() { paulo@89: x0 = NaN; paulo@89: resample.point = linePoint; paulo@89: stream.lineStart(); paulo@89: } paulo@89: function linePoint(λ, φ) { paulo@89: var c = d3_geo_cartesian([ λ, φ ]), p = project(λ, φ); paulo@89: resampleLineTo(x0, y0, λ0, a0, b0, c0, x0 = p[0], y0 = p[1], λ0 = λ, a0 = c[0], b0 = c[1], c0 = c[2], maxDepth, stream); paulo@89: stream.point(x0, y0); paulo@89: } paulo@89: function lineEnd() { paulo@89: resample.point = point; paulo@89: stream.lineEnd(); paulo@89: } paulo@89: function ringStart() { paulo@89: lineStart(); paulo@89: resample.point = ringPoint; paulo@89: resample.lineEnd = ringEnd; paulo@89: } paulo@89: function ringPoint(λ, φ) { paulo@89: linePoint(λ00 = λ, φ00 = φ), x00 = x0, y00 = y0, a00 = a0, b00 = b0, c00 = c0; paulo@89: resample.point = linePoint; paulo@89: } paulo@89: function ringEnd() { paulo@89: resampleLineTo(x0, y0, λ0, a0, b0, c0, x00, y00, λ00, a00, b00, c00, maxDepth, stream); paulo@89: resample.lineEnd = lineEnd; paulo@89: lineEnd(); paulo@89: } paulo@89: return resample; paulo@89: } paulo@89: function resampleLineTo(x0, y0, λ0, a0, b0, c0, x1, y1, λ1, a1, b1, c1, depth, stream) { paulo@89: var dx = x1 - x0, dy = y1 - y0, d2 = dx * dx + dy * dy; paulo@89: if (d2 > 4 * δ2 && depth--) { paulo@89: var a = a0 + a1, b = b0 + b1, c = c0 + c1, m = Math.sqrt(a * a + b * b + c * c), φ2 = Math.asin(c /= m), λ2 = abs(abs(c) - 1) < ε || abs(λ0 - λ1) < ε ? (λ0 + λ1) / 2 : Math.atan2(b, a), p = project(λ2, φ2), x2 = p[0], y2 = p[1], dx2 = x2 - x0, dy2 = y2 - y0, dz = dy * dx2 - dx * dy2; paulo@89: if (dz * dz / d2 > δ2 || abs((dx * dx2 + dy * dy2) / d2 - .5) > .3 || a0 * a1 + b0 * b1 + c0 * c1 < cosMinDistance) { paulo@89: resampleLineTo(x0, y0, λ0, a0, b0, c0, x2, y2, λ2, a /= m, b /= m, c, depth, stream); paulo@89: stream.point(x2, y2); paulo@89: resampleLineTo(x2, y2, λ2, a, b, c, x1, y1, λ1, a1, b1, c1, depth, stream); paulo@89: } paulo@89: } paulo@89: } paulo@89: resample.precision = function(_) { paulo@89: if (!arguments.length) return Math.sqrt(δ2); paulo@89: maxDepth = (δ2 = _ * _) > 0 && 16; paulo@89: return resample; paulo@89: }; paulo@89: return resample; paulo@89: } paulo@89: d3.geo.path = function() { paulo@89: var pointRadius = 4.5, projection, context, projectStream, contextStream, cacheStream; paulo@89: function path(object) { paulo@89: if (object) { paulo@89: if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments)); paulo@89: if (!cacheStream || !cacheStream.valid) cacheStream = projectStream(contextStream); paulo@89: d3.geo.stream(object, cacheStream); paulo@89: } paulo@89: return contextStream.result(); paulo@89: } paulo@89: path.area = function(object) { paulo@89: d3_geo_pathAreaSum = 0; paulo@89: d3.geo.stream(object, projectStream(d3_geo_pathArea)); paulo@89: return d3_geo_pathAreaSum; paulo@89: }; paulo@89: path.centroid = function(object) { paulo@89: d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; paulo@89: d3.geo.stream(object, projectStream(d3_geo_pathCentroid)); paulo@89: return d3_geo_centroidZ2 ? [ d3_geo_centroidX2 / d3_geo_centroidZ2, d3_geo_centroidY2 / d3_geo_centroidZ2 ] : d3_geo_centroidZ1 ? [ d3_geo_centroidX1 / d3_geo_centroidZ1, d3_geo_centroidY1 / d3_geo_centroidZ1 ] : d3_geo_centroidZ0 ? [ d3_geo_centroidX0 / d3_geo_centroidZ0, d3_geo_centroidY0 / d3_geo_centroidZ0 ] : [ NaN, NaN ]; paulo@89: }; paulo@89: path.bounds = function(object) { paulo@89: d3_geo_pathBoundsX1 = d3_geo_pathBoundsY1 = -(d3_geo_pathBoundsX0 = d3_geo_pathBoundsY0 = Infinity); paulo@89: d3.geo.stream(object, projectStream(d3_geo_pathBounds)); paulo@89: return [ [ d3_geo_pathBoundsX0, d3_geo_pathBoundsY0 ], [ d3_geo_pathBoundsX1, d3_geo_pathBoundsY1 ] ]; paulo@89: }; paulo@89: path.projection = function(_) { paulo@89: if (!arguments.length) return projection; paulo@89: projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity; paulo@89: return reset(); paulo@89: }; paulo@89: path.context = function(_) { paulo@89: if (!arguments.length) return context; paulo@89: contextStream = (context = _) == null ? new d3_geo_pathBuffer() : new d3_geo_pathContext(_); paulo@89: if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius); paulo@89: return reset(); paulo@89: }; paulo@89: path.pointRadius = function(_) { paulo@89: if (!arguments.length) return pointRadius; paulo@89: pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_); paulo@89: return path; paulo@89: }; paulo@89: function reset() { paulo@89: cacheStream = null; paulo@89: return path; paulo@89: } paulo@89: return path.projection(d3.geo.albersUsa()).context(null); paulo@89: }; paulo@89: function d3_geo_pathProjectStream(project) { paulo@89: var resample = d3_geo_resample(function(x, y) { paulo@89: return project([ x * d3_degrees, y * d3_degrees ]); paulo@89: }); paulo@89: return function(stream) { paulo@89: return d3_geo_projectionRadians(resample(stream)); paulo@89: }; paulo@89: } paulo@89: d3.geo.transform = function(methods) { paulo@89: return { paulo@89: stream: function(stream) { paulo@89: var transform = new d3_geo_transform(stream); paulo@89: for (var k in methods) transform[k] = methods[k]; paulo@89: return transform; paulo@89: } paulo@89: }; paulo@89: }; paulo@89: function d3_geo_transform(stream) { paulo@89: this.stream = stream; paulo@89: } paulo@89: d3_geo_transform.prototype = { paulo@89: point: function(x, y) { paulo@89: this.stream.point(x, y); paulo@89: }, paulo@89: sphere: function() { paulo@89: this.stream.sphere(); paulo@89: }, paulo@89: lineStart: function() { paulo@89: this.stream.lineStart(); paulo@89: }, paulo@89: lineEnd: function() { paulo@89: this.stream.lineEnd(); paulo@89: }, paulo@89: polygonStart: function() { paulo@89: this.stream.polygonStart(); paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: this.stream.polygonEnd(); paulo@89: } paulo@89: }; paulo@89: function d3_geo_transformPoint(stream, point) { paulo@89: return { paulo@89: point: point, paulo@89: sphere: function() { paulo@89: stream.sphere(); paulo@89: }, paulo@89: lineStart: function() { paulo@89: stream.lineStart(); paulo@89: }, paulo@89: lineEnd: function() { paulo@89: stream.lineEnd(); paulo@89: }, paulo@89: polygonStart: function() { paulo@89: stream.polygonStart(); paulo@89: }, paulo@89: polygonEnd: function() { paulo@89: stream.polygonEnd(); paulo@89: } paulo@89: }; paulo@89: } paulo@89: d3.geo.projection = d3_geo_projection; paulo@89: d3.geo.projectionMutator = d3_geo_projectionMutator; paulo@89: function d3_geo_projection(project) { paulo@89: return d3_geo_projectionMutator(function() { paulo@89: return project; paulo@89: })(); paulo@89: } paulo@89: function d3_geo_projectionMutator(projectAt) { paulo@89: var project, rotate, projectRotate, projectResample = d3_geo_resample(function(x, y) { paulo@89: x = project(x, y); paulo@89: return [ x[0] * k + δx, δy - x[1] * k ]; paulo@89: }), k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx, δy, preclip = d3_geo_clipAntimeridian, postclip = d3_identity, clipAngle = null, clipExtent = null, stream; paulo@89: function projection(point) { paulo@89: point = projectRotate(point[0] * d3_radians, point[1] * d3_radians); paulo@89: return [ point[0] * k + δx, δy - point[1] * k ]; paulo@89: } paulo@89: function invert(point) { paulo@89: point = projectRotate.invert((point[0] - δx) / k, (δy - point[1]) / k); paulo@89: return point && [ point[0] * d3_degrees, point[1] * d3_degrees ]; paulo@89: } paulo@89: projection.stream = function(output) { paulo@89: if (stream) stream.valid = false; paulo@89: stream = d3_geo_projectionRadians(preclip(rotate, projectResample(postclip(output)))); paulo@89: stream.valid = true; paulo@89: return stream; paulo@89: }; paulo@89: projection.clipAngle = function(_) { paulo@89: if (!arguments.length) return clipAngle; paulo@89: preclip = _ == null ? (clipAngle = _, d3_geo_clipAntimeridian) : d3_geo_clipCircle((clipAngle = +_) * d3_radians); paulo@89: return invalidate(); paulo@89: }; paulo@89: projection.clipExtent = function(_) { paulo@89: if (!arguments.length) return clipExtent; paulo@89: clipExtent = _; paulo@89: postclip = _ ? d3_geo_clipExtent(_[0][0], _[0][1], _[1][0], _[1][1]) : d3_identity; paulo@89: return invalidate(); paulo@89: }; paulo@89: projection.scale = function(_) { paulo@89: if (!arguments.length) return k; paulo@89: k = +_; paulo@89: return reset(); paulo@89: }; paulo@89: projection.translate = function(_) { paulo@89: if (!arguments.length) return [ x, y ]; paulo@89: x = +_[0]; paulo@89: y = +_[1]; paulo@89: return reset(); paulo@89: }; paulo@89: projection.center = function(_) { paulo@89: if (!arguments.length) return [ λ * d3_degrees, φ * d3_degrees ]; paulo@89: λ = _[0] % 360 * d3_radians; paulo@89: φ = _[1] % 360 * d3_radians; paulo@89: return reset(); paulo@89: }; paulo@89: projection.rotate = function(_) { paulo@89: if (!arguments.length) return [ δλ * d3_degrees, δφ * d3_degrees, δγ * d3_degrees ]; paulo@89: δλ = _[0] % 360 * d3_radians; paulo@89: δφ = _[1] % 360 * d3_radians; paulo@89: δγ = _.length > 2 ? _[2] % 360 * d3_radians : 0; paulo@89: return reset(); paulo@89: }; paulo@89: d3.rebind(projection, projectResample, "precision"); paulo@89: function reset() { paulo@89: projectRotate = d3_geo_compose(rotate = d3_geo_rotation(δλ, δφ, δγ), project); paulo@89: var center = project(λ, φ); paulo@89: δx = x - center[0] * k; paulo@89: δy = y + center[1] * k; paulo@89: return invalidate(); paulo@89: } paulo@89: function invalidate() { paulo@89: if (stream) stream.valid = false, stream = null; paulo@89: return projection; paulo@89: } paulo@89: return function() { paulo@89: project = projectAt.apply(this, arguments); paulo@89: projection.invert = project.invert && invert; paulo@89: return reset(); paulo@89: }; paulo@89: } paulo@89: function d3_geo_projectionRadians(stream) { paulo@89: return d3_geo_transformPoint(stream, function(x, y) { paulo@89: stream.point(x * d3_radians, y * d3_radians); paulo@89: }); paulo@89: } paulo@89: function d3_geo_equirectangular(λ, φ) { paulo@89: return [ λ, φ ]; paulo@89: } paulo@89: (d3.geo.equirectangular = function() { paulo@89: return d3_geo_projection(d3_geo_equirectangular); paulo@89: }).raw = d3_geo_equirectangular.invert = d3_geo_equirectangular; paulo@89: d3.geo.rotation = function(rotate) { paulo@89: rotate = d3_geo_rotation(rotate[0] % 360 * d3_radians, rotate[1] * d3_radians, rotate.length > 2 ? rotate[2] * d3_radians : 0); paulo@89: function forward(coordinates) { paulo@89: coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians); paulo@89: return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; paulo@89: } paulo@89: forward.invert = function(coordinates) { paulo@89: coordinates = rotate.invert(coordinates[0] * d3_radians, coordinates[1] * d3_radians); paulo@89: return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; paulo@89: }; paulo@89: return forward; paulo@89: }; paulo@89: function d3_geo_identityRotation(λ, φ) { paulo@89: return [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; paulo@89: } paulo@89: d3_geo_identityRotation.invert = d3_geo_equirectangular; paulo@89: function d3_geo_rotation(δλ, δφ, δγ) { paulo@89: return δλ ? δφ || δγ ? d3_geo_compose(d3_geo_rotationλ(δλ), d3_geo_rotationφγ(δφ, δγ)) : d3_geo_rotationλ(δλ) : δφ || δγ ? d3_geo_rotationφγ(δφ, δγ) : d3_geo_identityRotation; paulo@89: } paulo@89: function d3_geo_forwardRotationλ(δλ) { paulo@89: return function(λ, φ) { paulo@89: return λ += δλ, [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; paulo@89: }; paulo@89: } paulo@89: function d3_geo_rotationλ(δλ) { paulo@89: var rotation = d3_geo_forwardRotationλ(δλ); paulo@89: rotation.invert = d3_geo_forwardRotationλ(-δλ); paulo@89: return rotation; paulo@89: } paulo@89: function d3_geo_rotationφγ(δφ, δγ) { paulo@89: var cosδφ = Math.cos(δφ), sinδφ = Math.sin(δφ), cosδγ = Math.cos(δγ), sinδγ = Math.sin(δγ); paulo@89: function rotation(λ, φ) { paulo@89: var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδφ + x * sinδφ; paulo@89: return [ Math.atan2(y * cosδγ - k * sinδγ, x * cosδφ - z * sinδφ), d3_asin(k * cosδγ + y * sinδγ) ]; paulo@89: } paulo@89: rotation.invert = function(λ, φ) { paulo@89: var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδγ - y * sinδγ; paulo@89: return [ Math.atan2(y * cosδγ + z * sinδγ, x * cosδφ + k * sinδφ), d3_asin(k * cosδφ - x * sinδφ) ]; paulo@89: }; paulo@89: return rotation; paulo@89: } paulo@89: d3.geo.circle = function() { paulo@89: var origin = [ 0, 0 ], angle, precision = 6, interpolate; paulo@89: function circle() { paulo@89: var center = typeof origin === "function" ? origin.apply(this, arguments) : origin, rotate = d3_geo_rotation(-center[0] * d3_radians, -center[1] * d3_radians, 0).invert, ring = []; paulo@89: interpolate(null, null, 1, { paulo@89: point: function(x, y) { paulo@89: ring.push(x = rotate(x, y)); paulo@89: x[0] *= d3_degrees, x[1] *= d3_degrees; paulo@89: } paulo@89: }); paulo@89: return { paulo@89: type: "Polygon", paulo@89: coordinates: [ ring ] paulo@89: }; paulo@89: } paulo@89: circle.origin = function(x) { paulo@89: if (!arguments.length) return origin; paulo@89: origin = x; paulo@89: return circle; paulo@89: }; paulo@89: circle.angle = function(x) { paulo@89: if (!arguments.length) return angle; paulo@89: interpolate = d3_geo_circleInterpolate((angle = +x) * d3_radians, precision * d3_radians); paulo@89: return circle; paulo@89: }; paulo@89: circle.precision = function(_) { paulo@89: if (!arguments.length) return precision; paulo@89: interpolate = d3_geo_circleInterpolate(angle * d3_radians, (precision = +_) * d3_radians); paulo@89: return circle; paulo@89: }; paulo@89: return circle.angle(90); paulo@89: }; paulo@89: function d3_geo_circleInterpolate(radius, precision) { paulo@89: var cr = Math.cos(radius), sr = Math.sin(radius); paulo@89: return function(from, to, direction, listener) { paulo@89: var step = direction * precision; paulo@89: if (from != null) { paulo@89: from = d3_geo_circleAngle(cr, from); paulo@89: to = d3_geo_circleAngle(cr, to); paulo@89: if (direction > 0 ? from < to : from > to) from += direction * τ; paulo@89: } else { paulo@89: from = radius + direction * τ; paulo@89: to = radius - .5 * step; paulo@89: } paulo@89: for (var point, t = from; direction > 0 ? t > to : t < to; t -= step) { paulo@89: listener.point((point = d3_geo_spherical([ cr, -sr * Math.cos(t), -sr * Math.sin(t) ]))[0], point[1]); paulo@89: } paulo@89: }; paulo@89: } paulo@89: function d3_geo_circleAngle(cr, point) { paulo@89: var a = d3_geo_cartesian(point); paulo@89: a[0] -= cr; paulo@89: d3_geo_cartesianNormalize(a); paulo@89: var angle = d3_acos(-a[1]); paulo@89: return ((-a[2] < 0 ? -angle : angle) + 2 * Math.PI - ε) % (2 * Math.PI); paulo@89: } paulo@89: d3.geo.distance = function(a, b) { paulo@89: var Δλ = (b[0] - a[0]) * d3_radians, φ0 = a[1] * d3_radians, φ1 = b[1] * d3_radians, sinΔλ = Math.sin(Δλ), cosΔλ = Math.cos(Δλ), sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), sinφ1 = Math.sin(φ1), cosφ1 = Math.cos(φ1), t; paulo@89: return Math.atan2(Math.sqrt((t = cosφ1 * sinΔλ) * t + (t = cosφ0 * sinφ1 - sinφ0 * cosφ1 * cosΔλ) * t), sinφ0 * sinφ1 + cosφ0 * cosφ1 * cosΔλ); paulo@89: }; paulo@89: d3.geo.graticule = function() { paulo@89: var x1, x0, X1, X0, y1, y0, Y1, Y0, dx = 10, dy = dx, DX = 90, DY = 360, x, y, X, Y, precision = 2.5; paulo@89: function graticule() { paulo@89: return { paulo@89: type: "MultiLineString", paulo@89: coordinates: lines() paulo@89: }; paulo@89: } paulo@89: function lines() { paulo@89: return d3.range(Math.ceil(X0 / DX) * DX, X1, DX).map(X).concat(d3.range(Math.ceil(Y0 / DY) * DY, Y1, DY).map(Y)).concat(d3.range(Math.ceil(x0 / dx) * dx, x1, dx).filter(function(x) { paulo@89: return abs(x % DX) > ε; paulo@89: }).map(x)).concat(d3.range(Math.ceil(y0 / dy) * dy, y1, dy).filter(function(y) { paulo@89: return abs(y % DY) > ε; paulo@89: }).map(y)); paulo@89: } paulo@89: graticule.lines = function() { paulo@89: return lines().map(function(coordinates) { paulo@89: return { paulo@89: type: "LineString", paulo@89: coordinates: coordinates paulo@89: }; paulo@89: }); paulo@89: }; paulo@89: graticule.outline = function() { paulo@89: return { paulo@89: type: "Polygon", paulo@89: coordinates: [ X(X0).concat(Y(Y1).slice(1), X(X1).reverse().slice(1), Y(Y0).reverse().slice(1)) ] paulo@89: }; paulo@89: }; paulo@89: graticule.extent = function(_) { paulo@89: if (!arguments.length) return graticule.minorExtent(); paulo@89: return graticule.majorExtent(_).minorExtent(_); paulo@89: }; paulo@89: graticule.majorExtent = function(_) { paulo@89: if (!arguments.length) return [ [ X0, Y0 ], [ X1, Y1 ] ]; paulo@89: X0 = +_[0][0], X1 = +_[1][0]; paulo@89: Y0 = +_[0][1], Y1 = +_[1][1]; paulo@89: if (X0 > X1) _ = X0, X0 = X1, X1 = _; paulo@89: if (Y0 > Y1) _ = Y0, Y0 = Y1, Y1 = _; paulo@89: return graticule.precision(precision); paulo@89: }; paulo@89: graticule.minorExtent = function(_) { paulo@89: if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; paulo@89: x0 = +_[0][0], x1 = +_[1][0]; paulo@89: y0 = +_[0][1], y1 = +_[1][1]; paulo@89: if (x0 > x1) _ = x0, x0 = x1, x1 = _; paulo@89: if (y0 > y1) _ = y0, y0 = y1, y1 = _; paulo@89: return graticule.precision(precision); paulo@89: }; paulo@89: graticule.step = function(_) { paulo@89: if (!arguments.length) return graticule.minorStep(); paulo@89: return graticule.majorStep(_).minorStep(_); paulo@89: }; paulo@89: graticule.majorStep = function(_) { paulo@89: if (!arguments.length) return [ DX, DY ]; paulo@89: DX = +_[0], DY = +_[1]; paulo@89: return graticule; paulo@89: }; paulo@89: graticule.minorStep = function(_) { paulo@89: if (!arguments.length) return [ dx, dy ]; paulo@89: dx = +_[0], dy = +_[1]; paulo@89: return graticule; paulo@89: }; paulo@89: graticule.precision = function(_) { paulo@89: if (!arguments.length) return precision; paulo@89: precision = +_; paulo@89: x = d3_geo_graticuleX(y0, y1, 90); paulo@89: y = d3_geo_graticuleY(x0, x1, precision); paulo@89: X = d3_geo_graticuleX(Y0, Y1, 90); paulo@89: Y = d3_geo_graticuleY(X0, X1, precision); paulo@89: return graticule; paulo@89: }; paulo@89: return graticule.majorExtent([ [ -180, -90 + ε ], [ 180, 90 - ε ] ]).minorExtent([ [ -180, -80 - ε ], [ 180, 80 + ε ] ]); paulo@89: }; paulo@89: function d3_geo_graticuleX(y0, y1, dy) { paulo@89: var y = d3.range(y0, y1 - ε, dy).concat(y1); paulo@89: return function(x) { paulo@89: return y.map(function(y) { paulo@89: return [ x, y ]; paulo@89: }); paulo@89: }; paulo@89: } paulo@89: function d3_geo_graticuleY(x0, x1, dx) { paulo@89: var x = d3.range(x0, x1 - ε, dx).concat(x1); paulo@89: return function(y) { paulo@89: return x.map(function(x) { paulo@89: return [ x, y ]; paulo@89: }); paulo@89: }; paulo@89: } paulo@89: function d3_source(d) { paulo@89: return d.source; paulo@89: } paulo@89: function d3_target(d) { paulo@89: return d.target; paulo@89: } paulo@89: d3.geo.greatArc = function() { paulo@89: var source = d3_source, source_, target = d3_target, target_; paulo@89: function greatArc() { paulo@89: return { paulo@89: type: "LineString", paulo@89: coordinates: [ source_ || source.apply(this, arguments), target_ || target.apply(this, arguments) ] paulo@89: }; paulo@89: } paulo@89: greatArc.distance = function() { paulo@89: return d3.geo.distance(source_ || source.apply(this, arguments), target_ || target.apply(this, arguments)); paulo@89: }; paulo@89: greatArc.source = function(_) { paulo@89: if (!arguments.length) return source; paulo@89: source = _, source_ = typeof _ === "function" ? null : _; paulo@89: return greatArc; paulo@89: }; paulo@89: greatArc.target = function(_) { paulo@89: if (!arguments.length) return target; paulo@89: target = _, target_ = typeof _ === "function" ? null : _; paulo@89: return greatArc; paulo@89: }; paulo@89: greatArc.precision = function() { paulo@89: return arguments.length ? greatArc : 0; paulo@89: }; paulo@89: return greatArc; paulo@89: }; paulo@89: d3.geo.interpolate = function(source, target) { paulo@89: return d3_geo_interpolate(source[0] * d3_radians, source[1] * d3_radians, target[0] * d3_radians, target[1] * d3_radians); paulo@89: }; paulo@89: function d3_geo_interpolate(x0, y0, x1, y1) { paulo@89: var cy0 = Math.cos(y0), sy0 = Math.sin(y0), cy1 = Math.cos(y1), sy1 = Math.sin(y1), kx0 = cy0 * Math.cos(x0), ky0 = cy0 * Math.sin(x0), kx1 = cy1 * Math.cos(x1), ky1 = cy1 * Math.sin(x1), d = 2 * Math.asin(Math.sqrt(d3_haversin(y1 - y0) + cy0 * cy1 * d3_haversin(x1 - x0))), k = 1 / Math.sin(d); paulo@89: var interpolate = d ? function(t) { paulo@89: var B = Math.sin(t *= d) * k, A = Math.sin(d - t) * k, x = A * kx0 + B * kx1, y = A * ky0 + B * ky1, z = A * sy0 + B * sy1; paulo@89: return [ Math.atan2(y, x) * d3_degrees, Math.atan2(z, Math.sqrt(x * x + y * y)) * d3_degrees ]; paulo@89: } : function() { paulo@89: return [ x0 * d3_degrees, y0 * d3_degrees ]; paulo@89: }; paulo@89: interpolate.distance = d; paulo@89: return interpolate; paulo@89: } paulo@89: d3.geo.length = function(object) { paulo@89: d3_geo_lengthSum = 0; paulo@89: d3.geo.stream(object, d3_geo_length); paulo@89: return d3_geo_lengthSum; paulo@89: }; paulo@89: var d3_geo_lengthSum; paulo@89: var d3_geo_length = { paulo@89: sphere: d3_noop, paulo@89: point: d3_noop, paulo@89: lineStart: d3_geo_lengthLineStart, paulo@89: lineEnd: d3_noop, paulo@89: polygonStart: d3_noop, paulo@89: polygonEnd: d3_noop paulo@89: }; paulo@89: function d3_geo_lengthLineStart() { paulo@89: var λ0, sinφ0, cosφ0; paulo@89: d3_geo_length.point = function(λ, φ) { paulo@89: λ0 = λ * d3_radians, sinφ0 = Math.sin(φ *= d3_radians), cosφ0 = Math.cos(φ); paulo@89: d3_geo_length.point = nextPoint; paulo@89: }; paulo@89: d3_geo_length.lineEnd = function() { paulo@89: d3_geo_length.point = d3_geo_length.lineEnd = d3_noop; paulo@89: }; paulo@89: function nextPoint(λ, φ) { paulo@89: var sinφ = Math.sin(φ *= d3_radians), cosφ = Math.cos(φ), t = abs((λ *= d3_radians) - λ0), cosΔλ = Math.cos(t); paulo@89: d3_geo_lengthSum += Math.atan2(Math.sqrt((t = cosφ * Math.sin(t)) * t + (t = cosφ0 * sinφ - sinφ0 * cosφ * cosΔλ) * t), sinφ0 * sinφ + cosφ0 * cosφ * cosΔλ); paulo@89: λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ; paulo@89: } paulo@89: } paulo@89: function d3_geo_azimuthal(scale, angle) { paulo@89: function azimuthal(λ, φ) { paulo@89: var cosλ = Math.cos(λ), cosφ = Math.cos(φ), k = scale(cosλ * cosφ); paulo@89: return [ k * cosφ * Math.sin(λ), k * Math.sin(φ) ]; paulo@89: } paulo@89: azimuthal.invert = function(x, y) { paulo@89: var ρ = Math.sqrt(x * x + y * y), c = angle(ρ), sinc = Math.sin(c), cosc = Math.cos(c); paulo@89: return [ Math.atan2(x * sinc, ρ * cosc), Math.asin(ρ && y * sinc / ρ) ]; paulo@89: }; paulo@89: return azimuthal; paulo@89: } paulo@89: var d3_geo_azimuthalEqualArea = d3_geo_azimuthal(function(cosλcosφ) { paulo@89: return Math.sqrt(2 / (1 + cosλcosφ)); paulo@89: }, function(ρ) { paulo@89: return 2 * Math.asin(ρ / 2); paulo@89: }); paulo@89: (d3.geo.azimuthalEqualArea = function() { paulo@89: return d3_geo_projection(d3_geo_azimuthalEqualArea); paulo@89: }).raw = d3_geo_azimuthalEqualArea; paulo@89: var d3_geo_azimuthalEquidistant = d3_geo_azimuthal(function(cosλcosφ) { paulo@89: var c = Math.acos(cosλcosφ); paulo@89: return c && c / Math.sin(c); paulo@89: }, d3_identity); paulo@89: (d3.geo.azimuthalEquidistant = function() { paulo@89: return d3_geo_projection(d3_geo_azimuthalEquidistant); paulo@89: }).raw = d3_geo_azimuthalEquidistant; paulo@89: function d3_geo_conicConformal(φ0, φ1) { paulo@89: var cosφ0 = Math.cos(φ0), t = function(φ) { paulo@89: return Math.tan(π / 4 + φ / 2); paulo@89: }, n = φ0 === φ1 ? Math.sin(φ0) : Math.log(cosφ0 / Math.cos(φ1)) / Math.log(t(φ1) / t(φ0)), F = cosφ0 * Math.pow(t(φ0), n) / n; paulo@89: if (!n) return d3_geo_mercator; paulo@89: function forward(λ, φ) { paulo@89: if (F > 0) { paulo@89: if (φ < -halfπ + ε) φ = -halfπ + ε; paulo@89: } else { paulo@89: if (φ > halfπ - ε) φ = halfπ - ε; paulo@89: } paulo@89: var ρ = F / Math.pow(t(φ), n); paulo@89: return [ ρ * Math.sin(n * λ), F - ρ * Math.cos(n * λ) ]; paulo@89: } paulo@89: forward.invert = function(x, y) { paulo@89: var ρ0_y = F - y, ρ = d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y); paulo@89: return [ Math.atan2(x, ρ0_y) / n, 2 * Math.atan(Math.pow(F / ρ, 1 / n)) - halfπ ]; paulo@89: }; paulo@89: return forward; paulo@89: } paulo@89: (d3.geo.conicConformal = function() { paulo@89: return d3_geo_conic(d3_geo_conicConformal); paulo@89: }).raw = d3_geo_conicConformal; paulo@89: function d3_geo_conicEquidistant(φ0, φ1) { paulo@89: var cosφ0 = Math.cos(φ0), n = φ0 === φ1 ? Math.sin(φ0) : (cosφ0 - Math.cos(φ1)) / (φ1 - φ0), G = cosφ0 / n + φ0; paulo@89: if (abs(n) < ε) return d3_geo_equirectangular; paulo@89: function forward(λ, φ) { paulo@89: var ρ = G - φ; paulo@89: return [ ρ * Math.sin(n * λ), G - ρ * Math.cos(n * λ) ]; paulo@89: } paulo@89: forward.invert = function(x, y) { paulo@89: var ρ0_y = G - y; paulo@89: return [ Math.atan2(x, ρ0_y) / n, G - d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y) ]; paulo@89: }; paulo@89: return forward; paulo@89: } paulo@89: (d3.geo.conicEquidistant = function() { paulo@89: return d3_geo_conic(d3_geo_conicEquidistant); paulo@89: }).raw = d3_geo_conicEquidistant; paulo@89: var d3_geo_gnomonic = d3_geo_azimuthal(function(cosλcosφ) { paulo@89: return 1 / cosλcosφ; paulo@89: }, Math.atan); paulo@89: (d3.geo.gnomonic = function() { paulo@89: return d3_geo_projection(d3_geo_gnomonic); paulo@89: }).raw = d3_geo_gnomonic; paulo@89: function d3_geo_mercator(λ, φ) { paulo@89: return [ λ, Math.log(Math.tan(π / 4 + φ / 2)) ]; paulo@89: } paulo@89: d3_geo_mercator.invert = function(x, y) { paulo@89: return [ x, 2 * Math.atan(Math.exp(y)) - halfπ ]; paulo@89: }; paulo@89: function d3_geo_mercatorProjection(project) { paulo@89: var m = d3_geo_projection(project), scale = m.scale, translate = m.translate, clipExtent = m.clipExtent, clipAuto; paulo@89: m.scale = function() { paulo@89: var v = scale.apply(m, arguments); paulo@89: return v === m ? clipAuto ? m.clipExtent(null) : m : v; paulo@89: }; paulo@89: m.translate = function() { paulo@89: var v = translate.apply(m, arguments); paulo@89: return v === m ? clipAuto ? m.clipExtent(null) : m : v; paulo@89: }; paulo@89: m.clipExtent = function(_) { paulo@89: var v = clipExtent.apply(m, arguments); paulo@89: if (v === m) { paulo@89: if (clipAuto = _ == null) { paulo@89: var k = π * scale(), t = translate(); paulo@89: clipExtent([ [ t[0] - k, t[1] - k ], [ t[0] + k, t[1] + k ] ]); paulo@89: } paulo@89: } else if (clipAuto) { paulo@89: v = null; paulo@89: } paulo@89: return v; paulo@89: }; paulo@89: return m.clipExtent(null); paulo@89: } paulo@89: (d3.geo.mercator = function() { paulo@89: return d3_geo_mercatorProjection(d3_geo_mercator); paulo@89: }).raw = d3_geo_mercator; paulo@89: var d3_geo_orthographic = d3_geo_azimuthal(function() { paulo@89: return 1; paulo@89: }, Math.asin); paulo@89: (d3.geo.orthographic = function() { paulo@89: return d3_geo_projection(d3_geo_orthographic); paulo@89: }).raw = d3_geo_orthographic; paulo@89: var d3_geo_stereographic = d3_geo_azimuthal(function(cosλcosφ) { paulo@89: return 1 / (1 + cosλcosφ); paulo@89: }, function(ρ) { paulo@89: return 2 * Math.atan(ρ); paulo@89: }); paulo@89: (d3.geo.stereographic = function() { paulo@89: return d3_geo_projection(d3_geo_stereographic); paulo@89: }).raw = d3_geo_stereographic; paulo@89: function d3_geo_transverseMercator(λ, φ) { paulo@89: return [ Math.log(Math.tan(π / 4 + φ / 2)), -λ ]; paulo@89: } paulo@89: d3_geo_transverseMercator.invert = function(x, y) { paulo@89: return [ -y, 2 * Math.atan(Math.exp(x)) - halfπ ]; paulo@89: }; paulo@89: (d3.geo.transverseMercator = function() { paulo@89: var projection = d3_geo_mercatorProjection(d3_geo_transverseMercator), center = projection.center, rotate = projection.rotate; paulo@89: projection.center = function(_) { paulo@89: return _ ? center([ -_[1], _[0] ]) : (_ = center(), [ _[1], -_[0] ]); paulo@89: }; paulo@89: projection.rotate = function(_) { paulo@89: return _ ? rotate([ _[0], _[1], _.length > 2 ? _[2] + 90 : 90 ]) : (_ = rotate(), paulo@89: [ _[0], _[1], _[2] - 90 ]); paulo@89: }; paulo@89: return rotate([ 0, 0, 90 ]); paulo@89: }).raw = d3_geo_transverseMercator; paulo@89: d3.geom = {}; paulo@89: function d3_geom_pointX(d) { paulo@89: return d[0]; paulo@89: } paulo@89: function d3_geom_pointY(d) { paulo@89: return d[1]; paulo@89: } paulo@89: d3.geom.hull = function(vertices) { paulo@89: var x = d3_geom_pointX, y = d3_geom_pointY; paulo@89: if (arguments.length) return hull(vertices); paulo@89: function hull(data) { paulo@89: if (data.length < 3) return []; paulo@89: var fx = d3_functor(x), fy = d3_functor(y), i, n = data.length, points = [], flippedPoints = []; paulo@89: for (i = 0; i < n; i++) { paulo@89: points.push([ +fx.call(this, data[i], i), +fy.call(this, data[i], i), i ]); paulo@89: } paulo@89: points.sort(d3_geom_hullOrder); paulo@89: for (i = 0; i < n; i++) flippedPoints.push([ points[i][0], -points[i][1] ]); paulo@89: var upper = d3_geom_hullUpper(points), lower = d3_geom_hullUpper(flippedPoints); paulo@89: var skipLeft = lower[0] === upper[0], skipRight = lower[lower.length - 1] === upper[upper.length - 1], polygon = []; paulo@89: for (i = upper.length - 1; i >= 0; --i) polygon.push(data[points[upper[i]][2]]); paulo@89: for (i = +skipLeft; i < lower.length - skipRight; ++i) polygon.push(data[points[lower[i]][2]]); paulo@89: return polygon; paulo@89: } paulo@89: hull.x = function(_) { paulo@89: return arguments.length ? (x = _, hull) : x; paulo@89: }; paulo@89: hull.y = function(_) { paulo@89: return arguments.length ? (y = _, hull) : y; paulo@89: }; paulo@89: return hull; paulo@89: }; paulo@89: function d3_geom_hullUpper(points) { paulo@89: var n = points.length, hull = [ 0, 1 ], hs = 2; paulo@89: for (var i = 2; i < n; i++) { paulo@89: while (hs > 1 && d3_cross2d(points[hull[hs - 2]], points[hull[hs - 1]], points[i]) <= 0) --hs; paulo@89: hull[hs++] = i; paulo@89: } paulo@89: return hull.slice(0, hs); paulo@89: } paulo@89: function d3_geom_hullOrder(a, b) { paulo@89: return a[0] - b[0] || a[1] - b[1]; paulo@89: } paulo@89: d3.geom.polygon = function(coordinates) { paulo@89: d3_subclass(coordinates, d3_geom_polygonPrototype); paulo@89: return coordinates; paulo@89: }; paulo@89: var d3_geom_polygonPrototype = d3.geom.polygon.prototype = []; paulo@89: d3_geom_polygonPrototype.area = function() { paulo@89: var i = -1, n = this.length, a, b = this[n - 1], area = 0; paulo@89: while (++i < n) { paulo@89: a = b; paulo@89: b = this[i]; paulo@89: area += a[1] * b[0] - a[0] * b[1]; paulo@89: } paulo@89: return area * .5; paulo@89: }; paulo@89: d3_geom_polygonPrototype.centroid = function(k) { paulo@89: var i = -1, n = this.length, x = 0, y = 0, a, b = this[n - 1], c; paulo@89: if (!arguments.length) k = -1 / (6 * this.area()); paulo@89: while (++i < n) { paulo@89: a = b; paulo@89: b = this[i]; paulo@89: c = a[0] * b[1] - b[0] * a[1]; paulo@89: x += (a[0] + b[0]) * c; paulo@89: y += (a[1] + b[1]) * c; paulo@89: } paulo@89: return [ x * k, y * k ]; paulo@89: }; paulo@89: d3_geom_polygonPrototype.clip = function(subject) { paulo@89: var input, closed = d3_geom_polygonClosed(subject), i = -1, n = this.length - d3_geom_polygonClosed(this), j, m, a = this[n - 1], b, c, d; paulo@89: while (++i < n) { paulo@89: input = subject.slice(); paulo@89: subject.length = 0; paulo@89: b = this[i]; paulo@89: c = input[(m = input.length - closed) - 1]; paulo@89: j = -1; paulo@89: while (++j < m) { paulo@89: d = input[j]; paulo@89: if (d3_geom_polygonInside(d, a, b)) { paulo@89: if (!d3_geom_polygonInside(c, a, b)) { paulo@89: subject.push(d3_geom_polygonIntersect(c, d, a, b)); paulo@89: } paulo@89: subject.push(d); paulo@89: } else if (d3_geom_polygonInside(c, a, b)) { paulo@89: subject.push(d3_geom_polygonIntersect(c, d, a, b)); paulo@89: } paulo@89: c = d; paulo@89: } paulo@89: if (closed) subject.push(subject[0]); paulo@89: a = b; paulo@89: } paulo@89: return subject; paulo@89: }; paulo@89: function d3_geom_polygonInside(p, a, b) { paulo@89: return (b[0] - a[0]) * (p[1] - a[1]) < (b[1] - a[1]) * (p[0] - a[0]); paulo@89: } paulo@89: function d3_geom_polygonIntersect(c, d, a, b) { paulo@89: var x1 = c[0], x3 = a[0], x21 = d[0] - x1, x43 = b[0] - x3, y1 = c[1], y3 = a[1], y21 = d[1] - y1, y43 = b[1] - y3, ua = (x43 * (y1 - y3) - y43 * (x1 - x3)) / (y43 * x21 - x43 * y21); paulo@89: return [ x1 + ua * x21, y1 + ua * y21 ]; paulo@89: } paulo@89: function d3_geom_polygonClosed(coordinates) { paulo@89: var a = coordinates[0], b = coordinates[coordinates.length - 1]; paulo@89: return !(a[0] - b[0] || a[1] - b[1]); paulo@89: } paulo@89: var d3_geom_voronoiEdges, d3_geom_voronoiCells, d3_geom_voronoiBeaches, d3_geom_voronoiBeachPool = [], d3_geom_voronoiFirstCircle, d3_geom_voronoiCircles, d3_geom_voronoiCirclePool = []; paulo@89: function d3_geom_voronoiBeach() { paulo@89: d3_geom_voronoiRedBlackNode(this); paulo@89: this.edge = this.site = this.circle = null; paulo@89: } paulo@89: function d3_geom_voronoiCreateBeach(site) { paulo@89: var beach = d3_geom_voronoiBeachPool.pop() || new d3_geom_voronoiBeach(); paulo@89: beach.site = site; paulo@89: return beach; paulo@89: } paulo@89: function d3_geom_voronoiDetachBeach(beach) { paulo@89: d3_geom_voronoiDetachCircle(beach); paulo@89: d3_geom_voronoiBeaches.remove(beach); paulo@89: d3_geom_voronoiBeachPool.push(beach); paulo@89: d3_geom_voronoiRedBlackNode(beach); paulo@89: } paulo@89: function d3_geom_voronoiRemoveBeach(beach) { paulo@89: var circle = beach.circle, x = circle.x, y = circle.cy, vertex = { paulo@89: x: x, paulo@89: y: y paulo@89: }, previous = beach.P, next = beach.N, disappearing = [ beach ]; paulo@89: d3_geom_voronoiDetachBeach(beach); paulo@89: var lArc = previous; paulo@89: while (lArc.circle && abs(x - lArc.circle.x) < ε && abs(y - lArc.circle.cy) < ε) { paulo@89: previous = lArc.P; paulo@89: disappearing.unshift(lArc); paulo@89: d3_geom_voronoiDetachBeach(lArc); paulo@89: lArc = previous; paulo@89: } paulo@89: disappearing.unshift(lArc); paulo@89: d3_geom_voronoiDetachCircle(lArc); paulo@89: var rArc = next; paulo@89: while (rArc.circle && abs(x - rArc.circle.x) < ε && abs(y - rArc.circle.cy) < ε) { paulo@89: next = rArc.N; paulo@89: disappearing.push(rArc); paulo@89: d3_geom_voronoiDetachBeach(rArc); paulo@89: rArc = next; paulo@89: } paulo@89: disappearing.push(rArc); paulo@89: d3_geom_voronoiDetachCircle(rArc); paulo@89: var nArcs = disappearing.length, iArc; paulo@89: for (iArc = 1; iArc < nArcs; ++iArc) { paulo@89: rArc = disappearing[iArc]; paulo@89: lArc = disappearing[iArc - 1]; paulo@89: d3_geom_voronoiSetEdgeEnd(rArc.edge, lArc.site, rArc.site, vertex); paulo@89: } paulo@89: lArc = disappearing[0]; paulo@89: rArc = disappearing[nArcs - 1]; paulo@89: rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, rArc.site, null, vertex); paulo@89: d3_geom_voronoiAttachCircle(lArc); paulo@89: d3_geom_voronoiAttachCircle(rArc); paulo@89: } paulo@89: function d3_geom_voronoiAddBeach(site) { paulo@89: var x = site.x, directrix = site.y, lArc, rArc, dxl, dxr, node = d3_geom_voronoiBeaches._; paulo@89: while (node) { paulo@89: dxl = d3_geom_voronoiLeftBreakPoint(node, directrix) - x; paulo@89: if (dxl > ε) node = node.L; else { paulo@89: dxr = x - d3_geom_voronoiRightBreakPoint(node, directrix); paulo@89: if (dxr > ε) { paulo@89: if (!node.R) { paulo@89: lArc = node; paulo@89: break; paulo@89: } paulo@89: node = node.R; paulo@89: } else { paulo@89: if (dxl > -ε) { paulo@89: lArc = node.P; paulo@89: rArc = node; paulo@89: } else if (dxr > -ε) { paulo@89: lArc = node; paulo@89: rArc = node.N; paulo@89: } else { paulo@89: lArc = rArc = node; paulo@89: } paulo@89: break; paulo@89: } paulo@89: } paulo@89: } paulo@89: var newArc = d3_geom_voronoiCreateBeach(site); paulo@89: d3_geom_voronoiBeaches.insert(lArc, newArc); paulo@89: if (!lArc && !rArc) return; paulo@89: if (lArc === rArc) { paulo@89: d3_geom_voronoiDetachCircle(lArc); paulo@89: rArc = d3_geom_voronoiCreateBeach(lArc.site); paulo@89: d3_geom_voronoiBeaches.insert(newArc, rArc); paulo@89: newArc.edge = rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); paulo@89: d3_geom_voronoiAttachCircle(lArc); paulo@89: d3_geom_voronoiAttachCircle(rArc); paulo@89: return; paulo@89: } paulo@89: if (!rArc) { paulo@89: newArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); paulo@89: return; paulo@89: } paulo@89: d3_geom_voronoiDetachCircle(lArc); paulo@89: d3_geom_voronoiDetachCircle(rArc); paulo@89: var lSite = lArc.site, ax = lSite.x, ay = lSite.y, bx = site.x - ax, by = site.y - ay, rSite = rArc.site, cx = rSite.x - ax, cy = rSite.y - ay, d = 2 * (bx * cy - by * cx), hb = bx * bx + by * by, hc = cx * cx + cy * cy, vertex = { paulo@89: x: (cy * hb - by * hc) / d + ax, paulo@89: y: (bx * hc - cx * hb) / d + ay paulo@89: }; paulo@89: d3_geom_voronoiSetEdgeEnd(rArc.edge, lSite, rSite, vertex); paulo@89: newArc.edge = d3_geom_voronoiCreateEdge(lSite, site, null, vertex); paulo@89: rArc.edge = d3_geom_voronoiCreateEdge(site, rSite, null, vertex); paulo@89: d3_geom_voronoiAttachCircle(lArc); paulo@89: d3_geom_voronoiAttachCircle(rArc); paulo@89: } paulo@89: function d3_geom_voronoiLeftBreakPoint(arc, directrix) { paulo@89: var site = arc.site, rfocx = site.x, rfocy = site.y, pby2 = rfocy - directrix; paulo@89: if (!pby2) return rfocx; paulo@89: var lArc = arc.P; paulo@89: if (!lArc) return -Infinity; paulo@89: site = lArc.site; paulo@89: var lfocx = site.x, lfocy = site.y, plby2 = lfocy - directrix; paulo@89: if (!plby2) return lfocx; paulo@89: var hl = lfocx - rfocx, aby2 = 1 / pby2 - 1 / plby2, b = hl / plby2; paulo@89: if (aby2) return (-b + Math.sqrt(b * b - 2 * aby2 * (hl * hl / (-2 * plby2) - lfocy + plby2 / 2 + rfocy - pby2 / 2))) / aby2 + rfocx; paulo@89: return (rfocx + lfocx) / 2; paulo@89: } paulo@89: function d3_geom_voronoiRightBreakPoint(arc, directrix) { paulo@89: var rArc = arc.N; paulo@89: if (rArc) return d3_geom_voronoiLeftBreakPoint(rArc, directrix); paulo@89: var site = arc.site; paulo@89: return site.y === directrix ? site.x : Infinity; paulo@89: } paulo@89: function d3_geom_voronoiCell(site) { paulo@89: this.site = site; paulo@89: this.edges = []; paulo@89: } paulo@89: d3_geom_voronoiCell.prototype.prepare = function() { paulo@89: var halfEdges = this.edges, iHalfEdge = halfEdges.length, edge; paulo@89: while (iHalfEdge--) { paulo@89: edge = halfEdges[iHalfEdge].edge; paulo@89: if (!edge.b || !edge.a) halfEdges.splice(iHalfEdge, 1); paulo@89: } paulo@89: halfEdges.sort(d3_geom_voronoiHalfEdgeOrder); paulo@89: return halfEdges.length; paulo@89: }; paulo@89: function d3_geom_voronoiCloseCells(extent) { paulo@89: var x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], x2, y2, x3, y3, cells = d3_geom_voronoiCells, iCell = cells.length, cell, iHalfEdge, halfEdges, nHalfEdges, start, end; paulo@89: while (iCell--) { paulo@89: cell = cells[iCell]; paulo@89: if (!cell || !cell.prepare()) continue; paulo@89: halfEdges = cell.edges; paulo@89: nHalfEdges = halfEdges.length; paulo@89: iHalfEdge = 0; paulo@89: while (iHalfEdge < nHalfEdges) { paulo@89: end = halfEdges[iHalfEdge].end(), x3 = end.x, y3 = end.y; paulo@89: start = halfEdges[++iHalfEdge % nHalfEdges].start(), x2 = start.x, y2 = start.y; paulo@89: if (abs(x3 - x2) > ε || abs(y3 - y2) > ε) { paulo@89: halfEdges.splice(iHalfEdge, 0, new d3_geom_voronoiHalfEdge(d3_geom_voronoiCreateBorderEdge(cell.site, end, abs(x3 - x0) < ε && y1 - y3 > ε ? { paulo@89: x: x0, paulo@89: y: abs(x2 - x0) < ε ? y2 : y1 paulo@89: } : abs(y3 - y1) < ε && x1 - x3 > ε ? { paulo@89: x: abs(y2 - y1) < ε ? x2 : x1, paulo@89: y: y1 paulo@89: } : abs(x3 - x1) < ε && y3 - y0 > ε ? { paulo@89: x: x1, paulo@89: y: abs(x2 - x1) < ε ? y2 : y0 paulo@89: } : abs(y3 - y0) < ε && x3 - x0 > ε ? { paulo@89: x: abs(y2 - y0) < ε ? x2 : x0, paulo@89: y: y0 paulo@89: } : null), cell.site, null)); paulo@89: ++nHalfEdges; paulo@89: } paulo@89: } paulo@89: } paulo@89: } paulo@89: function d3_geom_voronoiHalfEdgeOrder(a, b) { paulo@89: return b.angle - a.angle; paulo@89: } paulo@89: function d3_geom_voronoiCircle() { paulo@89: d3_geom_voronoiRedBlackNode(this); paulo@89: this.x = this.y = this.arc = this.site = this.cy = null; paulo@89: } paulo@89: function d3_geom_voronoiAttachCircle(arc) { paulo@89: var lArc = arc.P, rArc = arc.N; paulo@89: if (!lArc || !rArc) return; paulo@89: var lSite = lArc.site, cSite = arc.site, rSite = rArc.site; paulo@89: if (lSite === rSite) return; paulo@89: var bx = cSite.x, by = cSite.y, ax = lSite.x - bx, ay = lSite.y - by, cx = rSite.x - bx, cy = rSite.y - by; paulo@89: var d = 2 * (ax * cy - ay * cx); paulo@89: if (d >= -ε2) return; paulo@89: var ha = ax * ax + ay * ay, hc = cx * cx + cy * cy, x = (cy * ha - ay * hc) / d, y = (ax * hc - cx * ha) / d, cy = y + by; paulo@89: var circle = d3_geom_voronoiCirclePool.pop() || new d3_geom_voronoiCircle(); paulo@89: circle.arc = arc; paulo@89: circle.site = cSite; paulo@89: circle.x = x + bx; paulo@89: circle.y = cy + Math.sqrt(x * x + y * y); paulo@89: circle.cy = cy; paulo@89: arc.circle = circle; paulo@89: var before = null, node = d3_geom_voronoiCircles._; paulo@89: while (node) { paulo@89: if (circle.y < node.y || circle.y === node.y && circle.x <= node.x) { paulo@89: if (node.L) node = node.L; else { paulo@89: before = node.P; paulo@89: break; paulo@89: } paulo@89: } else { paulo@89: if (node.R) node = node.R; else { paulo@89: before = node; paulo@89: break; paulo@89: } paulo@89: } paulo@89: } paulo@89: d3_geom_voronoiCircles.insert(before, circle); paulo@89: if (!before) d3_geom_voronoiFirstCircle = circle; paulo@89: } paulo@89: function d3_geom_voronoiDetachCircle(arc) { paulo@89: var circle = arc.circle; paulo@89: if (circle) { paulo@89: if (!circle.P) d3_geom_voronoiFirstCircle = circle.N; paulo@89: d3_geom_voronoiCircles.remove(circle); paulo@89: d3_geom_voronoiCirclePool.push(circle); paulo@89: d3_geom_voronoiRedBlackNode(circle); paulo@89: arc.circle = null; paulo@89: } paulo@89: } paulo@89: function d3_geom_voronoiClipEdges(extent) { paulo@89: var edges = d3_geom_voronoiEdges, clip = d3_geom_clipLine(extent[0][0], extent[0][1], extent[1][0], extent[1][1]), i = edges.length, e; paulo@89: while (i--) { paulo@89: e = edges[i]; paulo@89: if (!d3_geom_voronoiConnectEdge(e, extent) || !clip(e) || abs(e.a.x - e.b.x) < ε && abs(e.a.y - e.b.y) < ε) { paulo@89: e.a = e.b = null; paulo@89: edges.splice(i, 1); paulo@89: } paulo@89: } paulo@89: } paulo@89: function d3_geom_voronoiConnectEdge(edge, extent) { paulo@89: var vb = edge.b; paulo@89: if (vb) return true; paulo@89: var va = edge.a, x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], lSite = edge.l, rSite = edge.r, lx = lSite.x, ly = lSite.y, rx = rSite.x, ry = rSite.y, fx = (lx + rx) / 2, fy = (ly + ry) / 2, fm, fb; paulo@89: if (ry === ly) { paulo@89: if (fx < x0 || fx >= x1) return; paulo@89: if (lx > rx) { paulo@89: if (!va) va = { paulo@89: x: fx, paulo@89: y: y0 paulo@89: }; else if (va.y >= y1) return; paulo@89: vb = { paulo@89: x: fx, paulo@89: y: y1 paulo@89: }; paulo@89: } else { paulo@89: if (!va) va = { paulo@89: x: fx, paulo@89: y: y1 paulo@89: }; else if (va.y < y0) return; paulo@89: vb = { paulo@89: x: fx, paulo@89: y: y0 paulo@89: }; paulo@89: } paulo@89: } else { paulo@89: fm = (lx - rx) / (ry - ly); paulo@89: fb = fy - fm * fx; paulo@89: if (fm < -1 || fm > 1) { paulo@89: if (lx > rx) { paulo@89: if (!va) va = { paulo@89: x: (y0 - fb) / fm, paulo@89: y: y0 paulo@89: }; else if (va.y >= y1) return; paulo@89: vb = { paulo@89: x: (y1 - fb) / fm, paulo@89: y: y1 paulo@89: }; paulo@89: } else { paulo@89: if (!va) va = { paulo@89: x: (y1 - fb) / fm, paulo@89: y: y1 paulo@89: }; else if (va.y < y0) return; paulo@89: vb = { paulo@89: x: (y0 - fb) / fm, paulo@89: y: y0 paulo@89: }; paulo@89: } paulo@89: } else { paulo@89: if (ly < ry) { paulo@89: if (!va) va = { paulo@89: x: x0, paulo@89: y: fm * x0 + fb paulo@89: }; else if (va.x >= x1) return; paulo@89: vb = { paulo@89: x: x1, paulo@89: y: fm * x1 + fb paulo@89: }; paulo@89: } else { paulo@89: if (!va) va = { paulo@89: x: x1, paulo@89: y: fm * x1 + fb paulo@89: }; else if (va.x < x0) return; paulo@89: vb = { paulo@89: x: x0, paulo@89: y: fm * x0 + fb paulo@89: }; paulo@89: } paulo@89: } paulo@89: } paulo@89: edge.a = va; paulo@89: edge.b = vb; paulo@89: return true; paulo@89: } paulo@89: function d3_geom_voronoiEdge(lSite, rSite) { paulo@89: this.l = lSite; paulo@89: this.r = rSite; paulo@89: this.a = this.b = null; paulo@89: } paulo@89: function d3_geom_voronoiCreateEdge(lSite, rSite, va, vb) { paulo@89: var edge = new d3_geom_voronoiEdge(lSite, rSite); paulo@89: d3_geom_voronoiEdges.push(edge); paulo@89: if (va) d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, va); paulo@89: if (vb) d3_geom_voronoiSetEdgeEnd(edge, rSite, lSite, vb); paulo@89: d3_geom_voronoiCells[lSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, lSite, rSite)); paulo@89: d3_geom_voronoiCells[rSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, rSite, lSite)); paulo@89: return edge; paulo@89: } paulo@89: function d3_geom_voronoiCreateBorderEdge(lSite, va, vb) { paulo@89: var edge = new d3_geom_voronoiEdge(lSite, null); paulo@89: edge.a = va; paulo@89: edge.b = vb; paulo@89: d3_geom_voronoiEdges.push(edge); paulo@89: return edge; paulo@89: } paulo@89: function d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, vertex) { paulo@89: if (!edge.a && !edge.b) { paulo@89: edge.a = vertex; paulo@89: edge.l = lSite; paulo@89: edge.r = rSite; paulo@89: } else if (edge.l === rSite) { paulo@89: edge.b = vertex; paulo@89: } else { paulo@89: edge.a = vertex; paulo@89: } paulo@89: } paulo@89: function d3_geom_voronoiHalfEdge(edge, lSite, rSite) { paulo@89: var va = edge.a, vb = edge.b; paulo@89: this.edge = edge; paulo@89: this.site = lSite; paulo@89: this.angle = rSite ? Math.atan2(rSite.y - lSite.y, rSite.x - lSite.x) : edge.l === lSite ? Math.atan2(vb.x - va.x, va.y - vb.y) : Math.atan2(va.x - vb.x, vb.y - va.y); paulo@89: } paulo@89: d3_geom_voronoiHalfEdge.prototype = { paulo@89: start: function() { paulo@89: return this.edge.l === this.site ? this.edge.a : this.edge.b; paulo@89: }, paulo@89: end: function() { paulo@89: return this.edge.l === this.site ? this.edge.b : this.edge.a; paulo@89: } paulo@89: }; paulo@89: function d3_geom_voronoiRedBlackTree() { paulo@89: this._ = null; paulo@89: } paulo@89: function d3_geom_voronoiRedBlackNode(node) { paulo@89: node.U = node.C = node.L = node.R = node.P = node.N = null; paulo@89: } paulo@89: d3_geom_voronoiRedBlackTree.prototype = { paulo@89: insert: function(after, node) { paulo@89: var parent, grandpa, uncle; paulo@89: if (after) { paulo@89: node.P = after; paulo@89: node.N = after.N; paulo@89: if (after.N) after.N.P = node; paulo@89: after.N = node; paulo@89: if (after.R) { paulo@89: after = after.R; paulo@89: while (after.L) after = after.L; paulo@89: after.L = node; paulo@89: } else { paulo@89: after.R = node; paulo@89: } paulo@89: parent = after; paulo@89: } else if (this._) { paulo@89: after = d3_geom_voronoiRedBlackFirst(this._); paulo@89: node.P = null; paulo@89: node.N = after; paulo@89: after.P = after.L = node; paulo@89: parent = after; paulo@89: } else { paulo@89: node.P = node.N = null; paulo@89: this._ = node; paulo@89: parent = null; paulo@89: } paulo@89: node.L = node.R = null; paulo@89: node.U = parent; paulo@89: node.C = true; paulo@89: after = node; paulo@89: while (parent && parent.C) { paulo@89: grandpa = parent.U; paulo@89: if (parent === grandpa.L) { paulo@89: uncle = grandpa.R; paulo@89: if (uncle && uncle.C) { paulo@89: parent.C = uncle.C = false; paulo@89: grandpa.C = true; paulo@89: after = grandpa; paulo@89: } else { paulo@89: if (after === parent.R) { paulo@89: d3_geom_voronoiRedBlackRotateLeft(this, parent); paulo@89: after = parent; paulo@89: parent = after.U; paulo@89: } paulo@89: parent.C = false; paulo@89: grandpa.C = true; paulo@89: d3_geom_voronoiRedBlackRotateRight(this, grandpa); paulo@89: } paulo@89: } else { paulo@89: uncle = grandpa.L; paulo@89: if (uncle && uncle.C) { paulo@89: parent.C = uncle.C = false; paulo@89: grandpa.C = true; paulo@89: after = grandpa; paulo@89: } else { paulo@89: if (after === parent.L) { paulo@89: d3_geom_voronoiRedBlackRotateRight(this, parent); paulo@89: after = parent; paulo@89: parent = after.U; paulo@89: } paulo@89: parent.C = false; paulo@89: grandpa.C = true; paulo@89: d3_geom_voronoiRedBlackRotateLeft(this, grandpa); paulo@89: } paulo@89: } paulo@89: parent = after.U; paulo@89: } paulo@89: this._.C = false; paulo@89: }, paulo@89: remove: function(node) { paulo@89: if (node.N) node.N.P = node.P; paulo@89: if (node.P) node.P.N = node.N; paulo@89: node.N = node.P = null; paulo@89: var parent = node.U, sibling, left = node.L, right = node.R, next, red; paulo@89: if (!left) next = right; else if (!right) next = left; else next = d3_geom_voronoiRedBlackFirst(right); paulo@89: if (parent) { paulo@89: if (parent.L === node) parent.L = next; else parent.R = next; paulo@89: } else { paulo@89: this._ = next; paulo@89: } paulo@89: if (left && right) { paulo@89: red = next.C; paulo@89: next.C = node.C; paulo@89: next.L = left; paulo@89: left.U = next; paulo@89: if (next !== right) { paulo@89: parent = next.U; paulo@89: next.U = node.U; paulo@89: node = next.R; paulo@89: parent.L = node; paulo@89: next.R = right; paulo@89: right.U = next; paulo@89: } else { paulo@89: next.U = parent; paulo@89: parent = next; paulo@89: node = next.R; paulo@89: } paulo@89: } else { paulo@89: red = node.C; paulo@89: node = next; paulo@89: } paulo@89: if (node) node.U = parent; paulo@89: if (red) return; paulo@89: if (node && node.C) { paulo@89: node.C = false; paulo@89: return; paulo@89: } paulo@89: do { paulo@89: if (node === this._) break; paulo@89: if (node === parent.L) { paulo@89: sibling = parent.R; paulo@89: if (sibling.C) { paulo@89: sibling.C = false; paulo@89: parent.C = true; paulo@89: d3_geom_voronoiRedBlackRotateLeft(this, parent); paulo@89: sibling = parent.R; paulo@89: } paulo@89: if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { paulo@89: if (!sibling.R || !sibling.R.C) { paulo@89: sibling.L.C = false; paulo@89: sibling.C = true; paulo@89: d3_geom_voronoiRedBlackRotateRight(this, sibling); paulo@89: sibling = parent.R; paulo@89: } paulo@89: sibling.C = parent.C; paulo@89: parent.C = sibling.R.C = false; paulo@89: d3_geom_voronoiRedBlackRotateLeft(this, parent); paulo@89: node = this._; paulo@89: break; paulo@89: } paulo@89: } else { paulo@89: sibling = parent.L; paulo@89: if (sibling.C) { paulo@89: sibling.C = false; paulo@89: parent.C = true; paulo@89: d3_geom_voronoiRedBlackRotateRight(this, parent); paulo@89: sibling = parent.L; paulo@89: } paulo@89: if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { paulo@89: if (!sibling.L || !sibling.L.C) { paulo@89: sibling.R.C = false; paulo@89: sibling.C = true; paulo@89: d3_geom_voronoiRedBlackRotateLeft(this, sibling); paulo@89: sibling = parent.L; paulo@89: } paulo@89: sibling.C = parent.C; paulo@89: parent.C = sibling.L.C = false; paulo@89: d3_geom_voronoiRedBlackRotateRight(this, parent); paulo@89: node = this._; paulo@89: break; paulo@89: } paulo@89: } paulo@89: sibling.C = true; paulo@89: node = parent; paulo@89: parent = parent.U; paulo@89: } while (!node.C); paulo@89: if (node) node.C = false; paulo@89: } paulo@89: }; paulo@89: function d3_geom_voronoiRedBlackRotateLeft(tree, node) { paulo@89: var p = node, q = node.R, parent = p.U; paulo@89: if (parent) { paulo@89: if (parent.L === p) parent.L = q; else parent.R = q; paulo@89: } else { paulo@89: tree._ = q; paulo@89: } paulo@89: q.U = parent; paulo@89: p.U = q; paulo@89: p.R = q.L; paulo@89: if (p.R) p.R.U = p; paulo@89: q.L = p; paulo@89: } paulo@89: function d3_geom_voronoiRedBlackRotateRight(tree, node) { paulo@89: var p = node, q = node.L, parent = p.U; paulo@89: if (parent) { paulo@89: if (parent.L === p) parent.L = q; else parent.R = q; paulo@89: } else { paulo@89: tree._ = q; paulo@89: } paulo@89: q.U = parent; paulo@89: p.U = q; paulo@89: p.L = q.R; paulo@89: if (p.L) p.L.U = p; paulo@89: q.R = p; paulo@89: } paulo@89: function d3_geom_voronoiRedBlackFirst(node) { paulo@89: while (node.L) node = node.L; paulo@89: return node; paulo@89: } paulo@89: function d3_geom_voronoi(sites, bbox) { paulo@89: var site = sites.sort(d3_geom_voronoiVertexOrder).pop(), x0, y0, circle; paulo@89: d3_geom_voronoiEdges = []; paulo@89: d3_geom_voronoiCells = new Array(sites.length); paulo@89: d3_geom_voronoiBeaches = new d3_geom_voronoiRedBlackTree(); paulo@89: d3_geom_voronoiCircles = new d3_geom_voronoiRedBlackTree(); paulo@89: while (true) { paulo@89: circle = d3_geom_voronoiFirstCircle; paulo@89: if (site && (!circle || site.y < circle.y || site.y === circle.y && site.x < circle.x)) { paulo@89: if (site.x !== x0 || site.y !== y0) { paulo@89: d3_geom_voronoiCells[site.i] = new d3_geom_voronoiCell(site); paulo@89: d3_geom_voronoiAddBeach(site); paulo@89: x0 = site.x, y0 = site.y; paulo@89: } paulo@89: site = sites.pop(); paulo@89: } else if (circle) { paulo@89: d3_geom_voronoiRemoveBeach(circle.arc); paulo@89: } else { paulo@89: break; paulo@89: } paulo@89: } paulo@89: if (bbox) d3_geom_voronoiClipEdges(bbox), d3_geom_voronoiCloseCells(bbox); paulo@89: var diagram = { paulo@89: cells: d3_geom_voronoiCells, paulo@89: edges: d3_geom_voronoiEdges paulo@89: }; paulo@89: d3_geom_voronoiBeaches = d3_geom_voronoiCircles = d3_geom_voronoiEdges = d3_geom_voronoiCells = null; paulo@89: return diagram; paulo@89: } paulo@89: function d3_geom_voronoiVertexOrder(a, b) { paulo@89: return b.y - a.y || b.x - a.x; paulo@89: } paulo@89: d3.geom.voronoi = function(points) { paulo@89: var x = d3_geom_pointX, y = d3_geom_pointY, fx = x, fy = y, clipExtent = d3_geom_voronoiClipExtent; paulo@89: if (points) return voronoi(points); paulo@89: function voronoi(data) { paulo@89: var polygons = new Array(data.length), x0 = clipExtent[0][0], y0 = clipExtent[0][1], x1 = clipExtent[1][0], y1 = clipExtent[1][1]; paulo@89: d3_geom_voronoi(sites(data), clipExtent).cells.forEach(function(cell, i) { paulo@89: var edges = cell.edges, site = cell.site, polygon = polygons[i] = edges.length ? edges.map(function(e) { paulo@89: var s = e.start(); paulo@89: return [ s.x, s.y ]; paulo@89: }) : site.x >= x0 && site.x <= x1 && site.y >= y0 && site.y <= y1 ? [ [ x0, y1 ], [ x1, y1 ], [ x1, y0 ], [ x0, y0 ] ] : []; paulo@89: polygon.point = data[i]; paulo@89: }); paulo@89: return polygons; paulo@89: } paulo@89: function sites(data) { paulo@89: return data.map(function(d, i) { paulo@89: return { paulo@89: x: Math.round(fx(d, i) / ε) * ε, paulo@89: y: Math.round(fy(d, i) / ε) * ε, paulo@89: i: i paulo@89: }; paulo@89: }); paulo@89: } paulo@89: voronoi.links = function(data) { paulo@89: return d3_geom_voronoi(sites(data)).edges.filter(function(edge) { paulo@89: return edge.l && edge.r; paulo@89: }).map(function(edge) { paulo@89: return { paulo@89: source: data[edge.l.i], paulo@89: target: data[edge.r.i] paulo@89: }; paulo@89: }); paulo@89: }; paulo@89: voronoi.triangles = function(data) { paulo@89: var triangles = []; paulo@89: d3_geom_voronoi(sites(data)).cells.forEach(function(cell, i) { paulo@89: var site = cell.site, edges = cell.edges.sort(d3_geom_voronoiHalfEdgeOrder), j = -1, m = edges.length, e0, s0, e1 = edges[m - 1].edge, s1 = e1.l === site ? e1.r : e1.l; paulo@89: while (++j < m) { paulo@89: e0 = e1; paulo@89: s0 = s1; paulo@89: e1 = edges[j].edge; paulo@89: s1 = e1.l === site ? e1.r : e1.l; paulo@89: if (i < s0.i && i < s1.i && d3_geom_voronoiTriangleArea(site, s0, s1) < 0) { paulo@89: triangles.push([ data[i], data[s0.i], data[s1.i] ]); paulo@89: } paulo@89: } paulo@89: }); paulo@89: return triangles; paulo@89: }; paulo@89: voronoi.x = function(_) { paulo@89: return arguments.length ? (fx = d3_functor(x = _), voronoi) : x; paulo@89: }; paulo@89: voronoi.y = function(_) { paulo@89: return arguments.length ? (fy = d3_functor(y = _), voronoi) : y; paulo@89: }; paulo@89: voronoi.clipExtent = function(_) { paulo@89: if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent; paulo@89: clipExtent = _ == null ? d3_geom_voronoiClipExtent : _; paulo@89: return voronoi; paulo@89: }; paulo@89: voronoi.size = function(_) { paulo@89: if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent && clipExtent[1]; paulo@89: return voronoi.clipExtent(_ && [ [ 0, 0 ], _ ]); paulo@89: }; paulo@89: return voronoi; paulo@89: }; paulo@89: var d3_geom_voronoiClipExtent = [ [ -1e6, -1e6 ], [ 1e6, 1e6 ] ]; paulo@89: function d3_geom_voronoiTriangleArea(a, b, c) { paulo@89: return (a.x - c.x) * (b.y - a.y) - (a.x - b.x) * (c.y - a.y); paulo@89: } paulo@89: d3.geom.delaunay = function(vertices) { paulo@89: return d3.geom.voronoi().triangles(vertices); paulo@89: }; paulo@89: d3.geom.quadtree = function(points, x1, y1, x2, y2) { paulo@89: var x = d3_geom_pointX, y = d3_geom_pointY, compat; paulo@89: if (compat = arguments.length) { paulo@89: x = d3_geom_quadtreeCompatX; paulo@89: y = d3_geom_quadtreeCompatY; paulo@89: if (compat === 3) { paulo@89: y2 = y1; paulo@89: x2 = x1; paulo@89: y1 = x1 = 0; paulo@89: } paulo@89: return quadtree(points); paulo@89: } paulo@89: function quadtree(data) { paulo@89: var d, fx = d3_functor(x), fy = d3_functor(y), xs, ys, i, n, x1_, y1_, x2_, y2_; paulo@89: if (x1 != null) { paulo@89: x1_ = x1, y1_ = y1, x2_ = x2, y2_ = y2; paulo@89: } else { paulo@89: x2_ = y2_ = -(x1_ = y1_ = Infinity); paulo@89: xs = [], ys = []; paulo@89: n = data.length; paulo@89: if (compat) for (i = 0; i < n; ++i) { paulo@89: d = data[i]; paulo@89: if (d.x < x1_) x1_ = d.x; paulo@89: if (d.y < y1_) y1_ = d.y; paulo@89: if (d.x > x2_) x2_ = d.x; paulo@89: if (d.y > y2_) y2_ = d.y; paulo@89: xs.push(d.x); paulo@89: ys.push(d.y); paulo@89: } else for (i = 0; i < n; ++i) { paulo@89: var x_ = +fx(d = data[i], i), y_ = +fy(d, i); paulo@89: if (x_ < x1_) x1_ = x_; paulo@89: if (y_ < y1_) y1_ = y_; paulo@89: if (x_ > x2_) x2_ = x_; paulo@89: if (y_ > y2_) y2_ = y_; paulo@89: xs.push(x_); paulo@89: ys.push(y_); paulo@89: } paulo@89: } paulo@89: var dx = x2_ - x1_, dy = y2_ - y1_; paulo@89: if (dx > dy) y2_ = y1_ + dx; else x2_ = x1_ + dy; paulo@89: function insert(n, d, x, y, x1, y1, x2, y2) { paulo@89: if (isNaN(x) || isNaN(y)) return; paulo@89: if (n.leaf) { paulo@89: var nx = n.x, ny = n.y; paulo@89: if (nx != null) { paulo@89: if (abs(nx - x) + abs(ny - y) < .01) { paulo@89: insertChild(n, d, x, y, x1, y1, x2, y2); paulo@89: } else { paulo@89: var nPoint = n.point; paulo@89: n.x = n.y = n.point = null; paulo@89: insertChild(n, nPoint, nx, ny, x1, y1, x2, y2); paulo@89: insertChild(n, d, x, y, x1, y1, x2, y2); paulo@89: } paulo@89: } else { paulo@89: n.x = x, n.y = y, n.point = d; paulo@89: } paulo@89: } else { paulo@89: insertChild(n, d, x, y, x1, y1, x2, y2); paulo@89: } paulo@89: } paulo@89: function insertChild(n, d, x, y, x1, y1, x2, y2) { paulo@89: var xm = (x1 + x2) * .5, ym = (y1 + y2) * .5, right = x >= xm, below = y >= ym, i = below << 1 | right; paulo@89: n.leaf = false; paulo@89: n = n.nodes[i] || (n.nodes[i] = d3_geom_quadtreeNode()); paulo@89: if (right) x1 = xm; else x2 = xm; paulo@89: if (below) y1 = ym; else y2 = ym; paulo@89: insert(n, d, x, y, x1, y1, x2, y2); paulo@89: } paulo@89: var root = d3_geom_quadtreeNode(); paulo@89: root.add = function(d) { paulo@89: insert(root, d, +fx(d, ++i), +fy(d, i), x1_, y1_, x2_, y2_); paulo@89: }; paulo@89: root.visit = function(f) { paulo@89: d3_geom_quadtreeVisit(f, root, x1_, y1_, x2_, y2_); paulo@89: }; paulo@89: root.find = function(point) { paulo@89: return d3_geom_quadtreeFind(root, point[0], point[1], x1_, y1_, x2_, y2_); paulo@89: }; paulo@89: i = -1; paulo@89: if (x1 == null) { paulo@89: while (++i < n) { paulo@89: insert(root, data[i], xs[i], ys[i], x1_, y1_, x2_, y2_); paulo@89: } paulo@89: --i; paulo@89: } else data.forEach(root.add); paulo@89: xs = ys = data = d = null; paulo@89: return root; paulo@89: } paulo@89: quadtree.x = function(_) { paulo@89: return arguments.length ? (x = _, quadtree) : x; paulo@89: }; paulo@89: quadtree.y = function(_) { paulo@89: return arguments.length ? (y = _, quadtree) : y; paulo@89: }; paulo@89: quadtree.extent = function(_) { paulo@89: if (!arguments.length) return x1 == null ? null : [ [ x1, y1 ], [ x2, y2 ] ]; paulo@89: if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = +_[0][0], y1 = +_[0][1], x2 = +_[1][0], paulo@89: y2 = +_[1][1]; paulo@89: return quadtree; paulo@89: }; paulo@89: quadtree.size = function(_) { paulo@89: if (!arguments.length) return x1 == null ? null : [ x2 - x1, y2 - y1 ]; paulo@89: if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = y1 = 0, x2 = +_[0], y2 = +_[1]; paulo@89: return quadtree; paulo@89: }; paulo@89: return quadtree; paulo@89: }; paulo@89: function d3_geom_quadtreeCompatX(d) { paulo@89: return d.x; paulo@89: } paulo@89: function d3_geom_quadtreeCompatY(d) { paulo@89: return d.y; paulo@89: } paulo@89: function d3_geom_quadtreeNode() { paulo@89: return { paulo@89: leaf: true, paulo@89: nodes: [], paulo@89: point: null, paulo@89: x: null, paulo@89: y: null paulo@89: }; paulo@89: } paulo@89: function d3_geom_quadtreeVisit(f, node, x1, y1, x2, y2) { paulo@89: if (!f(node, x1, y1, x2, y2)) { paulo@89: var sx = (x1 + x2) * .5, sy = (y1 + y2) * .5, children = node.nodes; paulo@89: if (children[0]) d3_geom_quadtreeVisit(f, children[0], x1, y1, sx, sy); paulo@89: if (children[1]) d3_geom_quadtreeVisit(f, children[1], sx, y1, x2, sy); paulo@89: if (children[2]) d3_geom_quadtreeVisit(f, children[2], x1, sy, sx, y2); paulo@89: if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2); paulo@89: } paulo@89: } paulo@89: function d3_geom_quadtreeFind(root, x, y, x0, y0, x3, y3) { paulo@89: var minDistance2 = Infinity, closestPoint; paulo@89: (function find(node, x1, y1, x2, y2) { paulo@89: if (x1 > x3 || y1 > y3 || x2 < x0 || y2 < y0) return; paulo@89: if (point = node.point) { paulo@89: var point, dx = x - node.x, dy = y - node.y, distance2 = dx * dx + dy * dy; paulo@89: if (distance2 < minDistance2) { paulo@89: var distance = Math.sqrt(minDistance2 = distance2); paulo@89: x0 = x - distance, y0 = y - distance; paulo@89: x3 = x + distance, y3 = y + distance; paulo@89: closestPoint = point; paulo@89: } paulo@89: } paulo@89: var children = node.nodes, xm = (x1 + x2) * .5, ym = (y1 + y2) * .5, right = x >= xm, below = y >= ym; paulo@89: for (var i = below << 1 | right, j = i + 4; i < j; ++i) { paulo@89: if (node = children[i & 3]) switch (i & 3) { paulo@89: case 0: paulo@89: find(node, x1, y1, xm, ym); paulo@89: break; paulo@89: paulo@89: case 1: paulo@89: find(node, xm, y1, x2, ym); paulo@89: break; paulo@89: paulo@89: case 2: paulo@89: find(node, x1, ym, xm, y2); paulo@89: break; paulo@89: paulo@89: case 3: paulo@89: find(node, xm, ym, x2, y2); paulo@89: break; paulo@89: } paulo@89: } paulo@89: })(root, x0, y0, x3, y3); paulo@89: return closestPoint; paulo@89: } paulo@89: d3.interpolateRgb = d3_interpolateRgb; paulo@89: function d3_interpolateRgb(a, b) { paulo@89: a = d3.rgb(a); paulo@89: b = d3.rgb(b); paulo@89: var ar = a.r, ag = a.g, ab = a.b, br = b.r - ar, bg = b.g - ag, bb = b.b - ab; paulo@89: return function(t) { paulo@89: return "#" + d3_rgb_hex(Math.round(ar + br * t)) + d3_rgb_hex(Math.round(ag + bg * t)) + d3_rgb_hex(Math.round(ab + bb * t)); paulo@89: }; paulo@89: } paulo@89: d3.interpolateObject = d3_interpolateObject; paulo@89: function d3_interpolateObject(a, b) { paulo@89: var i = {}, c = {}, k; paulo@89: for (k in a) { paulo@89: if (k in b) { paulo@89: i[k] = d3_interpolate(a[k], b[k]); paulo@89: } else { paulo@89: c[k] = a[k]; paulo@89: } paulo@89: } paulo@89: for (k in b) { paulo@89: if (!(k in a)) { paulo@89: c[k] = b[k]; paulo@89: } paulo@89: } paulo@89: return function(t) { paulo@89: for (k in i) c[k] = i[k](t); paulo@89: return c; paulo@89: }; paulo@89: } paulo@89: d3.interpolateNumber = d3_interpolateNumber; paulo@89: function d3_interpolateNumber(a, b) { paulo@89: a = +a, b = +b; paulo@89: return function(t) { paulo@89: return a * (1 - t) + b * t; paulo@89: }; paulo@89: } paulo@89: d3.interpolateString = d3_interpolateString; paulo@89: function d3_interpolateString(a, b) { paulo@89: var bi = d3_interpolate_numberA.lastIndex = d3_interpolate_numberB.lastIndex = 0, am, bm, bs, i = -1, s = [], q = []; paulo@89: a = a + "", b = b + ""; paulo@89: while ((am = d3_interpolate_numberA.exec(a)) && (bm = d3_interpolate_numberB.exec(b))) { paulo@89: if ((bs = bm.index) > bi) { paulo@89: bs = b.slice(bi, bs); paulo@89: if (s[i]) s[i] += bs; else s[++i] = bs; paulo@89: } paulo@89: if ((am = am[0]) === (bm = bm[0])) { paulo@89: if (s[i]) s[i] += bm; else s[++i] = bm; paulo@89: } else { paulo@89: s[++i] = null; paulo@89: q.push({ paulo@89: i: i, paulo@89: x: d3_interpolateNumber(am, bm) paulo@89: }); paulo@89: } paulo@89: bi = d3_interpolate_numberB.lastIndex; paulo@89: } paulo@89: if (bi < b.length) { paulo@89: bs = b.slice(bi); paulo@89: if (s[i]) s[i] += bs; else s[++i] = bs; paulo@89: } paulo@89: return s.length < 2 ? q[0] ? (b = q[0].x, function(t) { paulo@89: return b(t) + ""; paulo@89: }) : function() { paulo@89: return b; paulo@89: } : (b = q.length, function(t) { paulo@89: for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t); paulo@89: return s.join(""); paulo@89: }); paulo@89: } paulo@89: var d3_interpolate_numberA = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g, d3_interpolate_numberB = new RegExp(d3_interpolate_numberA.source, "g"); paulo@89: d3.interpolate = d3_interpolate; paulo@89: function d3_interpolate(a, b) { paulo@89: var i = d3.interpolators.length, f; paulo@89: while (--i >= 0 && !(f = d3.interpolators[i](a, b))) ; paulo@89: return f; paulo@89: } paulo@89: d3.interpolators = [ function(a, b) { paulo@89: var t = typeof b; paulo@89: return (t === "string" ? d3_rgb_names.has(b.toLowerCase()) || /^(#|rgb\(|hsl\()/i.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_color ? d3_interpolateRgb : Array.isArray(b) ? d3_interpolateArray : t === "object" && isNaN(b) ? d3_interpolateObject : d3_interpolateNumber)(a, b); paulo@89: } ]; paulo@89: d3.interpolateArray = d3_interpolateArray; paulo@89: function d3_interpolateArray(a, b) { paulo@89: var x = [], c = [], na = a.length, nb = b.length, n0 = Math.min(a.length, b.length), i; paulo@89: for (i = 0; i < n0; ++i) x.push(d3_interpolate(a[i], b[i])); paulo@89: for (;i < na; ++i) c[i] = a[i]; paulo@89: for (;i < nb; ++i) c[i] = b[i]; paulo@89: return function(t) { paulo@89: for (i = 0; i < n0; ++i) c[i] = x[i](t); paulo@89: return c; paulo@89: }; paulo@89: } paulo@89: var d3_ease_default = function() { paulo@89: return d3_identity; paulo@89: }; paulo@89: var d3_ease = d3.map({ paulo@89: linear: d3_ease_default, paulo@89: poly: d3_ease_poly, paulo@89: quad: function() { paulo@89: return d3_ease_quad; paulo@89: }, paulo@89: cubic: function() { paulo@89: return d3_ease_cubic; paulo@89: }, paulo@89: sin: function() { paulo@89: return d3_ease_sin; paulo@89: }, paulo@89: exp: function() { paulo@89: return d3_ease_exp; paulo@89: }, paulo@89: circle: function() { paulo@89: return d3_ease_circle; paulo@89: }, paulo@89: elastic: d3_ease_elastic, paulo@89: back: d3_ease_back, paulo@89: bounce: function() { paulo@89: return d3_ease_bounce; paulo@89: } paulo@89: }); paulo@89: var d3_ease_mode = d3.map({ paulo@89: "in": d3_identity, paulo@89: out: d3_ease_reverse, paulo@89: "in-out": d3_ease_reflect, paulo@89: "out-in": function(f) { paulo@89: return d3_ease_reflect(d3_ease_reverse(f)); paulo@89: } paulo@89: }); paulo@89: d3.ease = function(name) { paulo@89: var i = name.indexOf("-"), t = i >= 0 ? name.slice(0, i) : name, m = i >= 0 ? name.slice(i + 1) : "in"; paulo@89: t = d3_ease.get(t) || d3_ease_default; paulo@89: m = d3_ease_mode.get(m) || d3_identity; paulo@89: return d3_ease_clamp(m(t.apply(null, d3_arraySlice.call(arguments, 1)))); paulo@89: }; paulo@89: function d3_ease_clamp(f) { paulo@89: return function(t) { paulo@89: return t <= 0 ? 0 : t >= 1 ? 1 : f(t); paulo@89: }; paulo@89: } paulo@89: function d3_ease_reverse(f) { paulo@89: return function(t) { paulo@89: return 1 - f(1 - t); paulo@89: }; paulo@89: } paulo@89: function d3_ease_reflect(f) { paulo@89: return function(t) { paulo@89: return .5 * (t < .5 ? f(2 * t) : 2 - f(2 - 2 * t)); paulo@89: }; paulo@89: } paulo@89: function d3_ease_quad(t) { paulo@89: return t * t; paulo@89: } paulo@89: function d3_ease_cubic(t) { paulo@89: return t * t * t; paulo@89: } paulo@89: function d3_ease_cubicInOut(t) { paulo@89: if (t <= 0) return 0; paulo@89: if (t >= 1) return 1; paulo@89: var t2 = t * t, t3 = t2 * t; paulo@89: return 4 * (t < .5 ? t3 : 3 * (t - t2) + t3 - .75); paulo@89: } paulo@89: function d3_ease_poly(e) { paulo@89: return function(t) { paulo@89: return Math.pow(t, e); paulo@89: }; paulo@89: } paulo@89: function d3_ease_sin(t) { paulo@89: return 1 - Math.cos(t * halfπ); paulo@89: } paulo@89: function d3_ease_exp(t) { paulo@89: return Math.pow(2, 10 * (t - 1)); paulo@89: } paulo@89: function d3_ease_circle(t) { paulo@89: return 1 - Math.sqrt(1 - t * t); paulo@89: } paulo@89: function d3_ease_elastic(a, p) { paulo@89: var s; paulo@89: if (arguments.length < 2) p = .45; paulo@89: if (arguments.length) s = p / τ * Math.asin(1 / a); else a = 1, s = p / 4; paulo@89: return function(t) { paulo@89: return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * τ / p); paulo@89: }; paulo@89: } paulo@89: function d3_ease_back(s) { paulo@89: if (!s) s = 1.70158; paulo@89: return function(t) { paulo@89: return t * t * ((s + 1) * t - s); paulo@89: }; paulo@89: } paulo@89: function d3_ease_bounce(t) { paulo@89: return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; paulo@89: } paulo@89: d3.interpolateHcl = d3_interpolateHcl; paulo@89: function d3_interpolateHcl(a, b) { paulo@89: a = d3.hcl(a); paulo@89: b = d3.hcl(b); paulo@89: var ah = a.h, ac = a.c, al = a.l, bh = b.h - ah, bc = b.c - ac, bl = b.l - al; paulo@89: if (isNaN(bc)) bc = 0, ac = isNaN(ac) ? b.c : ac; paulo@89: if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; paulo@89: return function(t) { paulo@89: return d3_hcl_lab(ah + bh * t, ac + bc * t, al + bl * t) + ""; paulo@89: }; paulo@89: } paulo@89: d3.interpolateHsl = d3_interpolateHsl; paulo@89: function d3_interpolateHsl(a, b) { paulo@89: a = d3.hsl(a); paulo@89: b = d3.hsl(b); paulo@89: var ah = a.h, as = a.s, al = a.l, bh = b.h - ah, bs = b.s - as, bl = b.l - al; paulo@89: if (isNaN(bs)) bs = 0, as = isNaN(as) ? b.s : as; paulo@89: if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; paulo@89: return function(t) { paulo@89: return d3_hsl_rgb(ah + bh * t, as + bs * t, al + bl * t) + ""; paulo@89: }; paulo@89: } paulo@89: d3.interpolateLab = d3_interpolateLab; paulo@89: function d3_interpolateLab(a, b) { paulo@89: a = d3.lab(a); paulo@89: b = d3.lab(b); paulo@89: var al = a.l, aa = a.a, ab = a.b, bl = b.l - al, ba = b.a - aa, bb = b.b - ab; paulo@89: return function(t) { paulo@89: return d3_lab_rgb(al + bl * t, aa + ba * t, ab + bb * t) + ""; paulo@89: }; paulo@89: } paulo@89: d3.interpolateRound = d3_interpolateRound; paulo@89: function d3_interpolateRound(a, b) { paulo@89: b -= a; paulo@89: return function(t) { paulo@89: return Math.round(a + b * t); paulo@89: }; paulo@89: } paulo@89: d3.transform = function(string) { paulo@89: var g = d3_document.createElementNS(d3.ns.prefix.svg, "g"); paulo@89: return (d3.transform = function(string) { paulo@89: if (string != null) { paulo@89: g.setAttribute("transform", string); paulo@89: var t = g.transform.baseVal.consolidate(); paulo@89: } paulo@89: return new d3_transform(t ? t.matrix : d3_transformIdentity); paulo@89: })(string); paulo@89: }; paulo@89: function d3_transform(m) { paulo@89: var r0 = [ m.a, m.b ], r1 = [ m.c, m.d ], kx = d3_transformNormalize(r0), kz = d3_transformDot(r0, r1), ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0; paulo@89: if (r0[0] * r1[1] < r1[0] * r0[1]) { paulo@89: r0[0] *= -1; paulo@89: r0[1] *= -1; paulo@89: kx *= -1; paulo@89: kz *= -1; paulo@89: } paulo@89: this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_degrees; paulo@89: this.translate = [ m.e, m.f ]; paulo@89: this.scale = [ kx, ky ]; paulo@89: this.skew = ky ? Math.atan2(kz, ky) * d3_degrees : 0; paulo@89: } paulo@89: d3_transform.prototype.toString = function() { paulo@89: return "translate(" + this.translate + ")rotate(" + this.rotate + ")skewX(" + this.skew + ")scale(" + this.scale + ")"; paulo@89: }; paulo@89: function d3_transformDot(a, b) { paulo@89: return a[0] * b[0] + a[1] * b[1]; paulo@89: } paulo@89: function d3_transformNormalize(a) { paulo@89: var k = Math.sqrt(d3_transformDot(a, a)); paulo@89: if (k) { paulo@89: a[0] /= k; paulo@89: a[1] /= k; paulo@89: } paulo@89: return k; paulo@89: } paulo@89: function d3_transformCombine(a, b, k) { paulo@89: a[0] += k * b[0]; paulo@89: a[1] += k * b[1]; paulo@89: return a; paulo@89: } paulo@89: var d3_transformIdentity = { paulo@89: a: 1, paulo@89: b: 0, paulo@89: c: 0, paulo@89: d: 1, paulo@89: e: 0, paulo@89: f: 0 paulo@89: }; paulo@89: d3.interpolateTransform = d3_interpolateTransform; paulo@89: function d3_interpolateTransformPop(s) { paulo@89: return s.length ? s.pop() + "," : ""; paulo@89: } paulo@89: function d3_interpolateTranslate(ta, tb, s, q) { paulo@89: if (ta[0] !== tb[0] || ta[1] !== tb[1]) { paulo@89: var i = s.push("translate(", null, ",", null, ")"); paulo@89: q.push({ paulo@89: i: i - 4, paulo@89: x: d3_interpolateNumber(ta[0], tb[0]) paulo@89: }, { paulo@89: i: i - 2, paulo@89: x: d3_interpolateNumber(ta[1], tb[1]) paulo@89: }); paulo@89: } else if (tb[0] || tb[1]) { paulo@89: s.push("translate(" + tb + ")"); paulo@89: } paulo@89: } paulo@89: function d3_interpolateRotate(ra, rb, s, q) { paulo@89: if (ra !== rb) { paulo@89: if (ra - rb > 180) rb += 360; else if (rb - ra > 180) ra += 360; paulo@89: q.push({ paulo@89: i: s.push(d3_interpolateTransformPop(s) + "rotate(", null, ")") - 2, paulo@89: x: d3_interpolateNumber(ra, rb) paulo@89: }); paulo@89: } else if (rb) { paulo@89: s.push(d3_interpolateTransformPop(s) + "rotate(" + rb + ")"); paulo@89: } paulo@89: } paulo@89: function d3_interpolateSkew(wa, wb, s, q) { paulo@89: if (wa !== wb) { paulo@89: q.push({ paulo@89: i: s.push(d3_interpolateTransformPop(s) + "skewX(", null, ")") - 2, paulo@89: x: d3_interpolateNumber(wa, wb) paulo@89: }); paulo@89: } else if (wb) { paulo@89: s.push(d3_interpolateTransformPop(s) + "skewX(" + wb + ")"); paulo@89: } paulo@89: } paulo@89: function d3_interpolateScale(ka, kb, s, q) { paulo@89: if (ka[0] !== kb[0] || ka[1] !== kb[1]) { paulo@89: var i = s.push(d3_interpolateTransformPop(s) + "scale(", null, ",", null, ")"); paulo@89: q.push({ paulo@89: i: i - 4, paulo@89: x: d3_interpolateNumber(ka[0], kb[0]) paulo@89: }, { paulo@89: i: i - 2, paulo@89: x: d3_interpolateNumber(ka[1], kb[1]) paulo@89: }); paulo@89: } else if (kb[0] !== 1 || kb[1] !== 1) { paulo@89: s.push(d3_interpolateTransformPop(s) + "scale(" + kb + ")"); paulo@89: } paulo@89: } paulo@89: function d3_interpolateTransform(a, b) { paulo@89: var s = [], q = []; paulo@89: a = d3.transform(a), b = d3.transform(b); paulo@89: d3_interpolateTranslate(a.translate, b.translate, s, q); paulo@89: d3_interpolateRotate(a.rotate, b.rotate, s, q); paulo@89: d3_interpolateSkew(a.skew, b.skew, s, q); paulo@89: d3_interpolateScale(a.scale, b.scale, s, q); paulo@89: a = b = null; paulo@89: return function(t) { paulo@89: var i = -1, n = q.length, o; paulo@89: while (++i < n) s[(o = q[i]).i] = o.x(t); paulo@89: return s.join(""); paulo@89: }; paulo@89: } paulo@89: function d3_uninterpolateNumber(a, b) { paulo@89: b = (b -= a = +a) || 1 / b; paulo@89: return function(x) { paulo@89: return (x - a) / b; paulo@89: }; paulo@89: } paulo@89: function d3_uninterpolateClamp(a, b) { paulo@89: b = (b -= a = +a) || 1 / b; paulo@89: return function(x) { paulo@89: return Math.max(0, Math.min(1, (x - a) / b)); paulo@89: }; paulo@89: } paulo@89: d3.layout = {}; paulo@89: d3.layout.bundle = function() { paulo@89: return function(links) { paulo@89: var paths = [], i = -1, n = links.length; paulo@89: while (++i < n) paths.push(d3_layout_bundlePath(links[i])); paulo@89: return paths; paulo@89: }; paulo@89: }; paulo@89: function d3_layout_bundlePath(link) { paulo@89: var start = link.source, end = link.target, lca = d3_layout_bundleLeastCommonAncestor(start, end), points = [ start ]; paulo@89: while (start !== lca) { paulo@89: start = start.parent; paulo@89: points.push(start); paulo@89: } paulo@89: var k = points.length; paulo@89: while (end !== lca) { paulo@89: points.splice(k, 0, end); paulo@89: end = end.parent; paulo@89: } paulo@89: return points; paulo@89: } paulo@89: function d3_layout_bundleAncestors(node) { paulo@89: var ancestors = [], parent = node.parent; paulo@89: while (parent != null) { paulo@89: ancestors.push(node); paulo@89: node = parent; paulo@89: parent = parent.parent; paulo@89: } paulo@89: ancestors.push(node); paulo@89: return ancestors; paulo@89: } paulo@89: function d3_layout_bundleLeastCommonAncestor(a, b) { paulo@89: if (a === b) return a; paulo@89: var aNodes = d3_layout_bundleAncestors(a), bNodes = d3_layout_bundleAncestors(b), aNode = aNodes.pop(), bNode = bNodes.pop(), sharedNode = null; paulo@89: while (aNode === bNode) { paulo@89: sharedNode = aNode; paulo@89: aNode = aNodes.pop(); paulo@89: bNode = bNodes.pop(); paulo@89: } paulo@89: return sharedNode; paulo@89: } paulo@89: d3.layout.chord = function() { paulo@89: var chord = {}, chords, groups, matrix, n, padding = 0, sortGroups, sortSubgroups, sortChords; paulo@89: function relayout() { paulo@89: var subgroups = {}, groupSums = [], groupIndex = d3.range(n), subgroupIndex = [], k, x, x0, i, j; paulo@89: chords = []; paulo@89: groups = []; paulo@89: k = 0, i = -1; paulo@89: while (++i < n) { paulo@89: x = 0, j = -1; paulo@89: while (++j < n) { paulo@89: x += matrix[i][j]; paulo@89: } paulo@89: groupSums.push(x); paulo@89: subgroupIndex.push(d3.range(n)); paulo@89: k += x; paulo@89: } paulo@89: if (sortGroups) { paulo@89: groupIndex.sort(function(a, b) { paulo@89: return sortGroups(groupSums[a], groupSums[b]); paulo@89: }); paulo@89: } paulo@89: if (sortSubgroups) { paulo@89: subgroupIndex.forEach(function(d, i) { paulo@89: d.sort(function(a, b) { paulo@89: return sortSubgroups(matrix[i][a], matrix[i][b]); paulo@89: }); paulo@89: }); paulo@89: } paulo@89: k = (τ - padding * n) / k; paulo@89: x = 0, i = -1; paulo@89: while (++i < n) { paulo@89: x0 = x, j = -1; paulo@89: while (++j < n) { paulo@89: var di = groupIndex[i], dj = subgroupIndex[di][j], v = matrix[di][dj], a0 = x, a1 = x += v * k; paulo@89: subgroups[di + "-" + dj] = { paulo@89: index: di, paulo@89: subindex: dj, paulo@89: startAngle: a0, paulo@89: endAngle: a1, paulo@89: value: v paulo@89: }; paulo@89: } paulo@89: groups[di] = { paulo@89: index: di, paulo@89: startAngle: x0, paulo@89: endAngle: x, paulo@89: value: groupSums[di] paulo@89: }; paulo@89: x += padding; paulo@89: } paulo@89: i = -1; paulo@89: while (++i < n) { paulo@89: j = i - 1; paulo@89: while (++j < n) { paulo@89: var source = subgroups[i + "-" + j], target = subgroups[j + "-" + i]; paulo@89: if (source.value || target.value) { paulo@89: chords.push(source.value < target.value ? { paulo@89: source: target, paulo@89: target: source paulo@89: } : { paulo@89: source: source, paulo@89: target: target paulo@89: }); paulo@89: } paulo@89: } paulo@89: } paulo@89: if (sortChords) resort(); paulo@89: } paulo@89: function resort() { paulo@89: chords.sort(function(a, b) { paulo@89: return sortChords((a.source.value + a.target.value) / 2, (b.source.value + b.target.value) / 2); paulo@89: }); paulo@89: } paulo@89: chord.matrix = function(x) { paulo@89: if (!arguments.length) return matrix; paulo@89: n = (matrix = x) && matrix.length; paulo@89: chords = groups = null; paulo@89: return chord; paulo@89: }; paulo@89: chord.padding = function(x) { paulo@89: if (!arguments.length) return padding; paulo@89: padding = x; paulo@89: chords = groups = null; paulo@89: return chord; paulo@89: }; paulo@89: chord.sortGroups = function(x) { paulo@89: if (!arguments.length) return sortGroups; paulo@89: sortGroups = x; paulo@89: chords = groups = null; paulo@89: return chord; paulo@89: }; paulo@89: chord.sortSubgroups = function(x) { paulo@89: if (!arguments.length) return sortSubgroups; paulo@89: sortSubgroups = x; paulo@89: chords = null; paulo@89: return chord; paulo@89: }; paulo@89: chord.sortChords = function(x) { paulo@89: if (!arguments.length) return sortChords; paulo@89: sortChords = x; paulo@89: if (chords) resort(); paulo@89: return chord; paulo@89: }; paulo@89: chord.chords = function() { paulo@89: if (!chords) relayout(); paulo@89: return chords; paulo@89: }; paulo@89: chord.groups = function() { paulo@89: if (!groups) relayout(); paulo@89: return groups; paulo@89: }; paulo@89: return chord; paulo@89: }; paulo@89: d3.layout.force = function() { paulo@89: var force = {}, event = d3.dispatch("start", "tick", "end"), timer, size = [ 1, 1 ], drag, alpha, friction = .9, linkDistance = d3_layout_forceLinkDistance, linkStrength = d3_layout_forceLinkStrength, charge = -30, chargeDistance2 = d3_layout_forceChargeDistance2, gravity = .1, theta2 = .64, nodes = [], links = [], distances, strengths, charges; paulo@89: function repulse(node) { paulo@89: return function(quad, x1, _, x2) { paulo@89: if (quad.point !== node) { paulo@89: var dx = quad.cx - node.x, dy = quad.cy - node.y, dw = x2 - x1, dn = dx * dx + dy * dy; paulo@89: if (dw * dw / theta2 < dn) { paulo@89: if (dn < chargeDistance2) { paulo@89: var k = quad.charge / dn; paulo@89: node.px -= dx * k; paulo@89: node.py -= dy * k; paulo@89: } paulo@89: return true; paulo@89: } paulo@89: if (quad.point && dn && dn < chargeDistance2) { paulo@89: var k = quad.pointCharge / dn; paulo@89: node.px -= dx * k; paulo@89: node.py -= dy * k; paulo@89: } paulo@89: } paulo@89: return !quad.charge; paulo@89: }; paulo@89: } paulo@89: force.tick = function() { paulo@89: if ((alpha *= .99) < .005) { paulo@89: timer = null; paulo@89: event.end({ paulo@89: type: "end", paulo@89: alpha: alpha = 0 paulo@89: }); paulo@89: return true; paulo@89: } paulo@89: var n = nodes.length, m = links.length, q, i, o, s, t, l, k, x, y; paulo@89: for (i = 0; i < m; ++i) { paulo@89: o = links[i]; paulo@89: s = o.source; paulo@89: t = o.target; paulo@89: x = t.x - s.x; paulo@89: y = t.y - s.y; paulo@89: if (l = x * x + y * y) { paulo@89: l = alpha * strengths[i] * ((l = Math.sqrt(l)) - distances[i]) / l; paulo@89: x *= l; paulo@89: y *= l; paulo@89: t.x -= x * (k = s.weight + t.weight ? s.weight / (s.weight + t.weight) : .5); paulo@89: t.y -= y * k; paulo@89: s.x += x * (k = 1 - k); paulo@89: s.y += y * k; paulo@89: } paulo@89: } paulo@89: if (k = alpha * gravity) { paulo@89: x = size[0] / 2; paulo@89: y = size[1] / 2; paulo@89: i = -1; paulo@89: if (k) while (++i < n) { paulo@89: o = nodes[i]; paulo@89: o.x += (x - o.x) * k; paulo@89: o.y += (y - o.y) * k; paulo@89: } paulo@89: } paulo@89: if (charge) { paulo@89: d3_layout_forceAccumulate(q = d3.geom.quadtree(nodes), alpha, charges); paulo@89: i = -1; paulo@89: while (++i < n) { paulo@89: if (!(o = nodes[i]).fixed) { paulo@89: q.visit(repulse(o)); paulo@89: } paulo@89: } paulo@89: } paulo@89: i = -1; paulo@89: while (++i < n) { paulo@89: o = nodes[i]; paulo@89: if (o.fixed) { paulo@89: o.x = o.px; paulo@89: o.y = o.py; paulo@89: } else { paulo@89: o.x -= (o.px - (o.px = o.x)) * friction; paulo@89: o.y -= (o.py - (o.py = o.y)) * friction; paulo@89: } paulo@89: } paulo@89: event.tick({ paulo@89: type: "tick", paulo@89: alpha: alpha paulo@89: }); paulo@89: }; paulo@89: force.nodes = function(x) { paulo@89: if (!arguments.length) return nodes; paulo@89: nodes = x; paulo@89: return force; paulo@89: }; paulo@89: force.links = function(x) { paulo@89: if (!arguments.length) return links; paulo@89: links = x; paulo@89: return force; paulo@89: }; paulo@89: force.size = function(x) { paulo@89: if (!arguments.length) return size; paulo@89: size = x; paulo@89: return force; paulo@89: }; paulo@89: force.linkDistance = function(x) { paulo@89: if (!arguments.length) return linkDistance; paulo@89: linkDistance = typeof x === "function" ? x : +x; paulo@89: return force; paulo@89: }; paulo@89: force.distance = force.linkDistance; paulo@89: force.linkStrength = function(x) { paulo@89: if (!arguments.length) return linkStrength; paulo@89: linkStrength = typeof x === "function" ? x : +x; paulo@89: return force; paulo@89: }; paulo@89: force.friction = function(x) { paulo@89: if (!arguments.length) return friction; paulo@89: friction = +x; paulo@89: return force; paulo@89: }; paulo@89: force.charge = function(x) { paulo@89: if (!arguments.length) return charge; paulo@89: charge = typeof x === "function" ? x : +x; paulo@89: return force; paulo@89: }; paulo@89: force.chargeDistance = function(x) { paulo@89: if (!arguments.length) return Math.sqrt(chargeDistance2); paulo@89: chargeDistance2 = x * x; paulo@89: return force; paulo@89: }; paulo@89: force.gravity = function(x) { paulo@89: if (!arguments.length) return gravity; paulo@89: gravity = +x; paulo@89: return force; paulo@89: }; paulo@89: force.theta = function(x) { paulo@89: if (!arguments.length) return Math.sqrt(theta2); paulo@89: theta2 = x * x; paulo@89: return force; paulo@89: }; paulo@89: force.alpha = function(x) { paulo@89: if (!arguments.length) return alpha; paulo@89: x = +x; paulo@89: if (alpha) { paulo@89: if (x > 0) { paulo@89: alpha = x; paulo@89: } else { paulo@89: timer.c = null, timer.t = NaN, timer = null; paulo@89: event.end({ paulo@89: type: "end", paulo@89: alpha: alpha = 0 paulo@89: }); paulo@89: } paulo@89: } else if (x > 0) { paulo@89: event.start({ paulo@89: type: "start", paulo@89: alpha: alpha = x paulo@89: }); paulo@89: timer = d3_timer(force.tick); paulo@89: } paulo@89: return force; paulo@89: }; paulo@89: force.start = function() { paulo@89: var i, n = nodes.length, m = links.length, w = size[0], h = size[1], neighbors, o; paulo@89: for (i = 0; i < n; ++i) { paulo@89: (o = nodes[i]).index = i; paulo@89: o.weight = 0; paulo@89: } paulo@89: for (i = 0; i < m; ++i) { paulo@89: o = links[i]; paulo@89: if (typeof o.source == "number") o.source = nodes[o.source]; paulo@89: if (typeof o.target == "number") o.target = nodes[o.target]; paulo@89: ++o.source.weight; paulo@89: ++o.target.weight; paulo@89: } paulo@89: for (i = 0; i < n; ++i) { paulo@89: o = nodes[i]; paulo@89: if (isNaN(o.x)) o.x = position("x", w); paulo@89: if (isNaN(o.y)) o.y = position("y", h); paulo@89: if (isNaN(o.px)) o.px = o.x; paulo@89: if (isNaN(o.py)) o.py = o.y; paulo@89: } paulo@89: distances = []; paulo@89: if (typeof linkDistance === "function") for (i = 0; i < m; ++i) distances[i] = +linkDistance.call(this, links[i], i); else for (i = 0; i < m; ++i) distances[i] = linkDistance; paulo@89: strengths = []; paulo@89: if (typeof linkStrength === "function") for (i = 0; i < m; ++i) strengths[i] = +linkStrength.call(this, links[i], i); else for (i = 0; i < m; ++i) strengths[i] = linkStrength; paulo@89: charges = []; paulo@89: if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i); else for (i = 0; i < n; ++i) charges[i] = charge; paulo@89: function position(dimension, size) { paulo@89: if (!neighbors) { paulo@89: neighbors = new Array(n); paulo@89: for (j = 0; j < n; ++j) { paulo@89: neighbors[j] = []; paulo@89: } paulo@89: for (j = 0; j < m; ++j) { paulo@89: var o = links[j]; paulo@89: neighbors[o.source.index].push(o.target); paulo@89: neighbors[o.target.index].push(o.source); paulo@89: } paulo@89: } paulo@89: var candidates = neighbors[i], j = -1, l = candidates.length, x; paulo@89: while (++j < l) if (!isNaN(x = candidates[j][dimension])) return x; paulo@89: return Math.random() * size; paulo@89: } paulo@89: return force.resume(); paulo@89: }; paulo@89: force.resume = function() { paulo@89: return force.alpha(.1); paulo@89: }; paulo@89: force.stop = function() { paulo@89: return force.alpha(0); paulo@89: }; paulo@89: force.drag = function() { paulo@89: if (!drag) drag = d3.behavior.drag().origin(d3_identity).on("dragstart.force", d3_layout_forceDragstart).on("drag.force", dragmove).on("dragend.force", d3_layout_forceDragend); paulo@89: if (!arguments.length) return drag; paulo@89: this.on("mouseover.force", d3_layout_forceMouseover).on("mouseout.force", d3_layout_forceMouseout).call(drag); paulo@89: }; paulo@89: function dragmove(d) { paulo@89: d.px = d3.event.x, d.py = d3.event.y; paulo@89: force.resume(); paulo@89: } paulo@89: return d3.rebind(force, event, "on"); paulo@89: }; paulo@89: function d3_layout_forceDragstart(d) { paulo@89: d.fixed |= 2; paulo@89: } paulo@89: function d3_layout_forceDragend(d) { paulo@89: d.fixed &= ~6; paulo@89: } paulo@89: function d3_layout_forceMouseover(d) { paulo@89: d.fixed |= 4; paulo@89: d.px = d.x, d.py = d.y; paulo@89: } paulo@89: function d3_layout_forceMouseout(d) { paulo@89: d.fixed &= ~4; paulo@89: } paulo@89: function d3_layout_forceAccumulate(quad, alpha, charges) { paulo@89: var cx = 0, cy = 0; paulo@89: quad.charge = 0; paulo@89: if (!quad.leaf) { paulo@89: var nodes = quad.nodes, n = nodes.length, i = -1, c; paulo@89: while (++i < n) { paulo@89: c = nodes[i]; paulo@89: if (c == null) continue; paulo@89: d3_layout_forceAccumulate(c, alpha, charges); paulo@89: quad.charge += c.charge; paulo@89: cx += c.charge * c.cx; paulo@89: cy += c.charge * c.cy; paulo@89: } paulo@89: } paulo@89: if (quad.point) { paulo@89: if (!quad.leaf) { paulo@89: quad.point.x += Math.random() - .5; paulo@89: quad.point.y += Math.random() - .5; paulo@89: } paulo@89: var k = alpha * charges[quad.point.index]; paulo@89: quad.charge += quad.pointCharge = k; paulo@89: cx += k * quad.point.x; paulo@89: cy += k * quad.point.y; paulo@89: } paulo@89: quad.cx = cx / quad.charge; paulo@89: quad.cy = cy / quad.charge; paulo@89: } paulo@89: var d3_layout_forceLinkDistance = 20, d3_layout_forceLinkStrength = 1, d3_layout_forceChargeDistance2 = Infinity; paulo@89: d3.layout.hierarchy = function() { paulo@89: var sort = d3_layout_hierarchySort, children = d3_layout_hierarchyChildren, value = d3_layout_hierarchyValue; paulo@89: function hierarchy(root) { paulo@89: var stack = [ root ], nodes = [], node; paulo@89: root.depth = 0; paulo@89: while ((node = stack.pop()) != null) { paulo@89: nodes.push(node); paulo@89: if ((childs = children.call(hierarchy, node, node.depth)) && (n = childs.length)) { paulo@89: var n, childs, child; paulo@89: while (--n >= 0) { paulo@89: stack.push(child = childs[n]); paulo@89: child.parent = node; paulo@89: child.depth = node.depth + 1; paulo@89: } paulo@89: if (value) node.value = 0; paulo@89: node.children = childs; paulo@89: } else { paulo@89: if (value) node.value = +value.call(hierarchy, node, node.depth) || 0; paulo@89: delete node.children; paulo@89: } paulo@89: } paulo@89: d3_layout_hierarchyVisitAfter(root, function(node) { paulo@89: var childs, parent; paulo@89: if (sort && (childs = node.children)) childs.sort(sort); paulo@89: if (value && (parent = node.parent)) parent.value += node.value; paulo@89: }); paulo@89: return nodes; paulo@89: } paulo@89: hierarchy.sort = function(x) { paulo@89: if (!arguments.length) return sort; paulo@89: sort = x; paulo@89: return hierarchy; paulo@89: }; paulo@89: hierarchy.children = function(x) { paulo@89: if (!arguments.length) return children; paulo@89: children = x; paulo@89: return hierarchy; paulo@89: }; paulo@89: hierarchy.value = function(x) { paulo@89: if (!arguments.length) return value; paulo@89: value = x; paulo@89: return hierarchy; paulo@89: }; paulo@89: hierarchy.revalue = function(root) { paulo@89: if (value) { paulo@89: d3_layout_hierarchyVisitBefore(root, function(node) { paulo@89: if (node.children) node.value = 0; paulo@89: }); paulo@89: d3_layout_hierarchyVisitAfter(root, function(node) { paulo@89: var parent; paulo@89: if (!node.children) node.value = +value.call(hierarchy, node, node.depth) || 0; paulo@89: if (parent = node.parent) parent.value += node.value; paulo@89: }); paulo@89: } paulo@89: return root; paulo@89: }; paulo@89: return hierarchy; paulo@89: }; paulo@89: function d3_layout_hierarchyRebind(object, hierarchy) { paulo@89: d3.rebind(object, hierarchy, "sort", "children", "value"); paulo@89: object.nodes = object; paulo@89: object.links = d3_layout_hierarchyLinks; paulo@89: return object; paulo@89: } paulo@89: function d3_layout_hierarchyVisitBefore(node, callback) { paulo@89: var nodes = [ node ]; paulo@89: while ((node = nodes.pop()) != null) { paulo@89: callback(node); paulo@89: if ((children = node.children) && (n = children.length)) { paulo@89: var n, children; paulo@89: while (--n >= 0) nodes.push(children[n]); paulo@89: } paulo@89: } paulo@89: } paulo@89: function d3_layout_hierarchyVisitAfter(node, callback) { paulo@89: var nodes = [ node ], nodes2 = []; paulo@89: while ((node = nodes.pop()) != null) { paulo@89: nodes2.push(node); paulo@89: if ((children = node.children) && (n = children.length)) { paulo@89: var i = -1, n, children; paulo@89: while (++i < n) nodes.push(children[i]); paulo@89: } paulo@89: } paulo@89: while ((node = nodes2.pop()) != null) { paulo@89: callback(node); paulo@89: } paulo@89: } paulo@89: function d3_layout_hierarchyChildren(d) { paulo@89: return d.children; paulo@89: } paulo@89: function d3_layout_hierarchyValue(d) { paulo@89: return d.value; paulo@89: } paulo@89: function d3_layout_hierarchySort(a, b) { paulo@89: return b.value - a.value; paulo@89: } paulo@89: function d3_layout_hierarchyLinks(nodes) { paulo@89: return d3.merge(nodes.map(function(parent) { paulo@89: return (parent.children || []).map(function(child) { paulo@89: return { paulo@89: source: parent, paulo@89: target: child paulo@89: }; paulo@89: }); paulo@89: })); paulo@89: } paulo@89: d3.layout.partition = function() { paulo@89: var hierarchy = d3.layout.hierarchy(), size = [ 1, 1 ]; paulo@89: function position(node, x, dx, dy) { paulo@89: var children = node.children; paulo@89: node.x = x; paulo@89: node.y = node.depth * dy; paulo@89: node.dx = dx; paulo@89: node.dy = dy; paulo@89: if (children && (n = children.length)) { paulo@89: var i = -1, n, c, d; paulo@89: dx = node.value ? dx / node.value : 0; paulo@89: while (++i < n) { paulo@89: position(c = children[i], x, d = c.value * dx, dy); paulo@89: x += d; paulo@89: } paulo@89: } paulo@89: } paulo@89: function depth(node) { paulo@89: var children = node.children, d = 0; paulo@89: if (children && (n = children.length)) { paulo@89: var i = -1, n; paulo@89: while (++i < n) d = Math.max(d, depth(children[i])); paulo@89: } paulo@89: return 1 + d; paulo@89: } paulo@89: function partition(d, i) { paulo@89: var nodes = hierarchy.call(this, d, i); paulo@89: position(nodes[0], 0, size[0], size[1] / depth(nodes[0])); paulo@89: return nodes; paulo@89: } paulo@89: partition.size = function(x) { paulo@89: if (!arguments.length) return size; paulo@89: size = x; paulo@89: return partition; paulo@89: }; paulo@89: return d3_layout_hierarchyRebind(partition, hierarchy); paulo@89: }; paulo@89: d3.layout.pie = function() { paulo@89: var value = Number, sort = d3_layout_pieSortByValue, startAngle = 0, endAngle = τ, padAngle = 0; paulo@89: function pie(data) { paulo@89: var n = data.length, values = data.map(function(d, i) { paulo@89: return +value.call(pie, d, i); paulo@89: }), a = +(typeof startAngle === "function" ? startAngle.apply(this, arguments) : startAngle), da = (typeof endAngle === "function" ? endAngle.apply(this, arguments) : endAngle) - a, p = Math.min(Math.abs(da) / n, +(typeof padAngle === "function" ? padAngle.apply(this, arguments) : padAngle)), pa = p * (da < 0 ? -1 : 1), sum = d3.sum(values), k = sum ? (da - n * pa) / sum : 0, index = d3.range(n), arcs = [], v; paulo@89: if (sort != null) index.sort(sort === d3_layout_pieSortByValue ? function(i, j) { paulo@89: return values[j] - values[i]; paulo@89: } : function(i, j) { paulo@89: return sort(data[i], data[j]); paulo@89: }); paulo@89: index.forEach(function(i) { paulo@89: arcs[i] = { paulo@89: data: data[i], paulo@89: value: v = values[i], paulo@89: startAngle: a, paulo@89: endAngle: a += v * k + pa, paulo@89: padAngle: p paulo@89: }; paulo@89: }); paulo@89: return arcs; paulo@89: } paulo@89: pie.value = function(_) { paulo@89: if (!arguments.length) return value; paulo@89: value = _; paulo@89: return pie; paulo@89: }; paulo@89: pie.sort = function(_) { paulo@89: if (!arguments.length) return sort; paulo@89: sort = _; paulo@89: return pie; paulo@89: }; paulo@89: pie.startAngle = function(_) { paulo@89: if (!arguments.length) return startAngle; paulo@89: startAngle = _; paulo@89: return pie; paulo@89: }; paulo@89: pie.endAngle = function(_) { paulo@89: if (!arguments.length) return endAngle; paulo@89: endAngle = _; paulo@89: return pie; paulo@89: }; paulo@89: pie.padAngle = function(_) { paulo@89: if (!arguments.length) return padAngle; paulo@89: padAngle = _; paulo@89: return pie; paulo@89: }; paulo@89: return pie; paulo@89: }; paulo@89: var d3_layout_pieSortByValue = {}; paulo@89: d3.layout.stack = function() { paulo@89: var values = d3_identity, order = d3_layout_stackOrderDefault, offset = d3_layout_stackOffsetZero, out = d3_layout_stackOut, x = d3_layout_stackX, y = d3_layout_stackY; paulo@89: function stack(data, index) { paulo@89: if (!(n = data.length)) return data; paulo@89: var series = data.map(function(d, i) { paulo@89: return values.call(stack, d, i); paulo@89: }); paulo@89: var points = series.map(function(d) { paulo@89: return d.map(function(v, i) { paulo@89: return [ x.call(stack, v, i), y.call(stack, v, i) ]; paulo@89: }); paulo@89: }); paulo@89: var orders = order.call(stack, points, index); paulo@89: series = d3.permute(series, orders); paulo@89: points = d3.permute(points, orders); paulo@89: var offsets = offset.call(stack, points, index); paulo@89: var m = series[0].length, n, i, j, o; paulo@89: for (j = 0; j < m; ++j) { paulo@89: out.call(stack, series[0][j], o = offsets[j], points[0][j][1]); paulo@89: for (i = 1; i < n; ++i) { paulo@89: out.call(stack, series[i][j], o += points[i - 1][j][1], points[i][j][1]); paulo@89: } paulo@89: } paulo@89: return data; paulo@89: } paulo@89: stack.values = function(x) { paulo@89: if (!arguments.length) return values; paulo@89: values = x; paulo@89: return stack; paulo@89: }; paulo@89: stack.order = function(x) { paulo@89: if (!arguments.length) return order; paulo@89: order = typeof x === "function" ? x : d3_layout_stackOrders.get(x) || d3_layout_stackOrderDefault; paulo@89: return stack; paulo@89: }; paulo@89: stack.offset = function(x) { paulo@89: if (!arguments.length) return offset; paulo@89: offset = typeof x === "function" ? x : d3_layout_stackOffsets.get(x) || d3_layout_stackOffsetZero; paulo@89: return stack; paulo@89: }; paulo@89: stack.x = function(z) { paulo@89: if (!arguments.length) return x; paulo@89: x = z; paulo@89: return stack; paulo@89: }; paulo@89: stack.y = function(z) { paulo@89: if (!arguments.length) return y; paulo@89: y = z; paulo@89: return stack; paulo@89: }; paulo@89: stack.out = function(z) { paulo@89: if (!arguments.length) return out; paulo@89: out = z; paulo@89: return stack; paulo@89: }; paulo@89: return stack; paulo@89: }; paulo@89: function d3_layout_stackX(d) { paulo@89: return d.x; paulo@89: } paulo@89: function d3_layout_stackY(d) { paulo@89: return d.y; paulo@89: } paulo@89: function d3_layout_stackOut(d, y0, y) { paulo@89: d.y0 = y0; paulo@89: d.y = y; paulo@89: } paulo@89: var d3_layout_stackOrders = d3.map({ paulo@89: "inside-out": function(data) { paulo@89: var n = data.length, i, j, max = data.map(d3_layout_stackMaxIndex), sums = data.map(d3_layout_stackReduceSum), index = d3.range(n).sort(function(a, b) { paulo@89: return max[a] - max[b]; paulo@89: }), top = 0, bottom = 0, tops = [], bottoms = []; paulo@89: for (i = 0; i < n; ++i) { paulo@89: j = index[i]; paulo@89: if (top < bottom) { paulo@89: top += sums[j]; paulo@89: tops.push(j); paulo@89: } else { paulo@89: bottom += sums[j]; paulo@89: bottoms.push(j); paulo@89: } paulo@89: } paulo@89: return bottoms.reverse().concat(tops); paulo@89: }, paulo@89: reverse: function(data) { paulo@89: return d3.range(data.length).reverse(); paulo@89: }, paulo@89: "default": d3_layout_stackOrderDefault paulo@89: }); paulo@89: var d3_layout_stackOffsets = d3.map({ paulo@89: silhouette: function(data) { paulo@89: var n = data.length, m = data[0].length, sums = [], max = 0, i, j, o, y0 = []; paulo@89: for (j = 0; j < m; ++j) { paulo@89: for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; paulo@89: if (o > max) max = o; paulo@89: sums.push(o); paulo@89: } paulo@89: for (j = 0; j < m; ++j) { paulo@89: y0[j] = (max - sums[j]) / 2; paulo@89: } paulo@89: return y0; paulo@89: }, paulo@89: wiggle: function(data) { paulo@89: var n = data.length, x = data[0], m = x.length, i, j, k, s1, s2, s3, dx, o, o0, y0 = []; paulo@89: y0[0] = o = o0 = 0; paulo@89: for (j = 1; j < m; ++j) { paulo@89: for (i = 0, s1 = 0; i < n; ++i) s1 += data[i][j][1]; paulo@89: for (i = 0, s2 = 0, dx = x[j][0] - x[j - 1][0]; i < n; ++i) { paulo@89: for (k = 0, s3 = (data[i][j][1] - data[i][j - 1][1]) / (2 * dx); k < i; ++k) { paulo@89: s3 += (data[k][j][1] - data[k][j - 1][1]) / dx; paulo@89: } paulo@89: s2 += s3 * data[i][j][1]; paulo@89: } paulo@89: y0[j] = o -= s1 ? s2 / s1 * dx : 0; paulo@89: if (o < o0) o0 = o; paulo@89: } paulo@89: for (j = 0; j < m; ++j) y0[j] -= o0; paulo@89: return y0; paulo@89: }, paulo@89: expand: function(data) { paulo@89: var n = data.length, m = data[0].length, k = 1 / n, i, j, o, y0 = []; paulo@89: for (j = 0; j < m; ++j) { paulo@89: for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; paulo@89: if (o) for (i = 0; i < n; i++) data[i][j][1] /= o; else for (i = 0; i < n; i++) data[i][j][1] = k; paulo@89: } paulo@89: for (j = 0; j < m; ++j) y0[j] = 0; paulo@89: return y0; paulo@89: }, paulo@89: zero: d3_layout_stackOffsetZero paulo@89: }); paulo@89: function d3_layout_stackOrderDefault(data) { paulo@89: return d3.range(data.length); paulo@89: } paulo@89: function d3_layout_stackOffsetZero(data) { paulo@89: var j = -1, m = data[0].length, y0 = []; paulo@89: while (++j < m) y0[j] = 0; paulo@89: return y0; paulo@89: } paulo@89: function d3_layout_stackMaxIndex(array) { paulo@89: var i = 1, j = 0, v = array[0][1], k, n = array.length; paulo@89: for (;i < n; ++i) { paulo@89: if ((k = array[i][1]) > v) { paulo@89: j = i; paulo@89: v = k; paulo@89: } paulo@89: } paulo@89: return j; paulo@89: } paulo@89: function d3_layout_stackReduceSum(d) { paulo@89: return d.reduce(d3_layout_stackSum, 0); paulo@89: } paulo@89: function d3_layout_stackSum(p, d) { paulo@89: return p + d[1]; paulo@89: } paulo@89: d3.layout.histogram = function() { paulo@89: var frequency = true, valuer = Number, ranger = d3_layout_histogramRange, binner = d3_layout_histogramBinSturges; paulo@89: function histogram(data, i) { paulo@89: var bins = [], values = data.map(valuer, this), range = ranger.call(this, values, i), thresholds = binner.call(this, range, values, i), bin, i = -1, n = values.length, m = thresholds.length - 1, k = frequency ? 1 : 1 / n, x; paulo@89: while (++i < m) { paulo@89: bin = bins[i] = []; paulo@89: bin.dx = thresholds[i + 1] - (bin.x = thresholds[i]); paulo@89: bin.y = 0; paulo@89: } paulo@89: if (m > 0) { paulo@89: i = -1; paulo@89: while (++i < n) { paulo@89: x = values[i]; paulo@89: if (x >= range[0] && x <= range[1]) { paulo@89: bin = bins[d3.bisect(thresholds, x, 1, m) - 1]; paulo@89: bin.y += k; paulo@89: bin.push(data[i]); paulo@89: } paulo@89: } paulo@89: } paulo@89: return bins; paulo@89: } paulo@89: histogram.value = function(x) { paulo@89: if (!arguments.length) return valuer; paulo@89: valuer = x; paulo@89: return histogram; paulo@89: }; paulo@89: histogram.range = function(x) { paulo@89: if (!arguments.length) return ranger; paulo@89: ranger = d3_functor(x); paulo@89: return histogram; paulo@89: }; paulo@89: histogram.bins = function(x) { paulo@89: if (!arguments.length) return binner; paulo@89: binner = typeof x === "number" ? function(range) { paulo@89: return d3_layout_histogramBinFixed(range, x); paulo@89: } : d3_functor(x); paulo@89: return histogram; paulo@89: }; paulo@89: histogram.frequency = function(x) { paulo@89: if (!arguments.length) return frequency; paulo@89: frequency = !!x; paulo@89: return histogram; paulo@89: }; paulo@89: return histogram; paulo@89: }; paulo@89: function d3_layout_histogramBinSturges(range, values) { paulo@89: return d3_layout_histogramBinFixed(range, Math.ceil(Math.log(values.length) / Math.LN2 + 1)); paulo@89: } paulo@89: function d3_layout_histogramBinFixed(range, n) { paulo@89: var x = -1, b = +range[0], m = (range[1] - b) / n, f = []; paulo@89: while (++x <= n) f[x] = m * x + b; paulo@89: return f; paulo@89: } paulo@89: function d3_layout_histogramRange(values) { paulo@89: return [ d3.min(values), d3.max(values) ]; paulo@89: } paulo@89: d3.layout.pack = function() { paulo@89: var hierarchy = d3.layout.hierarchy().sort(d3_layout_packSort), padding = 0, size = [ 1, 1 ], radius; paulo@89: function pack(d, i) { paulo@89: var nodes = hierarchy.call(this, d, i), root = nodes[0], w = size[0], h = size[1], r = radius == null ? Math.sqrt : typeof radius === "function" ? radius : function() { paulo@89: return radius; paulo@89: }; paulo@89: root.x = root.y = 0; paulo@89: d3_layout_hierarchyVisitAfter(root, function(d) { paulo@89: d.r = +r(d.value); paulo@89: }); paulo@89: d3_layout_hierarchyVisitAfter(root, d3_layout_packSiblings); paulo@89: if (padding) { paulo@89: var dr = padding * (radius ? 1 : Math.max(2 * root.r / w, 2 * root.r / h)) / 2; paulo@89: d3_layout_hierarchyVisitAfter(root, function(d) { paulo@89: d.r += dr; paulo@89: }); paulo@89: d3_layout_hierarchyVisitAfter(root, d3_layout_packSiblings); paulo@89: d3_layout_hierarchyVisitAfter(root, function(d) { paulo@89: d.r -= dr; paulo@89: }); paulo@89: } paulo@89: d3_layout_packTransform(root, w / 2, h / 2, radius ? 1 : 1 / Math.max(2 * root.r / w, 2 * root.r / h)); paulo@89: return nodes; paulo@89: } paulo@89: pack.size = function(_) { paulo@89: if (!arguments.length) return size; paulo@89: size = _; paulo@89: return pack; paulo@89: }; paulo@89: pack.radius = function(_) { paulo@89: if (!arguments.length) return radius; paulo@89: radius = _ == null || typeof _ === "function" ? _ : +_; paulo@89: return pack; paulo@89: }; paulo@89: pack.padding = function(_) { paulo@89: if (!arguments.length) return padding; paulo@89: padding = +_; paulo@89: return pack; paulo@89: }; paulo@89: return d3_layout_hierarchyRebind(pack, hierarchy); paulo@89: }; paulo@89: function d3_layout_packSort(a, b) { paulo@89: return a.value - b.value; paulo@89: } paulo@89: function d3_layout_packInsert(a, b) { paulo@89: var c = a._pack_next; paulo@89: a._pack_next = b; paulo@89: b._pack_prev = a; paulo@89: b._pack_next = c; paulo@89: c._pack_prev = b; paulo@89: } paulo@89: function d3_layout_packSplice(a, b) { paulo@89: a._pack_next = b; paulo@89: b._pack_prev = a; paulo@89: } paulo@89: function d3_layout_packIntersects(a, b) { paulo@89: var dx = b.x - a.x, dy = b.y - a.y, dr = a.r + b.r; paulo@89: return .999 * dr * dr > dx * dx + dy * dy; paulo@89: } paulo@89: function d3_layout_packSiblings(node) { paulo@89: if (!(nodes = node.children) || !(n = nodes.length)) return; paulo@89: var nodes, xMin = Infinity, xMax = -Infinity, yMin = Infinity, yMax = -Infinity, a, b, c, i, j, k, n; paulo@89: function bound(node) { paulo@89: xMin = Math.min(node.x - node.r, xMin); paulo@89: xMax = Math.max(node.x + node.r, xMax); paulo@89: yMin = Math.min(node.y - node.r, yMin); paulo@89: yMax = Math.max(node.y + node.r, yMax); paulo@89: } paulo@89: nodes.forEach(d3_layout_packLink); paulo@89: a = nodes[0]; paulo@89: a.x = -a.r; paulo@89: a.y = 0; paulo@89: bound(a); paulo@89: if (n > 1) { paulo@89: b = nodes[1]; paulo@89: b.x = b.r; paulo@89: b.y = 0; paulo@89: bound(b); paulo@89: if (n > 2) { paulo@89: c = nodes[2]; paulo@89: d3_layout_packPlace(a, b, c); paulo@89: bound(c); paulo@89: d3_layout_packInsert(a, c); paulo@89: a._pack_prev = c; paulo@89: d3_layout_packInsert(c, b); paulo@89: b = a._pack_next; paulo@89: for (i = 3; i < n; i++) { paulo@89: d3_layout_packPlace(a, b, c = nodes[i]); paulo@89: var isect = 0, s1 = 1, s2 = 1; paulo@89: for (j = b._pack_next; j !== b; j = j._pack_next, s1++) { paulo@89: if (d3_layout_packIntersects(j, c)) { paulo@89: isect = 1; paulo@89: break; paulo@89: } paulo@89: } paulo@89: if (isect == 1) { paulo@89: for (k = a._pack_prev; k !== j._pack_prev; k = k._pack_prev, s2++) { paulo@89: if (d3_layout_packIntersects(k, c)) { paulo@89: break; paulo@89: } paulo@89: } paulo@89: } paulo@89: if (isect) { paulo@89: if (s1 < s2 || s1 == s2 && b.r < a.r) d3_layout_packSplice(a, b = j); else d3_layout_packSplice(a = k, b); paulo@89: i--; paulo@89: } else { paulo@89: d3_layout_packInsert(a, c); paulo@89: b = c; paulo@89: bound(c); paulo@89: } paulo@89: } paulo@89: } paulo@89: } paulo@89: var cx = (xMin + xMax) / 2, cy = (yMin + yMax) / 2, cr = 0; paulo@89: for (i = 0; i < n; i++) { paulo@89: c = nodes[i]; paulo@89: c.x -= cx; paulo@89: c.y -= cy; paulo@89: cr = Math.max(cr, c.r + Math.sqrt(c.x * c.x + c.y * c.y)); paulo@89: } paulo@89: node.r = cr; paulo@89: nodes.forEach(d3_layout_packUnlink); paulo@89: } paulo@89: function d3_layout_packLink(node) { paulo@89: node._pack_next = node._pack_prev = node; paulo@89: } paulo@89: function d3_layout_packUnlink(node) { paulo@89: delete node._pack_next; paulo@89: delete node._pack_prev; paulo@89: } paulo@89: function d3_layout_packTransform(node, x, y, k) { paulo@89: var children = node.children; paulo@89: node.x = x += k * node.x; paulo@89: node.y = y += k * node.y; paulo@89: node.r *= k; paulo@89: if (children) { paulo@89: var i = -1, n = children.length; paulo@89: while (++i < n) d3_layout_packTransform(children[i], x, y, k); paulo@89: } paulo@89: } paulo@89: function d3_layout_packPlace(a, b, c) { paulo@89: var db = a.r + c.r, dx = b.x - a.x, dy = b.y - a.y; paulo@89: if (db && (dx || dy)) { paulo@89: var da = b.r + c.r, dc = dx * dx + dy * dy; paulo@89: da *= da; paulo@89: db *= db; paulo@89: var x = .5 + (db - da) / (2 * dc), y = Math.sqrt(Math.max(0, 2 * da * (db + dc) - (db -= dc) * db - da * da)) / (2 * dc); paulo@89: c.x = a.x + x * dx + y * dy; paulo@89: c.y = a.y + x * dy - y * dx; paulo@89: } else { paulo@89: c.x = a.x + db; paulo@89: c.y = a.y; paulo@89: } paulo@89: } paulo@89: d3.layout.tree = function() { paulo@89: var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = null; paulo@89: function tree(d, i) { paulo@89: var nodes = hierarchy.call(this, d, i), root0 = nodes[0], root1 = wrapTree(root0); paulo@89: d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.m = -root1.z; paulo@89: d3_layout_hierarchyVisitBefore(root1, secondWalk); paulo@89: if (nodeSize) d3_layout_hierarchyVisitBefore(root0, sizeNode); else { paulo@89: var left = root0, right = root0, bottom = root0; paulo@89: d3_layout_hierarchyVisitBefore(root0, function(node) { paulo@89: if (node.x < left.x) left = node; paulo@89: if (node.x > right.x) right = node; paulo@89: if (node.depth > bottom.depth) bottom = node; paulo@89: }); paulo@89: var tx = separation(left, right) / 2 - left.x, kx = size[0] / (right.x + separation(right, left) / 2 + tx), ky = size[1] / (bottom.depth || 1); paulo@89: d3_layout_hierarchyVisitBefore(root0, function(node) { paulo@89: node.x = (node.x + tx) * kx; paulo@89: node.y = node.depth * ky; paulo@89: }); paulo@89: } paulo@89: return nodes; paulo@89: } paulo@89: function wrapTree(root0) { paulo@89: var root1 = { paulo@89: A: null, paulo@89: children: [ root0 ] paulo@89: }, queue = [ root1 ], node1; paulo@89: while ((node1 = queue.pop()) != null) { paulo@89: for (var children = node1.children, child, i = 0, n = children.length; i < n; ++i) { paulo@89: queue.push((children[i] = child = { paulo@89: _: children[i], paulo@89: parent: node1, paulo@89: children: (child = children[i].children) && child.slice() || [], paulo@89: A: null, paulo@89: a: null, paulo@89: z: 0, paulo@89: m: 0, paulo@89: c: 0, paulo@89: s: 0, paulo@89: t: null, paulo@89: i: i paulo@89: }).a = child); paulo@89: } paulo@89: } paulo@89: return root1.children[0]; paulo@89: } paulo@89: function firstWalk(v) { paulo@89: var children = v.children, siblings = v.parent.children, w = v.i ? siblings[v.i - 1] : null; paulo@89: if (children.length) { paulo@89: d3_layout_treeShift(v); paulo@89: var midpoint = (children[0].z + children[children.length - 1].z) / 2; paulo@89: if (w) { paulo@89: v.z = w.z + separation(v._, w._); paulo@89: v.m = v.z - midpoint; paulo@89: } else { paulo@89: v.z = midpoint; paulo@89: } paulo@89: } else if (w) { paulo@89: v.z = w.z + separation(v._, w._); paulo@89: } paulo@89: v.parent.A = apportion(v, w, v.parent.A || siblings[0]); paulo@89: } paulo@89: function secondWalk(v) { paulo@89: v._.x = v.z + v.parent.m; paulo@89: v.m += v.parent.m; paulo@89: } paulo@89: function apportion(v, w, ancestor) { paulo@89: if (w) { paulo@89: var vip = v, vop = v, vim = w, vom = vip.parent.children[0], sip = vip.m, sop = vop.m, sim = vim.m, som = vom.m, shift; paulo@89: while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) { paulo@89: vom = d3_layout_treeLeft(vom); paulo@89: vop = d3_layout_treeRight(vop); paulo@89: vop.a = v; paulo@89: shift = vim.z + sim - vip.z - sip + separation(vim._, vip._); paulo@89: if (shift > 0) { paulo@89: d3_layout_treeMove(d3_layout_treeAncestor(vim, v, ancestor), v, shift); paulo@89: sip += shift; paulo@89: sop += shift; paulo@89: } paulo@89: sim += vim.m; paulo@89: sip += vip.m; paulo@89: som += vom.m; paulo@89: sop += vop.m; paulo@89: } paulo@89: if (vim && !d3_layout_treeRight(vop)) { paulo@89: vop.t = vim; paulo@89: vop.m += sim - sop; paulo@89: } paulo@89: if (vip && !d3_layout_treeLeft(vom)) { paulo@89: vom.t = vip; paulo@89: vom.m += sip - som; paulo@89: ancestor = v; paulo@89: } paulo@89: } paulo@89: return ancestor; paulo@89: } paulo@89: function sizeNode(node) { paulo@89: node.x *= size[0]; paulo@89: node.y = node.depth * size[1]; paulo@89: } paulo@89: tree.separation = function(x) { paulo@89: if (!arguments.length) return separation; paulo@89: separation = x; paulo@89: return tree; paulo@89: }; paulo@89: tree.size = function(x) { paulo@89: if (!arguments.length) return nodeSize ? null : size; paulo@89: nodeSize = (size = x) == null ? sizeNode : null; paulo@89: return tree; paulo@89: }; paulo@89: tree.nodeSize = function(x) { paulo@89: if (!arguments.length) return nodeSize ? size : null; paulo@89: nodeSize = (size = x) == null ? null : sizeNode; paulo@89: return tree; paulo@89: }; paulo@89: return d3_layout_hierarchyRebind(tree, hierarchy); paulo@89: }; paulo@89: function d3_layout_treeSeparation(a, b) { paulo@89: return a.parent == b.parent ? 1 : 2; paulo@89: } paulo@89: function d3_layout_treeLeft(v) { paulo@89: var children = v.children; paulo@89: return children.length ? children[0] : v.t; paulo@89: } paulo@89: function d3_layout_treeRight(v) { paulo@89: var children = v.children, n; paulo@89: return (n = children.length) ? children[n - 1] : v.t; paulo@89: } paulo@89: function d3_layout_treeMove(wm, wp, shift) { paulo@89: var change = shift / (wp.i - wm.i); paulo@89: wp.c -= change; paulo@89: wp.s += shift; paulo@89: wm.c += change; paulo@89: wp.z += shift; paulo@89: wp.m += shift; paulo@89: } paulo@89: function d3_layout_treeShift(v) { paulo@89: var shift = 0, change = 0, children = v.children, i = children.length, w; paulo@89: while (--i >= 0) { paulo@89: w = children[i]; paulo@89: w.z += shift; paulo@89: w.m += shift; paulo@89: shift += w.s + (change += w.c); paulo@89: } paulo@89: } paulo@89: function d3_layout_treeAncestor(vim, v, ancestor) { paulo@89: return vim.a.parent === v.parent ? vim.a : ancestor; paulo@89: } paulo@89: d3.layout.cluster = function() { paulo@89: var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false; paulo@89: function cluster(d, i) { paulo@89: var nodes = hierarchy.call(this, d, i), root = nodes[0], previousNode, x = 0; paulo@89: d3_layout_hierarchyVisitAfter(root, function(node) { paulo@89: var children = node.children; paulo@89: if (children && children.length) { paulo@89: node.x = d3_layout_clusterX(children); paulo@89: node.y = d3_layout_clusterY(children); paulo@89: } else { paulo@89: node.x = previousNode ? x += separation(node, previousNode) : 0; paulo@89: node.y = 0; paulo@89: previousNode = node; paulo@89: } paulo@89: }); paulo@89: var left = d3_layout_clusterLeft(root), right = d3_layout_clusterRight(root), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2; paulo@89: d3_layout_hierarchyVisitAfter(root, nodeSize ? function(node) { paulo@89: node.x = (node.x - root.x) * size[0]; paulo@89: node.y = (root.y - node.y) * size[1]; paulo@89: } : function(node) { paulo@89: node.x = (node.x - x0) / (x1 - x0) * size[0]; paulo@89: node.y = (1 - (root.y ? node.y / root.y : 1)) * size[1]; paulo@89: }); paulo@89: return nodes; paulo@89: } paulo@89: cluster.separation = function(x) { paulo@89: if (!arguments.length) return separation; paulo@89: separation = x; paulo@89: return cluster; paulo@89: }; paulo@89: cluster.size = function(x) { paulo@89: if (!arguments.length) return nodeSize ? null : size; paulo@89: nodeSize = (size = x) == null; paulo@89: return cluster; paulo@89: }; paulo@89: cluster.nodeSize = function(x) { paulo@89: if (!arguments.length) return nodeSize ? size : null; paulo@89: nodeSize = (size = x) != null; paulo@89: return cluster; paulo@89: }; paulo@89: return d3_layout_hierarchyRebind(cluster, hierarchy); paulo@89: }; paulo@89: function d3_layout_clusterY(children) { paulo@89: return 1 + d3.max(children, function(child) { paulo@89: return child.y; paulo@89: }); paulo@89: } paulo@89: function d3_layout_clusterX(children) { paulo@89: return children.reduce(function(x, child) { paulo@89: return x + child.x; paulo@89: }, 0) / children.length; paulo@89: } paulo@89: function d3_layout_clusterLeft(node) { paulo@89: var children = node.children; paulo@89: return children && children.length ? d3_layout_clusterLeft(children[0]) : node; paulo@89: } paulo@89: function d3_layout_clusterRight(node) { paulo@89: var children = node.children, n; paulo@89: return children && (n = children.length) ? d3_layout_clusterRight(children[n - 1]) : node; paulo@89: } paulo@89: d3.layout.treemap = function() { paulo@89: var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [ 1, 1 ], padding = null, pad = d3_layout_treemapPadNull, sticky = false, stickies, mode = "squarify", ratio = .5 * (1 + Math.sqrt(5)); paulo@89: function scale(children, k) { paulo@89: var i = -1, n = children.length, child, area; paulo@89: while (++i < n) { paulo@89: area = (child = children[i]).value * (k < 0 ? 0 : k); paulo@89: child.area = isNaN(area) || area <= 0 ? 0 : area; paulo@89: } paulo@89: } paulo@89: function squarify(node) { paulo@89: var children = node.children; paulo@89: if (children && children.length) { paulo@89: var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = mode === "slice" ? rect.dx : mode === "dice" ? rect.dy : mode === "slice-dice" ? node.depth & 1 ? rect.dy : rect.dx : Math.min(rect.dx, rect.dy), n; paulo@89: scale(remaining, rect.dx * rect.dy / node.value); paulo@89: row.area = 0; paulo@89: while ((n = remaining.length) > 0) { paulo@89: row.push(child = remaining[n - 1]); paulo@89: row.area += child.area; paulo@89: if (mode !== "squarify" || (score = worst(row, u)) <= best) { paulo@89: remaining.pop(); paulo@89: best = score; paulo@89: } else { paulo@89: row.area -= row.pop().area; paulo@89: position(row, u, rect, false); paulo@89: u = Math.min(rect.dx, rect.dy); paulo@89: row.length = row.area = 0; paulo@89: best = Infinity; paulo@89: } paulo@89: } paulo@89: if (row.length) { paulo@89: position(row, u, rect, true); paulo@89: row.length = row.area = 0; paulo@89: } paulo@89: children.forEach(squarify); paulo@89: } paulo@89: } paulo@89: function stickify(node) { paulo@89: var children = node.children; paulo@89: if (children && children.length) { paulo@89: var rect = pad(node), remaining = children.slice(), child, row = []; paulo@89: scale(remaining, rect.dx * rect.dy / node.value); paulo@89: row.area = 0; paulo@89: while (child = remaining.pop()) { paulo@89: row.push(child); paulo@89: row.area += child.area; paulo@89: if (child.z != null) { paulo@89: position(row, child.z ? rect.dx : rect.dy, rect, !remaining.length); paulo@89: row.length = row.area = 0; paulo@89: } paulo@89: } paulo@89: children.forEach(stickify); paulo@89: } paulo@89: } paulo@89: function worst(row, u) { paulo@89: var s = row.area, r, rmax = 0, rmin = Infinity, i = -1, n = row.length; paulo@89: while (++i < n) { paulo@89: if (!(r = row[i].area)) continue; paulo@89: if (r < rmin) rmin = r; paulo@89: if (r > rmax) rmax = r; paulo@89: } paulo@89: s *= s; paulo@89: u *= u; paulo@89: return s ? Math.max(u * rmax * ratio / s, s / (u * rmin * ratio)) : Infinity; paulo@89: } paulo@89: function position(row, u, rect, flush) { paulo@89: var i = -1, n = row.length, x = rect.x, y = rect.y, v = u ? round(row.area / u) : 0, o; paulo@89: if (u == rect.dx) { paulo@89: if (flush || v > rect.dy) v = rect.dy; paulo@89: while (++i < n) { paulo@89: o = row[i]; paulo@89: o.x = x; paulo@89: o.y = y; paulo@89: o.dy = v; paulo@89: x += o.dx = Math.min(rect.x + rect.dx - x, v ? round(o.area / v) : 0); paulo@89: } paulo@89: o.z = true; paulo@89: o.dx += rect.x + rect.dx - x; paulo@89: rect.y += v; paulo@89: rect.dy -= v; paulo@89: } else { paulo@89: if (flush || v > rect.dx) v = rect.dx; paulo@89: while (++i < n) { paulo@89: o = row[i]; paulo@89: o.x = x; paulo@89: o.y = y; paulo@89: o.dx = v; paulo@89: y += o.dy = Math.min(rect.y + rect.dy - y, v ? round(o.area / v) : 0); paulo@89: } paulo@89: o.z = false; paulo@89: o.dy += rect.y + rect.dy - y; paulo@89: rect.x += v; paulo@89: rect.dx -= v; paulo@89: } paulo@89: } paulo@89: function treemap(d) { paulo@89: var nodes = stickies || hierarchy(d), root = nodes[0]; paulo@89: root.x = root.y = 0; paulo@89: if (root.value) root.dx = size[0], root.dy = size[1]; else root.dx = root.dy = 0; paulo@89: if (stickies) hierarchy.revalue(root); paulo@89: scale([ root ], root.dx * root.dy / root.value); paulo@89: (stickies ? stickify : squarify)(root); paulo@89: if (sticky) stickies = nodes; paulo@89: return nodes; paulo@89: } paulo@89: treemap.size = function(x) { paulo@89: if (!arguments.length) return size; paulo@89: size = x; paulo@89: return treemap; paulo@89: }; paulo@89: treemap.padding = function(x) { paulo@89: if (!arguments.length) return padding; paulo@89: function padFunction(node) { paulo@89: var p = x.call(treemap, node, node.depth); paulo@89: return p == null ? d3_layout_treemapPadNull(node) : d3_layout_treemapPad(node, typeof p === "number" ? [ p, p, p, p ] : p); paulo@89: } paulo@89: function padConstant(node) { paulo@89: return d3_layout_treemapPad(node, x); paulo@89: } paulo@89: var type; paulo@89: pad = (padding = x) == null ? d3_layout_treemapPadNull : (type = typeof x) === "function" ? padFunction : type === "number" ? (x = [ x, x, x, x ], paulo@89: padConstant) : padConstant; paulo@89: return treemap; paulo@89: }; paulo@89: treemap.round = function(x) { paulo@89: if (!arguments.length) return round != Number; paulo@89: round = x ? Math.round : Number; paulo@89: return treemap; paulo@89: }; paulo@89: treemap.sticky = function(x) { paulo@89: if (!arguments.length) return sticky; paulo@89: sticky = x; paulo@89: stickies = null; paulo@89: return treemap; paulo@89: }; paulo@89: treemap.ratio = function(x) { paulo@89: if (!arguments.length) return ratio; paulo@89: ratio = x; paulo@89: return treemap; paulo@89: }; paulo@89: treemap.mode = function(x) { paulo@89: if (!arguments.length) return mode; paulo@89: mode = x + ""; paulo@89: return treemap; paulo@89: }; paulo@89: return d3_layout_hierarchyRebind(treemap, hierarchy); paulo@89: }; paulo@89: function d3_layout_treemapPadNull(node) { paulo@89: return { paulo@89: x: node.x, paulo@89: y: node.y, paulo@89: dx: node.dx, paulo@89: dy: node.dy paulo@89: }; paulo@89: } paulo@89: function d3_layout_treemapPad(node, padding) { paulo@89: var x = node.x + padding[3], y = node.y + padding[0], dx = node.dx - padding[1] - padding[3], dy = node.dy - padding[0] - padding[2]; paulo@89: if (dx < 0) { paulo@89: x += dx / 2; paulo@89: dx = 0; paulo@89: } paulo@89: if (dy < 0) { paulo@89: y += dy / 2; paulo@89: dy = 0; paulo@89: } paulo@89: return { paulo@89: x: x, paulo@89: y: y, paulo@89: dx: dx, paulo@89: dy: dy paulo@89: }; paulo@89: } paulo@89: d3.random = { paulo@89: normal: function(µ, σ) { paulo@89: var n = arguments.length; paulo@89: if (n < 2) σ = 1; paulo@89: if (n < 1) µ = 0; paulo@89: return function() { paulo@89: var x, y, r; paulo@89: do { paulo@89: x = Math.random() * 2 - 1; paulo@89: y = Math.random() * 2 - 1; paulo@89: r = x * x + y * y; paulo@89: } while (!r || r > 1); paulo@89: return µ + σ * x * Math.sqrt(-2 * Math.log(r) / r); paulo@89: }; paulo@89: }, paulo@89: logNormal: function() { paulo@89: var random = d3.random.normal.apply(d3, arguments); paulo@89: return function() { paulo@89: return Math.exp(random()); paulo@89: }; paulo@89: }, paulo@89: bates: function(m) { paulo@89: var random = d3.random.irwinHall(m); paulo@89: return function() { paulo@89: return random() / m; paulo@89: }; paulo@89: }, paulo@89: irwinHall: function(m) { paulo@89: return function() { paulo@89: for (var s = 0, j = 0; j < m; j++) s += Math.random(); paulo@89: return s; paulo@89: }; paulo@89: } paulo@89: }; paulo@89: d3.scale = {}; paulo@89: function d3_scaleExtent(domain) { paulo@89: var start = domain[0], stop = domain[domain.length - 1]; paulo@89: return start < stop ? [ start, stop ] : [ stop, start ]; paulo@89: } paulo@89: function d3_scaleRange(scale) { paulo@89: return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range()); paulo@89: } paulo@89: function d3_scale_bilinear(domain, range, uninterpolate, interpolate) { paulo@89: var u = uninterpolate(domain[0], domain[1]), i = interpolate(range[0], range[1]); paulo@89: return function(x) { paulo@89: return i(u(x)); paulo@89: }; paulo@89: } paulo@89: function d3_scale_nice(domain, nice) { paulo@89: var i0 = 0, i1 = domain.length - 1, x0 = domain[i0], x1 = domain[i1], dx; paulo@89: if (x1 < x0) { paulo@89: dx = i0, i0 = i1, i1 = dx; paulo@89: dx = x0, x0 = x1, x1 = dx; paulo@89: } paulo@89: domain[i0] = nice.floor(x0); paulo@89: domain[i1] = nice.ceil(x1); paulo@89: return domain; paulo@89: } paulo@89: function d3_scale_niceStep(step) { paulo@89: return step ? { paulo@89: floor: function(x) { paulo@89: return Math.floor(x / step) * step; paulo@89: }, paulo@89: ceil: function(x) { paulo@89: return Math.ceil(x / step) * step; paulo@89: } paulo@89: } : d3_scale_niceIdentity; paulo@89: } paulo@89: var d3_scale_niceIdentity = { paulo@89: floor: d3_identity, paulo@89: ceil: d3_identity paulo@89: }; paulo@89: function d3_scale_polylinear(domain, range, uninterpolate, interpolate) { paulo@89: var u = [], i = [], j = 0, k = Math.min(domain.length, range.length) - 1; paulo@89: if (domain[k] < domain[0]) { paulo@89: domain = domain.slice().reverse(); paulo@89: range = range.slice().reverse(); paulo@89: } paulo@89: while (++j <= k) { paulo@89: u.push(uninterpolate(domain[j - 1], domain[j])); paulo@89: i.push(interpolate(range[j - 1], range[j])); paulo@89: } paulo@89: return function(x) { paulo@89: var j = d3.bisect(domain, x, 1, k) - 1; paulo@89: return i[j](u[j](x)); paulo@89: }; paulo@89: } paulo@89: d3.scale.linear = function() { paulo@89: return d3_scale_linear([ 0, 1 ], [ 0, 1 ], d3_interpolate, false); paulo@89: }; paulo@89: function d3_scale_linear(domain, range, interpolate, clamp) { paulo@89: var output, input; paulo@89: function rescale() { paulo@89: var linear = Math.min(domain.length, range.length) > 2 ? d3_scale_polylinear : d3_scale_bilinear, uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber; paulo@89: output = linear(domain, range, uninterpolate, interpolate); paulo@89: input = linear(range, domain, uninterpolate, d3_interpolate); paulo@89: return scale; paulo@89: } paulo@89: function scale(x) { paulo@89: return output(x); paulo@89: } paulo@89: scale.invert = function(y) { paulo@89: return input(y); paulo@89: }; paulo@89: scale.domain = function(x) { paulo@89: if (!arguments.length) return domain; paulo@89: domain = x.map(Number); paulo@89: return rescale(); paulo@89: }; paulo@89: scale.range = function(x) { paulo@89: if (!arguments.length) return range; paulo@89: range = x; paulo@89: return rescale(); paulo@89: }; paulo@89: scale.rangeRound = function(x) { paulo@89: return scale.range(x).interpolate(d3_interpolateRound); paulo@89: }; paulo@89: scale.clamp = function(x) { paulo@89: if (!arguments.length) return clamp; paulo@89: clamp = x; paulo@89: return rescale(); paulo@89: }; paulo@89: scale.interpolate = function(x) { paulo@89: if (!arguments.length) return interpolate; paulo@89: interpolate = x; paulo@89: return rescale(); paulo@89: }; paulo@89: scale.ticks = function(m) { paulo@89: return d3_scale_linearTicks(domain, m); paulo@89: }; paulo@89: scale.tickFormat = function(m, format) { paulo@89: return d3_scale_linearTickFormat(domain, m, format); paulo@89: }; paulo@89: scale.nice = function(m) { paulo@89: d3_scale_linearNice(domain, m); paulo@89: return rescale(); paulo@89: }; paulo@89: scale.copy = function() { paulo@89: return d3_scale_linear(domain, range, interpolate, clamp); paulo@89: }; paulo@89: return rescale(); paulo@89: } paulo@89: function d3_scale_linearRebind(scale, linear) { paulo@89: return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp"); paulo@89: } paulo@89: function d3_scale_linearNice(domain, m) { paulo@89: d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2])); paulo@89: d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2])); paulo@89: return domain; paulo@89: } paulo@89: function d3_scale_linearTickRange(domain, m) { paulo@89: if (m == null) m = 10; paulo@89: var extent = d3_scaleExtent(domain), span = extent[1] - extent[0], step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)), err = m / span * step; paulo@89: if (err <= .15) step *= 10; else if (err <= .35) step *= 5; else if (err <= .75) step *= 2; paulo@89: extent[0] = Math.ceil(extent[0] / step) * step; paulo@89: extent[1] = Math.floor(extent[1] / step) * step + step * .5; paulo@89: extent[2] = step; paulo@89: return extent; paulo@89: } paulo@89: function d3_scale_linearTicks(domain, m) { paulo@89: return d3.range.apply(d3, d3_scale_linearTickRange(domain, m)); paulo@89: } paulo@89: function d3_scale_linearTickFormat(domain, m, format) { paulo@89: var range = d3_scale_linearTickRange(domain, m); paulo@89: if (format) { paulo@89: var match = d3_format_re.exec(format); paulo@89: match.shift(); paulo@89: if (match[8] === "s") { paulo@89: var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1]))); paulo@89: if (!match[7]) match[7] = "." + d3_scale_linearPrecision(prefix.scale(range[2])); paulo@89: match[8] = "f"; paulo@89: format = d3.format(match.join("")); paulo@89: return function(d) { paulo@89: return format(prefix.scale(d)) + prefix.symbol; paulo@89: }; paulo@89: } paulo@89: if (!match[7]) match[7] = "." + d3_scale_linearFormatPrecision(match[8], range); paulo@89: format = match.join(""); paulo@89: } else { paulo@89: format = ",." + d3_scale_linearPrecision(range[2]) + "f"; paulo@89: } paulo@89: return d3.format(format); paulo@89: } paulo@89: var d3_scale_linearFormatSignificant = { paulo@89: s: 1, paulo@89: g: 1, paulo@89: p: 1, paulo@89: r: 1, paulo@89: e: 1 paulo@89: }; paulo@89: function d3_scale_linearPrecision(value) { paulo@89: return -Math.floor(Math.log(value) / Math.LN10 + .01); paulo@89: } paulo@89: function d3_scale_linearFormatPrecision(type, range) { paulo@89: var p = d3_scale_linearPrecision(range[2]); paulo@89: return type in d3_scale_linearFormatSignificant ? Math.abs(p - d3_scale_linearPrecision(Math.max(abs(range[0]), abs(range[1])))) + +(type !== "e") : p - (type === "%") * 2; paulo@89: } paulo@89: d3.scale.log = function() { paulo@89: return d3_scale_log(d3.scale.linear().domain([ 0, 1 ]), 10, true, [ 1, 10 ]); paulo@89: }; paulo@89: function d3_scale_log(linear, base, positive, domain) { paulo@89: function log(x) { paulo@89: return (positive ? Math.log(x < 0 ? 0 : x) : -Math.log(x > 0 ? 0 : -x)) / Math.log(base); paulo@89: } paulo@89: function pow(x) { paulo@89: return positive ? Math.pow(base, x) : -Math.pow(base, -x); paulo@89: } paulo@89: function scale(x) { paulo@89: return linear(log(x)); paulo@89: } paulo@89: scale.invert = function(x) { paulo@89: return pow(linear.invert(x)); paulo@89: }; paulo@89: scale.domain = function(x) { paulo@89: if (!arguments.length) return domain; paulo@89: positive = x[0] >= 0; paulo@89: linear.domain((domain = x.map(Number)).map(log)); paulo@89: return scale; paulo@89: }; paulo@89: scale.base = function(_) { paulo@89: if (!arguments.length) return base; paulo@89: base = +_; paulo@89: linear.domain(domain.map(log)); paulo@89: return scale; paulo@89: }; paulo@89: scale.nice = function() { paulo@89: var niced = d3_scale_nice(domain.map(log), positive ? Math : d3_scale_logNiceNegative); paulo@89: linear.domain(niced); paulo@89: domain = niced.map(pow); paulo@89: return scale; paulo@89: }; paulo@89: scale.ticks = function() { paulo@89: var extent = d3_scaleExtent(domain), ticks = [], u = extent[0], v = extent[1], i = Math.floor(log(u)), j = Math.ceil(log(v)), n = base % 1 ? 2 : base; paulo@89: if (isFinite(j - i)) { paulo@89: if (positive) { paulo@89: for (;i < j; i++) for (var k = 1; k < n; k++) ticks.push(pow(i) * k); paulo@89: ticks.push(pow(i)); paulo@89: } else { paulo@89: ticks.push(pow(i)); paulo@89: for (;i++ < j; ) for (var k = n - 1; k > 0; k--) ticks.push(pow(i) * k); paulo@89: } paulo@89: for (i = 0; ticks[i] < u; i++) {} paulo@89: for (j = ticks.length; ticks[j - 1] > v; j--) {} paulo@89: ticks = ticks.slice(i, j); paulo@89: } paulo@89: return ticks; paulo@89: }; paulo@89: scale.tickFormat = function(n, format) { paulo@89: if (!arguments.length) return d3_scale_logFormat; paulo@89: if (arguments.length < 2) format = d3_scale_logFormat; else if (typeof format !== "function") format = d3.format(format); paulo@89: var k = Math.max(1, base * n / scale.ticks().length); paulo@89: return function(d) { paulo@89: var i = d / pow(Math.round(log(d))); paulo@89: if (i * base < base - .5) i *= base; paulo@89: return i <= k ? format(d) : ""; paulo@89: }; paulo@89: }; paulo@89: scale.copy = function() { paulo@89: return d3_scale_log(linear.copy(), base, positive, domain); paulo@89: }; paulo@89: return d3_scale_linearRebind(scale, linear); paulo@89: } paulo@89: var d3_scale_logFormat = d3.format(".0e"), d3_scale_logNiceNegative = { paulo@89: floor: function(x) { paulo@89: return -Math.ceil(-x); paulo@89: }, paulo@89: ceil: function(x) { paulo@89: return -Math.floor(-x); paulo@89: } paulo@89: }; paulo@89: d3.scale.pow = function() { paulo@89: return d3_scale_pow(d3.scale.linear(), 1, [ 0, 1 ]); paulo@89: }; paulo@89: function d3_scale_pow(linear, exponent, domain) { paulo@89: var powp = d3_scale_powPow(exponent), powb = d3_scale_powPow(1 / exponent); paulo@89: function scale(x) { paulo@89: return linear(powp(x)); paulo@89: } paulo@89: scale.invert = function(x) { paulo@89: return powb(linear.invert(x)); paulo@89: }; paulo@89: scale.domain = function(x) { paulo@89: if (!arguments.length) return domain; paulo@89: linear.domain((domain = x.map(Number)).map(powp)); paulo@89: return scale; paulo@89: }; paulo@89: scale.ticks = function(m) { paulo@89: return d3_scale_linearTicks(domain, m); paulo@89: }; paulo@89: scale.tickFormat = function(m, format) { paulo@89: return d3_scale_linearTickFormat(domain, m, format); paulo@89: }; paulo@89: scale.nice = function(m) { paulo@89: return scale.domain(d3_scale_linearNice(domain, m)); paulo@89: }; paulo@89: scale.exponent = function(x) { paulo@89: if (!arguments.length) return exponent; paulo@89: powp = d3_scale_powPow(exponent = x); paulo@89: powb = d3_scale_powPow(1 / exponent); paulo@89: linear.domain(domain.map(powp)); paulo@89: return scale; paulo@89: }; paulo@89: scale.copy = function() { paulo@89: return d3_scale_pow(linear.copy(), exponent, domain); paulo@89: }; paulo@89: return d3_scale_linearRebind(scale, linear); paulo@89: } paulo@89: function d3_scale_powPow(e) { paulo@89: return function(x) { paulo@89: return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e); paulo@89: }; paulo@89: } paulo@89: d3.scale.sqrt = function() { paulo@89: return d3.scale.pow().exponent(.5); paulo@89: }; paulo@89: d3.scale.ordinal = function() { paulo@89: return d3_scale_ordinal([], { paulo@89: t: "range", paulo@89: a: [ [] ] paulo@89: }); paulo@89: }; paulo@89: function d3_scale_ordinal(domain, ranger) { paulo@89: var index, range, rangeBand; paulo@89: function scale(x) { paulo@89: return range[((index.get(x) || (ranger.t === "range" ? index.set(x, domain.push(x)) : NaN)) - 1) % range.length]; paulo@89: } paulo@89: function steps(start, step) { paulo@89: return d3.range(domain.length).map(function(i) { paulo@89: return start + step * i; paulo@89: }); paulo@89: } paulo@89: scale.domain = function(x) { paulo@89: if (!arguments.length) return domain; paulo@89: domain = []; paulo@89: index = new d3_Map(); paulo@89: var i = -1, n = x.length, xi; paulo@89: while (++i < n) if (!index.has(xi = x[i])) index.set(xi, domain.push(xi)); paulo@89: return scale[ranger.t].apply(scale, ranger.a); paulo@89: }; paulo@89: scale.range = function(x) { paulo@89: if (!arguments.length) return range; paulo@89: range = x; paulo@89: rangeBand = 0; paulo@89: ranger = { paulo@89: t: "range", paulo@89: a: arguments paulo@89: }; paulo@89: return scale; paulo@89: }; paulo@89: scale.rangePoints = function(x, padding) { paulo@89: if (arguments.length < 2) padding = 0; paulo@89: var start = x[0], stop = x[1], step = domain.length < 2 ? (start = (start + stop) / 2, paulo@89: 0) : (stop - start) / (domain.length - 1 + padding); paulo@89: range = steps(start + step * padding / 2, step); paulo@89: rangeBand = 0; paulo@89: ranger = { paulo@89: t: "rangePoints", paulo@89: a: arguments paulo@89: }; paulo@89: return scale; paulo@89: }; paulo@89: scale.rangeRoundPoints = function(x, padding) { paulo@89: if (arguments.length < 2) padding = 0; paulo@89: var start = x[0], stop = x[1], step = domain.length < 2 ? (start = stop = Math.round((start + stop) / 2), paulo@89: 0) : (stop - start) / (domain.length - 1 + padding) | 0; paulo@89: range = steps(start + Math.round(step * padding / 2 + (stop - start - (domain.length - 1 + padding) * step) / 2), step); paulo@89: rangeBand = 0; paulo@89: ranger = { paulo@89: t: "rangeRoundPoints", paulo@89: a: arguments paulo@89: }; paulo@89: return scale; paulo@89: }; paulo@89: scale.rangeBands = function(x, padding, outerPadding) { paulo@89: if (arguments.length < 2) padding = 0; paulo@89: if (arguments.length < 3) outerPadding = padding; paulo@89: var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = (stop - start) / (domain.length - padding + 2 * outerPadding); paulo@89: range = steps(start + step * outerPadding, step); paulo@89: if (reverse) range.reverse(); paulo@89: rangeBand = step * (1 - padding); paulo@89: ranger = { paulo@89: t: "rangeBands", paulo@89: a: arguments paulo@89: }; paulo@89: return scale; paulo@89: }; paulo@89: scale.rangeRoundBands = function(x, padding, outerPadding) { paulo@89: if (arguments.length < 2) padding = 0; paulo@89: if (arguments.length < 3) outerPadding = padding; paulo@89: var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = Math.floor((stop - start) / (domain.length - padding + 2 * outerPadding)); paulo@89: range = steps(start + Math.round((stop - start - (domain.length - padding) * step) / 2), step); paulo@89: if (reverse) range.reverse(); paulo@89: rangeBand = Math.round(step * (1 - padding)); paulo@89: ranger = { paulo@89: t: "rangeRoundBands", paulo@89: a: arguments paulo@89: }; paulo@89: return scale; paulo@89: }; paulo@89: scale.rangeBand = function() { paulo@89: return rangeBand; paulo@89: }; paulo@89: scale.rangeExtent = function() { paulo@89: return d3_scaleExtent(ranger.a[0]); paulo@89: }; paulo@89: scale.copy = function() { paulo@89: return d3_scale_ordinal(domain, ranger); paulo@89: }; paulo@89: return scale.domain(domain); paulo@89: } paulo@89: d3.scale.category10 = function() { paulo@89: return d3.scale.ordinal().range(d3_category10); paulo@89: }; paulo@89: d3.scale.category20 = function() { paulo@89: return d3.scale.ordinal().range(d3_category20); paulo@89: }; paulo@89: d3.scale.category20b = function() { paulo@89: return d3.scale.ordinal().range(d3_category20b); paulo@89: }; paulo@89: d3.scale.category20c = function() { paulo@89: return d3.scale.ordinal().range(d3_category20c); paulo@89: }; paulo@89: var d3_category10 = [ 2062260, 16744206, 2924588, 14034728, 9725885, 9197131, 14907330, 8355711, 12369186, 1556175 ].map(d3_rgbString); paulo@89: var d3_category20 = [ 2062260, 11454440, 16744206, 16759672, 2924588, 10018698, 14034728, 16750742, 9725885, 12955861, 9197131, 12885140, 14907330, 16234194, 8355711, 13092807, 12369186, 14408589, 1556175, 10410725 ].map(d3_rgbString); paulo@89: var d3_category20b = [ 3750777, 5395619, 7040719, 10264286, 6519097, 9216594, 11915115, 13556636, 9202993, 12426809, 15186514, 15190932, 8666169, 11356490, 14049643, 15177372, 8077683, 10834324, 13528509, 14589654 ].map(d3_rgbString); paulo@89: var d3_category20c = [ 3244733, 7057110, 10406625, 13032431, 15095053, 16616764, 16625259, 16634018, 3253076, 7652470, 10607003, 13101504, 7695281, 10394312, 12369372, 14342891, 6513507, 9868950, 12434877, 14277081 ].map(d3_rgbString); paulo@89: d3.scale.quantile = function() { paulo@89: return d3_scale_quantile([], []); paulo@89: }; paulo@89: function d3_scale_quantile(domain, range) { paulo@89: var thresholds; paulo@89: function rescale() { paulo@89: var k = 0, q = range.length; paulo@89: thresholds = []; paulo@89: while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q); paulo@89: return scale; paulo@89: } paulo@89: function scale(x) { paulo@89: if (!isNaN(x = +x)) return range[d3.bisect(thresholds, x)]; paulo@89: } paulo@89: scale.domain = function(x) { paulo@89: if (!arguments.length) return domain; paulo@89: domain = x.map(d3_number).filter(d3_numeric).sort(d3_ascending); paulo@89: return rescale(); paulo@89: }; paulo@89: scale.range = function(x) { paulo@89: if (!arguments.length) return range; paulo@89: range = x; paulo@89: return rescale(); paulo@89: }; paulo@89: scale.quantiles = function() { paulo@89: return thresholds; paulo@89: }; paulo@89: scale.invertExtent = function(y) { paulo@89: y = range.indexOf(y); paulo@89: return y < 0 ? [ NaN, NaN ] : [ y > 0 ? thresholds[y - 1] : domain[0], y < thresholds.length ? thresholds[y] : domain[domain.length - 1] ]; paulo@89: }; paulo@89: scale.copy = function() { paulo@89: return d3_scale_quantile(domain, range); paulo@89: }; paulo@89: return rescale(); paulo@89: } paulo@89: d3.scale.quantize = function() { paulo@89: return d3_scale_quantize(0, 1, [ 0, 1 ]); paulo@89: }; paulo@89: function d3_scale_quantize(x0, x1, range) { paulo@89: var kx, i; paulo@89: function scale(x) { paulo@89: return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))]; paulo@89: } paulo@89: function rescale() { paulo@89: kx = range.length / (x1 - x0); paulo@89: i = range.length - 1; paulo@89: return scale; paulo@89: } paulo@89: scale.domain = function(x) { paulo@89: if (!arguments.length) return [ x0, x1 ]; paulo@89: x0 = +x[0]; paulo@89: x1 = +x[x.length - 1]; paulo@89: return rescale(); paulo@89: }; paulo@89: scale.range = function(x) { paulo@89: if (!arguments.length) return range; paulo@89: range = x; paulo@89: return rescale(); paulo@89: }; paulo@89: scale.invertExtent = function(y) { paulo@89: y = range.indexOf(y); paulo@89: y = y < 0 ? NaN : y / kx + x0; paulo@89: return [ y, y + 1 / kx ]; paulo@89: }; paulo@89: scale.copy = function() { paulo@89: return d3_scale_quantize(x0, x1, range); paulo@89: }; paulo@89: return rescale(); paulo@89: } paulo@89: d3.scale.threshold = function() { paulo@89: return d3_scale_threshold([ .5 ], [ 0, 1 ]); paulo@89: }; paulo@89: function d3_scale_threshold(domain, range) { paulo@89: function scale(x) { paulo@89: if (x <= x) return range[d3.bisect(domain, x)]; paulo@89: } paulo@89: scale.domain = function(_) { paulo@89: if (!arguments.length) return domain; paulo@89: domain = _; paulo@89: return scale; paulo@89: }; paulo@89: scale.range = function(_) { paulo@89: if (!arguments.length) return range; paulo@89: range = _; paulo@89: return scale; paulo@89: }; paulo@89: scale.invertExtent = function(y) { paulo@89: y = range.indexOf(y); paulo@89: return [ domain[y - 1], domain[y] ]; paulo@89: }; paulo@89: scale.copy = function() { paulo@89: return d3_scale_threshold(domain, range); paulo@89: }; paulo@89: return scale; paulo@89: } paulo@89: d3.scale.identity = function() { paulo@89: return d3_scale_identity([ 0, 1 ]); paulo@89: }; paulo@89: function d3_scale_identity(domain) { paulo@89: function identity(x) { paulo@89: return +x; paulo@89: } paulo@89: identity.invert = identity; paulo@89: identity.domain = identity.range = function(x) { paulo@89: if (!arguments.length) return domain; paulo@89: domain = x.map(identity); paulo@89: return identity; paulo@89: }; paulo@89: identity.ticks = function(m) { paulo@89: return d3_scale_linearTicks(domain, m); paulo@89: }; paulo@89: identity.tickFormat = function(m, format) { paulo@89: return d3_scale_linearTickFormat(domain, m, format); paulo@89: }; paulo@89: identity.copy = function() { paulo@89: return d3_scale_identity(domain); paulo@89: }; paulo@89: return identity; paulo@89: } paulo@89: d3.svg = {}; paulo@89: function d3_zero() { paulo@89: return 0; paulo@89: } paulo@89: d3.svg.arc = function() { paulo@89: var innerRadius = d3_svg_arcInnerRadius, outerRadius = d3_svg_arcOuterRadius, cornerRadius = d3_zero, padRadius = d3_svg_arcAuto, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle, padAngle = d3_svg_arcPadAngle; paulo@89: function arc() { paulo@89: var r0 = Math.max(0, +innerRadius.apply(this, arguments)), r1 = Math.max(0, +outerRadius.apply(this, arguments)), a0 = startAngle.apply(this, arguments) - halfπ, a1 = endAngle.apply(this, arguments) - halfπ, da = Math.abs(a1 - a0), cw = a0 > a1 ? 0 : 1; paulo@89: if (r1 < r0) rc = r1, r1 = r0, r0 = rc; paulo@89: if (da >= τε) return circleSegment(r1, cw) + (r0 ? circleSegment(r0, 1 - cw) : "") + "Z"; paulo@89: var rc, cr, rp, ap, p0 = 0, p1 = 0, x0, y0, x1, y1, x2, y2, x3, y3, path = []; paulo@89: if (ap = (+padAngle.apply(this, arguments) || 0) / 2) { paulo@89: rp = padRadius === d3_svg_arcAuto ? Math.sqrt(r0 * r0 + r1 * r1) : +padRadius.apply(this, arguments); paulo@89: if (!cw) p1 *= -1; paulo@89: if (r1) p1 = d3_asin(rp / r1 * Math.sin(ap)); paulo@89: if (r0) p0 = d3_asin(rp / r0 * Math.sin(ap)); paulo@89: } paulo@89: if (r1) { paulo@89: x0 = r1 * Math.cos(a0 + p1); paulo@89: y0 = r1 * Math.sin(a0 + p1); paulo@89: x1 = r1 * Math.cos(a1 - p1); paulo@89: y1 = r1 * Math.sin(a1 - p1); paulo@89: var l1 = Math.abs(a1 - a0 - 2 * p1) <= π ? 0 : 1; paulo@89: if (p1 && d3_svg_arcSweep(x0, y0, x1, y1) === cw ^ l1) { paulo@89: var h1 = (a0 + a1) / 2; paulo@89: x0 = r1 * Math.cos(h1); paulo@89: y0 = r1 * Math.sin(h1); paulo@89: x1 = y1 = null; paulo@89: } paulo@89: } else { paulo@89: x0 = y0 = 0; paulo@89: } paulo@89: if (r0) { paulo@89: x2 = r0 * Math.cos(a1 - p0); paulo@89: y2 = r0 * Math.sin(a1 - p0); paulo@89: x3 = r0 * Math.cos(a0 + p0); paulo@89: y3 = r0 * Math.sin(a0 + p0); paulo@89: var l0 = Math.abs(a0 - a1 + 2 * p0) <= π ? 0 : 1; paulo@89: if (p0 && d3_svg_arcSweep(x2, y2, x3, y3) === 1 - cw ^ l0) { paulo@89: var h0 = (a0 + a1) / 2; paulo@89: x2 = r0 * Math.cos(h0); paulo@89: y2 = r0 * Math.sin(h0); paulo@89: x3 = y3 = null; paulo@89: } paulo@89: } else { paulo@89: x2 = y2 = 0; paulo@89: } paulo@89: if (da > ε && (rc = Math.min(Math.abs(r1 - r0) / 2, +cornerRadius.apply(this, arguments))) > .001) { paulo@89: cr = r0 < r1 ^ cw ? 0 : 1; paulo@89: var rc1 = rc, rc0 = rc; paulo@89: if (da < π) { paulo@89: var oc = x3 == null ? [ x2, y2 ] : x1 == null ? [ x0, y0 ] : d3_geom_polygonIntersect([ x0, y0 ], [ x3, y3 ], [ x1, y1 ], [ x2, y2 ]), ax = x0 - oc[0], ay = y0 - oc[1], bx = x1 - oc[0], by = y1 - oc[1], kc = 1 / Math.sin(Math.acos((ax * bx + ay * by) / (Math.sqrt(ax * ax + ay * ay) * Math.sqrt(bx * bx + by * by))) / 2), lc = Math.sqrt(oc[0] * oc[0] + oc[1] * oc[1]); paulo@89: rc0 = Math.min(rc, (r0 - lc) / (kc - 1)); paulo@89: rc1 = Math.min(rc, (r1 - lc) / (kc + 1)); paulo@89: } paulo@89: if (x1 != null) { paulo@89: var t30 = d3_svg_arcCornerTangents(x3 == null ? [ x2, y2 ] : [ x3, y3 ], [ x0, y0 ], r1, rc1, cw), t12 = d3_svg_arcCornerTangents([ x1, y1 ], [ x2, y2 ], r1, rc1, cw); paulo@89: if (rc === rc1) { paulo@89: path.push("M", t30[0], "A", rc1, ",", rc1, " 0 0,", cr, " ", t30[1], "A", r1, ",", r1, " 0 ", 1 - cw ^ d3_svg_arcSweep(t30[1][0], t30[1][1], t12[1][0], t12[1][1]), ",", cw, " ", t12[1], "A", rc1, ",", rc1, " 0 0,", cr, " ", t12[0]); paulo@89: } else { paulo@89: path.push("M", t30[0], "A", rc1, ",", rc1, " 0 1,", cr, " ", t12[0]); paulo@89: } paulo@89: } else { paulo@89: path.push("M", x0, ",", y0); paulo@89: } paulo@89: if (x3 != null) { paulo@89: var t03 = d3_svg_arcCornerTangents([ x0, y0 ], [ x3, y3 ], r0, -rc0, cw), t21 = d3_svg_arcCornerTangents([ x2, y2 ], x1 == null ? [ x0, y0 ] : [ x1, y1 ], r0, -rc0, cw); paulo@89: if (rc === rc0) { paulo@89: path.push("L", t21[0], "A", rc0, ",", rc0, " 0 0,", cr, " ", t21[1], "A", r0, ",", r0, " 0 ", cw ^ d3_svg_arcSweep(t21[1][0], t21[1][1], t03[1][0], t03[1][1]), ",", 1 - cw, " ", t03[1], "A", rc0, ",", rc0, " 0 0,", cr, " ", t03[0]); paulo@89: } else { paulo@89: path.push("L", t21[0], "A", rc0, ",", rc0, " 0 0,", cr, " ", t03[0]); paulo@89: } paulo@89: } else { paulo@89: path.push("L", x2, ",", y2); paulo@89: } paulo@89: } else { paulo@89: path.push("M", x0, ",", y0); paulo@89: if (x1 != null) path.push("A", r1, ",", r1, " 0 ", l1, ",", cw, " ", x1, ",", y1); paulo@89: path.push("L", x2, ",", y2); paulo@89: if (x3 != null) path.push("A", r0, ",", r0, " 0 ", l0, ",", 1 - cw, " ", x3, ",", y3); paulo@89: } paulo@89: path.push("Z"); paulo@89: return path.join(""); paulo@89: } paulo@89: function circleSegment(r1, cw) { paulo@89: return "M0," + r1 + "A" + r1 + "," + r1 + " 0 1," + cw + " 0," + -r1 + "A" + r1 + "," + r1 + " 0 1," + cw + " 0," + r1; paulo@89: } paulo@89: arc.innerRadius = function(v) { paulo@89: if (!arguments.length) return innerRadius; paulo@89: innerRadius = d3_functor(v); paulo@89: return arc; paulo@89: }; paulo@89: arc.outerRadius = function(v) { paulo@89: if (!arguments.length) return outerRadius; paulo@89: outerRadius = d3_functor(v); paulo@89: return arc; paulo@89: }; paulo@89: arc.cornerRadius = function(v) { paulo@89: if (!arguments.length) return cornerRadius; paulo@89: cornerRadius = d3_functor(v); paulo@89: return arc; paulo@89: }; paulo@89: arc.padRadius = function(v) { paulo@89: if (!arguments.length) return padRadius; paulo@89: padRadius = v == d3_svg_arcAuto ? d3_svg_arcAuto : d3_functor(v); paulo@89: return arc; paulo@89: }; paulo@89: arc.startAngle = function(v) { paulo@89: if (!arguments.length) return startAngle; paulo@89: startAngle = d3_functor(v); paulo@89: return arc; paulo@89: }; paulo@89: arc.endAngle = function(v) { paulo@89: if (!arguments.length) return endAngle; paulo@89: endAngle = d3_functor(v); paulo@89: return arc; paulo@89: }; paulo@89: arc.padAngle = function(v) { paulo@89: if (!arguments.length) return padAngle; paulo@89: padAngle = d3_functor(v); paulo@89: return arc; paulo@89: }; paulo@89: arc.centroid = function() { paulo@89: var r = (+innerRadius.apply(this, arguments) + +outerRadius.apply(this, arguments)) / 2, a = (+startAngle.apply(this, arguments) + +endAngle.apply(this, arguments)) / 2 - halfπ; paulo@89: return [ Math.cos(a) * r, Math.sin(a) * r ]; paulo@89: }; paulo@89: return arc; paulo@89: }; paulo@89: var d3_svg_arcAuto = "auto"; paulo@89: function d3_svg_arcInnerRadius(d) { paulo@89: return d.innerRadius; paulo@89: } paulo@89: function d3_svg_arcOuterRadius(d) { paulo@89: return d.outerRadius; paulo@89: } paulo@89: function d3_svg_arcStartAngle(d) { paulo@89: return d.startAngle; paulo@89: } paulo@89: function d3_svg_arcEndAngle(d) { paulo@89: return d.endAngle; paulo@89: } paulo@89: function d3_svg_arcPadAngle(d) { paulo@89: return d && d.padAngle; paulo@89: } paulo@89: function d3_svg_arcSweep(x0, y0, x1, y1) { paulo@89: return (x0 - x1) * y0 - (y0 - y1) * x0 > 0 ? 0 : 1; paulo@89: } paulo@89: function d3_svg_arcCornerTangents(p0, p1, r1, rc, cw) { paulo@89: var x01 = p0[0] - p1[0], y01 = p0[1] - p1[1], lo = (cw ? rc : -rc) / Math.sqrt(x01 * x01 + y01 * y01), ox = lo * y01, oy = -lo * x01, x1 = p0[0] + ox, y1 = p0[1] + oy, x2 = p1[0] + ox, y2 = p1[1] + oy, x3 = (x1 + x2) / 2, y3 = (y1 + y2) / 2, dx = x2 - x1, dy = y2 - y1, d2 = dx * dx + dy * dy, r = r1 - rc, D = x1 * y2 - x2 * y1, d = (dy < 0 ? -1 : 1) * Math.sqrt(Math.max(0, r * r * d2 - D * D)), cx0 = (D * dy - dx * d) / d2, cy0 = (-D * dx - dy * d) / d2, cx1 = (D * dy + dx * d) / d2, cy1 = (-D * dx + dy * d) / d2, dx0 = cx0 - x3, dy0 = cy0 - y3, dx1 = cx1 - x3, dy1 = cy1 - y3; paulo@89: if (dx0 * dx0 + dy0 * dy0 > dx1 * dx1 + dy1 * dy1) cx0 = cx1, cy0 = cy1; paulo@89: return [ [ cx0 - ox, cy0 - oy ], [ cx0 * r1 / r, cy0 * r1 / r ] ]; paulo@89: } paulo@89: function d3_svg_line(projection) { paulo@89: var x = d3_geom_pointX, y = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, tension = .7; paulo@89: function line(data) { paulo@89: var segments = [], points = [], i = -1, n = data.length, d, fx = d3_functor(x), fy = d3_functor(y); paulo@89: function segment() { paulo@89: segments.push("M", interpolate(projection(points), tension)); paulo@89: } paulo@89: while (++i < n) { paulo@89: if (defined.call(this, d = data[i], i)) { paulo@89: points.push([ +fx.call(this, d, i), +fy.call(this, d, i) ]); paulo@89: } else if (points.length) { paulo@89: segment(); paulo@89: points = []; paulo@89: } paulo@89: } paulo@89: if (points.length) segment(); paulo@89: return segments.length ? segments.join("") : null; paulo@89: } paulo@89: line.x = function(_) { paulo@89: if (!arguments.length) return x; paulo@89: x = _; paulo@89: return line; paulo@89: }; paulo@89: line.y = function(_) { paulo@89: if (!arguments.length) return y; paulo@89: y = _; paulo@89: return line; paulo@89: }; paulo@89: line.defined = function(_) { paulo@89: if (!arguments.length) return defined; paulo@89: defined = _; paulo@89: return line; paulo@89: }; paulo@89: line.interpolate = function(_) { paulo@89: if (!arguments.length) return interpolateKey; paulo@89: if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; paulo@89: return line; paulo@89: }; paulo@89: line.tension = function(_) { paulo@89: if (!arguments.length) return tension; paulo@89: tension = _; paulo@89: return line; paulo@89: }; paulo@89: return line; paulo@89: } paulo@89: d3.svg.line = function() { paulo@89: return d3_svg_line(d3_identity); paulo@89: }; paulo@89: var d3_svg_lineInterpolators = d3.map({ paulo@89: linear: d3_svg_lineLinear, paulo@89: "linear-closed": d3_svg_lineLinearClosed, paulo@89: step: d3_svg_lineStep, paulo@89: "step-before": d3_svg_lineStepBefore, paulo@89: "step-after": d3_svg_lineStepAfter, paulo@89: basis: d3_svg_lineBasis, paulo@89: "basis-open": d3_svg_lineBasisOpen, paulo@89: "basis-closed": d3_svg_lineBasisClosed, paulo@89: bundle: d3_svg_lineBundle, paulo@89: cardinal: d3_svg_lineCardinal, paulo@89: "cardinal-open": d3_svg_lineCardinalOpen, paulo@89: "cardinal-closed": d3_svg_lineCardinalClosed, paulo@89: monotone: d3_svg_lineMonotone paulo@89: }); paulo@89: d3_svg_lineInterpolators.forEach(function(key, value) { paulo@89: value.key = key; paulo@89: value.closed = /-closed$/.test(key); paulo@89: }); paulo@89: function d3_svg_lineLinear(points) { paulo@89: return points.length > 1 ? points.join("L") : points + "Z"; paulo@89: } paulo@89: function d3_svg_lineLinearClosed(points) { paulo@89: return points.join("L") + "Z"; paulo@89: } paulo@89: function d3_svg_lineStep(points) { paulo@89: var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; paulo@89: while (++i < n) path.push("H", (p[0] + (p = points[i])[0]) / 2, "V", p[1]); paulo@89: if (n > 1) path.push("H", p[0]); paulo@89: return path.join(""); paulo@89: } paulo@89: function d3_svg_lineStepBefore(points) { paulo@89: var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; paulo@89: while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]); paulo@89: return path.join(""); paulo@89: } paulo@89: function d3_svg_lineStepAfter(points) { paulo@89: var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; paulo@89: while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]); paulo@89: return path.join(""); paulo@89: } paulo@89: function d3_svg_lineCardinalOpen(points, tension) { paulo@89: return points.length < 4 ? d3_svg_lineLinear(points) : points[1] + d3_svg_lineHermite(points.slice(1, -1), d3_svg_lineCardinalTangents(points, tension)); paulo@89: } paulo@89: function d3_svg_lineCardinalClosed(points, tension) { paulo@89: return points.length < 3 ? d3_svg_lineLinearClosed(points) : points[0] + d3_svg_lineHermite((points.push(points[0]), paulo@89: points), d3_svg_lineCardinalTangents([ points[points.length - 2] ].concat(points, [ points[1] ]), tension)); paulo@89: } paulo@89: function d3_svg_lineCardinal(points, tension) { paulo@89: return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineCardinalTangents(points, tension)); paulo@89: } paulo@89: function d3_svg_lineHermite(points, tangents) { paulo@89: if (tangents.length < 1 || points.length != tangents.length && points.length != tangents.length + 2) { paulo@89: return d3_svg_lineLinear(points); paulo@89: } paulo@89: var quad = points.length != tangents.length, path = "", p0 = points[0], p = points[1], t0 = tangents[0], t = t0, pi = 1; paulo@89: if (quad) { paulo@89: path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3) + "," + p[0] + "," + p[1]; paulo@89: p0 = points[1]; paulo@89: pi = 2; paulo@89: } paulo@89: if (tangents.length > 1) { paulo@89: t = tangents[1]; paulo@89: p = points[pi]; paulo@89: pi++; paulo@89: path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1]) + "," + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; paulo@89: for (var i = 2; i < tangents.length; i++, pi++) { paulo@89: p = points[pi]; paulo@89: t = tangents[i]; paulo@89: path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; paulo@89: } paulo@89: } paulo@89: if (quad) { paulo@89: var lp = points[pi]; paulo@89: path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3) + "," + lp[0] + "," + lp[1]; paulo@89: } paulo@89: return path; paulo@89: } paulo@89: function d3_svg_lineCardinalTangents(points, tension) { paulo@89: var tangents = [], a = (1 - tension) / 2, p0, p1 = points[0], p2 = points[1], i = 1, n = points.length; paulo@89: while (++i < n) { paulo@89: p0 = p1; paulo@89: p1 = p2; paulo@89: p2 = points[i]; paulo@89: tangents.push([ a * (p2[0] - p0[0]), a * (p2[1] - p0[1]) ]); paulo@89: } paulo@89: return tangents; paulo@89: } paulo@89: function d3_svg_lineBasis(points) { paulo@89: if (points.length < 3) return d3_svg_lineLinear(points); paulo@89: var i = 1, n = points.length, pi = points[0], x0 = pi[0], y0 = pi[1], px = [ x0, x0, x0, (pi = points[1])[0] ], py = [ y0, y0, y0, pi[1] ], path = [ x0, ",", y0, "L", d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; paulo@89: points.push(points[n - 1]); paulo@89: while (++i <= n) { paulo@89: pi = points[i]; paulo@89: px.shift(); paulo@89: px.push(pi[0]); paulo@89: py.shift(); paulo@89: py.push(pi[1]); paulo@89: d3_svg_lineBasisBezier(path, px, py); paulo@89: } paulo@89: points.pop(); paulo@89: path.push("L", pi); paulo@89: return path.join(""); paulo@89: } paulo@89: function d3_svg_lineBasisOpen(points) { paulo@89: if (points.length < 4) return d3_svg_lineLinear(points); paulo@89: var path = [], i = -1, n = points.length, pi, px = [ 0 ], py = [ 0 ]; paulo@89: while (++i < 3) { paulo@89: pi = points[i]; paulo@89: px.push(pi[0]); paulo@89: py.push(pi[1]); paulo@89: } paulo@89: path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px) + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)); paulo@89: --i; paulo@89: while (++i < n) { paulo@89: pi = points[i]; paulo@89: px.shift(); paulo@89: px.push(pi[0]); paulo@89: py.shift(); paulo@89: py.push(pi[1]); paulo@89: d3_svg_lineBasisBezier(path, px, py); paulo@89: } paulo@89: return path.join(""); paulo@89: } paulo@89: function d3_svg_lineBasisClosed(points) { paulo@89: var path, i = -1, n = points.length, m = n + 4, pi, px = [], py = []; paulo@89: while (++i < 4) { paulo@89: pi = points[i % n]; paulo@89: px.push(pi[0]); paulo@89: py.push(pi[1]); paulo@89: } paulo@89: path = [ d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; paulo@89: --i; paulo@89: while (++i < m) { paulo@89: pi = points[i % n]; paulo@89: px.shift(); paulo@89: px.push(pi[0]); paulo@89: py.shift(); paulo@89: py.push(pi[1]); paulo@89: d3_svg_lineBasisBezier(path, px, py); paulo@89: } paulo@89: return path.join(""); paulo@89: } paulo@89: function d3_svg_lineBundle(points, tension) { paulo@89: var n = points.length - 1; paulo@89: if (n) { paulo@89: var x0 = points[0][0], y0 = points[0][1], dx = points[n][0] - x0, dy = points[n][1] - y0, i = -1, p, t; paulo@89: while (++i <= n) { paulo@89: p = points[i]; paulo@89: t = i / n; paulo@89: p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx); paulo@89: p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy); paulo@89: } paulo@89: } paulo@89: return d3_svg_lineBasis(points); paulo@89: } paulo@89: function d3_svg_lineDot4(a, b) { paulo@89: return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; paulo@89: } paulo@89: var d3_svg_lineBasisBezier1 = [ 0, 2 / 3, 1 / 3, 0 ], d3_svg_lineBasisBezier2 = [ 0, 1 / 3, 2 / 3, 0 ], d3_svg_lineBasisBezier3 = [ 0, 1 / 6, 2 / 3, 1 / 6 ]; paulo@89: function d3_svg_lineBasisBezier(path, x, y) { paulo@89: path.push("C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y)); paulo@89: } paulo@89: function d3_svg_lineSlope(p0, p1) { paulo@89: return (p1[1] - p0[1]) / (p1[0] - p0[0]); paulo@89: } paulo@89: function d3_svg_lineFiniteDifferences(points) { paulo@89: var i = 0, j = points.length - 1, m = [], p0 = points[0], p1 = points[1], d = m[0] = d3_svg_lineSlope(p0, p1); paulo@89: while (++i < j) { paulo@89: m[i] = (d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1]))) / 2; paulo@89: } paulo@89: m[i] = d; paulo@89: return m; paulo@89: } paulo@89: function d3_svg_lineMonotoneTangents(points) { paulo@89: var tangents = [], d, a, b, s, m = d3_svg_lineFiniteDifferences(points), i = -1, j = points.length - 1; paulo@89: while (++i < j) { paulo@89: d = d3_svg_lineSlope(points[i], points[i + 1]); paulo@89: if (abs(d) < ε) { paulo@89: m[i] = m[i + 1] = 0; paulo@89: } else { paulo@89: a = m[i] / d; paulo@89: b = m[i + 1] / d; paulo@89: s = a * a + b * b; paulo@89: if (s > 9) { paulo@89: s = d * 3 / Math.sqrt(s); paulo@89: m[i] = s * a; paulo@89: m[i + 1] = s * b; paulo@89: } paulo@89: } paulo@89: } paulo@89: i = -1; paulo@89: while (++i <= j) { paulo@89: s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0]) / (6 * (1 + m[i] * m[i])); paulo@89: tangents.push([ s || 0, m[i] * s || 0 ]); paulo@89: } paulo@89: return tangents; paulo@89: } paulo@89: function d3_svg_lineMonotone(points) { paulo@89: return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points)); paulo@89: } paulo@89: d3.svg.line.radial = function() { paulo@89: var line = d3_svg_line(d3_svg_lineRadial); paulo@89: line.radius = line.x, delete line.x; paulo@89: line.angle = line.y, delete line.y; paulo@89: return line; paulo@89: }; paulo@89: function d3_svg_lineRadial(points) { paulo@89: var point, i = -1, n = points.length, r, a; paulo@89: while (++i < n) { paulo@89: point = points[i]; paulo@89: r = point[0]; paulo@89: a = point[1] - halfπ; paulo@89: point[0] = r * Math.cos(a); paulo@89: point[1] = r * Math.sin(a); paulo@89: } paulo@89: return points; paulo@89: } paulo@89: function d3_svg_area(projection) { paulo@89: var x0 = d3_geom_pointX, x1 = d3_geom_pointX, y0 = 0, y1 = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, interpolateReverse = interpolate, L = "L", tension = .7; paulo@89: function area(data) { paulo@89: var segments = [], points0 = [], points1 = [], i = -1, n = data.length, d, fx0 = d3_functor(x0), fy0 = d3_functor(y0), fx1 = x0 === x1 ? function() { paulo@89: return x; paulo@89: } : d3_functor(x1), fy1 = y0 === y1 ? function() { paulo@89: return y; paulo@89: } : d3_functor(y1), x, y; paulo@89: function segment() { paulo@89: segments.push("M", interpolate(projection(points1), tension), L, interpolateReverse(projection(points0.reverse()), tension), "Z"); paulo@89: } paulo@89: while (++i < n) { paulo@89: if (defined.call(this, d = data[i], i)) { paulo@89: points0.push([ x = +fx0.call(this, d, i), y = +fy0.call(this, d, i) ]); paulo@89: points1.push([ +fx1.call(this, d, i), +fy1.call(this, d, i) ]); paulo@89: } else if (points0.length) { paulo@89: segment(); paulo@89: points0 = []; paulo@89: points1 = []; paulo@89: } paulo@89: } paulo@89: if (points0.length) segment(); paulo@89: return segments.length ? segments.join("") : null; paulo@89: } paulo@89: area.x = function(_) { paulo@89: if (!arguments.length) return x1; paulo@89: x0 = x1 = _; paulo@89: return area; paulo@89: }; paulo@89: area.x0 = function(_) { paulo@89: if (!arguments.length) return x0; paulo@89: x0 = _; paulo@89: return area; paulo@89: }; paulo@89: area.x1 = function(_) { paulo@89: if (!arguments.length) return x1; paulo@89: x1 = _; paulo@89: return area; paulo@89: }; paulo@89: area.y = function(_) { paulo@89: if (!arguments.length) return y1; paulo@89: y0 = y1 = _; paulo@89: return area; paulo@89: }; paulo@89: area.y0 = function(_) { paulo@89: if (!arguments.length) return y0; paulo@89: y0 = _; paulo@89: return area; paulo@89: }; paulo@89: area.y1 = function(_) { paulo@89: if (!arguments.length) return y1; paulo@89: y1 = _; paulo@89: return area; paulo@89: }; paulo@89: area.defined = function(_) { paulo@89: if (!arguments.length) return defined; paulo@89: defined = _; paulo@89: return area; paulo@89: }; paulo@89: area.interpolate = function(_) { paulo@89: if (!arguments.length) return interpolateKey; paulo@89: if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; paulo@89: interpolateReverse = interpolate.reverse || interpolate; paulo@89: L = interpolate.closed ? "M" : "L"; paulo@89: return area; paulo@89: }; paulo@89: area.tension = function(_) { paulo@89: if (!arguments.length) return tension; paulo@89: tension = _; paulo@89: return area; paulo@89: }; paulo@89: return area; paulo@89: } paulo@89: d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter; paulo@89: d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore; paulo@89: d3.svg.area = function() { paulo@89: return d3_svg_area(d3_identity); paulo@89: }; paulo@89: d3.svg.area.radial = function() { paulo@89: var area = d3_svg_area(d3_svg_lineRadial); paulo@89: area.radius = area.x, delete area.x; paulo@89: area.innerRadius = area.x0, delete area.x0; paulo@89: area.outerRadius = area.x1, delete area.x1; paulo@89: area.angle = area.y, delete area.y; paulo@89: area.startAngle = area.y0, delete area.y0; paulo@89: area.endAngle = area.y1, delete area.y1; paulo@89: return area; paulo@89: }; paulo@89: d3.svg.chord = function() { paulo@89: var source = d3_source, target = d3_target, radius = d3_svg_chordRadius, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle; paulo@89: function chord(d, i) { paulo@89: var s = subgroup(this, source, d, i), t = subgroup(this, target, d, i); paulo@89: return "M" + s.p0 + arc(s.r, s.p1, s.a1 - s.a0) + (equals(s, t) ? curve(s.r, s.p1, s.r, s.p0) : curve(s.r, s.p1, t.r, t.p0) + arc(t.r, t.p1, t.a1 - t.a0) + curve(t.r, t.p1, s.r, s.p0)) + "Z"; paulo@89: } paulo@89: function subgroup(self, f, d, i) { paulo@89: var subgroup = f.call(self, d, i), r = radius.call(self, subgroup, i), a0 = startAngle.call(self, subgroup, i) - halfπ, a1 = endAngle.call(self, subgroup, i) - halfπ; paulo@89: return { paulo@89: r: r, paulo@89: a0: a0, paulo@89: a1: a1, paulo@89: p0: [ r * Math.cos(a0), r * Math.sin(a0) ], paulo@89: p1: [ r * Math.cos(a1), r * Math.sin(a1) ] paulo@89: }; paulo@89: } paulo@89: function equals(a, b) { paulo@89: return a.a0 == b.a0 && a.a1 == b.a1; paulo@89: } paulo@89: function arc(r, p, a) { paulo@89: return "A" + r + "," + r + " 0 " + +(a > π) + ",1 " + p; paulo@89: } paulo@89: function curve(r0, p0, r1, p1) { paulo@89: return "Q 0,0 " + p1; paulo@89: } paulo@89: chord.radius = function(v) { paulo@89: if (!arguments.length) return radius; paulo@89: radius = d3_functor(v); paulo@89: return chord; paulo@89: }; paulo@89: chord.source = function(v) { paulo@89: if (!arguments.length) return source; paulo@89: source = d3_functor(v); paulo@89: return chord; paulo@89: }; paulo@89: chord.target = function(v) { paulo@89: if (!arguments.length) return target; paulo@89: target = d3_functor(v); paulo@89: return chord; paulo@89: }; paulo@89: chord.startAngle = function(v) { paulo@89: if (!arguments.length) return startAngle; paulo@89: startAngle = d3_functor(v); paulo@89: return chord; paulo@89: }; paulo@89: chord.endAngle = function(v) { paulo@89: if (!arguments.length) return endAngle; paulo@89: endAngle = d3_functor(v); paulo@89: return chord; paulo@89: }; paulo@89: return chord; paulo@89: }; paulo@89: function d3_svg_chordRadius(d) { paulo@89: return d.radius; paulo@89: } paulo@89: d3.svg.diagonal = function() { paulo@89: var source = d3_source, target = d3_target, projection = d3_svg_diagonalProjection; paulo@89: function diagonal(d, i) { paulo@89: var p0 = source.call(this, d, i), p3 = target.call(this, d, i), m = (p0.y + p3.y) / 2, p = [ p0, { paulo@89: x: p0.x, paulo@89: y: m paulo@89: }, { paulo@89: x: p3.x, paulo@89: y: m paulo@89: }, p3 ]; paulo@89: p = p.map(projection); paulo@89: return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3]; paulo@89: } paulo@89: diagonal.source = function(x) { paulo@89: if (!arguments.length) return source; paulo@89: source = d3_functor(x); paulo@89: return diagonal; paulo@89: }; paulo@89: diagonal.target = function(x) { paulo@89: if (!arguments.length) return target; paulo@89: target = d3_functor(x); paulo@89: return diagonal; paulo@89: }; paulo@89: diagonal.projection = function(x) { paulo@89: if (!arguments.length) return projection; paulo@89: projection = x; paulo@89: return diagonal; paulo@89: }; paulo@89: return diagonal; paulo@89: }; paulo@89: function d3_svg_diagonalProjection(d) { paulo@89: return [ d.x, d.y ]; paulo@89: } paulo@89: d3.svg.diagonal.radial = function() { paulo@89: var diagonal = d3.svg.diagonal(), projection = d3_svg_diagonalProjection, projection_ = diagonal.projection; paulo@89: diagonal.projection = function(x) { paulo@89: return arguments.length ? projection_(d3_svg_diagonalRadialProjection(projection = x)) : projection; paulo@89: }; paulo@89: return diagonal; paulo@89: }; paulo@89: function d3_svg_diagonalRadialProjection(projection) { paulo@89: return function() { paulo@89: var d = projection.apply(this, arguments), r = d[0], a = d[1] - halfπ; paulo@89: return [ r * Math.cos(a), r * Math.sin(a) ]; paulo@89: }; paulo@89: } paulo@89: d3.svg.symbol = function() { paulo@89: var type = d3_svg_symbolType, size = d3_svg_symbolSize; paulo@89: function symbol(d, i) { paulo@89: return (d3_svg_symbols.get(type.call(this, d, i)) || d3_svg_symbolCircle)(size.call(this, d, i)); paulo@89: } paulo@89: symbol.type = function(x) { paulo@89: if (!arguments.length) return type; paulo@89: type = d3_functor(x); paulo@89: return symbol; paulo@89: }; paulo@89: symbol.size = function(x) { paulo@89: if (!arguments.length) return size; paulo@89: size = d3_functor(x); paulo@89: return symbol; paulo@89: }; paulo@89: return symbol; paulo@89: }; paulo@89: function d3_svg_symbolSize() { paulo@89: return 64; paulo@89: } paulo@89: function d3_svg_symbolType() { paulo@89: return "circle"; paulo@89: } paulo@89: function d3_svg_symbolCircle(size) { paulo@89: var r = Math.sqrt(size / π); paulo@89: return "M0," + r + "A" + r + "," + r + " 0 1,1 0," + -r + "A" + r + "," + r + " 0 1,1 0," + r + "Z"; paulo@89: } paulo@89: var d3_svg_symbols = d3.map({ paulo@89: circle: d3_svg_symbolCircle, paulo@89: cross: function(size) { paulo@89: var r = Math.sqrt(size / 5) / 2; paulo@89: return "M" + -3 * r + "," + -r + "H" + -r + "V" + -3 * r + "H" + r + "V" + -r + "H" + 3 * r + "V" + r + "H" + r + "V" + 3 * r + "H" + -r + "V" + r + "H" + -3 * r + "Z"; paulo@89: }, paulo@89: diamond: function(size) { paulo@89: var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)), rx = ry * d3_svg_symbolTan30; paulo@89: return "M0," + -ry + "L" + rx + ",0" + " 0," + ry + " " + -rx + ",0" + "Z"; paulo@89: }, paulo@89: square: function(size) { paulo@89: var r = Math.sqrt(size) / 2; paulo@89: return "M" + -r + "," + -r + "L" + r + "," + -r + " " + r + "," + r + " " + -r + "," + r + "Z"; paulo@89: }, paulo@89: "triangle-down": function(size) { paulo@89: var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; paulo@89: return "M0," + ry + "L" + rx + "," + -ry + " " + -rx + "," + -ry + "Z"; paulo@89: }, paulo@89: "triangle-up": function(size) { paulo@89: var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; paulo@89: return "M0," + -ry + "L" + rx + "," + ry + " " + -rx + "," + ry + "Z"; paulo@89: } paulo@89: }); paulo@89: d3.svg.symbolTypes = d3_svg_symbols.keys(); paulo@89: var d3_svg_symbolSqrt3 = Math.sqrt(3), d3_svg_symbolTan30 = Math.tan(30 * d3_radians); paulo@89: d3_selectionPrototype.transition = function(name) { paulo@89: var id = d3_transitionInheritId || ++d3_transitionId, ns = d3_transitionNamespace(name), subgroups = [], subgroup, node, transition = d3_transitionInherit || { paulo@89: time: Date.now(), paulo@89: ease: d3_ease_cubicInOut, paulo@89: delay: 0, paulo@89: duration: 250 paulo@89: }; paulo@89: for (var j = -1, m = this.length; ++j < m; ) { paulo@89: subgroups.push(subgroup = []); paulo@89: for (var group = this[j], i = -1, n = group.length; ++i < n; ) { paulo@89: if (node = group[i]) d3_transitionNode(node, i, ns, id, transition); paulo@89: subgroup.push(node); paulo@89: } paulo@89: } paulo@89: return d3_transition(subgroups, ns, id); paulo@89: }; paulo@89: d3_selectionPrototype.interrupt = function(name) { paulo@89: return this.each(name == null ? d3_selection_interrupt : d3_selection_interruptNS(d3_transitionNamespace(name))); paulo@89: }; paulo@89: var d3_selection_interrupt = d3_selection_interruptNS(d3_transitionNamespace()); paulo@89: function d3_selection_interruptNS(ns) { paulo@89: return function() { paulo@89: var lock, activeId, active; paulo@89: if ((lock = this[ns]) && (active = lock[activeId = lock.active])) { paulo@89: active.timer.c = null; paulo@89: active.timer.t = NaN; paulo@89: if (--lock.count) delete lock[activeId]; else delete this[ns]; paulo@89: lock.active += .5; paulo@89: active.event && active.event.interrupt.call(this, this.__data__, active.index); paulo@89: } paulo@89: }; paulo@89: } paulo@89: function d3_transition(groups, ns, id) { paulo@89: d3_subclass(groups, d3_transitionPrototype); paulo@89: groups.namespace = ns; paulo@89: groups.id = id; paulo@89: return groups; paulo@89: } paulo@89: var d3_transitionPrototype = [], d3_transitionId = 0, d3_transitionInheritId, d3_transitionInherit; paulo@89: d3_transitionPrototype.call = d3_selectionPrototype.call; paulo@89: d3_transitionPrototype.empty = d3_selectionPrototype.empty; paulo@89: d3_transitionPrototype.node = d3_selectionPrototype.node; paulo@89: d3_transitionPrototype.size = d3_selectionPrototype.size; paulo@89: d3.transition = function(selection, name) { paulo@89: return selection && selection.transition ? d3_transitionInheritId ? selection.transition(name) : selection : d3.selection().transition(selection); paulo@89: }; paulo@89: d3.transition.prototype = d3_transitionPrototype; paulo@89: d3_transitionPrototype.select = function(selector) { paulo@89: var id = this.id, ns = this.namespace, subgroups = [], subgroup, subnode, node; paulo@89: selector = d3_selection_selector(selector); paulo@89: for (var j = -1, m = this.length; ++j < m; ) { paulo@89: subgroups.push(subgroup = []); paulo@89: for (var group = this[j], i = -1, n = group.length; ++i < n; ) { paulo@89: if ((node = group[i]) && (subnode = selector.call(node, node.__data__, i, j))) { paulo@89: if ("__data__" in node) subnode.__data__ = node.__data__; paulo@89: d3_transitionNode(subnode, i, ns, id, node[ns][id]); paulo@89: subgroup.push(subnode); paulo@89: } else { paulo@89: subgroup.push(null); paulo@89: } paulo@89: } paulo@89: } paulo@89: return d3_transition(subgroups, ns, id); paulo@89: }; paulo@89: d3_transitionPrototype.selectAll = function(selector) { paulo@89: var id = this.id, ns = this.namespace, subgroups = [], subgroup, subnodes, node, subnode, transition; paulo@89: selector = d3_selection_selectorAll(selector); paulo@89: for (var j = -1, m = this.length; ++j < m; ) { paulo@89: for (var group = this[j], i = -1, n = group.length; ++i < n; ) { paulo@89: if (node = group[i]) { paulo@89: transition = node[ns][id]; paulo@89: subnodes = selector.call(node, node.__data__, i, j); paulo@89: subgroups.push(subgroup = []); paulo@89: for (var k = -1, o = subnodes.length; ++k < o; ) { paulo@89: if (subnode = subnodes[k]) d3_transitionNode(subnode, k, ns, id, transition); paulo@89: subgroup.push(subnode); paulo@89: } paulo@89: } paulo@89: } paulo@89: } paulo@89: return d3_transition(subgroups, ns, id); paulo@89: }; paulo@89: d3_transitionPrototype.filter = function(filter) { paulo@89: var subgroups = [], subgroup, group, node; paulo@89: if (typeof filter !== "function") filter = d3_selection_filter(filter); paulo@89: for (var j = 0, m = this.length; j < m; j++) { paulo@89: subgroups.push(subgroup = []); paulo@89: for (var group = this[j], i = 0, n = group.length; i < n; i++) { paulo@89: if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { paulo@89: subgroup.push(node); paulo@89: } paulo@89: } paulo@89: } paulo@89: return d3_transition(subgroups, this.namespace, this.id); paulo@89: }; paulo@89: d3_transitionPrototype.tween = function(name, tween) { paulo@89: var id = this.id, ns = this.namespace; paulo@89: if (arguments.length < 2) return this.node()[ns][id].tween.get(name); paulo@89: return d3_selection_each(this, tween == null ? function(node) { paulo@89: node[ns][id].tween.remove(name); paulo@89: } : function(node) { paulo@89: node[ns][id].tween.set(name, tween); paulo@89: }); paulo@89: }; paulo@89: function d3_transition_tween(groups, name, value, tween) { paulo@89: var id = groups.id, ns = groups.namespace; paulo@89: return d3_selection_each(groups, typeof value === "function" ? function(node, i, j) { paulo@89: node[ns][id].tween.set(name, tween(value.call(node, node.__data__, i, j))); paulo@89: } : (value = tween(value), function(node) { paulo@89: node[ns][id].tween.set(name, value); paulo@89: })); paulo@89: } paulo@89: d3_transitionPrototype.attr = function(nameNS, value) { paulo@89: if (arguments.length < 2) { paulo@89: for (value in nameNS) this.attr(value, nameNS[value]); paulo@89: return this; paulo@89: } paulo@89: var interpolate = nameNS == "transform" ? d3_interpolateTransform : d3_interpolate, name = d3.ns.qualify(nameNS); paulo@89: function attrNull() { paulo@89: this.removeAttribute(name); paulo@89: } paulo@89: function attrNullNS() { paulo@89: this.removeAttributeNS(name.space, name.local); paulo@89: } paulo@89: function attrTween(b) { paulo@89: return b == null ? attrNull : (b += "", function() { paulo@89: var a = this.getAttribute(name), i; paulo@89: return a !== b && (i = interpolate(a, b), function(t) { paulo@89: this.setAttribute(name, i(t)); paulo@89: }); paulo@89: }); paulo@89: } paulo@89: function attrTweenNS(b) { paulo@89: return b == null ? attrNullNS : (b += "", function() { paulo@89: var a = this.getAttributeNS(name.space, name.local), i; paulo@89: return a !== b && (i = interpolate(a, b), function(t) { paulo@89: this.setAttributeNS(name.space, name.local, i(t)); paulo@89: }); paulo@89: }); paulo@89: } paulo@89: return d3_transition_tween(this, "attr." + nameNS, value, name.local ? attrTweenNS : attrTween); paulo@89: }; paulo@89: d3_transitionPrototype.attrTween = function(nameNS, tween) { paulo@89: var name = d3.ns.qualify(nameNS); paulo@89: function attrTween(d, i) { paulo@89: var f = tween.call(this, d, i, this.getAttribute(name)); paulo@89: return f && function(t) { paulo@89: this.setAttribute(name, f(t)); paulo@89: }; paulo@89: } paulo@89: function attrTweenNS(d, i) { paulo@89: var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local)); paulo@89: return f && function(t) { paulo@89: this.setAttributeNS(name.space, name.local, f(t)); paulo@89: }; paulo@89: } paulo@89: return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween); paulo@89: }; paulo@89: d3_transitionPrototype.style = function(name, value, priority) { paulo@89: var n = arguments.length; paulo@89: if (n < 3) { paulo@89: if (typeof name !== "string") { paulo@89: if (n < 2) value = ""; paulo@89: for (priority in name) this.style(priority, name[priority], value); paulo@89: return this; paulo@89: } paulo@89: priority = ""; paulo@89: } paulo@89: function styleNull() { paulo@89: this.style.removeProperty(name); paulo@89: } paulo@89: function styleString(b) { paulo@89: return b == null ? styleNull : (b += "", function() { paulo@89: var a = d3_window(this).getComputedStyle(this, null).getPropertyValue(name), i; paulo@89: return a !== b && (i = d3_interpolate(a, b), function(t) { paulo@89: this.style.setProperty(name, i(t), priority); paulo@89: }); paulo@89: }); paulo@89: } paulo@89: return d3_transition_tween(this, "style." + name, value, styleString); paulo@89: }; paulo@89: d3_transitionPrototype.styleTween = function(name, tween, priority) { paulo@89: if (arguments.length < 3) priority = ""; paulo@89: function styleTween(d, i) { paulo@89: var f = tween.call(this, d, i, d3_window(this).getComputedStyle(this, null).getPropertyValue(name)); paulo@89: return f && function(t) { paulo@89: this.style.setProperty(name, f(t), priority); paulo@89: }; paulo@89: } paulo@89: return this.tween("style." + name, styleTween); paulo@89: }; paulo@89: d3_transitionPrototype.text = function(value) { paulo@89: return d3_transition_tween(this, "text", value, d3_transition_text); paulo@89: }; paulo@89: function d3_transition_text(b) { paulo@89: if (b == null) b = ""; paulo@89: return function() { paulo@89: this.textContent = b; paulo@89: }; paulo@89: } paulo@89: d3_transitionPrototype.remove = function() { paulo@89: var ns = this.namespace; paulo@89: return this.each("end.transition", function() { paulo@89: var p; paulo@89: if (this[ns].count < 2 && (p = this.parentNode)) p.removeChild(this); paulo@89: }); paulo@89: }; paulo@89: d3_transitionPrototype.ease = function(value) { paulo@89: var id = this.id, ns = this.namespace; paulo@89: if (arguments.length < 1) return this.node()[ns][id].ease; paulo@89: if (typeof value !== "function") value = d3.ease.apply(d3, arguments); paulo@89: return d3_selection_each(this, function(node) { paulo@89: node[ns][id].ease = value; paulo@89: }); paulo@89: }; paulo@89: d3_transitionPrototype.delay = function(value) { paulo@89: var id = this.id, ns = this.namespace; paulo@89: if (arguments.length < 1) return this.node()[ns][id].delay; paulo@89: return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { paulo@89: node[ns][id].delay = +value.call(node, node.__data__, i, j); paulo@89: } : (value = +value, function(node) { paulo@89: node[ns][id].delay = value; paulo@89: })); paulo@89: }; paulo@89: d3_transitionPrototype.duration = function(value) { paulo@89: var id = this.id, ns = this.namespace; paulo@89: if (arguments.length < 1) return this.node()[ns][id].duration; paulo@89: return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { paulo@89: node[ns][id].duration = Math.max(1, value.call(node, node.__data__, i, j)); paulo@89: } : (value = Math.max(1, value), function(node) { paulo@89: node[ns][id].duration = value; paulo@89: })); paulo@89: }; paulo@89: d3_transitionPrototype.each = function(type, listener) { paulo@89: var id = this.id, ns = this.namespace; paulo@89: if (arguments.length < 2) { paulo@89: var inherit = d3_transitionInherit, inheritId = d3_transitionInheritId; paulo@89: try { paulo@89: d3_transitionInheritId = id; paulo@89: d3_selection_each(this, function(node, i, j) { paulo@89: d3_transitionInherit = node[ns][id]; paulo@89: type.call(node, node.__data__, i, j); paulo@89: }); paulo@89: } finally { paulo@89: d3_transitionInherit = inherit; paulo@89: d3_transitionInheritId = inheritId; paulo@89: } paulo@89: } else { paulo@89: d3_selection_each(this, function(node) { paulo@89: var transition = node[ns][id]; paulo@89: (transition.event || (transition.event = d3.dispatch("start", "end", "interrupt"))).on(type, listener); paulo@89: }); paulo@89: } paulo@89: return this; paulo@89: }; paulo@89: d3_transitionPrototype.transition = function() { paulo@89: var id0 = this.id, id1 = ++d3_transitionId, ns = this.namespace, subgroups = [], subgroup, group, node, transition; paulo@89: for (var j = 0, m = this.length; j < m; j++) { paulo@89: subgroups.push(subgroup = []); paulo@89: for (var group = this[j], i = 0, n = group.length; i < n; i++) { paulo@89: if (node = group[i]) { paulo@89: transition = node[ns][id0]; paulo@89: d3_transitionNode(node, i, ns, id1, { paulo@89: time: transition.time, paulo@89: ease: transition.ease, paulo@89: delay: transition.delay + transition.duration, paulo@89: duration: transition.duration paulo@89: }); paulo@89: } paulo@89: subgroup.push(node); paulo@89: } paulo@89: } paulo@89: return d3_transition(subgroups, ns, id1); paulo@89: }; paulo@89: function d3_transitionNamespace(name) { paulo@89: return name == null ? "__transition__" : "__transition_" + name + "__"; paulo@89: } paulo@89: function d3_transitionNode(node, i, ns, id, inherit) { paulo@89: var lock = node[ns] || (node[ns] = { paulo@89: active: 0, paulo@89: count: 0 paulo@89: }), transition = lock[id], time, timer, duration, ease, tweens; paulo@89: function schedule(elapsed) { paulo@89: var delay = transition.delay; paulo@89: timer.t = delay + time; paulo@89: if (delay <= elapsed) return start(elapsed - delay); paulo@89: timer.c = start; paulo@89: } paulo@89: function start(elapsed) { paulo@89: var activeId = lock.active, active = lock[activeId]; paulo@89: if (active) { paulo@89: active.timer.c = null; paulo@89: active.timer.t = NaN; paulo@89: --lock.count; paulo@89: delete lock[activeId]; paulo@89: active.event && active.event.interrupt.call(node, node.__data__, active.index); paulo@89: } paulo@89: for (var cancelId in lock) { paulo@89: if (+cancelId < id) { paulo@89: var cancel = lock[cancelId]; paulo@89: cancel.timer.c = null; paulo@89: cancel.timer.t = NaN; paulo@89: --lock.count; paulo@89: delete lock[cancelId]; paulo@89: } paulo@89: } paulo@89: timer.c = tick; paulo@89: d3_timer(function() { paulo@89: if (timer.c && tick(elapsed || 1)) { paulo@89: timer.c = null; paulo@89: timer.t = NaN; paulo@89: } paulo@89: return 1; paulo@89: }, 0, time); paulo@89: lock.active = id; paulo@89: transition.event && transition.event.start.call(node, node.__data__, i); paulo@89: tweens = []; paulo@89: transition.tween.forEach(function(key, value) { paulo@89: if (value = value.call(node, node.__data__, i)) { paulo@89: tweens.push(value); paulo@89: } paulo@89: }); paulo@89: ease = transition.ease; paulo@89: duration = transition.duration; paulo@89: } paulo@89: function tick(elapsed) { paulo@89: var t = elapsed / duration, e = ease(t), n = tweens.length; paulo@89: while (n > 0) { paulo@89: tweens[--n].call(node, e); paulo@89: } paulo@89: if (t >= 1) { paulo@89: transition.event && transition.event.end.call(node, node.__data__, i); paulo@89: if (--lock.count) delete lock[id]; else delete node[ns]; paulo@89: return 1; paulo@89: } paulo@89: } paulo@89: if (!transition) { paulo@89: time = inherit.time; paulo@89: timer = d3_timer(schedule, 0, time); paulo@89: transition = lock[id] = { paulo@89: tween: new d3_Map(), paulo@89: time: time, paulo@89: timer: timer, paulo@89: delay: inherit.delay, paulo@89: duration: inherit.duration, paulo@89: ease: inherit.ease, paulo@89: index: i paulo@89: }; paulo@89: inherit = null; paulo@89: ++lock.count; paulo@89: } paulo@89: } paulo@89: d3.svg.axis = function() { paulo@89: var scale = d3.scale.linear(), orient = d3_svg_axisDefaultOrient, innerTickSize = 6, outerTickSize = 6, tickPadding = 3, tickArguments_ = [ 10 ], tickValues = null, tickFormat_; paulo@89: function axis(g) { paulo@89: g.each(function() { paulo@89: var g = d3.select(this); paulo@89: var scale0 = this.__chart__ || scale, scale1 = this.__chart__ = scale.copy(); paulo@89: var ticks = tickValues == null ? scale1.ticks ? scale1.ticks.apply(scale1, tickArguments_) : scale1.domain() : tickValues, tickFormat = tickFormat_ == null ? scale1.tickFormat ? scale1.tickFormat.apply(scale1, tickArguments_) : d3_identity : tickFormat_, tick = g.selectAll(".tick").data(ticks, scale1), tickEnter = tick.enter().insert("g", ".domain").attr("class", "tick").style("opacity", ε), tickExit = d3.transition(tick.exit()).style("opacity", ε).remove(), tickUpdate = d3.transition(tick.order()).style("opacity", 1), tickSpacing = Math.max(innerTickSize, 0) + tickPadding, tickTransform; paulo@89: var range = d3_scaleRange(scale1), path = g.selectAll(".domain").data([ 0 ]), pathUpdate = (path.enter().append("path").attr("class", "domain"), paulo@89: d3.transition(path)); paulo@89: tickEnter.append("line"); paulo@89: tickEnter.append("text"); paulo@89: var lineEnter = tickEnter.select("line"), lineUpdate = tickUpdate.select("line"), text = tick.select("text").text(tickFormat), textEnter = tickEnter.select("text"), textUpdate = tickUpdate.select("text"), sign = orient === "top" || orient === "left" ? -1 : 1, x1, x2, y1, y2; paulo@89: if (orient === "bottom" || orient === "top") { paulo@89: tickTransform = d3_svg_axisX, x1 = "x", y1 = "y", x2 = "x2", y2 = "y2"; paulo@89: text.attr("dy", sign < 0 ? "0em" : ".71em").style("text-anchor", "middle"); paulo@89: pathUpdate.attr("d", "M" + range[0] + "," + sign * outerTickSize + "V0H" + range[1] + "V" + sign * outerTickSize); paulo@89: } else { paulo@89: tickTransform = d3_svg_axisY, x1 = "y", y1 = "x", x2 = "y2", y2 = "x2"; paulo@89: text.attr("dy", ".32em").style("text-anchor", sign < 0 ? "end" : "start"); paulo@89: pathUpdate.attr("d", "M" + sign * outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + sign * outerTickSize); paulo@89: } paulo@89: lineEnter.attr(y2, sign * innerTickSize); paulo@89: textEnter.attr(y1, sign * tickSpacing); paulo@89: lineUpdate.attr(x2, 0).attr(y2, sign * innerTickSize); paulo@89: textUpdate.attr(x1, 0).attr(y1, sign * tickSpacing); paulo@89: if (scale1.rangeBand) { paulo@89: var x = scale1, dx = x.rangeBand() / 2; paulo@89: scale0 = scale1 = function(d) { paulo@89: return x(d) + dx; paulo@89: }; paulo@89: } else if (scale0.rangeBand) { paulo@89: scale0 = scale1; paulo@89: } else { paulo@89: tickExit.call(tickTransform, scale1, scale0); paulo@89: } paulo@89: tickEnter.call(tickTransform, scale0, scale1); paulo@89: tickUpdate.call(tickTransform, scale1, scale1); paulo@89: }); paulo@89: } paulo@89: axis.scale = function(x) { paulo@89: if (!arguments.length) return scale; paulo@89: scale = x; paulo@89: return axis; paulo@89: }; paulo@89: axis.orient = function(x) { paulo@89: if (!arguments.length) return orient; paulo@89: orient = x in d3_svg_axisOrients ? x + "" : d3_svg_axisDefaultOrient; paulo@89: return axis; paulo@89: }; paulo@89: axis.ticks = function() { paulo@89: if (!arguments.length) return tickArguments_; paulo@89: tickArguments_ = d3_array(arguments); paulo@89: return axis; paulo@89: }; paulo@89: axis.tickValues = function(x) { paulo@89: if (!arguments.length) return tickValues; paulo@89: tickValues = x; paulo@89: return axis; paulo@89: }; paulo@89: axis.tickFormat = function(x) { paulo@89: if (!arguments.length) return tickFormat_; paulo@89: tickFormat_ = x; paulo@89: return axis; paulo@89: }; paulo@89: axis.tickSize = function(x) { paulo@89: var n = arguments.length; paulo@89: if (!n) return innerTickSize; paulo@89: innerTickSize = +x; paulo@89: outerTickSize = +arguments[n - 1]; paulo@89: return axis; paulo@89: }; paulo@89: axis.innerTickSize = function(x) { paulo@89: if (!arguments.length) return innerTickSize; paulo@89: innerTickSize = +x; paulo@89: return axis; paulo@89: }; paulo@89: axis.outerTickSize = function(x) { paulo@89: if (!arguments.length) return outerTickSize; paulo@89: outerTickSize = +x; paulo@89: return axis; paulo@89: }; paulo@89: axis.tickPadding = function(x) { paulo@89: if (!arguments.length) return tickPadding; paulo@89: tickPadding = +x; paulo@89: return axis; paulo@89: }; paulo@89: axis.tickSubdivide = function() { paulo@89: return arguments.length && axis; paulo@89: }; paulo@89: return axis; paulo@89: }; paulo@89: var d3_svg_axisDefaultOrient = "bottom", d3_svg_axisOrients = { paulo@89: top: 1, paulo@89: right: 1, paulo@89: bottom: 1, paulo@89: left: 1 paulo@89: }; paulo@89: function d3_svg_axisX(selection, x0, x1) { paulo@89: selection.attr("transform", function(d) { paulo@89: var v0 = x0(d); paulo@89: return "translate(" + (isFinite(v0) ? v0 : x1(d)) + ",0)"; paulo@89: }); paulo@89: } paulo@89: function d3_svg_axisY(selection, y0, y1) { paulo@89: selection.attr("transform", function(d) { paulo@89: var v0 = y0(d); paulo@89: return "translate(0," + (isFinite(v0) ? v0 : y1(d)) + ")"; paulo@89: }); paulo@89: } paulo@89: d3.svg.brush = function() { paulo@89: var event = d3_eventDispatch(brush, "brushstart", "brush", "brushend"), x = null, y = null, xExtent = [ 0, 0 ], yExtent = [ 0, 0 ], xExtentDomain, yExtentDomain, xClamp = true, yClamp = true, resizes = d3_svg_brushResizes[0]; paulo@89: function brush(g) { paulo@89: g.each(function() { paulo@89: var g = d3.select(this).style("pointer-events", "all").style("-webkit-tap-highlight-color", "rgba(0,0,0,0)").on("mousedown.brush", brushstart).on("touchstart.brush", brushstart); paulo@89: var background = g.selectAll(".background").data([ 0 ]); paulo@89: background.enter().append("rect").attr("class", "background").style("visibility", "hidden").style("cursor", "crosshair"); paulo@89: g.selectAll(".extent").data([ 0 ]).enter().append("rect").attr("class", "extent").style("cursor", "move"); paulo@89: var resize = g.selectAll(".resize").data(resizes, d3_identity); paulo@89: resize.exit().remove(); paulo@89: resize.enter().append("g").attr("class", function(d) { paulo@89: return "resize " + d; paulo@89: }).style("cursor", function(d) { paulo@89: return d3_svg_brushCursor[d]; paulo@89: }).append("rect").attr("x", function(d) { paulo@89: return /[ew]$/.test(d) ? -3 : null; paulo@89: }).attr("y", function(d) { paulo@89: return /^[ns]/.test(d) ? -3 : null; paulo@89: }).attr("width", 6).attr("height", 6).style("visibility", "hidden"); paulo@89: resize.style("display", brush.empty() ? "none" : null); paulo@89: var gUpdate = d3.transition(g), backgroundUpdate = d3.transition(background), range; paulo@89: if (x) { paulo@89: range = d3_scaleRange(x); paulo@89: backgroundUpdate.attr("x", range[0]).attr("width", range[1] - range[0]); paulo@89: redrawX(gUpdate); paulo@89: } paulo@89: if (y) { paulo@89: range = d3_scaleRange(y); paulo@89: backgroundUpdate.attr("y", range[0]).attr("height", range[1] - range[0]); paulo@89: redrawY(gUpdate); paulo@89: } paulo@89: redraw(gUpdate); paulo@89: }); paulo@89: } paulo@89: brush.event = function(g) { paulo@89: g.each(function() { paulo@89: var event_ = event.of(this, arguments), extent1 = { paulo@89: x: xExtent, paulo@89: y: yExtent, paulo@89: i: xExtentDomain, paulo@89: j: yExtentDomain paulo@89: }, extent0 = this.__chart__ || extent1; paulo@89: this.__chart__ = extent1; paulo@89: if (d3_transitionInheritId) { paulo@89: d3.select(this).transition().each("start.brush", function() { paulo@89: xExtentDomain = extent0.i; paulo@89: yExtentDomain = extent0.j; paulo@89: xExtent = extent0.x; paulo@89: yExtent = extent0.y; paulo@89: event_({ paulo@89: type: "brushstart" paulo@89: }); paulo@89: }).tween("brush:brush", function() { paulo@89: var xi = d3_interpolateArray(xExtent, extent1.x), yi = d3_interpolateArray(yExtent, extent1.y); paulo@89: xExtentDomain = yExtentDomain = null; paulo@89: return function(t) { paulo@89: xExtent = extent1.x = xi(t); paulo@89: yExtent = extent1.y = yi(t); paulo@89: event_({ paulo@89: type: "brush", paulo@89: mode: "resize" paulo@89: }); paulo@89: }; paulo@89: }).each("end.brush", function() { paulo@89: xExtentDomain = extent1.i; paulo@89: yExtentDomain = extent1.j; paulo@89: event_({ paulo@89: type: "brush", paulo@89: mode: "resize" paulo@89: }); paulo@89: event_({ paulo@89: type: "brushend" paulo@89: }); paulo@89: }); paulo@89: } else { paulo@89: event_({ paulo@89: type: "brushstart" paulo@89: }); paulo@89: event_({ paulo@89: type: "brush", paulo@89: mode: "resize" paulo@89: }); paulo@89: event_({ paulo@89: type: "brushend" paulo@89: }); paulo@89: } paulo@89: }); paulo@89: }; paulo@89: function redraw(g) { paulo@89: g.selectAll(".resize").attr("transform", function(d) { paulo@89: return "translate(" + xExtent[+/e$/.test(d)] + "," + yExtent[+/^s/.test(d)] + ")"; paulo@89: }); paulo@89: } paulo@89: function redrawX(g) { paulo@89: g.select(".extent").attr("x", xExtent[0]); paulo@89: g.selectAll(".extent,.n>rect,.s>rect").attr("width", xExtent[1] - xExtent[0]); paulo@89: } paulo@89: function redrawY(g) { paulo@89: g.select(".extent").attr("y", yExtent[0]); paulo@89: g.selectAll(".extent,.e>rect,.w>rect").attr("height", yExtent[1] - yExtent[0]); paulo@89: } paulo@89: function brushstart() { paulo@89: var target = this, eventTarget = d3.select(d3.event.target), event_ = event.of(target, arguments), g = d3.select(target), resizing = eventTarget.datum(), resizingX = !/^(n|s)$/.test(resizing) && x, resizingY = !/^(e|w)$/.test(resizing) && y, dragging = eventTarget.classed("extent"), dragRestore = d3_event_dragSuppress(target), center, origin = d3.mouse(target), offset; paulo@89: var w = d3.select(d3_window(target)).on("keydown.brush", keydown).on("keyup.brush", keyup); paulo@89: if (d3.event.changedTouches) { paulo@89: w.on("touchmove.brush", brushmove).on("touchend.brush", brushend); paulo@89: } else { paulo@89: w.on("mousemove.brush", brushmove).on("mouseup.brush", brushend); paulo@89: } paulo@89: g.interrupt().selectAll("*").interrupt(); paulo@89: if (dragging) { paulo@89: origin[0] = xExtent[0] - origin[0]; paulo@89: origin[1] = yExtent[0] - origin[1]; paulo@89: } else if (resizing) { paulo@89: var ex = +/w$/.test(resizing), ey = +/^n/.test(resizing); paulo@89: offset = [ xExtent[1 - ex] - origin[0], yExtent[1 - ey] - origin[1] ]; paulo@89: origin[0] = xExtent[ex]; paulo@89: origin[1] = yExtent[ey]; paulo@89: } else if (d3.event.altKey) center = origin.slice(); paulo@89: g.style("pointer-events", "none").selectAll(".resize").style("display", null); paulo@89: d3.select("body").style("cursor", eventTarget.style("cursor")); paulo@89: event_({ paulo@89: type: "brushstart" paulo@89: }); paulo@89: brushmove(); paulo@89: function keydown() { paulo@89: if (d3.event.keyCode == 32) { paulo@89: if (!dragging) { paulo@89: center = null; paulo@89: origin[0] -= xExtent[1]; paulo@89: origin[1] -= yExtent[1]; paulo@89: dragging = 2; paulo@89: } paulo@89: d3_eventPreventDefault(); paulo@89: } paulo@89: } paulo@89: function keyup() { paulo@89: if (d3.event.keyCode == 32 && dragging == 2) { paulo@89: origin[0] += xExtent[1]; paulo@89: origin[1] += yExtent[1]; paulo@89: dragging = 0; paulo@89: d3_eventPreventDefault(); paulo@89: } paulo@89: } paulo@89: function brushmove() { paulo@89: var point = d3.mouse(target), moved = false; paulo@89: if (offset) { paulo@89: point[0] += offset[0]; paulo@89: point[1] += offset[1]; paulo@89: } paulo@89: if (!dragging) { paulo@89: if (d3.event.altKey) { paulo@89: if (!center) center = [ (xExtent[0] + xExtent[1]) / 2, (yExtent[0] + yExtent[1]) / 2 ]; paulo@89: origin[0] = xExtent[+(point[0] < center[0])]; paulo@89: origin[1] = yExtent[+(point[1] < center[1])]; paulo@89: } else center = null; paulo@89: } paulo@89: if (resizingX && move1(point, x, 0)) { paulo@89: redrawX(g); paulo@89: moved = true; paulo@89: } paulo@89: if (resizingY && move1(point, y, 1)) { paulo@89: redrawY(g); paulo@89: moved = true; paulo@89: } paulo@89: if (moved) { paulo@89: redraw(g); paulo@89: event_({ paulo@89: type: "brush", paulo@89: mode: dragging ? "move" : "resize" paulo@89: }); paulo@89: } paulo@89: } paulo@89: function move1(point, scale, i) { paulo@89: var range = d3_scaleRange(scale), r0 = range[0], r1 = range[1], position = origin[i], extent = i ? yExtent : xExtent, size = extent[1] - extent[0], min, max; paulo@89: if (dragging) { paulo@89: r0 -= position; paulo@89: r1 -= size + position; paulo@89: } paulo@89: min = (i ? yClamp : xClamp) ? Math.max(r0, Math.min(r1, point[i])) : point[i]; paulo@89: if (dragging) { paulo@89: max = (min += position) + size; paulo@89: } else { paulo@89: if (center) position = Math.max(r0, Math.min(r1, 2 * center[i] - min)); paulo@89: if (position < min) { paulo@89: max = min; paulo@89: min = position; paulo@89: } else { paulo@89: max = position; paulo@89: } paulo@89: } paulo@89: if (extent[0] != min || extent[1] != max) { paulo@89: if (i) yExtentDomain = null; else xExtentDomain = null; paulo@89: extent[0] = min; paulo@89: extent[1] = max; paulo@89: return true; paulo@89: } paulo@89: } paulo@89: function brushend() { paulo@89: brushmove(); paulo@89: g.style("pointer-events", "all").selectAll(".resize").style("display", brush.empty() ? "none" : null); paulo@89: d3.select("body").style("cursor", null); paulo@89: w.on("mousemove.brush", null).on("mouseup.brush", null).on("touchmove.brush", null).on("touchend.brush", null).on("keydown.brush", null).on("keyup.brush", null); paulo@89: dragRestore(); paulo@89: event_({ paulo@89: type: "brushend" paulo@89: }); paulo@89: } paulo@89: } paulo@89: brush.x = function(z) { paulo@89: if (!arguments.length) return x; paulo@89: x = z; paulo@89: resizes = d3_svg_brushResizes[!x << 1 | !y]; paulo@89: return brush; paulo@89: }; paulo@89: brush.y = function(z) { paulo@89: if (!arguments.length) return y; paulo@89: y = z; paulo@89: resizes = d3_svg_brushResizes[!x << 1 | !y]; paulo@89: return brush; paulo@89: }; paulo@89: brush.clamp = function(z) { paulo@89: if (!arguments.length) return x && y ? [ xClamp, yClamp ] : x ? xClamp : y ? yClamp : null; paulo@89: if (x && y) xClamp = !!z[0], yClamp = !!z[1]; else if (x) xClamp = !!z; else if (y) yClamp = !!z; paulo@89: return brush; paulo@89: }; paulo@89: brush.extent = function(z) { paulo@89: var x0, x1, y0, y1, t; paulo@89: if (!arguments.length) { paulo@89: if (x) { paulo@89: if (xExtentDomain) { paulo@89: x0 = xExtentDomain[0], x1 = xExtentDomain[1]; paulo@89: } else { paulo@89: x0 = xExtent[0], x1 = xExtent[1]; paulo@89: if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1); paulo@89: if (x1 < x0) t = x0, x0 = x1, x1 = t; paulo@89: } paulo@89: } paulo@89: if (y) { paulo@89: if (yExtentDomain) { paulo@89: y0 = yExtentDomain[0], y1 = yExtentDomain[1]; paulo@89: } else { paulo@89: y0 = yExtent[0], y1 = yExtent[1]; paulo@89: if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1); paulo@89: if (y1 < y0) t = y0, y0 = y1, y1 = t; paulo@89: } paulo@89: } paulo@89: return x && y ? [ [ x0, y0 ], [ x1, y1 ] ] : x ? [ x0, x1 ] : y && [ y0, y1 ]; paulo@89: } paulo@89: if (x) { paulo@89: x0 = z[0], x1 = z[1]; paulo@89: if (y) x0 = x0[0], x1 = x1[0]; paulo@89: xExtentDomain = [ x0, x1 ]; paulo@89: if (x.invert) x0 = x(x0), x1 = x(x1); paulo@89: if (x1 < x0) t = x0, x0 = x1, x1 = t; paulo@89: if (x0 != xExtent[0] || x1 != xExtent[1]) xExtent = [ x0, x1 ]; paulo@89: } paulo@89: if (y) { paulo@89: y0 = z[0], y1 = z[1]; paulo@89: if (x) y0 = y0[1], y1 = y1[1]; paulo@89: yExtentDomain = [ y0, y1 ]; paulo@89: if (y.invert) y0 = y(y0), y1 = y(y1); paulo@89: if (y1 < y0) t = y0, y0 = y1, y1 = t; paulo@89: if (y0 != yExtent[0] || y1 != yExtent[1]) yExtent = [ y0, y1 ]; paulo@89: } paulo@89: return brush; paulo@89: }; paulo@89: brush.clear = function() { paulo@89: if (!brush.empty()) { paulo@89: xExtent = [ 0, 0 ], yExtent = [ 0, 0 ]; paulo@89: xExtentDomain = yExtentDomain = null; paulo@89: } paulo@89: return brush; paulo@89: }; paulo@89: brush.empty = function() { paulo@89: return !!x && xExtent[0] == xExtent[1] || !!y && yExtent[0] == yExtent[1]; paulo@89: }; paulo@89: return d3.rebind(brush, event, "on"); paulo@89: }; paulo@89: var d3_svg_brushCursor = { paulo@89: n: "ns-resize", paulo@89: e: "ew-resize", paulo@89: s: "ns-resize", paulo@89: w: "ew-resize", paulo@89: nw: "nwse-resize", paulo@89: ne: "nesw-resize", paulo@89: se: "nwse-resize", paulo@89: sw: "nesw-resize" paulo@89: }; paulo@89: var d3_svg_brushResizes = [ [ "n", "e", "s", "w", "nw", "ne", "se", "sw" ], [ "e", "w" ], [ "n", "s" ], [] ]; paulo@89: var d3_time_format = d3_time.format = d3_locale_enUS.timeFormat; paulo@89: var d3_time_formatUtc = d3_time_format.utc; paulo@89: var d3_time_formatIso = d3_time_formatUtc("%Y-%m-%dT%H:%M:%S.%LZ"); paulo@89: d3_time_format.iso = Date.prototype.toISOString && +new Date("2000-01-01T00:00:00.000Z") ? d3_time_formatIsoNative : d3_time_formatIso; paulo@89: function d3_time_formatIsoNative(date) { paulo@89: return date.toISOString(); paulo@89: } paulo@89: d3_time_formatIsoNative.parse = function(string) { paulo@89: var date = new Date(string); paulo@89: return isNaN(date) ? null : date; paulo@89: }; paulo@89: d3_time_formatIsoNative.toString = d3_time_formatIso.toString; paulo@89: d3_time.second = d3_time_interval(function(date) { paulo@89: return new d3_date(Math.floor(date / 1e3) * 1e3); paulo@89: }, function(date, offset) { paulo@89: date.setTime(date.getTime() + Math.floor(offset) * 1e3); paulo@89: }, function(date) { paulo@89: return date.getSeconds(); paulo@89: }); paulo@89: d3_time.seconds = d3_time.second.range; paulo@89: d3_time.seconds.utc = d3_time.second.utc.range; paulo@89: d3_time.minute = d3_time_interval(function(date) { paulo@89: return new d3_date(Math.floor(date / 6e4) * 6e4); paulo@89: }, function(date, offset) { paulo@89: date.setTime(date.getTime() + Math.floor(offset) * 6e4); paulo@89: }, function(date) { paulo@89: return date.getMinutes(); paulo@89: }); paulo@89: d3_time.minutes = d3_time.minute.range; paulo@89: d3_time.minutes.utc = d3_time.minute.utc.range; paulo@89: d3_time.hour = d3_time_interval(function(date) { paulo@89: var timezone = date.getTimezoneOffset() / 60; paulo@89: return new d3_date((Math.floor(date / 36e5 - timezone) + timezone) * 36e5); paulo@89: }, function(date, offset) { paulo@89: date.setTime(date.getTime() + Math.floor(offset) * 36e5); paulo@89: }, function(date) { paulo@89: return date.getHours(); paulo@89: }); paulo@89: d3_time.hours = d3_time.hour.range; paulo@89: d3_time.hours.utc = d3_time.hour.utc.range; paulo@89: d3_time.month = d3_time_interval(function(date) { paulo@89: date = d3_time.day(date); paulo@89: date.setDate(1); paulo@89: return date; paulo@89: }, function(date, offset) { paulo@89: date.setMonth(date.getMonth() + offset); paulo@89: }, function(date) { paulo@89: return date.getMonth(); paulo@89: }); paulo@89: d3_time.months = d3_time.month.range; paulo@89: d3_time.months.utc = d3_time.month.utc.range; paulo@89: function d3_time_scale(linear, methods, format) { paulo@89: function scale(x) { paulo@89: return linear(x); paulo@89: } paulo@89: scale.invert = function(x) { paulo@89: return d3_time_scaleDate(linear.invert(x)); paulo@89: }; paulo@89: scale.domain = function(x) { paulo@89: if (!arguments.length) return linear.domain().map(d3_time_scaleDate); paulo@89: linear.domain(x); paulo@89: return scale; paulo@89: }; paulo@89: function tickMethod(extent, count) { paulo@89: var span = extent[1] - extent[0], target = span / count, i = d3.bisect(d3_time_scaleSteps, target); paulo@89: return i == d3_time_scaleSteps.length ? [ methods.year, d3_scale_linearTickRange(extent.map(function(d) { paulo@89: return d / 31536e6; paulo@89: }), count)[2] ] : !i ? [ d3_time_scaleMilliseconds, d3_scale_linearTickRange(extent, count)[2] ] : methods[target / d3_time_scaleSteps[i - 1] < d3_time_scaleSteps[i] / target ? i - 1 : i]; paulo@89: } paulo@89: scale.nice = function(interval, skip) { paulo@89: var domain = scale.domain(), extent = d3_scaleExtent(domain), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" && tickMethod(extent, interval); paulo@89: if (method) interval = method[0], skip = method[1]; paulo@89: function skipped(date) { paulo@89: return !isNaN(date) && !interval.range(date, d3_time_scaleDate(+date + 1), skip).length; paulo@89: } paulo@89: return scale.domain(d3_scale_nice(domain, skip > 1 ? { paulo@89: floor: function(date) { paulo@89: while (skipped(date = interval.floor(date))) date = d3_time_scaleDate(date - 1); paulo@89: return date; paulo@89: }, paulo@89: ceil: function(date) { paulo@89: while (skipped(date = interval.ceil(date))) date = d3_time_scaleDate(+date + 1); paulo@89: return date; paulo@89: } paulo@89: } : interval)); paulo@89: }; paulo@89: scale.ticks = function(interval, skip) { paulo@89: var extent = d3_scaleExtent(scale.domain()), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" ? tickMethod(extent, interval) : !interval.range && [ { paulo@89: range: interval paulo@89: }, skip ]; paulo@89: if (method) interval = method[0], skip = method[1]; paulo@89: return interval.range(extent[0], d3_time_scaleDate(+extent[1] + 1), skip < 1 ? 1 : skip); paulo@89: }; paulo@89: scale.tickFormat = function() { paulo@89: return format; paulo@89: }; paulo@89: scale.copy = function() { paulo@89: return d3_time_scale(linear.copy(), methods, format); paulo@89: }; paulo@89: return d3_scale_linearRebind(scale, linear); paulo@89: } paulo@89: function d3_time_scaleDate(t) { paulo@89: return new Date(t); paulo@89: } paulo@89: var d3_time_scaleSteps = [ 1e3, 5e3, 15e3, 3e4, 6e4, 3e5, 9e5, 18e5, 36e5, 108e5, 216e5, 432e5, 864e5, 1728e5, 6048e5, 2592e6, 7776e6, 31536e6 ]; paulo@89: var d3_time_scaleLocalMethods = [ [ d3_time.second, 1 ], [ d3_time.second, 5 ], [ d3_time.second, 15 ], [ d3_time.second, 30 ], [ d3_time.minute, 1 ], [ d3_time.minute, 5 ], [ d3_time.minute, 15 ], [ d3_time.minute, 30 ], [ d3_time.hour, 1 ], [ d3_time.hour, 3 ], [ d3_time.hour, 6 ], [ d3_time.hour, 12 ], [ d3_time.day, 1 ], [ d3_time.day, 2 ], [ d3_time.week, 1 ], [ d3_time.month, 1 ], [ d3_time.month, 3 ], [ d3_time.year, 1 ] ]; paulo@89: var d3_time_scaleLocalFormat = d3_time_format.multi([ [ ".%L", function(d) { paulo@89: return d.getMilliseconds(); paulo@89: } ], [ ":%S", function(d) { paulo@89: return d.getSeconds(); paulo@89: } ], [ "%I:%M", function(d) { paulo@89: return d.getMinutes(); paulo@89: } ], [ "%I %p", function(d) { paulo@89: return d.getHours(); paulo@89: } ], [ "%a %d", function(d) { paulo@89: return d.getDay() && d.getDate() != 1; paulo@89: } ], [ "%b %d", function(d) { paulo@89: return d.getDate() != 1; paulo@89: } ], [ "%B", function(d) { paulo@89: return d.getMonth(); paulo@89: } ], [ "%Y", d3_true ] ]); paulo@89: var d3_time_scaleMilliseconds = { paulo@89: range: function(start, stop, step) { paulo@89: return d3.range(Math.ceil(start / step) * step, +stop, step).map(d3_time_scaleDate); paulo@89: }, paulo@89: floor: d3_identity, paulo@89: ceil: d3_identity paulo@89: }; paulo@89: d3_time_scaleLocalMethods.year = d3_time.year; paulo@89: d3_time.scale = function() { paulo@89: return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat); paulo@89: }; paulo@89: var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) { paulo@89: return [ m[0].utc, m[1] ]; paulo@89: }); paulo@89: var d3_time_scaleUtcFormat = d3_time_formatUtc.multi([ [ ".%L", function(d) { paulo@89: return d.getUTCMilliseconds(); paulo@89: } ], [ ":%S", function(d) { paulo@89: return d.getUTCSeconds(); paulo@89: } ], [ "%I:%M", function(d) { paulo@89: return d.getUTCMinutes(); paulo@89: } ], [ "%I %p", function(d) { paulo@89: return d.getUTCHours(); paulo@89: } ], [ "%a %d", function(d) { paulo@89: return d.getUTCDay() && d.getUTCDate() != 1; paulo@89: } ], [ "%b %d", function(d) { paulo@89: return d.getUTCDate() != 1; paulo@89: } ], [ "%B", function(d) { paulo@89: return d.getUTCMonth(); paulo@89: } ], [ "%Y", d3_true ] ]); paulo@89: d3_time_scaleUtcMethods.year = d3_time.year.utc; paulo@89: d3_time.scale.utc = function() { paulo@89: return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_time_scaleUtcFormat); paulo@89: }; paulo@89: d3.text = d3_xhrType(function(request) { paulo@89: return request.responseText; paulo@89: }); paulo@89: d3.json = function(url, callback) { paulo@89: return d3_xhr(url, "application/json", d3_json, callback); paulo@89: }; paulo@89: function d3_json(request) { paulo@89: return JSON.parse(request.responseText); paulo@89: } paulo@89: d3.html = function(url, callback) { paulo@89: return d3_xhr(url, "text/html", d3_html, callback); paulo@89: }; paulo@89: function d3_html(request) { paulo@89: var range = d3_document.createRange(); paulo@89: range.selectNode(d3_document.body); paulo@89: return range.createContextualFragment(request.responseText); paulo@89: } paulo@89: d3.xml = d3_xhrType(function(request) { paulo@89: return request.responseXML; paulo@89: }); paulo@89: if (typeof define === "function" && define.amd) this.d3 = d3, define(d3); else if (typeof module === "object" && module.exports) module.exports = d3; else this.d3 = d3; paulo@89: }();