Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | !function() { |
michael@0 | 2 | var d3 = { |
michael@0 | 3 | version: "3.4.2" |
michael@0 | 4 | }; |
michael@0 | 5 | if (!Date.now) Date.now = function() { |
michael@0 | 6 | return +new Date(); |
michael@0 | 7 | }; |
michael@0 | 8 | var d3_arraySlice = [].slice, d3_array = function(list) { |
michael@0 | 9 | return d3_arraySlice.call(list); |
michael@0 | 10 | }; |
michael@0 | 11 | var d3_document = document, d3_documentElement = d3_document.documentElement, d3_window = window; |
michael@0 | 12 | try { |
michael@0 | 13 | d3_array(d3_documentElement.childNodes)[0].nodeType; |
michael@0 | 14 | } catch (e) { |
michael@0 | 15 | d3_array = function(list) { |
michael@0 | 16 | var i = list.length, array = new Array(i); |
michael@0 | 17 | while (i--) array[i] = list[i]; |
michael@0 | 18 | return array; |
michael@0 | 19 | }; |
michael@0 | 20 | } |
michael@0 | 21 | try { |
michael@0 | 22 | d3_document.createElement("div").style.setProperty("opacity", 0, ""); |
michael@0 | 23 | } catch (error) { |
michael@0 | 24 | var d3_element_prototype = d3_window.Element.prototype, d3_element_setAttribute = d3_element_prototype.setAttribute, d3_element_setAttributeNS = d3_element_prototype.setAttributeNS, d3_style_prototype = d3_window.CSSStyleDeclaration.prototype, d3_style_setProperty = d3_style_prototype.setProperty; |
michael@0 | 25 | d3_element_prototype.setAttribute = function(name, value) { |
michael@0 | 26 | d3_element_setAttribute.call(this, name, value + ""); |
michael@0 | 27 | }; |
michael@0 | 28 | d3_element_prototype.setAttributeNS = function(space, local, value) { |
michael@0 | 29 | d3_element_setAttributeNS.call(this, space, local, value + ""); |
michael@0 | 30 | }; |
michael@0 | 31 | d3_style_prototype.setProperty = function(name, value, priority) { |
michael@0 | 32 | d3_style_setProperty.call(this, name, value + "", priority); |
michael@0 | 33 | }; |
michael@0 | 34 | } |
michael@0 | 35 | d3.ascending = function(a, b) { |
michael@0 | 36 | return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN; |
michael@0 | 37 | }; |
michael@0 | 38 | d3.descending = function(a, b) { |
michael@0 | 39 | return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN; |
michael@0 | 40 | }; |
michael@0 | 41 | d3.min = function(array, f) { |
michael@0 | 42 | var i = -1, n = array.length, a, b; |
michael@0 | 43 | if (arguments.length === 1) { |
michael@0 | 44 | while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined; |
michael@0 | 45 | while (++i < n) if ((b = array[i]) != null && a > b) a = b; |
michael@0 | 46 | } else { |
michael@0 | 47 | while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined; |
michael@0 | 48 | while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b; |
michael@0 | 49 | } |
michael@0 | 50 | return a; |
michael@0 | 51 | }; |
michael@0 | 52 | d3.max = function(array, f) { |
michael@0 | 53 | var i = -1, n = array.length, a, b; |
michael@0 | 54 | if (arguments.length === 1) { |
michael@0 | 55 | while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined; |
michael@0 | 56 | while (++i < n) if ((b = array[i]) != null && b > a) a = b; |
michael@0 | 57 | } else { |
michael@0 | 58 | while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined; |
michael@0 | 59 | while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b; |
michael@0 | 60 | } |
michael@0 | 61 | return a; |
michael@0 | 62 | }; |
michael@0 | 63 | d3.extent = function(array, f) { |
michael@0 | 64 | var i = -1, n = array.length, a, b, c; |
michael@0 | 65 | if (arguments.length === 1) { |
michael@0 | 66 | while (++i < n && !((a = c = array[i]) != null && a <= a)) a = c = undefined; |
michael@0 | 67 | while (++i < n) if ((b = array[i]) != null) { |
michael@0 | 68 | if (a > b) a = b; |
michael@0 | 69 | if (c < b) c = b; |
michael@0 | 70 | } |
michael@0 | 71 | } else { |
michael@0 | 72 | while (++i < n && !((a = c = f.call(array, array[i], i)) != null && a <= a)) a = undefined; |
michael@0 | 73 | while (++i < n) if ((b = f.call(array, array[i], i)) != null) { |
michael@0 | 74 | if (a > b) a = b; |
michael@0 | 75 | if (c < b) c = b; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | return [ a, c ]; |
michael@0 | 79 | }; |
michael@0 | 80 | d3.sum = function(array, f) { |
michael@0 | 81 | var s = 0, n = array.length, a, i = -1; |
michael@0 | 82 | if (arguments.length === 1) { |
michael@0 | 83 | while (++i < n) if (!isNaN(a = +array[i])) s += a; |
michael@0 | 84 | } else { |
michael@0 | 85 | while (++i < n) if (!isNaN(a = +f.call(array, array[i], i))) s += a; |
michael@0 | 86 | } |
michael@0 | 87 | return s; |
michael@0 | 88 | }; |
michael@0 | 89 | function d3_number(x) { |
michael@0 | 90 | return x != null && !isNaN(x); |
michael@0 | 91 | } |
michael@0 | 92 | d3.mean = function(array, f) { |
michael@0 | 93 | var n = array.length, a, m = 0, i = -1, j = 0; |
michael@0 | 94 | if (arguments.length === 1) { |
michael@0 | 95 | while (++i < n) if (d3_number(a = array[i])) m += (a - m) / ++j; |
michael@0 | 96 | } else { |
michael@0 | 97 | while (++i < n) if (d3_number(a = f.call(array, array[i], i))) m += (a - m) / ++j; |
michael@0 | 98 | } |
michael@0 | 99 | return j ? m : undefined; |
michael@0 | 100 | }; |
michael@0 | 101 | d3.quantile = function(values, p) { |
michael@0 | 102 | var H = (values.length - 1) * p + 1, h = Math.floor(H), v = +values[h - 1], e = H - h; |
michael@0 | 103 | return e ? v + e * (values[h] - v) : v; |
michael@0 | 104 | }; |
michael@0 | 105 | d3.median = function(array, f) { |
michael@0 | 106 | if (arguments.length > 1) array = array.map(f); |
michael@0 | 107 | array = array.filter(d3_number); |
michael@0 | 108 | return array.length ? d3.quantile(array.sort(d3.ascending), .5) : undefined; |
michael@0 | 109 | }; |
michael@0 | 110 | d3.bisector = function(f) { |
michael@0 | 111 | return { |
michael@0 | 112 | left: function(a, x, lo, hi) { |
michael@0 | 113 | if (arguments.length < 3) lo = 0; |
michael@0 | 114 | if (arguments.length < 4) hi = a.length; |
michael@0 | 115 | while (lo < hi) { |
michael@0 | 116 | var mid = lo + hi >>> 1; |
michael@0 | 117 | if (f.call(a, a[mid], mid) < x) lo = mid + 1; else hi = mid; |
michael@0 | 118 | } |
michael@0 | 119 | return lo; |
michael@0 | 120 | }, |
michael@0 | 121 | right: function(a, x, lo, hi) { |
michael@0 | 122 | if (arguments.length < 3) lo = 0; |
michael@0 | 123 | if (arguments.length < 4) hi = a.length; |
michael@0 | 124 | while (lo < hi) { |
michael@0 | 125 | var mid = lo + hi >>> 1; |
michael@0 | 126 | if (x < f.call(a, a[mid], mid)) hi = mid; else lo = mid + 1; |
michael@0 | 127 | } |
michael@0 | 128 | return lo; |
michael@0 | 129 | } |
michael@0 | 130 | }; |
michael@0 | 131 | }; |
michael@0 | 132 | var d3_bisector = d3.bisector(function(d) { |
michael@0 | 133 | return d; |
michael@0 | 134 | }); |
michael@0 | 135 | d3.bisectLeft = d3_bisector.left; |
michael@0 | 136 | d3.bisect = d3.bisectRight = d3_bisector.right; |
michael@0 | 137 | d3.shuffle = function(array) { |
michael@0 | 138 | var m = array.length, t, i; |
michael@0 | 139 | while (m) { |
michael@0 | 140 | i = Math.random() * m-- | 0; |
michael@0 | 141 | t = array[m], array[m] = array[i], array[i] = t; |
michael@0 | 142 | } |
michael@0 | 143 | return array; |
michael@0 | 144 | }; |
michael@0 | 145 | d3.permute = function(array, indexes) { |
michael@0 | 146 | var i = indexes.length, permutes = new Array(i); |
michael@0 | 147 | while (i--) permutes[i] = array[indexes[i]]; |
michael@0 | 148 | return permutes; |
michael@0 | 149 | }; |
michael@0 | 150 | d3.pairs = function(array) { |
michael@0 | 151 | var i = 0, n = array.length - 1, p0, p1 = array[0], pairs = new Array(n < 0 ? 0 : n); |
michael@0 | 152 | while (i < n) pairs[i] = [ p0 = p1, p1 = array[++i] ]; |
michael@0 | 153 | return pairs; |
michael@0 | 154 | }; |
michael@0 | 155 | d3.zip = function() { |
michael@0 | 156 | if (!(n = arguments.length)) return []; |
michael@0 | 157 | for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m; ) { |
michael@0 | 158 | for (var j = -1, n, zip = zips[i] = new Array(n); ++j < n; ) { |
michael@0 | 159 | zip[j] = arguments[j][i]; |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | return zips; |
michael@0 | 163 | }; |
michael@0 | 164 | function d3_zipLength(d) { |
michael@0 | 165 | return d.length; |
michael@0 | 166 | } |
michael@0 | 167 | d3.transpose = function(matrix) { |
michael@0 | 168 | return d3.zip.apply(d3, matrix); |
michael@0 | 169 | }; |
michael@0 | 170 | d3.keys = function(map) { |
michael@0 | 171 | var keys = []; |
michael@0 | 172 | for (var key in map) keys.push(key); |
michael@0 | 173 | return keys; |
michael@0 | 174 | }; |
michael@0 | 175 | d3.values = function(map) { |
michael@0 | 176 | var values = []; |
michael@0 | 177 | for (var key in map) values.push(map[key]); |
michael@0 | 178 | return values; |
michael@0 | 179 | }; |
michael@0 | 180 | d3.entries = function(map) { |
michael@0 | 181 | var entries = []; |
michael@0 | 182 | for (var key in map) entries.push({ |
michael@0 | 183 | key: key, |
michael@0 | 184 | value: map[key] |
michael@0 | 185 | }); |
michael@0 | 186 | return entries; |
michael@0 | 187 | }; |
michael@0 | 188 | d3.merge = function(arrays) { |
michael@0 | 189 | var n = arrays.length, m, i = -1, j = 0, merged, array; |
michael@0 | 190 | while (++i < n) j += arrays[i].length; |
michael@0 | 191 | merged = new Array(j); |
michael@0 | 192 | while (--n >= 0) { |
michael@0 | 193 | array = arrays[n]; |
michael@0 | 194 | m = array.length; |
michael@0 | 195 | while (--m >= 0) { |
michael@0 | 196 | merged[--j] = array[m]; |
michael@0 | 197 | } |
michael@0 | 198 | } |
michael@0 | 199 | return merged; |
michael@0 | 200 | }; |
michael@0 | 201 | var abs = Math.abs; |
michael@0 | 202 | d3.range = function(start, stop, step) { |
michael@0 | 203 | if (arguments.length < 3) { |
michael@0 | 204 | step = 1; |
michael@0 | 205 | if (arguments.length < 2) { |
michael@0 | 206 | stop = start; |
michael@0 | 207 | start = 0; |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | if ((stop - start) / step === Infinity) throw new Error("infinite range"); |
michael@0 | 211 | var range = [], k = d3_range_integerScale(abs(step)), i = -1, j; |
michael@0 | 212 | start *= k, stop *= k, step *= k; |
michael@0 | 213 | if (step < 0) while ((j = start + step * ++i) > stop) range.push(j / k); else while ((j = start + step * ++i) < stop) range.push(j / k); |
michael@0 | 214 | return range; |
michael@0 | 215 | }; |
michael@0 | 216 | function d3_range_integerScale(x) { |
michael@0 | 217 | var k = 1; |
michael@0 | 218 | while (x * k % 1) k *= 10; |
michael@0 | 219 | return k; |
michael@0 | 220 | } |
michael@0 | 221 | function d3_class(ctor, properties) { |
michael@0 | 222 | try { |
michael@0 | 223 | for (var key in properties) { |
michael@0 | 224 | Object.defineProperty(ctor.prototype, key, { |
michael@0 | 225 | value: properties[key], |
michael@0 | 226 | enumerable: false |
michael@0 | 227 | }); |
michael@0 | 228 | } |
michael@0 | 229 | } catch (e) { |
michael@0 | 230 | ctor.prototype = properties; |
michael@0 | 231 | } |
michael@0 | 232 | } |
michael@0 | 233 | d3.map = function(object) { |
michael@0 | 234 | var map = new d3_Map(); |
michael@0 | 235 | if (object instanceof d3_Map) object.forEach(function(key, value) { |
michael@0 | 236 | map.set(key, value); |
michael@0 | 237 | }); else for (var key in object) map.set(key, object[key]); |
michael@0 | 238 | return map; |
michael@0 | 239 | }; |
michael@0 | 240 | function d3_Map() {} |
michael@0 | 241 | d3_class(d3_Map, { |
michael@0 | 242 | has: d3_map_has, |
michael@0 | 243 | get: function(key) { |
michael@0 | 244 | return this[d3_map_prefix + key]; |
michael@0 | 245 | }, |
michael@0 | 246 | set: function(key, value) { |
michael@0 | 247 | return this[d3_map_prefix + key] = value; |
michael@0 | 248 | }, |
michael@0 | 249 | remove: d3_map_remove, |
michael@0 | 250 | keys: d3_map_keys, |
michael@0 | 251 | values: function() { |
michael@0 | 252 | var values = []; |
michael@0 | 253 | this.forEach(function(key, value) { |
michael@0 | 254 | values.push(value); |
michael@0 | 255 | }); |
michael@0 | 256 | return values; |
michael@0 | 257 | }, |
michael@0 | 258 | entries: function() { |
michael@0 | 259 | var entries = []; |
michael@0 | 260 | this.forEach(function(key, value) { |
michael@0 | 261 | entries.push({ |
michael@0 | 262 | key: key, |
michael@0 | 263 | value: value |
michael@0 | 264 | }); |
michael@0 | 265 | }); |
michael@0 | 266 | return entries; |
michael@0 | 267 | }, |
michael@0 | 268 | size: d3_map_size, |
michael@0 | 269 | empty: d3_map_empty, |
michael@0 | 270 | forEach: function(f) { |
michael@0 | 271 | for (var key in this) if (key.charCodeAt(0) === d3_map_prefixCode) f.call(this, key.substring(1), this[key]); |
michael@0 | 272 | } |
michael@0 | 273 | }); |
michael@0 | 274 | var d3_map_prefix = "\x00", d3_map_prefixCode = d3_map_prefix.charCodeAt(0); |
michael@0 | 275 | function d3_map_has(key) { |
michael@0 | 276 | return d3_map_prefix + key in this; |
michael@0 | 277 | } |
michael@0 | 278 | function d3_map_remove(key) { |
michael@0 | 279 | key = d3_map_prefix + key; |
michael@0 | 280 | return key in this && delete this[key]; |
michael@0 | 281 | } |
michael@0 | 282 | function d3_map_keys() { |
michael@0 | 283 | var keys = []; |
michael@0 | 284 | this.forEach(function(key) { |
michael@0 | 285 | keys.push(key); |
michael@0 | 286 | }); |
michael@0 | 287 | return keys; |
michael@0 | 288 | } |
michael@0 | 289 | function d3_map_size() { |
michael@0 | 290 | var size = 0; |
michael@0 | 291 | for (var key in this) if (key.charCodeAt(0) === d3_map_prefixCode) ++size; |
michael@0 | 292 | return size; |
michael@0 | 293 | } |
michael@0 | 294 | function d3_map_empty() { |
michael@0 | 295 | for (var key in this) if (key.charCodeAt(0) === d3_map_prefixCode) return false; |
michael@0 | 296 | return true; |
michael@0 | 297 | } |
michael@0 | 298 | d3.nest = function() { |
michael@0 | 299 | var nest = {}, keys = [], sortKeys = [], sortValues, rollup; |
michael@0 | 300 | function map(mapType, array, depth) { |
michael@0 | 301 | if (depth >= keys.length) return rollup ? rollup.call(nest, array) : sortValues ? array.sort(sortValues) : array; |
michael@0 | 302 | var i = -1, n = array.length, key = keys[depth++], keyValue, object, setter, valuesByKey = new d3_Map(), values; |
michael@0 | 303 | while (++i < n) { |
michael@0 | 304 | if (values = valuesByKey.get(keyValue = key(object = array[i]))) { |
michael@0 | 305 | values.push(object); |
michael@0 | 306 | } else { |
michael@0 | 307 | valuesByKey.set(keyValue, [ object ]); |
michael@0 | 308 | } |
michael@0 | 309 | } |
michael@0 | 310 | if (mapType) { |
michael@0 | 311 | object = mapType(); |
michael@0 | 312 | setter = function(keyValue, values) { |
michael@0 | 313 | object.set(keyValue, map(mapType, values, depth)); |
michael@0 | 314 | }; |
michael@0 | 315 | } else { |
michael@0 | 316 | object = {}; |
michael@0 | 317 | setter = function(keyValue, values) { |
michael@0 | 318 | object[keyValue] = map(mapType, values, depth); |
michael@0 | 319 | }; |
michael@0 | 320 | } |
michael@0 | 321 | valuesByKey.forEach(setter); |
michael@0 | 322 | return object; |
michael@0 | 323 | } |
michael@0 | 324 | function entries(map, depth) { |
michael@0 | 325 | if (depth >= keys.length) return map; |
michael@0 | 326 | var array = [], sortKey = sortKeys[depth++]; |
michael@0 | 327 | map.forEach(function(key, keyMap) { |
michael@0 | 328 | array.push({ |
michael@0 | 329 | key: key, |
michael@0 | 330 | values: entries(keyMap, depth) |
michael@0 | 331 | }); |
michael@0 | 332 | }); |
michael@0 | 333 | return sortKey ? array.sort(function(a, b) { |
michael@0 | 334 | return sortKey(a.key, b.key); |
michael@0 | 335 | }) : array; |
michael@0 | 336 | } |
michael@0 | 337 | nest.map = function(array, mapType) { |
michael@0 | 338 | return map(mapType, array, 0); |
michael@0 | 339 | }; |
michael@0 | 340 | nest.entries = function(array) { |
michael@0 | 341 | return entries(map(d3.map, array, 0), 0); |
michael@0 | 342 | }; |
michael@0 | 343 | nest.key = function(d) { |
michael@0 | 344 | keys.push(d); |
michael@0 | 345 | return nest; |
michael@0 | 346 | }; |
michael@0 | 347 | nest.sortKeys = function(order) { |
michael@0 | 348 | sortKeys[keys.length - 1] = order; |
michael@0 | 349 | return nest; |
michael@0 | 350 | }; |
michael@0 | 351 | nest.sortValues = function(order) { |
michael@0 | 352 | sortValues = order; |
michael@0 | 353 | return nest; |
michael@0 | 354 | }; |
michael@0 | 355 | nest.rollup = function(f) { |
michael@0 | 356 | rollup = f; |
michael@0 | 357 | return nest; |
michael@0 | 358 | }; |
michael@0 | 359 | return nest; |
michael@0 | 360 | }; |
michael@0 | 361 | d3.set = function(array) { |
michael@0 | 362 | var set = new d3_Set(); |
michael@0 | 363 | if (array) for (var i = 0, n = array.length; i < n; ++i) set.add(array[i]); |
michael@0 | 364 | return set; |
michael@0 | 365 | }; |
michael@0 | 366 | function d3_Set() {} |
michael@0 | 367 | d3_class(d3_Set, { |
michael@0 | 368 | has: d3_map_has, |
michael@0 | 369 | add: function(value) { |
michael@0 | 370 | this[d3_map_prefix + value] = true; |
michael@0 | 371 | return value; |
michael@0 | 372 | }, |
michael@0 | 373 | remove: function(value) { |
michael@0 | 374 | value = d3_map_prefix + value; |
michael@0 | 375 | return value in this && delete this[value]; |
michael@0 | 376 | }, |
michael@0 | 377 | values: d3_map_keys, |
michael@0 | 378 | size: d3_map_size, |
michael@0 | 379 | empty: d3_map_empty, |
michael@0 | 380 | forEach: function(f) { |
michael@0 | 381 | for (var value in this) if (value.charCodeAt(0) === d3_map_prefixCode) f.call(this, value.substring(1)); |
michael@0 | 382 | } |
michael@0 | 383 | }); |
michael@0 | 384 | d3.behavior = {}; |
michael@0 | 385 | d3.rebind = function(target, source) { |
michael@0 | 386 | var i = 1, n = arguments.length, method; |
michael@0 | 387 | while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]); |
michael@0 | 388 | return target; |
michael@0 | 389 | }; |
michael@0 | 390 | function d3_rebind(target, source, method) { |
michael@0 | 391 | return function() { |
michael@0 | 392 | var value = method.apply(source, arguments); |
michael@0 | 393 | return value === source ? target : value; |
michael@0 | 394 | }; |
michael@0 | 395 | } |
michael@0 | 396 | function d3_vendorSymbol(object, name) { |
michael@0 | 397 | if (name in object) return name; |
michael@0 | 398 | name = name.charAt(0).toUpperCase() + name.substring(1); |
michael@0 | 399 | for (var i = 0, n = d3_vendorPrefixes.length; i < n; ++i) { |
michael@0 | 400 | var prefixName = d3_vendorPrefixes[i] + name; |
michael@0 | 401 | if (prefixName in object) return prefixName; |
michael@0 | 402 | } |
michael@0 | 403 | } |
michael@0 | 404 | var d3_vendorPrefixes = [ "webkit", "ms", "moz", "Moz", "o", "O" ]; |
michael@0 | 405 | function d3_noop() {} |
michael@0 | 406 | d3.dispatch = function() { |
michael@0 | 407 | var dispatch = new d3_dispatch(), i = -1, n = arguments.length; |
michael@0 | 408 | while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); |
michael@0 | 409 | return dispatch; |
michael@0 | 410 | }; |
michael@0 | 411 | function d3_dispatch() {} |
michael@0 | 412 | d3_dispatch.prototype.on = function(type, listener) { |
michael@0 | 413 | var i = type.indexOf("."), name = ""; |
michael@0 | 414 | if (i >= 0) { |
michael@0 | 415 | name = type.substring(i + 1); |
michael@0 | 416 | type = type.substring(0, i); |
michael@0 | 417 | } |
michael@0 | 418 | if (type) return arguments.length < 2 ? this[type].on(name) : this[type].on(name, listener); |
michael@0 | 419 | if (arguments.length === 2) { |
michael@0 | 420 | if (listener == null) for (type in this) { |
michael@0 | 421 | if (this.hasOwnProperty(type)) this[type].on(name, null); |
michael@0 | 422 | } |
michael@0 | 423 | return this; |
michael@0 | 424 | } |
michael@0 | 425 | }; |
michael@0 | 426 | function d3_dispatch_event(dispatch) { |
michael@0 | 427 | var listeners = [], listenerByName = new d3_Map(); |
michael@0 | 428 | function event() { |
michael@0 | 429 | var z = listeners, i = -1, n = z.length, l; |
michael@0 | 430 | while (++i < n) if (l = z[i].on) l.apply(this, arguments); |
michael@0 | 431 | return dispatch; |
michael@0 | 432 | } |
michael@0 | 433 | event.on = function(name, listener) { |
michael@0 | 434 | var l = listenerByName.get(name), i; |
michael@0 | 435 | if (arguments.length < 2) return l && l.on; |
michael@0 | 436 | if (l) { |
michael@0 | 437 | l.on = null; |
michael@0 | 438 | listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1)); |
michael@0 | 439 | listenerByName.remove(name); |
michael@0 | 440 | } |
michael@0 | 441 | if (listener) listeners.push(listenerByName.set(name, { |
michael@0 | 442 | on: listener |
michael@0 | 443 | })); |
michael@0 | 444 | return dispatch; |
michael@0 | 445 | }; |
michael@0 | 446 | return event; |
michael@0 | 447 | } |
michael@0 | 448 | d3.event = null; |
michael@0 | 449 | function d3_eventPreventDefault() { |
michael@0 | 450 | d3.event.preventDefault(); |
michael@0 | 451 | } |
michael@0 | 452 | function d3_eventSource() { |
michael@0 | 453 | var e = d3.event, s; |
michael@0 | 454 | while (s = e.sourceEvent) e = s; |
michael@0 | 455 | return e; |
michael@0 | 456 | } |
michael@0 | 457 | function d3_eventDispatch(target) { |
michael@0 | 458 | var dispatch = new d3_dispatch(), i = 0, n = arguments.length; |
michael@0 | 459 | while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); |
michael@0 | 460 | dispatch.of = function(thiz, argumentz) { |
michael@0 | 461 | return function(e1) { |
michael@0 | 462 | try { |
michael@0 | 463 | var e0 = e1.sourceEvent = d3.event; |
michael@0 | 464 | e1.target = target; |
michael@0 | 465 | d3.event = e1; |
michael@0 | 466 | dispatch[e1.type].apply(thiz, argumentz); |
michael@0 | 467 | } finally { |
michael@0 | 468 | d3.event = e0; |
michael@0 | 469 | } |
michael@0 | 470 | }; |
michael@0 | 471 | }; |
michael@0 | 472 | return dispatch; |
michael@0 | 473 | } |
michael@0 | 474 | d3.requote = function(s) { |
michael@0 | 475 | return s.replace(d3_requote_re, "\\$&"); |
michael@0 | 476 | }; |
michael@0 | 477 | var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g; |
michael@0 | 478 | var d3_subclass = {}.__proto__ ? function(object, prototype) { |
michael@0 | 479 | object.__proto__ = prototype; |
michael@0 | 480 | } : function(object, prototype) { |
michael@0 | 481 | for (var property in prototype) object[property] = prototype[property]; |
michael@0 | 482 | }; |
michael@0 | 483 | function d3_selection(groups) { |
michael@0 | 484 | d3_subclass(groups, d3_selectionPrototype); |
michael@0 | 485 | return groups; |
michael@0 | 486 | } |
michael@0 | 487 | var d3_select = function(s, n) { |
michael@0 | 488 | return n.querySelector(s); |
michael@0 | 489 | }, d3_selectAll = function(s, n) { |
michael@0 | 490 | return n.querySelectorAll(s); |
michael@0 | 491 | }, d3_selectMatcher = d3_documentElement[d3_vendorSymbol(d3_documentElement, "matchesSelector")], d3_selectMatches = function(n, s) { |
michael@0 | 492 | return d3_selectMatcher.call(n, s); |
michael@0 | 493 | }; |
michael@0 | 494 | if (typeof Sizzle === "function") { |
michael@0 | 495 | d3_select = function(s, n) { |
michael@0 | 496 | return Sizzle(s, n)[0] || null; |
michael@0 | 497 | }; |
michael@0 | 498 | d3_selectAll = function(s, n) { |
michael@0 | 499 | return Sizzle.uniqueSort(Sizzle(s, n)); |
michael@0 | 500 | }; |
michael@0 | 501 | d3_selectMatches = Sizzle.matchesSelector; |
michael@0 | 502 | } |
michael@0 | 503 | d3.selection = function() { |
michael@0 | 504 | return d3_selectionRoot; |
michael@0 | 505 | }; |
michael@0 | 506 | var d3_selectionPrototype = d3.selection.prototype = []; |
michael@0 | 507 | d3_selectionPrototype.select = function(selector) { |
michael@0 | 508 | var subgroups = [], subgroup, subnode, group, node; |
michael@0 | 509 | selector = d3_selection_selector(selector); |
michael@0 | 510 | for (var j = -1, m = this.length; ++j < m; ) { |
michael@0 | 511 | subgroups.push(subgroup = []); |
michael@0 | 512 | subgroup.parentNode = (group = this[j]).parentNode; |
michael@0 | 513 | for (var i = -1, n = group.length; ++i < n; ) { |
michael@0 | 514 | if (node = group[i]) { |
michael@0 | 515 | subgroup.push(subnode = selector.call(node, node.__data__, i, j)); |
michael@0 | 516 | if (subnode && "__data__" in node) subnode.__data__ = node.__data__; |
michael@0 | 517 | } else { |
michael@0 | 518 | subgroup.push(null); |
michael@0 | 519 | } |
michael@0 | 520 | } |
michael@0 | 521 | } |
michael@0 | 522 | return d3_selection(subgroups); |
michael@0 | 523 | }; |
michael@0 | 524 | function d3_selection_selector(selector) { |
michael@0 | 525 | return typeof selector === "function" ? selector : function() { |
michael@0 | 526 | return d3_select(selector, this); |
michael@0 | 527 | }; |
michael@0 | 528 | } |
michael@0 | 529 | d3_selectionPrototype.selectAll = function(selector) { |
michael@0 | 530 | var subgroups = [], subgroup, node; |
michael@0 | 531 | selector = d3_selection_selectorAll(selector); |
michael@0 | 532 | for (var j = -1, m = this.length; ++j < m; ) { |
michael@0 | 533 | for (var group = this[j], i = -1, n = group.length; ++i < n; ) { |
michael@0 | 534 | if (node = group[i]) { |
michael@0 | 535 | subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i, j))); |
michael@0 | 536 | subgroup.parentNode = node; |
michael@0 | 537 | } |
michael@0 | 538 | } |
michael@0 | 539 | } |
michael@0 | 540 | return d3_selection(subgroups); |
michael@0 | 541 | }; |
michael@0 | 542 | function d3_selection_selectorAll(selector) { |
michael@0 | 543 | return typeof selector === "function" ? selector : function() { |
michael@0 | 544 | return d3_selectAll(selector, this); |
michael@0 | 545 | }; |
michael@0 | 546 | } |
michael@0 | 547 | var d3_nsPrefix = { |
michael@0 | 548 | svg: "http://www.w3.org/2000/svg", |
michael@0 | 549 | xhtml: "http://www.w3.org/1999/xhtml", |
michael@0 | 550 | xlink: "http://www.w3.org/1999/xlink", |
michael@0 | 551 | xml: "http://www.w3.org/XML/1998/namespace", |
michael@0 | 552 | xmlns: "http://www.w3.org/2000/xmlns/" |
michael@0 | 553 | }; |
michael@0 | 554 | d3.ns = { |
michael@0 | 555 | prefix: d3_nsPrefix, |
michael@0 | 556 | qualify: function(name) { |
michael@0 | 557 | var i = name.indexOf(":"), prefix = name; |
michael@0 | 558 | if (i >= 0) { |
michael@0 | 559 | prefix = name.substring(0, i); |
michael@0 | 560 | name = name.substring(i + 1); |
michael@0 | 561 | } |
michael@0 | 562 | return d3_nsPrefix.hasOwnProperty(prefix) ? { |
michael@0 | 563 | space: d3_nsPrefix[prefix], |
michael@0 | 564 | local: name |
michael@0 | 565 | } : name; |
michael@0 | 566 | } |
michael@0 | 567 | }; |
michael@0 | 568 | d3_selectionPrototype.attr = function(name, value) { |
michael@0 | 569 | if (arguments.length < 2) { |
michael@0 | 570 | if (typeof name === "string") { |
michael@0 | 571 | var node = this.node(); |
michael@0 | 572 | name = d3.ns.qualify(name); |
michael@0 | 573 | return name.local ? node.getAttributeNS(name.space, name.local) : node.getAttribute(name); |
michael@0 | 574 | } |
michael@0 | 575 | for (value in name) this.each(d3_selection_attr(value, name[value])); |
michael@0 | 576 | return this; |
michael@0 | 577 | } |
michael@0 | 578 | return this.each(d3_selection_attr(name, value)); |
michael@0 | 579 | }; |
michael@0 | 580 | function d3_selection_attr(name, value) { |
michael@0 | 581 | name = d3.ns.qualify(name); |
michael@0 | 582 | function attrNull() { |
michael@0 | 583 | this.removeAttribute(name); |
michael@0 | 584 | } |
michael@0 | 585 | function attrNullNS() { |
michael@0 | 586 | this.removeAttributeNS(name.space, name.local); |
michael@0 | 587 | } |
michael@0 | 588 | function attrConstant() { |
michael@0 | 589 | this.setAttribute(name, value); |
michael@0 | 590 | } |
michael@0 | 591 | function attrConstantNS() { |
michael@0 | 592 | this.setAttributeNS(name.space, name.local, value); |
michael@0 | 593 | } |
michael@0 | 594 | function attrFunction() { |
michael@0 | 595 | var x = value.apply(this, arguments); |
michael@0 | 596 | if (x == null) this.removeAttribute(name); else this.setAttribute(name, x); |
michael@0 | 597 | } |
michael@0 | 598 | function attrFunctionNS() { |
michael@0 | 599 | var x = value.apply(this, arguments); |
michael@0 | 600 | if (x == null) this.removeAttributeNS(name.space, name.local); else this.setAttributeNS(name.space, name.local, x); |
michael@0 | 601 | } |
michael@0 | 602 | return value == null ? name.local ? attrNullNS : attrNull : typeof value === "function" ? name.local ? attrFunctionNS : attrFunction : name.local ? attrConstantNS : attrConstant; |
michael@0 | 603 | } |
michael@0 | 604 | function d3_collapse(s) { |
michael@0 | 605 | return s.trim().replace(/\s+/g, " "); |
michael@0 | 606 | } |
michael@0 | 607 | d3_selectionPrototype.classed = function(name, value) { |
michael@0 | 608 | if (arguments.length < 2) { |
michael@0 | 609 | if (typeof name === "string") { |
michael@0 | 610 | var node = this.node(), n = (name = d3_selection_classes(name)).length, i = -1; |
michael@0 | 611 | if (value = node.classList) { |
michael@0 | 612 | while (++i < n) if (!value.contains(name[i])) return false; |
michael@0 | 613 | } else { |
michael@0 | 614 | value = node.getAttribute("class"); |
michael@0 | 615 | while (++i < n) if (!d3_selection_classedRe(name[i]).test(value)) return false; |
michael@0 | 616 | } |
michael@0 | 617 | return true; |
michael@0 | 618 | } |
michael@0 | 619 | for (value in name) this.each(d3_selection_classed(value, name[value])); |
michael@0 | 620 | return this; |
michael@0 | 621 | } |
michael@0 | 622 | return this.each(d3_selection_classed(name, value)); |
michael@0 | 623 | }; |
michael@0 | 624 | function d3_selection_classedRe(name) { |
michael@0 | 625 | return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g"); |
michael@0 | 626 | } |
michael@0 | 627 | function d3_selection_classes(name) { |
michael@0 | 628 | return name.trim().split(/^|\s+/); |
michael@0 | 629 | } |
michael@0 | 630 | function d3_selection_classed(name, value) { |
michael@0 | 631 | name = d3_selection_classes(name).map(d3_selection_classedName); |
michael@0 | 632 | var n = name.length; |
michael@0 | 633 | function classedConstant() { |
michael@0 | 634 | var i = -1; |
michael@0 | 635 | while (++i < n) name[i](this, value); |
michael@0 | 636 | } |
michael@0 | 637 | function classedFunction() { |
michael@0 | 638 | var i = -1, x = value.apply(this, arguments); |
michael@0 | 639 | while (++i < n) name[i](this, x); |
michael@0 | 640 | } |
michael@0 | 641 | return typeof value === "function" ? classedFunction : classedConstant; |
michael@0 | 642 | } |
michael@0 | 643 | function d3_selection_classedName(name) { |
michael@0 | 644 | var re = d3_selection_classedRe(name); |
michael@0 | 645 | return function(node, value) { |
michael@0 | 646 | if (c = node.classList) return value ? c.add(name) : c.remove(name); |
michael@0 | 647 | var c = node.getAttribute("class") || ""; |
michael@0 | 648 | if (value) { |
michael@0 | 649 | re.lastIndex = 0; |
michael@0 | 650 | if (!re.test(c)) node.setAttribute("class", d3_collapse(c + " " + name)); |
michael@0 | 651 | } else { |
michael@0 | 652 | node.setAttribute("class", d3_collapse(c.replace(re, " "))); |
michael@0 | 653 | } |
michael@0 | 654 | }; |
michael@0 | 655 | } |
michael@0 | 656 | d3_selectionPrototype.style = function(name, value, priority) { |
michael@0 | 657 | var n = arguments.length; |
michael@0 | 658 | if (n < 3) { |
michael@0 | 659 | if (typeof name !== "string") { |
michael@0 | 660 | if (n < 2) value = ""; |
michael@0 | 661 | for (priority in name) this.each(d3_selection_style(priority, name[priority], value)); |
michael@0 | 662 | return this; |
michael@0 | 663 | } |
michael@0 | 664 | if (n < 2) return d3_window.getComputedStyle(this.node(), null).getPropertyValue(name); |
michael@0 | 665 | priority = ""; |
michael@0 | 666 | } |
michael@0 | 667 | return this.each(d3_selection_style(name, value, priority)); |
michael@0 | 668 | }; |
michael@0 | 669 | function d3_selection_style(name, value, priority) { |
michael@0 | 670 | function styleNull() { |
michael@0 | 671 | this.style.removeProperty(name); |
michael@0 | 672 | } |
michael@0 | 673 | function styleConstant() { |
michael@0 | 674 | this.style.setProperty(name, value, priority); |
michael@0 | 675 | } |
michael@0 | 676 | function styleFunction() { |
michael@0 | 677 | var x = value.apply(this, arguments); |
michael@0 | 678 | if (x == null) this.style.removeProperty(name); else this.style.setProperty(name, x, priority); |
michael@0 | 679 | } |
michael@0 | 680 | return value == null ? styleNull : typeof value === "function" ? styleFunction : styleConstant; |
michael@0 | 681 | } |
michael@0 | 682 | d3_selectionPrototype.property = function(name, value) { |
michael@0 | 683 | if (arguments.length < 2) { |
michael@0 | 684 | if (typeof name === "string") return this.node()[name]; |
michael@0 | 685 | for (value in name) this.each(d3_selection_property(value, name[value])); |
michael@0 | 686 | return this; |
michael@0 | 687 | } |
michael@0 | 688 | return this.each(d3_selection_property(name, value)); |
michael@0 | 689 | }; |
michael@0 | 690 | function d3_selection_property(name, value) { |
michael@0 | 691 | function propertyNull() { |
michael@0 | 692 | delete this[name]; |
michael@0 | 693 | } |
michael@0 | 694 | function propertyConstant() { |
michael@0 | 695 | this[name] = value; |
michael@0 | 696 | } |
michael@0 | 697 | function propertyFunction() { |
michael@0 | 698 | var x = value.apply(this, arguments); |
michael@0 | 699 | if (x == null) delete this[name]; else this[name] = x; |
michael@0 | 700 | } |
michael@0 | 701 | return value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant; |
michael@0 | 702 | } |
michael@0 | 703 | d3_selectionPrototype.text = function(value) { |
michael@0 | 704 | return arguments.length ? this.each(typeof value === "function" ? function() { |
michael@0 | 705 | var v = value.apply(this, arguments); |
michael@0 | 706 | this.textContent = v == null ? "" : v; |
michael@0 | 707 | } : value == null ? function() { |
michael@0 | 708 | this.textContent = ""; |
michael@0 | 709 | } : function() { |
michael@0 | 710 | this.textContent = value; |
michael@0 | 711 | }) : this.node().textContent; |
michael@0 | 712 | }; |
michael@0 | 713 | d3_selectionPrototype.html = function(value) { |
michael@0 | 714 | return arguments.length ? this.each(typeof value === "function" ? function() { |
michael@0 | 715 | var v = value.apply(this, arguments); |
michael@0 | 716 | this.innerHTML = v == null ? "" : v; |
michael@0 | 717 | } : value == null ? function() { |
michael@0 | 718 | this.innerHTML = ""; |
michael@0 | 719 | } : function() { |
michael@0 | 720 | this.innerHTML = value; |
michael@0 | 721 | }) : this.node().innerHTML; |
michael@0 | 722 | }; |
michael@0 | 723 | d3_selectionPrototype.append = function(name) { |
michael@0 | 724 | name = d3_selection_creator(name); |
michael@0 | 725 | return this.select(function() { |
michael@0 | 726 | return this.appendChild(name.apply(this, arguments)); |
michael@0 | 727 | }); |
michael@0 | 728 | }; |
michael@0 | 729 | function d3_selection_creator(name) { |
michael@0 | 730 | return typeof name === "function" ? name : (name = d3.ns.qualify(name)).local ? function() { |
michael@0 | 731 | return this.ownerDocument.createElementNS(name.space, name.local); |
michael@0 | 732 | } : function() { |
michael@0 | 733 | return this.ownerDocument.createElementNS(this.namespaceURI, name); |
michael@0 | 734 | }; |
michael@0 | 735 | } |
michael@0 | 736 | d3_selectionPrototype.insert = function(name, before) { |
michael@0 | 737 | name = d3_selection_creator(name); |
michael@0 | 738 | before = d3_selection_selector(before); |
michael@0 | 739 | return this.select(function() { |
michael@0 | 740 | return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments) || null); |
michael@0 | 741 | }); |
michael@0 | 742 | }; |
michael@0 | 743 | d3_selectionPrototype.remove = function() { |
michael@0 | 744 | return this.each(function() { |
michael@0 | 745 | var parent = this.parentNode; |
michael@0 | 746 | if (parent) parent.removeChild(this); |
michael@0 | 747 | }); |
michael@0 | 748 | }; |
michael@0 | 749 | d3_selectionPrototype.data = function(value, key) { |
michael@0 | 750 | var i = -1, n = this.length, group, node; |
michael@0 | 751 | if (!arguments.length) { |
michael@0 | 752 | value = new Array(n = (group = this[0]).length); |
michael@0 | 753 | while (++i < n) { |
michael@0 | 754 | if (node = group[i]) { |
michael@0 | 755 | value[i] = node.__data__; |
michael@0 | 756 | } |
michael@0 | 757 | } |
michael@0 | 758 | return value; |
michael@0 | 759 | } |
michael@0 | 760 | function bind(group, groupData) { |
michael@0 | 761 | var i, n = group.length, m = groupData.length, n0 = Math.min(n, m), updateNodes = new Array(m), enterNodes = new Array(m), exitNodes = new Array(n), node, nodeData; |
michael@0 | 762 | if (key) { |
michael@0 | 763 | var nodeByKeyValue = new d3_Map(), dataByKeyValue = new d3_Map(), keyValues = [], keyValue; |
michael@0 | 764 | for (i = -1; ++i < n; ) { |
michael@0 | 765 | keyValue = key.call(node = group[i], node.__data__, i); |
michael@0 | 766 | if (nodeByKeyValue.has(keyValue)) { |
michael@0 | 767 | exitNodes[i] = node; |
michael@0 | 768 | } else { |
michael@0 | 769 | nodeByKeyValue.set(keyValue, node); |
michael@0 | 770 | } |
michael@0 | 771 | keyValues.push(keyValue); |
michael@0 | 772 | } |
michael@0 | 773 | for (i = -1; ++i < m; ) { |
michael@0 | 774 | keyValue = key.call(groupData, nodeData = groupData[i], i); |
michael@0 | 775 | if (node = nodeByKeyValue.get(keyValue)) { |
michael@0 | 776 | updateNodes[i] = node; |
michael@0 | 777 | node.__data__ = nodeData; |
michael@0 | 778 | } else if (!dataByKeyValue.has(keyValue)) { |
michael@0 | 779 | enterNodes[i] = d3_selection_dataNode(nodeData); |
michael@0 | 780 | } |
michael@0 | 781 | dataByKeyValue.set(keyValue, nodeData); |
michael@0 | 782 | nodeByKeyValue.remove(keyValue); |
michael@0 | 783 | } |
michael@0 | 784 | for (i = -1; ++i < n; ) { |
michael@0 | 785 | if (nodeByKeyValue.has(keyValues[i])) { |
michael@0 | 786 | exitNodes[i] = group[i]; |
michael@0 | 787 | } |
michael@0 | 788 | } |
michael@0 | 789 | } else { |
michael@0 | 790 | for (i = -1; ++i < n0; ) { |
michael@0 | 791 | node = group[i]; |
michael@0 | 792 | nodeData = groupData[i]; |
michael@0 | 793 | if (node) { |
michael@0 | 794 | node.__data__ = nodeData; |
michael@0 | 795 | updateNodes[i] = node; |
michael@0 | 796 | } else { |
michael@0 | 797 | enterNodes[i] = d3_selection_dataNode(nodeData); |
michael@0 | 798 | } |
michael@0 | 799 | } |
michael@0 | 800 | for (;i < m; ++i) { |
michael@0 | 801 | enterNodes[i] = d3_selection_dataNode(groupData[i]); |
michael@0 | 802 | } |
michael@0 | 803 | for (;i < n; ++i) { |
michael@0 | 804 | exitNodes[i] = group[i]; |
michael@0 | 805 | } |
michael@0 | 806 | } |
michael@0 | 807 | enterNodes.update = updateNodes; |
michael@0 | 808 | enterNodes.parentNode = updateNodes.parentNode = exitNodes.parentNode = group.parentNode; |
michael@0 | 809 | enter.push(enterNodes); |
michael@0 | 810 | update.push(updateNodes); |
michael@0 | 811 | exit.push(exitNodes); |
michael@0 | 812 | } |
michael@0 | 813 | var enter = d3_selection_enter([]), update = d3_selection([]), exit = d3_selection([]); |
michael@0 | 814 | if (typeof value === "function") { |
michael@0 | 815 | while (++i < n) { |
michael@0 | 816 | bind(group = this[i], value.call(group, group.parentNode.__data__, i)); |
michael@0 | 817 | } |
michael@0 | 818 | } else { |
michael@0 | 819 | while (++i < n) { |
michael@0 | 820 | bind(group = this[i], value); |
michael@0 | 821 | } |
michael@0 | 822 | } |
michael@0 | 823 | update.enter = function() { |
michael@0 | 824 | return enter; |
michael@0 | 825 | }; |
michael@0 | 826 | update.exit = function() { |
michael@0 | 827 | return exit; |
michael@0 | 828 | }; |
michael@0 | 829 | return update; |
michael@0 | 830 | }; |
michael@0 | 831 | function d3_selection_dataNode(data) { |
michael@0 | 832 | return { |
michael@0 | 833 | __data__: data |
michael@0 | 834 | }; |
michael@0 | 835 | } |
michael@0 | 836 | d3_selectionPrototype.datum = function(value) { |
michael@0 | 837 | return arguments.length ? this.property("__data__", value) : this.property("__data__"); |
michael@0 | 838 | }; |
michael@0 | 839 | d3_selectionPrototype.filter = function(filter) { |
michael@0 | 840 | var subgroups = [], subgroup, group, node; |
michael@0 | 841 | if (typeof filter !== "function") filter = d3_selection_filter(filter); |
michael@0 | 842 | for (var j = 0, m = this.length; j < m; j++) { |
michael@0 | 843 | subgroups.push(subgroup = []); |
michael@0 | 844 | subgroup.parentNode = (group = this[j]).parentNode; |
michael@0 | 845 | for (var i = 0, n = group.length; i < n; i++) { |
michael@0 | 846 | if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { |
michael@0 | 847 | subgroup.push(node); |
michael@0 | 848 | } |
michael@0 | 849 | } |
michael@0 | 850 | } |
michael@0 | 851 | return d3_selection(subgroups); |
michael@0 | 852 | }; |
michael@0 | 853 | function d3_selection_filter(selector) { |
michael@0 | 854 | return function() { |
michael@0 | 855 | return d3_selectMatches(this, selector); |
michael@0 | 856 | }; |
michael@0 | 857 | } |
michael@0 | 858 | d3_selectionPrototype.order = function() { |
michael@0 | 859 | for (var j = -1, m = this.length; ++j < m; ) { |
michael@0 | 860 | for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0; ) { |
michael@0 | 861 | if (node = group[i]) { |
michael@0 | 862 | if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next); |
michael@0 | 863 | next = node; |
michael@0 | 864 | } |
michael@0 | 865 | } |
michael@0 | 866 | } |
michael@0 | 867 | return this; |
michael@0 | 868 | }; |
michael@0 | 869 | d3_selectionPrototype.sort = function(comparator) { |
michael@0 | 870 | comparator = d3_selection_sortComparator.apply(this, arguments); |
michael@0 | 871 | for (var j = -1, m = this.length; ++j < m; ) this[j].sort(comparator); |
michael@0 | 872 | return this.order(); |
michael@0 | 873 | }; |
michael@0 | 874 | function d3_selection_sortComparator(comparator) { |
michael@0 | 875 | if (!arguments.length) comparator = d3.ascending; |
michael@0 | 876 | return function(a, b) { |
michael@0 | 877 | return a && b ? comparator(a.__data__, b.__data__) : !a - !b; |
michael@0 | 878 | }; |
michael@0 | 879 | } |
michael@0 | 880 | d3_selectionPrototype.each = function(callback) { |
michael@0 | 881 | return d3_selection_each(this, function(node, i, j) { |
michael@0 | 882 | callback.call(node, node.__data__, i, j); |
michael@0 | 883 | }); |
michael@0 | 884 | }; |
michael@0 | 885 | function d3_selection_each(groups, callback) { |
michael@0 | 886 | for (var j = 0, m = groups.length; j < m; j++) { |
michael@0 | 887 | for (var group = groups[j], i = 0, n = group.length, node; i < n; i++) { |
michael@0 | 888 | if (node = group[i]) callback(node, i, j); |
michael@0 | 889 | } |
michael@0 | 890 | } |
michael@0 | 891 | return groups; |
michael@0 | 892 | } |
michael@0 | 893 | d3_selectionPrototype.call = function(callback) { |
michael@0 | 894 | var args = d3_array(arguments); |
michael@0 | 895 | callback.apply(args[0] = this, args); |
michael@0 | 896 | return this; |
michael@0 | 897 | }; |
michael@0 | 898 | d3_selectionPrototype.empty = function() { |
michael@0 | 899 | return !this.node(); |
michael@0 | 900 | }; |
michael@0 | 901 | d3_selectionPrototype.node = function() { |
michael@0 | 902 | for (var j = 0, m = this.length; j < m; j++) { |
michael@0 | 903 | for (var group = this[j], i = 0, n = group.length; i < n; i++) { |
michael@0 | 904 | var node = group[i]; |
michael@0 | 905 | if (node) return node; |
michael@0 | 906 | } |
michael@0 | 907 | } |
michael@0 | 908 | return null; |
michael@0 | 909 | }; |
michael@0 | 910 | d3_selectionPrototype.size = function() { |
michael@0 | 911 | var n = 0; |
michael@0 | 912 | this.each(function() { |
michael@0 | 913 | ++n; |
michael@0 | 914 | }); |
michael@0 | 915 | return n; |
michael@0 | 916 | }; |
michael@0 | 917 | function d3_selection_enter(selection) { |
michael@0 | 918 | d3_subclass(selection, d3_selection_enterPrototype); |
michael@0 | 919 | return selection; |
michael@0 | 920 | } |
michael@0 | 921 | var d3_selection_enterPrototype = []; |
michael@0 | 922 | d3.selection.enter = d3_selection_enter; |
michael@0 | 923 | d3.selection.enter.prototype = d3_selection_enterPrototype; |
michael@0 | 924 | d3_selection_enterPrototype.append = d3_selectionPrototype.append; |
michael@0 | 925 | d3_selection_enterPrototype.empty = d3_selectionPrototype.empty; |
michael@0 | 926 | d3_selection_enterPrototype.node = d3_selectionPrototype.node; |
michael@0 | 927 | d3_selection_enterPrototype.call = d3_selectionPrototype.call; |
michael@0 | 928 | d3_selection_enterPrototype.size = d3_selectionPrototype.size; |
michael@0 | 929 | d3_selection_enterPrototype.select = function(selector) { |
michael@0 | 930 | var subgroups = [], subgroup, subnode, upgroup, group, node; |
michael@0 | 931 | for (var j = -1, m = this.length; ++j < m; ) { |
michael@0 | 932 | upgroup = (group = this[j]).update; |
michael@0 | 933 | subgroups.push(subgroup = []); |
michael@0 | 934 | subgroup.parentNode = group.parentNode; |
michael@0 | 935 | for (var i = -1, n = group.length; ++i < n; ) { |
michael@0 | 936 | if (node = group[i]) { |
michael@0 | 937 | subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i, j)); |
michael@0 | 938 | subnode.__data__ = node.__data__; |
michael@0 | 939 | } else { |
michael@0 | 940 | subgroup.push(null); |
michael@0 | 941 | } |
michael@0 | 942 | } |
michael@0 | 943 | } |
michael@0 | 944 | return d3_selection(subgroups); |
michael@0 | 945 | }; |
michael@0 | 946 | d3_selection_enterPrototype.insert = function(name, before) { |
michael@0 | 947 | if (arguments.length < 2) before = d3_selection_enterInsertBefore(this); |
michael@0 | 948 | return d3_selectionPrototype.insert.call(this, name, before); |
michael@0 | 949 | }; |
michael@0 | 950 | function d3_selection_enterInsertBefore(enter) { |
michael@0 | 951 | var i0, j0; |
michael@0 | 952 | return function(d, i, j) { |
michael@0 | 953 | var group = enter[j].update, n = group.length, node; |
michael@0 | 954 | if (j != j0) j0 = j, i0 = 0; |
michael@0 | 955 | if (i >= i0) i0 = i + 1; |
michael@0 | 956 | while (!(node = group[i0]) && ++i0 < n) ; |
michael@0 | 957 | return node; |
michael@0 | 958 | }; |
michael@0 | 959 | } |
michael@0 | 960 | d3_selectionPrototype.transition = function() { |
michael@0 | 961 | var id = d3_transitionInheritId || ++d3_transitionId, subgroups = [], subgroup, node, transition = d3_transitionInherit || { |
michael@0 | 962 | time: Date.now(), |
michael@0 | 963 | ease: d3_ease_cubicInOut, |
michael@0 | 964 | delay: 0, |
michael@0 | 965 | duration: 250 |
michael@0 | 966 | }; |
michael@0 | 967 | for (var j = -1, m = this.length; ++j < m; ) { |
michael@0 | 968 | subgroups.push(subgroup = []); |
michael@0 | 969 | for (var group = this[j], i = -1, n = group.length; ++i < n; ) { |
michael@0 | 970 | if (node = group[i]) d3_transitionNode(node, i, id, transition); |
michael@0 | 971 | subgroup.push(node); |
michael@0 | 972 | } |
michael@0 | 973 | } |
michael@0 | 974 | return d3_transition(subgroups, id); |
michael@0 | 975 | }; |
michael@0 | 976 | d3_selectionPrototype.interrupt = function() { |
michael@0 | 977 | return this.each(d3_selection_interrupt); |
michael@0 | 978 | }; |
michael@0 | 979 | function d3_selection_interrupt() { |
michael@0 | 980 | var lock = this.__transition__; |
michael@0 | 981 | if (lock) ++lock.active; |
michael@0 | 982 | } |
michael@0 | 983 | d3.select = function(node) { |
michael@0 | 984 | var group = [ typeof node === "string" ? d3_select(node, d3_document) : node ]; |
michael@0 | 985 | group.parentNode = d3_documentElement; |
michael@0 | 986 | return d3_selection([ group ]); |
michael@0 | 987 | }; |
michael@0 | 988 | d3.selectAll = function(nodes) { |
michael@0 | 989 | var group = d3_array(typeof nodes === "string" ? d3_selectAll(nodes, d3_document) : nodes); |
michael@0 | 990 | group.parentNode = d3_documentElement; |
michael@0 | 991 | return d3_selection([ group ]); |
michael@0 | 992 | }; |
michael@0 | 993 | var d3_selectionRoot = d3.select(d3_documentElement); |
michael@0 | 994 | d3_selectionPrototype.on = function(type, listener, capture) { |
michael@0 | 995 | var n = arguments.length; |
michael@0 | 996 | if (n < 3) { |
michael@0 | 997 | if (typeof type !== "string") { |
michael@0 | 998 | if (n < 2) listener = false; |
michael@0 | 999 | for (capture in type) this.each(d3_selection_on(capture, type[capture], listener)); |
michael@0 | 1000 | return this; |
michael@0 | 1001 | } |
michael@0 | 1002 | if (n < 2) return (n = this.node()["__on" + type]) && n._; |
michael@0 | 1003 | capture = false; |
michael@0 | 1004 | } |
michael@0 | 1005 | return this.each(d3_selection_on(type, listener, capture)); |
michael@0 | 1006 | }; |
michael@0 | 1007 | function d3_selection_on(type, listener, capture) { |
michael@0 | 1008 | var name = "__on" + type, i = type.indexOf("."), wrap = d3_selection_onListener; |
michael@0 | 1009 | if (i > 0) type = type.substring(0, i); |
michael@0 | 1010 | var filter = d3_selection_onFilters.get(type); |
michael@0 | 1011 | if (filter) type = filter, wrap = d3_selection_onFilter; |
michael@0 | 1012 | function onRemove() { |
michael@0 | 1013 | var l = this[name]; |
michael@0 | 1014 | if (l) { |
michael@0 | 1015 | this.removeEventListener(type, l, l.$); |
michael@0 | 1016 | delete this[name]; |
michael@0 | 1017 | } |
michael@0 | 1018 | } |
michael@0 | 1019 | function onAdd() { |
michael@0 | 1020 | var l = wrap(listener, d3_array(arguments)); |
michael@0 | 1021 | onRemove.call(this); |
michael@0 | 1022 | this.addEventListener(type, this[name] = l, l.$ = capture); |
michael@0 | 1023 | l._ = listener; |
michael@0 | 1024 | } |
michael@0 | 1025 | function removeAll() { |
michael@0 | 1026 | var re = new RegExp("^__on([^.]+)" + d3.requote(type) + "$"), match; |
michael@0 | 1027 | for (var name in this) { |
michael@0 | 1028 | if (match = name.match(re)) { |
michael@0 | 1029 | var l = this[name]; |
michael@0 | 1030 | this.removeEventListener(match[1], l, l.$); |
michael@0 | 1031 | delete this[name]; |
michael@0 | 1032 | } |
michael@0 | 1033 | } |
michael@0 | 1034 | } |
michael@0 | 1035 | return i ? listener ? onAdd : onRemove : listener ? d3_noop : removeAll; |
michael@0 | 1036 | } |
michael@0 | 1037 | var d3_selection_onFilters = d3.map({ |
michael@0 | 1038 | mouseenter: "mouseover", |
michael@0 | 1039 | mouseleave: "mouseout" |
michael@0 | 1040 | }); |
michael@0 | 1041 | d3_selection_onFilters.forEach(function(k) { |
michael@0 | 1042 | if ("on" + k in d3_document) d3_selection_onFilters.remove(k); |
michael@0 | 1043 | }); |
michael@0 | 1044 | function d3_selection_onListener(listener, argumentz) { |
michael@0 | 1045 | return function(e) { |
michael@0 | 1046 | var o = d3.event; |
michael@0 | 1047 | d3.event = e; |
michael@0 | 1048 | argumentz[0] = this.__data__; |
michael@0 | 1049 | try { |
michael@0 | 1050 | listener.apply(this, argumentz); |
michael@0 | 1051 | } finally { |
michael@0 | 1052 | d3.event = o; |
michael@0 | 1053 | } |
michael@0 | 1054 | }; |
michael@0 | 1055 | } |
michael@0 | 1056 | function d3_selection_onFilter(listener, argumentz) { |
michael@0 | 1057 | var l = d3_selection_onListener(listener, argumentz); |
michael@0 | 1058 | return function(e) { |
michael@0 | 1059 | var target = this, related = e.relatedTarget; |
michael@0 | 1060 | if (!related || related !== target && !(related.compareDocumentPosition(target) & 8)) { |
michael@0 | 1061 | l.call(target, e); |
michael@0 | 1062 | } |
michael@0 | 1063 | }; |
michael@0 | 1064 | } |
michael@0 | 1065 | var d3_event_dragSelect = "onselectstart" in d3_document ? null : d3_vendorSymbol(d3_documentElement.style, "userSelect"), d3_event_dragId = 0; |
michael@0 | 1066 | function d3_event_dragSuppress() { |
michael@0 | 1067 | var name = ".dragsuppress-" + ++d3_event_dragId, click = "click" + name, w = d3.select(d3_window).on("touchmove" + name, d3_eventPreventDefault).on("dragstart" + name, d3_eventPreventDefault).on("selectstart" + name, d3_eventPreventDefault); |
michael@0 | 1068 | if (d3_event_dragSelect) { |
michael@0 | 1069 | var style = d3_documentElement.style, select = style[d3_event_dragSelect]; |
michael@0 | 1070 | style[d3_event_dragSelect] = "none"; |
michael@0 | 1071 | } |
michael@0 | 1072 | return function(suppressClick) { |
michael@0 | 1073 | w.on(name, null); |
michael@0 | 1074 | if (d3_event_dragSelect) style[d3_event_dragSelect] = select; |
michael@0 | 1075 | if (suppressClick) { |
michael@0 | 1076 | function off() { |
michael@0 | 1077 | w.on(click, null); |
michael@0 | 1078 | } |
michael@0 | 1079 | w.on(click, function() { |
michael@0 | 1080 | d3_eventPreventDefault(); |
michael@0 | 1081 | off(); |
michael@0 | 1082 | }, true); |
michael@0 | 1083 | setTimeout(off, 0); |
michael@0 | 1084 | } |
michael@0 | 1085 | }; |
michael@0 | 1086 | } |
michael@0 | 1087 | d3.mouse = function(container) { |
michael@0 | 1088 | return d3_mousePoint(container, d3_eventSource()); |
michael@0 | 1089 | }; |
michael@0 | 1090 | var d3_mouse_bug44083 = /WebKit/.test(d3_window.navigator.userAgent) ? -1 : 0; |
michael@0 | 1091 | function d3_mousePoint(container, e) { |
michael@0 | 1092 | if (e.changedTouches) e = e.changedTouches[0]; |
michael@0 | 1093 | var svg = container.ownerSVGElement || container; |
michael@0 | 1094 | if (svg.createSVGPoint) { |
michael@0 | 1095 | var point = svg.createSVGPoint(); |
michael@0 | 1096 | if (d3_mouse_bug44083 < 0 && (d3_window.scrollX || d3_window.scrollY)) { |
michael@0 | 1097 | svg = d3.select("body").append("svg").style({ |
michael@0 | 1098 | position: "absolute", |
michael@0 | 1099 | top: 0, |
michael@0 | 1100 | left: 0, |
michael@0 | 1101 | margin: 0, |
michael@0 | 1102 | padding: 0, |
michael@0 | 1103 | border: "none" |
michael@0 | 1104 | }, "important"); |
michael@0 | 1105 | var ctm = svg[0][0].getScreenCTM(); |
michael@0 | 1106 | d3_mouse_bug44083 = !(ctm.f || ctm.e); |
michael@0 | 1107 | svg.remove(); |
michael@0 | 1108 | } |
michael@0 | 1109 | if (d3_mouse_bug44083) point.x = e.pageX, point.y = e.pageY; else point.x = e.clientX, |
michael@0 | 1110 | point.y = e.clientY; |
michael@0 | 1111 | point = point.matrixTransform(container.getScreenCTM().inverse()); |
michael@0 | 1112 | return [ point.x, point.y ]; |
michael@0 | 1113 | } |
michael@0 | 1114 | var rect = container.getBoundingClientRect(); |
michael@0 | 1115 | return [ e.clientX - rect.left - container.clientLeft, e.clientY - rect.top - container.clientTop ]; |
michael@0 | 1116 | } |
michael@0 | 1117 | d3.touches = function(container, touches) { |
michael@0 | 1118 | if (arguments.length < 2) touches = d3_eventSource().touches; |
michael@0 | 1119 | return touches ? d3_array(touches).map(function(touch) { |
michael@0 | 1120 | var point = d3_mousePoint(container, touch); |
michael@0 | 1121 | point.identifier = touch.identifier; |
michael@0 | 1122 | return point; |
michael@0 | 1123 | }) : []; |
michael@0 | 1124 | }; |
michael@0 | 1125 | d3.behavior.drag = function() { |
michael@0 | 1126 | var event = d3_eventDispatch(drag, "drag", "dragstart", "dragend"), origin = null, mousedown = dragstart(d3_noop, d3.mouse, "mousemove", "mouseup"), touchstart = dragstart(touchid, touchposition, "touchmove", "touchend"); |
michael@0 | 1127 | function drag() { |
michael@0 | 1128 | this.on("mousedown.drag", mousedown).on("touchstart.drag", touchstart); |
michael@0 | 1129 | } |
michael@0 | 1130 | function touchid() { |
michael@0 | 1131 | return d3.event.changedTouches[0].identifier; |
michael@0 | 1132 | } |
michael@0 | 1133 | function touchposition(parent, id) { |
michael@0 | 1134 | return d3.touches(parent).filter(function(p) { |
michael@0 | 1135 | return p.identifier === id; |
michael@0 | 1136 | })[0]; |
michael@0 | 1137 | } |
michael@0 | 1138 | function dragstart(id, position, move, end) { |
michael@0 | 1139 | return function() { |
michael@0 | 1140 | var target = this, parent = target.parentNode, event_ = event.of(target, arguments), eventTarget = d3.event.target, eventId = id(), drag = eventId == null ? "drag" : "drag-" + eventId, origin_ = position(parent, eventId), dragged = 0, offset, w = d3.select(d3_window).on(move + "." + drag, moved).on(end + "." + drag, ended), dragRestore = d3_event_dragSuppress(); |
michael@0 | 1141 | if (origin) { |
michael@0 | 1142 | offset = origin.apply(target, arguments); |
michael@0 | 1143 | offset = [ offset.x - origin_[0], offset.y - origin_[1] ]; |
michael@0 | 1144 | } else { |
michael@0 | 1145 | offset = [ 0, 0 ]; |
michael@0 | 1146 | } |
michael@0 | 1147 | event_({ |
michael@0 | 1148 | type: "dragstart" |
michael@0 | 1149 | }); |
michael@0 | 1150 | function moved() { |
michael@0 | 1151 | var p = position(parent, eventId), dx = p[0] - origin_[0], dy = p[1] - origin_[1]; |
michael@0 | 1152 | dragged |= dx | dy; |
michael@0 | 1153 | origin_ = p; |
michael@0 | 1154 | event_({ |
michael@0 | 1155 | type: "drag", |
michael@0 | 1156 | x: p[0] + offset[0], |
michael@0 | 1157 | y: p[1] + offset[1], |
michael@0 | 1158 | dx: dx, |
michael@0 | 1159 | dy: dy |
michael@0 | 1160 | }); |
michael@0 | 1161 | } |
michael@0 | 1162 | function ended() { |
michael@0 | 1163 | w.on(move + "." + drag, null).on(end + "." + drag, null); |
michael@0 | 1164 | dragRestore(dragged && d3.event.target === eventTarget); |
michael@0 | 1165 | event_({ |
michael@0 | 1166 | type: "dragend" |
michael@0 | 1167 | }); |
michael@0 | 1168 | } |
michael@0 | 1169 | }; |
michael@0 | 1170 | } |
michael@0 | 1171 | drag.origin = function(x) { |
michael@0 | 1172 | if (!arguments.length) return origin; |
michael@0 | 1173 | origin = x; |
michael@0 | 1174 | return drag; |
michael@0 | 1175 | }; |
michael@0 | 1176 | return d3.rebind(drag, event, "on"); |
michael@0 | 1177 | }; |
michael@0 | 1178 | var π = Math.PI, τ = 2 * π, halfπ = π / 2, ε = 1e-6, ε2 = ε * ε, d3_radians = π / 180, d3_degrees = 180 / π; |
michael@0 | 1179 | function d3_sgn(x) { |
michael@0 | 1180 | return x > 0 ? 1 : x < 0 ? -1 : 0; |
michael@0 | 1181 | } |
michael@0 | 1182 | function d3_cross2d(a, b, c) { |
michael@0 | 1183 | return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); |
michael@0 | 1184 | } |
michael@0 | 1185 | function d3_acos(x) { |
michael@0 | 1186 | return x > 1 ? 0 : x < -1 ? π : Math.acos(x); |
michael@0 | 1187 | } |
michael@0 | 1188 | function d3_asin(x) { |
michael@0 | 1189 | return x > 1 ? halfπ : x < -1 ? -halfπ : Math.asin(x); |
michael@0 | 1190 | } |
michael@0 | 1191 | function d3_sinh(x) { |
michael@0 | 1192 | return ((x = Math.exp(x)) - 1 / x) / 2; |
michael@0 | 1193 | } |
michael@0 | 1194 | function d3_cosh(x) { |
michael@0 | 1195 | return ((x = Math.exp(x)) + 1 / x) / 2; |
michael@0 | 1196 | } |
michael@0 | 1197 | function d3_tanh(x) { |
michael@0 | 1198 | return ((x = Math.exp(2 * x)) - 1) / (x + 1); |
michael@0 | 1199 | } |
michael@0 | 1200 | function d3_haversin(x) { |
michael@0 | 1201 | return (x = Math.sin(x / 2)) * x; |
michael@0 | 1202 | } |
michael@0 | 1203 | var ρ = Math.SQRT2, ρ2 = 2, ρ4 = 4; |
michael@0 | 1204 | d3.interpolateZoom = function(p0, p1) { |
michael@0 | 1205 | var ux0 = p0[0], uy0 = p0[1], w0 = p0[2], ux1 = p1[0], uy1 = p1[1], w1 = p1[2]; |
michael@0 | 1206 | var dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, d1 = Math.sqrt(d2), b0 = (w1 * w1 - w0 * w0 + ρ4 * d2) / (2 * w0 * ρ2 * d1), b1 = (w1 * w1 - w0 * w0 - ρ4 * d2) / (2 * w1 * ρ2 * d1), r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0), r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1), dr = r1 - r0, S = (dr || Math.log(w1 / w0)) / ρ; |
michael@0 | 1207 | function interpolate(t) { |
michael@0 | 1208 | var s = t * S; |
michael@0 | 1209 | if (dr) { |
michael@0 | 1210 | var coshr0 = d3_cosh(r0), u = w0 / (ρ2 * d1) * (coshr0 * d3_tanh(ρ * s + r0) - d3_sinh(r0)); |
michael@0 | 1211 | return [ ux0 + u * dx, uy0 + u * dy, w0 * coshr0 / d3_cosh(ρ * s + r0) ]; |
michael@0 | 1212 | } |
michael@0 | 1213 | return [ ux0 + t * dx, uy0 + t * dy, w0 * Math.exp(ρ * s) ]; |
michael@0 | 1214 | } |
michael@0 | 1215 | interpolate.duration = S * 1e3; |
michael@0 | 1216 | return interpolate; |
michael@0 | 1217 | }; |
michael@0 | 1218 | d3.behavior.zoom = function() { |
michael@0 | 1219 | var view = { |
michael@0 | 1220 | x: 0, |
michael@0 | 1221 | y: 0, |
michael@0 | 1222 | k: 1 |
michael@0 | 1223 | }, translate0, center, size = [ 960, 500 ], scaleExtent = d3_behavior_zoomInfinity, mousedown = "mousedown.zoom", mousemove = "mousemove.zoom", mouseup = "mouseup.zoom", mousewheelTimer, touchstart = "touchstart.zoom", touchtime, event = d3_eventDispatch(zoom, "zoomstart", "zoom", "zoomend"), x0, x1, y0, y1; |
michael@0 | 1224 | function zoom(g) { |
michael@0 | 1225 | g.on(mousedown, mousedowned).on(d3_behavior_zoomWheel + ".zoom", mousewheeled).on(mousemove, mousewheelreset).on("dblclick.zoom", dblclicked).on(touchstart, touchstarted); |
michael@0 | 1226 | } |
michael@0 | 1227 | zoom.event = function(g) { |
michael@0 | 1228 | g.each(function() { |
michael@0 | 1229 | var event_ = event.of(this, arguments), view1 = view; |
michael@0 | 1230 | if (d3_transitionInheritId) { |
michael@0 | 1231 | d3.select(this).transition().each("start.zoom", function() { |
michael@0 | 1232 | view = this.__chart__ || { |
michael@0 | 1233 | x: 0, |
michael@0 | 1234 | y: 0, |
michael@0 | 1235 | k: 1 |
michael@0 | 1236 | }; |
michael@0 | 1237 | zoomstarted(event_); |
michael@0 | 1238 | }).tween("zoom:zoom", function() { |
michael@0 | 1239 | var dx = size[0], dy = size[1], cx = dx / 2, cy = dy / 2, i = d3.interpolateZoom([ (cx - view.x) / view.k, (cy - view.y) / view.k, dx / view.k ], [ (cx - view1.x) / view1.k, (cy - view1.y) / view1.k, dx / view1.k ]); |
michael@0 | 1240 | return function(t) { |
michael@0 | 1241 | var l = i(t), k = dx / l[2]; |
michael@0 | 1242 | this.__chart__ = view = { |
michael@0 | 1243 | x: cx - l[0] * k, |
michael@0 | 1244 | y: cy - l[1] * k, |
michael@0 | 1245 | k: k |
michael@0 | 1246 | }; |
michael@0 | 1247 | zoomed(event_); |
michael@0 | 1248 | }; |
michael@0 | 1249 | }).each("end.zoom", function() { |
michael@0 | 1250 | zoomended(event_); |
michael@0 | 1251 | }); |
michael@0 | 1252 | } else { |
michael@0 | 1253 | this.__chart__ = view; |
michael@0 | 1254 | zoomstarted(event_); |
michael@0 | 1255 | zoomed(event_); |
michael@0 | 1256 | zoomended(event_); |
michael@0 | 1257 | } |
michael@0 | 1258 | }); |
michael@0 | 1259 | }; |
michael@0 | 1260 | zoom.translate = function(_) { |
michael@0 | 1261 | if (!arguments.length) return [ view.x, view.y ]; |
michael@0 | 1262 | view = { |
michael@0 | 1263 | x: +_[0], |
michael@0 | 1264 | y: +_[1], |
michael@0 | 1265 | k: view.k |
michael@0 | 1266 | }; |
michael@0 | 1267 | rescale(); |
michael@0 | 1268 | return zoom; |
michael@0 | 1269 | }; |
michael@0 | 1270 | zoom.scale = function(_) { |
michael@0 | 1271 | if (!arguments.length) return view.k; |
michael@0 | 1272 | view = { |
michael@0 | 1273 | x: view.x, |
michael@0 | 1274 | y: view.y, |
michael@0 | 1275 | k: +_ |
michael@0 | 1276 | }; |
michael@0 | 1277 | rescale(); |
michael@0 | 1278 | return zoom; |
michael@0 | 1279 | }; |
michael@0 | 1280 | zoom.scaleExtent = function(_) { |
michael@0 | 1281 | if (!arguments.length) return scaleExtent; |
michael@0 | 1282 | scaleExtent = _ == null ? d3_behavior_zoomInfinity : [ +_[0], +_[1] ]; |
michael@0 | 1283 | return zoom; |
michael@0 | 1284 | }; |
michael@0 | 1285 | zoom.center = function(_) { |
michael@0 | 1286 | if (!arguments.length) return center; |
michael@0 | 1287 | center = _ && [ +_[0], +_[1] ]; |
michael@0 | 1288 | return zoom; |
michael@0 | 1289 | }; |
michael@0 | 1290 | zoom.size = function(_) { |
michael@0 | 1291 | if (!arguments.length) return size; |
michael@0 | 1292 | size = _ && [ +_[0], +_[1] ]; |
michael@0 | 1293 | return zoom; |
michael@0 | 1294 | }; |
michael@0 | 1295 | zoom.x = function(z) { |
michael@0 | 1296 | if (!arguments.length) return x1; |
michael@0 | 1297 | x1 = z; |
michael@0 | 1298 | x0 = z.copy(); |
michael@0 | 1299 | view = { |
michael@0 | 1300 | x: 0, |
michael@0 | 1301 | y: 0, |
michael@0 | 1302 | k: 1 |
michael@0 | 1303 | }; |
michael@0 | 1304 | return zoom; |
michael@0 | 1305 | }; |
michael@0 | 1306 | zoom.y = function(z) { |
michael@0 | 1307 | if (!arguments.length) return y1; |
michael@0 | 1308 | y1 = z; |
michael@0 | 1309 | y0 = z.copy(); |
michael@0 | 1310 | view = { |
michael@0 | 1311 | x: 0, |
michael@0 | 1312 | y: 0, |
michael@0 | 1313 | k: 1 |
michael@0 | 1314 | }; |
michael@0 | 1315 | return zoom; |
michael@0 | 1316 | }; |
michael@0 | 1317 | function location(p) { |
michael@0 | 1318 | return [ (p[0] - view.x) / view.k, (p[1] - view.y) / view.k ]; |
michael@0 | 1319 | } |
michael@0 | 1320 | function point(l) { |
michael@0 | 1321 | return [ l[0] * view.k + view.x, l[1] * view.k + view.y ]; |
michael@0 | 1322 | } |
michael@0 | 1323 | function scaleTo(s) { |
michael@0 | 1324 | view.k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], s)); |
michael@0 | 1325 | } |
michael@0 | 1326 | function translateTo(p, l) { |
michael@0 | 1327 | l = point(l); |
michael@0 | 1328 | view.x += p[0] - l[0]; |
michael@0 | 1329 | view.y += p[1] - l[1]; |
michael@0 | 1330 | } |
michael@0 | 1331 | function rescale() { |
michael@0 | 1332 | if (x1) x1.domain(x0.range().map(function(x) { |
michael@0 | 1333 | return (x - view.x) / view.k; |
michael@0 | 1334 | }).map(x0.invert)); |
michael@0 | 1335 | if (y1) y1.domain(y0.range().map(function(y) { |
michael@0 | 1336 | return (y - view.y) / view.k; |
michael@0 | 1337 | }).map(y0.invert)); |
michael@0 | 1338 | } |
michael@0 | 1339 | function zoomstarted(event) { |
michael@0 | 1340 | event({ |
michael@0 | 1341 | type: "zoomstart" |
michael@0 | 1342 | }); |
michael@0 | 1343 | } |
michael@0 | 1344 | function zoomed(event) { |
michael@0 | 1345 | rescale(); |
michael@0 | 1346 | event({ |
michael@0 | 1347 | type: "zoom", |
michael@0 | 1348 | scale: view.k, |
michael@0 | 1349 | translate: [ view.x, view.y ] |
michael@0 | 1350 | }); |
michael@0 | 1351 | } |
michael@0 | 1352 | function zoomended(event) { |
michael@0 | 1353 | event({ |
michael@0 | 1354 | type: "zoomend" |
michael@0 | 1355 | }); |
michael@0 | 1356 | } |
michael@0 | 1357 | function mousedowned() { |
michael@0 | 1358 | var target = this, event_ = event.of(target, arguments), eventTarget = d3.event.target, dragged = 0, w = d3.select(d3_window).on(mousemove, moved).on(mouseup, ended), l = location(d3.mouse(target)), dragRestore = d3_event_dragSuppress(); |
michael@0 | 1359 | d3_selection_interrupt.call(target); |
michael@0 | 1360 | zoomstarted(event_); |
michael@0 | 1361 | function moved() { |
michael@0 | 1362 | dragged = 1; |
michael@0 | 1363 | translateTo(d3.mouse(target), l); |
michael@0 | 1364 | zoomed(event_); |
michael@0 | 1365 | } |
michael@0 | 1366 | function ended() { |
michael@0 | 1367 | w.on(mousemove, d3_window === target ? mousewheelreset : null).on(mouseup, null); |
michael@0 | 1368 | dragRestore(dragged && d3.event.target === eventTarget); |
michael@0 | 1369 | zoomended(event_); |
michael@0 | 1370 | } |
michael@0 | 1371 | } |
michael@0 | 1372 | function touchstarted() { |
michael@0 | 1373 | var target = this, event_ = event.of(target, arguments), locations0 = {}, distance0 = 0, scale0, eventId = d3.event.changedTouches[0].identifier, touchmove = "touchmove.zoom-" + eventId, touchend = "touchend.zoom-" + eventId, w = d3.select(d3_window).on(touchmove, moved).on(touchend, ended), t = d3.select(target).on(mousedown, null).on(touchstart, started), dragRestore = d3_event_dragSuppress(); |
michael@0 | 1374 | d3_selection_interrupt.call(target); |
michael@0 | 1375 | started(); |
michael@0 | 1376 | zoomstarted(event_); |
michael@0 | 1377 | function relocate() { |
michael@0 | 1378 | var touches = d3.touches(target); |
michael@0 | 1379 | scale0 = view.k; |
michael@0 | 1380 | touches.forEach(function(t) { |
michael@0 | 1381 | if (t.identifier in locations0) locations0[t.identifier] = location(t); |
michael@0 | 1382 | }); |
michael@0 | 1383 | return touches; |
michael@0 | 1384 | } |
michael@0 | 1385 | function started() { |
michael@0 | 1386 | var changed = d3.event.changedTouches; |
michael@0 | 1387 | for (var i = 0, n = changed.length; i < n; ++i) { |
michael@0 | 1388 | locations0[changed[i].identifier] = null; |
michael@0 | 1389 | } |
michael@0 | 1390 | var touches = relocate(), now = Date.now(); |
michael@0 | 1391 | if (touches.length === 1) { |
michael@0 | 1392 | if (now - touchtime < 500) { |
michael@0 | 1393 | var p = touches[0], l = locations0[p.identifier]; |
michael@0 | 1394 | scaleTo(view.k * 2); |
michael@0 | 1395 | translateTo(p, l); |
michael@0 | 1396 | d3_eventPreventDefault(); |
michael@0 | 1397 | zoomed(event_); |
michael@0 | 1398 | } |
michael@0 | 1399 | touchtime = now; |
michael@0 | 1400 | } else if (touches.length > 1) { |
michael@0 | 1401 | var p = touches[0], q = touches[1], dx = p[0] - q[0], dy = p[1] - q[1]; |
michael@0 | 1402 | distance0 = dx * dx + dy * dy; |
michael@0 | 1403 | } |
michael@0 | 1404 | } |
michael@0 | 1405 | function moved() { |
michael@0 | 1406 | var touches = d3.touches(target), p0, l0, p1, l1; |
michael@0 | 1407 | for (var i = 0, n = touches.length; i < n; ++i, l1 = null) { |
michael@0 | 1408 | p1 = touches[i]; |
michael@0 | 1409 | if (l1 = locations0[p1.identifier]) { |
michael@0 | 1410 | if (l0) break; |
michael@0 | 1411 | p0 = p1, l0 = l1; |
michael@0 | 1412 | } |
michael@0 | 1413 | } |
michael@0 | 1414 | if (l1) { |
michael@0 | 1415 | var distance1 = (distance1 = p1[0] - p0[0]) * distance1 + (distance1 = p1[1] - p0[1]) * distance1, scale1 = distance0 && Math.sqrt(distance1 / distance0); |
michael@0 | 1416 | p0 = [ (p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2 ]; |
michael@0 | 1417 | l0 = [ (l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2 ]; |
michael@0 | 1418 | scaleTo(scale1 * scale0); |
michael@0 | 1419 | } |
michael@0 | 1420 | touchtime = null; |
michael@0 | 1421 | translateTo(p0, l0); |
michael@0 | 1422 | zoomed(event_); |
michael@0 | 1423 | } |
michael@0 | 1424 | function ended() { |
michael@0 | 1425 | if (d3.event.touches.length) { |
michael@0 | 1426 | var changed = d3.event.changedTouches; |
michael@0 | 1427 | for (var i = 0, n = changed.length; i < n; ++i) { |
michael@0 | 1428 | delete locations0[changed[i].identifier]; |
michael@0 | 1429 | } |
michael@0 | 1430 | for (var identifier in locations0) { |
michael@0 | 1431 | return void relocate(); |
michael@0 | 1432 | } |
michael@0 | 1433 | } |
michael@0 | 1434 | w.on(touchmove, null).on(touchend, null); |
michael@0 | 1435 | t.on(mousedown, mousedowned).on(touchstart, touchstarted); |
michael@0 | 1436 | dragRestore(); |
michael@0 | 1437 | zoomended(event_); |
michael@0 | 1438 | } |
michael@0 | 1439 | } |
michael@0 | 1440 | function mousewheeled() { |
michael@0 | 1441 | var event_ = event.of(this, arguments); |
michael@0 | 1442 | if (mousewheelTimer) clearTimeout(mousewheelTimer); else d3_selection_interrupt.call(this), |
michael@0 | 1443 | zoomstarted(event_); |
michael@0 | 1444 | mousewheelTimer = setTimeout(function() { |
michael@0 | 1445 | mousewheelTimer = null; |
michael@0 | 1446 | zoomended(event_); |
michael@0 | 1447 | }, 50); |
michael@0 | 1448 | d3_eventPreventDefault(); |
michael@0 | 1449 | var point = center || d3.mouse(this); |
michael@0 | 1450 | if (!translate0) translate0 = location(point); |
michael@0 | 1451 | scaleTo(Math.pow(2, d3_behavior_zoomDelta() * .002) * view.k); |
michael@0 | 1452 | translateTo(point, translate0); |
michael@0 | 1453 | zoomed(event_); |
michael@0 | 1454 | } |
michael@0 | 1455 | function mousewheelreset() { |
michael@0 | 1456 | translate0 = null; |
michael@0 | 1457 | } |
michael@0 | 1458 | function dblclicked() { |
michael@0 | 1459 | var event_ = event.of(this, arguments), p = d3.mouse(this), l = location(p), k = Math.log(view.k) / Math.LN2; |
michael@0 | 1460 | zoomstarted(event_); |
michael@0 | 1461 | scaleTo(Math.pow(2, d3.event.shiftKey ? Math.ceil(k) - 1 : Math.floor(k) + 1)); |
michael@0 | 1462 | translateTo(p, l); |
michael@0 | 1463 | zoomed(event_); |
michael@0 | 1464 | zoomended(event_); |
michael@0 | 1465 | } |
michael@0 | 1466 | return d3.rebind(zoom, event, "on"); |
michael@0 | 1467 | }; |
michael@0 | 1468 | var d3_behavior_zoomInfinity = [ 0, Infinity ]; |
michael@0 | 1469 | var d3_behavior_zoomDelta, d3_behavior_zoomWheel = "onwheel" in d3_document ? (d3_behavior_zoomDelta = function() { |
michael@0 | 1470 | return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1); |
michael@0 | 1471 | }, "wheel") : "onmousewheel" in d3_document ? (d3_behavior_zoomDelta = function() { |
michael@0 | 1472 | return d3.event.wheelDelta; |
michael@0 | 1473 | }, "mousewheel") : (d3_behavior_zoomDelta = function() { |
michael@0 | 1474 | return -d3.event.detail; |
michael@0 | 1475 | }, "MozMousePixelScroll"); |
michael@0 | 1476 | function d3_Color() {} |
michael@0 | 1477 | d3_Color.prototype.toString = function() { |
michael@0 | 1478 | return this.rgb() + ""; |
michael@0 | 1479 | }; |
michael@0 | 1480 | d3.hsl = function(h, s, l) { |
michael@0 | 1481 | return arguments.length === 1 ? h instanceof d3_Hsl ? d3_hsl(h.h, h.s, h.l) : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl) : d3_hsl(+h, +s, +l); |
michael@0 | 1482 | }; |
michael@0 | 1483 | function d3_hsl(h, s, l) { |
michael@0 | 1484 | return new d3_Hsl(h, s, l); |
michael@0 | 1485 | } |
michael@0 | 1486 | function d3_Hsl(h, s, l) { |
michael@0 | 1487 | this.h = h; |
michael@0 | 1488 | this.s = s; |
michael@0 | 1489 | this.l = l; |
michael@0 | 1490 | } |
michael@0 | 1491 | var d3_hslPrototype = d3_Hsl.prototype = new d3_Color(); |
michael@0 | 1492 | d3_hslPrototype.brighter = function(k) { |
michael@0 | 1493 | k = Math.pow(.7, arguments.length ? k : 1); |
michael@0 | 1494 | return d3_hsl(this.h, this.s, this.l / k); |
michael@0 | 1495 | }; |
michael@0 | 1496 | d3_hslPrototype.darker = function(k) { |
michael@0 | 1497 | k = Math.pow(.7, arguments.length ? k : 1); |
michael@0 | 1498 | return d3_hsl(this.h, this.s, k * this.l); |
michael@0 | 1499 | }; |
michael@0 | 1500 | d3_hslPrototype.rgb = function() { |
michael@0 | 1501 | return d3_hsl_rgb(this.h, this.s, this.l); |
michael@0 | 1502 | }; |
michael@0 | 1503 | function d3_hsl_rgb(h, s, l) { |
michael@0 | 1504 | var m1, m2; |
michael@0 | 1505 | h = isNaN(h) ? 0 : (h %= 360) < 0 ? h + 360 : h; |
michael@0 | 1506 | s = isNaN(s) ? 0 : s < 0 ? 0 : s > 1 ? 1 : s; |
michael@0 | 1507 | l = l < 0 ? 0 : l > 1 ? 1 : l; |
michael@0 | 1508 | m2 = l <= .5 ? l * (1 + s) : l + s - l * s; |
michael@0 | 1509 | m1 = 2 * l - m2; |
michael@0 | 1510 | function v(h) { |
michael@0 | 1511 | if (h > 360) h -= 360; else if (h < 0) h += 360; |
michael@0 | 1512 | if (h < 60) return m1 + (m2 - m1) * h / 60; |
michael@0 | 1513 | if (h < 180) return m2; |
michael@0 | 1514 | if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60; |
michael@0 | 1515 | return m1; |
michael@0 | 1516 | } |
michael@0 | 1517 | function vv(h) { |
michael@0 | 1518 | return Math.round(v(h) * 255); |
michael@0 | 1519 | } |
michael@0 | 1520 | return d3_rgb(vv(h + 120), vv(h), vv(h - 120)); |
michael@0 | 1521 | } |
michael@0 | 1522 | d3.hcl = function(h, c, l) { |
michael@0 | 1523 | return arguments.length === 1 ? h instanceof d3_Hcl ? d3_hcl(h.h, h.c, h.l) : h instanceof d3_Lab ? d3_lab_hcl(h.l, h.a, h.b) : d3_lab_hcl((h = d3_rgb_lab((h = d3.rgb(h)).r, h.g, h.b)).l, h.a, h.b) : d3_hcl(+h, +c, +l); |
michael@0 | 1524 | }; |
michael@0 | 1525 | function d3_hcl(h, c, l) { |
michael@0 | 1526 | return new d3_Hcl(h, c, l); |
michael@0 | 1527 | } |
michael@0 | 1528 | function d3_Hcl(h, c, l) { |
michael@0 | 1529 | this.h = h; |
michael@0 | 1530 | this.c = c; |
michael@0 | 1531 | this.l = l; |
michael@0 | 1532 | } |
michael@0 | 1533 | var d3_hclPrototype = d3_Hcl.prototype = new d3_Color(); |
michael@0 | 1534 | d3_hclPrototype.brighter = function(k) { |
michael@0 | 1535 | return d3_hcl(this.h, this.c, Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1))); |
michael@0 | 1536 | }; |
michael@0 | 1537 | d3_hclPrototype.darker = function(k) { |
michael@0 | 1538 | return d3_hcl(this.h, this.c, Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1))); |
michael@0 | 1539 | }; |
michael@0 | 1540 | d3_hclPrototype.rgb = function() { |
michael@0 | 1541 | return d3_hcl_lab(this.h, this.c, this.l).rgb(); |
michael@0 | 1542 | }; |
michael@0 | 1543 | function d3_hcl_lab(h, c, l) { |
michael@0 | 1544 | if (isNaN(h)) h = 0; |
michael@0 | 1545 | if (isNaN(c)) c = 0; |
michael@0 | 1546 | return d3_lab(l, Math.cos(h *= d3_radians) * c, Math.sin(h) * c); |
michael@0 | 1547 | } |
michael@0 | 1548 | d3.lab = function(l, a, b) { |
michael@0 | 1549 | return arguments.length === 1 ? l instanceof d3_Lab ? d3_lab(l.l, l.a, l.b) : l instanceof d3_Hcl ? d3_hcl_lab(l.l, l.c, l.h) : d3_rgb_lab((l = d3.rgb(l)).r, l.g, l.b) : d3_lab(+l, +a, +b); |
michael@0 | 1550 | }; |
michael@0 | 1551 | function d3_lab(l, a, b) { |
michael@0 | 1552 | return new d3_Lab(l, a, b); |
michael@0 | 1553 | } |
michael@0 | 1554 | function d3_Lab(l, a, b) { |
michael@0 | 1555 | this.l = l; |
michael@0 | 1556 | this.a = a; |
michael@0 | 1557 | this.b = b; |
michael@0 | 1558 | } |
michael@0 | 1559 | var d3_lab_K = 18; |
michael@0 | 1560 | var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883; |
michael@0 | 1561 | var d3_labPrototype = d3_Lab.prototype = new d3_Color(); |
michael@0 | 1562 | d3_labPrototype.brighter = function(k) { |
michael@0 | 1563 | return d3_lab(Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); |
michael@0 | 1564 | }; |
michael@0 | 1565 | d3_labPrototype.darker = function(k) { |
michael@0 | 1566 | return d3_lab(Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); |
michael@0 | 1567 | }; |
michael@0 | 1568 | d3_labPrototype.rgb = function() { |
michael@0 | 1569 | return d3_lab_rgb(this.l, this.a, this.b); |
michael@0 | 1570 | }; |
michael@0 | 1571 | function d3_lab_rgb(l, a, b) { |
michael@0 | 1572 | var y = (l + 16) / 116, x = y + a / 500, z = y - b / 200; |
michael@0 | 1573 | x = d3_lab_xyz(x) * d3_lab_X; |
michael@0 | 1574 | y = d3_lab_xyz(y) * d3_lab_Y; |
michael@0 | 1575 | z = d3_lab_xyz(z) * d3_lab_Z; |
michael@0 | 1576 | return d3_rgb(d3_xyz_rgb(3.2404542 * x - 1.5371385 * y - .4985314 * z), d3_xyz_rgb(-.969266 * x + 1.8760108 * y + .041556 * z), d3_xyz_rgb(.0556434 * x - .2040259 * y + 1.0572252 * z)); |
michael@0 | 1577 | } |
michael@0 | 1578 | function d3_lab_hcl(l, a, b) { |
michael@0 | 1579 | return l > 0 ? d3_hcl(Math.atan2(b, a) * d3_degrees, Math.sqrt(a * a + b * b), l) : d3_hcl(NaN, NaN, l); |
michael@0 | 1580 | } |
michael@0 | 1581 | function d3_lab_xyz(x) { |
michael@0 | 1582 | return x > .206893034 ? x * x * x : (x - 4 / 29) / 7.787037; |
michael@0 | 1583 | } |
michael@0 | 1584 | function d3_xyz_lab(x) { |
michael@0 | 1585 | return x > .008856 ? Math.pow(x, 1 / 3) : 7.787037 * x + 4 / 29; |
michael@0 | 1586 | } |
michael@0 | 1587 | function d3_xyz_rgb(r) { |
michael@0 | 1588 | return Math.round(255 * (r <= .00304 ? 12.92 * r : 1.055 * Math.pow(r, 1 / 2.4) - .055)); |
michael@0 | 1589 | } |
michael@0 | 1590 | d3.rgb = function(r, g, b) { |
michael@0 | 1591 | return arguments.length === 1 ? r instanceof d3_Rgb ? d3_rgb(r.r, r.g, r.b) : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb) : d3_rgb(~~r, ~~g, ~~b); |
michael@0 | 1592 | }; |
michael@0 | 1593 | function d3_rgbNumber(value) { |
michael@0 | 1594 | return d3_rgb(value >> 16, value >> 8 & 255, value & 255); |
michael@0 | 1595 | } |
michael@0 | 1596 | function d3_rgbString(value) { |
michael@0 | 1597 | return d3_rgbNumber(value) + ""; |
michael@0 | 1598 | } |
michael@0 | 1599 | function d3_rgb(r, g, b) { |
michael@0 | 1600 | return new d3_Rgb(r, g, b); |
michael@0 | 1601 | } |
michael@0 | 1602 | function d3_Rgb(r, g, b) { |
michael@0 | 1603 | this.r = r; |
michael@0 | 1604 | this.g = g; |
michael@0 | 1605 | this.b = b; |
michael@0 | 1606 | } |
michael@0 | 1607 | var d3_rgbPrototype = d3_Rgb.prototype = new d3_Color(); |
michael@0 | 1608 | d3_rgbPrototype.brighter = function(k) { |
michael@0 | 1609 | k = Math.pow(.7, arguments.length ? k : 1); |
michael@0 | 1610 | var r = this.r, g = this.g, b = this.b, i = 30; |
michael@0 | 1611 | if (!r && !g && !b) return d3_rgb(i, i, i); |
michael@0 | 1612 | if (r && r < i) r = i; |
michael@0 | 1613 | if (g && g < i) g = i; |
michael@0 | 1614 | if (b && b < i) b = i; |
michael@0 | 1615 | return d3_rgb(Math.min(255, ~~(r / k)), Math.min(255, ~~(g / k)), Math.min(255, ~~(b / k))); |
michael@0 | 1616 | }; |
michael@0 | 1617 | d3_rgbPrototype.darker = function(k) { |
michael@0 | 1618 | k = Math.pow(.7, arguments.length ? k : 1); |
michael@0 | 1619 | return d3_rgb(~~(k * this.r), ~~(k * this.g), ~~(k * this.b)); |
michael@0 | 1620 | }; |
michael@0 | 1621 | d3_rgbPrototype.hsl = function() { |
michael@0 | 1622 | return d3_rgb_hsl(this.r, this.g, this.b); |
michael@0 | 1623 | }; |
michael@0 | 1624 | d3_rgbPrototype.toString = function() { |
michael@0 | 1625 | return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b); |
michael@0 | 1626 | }; |
michael@0 | 1627 | function d3_rgb_hex(v) { |
michael@0 | 1628 | return v < 16 ? "0" + Math.max(0, v).toString(16) : Math.min(255, v).toString(16); |
michael@0 | 1629 | } |
michael@0 | 1630 | function d3_rgb_parse(format, rgb, hsl) { |
michael@0 | 1631 | var r = 0, g = 0, b = 0, m1, m2, name; |
michael@0 | 1632 | m1 = /([a-z]+)\((.*)\)/i.exec(format); |
michael@0 | 1633 | if (m1) { |
michael@0 | 1634 | m2 = m1[2].split(","); |
michael@0 | 1635 | switch (m1[1]) { |
michael@0 | 1636 | case "hsl": |
michael@0 | 1637 | { |
michael@0 | 1638 | return hsl(parseFloat(m2[0]), parseFloat(m2[1]) / 100, parseFloat(m2[2]) / 100); |
michael@0 | 1639 | } |
michael@0 | 1640 | |
michael@0 | 1641 | case "rgb": |
michael@0 | 1642 | { |
michael@0 | 1643 | return rgb(d3_rgb_parseNumber(m2[0]), d3_rgb_parseNumber(m2[1]), d3_rgb_parseNumber(m2[2])); |
michael@0 | 1644 | } |
michael@0 | 1645 | } |
michael@0 | 1646 | } |
michael@0 | 1647 | if (name = d3_rgb_names.get(format)) return rgb(name.r, name.g, name.b); |
michael@0 | 1648 | if (format != null && format.charAt(0) === "#") { |
michael@0 | 1649 | if (format.length === 4) { |
michael@0 | 1650 | r = format.charAt(1); |
michael@0 | 1651 | r += r; |
michael@0 | 1652 | g = format.charAt(2); |
michael@0 | 1653 | g += g; |
michael@0 | 1654 | b = format.charAt(3); |
michael@0 | 1655 | b += b; |
michael@0 | 1656 | } else if (format.length === 7) { |
michael@0 | 1657 | r = format.substring(1, 3); |
michael@0 | 1658 | g = format.substring(3, 5); |
michael@0 | 1659 | b = format.substring(5, 7); |
michael@0 | 1660 | } |
michael@0 | 1661 | r = parseInt(r, 16); |
michael@0 | 1662 | g = parseInt(g, 16); |
michael@0 | 1663 | b = parseInt(b, 16); |
michael@0 | 1664 | } |
michael@0 | 1665 | return rgb(r, g, b); |
michael@0 | 1666 | } |
michael@0 | 1667 | function d3_rgb_hsl(r, g, b) { |
michael@0 | 1668 | var min = Math.min(r /= 255, g /= 255, b /= 255), max = Math.max(r, g, b), d = max - min, h, s, l = (max + min) / 2; |
michael@0 | 1669 | if (d) { |
michael@0 | 1670 | s = l < .5 ? d / (max + min) : d / (2 - max - min); |
michael@0 | 1671 | if (r == max) h = (g - b) / d + (g < b ? 6 : 0); else if (g == max) h = (b - r) / d + 2; else h = (r - g) / d + 4; |
michael@0 | 1672 | h *= 60; |
michael@0 | 1673 | } else { |
michael@0 | 1674 | h = NaN; |
michael@0 | 1675 | s = l > 0 && l < 1 ? 0 : h; |
michael@0 | 1676 | } |
michael@0 | 1677 | return d3_hsl(h, s, l); |
michael@0 | 1678 | } |
michael@0 | 1679 | function d3_rgb_lab(r, g, b) { |
michael@0 | 1680 | r = d3_rgb_xyz(r); |
michael@0 | 1681 | g = d3_rgb_xyz(g); |
michael@0 | 1682 | b = d3_rgb_xyz(b); |
michael@0 | 1683 | var x = d3_xyz_lab((.4124564 * r + .3575761 * g + .1804375 * b) / d3_lab_X), y = d3_xyz_lab((.2126729 * r + .7151522 * g + .072175 * b) / d3_lab_Y), z = d3_xyz_lab((.0193339 * r + .119192 * g + .9503041 * b) / d3_lab_Z); |
michael@0 | 1684 | return d3_lab(116 * y - 16, 500 * (x - y), 200 * (y - z)); |
michael@0 | 1685 | } |
michael@0 | 1686 | function d3_rgb_xyz(r) { |
michael@0 | 1687 | return (r /= 255) <= .04045 ? r / 12.92 : Math.pow((r + .055) / 1.055, 2.4); |
michael@0 | 1688 | } |
michael@0 | 1689 | function d3_rgb_parseNumber(c) { |
michael@0 | 1690 | var f = parseFloat(c); |
michael@0 | 1691 | return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f; |
michael@0 | 1692 | } |
michael@0 | 1693 | var d3_rgb_names = d3.map({ |
michael@0 | 1694 | aliceblue: 15792383, |
michael@0 | 1695 | antiquewhite: 16444375, |
michael@0 | 1696 | aqua: 65535, |
michael@0 | 1697 | aquamarine: 8388564, |
michael@0 | 1698 | azure: 15794175, |
michael@0 | 1699 | beige: 16119260, |
michael@0 | 1700 | bisque: 16770244, |
michael@0 | 1701 | black: 0, |
michael@0 | 1702 | blanchedalmond: 16772045, |
michael@0 | 1703 | blue: 255, |
michael@0 | 1704 | blueviolet: 9055202, |
michael@0 | 1705 | brown: 10824234, |
michael@0 | 1706 | burlywood: 14596231, |
michael@0 | 1707 | cadetblue: 6266528, |
michael@0 | 1708 | chartreuse: 8388352, |
michael@0 | 1709 | chocolate: 13789470, |
michael@0 | 1710 | coral: 16744272, |
michael@0 | 1711 | cornflowerblue: 6591981, |
michael@0 | 1712 | cornsilk: 16775388, |
michael@0 | 1713 | crimson: 14423100, |
michael@0 | 1714 | cyan: 65535, |
michael@0 | 1715 | darkblue: 139, |
michael@0 | 1716 | darkcyan: 35723, |
michael@0 | 1717 | darkgoldenrod: 12092939, |
michael@0 | 1718 | darkgray: 11119017, |
michael@0 | 1719 | darkgreen: 25600, |
michael@0 | 1720 | darkgrey: 11119017, |
michael@0 | 1721 | darkkhaki: 12433259, |
michael@0 | 1722 | darkmagenta: 9109643, |
michael@0 | 1723 | darkolivegreen: 5597999, |
michael@0 | 1724 | darkorange: 16747520, |
michael@0 | 1725 | darkorchid: 10040012, |
michael@0 | 1726 | darkred: 9109504, |
michael@0 | 1727 | darksalmon: 15308410, |
michael@0 | 1728 | darkseagreen: 9419919, |
michael@0 | 1729 | darkslateblue: 4734347, |
michael@0 | 1730 | darkslategray: 3100495, |
michael@0 | 1731 | darkslategrey: 3100495, |
michael@0 | 1732 | darkturquoise: 52945, |
michael@0 | 1733 | darkviolet: 9699539, |
michael@0 | 1734 | deeppink: 16716947, |
michael@0 | 1735 | deepskyblue: 49151, |
michael@0 | 1736 | dimgray: 6908265, |
michael@0 | 1737 | dimgrey: 6908265, |
michael@0 | 1738 | dodgerblue: 2003199, |
michael@0 | 1739 | firebrick: 11674146, |
michael@0 | 1740 | floralwhite: 16775920, |
michael@0 | 1741 | forestgreen: 2263842, |
michael@0 | 1742 | fuchsia: 16711935, |
michael@0 | 1743 | gainsboro: 14474460, |
michael@0 | 1744 | ghostwhite: 16316671, |
michael@0 | 1745 | gold: 16766720, |
michael@0 | 1746 | goldenrod: 14329120, |
michael@0 | 1747 | gray: 8421504, |
michael@0 | 1748 | green: 32768, |
michael@0 | 1749 | greenyellow: 11403055, |
michael@0 | 1750 | grey: 8421504, |
michael@0 | 1751 | honeydew: 15794160, |
michael@0 | 1752 | hotpink: 16738740, |
michael@0 | 1753 | indianred: 13458524, |
michael@0 | 1754 | indigo: 4915330, |
michael@0 | 1755 | ivory: 16777200, |
michael@0 | 1756 | khaki: 15787660, |
michael@0 | 1757 | lavender: 15132410, |
michael@0 | 1758 | lavenderblush: 16773365, |
michael@0 | 1759 | lawngreen: 8190976, |
michael@0 | 1760 | lemonchiffon: 16775885, |
michael@0 | 1761 | lightblue: 11393254, |
michael@0 | 1762 | lightcoral: 15761536, |
michael@0 | 1763 | lightcyan: 14745599, |
michael@0 | 1764 | lightgoldenrodyellow: 16448210, |
michael@0 | 1765 | lightgray: 13882323, |
michael@0 | 1766 | lightgreen: 9498256, |
michael@0 | 1767 | lightgrey: 13882323, |
michael@0 | 1768 | lightpink: 16758465, |
michael@0 | 1769 | lightsalmon: 16752762, |
michael@0 | 1770 | lightseagreen: 2142890, |
michael@0 | 1771 | lightskyblue: 8900346, |
michael@0 | 1772 | lightslategray: 7833753, |
michael@0 | 1773 | lightslategrey: 7833753, |
michael@0 | 1774 | lightsteelblue: 11584734, |
michael@0 | 1775 | lightyellow: 16777184, |
michael@0 | 1776 | lime: 65280, |
michael@0 | 1777 | limegreen: 3329330, |
michael@0 | 1778 | linen: 16445670, |
michael@0 | 1779 | magenta: 16711935, |
michael@0 | 1780 | maroon: 8388608, |
michael@0 | 1781 | mediumaquamarine: 6737322, |
michael@0 | 1782 | mediumblue: 205, |
michael@0 | 1783 | mediumorchid: 12211667, |
michael@0 | 1784 | mediumpurple: 9662683, |
michael@0 | 1785 | mediumseagreen: 3978097, |
michael@0 | 1786 | mediumslateblue: 8087790, |
michael@0 | 1787 | mediumspringgreen: 64154, |
michael@0 | 1788 | mediumturquoise: 4772300, |
michael@0 | 1789 | mediumvioletred: 13047173, |
michael@0 | 1790 | midnightblue: 1644912, |
michael@0 | 1791 | mintcream: 16121850, |
michael@0 | 1792 | mistyrose: 16770273, |
michael@0 | 1793 | moccasin: 16770229, |
michael@0 | 1794 | navajowhite: 16768685, |
michael@0 | 1795 | navy: 128, |
michael@0 | 1796 | oldlace: 16643558, |
michael@0 | 1797 | olive: 8421376, |
michael@0 | 1798 | olivedrab: 7048739, |
michael@0 | 1799 | orange: 16753920, |
michael@0 | 1800 | orangered: 16729344, |
michael@0 | 1801 | orchid: 14315734, |
michael@0 | 1802 | palegoldenrod: 15657130, |
michael@0 | 1803 | palegreen: 10025880, |
michael@0 | 1804 | paleturquoise: 11529966, |
michael@0 | 1805 | palevioletred: 14381203, |
michael@0 | 1806 | papayawhip: 16773077, |
michael@0 | 1807 | peachpuff: 16767673, |
michael@0 | 1808 | peru: 13468991, |
michael@0 | 1809 | pink: 16761035, |
michael@0 | 1810 | plum: 14524637, |
michael@0 | 1811 | powderblue: 11591910, |
michael@0 | 1812 | purple: 8388736, |
michael@0 | 1813 | red: 16711680, |
michael@0 | 1814 | rosybrown: 12357519, |
michael@0 | 1815 | royalblue: 4286945, |
michael@0 | 1816 | saddlebrown: 9127187, |
michael@0 | 1817 | salmon: 16416882, |
michael@0 | 1818 | sandybrown: 16032864, |
michael@0 | 1819 | seagreen: 3050327, |
michael@0 | 1820 | seashell: 16774638, |
michael@0 | 1821 | sienna: 10506797, |
michael@0 | 1822 | silver: 12632256, |
michael@0 | 1823 | skyblue: 8900331, |
michael@0 | 1824 | slateblue: 6970061, |
michael@0 | 1825 | slategray: 7372944, |
michael@0 | 1826 | slategrey: 7372944, |
michael@0 | 1827 | snow: 16775930, |
michael@0 | 1828 | springgreen: 65407, |
michael@0 | 1829 | steelblue: 4620980, |
michael@0 | 1830 | tan: 13808780, |
michael@0 | 1831 | teal: 32896, |
michael@0 | 1832 | thistle: 14204888, |
michael@0 | 1833 | tomato: 16737095, |
michael@0 | 1834 | turquoise: 4251856, |
michael@0 | 1835 | violet: 15631086, |
michael@0 | 1836 | wheat: 16113331, |
michael@0 | 1837 | white: 16777215, |
michael@0 | 1838 | whitesmoke: 16119285, |
michael@0 | 1839 | yellow: 16776960, |
michael@0 | 1840 | yellowgreen: 10145074 |
michael@0 | 1841 | }); |
michael@0 | 1842 | d3_rgb_names.forEach(function(key, value) { |
michael@0 | 1843 | d3_rgb_names.set(key, d3_rgbNumber(value)); |
michael@0 | 1844 | }); |
michael@0 | 1845 | function d3_functor(v) { |
michael@0 | 1846 | return typeof v === "function" ? v : function() { |
michael@0 | 1847 | return v; |
michael@0 | 1848 | }; |
michael@0 | 1849 | } |
michael@0 | 1850 | d3.functor = d3_functor; |
michael@0 | 1851 | function d3_identity(d) { |
michael@0 | 1852 | return d; |
michael@0 | 1853 | } |
michael@0 | 1854 | d3.xhr = d3_xhrType(d3_identity); |
michael@0 | 1855 | function d3_xhrType(response) { |
michael@0 | 1856 | return function(url, mimeType, callback) { |
michael@0 | 1857 | if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType, |
michael@0 | 1858 | mimeType = null; |
michael@0 | 1859 | return d3_xhr(url, mimeType, response, callback); |
michael@0 | 1860 | }; |
michael@0 | 1861 | } |
michael@0 | 1862 | function d3_xhr(url, mimeType, response, callback) { |
michael@0 | 1863 | var xhr = {}, dispatch = d3.dispatch("beforesend", "progress", "load", "error"), headers = {}, request = new XMLHttpRequest(), responseType = null; |
michael@0 | 1864 | if (d3_window.XDomainRequest && !("withCredentials" in request) && /^(http(s)?:)?\/\//.test(url)) request = new XDomainRequest(); |
michael@0 | 1865 | "onload" in request ? request.onload = request.onerror = respond : request.onreadystatechange = function() { |
michael@0 | 1866 | request.readyState > 3 && respond(); |
michael@0 | 1867 | }; |
michael@0 | 1868 | function respond() { |
michael@0 | 1869 | var status = request.status, result; |
michael@0 | 1870 | if (!status && request.responseText || status >= 200 && status < 300 || status === 304) { |
michael@0 | 1871 | try { |
michael@0 | 1872 | result = response.call(xhr, request); |
michael@0 | 1873 | } catch (e) { |
michael@0 | 1874 | dispatch.error.call(xhr, e); |
michael@0 | 1875 | return; |
michael@0 | 1876 | } |
michael@0 | 1877 | dispatch.load.call(xhr, result); |
michael@0 | 1878 | } else { |
michael@0 | 1879 | dispatch.error.call(xhr, request); |
michael@0 | 1880 | } |
michael@0 | 1881 | } |
michael@0 | 1882 | request.onprogress = function(event) { |
michael@0 | 1883 | var o = d3.event; |
michael@0 | 1884 | d3.event = event; |
michael@0 | 1885 | try { |
michael@0 | 1886 | dispatch.progress.call(xhr, request); |
michael@0 | 1887 | } finally { |
michael@0 | 1888 | d3.event = o; |
michael@0 | 1889 | } |
michael@0 | 1890 | }; |
michael@0 | 1891 | xhr.header = function(name, value) { |
michael@0 | 1892 | name = (name + "").toLowerCase(); |
michael@0 | 1893 | if (arguments.length < 2) return headers[name]; |
michael@0 | 1894 | if (value == null) delete headers[name]; else headers[name] = value + ""; |
michael@0 | 1895 | return xhr; |
michael@0 | 1896 | }; |
michael@0 | 1897 | xhr.mimeType = function(value) { |
michael@0 | 1898 | if (!arguments.length) return mimeType; |
michael@0 | 1899 | mimeType = value == null ? null : value + ""; |
michael@0 | 1900 | return xhr; |
michael@0 | 1901 | }; |
michael@0 | 1902 | xhr.responseType = function(value) { |
michael@0 | 1903 | if (!arguments.length) return responseType; |
michael@0 | 1904 | responseType = value; |
michael@0 | 1905 | return xhr; |
michael@0 | 1906 | }; |
michael@0 | 1907 | xhr.response = function(value) { |
michael@0 | 1908 | response = value; |
michael@0 | 1909 | return xhr; |
michael@0 | 1910 | }; |
michael@0 | 1911 | [ "get", "post" ].forEach(function(method) { |
michael@0 | 1912 | xhr[method] = function() { |
michael@0 | 1913 | return xhr.send.apply(xhr, [ method ].concat(d3_array(arguments))); |
michael@0 | 1914 | }; |
michael@0 | 1915 | }); |
michael@0 | 1916 | xhr.send = function(method, data, callback) { |
michael@0 | 1917 | if (arguments.length === 2 && typeof data === "function") callback = data, data = null; |
michael@0 | 1918 | request.open(method, url, true); |
michael@0 | 1919 | if (mimeType != null && !("accept" in headers)) headers["accept"] = mimeType + ",*/*"; |
michael@0 | 1920 | if (request.setRequestHeader) for (var name in headers) request.setRequestHeader(name, headers[name]); |
michael@0 | 1921 | if (mimeType != null && request.overrideMimeType) request.overrideMimeType(mimeType); |
michael@0 | 1922 | if (responseType != null) request.responseType = responseType; |
michael@0 | 1923 | if (callback != null) xhr.on("error", callback).on("load", function(request) { |
michael@0 | 1924 | callback(null, request); |
michael@0 | 1925 | }); |
michael@0 | 1926 | dispatch.beforesend.call(xhr, request); |
michael@0 | 1927 | request.send(data == null ? null : data); |
michael@0 | 1928 | return xhr; |
michael@0 | 1929 | }; |
michael@0 | 1930 | xhr.abort = function() { |
michael@0 | 1931 | request.abort(); |
michael@0 | 1932 | return xhr; |
michael@0 | 1933 | }; |
michael@0 | 1934 | d3.rebind(xhr, dispatch, "on"); |
michael@0 | 1935 | return callback == null ? xhr : xhr.get(d3_xhr_fixCallback(callback)); |
michael@0 | 1936 | } |
michael@0 | 1937 | function d3_xhr_fixCallback(callback) { |
michael@0 | 1938 | return callback.length === 1 ? function(error, request) { |
michael@0 | 1939 | callback(error == null ? request : null); |
michael@0 | 1940 | } : callback; |
michael@0 | 1941 | } |
michael@0 | 1942 | d3.dsv = function(delimiter, mimeType) { |
michael@0 | 1943 | var reFormat = new RegExp('["' + delimiter + "\n]"), delimiterCode = delimiter.charCodeAt(0); |
michael@0 | 1944 | function dsv(url, row, callback) { |
michael@0 | 1945 | if (arguments.length < 3) callback = row, row = null; |
michael@0 | 1946 | var xhr = d3_xhr(url, mimeType, row == null ? response : typedResponse(row), callback); |
michael@0 | 1947 | xhr.row = function(_) { |
michael@0 | 1948 | return arguments.length ? xhr.response((row = _) == null ? response : typedResponse(_)) : row; |
michael@0 | 1949 | }; |
michael@0 | 1950 | return xhr; |
michael@0 | 1951 | } |
michael@0 | 1952 | function response(request) { |
michael@0 | 1953 | return dsv.parse(request.responseText); |
michael@0 | 1954 | } |
michael@0 | 1955 | function typedResponse(f) { |
michael@0 | 1956 | return function(request) { |
michael@0 | 1957 | return dsv.parse(request.responseText, f); |
michael@0 | 1958 | }; |
michael@0 | 1959 | } |
michael@0 | 1960 | dsv.parse = function(text, f) { |
michael@0 | 1961 | var o; |
michael@0 | 1962 | return dsv.parseRows(text, function(row, i) { |
michael@0 | 1963 | if (o) return o(row, i - 1); |
michael@0 | 1964 | var a = new Function("d", "return {" + row.map(function(name, i) { |
michael@0 | 1965 | return JSON.stringify(name) + ": d[" + i + "]"; |
michael@0 | 1966 | }).join(",") + "}"); |
michael@0 | 1967 | o = f ? function(row, i) { |
michael@0 | 1968 | return f(a(row), i); |
michael@0 | 1969 | } : a; |
michael@0 | 1970 | }); |
michael@0 | 1971 | }; |
michael@0 | 1972 | dsv.parseRows = function(text, f) { |
michael@0 | 1973 | var EOL = {}, EOF = {}, rows = [], N = text.length, I = 0, n = 0, t, eol; |
michael@0 | 1974 | function token() { |
michael@0 | 1975 | if (I >= N) return EOF; |
michael@0 | 1976 | if (eol) return eol = false, EOL; |
michael@0 | 1977 | var j = I; |
michael@0 | 1978 | if (text.charCodeAt(j) === 34) { |
michael@0 | 1979 | var i = j; |
michael@0 | 1980 | while (i++ < N) { |
michael@0 | 1981 | if (text.charCodeAt(i) === 34) { |
michael@0 | 1982 | if (text.charCodeAt(i + 1) !== 34) break; |
michael@0 | 1983 | ++i; |
michael@0 | 1984 | } |
michael@0 | 1985 | } |
michael@0 | 1986 | I = i + 2; |
michael@0 | 1987 | var c = text.charCodeAt(i + 1); |
michael@0 | 1988 | if (c === 13) { |
michael@0 | 1989 | eol = true; |
michael@0 | 1990 | if (text.charCodeAt(i + 2) === 10) ++I; |
michael@0 | 1991 | } else if (c === 10) { |
michael@0 | 1992 | eol = true; |
michael@0 | 1993 | } |
michael@0 | 1994 | return text.substring(j + 1, i).replace(/""/g, '"'); |
michael@0 | 1995 | } |
michael@0 | 1996 | while (I < N) { |
michael@0 | 1997 | var c = text.charCodeAt(I++), k = 1; |
michael@0 | 1998 | if (c === 10) eol = true; else if (c === 13) { |
michael@0 | 1999 | eol = true; |
michael@0 | 2000 | if (text.charCodeAt(I) === 10) ++I, ++k; |
michael@0 | 2001 | } else if (c !== delimiterCode) continue; |
michael@0 | 2002 | return text.substring(j, I - k); |
michael@0 | 2003 | } |
michael@0 | 2004 | return text.substring(j); |
michael@0 | 2005 | } |
michael@0 | 2006 | while ((t = token()) !== EOF) { |
michael@0 | 2007 | var a = []; |
michael@0 | 2008 | while (t !== EOL && t !== EOF) { |
michael@0 | 2009 | a.push(t); |
michael@0 | 2010 | t = token(); |
michael@0 | 2011 | } |
michael@0 | 2012 | if (f && !(a = f(a, n++))) continue; |
michael@0 | 2013 | rows.push(a); |
michael@0 | 2014 | } |
michael@0 | 2015 | return rows; |
michael@0 | 2016 | }; |
michael@0 | 2017 | dsv.format = function(rows) { |
michael@0 | 2018 | if (Array.isArray(rows[0])) return dsv.formatRows(rows); |
michael@0 | 2019 | var fieldSet = new d3_Set(), fields = []; |
michael@0 | 2020 | rows.forEach(function(row) { |
michael@0 | 2021 | for (var field in row) { |
michael@0 | 2022 | if (!fieldSet.has(field)) { |
michael@0 | 2023 | fields.push(fieldSet.add(field)); |
michael@0 | 2024 | } |
michael@0 | 2025 | } |
michael@0 | 2026 | }); |
michael@0 | 2027 | return [ fields.map(formatValue).join(delimiter) ].concat(rows.map(function(row) { |
michael@0 | 2028 | return fields.map(function(field) { |
michael@0 | 2029 | return formatValue(row[field]); |
michael@0 | 2030 | }).join(delimiter); |
michael@0 | 2031 | })).join("\n"); |
michael@0 | 2032 | }; |
michael@0 | 2033 | dsv.formatRows = function(rows) { |
michael@0 | 2034 | return rows.map(formatRow).join("\n"); |
michael@0 | 2035 | }; |
michael@0 | 2036 | function formatRow(row) { |
michael@0 | 2037 | return row.map(formatValue).join(delimiter); |
michael@0 | 2038 | } |
michael@0 | 2039 | function formatValue(text) { |
michael@0 | 2040 | return reFormat.test(text) ? '"' + text.replace(/\"/g, '""') + '"' : text; |
michael@0 | 2041 | } |
michael@0 | 2042 | return dsv; |
michael@0 | 2043 | }; |
michael@0 | 2044 | d3.csv = d3.dsv(",", "text/csv"); |
michael@0 | 2045 | d3.tsv = d3.dsv(" ", "text/tab-separated-values"); |
michael@0 | 2046 | var d3_timer_queueHead, d3_timer_queueTail, d3_timer_interval, d3_timer_timeout, d3_timer_active, d3_timer_frame = d3_window[d3_vendorSymbol(d3_window, "requestAnimationFrame")] || function(callback) { |
michael@0 | 2047 | setTimeout(callback, 17); |
michael@0 | 2048 | }; |
michael@0 | 2049 | d3.timer = function(callback, delay, then) { |
michael@0 | 2050 | var n = arguments.length; |
michael@0 | 2051 | if (n < 2) delay = 0; |
michael@0 | 2052 | if (n < 3) then = Date.now(); |
michael@0 | 2053 | var time = then + delay, timer = { |
michael@0 | 2054 | c: callback, |
michael@0 | 2055 | t: time, |
michael@0 | 2056 | f: false, |
michael@0 | 2057 | n: null |
michael@0 | 2058 | }; |
michael@0 | 2059 | if (d3_timer_queueTail) d3_timer_queueTail.n = timer; else d3_timer_queueHead = timer; |
michael@0 | 2060 | d3_timer_queueTail = timer; |
michael@0 | 2061 | if (!d3_timer_interval) { |
michael@0 | 2062 | d3_timer_timeout = clearTimeout(d3_timer_timeout); |
michael@0 | 2063 | d3_timer_interval = 1; |
michael@0 | 2064 | d3_timer_frame(d3_timer_step); |
michael@0 | 2065 | } |
michael@0 | 2066 | }; |
michael@0 | 2067 | function d3_timer_step() { |
michael@0 | 2068 | var now = d3_timer_mark(), delay = d3_timer_sweep() - now; |
michael@0 | 2069 | if (delay > 24) { |
michael@0 | 2070 | if (isFinite(delay)) { |
michael@0 | 2071 | clearTimeout(d3_timer_timeout); |
michael@0 | 2072 | d3_timer_timeout = setTimeout(d3_timer_step, delay); |
michael@0 | 2073 | } |
michael@0 | 2074 | d3_timer_interval = 0; |
michael@0 | 2075 | } else { |
michael@0 | 2076 | d3_timer_interval = 1; |
michael@0 | 2077 | d3_timer_frame(d3_timer_step); |
michael@0 | 2078 | } |
michael@0 | 2079 | } |
michael@0 | 2080 | d3.timer.flush = function() { |
michael@0 | 2081 | d3_timer_mark(); |
michael@0 | 2082 | d3_timer_sweep(); |
michael@0 | 2083 | }; |
michael@0 | 2084 | function d3_timer_mark() { |
michael@0 | 2085 | var now = Date.now(); |
michael@0 | 2086 | d3_timer_active = d3_timer_queueHead; |
michael@0 | 2087 | while (d3_timer_active) { |
michael@0 | 2088 | if (now >= d3_timer_active.t) d3_timer_active.f = d3_timer_active.c(now - d3_timer_active.t); |
michael@0 | 2089 | d3_timer_active = d3_timer_active.n; |
michael@0 | 2090 | } |
michael@0 | 2091 | return now; |
michael@0 | 2092 | } |
michael@0 | 2093 | function d3_timer_sweep() { |
michael@0 | 2094 | var t0, t1 = d3_timer_queueHead, time = Infinity; |
michael@0 | 2095 | while (t1) { |
michael@0 | 2096 | if (t1.f) { |
michael@0 | 2097 | t1 = t0 ? t0.n = t1.n : d3_timer_queueHead = t1.n; |
michael@0 | 2098 | } else { |
michael@0 | 2099 | if (t1.t < time) time = t1.t; |
michael@0 | 2100 | t1 = (t0 = t1).n; |
michael@0 | 2101 | } |
michael@0 | 2102 | } |
michael@0 | 2103 | d3_timer_queueTail = t0; |
michael@0 | 2104 | return time; |
michael@0 | 2105 | } |
michael@0 | 2106 | function d3_format_precision(x, p) { |
michael@0 | 2107 | return p - (x ? Math.ceil(Math.log(x) / Math.LN10) : 1); |
michael@0 | 2108 | } |
michael@0 | 2109 | d3.round = function(x, n) { |
michael@0 | 2110 | return n ? Math.round(x * (n = Math.pow(10, n))) / n : Math.round(x); |
michael@0 | 2111 | }; |
michael@0 | 2112 | var d3_formatPrefixes = [ "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" ].map(d3_formatPrefix); |
michael@0 | 2113 | d3.formatPrefix = function(value, precision) { |
michael@0 | 2114 | var i = 0; |
michael@0 | 2115 | if (value) { |
michael@0 | 2116 | if (value < 0) value *= -1; |
michael@0 | 2117 | if (precision) value = d3.round(value, d3_format_precision(value, precision)); |
michael@0 | 2118 | i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10); |
michael@0 | 2119 | i = Math.max(-24, Math.min(24, Math.floor((i <= 0 ? i + 1 : i - 1) / 3) * 3)); |
michael@0 | 2120 | } |
michael@0 | 2121 | return d3_formatPrefixes[8 + i / 3]; |
michael@0 | 2122 | }; |
michael@0 | 2123 | function d3_formatPrefix(d, i) { |
michael@0 | 2124 | var k = Math.pow(10, abs(8 - i) * 3); |
michael@0 | 2125 | return { |
michael@0 | 2126 | scale: i > 8 ? function(d) { |
michael@0 | 2127 | return d / k; |
michael@0 | 2128 | } : function(d) { |
michael@0 | 2129 | return d * k; |
michael@0 | 2130 | }, |
michael@0 | 2131 | symbol: d |
michael@0 | 2132 | }; |
michael@0 | 2133 | } |
michael@0 | 2134 | function d3_locale_numberFormat(locale) { |
michael@0 | 2135 | var locale_decimal = locale.decimal, locale_thousands = locale.thousands, locale_grouping = locale.grouping, locale_currency = locale.currency, formatGroup = locale_grouping ? function(value) { |
michael@0 | 2136 | var i = value.length, t = [], j = 0, g = locale_grouping[0]; |
michael@0 | 2137 | while (i > 0 && g > 0) { |
michael@0 | 2138 | t.push(value.substring(i -= g, i + g)); |
michael@0 | 2139 | g = locale_grouping[j = (j + 1) % locale_grouping.length]; |
michael@0 | 2140 | } |
michael@0 | 2141 | return t.reverse().join(locale_thousands); |
michael@0 | 2142 | } : d3_identity; |
michael@0 | 2143 | return function(specifier) { |
michael@0 | 2144 | var match = d3_format_re.exec(specifier), fill = match[1] || " ", align = match[2] || ">", sign = match[3] || "", symbol = match[4] || "", zfill = match[5], width = +match[6], comma = match[7], precision = match[8], type = match[9], scale = 1, prefix = "", suffix = "", integer = false; |
michael@0 | 2145 | if (precision) precision = +precision.substring(1); |
michael@0 | 2146 | if (zfill || fill === "0" && align === "=") { |
michael@0 | 2147 | zfill = fill = "0"; |
michael@0 | 2148 | align = "="; |
michael@0 | 2149 | if (comma) width -= Math.floor((width - 1) / 4); |
michael@0 | 2150 | } |
michael@0 | 2151 | switch (type) { |
michael@0 | 2152 | case "n": |
michael@0 | 2153 | comma = true; |
michael@0 | 2154 | type = "g"; |
michael@0 | 2155 | break; |
michael@0 | 2156 | |
michael@0 | 2157 | case "%": |
michael@0 | 2158 | scale = 100; |
michael@0 | 2159 | suffix = "%"; |
michael@0 | 2160 | type = "f"; |
michael@0 | 2161 | break; |
michael@0 | 2162 | |
michael@0 | 2163 | case "p": |
michael@0 | 2164 | scale = 100; |
michael@0 | 2165 | suffix = "%"; |
michael@0 | 2166 | type = "r"; |
michael@0 | 2167 | break; |
michael@0 | 2168 | |
michael@0 | 2169 | case "b": |
michael@0 | 2170 | case "o": |
michael@0 | 2171 | case "x": |
michael@0 | 2172 | case "X": |
michael@0 | 2173 | if (symbol === "#") prefix = "0" + type.toLowerCase(); |
michael@0 | 2174 | |
michael@0 | 2175 | case "c": |
michael@0 | 2176 | case "d": |
michael@0 | 2177 | integer = true; |
michael@0 | 2178 | precision = 0; |
michael@0 | 2179 | break; |
michael@0 | 2180 | |
michael@0 | 2181 | case "s": |
michael@0 | 2182 | scale = -1; |
michael@0 | 2183 | type = "r"; |
michael@0 | 2184 | break; |
michael@0 | 2185 | } |
michael@0 | 2186 | if (symbol === "$") prefix = locale_currency[0], suffix = locale_currency[1]; |
michael@0 | 2187 | if (type == "r" && !precision) type = "g"; |
michael@0 | 2188 | if (precision != null) { |
michael@0 | 2189 | if (type == "g") precision = Math.max(1, Math.min(21, precision)); else if (type == "e" || type == "f") precision = Math.max(0, Math.min(20, precision)); |
michael@0 | 2190 | } |
michael@0 | 2191 | type = d3_format_types.get(type) || d3_format_typeDefault; |
michael@0 | 2192 | var zcomma = zfill && comma; |
michael@0 | 2193 | return function(value) { |
michael@0 | 2194 | var fullSuffix = suffix; |
michael@0 | 2195 | if (integer && value % 1) return ""; |
michael@0 | 2196 | var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign; |
michael@0 | 2197 | if (scale < 0) { |
michael@0 | 2198 | var unit = d3.formatPrefix(value, precision); |
michael@0 | 2199 | value = unit.scale(value); |
michael@0 | 2200 | fullSuffix = unit.symbol + suffix; |
michael@0 | 2201 | } else { |
michael@0 | 2202 | value *= scale; |
michael@0 | 2203 | } |
michael@0 | 2204 | value = type(value, precision); |
michael@0 | 2205 | var i = value.lastIndexOf("."), before = i < 0 ? value : value.substring(0, i), after = i < 0 ? "" : locale_decimal + value.substring(i + 1); |
michael@0 | 2206 | if (!zfill && comma) before = formatGroup(before); |
michael@0 | 2207 | var length = prefix.length + before.length + after.length + (zcomma ? 0 : negative.length), padding = length < width ? new Array(length = width - length + 1).join(fill) : ""; |
michael@0 | 2208 | if (zcomma) before = formatGroup(padding + before); |
michael@0 | 2209 | negative += prefix; |
michael@0 | 2210 | value = before + after; |
michael@0 | 2211 | return (align === "<" ? negative + value + padding : align === ">" ? padding + negative + value : align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length) : negative + (zcomma ? value : padding + value)) + fullSuffix; |
michael@0 | 2212 | }; |
michael@0 | 2213 | }; |
michael@0 | 2214 | } |
michael@0 | 2215 | var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?([$#])?(0)?(\d+)?(,)?(\.-?\d+)?([a-z%])?/i; |
michael@0 | 2216 | var d3_format_types = d3.map({ |
michael@0 | 2217 | b: function(x) { |
michael@0 | 2218 | return x.toString(2); |
michael@0 | 2219 | }, |
michael@0 | 2220 | c: function(x) { |
michael@0 | 2221 | return String.fromCharCode(x); |
michael@0 | 2222 | }, |
michael@0 | 2223 | o: function(x) { |
michael@0 | 2224 | return x.toString(8); |
michael@0 | 2225 | }, |
michael@0 | 2226 | x: function(x) { |
michael@0 | 2227 | return x.toString(16); |
michael@0 | 2228 | }, |
michael@0 | 2229 | X: function(x) { |
michael@0 | 2230 | return x.toString(16).toUpperCase(); |
michael@0 | 2231 | }, |
michael@0 | 2232 | g: function(x, p) { |
michael@0 | 2233 | return x.toPrecision(p); |
michael@0 | 2234 | }, |
michael@0 | 2235 | e: function(x, p) { |
michael@0 | 2236 | return x.toExponential(p); |
michael@0 | 2237 | }, |
michael@0 | 2238 | f: function(x, p) { |
michael@0 | 2239 | return x.toFixed(p); |
michael@0 | 2240 | }, |
michael@0 | 2241 | r: function(x, p) { |
michael@0 | 2242 | return (x = d3.round(x, d3_format_precision(x, p))).toFixed(Math.max(0, Math.min(20, d3_format_precision(x * (1 + 1e-15), p)))); |
michael@0 | 2243 | } |
michael@0 | 2244 | }); |
michael@0 | 2245 | function d3_format_typeDefault(x) { |
michael@0 | 2246 | return x + ""; |
michael@0 | 2247 | } |
michael@0 | 2248 | var d3_time = d3.time = {}, d3_date = Date; |
michael@0 | 2249 | function d3_date_utc() { |
michael@0 | 2250 | this._ = new Date(arguments.length > 1 ? Date.UTC.apply(this, arguments) : arguments[0]); |
michael@0 | 2251 | } |
michael@0 | 2252 | d3_date_utc.prototype = { |
michael@0 | 2253 | getDate: function() { |
michael@0 | 2254 | return this._.getUTCDate(); |
michael@0 | 2255 | }, |
michael@0 | 2256 | getDay: function() { |
michael@0 | 2257 | return this._.getUTCDay(); |
michael@0 | 2258 | }, |
michael@0 | 2259 | getFullYear: function() { |
michael@0 | 2260 | return this._.getUTCFullYear(); |
michael@0 | 2261 | }, |
michael@0 | 2262 | getHours: function() { |
michael@0 | 2263 | return this._.getUTCHours(); |
michael@0 | 2264 | }, |
michael@0 | 2265 | getMilliseconds: function() { |
michael@0 | 2266 | return this._.getUTCMilliseconds(); |
michael@0 | 2267 | }, |
michael@0 | 2268 | getMinutes: function() { |
michael@0 | 2269 | return this._.getUTCMinutes(); |
michael@0 | 2270 | }, |
michael@0 | 2271 | getMonth: function() { |
michael@0 | 2272 | return this._.getUTCMonth(); |
michael@0 | 2273 | }, |
michael@0 | 2274 | getSeconds: function() { |
michael@0 | 2275 | return this._.getUTCSeconds(); |
michael@0 | 2276 | }, |
michael@0 | 2277 | getTime: function() { |
michael@0 | 2278 | return this._.getTime(); |
michael@0 | 2279 | }, |
michael@0 | 2280 | getTimezoneOffset: function() { |
michael@0 | 2281 | return 0; |
michael@0 | 2282 | }, |
michael@0 | 2283 | valueOf: function() { |
michael@0 | 2284 | return this._.valueOf(); |
michael@0 | 2285 | }, |
michael@0 | 2286 | setDate: function() { |
michael@0 | 2287 | d3_time_prototype.setUTCDate.apply(this._, arguments); |
michael@0 | 2288 | }, |
michael@0 | 2289 | setDay: function() { |
michael@0 | 2290 | d3_time_prototype.setUTCDay.apply(this._, arguments); |
michael@0 | 2291 | }, |
michael@0 | 2292 | setFullYear: function() { |
michael@0 | 2293 | d3_time_prototype.setUTCFullYear.apply(this._, arguments); |
michael@0 | 2294 | }, |
michael@0 | 2295 | setHours: function() { |
michael@0 | 2296 | d3_time_prototype.setUTCHours.apply(this._, arguments); |
michael@0 | 2297 | }, |
michael@0 | 2298 | setMilliseconds: function() { |
michael@0 | 2299 | d3_time_prototype.setUTCMilliseconds.apply(this._, arguments); |
michael@0 | 2300 | }, |
michael@0 | 2301 | setMinutes: function() { |
michael@0 | 2302 | d3_time_prototype.setUTCMinutes.apply(this._, arguments); |
michael@0 | 2303 | }, |
michael@0 | 2304 | setMonth: function() { |
michael@0 | 2305 | d3_time_prototype.setUTCMonth.apply(this._, arguments); |
michael@0 | 2306 | }, |
michael@0 | 2307 | setSeconds: function() { |
michael@0 | 2308 | d3_time_prototype.setUTCSeconds.apply(this._, arguments); |
michael@0 | 2309 | }, |
michael@0 | 2310 | setTime: function() { |
michael@0 | 2311 | d3_time_prototype.setTime.apply(this._, arguments); |
michael@0 | 2312 | } |
michael@0 | 2313 | }; |
michael@0 | 2314 | var d3_time_prototype = Date.prototype; |
michael@0 | 2315 | function d3_time_interval(local, step, number) { |
michael@0 | 2316 | function round(date) { |
michael@0 | 2317 | var d0 = local(date), d1 = offset(d0, 1); |
michael@0 | 2318 | return date - d0 < d1 - date ? d0 : d1; |
michael@0 | 2319 | } |
michael@0 | 2320 | function ceil(date) { |
michael@0 | 2321 | step(date = local(new d3_date(date - 1)), 1); |
michael@0 | 2322 | return date; |
michael@0 | 2323 | } |
michael@0 | 2324 | function offset(date, k) { |
michael@0 | 2325 | step(date = new d3_date(+date), k); |
michael@0 | 2326 | return date; |
michael@0 | 2327 | } |
michael@0 | 2328 | function range(t0, t1, dt) { |
michael@0 | 2329 | var time = ceil(t0), times = []; |
michael@0 | 2330 | if (dt > 1) { |
michael@0 | 2331 | while (time < t1) { |
michael@0 | 2332 | if (!(number(time) % dt)) times.push(new Date(+time)); |
michael@0 | 2333 | step(time, 1); |
michael@0 | 2334 | } |
michael@0 | 2335 | } else { |
michael@0 | 2336 | while (time < t1) times.push(new Date(+time)), step(time, 1); |
michael@0 | 2337 | } |
michael@0 | 2338 | return times; |
michael@0 | 2339 | } |
michael@0 | 2340 | function range_utc(t0, t1, dt) { |
michael@0 | 2341 | try { |
michael@0 | 2342 | d3_date = d3_date_utc; |
michael@0 | 2343 | var utc = new d3_date_utc(); |
michael@0 | 2344 | utc._ = t0; |
michael@0 | 2345 | return range(utc, t1, dt); |
michael@0 | 2346 | } finally { |
michael@0 | 2347 | d3_date = Date; |
michael@0 | 2348 | } |
michael@0 | 2349 | } |
michael@0 | 2350 | local.floor = local; |
michael@0 | 2351 | local.round = round; |
michael@0 | 2352 | local.ceil = ceil; |
michael@0 | 2353 | local.offset = offset; |
michael@0 | 2354 | local.range = range; |
michael@0 | 2355 | var utc = local.utc = d3_time_interval_utc(local); |
michael@0 | 2356 | utc.floor = utc; |
michael@0 | 2357 | utc.round = d3_time_interval_utc(round); |
michael@0 | 2358 | utc.ceil = d3_time_interval_utc(ceil); |
michael@0 | 2359 | utc.offset = d3_time_interval_utc(offset); |
michael@0 | 2360 | utc.range = range_utc; |
michael@0 | 2361 | return local; |
michael@0 | 2362 | } |
michael@0 | 2363 | function d3_time_interval_utc(method) { |
michael@0 | 2364 | return function(date, k) { |
michael@0 | 2365 | try { |
michael@0 | 2366 | d3_date = d3_date_utc; |
michael@0 | 2367 | var utc = new d3_date_utc(); |
michael@0 | 2368 | utc._ = date; |
michael@0 | 2369 | return method(utc, k)._; |
michael@0 | 2370 | } finally { |
michael@0 | 2371 | d3_date = Date; |
michael@0 | 2372 | } |
michael@0 | 2373 | }; |
michael@0 | 2374 | } |
michael@0 | 2375 | d3_time.year = d3_time_interval(function(date) { |
michael@0 | 2376 | date = d3_time.day(date); |
michael@0 | 2377 | date.setMonth(0, 1); |
michael@0 | 2378 | return date; |
michael@0 | 2379 | }, function(date, offset) { |
michael@0 | 2380 | date.setFullYear(date.getFullYear() + offset); |
michael@0 | 2381 | }, function(date) { |
michael@0 | 2382 | return date.getFullYear(); |
michael@0 | 2383 | }); |
michael@0 | 2384 | d3_time.years = d3_time.year.range; |
michael@0 | 2385 | d3_time.years.utc = d3_time.year.utc.range; |
michael@0 | 2386 | d3_time.day = d3_time_interval(function(date) { |
michael@0 | 2387 | var day = new d3_date(2e3, 0); |
michael@0 | 2388 | day.setFullYear(date.getFullYear(), date.getMonth(), date.getDate()); |
michael@0 | 2389 | return day; |
michael@0 | 2390 | }, function(date, offset) { |
michael@0 | 2391 | date.setDate(date.getDate() + offset); |
michael@0 | 2392 | }, function(date) { |
michael@0 | 2393 | return date.getDate() - 1; |
michael@0 | 2394 | }); |
michael@0 | 2395 | d3_time.days = d3_time.day.range; |
michael@0 | 2396 | d3_time.days.utc = d3_time.day.utc.range; |
michael@0 | 2397 | d3_time.dayOfYear = function(date) { |
michael@0 | 2398 | var year = d3_time.year(date); |
michael@0 | 2399 | return Math.floor((date - year - (date.getTimezoneOffset() - year.getTimezoneOffset()) * 6e4) / 864e5); |
michael@0 | 2400 | }; |
michael@0 | 2401 | [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ].forEach(function(day, i) { |
michael@0 | 2402 | i = 7 - i; |
michael@0 | 2403 | var interval = d3_time[day] = d3_time_interval(function(date) { |
michael@0 | 2404 | (date = d3_time.day(date)).setDate(date.getDate() - (date.getDay() + i) % 7); |
michael@0 | 2405 | return date; |
michael@0 | 2406 | }, function(date, offset) { |
michael@0 | 2407 | date.setDate(date.getDate() + Math.floor(offset) * 7); |
michael@0 | 2408 | }, function(date) { |
michael@0 | 2409 | var day = d3_time.year(date).getDay(); |
michael@0 | 2410 | return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7) - (day !== i); |
michael@0 | 2411 | }); |
michael@0 | 2412 | d3_time[day + "s"] = interval.range; |
michael@0 | 2413 | d3_time[day + "s"].utc = interval.utc.range; |
michael@0 | 2414 | d3_time[day + "OfYear"] = function(date) { |
michael@0 | 2415 | var day = d3_time.year(date).getDay(); |
michael@0 | 2416 | return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7); |
michael@0 | 2417 | }; |
michael@0 | 2418 | }); |
michael@0 | 2419 | d3_time.week = d3_time.sunday; |
michael@0 | 2420 | d3_time.weeks = d3_time.sunday.range; |
michael@0 | 2421 | d3_time.weeks.utc = d3_time.sunday.utc.range; |
michael@0 | 2422 | d3_time.weekOfYear = d3_time.sundayOfYear; |
michael@0 | 2423 | function d3_locale_timeFormat(locale) { |
michael@0 | 2424 | var locale_dateTime = locale.dateTime, locale_date = locale.date, locale_time = locale.time, locale_periods = locale.periods, locale_days = locale.days, locale_shortDays = locale.shortDays, locale_months = locale.months, locale_shortMonths = locale.shortMonths; |
michael@0 | 2425 | function d3_time_format(template) { |
michael@0 | 2426 | var n = template.length; |
michael@0 | 2427 | function format(date) { |
michael@0 | 2428 | var string = [], i = -1, j = 0, c, p, f; |
michael@0 | 2429 | while (++i < n) { |
michael@0 | 2430 | if (template.charCodeAt(i) === 37) { |
michael@0 | 2431 | string.push(template.substring(j, i)); |
michael@0 | 2432 | if ((p = d3_time_formatPads[c = template.charAt(++i)]) != null) c = template.charAt(++i); |
michael@0 | 2433 | if (f = d3_time_formats[c]) c = f(date, p == null ? c === "e" ? " " : "0" : p); |
michael@0 | 2434 | string.push(c); |
michael@0 | 2435 | j = i + 1; |
michael@0 | 2436 | } |
michael@0 | 2437 | } |
michael@0 | 2438 | string.push(template.substring(j, i)); |
michael@0 | 2439 | return string.join(""); |
michael@0 | 2440 | } |
michael@0 | 2441 | format.parse = function(string) { |
michael@0 | 2442 | var d = { |
michael@0 | 2443 | y: 1900, |
michael@0 | 2444 | m: 0, |
michael@0 | 2445 | d: 1, |
michael@0 | 2446 | H: 0, |
michael@0 | 2447 | M: 0, |
michael@0 | 2448 | S: 0, |
michael@0 | 2449 | L: 0, |
michael@0 | 2450 | Z: null |
michael@0 | 2451 | }, i = d3_time_parse(d, template, string, 0); |
michael@0 | 2452 | if (i != string.length) return null; |
michael@0 | 2453 | if ("p" in d) d.H = d.H % 12 + d.p * 12; |
michael@0 | 2454 | var localZ = d.Z != null && d3_date !== d3_date_utc, date = new (localZ ? d3_date_utc : d3_date)(); |
michael@0 | 2455 | if ("j" in d) date.setFullYear(d.y, 0, d.j); else if ("w" in d && ("W" in d || "U" in d)) { |
michael@0 | 2456 | date.setFullYear(d.y, 0, 1); |
michael@0 | 2457 | date.setFullYear(d.y, 0, "W" in d ? (d.w + 6) % 7 + d.W * 7 - (date.getDay() + 5) % 7 : d.w + d.U * 7 - (date.getDay() + 6) % 7); |
michael@0 | 2458 | } else date.setFullYear(d.y, d.m, d.d); |
michael@0 | 2459 | date.setHours(d.H + Math.floor(d.Z / 100), d.M + d.Z % 100, d.S, d.L); |
michael@0 | 2460 | return localZ ? date._ : date; |
michael@0 | 2461 | }; |
michael@0 | 2462 | format.toString = function() { |
michael@0 | 2463 | return template; |
michael@0 | 2464 | }; |
michael@0 | 2465 | return format; |
michael@0 | 2466 | } |
michael@0 | 2467 | function d3_time_parse(date, template, string, j) { |
michael@0 | 2468 | var c, p, t, i = 0, n = template.length, m = string.length; |
michael@0 | 2469 | while (i < n) { |
michael@0 | 2470 | if (j >= m) return -1; |
michael@0 | 2471 | c = template.charCodeAt(i++); |
michael@0 | 2472 | if (c === 37) { |
michael@0 | 2473 | t = template.charAt(i++); |
michael@0 | 2474 | p = d3_time_parsers[t in d3_time_formatPads ? template.charAt(i++) : t]; |
michael@0 | 2475 | if (!p || (j = p(date, string, j)) < 0) return -1; |
michael@0 | 2476 | } else if (c != string.charCodeAt(j++)) { |
michael@0 | 2477 | return -1; |
michael@0 | 2478 | } |
michael@0 | 2479 | } |
michael@0 | 2480 | return j; |
michael@0 | 2481 | } |
michael@0 | 2482 | d3_time_format.utc = function(template) { |
michael@0 | 2483 | var local = d3_time_format(template); |
michael@0 | 2484 | function format(date) { |
michael@0 | 2485 | try { |
michael@0 | 2486 | d3_date = d3_date_utc; |
michael@0 | 2487 | var utc = new d3_date(); |
michael@0 | 2488 | utc._ = date; |
michael@0 | 2489 | return local(utc); |
michael@0 | 2490 | } finally { |
michael@0 | 2491 | d3_date = Date; |
michael@0 | 2492 | } |
michael@0 | 2493 | } |
michael@0 | 2494 | format.parse = function(string) { |
michael@0 | 2495 | try { |
michael@0 | 2496 | d3_date = d3_date_utc; |
michael@0 | 2497 | var date = local.parse(string); |
michael@0 | 2498 | return date && date._; |
michael@0 | 2499 | } finally { |
michael@0 | 2500 | d3_date = Date; |
michael@0 | 2501 | } |
michael@0 | 2502 | }; |
michael@0 | 2503 | format.toString = local.toString; |
michael@0 | 2504 | return format; |
michael@0 | 2505 | }; |
michael@0 | 2506 | d3_time_format.multi = d3_time_format.utc.multi = d3_time_formatMulti; |
michael@0 | 2507 | var d3_time_periodLookup = d3.map(), d3_time_dayRe = d3_time_formatRe(locale_days), d3_time_dayLookup = d3_time_formatLookup(locale_days), d3_time_dayAbbrevRe = d3_time_formatRe(locale_shortDays), d3_time_dayAbbrevLookup = d3_time_formatLookup(locale_shortDays), d3_time_monthRe = d3_time_formatRe(locale_months), d3_time_monthLookup = d3_time_formatLookup(locale_months), d3_time_monthAbbrevRe = d3_time_formatRe(locale_shortMonths), d3_time_monthAbbrevLookup = d3_time_formatLookup(locale_shortMonths); |
michael@0 | 2508 | locale_periods.forEach(function(p, i) { |
michael@0 | 2509 | d3_time_periodLookup.set(p.toLowerCase(), i); |
michael@0 | 2510 | }); |
michael@0 | 2511 | var d3_time_formats = { |
michael@0 | 2512 | a: function(d) { |
michael@0 | 2513 | return locale_shortDays[d.getDay()]; |
michael@0 | 2514 | }, |
michael@0 | 2515 | A: function(d) { |
michael@0 | 2516 | return locale_days[d.getDay()]; |
michael@0 | 2517 | }, |
michael@0 | 2518 | b: function(d) { |
michael@0 | 2519 | return locale_shortMonths[d.getMonth()]; |
michael@0 | 2520 | }, |
michael@0 | 2521 | B: function(d) { |
michael@0 | 2522 | return locale_months[d.getMonth()]; |
michael@0 | 2523 | }, |
michael@0 | 2524 | c: d3_time_format(locale_dateTime), |
michael@0 | 2525 | d: function(d, p) { |
michael@0 | 2526 | return d3_time_formatPad(d.getDate(), p, 2); |
michael@0 | 2527 | }, |
michael@0 | 2528 | e: function(d, p) { |
michael@0 | 2529 | return d3_time_formatPad(d.getDate(), p, 2); |
michael@0 | 2530 | }, |
michael@0 | 2531 | H: function(d, p) { |
michael@0 | 2532 | return d3_time_formatPad(d.getHours(), p, 2); |
michael@0 | 2533 | }, |
michael@0 | 2534 | I: function(d, p) { |
michael@0 | 2535 | return d3_time_formatPad(d.getHours() % 12 || 12, p, 2); |
michael@0 | 2536 | }, |
michael@0 | 2537 | j: function(d, p) { |
michael@0 | 2538 | return d3_time_formatPad(1 + d3_time.dayOfYear(d), p, 3); |
michael@0 | 2539 | }, |
michael@0 | 2540 | L: function(d, p) { |
michael@0 | 2541 | return d3_time_formatPad(d.getMilliseconds(), p, 3); |
michael@0 | 2542 | }, |
michael@0 | 2543 | m: function(d, p) { |
michael@0 | 2544 | return d3_time_formatPad(d.getMonth() + 1, p, 2); |
michael@0 | 2545 | }, |
michael@0 | 2546 | M: function(d, p) { |
michael@0 | 2547 | return d3_time_formatPad(d.getMinutes(), p, 2); |
michael@0 | 2548 | }, |
michael@0 | 2549 | p: function(d) { |
michael@0 | 2550 | return locale_periods[+(d.getHours() >= 12)]; |
michael@0 | 2551 | }, |
michael@0 | 2552 | S: function(d, p) { |
michael@0 | 2553 | return d3_time_formatPad(d.getSeconds(), p, 2); |
michael@0 | 2554 | }, |
michael@0 | 2555 | U: function(d, p) { |
michael@0 | 2556 | return d3_time_formatPad(d3_time.sundayOfYear(d), p, 2); |
michael@0 | 2557 | }, |
michael@0 | 2558 | w: function(d) { |
michael@0 | 2559 | return d.getDay(); |
michael@0 | 2560 | }, |
michael@0 | 2561 | W: function(d, p) { |
michael@0 | 2562 | return d3_time_formatPad(d3_time.mondayOfYear(d), p, 2); |
michael@0 | 2563 | }, |
michael@0 | 2564 | x: d3_time_format(locale_date), |
michael@0 | 2565 | X: d3_time_format(locale_time), |
michael@0 | 2566 | y: function(d, p) { |
michael@0 | 2567 | return d3_time_formatPad(d.getFullYear() % 100, p, 2); |
michael@0 | 2568 | }, |
michael@0 | 2569 | Y: function(d, p) { |
michael@0 | 2570 | return d3_time_formatPad(d.getFullYear() % 1e4, p, 4); |
michael@0 | 2571 | }, |
michael@0 | 2572 | Z: d3_time_zone, |
michael@0 | 2573 | "%": function() { |
michael@0 | 2574 | return "%"; |
michael@0 | 2575 | } |
michael@0 | 2576 | }; |
michael@0 | 2577 | var d3_time_parsers = { |
michael@0 | 2578 | a: d3_time_parseWeekdayAbbrev, |
michael@0 | 2579 | A: d3_time_parseWeekday, |
michael@0 | 2580 | b: d3_time_parseMonthAbbrev, |
michael@0 | 2581 | B: d3_time_parseMonth, |
michael@0 | 2582 | c: d3_time_parseLocaleFull, |
michael@0 | 2583 | d: d3_time_parseDay, |
michael@0 | 2584 | e: d3_time_parseDay, |
michael@0 | 2585 | H: d3_time_parseHour24, |
michael@0 | 2586 | I: d3_time_parseHour24, |
michael@0 | 2587 | j: d3_time_parseDayOfYear, |
michael@0 | 2588 | L: d3_time_parseMilliseconds, |
michael@0 | 2589 | m: d3_time_parseMonthNumber, |
michael@0 | 2590 | M: d3_time_parseMinutes, |
michael@0 | 2591 | p: d3_time_parseAmPm, |
michael@0 | 2592 | S: d3_time_parseSeconds, |
michael@0 | 2593 | U: d3_time_parseWeekNumberSunday, |
michael@0 | 2594 | w: d3_time_parseWeekdayNumber, |
michael@0 | 2595 | W: d3_time_parseWeekNumberMonday, |
michael@0 | 2596 | x: d3_time_parseLocaleDate, |
michael@0 | 2597 | X: d3_time_parseLocaleTime, |
michael@0 | 2598 | y: d3_time_parseYear, |
michael@0 | 2599 | Y: d3_time_parseFullYear, |
michael@0 | 2600 | Z: d3_time_parseZone, |
michael@0 | 2601 | "%": d3_time_parseLiteralPercent |
michael@0 | 2602 | }; |
michael@0 | 2603 | function d3_time_parseWeekdayAbbrev(date, string, i) { |
michael@0 | 2604 | d3_time_dayAbbrevRe.lastIndex = 0; |
michael@0 | 2605 | var n = d3_time_dayAbbrevRe.exec(string.substring(i)); |
michael@0 | 2606 | return n ? (date.w = d3_time_dayAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; |
michael@0 | 2607 | } |
michael@0 | 2608 | function d3_time_parseWeekday(date, string, i) { |
michael@0 | 2609 | d3_time_dayRe.lastIndex = 0; |
michael@0 | 2610 | var n = d3_time_dayRe.exec(string.substring(i)); |
michael@0 | 2611 | return n ? (date.w = d3_time_dayLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; |
michael@0 | 2612 | } |
michael@0 | 2613 | function d3_time_parseMonthAbbrev(date, string, i) { |
michael@0 | 2614 | d3_time_monthAbbrevRe.lastIndex = 0; |
michael@0 | 2615 | var n = d3_time_monthAbbrevRe.exec(string.substring(i)); |
michael@0 | 2616 | return n ? (date.m = d3_time_monthAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; |
michael@0 | 2617 | } |
michael@0 | 2618 | function d3_time_parseMonth(date, string, i) { |
michael@0 | 2619 | d3_time_monthRe.lastIndex = 0; |
michael@0 | 2620 | var n = d3_time_monthRe.exec(string.substring(i)); |
michael@0 | 2621 | return n ? (date.m = d3_time_monthLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; |
michael@0 | 2622 | } |
michael@0 | 2623 | function d3_time_parseLocaleFull(date, string, i) { |
michael@0 | 2624 | return d3_time_parse(date, d3_time_formats.c.toString(), string, i); |
michael@0 | 2625 | } |
michael@0 | 2626 | function d3_time_parseLocaleDate(date, string, i) { |
michael@0 | 2627 | return d3_time_parse(date, d3_time_formats.x.toString(), string, i); |
michael@0 | 2628 | } |
michael@0 | 2629 | function d3_time_parseLocaleTime(date, string, i) { |
michael@0 | 2630 | return d3_time_parse(date, d3_time_formats.X.toString(), string, i); |
michael@0 | 2631 | } |
michael@0 | 2632 | function d3_time_parseAmPm(date, string, i) { |
michael@0 | 2633 | var n = d3_time_periodLookup.get(string.substring(i, i += 2).toLowerCase()); |
michael@0 | 2634 | return n == null ? -1 : (date.p = n, i); |
michael@0 | 2635 | } |
michael@0 | 2636 | return d3_time_format; |
michael@0 | 2637 | } |
michael@0 | 2638 | var d3_time_formatPads = { |
michael@0 | 2639 | "-": "", |
michael@0 | 2640 | _: " ", |
michael@0 | 2641 | "0": "0" |
michael@0 | 2642 | }, d3_time_numberRe = /^\s*\d+/, d3_time_percentRe = /^%/; |
michael@0 | 2643 | function d3_time_formatPad(value, fill, width) { |
michael@0 | 2644 | var sign = value < 0 ? "-" : "", string = (sign ? -value : value) + "", length = string.length; |
michael@0 | 2645 | return sign + (length < width ? new Array(width - length + 1).join(fill) + string : string); |
michael@0 | 2646 | } |
michael@0 | 2647 | function d3_time_formatRe(names) { |
michael@0 | 2648 | return new RegExp("^(?:" + names.map(d3.requote).join("|") + ")", "i"); |
michael@0 | 2649 | } |
michael@0 | 2650 | function d3_time_formatLookup(names) { |
michael@0 | 2651 | var map = new d3_Map(), i = -1, n = names.length; |
michael@0 | 2652 | while (++i < n) map.set(names[i].toLowerCase(), i); |
michael@0 | 2653 | return map; |
michael@0 | 2654 | } |
michael@0 | 2655 | function d3_time_parseWeekdayNumber(date, string, i) { |
michael@0 | 2656 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2657 | var n = d3_time_numberRe.exec(string.substring(i, i + 1)); |
michael@0 | 2658 | return n ? (date.w = +n[0], i + n[0].length) : -1; |
michael@0 | 2659 | } |
michael@0 | 2660 | function d3_time_parseWeekNumberSunday(date, string, i) { |
michael@0 | 2661 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2662 | var n = d3_time_numberRe.exec(string.substring(i)); |
michael@0 | 2663 | return n ? (date.U = +n[0], i + n[0].length) : -1; |
michael@0 | 2664 | } |
michael@0 | 2665 | function d3_time_parseWeekNumberMonday(date, string, i) { |
michael@0 | 2666 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2667 | var n = d3_time_numberRe.exec(string.substring(i)); |
michael@0 | 2668 | return n ? (date.W = +n[0], i + n[0].length) : -1; |
michael@0 | 2669 | } |
michael@0 | 2670 | function d3_time_parseFullYear(date, string, i) { |
michael@0 | 2671 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2672 | var n = d3_time_numberRe.exec(string.substring(i, i + 4)); |
michael@0 | 2673 | return n ? (date.y = +n[0], i + n[0].length) : -1; |
michael@0 | 2674 | } |
michael@0 | 2675 | function d3_time_parseYear(date, string, i) { |
michael@0 | 2676 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2677 | var n = d3_time_numberRe.exec(string.substring(i, i + 2)); |
michael@0 | 2678 | return n ? (date.y = d3_time_expandYear(+n[0]), i + n[0].length) : -1; |
michael@0 | 2679 | } |
michael@0 | 2680 | function d3_time_parseZone(date, string, i) { |
michael@0 | 2681 | return /^[+-]\d{4}$/.test(string = string.substring(i, i + 5)) ? (date.Z = +string, |
michael@0 | 2682 | i + 5) : -1; |
michael@0 | 2683 | } |
michael@0 | 2684 | function d3_time_expandYear(d) { |
michael@0 | 2685 | return d + (d > 68 ? 1900 : 2e3); |
michael@0 | 2686 | } |
michael@0 | 2687 | function d3_time_parseMonthNumber(date, string, i) { |
michael@0 | 2688 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2689 | var n = d3_time_numberRe.exec(string.substring(i, i + 2)); |
michael@0 | 2690 | return n ? (date.m = n[0] - 1, i + n[0].length) : -1; |
michael@0 | 2691 | } |
michael@0 | 2692 | function d3_time_parseDay(date, string, i) { |
michael@0 | 2693 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2694 | var n = d3_time_numberRe.exec(string.substring(i, i + 2)); |
michael@0 | 2695 | return n ? (date.d = +n[0], i + n[0].length) : -1; |
michael@0 | 2696 | } |
michael@0 | 2697 | function d3_time_parseDayOfYear(date, string, i) { |
michael@0 | 2698 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2699 | var n = d3_time_numberRe.exec(string.substring(i, i + 3)); |
michael@0 | 2700 | return n ? (date.j = +n[0], i + n[0].length) : -1; |
michael@0 | 2701 | } |
michael@0 | 2702 | function d3_time_parseHour24(date, string, i) { |
michael@0 | 2703 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2704 | var n = d3_time_numberRe.exec(string.substring(i, i + 2)); |
michael@0 | 2705 | return n ? (date.H = +n[0], i + n[0].length) : -1; |
michael@0 | 2706 | } |
michael@0 | 2707 | function d3_time_parseMinutes(date, string, i) { |
michael@0 | 2708 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2709 | var n = d3_time_numberRe.exec(string.substring(i, i + 2)); |
michael@0 | 2710 | return n ? (date.M = +n[0], i + n[0].length) : -1; |
michael@0 | 2711 | } |
michael@0 | 2712 | function d3_time_parseSeconds(date, string, i) { |
michael@0 | 2713 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2714 | var n = d3_time_numberRe.exec(string.substring(i, i + 2)); |
michael@0 | 2715 | return n ? (date.S = +n[0], i + n[0].length) : -1; |
michael@0 | 2716 | } |
michael@0 | 2717 | function d3_time_parseMilliseconds(date, string, i) { |
michael@0 | 2718 | d3_time_numberRe.lastIndex = 0; |
michael@0 | 2719 | var n = d3_time_numberRe.exec(string.substring(i, i + 3)); |
michael@0 | 2720 | return n ? (date.L = +n[0], i + n[0].length) : -1; |
michael@0 | 2721 | } |
michael@0 | 2722 | function d3_time_zone(d) { |
michael@0 | 2723 | var z = d.getTimezoneOffset(), zs = z > 0 ? "-" : "+", zh = ~~(abs(z) / 60), zm = abs(z) % 60; |
michael@0 | 2724 | return zs + d3_time_formatPad(zh, "0", 2) + d3_time_formatPad(zm, "0", 2); |
michael@0 | 2725 | } |
michael@0 | 2726 | function d3_time_parseLiteralPercent(date, string, i) { |
michael@0 | 2727 | d3_time_percentRe.lastIndex = 0; |
michael@0 | 2728 | var n = d3_time_percentRe.exec(string.substring(i, i + 1)); |
michael@0 | 2729 | return n ? i + n[0].length : -1; |
michael@0 | 2730 | } |
michael@0 | 2731 | function d3_time_formatMulti(formats) { |
michael@0 | 2732 | var n = formats.length, i = -1; |
michael@0 | 2733 | while (++i < n) formats[i][0] = this(formats[i][0]); |
michael@0 | 2734 | return function(date) { |
michael@0 | 2735 | var i = 0, f = formats[i]; |
michael@0 | 2736 | while (!f[1](date)) f = formats[++i]; |
michael@0 | 2737 | return f[0](date); |
michael@0 | 2738 | }; |
michael@0 | 2739 | } |
michael@0 | 2740 | d3.locale = function(locale) { |
michael@0 | 2741 | return { |
michael@0 | 2742 | numberFormat: d3_locale_numberFormat(locale), |
michael@0 | 2743 | timeFormat: d3_locale_timeFormat(locale) |
michael@0 | 2744 | }; |
michael@0 | 2745 | }; |
michael@0 | 2746 | var d3_locale_enUS = d3.locale({ |
michael@0 | 2747 | decimal: ".", |
michael@0 | 2748 | thousands: ",", |
michael@0 | 2749 | grouping: [ 3 ], |
michael@0 | 2750 | currency: [ "$", "" ], |
michael@0 | 2751 | dateTime: "%a %b %e %X %Y", |
michael@0 | 2752 | date: "%m/%d/%Y", |
michael@0 | 2753 | time: "%H:%M:%S", |
michael@0 | 2754 | periods: [ "AM", "PM" ], |
michael@0 | 2755 | days: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], |
michael@0 | 2756 | shortDays: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], |
michael@0 | 2757 | months: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], |
michael@0 | 2758 | shortMonths: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] |
michael@0 | 2759 | }); |
michael@0 | 2760 | d3.format = d3_locale_enUS.numberFormat; |
michael@0 | 2761 | d3.geo = {}; |
michael@0 | 2762 | function d3_adder() {} |
michael@0 | 2763 | d3_adder.prototype = { |
michael@0 | 2764 | s: 0, |
michael@0 | 2765 | t: 0, |
michael@0 | 2766 | add: function(y) { |
michael@0 | 2767 | d3_adderSum(y, this.t, d3_adderTemp); |
michael@0 | 2768 | d3_adderSum(d3_adderTemp.s, this.s, this); |
michael@0 | 2769 | if (this.s) this.t += d3_adderTemp.t; else this.s = d3_adderTemp.t; |
michael@0 | 2770 | }, |
michael@0 | 2771 | reset: function() { |
michael@0 | 2772 | this.s = this.t = 0; |
michael@0 | 2773 | }, |
michael@0 | 2774 | valueOf: function() { |
michael@0 | 2775 | return this.s; |
michael@0 | 2776 | } |
michael@0 | 2777 | }; |
michael@0 | 2778 | var d3_adderTemp = new d3_adder(); |
michael@0 | 2779 | function d3_adderSum(a, b, o) { |
michael@0 | 2780 | var x = o.s = a + b, bv = x - a, av = x - bv; |
michael@0 | 2781 | o.t = a - av + (b - bv); |
michael@0 | 2782 | } |
michael@0 | 2783 | d3.geo.stream = function(object, listener) { |
michael@0 | 2784 | if (object && d3_geo_streamObjectType.hasOwnProperty(object.type)) { |
michael@0 | 2785 | d3_geo_streamObjectType[object.type](object, listener); |
michael@0 | 2786 | } else { |
michael@0 | 2787 | d3_geo_streamGeometry(object, listener); |
michael@0 | 2788 | } |
michael@0 | 2789 | }; |
michael@0 | 2790 | function d3_geo_streamGeometry(geometry, listener) { |
michael@0 | 2791 | if (geometry && d3_geo_streamGeometryType.hasOwnProperty(geometry.type)) { |
michael@0 | 2792 | d3_geo_streamGeometryType[geometry.type](geometry, listener); |
michael@0 | 2793 | } |
michael@0 | 2794 | } |
michael@0 | 2795 | var d3_geo_streamObjectType = { |
michael@0 | 2796 | Feature: function(feature, listener) { |
michael@0 | 2797 | d3_geo_streamGeometry(feature.geometry, listener); |
michael@0 | 2798 | }, |
michael@0 | 2799 | FeatureCollection: function(object, listener) { |
michael@0 | 2800 | var features = object.features, i = -1, n = features.length; |
michael@0 | 2801 | while (++i < n) d3_geo_streamGeometry(features[i].geometry, listener); |
michael@0 | 2802 | } |
michael@0 | 2803 | }; |
michael@0 | 2804 | var d3_geo_streamGeometryType = { |
michael@0 | 2805 | Sphere: function(object, listener) { |
michael@0 | 2806 | listener.sphere(); |
michael@0 | 2807 | }, |
michael@0 | 2808 | Point: function(object, listener) { |
michael@0 | 2809 | object = object.coordinates; |
michael@0 | 2810 | listener.point(object[0], object[1], object[2]); |
michael@0 | 2811 | }, |
michael@0 | 2812 | MultiPoint: function(object, listener) { |
michael@0 | 2813 | var coordinates = object.coordinates, i = -1, n = coordinates.length; |
michael@0 | 2814 | while (++i < n) object = coordinates[i], listener.point(object[0], object[1], object[2]); |
michael@0 | 2815 | }, |
michael@0 | 2816 | LineString: function(object, listener) { |
michael@0 | 2817 | d3_geo_streamLine(object.coordinates, listener, 0); |
michael@0 | 2818 | }, |
michael@0 | 2819 | MultiLineString: function(object, listener) { |
michael@0 | 2820 | var coordinates = object.coordinates, i = -1, n = coordinates.length; |
michael@0 | 2821 | while (++i < n) d3_geo_streamLine(coordinates[i], listener, 0); |
michael@0 | 2822 | }, |
michael@0 | 2823 | Polygon: function(object, listener) { |
michael@0 | 2824 | d3_geo_streamPolygon(object.coordinates, listener); |
michael@0 | 2825 | }, |
michael@0 | 2826 | MultiPolygon: function(object, listener) { |
michael@0 | 2827 | var coordinates = object.coordinates, i = -1, n = coordinates.length; |
michael@0 | 2828 | while (++i < n) d3_geo_streamPolygon(coordinates[i], listener); |
michael@0 | 2829 | }, |
michael@0 | 2830 | GeometryCollection: function(object, listener) { |
michael@0 | 2831 | var geometries = object.geometries, i = -1, n = geometries.length; |
michael@0 | 2832 | while (++i < n) d3_geo_streamGeometry(geometries[i], listener); |
michael@0 | 2833 | } |
michael@0 | 2834 | }; |
michael@0 | 2835 | function d3_geo_streamLine(coordinates, listener, closed) { |
michael@0 | 2836 | var i = -1, n = coordinates.length - closed, coordinate; |
michael@0 | 2837 | listener.lineStart(); |
michael@0 | 2838 | while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1], coordinate[2]); |
michael@0 | 2839 | listener.lineEnd(); |
michael@0 | 2840 | } |
michael@0 | 2841 | function d3_geo_streamPolygon(coordinates, listener) { |
michael@0 | 2842 | var i = -1, n = coordinates.length; |
michael@0 | 2843 | listener.polygonStart(); |
michael@0 | 2844 | while (++i < n) d3_geo_streamLine(coordinates[i], listener, 1); |
michael@0 | 2845 | listener.polygonEnd(); |
michael@0 | 2846 | } |
michael@0 | 2847 | d3.geo.area = function(object) { |
michael@0 | 2848 | d3_geo_areaSum = 0; |
michael@0 | 2849 | d3.geo.stream(object, d3_geo_area); |
michael@0 | 2850 | return d3_geo_areaSum; |
michael@0 | 2851 | }; |
michael@0 | 2852 | var d3_geo_areaSum, d3_geo_areaRingSum = new d3_adder(); |
michael@0 | 2853 | var d3_geo_area = { |
michael@0 | 2854 | sphere: function() { |
michael@0 | 2855 | d3_geo_areaSum += 4 * π; |
michael@0 | 2856 | }, |
michael@0 | 2857 | point: d3_noop, |
michael@0 | 2858 | lineStart: d3_noop, |
michael@0 | 2859 | lineEnd: d3_noop, |
michael@0 | 2860 | polygonStart: function() { |
michael@0 | 2861 | d3_geo_areaRingSum.reset(); |
michael@0 | 2862 | d3_geo_area.lineStart = d3_geo_areaRingStart; |
michael@0 | 2863 | }, |
michael@0 | 2864 | polygonEnd: function() { |
michael@0 | 2865 | var area = 2 * d3_geo_areaRingSum; |
michael@0 | 2866 | d3_geo_areaSum += area < 0 ? 4 * π + area : area; |
michael@0 | 2867 | d3_geo_area.lineStart = d3_geo_area.lineEnd = d3_geo_area.point = d3_noop; |
michael@0 | 2868 | } |
michael@0 | 2869 | }; |
michael@0 | 2870 | function d3_geo_areaRingStart() { |
michael@0 | 2871 | var λ00, φ00, λ0, cosφ0, sinφ0; |
michael@0 | 2872 | d3_geo_area.point = function(λ, φ) { |
michael@0 | 2873 | d3_geo_area.point = nextPoint; |
michael@0 | 2874 | λ0 = (λ00 = λ) * d3_radians, cosφ0 = Math.cos(φ = (φ00 = φ) * d3_radians / 2 + π / 4), |
michael@0 | 2875 | sinφ0 = Math.sin(φ); |
michael@0 | 2876 | }; |
michael@0 | 2877 | function nextPoint(λ, φ) { |
michael@0 | 2878 | λ *= d3_radians; |
michael@0 | 2879 | φ = φ * d3_radians / 2 + π / 4; |
michael@0 | 2880 | var dλ = λ - λ0, cosφ = Math.cos(φ), sinφ = Math.sin(φ), k = sinφ0 * sinφ, u = cosφ0 * cosφ + k * Math.cos(dλ), v = k * Math.sin(dλ); |
michael@0 | 2881 | d3_geo_areaRingSum.add(Math.atan2(v, u)); |
michael@0 | 2882 | λ0 = λ, cosφ0 = cosφ, sinφ0 = sinφ; |
michael@0 | 2883 | } |
michael@0 | 2884 | d3_geo_area.lineEnd = function() { |
michael@0 | 2885 | nextPoint(λ00, φ00); |
michael@0 | 2886 | }; |
michael@0 | 2887 | } |
michael@0 | 2888 | function d3_geo_cartesian(spherical) { |
michael@0 | 2889 | var λ = spherical[0], φ = spherical[1], cosφ = Math.cos(φ); |
michael@0 | 2890 | return [ cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ) ]; |
michael@0 | 2891 | } |
michael@0 | 2892 | function d3_geo_cartesianDot(a, b) { |
michael@0 | 2893 | return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; |
michael@0 | 2894 | } |
michael@0 | 2895 | function d3_geo_cartesianCross(a, b) { |
michael@0 | 2896 | return [ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ]; |
michael@0 | 2897 | } |
michael@0 | 2898 | function d3_geo_cartesianAdd(a, b) { |
michael@0 | 2899 | a[0] += b[0]; |
michael@0 | 2900 | a[1] += b[1]; |
michael@0 | 2901 | a[2] += b[2]; |
michael@0 | 2902 | } |
michael@0 | 2903 | function d3_geo_cartesianScale(vector, k) { |
michael@0 | 2904 | return [ vector[0] * k, vector[1] * k, vector[2] * k ]; |
michael@0 | 2905 | } |
michael@0 | 2906 | function d3_geo_cartesianNormalize(d) { |
michael@0 | 2907 | var l = Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); |
michael@0 | 2908 | d[0] /= l; |
michael@0 | 2909 | d[1] /= l; |
michael@0 | 2910 | d[2] /= l; |
michael@0 | 2911 | } |
michael@0 | 2912 | function d3_geo_spherical(cartesian) { |
michael@0 | 2913 | return [ Math.atan2(cartesian[1], cartesian[0]), d3_asin(cartesian[2]) ]; |
michael@0 | 2914 | } |
michael@0 | 2915 | function d3_geo_sphericalEqual(a, b) { |
michael@0 | 2916 | return abs(a[0] - b[0]) < ε && abs(a[1] - b[1]) < ε; |
michael@0 | 2917 | } |
michael@0 | 2918 | d3.geo.bounds = function() { |
michael@0 | 2919 | var λ0, φ0, λ1, φ1, λ_, λ__, φ__, p0, dλSum, ranges, range; |
michael@0 | 2920 | var bound = { |
michael@0 | 2921 | point: point, |
michael@0 | 2922 | lineStart: lineStart, |
michael@0 | 2923 | lineEnd: lineEnd, |
michael@0 | 2924 | polygonStart: function() { |
michael@0 | 2925 | bound.point = ringPoint; |
michael@0 | 2926 | bound.lineStart = ringStart; |
michael@0 | 2927 | bound.lineEnd = ringEnd; |
michael@0 | 2928 | dλSum = 0; |
michael@0 | 2929 | d3_geo_area.polygonStart(); |
michael@0 | 2930 | }, |
michael@0 | 2931 | polygonEnd: function() { |
michael@0 | 2932 | d3_geo_area.polygonEnd(); |
michael@0 | 2933 | bound.point = point; |
michael@0 | 2934 | bound.lineStart = lineStart; |
michael@0 | 2935 | bound.lineEnd = lineEnd; |
michael@0 | 2936 | if (d3_geo_areaRingSum < 0) λ0 = -(λ1 = 180), φ0 = -(φ1 = 90); else if (dλSum > ε) φ1 = 90; else if (dλSum < -ε) φ0 = -90; |
michael@0 | 2937 | range[0] = λ0, range[1] = λ1; |
michael@0 | 2938 | } |
michael@0 | 2939 | }; |
michael@0 | 2940 | function point(λ, φ) { |
michael@0 | 2941 | ranges.push(range = [ λ0 = λ, λ1 = λ ]); |
michael@0 | 2942 | if (φ < φ0) φ0 = φ; |
michael@0 | 2943 | if (φ > φ1) φ1 = φ; |
michael@0 | 2944 | } |
michael@0 | 2945 | function linePoint(λ, φ) { |
michael@0 | 2946 | var p = d3_geo_cartesian([ λ * d3_radians, φ * d3_radians ]); |
michael@0 | 2947 | if (p0) { |
michael@0 | 2948 | var normal = d3_geo_cartesianCross(p0, p), equatorial = [ normal[1], -normal[0], 0 ], inflection = d3_geo_cartesianCross(equatorial, normal); |
michael@0 | 2949 | d3_geo_cartesianNormalize(inflection); |
michael@0 | 2950 | inflection = d3_geo_spherical(inflection); |
michael@0 | 2951 | var dλ = λ - λ_, s = dλ > 0 ? 1 : -1, λi = inflection[0] * d3_degrees * s, antimeridian = abs(dλ) > 180; |
michael@0 | 2952 | if (antimeridian ^ (s * λ_ < λi && λi < s * λ)) { |
michael@0 | 2953 | var φi = inflection[1] * d3_degrees; |
michael@0 | 2954 | if (φi > φ1) φ1 = φi; |
michael@0 | 2955 | } else if (λi = (λi + 360) % 360 - 180, antimeridian ^ (s * λ_ < λi && λi < s * λ)) { |
michael@0 | 2956 | var φi = -inflection[1] * d3_degrees; |
michael@0 | 2957 | if (φi < φ0) φ0 = φi; |
michael@0 | 2958 | } else { |
michael@0 | 2959 | if (φ < φ0) φ0 = φ; |
michael@0 | 2960 | if (φ > φ1) φ1 = φ; |
michael@0 | 2961 | } |
michael@0 | 2962 | if (antimeridian) { |
michael@0 | 2963 | if (λ < λ_) { |
michael@0 | 2964 | if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; |
michael@0 | 2965 | } else { |
michael@0 | 2966 | if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; |
michael@0 | 2967 | } |
michael@0 | 2968 | } else { |
michael@0 | 2969 | if (λ1 >= λ0) { |
michael@0 | 2970 | if (λ < λ0) λ0 = λ; |
michael@0 | 2971 | if (λ > λ1) λ1 = λ; |
michael@0 | 2972 | } else { |
michael@0 | 2973 | if (λ > λ_) { |
michael@0 | 2974 | if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; |
michael@0 | 2975 | } else { |
michael@0 | 2976 | if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; |
michael@0 | 2977 | } |
michael@0 | 2978 | } |
michael@0 | 2979 | } |
michael@0 | 2980 | } else { |
michael@0 | 2981 | point(λ, φ); |
michael@0 | 2982 | } |
michael@0 | 2983 | p0 = p, λ_ = λ; |
michael@0 | 2984 | } |
michael@0 | 2985 | function lineStart() { |
michael@0 | 2986 | bound.point = linePoint; |
michael@0 | 2987 | } |
michael@0 | 2988 | function lineEnd() { |
michael@0 | 2989 | range[0] = λ0, range[1] = λ1; |
michael@0 | 2990 | bound.point = point; |
michael@0 | 2991 | p0 = null; |
michael@0 | 2992 | } |
michael@0 | 2993 | function ringPoint(λ, φ) { |
michael@0 | 2994 | if (p0) { |
michael@0 | 2995 | var dλ = λ - λ_; |
michael@0 | 2996 | dλSum += abs(dλ) > 180 ? dλ + (dλ > 0 ? 360 : -360) : dλ; |
michael@0 | 2997 | } else λ__ = λ, φ__ = φ; |
michael@0 | 2998 | d3_geo_area.point(λ, φ); |
michael@0 | 2999 | linePoint(λ, φ); |
michael@0 | 3000 | } |
michael@0 | 3001 | function ringStart() { |
michael@0 | 3002 | d3_geo_area.lineStart(); |
michael@0 | 3003 | } |
michael@0 | 3004 | function ringEnd() { |
michael@0 | 3005 | ringPoint(λ__, φ__); |
michael@0 | 3006 | d3_geo_area.lineEnd(); |
michael@0 | 3007 | if (abs(dλSum) > ε) λ0 = -(λ1 = 180); |
michael@0 | 3008 | range[0] = λ0, range[1] = λ1; |
michael@0 | 3009 | p0 = null; |
michael@0 | 3010 | } |
michael@0 | 3011 | function angle(λ0, λ1) { |
michael@0 | 3012 | return (λ1 -= λ0) < 0 ? λ1 + 360 : λ1; |
michael@0 | 3013 | } |
michael@0 | 3014 | function compareRanges(a, b) { |
michael@0 | 3015 | return a[0] - b[0]; |
michael@0 | 3016 | } |
michael@0 | 3017 | function withinRange(x, range) { |
michael@0 | 3018 | return range[0] <= range[1] ? range[0] <= x && x <= range[1] : x < range[0] || range[1] < x; |
michael@0 | 3019 | } |
michael@0 | 3020 | return function(feature) { |
michael@0 | 3021 | φ1 = λ1 = -(λ0 = φ0 = Infinity); |
michael@0 | 3022 | ranges = []; |
michael@0 | 3023 | d3.geo.stream(feature, bound); |
michael@0 | 3024 | var n = ranges.length; |
michael@0 | 3025 | if (n) { |
michael@0 | 3026 | ranges.sort(compareRanges); |
michael@0 | 3027 | for (var i = 1, a = ranges[0], b, merged = [ a ]; i < n; ++i) { |
michael@0 | 3028 | b = ranges[i]; |
michael@0 | 3029 | if (withinRange(b[0], a) || withinRange(b[1], a)) { |
michael@0 | 3030 | if (angle(a[0], b[1]) > angle(a[0], a[1])) a[1] = b[1]; |
michael@0 | 3031 | if (angle(b[0], a[1]) > angle(a[0], a[1])) a[0] = b[0]; |
michael@0 | 3032 | } else { |
michael@0 | 3033 | merged.push(a = b); |
michael@0 | 3034 | } |
michael@0 | 3035 | } |
michael@0 | 3036 | var best = -Infinity, dλ; |
michael@0 | 3037 | for (var n = merged.length - 1, i = 0, a = merged[n], b; i <= n; a = b, ++i) { |
michael@0 | 3038 | b = merged[i]; |
michael@0 | 3039 | if ((dλ = angle(a[1], b[0])) > best) best = dλ, λ0 = b[0], λ1 = a[1]; |
michael@0 | 3040 | } |
michael@0 | 3041 | } |
michael@0 | 3042 | ranges = range = null; |
michael@0 | 3043 | return λ0 === Infinity || φ0 === Infinity ? [ [ NaN, NaN ], [ NaN, NaN ] ] : [ [ λ0, φ0 ], [ λ1, φ1 ] ]; |
michael@0 | 3044 | }; |
michael@0 | 3045 | }(); |
michael@0 | 3046 | d3.geo.centroid = function(object) { |
michael@0 | 3047 | d3_geo_centroidW0 = d3_geo_centroidW1 = d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; |
michael@0 | 3048 | d3.geo.stream(object, d3_geo_centroid); |
michael@0 | 3049 | var x = d3_geo_centroidX2, y = d3_geo_centroidY2, z = d3_geo_centroidZ2, m = x * x + y * y + z * z; |
michael@0 | 3050 | if (m < ε2) { |
michael@0 | 3051 | x = d3_geo_centroidX1, y = d3_geo_centroidY1, z = d3_geo_centroidZ1; |
michael@0 | 3052 | if (d3_geo_centroidW1 < ε) x = d3_geo_centroidX0, y = d3_geo_centroidY0, z = d3_geo_centroidZ0; |
michael@0 | 3053 | m = x * x + y * y + z * z; |
michael@0 | 3054 | if (m < ε2) return [ NaN, NaN ]; |
michael@0 | 3055 | } |
michael@0 | 3056 | return [ Math.atan2(y, x) * d3_degrees, d3_asin(z / Math.sqrt(m)) * d3_degrees ]; |
michael@0 | 3057 | }; |
michael@0 | 3058 | var d3_geo_centroidW0, d3_geo_centroidW1, d3_geo_centroidX0, d3_geo_centroidY0, d3_geo_centroidZ0, d3_geo_centroidX1, d3_geo_centroidY1, d3_geo_centroidZ1, d3_geo_centroidX2, d3_geo_centroidY2, d3_geo_centroidZ2; |
michael@0 | 3059 | var d3_geo_centroid = { |
michael@0 | 3060 | sphere: d3_noop, |
michael@0 | 3061 | point: d3_geo_centroidPoint, |
michael@0 | 3062 | lineStart: d3_geo_centroidLineStart, |
michael@0 | 3063 | lineEnd: d3_geo_centroidLineEnd, |
michael@0 | 3064 | polygonStart: function() { |
michael@0 | 3065 | d3_geo_centroid.lineStart = d3_geo_centroidRingStart; |
michael@0 | 3066 | }, |
michael@0 | 3067 | polygonEnd: function() { |
michael@0 | 3068 | d3_geo_centroid.lineStart = d3_geo_centroidLineStart; |
michael@0 | 3069 | } |
michael@0 | 3070 | }; |
michael@0 | 3071 | function d3_geo_centroidPoint(λ, φ) { |
michael@0 | 3072 | λ *= d3_radians; |
michael@0 | 3073 | var cosφ = Math.cos(φ *= d3_radians); |
michael@0 | 3074 | d3_geo_centroidPointXYZ(cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ)); |
michael@0 | 3075 | } |
michael@0 | 3076 | function d3_geo_centroidPointXYZ(x, y, z) { |
michael@0 | 3077 | ++d3_geo_centroidW0; |
michael@0 | 3078 | d3_geo_centroidX0 += (x - d3_geo_centroidX0) / d3_geo_centroidW0; |
michael@0 | 3079 | d3_geo_centroidY0 += (y - d3_geo_centroidY0) / d3_geo_centroidW0; |
michael@0 | 3080 | d3_geo_centroidZ0 += (z - d3_geo_centroidZ0) / d3_geo_centroidW0; |
michael@0 | 3081 | } |
michael@0 | 3082 | function d3_geo_centroidLineStart() { |
michael@0 | 3083 | var x0, y0, z0; |
michael@0 | 3084 | d3_geo_centroid.point = function(λ, φ) { |
michael@0 | 3085 | λ *= d3_radians; |
michael@0 | 3086 | var cosφ = Math.cos(φ *= d3_radians); |
michael@0 | 3087 | x0 = cosφ * Math.cos(λ); |
michael@0 | 3088 | y0 = cosφ * Math.sin(λ); |
michael@0 | 3089 | z0 = Math.sin(φ); |
michael@0 | 3090 | d3_geo_centroid.point = nextPoint; |
michael@0 | 3091 | d3_geo_centroidPointXYZ(x0, y0, z0); |
michael@0 | 3092 | }; |
michael@0 | 3093 | function nextPoint(λ, φ) { |
michael@0 | 3094 | λ *= d3_radians; |
michael@0 | 3095 | var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), w = Math.atan2(Math.sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z); |
michael@0 | 3096 | d3_geo_centroidW1 += w; |
michael@0 | 3097 | d3_geo_centroidX1 += w * (x0 + (x0 = x)); |
michael@0 | 3098 | d3_geo_centroidY1 += w * (y0 + (y0 = y)); |
michael@0 | 3099 | d3_geo_centroidZ1 += w * (z0 + (z0 = z)); |
michael@0 | 3100 | d3_geo_centroidPointXYZ(x0, y0, z0); |
michael@0 | 3101 | } |
michael@0 | 3102 | } |
michael@0 | 3103 | function d3_geo_centroidLineEnd() { |
michael@0 | 3104 | d3_geo_centroid.point = d3_geo_centroidPoint; |
michael@0 | 3105 | } |
michael@0 | 3106 | function d3_geo_centroidRingStart() { |
michael@0 | 3107 | var λ00, φ00, x0, y0, z0; |
michael@0 | 3108 | d3_geo_centroid.point = function(λ, φ) { |
michael@0 | 3109 | λ00 = λ, φ00 = φ; |
michael@0 | 3110 | d3_geo_centroid.point = nextPoint; |
michael@0 | 3111 | λ *= d3_radians; |
michael@0 | 3112 | var cosφ = Math.cos(φ *= d3_radians); |
michael@0 | 3113 | x0 = cosφ * Math.cos(λ); |
michael@0 | 3114 | y0 = cosφ * Math.sin(λ); |
michael@0 | 3115 | z0 = Math.sin(φ); |
michael@0 | 3116 | d3_geo_centroidPointXYZ(x0, y0, z0); |
michael@0 | 3117 | }; |
michael@0 | 3118 | d3_geo_centroid.lineEnd = function() { |
michael@0 | 3119 | nextPoint(λ00, φ00); |
michael@0 | 3120 | d3_geo_centroid.lineEnd = d3_geo_centroidLineEnd; |
michael@0 | 3121 | d3_geo_centroid.point = d3_geo_centroidPoint; |
michael@0 | 3122 | }; |
michael@0 | 3123 | function nextPoint(λ, φ) { |
michael@0 | 3124 | λ *= d3_radians; |
michael@0 | 3125 | var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), cx = y0 * z - z0 * y, cy = z0 * x - x0 * z, cz = x0 * y - y0 * x, m = Math.sqrt(cx * cx + cy * cy + cz * cz), u = x0 * x + y0 * y + z0 * z, v = m && -d3_acos(u) / m, w = Math.atan2(m, u); |
michael@0 | 3126 | d3_geo_centroidX2 += v * cx; |
michael@0 | 3127 | d3_geo_centroidY2 += v * cy; |
michael@0 | 3128 | d3_geo_centroidZ2 += v * cz; |
michael@0 | 3129 | d3_geo_centroidW1 += w; |
michael@0 | 3130 | d3_geo_centroidX1 += w * (x0 + (x0 = x)); |
michael@0 | 3131 | d3_geo_centroidY1 += w * (y0 + (y0 = y)); |
michael@0 | 3132 | d3_geo_centroidZ1 += w * (z0 + (z0 = z)); |
michael@0 | 3133 | d3_geo_centroidPointXYZ(x0, y0, z0); |
michael@0 | 3134 | } |
michael@0 | 3135 | } |
michael@0 | 3136 | function d3_true() { |
michael@0 | 3137 | return true; |
michael@0 | 3138 | } |
michael@0 | 3139 | function d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener) { |
michael@0 | 3140 | var subject = [], clip = []; |
michael@0 | 3141 | segments.forEach(function(segment) { |
michael@0 | 3142 | if ((n = segment.length - 1) <= 0) return; |
michael@0 | 3143 | var n, p0 = segment[0], p1 = segment[n]; |
michael@0 | 3144 | if (d3_geo_sphericalEqual(p0, p1)) { |
michael@0 | 3145 | listener.lineStart(); |
michael@0 | 3146 | for (var i = 0; i < n; ++i) listener.point((p0 = segment[i])[0], p0[1]); |
michael@0 | 3147 | listener.lineEnd(); |
michael@0 | 3148 | return; |
michael@0 | 3149 | } |
michael@0 | 3150 | var a = new d3_geo_clipPolygonIntersection(p0, segment, null, true), b = new d3_geo_clipPolygonIntersection(p0, null, a, false); |
michael@0 | 3151 | a.o = b; |
michael@0 | 3152 | subject.push(a); |
michael@0 | 3153 | clip.push(b); |
michael@0 | 3154 | a = new d3_geo_clipPolygonIntersection(p1, segment, null, false); |
michael@0 | 3155 | b = new d3_geo_clipPolygonIntersection(p1, null, a, true); |
michael@0 | 3156 | a.o = b; |
michael@0 | 3157 | subject.push(a); |
michael@0 | 3158 | clip.push(b); |
michael@0 | 3159 | }); |
michael@0 | 3160 | clip.sort(compare); |
michael@0 | 3161 | d3_geo_clipPolygonLinkCircular(subject); |
michael@0 | 3162 | d3_geo_clipPolygonLinkCircular(clip); |
michael@0 | 3163 | if (!subject.length) return; |
michael@0 | 3164 | for (var i = 0, entry = clipStartInside, n = clip.length; i < n; ++i) { |
michael@0 | 3165 | clip[i].e = entry = !entry; |
michael@0 | 3166 | } |
michael@0 | 3167 | var start = subject[0], points, point; |
michael@0 | 3168 | while (1) { |
michael@0 | 3169 | var current = start, isSubject = true; |
michael@0 | 3170 | while (current.v) if ((current = current.n) === start) return; |
michael@0 | 3171 | points = current.z; |
michael@0 | 3172 | listener.lineStart(); |
michael@0 | 3173 | do { |
michael@0 | 3174 | current.v = current.o.v = true; |
michael@0 | 3175 | if (current.e) { |
michael@0 | 3176 | if (isSubject) { |
michael@0 | 3177 | for (var i = 0, n = points.length; i < n; ++i) listener.point((point = points[i])[0], point[1]); |
michael@0 | 3178 | } else { |
michael@0 | 3179 | interpolate(current.x, current.n.x, 1, listener); |
michael@0 | 3180 | } |
michael@0 | 3181 | current = current.n; |
michael@0 | 3182 | } else { |
michael@0 | 3183 | if (isSubject) { |
michael@0 | 3184 | points = current.p.z; |
michael@0 | 3185 | for (var i = points.length - 1; i >= 0; --i) listener.point((point = points[i])[0], point[1]); |
michael@0 | 3186 | } else { |
michael@0 | 3187 | interpolate(current.x, current.p.x, -1, listener); |
michael@0 | 3188 | } |
michael@0 | 3189 | current = current.p; |
michael@0 | 3190 | } |
michael@0 | 3191 | current = current.o; |
michael@0 | 3192 | points = current.z; |
michael@0 | 3193 | isSubject = !isSubject; |
michael@0 | 3194 | } while (!current.v); |
michael@0 | 3195 | listener.lineEnd(); |
michael@0 | 3196 | } |
michael@0 | 3197 | } |
michael@0 | 3198 | function d3_geo_clipPolygonLinkCircular(array) { |
michael@0 | 3199 | if (!(n = array.length)) return; |
michael@0 | 3200 | var n, i = 0, a = array[0], b; |
michael@0 | 3201 | while (++i < n) { |
michael@0 | 3202 | a.n = b = array[i]; |
michael@0 | 3203 | b.p = a; |
michael@0 | 3204 | a = b; |
michael@0 | 3205 | } |
michael@0 | 3206 | a.n = b = array[0]; |
michael@0 | 3207 | b.p = a; |
michael@0 | 3208 | } |
michael@0 | 3209 | function d3_geo_clipPolygonIntersection(point, points, other, entry) { |
michael@0 | 3210 | this.x = point; |
michael@0 | 3211 | this.z = points; |
michael@0 | 3212 | this.o = other; |
michael@0 | 3213 | this.e = entry; |
michael@0 | 3214 | this.v = false; |
michael@0 | 3215 | this.n = this.p = null; |
michael@0 | 3216 | } |
michael@0 | 3217 | function d3_geo_clip(pointVisible, clipLine, interpolate, clipStart) { |
michael@0 | 3218 | return function(rotate, listener) { |
michael@0 | 3219 | var line = clipLine(listener), rotatedClipStart = rotate.invert(clipStart[0], clipStart[1]); |
michael@0 | 3220 | var clip = { |
michael@0 | 3221 | point: point, |
michael@0 | 3222 | lineStart: lineStart, |
michael@0 | 3223 | lineEnd: lineEnd, |
michael@0 | 3224 | polygonStart: function() { |
michael@0 | 3225 | clip.point = pointRing; |
michael@0 | 3226 | clip.lineStart = ringStart; |
michael@0 | 3227 | clip.lineEnd = ringEnd; |
michael@0 | 3228 | segments = []; |
michael@0 | 3229 | polygon = []; |
michael@0 | 3230 | listener.polygonStart(); |
michael@0 | 3231 | }, |
michael@0 | 3232 | polygonEnd: function() { |
michael@0 | 3233 | clip.point = point; |
michael@0 | 3234 | clip.lineStart = lineStart; |
michael@0 | 3235 | clip.lineEnd = lineEnd; |
michael@0 | 3236 | segments = d3.merge(segments); |
michael@0 | 3237 | var clipStartInside = d3_geo_pointInPolygon(rotatedClipStart, polygon); |
michael@0 | 3238 | if (segments.length) { |
michael@0 | 3239 | d3_geo_clipPolygon(segments, d3_geo_clipSort, clipStartInside, interpolate, listener); |
michael@0 | 3240 | } else if (clipStartInside) { |
michael@0 | 3241 | listener.lineStart(); |
michael@0 | 3242 | interpolate(null, null, 1, listener); |
michael@0 | 3243 | listener.lineEnd(); |
michael@0 | 3244 | } |
michael@0 | 3245 | listener.polygonEnd(); |
michael@0 | 3246 | segments = polygon = null; |
michael@0 | 3247 | }, |
michael@0 | 3248 | sphere: function() { |
michael@0 | 3249 | listener.polygonStart(); |
michael@0 | 3250 | listener.lineStart(); |
michael@0 | 3251 | interpolate(null, null, 1, listener); |
michael@0 | 3252 | listener.lineEnd(); |
michael@0 | 3253 | listener.polygonEnd(); |
michael@0 | 3254 | } |
michael@0 | 3255 | }; |
michael@0 | 3256 | function point(λ, φ) { |
michael@0 | 3257 | var point = rotate(λ, φ); |
michael@0 | 3258 | if (pointVisible(λ = point[0], φ = point[1])) listener.point(λ, φ); |
michael@0 | 3259 | } |
michael@0 | 3260 | function pointLine(λ, φ) { |
michael@0 | 3261 | var point = rotate(λ, φ); |
michael@0 | 3262 | line.point(point[0], point[1]); |
michael@0 | 3263 | } |
michael@0 | 3264 | function lineStart() { |
michael@0 | 3265 | clip.point = pointLine; |
michael@0 | 3266 | line.lineStart(); |
michael@0 | 3267 | } |
michael@0 | 3268 | function lineEnd() { |
michael@0 | 3269 | clip.point = point; |
michael@0 | 3270 | line.lineEnd(); |
michael@0 | 3271 | } |
michael@0 | 3272 | var segments; |
michael@0 | 3273 | var buffer = d3_geo_clipBufferListener(), ringListener = clipLine(buffer), polygon, ring; |
michael@0 | 3274 | function pointRing(λ, φ) { |
michael@0 | 3275 | ring.push([ λ, φ ]); |
michael@0 | 3276 | var point = rotate(λ, φ); |
michael@0 | 3277 | ringListener.point(point[0], point[1]); |
michael@0 | 3278 | } |
michael@0 | 3279 | function ringStart() { |
michael@0 | 3280 | ringListener.lineStart(); |
michael@0 | 3281 | ring = []; |
michael@0 | 3282 | } |
michael@0 | 3283 | function ringEnd() { |
michael@0 | 3284 | pointRing(ring[0][0], ring[0][1]); |
michael@0 | 3285 | ringListener.lineEnd(); |
michael@0 | 3286 | var clean = ringListener.clean(), ringSegments = buffer.buffer(), segment, n = ringSegments.length; |
michael@0 | 3287 | ring.pop(); |
michael@0 | 3288 | polygon.push(ring); |
michael@0 | 3289 | ring = null; |
michael@0 | 3290 | if (!n) return; |
michael@0 | 3291 | if (clean & 1) { |
michael@0 | 3292 | segment = ringSegments[0]; |
michael@0 | 3293 | var n = segment.length - 1, i = -1, point; |
michael@0 | 3294 | listener.lineStart(); |
michael@0 | 3295 | while (++i < n) listener.point((point = segment[i])[0], point[1]); |
michael@0 | 3296 | listener.lineEnd(); |
michael@0 | 3297 | return; |
michael@0 | 3298 | } |
michael@0 | 3299 | if (n > 1 && clean & 2) ringSegments.push(ringSegments.pop().concat(ringSegments.shift())); |
michael@0 | 3300 | segments.push(ringSegments.filter(d3_geo_clipSegmentLength1)); |
michael@0 | 3301 | } |
michael@0 | 3302 | return clip; |
michael@0 | 3303 | }; |
michael@0 | 3304 | } |
michael@0 | 3305 | function d3_geo_clipSegmentLength1(segment) { |
michael@0 | 3306 | return segment.length > 1; |
michael@0 | 3307 | } |
michael@0 | 3308 | function d3_geo_clipBufferListener() { |
michael@0 | 3309 | var lines = [], line; |
michael@0 | 3310 | return { |
michael@0 | 3311 | lineStart: function() { |
michael@0 | 3312 | lines.push(line = []); |
michael@0 | 3313 | }, |
michael@0 | 3314 | point: function(λ, φ) { |
michael@0 | 3315 | line.push([ λ, φ ]); |
michael@0 | 3316 | }, |
michael@0 | 3317 | lineEnd: d3_noop, |
michael@0 | 3318 | buffer: function() { |
michael@0 | 3319 | var buffer = lines; |
michael@0 | 3320 | lines = []; |
michael@0 | 3321 | line = null; |
michael@0 | 3322 | return buffer; |
michael@0 | 3323 | }, |
michael@0 | 3324 | rejoin: function() { |
michael@0 | 3325 | if (lines.length > 1) lines.push(lines.pop().concat(lines.shift())); |
michael@0 | 3326 | } |
michael@0 | 3327 | }; |
michael@0 | 3328 | } |
michael@0 | 3329 | function d3_geo_clipSort(a, b) { |
michael@0 | 3330 | return ((a = a.x)[0] < 0 ? a[1] - halfπ - ε : halfπ - a[1]) - ((b = b.x)[0] < 0 ? b[1] - halfπ - ε : halfπ - b[1]); |
michael@0 | 3331 | } |
michael@0 | 3332 | function d3_geo_pointInPolygon(point, polygon) { |
michael@0 | 3333 | var meridian = point[0], parallel = point[1], meridianNormal = [ Math.sin(meridian), -Math.cos(meridian), 0 ], polarAngle = 0, winding = 0; |
michael@0 | 3334 | d3_geo_areaRingSum.reset(); |
michael@0 | 3335 | for (var i = 0, n = polygon.length; i < n; ++i) { |
michael@0 | 3336 | var ring = polygon[i], m = ring.length; |
michael@0 | 3337 | if (!m) continue; |
michael@0 | 3338 | var point0 = ring[0], λ0 = point0[0], φ0 = point0[1] / 2 + π / 4, sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), j = 1; |
michael@0 | 3339 | while (true) { |
michael@0 | 3340 | if (j === m) j = 0; |
michael@0 | 3341 | point = ring[j]; |
michael@0 | 3342 | var λ = point[0], φ = point[1] / 2 + π / 4, sinφ = Math.sin(φ), cosφ = Math.cos(φ), dλ = λ - λ0, antimeridian = abs(dλ) > π, k = sinφ0 * sinφ; |
michael@0 | 3343 | d3_geo_areaRingSum.add(Math.atan2(k * Math.sin(dλ), cosφ0 * cosφ + k * Math.cos(dλ))); |
michael@0 | 3344 | polarAngle += antimeridian ? dλ + (dλ >= 0 ? τ : -τ) : dλ; |
michael@0 | 3345 | if (antimeridian ^ λ0 >= meridian ^ λ >= meridian) { |
michael@0 | 3346 | var arc = d3_geo_cartesianCross(d3_geo_cartesian(point0), d3_geo_cartesian(point)); |
michael@0 | 3347 | d3_geo_cartesianNormalize(arc); |
michael@0 | 3348 | var intersection = d3_geo_cartesianCross(meridianNormal, arc); |
michael@0 | 3349 | d3_geo_cartesianNormalize(intersection); |
michael@0 | 3350 | var φarc = (antimeridian ^ dλ >= 0 ? -1 : 1) * d3_asin(intersection[2]); |
michael@0 | 3351 | if (parallel > φarc || parallel === φarc && (arc[0] || arc[1])) { |
michael@0 | 3352 | winding += antimeridian ^ dλ >= 0 ? 1 : -1; |
michael@0 | 3353 | } |
michael@0 | 3354 | } |
michael@0 | 3355 | if (!j++) break; |
michael@0 | 3356 | λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ, point0 = point; |
michael@0 | 3357 | } |
michael@0 | 3358 | } |
michael@0 | 3359 | return (polarAngle < -ε || polarAngle < ε && d3_geo_areaRingSum < 0) ^ winding & 1; |
michael@0 | 3360 | } |
michael@0 | 3361 | var d3_geo_clipAntimeridian = d3_geo_clip(d3_true, d3_geo_clipAntimeridianLine, d3_geo_clipAntimeridianInterpolate, [ -π, -π / 2 ]); |
michael@0 | 3362 | function d3_geo_clipAntimeridianLine(listener) { |
michael@0 | 3363 | var λ0 = NaN, φ0 = NaN, sλ0 = NaN, clean; |
michael@0 | 3364 | return { |
michael@0 | 3365 | lineStart: function() { |
michael@0 | 3366 | listener.lineStart(); |
michael@0 | 3367 | clean = 1; |
michael@0 | 3368 | }, |
michael@0 | 3369 | point: function(λ1, φ1) { |
michael@0 | 3370 | var sλ1 = λ1 > 0 ? π : -π, dλ = abs(λ1 - λ0); |
michael@0 | 3371 | if (abs(dλ - π) < ε) { |
michael@0 | 3372 | listener.point(λ0, φ0 = (φ0 + φ1) / 2 > 0 ? halfπ : -halfπ); |
michael@0 | 3373 | listener.point(sλ0, φ0); |
michael@0 | 3374 | listener.lineEnd(); |
michael@0 | 3375 | listener.lineStart(); |
michael@0 | 3376 | listener.point(sλ1, φ0); |
michael@0 | 3377 | listener.point(λ1, φ0); |
michael@0 | 3378 | clean = 0; |
michael@0 | 3379 | } else if (sλ0 !== sλ1 && dλ >= π) { |
michael@0 | 3380 | if (abs(λ0 - sλ0) < ε) λ0 -= sλ0 * ε; |
michael@0 | 3381 | if (abs(λ1 - sλ1) < ε) λ1 -= sλ1 * ε; |
michael@0 | 3382 | φ0 = d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1); |
michael@0 | 3383 | listener.point(sλ0, φ0); |
michael@0 | 3384 | listener.lineEnd(); |
michael@0 | 3385 | listener.lineStart(); |
michael@0 | 3386 | listener.point(sλ1, φ0); |
michael@0 | 3387 | clean = 0; |
michael@0 | 3388 | } |
michael@0 | 3389 | listener.point(λ0 = λ1, φ0 = φ1); |
michael@0 | 3390 | sλ0 = sλ1; |
michael@0 | 3391 | }, |
michael@0 | 3392 | lineEnd: function() { |
michael@0 | 3393 | listener.lineEnd(); |
michael@0 | 3394 | λ0 = φ0 = NaN; |
michael@0 | 3395 | }, |
michael@0 | 3396 | clean: function() { |
michael@0 | 3397 | return 2 - clean; |
michael@0 | 3398 | } |
michael@0 | 3399 | }; |
michael@0 | 3400 | } |
michael@0 | 3401 | function d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1) { |
michael@0 | 3402 | var cosφ0, cosφ1, sinλ0_λ1 = Math.sin(λ0 - λ1); |
michael@0 | 3403 | return abs(sinλ0_λ1) > ε ? Math.atan((Math.sin(φ0) * (cosφ1 = Math.cos(φ1)) * Math.sin(λ1) - Math.sin(φ1) * (cosφ0 = Math.cos(φ0)) * Math.sin(λ0)) / (cosφ0 * cosφ1 * sinλ0_λ1)) : (φ0 + φ1) / 2; |
michael@0 | 3404 | } |
michael@0 | 3405 | function d3_geo_clipAntimeridianInterpolate(from, to, direction, listener) { |
michael@0 | 3406 | var φ; |
michael@0 | 3407 | if (from == null) { |
michael@0 | 3408 | φ = direction * halfπ; |
michael@0 | 3409 | listener.point(-π, φ); |
michael@0 | 3410 | listener.point(0, φ); |
michael@0 | 3411 | listener.point(π, φ); |
michael@0 | 3412 | listener.point(π, 0); |
michael@0 | 3413 | listener.point(π, -φ); |
michael@0 | 3414 | listener.point(0, -φ); |
michael@0 | 3415 | listener.point(-π, -φ); |
michael@0 | 3416 | listener.point(-π, 0); |
michael@0 | 3417 | listener.point(-π, φ); |
michael@0 | 3418 | } else if (abs(from[0] - to[0]) > ε) { |
michael@0 | 3419 | var s = from[0] < to[0] ? π : -π; |
michael@0 | 3420 | φ = direction * s / 2; |
michael@0 | 3421 | listener.point(-s, φ); |
michael@0 | 3422 | listener.point(0, φ); |
michael@0 | 3423 | listener.point(s, φ); |
michael@0 | 3424 | } else { |
michael@0 | 3425 | listener.point(to[0], to[1]); |
michael@0 | 3426 | } |
michael@0 | 3427 | } |
michael@0 | 3428 | function d3_geo_clipCircle(radius) { |
michael@0 | 3429 | var cr = Math.cos(radius), smallRadius = cr > 0, notHemisphere = abs(cr) > ε, interpolate = d3_geo_circleInterpolate(radius, 6 * d3_radians); |
michael@0 | 3430 | return d3_geo_clip(visible, clipLine, interpolate, smallRadius ? [ 0, -radius ] : [ -π, radius - π ]); |
michael@0 | 3431 | function visible(λ, φ) { |
michael@0 | 3432 | return Math.cos(λ) * Math.cos(φ) > cr; |
michael@0 | 3433 | } |
michael@0 | 3434 | function clipLine(listener) { |
michael@0 | 3435 | var point0, c0, v0, v00, clean; |
michael@0 | 3436 | return { |
michael@0 | 3437 | lineStart: function() { |
michael@0 | 3438 | v00 = v0 = false; |
michael@0 | 3439 | clean = 1; |
michael@0 | 3440 | }, |
michael@0 | 3441 | point: function(λ, φ) { |
michael@0 | 3442 | var point1 = [ λ, φ ], point2, v = visible(λ, φ), c = smallRadius ? v ? 0 : code(λ, φ) : v ? code(λ + (λ < 0 ? π : -π), φ) : 0; |
michael@0 | 3443 | if (!point0 && (v00 = v0 = v)) listener.lineStart(); |
michael@0 | 3444 | if (v !== v0) { |
michael@0 | 3445 | point2 = intersect(point0, point1); |
michael@0 | 3446 | if (d3_geo_sphericalEqual(point0, point2) || d3_geo_sphericalEqual(point1, point2)) { |
michael@0 | 3447 | point1[0] += ε; |
michael@0 | 3448 | point1[1] += ε; |
michael@0 | 3449 | v = visible(point1[0], point1[1]); |
michael@0 | 3450 | } |
michael@0 | 3451 | } |
michael@0 | 3452 | if (v !== v0) { |
michael@0 | 3453 | clean = 0; |
michael@0 | 3454 | if (v) { |
michael@0 | 3455 | listener.lineStart(); |
michael@0 | 3456 | point2 = intersect(point1, point0); |
michael@0 | 3457 | listener.point(point2[0], point2[1]); |
michael@0 | 3458 | } else { |
michael@0 | 3459 | point2 = intersect(point0, point1); |
michael@0 | 3460 | listener.point(point2[0], point2[1]); |
michael@0 | 3461 | listener.lineEnd(); |
michael@0 | 3462 | } |
michael@0 | 3463 | point0 = point2; |
michael@0 | 3464 | } else if (notHemisphere && point0 && smallRadius ^ v) { |
michael@0 | 3465 | var t; |
michael@0 | 3466 | if (!(c & c0) && (t = intersect(point1, point0, true))) { |
michael@0 | 3467 | clean = 0; |
michael@0 | 3468 | if (smallRadius) { |
michael@0 | 3469 | listener.lineStart(); |
michael@0 | 3470 | listener.point(t[0][0], t[0][1]); |
michael@0 | 3471 | listener.point(t[1][0], t[1][1]); |
michael@0 | 3472 | listener.lineEnd(); |
michael@0 | 3473 | } else { |
michael@0 | 3474 | listener.point(t[1][0], t[1][1]); |
michael@0 | 3475 | listener.lineEnd(); |
michael@0 | 3476 | listener.lineStart(); |
michael@0 | 3477 | listener.point(t[0][0], t[0][1]); |
michael@0 | 3478 | } |
michael@0 | 3479 | } |
michael@0 | 3480 | } |
michael@0 | 3481 | if (v && (!point0 || !d3_geo_sphericalEqual(point0, point1))) { |
michael@0 | 3482 | listener.point(point1[0], point1[1]); |
michael@0 | 3483 | } |
michael@0 | 3484 | point0 = point1, v0 = v, c0 = c; |
michael@0 | 3485 | }, |
michael@0 | 3486 | lineEnd: function() { |
michael@0 | 3487 | if (v0) listener.lineEnd(); |
michael@0 | 3488 | point0 = null; |
michael@0 | 3489 | }, |
michael@0 | 3490 | clean: function() { |
michael@0 | 3491 | return clean | (v00 && v0) << 1; |
michael@0 | 3492 | } |
michael@0 | 3493 | }; |
michael@0 | 3494 | } |
michael@0 | 3495 | function intersect(a, b, two) { |
michael@0 | 3496 | var pa = d3_geo_cartesian(a), pb = d3_geo_cartesian(b); |
michael@0 | 3497 | var n1 = [ 1, 0, 0 ], n2 = d3_geo_cartesianCross(pa, pb), n2n2 = d3_geo_cartesianDot(n2, n2), n1n2 = n2[0], determinant = n2n2 - n1n2 * n1n2; |
michael@0 | 3498 | if (!determinant) return !two && a; |
michael@0 | 3499 | var c1 = cr * n2n2 / determinant, c2 = -cr * n1n2 / determinant, n1xn2 = d3_geo_cartesianCross(n1, n2), A = d3_geo_cartesianScale(n1, c1), B = d3_geo_cartesianScale(n2, c2); |
michael@0 | 3500 | d3_geo_cartesianAdd(A, B); |
michael@0 | 3501 | var u = n1xn2, w = d3_geo_cartesianDot(A, u), uu = d3_geo_cartesianDot(u, u), t2 = w * w - uu * (d3_geo_cartesianDot(A, A) - 1); |
michael@0 | 3502 | if (t2 < 0) return; |
michael@0 | 3503 | var t = Math.sqrt(t2), q = d3_geo_cartesianScale(u, (-w - t) / uu); |
michael@0 | 3504 | d3_geo_cartesianAdd(q, A); |
michael@0 | 3505 | q = d3_geo_spherical(q); |
michael@0 | 3506 | if (!two) return q; |
michael@0 | 3507 | var λ0 = a[0], λ1 = b[0], φ0 = a[1], φ1 = b[1], z; |
michael@0 | 3508 | if (λ1 < λ0) z = λ0, λ0 = λ1, λ1 = z; |
michael@0 | 3509 | var δλ = λ1 - λ0, polar = abs(δλ - π) < ε, meridian = polar || δλ < ε; |
michael@0 | 3510 | if (!polar && φ1 < φ0) z = φ0, φ0 = φ1, φ1 = z; |
michael@0 | 3511 | if (meridian ? polar ? φ0 + φ1 > 0 ^ q[1] < (abs(q[0] - λ0) < ε ? φ0 : φ1) : φ0 <= q[1] && q[1] <= φ1 : δλ > π ^ (λ0 <= q[0] && q[0] <= λ1)) { |
michael@0 | 3512 | var q1 = d3_geo_cartesianScale(u, (-w + t) / uu); |
michael@0 | 3513 | d3_geo_cartesianAdd(q1, A); |
michael@0 | 3514 | return [ q, d3_geo_spherical(q1) ]; |
michael@0 | 3515 | } |
michael@0 | 3516 | } |
michael@0 | 3517 | function code(λ, φ) { |
michael@0 | 3518 | var r = smallRadius ? radius : π - radius, code = 0; |
michael@0 | 3519 | if (λ < -r) code |= 1; else if (λ > r) code |= 2; |
michael@0 | 3520 | if (φ < -r) code |= 4; else if (φ > r) code |= 8; |
michael@0 | 3521 | return code; |
michael@0 | 3522 | } |
michael@0 | 3523 | } |
michael@0 | 3524 | function d3_geom_clipLine(x0, y0, x1, y1) { |
michael@0 | 3525 | return function(line) { |
michael@0 | 3526 | var a = line.a, b = line.b, ax = a.x, ay = a.y, bx = b.x, by = b.y, t0 = 0, t1 = 1, dx = bx - ax, dy = by - ay, r; |
michael@0 | 3527 | r = x0 - ax; |
michael@0 | 3528 | if (!dx && r > 0) return; |
michael@0 | 3529 | r /= dx; |
michael@0 | 3530 | if (dx < 0) { |
michael@0 | 3531 | if (r < t0) return; |
michael@0 | 3532 | if (r < t1) t1 = r; |
michael@0 | 3533 | } else if (dx > 0) { |
michael@0 | 3534 | if (r > t1) return; |
michael@0 | 3535 | if (r > t0) t0 = r; |
michael@0 | 3536 | } |
michael@0 | 3537 | r = x1 - ax; |
michael@0 | 3538 | if (!dx && r < 0) return; |
michael@0 | 3539 | r /= dx; |
michael@0 | 3540 | if (dx < 0) { |
michael@0 | 3541 | if (r > t1) return; |
michael@0 | 3542 | if (r > t0) t0 = r; |
michael@0 | 3543 | } else if (dx > 0) { |
michael@0 | 3544 | if (r < t0) return; |
michael@0 | 3545 | if (r < t1) t1 = r; |
michael@0 | 3546 | } |
michael@0 | 3547 | r = y0 - ay; |
michael@0 | 3548 | if (!dy && r > 0) return; |
michael@0 | 3549 | r /= dy; |
michael@0 | 3550 | if (dy < 0) { |
michael@0 | 3551 | if (r < t0) return; |
michael@0 | 3552 | if (r < t1) t1 = r; |
michael@0 | 3553 | } else if (dy > 0) { |
michael@0 | 3554 | if (r > t1) return; |
michael@0 | 3555 | if (r > t0) t0 = r; |
michael@0 | 3556 | } |
michael@0 | 3557 | r = y1 - ay; |
michael@0 | 3558 | if (!dy && r < 0) return; |
michael@0 | 3559 | r /= dy; |
michael@0 | 3560 | if (dy < 0) { |
michael@0 | 3561 | if (r > t1) return; |
michael@0 | 3562 | if (r > t0) t0 = r; |
michael@0 | 3563 | } else if (dy > 0) { |
michael@0 | 3564 | if (r < t0) return; |
michael@0 | 3565 | if (r < t1) t1 = r; |
michael@0 | 3566 | } |
michael@0 | 3567 | if (t0 > 0) line.a = { |
michael@0 | 3568 | x: ax + t0 * dx, |
michael@0 | 3569 | y: ay + t0 * dy |
michael@0 | 3570 | }; |
michael@0 | 3571 | if (t1 < 1) line.b = { |
michael@0 | 3572 | x: ax + t1 * dx, |
michael@0 | 3573 | y: ay + t1 * dy |
michael@0 | 3574 | }; |
michael@0 | 3575 | return line; |
michael@0 | 3576 | }; |
michael@0 | 3577 | } |
michael@0 | 3578 | var d3_geo_clipExtentMAX = 1e9; |
michael@0 | 3579 | d3.geo.clipExtent = function() { |
michael@0 | 3580 | var x0, y0, x1, y1, stream, clip, clipExtent = { |
michael@0 | 3581 | stream: function(output) { |
michael@0 | 3582 | if (stream) stream.valid = false; |
michael@0 | 3583 | stream = clip(output); |
michael@0 | 3584 | stream.valid = true; |
michael@0 | 3585 | return stream; |
michael@0 | 3586 | }, |
michael@0 | 3587 | extent: function(_) { |
michael@0 | 3588 | if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; |
michael@0 | 3589 | clip = d3_geo_clipExtent(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]); |
michael@0 | 3590 | if (stream) stream.valid = false, stream = null; |
michael@0 | 3591 | return clipExtent; |
michael@0 | 3592 | } |
michael@0 | 3593 | }; |
michael@0 | 3594 | return clipExtent.extent([ [ 0, 0 ], [ 960, 500 ] ]); |
michael@0 | 3595 | }; |
michael@0 | 3596 | function d3_geo_clipExtent(x0, y0, x1, y1) { |
michael@0 | 3597 | return function(listener) { |
michael@0 | 3598 | var listener_ = listener, bufferListener = d3_geo_clipBufferListener(), clipLine = d3_geom_clipLine(x0, y0, x1, y1), segments, polygon, ring; |
michael@0 | 3599 | var clip = { |
michael@0 | 3600 | point: point, |
michael@0 | 3601 | lineStart: lineStart, |
michael@0 | 3602 | lineEnd: lineEnd, |
michael@0 | 3603 | polygonStart: function() { |
michael@0 | 3604 | listener = bufferListener; |
michael@0 | 3605 | segments = []; |
michael@0 | 3606 | polygon = []; |
michael@0 | 3607 | clean = true; |
michael@0 | 3608 | }, |
michael@0 | 3609 | polygonEnd: function() { |
michael@0 | 3610 | listener = listener_; |
michael@0 | 3611 | segments = d3.merge(segments); |
michael@0 | 3612 | var clipStartInside = insidePolygon([ x0, y1 ]), inside = clean && clipStartInside, visible = segments.length; |
michael@0 | 3613 | if (inside || visible) { |
michael@0 | 3614 | listener.polygonStart(); |
michael@0 | 3615 | if (inside) { |
michael@0 | 3616 | listener.lineStart(); |
michael@0 | 3617 | interpolate(null, null, 1, listener); |
michael@0 | 3618 | listener.lineEnd(); |
michael@0 | 3619 | } |
michael@0 | 3620 | if (visible) { |
michael@0 | 3621 | d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener); |
michael@0 | 3622 | } |
michael@0 | 3623 | listener.polygonEnd(); |
michael@0 | 3624 | } |
michael@0 | 3625 | segments = polygon = ring = null; |
michael@0 | 3626 | } |
michael@0 | 3627 | }; |
michael@0 | 3628 | function insidePolygon(p) { |
michael@0 | 3629 | var wn = 0, n = polygon.length, y = p[1]; |
michael@0 | 3630 | for (var i = 0; i < n; ++i) { |
michael@0 | 3631 | for (var j = 1, v = polygon[i], m = v.length, a = v[0], b; j < m; ++j) { |
michael@0 | 3632 | b = v[j]; |
michael@0 | 3633 | if (a[1] <= y) { |
michael@0 | 3634 | if (b[1] > y && d3_cross2d(a, b, p) > 0) ++wn; |
michael@0 | 3635 | } else { |
michael@0 | 3636 | if (b[1] <= y && d3_cross2d(a, b, p) < 0) --wn; |
michael@0 | 3637 | } |
michael@0 | 3638 | a = b; |
michael@0 | 3639 | } |
michael@0 | 3640 | } |
michael@0 | 3641 | return wn !== 0; |
michael@0 | 3642 | } |
michael@0 | 3643 | function interpolate(from, to, direction, listener) { |
michael@0 | 3644 | var a = 0, a1 = 0; |
michael@0 | 3645 | if (from == null || (a = corner(from, direction)) !== (a1 = corner(to, direction)) || comparePoints(from, to) < 0 ^ direction > 0) { |
michael@0 | 3646 | do { |
michael@0 | 3647 | listener.point(a === 0 || a === 3 ? x0 : x1, a > 1 ? y1 : y0); |
michael@0 | 3648 | } while ((a = (a + direction + 4) % 4) !== a1); |
michael@0 | 3649 | } else { |
michael@0 | 3650 | listener.point(to[0], to[1]); |
michael@0 | 3651 | } |
michael@0 | 3652 | } |
michael@0 | 3653 | function pointVisible(x, y) { |
michael@0 | 3654 | return x0 <= x && x <= x1 && y0 <= y && y <= y1; |
michael@0 | 3655 | } |
michael@0 | 3656 | function point(x, y) { |
michael@0 | 3657 | if (pointVisible(x, y)) listener.point(x, y); |
michael@0 | 3658 | } |
michael@0 | 3659 | var x__, y__, v__, x_, y_, v_, first, clean; |
michael@0 | 3660 | function lineStart() { |
michael@0 | 3661 | clip.point = linePoint; |
michael@0 | 3662 | if (polygon) polygon.push(ring = []); |
michael@0 | 3663 | first = true; |
michael@0 | 3664 | v_ = false; |
michael@0 | 3665 | x_ = y_ = NaN; |
michael@0 | 3666 | } |
michael@0 | 3667 | function lineEnd() { |
michael@0 | 3668 | if (segments) { |
michael@0 | 3669 | linePoint(x__, y__); |
michael@0 | 3670 | if (v__ && v_) bufferListener.rejoin(); |
michael@0 | 3671 | segments.push(bufferListener.buffer()); |
michael@0 | 3672 | } |
michael@0 | 3673 | clip.point = point; |
michael@0 | 3674 | if (v_) listener.lineEnd(); |
michael@0 | 3675 | } |
michael@0 | 3676 | function linePoint(x, y) { |
michael@0 | 3677 | x = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, x)); |
michael@0 | 3678 | y = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, y)); |
michael@0 | 3679 | var v = pointVisible(x, y); |
michael@0 | 3680 | if (polygon) ring.push([ x, y ]); |
michael@0 | 3681 | if (first) { |
michael@0 | 3682 | x__ = x, y__ = y, v__ = v; |
michael@0 | 3683 | first = false; |
michael@0 | 3684 | if (v) { |
michael@0 | 3685 | listener.lineStart(); |
michael@0 | 3686 | listener.point(x, y); |
michael@0 | 3687 | } |
michael@0 | 3688 | } else { |
michael@0 | 3689 | if (v && v_) listener.point(x, y); else { |
michael@0 | 3690 | var l = { |
michael@0 | 3691 | a: { |
michael@0 | 3692 | x: x_, |
michael@0 | 3693 | y: y_ |
michael@0 | 3694 | }, |
michael@0 | 3695 | b: { |
michael@0 | 3696 | x: x, |
michael@0 | 3697 | y: y |
michael@0 | 3698 | } |
michael@0 | 3699 | }; |
michael@0 | 3700 | if (clipLine(l)) { |
michael@0 | 3701 | if (!v_) { |
michael@0 | 3702 | listener.lineStart(); |
michael@0 | 3703 | listener.point(l.a.x, l.a.y); |
michael@0 | 3704 | } |
michael@0 | 3705 | listener.point(l.b.x, l.b.y); |
michael@0 | 3706 | if (!v) listener.lineEnd(); |
michael@0 | 3707 | clean = false; |
michael@0 | 3708 | } else if (v) { |
michael@0 | 3709 | listener.lineStart(); |
michael@0 | 3710 | listener.point(x, y); |
michael@0 | 3711 | clean = false; |
michael@0 | 3712 | } |
michael@0 | 3713 | } |
michael@0 | 3714 | } |
michael@0 | 3715 | x_ = x, y_ = y, v_ = v; |
michael@0 | 3716 | } |
michael@0 | 3717 | return clip; |
michael@0 | 3718 | }; |
michael@0 | 3719 | function corner(p, direction) { |
michael@0 | 3720 | return abs(p[0] - x0) < ε ? direction > 0 ? 0 : 3 : abs(p[0] - x1) < ε ? direction > 0 ? 2 : 1 : abs(p[1] - y0) < ε ? direction > 0 ? 1 : 0 : direction > 0 ? 3 : 2; |
michael@0 | 3721 | } |
michael@0 | 3722 | function compare(a, b) { |
michael@0 | 3723 | return comparePoints(a.x, b.x); |
michael@0 | 3724 | } |
michael@0 | 3725 | function comparePoints(a, b) { |
michael@0 | 3726 | var ca = corner(a, 1), cb = corner(b, 1); |
michael@0 | 3727 | return ca !== cb ? ca - cb : ca === 0 ? b[1] - a[1] : ca === 1 ? a[0] - b[0] : ca === 2 ? a[1] - b[1] : b[0] - a[0]; |
michael@0 | 3728 | } |
michael@0 | 3729 | } |
michael@0 | 3730 | function d3_geo_compose(a, b) { |
michael@0 | 3731 | function compose(x, y) { |
michael@0 | 3732 | return x = a(x, y), b(x[0], x[1]); |
michael@0 | 3733 | } |
michael@0 | 3734 | if (a.invert && b.invert) compose.invert = function(x, y) { |
michael@0 | 3735 | return x = b.invert(x, y), x && a.invert(x[0], x[1]); |
michael@0 | 3736 | }; |
michael@0 | 3737 | return compose; |
michael@0 | 3738 | } |
michael@0 | 3739 | function d3_geo_conic(projectAt) { |
michael@0 | 3740 | var φ0 = 0, φ1 = π / 3, m = d3_geo_projectionMutator(projectAt), p = m(φ0, φ1); |
michael@0 | 3741 | p.parallels = function(_) { |
michael@0 | 3742 | if (!arguments.length) return [ φ0 / π * 180, φ1 / π * 180 ]; |
michael@0 | 3743 | return m(φ0 = _[0] * π / 180, φ1 = _[1] * π / 180); |
michael@0 | 3744 | }; |
michael@0 | 3745 | return p; |
michael@0 | 3746 | } |
michael@0 | 3747 | function d3_geo_conicEqualArea(φ0, φ1) { |
michael@0 | 3748 | var sinφ0 = Math.sin(φ0), n = (sinφ0 + Math.sin(φ1)) / 2, C = 1 + sinφ0 * (2 * n - sinφ0), ρ0 = Math.sqrt(C) / n; |
michael@0 | 3749 | function forward(λ, φ) { |
michael@0 | 3750 | var ρ = Math.sqrt(C - 2 * n * Math.sin(φ)) / n; |
michael@0 | 3751 | return [ ρ * Math.sin(λ *= n), ρ0 - ρ * Math.cos(λ) ]; |
michael@0 | 3752 | } |
michael@0 | 3753 | forward.invert = function(x, y) { |
michael@0 | 3754 | var ρ0_y = ρ0 - y; |
michael@0 | 3755 | return [ Math.atan2(x, ρ0_y) / n, d3_asin((C - (x * x + ρ0_y * ρ0_y) * n * n) / (2 * n)) ]; |
michael@0 | 3756 | }; |
michael@0 | 3757 | return forward; |
michael@0 | 3758 | } |
michael@0 | 3759 | (d3.geo.conicEqualArea = function() { |
michael@0 | 3760 | return d3_geo_conic(d3_geo_conicEqualArea); |
michael@0 | 3761 | }).raw = d3_geo_conicEqualArea; |
michael@0 | 3762 | d3.geo.albers = function() { |
michael@0 | 3763 | return d3.geo.conicEqualArea().rotate([ 96, 0 ]).center([ -.6, 38.7 ]).parallels([ 29.5, 45.5 ]).scale(1070); |
michael@0 | 3764 | }; |
michael@0 | 3765 | d3.geo.albersUsa = function() { |
michael@0 | 3766 | var lower48 = d3.geo.albers(); |
michael@0 | 3767 | var alaska = d3.geo.conicEqualArea().rotate([ 154, 0 ]).center([ -2, 58.5 ]).parallels([ 55, 65 ]); |
michael@0 | 3768 | var hawaii = d3.geo.conicEqualArea().rotate([ 157, 0 ]).center([ -3, 19.9 ]).parallels([ 8, 18 ]); |
michael@0 | 3769 | var point, pointStream = { |
michael@0 | 3770 | point: function(x, y) { |
michael@0 | 3771 | point = [ x, y ]; |
michael@0 | 3772 | } |
michael@0 | 3773 | }, lower48Point, alaskaPoint, hawaiiPoint; |
michael@0 | 3774 | function albersUsa(coordinates) { |
michael@0 | 3775 | var x = coordinates[0], y = coordinates[1]; |
michael@0 | 3776 | point = null; |
michael@0 | 3777 | (lower48Point(x, y), point) || (alaskaPoint(x, y), point) || hawaiiPoint(x, y); |
michael@0 | 3778 | return point; |
michael@0 | 3779 | } |
michael@0 | 3780 | albersUsa.invert = function(coordinates) { |
michael@0 | 3781 | var k = lower48.scale(), t = lower48.translate(), x = (coordinates[0] - t[0]) / k, y = (coordinates[1] - t[1]) / k; |
michael@0 | 3782 | return (y >= .12 && y < .234 && x >= -.425 && x < -.214 ? alaska : y >= .166 && y < .234 && x >= -.214 && x < -.115 ? hawaii : lower48).invert(coordinates); |
michael@0 | 3783 | }; |
michael@0 | 3784 | albersUsa.stream = function(stream) { |
michael@0 | 3785 | var lower48Stream = lower48.stream(stream), alaskaStream = alaska.stream(stream), hawaiiStream = hawaii.stream(stream); |
michael@0 | 3786 | return { |
michael@0 | 3787 | point: function(x, y) { |
michael@0 | 3788 | lower48Stream.point(x, y); |
michael@0 | 3789 | alaskaStream.point(x, y); |
michael@0 | 3790 | hawaiiStream.point(x, y); |
michael@0 | 3791 | }, |
michael@0 | 3792 | sphere: function() { |
michael@0 | 3793 | lower48Stream.sphere(); |
michael@0 | 3794 | alaskaStream.sphere(); |
michael@0 | 3795 | hawaiiStream.sphere(); |
michael@0 | 3796 | }, |
michael@0 | 3797 | lineStart: function() { |
michael@0 | 3798 | lower48Stream.lineStart(); |
michael@0 | 3799 | alaskaStream.lineStart(); |
michael@0 | 3800 | hawaiiStream.lineStart(); |
michael@0 | 3801 | }, |
michael@0 | 3802 | lineEnd: function() { |
michael@0 | 3803 | lower48Stream.lineEnd(); |
michael@0 | 3804 | alaskaStream.lineEnd(); |
michael@0 | 3805 | hawaiiStream.lineEnd(); |
michael@0 | 3806 | }, |
michael@0 | 3807 | polygonStart: function() { |
michael@0 | 3808 | lower48Stream.polygonStart(); |
michael@0 | 3809 | alaskaStream.polygonStart(); |
michael@0 | 3810 | hawaiiStream.polygonStart(); |
michael@0 | 3811 | }, |
michael@0 | 3812 | polygonEnd: function() { |
michael@0 | 3813 | lower48Stream.polygonEnd(); |
michael@0 | 3814 | alaskaStream.polygonEnd(); |
michael@0 | 3815 | hawaiiStream.polygonEnd(); |
michael@0 | 3816 | } |
michael@0 | 3817 | }; |
michael@0 | 3818 | }; |
michael@0 | 3819 | albersUsa.precision = function(_) { |
michael@0 | 3820 | if (!arguments.length) return lower48.precision(); |
michael@0 | 3821 | lower48.precision(_); |
michael@0 | 3822 | alaska.precision(_); |
michael@0 | 3823 | hawaii.precision(_); |
michael@0 | 3824 | return albersUsa; |
michael@0 | 3825 | }; |
michael@0 | 3826 | albersUsa.scale = function(_) { |
michael@0 | 3827 | if (!arguments.length) return lower48.scale(); |
michael@0 | 3828 | lower48.scale(_); |
michael@0 | 3829 | alaska.scale(_ * .35); |
michael@0 | 3830 | hawaii.scale(_); |
michael@0 | 3831 | return albersUsa.translate(lower48.translate()); |
michael@0 | 3832 | }; |
michael@0 | 3833 | albersUsa.translate = function(_) { |
michael@0 | 3834 | if (!arguments.length) return lower48.translate(); |
michael@0 | 3835 | var k = lower48.scale(), x = +_[0], y = +_[1]; |
michael@0 | 3836 | lower48Point = lower48.translate(_).clipExtent([ [ x - .455 * k, y - .238 * k ], [ x + .455 * k, y + .238 * k ] ]).stream(pointStream).point; |
michael@0 | 3837 | alaskaPoint = alaska.translate([ x - .307 * k, y + .201 * k ]).clipExtent([ [ x - .425 * k + ε, y + .12 * k + ε ], [ x - .214 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; |
michael@0 | 3838 | hawaiiPoint = hawaii.translate([ x - .205 * k, y + .212 * k ]).clipExtent([ [ x - .214 * k + ε, y + .166 * k + ε ], [ x - .115 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; |
michael@0 | 3839 | return albersUsa; |
michael@0 | 3840 | }; |
michael@0 | 3841 | return albersUsa.scale(1070); |
michael@0 | 3842 | }; |
michael@0 | 3843 | var d3_geo_pathAreaSum, d3_geo_pathAreaPolygon, d3_geo_pathArea = { |
michael@0 | 3844 | point: d3_noop, |
michael@0 | 3845 | lineStart: d3_noop, |
michael@0 | 3846 | lineEnd: d3_noop, |
michael@0 | 3847 | polygonStart: function() { |
michael@0 | 3848 | d3_geo_pathAreaPolygon = 0; |
michael@0 | 3849 | d3_geo_pathArea.lineStart = d3_geo_pathAreaRingStart; |
michael@0 | 3850 | }, |
michael@0 | 3851 | polygonEnd: function() { |
michael@0 | 3852 | d3_geo_pathArea.lineStart = d3_geo_pathArea.lineEnd = d3_geo_pathArea.point = d3_noop; |
michael@0 | 3853 | d3_geo_pathAreaSum += abs(d3_geo_pathAreaPolygon / 2); |
michael@0 | 3854 | } |
michael@0 | 3855 | }; |
michael@0 | 3856 | function d3_geo_pathAreaRingStart() { |
michael@0 | 3857 | var x00, y00, x0, y0; |
michael@0 | 3858 | d3_geo_pathArea.point = function(x, y) { |
michael@0 | 3859 | d3_geo_pathArea.point = nextPoint; |
michael@0 | 3860 | x00 = x0 = x, y00 = y0 = y; |
michael@0 | 3861 | }; |
michael@0 | 3862 | function nextPoint(x, y) { |
michael@0 | 3863 | d3_geo_pathAreaPolygon += y0 * x - x0 * y; |
michael@0 | 3864 | x0 = x, y0 = y; |
michael@0 | 3865 | } |
michael@0 | 3866 | d3_geo_pathArea.lineEnd = function() { |
michael@0 | 3867 | nextPoint(x00, y00); |
michael@0 | 3868 | }; |
michael@0 | 3869 | } |
michael@0 | 3870 | var d3_geo_pathBoundsX0, d3_geo_pathBoundsY0, d3_geo_pathBoundsX1, d3_geo_pathBoundsY1; |
michael@0 | 3871 | var d3_geo_pathBounds = { |
michael@0 | 3872 | point: d3_geo_pathBoundsPoint, |
michael@0 | 3873 | lineStart: d3_noop, |
michael@0 | 3874 | lineEnd: d3_noop, |
michael@0 | 3875 | polygonStart: d3_noop, |
michael@0 | 3876 | polygonEnd: d3_noop |
michael@0 | 3877 | }; |
michael@0 | 3878 | function d3_geo_pathBoundsPoint(x, y) { |
michael@0 | 3879 | if (x < d3_geo_pathBoundsX0) d3_geo_pathBoundsX0 = x; |
michael@0 | 3880 | if (x > d3_geo_pathBoundsX1) d3_geo_pathBoundsX1 = x; |
michael@0 | 3881 | if (y < d3_geo_pathBoundsY0) d3_geo_pathBoundsY0 = y; |
michael@0 | 3882 | if (y > d3_geo_pathBoundsY1) d3_geo_pathBoundsY1 = y; |
michael@0 | 3883 | } |
michael@0 | 3884 | function d3_geo_pathBuffer() { |
michael@0 | 3885 | var pointCircle = d3_geo_pathBufferCircle(4.5), buffer = []; |
michael@0 | 3886 | var stream = { |
michael@0 | 3887 | point: point, |
michael@0 | 3888 | lineStart: function() { |
michael@0 | 3889 | stream.point = pointLineStart; |
michael@0 | 3890 | }, |
michael@0 | 3891 | lineEnd: lineEnd, |
michael@0 | 3892 | polygonStart: function() { |
michael@0 | 3893 | stream.lineEnd = lineEndPolygon; |
michael@0 | 3894 | }, |
michael@0 | 3895 | polygonEnd: function() { |
michael@0 | 3896 | stream.lineEnd = lineEnd; |
michael@0 | 3897 | stream.point = point; |
michael@0 | 3898 | }, |
michael@0 | 3899 | pointRadius: function(_) { |
michael@0 | 3900 | pointCircle = d3_geo_pathBufferCircle(_); |
michael@0 | 3901 | return stream; |
michael@0 | 3902 | }, |
michael@0 | 3903 | result: function() { |
michael@0 | 3904 | if (buffer.length) { |
michael@0 | 3905 | var result = buffer.join(""); |
michael@0 | 3906 | buffer = []; |
michael@0 | 3907 | return result; |
michael@0 | 3908 | } |
michael@0 | 3909 | } |
michael@0 | 3910 | }; |
michael@0 | 3911 | function point(x, y) { |
michael@0 | 3912 | buffer.push("M", x, ",", y, pointCircle); |
michael@0 | 3913 | } |
michael@0 | 3914 | function pointLineStart(x, y) { |
michael@0 | 3915 | buffer.push("M", x, ",", y); |
michael@0 | 3916 | stream.point = pointLine; |
michael@0 | 3917 | } |
michael@0 | 3918 | function pointLine(x, y) { |
michael@0 | 3919 | buffer.push("L", x, ",", y); |
michael@0 | 3920 | } |
michael@0 | 3921 | function lineEnd() { |
michael@0 | 3922 | stream.point = point; |
michael@0 | 3923 | } |
michael@0 | 3924 | function lineEndPolygon() { |
michael@0 | 3925 | buffer.push("Z"); |
michael@0 | 3926 | } |
michael@0 | 3927 | return stream; |
michael@0 | 3928 | } |
michael@0 | 3929 | function d3_geo_pathBufferCircle(radius) { |
michael@0 | 3930 | return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + 2 * radius + "z"; |
michael@0 | 3931 | } |
michael@0 | 3932 | var d3_geo_pathCentroid = { |
michael@0 | 3933 | point: d3_geo_pathCentroidPoint, |
michael@0 | 3934 | lineStart: d3_geo_pathCentroidLineStart, |
michael@0 | 3935 | lineEnd: d3_geo_pathCentroidLineEnd, |
michael@0 | 3936 | polygonStart: function() { |
michael@0 | 3937 | d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidRingStart; |
michael@0 | 3938 | }, |
michael@0 | 3939 | polygonEnd: function() { |
michael@0 | 3940 | d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; |
michael@0 | 3941 | d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidLineStart; |
michael@0 | 3942 | d3_geo_pathCentroid.lineEnd = d3_geo_pathCentroidLineEnd; |
michael@0 | 3943 | } |
michael@0 | 3944 | }; |
michael@0 | 3945 | function d3_geo_pathCentroidPoint(x, y) { |
michael@0 | 3946 | d3_geo_centroidX0 += x; |
michael@0 | 3947 | d3_geo_centroidY0 += y; |
michael@0 | 3948 | ++d3_geo_centroidZ0; |
michael@0 | 3949 | } |
michael@0 | 3950 | function d3_geo_pathCentroidLineStart() { |
michael@0 | 3951 | var x0, y0; |
michael@0 | 3952 | d3_geo_pathCentroid.point = function(x, y) { |
michael@0 | 3953 | d3_geo_pathCentroid.point = nextPoint; |
michael@0 | 3954 | d3_geo_pathCentroidPoint(x0 = x, y0 = y); |
michael@0 | 3955 | }; |
michael@0 | 3956 | function nextPoint(x, y) { |
michael@0 | 3957 | var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); |
michael@0 | 3958 | d3_geo_centroidX1 += z * (x0 + x) / 2; |
michael@0 | 3959 | d3_geo_centroidY1 += z * (y0 + y) / 2; |
michael@0 | 3960 | d3_geo_centroidZ1 += z; |
michael@0 | 3961 | d3_geo_pathCentroidPoint(x0 = x, y0 = y); |
michael@0 | 3962 | } |
michael@0 | 3963 | } |
michael@0 | 3964 | function d3_geo_pathCentroidLineEnd() { |
michael@0 | 3965 | d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; |
michael@0 | 3966 | } |
michael@0 | 3967 | function d3_geo_pathCentroidRingStart() { |
michael@0 | 3968 | var x00, y00, x0, y0; |
michael@0 | 3969 | d3_geo_pathCentroid.point = function(x, y) { |
michael@0 | 3970 | d3_geo_pathCentroid.point = nextPoint; |
michael@0 | 3971 | d3_geo_pathCentroidPoint(x00 = x0 = x, y00 = y0 = y); |
michael@0 | 3972 | }; |
michael@0 | 3973 | function nextPoint(x, y) { |
michael@0 | 3974 | var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); |
michael@0 | 3975 | d3_geo_centroidX1 += z * (x0 + x) / 2; |
michael@0 | 3976 | d3_geo_centroidY1 += z * (y0 + y) / 2; |
michael@0 | 3977 | d3_geo_centroidZ1 += z; |
michael@0 | 3978 | z = y0 * x - x0 * y; |
michael@0 | 3979 | d3_geo_centroidX2 += z * (x0 + x); |
michael@0 | 3980 | d3_geo_centroidY2 += z * (y0 + y); |
michael@0 | 3981 | d3_geo_centroidZ2 += z * 3; |
michael@0 | 3982 | d3_geo_pathCentroidPoint(x0 = x, y0 = y); |
michael@0 | 3983 | } |
michael@0 | 3984 | d3_geo_pathCentroid.lineEnd = function() { |
michael@0 | 3985 | nextPoint(x00, y00); |
michael@0 | 3986 | }; |
michael@0 | 3987 | } |
michael@0 | 3988 | function d3_geo_pathContext(context) { |
michael@0 | 3989 | var pointRadius = 4.5; |
michael@0 | 3990 | var stream = { |
michael@0 | 3991 | point: point, |
michael@0 | 3992 | lineStart: function() { |
michael@0 | 3993 | stream.point = pointLineStart; |
michael@0 | 3994 | }, |
michael@0 | 3995 | lineEnd: lineEnd, |
michael@0 | 3996 | polygonStart: function() { |
michael@0 | 3997 | stream.lineEnd = lineEndPolygon; |
michael@0 | 3998 | }, |
michael@0 | 3999 | polygonEnd: function() { |
michael@0 | 4000 | stream.lineEnd = lineEnd; |
michael@0 | 4001 | stream.point = point; |
michael@0 | 4002 | }, |
michael@0 | 4003 | pointRadius: function(_) { |
michael@0 | 4004 | pointRadius = _; |
michael@0 | 4005 | return stream; |
michael@0 | 4006 | }, |
michael@0 | 4007 | result: d3_noop |
michael@0 | 4008 | }; |
michael@0 | 4009 | function point(x, y) { |
michael@0 | 4010 | context.moveTo(x, y); |
michael@0 | 4011 | context.arc(x, y, pointRadius, 0, τ); |
michael@0 | 4012 | } |
michael@0 | 4013 | function pointLineStart(x, y) { |
michael@0 | 4014 | context.moveTo(x, y); |
michael@0 | 4015 | stream.point = pointLine; |
michael@0 | 4016 | } |
michael@0 | 4017 | function pointLine(x, y) { |
michael@0 | 4018 | context.lineTo(x, y); |
michael@0 | 4019 | } |
michael@0 | 4020 | function lineEnd() { |
michael@0 | 4021 | stream.point = point; |
michael@0 | 4022 | } |
michael@0 | 4023 | function lineEndPolygon() { |
michael@0 | 4024 | context.closePath(); |
michael@0 | 4025 | } |
michael@0 | 4026 | return stream; |
michael@0 | 4027 | } |
michael@0 | 4028 | function d3_geo_resample(project) { |
michael@0 | 4029 | var δ2 = .5, cosMinDistance = Math.cos(30 * d3_radians), maxDepth = 16; |
michael@0 | 4030 | function resample(stream) { |
michael@0 | 4031 | return (maxDepth ? resampleRecursive : resampleNone)(stream); |
michael@0 | 4032 | } |
michael@0 | 4033 | function resampleNone(stream) { |
michael@0 | 4034 | return d3_geo_transformPoint(stream, function(x, y) { |
michael@0 | 4035 | x = project(x, y); |
michael@0 | 4036 | stream.point(x[0], x[1]); |
michael@0 | 4037 | }); |
michael@0 | 4038 | } |
michael@0 | 4039 | function resampleRecursive(stream) { |
michael@0 | 4040 | var λ00, φ00, x00, y00, a00, b00, c00, λ0, x0, y0, a0, b0, c0; |
michael@0 | 4041 | var resample = { |
michael@0 | 4042 | point: point, |
michael@0 | 4043 | lineStart: lineStart, |
michael@0 | 4044 | lineEnd: lineEnd, |
michael@0 | 4045 | polygonStart: function() { |
michael@0 | 4046 | stream.polygonStart(); |
michael@0 | 4047 | resample.lineStart = ringStart; |
michael@0 | 4048 | }, |
michael@0 | 4049 | polygonEnd: function() { |
michael@0 | 4050 | stream.polygonEnd(); |
michael@0 | 4051 | resample.lineStart = lineStart; |
michael@0 | 4052 | } |
michael@0 | 4053 | }; |
michael@0 | 4054 | function point(x, y) { |
michael@0 | 4055 | x = project(x, y); |
michael@0 | 4056 | stream.point(x[0], x[1]); |
michael@0 | 4057 | } |
michael@0 | 4058 | function lineStart() { |
michael@0 | 4059 | x0 = NaN; |
michael@0 | 4060 | resample.point = linePoint; |
michael@0 | 4061 | stream.lineStart(); |
michael@0 | 4062 | } |
michael@0 | 4063 | function linePoint(λ, φ) { |
michael@0 | 4064 | var c = d3_geo_cartesian([ λ, φ ]), p = project(λ, φ); |
michael@0 | 4065 | resampleLineTo(x0, y0, λ0, a0, b0, c0, x0 = p[0], y0 = p[1], λ0 = λ, a0 = c[0], b0 = c[1], c0 = c[2], maxDepth, stream); |
michael@0 | 4066 | stream.point(x0, y0); |
michael@0 | 4067 | } |
michael@0 | 4068 | function lineEnd() { |
michael@0 | 4069 | resample.point = point; |
michael@0 | 4070 | stream.lineEnd(); |
michael@0 | 4071 | } |
michael@0 | 4072 | function ringStart() { |
michael@0 | 4073 | lineStart(); |
michael@0 | 4074 | resample.point = ringPoint; |
michael@0 | 4075 | resample.lineEnd = ringEnd; |
michael@0 | 4076 | } |
michael@0 | 4077 | function ringPoint(λ, φ) { |
michael@0 | 4078 | linePoint(λ00 = λ, φ00 = φ), x00 = x0, y00 = y0, a00 = a0, b00 = b0, c00 = c0; |
michael@0 | 4079 | resample.point = linePoint; |
michael@0 | 4080 | } |
michael@0 | 4081 | function ringEnd() { |
michael@0 | 4082 | resampleLineTo(x0, y0, λ0, a0, b0, c0, x00, y00, λ00, a00, b00, c00, maxDepth, stream); |
michael@0 | 4083 | resample.lineEnd = lineEnd; |
michael@0 | 4084 | lineEnd(); |
michael@0 | 4085 | } |
michael@0 | 4086 | return resample; |
michael@0 | 4087 | } |
michael@0 | 4088 | function resampleLineTo(x0, y0, λ0, a0, b0, c0, x1, y1, λ1, a1, b1, c1, depth, stream) { |
michael@0 | 4089 | var dx = x1 - x0, dy = y1 - y0, d2 = dx * dx + dy * dy; |
michael@0 | 4090 | if (d2 > 4 * δ2 && depth--) { |
michael@0 | 4091 | var a = a0 + a1, b = b0 + b1, c = c0 + c1, m = Math.sqrt(a * a + b * b + c * c), φ2 = Math.asin(c /= m), λ2 = abs(abs(c) - 1) < ε || abs(λ0 - λ1) < ε ? (λ0 + λ1) / 2 : Math.atan2(b, a), p = project(λ2, φ2), x2 = p[0], y2 = p[1], dx2 = x2 - x0, dy2 = y2 - y0, dz = dy * dx2 - dx * dy2; |
michael@0 | 4092 | if (dz * dz / d2 > δ2 || abs((dx * dx2 + dy * dy2) / d2 - .5) > .3 || a0 * a1 + b0 * b1 + c0 * c1 < cosMinDistance) { |
michael@0 | 4093 | resampleLineTo(x0, y0, λ0, a0, b0, c0, x2, y2, λ2, a /= m, b /= m, c, depth, stream); |
michael@0 | 4094 | stream.point(x2, y2); |
michael@0 | 4095 | resampleLineTo(x2, y2, λ2, a, b, c, x1, y1, λ1, a1, b1, c1, depth, stream); |
michael@0 | 4096 | } |
michael@0 | 4097 | } |
michael@0 | 4098 | } |
michael@0 | 4099 | resample.precision = function(_) { |
michael@0 | 4100 | if (!arguments.length) return Math.sqrt(δ2); |
michael@0 | 4101 | maxDepth = (δ2 = _ * _) > 0 && 16; |
michael@0 | 4102 | return resample; |
michael@0 | 4103 | }; |
michael@0 | 4104 | return resample; |
michael@0 | 4105 | } |
michael@0 | 4106 | d3.geo.path = function() { |
michael@0 | 4107 | var pointRadius = 4.5, projection, context, projectStream, contextStream, cacheStream; |
michael@0 | 4108 | function path(object) { |
michael@0 | 4109 | if (object) { |
michael@0 | 4110 | if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments)); |
michael@0 | 4111 | if (!cacheStream || !cacheStream.valid) cacheStream = projectStream(contextStream); |
michael@0 | 4112 | d3.geo.stream(object, cacheStream); |
michael@0 | 4113 | } |
michael@0 | 4114 | return contextStream.result(); |
michael@0 | 4115 | } |
michael@0 | 4116 | path.area = function(object) { |
michael@0 | 4117 | d3_geo_pathAreaSum = 0; |
michael@0 | 4118 | d3.geo.stream(object, projectStream(d3_geo_pathArea)); |
michael@0 | 4119 | return d3_geo_pathAreaSum; |
michael@0 | 4120 | }; |
michael@0 | 4121 | path.centroid = function(object) { |
michael@0 | 4122 | d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; |
michael@0 | 4123 | d3.geo.stream(object, projectStream(d3_geo_pathCentroid)); |
michael@0 | 4124 | return d3_geo_centroidZ2 ? [ d3_geo_centroidX2 / d3_geo_centroidZ2, d3_geo_centroidY2 / d3_geo_centroidZ2 ] : d3_geo_centroidZ1 ? [ d3_geo_centroidX1 / d3_geo_centroidZ1, d3_geo_centroidY1 / d3_geo_centroidZ1 ] : d3_geo_centroidZ0 ? [ d3_geo_centroidX0 / d3_geo_centroidZ0, d3_geo_centroidY0 / d3_geo_centroidZ0 ] : [ NaN, NaN ]; |
michael@0 | 4125 | }; |
michael@0 | 4126 | path.bounds = function(object) { |
michael@0 | 4127 | d3_geo_pathBoundsX1 = d3_geo_pathBoundsY1 = -(d3_geo_pathBoundsX0 = d3_geo_pathBoundsY0 = Infinity); |
michael@0 | 4128 | d3.geo.stream(object, projectStream(d3_geo_pathBounds)); |
michael@0 | 4129 | return [ [ d3_geo_pathBoundsX0, d3_geo_pathBoundsY0 ], [ d3_geo_pathBoundsX1, d3_geo_pathBoundsY1 ] ]; |
michael@0 | 4130 | }; |
michael@0 | 4131 | path.projection = function(_) { |
michael@0 | 4132 | if (!arguments.length) return projection; |
michael@0 | 4133 | projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity; |
michael@0 | 4134 | return reset(); |
michael@0 | 4135 | }; |
michael@0 | 4136 | path.context = function(_) { |
michael@0 | 4137 | if (!arguments.length) return context; |
michael@0 | 4138 | contextStream = (context = _) == null ? new d3_geo_pathBuffer() : new d3_geo_pathContext(_); |
michael@0 | 4139 | if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius); |
michael@0 | 4140 | return reset(); |
michael@0 | 4141 | }; |
michael@0 | 4142 | path.pointRadius = function(_) { |
michael@0 | 4143 | if (!arguments.length) return pointRadius; |
michael@0 | 4144 | pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_); |
michael@0 | 4145 | return path; |
michael@0 | 4146 | }; |
michael@0 | 4147 | function reset() { |
michael@0 | 4148 | cacheStream = null; |
michael@0 | 4149 | return path; |
michael@0 | 4150 | } |
michael@0 | 4151 | return path.projection(d3.geo.albersUsa()).context(null); |
michael@0 | 4152 | }; |
michael@0 | 4153 | function d3_geo_pathProjectStream(project) { |
michael@0 | 4154 | var resample = d3_geo_resample(function(x, y) { |
michael@0 | 4155 | return project([ x * d3_degrees, y * d3_degrees ]); |
michael@0 | 4156 | }); |
michael@0 | 4157 | return function(stream) { |
michael@0 | 4158 | return d3_geo_projectionRadians(resample(stream)); |
michael@0 | 4159 | }; |
michael@0 | 4160 | } |
michael@0 | 4161 | d3.geo.transform = function(methods) { |
michael@0 | 4162 | return { |
michael@0 | 4163 | stream: function(stream) { |
michael@0 | 4164 | var transform = new d3_geo_transform(stream); |
michael@0 | 4165 | for (var k in methods) transform[k] = methods[k]; |
michael@0 | 4166 | return transform; |
michael@0 | 4167 | } |
michael@0 | 4168 | }; |
michael@0 | 4169 | }; |
michael@0 | 4170 | function d3_geo_transform(stream) { |
michael@0 | 4171 | this.stream = stream; |
michael@0 | 4172 | } |
michael@0 | 4173 | d3_geo_transform.prototype = { |
michael@0 | 4174 | point: function(x, y) { |
michael@0 | 4175 | this.stream.point(x, y); |
michael@0 | 4176 | }, |
michael@0 | 4177 | sphere: function() { |
michael@0 | 4178 | this.stream.sphere(); |
michael@0 | 4179 | }, |
michael@0 | 4180 | lineStart: function() { |
michael@0 | 4181 | this.stream.lineStart(); |
michael@0 | 4182 | }, |
michael@0 | 4183 | lineEnd: function() { |
michael@0 | 4184 | this.stream.lineEnd(); |
michael@0 | 4185 | }, |
michael@0 | 4186 | polygonStart: function() { |
michael@0 | 4187 | this.stream.polygonStart(); |
michael@0 | 4188 | }, |
michael@0 | 4189 | polygonEnd: function() { |
michael@0 | 4190 | this.stream.polygonEnd(); |
michael@0 | 4191 | } |
michael@0 | 4192 | }; |
michael@0 | 4193 | function d3_geo_transformPoint(stream, point) { |
michael@0 | 4194 | return { |
michael@0 | 4195 | point: point, |
michael@0 | 4196 | sphere: function() { |
michael@0 | 4197 | stream.sphere(); |
michael@0 | 4198 | }, |
michael@0 | 4199 | lineStart: function() { |
michael@0 | 4200 | stream.lineStart(); |
michael@0 | 4201 | }, |
michael@0 | 4202 | lineEnd: function() { |
michael@0 | 4203 | stream.lineEnd(); |
michael@0 | 4204 | }, |
michael@0 | 4205 | polygonStart: function() { |
michael@0 | 4206 | stream.polygonStart(); |
michael@0 | 4207 | }, |
michael@0 | 4208 | polygonEnd: function() { |
michael@0 | 4209 | stream.polygonEnd(); |
michael@0 | 4210 | } |
michael@0 | 4211 | }; |
michael@0 | 4212 | } |
michael@0 | 4213 | d3.geo.projection = d3_geo_projection; |
michael@0 | 4214 | d3.geo.projectionMutator = d3_geo_projectionMutator; |
michael@0 | 4215 | function d3_geo_projection(project) { |
michael@0 | 4216 | return d3_geo_projectionMutator(function() { |
michael@0 | 4217 | return project; |
michael@0 | 4218 | })(); |
michael@0 | 4219 | } |
michael@0 | 4220 | function d3_geo_projectionMutator(projectAt) { |
michael@0 | 4221 | var project, rotate, projectRotate, projectResample = d3_geo_resample(function(x, y) { |
michael@0 | 4222 | x = project(x, y); |
michael@0 | 4223 | return [ x[0] * k + δx, δy - x[1] * k ]; |
michael@0 | 4224 | }), k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx, δy, preclip = d3_geo_clipAntimeridian, postclip = d3_identity, clipAngle = null, clipExtent = null, stream; |
michael@0 | 4225 | function projection(point) { |
michael@0 | 4226 | point = projectRotate(point[0] * d3_radians, point[1] * d3_radians); |
michael@0 | 4227 | return [ point[0] * k + δx, δy - point[1] * k ]; |
michael@0 | 4228 | } |
michael@0 | 4229 | function invert(point) { |
michael@0 | 4230 | point = projectRotate.invert((point[0] - δx) / k, (δy - point[1]) / k); |
michael@0 | 4231 | return point && [ point[0] * d3_degrees, point[1] * d3_degrees ]; |
michael@0 | 4232 | } |
michael@0 | 4233 | projection.stream = function(output) { |
michael@0 | 4234 | if (stream) stream.valid = false; |
michael@0 | 4235 | stream = d3_geo_projectionRadians(preclip(rotate, projectResample(postclip(output)))); |
michael@0 | 4236 | stream.valid = true; |
michael@0 | 4237 | return stream; |
michael@0 | 4238 | }; |
michael@0 | 4239 | projection.clipAngle = function(_) { |
michael@0 | 4240 | if (!arguments.length) return clipAngle; |
michael@0 | 4241 | preclip = _ == null ? (clipAngle = _, d3_geo_clipAntimeridian) : d3_geo_clipCircle((clipAngle = +_) * d3_radians); |
michael@0 | 4242 | return invalidate(); |
michael@0 | 4243 | }; |
michael@0 | 4244 | projection.clipExtent = function(_) { |
michael@0 | 4245 | if (!arguments.length) return clipExtent; |
michael@0 | 4246 | clipExtent = _; |
michael@0 | 4247 | postclip = _ ? d3_geo_clipExtent(_[0][0], _[0][1], _[1][0], _[1][1]) : d3_identity; |
michael@0 | 4248 | return invalidate(); |
michael@0 | 4249 | }; |
michael@0 | 4250 | projection.scale = function(_) { |
michael@0 | 4251 | if (!arguments.length) return k; |
michael@0 | 4252 | k = +_; |
michael@0 | 4253 | return reset(); |
michael@0 | 4254 | }; |
michael@0 | 4255 | projection.translate = function(_) { |
michael@0 | 4256 | if (!arguments.length) return [ x, y ]; |
michael@0 | 4257 | x = +_[0]; |
michael@0 | 4258 | y = +_[1]; |
michael@0 | 4259 | return reset(); |
michael@0 | 4260 | }; |
michael@0 | 4261 | projection.center = function(_) { |
michael@0 | 4262 | if (!arguments.length) return [ λ * d3_degrees, φ * d3_degrees ]; |
michael@0 | 4263 | λ = _[0] % 360 * d3_radians; |
michael@0 | 4264 | φ = _[1] % 360 * d3_radians; |
michael@0 | 4265 | return reset(); |
michael@0 | 4266 | }; |
michael@0 | 4267 | projection.rotate = function(_) { |
michael@0 | 4268 | if (!arguments.length) return [ δλ * d3_degrees, δφ * d3_degrees, δγ * d3_degrees ]; |
michael@0 | 4269 | δλ = _[0] % 360 * d3_radians; |
michael@0 | 4270 | δφ = _[1] % 360 * d3_radians; |
michael@0 | 4271 | δγ = _.length > 2 ? _[2] % 360 * d3_radians : 0; |
michael@0 | 4272 | return reset(); |
michael@0 | 4273 | }; |
michael@0 | 4274 | d3.rebind(projection, projectResample, "precision"); |
michael@0 | 4275 | function reset() { |
michael@0 | 4276 | projectRotate = d3_geo_compose(rotate = d3_geo_rotation(δλ, δφ, δγ), project); |
michael@0 | 4277 | var center = project(λ, φ); |
michael@0 | 4278 | δx = x - center[0] * k; |
michael@0 | 4279 | δy = y + center[1] * k; |
michael@0 | 4280 | return invalidate(); |
michael@0 | 4281 | } |
michael@0 | 4282 | function invalidate() { |
michael@0 | 4283 | if (stream) stream.valid = false, stream = null; |
michael@0 | 4284 | return projection; |
michael@0 | 4285 | } |
michael@0 | 4286 | return function() { |
michael@0 | 4287 | project = projectAt.apply(this, arguments); |
michael@0 | 4288 | projection.invert = project.invert && invert; |
michael@0 | 4289 | return reset(); |
michael@0 | 4290 | }; |
michael@0 | 4291 | } |
michael@0 | 4292 | function d3_geo_projectionRadians(stream) { |
michael@0 | 4293 | return d3_geo_transformPoint(stream, function(x, y) { |
michael@0 | 4294 | stream.point(x * d3_radians, y * d3_radians); |
michael@0 | 4295 | }); |
michael@0 | 4296 | } |
michael@0 | 4297 | function d3_geo_equirectangular(λ, φ) { |
michael@0 | 4298 | return [ λ, φ ]; |
michael@0 | 4299 | } |
michael@0 | 4300 | (d3.geo.equirectangular = function() { |
michael@0 | 4301 | return d3_geo_projection(d3_geo_equirectangular); |
michael@0 | 4302 | }).raw = d3_geo_equirectangular.invert = d3_geo_equirectangular; |
michael@0 | 4303 | d3.geo.rotation = function(rotate) { |
michael@0 | 4304 | rotate = d3_geo_rotation(rotate[0] % 360 * d3_radians, rotate[1] * d3_radians, rotate.length > 2 ? rotate[2] * d3_radians : 0); |
michael@0 | 4305 | function forward(coordinates) { |
michael@0 | 4306 | coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians); |
michael@0 | 4307 | return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; |
michael@0 | 4308 | } |
michael@0 | 4309 | forward.invert = function(coordinates) { |
michael@0 | 4310 | coordinates = rotate.invert(coordinates[0] * d3_radians, coordinates[1] * d3_radians); |
michael@0 | 4311 | return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; |
michael@0 | 4312 | }; |
michael@0 | 4313 | return forward; |
michael@0 | 4314 | }; |
michael@0 | 4315 | function d3_geo_identityRotation(λ, φ) { |
michael@0 | 4316 | return [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; |
michael@0 | 4317 | } |
michael@0 | 4318 | d3_geo_identityRotation.invert = d3_geo_equirectangular; |
michael@0 | 4319 | function d3_geo_rotation(δλ, δφ, δγ) { |
michael@0 | 4320 | return δλ ? δφ || δγ ? d3_geo_compose(d3_geo_rotationλ(δλ), d3_geo_rotationφγ(δφ, δγ)) : d3_geo_rotationλ(δλ) : δφ || δγ ? d3_geo_rotationφγ(δφ, δγ) : d3_geo_identityRotation; |
michael@0 | 4321 | } |
michael@0 | 4322 | function d3_geo_forwardRotationλ(δλ) { |
michael@0 | 4323 | return function(λ, φ) { |
michael@0 | 4324 | return λ += δλ, [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; |
michael@0 | 4325 | }; |
michael@0 | 4326 | } |
michael@0 | 4327 | function d3_geo_rotationλ(δλ) { |
michael@0 | 4328 | var rotation = d3_geo_forwardRotationλ(δλ); |
michael@0 | 4329 | rotation.invert = d3_geo_forwardRotationλ(-δλ); |
michael@0 | 4330 | return rotation; |
michael@0 | 4331 | } |
michael@0 | 4332 | function d3_geo_rotationφγ(δφ, δγ) { |
michael@0 | 4333 | var cosδφ = Math.cos(δφ), sinδφ = Math.sin(δφ), cosδγ = Math.cos(δγ), sinδγ = Math.sin(δγ); |
michael@0 | 4334 | function rotation(λ, φ) { |
michael@0 | 4335 | var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδφ + x * sinδφ; |
michael@0 | 4336 | return [ Math.atan2(y * cosδγ - k * sinδγ, x * cosδφ - z * sinδφ), d3_asin(k * cosδγ + y * sinδγ) ]; |
michael@0 | 4337 | } |
michael@0 | 4338 | rotation.invert = function(λ, φ) { |
michael@0 | 4339 | var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδγ - y * sinδγ; |
michael@0 | 4340 | return [ Math.atan2(y * cosδγ + z * sinδγ, x * cosδφ + k * sinδφ), d3_asin(k * cosδφ - x * sinδφ) ]; |
michael@0 | 4341 | }; |
michael@0 | 4342 | return rotation; |
michael@0 | 4343 | } |
michael@0 | 4344 | d3.geo.circle = function() { |
michael@0 | 4345 | var origin = [ 0, 0 ], angle, precision = 6, interpolate; |
michael@0 | 4346 | function circle() { |
michael@0 | 4347 | var center = typeof origin === "function" ? origin.apply(this, arguments) : origin, rotate = d3_geo_rotation(-center[0] * d3_radians, -center[1] * d3_radians, 0).invert, ring = []; |
michael@0 | 4348 | interpolate(null, null, 1, { |
michael@0 | 4349 | point: function(x, y) { |
michael@0 | 4350 | ring.push(x = rotate(x, y)); |
michael@0 | 4351 | x[0] *= d3_degrees, x[1] *= d3_degrees; |
michael@0 | 4352 | } |
michael@0 | 4353 | }); |
michael@0 | 4354 | return { |
michael@0 | 4355 | type: "Polygon", |
michael@0 | 4356 | coordinates: [ ring ] |
michael@0 | 4357 | }; |
michael@0 | 4358 | } |
michael@0 | 4359 | circle.origin = function(x) { |
michael@0 | 4360 | if (!arguments.length) return origin; |
michael@0 | 4361 | origin = x; |
michael@0 | 4362 | return circle; |
michael@0 | 4363 | }; |
michael@0 | 4364 | circle.angle = function(x) { |
michael@0 | 4365 | if (!arguments.length) return angle; |
michael@0 | 4366 | interpolate = d3_geo_circleInterpolate((angle = +x) * d3_radians, precision * d3_radians); |
michael@0 | 4367 | return circle; |
michael@0 | 4368 | }; |
michael@0 | 4369 | circle.precision = function(_) { |
michael@0 | 4370 | if (!arguments.length) return precision; |
michael@0 | 4371 | interpolate = d3_geo_circleInterpolate(angle * d3_radians, (precision = +_) * d3_radians); |
michael@0 | 4372 | return circle; |
michael@0 | 4373 | }; |
michael@0 | 4374 | return circle.angle(90); |
michael@0 | 4375 | }; |
michael@0 | 4376 | function d3_geo_circleInterpolate(radius, precision) { |
michael@0 | 4377 | var cr = Math.cos(radius), sr = Math.sin(radius); |
michael@0 | 4378 | return function(from, to, direction, listener) { |
michael@0 | 4379 | var step = direction * precision; |
michael@0 | 4380 | if (from != null) { |
michael@0 | 4381 | from = d3_geo_circleAngle(cr, from); |
michael@0 | 4382 | to = d3_geo_circleAngle(cr, to); |
michael@0 | 4383 | if (direction > 0 ? from < to : from > to) from += direction * τ; |
michael@0 | 4384 | } else { |
michael@0 | 4385 | from = radius + direction * τ; |
michael@0 | 4386 | to = radius - .5 * step; |
michael@0 | 4387 | } |
michael@0 | 4388 | for (var point, t = from; direction > 0 ? t > to : t < to; t -= step) { |
michael@0 | 4389 | listener.point((point = d3_geo_spherical([ cr, -sr * Math.cos(t), -sr * Math.sin(t) ]))[0], point[1]); |
michael@0 | 4390 | } |
michael@0 | 4391 | }; |
michael@0 | 4392 | } |
michael@0 | 4393 | function d3_geo_circleAngle(cr, point) { |
michael@0 | 4394 | var a = d3_geo_cartesian(point); |
michael@0 | 4395 | a[0] -= cr; |
michael@0 | 4396 | d3_geo_cartesianNormalize(a); |
michael@0 | 4397 | var angle = d3_acos(-a[1]); |
michael@0 | 4398 | return ((-a[2] < 0 ? -angle : angle) + 2 * Math.PI - ε) % (2 * Math.PI); |
michael@0 | 4399 | } |
michael@0 | 4400 | d3.geo.distance = function(a, b) { |
michael@0 | 4401 | var Δλ = (b[0] - a[0]) * d3_radians, φ0 = a[1] * d3_radians, φ1 = b[1] * d3_radians, sinΔλ = Math.sin(Δλ), cosΔλ = Math.cos(Δλ), sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), sinφ1 = Math.sin(φ1), cosφ1 = Math.cos(φ1), t; |
michael@0 | 4402 | return Math.atan2(Math.sqrt((t = cosφ1 * sinΔλ) * t + (t = cosφ0 * sinφ1 - sinφ0 * cosφ1 * cosΔλ) * t), sinφ0 * sinφ1 + cosφ0 * cosφ1 * cosΔλ); |
michael@0 | 4403 | }; |
michael@0 | 4404 | d3.geo.graticule = function() { |
michael@0 | 4405 | var x1, x0, X1, X0, y1, y0, Y1, Y0, dx = 10, dy = dx, DX = 90, DY = 360, x, y, X, Y, precision = 2.5; |
michael@0 | 4406 | function graticule() { |
michael@0 | 4407 | return { |
michael@0 | 4408 | type: "MultiLineString", |
michael@0 | 4409 | coordinates: lines() |
michael@0 | 4410 | }; |
michael@0 | 4411 | } |
michael@0 | 4412 | function lines() { |
michael@0 | 4413 | return d3.range(Math.ceil(X0 / DX) * DX, X1, DX).map(X).concat(d3.range(Math.ceil(Y0 / DY) * DY, Y1, DY).map(Y)).concat(d3.range(Math.ceil(x0 / dx) * dx, x1, dx).filter(function(x) { |
michael@0 | 4414 | return abs(x % DX) > ε; |
michael@0 | 4415 | }).map(x)).concat(d3.range(Math.ceil(y0 / dy) * dy, y1, dy).filter(function(y) { |
michael@0 | 4416 | return abs(y % DY) > ε; |
michael@0 | 4417 | }).map(y)); |
michael@0 | 4418 | } |
michael@0 | 4419 | graticule.lines = function() { |
michael@0 | 4420 | return lines().map(function(coordinates) { |
michael@0 | 4421 | return { |
michael@0 | 4422 | type: "LineString", |
michael@0 | 4423 | coordinates: coordinates |
michael@0 | 4424 | }; |
michael@0 | 4425 | }); |
michael@0 | 4426 | }; |
michael@0 | 4427 | graticule.outline = function() { |
michael@0 | 4428 | return { |
michael@0 | 4429 | type: "Polygon", |
michael@0 | 4430 | coordinates: [ X(X0).concat(Y(Y1).slice(1), X(X1).reverse().slice(1), Y(Y0).reverse().slice(1)) ] |
michael@0 | 4431 | }; |
michael@0 | 4432 | }; |
michael@0 | 4433 | graticule.extent = function(_) { |
michael@0 | 4434 | if (!arguments.length) return graticule.minorExtent(); |
michael@0 | 4435 | return graticule.majorExtent(_).minorExtent(_); |
michael@0 | 4436 | }; |
michael@0 | 4437 | graticule.majorExtent = function(_) { |
michael@0 | 4438 | if (!arguments.length) return [ [ X0, Y0 ], [ X1, Y1 ] ]; |
michael@0 | 4439 | X0 = +_[0][0], X1 = +_[1][0]; |
michael@0 | 4440 | Y0 = +_[0][1], Y1 = +_[1][1]; |
michael@0 | 4441 | if (X0 > X1) _ = X0, X0 = X1, X1 = _; |
michael@0 | 4442 | if (Y0 > Y1) _ = Y0, Y0 = Y1, Y1 = _; |
michael@0 | 4443 | return graticule.precision(precision); |
michael@0 | 4444 | }; |
michael@0 | 4445 | graticule.minorExtent = function(_) { |
michael@0 | 4446 | if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; |
michael@0 | 4447 | x0 = +_[0][0], x1 = +_[1][0]; |
michael@0 | 4448 | y0 = +_[0][1], y1 = +_[1][1]; |
michael@0 | 4449 | if (x0 > x1) _ = x0, x0 = x1, x1 = _; |
michael@0 | 4450 | if (y0 > y1) _ = y0, y0 = y1, y1 = _; |
michael@0 | 4451 | return graticule.precision(precision); |
michael@0 | 4452 | }; |
michael@0 | 4453 | graticule.step = function(_) { |
michael@0 | 4454 | if (!arguments.length) return graticule.minorStep(); |
michael@0 | 4455 | return graticule.majorStep(_).minorStep(_); |
michael@0 | 4456 | }; |
michael@0 | 4457 | graticule.majorStep = function(_) { |
michael@0 | 4458 | if (!arguments.length) return [ DX, DY ]; |
michael@0 | 4459 | DX = +_[0], DY = +_[1]; |
michael@0 | 4460 | return graticule; |
michael@0 | 4461 | }; |
michael@0 | 4462 | graticule.minorStep = function(_) { |
michael@0 | 4463 | if (!arguments.length) return [ dx, dy ]; |
michael@0 | 4464 | dx = +_[0], dy = +_[1]; |
michael@0 | 4465 | return graticule; |
michael@0 | 4466 | }; |
michael@0 | 4467 | graticule.precision = function(_) { |
michael@0 | 4468 | if (!arguments.length) return precision; |
michael@0 | 4469 | precision = +_; |
michael@0 | 4470 | x = d3_geo_graticuleX(y0, y1, 90); |
michael@0 | 4471 | y = d3_geo_graticuleY(x0, x1, precision); |
michael@0 | 4472 | X = d3_geo_graticuleX(Y0, Y1, 90); |
michael@0 | 4473 | Y = d3_geo_graticuleY(X0, X1, precision); |
michael@0 | 4474 | return graticule; |
michael@0 | 4475 | }; |
michael@0 | 4476 | return graticule.majorExtent([ [ -180, -90 + ε ], [ 180, 90 - ε ] ]).minorExtent([ [ -180, -80 - ε ], [ 180, 80 + ε ] ]); |
michael@0 | 4477 | }; |
michael@0 | 4478 | function d3_geo_graticuleX(y0, y1, dy) { |
michael@0 | 4479 | var y = d3.range(y0, y1 - ε, dy).concat(y1); |
michael@0 | 4480 | return function(x) { |
michael@0 | 4481 | return y.map(function(y) { |
michael@0 | 4482 | return [ x, y ]; |
michael@0 | 4483 | }); |
michael@0 | 4484 | }; |
michael@0 | 4485 | } |
michael@0 | 4486 | function d3_geo_graticuleY(x0, x1, dx) { |
michael@0 | 4487 | var x = d3.range(x0, x1 - ε, dx).concat(x1); |
michael@0 | 4488 | return function(y) { |
michael@0 | 4489 | return x.map(function(x) { |
michael@0 | 4490 | return [ x, y ]; |
michael@0 | 4491 | }); |
michael@0 | 4492 | }; |
michael@0 | 4493 | } |
michael@0 | 4494 | function d3_source(d) { |
michael@0 | 4495 | return d.source; |
michael@0 | 4496 | } |
michael@0 | 4497 | function d3_target(d) { |
michael@0 | 4498 | return d.target; |
michael@0 | 4499 | } |
michael@0 | 4500 | d3.geo.greatArc = function() { |
michael@0 | 4501 | var source = d3_source, source_, target = d3_target, target_; |
michael@0 | 4502 | function greatArc() { |
michael@0 | 4503 | return { |
michael@0 | 4504 | type: "LineString", |
michael@0 | 4505 | coordinates: [ source_ || source.apply(this, arguments), target_ || target.apply(this, arguments) ] |
michael@0 | 4506 | }; |
michael@0 | 4507 | } |
michael@0 | 4508 | greatArc.distance = function() { |
michael@0 | 4509 | return d3.geo.distance(source_ || source.apply(this, arguments), target_ || target.apply(this, arguments)); |
michael@0 | 4510 | }; |
michael@0 | 4511 | greatArc.source = function(_) { |
michael@0 | 4512 | if (!arguments.length) return source; |
michael@0 | 4513 | source = _, source_ = typeof _ === "function" ? null : _; |
michael@0 | 4514 | return greatArc; |
michael@0 | 4515 | }; |
michael@0 | 4516 | greatArc.target = function(_) { |
michael@0 | 4517 | if (!arguments.length) return target; |
michael@0 | 4518 | target = _, target_ = typeof _ === "function" ? null : _; |
michael@0 | 4519 | return greatArc; |
michael@0 | 4520 | }; |
michael@0 | 4521 | greatArc.precision = function() { |
michael@0 | 4522 | return arguments.length ? greatArc : 0; |
michael@0 | 4523 | }; |
michael@0 | 4524 | return greatArc; |
michael@0 | 4525 | }; |
michael@0 | 4526 | d3.geo.interpolate = function(source, target) { |
michael@0 | 4527 | return d3_geo_interpolate(source[0] * d3_radians, source[1] * d3_radians, target[0] * d3_radians, target[1] * d3_radians); |
michael@0 | 4528 | }; |
michael@0 | 4529 | function d3_geo_interpolate(x0, y0, x1, y1) { |
michael@0 | 4530 | var cy0 = Math.cos(y0), sy0 = Math.sin(y0), cy1 = Math.cos(y1), sy1 = Math.sin(y1), kx0 = cy0 * Math.cos(x0), ky0 = cy0 * Math.sin(x0), kx1 = cy1 * Math.cos(x1), ky1 = cy1 * Math.sin(x1), d = 2 * Math.asin(Math.sqrt(d3_haversin(y1 - y0) + cy0 * cy1 * d3_haversin(x1 - x0))), k = 1 / Math.sin(d); |
michael@0 | 4531 | var interpolate = d ? function(t) { |
michael@0 | 4532 | var B = Math.sin(t *= d) * k, A = Math.sin(d - t) * k, x = A * kx0 + B * kx1, y = A * ky0 + B * ky1, z = A * sy0 + B * sy1; |
michael@0 | 4533 | return [ Math.atan2(y, x) * d3_degrees, Math.atan2(z, Math.sqrt(x * x + y * y)) * d3_degrees ]; |
michael@0 | 4534 | } : function() { |
michael@0 | 4535 | return [ x0 * d3_degrees, y0 * d3_degrees ]; |
michael@0 | 4536 | }; |
michael@0 | 4537 | interpolate.distance = d; |
michael@0 | 4538 | return interpolate; |
michael@0 | 4539 | } |
michael@0 | 4540 | d3.geo.length = function(object) { |
michael@0 | 4541 | d3_geo_lengthSum = 0; |
michael@0 | 4542 | d3.geo.stream(object, d3_geo_length); |
michael@0 | 4543 | return d3_geo_lengthSum; |
michael@0 | 4544 | }; |
michael@0 | 4545 | var d3_geo_lengthSum; |
michael@0 | 4546 | var d3_geo_length = { |
michael@0 | 4547 | sphere: d3_noop, |
michael@0 | 4548 | point: d3_noop, |
michael@0 | 4549 | lineStart: d3_geo_lengthLineStart, |
michael@0 | 4550 | lineEnd: d3_noop, |
michael@0 | 4551 | polygonStart: d3_noop, |
michael@0 | 4552 | polygonEnd: d3_noop |
michael@0 | 4553 | }; |
michael@0 | 4554 | function d3_geo_lengthLineStart() { |
michael@0 | 4555 | var λ0, sinφ0, cosφ0; |
michael@0 | 4556 | d3_geo_length.point = function(λ, φ) { |
michael@0 | 4557 | λ0 = λ * d3_radians, sinφ0 = Math.sin(φ *= d3_radians), cosφ0 = Math.cos(φ); |
michael@0 | 4558 | d3_geo_length.point = nextPoint; |
michael@0 | 4559 | }; |
michael@0 | 4560 | d3_geo_length.lineEnd = function() { |
michael@0 | 4561 | d3_geo_length.point = d3_geo_length.lineEnd = d3_noop; |
michael@0 | 4562 | }; |
michael@0 | 4563 | function nextPoint(λ, φ) { |
michael@0 | 4564 | var sinφ = Math.sin(φ *= d3_radians), cosφ = Math.cos(φ), t = abs((λ *= d3_radians) - λ0), cosΔλ = Math.cos(t); |
michael@0 | 4565 | d3_geo_lengthSum += Math.atan2(Math.sqrt((t = cosφ * Math.sin(t)) * t + (t = cosφ0 * sinφ - sinφ0 * cosφ * cosΔλ) * t), sinφ0 * sinφ + cosφ0 * cosφ * cosΔλ); |
michael@0 | 4566 | λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ; |
michael@0 | 4567 | } |
michael@0 | 4568 | } |
michael@0 | 4569 | function d3_geo_azimuthal(scale, angle) { |
michael@0 | 4570 | function azimuthal(λ, φ) { |
michael@0 | 4571 | var cosλ = Math.cos(λ), cosφ = Math.cos(φ), k = scale(cosλ * cosφ); |
michael@0 | 4572 | return [ k * cosφ * Math.sin(λ), k * Math.sin(φ) ]; |
michael@0 | 4573 | } |
michael@0 | 4574 | azimuthal.invert = function(x, y) { |
michael@0 | 4575 | var ρ = Math.sqrt(x * x + y * y), c = angle(ρ), sinc = Math.sin(c), cosc = Math.cos(c); |
michael@0 | 4576 | return [ Math.atan2(x * sinc, ρ * cosc), Math.asin(ρ && y * sinc / ρ) ]; |
michael@0 | 4577 | }; |
michael@0 | 4578 | return azimuthal; |
michael@0 | 4579 | } |
michael@0 | 4580 | var d3_geo_azimuthalEqualArea = d3_geo_azimuthal(function(cosλcosφ) { |
michael@0 | 4581 | return Math.sqrt(2 / (1 + cosλcosφ)); |
michael@0 | 4582 | }, function(ρ) { |
michael@0 | 4583 | return 2 * Math.asin(ρ / 2); |
michael@0 | 4584 | }); |
michael@0 | 4585 | (d3.geo.azimuthalEqualArea = function() { |
michael@0 | 4586 | return d3_geo_projection(d3_geo_azimuthalEqualArea); |
michael@0 | 4587 | }).raw = d3_geo_azimuthalEqualArea; |
michael@0 | 4588 | var d3_geo_azimuthalEquidistant = d3_geo_azimuthal(function(cosλcosφ) { |
michael@0 | 4589 | var c = Math.acos(cosλcosφ); |
michael@0 | 4590 | return c && c / Math.sin(c); |
michael@0 | 4591 | }, d3_identity); |
michael@0 | 4592 | (d3.geo.azimuthalEquidistant = function() { |
michael@0 | 4593 | return d3_geo_projection(d3_geo_azimuthalEquidistant); |
michael@0 | 4594 | }).raw = d3_geo_azimuthalEquidistant; |
michael@0 | 4595 | function d3_geo_conicConformal(φ0, φ1) { |
michael@0 | 4596 | var cosφ0 = Math.cos(φ0), t = function(φ) { |
michael@0 | 4597 | return Math.tan(π / 4 + φ / 2); |
michael@0 | 4598 | }, n = φ0 === φ1 ? Math.sin(φ0) : Math.log(cosφ0 / Math.cos(φ1)) / Math.log(t(φ1) / t(φ0)), F = cosφ0 * Math.pow(t(φ0), n) / n; |
michael@0 | 4599 | if (!n) return d3_geo_mercator; |
michael@0 | 4600 | function forward(λ, φ) { |
michael@0 | 4601 | var ρ = abs(abs(φ) - halfπ) < ε ? 0 : F / Math.pow(t(φ), n); |
michael@0 | 4602 | return [ ρ * Math.sin(n * λ), F - ρ * Math.cos(n * λ) ]; |
michael@0 | 4603 | } |
michael@0 | 4604 | forward.invert = function(x, y) { |
michael@0 | 4605 | var ρ0_y = F - y, ρ = d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y); |
michael@0 | 4606 | return [ Math.atan2(x, ρ0_y) / n, 2 * Math.atan(Math.pow(F / ρ, 1 / n)) - halfπ ]; |
michael@0 | 4607 | }; |
michael@0 | 4608 | return forward; |
michael@0 | 4609 | } |
michael@0 | 4610 | (d3.geo.conicConformal = function() { |
michael@0 | 4611 | return d3_geo_conic(d3_geo_conicConformal); |
michael@0 | 4612 | }).raw = d3_geo_conicConformal; |
michael@0 | 4613 | function d3_geo_conicEquidistant(φ0, φ1) { |
michael@0 | 4614 | var cosφ0 = Math.cos(φ0), n = φ0 === φ1 ? Math.sin(φ0) : (cosφ0 - Math.cos(φ1)) / (φ1 - φ0), G = cosφ0 / n + φ0; |
michael@0 | 4615 | if (abs(n) < ε) return d3_geo_equirectangular; |
michael@0 | 4616 | function forward(λ, φ) { |
michael@0 | 4617 | var ρ = G - φ; |
michael@0 | 4618 | return [ ρ * Math.sin(n * λ), G - ρ * Math.cos(n * λ) ]; |
michael@0 | 4619 | } |
michael@0 | 4620 | forward.invert = function(x, y) { |
michael@0 | 4621 | var ρ0_y = G - y; |
michael@0 | 4622 | return [ Math.atan2(x, ρ0_y) / n, G - d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y) ]; |
michael@0 | 4623 | }; |
michael@0 | 4624 | return forward; |
michael@0 | 4625 | } |
michael@0 | 4626 | (d3.geo.conicEquidistant = function() { |
michael@0 | 4627 | return d3_geo_conic(d3_geo_conicEquidistant); |
michael@0 | 4628 | }).raw = d3_geo_conicEquidistant; |
michael@0 | 4629 | var d3_geo_gnomonic = d3_geo_azimuthal(function(cosλcosφ) { |
michael@0 | 4630 | return 1 / cosλcosφ; |
michael@0 | 4631 | }, Math.atan); |
michael@0 | 4632 | (d3.geo.gnomonic = function() { |
michael@0 | 4633 | return d3_geo_projection(d3_geo_gnomonic); |
michael@0 | 4634 | }).raw = d3_geo_gnomonic; |
michael@0 | 4635 | function d3_geo_mercator(λ, φ) { |
michael@0 | 4636 | return [ λ, Math.log(Math.tan(π / 4 + φ / 2)) ]; |
michael@0 | 4637 | } |
michael@0 | 4638 | d3_geo_mercator.invert = function(x, y) { |
michael@0 | 4639 | return [ x, 2 * Math.atan(Math.exp(y)) - halfπ ]; |
michael@0 | 4640 | }; |
michael@0 | 4641 | function d3_geo_mercatorProjection(project) { |
michael@0 | 4642 | var m = d3_geo_projection(project), scale = m.scale, translate = m.translate, clipExtent = m.clipExtent, clipAuto; |
michael@0 | 4643 | m.scale = function() { |
michael@0 | 4644 | var v = scale.apply(m, arguments); |
michael@0 | 4645 | return v === m ? clipAuto ? m.clipExtent(null) : m : v; |
michael@0 | 4646 | }; |
michael@0 | 4647 | m.translate = function() { |
michael@0 | 4648 | var v = translate.apply(m, arguments); |
michael@0 | 4649 | return v === m ? clipAuto ? m.clipExtent(null) : m : v; |
michael@0 | 4650 | }; |
michael@0 | 4651 | m.clipExtent = function(_) { |
michael@0 | 4652 | var v = clipExtent.apply(m, arguments); |
michael@0 | 4653 | if (v === m) { |
michael@0 | 4654 | if (clipAuto = _ == null) { |
michael@0 | 4655 | var k = π * scale(), t = translate(); |
michael@0 | 4656 | clipExtent([ [ t[0] - k, t[1] - k ], [ t[0] + k, t[1] + k ] ]); |
michael@0 | 4657 | } |
michael@0 | 4658 | } else if (clipAuto) { |
michael@0 | 4659 | v = null; |
michael@0 | 4660 | } |
michael@0 | 4661 | return v; |
michael@0 | 4662 | }; |
michael@0 | 4663 | return m.clipExtent(null); |
michael@0 | 4664 | } |
michael@0 | 4665 | (d3.geo.mercator = function() { |
michael@0 | 4666 | return d3_geo_mercatorProjection(d3_geo_mercator); |
michael@0 | 4667 | }).raw = d3_geo_mercator; |
michael@0 | 4668 | var d3_geo_orthographic = d3_geo_azimuthal(function() { |
michael@0 | 4669 | return 1; |
michael@0 | 4670 | }, Math.asin); |
michael@0 | 4671 | (d3.geo.orthographic = function() { |
michael@0 | 4672 | return d3_geo_projection(d3_geo_orthographic); |
michael@0 | 4673 | }).raw = d3_geo_orthographic; |
michael@0 | 4674 | var d3_geo_stereographic = d3_geo_azimuthal(function(cosλcosφ) { |
michael@0 | 4675 | return 1 / (1 + cosλcosφ); |
michael@0 | 4676 | }, function(ρ) { |
michael@0 | 4677 | return 2 * Math.atan(ρ); |
michael@0 | 4678 | }); |
michael@0 | 4679 | (d3.geo.stereographic = function() { |
michael@0 | 4680 | return d3_geo_projection(d3_geo_stereographic); |
michael@0 | 4681 | }).raw = d3_geo_stereographic; |
michael@0 | 4682 | function d3_geo_transverseMercator(λ, φ) { |
michael@0 | 4683 | return [ Math.log(Math.tan(π / 4 + φ / 2)), -λ ]; |
michael@0 | 4684 | } |
michael@0 | 4685 | d3_geo_transverseMercator.invert = function(x, y) { |
michael@0 | 4686 | return [ -y, 2 * Math.atan(Math.exp(x)) - halfπ ]; |
michael@0 | 4687 | }; |
michael@0 | 4688 | (d3.geo.transverseMercator = function() { |
michael@0 | 4689 | var projection = d3_geo_mercatorProjection(d3_geo_transverseMercator), center = projection.center, rotate = projection.rotate; |
michael@0 | 4690 | projection.center = function(_) { |
michael@0 | 4691 | return _ ? center([ -_[1], _[0] ]) : (_ = center(), [ -_[1], _[0] ]); |
michael@0 | 4692 | }; |
michael@0 | 4693 | projection.rotate = function(_) { |
michael@0 | 4694 | return _ ? rotate([ _[0], _[1], _.length > 2 ? _[2] + 90 : 90 ]) : (_ = rotate(), |
michael@0 | 4695 | [ _[0], _[1], _[2] - 90 ]); |
michael@0 | 4696 | }; |
michael@0 | 4697 | return projection.rotate([ 0, 0 ]); |
michael@0 | 4698 | }).raw = d3_geo_transverseMercator; |
michael@0 | 4699 | d3.geom = {}; |
michael@0 | 4700 | function d3_geom_pointX(d) { |
michael@0 | 4701 | return d[0]; |
michael@0 | 4702 | } |
michael@0 | 4703 | function d3_geom_pointY(d) { |
michael@0 | 4704 | return d[1]; |
michael@0 | 4705 | } |
michael@0 | 4706 | d3.geom.hull = function(vertices) { |
michael@0 | 4707 | var x = d3_geom_pointX, y = d3_geom_pointY; |
michael@0 | 4708 | if (arguments.length) return hull(vertices); |
michael@0 | 4709 | function hull(data) { |
michael@0 | 4710 | if (data.length < 3) return []; |
michael@0 | 4711 | var fx = d3_functor(x), fy = d3_functor(y), i, n = data.length, points = [], flippedPoints = []; |
michael@0 | 4712 | for (i = 0; i < n; i++) { |
michael@0 | 4713 | points.push([ +fx.call(this, data[i], i), +fy.call(this, data[i], i), i ]); |
michael@0 | 4714 | } |
michael@0 | 4715 | points.sort(d3_geom_hullOrder); |
michael@0 | 4716 | for (i = 0; i < n; i++) flippedPoints.push([ points[i][0], -points[i][1] ]); |
michael@0 | 4717 | var upper = d3_geom_hullUpper(points), lower = d3_geom_hullUpper(flippedPoints); |
michael@0 | 4718 | var skipLeft = lower[0] === upper[0], skipRight = lower[lower.length - 1] === upper[upper.length - 1], polygon = []; |
michael@0 | 4719 | for (i = upper.length - 1; i >= 0; --i) polygon.push(data[points[upper[i]][2]]); |
michael@0 | 4720 | for (i = +skipLeft; i < lower.length - skipRight; ++i) polygon.push(data[points[lower[i]][2]]); |
michael@0 | 4721 | return polygon; |
michael@0 | 4722 | } |
michael@0 | 4723 | hull.x = function(_) { |
michael@0 | 4724 | return arguments.length ? (x = _, hull) : x; |
michael@0 | 4725 | }; |
michael@0 | 4726 | hull.y = function(_) { |
michael@0 | 4727 | return arguments.length ? (y = _, hull) : y; |
michael@0 | 4728 | }; |
michael@0 | 4729 | return hull; |
michael@0 | 4730 | }; |
michael@0 | 4731 | function d3_geom_hullUpper(points) { |
michael@0 | 4732 | var n = points.length, hull = [ 0, 1 ], hs = 2; |
michael@0 | 4733 | for (var i = 2; i < n; i++) { |
michael@0 | 4734 | while (hs > 1 && d3_cross2d(points[hull[hs - 2]], points[hull[hs - 1]], points[i]) <= 0) --hs; |
michael@0 | 4735 | hull[hs++] = i; |
michael@0 | 4736 | } |
michael@0 | 4737 | return hull.slice(0, hs); |
michael@0 | 4738 | } |
michael@0 | 4739 | function d3_geom_hullOrder(a, b) { |
michael@0 | 4740 | return a[0] - b[0] || a[1] - b[1]; |
michael@0 | 4741 | } |
michael@0 | 4742 | d3.geom.polygon = function(coordinates) { |
michael@0 | 4743 | d3_subclass(coordinates, d3_geom_polygonPrototype); |
michael@0 | 4744 | return coordinates; |
michael@0 | 4745 | }; |
michael@0 | 4746 | var d3_geom_polygonPrototype = d3.geom.polygon.prototype = []; |
michael@0 | 4747 | d3_geom_polygonPrototype.area = function() { |
michael@0 | 4748 | var i = -1, n = this.length, a, b = this[n - 1], area = 0; |
michael@0 | 4749 | while (++i < n) { |
michael@0 | 4750 | a = b; |
michael@0 | 4751 | b = this[i]; |
michael@0 | 4752 | area += a[1] * b[0] - a[0] * b[1]; |
michael@0 | 4753 | } |
michael@0 | 4754 | return area * .5; |
michael@0 | 4755 | }; |
michael@0 | 4756 | d3_geom_polygonPrototype.centroid = function(k) { |
michael@0 | 4757 | var i = -1, n = this.length, x = 0, y = 0, a, b = this[n - 1], c; |
michael@0 | 4758 | if (!arguments.length) k = -1 / (6 * this.area()); |
michael@0 | 4759 | while (++i < n) { |
michael@0 | 4760 | a = b; |
michael@0 | 4761 | b = this[i]; |
michael@0 | 4762 | c = a[0] * b[1] - b[0] * a[1]; |
michael@0 | 4763 | x += (a[0] + b[0]) * c; |
michael@0 | 4764 | y += (a[1] + b[1]) * c; |
michael@0 | 4765 | } |
michael@0 | 4766 | return [ x * k, y * k ]; |
michael@0 | 4767 | }; |
michael@0 | 4768 | d3_geom_polygonPrototype.clip = function(subject) { |
michael@0 | 4769 | var input, closed = d3_geom_polygonClosed(subject), i = -1, n = this.length - d3_geom_polygonClosed(this), j, m, a = this[n - 1], b, c, d; |
michael@0 | 4770 | while (++i < n) { |
michael@0 | 4771 | input = subject.slice(); |
michael@0 | 4772 | subject.length = 0; |
michael@0 | 4773 | b = this[i]; |
michael@0 | 4774 | c = input[(m = input.length - closed) - 1]; |
michael@0 | 4775 | j = -1; |
michael@0 | 4776 | while (++j < m) { |
michael@0 | 4777 | d = input[j]; |
michael@0 | 4778 | if (d3_geom_polygonInside(d, a, b)) { |
michael@0 | 4779 | if (!d3_geom_polygonInside(c, a, b)) { |
michael@0 | 4780 | subject.push(d3_geom_polygonIntersect(c, d, a, b)); |
michael@0 | 4781 | } |
michael@0 | 4782 | subject.push(d); |
michael@0 | 4783 | } else if (d3_geom_polygonInside(c, a, b)) { |
michael@0 | 4784 | subject.push(d3_geom_polygonIntersect(c, d, a, b)); |
michael@0 | 4785 | } |
michael@0 | 4786 | c = d; |
michael@0 | 4787 | } |
michael@0 | 4788 | if (closed) subject.push(subject[0]); |
michael@0 | 4789 | a = b; |
michael@0 | 4790 | } |
michael@0 | 4791 | return subject; |
michael@0 | 4792 | }; |
michael@0 | 4793 | function d3_geom_polygonInside(p, a, b) { |
michael@0 | 4794 | return (b[0] - a[0]) * (p[1] - a[1]) < (b[1] - a[1]) * (p[0] - a[0]); |
michael@0 | 4795 | } |
michael@0 | 4796 | function d3_geom_polygonIntersect(c, d, a, b) { |
michael@0 | 4797 | var x1 = c[0], x3 = a[0], x21 = d[0] - x1, x43 = b[0] - x3, y1 = c[1], y3 = a[1], y21 = d[1] - y1, y43 = b[1] - y3, ua = (x43 * (y1 - y3) - y43 * (x1 - x3)) / (y43 * x21 - x43 * y21); |
michael@0 | 4798 | return [ x1 + ua * x21, y1 + ua * y21 ]; |
michael@0 | 4799 | } |
michael@0 | 4800 | function d3_geom_polygonClosed(coordinates) { |
michael@0 | 4801 | var a = coordinates[0], b = coordinates[coordinates.length - 1]; |
michael@0 | 4802 | return !(a[0] - b[0] || a[1] - b[1]); |
michael@0 | 4803 | } |
michael@0 | 4804 | var d3_geom_voronoiEdges, d3_geom_voronoiCells, d3_geom_voronoiBeaches, d3_geom_voronoiBeachPool = [], d3_geom_voronoiFirstCircle, d3_geom_voronoiCircles, d3_geom_voronoiCirclePool = []; |
michael@0 | 4805 | function d3_geom_voronoiBeach() { |
michael@0 | 4806 | d3_geom_voronoiRedBlackNode(this); |
michael@0 | 4807 | this.edge = this.site = this.circle = null; |
michael@0 | 4808 | } |
michael@0 | 4809 | function d3_geom_voronoiCreateBeach(site) { |
michael@0 | 4810 | var beach = d3_geom_voronoiBeachPool.pop() || new d3_geom_voronoiBeach(); |
michael@0 | 4811 | beach.site = site; |
michael@0 | 4812 | return beach; |
michael@0 | 4813 | } |
michael@0 | 4814 | function d3_geom_voronoiDetachBeach(beach) { |
michael@0 | 4815 | d3_geom_voronoiDetachCircle(beach); |
michael@0 | 4816 | d3_geom_voronoiBeaches.remove(beach); |
michael@0 | 4817 | d3_geom_voronoiBeachPool.push(beach); |
michael@0 | 4818 | d3_geom_voronoiRedBlackNode(beach); |
michael@0 | 4819 | } |
michael@0 | 4820 | function d3_geom_voronoiRemoveBeach(beach) { |
michael@0 | 4821 | var circle = beach.circle, x = circle.x, y = circle.cy, vertex = { |
michael@0 | 4822 | x: x, |
michael@0 | 4823 | y: y |
michael@0 | 4824 | }, previous = beach.P, next = beach.N, disappearing = [ beach ]; |
michael@0 | 4825 | d3_geom_voronoiDetachBeach(beach); |
michael@0 | 4826 | var lArc = previous; |
michael@0 | 4827 | while (lArc.circle && abs(x - lArc.circle.x) < ε && abs(y - lArc.circle.cy) < ε) { |
michael@0 | 4828 | previous = lArc.P; |
michael@0 | 4829 | disappearing.unshift(lArc); |
michael@0 | 4830 | d3_geom_voronoiDetachBeach(lArc); |
michael@0 | 4831 | lArc = previous; |
michael@0 | 4832 | } |
michael@0 | 4833 | disappearing.unshift(lArc); |
michael@0 | 4834 | d3_geom_voronoiDetachCircle(lArc); |
michael@0 | 4835 | var rArc = next; |
michael@0 | 4836 | while (rArc.circle && abs(x - rArc.circle.x) < ε && abs(y - rArc.circle.cy) < ε) { |
michael@0 | 4837 | next = rArc.N; |
michael@0 | 4838 | disappearing.push(rArc); |
michael@0 | 4839 | d3_geom_voronoiDetachBeach(rArc); |
michael@0 | 4840 | rArc = next; |
michael@0 | 4841 | } |
michael@0 | 4842 | disappearing.push(rArc); |
michael@0 | 4843 | d3_geom_voronoiDetachCircle(rArc); |
michael@0 | 4844 | var nArcs = disappearing.length, iArc; |
michael@0 | 4845 | for (iArc = 1; iArc < nArcs; ++iArc) { |
michael@0 | 4846 | rArc = disappearing[iArc]; |
michael@0 | 4847 | lArc = disappearing[iArc - 1]; |
michael@0 | 4848 | d3_geom_voronoiSetEdgeEnd(rArc.edge, lArc.site, rArc.site, vertex); |
michael@0 | 4849 | } |
michael@0 | 4850 | lArc = disappearing[0]; |
michael@0 | 4851 | rArc = disappearing[nArcs - 1]; |
michael@0 | 4852 | rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, rArc.site, null, vertex); |
michael@0 | 4853 | d3_geom_voronoiAttachCircle(lArc); |
michael@0 | 4854 | d3_geom_voronoiAttachCircle(rArc); |
michael@0 | 4855 | } |
michael@0 | 4856 | function d3_geom_voronoiAddBeach(site) { |
michael@0 | 4857 | var x = site.x, directrix = site.y, lArc, rArc, dxl, dxr, node = d3_geom_voronoiBeaches._; |
michael@0 | 4858 | while (node) { |
michael@0 | 4859 | dxl = d3_geom_voronoiLeftBreakPoint(node, directrix) - x; |
michael@0 | 4860 | if (dxl > ε) node = node.L; else { |
michael@0 | 4861 | dxr = x - d3_geom_voronoiRightBreakPoint(node, directrix); |
michael@0 | 4862 | if (dxr > ε) { |
michael@0 | 4863 | if (!node.R) { |
michael@0 | 4864 | lArc = node; |
michael@0 | 4865 | break; |
michael@0 | 4866 | } |
michael@0 | 4867 | node = node.R; |
michael@0 | 4868 | } else { |
michael@0 | 4869 | if (dxl > -ε) { |
michael@0 | 4870 | lArc = node.P; |
michael@0 | 4871 | rArc = node; |
michael@0 | 4872 | } else if (dxr > -ε) { |
michael@0 | 4873 | lArc = node; |
michael@0 | 4874 | rArc = node.N; |
michael@0 | 4875 | } else { |
michael@0 | 4876 | lArc = rArc = node; |
michael@0 | 4877 | } |
michael@0 | 4878 | break; |
michael@0 | 4879 | } |
michael@0 | 4880 | } |
michael@0 | 4881 | } |
michael@0 | 4882 | var newArc = d3_geom_voronoiCreateBeach(site); |
michael@0 | 4883 | d3_geom_voronoiBeaches.insert(lArc, newArc); |
michael@0 | 4884 | if (!lArc && !rArc) return; |
michael@0 | 4885 | if (lArc === rArc) { |
michael@0 | 4886 | d3_geom_voronoiDetachCircle(lArc); |
michael@0 | 4887 | rArc = d3_geom_voronoiCreateBeach(lArc.site); |
michael@0 | 4888 | d3_geom_voronoiBeaches.insert(newArc, rArc); |
michael@0 | 4889 | newArc.edge = rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); |
michael@0 | 4890 | d3_geom_voronoiAttachCircle(lArc); |
michael@0 | 4891 | d3_geom_voronoiAttachCircle(rArc); |
michael@0 | 4892 | return; |
michael@0 | 4893 | } |
michael@0 | 4894 | if (!rArc) { |
michael@0 | 4895 | newArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); |
michael@0 | 4896 | return; |
michael@0 | 4897 | } |
michael@0 | 4898 | d3_geom_voronoiDetachCircle(lArc); |
michael@0 | 4899 | d3_geom_voronoiDetachCircle(rArc); |
michael@0 | 4900 | var lSite = lArc.site, ax = lSite.x, ay = lSite.y, bx = site.x - ax, by = site.y - ay, rSite = rArc.site, cx = rSite.x - ax, cy = rSite.y - ay, d = 2 * (bx * cy - by * cx), hb = bx * bx + by * by, hc = cx * cx + cy * cy, vertex = { |
michael@0 | 4901 | x: (cy * hb - by * hc) / d + ax, |
michael@0 | 4902 | y: (bx * hc - cx * hb) / d + ay |
michael@0 | 4903 | }; |
michael@0 | 4904 | d3_geom_voronoiSetEdgeEnd(rArc.edge, lSite, rSite, vertex); |
michael@0 | 4905 | newArc.edge = d3_geom_voronoiCreateEdge(lSite, site, null, vertex); |
michael@0 | 4906 | rArc.edge = d3_geom_voronoiCreateEdge(site, rSite, null, vertex); |
michael@0 | 4907 | d3_geom_voronoiAttachCircle(lArc); |
michael@0 | 4908 | d3_geom_voronoiAttachCircle(rArc); |
michael@0 | 4909 | } |
michael@0 | 4910 | function d3_geom_voronoiLeftBreakPoint(arc, directrix) { |
michael@0 | 4911 | var site = arc.site, rfocx = site.x, rfocy = site.y, pby2 = rfocy - directrix; |
michael@0 | 4912 | if (!pby2) return rfocx; |
michael@0 | 4913 | var lArc = arc.P; |
michael@0 | 4914 | if (!lArc) return -Infinity; |
michael@0 | 4915 | site = lArc.site; |
michael@0 | 4916 | var lfocx = site.x, lfocy = site.y, plby2 = lfocy - directrix; |
michael@0 | 4917 | if (!plby2) return lfocx; |
michael@0 | 4918 | var hl = lfocx - rfocx, aby2 = 1 / pby2 - 1 / plby2, b = hl / plby2; |
michael@0 | 4919 | if (aby2) return (-b + Math.sqrt(b * b - 2 * aby2 * (hl * hl / (-2 * plby2) - lfocy + plby2 / 2 + rfocy - pby2 / 2))) / aby2 + rfocx; |
michael@0 | 4920 | return (rfocx + lfocx) / 2; |
michael@0 | 4921 | } |
michael@0 | 4922 | function d3_geom_voronoiRightBreakPoint(arc, directrix) { |
michael@0 | 4923 | var rArc = arc.N; |
michael@0 | 4924 | if (rArc) return d3_geom_voronoiLeftBreakPoint(rArc, directrix); |
michael@0 | 4925 | var site = arc.site; |
michael@0 | 4926 | return site.y === directrix ? site.x : Infinity; |
michael@0 | 4927 | } |
michael@0 | 4928 | function d3_geom_voronoiCell(site) { |
michael@0 | 4929 | this.site = site; |
michael@0 | 4930 | this.edges = []; |
michael@0 | 4931 | } |
michael@0 | 4932 | d3_geom_voronoiCell.prototype.prepare = function() { |
michael@0 | 4933 | var halfEdges = this.edges, iHalfEdge = halfEdges.length, edge; |
michael@0 | 4934 | while (iHalfEdge--) { |
michael@0 | 4935 | edge = halfEdges[iHalfEdge].edge; |
michael@0 | 4936 | if (!edge.b || !edge.a) halfEdges.splice(iHalfEdge, 1); |
michael@0 | 4937 | } |
michael@0 | 4938 | halfEdges.sort(d3_geom_voronoiHalfEdgeOrder); |
michael@0 | 4939 | return halfEdges.length; |
michael@0 | 4940 | }; |
michael@0 | 4941 | function d3_geom_voronoiCloseCells(extent) { |
michael@0 | 4942 | var x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], x2, y2, x3, y3, cells = d3_geom_voronoiCells, iCell = cells.length, cell, iHalfEdge, halfEdges, nHalfEdges, start, end; |
michael@0 | 4943 | while (iCell--) { |
michael@0 | 4944 | cell = cells[iCell]; |
michael@0 | 4945 | if (!cell || !cell.prepare()) continue; |
michael@0 | 4946 | halfEdges = cell.edges; |
michael@0 | 4947 | nHalfEdges = halfEdges.length; |
michael@0 | 4948 | iHalfEdge = 0; |
michael@0 | 4949 | while (iHalfEdge < nHalfEdges) { |
michael@0 | 4950 | end = halfEdges[iHalfEdge].end(), x3 = end.x, y3 = end.y; |
michael@0 | 4951 | start = halfEdges[++iHalfEdge % nHalfEdges].start(), x2 = start.x, y2 = start.y; |
michael@0 | 4952 | if (abs(x3 - x2) > ε || abs(y3 - y2) > ε) { |
michael@0 | 4953 | halfEdges.splice(iHalfEdge, 0, new d3_geom_voronoiHalfEdge(d3_geom_voronoiCreateBorderEdge(cell.site, end, abs(x3 - x0) < ε && y1 - y3 > ε ? { |
michael@0 | 4954 | x: x0, |
michael@0 | 4955 | y: abs(x2 - x0) < ε ? y2 : y1 |
michael@0 | 4956 | } : abs(y3 - y1) < ε && x1 - x3 > ε ? { |
michael@0 | 4957 | x: abs(y2 - y1) < ε ? x2 : x1, |
michael@0 | 4958 | y: y1 |
michael@0 | 4959 | } : abs(x3 - x1) < ε && y3 - y0 > ε ? { |
michael@0 | 4960 | x: x1, |
michael@0 | 4961 | y: abs(x2 - x1) < ε ? y2 : y0 |
michael@0 | 4962 | } : abs(y3 - y0) < ε && x3 - x0 > ε ? { |
michael@0 | 4963 | x: abs(y2 - y0) < ε ? x2 : x0, |
michael@0 | 4964 | y: y0 |
michael@0 | 4965 | } : null), cell.site, null)); |
michael@0 | 4966 | ++nHalfEdges; |
michael@0 | 4967 | } |
michael@0 | 4968 | } |
michael@0 | 4969 | } |
michael@0 | 4970 | } |
michael@0 | 4971 | function d3_geom_voronoiHalfEdgeOrder(a, b) { |
michael@0 | 4972 | return b.angle - a.angle; |
michael@0 | 4973 | } |
michael@0 | 4974 | function d3_geom_voronoiCircle() { |
michael@0 | 4975 | d3_geom_voronoiRedBlackNode(this); |
michael@0 | 4976 | this.x = this.y = this.arc = this.site = this.cy = null; |
michael@0 | 4977 | } |
michael@0 | 4978 | function d3_geom_voronoiAttachCircle(arc) { |
michael@0 | 4979 | var lArc = arc.P, rArc = arc.N; |
michael@0 | 4980 | if (!lArc || !rArc) return; |
michael@0 | 4981 | var lSite = lArc.site, cSite = arc.site, rSite = rArc.site; |
michael@0 | 4982 | if (lSite === rSite) return; |
michael@0 | 4983 | var bx = cSite.x, by = cSite.y, ax = lSite.x - bx, ay = lSite.y - by, cx = rSite.x - bx, cy = rSite.y - by; |
michael@0 | 4984 | var d = 2 * (ax * cy - ay * cx); |
michael@0 | 4985 | if (d >= -ε2) return; |
michael@0 | 4986 | var ha = ax * ax + ay * ay, hc = cx * cx + cy * cy, x = (cy * ha - ay * hc) / d, y = (ax * hc - cx * ha) / d, cy = y + by; |
michael@0 | 4987 | var circle = d3_geom_voronoiCirclePool.pop() || new d3_geom_voronoiCircle(); |
michael@0 | 4988 | circle.arc = arc; |
michael@0 | 4989 | circle.site = cSite; |
michael@0 | 4990 | circle.x = x + bx; |
michael@0 | 4991 | circle.y = cy + Math.sqrt(x * x + y * y); |
michael@0 | 4992 | circle.cy = cy; |
michael@0 | 4993 | arc.circle = circle; |
michael@0 | 4994 | var before = null, node = d3_geom_voronoiCircles._; |
michael@0 | 4995 | while (node) { |
michael@0 | 4996 | if (circle.y < node.y || circle.y === node.y && circle.x <= node.x) { |
michael@0 | 4997 | if (node.L) node = node.L; else { |
michael@0 | 4998 | before = node.P; |
michael@0 | 4999 | break; |
michael@0 | 5000 | } |
michael@0 | 5001 | } else { |
michael@0 | 5002 | if (node.R) node = node.R; else { |
michael@0 | 5003 | before = node; |
michael@0 | 5004 | break; |
michael@0 | 5005 | } |
michael@0 | 5006 | } |
michael@0 | 5007 | } |
michael@0 | 5008 | d3_geom_voronoiCircles.insert(before, circle); |
michael@0 | 5009 | if (!before) d3_geom_voronoiFirstCircle = circle; |
michael@0 | 5010 | } |
michael@0 | 5011 | function d3_geom_voronoiDetachCircle(arc) { |
michael@0 | 5012 | var circle = arc.circle; |
michael@0 | 5013 | if (circle) { |
michael@0 | 5014 | if (!circle.P) d3_geom_voronoiFirstCircle = circle.N; |
michael@0 | 5015 | d3_geom_voronoiCircles.remove(circle); |
michael@0 | 5016 | d3_geom_voronoiCirclePool.push(circle); |
michael@0 | 5017 | d3_geom_voronoiRedBlackNode(circle); |
michael@0 | 5018 | arc.circle = null; |
michael@0 | 5019 | } |
michael@0 | 5020 | } |
michael@0 | 5021 | function d3_geom_voronoiClipEdges(extent) { |
michael@0 | 5022 | var edges = d3_geom_voronoiEdges, clip = d3_geom_clipLine(extent[0][0], extent[0][1], extent[1][0], extent[1][1]), i = edges.length, e; |
michael@0 | 5023 | while (i--) { |
michael@0 | 5024 | e = edges[i]; |
michael@0 | 5025 | if (!d3_geom_voronoiConnectEdge(e, extent) || !clip(e) || abs(e.a.x - e.b.x) < ε && abs(e.a.y - e.b.y) < ε) { |
michael@0 | 5026 | e.a = e.b = null; |
michael@0 | 5027 | edges.splice(i, 1); |
michael@0 | 5028 | } |
michael@0 | 5029 | } |
michael@0 | 5030 | } |
michael@0 | 5031 | function d3_geom_voronoiConnectEdge(edge, extent) { |
michael@0 | 5032 | var vb = edge.b; |
michael@0 | 5033 | if (vb) return true; |
michael@0 | 5034 | var va = edge.a, x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], lSite = edge.l, rSite = edge.r, lx = lSite.x, ly = lSite.y, rx = rSite.x, ry = rSite.y, fx = (lx + rx) / 2, fy = (ly + ry) / 2, fm, fb; |
michael@0 | 5035 | if (ry === ly) { |
michael@0 | 5036 | if (fx < x0 || fx >= x1) return; |
michael@0 | 5037 | if (lx > rx) { |
michael@0 | 5038 | if (!va) va = { |
michael@0 | 5039 | x: fx, |
michael@0 | 5040 | y: y0 |
michael@0 | 5041 | }; else if (va.y >= y1) return; |
michael@0 | 5042 | vb = { |
michael@0 | 5043 | x: fx, |
michael@0 | 5044 | y: y1 |
michael@0 | 5045 | }; |
michael@0 | 5046 | } else { |
michael@0 | 5047 | if (!va) va = { |
michael@0 | 5048 | x: fx, |
michael@0 | 5049 | y: y1 |
michael@0 | 5050 | }; else if (va.y < y0) return; |
michael@0 | 5051 | vb = { |
michael@0 | 5052 | x: fx, |
michael@0 | 5053 | y: y0 |
michael@0 | 5054 | }; |
michael@0 | 5055 | } |
michael@0 | 5056 | } else { |
michael@0 | 5057 | fm = (lx - rx) / (ry - ly); |
michael@0 | 5058 | fb = fy - fm * fx; |
michael@0 | 5059 | if (fm < -1 || fm > 1) { |
michael@0 | 5060 | if (lx > rx) { |
michael@0 | 5061 | if (!va) va = { |
michael@0 | 5062 | x: (y0 - fb) / fm, |
michael@0 | 5063 | y: y0 |
michael@0 | 5064 | }; else if (va.y >= y1) return; |
michael@0 | 5065 | vb = { |
michael@0 | 5066 | x: (y1 - fb) / fm, |
michael@0 | 5067 | y: y1 |
michael@0 | 5068 | }; |
michael@0 | 5069 | } else { |
michael@0 | 5070 | if (!va) va = { |
michael@0 | 5071 | x: (y1 - fb) / fm, |
michael@0 | 5072 | y: y1 |
michael@0 | 5073 | }; else if (va.y < y0) return; |
michael@0 | 5074 | vb = { |
michael@0 | 5075 | x: (y0 - fb) / fm, |
michael@0 | 5076 | y: y0 |
michael@0 | 5077 | }; |
michael@0 | 5078 | } |
michael@0 | 5079 | } else { |
michael@0 | 5080 | if (ly < ry) { |
michael@0 | 5081 | if (!va) va = { |
michael@0 | 5082 | x: x0, |
michael@0 | 5083 | y: fm * x0 + fb |
michael@0 | 5084 | }; else if (va.x >= x1) return; |
michael@0 | 5085 | vb = { |
michael@0 | 5086 | x: x1, |
michael@0 | 5087 | y: fm * x1 + fb |
michael@0 | 5088 | }; |
michael@0 | 5089 | } else { |
michael@0 | 5090 | if (!va) va = { |
michael@0 | 5091 | x: x1, |
michael@0 | 5092 | y: fm * x1 + fb |
michael@0 | 5093 | }; else if (va.x < x0) return; |
michael@0 | 5094 | vb = { |
michael@0 | 5095 | x: x0, |
michael@0 | 5096 | y: fm * x0 + fb |
michael@0 | 5097 | }; |
michael@0 | 5098 | } |
michael@0 | 5099 | } |
michael@0 | 5100 | } |
michael@0 | 5101 | edge.a = va; |
michael@0 | 5102 | edge.b = vb; |
michael@0 | 5103 | return true; |
michael@0 | 5104 | } |
michael@0 | 5105 | function d3_geom_voronoiEdge(lSite, rSite) { |
michael@0 | 5106 | this.l = lSite; |
michael@0 | 5107 | this.r = rSite; |
michael@0 | 5108 | this.a = this.b = null; |
michael@0 | 5109 | } |
michael@0 | 5110 | function d3_geom_voronoiCreateEdge(lSite, rSite, va, vb) { |
michael@0 | 5111 | var edge = new d3_geom_voronoiEdge(lSite, rSite); |
michael@0 | 5112 | d3_geom_voronoiEdges.push(edge); |
michael@0 | 5113 | if (va) d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, va); |
michael@0 | 5114 | if (vb) d3_geom_voronoiSetEdgeEnd(edge, rSite, lSite, vb); |
michael@0 | 5115 | d3_geom_voronoiCells[lSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, lSite, rSite)); |
michael@0 | 5116 | d3_geom_voronoiCells[rSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, rSite, lSite)); |
michael@0 | 5117 | return edge; |
michael@0 | 5118 | } |
michael@0 | 5119 | function d3_geom_voronoiCreateBorderEdge(lSite, va, vb) { |
michael@0 | 5120 | var edge = new d3_geom_voronoiEdge(lSite, null); |
michael@0 | 5121 | edge.a = va; |
michael@0 | 5122 | edge.b = vb; |
michael@0 | 5123 | d3_geom_voronoiEdges.push(edge); |
michael@0 | 5124 | return edge; |
michael@0 | 5125 | } |
michael@0 | 5126 | function d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, vertex) { |
michael@0 | 5127 | if (!edge.a && !edge.b) { |
michael@0 | 5128 | edge.a = vertex; |
michael@0 | 5129 | edge.l = lSite; |
michael@0 | 5130 | edge.r = rSite; |
michael@0 | 5131 | } else if (edge.l === rSite) { |
michael@0 | 5132 | edge.b = vertex; |
michael@0 | 5133 | } else { |
michael@0 | 5134 | edge.a = vertex; |
michael@0 | 5135 | } |
michael@0 | 5136 | } |
michael@0 | 5137 | function d3_geom_voronoiHalfEdge(edge, lSite, rSite) { |
michael@0 | 5138 | var va = edge.a, vb = edge.b; |
michael@0 | 5139 | this.edge = edge; |
michael@0 | 5140 | this.site = lSite; |
michael@0 | 5141 | this.angle = rSite ? Math.atan2(rSite.y - lSite.y, rSite.x - lSite.x) : edge.l === lSite ? Math.atan2(vb.x - va.x, va.y - vb.y) : Math.atan2(va.x - vb.x, vb.y - va.y); |
michael@0 | 5142 | } |
michael@0 | 5143 | d3_geom_voronoiHalfEdge.prototype = { |
michael@0 | 5144 | start: function() { |
michael@0 | 5145 | return this.edge.l === this.site ? this.edge.a : this.edge.b; |
michael@0 | 5146 | }, |
michael@0 | 5147 | end: function() { |
michael@0 | 5148 | return this.edge.l === this.site ? this.edge.b : this.edge.a; |
michael@0 | 5149 | } |
michael@0 | 5150 | }; |
michael@0 | 5151 | function d3_geom_voronoiRedBlackTree() { |
michael@0 | 5152 | this._ = null; |
michael@0 | 5153 | } |
michael@0 | 5154 | function d3_geom_voronoiRedBlackNode(node) { |
michael@0 | 5155 | node.U = node.C = node.L = node.R = node.P = node.N = null; |
michael@0 | 5156 | } |
michael@0 | 5157 | d3_geom_voronoiRedBlackTree.prototype = { |
michael@0 | 5158 | insert: function(after, node) { |
michael@0 | 5159 | var parent, grandpa, uncle; |
michael@0 | 5160 | if (after) { |
michael@0 | 5161 | node.P = after; |
michael@0 | 5162 | node.N = after.N; |
michael@0 | 5163 | if (after.N) after.N.P = node; |
michael@0 | 5164 | after.N = node; |
michael@0 | 5165 | if (after.R) { |
michael@0 | 5166 | after = after.R; |
michael@0 | 5167 | while (after.L) after = after.L; |
michael@0 | 5168 | after.L = node; |
michael@0 | 5169 | } else { |
michael@0 | 5170 | after.R = node; |
michael@0 | 5171 | } |
michael@0 | 5172 | parent = after; |
michael@0 | 5173 | } else if (this._) { |
michael@0 | 5174 | after = d3_geom_voronoiRedBlackFirst(this._); |
michael@0 | 5175 | node.P = null; |
michael@0 | 5176 | node.N = after; |
michael@0 | 5177 | after.P = after.L = node; |
michael@0 | 5178 | parent = after; |
michael@0 | 5179 | } else { |
michael@0 | 5180 | node.P = node.N = null; |
michael@0 | 5181 | this._ = node; |
michael@0 | 5182 | parent = null; |
michael@0 | 5183 | } |
michael@0 | 5184 | node.L = node.R = null; |
michael@0 | 5185 | node.U = parent; |
michael@0 | 5186 | node.C = true; |
michael@0 | 5187 | after = node; |
michael@0 | 5188 | while (parent && parent.C) { |
michael@0 | 5189 | grandpa = parent.U; |
michael@0 | 5190 | if (parent === grandpa.L) { |
michael@0 | 5191 | uncle = grandpa.R; |
michael@0 | 5192 | if (uncle && uncle.C) { |
michael@0 | 5193 | parent.C = uncle.C = false; |
michael@0 | 5194 | grandpa.C = true; |
michael@0 | 5195 | after = grandpa; |
michael@0 | 5196 | } else { |
michael@0 | 5197 | if (after === parent.R) { |
michael@0 | 5198 | d3_geom_voronoiRedBlackRotateLeft(this, parent); |
michael@0 | 5199 | after = parent; |
michael@0 | 5200 | parent = after.U; |
michael@0 | 5201 | } |
michael@0 | 5202 | parent.C = false; |
michael@0 | 5203 | grandpa.C = true; |
michael@0 | 5204 | d3_geom_voronoiRedBlackRotateRight(this, grandpa); |
michael@0 | 5205 | } |
michael@0 | 5206 | } else { |
michael@0 | 5207 | uncle = grandpa.L; |
michael@0 | 5208 | if (uncle && uncle.C) { |
michael@0 | 5209 | parent.C = uncle.C = false; |
michael@0 | 5210 | grandpa.C = true; |
michael@0 | 5211 | after = grandpa; |
michael@0 | 5212 | } else { |
michael@0 | 5213 | if (after === parent.L) { |
michael@0 | 5214 | d3_geom_voronoiRedBlackRotateRight(this, parent); |
michael@0 | 5215 | after = parent; |
michael@0 | 5216 | parent = after.U; |
michael@0 | 5217 | } |
michael@0 | 5218 | parent.C = false; |
michael@0 | 5219 | grandpa.C = true; |
michael@0 | 5220 | d3_geom_voronoiRedBlackRotateLeft(this, grandpa); |
michael@0 | 5221 | } |
michael@0 | 5222 | } |
michael@0 | 5223 | parent = after.U; |
michael@0 | 5224 | } |
michael@0 | 5225 | this._.C = false; |
michael@0 | 5226 | }, |
michael@0 | 5227 | remove: function(node) { |
michael@0 | 5228 | if (node.N) node.N.P = node.P; |
michael@0 | 5229 | if (node.P) node.P.N = node.N; |
michael@0 | 5230 | node.N = node.P = null; |
michael@0 | 5231 | var parent = node.U, sibling, left = node.L, right = node.R, next, red; |
michael@0 | 5232 | if (!left) next = right; else if (!right) next = left; else next = d3_geom_voronoiRedBlackFirst(right); |
michael@0 | 5233 | if (parent) { |
michael@0 | 5234 | if (parent.L === node) parent.L = next; else parent.R = next; |
michael@0 | 5235 | } else { |
michael@0 | 5236 | this._ = next; |
michael@0 | 5237 | } |
michael@0 | 5238 | if (left && right) { |
michael@0 | 5239 | red = next.C; |
michael@0 | 5240 | next.C = node.C; |
michael@0 | 5241 | next.L = left; |
michael@0 | 5242 | left.U = next; |
michael@0 | 5243 | if (next !== right) { |
michael@0 | 5244 | parent = next.U; |
michael@0 | 5245 | next.U = node.U; |
michael@0 | 5246 | node = next.R; |
michael@0 | 5247 | parent.L = node; |
michael@0 | 5248 | next.R = right; |
michael@0 | 5249 | right.U = next; |
michael@0 | 5250 | } else { |
michael@0 | 5251 | next.U = parent; |
michael@0 | 5252 | parent = next; |
michael@0 | 5253 | node = next.R; |
michael@0 | 5254 | } |
michael@0 | 5255 | } else { |
michael@0 | 5256 | red = node.C; |
michael@0 | 5257 | node = next; |
michael@0 | 5258 | } |
michael@0 | 5259 | if (node) node.U = parent; |
michael@0 | 5260 | if (red) return; |
michael@0 | 5261 | if (node && node.C) { |
michael@0 | 5262 | node.C = false; |
michael@0 | 5263 | return; |
michael@0 | 5264 | } |
michael@0 | 5265 | do { |
michael@0 | 5266 | if (node === this._) break; |
michael@0 | 5267 | if (node === parent.L) { |
michael@0 | 5268 | sibling = parent.R; |
michael@0 | 5269 | if (sibling.C) { |
michael@0 | 5270 | sibling.C = false; |
michael@0 | 5271 | parent.C = true; |
michael@0 | 5272 | d3_geom_voronoiRedBlackRotateLeft(this, parent); |
michael@0 | 5273 | sibling = parent.R; |
michael@0 | 5274 | } |
michael@0 | 5275 | if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { |
michael@0 | 5276 | if (!sibling.R || !sibling.R.C) { |
michael@0 | 5277 | sibling.L.C = false; |
michael@0 | 5278 | sibling.C = true; |
michael@0 | 5279 | d3_geom_voronoiRedBlackRotateRight(this, sibling); |
michael@0 | 5280 | sibling = parent.R; |
michael@0 | 5281 | } |
michael@0 | 5282 | sibling.C = parent.C; |
michael@0 | 5283 | parent.C = sibling.R.C = false; |
michael@0 | 5284 | d3_geom_voronoiRedBlackRotateLeft(this, parent); |
michael@0 | 5285 | node = this._; |
michael@0 | 5286 | break; |
michael@0 | 5287 | } |
michael@0 | 5288 | } else { |
michael@0 | 5289 | sibling = parent.L; |
michael@0 | 5290 | if (sibling.C) { |
michael@0 | 5291 | sibling.C = false; |
michael@0 | 5292 | parent.C = true; |
michael@0 | 5293 | d3_geom_voronoiRedBlackRotateRight(this, parent); |
michael@0 | 5294 | sibling = parent.L; |
michael@0 | 5295 | } |
michael@0 | 5296 | if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { |
michael@0 | 5297 | if (!sibling.L || !sibling.L.C) { |
michael@0 | 5298 | sibling.R.C = false; |
michael@0 | 5299 | sibling.C = true; |
michael@0 | 5300 | d3_geom_voronoiRedBlackRotateLeft(this, sibling); |
michael@0 | 5301 | sibling = parent.L; |
michael@0 | 5302 | } |
michael@0 | 5303 | sibling.C = parent.C; |
michael@0 | 5304 | parent.C = sibling.L.C = false; |
michael@0 | 5305 | d3_geom_voronoiRedBlackRotateRight(this, parent); |
michael@0 | 5306 | node = this._; |
michael@0 | 5307 | break; |
michael@0 | 5308 | } |
michael@0 | 5309 | } |
michael@0 | 5310 | sibling.C = true; |
michael@0 | 5311 | node = parent; |
michael@0 | 5312 | parent = parent.U; |
michael@0 | 5313 | } while (!node.C); |
michael@0 | 5314 | if (node) node.C = false; |
michael@0 | 5315 | } |
michael@0 | 5316 | }; |
michael@0 | 5317 | function d3_geom_voronoiRedBlackRotateLeft(tree, node) { |
michael@0 | 5318 | var p = node, q = node.R, parent = p.U; |
michael@0 | 5319 | if (parent) { |
michael@0 | 5320 | if (parent.L === p) parent.L = q; else parent.R = q; |
michael@0 | 5321 | } else { |
michael@0 | 5322 | tree._ = q; |
michael@0 | 5323 | } |
michael@0 | 5324 | q.U = parent; |
michael@0 | 5325 | p.U = q; |
michael@0 | 5326 | p.R = q.L; |
michael@0 | 5327 | if (p.R) p.R.U = p; |
michael@0 | 5328 | q.L = p; |
michael@0 | 5329 | } |
michael@0 | 5330 | function d3_geom_voronoiRedBlackRotateRight(tree, node) { |
michael@0 | 5331 | var p = node, q = node.L, parent = p.U; |
michael@0 | 5332 | if (parent) { |
michael@0 | 5333 | if (parent.L === p) parent.L = q; else parent.R = q; |
michael@0 | 5334 | } else { |
michael@0 | 5335 | tree._ = q; |
michael@0 | 5336 | } |
michael@0 | 5337 | q.U = parent; |
michael@0 | 5338 | p.U = q; |
michael@0 | 5339 | p.L = q.R; |
michael@0 | 5340 | if (p.L) p.L.U = p; |
michael@0 | 5341 | q.R = p; |
michael@0 | 5342 | } |
michael@0 | 5343 | function d3_geom_voronoiRedBlackFirst(node) { |
michael@0 | 5344 | while (node.L) node = node.L; |
michael@0 | 5345 | return node; |
michael@0 | 5346 | } |
michael@0 | 5347 | function d3_geom_voronoi(sites, bbox) { |
michael@0 | 5348 | var site = sites.sort(d3_geom_voronoiVertexOrder).pop(), x0, y0, circle; |
michael@0 | 5349 | d3_geom_voronoiEdges = []; |
michael@0 | 5350 | d3_geom_voronoiCells = new Array(sites.length); |
michael@0 | 5351 | d3_geom_voronoiBeaches = new d3_geom_voronoiRedBlackTree(); |
michael@0 | 5352 | d3_geom_voronoiCircles = new d3_geom_voronoiRedBlackTree(); |
michael@0 | 5353 | while (true) { |
michael@0 | 5354 | circle = d3_geom_voronoiFirstCircle; |
michael@0 | 5355 | if (site && (!circle || site.y < circle.y || site.y === circle.y && site.x < circle.x)) { |
michael@0 | 5356 | if (site.x !== x0 || site.y !== y0) { |
michael@0 | 5357 | d3_geom_voronoiCells[site.i] = new d3_geom_voronoiCell(site); |
michael@0 | 5358 | d3_geom_voronoiAddBeach(site); |
michael@0 | 5359 | x0 = site.x, y0 = site.y; |
michael@0 | 5360 | } |
michael@0 | 5361 | site = sites.pop(); |
michael@0 | 5362 | } else if (circle) { |
michael@0 | 5363 | d3_geom_voronoiRemoveBeach(circle.arc); |
michael@0 | 5364 | } else { |
michael@0 | 5365 | break; |
michael@0 | 5366 | } |
michael@0 | 5367 | } |
michael@0 | 5368 | if (bbox) d3_geom_voronoiClipEdges(bbox), d3_geom_voronoiCloseCells(bbox); |
michael@0 | 5369 | var diagram = { |
michael@0 | 5370 | cells: d3_geom_voronoiCells, |
michael@0 | 5371 | edges: d3_geom_voronoiEdges |
michael@0 | 5372 | }; |
michael@0 | 5373 | d3_geom_voronoiBeaches = d3_geom_voronoiCircles = d3_geom_voronoiEdges = d3_geom_voronoiCells = null; |
michael@0 | 5374 | return diagram; |
michael@0 | 5375 | } |
michael@0 | 5376 | function d3_geom_voronoiVertexOrder(a, b) { |
michael@0 | 5377 | return b.y - a.y || b.x - a.x; |
michael@0 | 5378 | } |
michael@0 | 5379 | d3.geom.voronoi = function(points) { |
michael@0 | 5380 | var x = d3_geom_pointX, y = d3_geom_pointY, fx = x, fy = y, clipExtent = d3_geom_voronoiClipExtent; |
michael@0 | 5381 | if (points) return voronoi(points); |
michael@0 | 5382 | function voronoi(data) { |
michael@0 | 5383 | var polygons = new Array(data.length), x0 = clipExtent[0][0], y0 = clipExtent[0][1], x1 = clipExtent[1][0], y1 = clipExtent[1][1]; |
michael@0 | 5384 | d3_geom_voronoi(sites(data), clipExtent).cells.forEach(function(cell, i) { |
michael@0 | 5385 | var edges = cell.edges, site = cell.site, polygon = polygons[i] = edges.length ? edges.map(function(e) { |
michael@0 | 5386 | var s = e.start(); |
michael@0 | 5387 | return [ s.x, s.y ]; |
michael@0 | 5388 | }) : site.x >= x0 && site.x <= x1 && site.y >= y0 && site.y <= y1 ? [ [ x0, y1 ], [ x1, y1 ], [ x1, y0 ], [ x0, y0 ] ] : []; |
michael@0 | 5389 | polygon.point = data[i]; |
michael@0 | 5390 | }); |
michael@0 | 5391 | return polygons; |
michael@0 | 5392 | } |
michael@0 | 5393 | function sites(data) { |
michael@0 | 5394 | return data.map(function(d, i) { |
michael@0 | 5395 | return { |
michael@0 | 5396 | x: Math.round(fx(d, i) / ε) * ε, |
michael@0 | 5397 | y: Math.round(fy(d, i) / ε) * ε, |
michael@0 | 5398 | i: i |
michael@0 | 5399 | }; |
michael@0 | 5400 | }); |
michael@0 | 5401 | } |
michael@0 | 5402 | voronoi.links = function(data) { |
michael@0 | 5403 | return d3_geom_voronoi(sites(data)).edges.filter(function(edge) { |
michael@0 | 5404 | return edge.l && edge.r; |
michael@0 | 5405 | }).map(function(edge) { |
michael@0 | 5406 | return { |
michael@0 | 5407 | source: data[edge.l.i], |
michael@0 | 5408 | target: data[edge.r.i] |
michael@0 | 5409 | }; |
michael@0 | 5410 | }); |
michael@0 | 5411 | }; |
michael@0 | 5412 | voronoi.triangles = function(data) { |
michael@0 | 5413 | var triangles = []; |
michael@0 | 5414 | d3_geom_voronoi(sites(data)).cells.forEach(function(cell, i) { |
michael@0 | 5415 | var site = cell.site, edges = cell.edges.sort(d3_geom_voronoiHalfEdgeOrder), j = -1, m = edges.length, e0, s0, e1 = edges[m - 1].edge, s1 = e1.l === site ? e1.r : e1.l; |
michael@0 | 5416 | while (++j < m) { |
michael@0 | 5417 | e0 = e1; |
michael@0 | 5418 | s0 = s1; |
michael@0 | 5419 | e1 = edges[j].edge; |
michael@0 | 5420 | s1 = e1.l === site ? e1.r : e1.l; |
michael@0 | 5421 | if (i < s0.i && i < s1.i && d3_geom_voronoiTriangleArea(site, s0, s1) < 0) { |
michael@0 | 5422 | triangles.push([ data[i], data[s0.i], data[s1.i] ]); |
michael@0 | 5423 | } |
michael@0 | 5424 | } |
michael@0 | 5425 | }); |
michael@0 | 5426 | return triangles; |
michael@0 | 5427 | }; |
michael@0 | 5428 | voronoi.x = function(_) { |
michael@0 | 5429 | return arguments.length ? (fx = d3_functor(x = _), voronoi) : x; |
michael@0 | 5430 | }; |
michael@0 | 5431 | voronoi.y = function(_) { |
michael@0 | 5432 | return arguments.length ? (fy = d3_functor(y = _), voronoi) : y; |
michael@0 | 5433 | }; |
michael@0 | 5434 | voronoi.clipExtent = function(_) { |
michael@0 | 5435 | if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent; |
michael@0 | 5436 | clipExtent = _ == null ? d3_geom_voronoiClipExtent : _; |
michael@0 | 5437 | return voronoi; |
michael@0 | 5438 | }; |
michael@0 | 5439 | voronoi.size = function(_) { |
michael@0 | 5440 | if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent && clipExtent[1]; |
michael@0 | 5441 | return voronoi.clipExtent(_ && [ [ 0, 0 ], _ ]); |
michael@0 | 5442 | }; |
michael@0 | 5443 | return voronoi; |
michael@0 | 5444 | }; |
michael@0 | 5445 | var d3_geom_voronoiClipExtent = [ [ -1e6, -1e6 ], [ 1e6, 1e6 ] ]; |
michael@0 | 5446 | function d3_geom_voronoiTriangleArea(a, b, c) { |
michael@0 | 5447 | return (a.x - c.x) * (b.y - a.y) - (a.x - b.x) * (c.y - a.y); |
michael@0 | 5448 | } |
michael@0 | 5449 | d3.geom.delaunay = function(vertices) { |
michael@0 | 5450 | return d3.geom.voronoi().triangles(vertices); |
michael@0 | 5451 | }; |
michael@0 | 5452 | d3.geom.quadtree = function(points, x1, y1, x2, y2) { |
michael@0 | 5453 | var x = d3_geom_pointX, y = d3_geom_pointY, compat; |
michael@0 | 5454 | if (compat = arguments.length) { |
michael@0 | 5455 | x = d3_geom_quadtreeCompatX; |
michael@0 | 5456 | y = d3_geom_quadtreeCompatY; |
michael@0 | 5457 | if (compat === 3) { |
michael@0 | 5458 | y2 = y1; |
michael@0 | 5459 | x2 = x1; |
michael@0 | 5460 | y1 = x1 = 0; |
michael@0 | 5461 | } |
michael@0 | 5462 | return quadtree(points); |
michael@0 | 5463 | } |
michael@0 | 5464 | function quadtree(data) { |
michael@0 | 5465 | var d, fx = d3_functor(x), fy = d3_functor(y), xs, ys, i, n, x1_, y1_, x2_, y2_; |
michael@0 | 5466 | if (x1 != null) { |
michael@0 | 5467 | x1_ = x1, y1_ = y1, x2_ = x2, y2_ = y2; |
michael@0 | 5468 | } else { |
michael@0 | 5469 | x2_ = y2_ = -(x1_ = y1_ = Infinity); |
michael@0 | 5470 | xs = [], ys = []; |
michael@0 | 5471 | n = data.length; |
michael@0 | 5472 | if (compat) for (i = 0; i < n; ++i) { |
michael@0 | 5473 | d = data[i]; |
michael@0 | 5474 | if (d.x < x1_) x1_ = d.x; |
michael@0 | 5475 | if (d.y < y1_) y1_ = d.y; |
michael@0 | 5476 | if (d.x > x2_) x2_ = d.x; |
michael@0 | 5477 | if (d.y > y2_) y2_ = d.y; |
michael@0 | 5478 | xs.push(d.x); |
michael@0 | 5479 | ys.push(d.y); |
michael@0 | 5480 | } else for (i = 0; i < n; ++i) { |
michael@0 | 5481 | var x_ = +fx(d = data[i], i), y_ = +fy(d, i); |
michael@0 | 5482 | if (x_ < x1_) x1_ = x_; |
michael@0 | 5483 | if (y_ < y1_) y1_ = y_; |
michael@0 | 5484 | if (x_ > x2_) x2_ = x_; |
michael@0 | 5485 | if (y_ > y2_) y2_ = y_; |
michael@0 | 5486 | xs.push(x_); |
michael@0 | 5487 | ys.push(y_); |
michael@0 | 5488 | } |
michael@0 | 5489 | } |
michael@0 | 5490 | var dx = x2_ - x1_, dy = y2_ - y1_; |
michael@0 | 5491 | if (dx > dy) y2_ = y1_ + dx; else x2_ = x1_ + dy; |
michael@0 | 5492 | function insert(n, d, x, y, x1, y1, x2, y2) { |
michael@0 | 5493 | if (isNaN(x) || isNaN(y)) return; |
michael@0 | 5494 | if (n.leaf) { |
michael@0 | 5495 | var nx = n.x, ny = n.y; |
michael@0 | 5496 | if (nx != null) { |
michael@0 | 5497 | if (abs(nx - x) + abs(ny - y) < .01) { |
michael@0 | 5498 | insertChild(n, d, x, y, x1, y1, x2, y2); |
michael@0 | 5499 | } else { |
michael@0 | 5500 | var nPoint = n.point; |
michael@0 | 5501 | n.x = n.y = n.point = null; |
michael@0 | 5502 | insertChild(n, nPoint, nx, ny, x1, y1, x2, y2); |
michael@0 | 5503 | insertChild(n, d, x, y, x1, y1, x2, y2); |
michael@0 | 5504 | } |
michael@0 | 5505 | } else { |
michael@0 | 5506 | n.x = x, n.y = y, n.point = d; |
michael@0 | 5507 | } |
michael@0 | 5508 | } else { |
michael@0 | 5509 | insertChild(n, d, x, y, x1, y1, x2, y2); |
michael@0 | 5510 | } |
michael@0 | 5511 | } |
michael@0 | 5512 | function insertChild(n, d, x, y, x1, y1, x2, y2) { |
michael@0 | 5513 | var sx = (x1 + x2) * .5, sy = (y1 + y2) * .5, right = x >= sx, bottom = y >= sy, i = (bottom << 1) + right; |
michael@0 | 5514 | n.leaf = false; |
michael@0 | 5515 | n = n.nodes[i] || (n.nodes[i] = d3_geom_quadtreeNode()); |
michael@0 | 5516 | if (right) x1 = sx; else x2 = sx; |
michael@0 | 5517 | if (bottom) y1 = sy; else y2 = sy; |
michael@0 | 5518 | insert(n, d, x, y, x1, y1, x2, y2); |
michael@0 | 5519 | } |
michael@0 | 5520 | var root = d3_geom_quadtreeNode(); |
michael@0 | 5521 | root.add = function(d) { |
michael@0 | 5522 | insert(root, d, +fx(d, ++i), +fy(d, i), x1_, y1_, x2_, y2_); |
michael@0 | 5523 | }; |
michael@0 | 5524 | root.visit = function(f) { |
michael@0 | 5525 | d3_geom_quadtreeVisit(f, root, x1_, y1_, x2_, y2_); |
michael@0 | 5526 | }; |
michael@0 | 5527 | i = -1; |
michael@0 | 5528 | if (x1 == null) { |
michael@0 | 5529 | while (++i < n) { |
michael@0 | 5530 | insert(root, data[i], xs[i], ys[i], x1_, y1_, x2_, y2_); |
michael@0 | 5531 | } |
michael@0 | 5532 | --i; |
michael@0 | 5533 | } else data.forEach(root.add); |
michael@0 | 5534 | xs = ys = data = d = null; |
michael@0 | 5535 | return root; |
michael@0 | 5536 | } |
michael@0 | 5537 | quadtree.x = function(_) { |
michael@0 | 5538 | return arguments.length ? (x = _, quadtree) : x; |
michael@0 | 5539 | }; |
michael@0 | 5540 | quadtree.y = function(_) { |
michael@0 | 5541 | return arguments.length ? (y = _, quadtree) : y; |
michael@0 | 5542 | }; |
michael@0 | 5543 | quadtree.extent = function(_) { |
michael@0 | 5544 | if (!arguments.length) return x1 == null ? null : [ [ x1, y1 ], [ x2, y2 ] ]; |
michael@0 | 5545 | if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = +_[0][0], y1 = +_[0][1], x2 = +_[1][0], |
michael@0 | 5546 | y2 = +_[1][1]; |
michael@0 | 5547 | return quadtree; |
michael@0 | 5548 | }; |
michael@0 | 5549 | quadtree.size = function(_) { |
michael@0 | 5550 | if (!arguments.length) return x1 == null ? null : [ x2 - x1, y2 - y1 ]; |
michael@0 | 5551 | if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = y1 = 0, x2 = +_[0], y2 = +_[1]; |
michael@0 | 5552 | return quadtree; |
michael@0 | 5553 | }; |
michael@0 | 5554 | return quadtree; |
michael@0 | 5555 | }; |
michael@0 | 5556 | function d3_geom_quadtreeCompatX(d) { |
michael@0 | 5557 | return d.x; |
michael@0 | 5558 | } |
michael@0 | 5559 | function d3_geom_quadtreeCompatY(d) { |
michael@0 | 5560 | return d.y; |
michael@0 | 5561 | } |
michael@0 | 5562 | function d3_geom_quadtreeNode() { |
michael@0 | 5563 | return { |
michael@0 | 5564 | leaf: true, |
michael@0 | 5565 | nodes: [], |
michael@0 | 5566 | point: null, |
michael@0 | 5567 | x: null, |
michael@0 | 5568 | y: null |
michael@0 | 5569 | }; |
michael@0 | 5570 | } |
michael@0 | 5571 | function d3_geom_quadtreeVisit(f, node, x1, y1, x2, y2) { |
michael@0 | 5572 | if (!f(node, x1, y1, x2, y2)) { |
michael@0 | 5573 | var sx = (x1 + x2) * .5, sy = (y1 + y2) * .5, children = node.nodes; |
michael@0 | 5574 | if (children[0]) d3_geom_quadtreeVisit(f, children[0], x1, y1, sx, sy); |
michael@0 | 5575 | if (children[1]) d3_geom_quadtreeVisit(f, children[1], sx, y1, x2, sy); |
michael@0 | 5576 | if (children[2]) d3_geom_quadtreeVisit(f, children[2], x1, sy, sx, y2); |
michael@0 | 5577 | if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2); |
michael@0 | 5578 | } |
michael@0 | 5579 | } |
michael@0 | 5580 | d3.interpolateRgb = d3_interpolateRgb; |
michael@0 | 5581 | function d3_interpolateRgb(a, b) { |
michael@0 | 5582 | a = d3.rgb(a); |
michael@0 | 5583 | b = d3.rgb(b); |
michael@0 | 5584 | var ar = a.r, ag = a.g, ab = a.b, br = b.r - ar, bg = b.g - ag, bb = b.b - ab; |
michael@0 | 5585 | return function(t) { |
michael@0 | 5586 | return "#" + d3_rgb_hex(Math.round(ar + br * t)) + d3_rgb_hex(Math.round(ag + bg * t)) + d3_rgb_hex(Math.round(ab + bb * t)); |
michael@0 | 5587 | }; |
michael@0 | 5588 | } |
michael@0 | 5589 | d3.interpolateObject = d3_interpolateObject; |
michael@0 | 5590 | function d3_interpolateObject(a, b) { |
michael@0 | 5591 | var i = {}, c = {}, k; |
michael@0 | 5592 | for (k in a) { |
michael@0 | 5593 | if (k in b) { |
michael@0 | 5594 | i[k] = d3_interpolate(a[k], b[k]); |
michael@0 | 5595 | } else { |
michael@0 | 5596 | c[k] = a[k]; |
michael@0 | 5597 | } |
michael@0 | 5598 | } |
michael@0 | 5599 | for (k in b) { |
michael@0 | 5600 | if (!(k in a)) { |
michael@0 | 5601 | c[k] = b[k]; |
michael@0 | 5602 | } |
michael@0 | 5603 | } |
michael@0 | 5604 | return function(t) { |
michael@0 | 5605 | for (k in i) c[k] = i[k](t); |
michael@0 | 5606 | return c; |
michael@0 | 5607 | }; |
michael@0 | 5608 | } |
michael@0 | 5609 | d3.interpolateNumber = d3_interpolateNumber; |
michael@0 | 5610 | function d3_interpolateNumber(a, b) { |
michael@0 | 5611 | b -= a = +a; |
michael@0 | 5612 | return function(t) { |
michael@0 | 5613 | return a + b * t; |
michael@0 | 5614 | }; |
michael@0 | 5615 | } |
michael@0 | 5616 | d3.interpolateString = d3_interpolateString; |
michael@0 | 5617 | function d3_interpolateString(a, b) { |
michael@0 | 5618 | var m, i, j, s0 = 0, s1 = 0, s = [], q = [], n, o; |
michael@0 | 5619 | a = a + "", b = b + ""; |
michael@0 | 5620 | d3_interpolate_number.lastIndex = 0; |
michael@0 | 5621 | for (i = 0; m = d3_interpolate_number.exec(b); ++i) { |
michael@0 | 5622 | if (m.index) s.push(b.substring(s0, s1 = m.index)); |
michael@0 | 5623 | q.push({ |
michael@0 | 5624 | i: s.length, |
michael@0 | 5625 | x: m[0] |
michael@0 | 5626 | }); |
michael@0 | 5627 | s.push(null); |
michael@0 | 5628 | s0 = d3_interpolate_number.lastIndex; |
michael@0 | 5629 | } |
michael@0 | 5630 | if (s0 < b.length) s.push(b.substring(s0)); |
michael@0 | 5631 | for (i = 0, n = q.length; (m = d3_interpolate_number.exec(a)) && i < n; ++i) { |
michael@0 | 5632 | o = q[i]; |
michael@0 | 5633 | if (o.x == m[0]) { |
michael@0 | 5634 | if (o.i) { |
michael@0 | 5635 | if (s[o.i + 1] == null) { |
michael@0 | 5636 | s[o.i - 1] += o.x; |
michael@0 | 5637 | s.splice(o.i, 1); |
michael@0 | 5638 | for (j = i + 1; j < n; ++j) q[j].i--; |
michael@0 | 5639 | } else { |
michael@0 | 5640 | s[o.i - 1] += o.x + s[o.i + 1]; |
michael@0 | 5641 | s.splice(o.i, 2); |
michael@0 | 5642 | for (j = i + 1; j < n; ++j) q[j].i -= 2; |
michael@0 | 5643 | } |
michael@0 | 5644 | } else { |
michael@0 | 5645 | if (s[o.i + 1] == null) { |
michael@0 | 5646 | s[o.i] = o.x; |
michael@0 | 5647 | } else { |
michael@0 | 5648 | s[o.i] = o.x + s[o.i + 1]; |
michael@0 | 5649 | s.splice(o.i + 1, 1); |
michael@0 | 5650 | for (j = i + 1; j < n; ++j) q[j].i--; |
michael@0 | 5651 | } |
michael@0 | 5652 | } |
michael@0 | 5653 | q.splice(i, 1); |
michael@0 | 5654 | n--; |
michael@0 | 5655 | i--; |
michael@0 | 5656 | } else { |
michael@0 | 5657 | o.x = d3_interpolateNumber(parseFloat(m[0]), parseFloat(o.x)); |
michael@0 | 5658 | } |
michael@0 | 5659 | } |
michael@0 | 5660 | while (i < n) { |
michael@0 | 5661 | o = q.pop(); |
michael@0 | 5662 | if (s[o.i + 1] == null) { |
michael@0 | 5663 | s[o.i] = o.x; |
michael@0 | 5664 | } else { |
michael@0 | 5665 | s[o.i] = o.x + s[o.i + 1]; |
michael@0 | 5666 | s.splice(o.i + 1, 1); |
michael@0 | 5667 | } |
michael@0 | 5668 | n--; |
michael@0 | 5669 | } |
michael@0 | 5670 | if (s.length === 1) { |
michael@0 | 5671 | return s[0] == null ? (o = q[0].x, function(t) { |
michael@0 | 5672 | return o(t) + ""; |
michael@0 | 5673 | }) : function() { |
michael@0 | 5674 | return b; |
michael@0 | 5675 | }; |
michael@0 | 5676 | } |
michael@0 | 5677 | return function(t) { |
michael@0 | 5678 | for (i = 0; i < n; ++i) s[(o = q[i]).i] = o.x(t); |
michael@0 | 5679 | return s.join(""); |
michael@0 | 5680 | }; |
michael@0 | 5681 | } |
michael@0 | 5682 | var d3_interpolate_number = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g; |
michael@0 | 5683 | d3.interpolate = d3_interpolate; |
michael@0 | 5684 | function d3_interpolate(a, b) { |
michael@0 | 5685 | var i = d3.interpolators.length, f; |
michael@0 | 5686 | while (--i >= 0 && !(f = d3.interpolators[i](a, b))) ; |
michael@0 | 5687 | return f; |
michael@0 | 5688 | } |
michael@0 | 5689 | d3.interpolators = [ function(a, b) { |
michael@0 | 5690 | var t = typeof b; |
michael@0 | 5691 | return (t === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_Color ? d3_interpolateRgb : t === "object" ? Array.isArray(b) ? d3_interpolateArray : d3_interpolateObject : d3_interpolateNumber)(a, b); |
michael@0 | 5692 | } ]; |
michael@0 | 5693 | d3.interpolateArray = d3_interpolateArray; |
michael@0 | 5694 | function d3_interpolateArray(a, b) { |
michael@0 | 5695 | var x = [], c = [], na = a.length, nb = b.length, n0 = Math.min(a.length, b.length), i; |
michael@0 | 5696 | for (i = 0; i < n0; ++i) x.push(d3_interpolate(a[i], b[i])); |
michael@0 | 5697 | for (;i < na; ++i) c[i] = a[i]; |
michael@0 | 5698 | for (;i < nb; ++i) c[i] = b[i]; |
michael@0 | 5699 | return function(t) { |
michael@0 | 5700 | for (i = 0; i < n0; ++i) c[i] = x[i](t); |
michael@0 | 5701 | return c; |
michael@0 | 5702 | }; |
michael@0 | 5703 | } |
michael@0 | 5704 | var d3_ease_default = function() { |
michael@0 | 5705 | return d3_identity; |
michael@0 | 5706 | }; |
michael@0 | 5707 | var d3_ease = d3.map({ |
michael@0 | 5708 | linear: d3_ease_default, |
michael@0 | 5709 | poly: d3_ease_poly, |
michael@0 | 5710 | quad: function() { |
michael@0 | 5711 | return d3_ease_quad; |
michael@0 | 5712 | }, |
michael@0 | 5713 | cubic: function() { |
michael@0 | 5714 | return d3_ease_cubic; |
michael@0 | 5715 | }, |
michael@0 | 5716 | sin: function() { |
michael@0 | 5717 | return d3_ease_sin; |
michael@0 | 5718 | }, |
michael@0 | 5719 | exp: function() { |
michael@0 | 5720 | return d3_ease_exp; |
michael@0 | 5721 | }, |
michael@0 | 5722 | circle: function() { |
michael@0 | 5723 | return d3_ease_circle; |
michael@0 | 5724 | }, |
michael@0 | 5725 | elastic: d3_ease_elastic, |
michael@0 | 5726 | back: d3_ease_back, |
michael@0 | 5727 | bounce: function() { |
michael@0 | 5728 | return d3_ease_bounce; |
michael@0 | 5729 | } |
michael@0 | 5730 | }); |
michael@0 | 5731 | var d3_ease_mode = d3.map({ |
michael@0 | 5732 | "in": d3_identity, |
michael@0 | 5733 | out: d3_ease_reverse, |
michael@0 | 5734 | "in-out": d3_ease_reflect, |
michael@0 | 5735 | "out-in": function(f) { |
michael@0 | 5736 | return d3_ease_reflect(d3_ease_reverse(f)); |
michael@0 | 5737 | } |
michael@0 | 5738 | }); |
michael@0 | 5739 | d3.ease = function(name) { |
michael@0 | 5740 | var i = name.indexOf("-"), t = i >= 0 ? name.substring(0, i) : name, m = i >= 0 ? name.substring(i + 1) : "in"; |
michael@0 | 5741 | t = d3_ease.get(t) || d3_ease_default; |
michael@0 | 5742 | m = d3_ease_mode.get(m) || d3_identity; |
michael@0 | 5743 | return d3_ease_clamp(m(t.apply(null, d3_arraySlice.call(arguments, 1)))); |
michael@0 | 5744 | }; |
michael@0 | 5745 | function d3_ease_clamp(f) { |
michael@0 | 5746 | return function(t) { |
michael@0 | 5747 | return t <= 0 ? 0 : t >= 1 ? 1 : f(t); |
michael@0 | 5748 | }; |
michael@0 | 5749 | } |
michael@0 | 5750 | function d3_ease_reverse(f) { |
michael@0 | 5751 | return function(t) { |
michael@0 | 5752 | return 1 - f(1 - t); |
michael@0 | 5753 | }; |
michael@0 | 5754 | } |
michael@0 | 5755 | function d3_ease_reflect(f) { |
michael@0 | 5756 | return function(t) { |
michael@0 | 5757 | return .5 * (t < .5 ? f(2 * t) : 2 - f(2 - 2 * t)); |
michael@0 | 5758 | }; |
michael@0 | 5759 | } |
michael@0 | 5760 | function d3_ease_quad(t) { |
michael@0 | 5761 | return t * t; |
michael@0 | 5762 | } |
michael@0 | 5763 | function d3_ease_cubic(t) { |
michael@0 | 5764 | return t * t * t; |
michael@0 | 5765 | } |
michael@0 | 5766 | function d3_ease_cubicInOut(t) { |
michael@0 | 5767 | if (t <= 0) return 0; |
michael@0 | 5768 | if (t >= 1) return 1; |
michael@0 | 5769 | var t2 = t * t, t3 = t2 * t; |
michael@0 | 5770 | return 4 * (t < .5 ? t3 : 3 * (t - t2) + t3 - .75); |
michael@0 | 5771 | } |
michael@0 | 5772 | function d3_ease_poly(e) { |
michael@0 | 5773 | return function(t) { |
michael@0 | 5774 | return Math.pow(t, e); |
michael@0 | 5775 | }; |
michael@0 | 5776 | } |
michael@0 | 5777 | function d3_ease_sin(t) { |
michael@0 | 5778 | return 1 - Math.cos(t * halfπ); |
michael@0 | 5779 | } |
michael@0 | 5780 | function d3_ease_exp(t) { |
michael@0 | 5781 | return Math.pow(2, 10 * (t - 1)); |
michael@0 | 5782 | } |
michael@0 | 5783 | function d3_ease_circle(t) { |
michael@0 | 5784 | return 1 - Math.sqrt(1 - t * t); |
michael@0 | 5785 | } |
michael@0 | 5786 | function d3_ease_elastic(a, p) { |
michael@0 | 5787 | var s; |
michael@0 | 5788 | if (arguments.length < 2) p = .45; |
michael@0 | 5789 | if (arguments.length) s = p / τ * Math.asin(1 / a); else a = 1, s = p / 4; |
michael@0 | 5790 | return function(t) { |
michael@0 | 5791 | return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * τ / p); |
michael@0 | 5792 | }; |
michael@0 | 5793 | } |
michael@0 | 5794 | function d3_ease_back(s) { |
michael@0 | 5795 | if (!s) s = 1.70158; |
michael@0 | 5796 | return function(t) { |
michael@0 | 5797 | return t * t * ((s + 1) * t - s); |
michael@0 | 5798 | }; |
michael@0 | 5799 | } |
michael@0 | 5800 | function d3_ease_bounce(t) { |
michael@0 | 5801 | return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; |
michael@0 | 5802 | } |
michael@0 | 5803 | d3.interpolateHcl = d3_interpolateHcl; |
michael@0 | 5804 | function d3_interpolateHcl(a, b) { |
michael@0 | 5805 | a = d3.hcl(a); |
michael@0 | 5806 | b = d3.hcl(b); |
michael@0 | 5807 | var ah = a.h, ac = a.c, al = a.l, bh = b.h - ah, bc = b.c - ac, bl = b.l - al; |
michael@0 | 5808 | if (isNaN(bc)) bc = 0, ac = isNaN(ac) ? b.c : ac; |
michael@0 | 5809 | if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; |
michael@0 | 5810 | return function(t) { |
michael@0 | 5811 | return d3_hcl_lab(ah + bh * t, ac + bc * t, al + bl * t) + ""; |
michael@0 | 5812 | }; |
michael@0 | 5813 | } |
michael@0 | 5814 | d3.interpolateHsl = d3_interpolateHsl; |
michael@0 | 5815 | function d3_interpolateHsl(a, b) { |
michael@0 | 5816 | a = d3.hsl(a); |
michael@0 | 5817 | b = d3.hsl(b); |
michael@0 | 5818 | var ah = a.h, as = a.s, al = a.l, bh = b.h - ah, bs = b.s - as, bl = b.l - al; |
michael@0 | 5819 | if (isNaN(bs)) bs = 0, as = isNaN(as) ? b.s : as; |
michael@0 | 5820 | if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; |
michael@0 | 5821 | return function(t) { |
michael@0 | 5822 | return d3_hsl_rgb(ah + bh * t, as + bs * t, al + bl * t) + ""; |
michael@0 | 5823 | }; |
michael@0 | 5824 | } |
michael@0 | 5825 | d3.interpolateLab = d3_interpolateLab; |
michael@0 | 5826 | function d3_interpolateLab(a, b) { |
michael@0 | 5827 | a = d3.lab(a); |
michael@0 | 5828 | b = d3.lab(b); |
michael@0 | 5829 | var al = a.l, aa = a.a, ab = a.b, bl = b.l - al, ba = b.a - aa, bb = b.b - ab; |
michael@0 | 5830 | return function(t) { |
michael@0 | 5831 | return d3_lab_rgb(al + bl * t, aa + ba * t, ab + bb * t) + ""; |
michael@0 | 5832 | }; |
michael@0 | 5833 | } |
michael@0 | 5834 | d3.interpolateRound = d3_interpolateRound; |
michael@0 | 5835 | function d3_interpolateRound(a, b) { |
michael@0 | 5836 | b -= a; |
michael@0 | 5837 | return function(t) { |
michael@0 | 5838 | return Math.round(a + b * t); |
michael@0 | 5839 | }; |
michael@0 | 5840 | } |
michael@0 | 5841 | d3.transform = function(string) { |
michael@0 | 5842 | var g = d3_document.createElementNS(d3.ns.prefix.svg, "g"); |
michael@0 | 5843 | return (d3.transform = function(string) { |
michael@0 | 5844 | if (string != null) { |
michael@0 | 5845 | g.setAttribute("transform", string); |
michael@0 | 5846 | var t = g.transform.baseVal.consolidate(); |
michael@0 | 5847 | } |
michael@0 | 5848 | return new d3_transform(t ? t.matrix : d3_transformIdentity); |
michael@0 | 5849 | })(string); |
michael@0 | 5850 | }; |
michael@0 | 5851 | function d3_transform(m) { |
michael@0 | 5852 | var r0 = [ m.a, m.b ], r1 = [ m.c, m.d ], kx = d3_transformNormalize(r0), kz = d3_transformDot(r0, r1), ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0; |
michael@0 | 5853 | if (r0[0] * r1[1] < r1[0] * r0[1]) { |
michael@0 | 5854 | r0[0] *= -1; |
michael@0 | 5855 | r0[1] *= -1; |
michael@0 | 5856 | kx *= -1; |
michael@0 | 5857 | kz *= -1; |
michael@0 | 5858 | } |
michael@0 | 5859 | this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_degrees; |
michael@0 | 5860 | this.translate = [ m.e, m.f ]; |
michael@0 | 5861 | this.scale = [ kx, ky ]; |
michael@0 | 5862 | this.skew = ky ? Math.atan2(kz, ky) * d3_degrees : 0; |
michael@0 | 5863 | } |
michael@0 | 5864 | d3_transform.prototype.toString = function() { |
michael@0 | 5865 | return "translate(" + this.translate + ")rotate(" + this.rotate + ")skewX(" + this.skew + ")scale(" + this.scale + ")"; |
michael@0 | 5866 | }; |
michael@0 | 5867 | function d3_transformDot(a, b) { |
michael@0 | 5868 | return a[0] * b[0] + a[1] * b[1]; |
michael@0 | 5869 | } |
michael@0 | 5870 | function d3_transformNormalize(a) { |
michael@0 | 5871 | var k = Math.sqrt(d3_transformDot(a, a)); |
michael@0 | 5872 | if (k) { |
michael@0 | 5873 | a[0] /= k; |
michael@0 | 5874 | a[1] /= k; |
michael@0 | 5875 | } |
michael@0 | 5876 | return k; |
michael@0 | 5877 | } |
michael@0 | 5878 | function d3_transformCombine(a, b, k) { |
michael@0 | 5879 | a[0] += k * b[0]; |
michael@0 | 5880 | a[1] += k * b[1]; |
michael@0 | 5881 | return a; |
michael@0 | 5882 | } |
michael@0 | 5883 | var d3_transformIdentity = { |
michael@0 | 5884 | a: 1, |
michael@0 | 5885 | b: 0, |
michael@0 | 5886 | c: 0, |
michael@0 | 5887 | d: 1, |
michael@0 | 5888 | e: 0, |
michael@0 | 5889 | f: 0 |
michael@0 | 5890 | }; |
michael@0 | 5891 | d3.interpolateTransform = d3_interpolateTransform; |
michael@0 | 5892 | function d3_interpolateTransform(a, b) { |
michael@0 | 5893 | var s = [], q = [], n, A = d3.transform(a), B = d3.transform(b), ta = A.translate, tb = B.translate, ra = A.rotate, rb = B.rotate, wa = A.skew, wb = B.skew, ka = A.scale, kb = B.scale; |
michael@0 | 5894 | if (ta[0] != tb[0] || ta[1] != tb[1]) { |
michael@0 | 5895 | s.push("translate(", null, ",", null, ")"); |
michael@0 | 5896 | q.push({ |
michael@0 | 5897 | i: 1, |
michael@0 | 5898 | x: d3_interpolateNumber(ta[0], tb[0]) |
michael@0 | 5899 | }, { |
michael@0 | 5900 | i: 3, |
michael@0 | 5901 | x: d3_interpolateNumber(ta[1], tb[1]) |
michael@0 | 5902 | }); |
michael@0 | 5903 | } else if (tb[0] || tb[1]) { |
michael@0 | 5904 | s.push("translate(" + tb + ")"); |
michael@0 | 5905 | } else { |
michael@0 | 5906 | s.push(""); |
michael@0 | 5907 | } |
michael@0 | 5908 | if (ra != rb) { |
michael@0 | 5909 | if (ra - rb > 180) rb += 360; else if (rb - ra > 180) ra += 360; |
michael@0 | 5910 | q.push({ |
michael@0 | 5911 | i: s.push(s.pop() + "rotate(", null, ")") - 2, |
michael@0 | 5912 | x: d3_interpolateNumber(ra, rb) |
michael@0 | 5913 | }); |
michael@0 | 5914 | } else if (rb) { |
michael@0 | 5915 | s.push(s.pop() + "rotate(" + rb + ")"); |
michael@0 | 5916 | } |
michael@0 | 5917 | if (wa != wb) { |
michael@0 | 5918 | q.push({ |
michael@0 | 5919 | i: s.push(s.pop() + "skewX(", null, ")") - 2, |
michael@0 | 5920 | x: d3_interpolateNumber(wa, wb) |
michael@0 | 5921 | }); |
michael@0 | 5922 | } else if (wb) { |
michael@0 | 5923 | s.push(s.pop() + "skewX(" + wb + ")"); |
michael@0 | 5924 | } |
michael@0 | 5925 | if (ka[0] != kb[0] || ka[1] != kb[1]) { |
michael@0 | 5926 | n = s.push(s.pop() + "scale(", null, ",", null, ")"); |
michael@0 | 5927 | q.push({ |
michael@0 | 5928 | i: n - 4, |
michael@0 | 5929 | x: d3_interpolateNumber(ka[0], kb[0]) |
michael@0 | 5930 | }, { |
michael@0 | 5931 | i: n - 2, |
michael@0 | 5932 | x: d3_interpolateNumber(ka[1], kb[1]) |
michael@0 | 5933 | }); |
michael@0 | 5934 | } else if (kb[0] != 1 || kb[1] != 1) { |
michael@0 | 5935 | s.push(s.pop() + "scale(" + kb + ")"); |
michael@0 | 5936 | } |
michael@0 | 5937 | n = q.length; |
michael@0 | 5938 | return function(t) { |
michael@0 | 5939 | var i = -1, o; |
michael@0 | 5940 | while (++i < n) s[(o = q[i]).i] = o.x(t); |
michael@0 | 5941 | return s.join(""); |
michael@0 | 5942 | }; |
michael@0 | 5943 | } |
michael@0 | 5944 | function d3_uninterpolateNumber(a, b) { |
michael@0 | 5945 | b = b - (a = +a) ? 1 / (b - a) : 0; |
michael@0 | 5946 | return function(x) { |
michael@0 | 5947 | return (x - a) * b; |
michael@0 | 5948 | }; |
michael@0 | 5949 | } |
michael@0 | 5950 | function d3_uninterpolateClamp(a, b) { |
michael@0 | 5951 | b = b - (a = +a) ? 1 / (b - a) : 0; |
michael@0 | 5952 | return function(x) { |
michael@0 | 5953 | return Math.max(0, Math.min(1, (x - a) * b)); |
michael@0 | 5954 | }; |
michael@0 | 5955 | } |
michael@0 | 5956 | d3.layout = {}; |
michael@0 | 5957 | d3.layout.bundle = function() { |
michael@0 | 5958 | return function(links) { |
michael@0 | 5959 | var paths = [], i = -1, n = links.length; |
michael@0 | 5960 | while (++i < n) paths.push(d3_layout_bundlePath(links[i])); |
michael@0 | 5961 | return paths; |
michael@0 | 5962 | }; |
michael@0 | 5963 | }; |
michael@0 | 5964 | function d3_layout_bundlePath(link) { |
michael@0 | 5965 | var start = link.source, end = link.target, lca = d3_layout_bundleLeastCommonAncestor(start, end), points = [ start ]; |
michael@0 | 5966 | while (start !== lca) { |
michael@0 | 5967 | start = start.parent; |
michael@0 | 5968 | points.push(start); |
michael@0 | 5969 | } |
michael@0 | 5970 | var k = points.length; |
michael@0 | 5971 | while (end !== lca) { |
michael@0 | 5972 | points.splice(k, 0, end); |
michael@0 | 5973 | end = end.parent; |
michael@0 | 5974 | } |
michael@0 | 5975 | return points; |
michael@0 | 5976 | } |
michael@0 | 5977 | function d3_layout_bundleAncestors(node) { |
michael@0 | 5978 | var ancestors = [], parent = node.parent; |
michael@0 | 5979 | while (parent != null) { |
michael@0 | 5980 | ancestors.push(node); |
michael@0 | 5981 | node = parent; |
michael@0 | 5982 | parent = parent.parent; |
michael@0 | 5983 | } |
michael@0 | 5984 | ancestors.push(node); |
michael@0 | 5985 | return ancestors; |
michael@0 | 5986 | } |
michael@0 | 5987 | function d3_layout_bundleLeastCommonAncestor(a, b) { |
michael@0 | 5988 | if (a === b) return a; |
michael@0 | 5989 | var aNodes = d3_layout_bundleAncestors(a), bNodes = d3_layout_bundleAncestors(b), aNode = aNodes.pop(), bNode = bNodes.pop(), sharedNode = null; |
michael@0 | 5990 | while (aNode === bNode) { |
michael@0 | 5991 | sharedNode = aNode; |
michael@0 | 5992 | aNode = aNodes.pop(); |
michael@0 | 5993 | bNode = bNodes.pop(); |
michael@0 | 5994 | } |
michael@0 | 5995 | return sharedNode; |
michael@0 | 5996 | } |
michael@0 | 5997 | d3.layout.chord = function() { |
michael@0 | 5998 | var chord = {}, chords, groups, matrix, n, padding = 0, sortGroups, sortSubgroups, sortChords; |
michael@0 | 5999 | function relayout() { |
michael@0 | 6000 | var subgroups = {}, groupSums = [], groupIndex = d3.range(n), subgroupIndex = [], k, x, x0, i, j; |
michael@0 | 6001 | chords = []; |
michael@0 | 6002 | groups = []; |
michael@0 | 6003 | k = 0, i = -1; |
michael@0 | 6004 | while (++i < n) { |
michael@0 | 6005 | x = 0, j = -1; |
michael@0 | 6006 | while (++j < n) { |
michael@0 | 6007 | x += matrix[i][j]; |
michael@0 | 6008 | } |
michael@0 | 6009 | groupSums.push(x); |
michael@0 | 6010 | subgroupIndex.push(d3.range(n)); |
michael@0 | 6011 | k += x; |
michael@0 | 6012 | } |
michael@0 | 6013 | if (sortGroups) { |
michael@0 | 6014 | groupIndex.sort(function(a, b) { |
michael@0 | 6015 | return sortGroups(groupSums[a], groupSums[b]); |
michael@0 | 6016 | }); |
michael@0 | 6017 | } |
michael@0 | 6018 | if (sortSubgroups) { |
michael@0 | 6019 | subgroupIndex.forEach(function(d, i) { |
michael@0 | 6020 | d.sort(function(a, b) { |
michael@0 | 6021 | return sortSubgroups(matrix[i][a], matrix[i][b]); |
michael@0 | 6022 | }); |
michael@0 | 6023 | }); |
michael@0 | 6024 | } |
michael@0 | 6025 | k = (τ - padding * n) / k; |
michael@0 | 6026 | x = 0, i = -1; |
michael@0 | 6027 | while (++i < n) { |
michael@0 | 6028 | x0 = x, j = -1; |
michael@0 | 6029 | while (++j < n) { |
michael@0 | 6030 | var di = groupIndex[i], dj = subgroupIndex[di][j], v = matrix[di][dj], a0 = x, a1 = x += v * k; |
michael@0 | 6031 | subgroups[di + "-" + dj] = { |
michael@0 | 6032 | index: di, |
michael@0 | 6033 | subindex: dj, |
michael@0 | 6034 | startAngle: a0, |
michael@0 | 6035 | endAngle: a1, |
michael@0 | 6036 | value: v |
michael@0 | 6037 | }; |
michael@0 | 6038 | } |
michael@0 | 6039 | groups[di] = { |
michael@0 | 6040 | index: di, |
michael@0 | 6041 | startAngle: x0, |
michael@0 | 6042 | endAngle: x, |
michael@0 | 6043 | value: (x - x0) / k |
michael@0 | 6044 | }; |
michael@0 | 6045 | x += padding; |
michael@0 | 6046 | } |
michael@0 | 6047 | i = -1; |
michael@0 | 6048 | while (++i < n) { |
michael@0 | 6049 | j = i - 1; |
michael@0 | 6050 | while (++j < n) { |
michael@0 | 6051 | var source = subgroups[i + "-" + j], target = subgroups[j + "-" + i]; |
michael@0 | 6052 | if (source.value || target.value) { |
michael@0 | 6053 | chords.push(source.value < target.value ? { |
michael@0 | 6054 | source: target, |
michael@0 | 6055 | target: source |
michael@0 | 6056 | } : { |
michael@0 | 6057 | source: source, |
michael@0 | 6058 | target: target |
michael@0 | 6059 | }); |
michael@0 | 6060 | } |
michael@0 | 6061 | } |
michael@0 | 6062 | } |
michael@0 | 6063 | if (sortChords) resort(); |
michael@0 | 6064 | } |
michael@0 | 6065 | function resort() { |
michael@0 | 6066 | chords.sort(function(a, b) { |
michael@0 | 6067 | return sortChords((a.source.value + a.target.value) / 2, (b.source.value + b.target.value) / 2); |
michael@0 | 6068 | }); |
michael@0 | 6069 | } |
michael@0 | 6070 | chord.matrix = function(x) { |
michael@0 | 6071 | if (!arguments.length) return matrix; |
michael@0 | 6072 | n = (matrix = x) && matrix.length; |
michael@0 | 6073 | chords = groups = null; |
michael@0 | 6074 | return chord; |
michael@0 | 6075 | }; |
michael@0 | 6076 | chord.padding = function(x) { |
michael@0 | 6077 | if (!arguments.length) return padding; |
michael@0 | 6078 | padding = x; |
michael@0 | 6079 | chords = groups = null; |
michael@0 | 6080 | return chord; |
michael@0 | 6081 | }; |
michael@0 | 6082 | chord.sortGroups = function(x) { |
michael@0 | 6083 | if (!arguments.length) return sortGroups; |
michael@0 | 6084 | sortGroups = x; |
michael@0 | 6085 | chords = groups = null; |
michael@0 | 6086 | return chord; |
michael@0 | 6087 | }; |
michael@0 | 6088 | chord.sortSubgroups = function(x) { |
michael@0 | 6089 | if (!arguments.length) return sortSubgroups; |
michael@0 | 6090 | sortSubgroups = x; |
michael@0 | 6091 | chords = null; |
michael@0 | 6092 | return chord; |
michael@0 | 6093 | }; |
michael@0 | 6094 | chord.sortChords = function(x) { |
michael@0 | 6095 | if (!arguments.length) return sortChords; |
michael@0 | 6096 | sortChords = x; |
michael@0 | 6097 | if (chords) resort(); |
michael@0 | 6098 | return chord; |
michael@0 | 6099 | }; |
michael@0 | 6100 | chord.chords = function() { |
michael@0 | 6101 | if (!chords) relayout(); |
michael@0 | 6102 | return chords; |
michael@0 | 6103 | }; |
michael@0 | 6104 | chord.groups = function() { |
michael@0 | 6105 | if (!groups) relayout(); |
michael@0 | 6106 | return groups; |
michael@0 | 6107 | }; |
michael@0 | 6108 | return chord; |
michael@0 | 6109 | }; |
michael@0 | 6110 | d3.layout.force = function() { |
michael@0 | 6111 | var force = {}, event = d3.dispatch("start", "tick", "end"), size = [ 1, 1 ], drag, alpha, friction = .9, linkDistance = d3_layout_forceLinkDistance, linkStrength = d3_layout_forceLinkStrength, charge = -30, chargeDistance2 = d3_layout_forceChargeDistance2, gravity = .1, theta2 = .64, nodes = [], links = [], distances, strengths, charges; |
michael@0 | 6112 | function repulse(node) { |
michael@0 | 6113 | return function(quad, x1, _, x2) { |
michael@0 | 6114 | if (quad.point !== node) { |
michael@0 | 6115 | var dx = quad.cx - node.x, dy = quad.cy - node.y, dw = x2 - x1, dn = dx * dx + dy * dy; |
michael@0 | 6116 | if (dw * dw / theta2 < dn) { |
michael@0 | 6117 | if (dn < chargeDistance2) { |
michael@0 | 6118 | var k = quad.charge / dn; |
michael@0 | 6119 | node.px -= dx * k; |
michael@0 | 6120 | node.py -= dy * k; |
michael@0 | 6121 | } |
michael@0 | 6122 | return true; |
michael@0 | 6123 | } |
michael@0 | 6124 | if (quad.point && dn && dn < chargeDistance2) { |
michael@0 | 6125 | var k = quad.pointCharge / dn; |
michael@0 | 6126 | node.px -= dx * k; |
michael@0 | 6127 | node.py -= dy * k; |
michael@0 | 6128 | } |
michael@0 | 6129 | } |
michael@0 | 6130 | return !quad.charge; |
michael@0 | 6131 | }; |
michael@0 | 6132 | } |
michael@0 | 6133 | force.tick = function() { |
michael@0 | 6134 | if ((alpha *= .99) < .005) { |
michael@0 | 6135 | event.end({ |
michael@0 | 6136 | type: "end", |
michael@0 | 6137 | alpha: alpha = 0 |
michael@0 | 6138 | }); |
michael@0 | 6139 | return true; |
michael@0 | 6140 | } |
michael@0 | 6141 | var n = nodes.length, m = links.length, q, i, o, s, t, l, k, x, y; |
michael@0 | 6142 | for (i = 0; i < m; ++i) { |
michael@0 | 6143 | o = links[i]; |
michael@0 | 6144 | s = o.source; |
michael@0 | 6145 | t = o.target; |
michael@0 | 6146 | x = t.x - s.x; |
michael@0 | 6147 | y = t.y - s.y; |
michael@0 | 6148 | if (l = x * x + y * y) { |
michael@0 | 6149 | l = alpha * strengths[i] * ((l = Math.sqrt(l)) - distances[i]) / l; |
michael@0 | 6150 | x *= l; |
michael@0 | 6151 | y *= l; |
michael@0 | 6152 | t.x -= x * (k = s.weight / (t.weight + s.weight)); |
michael@0 | 6153 | t.y -= y * k; |
michael@0 | 6154 | s.x += x * (k = 1 - k); |
michael@0 | 6155 | s.y += y * k; |
michael@0 | 6156 | } |
michael@0 | 6157 | } |
michael@0 | 6158 | if (k = alpha * gravity) { |
michael@0 | 6159 | x = size[0] / 2; |
michael@0 | 6160 | y = size[1] / 2; |
michael@0 | 6161 | i = -1; |
michael@0 | 6162 | if (k) while (++i < n) { |
michael@0 | 6163 | o = nodes[i]; |
michael@0 | 6164 | o.x += (x - o.x) * k; |
michael@0 | 6165 | o.y += (y - o.y) * k; |
michael@0 | 6166 | } |
michael@0 | 6167 | } |
michael@0 | 6168 | if (charge) { |
michael@0 | 6169 | d3_layout_forceAccumulate(q = d3.geom.quadtree(nodes), alpha, charges); |
michael@0 | 6170 | i = -1; |
michael@0 | 6171 | while (++i < n) { |
michael@0 | 6172 | if (!(o = nodes[i]).fixed) { |
michael@0 | 6173 | q.visit(repulse(o)); |
michael@0 | 6174 | } |
michael@0 | 6175 | } |
michael@0 | 6176 | } |
michael@0 | 6177 | i = -1; |
michael@0 | 6178 | while (++i < n) { |
michael@0 | 6179 | o = nodes[i]; |
michael@0 | 6180 | if (o.fixed) { |
michael@0 | 6181 | o.x = o.px; |
michael@0 | 6182 | o.y = o.py; |
michael@0 | 6183 | } else { |
michael@0 | 6184 | o.x -= (o.px - (o.px = o.x)) * friction; |
michael@0 | 6185 | o.y -= (o.py - (o.py = o.y)) * friction; |
michael@0 | 6186 | } |
michael@0 | 6187 | } |
michael@0 | 6188 | event.tick({ |
michael@0 | 6189 | type: "tick", |
michael@0 | 6190 | alpha: alpha |
michael@0 | 6191 | }); |
michael@0 | 6192 | }; |
michael@0 | 6193 | force.nodes = function(x) { |
michael@0 | 6194 | if (!arguments.length) return nodes; |
michael@0 | 6195 | nodes = x; |
michael@0 | 6196 | return force; |
michael@0 | 6197 | }; |
michael@0 | 6198 | force.links = function(x) { |
michael@0 | 6199 | if (!arguments.length) return links; |
michael@0 | 6200 | links = x; |
michael@0 | 6201 | return force; |
michael@0 | 6202 | }; |
michael@0 | 6203 | force.size = function(x) { |
michael@0 | 6204 | if (!arguments.length) return size; |
michael@0 | 6205 | size = x; |
michael@0 | 6206 | return force; |
michael@0 | 6207 | }; |
michael@0 | 6208 | force.linkDistance = function(x) { |
michael@0 | 6209 | if (!arguments.length) return linkDistance; |
michael@0 | 6210 | linkDistance = typeof x === "function" ? x : +x; |
michael@0 | 6211 | return force; |
michael@0 | 6212 | }; |
michael@0 | 6213 | force.distance = force.linkDistance; |
michael@0 | 6214 | force.linkStrength = function(x) { |
michael@0 | 6215 | if (!arguments.length) return linkStrength; |
michael@0 | 6216 | linkStrength = typeof x === "function" ? x : +x; |
michael@0 | 6217 | return force; |
michael@0 | 6218 | }; |
michael@0 | 6219 | force.friction = function(x) { |
michael@0 | 6220 | if (!arguments.length) return friction; |
michael@0 | 6221 | friction = +x; |
michael@0 | 6222 | return force; |
michael@0 | 6223 | }; |
michael@0 | 6224 | force.charge = function(x) { |
michael@0 | 6225 | if (!arguments.length) return charge; |
michael@0 | 6226 | charge = typeof x === "function" ? x : +x; |
michael@0 | 6227 | return force; |
michael@0 | 6228 | }; |
michael@0 | 6229 | force.chargeDistance = function(x) { |
michael@0 | 6230 | if (!arguments.length) return Math.sqrt(chargeDistance2); |
michael@0 | 6231 | chargeDistance2 = x * x; |
michael@0 | 6232 | return force; |
michael@0 | 6233 | }; |
michael@0 | 6234 | force.gravity = function(x) { |
michael@0 | 6235 | if (!arguments.length) return gravity; |
michael@0 | 6236 | gravity = +x; |
michael@0 | 6237 | return force; |
michael@0 | 6238 | }; |
michael@0 | 6239 | force.theta = function(x) { |
michael@0 | 6240 | if (!arguments.length) return Math.sqrt(theta2); |
michael@0 | 6241 | theta2 = x * x; |
michael@0 | 6242 | return force; |
michael@0 | 6243 | }; |
michael@0 | 6244 | force.alpha = function(x) { |
michael@0 | 6245 | if (!arguments.length) return alpha; |
michael@0 | 6246 | x = +x; |
michael@0 | 6247 | if (alpha) { |
michael@0 | 6248 | if (x > 0) alpha = x; else alpha = 0; |
michael@0 | 6249 | } else if (x > 0) { |
michael@0 | 6250 | event.start({ |
michael@0 | 6251 | type: "start", |
michael@0 | 6252 | alpha: alpha = x |
michael@0 | 6253 | }); |
michael@0 | 6254 | d3.timer(force.tick); |
michael@0 | 6255 | } |
michael@0 | 6256 | return force; |
michael@0 | 6257 | }; |
michael@0 | 6258 | force.start = function() { |
michael@0 | 6259 | var i, n = nodes.length, m = links.length, w = size[0], h = size[1], neighbors, o; |
michael@0 | 6260 | for (i = 0; i < n; ++i) { |
michael@0 | 6261 | (o = nodes[i]).index = i; |
michael@0 | 6262 | o.weight = 0; |
michael@0 | 6263 | } |
michael@0 | 6264 | for (i = 0; i < m; ++i) { |
michael@0 | 6265 | o = links[i]; |
michael@0 | 6266 | if (typeof o.source == "number") o.source = nodes[o.source]; |
michael@0 | 6267 | if (typeof o.target == "number") o.target = nodes[o.target]; |
michael@0 | 6268 | ++o.source.weight; |
michael@0 | 6269 | ++o.target.weight; |
michael@0 | 6270 | } |
michael@0 | 6271 | for (i = 0; i < n; ++i) { |
michael@0 | 6272 | o = nodes[i]; |
michael@0 | 6273 | if (isNaN(o.x)) o.x = position("x", w); |
michael@0 | 6274 | if (isNaN(o.y)) o.y = position("y", h); |
michael@0 | 6275 | if (isNaN(o.px)) o.px = o.x; |
michael@0 | 6276 | if (isNaN(o.py)) o.py = o.y; |
michael@0 | 6277 | } |
michael@0 | 6278 | distances = []; |
michael@0 | 6279 | if (typeof linkDistance === "function") for (i = 0; i < m; ++i) distances[i] = +linkDistance.call(this, links[i], i); else for (i = 0; i < m; ++i) distances[i] = linkDistance; |
michael@0 | 6280 | strengths = []; |
michael@0 | 6281 | if (typeof linkStrength === "function") for (i = 0; i < m; ++i) strengths[i] = +linkStrength.call(this, links[i], i); else for (i = 0; i < m; ++i) strengths[i] = linkStrength; |
michael@0 | 6282 | charges = []; |
michael@0 | 6283 | if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i); else for (i = 0; i < n; ++i) charges[i] = charge; |
michael@0 | 6284 | function position(dimension, size) { |
michael@0 | 6285 | if (!neighbors) { |
michael@0 | 6286 | neighbors = new Array(n); |
michael@0 | 6287 | for (j = 0; j < n; ++j) { |
michael@0 | 6288 | neighbors[j] = []; |
michael@0 | 6289 | } |
michael@0 | 6290 | for (j = 0; j < m; ++j) { |
michael@0 | 6291 | var o = links[j]; |
michael@0 | 6292 | neighbors[o.source.index].push(o.target); |
michael@0 | 6293 | neighbors[o.target.index].push(o.source); |
michael@0 | 6294 | } |
michael@0 | 6295 | } |
michael@0 | 6296 | var candidates = neighbors[i], j = -1, m = candidates.length, x; |
michael@0 | 6297 | while (++j < m) if (!isNaN(x = candidates[j][dimension])) return x; |
michael@0 | 6298 | return Math.random() * size; |
michael@0 | 6299 | } |
michael@0 | 6300 | return force.resume(); |
michael@0 | 6301 | }; |
michael@0 | 6302 | force.resume = function() { |
michael@0 | 6303 | return force.alpha(.1); |
michael@0 | 6304 | }; |
michael@0 | 6305 | force.stop = function() { |
michael@0 | 6306 | return force.alpha(0); |
michael@0 | 6307 | }; |
michael@0 | 6308 | force.drag = function() { |
michael@0 | 6309 | if (!drag) drag = d3.behavior.drag().origin(d3_identity).on("dragstart.force", d3_layout_forceDragstart).on("drag.force", dragmove).on("dragend.force", d3_layout_forceDragend); |
michael@0 | 6310 | if (!arguments.length) return drag; |
michael@0 | 6311 | this.on("mouseover.force", d3_layout_forceMouseover).on("mouseout.force", d3_layout_forceMouseout).call(drag); |
michael@0 | 6312 | }; |
michael@0 | 6313 | function dragmove(d) { |
michael@0 | 6314 | d.px = d3.event.x, d.py = d3.event.y; |
michael@0 | 6315 | force.resume(); |
michael@0 | 6316 | } |
michael@0 | 6317 | return d3.rebind(force, event, "on"); |
michael@0 | 6318 | }; |
michael@0 | 6319 | function d3_layout_forceDragstart(d) { |
michael@0 | 6320 | d.fixed |= 2; |
michael@0 | 6321 | } |
michael@0 | 6322 | function d3_layout_forceDragend(d) { |
michael@0 | 6323 | d.fixed &= ~6; |
michael@0 | 6324 | } |
michael@0 | 6325 | function d3_layout_forceMouseover(d) { |
michael@0 | 6326 | d.fixed |= 4; |
michael@0 | 6327 | d.px = d.x, d.py = d.y; |
michael@0 | 6328 | } |
michael@0 | 6329 | function d3_layout_forceMouseout(d) { |
michael@0 | 6330 | d.fixed &= ~4; |
michael@0 | 6331 | } |
michael@0 | 6332 | function d3_layout_forceAccumulate(quad, alpha, charges) { |
michael@0 | 6333 | var cx = 0, cy = 0; |
michael@0 | 6334 | quad.charge = 0; |
michael@0 | 6335 | if (!quad.leaf) { |
michael@0 | 6336 | var nodes = quad.nodes, n = nodes.length, i = -1, c; |
michael@0 | 6337 | while (++i < n) { |
michael@0 | 6338 | c = nodes[i]; |
michael@0 | 6339 | if (c == null) continue; |
michael@0 | 6340 | d3_layout_forceAccumulate(c, alpha, charges); |
michael@0 | 6341 | quad.charge += c.charge; |
michael@0 | 6342 | cx += c.charge * c.cx; |
michael@0 | 6343 | cy += c.charge * c.cy; |
michael@0 | 6344 | } |
michael@0 | 6345 | } |
michael@0 | 6346 | if (quad.point) { |
michael@0 | 6347 | if (!quad.leaf) { |
michael@0 | 6348 | quad.point.x += Math.random() - .5; |
michael@0 | 6349 | quad.point.y += Math.random() - .5; |
michael@0 | 6350 | } |
michael@0 | 6351 | var k = alpha * charges[quad.point.index]; |
michael@0 | 6352 | quad.charge += quad.pointCharge = k; |
michael@0 | 6353 | cx += k * quad.point.x; |
michael@0 | 6354 | cy += k * quad.point.y; |
michael@0 | 6355 | } |
michael@0 | 6356 | quad.cx = cx / quad.charge; |
michael@0 | 6357 | quad.cy = cy / quad.charge; |
michael@0 | 6358 | } |
michael@0 | 6359 | var d3_layout_forceLinkDistance = 20, d3_layout_forceLinkStrength = 1, d3_layout_forceChargeDistance2 = Infinity; |
michael@0 | 6360 | d3.layout.hierarchy = function() { |
michael@0 | 6361 | var sort = d3_layout_hierarchySort, children = d3_layout_hierarchyChildren, value = d3_layout_hierarchyValue; |
michael@0 | 6362 | function recurse(node, depth, nodes) { |
michael@0 | 6363 | var childs = children.call(hierarchy, node, depth); |
michael@0 | 6364 | node.depth = depth; |
michael@0 | 6365 | nodes.push(node); |
michael@0 | 6366 | if (childs && (n = childs.length)) { |
michael@0 | 6367 | var i = -1, n, c = node.children = new Array(n), v = 0, j = depth + 1, d; |
michael@0 | 6368 | while (++i < n) { |
michael@0 | 6369 | d = c[i] = recurse(childs[i], j, nodes); |
michael@0 | 6370 | d.parent = node; |
michael@0 | 6371 | v += d.value; |
michael@0 | 6372 | } |
michael@0 | 6373 | if (sort) c.sort(sort); |
michael@0 | 6374 | if (value) node.value = v; |
michael@0 | 6375 | } else { |
michael@0 | 6376 | delete node.children; |
michael@0 | 6377 | if (value) { |
michael@0 | 6378 | node.value = +value.call(hierarchy, node, depth) || 0; |
michael@0 | 6379 | } |
michael@0 | 6380 | } |
michael@0 | 6381 | return node; |
michael@0 | 6382 | } |
michael@0 | 6383 | function revalue(node, depth) { |
michael@0 | 6384 | var children = node.children, v = 0; |
michael@0 | 6385 | if (children && (n = children.length)) { |
michael@0 | 6386 | var i = -1, n, j = depth + 1; |
michael@0 | 6387 | while (++i < n) v += revalue(children[i], j); |
michael@0 | 6388 | } else if (value) { |
michael@0 | 6389 | v = +value.call(hierarchy, node, depth) || 0; |
michael@0 | 6390 | } |
michael@0 | 6391 | if (value) node.value = v; |
michael@0 | 6392 | return v; |
michael@0 | 6393 | } |
michael@0 | 6394 | function hierarchy(d) { |
michael@0 | 6395 | var nodes = []; |
michael@0 | 6396 | recurse(d, 0, nodes); |
michael@0 | 6397 | return nodes; |
michael@0 | 6398 | } |
michael@0 | 6399 | hierarchy.sort = function(x) { |
michael@0 | 6400 | if (!arguments.length) return sort; |
michael@0 | 6401 | sort = x; |
michael@0 | 6402 | return hierarchy; |
michael@0 | 6403 | }; |
michael@0 | 6404 | hierarchy.children = function(x) { |
michael@0 | 6405 | if (!arguments.length) return children; |
michael@0 | 6406 | children = x; |
michael@0 | 6407 | return hierarchy; |
michael@0 | 6408 | }; |
michael@0 | 6409 | hierarchy.value = function(x) { |
michael@0 | 6410 | if (!arguments.length) return value; |
michael@0 | 6411 | value = x; |
michael@0 | 6412 | return hierarchy; |
michael@0 | 6413 | }; |
michael@0 | 6414 | hierarchy.revalue = function(root) { |
michael@0 | 6415 | revalue(root, 0); |
michael@0 | 6416 | return root; |
michael@0 | 6417 | }; |
michael@0 | 6418 | return hierarchy; |
michael@0 | 6419 | }; |
michael@0 | 6420 | function d3_layout_hierarchyRebind(object, hierarchy) { |
michael@0 | 6421 | d3.rebind(object, hierarchy, "sort", "children", "value"); |
michael@0 | 6422 | object.nodes = object; |
michael@0 | 6423 | object.links = d3_layout_hierarchyLinks; |
michael@0 | 6424 | return object; |
michael@0 | 6425 | } |
michael@0 | 6426 | function d3_layout_hierarchyChildren(d) { |
michael@0 | 6427 | return d.children; |
michael@0 | 6428 | } |
michael@0 | 6429 | function d3_layout_hierarchyValue(d) { |
michael@0 | 6430 | return d.value; |
michael@0 | 6431 | } |
michael@0 | 6432 | function d3_layout_hierarchySort(a, b) { |
michael@0 | 6433 | return b.value - a.value; |
michael@0 | 6434 | } |
michael@0 | 6435 | function d3_layout_hierarchyLinks(nodes) { |
michael@0 | 6436 | return d3.merge(nodes.map(function(parent) { |
michael@0 | 6437 | return (parent.children || []).map(function(child) { |
michael@0 | 6438 | return { |
michael@0 | 6439 | source: parent, |
michael@0 | 6440 | target: child |
michael@0 | 6441 | }; |
michael@0 | 6442 | }); |
michael@0 | 6443 | })); |
michael@0 | 6444 | } |
michael@0 | 6445 | d3.layout.partition = function() { |
michael@0 | 6446 | var hierarchy = d3.layout.hierarchy(), size = [ 1, 1 ]; |
michael@0 | 6447 | function position(node, x, dx, dy) { |
michael@0 | 6448 | var children = node.children; |
michael@0 | 6449 | node.x = x; |
michael@0 | 6450 | node.y = node.depth * dy; |
michael@0 | 6451 | node.dx = dx; |
michael@0 | 6452 | node.dy = dy; |
michael@0 | 6453 | if (children && (n = children.length)) { |
michael@0 | 6454 | var i = -1, n, c, d; |
michael@0 | 6455 | dx = node.value ? dx / node.value : 0; |
michael@0 | 6456 | while (++i < n) { |
michael@0 | 6457 | position(c = children[i], x, d = c.value * dx, dy); |
michael@0 | 6458 | x += d; |
michael@0 | 6459 | } |
michael@0 | 6460 | } |
michael@0 | 6461 | } |
michael@0 | 6462 | function depth(node) { |
michael@0 | 6463 | var children = node.children, d = 0; |
michael@0 | 6464 | if (children && (n = children.length)) { |
michael@0 | 6465 | var i = -1, n; |
michael@0 | 6466 | while (++i < n) d = Math.max(d, depth(children[i])); |
michael@0 | 6467 | } |
michael@0 | 6468 | return 1 + d; |
michael@0 | 6469 | } |
michael@0 | 6470 | function partition(d, i) { |
michael@0 | 6471 | var nodes = hierarchy.call(this, d, i); |
michael@0 | 6472 | position(nodes[0], 0, size[0], size[1] / depth(nodes[0])); |
michael@0 | 6473 | return nodes; |
michael@0 | 6474 | } |
michael@0 | 6475 | partition.size = function(x) { |
michael@0 | 6476 | if (!arguments.length) return size; |
michael@0 | 6477 | size = x; |
michael@0 | 6478 | return partition; |
michael@0 | 6479 | }; |
michael@0 | 6480 | return d3_layout_hierarchyRebind(partition, hierarchy); |
michael@0 | 6481 | }; |
michael@0 | 6482 | d3.layout.pie = function() { |
michael@0 | 6483 | var value = Number, sort = d3_layout_pieSortByValue, startAngle = 0, endAngle = τ; |
michael@0 | 6484 | function pie(data) { |
michael@0 | 6485 | var values = data.map(function(d, i) { |
michael@0 | 6486 | return +value.call(pie, d, i); |
michael@0 | 6487 | }); |
michael@0 | 6488 | var a = +(typeof startAngle === "function" ? startAngle.apply(this, arguments) : startAngle); |
michael@0 | 6489 | var k = ((typeof endAngle === "function" ? endAngle.apply(this, arguments) : endAngle) - a) / d3.sum(values); |
michael@0 | 6490 | var index = d3.range(data.length); |
michael@0 | 6491 | if (sort != null) index.sort(sort === d3_layout_pieSortByValue ? function(i, j) { |
michael@0 | 6492 | return values[j] - values[i]; |
michael@0 | 6493 | } : function(i, j) { |
michael@0 | 6494 | return sort(data[i], data[j]); |
michael@0 | 6495 | }); |
michael@0 | 6496 | var arcs = []; |
michael@0 | 6497 | index.forEach(function(i) { |
michael@0 | 6498 | var d; |
michael@0 | 6499 | arcs[i] = { |
michael@0 | 6500 | data: data[i], |
michael@0 | 6501 | value: d = values[i], |
michael@0 | 6502 | startAngle: a, |
michael@0 | 6503 | endAngle: a += d * k |
michael@0 | 6504 | }; |
michael@0 | 6505 | }); |
michael@0 | 6506 | return arcs; |
michael@0 | 6507 | } |
michael@0 | 6508 | pie.value = function(x) { |
michael@0 | 6509 | if (!arguments.length) return value; |
michael@0 | 6510 | value = x; |
michael@0 | 6511 | return pie; |
michael@0 | 6512 | }; |
michael@0 | 6513 | pie.sort = function(x) { |
michael@0 | 6514 | if (!arguments.length) return sort; |
michael@0 | 6515 | sort = x; |
michael@0 | 6516 | return pie; |
michael@0 | 6517 | }; |
michael@0 | 6518 | pie.startAngle = function(x) { |
michael@0 | 6519 | if (!arguments.length) return startAngle; |
michael@0 | 6520 | startAngle = x; |
michael@0 | 6521 | return pie; |
michael@0 | 6522 | }; |
michael@0 | 6523 | pie.endAngle = function(x) { |
michael@0 | 6524 | if (!arguments.length) return endAngle; |
michael@0 | 6525 | endAngle = x; |
michael@0 | 6526 | return pie; |
michael@0 | 6527 | }; |
michael@0 | 6528 | return pie; |
michael@0 | 6529 | }; |
michael@0 | 6530 | var d3_layout_pieSortByValue = {}; |
michael@0 | 6531 | d3.layout.stack = function() { |
michael@0 | 6532 | var values = d3_identity, order = d3_layout_stackOrderDefault, offset = d3_layout_stackOffsetZero, out = d3_layout_stackOut, x = d3_layout_stackX, y = d3_layout_stackY; |
michael@0 | 6533 | function stack(data, index) { |
michael@0 | 6534 | var series = data.map(function(d, i) { |
michael@0 | 6535 | return values.call(stack, d, i); |
michael@0 | 6536 | }); |
michael@0 | 6537 | var points = series.map(function(d) { |
michael@0 | 6538 | return d.map(function(v, i) { |
michael@0 | 6539 | return [ x.call(stack, v, i), y.call(stack, v, i) ]; |
michael@0 | 6540 | }); |
michael@0 | 6541 | }); |
michael@0 | 6542 | var orders = order.call(stack, points, index); |
michael@0 | 6543 | series = d3.permute(series, orders); |
michael@0 | 6544 | points = d3.permute(points, orders); |
michael@0 | 6545 | var offsets = offset.call(stack, points, index); |
michael@0 | 6546 | var n = series.length, m = series[0].length, i, j, o; |
michael@0 | 6547 | for (j = 0; j < m; ++j) { |
michael@0 | 6548 | out.call(stack, series[0][j], o = offsets[j], points[0][j][1]); |
michael@0 | 6549 | for (i = 1; i < n; ++i) { |
michael@0 | 6550 | out.call(stack, series[i][j], o += points[i - 1][j][1], points[i][j][1]); |
michael@0 | 6551 | } |
michael@0 | 6552 | } |
michael@0 | 6553 | return data; |
michael@0 | 6554 | } |
michael@0 | 6555 | stack.values = function(x) { |
michael@0 | 6556 | if (!arguments.length) return values; |
michael@0 | 6557 | values = x; |
michael@0 | 6558 | return stack; |
michael@0 | 6559 | }; |
michael@0 | 6560 | stack.order = function(x) { |
michael@0 | 6561 | if (!arguments.length) return order; |
michael@0 | 6562 | order = typeof x === "function" ? x : d3_layout_stackOrders.get(x) || d3_layout_stackOrderDefault; |
michael@0 | 6563 | return stack; |
michael@0 | 6564 | }; |
michael@0 | 6565 | stack.offset = function(x) { |
michael@0 | 6566 | if (!arguments.length) return offset; |
michael@0 | 6567 | offset = typeof x === "function" ? x : d3_layout_stackOffsets.get(x) || d3_layout_stackOffsetZero; |
michael@0 | 6568 | return stack; |
michael@0 | 6569 | }; |
michael@0 | 6570 | stack.x = function(z) { |
michael@0 | 6571 | if (!arguments.length) return x; |
michael@0 | 6572 | x = z; |
michael@0 | 6573 | return stack; |
michael@0 | 6574 | }; |
michael@0 | 6575 | stack.y = function(z) { |
michael@0 | 6576 | if (!arguments.length) return y; |
michael@0 | 6577 | y = z; |
michael@0 | 6578 | return stack; |
michael@0 | 6579 | }; |
michael@0 | 6580 | stack.out = function(z) { |
michael@0 | 6581 | if (!arguments.length) return out; |
michael@0 | 6582 | out = z; |
michael@0 | 6583 | return stack; |
michael@0 | 6584 | }; |
michael@0 | 6585 | return stack; |
michael@0 | 6586 | }; |
michael@0 | 6587 | function d3_layout_stackX(d) { |
michael@0 | 6588 | return d.x; |
michael@0 | 6589 | } |
michael@0 | 6590 | function d3_layout_stackY(d) { |
michael@0 | 6591 | return d.y; |
michael@0 | 6592 | } |
michael@0 | 6593 | function d3_layout_stackOut(d, y0, y) { |
michael@0 | 6594 | d.y0 = y0; |
michael@0 | 6595 | d.y = y; |
michael@0 | 6596 | } |
michael@0 | 6597 | var d3_layout_stackOrders = d3.map({ |
michael@0 | 6598 | "inside-out": function(data) { |
michael@0 | 6599 | var n = data.length, i, j, max = data.map(d3_layout_stackMaxIndex), sums = data.map(d3_layout_stackReduceSum), index = d3.range(n).sort(function(a, b) { |
michael@0 | 6600 | return max[a] - max[b]; |
michael@0 | 6601 | }), top = 0, bottom = 0, tops = [], bottoms = []; |
michael@0 | 6602 | for (i = 0; i < n; ++i) { |
michael@0 | 6603 | j = index[i]; |
michael@0 | 6604 | if (top < bottom) { |
michael@0 | 6605 | top += sums[j]; |
michael@0 | 6606 | tops.push(j); |
michael@0 | 6607 | } else { |
michael@0 | 6608 | bottom += sums[j]; |
michael@0 | 6609 | bottoms.push(j); |
michael@0 | 6610 | } |
michael@0 | 6611 | } |
michael@0 | 6612 | return bottoms.reverse().concat(tops); |
michael@0 | 6613 | }, |
michael@0 | 6614 | reverse: function(data) { |
michael@0 | 6615 | return d3.range(data.length).reverse(); |
michael@0 | 6616 | }, |
michael@0 | 6617 | "default": d3_layout_stackOrderDefault |
michael@0 | 6618 | }); |
michael@0 | 6619 | var d3_layout_stackOffsets = d3.map({ |
michael@0 | 6620 | silhouette: function(data) { |
michael@0 | 6621 | var n = data.length, m = data[0].length, sums = [], max = 0, i, j, o, y0 = []; |
michael@0 | 6622 | for (j = 0; j < m; ++j) { |
michael@0 | 6623 | for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; |
michael@0 | 6624 | if (o > max) max = o; |
michael@0 | 6625 | sums.push(o); |
michael@0 | 6626 | } |
michael@0 | 6627 | for (j = 0; j < m; ++j) { |
michael@0 | 6628 | y0[j] = (max - sums[j]) / 2; |
michael@0 | 6629 | } |
michael@0 | 6630 | return y0; |
michael@0 | 6631 | }, |
michael@0 | 6632 | wiggle: function(data) { |
michael@0 | 6633 | var n = data.length, x = data[0], m = x.length, i, j, k, s1, s2, s3, dx, o, o0, y0 = []; |
michael@0 | 6634 | y0[0] = o = o0 = 0; |
michael@0 | 6635 | for (j = 1; j < m; ++j) { |
michael@0 | 6636 | for (i = 0, s1 = 0; i < n; ++i) s1 += data[i][j][1]; |
michael@0 | 6637 | for (i = 0, s2 = 0, dx = x[j][0] - x[j - 1][0]; i < n; ++i) { |
michael@0 | 6638 | for (k = 0, s3 = (data[i][j][1] - data[i][j - 1][1]) / (2 * dx); k < i; ++k) { |
michael@0 | 6639 | s3 += (data[k][j][1] - data[k][j - 1][1]) / dx; |
michael@0 | 6640 | } |
michael@0 | 6641 | s2 += s3 * data[i][j][1]; |
michael@0 | 6642 | } |
michael@0 | 6643 | y0[j] = o -= s1 ? s2 / s1 * dx : 0; |
michael@0 | 6644 | if (o < o0) o0 = o; |
michael@0 | 6645 | } |
michael@0 | 6646 | for (j = 0; j < m; ++j) y0[j] -= o0; |
michael@0 | 6647 | return y0; |
michael@0 | 6648 | }, |
michael@0 | 6649 | expand: function(data) { |
michael@0 | 6650 | var n = data.length, m = data[0].length, k = 1 / n, i, j, o, y0 = []; |
michael@0 | 6651 | for (j = 0; j < m; ++j) { |
michael@0 | 6652 | for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; |
michael@0 | 6653 | if (o) for (i = 0; i < n; i++) data[i][j][1] /= o; else for (i = 0; i < n; i++) data[i][j][1] = k; |
michael@0 | 6654 | } |
michael@0 | 6655 | for (j = 0; j < m; ++j) y0[j] = 0; |
michael@0 | 6656 | return y0; |
michael@0 | 6657 | }, |
michael@0 | 6658 | zero: d3_layout_stackOffsetZero |
michael@0 | 6659 | }); |
michael@0 | 6660 | function d3_layout_stackOrderDefault(data) { |
michael@0 | 6661 | return d3.range(data.length); |
michael@0 | 6662 | } |
michael@0 | 6663 | function d3_layout_stackOffsetZero(data) { |
michael@0 | 6664 | var j = -1, m = data[0].length, y0 = []; |
michael@0 | 6665 | while (++j < m) y0[j] = 0; |
michael@0 | 6666 | return y0; |
michael@0 | 6667 | } |
michael@0 | 6668 | function d3_layout_stackMaxIndex(array) { |
michael@0 | 6669 | var i = 1, j = 0, v = array[0][1], k, n = array.length; |
michael@0 | 6670 | for (;i < n; ++i) { |
michael@0 | 6671 | if ((k = array[i][1]) > v) { |
michael@0 | 6672 | j = i; |
michael@0 | 6673 | v = k; |
michael@0 | 6674 | } |
michael@0 | 6675 | } |
michael@0 | 6676 | return j; |
michael@0 | 6677 | } |
michael@0 | 6678 | function d3_layout_stackReduceSum(d) { |
michael@0 | 6679 | return d.reduce(d3_layout_stackSum, 0); |
michael@0 | 6680 | } |
michael@0 | 6681 | function d3_layout_stackSum(p, d) { |
michael@0 | 6682 | return p + d[1]; |
michael@0 | 6683 | } |
michael@0 | 6684 | d3.layout.histogram = function() { |
michael@0 | 6685 | var frequency = true, valuer = Number, ranger = d3_layout_histogramRange, binner = d3_layout_histogramBinSturges; |
michael@0 | 6686 | function histogram(data, i) { |
michael@0 | 6687 | var bins = [], values = data.map(valuer, this), range = ranger.call(this, values, i), thresholds = binner.call(this, range, values, i), bin, i = -1, n = values.length, m = thresholds.length - 1, k = frequency ? 1 : 1 / n, x; |
michael@0 | 6688 | while (++i < m) { |
michael@0 | 6689 | bin = bins[i] = []; |
michael@0 | 6690 | bin.dx = thresholds[i + 1] - (bin.x = thresholds[i]); |
michael@0 | 6691 | bin.y = 0; |
michael@0 | 6692 | } |
michael@0 | 6693 | if (m > 0) { |
michael@0 | 6694 | i = -1; |
michael@0 | 6695 | while (++i < n) { |
michael@0 | 6696 | x = values[i]; |
michael@0 | 6697 | if (x >= range[0] && x <= range[1]) { |
michael@0 | 6698 | bin = bins[d3.bisect(thresholds, x, 1, m) - 1]; |
michael@0 | 6699 | bin.y += k; |
michael@0 | 6700 | bin.push(data[i]); |
michael@0 | 6701 | } |
michael@0 | 6702 | } |
michael@0 | 6703 | } |
michael@0 | 6704 | return bins; |
michael@0 | 6705 | } |
michael@0 | 6706 | histogram.value = function(x) { |
michael@0 | 6707 | if (!arguments.length) return valuer; |
michael@0 | 6708 | valuer = x; |
michael@0 | 6709 | return histogram; |
michael@0 | 6710 | }; |
michael@0 | 6711 | histogram.range = function(x) { |
michael@0 | 6712 | if (!arguments.length) return ranger; |
michael@0 | 6713 | ranger = d3_functor(x); |
michael@0 | 6714 | return histogram; |
michael@0 | 6715 | }; |
michael@0 | 6716 | histogram.bins = function(x) { |
michael@0 | 6717 | if (!arguments.length) return binner; |
michael@0 | 6718 | binner = typeof x === "number" ? function(range) { |
michael@0 | 6719 | return d3_layout_histogramBinFixed(range, x); |
michael@0 | 6720 | } : d3_functor(x); |
michael@0 | 6721 | return histogram; |
michael@0 | 6722 | }; |
michael@0 | 6723 | histogram.frequency = function(x) { |
michael@0 | 6724 | if (!arguments.length) return frequency; |
michael@0 | 6725 | frequency = !!x; |
michael@0 | 6726 | return histogram; |
michael@0 | 6727 | }; |
michael@0 | 6728 | return histogram; |
michael@0 | 6729 | }; |
michael@0 | 6730 | function d3_layout_histogramBinSturges(range, values) { |
michael@0 | 6731 | return d3_layout_histogramBinFixed(range, Math.ceil(Math.log(values.length) / Math.LN2 + 1)); |
michael@0 | 6732 | } |
michael@0 | 6733 | function d3_layout_histogramBinFixed(range, n) { |
michael@0 | 6734 | var x = -1, b = +range[0], m = (range[1] - b) / n, f = []; |
michael@0 | 6735 | while (++x <= n) f[x] = m * x + b; |
michael@0 | 6736 | return f; |
michael@0 | 6737 | } |
michael@0 | 6738 | function d3_layout_histogramRange(values) { |
michael@0 | 6739 | return [ d3.min(values), d3.max(values) ]; |
michael@0 | 6740 | } |
michael@0 | 6741 | d3.layout.tree = function() { |
michael@0 | 6742 | var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false; |
michael@0 | 6743 | function tree(d, i) { |
michael@0 | 6744 | var nodes = hierarchy.call(this, d, i), root = nodes[0]; |
michael@0 | 6745 | function firstWalk(node, previousSibling) { |
michael@0 | 6746 | var children = node.children, layout = node._tree; |
michael@0 | 6747 | if (children && (n = children.length)) { |
michael@0 | 6748 | var n, firstChild = children[0], previousChild, ancestor = firstChild, child, i = -1; |
michael@0 | 6749 | while (++i < n) { |
michael@0 | 6750 | child = children[i]; |
michael@0 | 6751 | firstWalk(child, previousChild); |
michael@0 | 6752 | ancestor = apportion(child, previousChild, ancestor); |
michael@0 | 6753 | previousChild = child; |
michael@0 | 6754 | } |
michael@0 | 6755 | d3_layout_treeShift(node); |
michael@0 | 6756 | var midpoint = .5 * (firstChild._tree.prelim + child._tree.prelim); |
michael@0 | 6757 | if (previousSibling) { |
michael@0 | 6758 | layout.prelim = previousSibling._tree.prelim + separation(node, previousSibling); |
michael@0 | 6759 | layout.mod = layout.prelim - midpoint; |
michael@0 | 6760 | } else { |
michael@0 | 6761 | layout.prelim = midpoint; |
michael@0 | 6762 | } |
michael@0 | 6763 | } else { |
michael@0 | 6764 | if (previousSibling) { |
michael@0 | 6765 | layout.prelim = previousSibling._tree.prelim + separation(node, previousSibling); |
michael@0 | 6766 | } |
michael@0 | 6767 | } |
michael@0 | 6768 | } |
michael@0 | 6769 | function secondWalk(node, x) { |
michael@0 | 6770 | node.x = node._tree.prelim + x; |
michael@0 | 6771 | var children = node.children; |
michael@0 | 6772 | if (children && (n = children.length)) { |
michael@0 | 6773 | var i = -1, n; |
michael@0 | 6774 | x += node._tree.mod; |
michael@0 | 6775 | while (++i < n) { |
michael@0 | 6776 | secondWalk(children[i], x); |
michael@0 | 6777 | } |
michael@0 | 6778 | } |
michael@0 | 6779 | } |
michael@0 | 6780 | function apportion(node, previousSibling, ancestor) { |
michael@0 | 6781 | if (previousSibling) { |
michael@0 | 6782 | var vip = node, vop = node, vim = previousSibling, vom = node.parent.children[0], sip = vip._tree.mod, sop = vop._tree.mod, sim = vim._tree.mod, som = vom._tree.mod, shift; |
michael@0 | 6783 | while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) { |
michael@0 | 6784 | vom = d3_layout_treeLeft(vom); |
michael@0 | 6785 | vop = d3_layout_treeRight(vop); |
michael@0 | 6786 | vop._tree.ancestor = node; |
michael@0 | 6787 | shift = vim._tree.prelim + sim - vip._tree.prelim - sip + separation(vim, vip); |
michael@0 | 6788 | if (shift > 0) { |
michael@0 | 6789 | d3_layout_treeMove(d3_layout_treeAncestor(vim, node, ancestor), node, shift); |
michael@0 | 6790 | sip += shift; |
michael@0 | 6791 | sop += shift; |
michael@0 | 6792 | } |
michael@0 | 6793 | sim += vim._tree.mod; |
michael@0 | 6794 | sip += vip._tree.mod; |
michael@0 | 6795 | som += vom._tree.mod; |
michael@0 | 6796 | sop += vop._tree.mod; |
michael@0 | 6797 | } |
michael@0 | 6798 | if (vim && !d3_layout_treeRight(vop)) { |
michael@0 | 6799 | vop._tree.thread = vim; |
michael@0 | 6800 | vop._tree.mod += sim - sop; |
michael@0 | 6801 | } |
michael@0 | 6802 | if (vip && !d3_layout_treeLeft(vom)) { |
michael@0 | 6803 | vom._tree.thread = vip; |
michael@0 | 6804 | vom._tree.mod += sip - som; |
michael@0 | 6805 | ancestor = node; |
michael@0 | 6806 | } |
michael@0 | 6807 | } |
michael@0 | 6808 | return ancestor; |
michael@0 | 6809 | } |
michael@0 | 6810 | d3_layout_treeVisitAfter(root, function(node, previousSibling) { |
michael@0 | 6811 | node._tree = { |
michael@0 | 6812 | ancestor: node, |
michael@0 | 6813 | prelim: 0, |
michael@0 | 6814 | mod: 0, |
michael@0 | 6815 | change: 0, |
michael@0 | 6816 | shift: 0, |
michael@0 | 6817 | number: previousSibling ? previousSibling._tree.number + 1 : 0 |
michael@0 | 6818 | }; |
michael@0 | 6819 | }); |
michael@0 | 6820 | firstWalk(root); |
michael@0 | 6821 | secondWalk(root, -root._tree.prelim); |
michael@0 | 6822 | var left = d3_layout_treeSearch(root, d3_layout_treeLeftmost), right = d3_layout_treeSearch(root, d3_layout_treeRightmost), deep = d3_layout_treeSearch(root, d3_layout_treeDeepest), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2, y1 = deep.depth || 1; |
michael@0 | 6823 | d3_layout_treeVisitAfter(root, nodeSize ? function(node) { |
michael@0 | 6824 | node.x *= size[0]; |
michael@0 | 6825 | node.y = node.depth * size[1]; |
michael@0 | 6826 | delete node._tree; |
michael@0 | 6827 | } : function(node) { |
michael@0 | 6828 | node.x = (node.x - x0) / (x1 - x0) * size[0]; |
michael@0 | 6829 | node.y = node.depth / y1 * size[1]; |
michael@0 | 6830 | delete node._tree; |
michael@0 | 6831 | }); |
michael@0 | 6832 | return nodes; |
michael@0 | 6833 | } |
michael@0 | 6834 | tree.separation = function(x) { |
michael@0 | 6835 | if (!arguments.length) return separation; |
michael@0 | 6836 | separation = x; |
michael@0 | 6837 | return tree; |
michael@0 | 6838 | }; |
michael@0 | 6839 | tree.size = function(x) { |
michael@0 | 6840 | if (!arguments.length) return nodeSize ? null : size; |
michael@0 | 6841 | nodeSize = (size = x) == null; |
michael@0 | 6842 | return tree; |
michael@0 | 6843 | }; |
michael@0 | 6844 | tree.nodeSize = function(x) { |
michael@0 | 6845 | if (!arguments.length) return nodeSize ? size : null; |
michael@0 | 6846 | nodeSize = (size = x) != null; |
michael@0 | 6847 | return tree; |
michael@0 | 6848 | }; |
michael@0 | 6849 | return d3_layout_hierarchyRebind(tree, hierarchy); |
michael@0 | 6850 | }; |
michael@0 | 6851 | function d3_layout_treeSeparation(a, b) { |
michael@0 | 6852 | return a.parent == b.parent ? 1 : 2; |
michael@0 | 6853 | } |
michael@0 | 6854 | function d3_layout_treeLeft(node) { |
michael@0 | 6855 | var children = node.children; |
michael@0 | 6856 | return children && children.length ? children[0] : node._tree.thread; |
michael@0 | 6857 | } |
michael@0 | 6858 | function d3_layout_treeRight(node) { |
michael@0 | 6859 | var children = node.children, n; |
michael@0 | 6860 | return children && (n = children.length) ? children[n - 1] : node._tree.thread; |
michael@0 | 6861 | } |
michael@0 | 6862 | function d3_layout_treeSearch(node, compare) { |
michael@0 | 6863 | var children = node.children; |
michael@0 | 6864 | if (children && (n = children.length)) { |
michael@0 | 6865 | var child, n, i = -1; |
michael@0 | 6866 | while (++i < n) { |
michael@0 | 6867 | if (compare(child = d3_layout_treeSearch(children[i], compare), node) > 0) { |
michael@0 | 6868 | node = child; |
michael@0 | 6869 | } |
michael@0 | 6870 | } |
michael@0 | 6871 | } |
michael@0 | 6872 | return node; |
michael@0 | 6873 | } |
michael@0 | 6874 | function d3_layout_treeRightmost(a, b) { |
michael@0 | 6875 | return a.x - b.x; |
michael@0 | 6876 | } |
michael@0 | 6877 | function d3_layout_treeLeftmost(a, b) { |
michael@0 | 6878 | return b.x - a.x; |
michael@0 | 6879 | } |
michael@0 | 6880 | function d3_layout_treeDeepest(a, b) { |
michael@0 | 6881 | return a.depth - b.depth; |
michael@0 | 6882 | } |
michael@0 | 6883 | function d3_layout_treeVisitAfter(node, callback) { |
michael@0 | 6884 | function visit(node, previousSibling) { |
michael@0 | 6885 | var children = node.children; |
michael@0 | 6886 | if (children && (n = children.length)) { |
michael@0 | 6887 | var child, previousChild = null, i = -1, n; |
michael@0 | 6888 | while (++i < n) { |
michael@0 | 6889 | child = children[i]; |
michael@0 | 6890 | visit(child, previousChild); |
michael@0 | 6891 | previousChild = child; |
michael@0 | 6892 | } |
michael@0 | 6893 | } |
michael@0 | 6894 | callback(node, previousSibling); |
michael@0 | 6895 | } |
michael@0 | 6896 | visit(node, null); |
michael@0 | 6897 | } |
michael@0 | 6898 | function d3_layout_treeShift(node) { |
michael@0 | 6899 | var shift = 0, change = 0, children = node.children, i = children.length, child; |
michael@0 | 6900 | while (--i >= 0) { |
michael@0 | 6901 | child = children[i]._tree; |
michael@0 | 6902 | child.prelim += shift; |
michael@0 | 6903 | child.mod += shift; |
michael@0 | 6904 | shift += child.shift + (change += child.change); |
michael@0 | 6905 | } |
michael@0 | 6906 | } |
michael@0 | 6907 | function d3_layout_treeMove(ancestor, node, shift) { |
michael@0 | 6908 | ancestor = ancestor._tree; |
michael@0 | 6909 | node = node._tree; |
michael@0 | 6910 | var change = shift / (node.number - ancestor.number); |
michael@0 | 6911 | ancestor.change += change; |
michael@0 | 6912 | node.change -= change; |
michael@0 | 6913 | node.shift += shift; |
michael@0 | 6914 | node.prelim += shift; |
michael@0 | 6915 | node.mod += shift; |
michael@0 | 6916 | } |
michael@0 | 6917 | function d3_layout_treeAncestor(vim, node, ancestor) { |
michael@0 | 6918 | return vim._tree.ancestor.parent == node.parent ? vim._tree.ancestor : ancestor; |
michael@0 | 6919 | } |
michael@0 | 6920 | d3.layout.pack = function() { |
michael@0 | 6921 | var hierarchy = d3.layout.hierarchy().sort(d3_layout_packSort), padding = 0, size = [ 1, 1 ], radius; |
michael@0 | 6922 | function pack(d, i) { |
michael@0 | 6923 | var nodes = hierarchy.call(this, d, i), root = nodes[0], w = size[0], h = size[1], r = radius == null ? Math.sqrt : typeof radius === "function" ? radius : function() { |
michael@0 | 6924 | return radius; |
michael@0 | 6925 | }; |
michael@0 | 6926 | root.x = root.y = 0; |
michael@0 | 6927 | d3_layout_treeVisitAfter(root, function(d) { |
michael@0 | 6928 | d.r = +r(d.value); |
michael@0 | 6929 | }); |
michael@0 | 6930 | d3_layout_treeVisitAfter(root, d3_layout_packSiblings); |
michael@0 | 6931 | if (padding) { |
michael@0 | 6932 | var dr = padding * (radius ? 1 : Math.max(2 * root.r / w, 2 * root.r / h)) / 2; |
michael@0 | 6933 | d3_layout_treeVisitAfter(root, function(d) { |
michael@0 | 6934 | d.r += dr; |
michael@0 | 6935 | }); |
michael@0 | 6936 | d3_layout_treeVisitAfter(root, d3_layout_packSiblings); |
michael@0 | 6937 | d3_layout_treeVisitAfter(root, function(d) { |
michael@0 | 6938 | d.r -= dr; |
michael@0 | 6939 | }); |
michael@0 | 6940 | } |
michael@0 | 6941 | d3_layout_packTransform(root, w / 2, h / 2, radius ? 1 : 1 / Math.max(2 * root.r / w, 2 * root.r / h)); |
michael@0 | 6942 | return nodes; |
michael@0 | 6943 | } |
michael@0 | 6944 | pack.size = function(_) { |
michael@0 | 6945 | if (!arguments.length) return size; |
michael@0 | 6946 | size = _; |
michael@0 | 6947 | return pack; |
michael@0 | 6948 | }; |
michael@0 | 6949 | pack.radius = function(_) { |
michael@0 | 6950 | if (!arguments.length) return radius; |
michael@0 | 6951 | radius = _ == null || typeof _ === "function" ? _ : +_; |
michael@0 | 6952 | return pack; |
michael@0 | 6953 | }; |
michael@0 | 6954 | pack.padding = function(_) { |
michael@0 | 6955 | if (!arguments.length) return padding; |
michael@0 | 6956 | padding = +_; |
michael@0 | 6957 | return pack; |
michael@0 | 6958 | }; |
michael@0 | 6959 | return d3_layout_hierarchyRebind(pack, hierarchy); |
michael@0 | 6960 | }; |
michael@0 | 6961 | function d3_layout_packSort(a, b) { |
michael@0 | 6962 | return a.value - b.value; |
michael@0 | 6963 | } |
michael@0 | 6964 | function d3_layout_packInsert(a, b) { |
michael@0 | 6965 | var c = a._pack_next; |
michael@0 | 6966 | a._pack_next = b; |
michael@0 | 6967 | b._pack_prev = a; |
michael@0 | 6968 | b._pack_next = c; |
michael@0 | 6969 | c._pack_prev = b; |
michael@0 | 6970 | } |
michael@0 | 6971 | function d3_layout_packSplice(a, b) { |
michael@0 | 6972 | a._pack_next = b; |
michael@0 | 6973 | b._pack_prev = a; |
michael@0 | 6974 | } |
michael@0 | 6975 | function d3_layout_packIntersects(a, b) { |
michael@0 | 6976 | var dx = b.x - a.x, dy = b.y - a.y, dr = a.r + b.r; |
michael@0 | 6977 | return .999 * dr * dr > dx * dx + dy * dy; |
michael@0 | 6978 | } |
michael@0 | 6979 | function d3_layout_packSiblings(node) { |
michael@0 | 6980 | if (!(nodes = node.children) || !(n = nodes.length)) return; |
michael@0 | 6981 | var nodes, xMin = Infinity, xMax = -Infinity, yMin = Infinity, yMax = -Infinity, a, b, c, i, j, k, n; |
michael@0 | 6982 | function bound(node) { |
michael@0 | 6983 | xMin = Math.min(node.x - node.r, xMin); |
michael@0 | 6984 | xMax = Math.max(node.x + node.r, xMax); |
michael@0 | 6985 | yMin = Math.min(node.y - node.r, yMin); |
michael@0 | 6986 | yMax = Math.max(node.y + node.r, yMax); |
michael@0 | 6987 | } |
michael@0 | 6988 | nodes.forEach(d3_layout_packLink); |
michael@0 | 6989 | a = nodes[0]; |
michael@0 | 6990 | a.x = -a.r; |
michael@0 | 6991 | a.y = 0; |
michael@0 | 6992 | bound(a); |
michael@0 | 6993 | if (n > 1) { |
michael@0 | 6994 | b = nodes[1]; |
michael@0 | 6995 | b.x = b.r; |
michael@0 | 6996 | b.y = 0; |
michael@0 | 6997 | bound(b); |
michael@0 | 6998 | if (n > 2) { |
michael@0 | 6999 | c = nodes[2]; |
michael@0 | 7000 | d3_layout_packPlace(a, b, c); |
michael@0 | 7001 | bound(c); |
michael@0 | 7002 | d3_layout_packInsert(a, c); |
michael@0 | 7003 | a._pack_prev = c; |
michael@0 | 7004 | d3_layout_packInsert(c, b); |
michael@0 | 7005 | b = a._pack_next; |
michael@0 | 7006 | for (i = 3; i < n; i++) { |
michael@0 | 7007 | d3_layout_packPlace(a, b, c = nodes[i]); |
michael@0 | 7008 | var isect = 0, s1 = 1, s2 = 1; |
michael@0 | 7009 | for (j = b._pack_next; j !== b; j = j._pack_next, s1++) { |
michael@0 | 7010 | if (d3_layout_packIntersects(j, c)) { |
michael@0 | 7011 | isect = 1; |
michael@0 | 7012 | break; |
michael@0 | 7013 | } |
michael@0 | 7014 | } |
michael@0 | 7015 | if (isect == 1) { |
michael@0 | 7016 | for (k = a._pack_prev; k !== j._pack_prev; k = k._pack_prev, s2++) { |
michael@0 | 7017 | if (d3_layout_packIntersects(k, c)) { |
michael@0 | 7018 | break; |
michael@0 | 7019 | } |
michael@0 | 7020 | } |
michael@0 | 7021 | } |
michael@0 | 7022 | if (isect) { |
michael@0 | 7023 | if (s1 < s2 || s1 == s2 && b.r < a.r) d3_layout_packSplice(a, b = j); else d3_layout_packSplice(a = k, b); |
michael@0 | 7024 | i--; |
michael@0 | 7025 | } else { |
michael@0 | 7026 | d3_layout_packInsert(a, c); |
michael@0 | 7027 | b = c; |
michael@0 | 7028 | bound(c); |
michael@0 | 7029 | } |
michael@0 | 7030 | } |
michael@0 | 7031 | } |
michael@0 | 7032 | } |
michael@0 | 7033 | var cx = (xMin + xMax) / 2, cy = (yMin + yMax) / 2, cr = 0; |
michael@0 | 7034 | for (i = 0; i < n; i++) { |
michael@0 | 7035 | c = nodes[i]; |
michael@0 | 7036 | c.x -= cx; |
michael@0 | 7037 | c.y -= cy; |
michael@0 | 7038 | cr = Math.max(cr, c.r + Math.sqrt(c.x * c.x + c.y * c.y)); |
michael@0 | 7039 | } |
michael@0 | 7040 | node.r = cr; |
michael@0 | 7041 | nodes.forEach(d3_layout_packUnlink); |
michael@0 | 7042 | } |
michael@0 | 7043 | function d3_layout_packLink(node) { |
michael@0 | 7044 | node._pack_next = node._pack_prev = node; |
michael@0 | 7045 | } |
michael@0 | 7046 | function d3_layout_packUnlink(node) { |
michael@0 | 7047 | delete node._pack_next; |
michael@0 | 7048 | delete node._pack_prev; |
michael@0 | 7049 | } |
michael@0 | 7050 | function d3_layout_packTransform(node, x, y, k) { |
michael@0 | 7051 | var children = node.children; |
michael@0 | 7052 | node.x = x += k * node.x; |
michael@0 | 7053 | node.y = y += k * node.y; |
michael@0 | 7054 | node.r *= k; |
michael@0 | 7055 | if (children) { |
michael@0 | 7056 | var i = -1, n = children.length; |
michael@0 | 7057 | while (++i < n) d3_layout_packTransform(children[i], x, y, k); |
michael@0 | 7058 | } |
michael@0 | 7059 | } |
michael@0 | 7060 | function d3_layout_packPlace(a, b, c) { |
michael@0 | 7061 | var db = a.r + c.r, dx = b.x - a.x, dy = b.y - a.y; |
michael@0 | 7062 | if (db && (dx || dy)) { |
michael@0 | 7063 | var da = b.r + c.r, dc = dx * dx + dy * dy; |
michael@0 | 7064 | da *= da; |
michael@0 | 7065 | db *= db; |
michael@0 | 7066 | var x = .5 + (db - da) / (2 * dc), y = Math.sqrt(Math.max(0, 2 * da * (db + dc) - (db -= dc) * db - da * da)) / (2 * dc); |
michael@0 | 7067 | c.x = a.x + x * dx + y * dy; |
michael@0 | 7068 | c.y = a.y + x * dy - y * dx; |
michael@0 | 7069 | } else { |
michael@0 | 7070 | c.x = a.x + db; |
michael@0 | 7071 | c.y = a.y; |
michael@0 | 7072 | } |
michael@0 | 7073 | } |
michael@0 | 7074 | d3.layout.cluster = function() { |
michael@0 | 7075 | var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false; |
michael@0 | 7076 | function cluster(d, i) { |
michael@0 | 7077 | var nodes = hierarchy.call(this, d, i), root = nodes[0], previousNode, x = 0; |
michael@0 | 7078 | d3_layout_treeVisitAfter(root, function(node) { |
michael@0 | 7079 | var children = node.children; |
michael@0 | 7080 | if (children && children.length) { |
michael@0 | 7081 | node.x = d3_layout_clusterX(children); |
michael@0 | 7082 | node.y = d3_layout_clusterY(children); |
michael@0 | 7083 | } else { |
michael@0 | 7084 | node.x = previousNode ? x += separation(node, previousNode) : 0; |
michael@0 | 7085 | node.y = 0; |
michael@0 | 7086 | previousNode = node; |
michael@0 | 7087 | } |
michael@0 | 7088 | }); |
michael@0 | 7089 | var left = d3_layout_clusterLeft(root), right = d3_layout_clusterRight(root), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2; |
michael@0 | 7090 | d3_layout_treeVisitAfter(root, nodeSize ? function(node) { |
michael@0 | 7091 | node.x = (node.x - root.x) * size[0]; |
michael@0 | 7092 | node.y = (root.y - node.y) * size[1]; |
michael@0 | 7093 | } : function(node) { |
michael@0 | 7094 | node.x = (node.x - x0) / (x1 - x0) * size[0]; |
michael@0 | 7095 | node.y = (1 - (root.y ? node.y / root.y : 1)) * size[1]; |
michael@0 | 7096 | }); |
michael@0 | 7097 | return nodes; |
michael@0 | 7098 | } |
michael@0 | 7099 | cluster.separation = function(x) { |
michael@0 | 7100 | if (!arguments.length) return separation; |
michael@0 | 7101 | separation = x; |
michael@0 | 7102 | return cluster; |
michael@0 | 7103 | }; |
michael@0 | 7104 | cluster.size = function(x) { |
michael@0 | 7105 | if (!arguments.length) return nodeSize ? null : size; |
michael@0 | 7106 | nodeSize = (size = x) == null; |
michael@0 | 7107 | return cluster; |
michael@0 | 7108 | }; |
michael@0 | 7109 | cluster.nodeSize = function(x) { |
michael@0 | 7110 | if (!arguments.length) return nodeSize ? size : null; |
michael@0 | 7111 | nodeSize = (size = x) != null; |
michael@0 | 7112 | return cluster; |
michael@0 | 7113 | }; |
michael@0 | 7114 | return d3_layout_hierarchyRebind(cluster, hierarchy); |
michael@0 | 7115 | }; |
michael@0 | 7116 | function d3_layout_clusterY(children) { |
michael@0 | 7117 | return 1 + d3.max(children, function(child) { |
michael@0 | 7118 | return child.y; |
michael@0 | 7119 | }); |
michael@0 | 7120 | } |
michael@0 | 7121 | function d3_layout_clusterX(children) { |
michael@0 | 7122 | return children.reduce(function(x, child) { |
michael@0 | 7123 | return x + child.x; |
michael@0 | 7124 | }, 0) / children.length; |
michael@0 | 7125 | } |
michael@0 | 7126 | function d3_layout_clusterLeft(node) { |
michael@0 | 7127 | var children = node.children; |
michael@0 | 7128 | return children && children.length ? d3_layout_clusterLeft(children[0]) : node; |
michael@0 | 7129 | } |
michael@0 | 7130 | function d3_layout_clusterRight(node) { |
michael@0 | 7131 | var children = node.children, n; |
michael@0 | 7132 | return children && (n = children.length) ? d3_layout_clusterRight(children[n - 1]) : node; |
michael@0 | 7133 | } |
michael@0 | 7134 | d3.layout.treemap = function() { |
michael@0 | 7135 | var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [ 1, 1 ], padding = null, pad = d3_layout_treemapPadNull, sticky = false, stickies, mode = "squarify", ratio = .5 * (1 + Math.sqrt(5)); |
michael@0 | 7136 | function scale(children, k) { |
michael@0 | 7137 | var i = -1, n = children.length, child, area; |
michael@0 | 7138 | while (++i < n) { |
michael@0 | 7139 | area = (child = children[i]).value * (k < 0 ? 0 : k); |
michael@0 | 7140 | child.area = isNaN(area) || area <= 0 ? 0 : area; |
michael@0 | 7141 | } |
michael@0 | 7142 | } |
michael@0 | 7143 | function squarify(node) { |
michael@0 | 7144 | var children = node.children; |
michael@0 | 7145 | if (children && children.length) { |
michael@0 | 7146 | var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = mode === "slice" ? rect.dx : mode === "dice" ? rect.dy : mode === "slice-dice" ? node.depth & 1 ? rect.dy : rect.dx : Math.min(rect.dx, rect.dy), n; |
michael@0 | 7147 | scale(remaining, rect.dx * rect.dy / node.value); |
michael@0 | 7148 | row.area = 0; |
michael@0 | 7149 | while ((n = remaining.length) > 0) { |
michael@0 | 7150 | row.push(child = remaining[n - 1]); |
michael@0 | 7151 | row.area += child.area; |
michael@0 | 7152 | if (mode !== "squarify" || (score = worst(row, u)) <= best) { |
michael@0 | 7153 | remaining.pop(); |
michael@0 | 7154 | best = score; |
michael@0 | 7155 | } else { |
michael@0 | 7156 | row.area -= row.pop().area; |
michael@0 | 7157 | position(row, u, rect, false); |
michael@0 | 7158 | u = Math.min(rect.dx, rect.dy); |
michael@0 | 7159 | row.length = row.area = 0; |
michael@0 | 7160 | best = Infinity; |
michael@0 | 7161 | } |
michael@0 | 7162 | } |
michael@0 | 7163 | if (row.length) { |
michael@0 | 7164 | position(row, u, rect, true); |
michael@0 | 7165 | row.length = row.area = 0; |
michael@0 | 7166 | } |
michael@0 | 7167 | children.forEach(squarify); |
michael@0 | 7168 | } |
michael@0 | 7169 | } |
michael@0 | 7170 | function stickify(node) { |
michael@0 | 7171 | var children = node.children; |
michael@0 | 7172 | if (children && children.length) { |
michael@0 | 7173 | var rect = pad(node), remaining = children.slice(), child, row = []; |
michael@0 | 7174 | scale(remaining, rect.dx * rect.dy / node.value); |
michael@0 | 7175 | row.area = 0; |
michael@0 | 7176 | while (child = remaining.pop()) { |
michael@0 | 7177 | row.push(child); |
michael@0 | 7178 | row.area += child.area; |
michael@0 | 7179 | if (child.z != null) { |
michael@0 | 7180 | position(row, child.z ? rect.dx : rect.dy, rect, !remaining.length); |
michael@0 | 7181 | row.length = row.area = 0; |
michael@0 | 7182 | } |
michael@0 | 7183 | } |
michael@0 | 7184 | children.forEach(stickify); |
michael@0 | 7185 | } |
michael@0 | 7186 | } |
michael@0 | 7187 | function worst(row, u) { |
michael@0 | 7188 | var s = row.area, r, rmax = 0, rmin = Infinity, i = -1, n = row.length; |
michael@0 | 7189 | while (++i < n) { |
michael@0 | 7190 | if (!(r = row[i].area)) continue; |
michael@0 | 7191 | if (r < rmin) rmin = r; |
michael@0 | 7192 | if (r > rmax) rmax = r; |
michael@0 | 7193 | } |
michael@0 | 7194 | s *= s; |
michael@0 | 7195 | u *= u; |
michael@0 | 7196 | return s ? Math.max(u * rmax * ratio / s, s / (u * rmin * ratio)) : Infinity; |
michael@0 | 7197 | } |
michael@0 | 7198 | function position(row, u, rect, flush) { |
michael@0 | 7199 | var i = -1, n = row.length, x = rect.x, y = rect.y, v = u ? round(row.area / u) : 0, o; |
michael@0 | 7200 | if (u == rect.dx) { |
michael@0 | 7201 | if (flush || v > rect.dy) v = rect.dy; |
michael@0 | 7202 | while (++i < n) { |
michael@0 | 7203 | o = row[i]; |
michael@0 | 7204 | o.x = x; |
michael@0 | 7205 | o.y = y; |
michael@0 | 7206 | o.dy = v; |
michael@0 | 7207 | x += o.dx = Math.min(rect.x + rect.dx - x, v ? round(o.area / v) : 0); |
michael@0 | 7208 | } |
michael@0 | 7209 | o.z = true; |
michael@0 | 7210 | o.dx += rect.x + rect.dx - x; |
michael@0 | 7211 | rect.y += v; |
michael@0 | 7212 | rect.dy -= v; |
michael@0 | 7213 | } else { |
michael@0 | 7214 | if (flush || v > rect.dx) v = rect.dx; |
michael@0 | 7215 | while (++i < n) { |
michael@0 | 7216 | o = row[i]; |
michael@0 | 7217 | o.x = x; |
michael@0 | 7218 | o.y = y; |
michael@0 | 7219 | o.dx = v; |
michael@0 | 7220 | y += o.dy = Math.min(rect.y + rect.dy - y, v ? round(o.area / v) : 0); |
michael@0 | 7221 | } |
michael@0 | 7222 | o.z = false; |
michael@0 | 7223 | o.dy += rect.y + rect.dy - y; |
michael@0 | 7224 | rect.x += v; |
michael@0 | 7225 | rect.dx -= v; |
michael@0 | 7226 | } |
michael@0 | 7227 | } |
michael@0 | 7228 | function treemap(d) { |
michael@0 | 7229 | var nodes = stickies || hierarchy(d), root = nodes[0]; |
michael@0 | 7230 | root.x = 0; |
michael@0 | 7231 | root.y = 0; |
michael@0 | 7232 | root.dx = size[0]; |
michael@0 | 7233 | root.dy = size[1]; |
michael@0 | 7234 | if (stickies) hierarchy.revalue(root); |
michael@0 | 7235 | scale([ root ], root.dx * root.dy / root.value); |
michael@0 | 7236 | (stickies ? stickify : squarify)(root); |
michael@0 | 7237 | if (sticky) stickies = nodes; |
michael@0 | 7238 | return nodes; |
michael@0 | 7239 | } |
michael@0 | 7240 | treemap.size = function(x) { |
michael@0 | 7241 | if (!arguments.length) return size; |
michael@0 | 7242 | size = x; |
michael@0 | 7243 | return treemap; |
michael@0 | 7244 | }; |
michael@0 | 7245 | treemap.padding = function(x) { |
michael@0 | 7246 | if (!arguments.length) return padding; |
michael@0 | 7247 | function padFunction(node) { |
michael@0 | 7248 | var p = x.call(treemap, node, node.depth); |
michael@0 | 7249 | return p == null ? d3_layout_treemapPadNull(node) : d3_layout_treemapPad(node, typeof p === "number" ? [ p, p, p, p ] : p); |
michael@0 | 7250 | } |
michael@0 | 7251 | function padConstant(node) { |
michael@0 | 7252 | return d3_layout_treemapPad(node, x); |
michael@0 | 7253 | } |
michael@0 | 7254 | var type; |
michael@0 | 7255 | pad = (padding = x) == null ? d3_layout_treemapPadNull : (type = typeof x) === "function" ? padFunction : type === "number" ? (x = [ x, x, x, x ], |
michael@0 | 7256 | padConstant) : padConstant; |
michael@0 | 7257 | return treemap; |
michael@0 | 7258 | }; |
michael@0 | 7259 | treemap.round = function(x) { |
michael@0 | 7260 | if (!arguments.length) return round != Number; |
michael@0 | 7261 | round = x ? Math.round : Number; |
michael@0 | 7262 | return treemap; |
michael@0 | 7263 | }; |
michael@0 | 7264 | treemap.sticky = function(x) { |
michael@0 | 7265 | if (!arguments.length) return sticky; |
michael@0 | 7266 | sticky = x; |
michael@0 | 7267 | stickies = null; |
michael@0 | 7268 | return treemap; |
michael@0 | 7269 | }; |
michael@0 | 7270 | treemap.ratio = function(x) { |
michael@0 | 7271 | if (!arguments.length) return ratio; |
michael@0 | 7272 | ratio = x; |
michael@0 | 7273 | return treemap; |
michael@0 | 7274 | }; |
michael@0 | 7275 | treemap.mode = function(x) { |
michael@0 | 7276 | if (!arguments.length) return mode; |
michael@0 | 7277 | mode = x + ""; |
michael@0 | 7278 | return treemap; |
michael@0 | 7279 | }; |
michael@0 | 7280 | return d3_layout_hierarchyRebind(treemap, hierarchy); |
michael@0 | 7281 | }; |
michael@0 | 7282 | function d3_layout_treemapPadNull(node) { |
michael@0 | 7283 | return { |
michael@0 | 7284 | x: node.x, |
michael@0 | 7285 | y: node.y, |
michael@0 | 7286 | dx: node.dx, |
michael@0 | 7287 | dy: node.dy |
michael@0 | 7288 | }; |
michael@0 | 7289 | } |
michael@0 | 7290 | function d3_layout_treemapPad(node, padding) { |
michael@0 | 7291 | var x = node.x + padding[3], y = node.y + padding[0], dx = node.dx - padding[1] - padding[3], dy = node.dy - padding[0] - padding[2]; |
michael@0 | 7292 | if (dx < 0) { |
michael@0 | 7293 | x += dx / 2; |
michael@0 | 7294 | dx = 0; |
michael@0 | 7295 | } |
michael@0 | 7296 | if (dy < 0) { |
michael@0 | 7297 | y += dy / 2; |
michael@0 | 7298 | dy = 0; |
michael@0 | 7299 | } |
michael@0 | 7300 | return { |
michael@0 | 7301 | x: x, |
michael@0 | 7302 | y: y, |
michael@0 | 7303 | dx: dx, |
michael@0 | 7304 | dy: dy |
michael@0 | 7305 | }; |
michael@0 | 7306 | } |
michael@0 | 7307 | d3.random = { |
michael@0 | 7308 | normal: function(µ, σ) { |
michael@0 | 7309 | var n = arguments.length; |
michael@0 | 7310 | if (n < 2) σ = 1; |
michael@0 | 7311 | if (n < 1) µ = 0; |
michael@0 | 7312 | return function() { |
michael@0 | 7313 | var x, y, r; |
michael@0 | 7314 | do { |
michael@0 | 7315 | x = Math.random() * 2 - 1; |
michael@0 | 7316 | y = Math.random() * 2 - 1; |
michael@0 | 7317 | r = x * x + y * y; |
michael@0 | 7318 | } while (!r || r > 1); |
michael@0 | 7319 | return µ + σ * x * Math.sqrt(-2 * Math.log(r) / r); |
michael@0 | 7320 | }; |
michael@0 | 7321 | }, |
michael@0 | 7322 | logNormal: function() { |
michael@0 | 7323 | var random = d3.random.normal.apply(d3, arguments); |
michael@0 | 7324 | return function() { |
michael@0 | 7325 | return Math.exp(random()); |
michael@0 | 7326 | }; |
michael@0 | 7327 | }, |
michael@0 | 7328 | bates: function(m) { |
michael@0 | 7329 | var random = d3.random.irwinHall(m); |
michael@0 | 7330 | return function() { |
michael@0 | 7331 | return random() / m; |
michael@0 | 7332 | }; |
michael@0 | 7333 | }, |
michael@0 | 7334 | irwinHall: function(m) { |
michael@0 | 7335 | return function() { |
michael@0 | 7336 | for (var s = 0, j = 0; j < m; j++) s += Math.random(); |
michael@0 | 7337 | return s; |
michael@0 | 7338 | }; |
michael@0 | 7339 | } |
michael@0 | 7340 | }; |
michael@0 | 7341 | d3.scale = {}; |
michael@0 | 7342 | function d3_scaleExtent(domain) { |
michael@0 | 7343 | var start = domain[0], stop = domain[domain.length - 1]; |
michael@0 | 7344 | return start < stop ? [ start, stop ] : [ stop, start ]; |
michael@0 | 7345 | } |
michael@0 | 7346 | function d3_scaleRange(scale) { |
michael@0 | 7347 | return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range()); |
michael@0 | 7348 | } |
michael@0 | 7349 | function d3_scale_bilinear(domain, range, uninterpolate, interpolate) { |
michael@0 | 7350 | var u = uninterpolate(domain[0], domain[1]), i = interpolate(range[0], range[1]); |
michael@0 | 7351 | return function(x) { |
michael@0 | 7352 | return i(u(x)); |
michael@0 | 7353 | }; |
michael@0 | 7354 | } |
michael@0 | 7355 | function d3_scale_nice(domain, nice) { |
michael@0 | 7356 | var i0 = 0, i1 = domain.length - 1, x0 = domain[i0], x1 = domain[i1], dx; |
michael@0 | 7357 | if (x1 < x0) { |
michael@0 | 7358 | dx = i0, i0 = i1, i1 = dx; |
michael@0 | 7359 | dx = x0, x0 = x1, x1 = dx; |
michael@0 | 7360 | } |
michael@0 | 7361 | domain[i0] = nice.floor(x0); |
michael@0 | 7362 | domain[i1] = nice.ceil(x1); |
michael@0 | 7363 | return domain; |
michael@0 | 7364 | } |
michael@0 | 7365 | function d3_scale_niceStep(step) { |
michael@0 | 7366 | return step ? { |
michael@0 | 7367 | floor: function(x) { |
michael@0 | 7368 | return Math.floor(x / step) * step; |
michael@0 | 7369 | }, |
michael@0 | 7370 | ceil: function(x) { |
michael@0 | 7371 | return Math.ceil(x / step) * step; |
michael@0 | 7372 | } |
michael@0 | 7373 | } : d3_scale_niceIdentity; |
michael@0 | 7374 | } |
michael@0 | 7375 | var d3_scale_niceIdentity = { |
michael@0 | 7376 | floor: d3_identity, |
michael@0 | 7377 | ceil: d3_identity |
michael@0 | 7378 | }; |
michael@0 | 7379 | function d3_scale_polylinear(domain, range, uninterpolate, interpolate) { |
michael@0 | 7380 | var u = [], i = [], j = 0, k = Math.min(domain.length, range.length) - 1; |
michael@0 | 7381 | if (domain[k] < domain[0]) { |
michael@0 | 7382 | domain = domain.slice().reverse(); |
michael@0 | 7383 | range = range.slice().reverse(); |
michael@0 | 7384 | } |
michael@0 | 7385 | while (++j <= k) { |
michael@0 | 7386 | u.push(uninterpolate(domain[j - 1], domain[j])); |
michael@0 | 7387 | i.push(interpolate(range[j - 1], range[j])); |
michael@0 | 7388 | } |
michael@0 | 7389 | return function(x) { |
michael@0 | 7390 | var j = d3.bisect(domain, x, 1, k) - 1; |
michael@0 | 7391 | return i[j](u[j](x)); |
michael@0 | 7392 | }; |
michael@0 | 7393 | } |
michael@0 | 7394 | d3.scale.linear = function() { |
michael@0 | 7395 | return d3_scale_linear([ 0, 1 ], [ 0, 1 ], d3_interpolate, false); |
michael@0 | 7396 | }; |
michael@0 | 7397 | function d3_scale_linear(domain, range, interpolate, clamp) { |
michael@0 | 7398 | var output, input; |
michael@0 | 7399 | function rescale() { |
michael@0 | 7400 | var linear = Math.min(domain.length, range.length) > 2 ? d3_scale_polylinear : d3_scale_bilinear, uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber; |
michael@0 | 7401 | output = linear(domain, range, uninterpolate, interpolate); |
michael@0 | 7402 | input = linear(range, domain, uninterpolate, d3_interpolate); |
michael@0 | 7403 | return scale; |
michael@0 | 7404 | } |
michael@0 | 7405 | function scale(x) { |
michael@0 | 7406 | return output(x); |
michael@0 | 7407 | } |
michael@0 | 7408 | scale.invert = function(y) { |
michael@0 | 7409 | return input(y); |
michael@0 | 7410 | }; |
michael@0 | 7411 | scale.domain = function(x) { |
michael@0 | 7412 | if (!arguments.length) return domain; |
michael@0 | 7413 | domain = x.map(Number); |
michael@0 | 7414 | return rescale(); |
michael@0 | 7415 | }; |
michael@0 | 7416 | scale.range = function(x) { |
michael@0 | 7417 | if (!arguments.length) return range; |
michael@0 | 7418 | range = x; |
michael@0 | 7419 | return rescale(); |
michael@0 | 7420 | }; |
michael@0 | 7421 | scale.rangeRound = function(x) { |
michael@0 | 7422 | return scale.range(x).interpolate(d3_interpolateRound); |
michael@0 | 7423 | }; |
michael@0 | 7424 | scale.clamp = function(x) { |
michael@0 | 7425 | if (!arguments.length) return clamp; |
michael@0 | 7426 | clamp = x; |
michael@0 | 7427 | return rescale(); |
michael@0 | 7428 | }; |
michael@0 | 7429 | scale.interpolate = function(x) { |
michael@0 | 7430 | if (!arguments.length) return interpolate; |
michael@0 | 7431 | interpolate = x; |
michael@0 | 7432 | return rescale(); |
michael@0 | 7433 | }; |
michael@0 | 7434 | scale.ticks = function(m) { |
michael@0 | 7435 | return d3_scale_linearTicks(domain, m); |
michael@0 | 7436 | }; |
michael@0 | 7437 | scale.tickFormat = function(m, format) { |
michael@0 | 7438 | return d3_scale_linearTickFormat(domain, m, format); |
michael@0 | 7439 | }; |
michael@0 | 7440 | scale.nice = function(m) { |
michael@0 | 7441 | d3_scale_linearNice(domain, m); |
michael@0 | 7442 | return rescale(); |
michael@0 | 7443 | }; |
michael@0 | 7444 | scale.copy = function() { |
michael@0 | 7445 | return d3_scale_linear(domain, range, interpolate, clamp); |
michael@0 | 7446 | }; |
michael@0 | 7447 | return rescale(); |
michael@0 | 7448 | } |
michael@0 | 7449 | function d3_scale_linearRebind(scale, linear) { |
michael@0 | 7450 | return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp"); |
michael@0 | 7451 | } |
michael@0 | 7452 | function d3_scale_linearNice(domain, m) { |
michael@0 | 7453 | return d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2])); |
michael@0 | 7454 | } |
michael@0 | 7455 | function d3_scale_linearTickRange(domain, m) { |
michael@0 | 7456 | if (m == null) m = 10; |
michael@0 | 7457 | var extent = d3_scaleExtent(domain), span = extent[1] - extent[0], step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)), err = m / span * step; |
michael@0 | 7458 | if (err <= .15) step *= 10; else if (err <= .35) step *= 5; else if (err <= .75) step *= 2; |
michael@0 | 7459 | extent[0] = Math.ceil(extent[0] / step) * step; |
michael@0 | 7460 | extent[1] = Math.floor(extent[1] / step) * step + step * .5; |
michael@0 | 7461 | extent[2] = step; |
michael@0 | 7462 | return extent; |
michael@0 | 7463 | } |
michael@0 | 7464 | function d3_scale_linearTicks(domain, m) { |
michael@0 | 7465 | return d3.range.apply(d3, d3_scale_linearTickRange(domain, m)); |
michael@0 | 7466 | } |
michael@0 | 7467 | function d3_scale_linearTickFormat(domain, m, format) { |
michael@0 | 7468 | var range = d3_scale_linearTickRange(domain, m); |
michael@0 | 7469 | return d3.format(format ? format.replace(d3_format_re, function(a, b, c, d, e, f, g, h, i, j) { |
michael@0 | 7470 | return [ b, c, d, e, f, g, h, i || "." + d3_scale_linearFormatPrecision(j, range), j ].join(""); |
michael@0 | 7471 | }) : ",." + d3_scale_linearPrecision(range[2]) + "f"); |
michael@0 | 7472 | } |
michael@0 | 7473 | var d3_scale_linearFormatSignificant = { |
michael@0 | 7474 | s: 1, |
michael@0 | 7475 | g: 1, |
michael@0 | 7476 | p: 1, |
michael@0 | 7477 | r: 1, |
michael@0 | 7478 | e: 1 |
michael@0 | 7479 | }; |
michael@0 | 7480 | function d3_scale_linearPrecision(value) { |
michael@0 | 7481 | return -Math.floor(Math.log(value) / Math.LN10 + .01); |
michael@0 | 7482 | } |
michael@0 | 7483 | function d3_scale_linearFormatPrecision(type, range) { |
michael@0 | 7484 | var p = d3_scale_linearPrecision(range[2]); |
michael@0 | 7485 | return type in d3_scale_linearFormatSignificant ? Math.abs(p - d3_scale_linearPrecision(Math.max(Math.abs(range[0]), Math.abs(range[1])))) + +(type !== "e") : p - (type === "%") * 2; |
michael@0 | 7486 | } |
michael@0 | 7487 | d3.scale.log = function() { |
michael@0 | 7488 | return d3_scale_log(d3.scale.linear().domain([ 0, 1 ]), 10, true, [ 1, 10 ]); |
michael@0 | 7489 | }; |
michael@0 | 7490 | function d3_scale_log(linear, base, positive, domain) { |
michael@0 | 7491 | function log(x) { |
michael@0 | 7492 | return (positive ? Math.log(x < 0 ? 0 : x) : -Math.log(x > 0 ? 0 : -x)) / Math.log(base); |
michael@0 | 7493 | } |
michael@0 | 7494 | function pow(x) { |
michael@0 | 7495 | return positive ? Math.pow(base, x) : -Math.pow(base, -x); |
michael@0 | 7496 | } |
michael@0 | 7497 | function scale(x) { |
michael@0 | 7498 | return linear(log(x)); |
michael@0 | 7499 | } |
michael@0 | 7500 | scale.invert = function(x) { |
michael@0 | 7501 | return pow(linear.invert(x)); |
michael@0 | 7502 | }; |
michael@0 | 7503 | scale.domain = function(x) { |
michael@0 | 7504 | if (!arguments.length) return domain; |
michael@0 | 7505 | positive = x[0] >= 0; |
michael@0 | 7506 | linear.domain((domain = x.map(Number)).map(log)); |
michael@0 | 7507 | return scale; |
michael@0 | 7508 | }; |
michael@0 | 7509 | scale.base = function(_) { |
michael@0 | 7510 | if (!arguments.length) return base; |
michael@0 | 7511 | base = +_; |
michael@0 | 7512 | linear.domain(domain.map(log)); |
michael@0 | 7513 | return scale; |
michael@0 | 7514 | }; |
michael@0 | 7515 | scale.nice = function() { |
michael@0 | 7516 | var niced = d3_scale_nice(domain.map(log), positive ? Math : d3_scale_logNiceNegative); |
michael@0 | 7517 | linear.domain(niced); |
michael@0 | 7518 | domain = niced.map(pow); |
michael@0 | 7519 | return scale; |
michael@0 | 7520 | }; |
michael@0 | 7521 | scale.ticks = function() { |
michael@0 | 7522 | var extent = d3_scaleExtent(domain), ticks = [], u = extent[0], v = extent[1], i = Math.floor(log(u)), j = Math.ceil(log(v)), n = base % 1 ? 2 : base; |
michael@0 | 7523 | if (isFinite(j - i)) { |
michael@0 | 7524 | if (positive) { |
michael@0 | 7525 | for (;i < j; i++) for (var k = 1; k < n; k++) ticks.push(pow(i) * k); |
michael@0 | 7526 | ticks.push(pow(i)); |
michael@0 | 7527 | } else { |
michael@0 | 7528 | ticks.push(pow(i)); |
michael@0 | 7529 | for (;i++ < j; ) for (var k = n - 1; k > 0; k--) ticks.push(pow(i) * k); |
michael@0 | 7530 | } |
michael@0 | 7531 | for (i = 0; ticks[i] < u; i++) {} |
michael@0 | 7532 | for (j = ticks.length; ticks[j - 1] > v; j--) {} |
michael@0 | 7533 | ticks = ticks.slice(i, j); |
michael@0 | 7534 | } |
michael@0 | 7535 | return ticks; |
michael@0 | 7536 | }; |
michael@0 | 7537 | scale.tickFormat = function(n, format) { |
michael@0 | 7538 | if (!arguments.length) return d3_scale_logFormat; |
michael@0 | 7539 | if (arguments.length < 2) format = d3_scale_logFormat; else if (typeof format !== "function") format = d3.format(format); |
michael@0 | 7540 | var k = Math.max(.1, n / scale.ticks().length), f = positive ? (e = 1e-12, Math.ceil) : (e = -1e-12, |
michael@0 | 7541 | Math.floor), e; |
michael@0 | 7542 | return function(d) { |
michael@0 | 7543 | return d / pow(f(log(d) + e)) <= k ? format(d) : ""; |
michael@0 | 7544 | }; |
michael@0 | 7545 | }; |
michael@0 | 7546 | scale.copy = function() { |
michael@0 | 7547 | return d3_scale_log(linear.copy(), base, positive, domain); |
michael@0 | 7548 | }; |
michael@0 | 7549 | return d3_scale_linearRebind(scale, linear); |
michael@0 | 7550 | } |
michael@0 | 7551 | var d3_scale_logFormat = d3.format(".0e"), d3_scale_logNiceNegative = { |
michael@0 | 7552 | floor: function(x) { |
michael@0 | 7553 | return -Math.ceil(-x); |
michael@0 | 7554 | }, |
michael@0 | 7555 | ceil: function(x) { |
michael@0 | 7556 | return -Math.floor(-x); |
michael@0 | 7557 | } |
michael@0 | 7558 | }; |
michael@0 | 7559 | d3.scale.pow = function() { |
michael@0 | 7560 | return d3_scale_pow(d3.scale.linear(), 1, [ 0, 1 ]); |
michael@0 | 7561 | }; |
michael@0 | 7562 | function d3_scale_pow(linear, exponent, domain) { |
michael@0 | 7563 | var powp = d3_scale_powPow(exponent), powb = d3_scale_powPow(1 / exponent); |
michael@0 | 7564 | function scale(x) { |
michael@0 | 7565 | return linear(powp(x)); |
michael@0 | 7566 | } |
michael@0 | 7567 | scale.invert = function(x) { |
michael@0 | 7568 | return powb(linear.invert(x)); |
michael@0 | 7569 | }; |
michael@0 | 7570 | scale.domain = function(x) { |
michael@0 | 7571 | if (!arguments.length) return domain; |
michael@0 | 7572 | linear.domain((domain = x.map(Number)).map(powp)); |
michael@0 | 7573 | return scale; |
michael@0 | 7574 | }; |
michael@0 | 7575 | scale.ticks = function(m) { |
michael@0 | 7576 | return d3_scale_linearTicks(domain, m); |
michael@0 | 7577 | }; |
michael@0 | 7578 | scale.tickFormat = function(m, format) { |
michael@0 | 7579 | return d3_scale_linearTickFormat(domain, m, format); |
michael@0 | 7580 | }; |
michael@0 | 7581 | scale.nice = function(m) { |
michael@0 | 7582 | return scale.domain(d3_scale_linearNice(domain, m)); |
michael@0 | 7583 | }; |
michael@0 | 7584 | scale.exponent = function(x) { |
michael@0 | 7585 | if (!arguments.length) return exponent; |
michael@0 | 7586 | powp = d3_scale_powPow(exponent = x); |
michael@0 | 7587 | powb = d3_scale_powPow(1 / exponent); |
michael@0 | 7588 | linear.domain(domain.map(powp)); |
michael@0 | 7589 | return scale; |
michael@0 | 7590 | }; |
michael@0 | 7591 | scale.copy = function() { |
michael@0 | 7592 | return d3_scale_pow(linear.copy(), exponent, domain); |
michael@0 | 7593 | }; |
michael@0 | 7594 | return d3_scale_linearRebind(scale, linear); |
michael@0 | 7595 | } |
michael@0 | 7596 | function d3_scale_powPow(e) { |
michael@0 | 7597 | return function(x) { |
michael@0 | 7598 | return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e); |
michael@0 | 7599 | }; |
michael@0 | 7600 | } |
michael@0 | 7601 | d3.scale.sqrt = function() { |
michael@0 | 7602 | return d3.scale.pow().exponent(.5); |
michael@0 | 7603 | }; |
michael@0 | 7604 | d3.scale.ordinal = function() { |
michael@0 | 7605 | return d3_scale_ordinal([], { |
michael@0 | 7606 | t: "range", |
michael@0 | 7607 | a: [ [] ] |
michael@0 | 7608 | }); |
michael@0 | 7609 | }; |
michael@0 | 7610 | function d3_scale_ordinal(domain, ranger) { |
michael@0 | 7611 | var index, range, rangeBand; |
michael@0 | 7612 | function scale(x) { |
michael@0 | 7613 | return range[((index.get(x) || ranger.t === "range" && index.set(x, domain.push(x))) - 1) % range.length]; |
michael@0 | 7614 | } |
michael@0 | 7615 | function steps(start, step) { |
michael@0 | 7616 | return d3.range(domain.length).map(function(i) { |
michael@0 | 7617 | return start + step * i; |
michael@0 | 7618 | }); |
michael@0 | 7619 | } |
michael@0 | 7620 | scale.domain = function(x) { |
michael@0 | 7621 | if (!arguments.length) return domain; |
michael@0 | 7622 | domain = []; |
michael@0 | 7623 | index = new d3_Map(); |
michael@0 | 7624 | var i = -1, n = x.length, xi; |
michael@0 | 7625 | while (++i < n) if (!index.has(xi = x[i])) index.set(xi, domain.push(xi)); |
michael@0 | 7626 | return scale[ranger.t].apply(scale, ranger.a); |
michael@0 | 7627 | }; |
michael@0 | 7628 | scale.range = function(x) { |
michael@0 | 7629 | if (!arguments.length) return range; |
michael@0 | 7630 | range = x; |
michael@0 | 7631 | rangeBand = 0; |
michael@0 | 7632 | ranger = { |
michael@0 | 7633 | t: "range", |
michael@0 | 7634 | a: arguments |
michael@0 | 7635 | }; |
michael@0 | 7636 | return scale; |
michael@0 | 7637 | }; |
michael@0 | 7638 | scale.rangePoints = function(x, padding) { |
michael@0 | 7639 | if (arguments.length < 2) padding = 0; |
michael@0 | 7640 | var start = x[0], stop = x[1], step = (stop - start) / (Math.max(1, domain.length - 1) + padding); |
michael@0 | 7641 | range = steps(domain.length < 2 ? (start + stop) / 2 : start + step * padding / 2, step); |
michael@0 | 7642 | rangeBand = 0; |
michael@0 | 7643 | ranger = { |
michael@0 | 7644 | t: "rangePoints", |
michael@0 | 7645 | a: arguments |
michael@0 | 7646 | }; |
michael@0 | 7647 | return scale; |
michael@0 | 7648 | }; |
michael@0 | 7649 | scale.rangeBands = function(x, padding, outerPadding) { |
michael@0 | 7650 | if (arguments.length < 2) padding = 0; |
michael@0 | 7651 | if (arguments.length < 3) outerPadding = padding; |
michael@0 | 7652 | var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = (stop - start) / (domain.length - padding + 2 * outerPadding); |
michael@0 | 7653 | range = steps(start + step * outerPadding, step); |
michael@0 | 7654 | if (reverse) range.reverse(); |
michael@0 | 7655 | rangeBand = step * (1 - padding); |
michael@0 | 7656 | ranger = { |
michael@0 | 7657 | t: "rangeBands", |
michael@0 | 7658 | a: arguments |
michael@0 | 7659 | }; |
michael@0 | 7660 | return scale; |
michael@0 | 7661 | }; |
michael@0 | 7662 | scale.rangeRoundBands = function(x, padding, outerPadding) { |
michael@0 | 7663 | if (arguments.length < 2) padding = 0; |
michael@0 | 7664 | if (arguments.length < 3) outerPadding = padding; |
michael@0 | 7665 | var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = Math.floor((stop - start) / (domain.length - padding + 2 * outerPadding)), error = stop - start - (domain.length - padding) * step; |
michael@0 | 7666 | range = steps(start + Math.round(error / 2), step); |
michael@0 | 7667 | if (reverse) range.reverse(); |
michael@0 | 7668 | rangeBand = Math.round(step * (1 - padding)); |
michael@0 | 7669 | ranger = { |
michael@0 | 7670 | t: "rangeRoundBands", |
michael@0 | 7671 | a: arguments |
michael@0 | 7672 | }; |
michael@0 | 7673 | return scale; |
michael@0 | 7674 | }; |
michael@0 | 7675 | scale.rangeBand = function() { |
michael@0 | 7676 | return rangeBand; |
michael@0 | 7677 | }; |
michael@0 | 7678 | scale.rangeExtent = function() { |
michael@0 | 7679 | return d3_scaleExtent(ranger.a[0]); |
michael@0 | 7680 | }; |
michael@0 | 7681 | scale.copy = function() { |
michael@0 | 7682 | return d3_scale_ordinal(domain, ranger); |
michael@0 | 7683 | }; |
michael@0 | 7684 | return scale.domain(domain); |
michael@0 | 7685 | } |
michael@0 | 7686 | d3.scale.category10 = function() { |
michael@0 | 7687 | return d3.scale.ordinal().range(d3_category10); |
michael@0 | 7688 | }; |
michael@0 | 7689 | d3.scale.category20 = function() { |
michael@0 | 7690 | return d3.scale.ordinal().range(d3_category20); |
michael@0 | 7691 | }; |
michael@0 | 7692 | d3.scale.category20b = function() { |
michael@0 | 7693 | return d3.scale.ordinal().range(d3_category20b); |
michael@0 | 7694 | }; |
michael@0 | 7695 | d3.scale.category20c = function() { |
michael@0 | 7696 | return d3.scale.ordinal().range(d3_category20c); |
michael@0 | 7697 | }; |
michael@0 | 7698 | var d3_category10 = [ 2062260, 16744206, 2924588, 14034728, 9725885, 9197131, 14907330, 8355711, 12369186, 1556175 ].map(d3_rgbString); |
michael@0 | 7699 | var d3_category20 = [ 2062260, 11454440, 16744206, 16759672, 2924588, 10018698, 14034728, 16750742, 9725885, 12955861, 9197131, 12885140, 14907330, 16234194, 8355711, 13092807, 12369186, 14408589, 1556175, 10410725 ].map(d3_rgbString); |
michael@0 | 7700 | var d3_category20b = [ 3750777, 5395619, 7040719, 10264286, 6519097, 9216594, 11915115, 13556636, 9202993, 12426809, 15186514, 15190932, 8666169, 11356490, 14049643, 15177372, 8077683, 10834324, 13528509, 14589654 ].map(d3_rgbString); |
michael@0 | 7701 | var d3_category20c = [ 3244733, 7057110, 10406625, 13032431, 15095053, 16616764, 16625259, 16634018, 3253076, 7652470, 10607003, 13101504, 7695281, 10394312, 12369372, 14342891, 6513507, 9868950, 12434877, 14277081 ].map(d3_rgbString); |
michael@0 | 7702 | d3.scale.quantile = function() { |
michael@0 | 7703 | return d3_scale_quantile([], []); |
michael@0 | 7704 | }; |
michael@0 | 7705 | function d3_scale_quantile(domain, range) { |
michael@0 | 7706 | var thresholds; |
michael@0 | 7707 | function rescale() { |
michael@0 | 7708 | var k = 0, q = range.length; |
michael@0 | 7709 | thresholds = []; |
michael@0 | 7710 | while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q); |
michael@0 | 7711 | return scale; |
michael@0 | 7712 | } |
michael@0 | 7713 | function scale(x) { |
michael@0 | 7714 | if (!isNaN(x = +x)) return range[d3.bisect(thresholds, x)]; |
michael@0 | 7715 | } |
michael@0 | 7716 | scale.domain = function(x) { |
michael@0 | 7717 | if (!arguments.length) return domain; |
michael@0 | 7718 | domain = x.filter(function(d) { |
michael@0 | 7719 | return !isNaN(d); |
michael@0 | 7720 | }).sort(d3.ascending); |
michael@0 | 7721 | return rescale(); |
michael@0 | 7722 | }; |
michael@0 | 7723 | scale.range = function(x) { |
michael@0 | 7724 | if (!arguments.length) return range; |
michael@0 | 7725 | range = x; |
michael@0 | 7726 | return rescale(); |
michael@0 | 7727 | }; |
michael@0 | 7728 | scale.quantiles = function() { |
michael@0 | 7729 | return thresholds; |
michael@0 | 7730 | }; |
michael@0 | 7731 | scale.invertExtent = function(y) { |
michael@0 | 7732 | y = range.indexOf(y); |
michael@0 | 7733 | return y < 0 ? [ NaN, NaN ] : [ y > 0 ? thresholds[y - 1] : domain[0], y < thresholds.length ? thresholds[y] : domain[domain.length - 1] ]; |
michael@0 | 7734 | }; |
michael@0 | 7735 | scale.copy = function() { |
michael@0 | 7736 | return d3_scale_quantile(domain, range); |
michael@0 | 7737 | }; |
michael@0 | 7738 | return rescale(); |
michael@0 | 7739 | } |
michael@0 | 7740 | d3.scale.quantize = function() { |
michael@0 | 7741 | return d3_scale_quantize(0, 1, [ 0, 1 ]); |
michael@0 | 7742 | }; |
michael@0 | 7743 | function d3_scale_quantize(x0, x1, range) { |
michael@0 | 7744 | var kx, i; |
michael@0 | 7745 | function scale(x) { |
michael@0 | 7746 | return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))]; |
michael@0 | 7747 | } |
michael@0 | 7748 | function rescale() { |
michael@0 | 7749 | kx = range.length / (x1 - x0); |
michael@0 | 7750 | i = range.length - 1; |
michael@0 | 7751 | return scale; |
michael@0 | 7752 | } |
michael@0 | 7753 | scale.domain = function(x) { |
michael@0 | 7754 | if (!arguments.length) return [ x0, x1 ]; |
michael@0 | 7755 | x0 = +x[0]; |
michael@0 | 7756 | x1 = +x[x.length - 1]; |
michael@0 | 7757 | return rescale(); |
michael@0 | 7758 | }; |
michael@0 | 7759 | scale.range = function(x) { |
michael@0 | 7760 | if (!arguments.length) return range; |
michael@0 | 7761 | range = x; |
michael@0 | 7762 | return rescale(); |
michael@0 | 7763 | }; |
michael@0 | 7764 | scale.invertExtent = function(y) { |
michael@0 | 7765 | y = range.indexOf(y); |
michael@0 | 7766 | y = y < 0 ? NaN : y / kx + x0; |
michael@0 | 7767 | return [ y, y + 1 / kx ]; |
michael@0 | 7768 | }; |
michael@0 | 7769 | scale.copy = function() { |
michael@0 | 7770 | return d3_scale_quantize(x0, x1, range); |
michael@0 | 7771 | }; |
michael@0 | 7772 | return rescale(); |
michael@0 | 7773 | } |
michael@0 | 7774 | d3.scale.threshold = function() { |
michael@0 | 7775 | return d3_scale_threshold([ .5 ], [ 0, 1 ]); |
michael@0 | 7776 | }; |
michael@0 | 7777 | function d3_scale_threshold(domain, range) { |
michael@0 | 7778 | function scale(x) { |
michael@0 | 7779 | if (x <= x) return range[d3.bisect(domain, x)]; |
michael@0 | 7780 | } |
michael@0 | 7781 | scale.domain = function(_) { |
michael@0 | 7782 | if (!arguments.length) return domain; |
michael@0 | 7783 | domain = _; |
michael@0 | 7784 | return scale; |
michael@0 | 7785 | }; |
michael@0 | 7786 | scale.range = function(_) { |
michael@0 | 7787 | if (!arguments.length) return range; |
michael@0 | 7788 | range = _; |
michael@0 | 7789 | return scale; |
michael@0 | 7790 | }; |
michael@0 | 7791 | scale.invertExtent = function(y) { |
michael@0 | 7792 | y = range.indexOf(y); |
michael@0 | 7793 | return [ domain[y - 1], domain[y] ]; |
michael@0 | 7794 | }; |
michael@0 | 7795 | scale.copy = function() { |
michael@0 | 7796 | return d3_scale_threshold(domain, range); |
michael@0 | 7797 | }; |
michael@0 | 7798 | return scale; |
michael@0 | 7799 | } |
michael@0 | 7800 | d3.scale.identity = function() { |
michael@0 | 7801 | return d3_scale_identity([ 0, 1 ]); |
michael@0 | 7802 | }; |
michael@0 | 7803 | function d3_scale_identity(domain) { |
michael@0 | 7804 | function identity(x) { |
michael@0 | 7805 | return +x; |
michael@0 | 7806 | } |
michael@0 | 7807 | identity.invert = identity; |
michael@0 | 7808 | identity.domain = identity.range = function(x) { |
michael@0 | 7809 | if (!arguments.length) return domain; |
michael@0 | 7810 | domain = x.map(identity); |
michael@0 | 7811 | return identity; |
michael@0 | 7812 | }; |
michael@0 | 7813 | identity.ticks = function(m) { |
michael@0 | 7814 | return d3_scale_linearTicks(domain, m); |
michael@0 | 7815 | }; |
michael@0 | 7816 | identity.tickFormat = function(m, format) { |
michael@0 | 7817 | return d3_scale_linearTickFormat(domain, m, format); |
michael@0 | 7818 | }; |
michael@0 | 7819 | identity.copy = function() { |
michael@0 | 7820 | return d3_scale_identity(domain); |
michael@0 | 7821 | }; |
michael@0 | 7822 | return identity; |
michael@0 | 7823 | } |
michael@0 | 7824 | d3.svg = {}; |
michael@0 | 7825 | d3.svg.arc = function() { |
michael@0 | 7826 | var innerRadius = d3_svg_arcInnerRadius, outerRadius = d3_svg_arcOuterRadius, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle; |
michael@0 | 7827 | function arc() { |
michael@0 | 7828 | var r0 = innerRadius.apply(this, arguments), r1 = outerRadius.apply(this, arguments), a0 = startAngle.apply(this, arguments) + d3_svg_arcOffset, a1 = endAngle.apply(this, arguments) + d3_svg_arcOffset, da = (a1 < a0 && (da = a0, |
michael@0 | 7829 | a0 = a1, a1 = da), a1 - a0), df = da < π ? "0" : "1", c0 = Math.cos(a0), s0 = Math.sin(a0), c1 = Math.cos(a1), s1 = Math.sin(a1); |
michael@0 | 7830 | return da >= d3_svg_arcMax ? r0 ? "M0," + r1 + "A" + r1 + "," + r1 + " 0 1,1 0," + -r1 + "A" + r1 + "," + r1 + " 0 1,1 0," + r1 + "M0," + r0 + "A" + r0 + "," + r0 + " 0 1,0 0," + -r0 + "A" + r0 + "," + r0 + " 0 1,0 0," + r0 + "Z" : "M0," + r1 + "A" + r1 + "," + r1 + " 0 1,1 0," + -r1 + "A" + r1 + "," + r1 + " 0 1,1 0," + r1 + "Z" : r0 ? "M" + r1 * c0 + "," + r1 * s0 + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1 + "L" + r0 * c1 + "," + r0 * s1 + "A" + r0 + "," + r0 + " 0 " + df + ",0 " + r0 * c0 + "," + r0 * s0 + "Z" : "M" + r1 * c0 + "," + r1 * s0 + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1 + "L0,0" + "Z"; |
michael@0 | 7831 | } |
michael@0 | 7832 | arc.innerRadius = function(v) { |
michael@0 | 7833 | if (!arguments.length) return innerRadius; |
michael@0 | 7834 | innerRadius = d3_functor(v); |
michael@0 | 7835 | return arc; |
michael@0 | 7836 | }; |
michael@0 | 7837 | arc.outerRadius = function(v) { |
michael@0 | 7838 | if (!arguments.length) return outerRadius; |
michael@0 | 7839 | outerRadius = d3_functor(v); |
michael@0 | 7840 | return arc; |
michael@0 | 7841 | }; |
michael@0 | 7842 | arc.startAngle = function(v) { |
michael@0 | 7843 | if (!arguments.length) return startAngle; |
michael@0 | 7844 | startAngle = d3_functor(v); |
michael@0 | 7845 | return arc; |
michael@0 | 7846 | }; |
michael@0 | 7847 | arc.endAngle = function(v) { |
michael@0 | 7848 | if (!arguments.length) return endAngle; |
michael@0 | 7849 | endAngle = d3_functor(v); |
michael@0 | 7850 | return arc; |
michael@0 | 7851 | }; |
michael@0 | 7852 | arc.centroid = function() { |
michael@0 | 7853 | var r = (innerRadius.apply(this, arguments) + outerRadius.apply(this, arguments)) / 2, a = (startAngle.apply(this, arguments) + endAngle.apply(this, arguments)) / 2 + d3_svg_arcOffset; |
michael@0 | 7854 | return [ Math.cos(a) * r, Math.sin(a) * r ]; |
michael@0 | 7855 | }; |
michael@0 | 7856 | return arc; |
michael@0 | 7857 | }; |
michael@0 | 7858 | var d3_svg_arcOffset = -halfπ, d3_svg_arcMax = τ - ε; |
michael@0 | 7859 | function d3_svg_arcInnerRadius(d) { |
michael@0 | 7860 | return d.innerRadius; |
michael@0 | 7861 | } |
michael@0 | 7862 | function d3_svg_arcOuterRadius(d) { |
michael@0 | 7863 | return d.outerRadius; |
michael@0 | 7864 | } |
michael@0 | 7865 | function d3_svg_arcStartAngle(d) { |
michael@0 | 7866 | return d.startAngle; |
michael@0 | 7867 | } |
michael@0 | 7868 | function d3_svg_arcEndAngle(d) { |
michael@0 | 7869 | return d.endAngle; |
michael@0 | 7870 | } |
michael@0 | 7871 | function d3_svg_line(projection) { |
michael@0 | 7872 | var x = d3_geom_pointX, y = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, tension = .7; |
michael@0 | 7873 | function line(data) { |
michael@0 | 7874 | var segments = [], points = [], i = -1, n = data.length, d, fx = d3_functor(x), fy = d3_functor(y); |
michael@0 | 7875 | function segment() { |
michael@0 | 7876 | segments.push("M", interpolate(projection(points), tension)); |
michael@0 | 7877 | } |
michael@0 | 7878 | while (++i < n) { |
michael@0 | 7879 | if (defined.call(this, d = data[i], i)) { |
michael@0 | 7880 | points.push([ +fx.call(this, d, i), +fy.call(this, d, i) ]); |
michael@0 | 7881 | } else if (points.length) { |
michael@0 | 7882 | segment(); |
michael@0 | 7883 | points = []; |
michael@0 | 7884 | } |
michael@0 | 7885 | } |
michael@0 | 7886 | if (points.length) segment(); |
michael@0 | 7887 | return segments.length ? segments.join("") : null; |
michael@0 | 7888 | } |
michael@0 | 7889 | line.x = function(_) { |
michael@0 | 7890 | if (!arguments.length) return x; |
michael@0 | 7891 | x = _; |
michael@0 | 7892 | return line; |
michael@0 | 7893 | }; |
michael@0 | 7894 | line.y = function(_) { |
michael@0 | 7895 | if (!arguments.length) return y; |
michael@0 | 7896 | y = _; |
michael@0 | 7897 | return line; |
michael@0 | 7898 | }; |
michael@0 | 7899 | line.defined = function(_) { |
michael@0 | 7900 | if (!arguments.length) return defined; |
michael@0 | 7901 | defined = _; |
michael@0 | 7902 | return line; |
michael@0 | 7903 | }; |
michael@0 | 7904 | line.interpolate = function(_) { |
michael@0 | 7905 | if (!arguments.length) return interpolateKey; |
michael@0 | 7906 | if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; |
michael@0 | 7907 | return line; |
michael@0 | 7908 | }; |
michael@0 | 7909 | line.tension = function(_) { |
michael@0 | 7910 | if (!arguments.length) return tension; |
michael@0 | 7911 | tension = _; |
michael@0 | 7912 | return line; |
michael@0 | 7913 | }; |
michael@0 | 7914 | return line; |
michael@0 | 7915 | } |
michael@0 | 7916 | d3.svg.line = function() { |
michael@0 | 7917 | return d3_svg_line(d3_identity); |
michael@0 | 7918 | }; |
michael@0 | 7919 | var d3_svg_lineInterpolators = d3.map({ |
michael@0 | 7920 | linear: d3_svg_lineLinear, |
michael@0 | 7921 | "linear-closed": d3_svg_lineLinearClosed, |
michael@0 | 7922 | step: d3_svg_lineStep, |
michael@0 | 7923 | "step-before": d3_svg_lineStepBefore, |
michael@0 | 7924 | "step-after": d3_svg_lineStepAfter, |
michael@0 | 7925 | basis: d3_svg_lineBasis, |
michael@0 | 7926 | "basis-open": d3_svg_lineBasisOpen, |
michael@0 | 7927 | "basis-closed": d3_svg_lineBasisClosed, |
michael@0 | 7928 | bundle: d3_svg_lineBundle, |
michael@0 | 7929 | cardinal: d3_svg_lineCardinal, |
michael@0 | 7930 | "cardinal-open": d3_svg_lineCardinalOpen, |
michael@0 | 7931 | "cardinal-closed": d3_svg_lineCardinalClosed, |
michael@0 | 7932 | monotone: d3_svg_lineMonotone |
michael@0 | 7933 | }); |
michael@0 | 7934 | d3_svg_lineInterpolators.forEach(function(key, value) { |
michael@0 | 7935 | value.key = key; |
michael@0 | 7936 | value.closed = /-closed$/.test(key); |
michael@0 | 7937 | }); |
michael@0 | 7938 | function d3_svg_lineLinear(points) { |
michael@0 | 7939 | return points.join("L"); |
michael@0 | 7940 | } |
michael@0 | 7941 | function d3_svg_lineLinearClosed(points) { |
michael@0 | 7942 | return d3_svg_lineLinear(points) + "Z"; |
michael@0 | 7943 | } |
michael@0 | 7944 | function d3_svg_lineStep(points) { |
michael@0 | 7945 | var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; |
michael@0 | 7946 | while (++i < n) path.push("H", (p[0] + (p = points[i])[0]) / 2, "V", p[1]); |
michael@0 | 7947 | if (n > 1) path.push("H", p[0]); |
michael@0 | 7948 | return path.join(""); |
michael@0 | 7949 | } |
michael@0 | 7950 | function d3_svg_lineStepBefore(points) { |
michael@0 | 7951 | var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; |
michael@0 | 7952 | while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]); |
michael@0 | 7953 | return path.join(""); |
michael@0 | 7954 | } |
michael@0 | 7955 | function d3_svg_lineStepAfter(points) { |
michael@0 | 7956 | var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; |
michael@0 | 7957 | while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]); |
michael@0 | 7958 | return path.join(""); |
michael@0 | 7959 | } |
michael@0 | 7960 | function d3_svg_lineCardinalOpen(points, tension) { |
michael@0 | 7961 | return points.length < 4 ? d3_svg_lineLinear(points) : points[1] + d3_svg_lineHermite(points.slice(1, points.length - 1), d3_svg_lineCardinalTangents(points, tension)); |
michael@0 | 7962 | } |
michael@0 | 7963 | function d3_svg_lineCardinalClosed(points, tension) { |
michael@0 | 7964 | return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite((points.push(points[0]), |
michael@0 | 7965 | points), d3_svg_lineCardinalTangents([ points[points.length - 2] ].concat(points, [ points[1] ]), tension)); |
michael@0 | 7966 | } |
michael@0 | 7967 | function d3_svg_lineCardinal(points, tension) { |
michael@0 | 7968 | return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineCardinalTangents(points, tension)); |
michael@0 | 7969 | } |
michael@0 | 7970 | function d3_svg_lineHermite(points, tangents) { |
michael@0 | 7971 | if (tangents.length < 1 || points.length != tangents.length && points.length != tangents.length + 2) { |
michael@0 | 7972 | return d3_svg_lineLinear(points); |
michael@0 | 7973 | } |
michael@0 | 7974 | var quad = points.length != tangents.length, path = "", p0 = points[0], p = points[1], t0 = tangents[0], t = t0, pi = 1; |
michael@0 | 7975 | if (quad) { |
michael@0 | 7976 | path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3) + "," + p[0] + "," + p[1]; |
michael@0 | 7977 | p0 = points[1]; |
michael@0 | 7978 | pi = 2; |
michael@0 | 7979 | } |
michael@0 | 7980 | if (tangents.length > 1) { |
michael@0 | 7981 | t = tangents[1]; |
michael@0 | 7982 | p = points[pi]; |
michael@0 | 7983 | pi++; |
michael@0 | 7984 | path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1]) + "," + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; |
michael@0 | 7985 | for (var i = 2; i < tangents.length; i++, pi++) { |
michael@0 | 7986 | p = points[pi]; |
michael@0 | 7987 | t = tangents[i]; |
michael@0 | 7988 | path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; |
michael@0 | 7989 | } |
michael@0 | 7990 | } |
michael@0 | 7991 | if (quad) { |
michael@0 | 7992 | var lp = points[pi]; |
michael@0 | 7993 | path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3) + "," + lp[0] + "," + lp[1]; |
michael@0 | 7994 | } |
michael@0 | 7995 | return path; |
michael@0 | 7996 | } |
michael@0 | 7997 | function d3_svg_lineCardinalTangents(points, tension) { |
michael@0 | 7998 | var tangents = [], a = (1 - tension) / 2, p0, p1 = points[0], p2 = points[1], i = 1, n = points.length; |
michael@0 | 7999 | while (++i < n) { |
michael@0 | 8000 | p0 = p1; |
michael@0 | 8001 | p1 = p2; |
michael@0 | 8002 | p2 = points[i]; |
michael@0 | 8003 | tangents.push([ a * (p2[0] - p0[0]), a * (p2[1] - p0[1]) ]); |
michael@0 | 8004 | } |
michael@0 | 8005 | return tangents; |
michael@0 | 8006 | } |
michael@0 | 8007 | function d3_svg_lineBasis(points) { |
michael@0 | 8008 | if (points.length < 3) return d3_svg_lineLinear(points); |
michael@0 | 8009 | var i = 1, n = points.length, pi = points[0], x0 = pi[0], y0 = pi[1], px = [ x0, x0, x0, (pi = points[1])[0] ], py = [ y0, y0, y0, pi[1] ], path = [ x0, ",", y0, "L", d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; |
michael@0 | 8010 | points.push(points[n - 1]); |
michael@0 | 8011 | while (++i <= n) { |
michael@0 | 8012 | pi = points[i]; |
michael@0 | 8013 | px.shift(); |
michael@0 | 8014 | px.push(pi[0]); |
michael@0 | 8015 | py.shift(); |
michael@0 | 8016 | py.push(pi[1]); |
michael@0 | 8017 | d3_svg_lineBasisBezier(path, px, py); |
michael@0 | 8018 | } |
michael@0 | 8019 | points.pop(); |
michael@0 | 8020 | path.push("L", pi); |
michael@0 | 8021 | return path.join(""); |
michael@0 | 8022 | } |
michael@0 | 8023 | function d3_svg_lineBasisOpen(points) { |
michael@0 | 8024 | if (points.length < 4) return d3_svg_lineLinear(points); |
michael@0 | 8025 | var path = [], i = -1, n = points.length, pi, px = [ 0 ], py = [ 0 ]; |
michael@0 | 8026 | while (++i < 3) { |
michael@0 | 8027 | pi = points[i]; |
michael@0 | 8028 | px.push(pi[0]); |
michael@0 | 8029 | py.push(pi[1]); |
michael@0 | 8030 | } |
michael@0 | 8031 | path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px) + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)); |
michael@0 | 8032 | --i; |
michael@0 | 8033 | while (++i < n) { |
michael@0 | 8034 | pi = points[i]; |
michael@0 | 8035 | px.shift(); |
michael@0 | 8036 | px.push(pi[0]); |
michael@0 | 8037 | py.shift(); |
michael@0 | 8038 | py.push(pi[1]); |
michael@0 | 8039 | d3_svg_lineBasisBezier(path, px, py); |
michael@0 | 8040 | } |
michael@0 | 8041 | return path.join(""); |
michael@0 | 8042 | } |
michael@0 | 8043 | function d3_svg_lineBasisClosed(points) { |
michael@0 | 8044 | var path, i = -1, n = points.length, m = n + 4, pi, px = [], py = []; |
michael@0 | 8045 | while (++i < 4) { |
michael@0 | 8046 | pi = points[i % n]; |
michael@0 | 8047 | px.push(pi[0]); |
michael@0 | 8048 | py.push(pi[1]); |
michael@0 | 8049 | } |
michael@0 | 8050 | path = [ d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; |
michael@0 | 8051 | --i; |
michael@0 | 8052 | while (++i < m) { |
michael@0 | 8053 | pi = points[i % n]; |
michael@0 | 8054 | px.shift(); |
michael@0 | 8055 | px.push(pi[0]); |
michael@0 | 8056 | py.shift(); |
michael@0 | 8057 | py.push(pi[1]); |
michael@0 | 8058 | d3_svg_lineBasisBezier(path, px, py); |
michael@0 | 8059 | } |
michael@0 | 8060 | return path.join(""); |
michael@0 | 8061 | } |
michael@0 | 8062 | function d3_svg_lineBundle(points, tension) { |
michael@0 | 8063 | var n = points.length - 1; |
michael@0 | 8064 | if (n) { |
michael@0 | 8065 | var x0 = points[0][0], y0 = points[0][1], dx = points[n][0] - x0, dy = points[n][1] - y0, i = -1, p, t; |
michael@0 | 8066 | while (++i <= n) { |
michael@0 | 8067 | p = points[i]; |
michael@0 | 8068 | t = i / n; |
michael@0 | 8069 | p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx); |
michael@0 | 8070 | p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy); |
michael@0 | 8071 | } |
michael@0 | 8072 | } |
michael@0 | 8073 | return d3_svg_lineBasis(points); |
michael@0 | 8074 | } |
michael@0 | 8075 | function d3_svg_lineDot4(a, b) { |
michael@0 | 8076 | return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; |
michael@0 | 8077 | } |
michael@0 | 8078 | var d3_svg_lineBasisBezier1 = [ 0, 2 / 3, 1 / 3, 0 ], d3_svg_lineBasisBezier2 = [ 0, 1 / 3, 2 / 3, 0 ], d3_svg_lineBasisBezier3 = [ 0, 1 / 6, 2 / 3, 1 / 6 ]; |
michael@0 | 8079 | function d3_svg_lineBasisBezier(path, x, y) { |
michael@0 | 8080 | path.push("C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y)); |
michael@0 | 8081 | } |
michael@0 | 8082 | function d3_svg_lineSlope(p0, p1) { |
michael@0 | 8083 | return (p1[1] - p0[1]) / (p1[0] - p0[0]); |
michael@0 | 8084 | } |
michael@0 | 8085 | function d3_svg_lineFiniteDifferences(points) { |
michael@0 | 8086 | var i = 0, j = points.length - 1, m = [], p0 = points[0], p1 = points[1], d = m[0] = d3_svg_lineSlope(p0, p1); |
michael@0 | 8087 | while (++i < j) { |
michael@0 | 8088 | m[i] = (d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1]))) / 2; |
michael@0 | 8089 | } |
michael@0 | 8090 | m[i] = d; |
michael@0 | 8091 | return m; |
michael@0 | 8092 | } |
michael@0 | 8093 | function d3_svg_lineMonotoneTangents(points) { |
michael@0 | 8094 | var tangents = [], d, a, b, s, m = d3_svg_lineFiniteDifferences(points), i = -1, j = points.length - 1; |
michael@0 | 8095 | while (++i < j) { |
michael@0 | 8096 | d = d3_svg_lineSlope(points[i], points[i + 1]); |
michael@0 | 8097 | if (abs(d) < ε) { |
michael@0 | 8098 | m[i] = m[i + 1] = 0; |
michael@0 | 8099 | } else { |
michael@0 | 8100 | a = m[i] / d; |
michael@0 | 8101 | b = m[i + 1] / d; |
michael@0 | 8102 | s = a * a + b * b; |
michael@0 | 8103 | if (s > 9) { |
michael@0 | 8104 | s = d * 3 / Math.sqrt(s); |
michael@0 | 8105 | m[i] = s * a; |
michael@0 | 8106 | m[i + 1] = s * b; |
michael@0 | 8107 | } |
michael@0 | 8108 | } |
michael@0 | 8109 | } |
michael@0 | 8110 | i = -1; |
michael@0 | 8111 | while (++i <= j) { |
michael@0 | 8112 | s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0]) / (6 * (1 + m[i] * m[i])); |
michael@0 | 8113 | tangents.push([ s || 0, m[i] * s || 0 ]); |
michael@0 | 8114 | } |
michael@0 | 8115 | return tangents; |
michael@0 | 8116 | } |
michael@0 | 8117 | function d3_svg_lineMonotone(points) { |
michael@0 | 8118 | return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points)); |
michael@0 | 8119 | } |
michael@0 | 8120 | d3.svg.line.radial = function() { |
michael@0 | 8121 | var line = d3_svg_line(d3_svg_lineRadial); |
michael@0 | 8122 | line.radius = line.x, delete line.x; |
michael@0 | 8123 | line.angle = line.y, delete line.y; |
michael@0 | 8124 | return line; |
michael@0 | 8125 | }; |
michael@0 | 8126 | function d3_svg_lineRadial(points) { |
michael@0 | 8127 | var point, i = -1, n = points.length, r, a; |
michael@0 | 8128 | while (++i < n) { |
michael@0 | 8129 | point = points[i]; |
michael@0 | 8130 | r = point[0]; |
michael@0 | 8131 | a = point[1] + d3_svg_arcOffset; |
michael@0 | 8132 | point[0] = r * Math.cos(a); |
michael@0 | 8133 | point[1] = r * Math.sin(a); |
michael@0 | 8134 | } |
michael@0 | 8135 | return points; |
michael@0 | 8136 | } |
michael@0 | 8137 | function d3_svg_area(projection) { |
michael@0 | 8138 | var x0 = d3_geom_pointX, x1 = d3_geom_pointX, y0 = 0, y1 = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, interpolateReverse = interpolate, L = "L", tension = .7; |
michael@0 | 8139 | function area(data) { |
michael@0 | 8140 | var segments = [], points0 = [], points1 = [], i = -1, n = data.length, d, fx0 = d3_functor(x0), fy0 = d3_functor(y0), fx1 = x0 === x1 ? function() { |
michael@0 | 8141 | return x; |
michael@0 | 8142 | } : d3_functor(x1), fy1 = y0 === y1 ? function() { |
michael@0 | 8143 | return y; |
michael@0 | 8144 | } : d3_functor(y1), x, y; |
michael@0 | 8145 | function segment() { |
michael@0 | 8146 | segments.push("M", interpolate(projection(points1), tension), L, interpolateReverse(projection(points0.reverse()), tension), "Z"); |
michael@0 | 8147 | } |
michael@0 | 8148 | while (++i < n) { |
michael@0 | 8149 | if (defined.call(this, d = data[i], i)) { |
michael@0 | 8150 | points0.push([ x = +fx0.call(this, d, i), y = +fy0.call(this, d, i) ]); |
michael@0 | 8151 | points1.push([ +fx1.call(this, d, i), +fy1.call(this, d, i) ]); |
michael@0 | 8152 | } else if (points0.length) { |
michael@0 | 8153 | segment(); |
michael@0 | 8154 | points0 = []; |
michael@0 | 8155 | points1 = []; |
michael@0 | 8156 | } |
michael@0 | 8157 | } |
michael@0 | 8158 | if (points0.length) segment(); |
michael@0 | 8159 | return segments.length ? segments.join("") : null; |
michael@0 | 8160 | } |
michael@0 | 8161 | area.x = function(_) { |
michael@0 | 8162 | if (!arguments.length) return x1; |
michael@0 | 8163 | x0 = x1 = _; |
michael@0 | 8164 | return area; |
michael@0 | 8165 | }; |
michael@0 | 8166 | area.x0 = function(_) { |
michael@0 | 8167 | if (!arguments.length) return x0; |
michael@0 | 8168 | x0 = _; |
michael@0 | 8169 | return area; |
michael@0 | 8170 | }; |
michael@0 | 8171 | area.x1 = function(_) { |
michael@0 | 8172 | if (!arguments.length) return x1; |
michael@0 | 8173 | x1 = _; |
michael@0 | 8174 | return area; |
michael@0 | 8175 | }; |
michael@0 | 8176 | area.y = function(_) { |
michael@0 | 8177 | if (!arguments.length) return y1; |
michael@0 | 8178 | y0 = y1 = _; |
michael@0 | 8179 | return area; |
michael@0 | 8180 | }; |
michael@0 | 8181 | area.y0 = function(_) { |
michael@0 | 8182 | if (!arguments.length) return y0; |
michael@0 | 8183 | y0 = _; |
michael@0 | 8184 | return area; |
michael@0 | 8185 | }; |
michael@0 | 8186 | area.y1 = function(_) { |
michael@0 | 8187 | if (!arguments.length) return y1; |
michael@0 | 8188 | y1 = _; |
michael@0 | 8189 | return area; |
michael@0 | 8190 | }; |
michael@0 | 8191 | area.defined = function(_) { |
michael@0 | 8192 | if (!arguments.length) return defined; |
michael@0 | 8193 | defined = _; |
michael@0 | 8194 | return area; |
michael@0 | 8195 | }; |
michael@0 | 8196 | area.interpolate = function(_) { |
michael@0 | 8197 | if (!arguments.length) return interpolateKey; |
michael@0 | 8198 | if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; |
michael@0 | 8199 | interpolateReverse = interpolate.reverse || interpolate; |
michael@0 | 8200 | L = interpolate.closed ? "M" : "L"; |
michael@0 | 8201 | return area; |
michael@0 | 8202 | }; |
michael@0 | 8203 | area.tension = function(_) { |
michael@0 | 8204 | if (!arguments.length) return tension; |
michael@0 | 8205 | tension = _; |
michael@0 | 8206 | return area; |
michael@0 | 8207 | }; |
michael@0 | 8208 | return area; |
michael@0 | 8209 | } |
michael@0 | 8210 | d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter; |
michael@0 | 8211 | d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore; |
michael@0 | 8212 | d3.svg.area = function() { |
michael@0 | 8213 | return d3_svg_area(d3_identity); |
michael@0 | 8214 | }; |
michael@0 | 8215 | d3.svg.area.radial = function() { |
michael@0 | 8216 | var area = d3_svg_area(d3_svg_lineRadial); |
michael@0 | 8217 | area.radius = area.x, delete area.x; |
michael@0 | 8218 | area.innerRadius = area.x0, delete area.x0; |
michael@0 | 8219 | area.outerRadius = area.x1, delete area.x1; |
michael@0 | 8220 | area.angle = area.y, delete area.y; |
michael@0 | 8221 | area.startAngle = area.y0, delete area.y0; |
michael@0 | 8222 | area.endAngle = area.y1, delete area.y1; |
michael@0 | 8223 | return area; |
michael@0 | 8224 | }; |
michael@0 | 8225 | d3.svg.chord = function() { |
michael@0 | 8226 | var source = d3_source, target = d3_target, radius = d3_svg_chordRadius, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle; |
michael@0 | 8227 | function chord(d, i) { |
michael@0 | 8228 | var s = subgroup(this, source, d, i), t = subgroup(this, target, d, i); |
michael@0 | 8229 | return "M" + s.p0 + arc(s.r, s.p1, s.a1 - s.a0) + (equals(s, t) ? curve(s.r, s.p1, s.r, s.p0) : curve(s.r, s.p1, t.r, t.p0) + arc(t.r, t.p1, t.a1 - t.a0) + curve(t.r, t.p1, s.r, s.p0)) + "Z"; |
michael@0 | 8230 | } |
michael@0 | 8231 | function subgroup(self, f, d, i) { |
michael@0 | 8232 | var subgroup = f.call(self, d, i), r = radius.call(self, subgroup, i), a0 = startAngle.call(self, subgroup, i) + d3_svg_arcOffset, a1 = endAngle.call(self, subgroup, i) + d3_svg_arcOffset; |
michael@0 | 8233 | return { |
michael@0 | 8234 | r: r, |
michael@0 | 8235 | a0: a0, |
michael@0 | 8236 | a1: a1, |
michael@0 | 8237 | p0: [ r * Math.cos(a0), r * Math.sin(a0) ], |
michael@0 | 8238 | p1: [ r * Math.cos(a1), r * Math.sin(a1) ] |
michael@0 | 8239 | }; |
michael@0 | 8240 | } |
michael@0 | 8241 | function equals(a, b) { |
michael@0 | 8242 | return a.a0 == b.a0 && a.a1 == b.a1; |
michael@0 | 8243 | } |
michael@0 | 8244 | function arc(r, p, a) { |
michael@0 | 8245 | return "A" + r + "," + r + " 0 " + +(a > π) + ",1 " + p; |
michael@0 | 8246 | } |
michael@0 | 8247 | function curve(r0, p0, r1, p1) { |
michael@0 | 8248 | return "Q 0,0 " + p1; |
michael@0 | 8249 | } |
michael@0 | 8250 | chord.radius = function(v) { |
michael@0 | 8251 | if (!arguments.length) return radius; |
michael@0 | 8252 | radius = d3_functor(v); |
michael@0 | 8253 | return chord; |
michael@0 | 8254 | }; |
michael@0 | 8255 | chord.source = function(v) { |
michael@0 | 8256 | if (!arguments.length) return source; |
michael@0 | 8257 | source = d3_functor(v); |
michael@0 | 8258 | return chord; |
michael@0 | 8259 | }; |
michael@0 | 8260 | chord.target = function(v) { |
michael@0 | 8261 | if (!arguments.length) return target; |
michael@0 | 8262 | target = d3_functor(v); |
michael@0 | 8263 | return chord; |
michael@0 | 8264 | }; |
michael@0 | 8265 | chord.startAngle = function(v) { |
michael@0 | 8266 | if (!arguments.length) return startAngle; |
michael@0 | 8267 | startAngle = d3_functor(v); |
michael@0 | 8268 | return chord; |
michael@0 | 8269 | }; |
michael@0 | 8270 | chord.endAngle = function(v) { |
michael@0 | 8271 | if (!arguments.length) return endAngle; |
michael@0 | 8272 | endAngle = d3_functor(v); |
michael@0 | 8273 | return chord; |
michael@0 | 8274 | }; |
michael@0 | 8275 | return chord; |
michael@0 | 8276 | }; |
michael@0 | 8277 | function d3_svg_chordRadius(d) { |
michael@0 | 8278 | return d.radius; |
michael@0 | 8279 | } |
michael@0 | 8280 | d3.svg.diagonal = function() { |
michael@0 | 8281 | var source = d3_source, target = d3_target, projection = d3_svg_diagonalProjection; |
michael@0 | 8282 | function diagonal(d, i) { |
michael@0 | 8283 | var p0 = source.call(this, d, i), p3 = target.call(this, d, i), m = (p0.y + p3.y) / 2, p = [ p0, { |
michael@0 | 8284 | x: p0.x, |
michael@0 | 8285 | y: m |
michael@0 | 8286 | }, { |
michael@0 | 8287 | x: p3.x, |
michael@0 | 8288 | y: m |
michael@0 | 8289 | }, p3 ]; |
michael@0 | 8290 | p = p.map(projection); |
michael@0 | 8291 | return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3]; |
michael@0 | 8292 | } |
michael@0 | 8293 | diagonal.source = function(x) { |
michael@0 | 8294 | if (!arguments.length) return source; |
michael@0 | 8295 | source = d3_functor(x); |
michael@0 | 8296 | return diagonal; |
michael@0 | 8297 | }; |
michael@0 | 8298 | diagonal.target = function(x) { |
michael@0 | 8299 | if (!arguments.length) return target; |
michael@0 | 8300 | target = d3_functor(x); |
michael@0 | 8301 | return diagonal; |
michael@0 | 8302 | }; |
michael@0 | 8303 | diagonal.projection = function(x) { |
michael@0 | 8304 | if (!arguments.length) return projection; |
michael@0 | 8305 | projection = x; |
michael@0 | 8306 | return diagonal; |
michael@0 | 8307 | }; |
michael@0 | 8308 | return diagonal; |
michael@0 | 8309 | }; |
michael@0 | 8310 | function d3_svg_diagonalProjection(d) { |
michael@0 | 8311 | return [ d.x, d.y ]; |
michael@0 | 8312 | } |
michael@0 | 8313 | d3.svg.diagonal.radial = function() { |
michael@0 | 8314 | var diagonal = d3.svg.diagonal(), projection = d3_svg_diagonalProjection, projection_ = diagonal.projection; |
michael@0 | 8315 | diagonal.projection = function(x) { |
michael@0 | 8316 | return arguments.length ? projection_(d3_svg_diagonalRadialProjection(projection = x)) : projection; |
michael@0 | 8317 | }; |
michael@0 | 8318 | return diagonal; |
michael@0 | 8319 | }; |
michael@0 | 8320 | function d3_svg_diagonalRadialProjection(projection) { |
michael@0 | 8321 | return function() { |
michael@0 | 8322 | var d = projection.apply(this, arguments), r = d[0], a = d[1] + d3_svg_arcOffset; |
michael@0 | 8323 | return [ r * Math.cos(a), r * Math.sin(a) ]; |
michael@0 | 8324 | }; |
michael@0 | 8325 | } |
michael@0 | 8326 | d3.svg.symbol = function() { |
michael@0 | 8327 | var type = d3_svg_symbolType, size = d3_svg_symbolSize; |
michael@0 | 8328 | function symbol(d, i) { |
michael@0 | 8329 | return (d3_svg_symbols.get(type.call(this, d, i)) || d3_svg_symbolCircle)(size.call(this, d, i)); |
michael@0 | 8330 | } |
michael@0 | 8331 | symbol.type = function(x) { |
michael@0 | 8332 | if (!arguments.length) return type; |
michael@0 | 8333 | type = d3_functor(x); |
michael@0 | 8334 | return symbol; |
michael@0 | 8335 | }; |
michael@0 | 8336 | symbol.size = function(x) { |
michael@0 | 8337 | if (!arguments.length) return size; |
michael@0 | 8338 | size = d3_functor(x); |
michael@0 | 8339 | return symbol; |
michael@0 | 8340 | }; |
michael@0 | 8341 | return symbol; |
michael@0 | 8342 | }; |
michael@0 | 8343 | function d3_svg_symbolSize() { |
michael@0 | 8344 | return 64; |
michael@0 | 8345 | } |
michael@0 | 8346 | function d3_svg_symbolType() { |
michael@0 | 8347 | return "circle"; |
michael@0 | 8348 | } |
michael@0 | 8349 | function d3_svg_symbolCircle(size) { |
michael@0 | 8350 | var r = Math.sqrt(size / π); |
michael@0 | 8351 | return "M0," + r + "A" + r + "," + r + " 0 1,1 0," + -r + "A" + r + "," + r + " 0 1,1 0," + r + "Z"; |
michael@0 | 8352 | } |
michael@0 | 8353 | var d3_svg_symbols = d3.map({ |
michael@0 | 8354 | circle: d3_svg_symbolCircle, |
michael@0 | 8355 | cross: function(size) { |
michael@0 | 8356 | var r = Math.sqrt(size / 5) / 2; |
michael@0 | 8357 | return "M" + -3 * r + "," + -r + "H" + -r + "V" + -3 * r + "H" + r + "V" + -r + "H" + 3 * r + "V" + r + "H" + r + "V" + 3 * r + "H" + -r + "V" + r + "H" + -3 * r + "Z"; |
michael@0 | 8358 | }, |
michael@0 | 8359 | diamond: function(size) { |
michael@0 | 8360 | var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)), rx = ry * d3_svg_symbolTan30; |
michael@0 | 8361 | return "M0," + -ry + "L" + rx + ",0" + " 0," + ry + " " + -rx + ",0" + "Z"; |
michael@0 | 8362 | }, |
michael@0 | 8363 | square: function(size) { |
michael@0 | 8364 | var r = Math.sqrt(size) / 2; |
michael@0 | 8365 | return "M" + -r + "," + -r + "L" + r + "," + -r + " " + r + "," + r + " " + -r + "," + r + "Z"; |
michael@0 | 8366 | }, |
michael@0 | 8367 | "triangle-down": function(size) { |
michael@0 | 8368 | var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; |
michael@0 | 8369 | return "M0," + ry + "L" + rx + "," + -ry + " " + -rx + "," + -ry + "Z"; |
michael@0 | 8370 | }, |
michael@0 | 8371 | "triangle-up": function(size) { |
michael@0 | 8372 | var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; |
michael@0 | 8373 | return "M0," + -ry + "L" + rx + "," + ry + " " + -rx + "," + ry + "Z"; |
michael@0 | 8374 | } |
michael@0 | 8375 | }); |
michael@0 | 8376 | d3.svg.symbolTypes = d3_svg_symbols.keys(); |
michael@0 | 8377 | var d3_svg_symbolSqrt3 = Math.sqrt(3), d3_svg_symbolTan30 = Math.tan(30 * d3_radians); |
michael@0 | 8378 | function d3_transition(groups, id) { |
michael@0 | 8379 | d3_subclass(groups, d3_transitionPrototype); |
michael@0 | 8380 | groups.id = id; |
michael@0 | 8381 | return groups; |
michael@0 | 8382 | } |
michael@0 | 8383 | var d3_transitionPrototype = [], d3_transitionId = 0, d3_transitionInheritId, d3_transitionInherit; |
michael@0 | 8384 | d3_transitionPrototype.call = d3_selectionPrototype.call; |
michael@0 | 8385 | d3_transitionPrototype.empty = d3_selectionPrototype.empty; |
michael@0 | 8386 | d3_transitionPrototype.node = d3_selectionPrototype.node; |
michael@0 | 8387 | d3_transitionPrototype.size = d3_selectionPrototype.size; |
michael@0 | 8388 | d3.transition = function(selection) { |
michael@0 | 8389 | return arguments.length ? d3_transitionInheritId ? selection.transition() : selection : d3_selectionRoot.transition(); |
michael@0 | 8390 | }; |
michael@0 | 8391 | d3.transition.prototype = d3_transitionPrototype; |
michael@0 | 8392 | d3_transitionPrototype.select = function(selector) { |
michael@0 | 8393 | var id = this.id, subgroups = [], subgroup, subnode, node; |
michael@0 | 8394 | selector = d3_selection_selector(selector); |
michael@0 | 8395 | for (var j = -1, m = this.length; ++j < m; ) { |
michael@0 | 8396 | subgroups.push(subgroup = []); |
michael@0 | 8397 | for (var group = this[j], i = -1, n = group.length; ++i < n; ) { |
michael@0 | 8398 | if ((node = group[i]) && (subnode = selector.call(node, node.__data__, i, j))) { |
michael@0 | 8399 | if ("__data__" in node) subnode.__data__ = node.__data__; |
michael@0 | 8400 | d3_transitionNode(subnode, i, id, node.__transition__[id]); |
michael@0 | 8401 | subgroup.push(subnode); |
michael@0 | 8402 | } else { |
michael@0 | 8403 | subgroup.push(null); |
michael@0 | 8404 | } |
michael@0 | 8405 | } |
michael@0 | 8406 | } |
michael@0 | 8407 | return d3_transition(subgroups, id); |
michael@0 | 8408 | }; |
michael@0 | 8409 | d3_transitionPrototype.selectAll = function(selector) { |
michael@0 | 8410 | var id = this.id, subgroups = [], subgroup, subnodes, node, subnode, transition; |
michael@0 | 8411 | selector = d3_selection_selectorAll(selector); |
michael@0 | 8412 | for (var j = -1, m = this.length; ++j < m; ) { |
michael@0 | 8413 | for (var group = this[j], i = -1, n = group.length; ++i < n; ) { |
michael@0 | 8414 | if (node = group[i]) { |
michael@0 | 8415 | transition = node.__transition__[id]; |
michael@0 | 8416 | subnodes = selector.call(node, node.__data__, i, j); |
michael@0 | 8417 | subgroups.push(subgroup = []); |
michael@0 | 8418 | for (var k = -1, o = subnodes.length; ++k < o; ) { |
michael@0 | 8419 | if (subnode = subnodes[k]) d3_transitionNode(subnode, k, id, transition); |
michael@0 | 8420 | subgroup.push(subnode); |
michael@0 | 8421 | } |
michael@0 | 8422 | } |
michael@0 | 8423 | } |
michael@0 | 8424 | } |
michael@0 | 8425 | return d3_transition(subgroups, id); |
michael@0 | 8426 | }; |
michael@0 | 8427 | d3_transitionPrototype.filter = function(filter) { |
michael@0 | 8428 | var subgroups = [], subgroup, group, node; |
michael@0 | 8429 | if (typeof filter !== "function") filter = d3_selection_filter(filter); |
michael@0 | 8430 | for (var j = 0, m = this.length; j < m; j++) { |
michael@0 | 8431 | subgroups.push(subgroup = []); |
michael@0 | 8432 | for (var group = this[j], i = 0, n = group.length; i < n; i++) { |
michael@0 | 8433 | if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { |
michael@0 | 8434 | subgroup.push(node); |
michael@0 | 8435 | } |
michael@0 | 8436 | } |
michael@0 | 8437 | } |
michael@0 | 8438 | return d3_transition(subgroups, this.id); |
michael@0 | 8439 | }; |
michael@0 | 8440 | d3_transitionPrototype.tween = function(name, tween) { |
michael@0 | 8441 | var id = this.id; |
michael@0 | 8442 | if (arguments.length < 2) return this.node().__transition__[id].tween.get(name); |
michael@0 | 8443 | return d3_selection_each(this, tween == null ? function(node) { |
michael@0 | 8444 | node.__transition__[id].tween.remove(name); |
michael@0 | 8445 | } : function(node) { |
michael@0 | 8446 | node.__transition__[id].tween.set(name, tween); |
michael@0 | 8447 | }); |
michael@0 | 8448 | }; |
michael@0 | 8449 | function d3_transition_tween(groups, name, value, tween) { |
michael@0 | 8450 | var id = groups.id; |
michael@0 | 8451 | return d3_selection_each(groups, typeof value === "function" ? function(node, i, j) { |
michael@0 | 8452 | node.__transition__[id].tween.set(name, tween(value.call(node, node.__data__, i, j))); |
michael@0 | 8453 | } : (value = tween(value), function(node) { |
michael@0 | 8454 | node.__transition__[id].tween.set(name, value); |
michael@0 | 8455 | })); |
michael@0 | 8456 | } |
michael@0 | 8457 | d3_transitionPrototype.attr = function(nameNS, value) { |
michael@0 | 8458 | if (arguments.length < 2) { |
michael@0 | 8459 | for (value in nameNS) this.attr(value, nameNS[value]); |
michael@0 | 8460 | return this; |
michael@0 | 8461 | } |
michael@0 | 8462 | var interpolate = nameNS == "transform" ? d3_interpolateTransform : d3_interpolate, name = d3.ns.qualify(nameNS); |
michael@0 | 8463 | function attrNull() { |
michael@0 | 8464 | this.removeAttribute(name); |
michael@0 | 8465 | } |
michael@0 | 8466 | function attrNullNS() { |
michael@0 | 8467 | this.removeAttributeNS(name.space, name.local); |
michael@0 | 8468 | } |
michael@0 | 8469 | function attrTween(b) { |
michael@0 | 8470 | return b == null ? attrNull : (b += "", function() { |
michael@0 | 8471 | var a = this.getAttribute(name), i; |
michael@0 | 8472 | return a !== b && (i = interpolate(a, b), function(t) { |
michael@0 | 8473 | this.setAttribute(name, i(t)); |
michael@0 | 8474 | }); |
michael@0 | 8475 | }); |
michael@0 | 8476 | } |
michael@0 | 8477 | function attrTweenNS(b) { |
michael@0 | 8478 | return b == null ? attrNullNS : (b += "", function() { |
michael@0 | 8479 | var a = this.getAttributeNS(name.space, name.local), i; |
michael@0 | 8480 | return a !== b && (i = interpolate(a, b), function(t) { |
michael@0 | 8481 | this.setAttributeNS(name.space, name.local, i(t)); |
michael@0 | 8482 | }); |
michael@0 | 8483 | }); |
michael@0 | 8484 | } |
michael@0 | 8485 | return d3_transition_tween(this, "attr." + nameNS, value, name.local ? attrTweenNS : attrTween); |
michael@0 | 8486 | }; |
michael@0 | 8487 | d3_transitionPrototype.attrTween = function(nameNS, tween) { |
michael@0 | 8488 | var name = d3.ns.qualify(nameNS); |
michael@0 | 8489 | function attrTween(d, i) { |
michael@0 | 8490 | var f = tween.call(this, d, i, this.getAttribute(name)); |
michael@0 | 8491 | return f && function(t) { |
michael@0 | 8492 | this.setAttribute(name, f(t)); |
michael@0 | 8493 | }; |
michael@0 | 8494 | } |
michael@0 | 8495 | function attrTweenNS(d, i) { |
michael@0 | 8496 | var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local)); |
michael@0 | 8497 | return f && function(t) { |
michael@0 | 8498 | this.setAttributeNS(name.space, name.local, f(t)); |
michael@0 | 8499 | }; |
michael@0 | 8500 | } |
michael@0 | 8501 | return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween); |
michael@0 | 8502 | }; |
michael@0 | 8503 | d3_transitionPrototype.style = function(name, value, priority) { |
michael@0 | 8504 | var n = arguments.length; |
michael@0 | 8505 | if (n < 3) { |
michael@0 | 8506 | if (typeof name !== "string") { |
michael@0 | 8507 | if (n < 2) value = ""; |
michael@0 | 8508 | for (priority in name) this.style(priority, name[priority], value); |
michael@0 | 8509 | return this; |
michael@0 | 8510 | } |
michael@0 | 8511 | priority = ""; |
michael@0 | 8512 | } |
michael@0 | 8513 | function styleNull() { |
michael@0 | 8514 | this.style.removeProperty(name); |
michael@0 | 8515 | } |
michael@0 | 8516 | function styleString(b) { |
michael@0 | 8517 | return b == null ? styleNull : (b += "", function() { |
michael@0 | 8518 | var a = d3_window.getComputedStyle(this, null).getPropertyValue(name), i; |
michael@0 | 8519 | return a !== b && (i = d3_interpolate(a, b), function(t) { |
michael@0 | 8520 | this.style.setProperty(name, i(t), priority); |
michael@0 | 8521 | }); |
michael@0 | 8522 | }); |
michael@0 | 8523 | } |
michael@0 | 8524 | return d3_transition_tween(this, "style." + name, value, styleString); |
michael@0 | 8525 | }; |
michael@0 | 8526 | d3_transitionPrototype.styleTween = function(name, tween, priority) { |
michael@0 | 8527 | if (arguments.length < 3) priority = ""; |
michael@0 | 8528 | function styleTween(d, i) { |
michael@0 | 8529 | var f = tween.call(this, d, i, d3_window.getComputedStyle(this, null).getPropertyValue(name)); |
michael@0 | 8530 | return f && function(t) { |
michael@0 | 8531 | this.style.setProperty(name, f(t), priority); |
michael@0 | 8532 | }; |
michael@0 | 8533 | } |
michael@0 | 8534 | return this.tween("style." + name, styleTween); |
michael@0 | 8535 | }; |
michael@0 | 8536 | d3_transitionPrototype.text = function(value) { |
michael@0 | 8537 | return d3_transition_tween(this, "text", value, d3_transition_text); |
michael@0 | 8538 | }; |
michael@0 | 8539 | function d3_transition_text(b) { |
michael@0 | 8540 | if (b == null) b = ""; |
michael@0 | 8541 | return function() { |
michael@0 | 8542 | this.textContent = b; |
michael@0 | 8543 | }; |
michael@0 | 8544 | } |
michael@0 | 8545 | d3_transitionPrototype.remove = function() { |
michael@0 | 8546 | return this.each("end.transition", function() { |
michael@0 | 8547 | var p; |
michael@0 | 8548 | if (this.__transition__.count < 2 && (p = this.parentNode)) p.removeChild(this); |
michael@0 | 8549 | }); |
michael@0 | 8550 | }; |
michael@0 | 8551 | d3_transitionPrototype.ease = function(value) { |
michael@0 | 8552 | var id = this.id; |
michael@0 | 8553 | if (arguments.length < 1) return this.node().__transition__[id].ease; |
michael@0 | 8554 | if (typeof value !== "function") value = d3.ease.apply(d3, arguments); |
michael@0 | 8555 | return d3_selection_each(this, function(node) { |
michael@0 | 8556 | node.__transition__[id].ease = value; |
michael@0 | 8557 | }); |
michael@0 | 8558 | }; |
michael@0 | 8559 | d3_transitionPrototype.delay = function(value) { |
michael@0 | 8560 | var id = this.id; |
michael@0 | 8561 | return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { |
michael@0 | 8562 | node.__transition__[id].delay = +value.call(node, node.__data__, i, j); |
michael@0 | 8563 | } : (value = +value, function(node) { |
michael@0 | 8564 | node.__transition__[id].delay = value; |
michael@0 | 8565 | })); |
michael@0 | 8566 | }; |
michael@0 | 8567 | d3_transitionPrototype.duration = function(value) { |
michael@0 | 8568 | var id = this.id; |
michael@0 | 8569 | return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { |
michael@0 | 8570 | node.__transition__[id].duration = Math.max(1, value.call(node, node.__data__, i, j)); |
michael@0 | 8571 | } : (value = Math.max(1, value), function(node) { |
michael@0 | 8572 | node.__transition__[id].duration = value; |
michael@0 | 8573 | })); |
michael@0 | 8574 | }; |
michael@0 | 8575 | d3_transitionPrototype.each = function(type, listener) { |
michael@0 | 8576 | var id = this.id; |
michael@0 | 8577 | if (arguments.length < 2) { |
michael@0 | 8578 | var inherit = d3_transitionInherit, inheritId = d3_transitionInheritId; |
michael@0 | 8579 | d3_transitionInheritId = id; |
michael@0 | 8580 | d3_selection_each(this, function(node, i, j) { |
michael@0 | 8581 | d3_transitionInherit = node.__transition__[id]; |
michael@0 | 8582 | type.call(node, node.__data__, i, j); |
michael@0 | 8583 | }); |
michael@0 | 8584 | d3_transitionInherit = inherit; |
michael@0 | 8585 | d3_transitionInheritId = inheritId; |
michael@0 | 8586 | } else { |
michael@0 | 8587 | d3_selection_each(this, function(node) { |
michael@0 | 8588 | var transition = node.__transition__[id]; |
michael@0 | 8589 | (transition.event || (transition.event = d3.dispatch("start", "end"))).on(type, listener); |
michael@0 | 8590 | }); |
michael@0 | 8591 | } |
michael@0 | 8592 | return this; |
michael@0 | 8593 | }; |
michael@0 | 8594 | d3_transitionPrototype.transition = function() { |
michael@0 | 8595 | var id0 = this.id, id1 = ++d3_transitionId, subgroups = [], subgroup, group, node, transition; |
michael@0 | 8596 | for (var j = 0, m = this.length; j < m; j++) { |
michael@0 | 8597 | subgroups.push(subgroup = []); |
michael@0 | 8598 | for (var group = this[j], i = 0, n = group.length; i < n; i++) { |
michael@0 | 8599 | if (node = group[i]) { |
michael@0 | 8600 | transition = Object.create(node.__transition__[id0]); |
michael@0 | 8601 | transition.delay += transition.duration; |
michael@0 | 8602 | d3_transitionNode(node, i, id1, transition); |
michael@0 | 8603 | } |
michael@0 | 8604 | subgroup.push(node); |
michael@0 | 8605 | } |
michael@0 | 8606 | } |
michael@0 | 8607 | return d3_transition(subgroups, id1); |
michael@0 | 8608 | }; |
michael@0 | 8609 | function d3_transitionNode(node, i, id, inherit) { |
michael@0 | 8610 | var lock = node.__transition__ || (node.__transition__ = { |
michael@0 | 8611 | active: 0, |
michael@0 | 8612 | count: 0 |
michael@0 | 8613 | }), transition = lock[id]; |
michael@0 | 8614 | if (!transition) { |
michael@0 | 8615 | var time = inherit.time; |
michael@0 | 8616 | transition = lock[id] = { |
michael@0 | 8617 | tween: new d3_Map(), |
michael@0 | 8618 | time: time, |
michael@0 | 8619 | ease: inherit.ease, |
michael@0 | 8620 | delay: inherit.delay, |
michael@0 | 8621 | duration: inherit.duration |
michael@0 | 8622 | }; |
michael@0 | 8623 | ++lock.count; |
michael@0 | 8624 | d3.timer(function(elapsed) { |
michael@0 | 8625 | var d = node.__data__, ease = transition.ease, delay = transition.delay, duration = transition.duration, timer = d3_timer_active, tweened = []; |
michael@0 | 8626 | timer.t = delay + time; |
michael@0 | 8627 | if (delay <= elapsed) return start(elapsed - delay); |
michael@0 | 8628 | timer.c = start; |
michael@0 | 8629 | function start(elapsed) { |
michael@0 | 8630 | if (lock.active > id) return stop(); |
michael@0 | 8631 | lock.active = id; |
michael@0 | 8632 | transition.event && transition.event.start.call(node, d, i); |
michael@0 | 8633 | transition.tween.forEach(function(key, value) { |
michael@0 | 8634 | if (value = value.call(node, d, i)) { |
michael@0 | 8635 | tweened.push(value); |
michael@0 | 8636 | } |
michael@0 | 8637 | }); |
michael@0 | 8638 | d3.timer(function() { |
michael@0 | 8639 | timer.c = tick(elapsed || 1) ? d3_true : tick; |
michael@0 | 8640 | return 1; |
michael@0 | 8641 | }, 0, time); |
michael@0 | 8642 | } |
michael@0 | 8643 | function tick(elapsed) { |
michael@0 | 8644 | if (lock.active !== id) return stop(); |
michael@0 | 8645 | var t = elapsed / duration, e = ease(t), n = tweened.length; |
michael@0 | 8646 | while (n > 0) { |
michael@0 | 8647 | tweened[--n].call(node, e); |
michael@0 | 8648 | } |
michael@0 | 8649 | if (t >= 1) { |
michael@0 | 8650 | transition.event && transition.event.end.call(node, d, i); |
michael@0 | 8651 | return stop(); |
michael@0 | 8652 | } |
michael@0 | 8653 | } |
michael@0 | 8654 | function stop() { |
michael@0 | 8655 | if (--lock.count) delete lock[id]; else delete node.__transition__; |
michael@0 | 8656 | return 1; |
michael@0 | 8657 | } |
michael@0 | 8658 | }, 0, time); |
michael@0 | 8659 | } |
michael@0 | 8660 | } |
michael@0 | 8661 | d3.svg.axis = function() { |
michael@0 | 8662 | var scale = d3.scale.linear(), orient = d3_svg_axisDefaultOrient, innerTickSize = 6, outerTickSize = 6, tickPadding = 3, tickArguments_ = [ 10 ], tickValues = null, tickFormat_; |
michael@0 | 8663 | function axis(g) { |
michael@0 | 8664 | g.each(function() { |
michael@0 | 8665 | var g = d3.select(this); |
michael@0 | 8666 | var scale0 = this.__chart__ || scale, scale1 = this.__chart__ = scale.copy(); |
michael@0 | 8667 | var ticks = tickValues == null ? scale1.ticks ? scale1.ticks.apply(scale1, tickArguments_) : scale1.domain() : tickValues, tickFormat = tickFormat_ == null ? scale1.tickFormat ? scale1.tickFormat.apply(scale1, tickArguments_) : d3_identity : tickFormat_, tick = g.selectAll(".tick").data(ticks, scale1), tickEnter = tick.enter().insert("g", ".domain").attr("class", "tick").style("opacity", ε), tickExit = d3.transition(tick.exit()).style("opacity", ε).remove(), tickUpdate = d3.transition(tick).style("opacity", 1), tickTransform; |
michael@0 | 8668 | var range = d3_scaleRange(scale1), path = g.selectAll(".domain").data([ 0 ]), pathUpdate = (path.enter().append("path").attr("class", "domain"), |
michael@0 | 8669 | d3.transition(path)); |
michael@0 | 8670 | tickEnter.append("line"); |
michael@0 | 8671 | tickEnter.append("text"); |
michael@0 | 8672 | var lineEnter = tickEnter.select("line"), lineUpdate = tickUpdate.select("line"), text = tick.select("text").text(tickFormat), textEnter = tickEnter.select("text"), textUpdate = tickUpdate.select("text"); |
michael@0 | 8673 | switch (orient) { |
michael@0 | 8674 | case "bottom": |
michael@0 | 8675 | { |
michael@0 | 8676 | tickTransform = d3_svg_axisX; |
michael@0 | 8677 | lineEnter.attr("y2", innerTickSize); |
michael@0 | 8678 | textEnter.attr("y", Math.max(innerTickSize, 0) + tickPadding); |
michael@0 | 8679 | lineUpdate.attr("x2", 0).attr("y2", innerTickSize); |
michael@0 | 8680 | textUpdate.attr("x", 0).attr("y", Math.max(innerTickSize, 0) + tickPadding); |
michael@0 | 8681 | text.attr("dy", ".71em").style("text-anchor", "middle"); |
michael@0 | 8682 | pathUpdate.attr("d", "M" + range[0] + "," + outerTickSize + "V0H" + range[1] + "V" + outerTickSize); |
michael@0 | 8683 | break; |
michael@0 | 8684 | } |
michael@0 | 8685 | |
michael@0 | 8686 | case "top": |
michael@0 | 8687 | { |
michael@0 | 8688 | tickTransform = d3_svg_axisX; |
michael@0 | 8689 | lineEnter.attr("y2", -innerTickSize); |
michael@0 | 8690 | textEnter.attr("y", -(Math.max(innerTickSize, 0) + tickPadding)); |
michael@0 | 8691 | lineUpdate.attr("x2", 0).attr("y2", -innerTickSize); |
michael@0 | 8692 | textUpdate.attr("x", 0).attr("y", -(Math.max(innerTickSize, 0) + tickPadding)); |
michael@0 | 8693 | text.attr("dy", "0em").style("text-anchor", "middle"); |
michael@0 | 8694 | pathUpdate.attr("d", "M" + range[0] + "," + -outerTickSize + "V0H" + range[1] + "V" + -outerTickSize); |
michael@0 | 8695 | break; |
michael@0 | 8696 | } |
michael@0 | 8697 | |
michael@0 | 8698 | case "left": |
michael@0 | 8699 | { |
michael@0 | 8700 | tickTransform = d3_svg_axisY; |
michael@0 | 8701 | lineEnter.attr("x2", -innerTickSize); |
michael@0 | 8702 | textEnter.attr("x", -(Math.max(innerTickSize, 0) + tickPadding)); |
michael@0 | 8703 | lineUpdate.attr("x2", -innerTickSize).attr("y2", 0); |
michael@0 | 8704 | textUpdate.attr("x", -(Math.max(innerTickSize, 0) + tickPadding)).attr("y", 0); |
michael@0 | 8705 | text.attr("dy", ".32em").style("text-anchor", "end"); |
michael@0 | 8706 | pathUpdate.attr("d", "M" + -outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + -outerTickSize); |
michael@0 | 8707 | break; |
michael@0 | 8708 | } |
michael@0 | 8709 | |
michael@0 | 8710 | case "right": |
michael@0 | 8711 | { |
michael@0 | 8712 | tickTransform = d3_svg_axisY; |
michael@0 | 8713 | lineEnter.attr("x2", innerTickSize); |
michael@0 | 8714 | textEnter.attr("x", Math.max(innerTickSize, 0) + tickPadding); |
michael@0 | 8715 | lineUpdate.attr("x2", innerTickSize).attr("y2", 0); |
michael@0 | 8716 | textUpdate.attr("x", Math.max(innerTickSize, 0) + tickPadding).attr("y", 0); |
michael@0 | 8717 | text.attr("dy", ".32em").style("text-anchor", "start"); |
michael@0 | 8718 | pathUpdate.attr("d", "M" + outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + outerTickSize); |
michael@0 | 8719 | break; |
michael@0 | 8720 | } |
michael@0 | 8721 | } |
michael@0 | 8722 | if (scale1.rangeBand) { |
michael@0 | 8723 | var x = scale1, dx = x.rangeBand() / 2; |
michael@0 | 8724 | scale0 = scale1 = function(d) { |
michael@0 | 8725 | return x(d) + dx; |
michael@0 | 8726 | }; |
michael@0 | 8727 | } else if (scale0.rangeBand) { |
michael@0 | 8728 | scale0 = scale1; |
michael@0 | 8729 | } else { |
michael@0 | 8730 | tickExit.call(tickTransform, scale1); |
michael@0 | 8731 | } |
michael@0 | 8732 | tickEnter.call(tickTransform, scale0); |
michael@0 | 8733 | tickUpdate.call(tickTransform, scale1); |
michael@0 | 8734 | }); |
michael@0 | 8735 | } |
michael@0 | 8736 | axis.scale = function(x) { |
michael@0 | 8737 | if (!arguments.length) return scale; |
michael@0 | 8738 | scale = x; |
michael@0 | 8739 | return axis; |
michael@0 | 8740 | }; |
michael@0 | 8741 | axis.orient = function(x) { |
michael@0 | 8742 | if (!arguments.length) return orient; |
michael@0 | 8743 | orient = x in d3_svg_axisOrients ? x + "" : d3_svg_axisDefaultOrient; |
michael@0 | 8744 | return axis; |
michael@0 | 8745 | }; |
michael@0 | 8746 | axis.ticks = function() { |
michael@0 | 8747 | if (!arguments.length) return tickArguments_; |
michael@0 | 8748 | tickArguments_ = arguments; |
michael@0 | 8749 | return axis; |
michael@0 | 8750 | }; |
michael@0 | 8751 | axis.tickValues = function(x) { |
michael@0 | 8752 | if (!arguments.length) return tickValues; |
michael@0 | 8753 | tickValues = x; |
michael@0 | 8754 | return axis; |
michael@0 | 8755 | }; |
michael@0 | 8756 | axis.tickFormat = function(x) { |
michael@0 | 8757 | if (!arguments.length) return tickFormat_; |
michael@0 | 8758 | tickFormat_ = x; |
michael@0 | 8759 | return axis; |
michael@0 | 8760 | }; |
michael@0 | 8761 | axis.tickSize = function(x) { |
michael@0 | 8762 | var n = arguments.length; |
michael@0 | 8763 | if (!n) return innerTickSize; |
michael@0 | 8764 | innerTickSize = +x; |
michael@0 | 8765 | outerTickSize = +arguments[n - 1]; |
michael@0 | 8766 | return axis; |
michael@0 | 8767 | }; |
michael@0 | 8768 | axis.innerTickSize = function(x) { |
michael@0 | 8769 | if (!arguments.length) return innerTickSize; |
michael@0 | 8770 | innerTickSize = +x; |
michael@0 | 8771 | return axis; |
michael@0 | 8772 | }; |
michael@0 | 8773 | axis.outerTickSize = function(x) { |
michael@0 | 8774 | if (!arguments.length) return outerTickSize; |
michael@0 | 8775 | outerTickSize = +x; |
michael@0 | 8776 | return axis; |
michael@0 | 8777 | }; |
michael@0 | 8778 | axis.tickPadding = function(x) { |
michael@0 | 8779 | if (!arguments.length) return tickPadding; |
michael@0 | 8780 | tickPadding = +x; |
michael@0 | 8781 | return axis; |
michael@0 | 8782 | }; |
michael@0 | 8783 | axis.tickSubdivide = function() { |
michael@0 | 8784 | return arguments.length && axis; |
michael@0 | 8785 | }; |
michael@0 | 8786 | return axis; |
michael@0 | 8787 | }; |
michael@0 | 8788 | var d3_svg_axisDefaultOrient = "bottom", d3_svg_axisOrients = { |
michael@0 | 8789 | top: 1, |
michael@0 | 8790 | right: 1, |
michael@0 | 8791 | bottom: 1, |
michael@0 | 8792 | left: 1 |
michael@0 | 8793 | }; |
michael@0 | 8794 | function d3_svg_axisX(selection, x) { |
michael@0 | 8795 | selection.attr("transform", function(d) { |
michael@0 | 8796 | return "translate(" + x(d) + ",0)"; |
michael@0 | 8797 | }); |
michael@0 | 8798 | } |
michael@0 | 8799 | function d3_svg_axisY(selection, y) { |
michael@0 | 8800 | selection.attr("transform", function(d) { |
michael@0 | 8801 | return "translate(0," + y(d) + ")"; |
michael@0 | 8802 | }); |
michael@0 | 8803 | } |
michael@0 | 8804 | d3.svg.brush = function() { |
michael@0 | 8805 | var event = d3_eventDispatch(brush, "brushstart", "brush", "brushend"), x = null, y = null, xExtent = [ 0, 0 ], yExtent = [ 0, 0 ], xExtentDomain, yExtentDomain, xClamp = true, yClamp = true, resizes = d3_svg_brushResizes[0]; |
michael@0 | 8806 | function brush(g) { |
michael@0 | 8807 | g.each(function() { |
michael@0 | 8808 | var g = d3.select(this).style("pointer-events", "all").style("-webkit-tap-highlight-color", "rgba(0,0,0,0)").on("mousedown.brush", brushstart).on("touchstart.brush", brushstart); |
michael@0 | 8809 | var background = g.selectAll(".background").data([ 0 ]); |
michael@0 | 8810 | background.enter().append("rect").attr("class", "background").style("visibility", "hidden").style("cursor", "crosshair"); |
michael@0 | 8811 | g.selectAll(".extent").data([ 0 ]).enter().append("rect").attr("class", "extent").style("cursor", "move"); |
michael@0 | 8812 | var resize = g.selectAll(".resize").data(resizes, d3_identity); |
michael@0 | 8813 | resize.exit().remove(); |
michael@0 | 8814 | resize.enter().append("g").attr("class", function(d) { |
michael@0 | 8815 | return "resize " + d; |
michael@0 | 8816 | }).style("cursor", function(d) { |
michael@0 | 8817 | return d3_svg_brushCursor[d]; |
michael@0 | 8818 | }).append("rect").attr("x", function(d) { |
michael@0 | 8819 | return /[ew]$/.test(d) ? -3 : null; |
michael@0 | 8820 | }).attr("y", function(d) { |
michael@0 | 8821 | return /^[ns]/.test(d) ? -3 : null; |
michael@0 | 8822 | }).attr("width", 6).attr("height", 6).style("visibility", "hidden"); |
michael@0 | 8823 | resize.style("display", brush.empty() ? "none" : null); |
michael@0 | 8824 | var gUpdate = d3.transition(g), backgroundUpdate = d3.transition(background), range; |
michael@0 | 8825 | if (x) { |
michael@0 | 8826 | range = d3_scaleRange(x); |
michael@0 | 8827 | backgroundUpdate.attr("x", range[0]).attr("width", range[1] - range[0]); |
michael@0 | 8828 | redrawX(gUpdate); |
michael@0 | 8829 | } |
michael@0 | 8830 | if (y) { |
michael@0 | 8831 | range = d3_scaleRange(y); |
michael@0 | 8832 | backgroundUpdate.attr("y", range[0]).attr("height", range[1] - range[0]); |
michael@0 | 8833 | redrawY(gUpdate); |
michael@0 | 8834 | } |
michael@0 | 8835 | redraw(gUpdate); |
michael@0 | 8836 | }); |
michael@0 | 8837 | } |
michael@0 | 8838 | brush.event = function(g) { |
michael@0 | 8839 | g.each(function() { |
michael@0 | 8840 | var event_ = event.of(this, arguments), extent1 = { |
michael@0 | 8841 | x: xExtent, |
michael@0 | 8842 | y: yExtent, |
michael@0 | 8843 | i: xExtentDomain, |
michael@0 | 8844 | j: yExtentDomain |
michael@0 | 8845 | }, extent0 = this.__chart__ || extent1; |
michael@0 | 8846 | this.__chart__ = extent1; |
michael@0 | 8847 | if (d3_transitionInheritId) { |
michael@0 | 8848 | d3.select(this).transition().each("start.brush", function() { |
michael@0 | 8849 | xExtentDomain = extent0.i; |
michael@0 | 8850 | yExtentDomain = extent0.j; |
michael@0 | 8851 | xExtent = extent0.x; |
michael@0 | 8852 | yExtent = extent0.y; |
michael@0 | 8853 | event_({ |
michael@0 | 8854 | type: "brushstart" |
michael@0 | 8855 | }); |
michael@0 | 8856 | }).tween("brush:brush", function() { |
michael@0 | 8857 | var xi = d3_interpolateArray(xExtent, extent1.x), yi = d3_interpolateArray(yExtent, extent1.y); |
michael@0 | 8858 | xExtentDomain = yExtentDomain = null; |
michael@0 | 8859 | return function(t) { |
michael@0 | 8860 | xExtent = extent1.x = xi(t); |
michael@0 | 8861 | yExtent = extent1.y = yi(t); |
michael@0 | 8862 | event_({ |
michael@0 | 8863 | type: "brush", |
michael@0 | 8864 | mode: "resize" |
michael@0 | 8865 | }); |
michael@0 | 8866 | }; |
michael@0 | 8867 | }).each("end.brush", function() { |
michael@0 | 8868 | xExtentDomain = extent1.i; |
michael@0 | 8869 | yExtentDomain = extent1.j; |
michael@0 | 8870 | event_({ |
michael@0 | 8871 | type: "brush", |
michael@0 | 8872 | mode: "resize" |
michael@0 | 8873 | }); |
michael@0 | 8874 | event_({ |
michael@0 | 8875 | type: "brushend" |
michael@0 | 8876 | }); |
michael@0 | 8877 | }); |
michael@0 | 8878 | } else { |
michael@0 | 8879 | event_({ |
michael@0 | 8880 | type: "brushstart" |
michael@0 | 8881 | }); |
michael@0 | 8882 | event_({ |
michael@0 | 8883 | type: "brush", |
michael@0 | 8884 | mode: "resize" |
michael@0 | 8885 | }); |
michael@0 | 8886 | event_({ |
michael@0 | 8887 | type: "brushend" |
michael@0 | 8888 | }); |
michael@0 | 8889 | } |
michael@0 | 8890 | }); |
michael@0 | 8891 | }; |
michael@0 | 8892 | function redraw(g) { |
michael@0 | 8893 | g.selectAll(".resize").attr("transform", function(d) { |
michael@0 | 8894 | return "translate(" + xExtent[+/e$/.test(d)] + "," + yExtent[+/^s/.test(d)] + ")"; |
michael@0 | 8895 | }); |
michael@0 | 8896 | } |
michael@0 | 8897 | function redrawX(g) { |
michael@0 | 8898 | g.select(".extent").attr("x", xExtent[0]); |
michael@0 | 8899 | g.selectAll(".extent,.n>rect,.s>rect").attr("width", xExtent[1] - xExtent[0]); |
michael@0 | 8900 | } |
michael@0 | 8901 | function redrawY(g) { |
michael@0 | 8902 | g.select(".extent").attr("y", yExtent[0]); |
michael@0 | 8903 | g.selectAll(".extent,.e>rect,.w>rect").attr("height", yExtent[1] - yExtent[0]); |
michael@0 | 8904 | } |
michael@0 | 8905 | function brushstart() { |
michael@0 | 8906 | var target = this, eventTarget = d3.select(d3.event.target), event_ = event.of(target, arguments), g = d3.select(target), resizing = eventTarget.datum(), resizingX = !/^(n|s)$/.test(resizing) && x, resizingY = !/^(e|w)$/.test(resizing) && y, dragging = eventTarget.classed("extent"), dragRestore = d3_event_dragSuppress(), center, origin = d3.mouse(target), offset; |
michael@0 | 8907 | var w = d3.select(d3_window).on("keydown.brush", keydown).on("keyup.brush", keyup); |
michael@0 | 8908 | if (d3.event.changedTouches) { |
michael@0 | 8909 | w.on("touchmove.brush", brushmove).on("touchend.brush", brushend); |
michael@0 | 8910 | } else { |
michael@0 | 8911 | w.on("mousemove.brush", brushmove).on("mouseup.brush", brushend); |
michael@0 | 8912 | } |
michael@0 | 8913 | g.interrupt().selectAll("*").interrupt(); |
michael@0 | 8914 | if (dragging) { |
michael@0 | 8915 | origin[0] = xExtent[0] - origin[0]; |
michael@0 | 8916 | origin[1] = yExtent[0] - origin[1]; |
michael@0 | 8917 | } else if (resizing) { |
michael@0 | 8918 | var ex = +/w$/.test(resizing), ey = +/^n/.test(resizing); |
michael@0 | 8919 | offset = [ xExtent[1 - ex] - origin[0], yExtent[1 - ey] - origin[1] ]; |
michael@0 | 8920 | origin[0] = xExtent[ex]; |
michael@0 | 8921 | origin[1] = yExtent[ey]; |
michael@0 | 8922 | } else if (d3.event.altKey) center = origin.slice(); |
michael@0 | 8923 | g.style("pointer-events", "none").selectAll(".resize").style("display", null); |
michael@0 | 8924 | d3.select("body").style("cursor", eventTarget.style("cursor")); |
michael@0 | 8925 | event_({ |
michael@0 | 8926 | type: "brushstart" |
michael@0 | 8927 | }); |
michael@0 | 8928 | brushmove(); |
michael@0 | 8929 | function keydown() { |
michael@0 | 8930 | if (d3.event.keyCode == 32) { |
michael@0 | 8931 | if (!dragging) { |
michael@0 | 8932 | center = null; |
michael@0 | 8933 | origin[0] -= xExtent[1]; |
michael@0 | 8934 | origin[1] -= yExtent[1]; |
michael@0 | 8935 | dragging = 2; |
michael@0 | 8936 | } |
michael@0 | 8937 | d3_eventPreventDefault(); |
michael@0 | 8938 | } |
michael@0 | 8939 | } |
michael@0 | 8940 | function keyup() { |
michael@0 | 8941 | if (d3.event.keyCode == 32 && dragging == 2) { |
michael@0 | 8942 | origin[0] += xExtent[1]; |
michael@0 | 8943 | origin[1] += yExtent[1]; |
michael@0 | 8944 | dragging = 0; |
michael@0 | 8945 | d3_eventPreventDefault(); |
michael@0 | 8946 | } |
michael@0 | 8947 | } |
michael@0 | 8948 | function brushmove() { |
michael@0 | 8949 | var point = d3.mouse(target), moved = false; |
michael@0 | 8950 | if (offset) { |
michael@0 | 8951 | point[0] += offset[0]; |
michael@0 | 8952 | point[1] += offset[1]; |
michael@0 | 8953 | } |
michael@0 | 8954 | if (!dragging) { |
michael@0 | 8955 | if (d3.event.altKey) { |
michael@0 | 8956 | if (!center) center = [ (xExtent[0] + xExtent[1]) / 2, (yExtent[0] + yExtent[1]) / 2 ]; |
michael@0 | 8957 | origin[0] = xExtent[+(point[0] < center[0])]; |
michael@0 | 8958 | origin[1] = yExtent[+(point[1] < center[1])]; |
michael@0 | 8959 | } else center = null; |
michael@0 | 8960 | } |
michael@0 | 8961 | if (resizingX && move1(point, x, 0)) { |
michael@0 | 8962 | redrawX(g); |
michael@0 | 8963 | moved = true; |
michael@0 | 8964 | } |
michael@0 | 8965 | if (resizingY && move1(point, y, 1)) { |
michael@0 | 8966 | redrawY(g); |
michael@0 | 8967 | moved = true; |
michael@0 | 8968 | } |
michael@0 | 8969 | if (moved) { |
michael@0 | 8970 | redraw(g); |
michael@0 | 8971 | event_({ |
michael@0 | 8972 | type: "brush", |
michael@0 | 8973 | mode: dragging ? "move" : "resize" |
michael@0 | 8974 | }); |
michael@0 | 8975 | } |
michael@0 | 8976 | } |
michael@0 | 8977 | function move1(point, scale, i) { |
michael@0 | 8978 | var range = d3_scaleRange(scale), r0 = range[0], r1 = range[1], position = origin[i], extent = i ? yExtent : xExtent, size = extent[1] - extent[0], min, max; |
michael@0 | 8979 | if (dragging) { |
michael@0 | 8980 | r0 -= position; |
michael@0 | 8981 | r1 -= size + position; |
michael@0 | 8982 | } |
michael@0 | 8983 | min = (i ? yClamp : xClamp) ? Math.max(r0, Math.min(r1, point[i])) : point[i]; |
michael@0 | 8984 | if (dragging) { |
michael@0 | 8985 | max = (min += position) + size; |
michael@0 | 8986 | } else { |
michael@0 | 8987 | if (center) position = Math.max(r0, Math.min(r1, 2 * center[i] - min)); |
michael@0 | 8988 | if (position < min) { |
michael@0 | 8989 | max = min; |
michael@0 | 8990 | min = position; |
michael@0 | 8991 | } else { |
michael@0 | 8992 | max = position; |
michael@0 | 8993 | } |
michael@0 | 8994 | } |
michael@0 | 8995 | if (extent[0] != min || extent[1] != max) { |
michael@0 | 8996 | if (i) yExtentDomain = null; else xExtentDomain = null; |
michael@0 | 8997 | extent[0] = min; |
michael@0 | 8998 | extent[1] = max; |
michael@0 | 8999 | return true; |
michael@0 | 9000 | } |
michael@0 | 9001 | } |
michael@0 | 9002 | function brushend() { |
michael@0 | 9003 | brushmove(); |
michael@0 | 9004 | g.style("pointer-events", "all").selectAll(".resize").style("display", brush.empty() ? "none" : null); |
michael@0 | 9005 | d3.select("body").style("cursor", null); |
michael@0 | 9006 | w.on("mousemove.brush", null).on("mouseup.brush", null).on("touchmove.brush", null).on("touchend.brush", null).on("keydown.brush", null).on("keyup.brush", null); |
michael@0 | 9007 | dragRestore(); |
michael@0 | 9008 | event_({ |
michael@0 | 9009 | type: "brushend" |
michael@0 | 9010 | }); |
michael@0 | 9011 | } |
michael@0 | 9012 | } |
michael@0 | 9013 | brush.x = function(z) { |
michael@0 | 9014 | if (!arguments.length) return x; |
michael@0 | 9015 | x = z; |
michael@0 | 9016 | resizes = d3_svg_brushResizes[!x << 1 | !y]; |
michael@0 | 9017 | return brush; |
michael@0 | 9018 | }; |
michael@0 | 9019 | brush.y = function(z) { |
michael@0 | 9020 | if (!arguments.length) return y; |
michael@0 | 9021 | y = z; |
michael@0 | 9022 | resizes = d3_svg_brushResizes[!x << 1 | !y]; |
michael@0 | 9023 | return brush; |
michael@0 | 9024 | }; |
michael@0 | 9025 | brush.clamp = function(z) { |
michael@0 | 9026 | if (!arguments.length) return x && y ? [ xClamp, yClamp ] : x ? xClamp : y ? yClamp : null; |
michael@0 | 9027 | if (x && y) xClamp = !!z[0], yClamp = !!z[1]; else if (x) xClamp = !!z; else if (y) yClamp = !!z; |
michael@0 | 9028 | return brush; |
michael@0 | 9029 | }; |
michael@0 | 9030 | brush.extent = function(z) { |
michael@0 | 9031 | var x0, x1, y0, y1, t; |
michael@0 | 9032 | if (!arguments.length) { |
michael@0 | 9033 | if (x) { |
michael@0 | 9034 | if (xExtentDomain) { |
michael@0 | 9035 | x0 = xExtentDomain[0], x1 = xExtentDomain[1]; |
michael@0 | 9036 | } else { |
michael@0 | 9037 | x0 = xExtent[0], x1 = xExtent[1]; |
michael@0 | 9038 | if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1); |
michael@0 | 9039 | if (x1 < x0) t = x0, x0 = x1, x1 = t; |
michael@0 | 9040 | } |
michael@0 | 9041 | } |
michael@0 | 9042 | if (y) { |
michael@0 | 9043 | if (yExtentDomain) { |
michael@0 | 9044 | y0 = yExtentDomain[0], y1 = yExtentDomain[1]; |
michael@0 | 9045 | } else { |
michael@0 | 9046 | y0 = yExtent[0], y1 = yExtent[1]; |
michael@0 | 9047 | if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1); |
michael@0 | 9048 | if (y1 < y0) t = y0, y0 = y1, y1 = t; |
michael@0 | 9049 | } |
michael@0 | 9050 | } |
michael@0 | 9051 | return x && y ? [ [ x0, y0 ], [ x1, y1 ] ] : x ? [ x0, x1 ] : y && [ y0, y1 ]; |
michael@0 | 9052 | } |
michael@0 | 9053 | if (x) { |
michael@0 | 9054 | x0 = z[0], x1 = z[1]; |
michael@0 | 9055 | if (y) x0 = x0[0], x1 = x1[0]; |
michael@0 | 9056 | xExtentDomain = [ x0, x1 ]; |
michael@0 | 9057 | if (x.invert) x0 = x(x0), x1 = x(x1); |
michael@0 | 9058 | if (x1 < x0) t = x0, x0 = x1, x1 = t; |
michael@0 | 9059 | if (x0 != xExtent[0] || x1 != xExtent[1]) xExtent = [ x0, x1 ]; |
michael@0 | 9060 | } |
michael@0 | 9061 | if (y) { |
michael@0 | 9062 | y0 = z[0], y1 = z[1]; |
michael@0 | 9063 | if (x) y0 = y0[1], y1 = y1[1]; |
michael@0 | 9064 | yExtentDomain = [ y0, y1 ]; |
michael@0 | 9065 | if (y.invert) y0 = y(y0), y1 = y(y1); |
michael@0 | 9066 | if (y1 < y0) t = y0, y0 = y1, y1 = t; |
michael@0 | 9067 | if (y0 != yExtent[0] || y1 != yExtent[1]) yExtent = [ y0, y1 ]; |
michael@0 | 9068 | } |
michael@0 | 9069 | return brush; |
michael@0 | 9070 | }; |
michael@0 | 9071 | brush.clear = function() { |
michael@0 | 9072 | if (!brush.empty()) { |
michael@0 | 9073 | xExtent = [ 0, 0 ], yExtent = [ 0, 0 ]; |
michael@0 | 9074 | xExtentDomain = yExtentDomain = null; |
michael@0 | 9075 | } |
michael@0 | 9076 | return brush; |
michael@0 | 9077 | }; |
michael@0 | 9078 | brush.empty = function() { |
michael@0 | 9079 | return !!x && xExtent[0] == xExtent[1] || !!y && yExtent[0] == yExtent[1]; |
michael@0 | 9080 | }; |
michael@0 | 9081 | return d3.rebind(brush, event, "on"); |
michael@0 | 9082 | }; |
michael@0 | 9083 | var d3_svg_brushCursor = { |
michael@0 | 9084 | n: "ns-resize", |
michael@0 | 9085 | e: "ew-resize", |
michael@0 | 9086 | s: "ns-resize", |
michael@0 | 9087 | w: "ew-resize", |
michael@0 | 9088 | nw: "nwse-resize", |
michael@0 | 9089 | ne: "nesw-resize", |
michael@0 | 9090 | se: "nwse-resize", |
michael@0 | 9091 | sw: "nesw-resize" |
michael@0 | 9092 | }; |
michael@0 | 9093 | var d3_svg_brushResizes = [ [ "n", "e", "s", "w", "nw", "ne", "se", "sw" ], [ "e", "w" ], [ "n", "s" ], [] ]; |
michael@0 | 9094 | var d3_time_format = d3_time.format = d3_locale_enUS.timeFormat; |
michael@0 | 9095 | var d3_time_formatUtc = d3_time_format.utc; |
michael@0 | 9096 | var d3_time_formatIso = d3_time_formatUtc("%Y-%m-%dT%H:%M:%S.%LZ"); |
michael@0 | 9097 | d3_time_format.iso = Date.prototype.toISOString && +new Date("2000-01-01T00:00:00.000Z") ? d3_time_formatIsoNative : d3_time_formatIso; |
michael@0 | 9098 | function d3_time_formatIsoNative(date) { |
michael@0 | 9099 | return date.toISOString(); |
michael@0 | 9100 | } |
michael@0 | 9101 | d3_time_formatIsoNative.parse = function(string) { |
michael@0 | 9102 | var date = new Date(string); |
michael@0 | 9103 | return isNaN(date) ? null : date; |
michael@0 | 9104 | }; |
michael@0 | 9105 | d3_time_formatIsoNative.toString = d3_time_formatIso.toString; |
michael@0 | 9106 | d3_time.second = d3_time_interval(function(date) { |
michael@0 | 9107 | return new d3_date(Math.floor(date / 1e3) * 1e3); |
michael@0 | 9108 | }, function(date, offset) { |
michael@0 | 9109 | date.setTime(date.getTime() + Math.floor(offset) * 1e3); |
michael@0 | 9110 | }, function(date) { |
michael@0 | 9111 | return date.getSeconds(); |
michael@0 | 9112 | }); |
michael@0 | 9113 | d3_time.seconds = d3_time.second.range; |
michael@0 | 9114 | d3_time.seconds.utc = d3_time.second.utc.range; |
michael@0 | 9115 | d3_time.minute = d3_time_interval(function(date) { |
michael@0 | 9116 | return new d3_date(Math.floor(date / 6e4) * 6e4); |
michael@0 | 9117 | }, function(date, offset) { |
michael@0 | 9118 | date.setTime(date.getTime() + Math.floor(offset) * 6e4); |
michael@0 | 9119 | }, function(date) { |
michael@0 | 9120 | return date.getMinutes(); |
michael@0 | 9121 | }); |
michael@0 | 9122 | d3_time.minutes = d3_time.minute.range; |
michael@0 | 9123 | d3_time.minutes.utc = d3_time.minute.utc.range; |
michael@0 | 9124 | d3_time.hour = d3_time_interval(function(date) { |
michael@0 | 9125 | var timezone = date.getTimezoneOffset() / 60; |
michael@0 | 9126 | return new d3_date((Math.floor(date / 36e5 - timezone) + timezone) * 36e5); |
michael@0 | 9127 | }, function(date, offset) { |
michael@0 | 9128 | date.setTime(date.getTime() + Math.floor(offset) * 36e5); |
michael@0 | 9129 | }, function(date) { |
michael@0 | 9130 | return date.getHours(); |
michael@0 | 9131 | }); |
michael@0 | 9132 | d3_time.hours = d3_time.hour.range; |
michael@0 | 9133 | d3_time.hours.utc = d3_time.hour.utc.range; |
michael@0 | 9134 | d3_time.month = d3_time_interval(function(date) { |
michael@0 | 9135 | date = d3_time.day(date); |
michael@0 | 9136 | date.setDate(1); |
michael@0 | 9137 | return date; |
michael@0 | 9138 | }, function(date, offset) { |
michael@0 | 9139 | date.setMonth(date.getMonth() + offset); |
michael@0 | 9140 | }, function(date) { |
michael@0 | 9141 | return date.getMonth(); |
michael@0 | 9142 | }); |
michael@0 | 9143 | d3_time.months = d3_time.month.range; |
michael@0 | 9144 | d3_time.months.utc = d3_time.month.utc.range; |
michael@0 | 9145 | function d3_time_scale(linear, methods, format) { |
michael@0 | 9146 | function scale(x) { |
michael@0 | 9147 | return linear(x); |
michael@0 | 9148 | } |
michael@0 | 9149 | scale.invert = function(x) { |
michael@0 | 9150 | return d3_time_scaleDate(linear.invert(x)); |
michael@0 | 9151 | }; |
michael@0 | 9152 | scale.domain = function(x) { |
michael@0 | 9153 | if (!arguments.length) return linear.domain().map(d3_time_scaleDate); |
michael@0 | 9154 | linear.domain(x); |
michael@0 | 9155 | return scale; |
michael@0 | 9156 | }; |
michael@0 | 9157 | function tickMethod(extent, count) { |
michael@0 | 9158 | var span = extent[1] - extent[0], target = span / count, i = d3.bisect(d3_time_scaleSteps, target); |
michael@0 | 9159 | return i == d3_time_scaleSteps.length ? [ methods.year, d3_scale_linearTickRange(extent.map(function(d) { |
michael@0 | 9160 | return d / 31536e6; |
michael@0 | 9161 | }), count)[2] ] : !i ? [ d3_time_scaleMilliseconds, d3_scale_linearTickRange(extent, count)[2] ] : methods[target / d3_time_scaleSteps[i - 1] < d3_time_scaleSteps[i] / target ? i - 1 : i]; |
michael@0 | 9162 | } |
michael@0 | 9163 | scale.nice = function(interval, skip) { |
michael@0 | 9164 | var domain = scale.domain(), extent = d3_scaleExtent(domain), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" && tickMethod(extent, interval); |
michael@0 | 9165 | if (method) interval = method[0], skip = method[1]; |
michael@0 | 9166 | function skipped(date) { |
michael@0 | 9167 | return !isNaN(date) && !interval.range(date, d3_time_scaleDate(+date + 1), skip).length; |
michael@0 | 9168 | } |
michael@0 | 9169 | return scale.domain(d3_scale_nice(domain, skip > 1 ? { |
michael@0 | 9170 | floor: function(date) { |
michael@0 | 9171 | while (skipped(date = interval.floor(date))) date = d3_time_scaleDate(date - 1); |
michael@0 | 9172 | return date; |
michael@0 | 9173 | }, |
michael@0 | 9174 | ceil: function(date) { |
michael@0 | 9175 | while (skipped(date = interval.ceil(date))) date = d3_time_scaleDate(+date + 1); |
michael@0 | 9176 | return date; |
michael@0 | 9177 | } |
michael@0 | 9178 | } : interval)); |
michael@0 | 9179 | }; |
michael@0 | 9180 | scale.ticks = function(interval, skip) { |
michael@0 | 9181 | var extent = d3_scaleExtent(scale.domain()), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" ? tickMethod(extent, interval) : !interval.range && [ { |
michael@0 | 9182 | range: interval |
michael@0 | 9183 | }, skip ]; |
michael@0 | 9184 | if (method) interval = method[0], skip = method[1]; |
michael@0 | 9185 | return interval.range(extent[0], d3_time_scaleDate(+extent[1] + 1), skip < 1 ? 1 : skip); |
michael@0 | 9186 | }; |
michael@0 | 9187 | scale.tickFormat = function() { |
michael@0 | 9188 | return format; |
michael@0 | 9189 | }; |
michael@0 | 9190 | scale.copy = function() { |
michael@0 | 9191 | return d3_time_scale(linear.copy(), methods, format); |
michael@0 | 9192 | }; |
michael@0 | 9193 | return d3_scale_linearRebind(scale, linear); |
michael@0 | 9194 | } |
michael@0 | 9195 | function d3_time_scaleDate(t) { |
michael@0 | 9196 | return new Date(t); |
michael@0 | 9197 | } |
michael@0 | 9198 | var d3_time_scaleSteps = [ 1e3, 5e3, 15e3, 3e4, 6e4, 3e5, 9e5, 18e5, 36e5, 108e5, 216e5, 432e5, 864e5, 1728e5, 6048e5, 2592e6, 7776e6, 31536e6 ]; |
michael@0 | 9199 | var d3_time_scaleLocalMethods = [ [ d3_time.second, 1 ], [ d3_time.second, 5 ], [ d3_time.second, 15 ], [ d3_time.second, 30 ], [ d3_time.minute, 1 ], [ d3_time.minute, 5 ], [ d3_time.minute, 15 ], [ d3_time.minute, 30 ], [ d3_time.hour, 1 ], [ d3_time.hour, 3 ], [ d3_time.hour, 6 ], [ d3_time.hour, 12 ], [ d3_time.day, 1 ], [ d3_time.day, 2 ], [ d3_time.week, 1 ], [ d3_time.month, 1 ], [ d3_time.month, 3 ], [ d3_time.year, 1 ] ]; |
michael@0 | 9200 | var d3_time_scaleLocalFormat = d3_time_format.multi([ [ ".%L", function(d) { |
michael@0 | 9201 | return d.getMilliseconds(); |
michael@0 | 9202 | } ], [ ":%S", function(d) { |
michael@0 | 9203 | return d.getSeconds(); |
michael@0 | 9204 | } ], [ "%I:%M", function(d) { |
michael@0 | 9205 | return d.getMinutes(); |
michael@0 | 9206 | } ], [ "%I %p", function(d) { |
michael@0 | 9207 | return d.getHours(); |
michael@0 | 9208 | } ], [ "%a %d", function(d) { |
michael@0 | 9209 | return d.getDay() && d.getDate() != 1; |
michael@0 | 9210 | } ], [ "%b %d", function(d) { |
michael@0 | 9211 | return d.getDate() != 1; |
michael@0 | 9212 | } ], [ "%B", function(d) { |
michael@0 | 9213 | return d.getMonth(); |
michael@0 | 9214 | } ], [ "%Y", d3_true ] ]); |
michael@0 | 9215 | var d3_time_scaleMilliseconds = { |
michael@0 | 9216 | range: function(start, stop, step) { |
michael@0 | 9217 | return d3.range(+start, +stop, step).map(d3_time_scaleDate); |
michael@0 | 9218 | }, |
michael@0 | 9219 | floor: d3_identity, |
michael@0 | 9220 | ceil: d3_identity |
michael@0 | 9221 | }; |
michael@0 | 9222 | d3_time_scaleLocalMethods.year = d3_time.year; |
michael@0 | 9223 | d3_time.scale = function() { |
michael@0 | 9224 | return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat); |
michael@0 | 9225 | }; |
michael@0 | 9226 | var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) { |
michael@0 | 9227 | return [ m[0].utc, m[1] ]; |
michael@0 | 9228 | }); |
michael@0 | 9229 | var d3_time_scaleUtcFormat = d3_time_formatUtc.multi([ [ ".%L", function(d) { |
michael@0 | 9230 | return d.getUTCMilliseconds(); |
michael@0 | 9231 | } ], [ ":%S", function(d) { |
michael@0 | 9232 | return d.getUTCSeconds(); |
michael@0 | 9233 | } ], [ "%I:%M", function(d) { |
michael@0 | 9234 | return d.getUTCMinutes(); |
michael@0 | 9235 | } ], [ "%I %p", function(d) { |
michael@0 | 9236 | return d.getUTCHours(); |
michael@0 | 9237 | } ], [ "%a %d", function(d) { |
michael@0 | 9238 | return d.getUTCDay() && d.getUTCDate() != 1; |
michael@0 | 9239 | } ], [ "%b %d", function(d) { |
michael@0 | 9240 | return d.getUTCDate() != 1; |
michael@0 | 9241 | } ], [ "%B", function(d) { |
michael@0 | 9242 | return d.getUTCMonth(); |
michael@0 | 9243 | } ], [ "%Y", d3_true ] ]); |
michael@0 | 9244 | d3_time_scaleUtcMethods.year = d3_time.year.utc; |
michael@0 | 9245 | d3_time.scale.utc = function() { |
michael@0 | 9246 | return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_time_scaleUtcFormat); |
michael@0 | 9247 | }; |
michael@0 | 9248 | d3.text = d3_xhrType(function(request) { |
michael@0 | 9249 | return request.responseText; |
michael@0 | 9250 | }); |
michael@0 | 9251 | d3.json = function(url, callback) { |
michael@0 | 9252 | return d3_xhr(url, "application/json", d3_json, callback); |
michael@0 | 9253 | }; |
michael@0 | 9254 | function d3_json(request) { |
michael@0 | 9255 | return JSON.parse(request.responseText); |
michael@0 | 9256 | } |
michael@0 | 9257 | d3.html = function(url, callback) { |
michael@0 | 9258 | return d3_xhr(url, "text/html", d3_html, callback); |
michael@0 | 9259 | }; |
michael@0 | 9260 | function d3_html(request) { |
michael@0 | 9261 | var range = d3_document.createRange(); |
michael@0 | 9262 | range.selectNode(d3_document.body); |
michael@0 | 9263 | return range.createContextualFragment(request.responseText); |
michael@0 | 9264 | } |
michael@0 | 9265 | d3.xml = d3_xhrType(function(request) { |
michael@0 | 9266 | return request.responseXML; |
michael@0 | 9267 | }); |
michael@0 | 9268 | if (typeof define === "function" && define.amd) { |
michael@0 | 9269 | define(d3); |
michael@0 | 9270 | } else if (typeof module === "object" && module.exports) { |
michael@0 | 9271 | module.exports = d3; |
michael@0 | 9272 | } else { |
michael@0 | 9273 | this.d3 = d3; |
michael@0 | 9274 | } |
michael@0 | 9275 | }(); |