Commit ab0e73093c2b33b07bc44b2bfa1dff6f67ec2008
- Diff rendering mode:
- inline
- side by side
swtr/static/js/lib/d3.js
(4762 / 0)
  | |||
1 | (function(){if (!Date.now) Date.now = function() { | ||
2 | return +new Date; | ||
3 | }; | ||
4 | try { | ||
5 | document.createElement("div").style.setProperty("opacity", 0, ""); | ||
6 | } catch (error) { | ||
7 | var d3_style_prototype = CSSStyleDeclaration.prototype, | ||
8 | d3_style_setProperty = d3_style_prototype.setProperty; | ||
9 | d3_style_prototype.setProperty = function(name, value, priority) { | ||
10 | d3_style_setProperty.call(this, name, value + "", priority); | ||
11 | }; | ||
12 | } | ||
13 | d3 = {version: "2.7.4"}; // semver | ||
14 | var d3_array = d3_arraySlice; // conversion for NodeLists | ||
15 | |||
16 | function d3_arrayCopy(pseudoarray) { | ||
17 | var i = -1, n = pseudoarray.length, array = []; | ||
18 | while (++i < n) array.push(pseudoarray[i]); | ||
19 | return array; | ||
20 | } | ||
21 | |||
22 | function d3_arraySlice(pseudoarray) { | ||
23 | return Array.prototype.slice.call(pseudoarray); | ||
24 | } | ||
25 | |||
26 | try { | ||
27 | d3_array(document.documentElement.childNodes)[0].nodeType; | ||
28 | } catch(e) { | ||
29 | d3_array = d3_arrayCopy; | ||
30 | } | ||
31 | |||
32 | var d3_arraySubclass = [].__proto__? | ||
33 | |||
34 | // Until ECMAScript supports array subclassing, prototype injection works well. | ||
35 | function(array, prototype) { | ||
36 | array.__proto__ = prototype; | ||
37 | }: | ||
38 | |||
39 | // And if your browser doesn't support __proto__, we'll use direct extension. | ||
40 | function(array, prototype) { | ||
41 | for (var property in prototype) array[property] = prototype[property]; | ||
42 | }; | ||
43 | function d3_this() { | ||
44 | return this; | ||
45 | } | ||
46 | d3.functor = function(v) { | ||
47 | return typeof v === "function" ? v : function() { return v; }; | ||
48 | }; | ||
49 | // Copies a variable number of methods from source to target. | ||
50 | d3.rebind = function(target, source) { | ||
51 | var i = 1, n = arguments.length, method; | ||
52 | while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]); | ||
53 | return target; | ||
54 | }; | ||
55 | |||
56 | // Method is assumed to be a standard D3 getter-setter: | ||
57 | // If passed with no arguments, gets the value. | ||
58 | // If passed with arguments, sets the value and returns the target. | ||
59 | function d3_rebind(target, source, method) { | ||
60 | return function() { | ||
61 | var value = method.apply(source, arguments); | ||
62 | return arguments.length ? target : value; | ||
63 | }; | ||
64 | } | ||
65 | d3.ascending = function(a, b) { | ||
66 | return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN; | ||
67 | }; | ||
68 | d3.descending = function(a, b) { | ||
69 | return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN; | ||
70 | }; | ||
71 | d3.mean = function(array, f) { | ||
72 | var n = array.length, | ||
73 | a, | ||
74 | m = 0, | ||
75 | i = -1, | ||
76 | j = 0; | ||
77 | if (arguments.length === 1) { | ||
78 | while (++i < n) if (d3_number(a = array[i])) m += (a - m) / ++j; | ||
79 | } else { | ||
80 | while (++i < n) if (d3_number(a = f.call(array, array[i], i))) m += (a - m) / ++j; | ||
81 | } | ||
82 | return j ? m : undefined; | ||
83 | }; | ||
84 | d3.median = function(array, f) { | ||
85 | if (arguments.length > 1) array = array.map(f); | ||
86 | array = array.filter(d3_number); | ||
87 | return array.length ? d3.quantile(array.sort(d3.ascending), .5) : undefined; | ||
88 | }; | ||
89 | d3.min = function(array, f) { | ||
90 | var i = -1, | ||
91 | n = array.length, | ||
92 | a, | ||
93 | b; | ||
94 | if (arguments.length === 1) { | ||
95 | while (++i < n && ((a = array[i]) == null || a != a)) a = undefined; | ||
96 | while (++i < n) if ((b = array[i]) != null && a > b) a = b; | ||
97 | } else { | ||
98 | while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined; | ||
99 | while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b; | ||
100 | } | ||
101 | return a; | ||
102 | }; | ||
103 | d3.max = function(array, f) { | ||
104 | var i = -1, | ||
105 | n = array.length, | ||
106 | a, | ||
107 | b; | ||
108 | if (arguments.length === 1) { | ||
109 | while (++i < n && ((a = array[i]) == null || a != a)) a = undefined; | ||
110 | while (++i < n) if ((b = array[i]) != null && b > a) a = b; | ||
111 | } else { | ||
112 | while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined; | ||
113 | while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b; | ||
114 | } | ||
115 | return a; | ||
116 | }; | ||
117 | d3.extent = function(array, f) { | ||
118 | var i = -1, | ||
119 | n = array.length, | ||
120 | a, | ||
121 | b, | ||
122 | c; | ||
123 | if (arguments.length === 1) { | ||
124 | while (++i < n && ((a = c = array[i]) == null || a != a)) a = c = undefined; | ||
125 | while (++i < n) if ((b = array[i]) != null) { | ||
126 | if (a > b) a = b; | ||
127 | if (c < b) c = b; | ||
128 | } | ||
129 | } else { | ||
130 | while (++i < n && ((a = c = f.call(array, array[i], i)) == null || a != a)) a = undefined; | ||
131 | while (++i < n) if ((b = f.call(array, array[i], i)) != null) { | ||
132 | if (a > b) a = b; | ||
133 | if (c < b) c = b; | ||
134 | } | ||
135 | } | ||
136 | return [a, c]; | ||
137 | }; | ||
138 | d3.random = { | ||
139 | normal: function(mean, deviation) { | ||
140 | if (arguments.length < 2) deviation = 1; | ||
141 | if (arguments.length < 1) mean = 0; | ||
142 | return function() { | ||
143 | var x, y, r; | ||
144 | do { | ||
145 | x = Math.random() * 2 - 1; | ||
146 | y = Math.random() * 2 - 1; | ||
147 | r = x * x + y * y; | ||
148 | } while (!r || r > 1); | ||
149 | return mean + deviation * x * Math.sqrt(-2 * Math.log(r) / r); | ||
150 | }; | ||
151 | } | ||
152 | }; | ||
153 | function d3_number(x) { | ||
154 | return x != null && !isNaN(x); | ||
155 | } | ||
156 | d3.sum = function(array, f) { | ||
157 | var s = 0, | ||
158 | n = array.length, | ||
159 | a, | ||
160 | i = -1; | ||
161 | |||
162 | if (arguments.length === 1) { | ||
163 | while (++i < n) if (!isNaN(a = +array[i])) s += a; | ||
164 | } else { | ||
165 | while (++i < n) if (!isNaN(a = +f.call(array, array[i], i))) s += a; | ||
166 | } | ||
167 | |||
168 | return s; | ||
169 | }; | ||
170 | // R-7 per <http://en.wikipedia.org/wiki/Quantile> | ||
171 | d3.quantile = function(values, p) { | ||
172 | var H = (values.length - 1) * p + 1, | ||
173 | h = Math.floor(H), | ||
174 | v = values[h - 1], | ||
175 | e = H - h; | ||
176 | return e ? v + e * (values[h] - v) : v; | ||
177 | }; | ||
178 | d3.transpose = function(matrix) { | ||
179 | return d3.zip.apply(d3, matrix); | ||
180 | }; | ||
181 | d3.zip = function() { | ||
182 | if (!(n = arguments.length)) return []; | ||
183 | for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m;) { | ||
184 | for (var j = -1, n, zip = zips[i] = new Array(n); ++j < n;) { | ||
185 | zip[j] = arguments[j][i]; | ||
186 | } | ||
187 | } | ||
188 | return zips; | ||
189 | }; | ||
190 | |||
191 | function d3_zipLength(d) { | ||
192 | return d.length; | ||
193 | } | ||
194 | // Locate the insertion point for x in a to maintain sorted order. The | ||
195 | // arguments lo and hi may be used to specify a subset of the array which should | ||
196 | // be considered; by default the entire array is used. If x is already present | ||
197 | // in a, the insertion point will be before (to the left of) any existing | ||
198 | // entries. The return value is suitable for use as the first argument to | ||
199 | // `array.splice` assuming that a is already sorted. | ||
200 | // | ||
201 | // The returned insertion point i partitions the array a into two halves so that | ||
202 | // all v < x for v in a[lo:i] for the left side and all v >= x for v in a[i:hi] | ||
203 | // for the right side. | ||
204 | d3.bisectLeft = function(a, x, lo, hi) { | ||
205 | if (arguments.length < 3) lo = 0; | ||
206 | if (arguments.length < 4) hi = a.length; | ||
207 | while (lo < hi) { | ||
208 | var mid = (lo + hi) >> 1; | ||
209 | if (a[mid] < x) lo = mid + 1; | ||
210 | else hi = mid; | ||
211 | } | ||
212 | return lo; | ||
213 | }; | ||
214 | |||
215 | // Similar to bisectLeft, but returns an insertion point which comes after (to | ||
216 | // the right of) any existing entries of x in a. | ||
217 | // | ||
218 | // The returned insertion point i partitions the array into two halves so that | ||
219 | // all v <= x for v in a[lo:i] for the left side and all v > x for v in a[i:hi] | ||
220 | // for the right side. | ||
221 | d3.bisect = | ||
222 | d3.bisectRight = function(a, x, lo, hi) { | ||
223 | if (arguments.length < 3) lo = 0; | ||
224 | if (arguments.length < 4) hi = a.length; | ||
225 | while (lo < hi) { | ||
226 | var mid = (lo + hi) >> 1; | ||
227 | if (x < a[mid]) hi = mid; | ||
228 | else lo = mid + 1; | ||
229 | } | ||
230 | return lo; | ||
231 | }; | ||
232 | d3.first = function(array, f) { | ||
233 | var i = 0, | ||
234 | n = array.length, | ||
235 | a = array[0], | ||
236 | b; | ||
237 | if (arguments.length === 1) f = d3.ascending; | ||
238 | while (++i < n) { | ||
239 | if (f.call(array, a, b = array[i]) > 0) { | ||
240 | a = b; | ||
241 | } | ||
242 | } | ||
243 | return a; | ||
244 | }; | ||
245 | d3.last = function(array, f) { | ||
246 | var i = 0, | ||
247 | n = array.length, | ||
248 | a = array[0], | ||
249 | b; | ||
250 | if (arguments.length === 1) f = d3.ascending; | ||
251 | while (++i < n) { | ||
252 | if (f.call(array, a, b = array[i]) <= 0) { | ||
253 | a = b; | ||
254 | } | ||
255 | } | ||
256 | return a; | ||
257 | }; | ||
258 | d3.nest = function() { | ||
259 | var nest = {}, | ||
260 | keys = [], | ||
261 | sortKeys = [], | ||
262 | sortValues, | ||
263 | rollup; | ||
264 | |||
265 | function map(array, depth) { | ||
266 | if (depth >= keys.length) return rollup | ||
267 | ? rollup.call(nest, array) : (sortValues | ||
268 | ? array.sort(sortValues) | ||
269 | : array); | ||
270 | |||
271 | var i = -1, | ||
272 | n = array.length, | ||
273 | key = keys[depth++], | ||
274 | keyValue, | ||
275 | object, | ||
276 | o = {}; | ||
277 | |||
278 | while (++i < n) { | ||
279 | if ((keyValue = key(object = array[i])) in o) { | ||
280 | o[keyValue].push(object); | ||
281 | } else { | ||
282 | o[keyValue] = [object]; | ||
283 | } | ||
284 | } | ||
285 | |||
286 | for (keyValue in o) { | ||
287 | o[keyValue] = map(o[keyValue], depth); | ||
288 | } | ||
289 | |||
290 | return o; | ||
291 | } | ||
292 | |||
293 | function entries(map, depth) { | ||
294 | if (depth >= keys.length) return map; | ||
295 | |||
296 | var a = [], | ||
297 | sortKey = sortKeys[depth++], | ||
298 | key; | ||
299 | |||
300 | for (key in map) { | ||
301 | a.push({key: key, values: entries(map[key], depth)}); | ||
302 | } | ||
303 | |||
304 | if (sortKey) a.sort(function(a, b) { | ||
305 | return sortKey(a.key, b.key); | ||
306 | }); | ||
307 | |||
308 | return a; | ||
309 | } | ||
310 | |||
311 | nest.map = function(array) { | ||
312 | return map(array, 0); | ||
313 | }; | ||
314 | |||
315 | nest.entries = function(array) { | ||
316 | return entries(map(array, 0), 0); | ||
317 | }; | ||
318 | |||
319 | nest.key = function(d) { | ||
320 | keys.push(d); | ||
321 | return nest; | ||
322 | }; | ||
323 | |||
324 | // Specifies the order for the most-recently specified key. | ||
325 | // Note: only applies to entries. Map keys are unordered! | ||
326 | nest.sortKeys = function(order) { | ||
327 | sortKeys[keys.length - 1] = order; | ||
328 | return nest; | ||
329 | }; | ||
330 | |||
331 | // Specifies the order for leaf values. | ||
332 | // Applies to both maps and entries array. | ||
333 | nest.sortValues = function(order) { | ||
334 | sortValues = order; | ||
335 | return nest; | ||
336 | }; | ||
337 | |||
338 | nest.rollup = function(f) { | ||
339 | rollup = f; | ||
340 | return nest; | ||
341 | }; | ||
342 | |||
343 | return nest; | ||
344 | }; | ||
345 | d3.keys = function(map) { | ||
346 | var keys = []; | ||
347 | for (var key in map) keys.push(key); | ||
348 | return keys; | ||
349 | }; | ||
350 | d3.values = function(map) { | ||
351 | var values = []; | ||
352 | for (var key in map) values.push(map[key]); | ||
353 | return values; | ||
354 | }; | ||
355 | d3.entries = function(map) { | ||
356 | var entries = []; | ||
357 | for (var key in map) entries.push({key: key, value: map[key]}); | ||
358 | return entries; | ||
359 | }; | ||
360 | d3.permute = function(array, indexes) { | ||
361 | var permutes = [], | ||
362 | i = -1, | ||
363 | n = indexes.length; | ||
364 | while (++i < n) permutes[i] = array[indexes[i]]; | ||
365 | return permutes; | ||
366 | }; | ||
367 | d3.merge = function(arrays) { | ||
368 | return Array.prototype.concat.apply([], arrays); | ||
369 | }; | ||
370 | d3.split = function(array, f) { | ||
371 | var arrays = [], | ||
372 | values = [], | ||
373 | value, | ||
374 | i = -1, | ||
375 | n = array.length; | ||
376 | if (arguments.length < 2) f = d3_splitter; | ||
377 | while (++i < n) { | ||
378 | if (f.call(values, value = array[i], i)) { | ||
379 | values = []; | ||
380 | } else { | ||
381 | if (!values.length) arrays.push(values); | ||
382 | values.push(value); | ||
383 | } | ||
384 | } | ||
385 | return arrays; | ||
386 | }; | ||
387 | |||
388 | function d3_splitter(d) { | ||
389 | return d == null; | ||
390 | } | ||
391 | function d3_collapse(s) { | ||
392 | return s.replace(/(^\s+)|(\s+$)/g, "").replace(/\s+/g, " "); | ||
393 | } | ||
394 | /** | ||
395 | * @param {number} start | ||
396 | * @param {number=} stop | ||
397 | * @param {number=} step | ||
398 | */ | ||
399 | d3.range = function(start, stop, step) { | ||
400 | if (arguments.length < 3) { | ||
401 | step = 1; | ||
402 | if (arguments.length < 2) { | ||
403 | stop = start; | ||
404 | start = 0; | ||
405 | } | ||
406 | } | ||
407 | if ((stop - start) / step == Infinity) throw new Error("infinite range"); | ||
408 | var range = [], | ||
409 | i = -1, | ||
410 | j; | ||
411 | if (step < 0) while ((j = start + step * ++i) > stop) range.push(j); | ||
412 | else while ((j = start + step * ++i) < stop) range.push(j); | ||
413 | return range; | ||
414 | }; | ||
415 | d3.requote = function(s) { | ||
416 | return s.replace(d3_requote_re, "\\$&"); | ||
417 | }; | ||
418 | |||
419 | var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g; | ||
420 | d3.round = function(x, n) { | ||
421 | return n | ||
422 | ? Math.round(x * (n = Math.pow(10, n))) / n | ||
423 | : Math.round(x); | ||
424 | }; | ||
425 | d3.xhr = function(url, mime, callback) { | ||
426 | var req = new XMLHttpRequest; | ||
427 | if (arguments.length < 3) callback = mime, mime = null; | ||
428 | else if (mime && req.overrideMimeType) req.overrideMimeType(mime); | ||
429 | req.open("GET", url, true); | ||
430 | if (mime) req.setRequestHeader("Accept", mime); | ||
431 | req.onreadystatechange = function() { | ||
432 | if (req.readyState === 4) callback(req.status < 300 ? req : null); | ||
433 | }; | ||
434 | req.send(null); | ||
435 | }; | ||
436 | d3.text = function(url, mime, callback) { | ||
437 | function ready(req) { | ||
438 | callback(req && req.responseText); | ||
439 | } | ||
440 | if (arguments.length < 3) { | ||
441 | callback = mime; | ||
442 | mime = null; | ||
443 | } | ||
444 | d3.xhr(url, mime, ready); | ||
445 | }; | ||
446 | d3.json = function(url, callback) { | ||
447 | d3.text(url, "application/json", function(text) { | ||
448 | callback(text ? JSON.parse(text) : null); | ||
449 | }); | ||
450 | }; | ||
451 | d3.html = function(url, callback) { | ||
452 | d3.text(url, "text/html", function(text) { | ||
453 | if (text != null) { // Treat empty string as valid HTML. | ||
454 | var range = document.createRange(); | ||
455 | range.selectNode(document.body); | ||
456 | text = range.createContextualFragment(text); | ||
457 | } | ||
458 | callback(text); | ||
459 | }); | ||
460 | }; | ||
461 | d3.xml = function(url, mime, callback) { | ||
462 | function ready(req) { | ||
463 | callback(req && req.responseXML); | ||
464 | } | ||
465 | if (arguments.length < 3) { | ||
466 | callback = mime; | ||
467 | mime = null; | ||
468 | } | ||
469 | d3.xhr(url, mime, ready); | ||
470 | }; | ||
471 | var d3_nsPrefix = { | ||
472 | svg: "http://www.w3.org/2000/svg", | ||
473 | xhtml: "http://www.w3.org/1999/xhtml", | ||
474 | xlink: "http://www.w3.org/1999/xlink", | ||
475 | xml: "http://www.w3.org/XML/1998/namespace", | ||
476 | xmlns: "http://www.w3.org/2000/xmlns/" | ||
477 | }; | ||
478 | |||
479 | d3.ns = { | ||
480 | prefix: d3_nsPrefix, | ||
481 | qualify: function(name) { | ||
482 | var i = name.indexOf(":"); | ||
483 | return i < 0 ? (name in d3_nsPrefix | ||
484 | ? {space: d3_nsPrefix[name], local: name} : name) | ||
485 | : {space: d3_nsPrefix[name.substring(0, i)], local: name.substring(i + 1)}; | ||
486 | } | ||
487 | }; | ||
488 | d3.dispatch = function() { | ||
489 | var dispatch = new d3_dispatch(), | ||
490 | i = -1, | ||
491 | n = arguments.length; | ||
492 | while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); | ||
493 | return dispatch; | ||
494 | }; | ||
495 | |||
496 | function d3_dispatch() {} | ||
497 | |||
498 | d3_dispatch.prototype.on = function(type, listener) { | ||
499 | var i = type.indexOf("."), | ||
500 | name = ""; | ||
501 | |||
502 | // Extract optional namespace, e.g., "click.foo" | ||
503 | if (i > 0) { | ||
504 | name = type.substring(i + 1); | ||
505 | type = type.substring(0, i); | ||
506 | } | ||
507 | |||
508 | return arguments.length < 2 | ||
509 | ? this[type].on(name) | ||
510 | : this[type].on(name, listener); | ||
511 | }; | ||
512 | |||
513 | function d3_dispatch_event(dispatch) { | ||
514 | var listeners = [], | ||
515 | listenerByName = {}; | ||
516 | |||
517 | function event() { | ||
518 | var z = listeners, // defensive reference | ||
519 | i = -1, | ||
520 | n = z.length, | ||
521 | l; | ||
522 | while (++i < n) if (l = z[i].on) l.apply(this, arguments); | ||
523 | return dispatch; | ||
524 | } | ||
525 | |||
526 | event.on = function(name, listener) { | ||
527 | var l, i; | ||
528 | |||
529 | // return the current listener, if any | ||
530 | if (arguments.length < 2) return (l = listenerByName[name]) && l.on; | ||
531 | |||
532 | // remove the old listener, if any (with copy-on-write) | ||
533 | if (l = listenerByName[name]) { | ||
534 | l.on = null; | ||
535 | listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1)); | ||
536 | delete listenerByName[name]; | ||
537 | } | ||
538 | |||
539 | // add the new listener, if any | ||
540 | if (listener) { | ||
541 | listeners.push(listenerByName[name] = {on: listener}); | ||
542 | } | ||
543 | |||
544 | return dispatch; | ||
545 | }; | ||
546 | |||
547 | return event; | ||
548 | } | ||
549 | // TODO align | ||
550 | d3.format = function(specifier) { | ||
551 | var match = d3_format_re.exec(specifier), | ||
552 | fill = match[1] || " ", | ||
553 | sign = match[3] || "", | ||
554 | zfill = match[5], | ||
555 | width = +match[6], | ||
556 | comma = match[7], | ||
557 | precision = match[8], | ||
558 | type = match[9], | ||
559 | scale = 1, | ||
560 | suffix = "", | ||
561 | integer = false; | ||
562 | |||
563 | if (precision) precision = +precision.substring(1); | ||
564 | |||
565 | if (zfill) { | ||
566 | fill = "0"; // TODO align = "="; | ||
567 | if (comma) width -= Math.floor((width - 1) / 4); | ||
568 | } | ||
569 | |||
570 | switch (type) { | ||
571 | case "n": comma = true; type = "g"; break; | ||
572 | case "%": scale = 100; suffix = "%"; type = "f"; break; | ||
573 | case "p": scale = 100; suffix = "%"; type = "r"; break; | ||
574 | case "d": integer = true; precision = 0; break; | ||
575 | case "s": scale = -1; type = "r"; break; | ||
576 | } | ||
577 | |||
578 | // If no precision is specified for r, fallback to general notation. | ||
579 | if (type == "r" && !precision) type = "g"; | ||
580 | |||
581 | type = d3_format_types[type] || d3_format_typeDefault; | ||
582 | |||
583 | return function(value) { | ||
584 | |||
585 | // Return the empty string for floats formatted as ints. | ||
586 | if (integer && (value % 1)) return ""; | ||
587 | |||
588 | // Convert negative to positive, and record the sign prefix. | ||
589 | var negative = (value < 0) && (value = -value) ? "\u2212" : sign; | ||
590 | |||
591 | // Apply the scale, computing it from the value's exponent for si format. | ||
592 | if (scale < 0) { | ||
593 | var prefix = d3.formatPrefix(value, precision); | ||
594 | value *= prefix.scale; | ||
595 | suffix = prefix.symbol; | ||
596 | } else { | ||
597 | value *= scale; | ||
598 | } | ||
599 | |||
600 | // Convert to the desired precision. | ||
601 | value = type(value, precision); | ||
602 | |||
603 | // If the fill character is 0, the sign and group is applied after the fill. | ||
604 | if (zfill) { | ||
605 | var length = value.length + negative.length; | ||
606 | if (length < width) value = new Array(width - length + 1).join(fill) + value; | ||
607 | if (comma) value = d3_format_group(value); | ||
608 | value = negative + value; | ||
609 | } | ||
610 | |||
611 | // Otherwise (e.g., space-filling), the sign and group is applied before. | ||
612 | else { | ||
613 | if (comma) value = d3_format_group(value); | ||
614 | value = negative + value; | ||
615 | var length = value.length; | ||
616 | if (length < width) value = new Array(width - length + 1).join(fill) + value; | ||
617 | } | ||
618 | |||
619 | return value + suffix; | ||
620 | }; | ||
621 | }; | ||
622 | |||
623 | // [[fill]align][sign][#][0][width][,][.precision][type] | ||
624 | var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/; | ||
625 | |||
626 | var d3_format_types = { | ||
627 | g: function(x, p) { return x.toPrecision(p); }, | ||
628 | e: function(x, p) { return x.toExponential(p); }, | ||
629 | f: function(x, p) { return x.toFixed(p); }, | ||
630 | r: function(x, p) { return d3.round(x, p = d3_format_precision(x, p)).toFixed(Math.max(0, Math.min(20, p))); } | ||
631 | }; | ||
632 | |||
633 | function d3_format_precision(x, p) { | ||
634 | return p - (x ? 1 + Math.floor(Math.log(x + Math.pow(10, 1 + Math.floor(Math.log(x) / Math.LN10) - p)) / Math.LN10) : 1); | ||
635 | } | ||
636 | |||
637 | function d3_format_typeDefault(x) { | ||
638 | return x + ""; | ||
639 | } | ||
640 | |||
641 | // Apply comma grouping for thousands. | ||
642 | function d3_format_group(value) { | ||
643 | var i = value.lastIndexOf("."), | ||
644 | f = i >= 0 ? value.substring(i) : (i = value.length, ""), | ||
645 | t = []; | ||
646 | while (i > 0) t.push(value.substring(i -= 3, i + 3)); | ||
647 | return t.reverse().join(",") + f; | ||
648 | } | ||
649 | var d3_formatPrefixes = ["y","z","a","f","p","n","μ","m","","k","M","G","T","P","E","Z","Y"].map(d3_formatPrefix); | ||
650 | |||
651 | d3.formatPrefix = function(value, precision) { | ||
652 | var i = 0; | ||
653 | if (value) { | ||
654 | if (value < 0) value *= -1; | ||
655 | if (precision) value = d3.round(value, d3_format_precision(value, precision)); | ||
656 | i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10); | ||
657 | i = Math.max(-24, Math.min(24, Math.floor((i <= 0 ? i + 1 : i - 1) / 3) * 3)); | ||
658 | } | ||
659 | return d3_formatPrefixes[8 + i / 3]; | ||
660 | }; | ||
661 | |||
662 | function d3_formatPrefix(d, i) { | ||
663 | return { | ||
664 | scale: Math.pow(10, (8 - i) * 3), | ||
665 | symbol: d | ||
666 | }; | ||
667 | } | ||
668 | |||
669 | /* | ||
670 | * TERMS OF USE - EASING EQUATIONS | ||
671 | * | ||
672 | * Open source under the BSD License. | ||
673 | * | ||
674 | * Copyright 2001 Robert Penner | ||
675 | * All rights reserved. | ||
676 | * | ||
677 | * Redistribution and use in source and binary forms, with or without | ||
678 | * modification, are permitted provided that the following conditions are met: | ||
679 | * | ||
680 | * - Redistributions of source code must retain the above copyright notice, this | ||
681 | * list of conditions and the following disclaimer. | ||
682 | * | ||
683 | * - Redistributions in binary form must reproduce the above copyright notice, | ||
684 | * this list of conditions and the following disclaimer in the documentation | ||
685 | * and/or other materials provided with the distribution. | ||
686 | * | ||
687 | * - Neither the name of the author nor the names of contributors may be used to | ||
688 | * endorse or promote products derived from this software without specific | ||
689 | * prior written permission. | ||
690 | * | ||
691 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
692 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
693 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
694 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
695 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
696 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
697 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
698 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
699 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
700 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
701 | * POSSIBILITY OF SUCH DAMAGE. | ||
702 | */ | ||
703 | |||
704 | var d3_ease_quad = d3_ease_poly(2), | ||
705 | d3_ease_cubic = d3_ease_poly(3); | ||
706 | |||
707 | var d3_ease = { | ||
708 | linear: function() { return d3_ease_linear; }, | ||
709 | poly: d3_ease_poly, | ||
710 | quad: function() { return d3_ease_quad; }, | ||
711 | cubic: function() { return d3_ease_cubic; }, | ||
712 | sin: function() { return d3_ease_sin; }, | ||
713 | exp: function() { return d3_ease_exp; }, | ||
714 | circle: function() { return d3_ease_circle; }, | ||
715 | elastic: d3_ease_elastic, | ||
716 | back: d3_ease_back, | ||
717 | bounce: function() { return d3_ease_bounce; } | ||
718 | }; | ||
719 | |||
720 | var d3_ease_mode = { | ||
721 | "in": function(f) { return f; }, | ||
722 | "out": d3_ease_reverse, | ||
723 | "in-out": d3_ease_reflect, | ||
724 | "out-in": function(f) { return d3_ease_reflect(d3_ease_reverse(f)); } | ||
725 | }; | ||
726 | |||
727 | d3.ease = function(name) { | ||
728 | var i = name.indexOf("-"), | ||
729 | t = i >= 0 ? name.substring(0, i) : name, | ||
730 | m = i >= 0 ? name.substring(i + 1) : "in"; | ||
731 | return d3_ease_clamp(d3_ease_mode[m](d3_ease[t].apply(null, Array.prototype.slice.call(arguments, 1)))); | ||
732 | }; | ||
733 | |||
734 | function d3_ease_clamp(f) { | ||
735 | return function(t) { | ||
736 | return t <= 0 ? 0 : t >= 1 ? 1 : f(t); | ||
737 | }; | ||
738 | } | ||
739 | |||
740 | function d3_ease_reverse(f) { | ||
741 | return function(t) { | ||
742 | return 1 - f(1 - t); | ||
743 | }; | ||
744 | } | ||
745 | |||
746 | function d3_ease_reflect(f) { | ||
747 | return function(t) { | ||
748 | return .5 * (t < .5 ? f(2 * t) : (2 - f(2 - 2 * t))); | ||
749 | }; | ||
750 | } | ||
751 | |||
752 | function d3_ease_linear(t) { | ||
753 | return t; | ||
754 | } | ||
755 | |||
756 | function d3_ease_poly(e) { | ||
757 | return function(t) { | ||
758 | return Math.pow(t, e); | ||
759 | } | ||
760 | } | ||
761 | |||
762 | function d3_ease_sin(t) { | ||
763 | return 1 - Math.cos(t * Math.PI / 2); | ||
764 | } | ||
765 | |||
766 | function d3_ease_exp(t) { | ||
767 | return Math.pow(2, 10 * (t - 1)); | ||
768 | } | ||
769 | |||
770 | function d3_ease_circle(t) { | ||
771 | return 1 - Math.sqrt(1 - t * t); | ||
772 | } | ||
773 | |||
774 | function d3_ease_elastic(a, p) { | ||
775 | var s; | ||
776 | if (arguments.length < 2) p = 0.45; | ||
777 | if (arguments.length < 1) { a = 1; s = p / 4; } | ||
778 | else s = p / (2 * Math.PI) * Math.asin(1 / a); | ||
779 | return function(t) { | ||
780 | return 1 + a * Math.pow(2, 10 * -t) * Math.sin((t - s) * 2 * Math.PI / p); | ||
781 | }; | ||
782 | } | ||
783 | |||
784 | function d3_ease_back(s) { | ||
785 | if (!s) s = 1.70158; | ||
786 | return function(t) { | ||
787 | return t * t * ((s + 1) * t - s); | ||
788 | }; | ||
789 | } | ||
790 | |||
791 | function d3_ease_bounce(t) { | ||
792 | return t < 1 / 2.75 ? 7.5625 * t * t | ||
793 | : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 | ||
794 | : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 | ||
795 | : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; | ||
796 | } | ||
797 | d3.event = null; | ||
798 | |||
799 | function d3_eventCancel() { | ||
800 | d3.event.stopPropagation(); | ||
801 | d3.event.preventDefault(); | ||
802 | } | ||
803 | d3.interpolate = function(a, b) { | ||
804 | var i = d3.interpolators.length, f; | ||
805 | while (--i >= 0 && !(f = d3.interpolators[i](a, b))); | ||
806 | return f; | ||
807 | }; | ||
808 | |||
809 | d3.interpolateNumber = function(a, b) { | ||
810 | b -= a; | ||
811 | return function(t) { return a + b * t; }; | ||
812 | }; | ||
813 | |||
814 | d3.interpolateRound = function(a, b) { | ||
815 | b -= a; | ||
816 | return function(t) { return Math.round(a + b * t); }; | ||
817 | }; | ||
818 | |||
819 | d3.interpolateString = function(a, b) { | ||
820 | var m, // current match | ||
821 | i, // current index | ||
822 | j, // current index (for coallescing) | ||
823 | s0 = 0, // start index of current string prefix | ||
824 | s1 = 0, // end index of current string prefix | ||
825 | s = [], // string constants and placeholders | ||
826 | q = [], // number interpolators | ||
827 | n, // q.length | ||
828 | o; | ||
829 | |||
830 | // Reset our regular expression! | ||
831 | d3_interpolate_number.lastIndex = 0; | ||
832 | |||
833 | // Find all numbers in b. | ||
834 | for (i = 0; m = d3_interpolate_number.exec(b); ++i) { | ||
835 | if (m.index) s.push(b.substring(s0, s1 = m.index)); | ||
836 | q.push({i: s.length, x: m[0]}); | ||
837 | s.push(null); | ||
838 | s0 = d3_interpolate_number.lastIndex; | ||
839 | } | ||
840 | if (s0 < b.length) s.push(b.substring(s0)); | ||
841 | |||
842 | // Find all numbers in a. | ||
843 | for (i = 0, n = q.length; (m = d3_interpolate_number.exec(a)) && i < n; ++i) { | ||
844 | o = q[i]; | ||
845 | if (o.x == m[0]) { // The numbers match, so coallesce. | ||
846 | if (o.i) { | ||
847 | if (s[o.i + 1] == null) { // This match is followed by another number. | ||
848 | s[o.i - 1] += o.x; | ||
849 | s.splice(o.i, 1); | ||
850 | for (j = i + 1; j < n; ++j) q[j].i--; | ||
851 | } else { // This match is followed by a string, so coallesce twice. | ||
852 | s[o.i - 1] += o.x + s[o.i + 1]; | ||
853 | s.splice(o.i, 2); | ||
854 | for (j = i + 1; j < n; ++j) q[j].i -= 2; | ||
855 | } | ||
856 | } else { | ||
857 | if (s[o.i + 1] == null) { // This match is followed by another number. | ||
858 | s[o.i] = o.x; | ||
859 | } else { // This match is followed by a string, so coallesce twice. | ||
860 | s[o.i] = o.x + s[o.i + 1]; | ||
861 | s.splice(o.i + 1, 1); | ||
862 | for (j = i + 1; j < n; ++j) q[j].i--; | ||
863 | } | ||
864 | } | ||
865 | q.splice(i, 1); | ||
866 | n--; | ||
867 | i--; | ||
868 | } else { | ||
869 | o.x = d3.interpolateNumber(parseFloat(m[0]), parseFloat(o.x)); | ||
870 | } | ||
871 | } | ||
872 | |||
873 | // Remove any numbers in b not found in a. | ||
874 | while (i < n) { | ||
875 | o = q.pop(); | ||
876 | if (s[o.i + 1] == null) { // This match is followed by another number. | ||
877 | s[o.i] = o.x; | ||
878 | } else { // This match is followed by a string, so coallesce twice. | ||
879 | s[o.i] = o.x + s[o.i + 1]; | ||
880 | s.splice(o.i + 1, 1); | ||
881 | } | ||
882 | n--; | ||
883 | } | ||
884 | |||
885 | // Special optimization for only a single match. | ||
886 | if (s.length === 1) { | ||
887 | return s[0] == null ? q[0].x : function() { return b; }; | ||
888 | } | ||
889 | |||
890 | // Otherwise, interpolate each of the numbers and rejoin the string. | ||
891 | return function(t) { | ||
892 | for (i = 0; i < n; ++i) s[(o = q[i]).i] = o.x(t); | ||
893 | return s.join(""); | ||
894 | }; | ||
895 | }; | ||
896 | |||
897 | d3.interpolateTransform = function(a, b) { | ||
898 | var s = [], // string constants and placeholders | ||
899 | q = [], // number interpolators | ||
900 | n, | ||
901 | A = d3.transform(a), | ||
902 | B = d3.transform(b), | ||
903 | ta = A.translate, | ||
904 | tb = B.translate, | ||
905 | ra = A.rotate, | ||
906 | rb = B.rotate, | ||
907 | wa = A.skew, | ||
908 | wb = B.skew, | ||
909 | ka = A.scale, | ||
910 | kb = B.scale; | ||
911 | |||
912 | if (ta[0] != tb[0] || ta[1] != tb[1]) { | ||
913 | s.push("translate(", null, ",", null, ")"); | ||
914 | q.push({i: 1, x: d3.interpolateNumber(ta[0], tb[0])}, {i: 3, x: d3.interpolateNumber(ta[1], tb[1])}); | ||
915 | } else if (tb[0] || tb[1]) { | ||
916 | s.push("translate(" + tb + ")"); | ||
917 | } else { | ||
918 | s.push(""); | ||
919 | } | ||
920 | |||
921 | if (ra != rb) { | ||
922 | q.push({i: s.push(s.pop() + "rotate(", null, ")") - 2, x: d3.interpolateNumber(ra, rb)}); | ||
923 | } else if (rb) { | ||
924 | s.push(s.pop() + "rotate(" + rb + ")"); | ||
925 | } | ||
926 | |||
927 | if (wa != wb) { | ||
928 | q.push({i: s.push(s.pop() + "skewX(", null, ")") - 2, x: d3.interpolateNumber(wa, wb)}); | ||
929 | } else if (wb) { | ||
930 | s.push(s.pop() + "skewX(" + wb + ")"); | ||
931 | } | ||
932 | |||
933 | if (ka[0] != kb[0] || ka[1] != kb[1]) { | ||
934 | n = s.push(s.pop() + "scale(", null, ",", null, ")"); | ||
935 | q.push({i: n - 4, x: d3.interpolateNumber(ka[0], kb[0])}, {i: n - 2, x: d3.interpolateNumber(ka[1], kb[1])}); | ||
936 | } else if (kb[0] != 1 || kb[1] != 1) { | ||
937 | s.push(s.pop() + "scale(" + kb + ")"); | ||
938 | } | ||
939 | |||
940 | n = q.length; | ||
941 | return function(t) { | ||
942 | var i = -1, o; | ||
943 | while (++i < n) s[(o = q[i]).i] = o.x(t); | ||
944 | return s.join(""); | ||
945 | }; | ||
946 | }; | ||
947 | |||
948 | d3.interpolateRgb = function(a, b) { | ||
949 | a = d3.rgb(a); | ||
950 | b = d3.rgb(b); | ||
951 | var ar = a.r, | ||
952 | ag = a.g, | ||
953 | ab = a.b, | ||
954 | br = b.r - ar, | ||
955 | bg = b.g - ag, | ||
956 | bb = b.b - ab; | ||
957 | return function(t) { | ||
958 | return "#" | ||
959 | + d3_rgb_hex(Math.round(ar + br * t)) | ||
960 | + d3_rgb_hex(Math.round(ag + bg * t)) | ||
961 | + d3_rgb_hex(Math.round(ab + bb * t)); | ||
962 | }; | ||
963 | }; | ||
964 | |||
965 | // interpolates HSL space, but outputs RGB string (for compatibility) | ||
966 | d3.interpolateHsl = function(a, b) { | ||
967 | a = d3.hsl(a); | ||
968 | b = d3.hsl(b); | ||
969 | var h0 = a.h, | ||
970 | s0 = a.s, | ||
971 | l0 = a.l, | ||
972 | h1 = b.h - h0, | ||
973 | s1 = b.s - s0, | ||
974 | l1 = b.l - l0; | ||
975 | return function(t) { | ||
976 | return d3_hsl_rgb(h0 + h1 * t, s0 + s1 * t, l0 + l1 * t).toString(); | ||
977 | }; | ||
978 | }; | ||
979 | |||
980 | d3.interpolateArray = function(a, b) { | ||
981 | var x = [], | ||
982 | c = [], | ||
983 | na = a.length, | ||
984 | nb = b.length, | ||
985 | n0 = Math.min(a.length, b.length), | ||
986 | i; | ||
987 | for (i = 0; i < n0; ++i) x.push(d3.interpolate(a[i], b[i])); | ||
988 | for (; i < na; ++i) c[i] = a[i]; | ||
989 | for (; i < nb; ++i) c[i] = b[i]; | ||
990 | return function(t) { | ||
991 | for (i = 0; i < n0; ++i) c[i] = x[i](t); | ||
992 | return c; | ||
993 | }; | ||
994 | }; | ||
995 | |||
996 | d3.interpolateObject = function(a, b) { | ||
997 | var i = {}, | ||
998 | c = {}, | ||
999 | k; | ||
1000 | for (k in a) { | ||
1001 | if (k in b) { | ||
1002 | i[k] = d3_interpolateByName(k)(a[k], b[k]); | ||
1003 | } else { | ||
1004 | c[k] = a[k]; | ||
1005 | } | ||
1006 | } | ||
1007 | for (k in b) { | ||
1008 | if (!(k in a)) { | ||
1009 | c[k] = b[k]; | ||
1010 | } | ||
1011 | } | ||
1012 | return function(t) { | ||
1013 | for (k in i) c[k] = i[k](t); | ||
1014 | return c; | ||
1015 | }; | ||
1016 | } | ||
1017 | |||
1018 | var d3_interpolate_number = /[-+]?(?:\d*\.?\d+)(?:[eE][-+]?\d+)?/g; | ||
1019 | |||
1020 | function d3_interpolateByName(n) { | ||
1021 | return n == "transform" | ||
1022 | ? d3.interpolateTransform | ||
1023 | : d3.interpolate; | ||
1024 | } | ||
1025 | |||
1026 | d3.interpolators = [ | ||
1027 | d3.interpolateObject, | ||
1028 | function(a, b) { return (b instanceof Array) && d3.interpolateArray(a, b); }, | ||
1029 | function(a, b) { return (typeof a === "string" || typeof b === "string") && d3.interpolateString(a + "", b + ""); }, | ||
1030 | function(a, b) { return (typeof b === "string" ? b in d3_rgb_names || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); }, | ||
1031 | function(a, b) { return !isNaN(a = +a) && !isNaN(b = +b) && d3.interpolateNumber(a, b); } | ||
1032 | ]; | ||
1033 | function d3_uninterpolateNumber(a, b) { | ||
1034 | b = b - (a = +a) ? 1 / (b - a) : 0; | ||
1035 | return function(x) { return (x - a) * b; }; | ||
1036 | } | ||
1037 | |||
1038 | function d3_uninterpolateClamp(a, b) { | ||
1039 | b = b - (a = +a) ? 1 / (b - a) : 0; | ||
1040 | return function(x) { return Math.max(0, Math.min(1, (x - a) * b)); }; | ||
1041 | } | ||
1042 | d3.rgb = function(r, g, b) { | ||
1043 | return arguments.length === 1 | ||
1044 | ? (r instanceof d3_Rgb ? d3_rgb(r.r, r.g, r.b) | ||
1045 | : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb)) | ||
1046 | : d3_rgb(~~r, ~~g, ~~b); | ||
1047 | }; | ||
1048 | |||
1049 | function d3_rgb(r, g, b) { | ||
1050 | return new d3_Rgb(r, g, b); | ||
1051 | } | ||
1052 | |||
1053 | function d3_Rgb(r, g, b) { | ||
1054 | this.r = r; | ||
1055 | this.g = g; | ||
1056 | this.b = b; | ||
1057 | } | ||
1058 | |||
1059 | d3_Rgb.prototype.brighter = function(k) { | ||
1060 | k = Math.pow(0.7, arguments.length ? k : 1); | ||
1061 | var r = this.r, | ||
1062 | g = this.g, | ||
1063 | b = this.b, | ||
1064 | i = 30; | ||
1065 | if (!r && !g && !b) return d3_rgb(i, i, i); | ||
1066 | if (r && r < i) r = i; | ||
1067 | if (g && g < i) g = i; | ||
1068 | if (b && b < i) b = i; | ||
1069 | return d3_rgb( | ||
1070 | Math.min(255, Math.floor(r / k)), | ||
1071 | Math.min(255, Math.floor(g / k)), | ||
1072 | Math.min(255, Math.floor(b / k))); | ||
1073 | }; | ||
1074 | |||
1075 | d3_Rgb.prototype.darker = function(k) { | ||
1076 | k = Math.pow(0.7, arguments.length ? k : 1); | ||
1077 | return d3_rgb( | ||
1078 | Math.floor(k * this.r), | ||
1079 | Math.floor(k * this.g), | ||
1080 | Math.floor(k * this.b)); | ||
1081 | }; | ||
1082 | |||
1083 | d3_Rgb.prototype.hsl = function() { | ||
1084 | return d3_rgb_hsl(this.r, this.g, this.b); | ||
1085 | }; | ||
1086 | |||
1087 | d3_Rgb.prototype.toString = function() { | ||
1088 | return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b); | ||
1089 | }; | ||
1090 | |||
1091 | function d3_rgb_hex(v) { | ||
1092 | return v < 0x10 | ||
1093 | ? "0" + Math.max(0, v).toString(16) | ||
1094 | : Math.min(255, v).toString(16); | ||
1095 | } | ||
1096 | |||
1097 | function d3_rgb_parse(format, rgb, hsl) { | ||
1098 | var r = 0, // red channel; int in [0, 255] | ||
1099 | g = 0, // green channel; int in [0, 255] | ||
1100 | b = 0, // blue channel; int in [0, 255] | ||
1101 | m1, // CSS color specification match | ||
1102 | m2, // CSS color specification type (e.g., rgb) | ||
1103 | name; | ||
1104 | |||
1105 | /* Handle hsl, rgb. */ | ||
1106 | m1 = /([a-z]+)\((.*)\)/i.exec(format); | ||
1107 | if (m1) { | ||
1108 | m2 = m1[2].split(","); | ||
1109 | switch (m1[1]) { | ||
1110 | case "hsl": { | ||
1111 | return hsl( | ||
1112 | parseFloat(m2[0]), // degrees | ||
1113 | parseFloat(m2[1]) / 100, // percentage | ||
1114 | parseFloat(m2[2]) / 100 // percentage | ||
1115 | ); | ||
1116 | } | ||
1117 | case "rgb": { | ||
1118 | return rgb( | ||
1119 | d3_rgb_parseNumber(m2[0]), | ||
1120 | d3_rgb_parseNumber(m2[1]), | ||
1121 | d3_rgb_parseNumber(m2[2]) | ||
1122 | ); | ||
1123 | } | ||
1124 | } | ||
1125 | } | ||
1126 | |||
1127 | /* Named colors. */ | ||
1128 | if (name = d3_rgb_names[format]) return rgb(name.r, name.g, name.b); | ||
1129 | |||
1130 | /* Hexadecimal colors: #rgb and #rrggbb. */ | ||
1131 | if (format != null && format.charAt(0) === "#") { | ||
1132 | if (format.length === 4) { | ||
1133 | r = format.charAt(1); r += r; | ||
1134 | g = format.charAt(2); g += g; | ||
1135 | b = format.charAt(3); b += b; | ||
1136 | } else if (format.length === 7) { | ||
1137 | r = format.substring(1, 3); | ||
1138 | g = format.substring(3, 5); | ||
1139 | b = format.substring(5, 7); | ||
1140 | } | ||
1141 | r = parseInt(r, 16); | ||
1142 | g = parseInt(g, 16); | ||
1143 | b = parseInt(b, 16); | ||
1144 | } | ||
1145 | |||
1146 | return rgb(r, g, b); | ||
1147 | } | ||
1148 | |||
1149 | function d3_rgb_hsl(r, g, b) { | ||
1150 | var min = Math.min(r /= 255, g /= 255, b /= 255), | ||
1151 | max = Math.max(r, g, b), | ||
1152 | d = max - min, | ||
1153 | h, | ||
1154 | s, | ||
1155 | l = (max + min) / 2; | ||
1156 | if (d) { | ||
1157 | s = l < .5 ? d / (max + min) : d / (2 - max - min); | ||
1158 | if (r == max) h = (g - b) / d + (g < b ? 6 : 0); | ||
1159 | else if (g == max) h = (b - r) / d + 2; | ||
1160 | else h = (r - g) / d + 4; | ||
1161 | h *= 60; | ||
1162 | } else { | ||
1163 | s = h = 0; | ||
1164 | } | ||
1165 | return d3_hsl(h, s, l); | ||
1166 | } | ||
1167 | |||
1168 | function d3_rgb_parseNumber(c) { // either integer or percentage | ||
1169 | var f = parseFloat(c); | ||
1170 | return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f; | ||
1171 | } | ||
1172 | |||
1173 | var d3_rgb_names = { | ||
1174 | aliceblue: "#f0f8ff", | ||
1175 | antiquewhite: "#faebd7", | ||
1176 | aqua: "#00ffff", | ||
1177 | aquamarine: "#7fffd4", | ||
1178 | azure: "#f0ffff", | ||
1179 | beige: "#f5f5dc", | ||
1180 | bisque: "#ffe4c4", | ||
1181 | black: "#000000", | ||
1182 | blanchedalmond: "#ffebcd", | ||
1183 | blue: "#0000ff", | ||
1184 | blueviolet: "#8a2be2", | ||
1185 | brown: "#a52a2a", | ||
1186 | burlywood: "#deb887", | ||
1187 | cadetblue: "#5f9ea0", | ||
1188 | chartreuse: "#7fff00", | ||
1189 | chocolate: "#d2691e", | ||
1190 | coral: "#ff7f50", | ||
1191 | cornflowerblue: "#6495ed", | ||
1192 | cornsilk: "#fff8dc", | ||
1193 | crimson: "#dc143c", | ||
1194 | cyan: "#00ffff", | ||
1195 | darkblue: "#00008b", | ||
1196 | darkcyan: "#008b8b", | ||
1197 | darkgoldenrod: "#b8860b", | ||
1198 | darkgray: "#a9a9a9", | ||
1199 | darkgreen: "#006400", | ||
1200 | darkgrey: "#a9a9a9", | ||
1201 | darkkhaki: "#bdb76b", | ||
1202 | darkmagenta: "#8b008b", | ||
1203 | darkolivegreen: "#556b2f", | ||
1204 | darkorange: "#ff8c00", | ||
1205 | darkorchid: "#9932cc", | ||
1206 | darkred: "#8b0000", | ||
1207 | darksalmon: "#e9967a", | ||
1208 | darkseagreen: "#8fbc8f", | ||
1209 | darkslateblue: "#483d8b", | ||
1210 | darkslategray: "#2f4f4f", | ||
1211 | darkslategrey: "#2f4f4f", | ||
1212 | darkturquoise: "#00ced1", | ||
1213 | darkviolet: "#9400d3", | ||
1214 | deeppink: "#ff1493", | ||
1215 | deepskyblue: "#00bfff", | ||
1216 | dimgray: "#696969", | ||
1217 | dimgrey: "#696969", | ||
1218 | dodgerblue: "#1e90ff", | ||
1219 | firebrick: "#b22222", | ||
1220 | floralwhite: "#fffaf0", | ||
1221 | forestgreen: "#228b22", | ||
1222 | fuchsia: "#ff00ff", | ||
1223 | gainsboro: "#dcdcdc", | ||
1224 | ghostwhite: "#f8f8ff", | ||
1225 | gold: "#ffd700", | ||
1226 | goldenrod: "#daa520", | ||
1227 | gray: "#808080", | ||
1228 | green: "#008000", | ||
1229 | greenyellow: "#adff2f", | ||
1230 | grey: "#808080", | ||
1231 | honeydew: "#f0fff0", | ||
1232 | hotpink: "#ff69b4", | ||
1233 | indianred: "#cd5c5c", | ||
1234 | indigo: "#4b0082", | ||
1235 | ivory: "#fffff0", | ||
1236 | khaki: "#f0e68c", | ||
1237 | lavender: "#e6e6fa", | ||
1238 | lavenderblush: "#fff0f5", | ||
1239 | lawngreen: "#7cfc00", | ||
1240 | lemonchiffon: "#fffacd", | ||
1241 | lightblue: "#add8e6", | ||
1242 | lightcoral: "#f08080", | ||
1243 | lightcyan: "#e0ffff", | ||
1244 | lightgoldenrodyellow: "#fafad2", | ||
1245 | lightgray: "#d3d3d3", | ||
1246 | lightgreen: "#90ee90", | ||
1247 | lightgrey: "#d3d3d3", | ||
1248 | lightpink: "#ffb6c1", | ||
1249 | lightsalmon: "#ffa07a", | ||
1250 | lightseagreen: "#20b2aa", | ||
1251 | lightskyblue: "#87cefa", | ||
1252 | lightslategray: "#778899", | ||
1253 | lightslategrey: "#778899", | ||
1254 | lightsteelblue: "#b0c4de", | ||
1255 | lightyellow: "#ffffe0", | ||
1256 | lime: "#00ff00", | ||
1257 | limegreen: "#32cd32", | ||
1258 | linen: "#faf0e6", | ||
1259 | magenta: "#ff00ff", | ||
1260 | maroon: "#800000", | ||
1261 | mediumaquamarine: "#66cdaa", | ||
1262 | mediumblue: "#0000cd", | ||
1263 | mediumorchid: "#ba55d3", | ||
1264 | mediumpurple: "#9370db", | ||
1265 | mediumseagreen: "#3cb371", | ||
1266 | mediumslateblue: "#7b68ee", | ||
1267 | mediumspringgreen: "#00fa9a", | ||
1268 | mediumturquoise: "#48d1cc", | ||
1269 | mediumvioletred: "#c71585", | ||
1270 | midnightblue: "#191970", | ||
1271 | mintcream: "#f5fffa", | ||
1272 | mistyrose: "#ffe4e1", | ||
1273 | moccasin: "#ffe4b5", | ||
1274 | navajowhite: "#ffdead", | ||
1275 | navy: "#000080", | ||
1276 | oldlace: "#fdf5e6", | ||
1277 | olive: "#808000", | ||
1278 | olivedrab: "#6b8e23", | ||
1279 | orange: "#ffa500", | ||
1280 | orangered: "#ff4500", | ||
1281 | orchid: "#da70d6", | ||
1282 | palegoldenrod: "#eee8aa", | ||
1283 | palegreen: "#98fb98", | ||
1284 | paleturquoise: "#afeeee", | ||
1285 | palevioletred: "#db7093", | ||
1286 | papayawhip: "#ffefd5", | ||
1287 | peachpuff: "#ffdab9", | ||
1288 | peru: "#cd853f", | ||
1289 | pink: "#ffc0cb", | ||
1290 | plum: "#dda0dd", | ||
1291 | powderblue: "#b0e0e6", | ||
1292 | purple: "#800080", | ||
1293 | red: "#ff0000", | ||
1294 | rosybrown: "#bc8f8f", | ||
1295 | royalblue: "#4169e1", | ||
1296 | saddlebrown: "#8b4513", | ||
1297 | salmon: "#fa8072", | ||
1298 | sandybrown: "#f4a460", | ||
1299 | seagreen: "#2e8b57", | ||
1300 | seashell: "#fff5ee", | ||
1301 | sienna: "#a0522d", | ||
1302 | silver: "#c0c0c0", | ||
1303 | skyblue: "#87ceeb", | ||
1304 | slateblue: "#6a5acd", | ||
1305 | slategray: "#708090", | ||
1306 | slategrey: "#708090", | ||
1307 | snow: "#fffafa", | ||
1308 | springgreen: "#00ff7f", | ||
1309 | steelblue: "#4682b4", | ||
1310 | tan: "#d2b48c", | ||
1311 | teal: "#008080", | ||
1312 | thistle: "#d8bfd8", | ||
1313 | tomato: "#ff6347", | ||
1314 | turquoise: "#40e0d0", | ||
1315 | violet: "#ee82ee", | ||
1316 | wheat: "#f5deb3", | ||
1317 | white: "#ffffff", | ||
1318 | whitesmoke: "#f5f5f5", | ||
1319 | yellow: "#ffff00", | ||
1320 | yellowgreen: "#9acd32" | ||
1321 | }; | ||
1322 | |||
1323 | for (var d3_rgb_name in d3_rgb_names) { | ||
1324 | d3_rgb_names[d3_rgb_name] = d3_rgb_parse( | ||
1325 | d3_rgb_names[d3_rgb_name], | ||
1326 | d3_rgb, | ||
1327 | d3_hsl_rgb); | ||
1328 | } | ||
1329 | d3.hsl = function(h, s, l) { | ||
1330 | return arguments.length === 1 | ||
1331 | ? (h instanceof d3_Hsl ? d3_hsl(h.h, h.s, h.l) | ||
1332 | : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl)) | ||
1333 | : d3_hsl(+h, +s, +l); | ||
1334 | }; | ||
1335 | |||
1336 | function d3_hsl(h, s, l) { | ||
1337 | return new d3_Hsl(h, s, l); | ||
1338 | } | ||
1339 | |||
1340 | function d3_Hsl(h, s, l) { | ||
1341 | this.h = h; | ||
1342 | this.s = s; | ||
1343 | this.l = l; | ||
1344 | } | ||
1345 | |||
1346 | d3_Hsl.prototype.brighter = function(k) { | ||
1347 | k = Math.pow(0.7, arguments.length ? k : 1); | ||
1348 | return d3_hsl(this.h, this.s, this.l / k); | ||
1349 | }; | ||
1350 | |||
1351 | d3_Hsl.prototype.darker = function(k) { | ||
1352 | k = Math.pow(0.7, arguments.length ? k : 1); | ||
1353 | return d3_hsl(this.h, this.s, k * this.l); | ||
1354 | }; | ||
1355 | |||
1356 | d3_Hsl.prototype.rgb = function() { | ||
1357 | return d3_hsl_rgb(this.h, this.s, this.l); | ||
1358 | }; | ||
1359 | |||
1360 | d3_Hsl.prototype.toString = function() { | ||
1361 | return this.rgb().toString(); | ||
1362 | }; | ||
1363 | |||
1364 | function d3_hsl_rgb(h, s, l) { | ||
1365 | var m1, | ||
1366 | m2; | ||
1367 | |||
1368 | /* Some simple corrections for h, s and l. */ | ||
1369 | h = h % 360; if (h < 0) h += 360; | ||
1370 | s = s < 0 ? 0 : s > 1 ? 1 : s; | ||
1371 | l = l < 0 ? 0 : l > 1 ? 1 : l; | ||
1372 | |||
1373 | /* From FvD 13.37, CSS Color Module Level 3 */ | ||
1374 | m2 = l <= .5 ? l * (1 + s) : l + s - l * s; | ||
1375 | m1 = 2 * l - m2; | ||
1376 | |||
1377 | function v(h) { | ||
1378 | if (h > 360) h -= 360; | ||
1379 | else if (h < 0) h += 360; | ||
1380 | if (h < 60) return m1 + (m2 - m1) * h / 60; | ||
1381 | if (h < 180) return m2; | ||
1382 | if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60; | ||
1383 | return m1; | ||
1384 | } | ||
1385 | |||
1386 | function vv(h) { | ||
1387 | return Math.round(v(h) * 255); | ||
1388 | } | ||
1389 | |||
1390 | return d3_rgb(vv(h + 120), vv(h), vv(h - 120)); | ||
1391 | } | ||
1392 | function d3_selection(groups) { | ||
1393 | d3_arraySubclass(groups, d3_selectionPrototype); | ||
1394 | return groups; | ||
1395 | } | ||
1396 | |||
1397 | var d3_select = function(s, n) { return n.querySelector(s); }, | ||
1398 | d3_selectAll = function(s, n) { return n.querySelectorAll(s); }, | ||
1399 | d3_selectRoot = document.documentElement, | ||
1400 | d3_selectMatcher = d3_selectRoot.matchesSelector || d3_selectRoot.webkitMatchesSelector || d3_selectRoot.mozMatchesSelector || d3_selectRoot.msMatchesSelector || d3_selectRoot.oMatchesSelector, | ||
1401 | d3_selectMatches = function(n, s) { return d3_selectMatcher.call(n, s); }; | ||
1402 | |||
1403 | // Prefer Sizzle, if available. | ||
1404 | if (typeof Sizzle === "function") { | ||
1405 | d3_select = function(s, n) { return Sizzle(s, n)[0]; }; | ||
1406 | d3_selectAll = function(s, n) { return Sizzle.uniqueSort(Sizzle(s, n)); }; | ||
1407 | d3_selectMatches = Sizzle.matchesSelector; | ||
1408 | } | ||
1409 | |||
1410 | var d3_selectionPrototype = []; | ||
1411 | |||
1412 | d3.selection = function() { | ||
1413 | return d3_selectionRoot; | ||
1414 | }; | ||
1415 | |||
1416 | d3.selection.prototype = d3_selectionPrototype; | ||
1417 | d3_selectionPrototype.select = function(selector) { | ||
1418 | var subgroups = [], | ||
1419 | subgroup, | ||
1420 | subnode, | ||
1421 | group, | ||
1422 | node; | ||
1423 | |||
1424 | if (typeof selector !== "function") selector = d3_selection_selector(selector); | ||
1425 | |||
1426 | for (var j = -1, m = this.length; ++j < m;) { | ||
1427 | subgroups.push(subgroup = []); | ||
1428 | subgroup.parentNode = (group = this[j]).parentNode; | ||
1429 | for (var i = -1, n = group.length; ++i < n;) { | ||
1430 | if (node = group[i]) { | ||
1431 | subgroup.push(subnode = selector.call(node, node.__data__, i)); | ||
1432 | if (subnode && "__data__" in node) subnode.__data__ = node.__data__; | ||
1433 | } else { | ||
1434 | subgroup.push(null); | ||
1435 | } | ||
1436 | } | ||
1437 | } | ||
1438 | |||
1439 | return d3_selection(subgroups); | ||
1440 | }; | ||
1441 | |||
1442 | function d3_selection_selector(selector) { | ||
1443 | return function() { | ||
1444 | return d3_select(selector, this); | ||
1445 | }; | ||
1446 | } | ||
1447 | d3_selectionPrototype.selectAll = function(selector) { | ||
1448 | var subgroups = [], | ||
1449 | subgroup, | ||
1450 | node; | ||
1451 | |||
1452 | if (typeof selector !== "function") selector = d3_selection_selectorAll(selector); | ||
1453 | |||
1454 | for (var j = -1, m = this.length; ++j < m;) { | ||
1455 | for (var group = this[j], i = -1, n = group.length; ++i < n;) { | ||
1456 | if (node = group[i]) { | ||
1457 | subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i))); | ||
1458 | subgroup.parentNode = node; | ||
1459 | } | ||
1460 | } | ||
1461 | } | ||
1462 | |||
1463 | return d3_selection(subgroups); | ||
1464 | }; | ||
1465 | |||
1466 | function d3_selection_selectorAll(selector) { | ||
1467 | return function() { | ||
1468 | return d3_selectAll(selector, this); | ||
1469 | }; | ||
1470 | } | ||
1471 | d3_selectionPrototype.attr = function(name, value) { | ||
1472 | name = d3.ns.qualify(name); | ||
1473 | |||
1474 | // If no value is specified, return the first value. | ||
1475 | if (arguments.length < 2) { | ||
1476 | var node = this.node(); | ||
1477 | return name.local | ||
1478 | ? node.getAttributeNS(name.space, name.local) | ||
1479 | : node.getAttribute(name); | ||
1480 | } | ||
1481 | |||
1482 | function attrNull() { | ||
1483 | this.removeAttribute(name); | ||
1484 | } | ||
1485 | |||
1486 | function attrNullNS() { | ||
1487 | this.removeAttributeNS(name.space, name.local); | ||
1488 | } | ||
1489 | |||
1490 | function attrConstant() { | ||
1491 | this.setAttribute(name, value); | ||
1492 | } | ||
1493 | |||
1494 | function attrConstantNS() { | ||
1495 | this.setAttributeNS(name.space, name.local, value); | ||
1496 | } | ||
1497 | |||
1498 | function attrFunction() { | ||
1499 | var x = value.apply(this, arguments); | ||
1500 | if (x == null) this.removeAttribute(name); | ||
1501 | else this.setAttribute(name, x); | ||
1502 | } | ||
1503 | |||
1504 | function attrFunctionNS() { | ||
1505 | var x = value.apply(this, arguments); | ||
1506 | if (x == null) this.removeAttributeNS(name.space, name.local); | ||
1507 | else this.setAttributeNS(name.space, name.local, x); | ||
1508 | } | ||
1509 | |||
1510 | return this.each(value == null | ||
1511 | ? (name.local ? attrNullNS : attrNull) : (typeof value === "function" | ||
1512 | ? (name.local ? attrFunctionNS : attrFunction) | ||
1513 | : (name.local ? attrConstantNS : attrConstant))); | ||
1514 | }; | ||
1515 | d3_selectionPrototype.classed = function(name, value) { | ||
1516 | var names = name.split(d3_selection_classedWhitespace), | ||
1517 | n = names.length, | ||
1518 | i = -1; | ||
1519 | if (arguments.length > 1) { | ||
1520 | while (++i < n) d3_selection_classed.call(this, names[i], value); | ||
1521 | return this; | ||
1522 | } else { | ||
1523 | while (++i < n) if (!d3_selection_classed.call(this, names[i])) return false; | ||
1524 | return true; | ||
1525 | } | ||
1526 | }; | ||
1527 | |||
1528 | var d3_selection_classedWhitespace = /\s+/g; | ||
1529 | |||
1530 | function d3_selection_classed(name, value) { | ||
1531 | var re = new RegExp("(^|\\s+)" + d3.requote(name) + "(\\s+|$)", "g"); | ||
1532 | |||
1533 | // If no value is specified, return the first value. | ||
1534 | if (arguments.length < 2) { | ||
1535 | var node = this.node(); | ||
1536 | if (c = node.classList) return c.contains(name); | ||
1537 | var c = node.className; | ||
1538 | re.lastIndex = 0; | ||
1539 | return re.test(c.baseVal != null ? c.baseVal : c); | ||
1540 | } | ||
1541 | |||
1542 | function classedAdd() { | ||
1543 | if (c = this.classList) return c.add(name); | ||
1544 | var c = this.className, | ||
1545 | cb = c.baseVal != null, | ||
1546 | cv = cb ? c.baseVal : c; | ||
1547 | re.lastIndex = 0; | ||
1548 | if (!re.test(cv)) { | ||
1549 | cv = d3_collapse(cv + " " + name); | ||
1550 | if (cb) c.baseVal = cv; | ||
1551 | else this.className = cv; | ||
1552 | } | ||
1553 | } | ||
1554 | |||
1555 | function classedRemove() { | ||
1556 | if (c = this.classList) return c.remove(name); | ||
1557 | var c = this.className, | ||
1558 | cb = c.baseVal != null, | ||
1559 | cv = cb ? c.baseVal : c; | ||
1560 | cv = d3_collapse(cv.replace(re, " ")); | ||
1561 | if (cb) c.baseVal = cv; | ||
1562 | else this.className = cv; | ||
1563 | } | ||
1564 | |||
1565 | function classedFunction() { | ||
1566 | (value.apply(this, arguments) | ||
1567 | ? classedAdd | ||
1568 | : classedRemove).call(this); | ||
1569 | } | ||
1570 | |||
1571 | return this.each(typeof value === "function" | ||
1572 | ? classedFunction : value | ||
1573 | ? classedAdd | ||
1574 | : classedRemove); | ||
1575 | } | ||
1576 | d3_selectionPrototype.style = function(name, value, priority) { | ||
1577 | if (arguments.length < 3) priority = ""; | ||
1578 | |||
1579 | // If no value is specified, return the first value. | ||
1580 | if (arguments.length < 2) return window | ||
1581 | .getComputedStyle(this.node(), null) | ||
1582 | .getPropertyValue(name); | ||
1583 | |||
1584 | function styleNull() { | ||
1585 | this.style.removeProperty(name); | ||
1586 | } | ||
1587 | |||
1588 | function styleConstant() { | ||
1589 | this.style.setProperty(name, value, priority); | ||
1590 | } | ||
1591 | |||
1592 | function styleFunction() { | ||
1593 | var x = value.apply(this, arguments); | ||
1594 | if (x == null) this.style.removeProperty(name); | ||
1595 | else this.style.setProperty(name, x, priority); | ||
1596 | } | ||
1597 | |||
1598 | return this.each(value == null | ||
1599 | ? styleNull : (typeof value === "function" | ||
1600 | ? styleFunction : styleConstant)); | ||
1601 | }; | ||
1602 | d3_selectionPrototype.property = function(name, value) { | ||
1603 | |||
1604 | // If no value is specified, return the first value. | ||
1605 | if (arguments.length < 2) return this.node()[name]; | ||
1606 | |||
1607 | function propertyNull() { | ||
1608 | delete this[name]; | ||
1609 | } | ||
1610 | |||
1611 | function propertyConstant() { | ||
1612 | this[name] = value; | ||
1613 | } | ||
1614 | |||
1615 | function propertyFunction() { | ||
1616 | var x = value.apply(this, arguments); | ||
1617 | if (x == null) delete this[name]; | ||
1618 | else this[name] = x; | ||
1619 | } | ||
1620 | |||
1621 | return this.each(value == null | ||
1622 | ? propertyNull : (typeof value === "function" | ||
1623 | ? propertyFunction : propertyConstant)); | ||
1624 | }; | ||
1625 | d3_selectionPrototype.text = function(value) { | ||
1626 | return arguments.length < 1 | ||
1627 | ? this.node().textContent : this.each(typeof value === "function" | ||
1628 | ? function() { var v = value.apply(this, arguments); this.textContent = v == null ? "" : v; } : value == null | ||
1629 | ? function() { this.textContent = ""; } | ||
1630 | : function() { this.textContent = value; }); | ||
1631 | }; | ||
1632 | d3_selectionPrototype.html = function(value) { | ||
1633 | return arguments.length < 1 | ||
1634 | ? this.node().innerHTML : this.each(typeof value === "function" | ||
1635 | ? function() { var v = value.apply(this, arguments); this.innerHTML = v == null ? "" : v; } : value == null | ||
1636 | ? function() { this.innerHTML = ""; } | ||
1637 | : function() { this.innerHTML = value; }); | ||
1638 | }; | ||
1639 | // TODO append(node)? | ||
1640 | // TODO append(function)? | ||
1641 | d3_selectionPrototype.append = function(name) { | ||
1642 | name = d3.ns.qualify(name); | ||
1643 | |||
1644 | function append() { | ||
1645 | return this.appendChild(document.createElementNS(this.namespaceURI, name)); | ||
1646 | } | ||
1647 | |||
1648 | function appendNS() { | ||
1649 | return this.appendChild(document.createElementNS(name.space, name.local)); | ||
1650 | } | ||
1651 | |||
1652 | return this.select(name.local ? appendNS : append); | ||
1653 | }; | ||
1654 | // TODO insert(node, function)? | ||
1655 | // TODO insert(function, string)? | ||
1656 | // TODO insert(function, function)? | ||
1657 | d3_selectionPrototype.insert = function(name, before) { | ||
1658 | name = d3.ns.qualify(name); | ||
1659 | |||
1660 | function insert() { | ||
1661 | return this.insertBefore( | ||
1662 | document.createElementNS(this.namespaceURI, name), | ||
1663 | d3_select(before, this)); | ||
1664 | } | ||
1665 | |||
1666 | function insertNS() { | ||
1667 | return this.insertBefore( | ||
1668 | document.createElementNS(name.space, name.local), | ||
1669 | d3_select(before, this)); | ||
1670 | } | ||
1671 | |||
1672 | return this.select(name.local ? insertNS : insert); | ||
1673 | }; | ||
1674 | // TODO remove(selector)? | ||
1675 | // TODO remove(node)? | ||
1676 | // TODO remove(function)? | ||
1677 | d3_selectionPrototype.remove = function() { | ||
1678 | return this.each(function() { | ||
1679 | var parent = this.parentNode; | ||
1680 | if (parent) parent.removeChild(this); | ||
1681 | }); | ||
1682 | }; | ||
1683 | // TODO data(null) for clearing data? | ||
1684 | d3_selectionPrototype.data = function(data, join) { | ||
1685 | var enter = [], | ||
1686 | update = [], | ||
1687 | exit = []; | ||
1688 | |||
1689 | function bind(group, groupData) { | ||
1690 | var i, | ||
1691 | n = group.length, | ||
1692 | m = groupData.length, | ||
1693 | n0 = Math.min(n, m), | ||
1694 | n1 = Math.max(n, m), | ||
1695 | updateNodes = [], | ||
1696 | enterNodes = [], | ||
1697 | exitNodes = [], | ||
1698 | node, | ||
1699 | nodeData; | ||
1700 | |||
1701 | if (join) { | ||
1702 | var nodeByKey = {}, | ||
1703 | keys = [], | ||
1704 | key, | ||
1705 | j = groupData.length; | ||
1706 | |||
1707 | for (i = -1; ++i < n;) { | ||
1708 | key = join.call(node = group[i], node.__data__, i); | ||
1709 | if (key in nodeByKey) { | ||
1710 | exitNodes[j++] = node; // duplicate key | ||
1711 | } else { | ||
1712 | nodeByKey[key] = node; | ||
1713 | } | ||
1714 | keys.push(key); | ||
1715 | } | ||
1716 | |||
1717 | for (i = -1; ++i < m;) { | ||
1718 | node = nodeByKey[key = join.call(groupData, nodeData = groupData[i], i)]; | ||
1719 | if (node) { | ||
1720 | node.__data__ = nodeData; | ||
1721 | updateNodes[i] = node; | ||
1722 | enterNodes[i] = exitNodes[i] = null; | ||
1723 | } else { | ||
1724 | enterNodes[i] = d3_selection_dataNode(nodeData); | ||
1725 | updateNodes[i] = exitNodes[i] = null; | ||
1726 | } | ||
1727 | delete nodeByKey[key]; | ||
1728 | } | ||
1729 | |||
1730 | for (i = -1; ++i < n;) { | ||
1731 | if (keys[i] in nodeByKey) { | ||
1732 | exitNodes[i] = group[i]; | ||
1733 | } | ||
1734 | } | ||
1735 | } else { | ||
1736 | for (i = -1; ++i < n0;) { | ||
1737 | node = group[i]; | ||
1738 | nodeData = groupData[i]; | ||
1739 | if (node) { | ||
1740 | node.__data__ = nodeData; | ||
1741 | updateNodes[i] = node; | ||
1742 | enterNodes[i] = exitNodes[i] = null; | ||
1743 | } else { | ||
1744 | enterNodes[i] = d3_selection_dataNode(nodeData); | ||
1745 | updateNodes[i] = exitNodes[i] = null; | ||
1746 | } | ||
1747 | } | ||
1748 | for (; i < m; ++i) { | ||
1749 | enterNodes[i] = d3_selection_dataNode(groupData[i]); | ||
1750 | updateNodes[i] = exitNodes[i] = null; | ||
1751 | } | ||
1752 | for (; i < n1; ++i) { | ||
1753 | exitNodes[i] = group[i]; | ||
1754 | enterNodes[i] = updateNodes[i] = null; | ||
1755 | } | ||
1756 | } | ||
1757 | |||
1758 | enterNodes.update | ||
1759 | = updateNodes; | ||
1760 | |||
1761 | enterNodes.parentNode | ||
1762 | = updateNodes.parentNode | ||
1763 | = exitNodes.parentNode | ||
1764 | = group.parentNode; | ||
1765 | |||
1766 | enter.push(enterNodes); | ||
1767 | update.push(updateNodes); | ||
1768 | exit.push(exitNodes); | ||
1769 | } | ||
1770 | |||
1771 | var i = -1, | ||
1772 | n = this.length, | ||
1773 | group; | ||
1774 | if (typeof data === "function") { | ||
1775 | while (++i < n) { | ||
1776 | bind(group = this[i], data.call(group, group.parentNode.__data__, i)); | ||
1777 | } | ||
1778 | } else { | ||
1779 | while (++i < n) { | ||
1780 | bind(group = this[i], data); | ||
1781 | } | ||
1782 | } | ||
1783 | |||
1784 | var selection = d3_selection(update); | ||
1785 | selection.enter = function() { return d3_selection_enter(enter); }; | ||
1786 | selection.exit = function() { return d3_selection(exit); }; | ||
1787 | return selection; | ||
1788 | }; | ||
1789 | |||
1790 | function d3_selection_dataNode(data) { | ||
1791 | return {__data__: data}; | ||
1792 | } | ||
1793 | d3_selectionPrototype.filter = function(filter) { | ||
1794 | var subgroups = [], | ||
1795 | subgroup, | ||
1796 | group, | ||
1797 | node; | ||
1798 | |||
1799 | if (typeof filter !== "function") filter = d3_selection_filter(filter); | ||
1800 | |||
1801 | for (var j = 0, m = this.length; j < m; j++) { | ||
1802 | subgroups.push(subgroup = []); | ||
1803 | subgroup.parentNode = (group = this[j]).parentNode; | ||
1804 | for (var i = 0, n = group.length; i < n; i++) { | ||
1805 | if ((node = group[i]) && filter.call(node, node.__data__, i)) { | ||
1806 | subgroup.push(node); | ||
1807 | } | ||
1808 | } | ||
1809 | } | ||
1810 | |||
1811 | return d3_selection(subgroups); | ||
1812 | }; | ||
1813 | |||
1814 | function d3_selection_filter(selector) { | ||
1815 | return function() { | ||
1816 | return d3_selectMatches(this, selector); | ||
1817 | }; | ||
1818 | } | ||
1819 | d3_selectionPrototype.map = function(map) { | ||
1820 | return this.each(function() { | ||
1821 | this.__data__ = map.apply(this, arguments); | ||
1822 | }); | ||
1823 | }; | ||
1824 | d3_selectionPrototype.order = function() { | ||
1825 | for (var j = -1, m = this.length; ++j < m;) { | ||
1826 | for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0;) { | ||
1827 | if (node = group[i]) { | ||
1828 | if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next); | ||
1829 | next = node; | ||
1830 | } | ||
1831 | } | ||
1832 | } | ||
1833 | return this; | ||
1834 | }; | ||
1835 | d3_selectionPrototype.sort = function(comparator) { | ||
1836 | comparator = d3_selection_sortComparator.apply(this, arguments); | ||
1837 | for (var j = -1, m = this.length; ++j < m;) this[j].sort(comparator); | ||
1838 | return this.order(); | ||
1839 | }; | ||
1840 | |||
1841 | function d3_selection_sortComparator(comparator) { | ||
1842 | if (!arguments.length) comparator = d3.ascending; | ||
1843 | return function(a, b) { | ||
1844 | return comparator(a && a.__data__, b && b.__data__); | ||
1845 | }; | ||
1846 | } | ||
1847 | // type can be namespaced, e.g., "click.foo" | ||
1848 | // listener can be null for removal | ||
1849 | d3_selectionPrototype.on = function(type, listener, capture) { | ||
1850 | if (arguments.length < 3) capture = false; | ||
1851 | |||
1852 | // parse the type specifier | ||
1853 | var name = "__on" + type, i = type.indexOf("."); | ||
1854 | if (i > 0) type = type.substring(0, i); | ||
1855 | |||
1856 | // if called with only one argument, return the current listener | ||
1857 | if (arguments.length < 2) return (i = this.node()[name]) && i._; | ||
1858 | |||
1859 | // remove the old event listener, and add the new event listener | ||
1860 | return this.each(function(d, i) { | ||
1861 | var node = this; | ||
1862 | |||
1863 | if (node[name]) node.removeEventListener(type, node[name], capture); | ||
1864 | if (listener) node.addEventListener(type, node[name] = l, capture); | ||
1865 | |||
1866 | // wrapped event listener that preserves i | ||
1867 | function l(e) { | ||
1868 | var o = d3.event; // Events can be reentrant (e.g., focus). | ||
1869 | d3.event = e; | ||
1870 | try { | ||
1871 | listener.call(node, node.__data__, i); | ||
1872 | } finally { | ||
1873 | d3.event = o; | ||
1874 | } | ||
1875 | } | ||
1876 | |||
1877 | // stash the unwrapped listener for retrieval | ||
1878 | l._ = listener; | ||
1879 | }); | ||
1880 | }; | ||
1881 | d3_selectionPrototype.each = function(callback) { | ||
1882 | for (var j = -1, m = this.length; ++j < m;) { | ||
1883 | for (var group = this[j], i = -1, n = group.length; ++i < n;) { | ||
1884 | var node = group[i]; | ||
1885 | if (node) callback.call(node, node.__data__, i, j); | ||
1886 | } | ||
1887 | } | ||
1888 | return this; | ||
1889 | }; | ||
1890 | // | ||
1891 | // Note: assigning to the arguments array simultaneously changes the value of | ||
1892 | // the corresponding argument! | ||
1893 | // | ||
1894 | // TODO The `this` argument probably shouldn't be the first argument to the | ||
1895 | // callback, anyway, since it's redundant. However, that will require a major | ||
1896 | // version bump due to backwards compatibility, so I'm not changing it right | ||
1897 | // away. | ||
1898 | // | ||
1899 | d3_selectionPrototype.call = function(callback) { | ||
1900 | callback.apply(this, (arguments[0] = this, arguments)); | ||
1901 | return this; | ||
1902 | }; | ||
1903 | d3_selectionPrototype.empty = function() { | ||
1904 | return !this.node(); | ||
1905 | }; | ||
1906 | d3_selectionPrototype.node = function(callback) { | ||
1907 | for (var j = 0, m = this.length; j < m; j++) { | ||
1908 | for (var group = this[j], i = 0, n = group.length; i < n; i++) { | ||
1909 | var node = group[i]; | ||
1910 | if (node) return node; | ||
1911 | } | ||
1912 | } | ||
1913 | return null; | ||
1914 | }; | ||
1915 | d3_selectionPrototype.transition = function() { | ||
1916 | var subgroups = [], | ||
1917 | subgroup, | ||
1918 | node; | ||
1919 | |||
1920 | for (var j = -1, m = this.length; ++j < m;) { | ||
1921 | subgroups.push(subgroup = []); | ||
1922 | for (var group = this[j], i = -1, n = group.length; ++i < n;) { | ||
1923 | subgroup.push((node = group[i]) ? {node: node, delay: 0, duration: 250} : null); | ||
1924 | } | ||
1925 | } | ||
1926 | |||
1927 | return d3_transition(subgroups, d3_transitionInheritId || ++d3_transitionId, Date.now()); | ||
1928 | }; | ||
1929 | var d3_selectionRoot = d3_selection([[document]]); | ||
1930 | |||
1931 | d3_selectionRoot[0].parentNode = d3_selectRoot; | ||
1932 | |||
1933 | // TODO fast singleton implementation! | ||
1934 | // TODO select(function) | ||
1935 | d3.select = function(selector) { | ||
1936 | return typeof selector === "string" | ||
1937 | ? d3_selectionRoot.select(selector) | ||
1938 | : d3_selection([[selector]]); // assume node | ||
1939 | }; | ||
1940 | |||
1941 | // TODO selectAll(function) | ||
1942 | d3.selectAll = function(selector) { | ||
1943 | return typeof selector === "string" | ||
1944 | ? d3_selectionRoot.selectAll(selector) | ||
1945 | : d3_selection([d3_array(selector)]); // assume node[] | ||
1946 | }; | ||
1947 | function d3_selection_enter(selection) { | ||
1948 | d3_arraySubclass(selection, d3_selection_enterPrototype); | ||
1949 | return selection; | ||
1950 | } | ||
1951 | |||
1952 | var d3_selection_enterPrototype = []; | ||
1953 | |||
1954 | d3_selection_enterPrototype.append = d3_selectionPrototype.append; | ||
1955 | d3_selection_enterPrototype.insert = d3_selectionPrototype.insert; | ||
1956 | d3_selection_enterPrototype.empty = d3_selectionPrototype.empty; | ||
1957 | d3_selection_enterPrototype.node = d3_selectionPrototype.node; | ||
1958 | d3_selection_enterPrototype.select = function(selector) { | ||
1959 | var subgroups = [], | ||
1960 | subgroup, | ||
1961 | subnode, | ||
1962 | upgroup, | ||
1963 | group, | ||
1964 | node; | ||
1965 | |||
1966 | for (var j = -1, m = this.length; ++j < m;) { | ||
1967 | upgroup = (group = this[j]).update; | ||
1968 | subgroups.push(subgroup = []); | ||
1969 | subgroup.parentNode = group.parentNode; | ||
1970 | for (var i = -1, n = group.length; ++i < n;) { | ||
1971 | if (node = group[i]) { | ||
1972 | subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i)); | ||
1973 | subnode.__data__ = node.__data__; | ||
1974 | } else { | ||
1975 | subgroup.push(null); | ||
1976 | } | ||
1977 | } | ||
1978 | } | ||
1979 | |||
1980 | return d3_selection(subgroups); | ||
1981 | }; | ||
1982 | function d3_transition(groups, id, time) { | ||
1983 | d3_arraySubclass(groups, d3_transitionPrototype); | ||
1984 | |||
1985 | var tweens = {}, | ||
1986 | event = d3.dispatch("start", "end"), | ||
1987 | ease = d3_transitionEase; | ||
1988 | |||
1989 | groups.id = id; | ||
1990 | |||
1991 | groups.time = time; | ||
1992 | |||
1993 | groups.tween = function(name, tween) { | ||
1994 | if (arguments.length < 2) return tweens[name]; | ||
1995 | if (tween == null) delete tweens[name]; | ||
1996 | else tweens[name] = tween; | ||
1997 | return groups; | ||
1998 | }; | ||
1999 | |||
2000 | groups.ease = function(value) { | ||
2001 | if (!arguments.length) return ease; | ||
2002 | ease = typeof value === "function" ? value : d3.ease.apply(d3, arguments); | ||
2003 | return groups; | ||
2004 | }; | ||
2005 | |||
2006 | groups.each = function(type, listener) { | ||
2007 | if (arguments.length < 2) return d3_transition_each.call(groups, type); | ||
2008 | event.on(type, listener); | ||
2009 | return groups; | ||
2010 | }; | ||
2011 | |||
2012 | d3.timer(function(elapsed) { | ||
2013 | groups.each(function(d, i, j) { | ||
2014 | var tweened = [], | ||
2015 | node = this, | ||
2016 | delay = groups[j][i].delay, | ||
2017 | duration = groups[j][i].duration, | ||
2018 | lock = node.__transition__ || (node.__transition__ = {active: 0, count: 0}); | ||
2019 | |||
2020 | ++lock.count; | ||
2021 | |||
2022 | delay <= elapsed ? start(elapsed) : d3.timer(start, delay, time); | ||
2023 | |||
2024 | function start(elapsed) { | ||
2025 | if (lock.active > id) return stop(); | ||
2026 | lock.active = id; | ||
2027 | |||
2028 | for (var tween in tweens) { | ||
2029 | if (tween = tweens[tween].call(node, d, i)) { | ||
2030 | tweened.push(tween); | ||
2031 | } | ||
2032 | } | ||
2033 | |||
2034 | event.start.call(node, d, i); | ||
2035 | if (!tick(elapsed)) d3.timer(tick, 0, time); | ||
2036 | return 1; | ||
2037 | } | ||
2038 | |||
2039 | function tick(elapsed) { | ||
2040 | if (lock.active !== id) return stop(); | ||
2041 | |||
2042 | var t = (elapsed - delay) / duration, | ||
2043 | e = ease(t), | ||
2044 | n = tweened.length; | ||
2045 | |||
2046 | while (n > 0) { | ||
2047 | tweened[--n].call(node, e); | ||
2048 | } | ||
2049 | |||
2050 | if (t >= 1) { | ||
2051 | stop(); | ||
2052 | d3_transitionInheritId = id; | ||
2053 | event.end.call(node, d, i); | ||
2054 | d3_transitionInheritId = 0; | ||
2055 | return 1; | ||
2056 | } | ||
2057 | } | ||
2058 | |||
2059 | function stop() { | ||
2060 | if (!--lock.count) delete node.__transition__; | ||
2061 | return 1; | ||
2062 | } | ||
2063 | }); | ||
2064 | return 1; | ||
2065 | }, 0, time); | ||
2066 | |||
2067 | return groups; | ||
2068 | } | ||
2069 | |||
2070 | var d3_transitionRemove = {}; | ||
2071 | |||
2072 | function d3_transitionNull(d, i, a) { | ||
2073 | return a != "" && d3_transitionRemove; | ||
2074 | } | ||
2075 | |||
2076 | function d3_transitionTween(name, b) { | ||
2077 | var interpolate = d3_interpolateByName(name); | ||
2078 | |||
2079 | function transitionFunction(d, i, a) { | ||
2080 | var v = b.call(this, d, i); | ||
2081 | return v == null | ||
2082 | ? a != "" && d3_transitionRemove | ||
2083 | : a != v && interpolate(a, v); | ||
2084 | } | ||
2085 | |||
2086 | function transitionString(d, i, a) { | ||
2087 | return a != b && interpolate(a, b); | ||
2088 | } | ||
2089 | |||
2090 | return typeof b === "function" ? transitionFunction | ||
2091 | : b == null ? d3_transitionNull | ||
2092 | : (b += "", transitionString); | ||
2093 | } | ||
2094 | |||
2095 | var d3_transitionPrototype = [], | ||
2096 | d3_transitionId = 0, | ||
2097 | d3_transitionInheritId = 0, | ||
2098 | d3_transitionEase = d3.ease("cubic-in-out"); | ||
2099 | |||
2100 | d3_transitionPrototype.call = d3_selectionPrototype.call; | ||
2101 | |||
2102 | d3.transition = function() { | ||
2103 | return d3_selectionRoot.transition(); | ||
2104 | }; | ||
2105 | |||
2106 | d3.transition.prototype = d3_transitionPrototype; | ||
2107 | d3_transitionPrototype.select = function(selector) { | ||
2108 | var subgroups = [], | ||
2109 | subgroup, | ||
2110 | subnode, | ||
2111 | node; | ||
2112 | |||
2113 | if (typeof selector !== "function") selector = d3_selection_selector(selector); | ||
2114 | |||
2115 | for (var j = -1, m = this.length; ++j < m;) { | ||
2116 | subgroups.push(subgroup = []); | ||
2117 | for (var group = this[j], i = -1, n = group.length; ++i < n;) { | ||
2118 | if ((node = group[i]) && (subnode = selector.call(node.node, node.node.__data__, i))) { | ||
2119 | if ("__data__" in node.node) subnode.__data__ = node.node.__data__; | ||
2120 | subgroup.push({node: subnode, delay: node.delay, duration: node.duration}); | ||
2121 | } else { | ||
2122 | subgroup.push(null); | ||
2123 | } | ||
2124 | } | ||
2125 | } | ||
2126 | |||
2127 | return d3_transition(subgroups, this.id, this.time).ease(this.ease()); | ||
2128 | }; | ||
2129 | d3_transitionPrototype.selectAll = function(selector) { | ||
2130 | var subgroups = [], | ||
2131 | subgroup, | ||
2132 | subnodes, | ||
2133 | node; | ||
2134 | |||
2135 | if (typeof selector !== "function") selector = d3_selection_selectorAll(selector); | ||
2136 | |||
2137 | for (var j = -1, m = this.length; ++j < m;) { | ||
2138 | for (var group = this[j], i = -1, n = group.length; ++i < n;) { | ||
2139 | if (node = group[i]) { | ||
2140 | subnodes = selector.call(node.node, node.node.__data__, i); | ||
2141 | subgroups.push(subgroup = []); | ||
2142 | for (var k = -1, o = subnodes.length; ++k < o;) { | ||
2143 | subgroup.push({node: subnodes[k], delay: node.delay, duration: node.duration}); | ||
2144 | } | ||
2145 | } | ||
2146 | } | ||
2147 | } | ||
2148 | |||
2149 | return d3_transition(subgroups, this.id, this.time).ease(this.ease()); | ||
2150 | }; | ||
2151 | d3_transitionPrototype.attr = function(name, value) { | ||
2152 | return this.attrTween(name, d3_transitionTween(name, value)); | ||
2153 | }; | ||
2154 | |||
2155 | d3_transitionPrototype.attrTween = function(nameNS, tween) { | ||
2156 | var name = d3.ns.qualify(nameNS); | ||
2157 | |||
2158 | function attrTween(d, i) { | ||
2159 | var f = tween.call(this, d, i, this.getAttribute(name)); | ||
2160 | return f === d3_transitionRemove | ||
2161 | ? (this.removeAttribute(name), null) | ||
2162 | : f && function(t) { this.setAttribute(name, f(t)); }; | ||
2163 | } | ||
2164 | |||
2165 | function attrTweenNS(d, i) { | ||
2166 | var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local)); | ||
2167 | return f === d3_transitionRemove | ||
2168 | ? (this.removeAttributeNS(name.space, name.local), null) | ||
2169 | : f && function(t) { this.setAttributeNS(name.space, name.local, f(t)); }; | ||
2170 | } | ||
2171 | |||
2172 | return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween); | ||
2173 | }; | ||
2174 | d3_transitionPrototype.style = function(name, value, priority) { | ||
2175 | if (arguments.length < 3) priority = ""; | ||
2176 | return this.styleTween(name, d3_transitionTween(name, value), priority); | ||
2177 | }; | ||
2178 | |||
2179 | d3_transitionPrototype.styleTween = function(name, tween, priority) { | ||
2180 | if (arguments.length < 3) priority = ""; | ||
2181 | return this.tween("style." + name, function(d, i) { | ||
2182 | var f = tween.call(this, d, i, window.getComputedStyle(this, null).getPropertyValue(name)); | ||
2183 | return f === d3_transitionRemove | ||
2184 | ? (this.style.removeProperty(name), null) | ||
2185 | : f && function(t) { this.style.setProperty(name, f(t), priority); }; | ||
2186 | }); | ||
2187 | }; | ||
2188 | d3_transitionPrototype.text = function(value) { | ||
2189 | return this.tween("text", function(d, i) { | ||
2190 | this.textContent = typeof value === "function" | ||
2191 | ? value.call(this, d, i) | ||
2192 | : value; | ||
2193 | }); | ||
2194 | }; | ||
2195 | d3_transitionPrototype.remove = function() { | ||
2196 | return this.each("end.transition", function() { | ||
2197 | var p; | ||
2198 | if (!this.__transition__ && (p = this.parentNode)) p.removeChild(this); | ||
2199 | }); | ||
2200 | }; | ||
2201 | d3_transitionPrototype.delay = function(value) { | ||
2202 | var groups = this; | ||
2203 | return groups.each(typeof value === "function" | ||
2204 | ? function(d, i, j) { groups[j][i].delay = +value.apply(this, arguments); } | ||
2205 | : (value = +value, function(d, i, j) { groups[j][i].delay = value; })); | ||
2206 | }; | ||
2207 | d3_transitionPrototype.duration = function(value) { | ||
2208 | var groups = this; | ||
2209 | return groups.each(typeof value === "function" | ||
2210 | ? function(d, i, j) { groups[j][i].duration = +value.apply(this, arguments); } | ||
2211 | : (value = +value, function(d, i, j) { groups[j][i].duration = value; })); | ||
2212 | }; | ||
2213 | function d3_transition_each(callback) { | ||
2214 | for (var j = 0, m = this.length; j < m; j++) { | ||
2215 | for (var group = this[j], i = 0, n = group.length; i < n; i++) { | ||
2216 | var node = group[i]; | ||
2217 | if (node) callback.call(node = node.node, node.__data__, i, j); | ||
2218 | } | ||
2219 | } | ||
2220 | return this; | ||
2221 | } | ||
2222 | d3_transitionPrototype.transition = function() { | ||
2223 | return this.select(d3_this); | ||
2224 | }; | ||
2225 | var d3_timer_queue = null, | ||
2226 | d3_timer_interval, // is an interval (or frame) active? | ||
2227 | d3_timer_timeout; // is a timeout active? | ||
2228 | |||
2229 | // The timer will continue to fire until callback returns true. | ||
2230 | d3.timer = function(callback, delay, then) { | ||
2231 | var found = false, | ||
2232 | t0, | ||
2233 | t1 = d3_timer_queue; | ||
2234 | |||
2235 | if (arguments.length < 3) { | ||
2236 | if (arguments.length < 2) delay = 0; | ||
2237 | else if (!isFinite(delay)) return; | ||
2238 | then = Date.now(); | ||
2239 | } | ||
2240 | |||
2241 | // See if the callback's already in the queue. | ||
2242 | while (t1) { | ||
2243 | if (t1.callback === callback) { | ||
2244 | t1.then = then; | ||
2245 | t1.delay = delay; | ||
2246 | found = true; | ||
2247 | break; | ||
2248 | } | ||
2249 | t0 = t1; | ||
2250 | t1 = t1.next; | ||
2251 | } | ||
2252 | |||
2253 | // Otherwise, add the callback to the queue. | ||
2254 | if (!found) d3_timer_queue = { | ||
2255 | callback: callback, | ||
2256 | then: then, | ||
2257 | delay: delay, | ||
2258 | next: d3_timer_queue | ||
2259 | }; | ||
2260 | |||
2261 | // Start animatin'! | ||
2262 | if (!d3_timer_interval) { | ||
2263 | d3_timer_timeout = clearTimeout(d3_timer_timeout); | ||
2264 | d3_timer_interval = 1; | ||
2265 | d3_timer_frame(d3_timer_step); | ||
2266 | } | ||
2267 | } | ||
2268 | |||
2269 | function d3_timer_step() { | ||
2270 | var elapsed, | ||
2271 | now = Date.now(), | ||
2272 | t1 = d3_timer_queue; | ||
2273 | |||
2274 | while (t1) { | ||
2275 | elapsed = now - t1.then; | ||
2276 | if (elapsed >= t1.delay) t1.flush = t1.callback(elapsed); | ||
2277 | t1 = t1.next; | ||
2278 | } | ||
2279 | |||
2280 | var delay = d3_timer_flush() - now; | ||
2281 | if (delay > 24) { | ||
2282 | if (isFinite(delay)) { | ||
2283 | clearTimeout(d3_timer_timeout); | ||
2284 | d3_timer_timeout = setTimeout(d3_timer_step, delay); | ||
2285 | } | ||
2286 | d3_timer_interval = 0; | ||
2287 | } else { | ||
2288 | d3_timer_interval = 1; | ||
2289 | d3_timer_frame(d3_timer_step); | ||
2290 | } | ||
2291 | } | ||
2292 | |||
2293 | d3.timer.flush = function() { | ||
2294 | var elapsed, | ||
2295 | now = Date.now(), | ||
2296 | t1 = d3_timer_queue; | ||
2297 | |||
2298 | while (t1) { | ||
2299 | elapsed = now - t1.then; | ||
2300 | if (!t1.delay) t1.flush = t1.callback(elapsed); | ||
2301 | t1 = t1.next; | ||
2302 | } | ||
2303 | |||
2304 | d3_timer_flush(); | ||
2305 | }; | ||
2306 | |||
2307 | // Flush after callbacks, to avoid concurrent queue modification. | ||
2308 | function d3_timer_flush() { | ||
2309 | var t0 = null, | ||
2310 | t1 = d3_timer_queue, | ||
2311 | then = Infinity; | ||
2312 | while (t1) { | ||
2313 | if (t1.flush) { | ||
2314 | t1 = t0 ? t0.next = t1.next : d3_timer_queue = t1.next; | ||
2315 | } else { | ||
2316 | then = Math.min(then, t1.then + t1.delay); | ||
2317 | t1 = (t0 = t1).next; | ||
2318 | } | ||
2319 | } | ||
2320 | return then; | ||
2321 | } | ||
2322 | |||
2323 | var d3_timer_frame = window.requestAnimationFrame | ||
2324 | || window.webkitRequestAnimationFrame | ||
2325 | || window.mozRequestAnimationFrame | ||
2326 | || window.oRequestAnimationFrame | ||
2327 | || window.msRequestAnimationFrame | ||
2328 | || function(callback) { setTimeout(callback, 17); }; | ||
2329 | d3.transform = function(string) { | ||
2330 | var g = document.createElementNS(d3.ns.prefix.svg, "g"), | ||
2331 | identity = {a: 1, b: 0, c: 0, d: 1, e: 0, f: 0}; | ||
2332 | return (d3.transform = function(string) { | ||
2333 | g.setAttribute("transform", string); | ||
2334 | var t = g.transform.baseVal.consolidate(); | ||
2335 | return new d3_transform(t ? t.matrix : identity); | ||
2336 | })(string); | ||
2337 | }; | ||
2338 | |||
2339 | // Compute x-scale and normalize the first row. | ||
2340 | // Compute shear and make second row orthogonal to first. | ||
2341 | // Compute y-scale and normalize the second row. | ||
2342 | // Finally, compute the rotation. | ||
2343 | function d3_transform(m) { | ||
2344 | var r0 = [m.a, m.b], | ||
2345 | r1 = [m.c, m.d], | ||
2346 | kx = d3_transformNormalize(r0), | ||
2347 | kz = d3_transformDot(r0, r1), | ||
2348 | ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0; | ||
2349 | if (r0[0] * r1[1] < r1[0] * r0[1]) { | ||
2350 | r0[0] *= -1; | ||
2351 | r0[1] *= -1; | ||
2352 | kx *= -1; | ||
2353 | kz *= -1; | ||
2354 | } | ||
2355 | this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_transformDegrees; | ||
2356 | this.translate = [m.e, m.f]; | ||
2357 | this.scale = [kx, ky]; | ||
2358 | this.skew = ky ? Math.atan2(kz, ky) * d3_transformDegrees : 0; | ||
2359 | }; | ||
2360 | |||
2361 | d3_transform.prototype.toString = function() { | ||
2362 | return "translate(" + this.translate | ||
2363 | + ")rotate(" + this.rotate | ||
2364 | + ")skewX(" + this.skew | ||
2365 | + ")scale(" + this.scale | ||
2366 | + ")"; | ||
2367 | }; | ||
2368 | |||
2369 | function d3_transformDot(a, b) { | ||
2370 | return a[0] * b[0] + a[1] * b[1]; | ||
2371 | } | ||
2372 | |||
2373 | function d3_transformNormalize(a) { | ||
2374 | var k = Math.sqrt(d3_transformDot(a, a)); | ||
2375 | if (k) { | ||
2376 | a[0] /= k; | ||
2377 | a[1] /= k; | ||
2378 | } | ||
2379 | return k; | ||
2380 | } | ||
2381 | |||
2382 | function d3_transformCombine(a, b, k) { | ||
2383 | a[0] += k * b[0]; | ||
2384 | a[1] += k * b[1]; | ||
2385 | return a; | ||
2386 | } | ||
2387 | |||
2388 | var d3_transformDegrees = 180 / Math.PI; | ||
2389 | function d3_noop() {} | ||
2390 | d3.scale = {}; | ||
2391 | |||
2392 | function d3_scaleExtent(domain) { | ||
2393 | var start = domain[0], stop = domain[domain.length - 1]; | ||
2394 | return start < stop ? [start, stop] : [stop, start]; | ||
2395 | } | ||
2396 | |||
2397 | function d3_scaleRange(scale) { | ||
2398 | return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range()); | ||
2399 | } | ||
2400 | function d3_scale_nice(domain, nice) { | ||
2401 | var i0 = 0, | ||
2402 | i1 = domain.length - 1, | ||
2403 | x0 = domain[i0], | ||
2404 | x1 = domain[i1], | ||
2405 | dx; | ||
2406 | |||
2407 | if (x1 < x0) { | ||
2408 | dx = i0; i0 = i1; i1 = dx; | ||
2409 | dx = x0; x0 = x1; x1 = dx; | ||
2410 | } | ||
2411 | |||
2412 | if (dx = x1 - x0) { | ||
2413 | nice = nice(dx); | ||
2414 | domain[i0] = nice.floor(x0); | ||
2415 | domain[i1] = nice.ceil(x1); | ||
2416 | } | ||
2417 | |||
2418 | return domain; | ||
2419 | } | ||
2420 | |||
2421 | function d3_scale_niceDefault() { | ||
2422 | return Math; | ||
2423 | } | ||
2424 | d3.scale.linear = function() { | ||
2425 | return d3_scale_linear([0, 1], [0, 1], d3.interpolate, false); | ||
2426 | }; | ||
2427 | |||
2428 | function d3_scale_linear(domain, range, interpolate, clamp) { | ||
2429 | var output, | ||
2430 | input; | ||
2431 | |||
2432 | function rescale() { | ||
2433 | var linear = domain.length == 2 ? d3_scale_bilinear : d3_scale_polylinear, | ||
2434 | uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber; | ||
2435 | output = linear(domain, range, uninterpolate, interpolate); | ||
2436 | input = linear(range, domain, uninterpolate, d3.interpolate); | ||
2437 | return scale; | ||
2438 | } | ||
2439 | |||
2440 | function scale(x) { | ||
2441 | return output(x); | ||
2442 | } | ||
2443 | |||
2444 | // Note: requires range is coercible to number! | ||
2445 | scale.invert = function(y) { | ||
2446 | return input(y); | ||
2447 | }; | ||
2448 | |||
2449 | scale.domain = function(x) { | ||
2450 | if (!arguments.length) return domain; | ||
2451 | domain = x.map(Number); | ||
2452 | return rescale(); | ||
2453 | }; | ||
2454 | |||
2455 | scale.range = function(x) { | ||
2456 | if (!arguments.length) return range; | ||
2457 | range = x; | ||
2458 | return rescale(); | ||
2459 | }; | ||
2460 | |||
2461 | scale.rangeRound = function(x) { | ||
2462 | return scale.range(x).interpolate(d3.interpolateRound); | ||
2463 | }; | ||
2464 | |||
2465 | scale.clamp = function(x) { | ||
2466 | if (!arguments.length) return clamp; | ||
2467 | clamp = x; | ||
2468 | return rescale(); | ||
2469 | }; | ||
2470 | |||
2471 | scale.interpolate = function(x) { | ||
2472 | if (!arguments.length) return interpolate; | ||
2473 | interpolate = x; | ||
2474 | return rescale(); | ||
2475 | }; | ||
2476 | |||
2477 | scale.ticks = function(m) { | ||
2478 | return d3_scale_linearTicks(domain, m); | ||
2479 | }; | ||
2480 | |||
2481 | scale.tickFormat = function(m) { | ||
2482 | return d3_scale_linearTickFormat(domain, m); | ||
2483 | }; | ||
2484 | |||
2485 | scale.nice = function() { | ||
2486 | d3_scale_nice(domain, d3_scale_linearNice); | ||
2487 | return rescale(); | ||
2488 | }; | ||
2489 | |||
2490 | scale.copy = function() { | ||
2491 | return d3_scale_linear(domain, range, interpolate, clamp); | ||
2492 | }; | ||
2493 | |||
2494 | return rescale(); | ||
2495 | }; | ||
2496 | |||
2497 | function d3_scale_linearRebind(scale, linear) { | ||
2498 | return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp"); | ||
2499 | } | ||
2500 | |||
2501 | function d3_scale_linearNice(dx) { | ||
2502 | dx = Math.pow(10, Math.round(Math.log(dx) / Math.LN10) - 1); | ||
2503 | return { | ||
2504 | floor: function(x) { return Math.floor(x / dx) * dx; }, | ||
2505 | ceil: function(x) { return Math.ceil(x / dx) * dx; } | ||
2506 | }; | ||
2507 | } | ||
2508 | |||
2509 | function d3_scale_linearTickRange(domain, m) { | ||
2510 | var extent = d3_scaleExtent(domain), | ||
2511 | span = extent[1] - extent[0], | ||
2512 | step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)), | ||
2513 | err = m / span * step; | ||
2514 | |||
2515 | // Filter ticks to get closer to the desired count. | ||
2516 | if (err <= .15) step *= 10; | ||
2517 | else if (err <= .35) step *= 5; | ||
2518 | else if (err <= .75) step *= 2; | ||
2519 | |||
2520 | // Round start and stop values to step interval. | ||
2521 | extent[0] = Math.ceil(extent[0] / step) * step; | ||
2522 | extent[1] = Math.floor(extent[1] / step) * step + step * .5; // inclusive | ||
2523 | extent[2] = step; | ||
2524 | return extent; | ||
2525 | } | ||
2526 | |||
2527 | function d3_scale_linearTicks(domain, m) { | ||
2528 | return d3.range.apply(d3, d3_scale_linearTickRange(domain, m)); | ||
2529 | } | ||
2530 | |||
2531 | function d3_scale_linearTickFormat(domain, m) { | ||
2532 | return d3.format(",." + Math.max(0, -Math.floor(Math.log(d3_scale_linearTickRange(domain, m)[2]) / Math.LN10 + .01)) + "f"); | ||
2533 | } | ||
2534 | function d3_scale_bilinear(domain, range, uninterpolate, interpolate) { | ||
2535 | var u = uninterpolate(domain[0], domain[1]), | ||
2536 | i = interpolate(range[0], range[1]); | ||
2537 | return function(x) { | ||
2538 | return i(u(x)); | ||
2539 | }; | ||
2540 | } | ||
2541 | function d3_scale_polylinear(domain, range, uninterpolate, interpolate) { | ||
2542 | var u = [], | ||
2543 | i = [], | ||
2544 | j = 0, | ||
2545 | k = domain.length - 1; | ||
2546 | |||
2547 | // Handle descending domains. | ||
2548 | if (domain[k] < domain[0]) { | ||
2549 | domain = domain.slice().reverse(); | ||
2550 | range = range.slice().reverse(); | ||
2551 | } | ||
2552 | |||
2553 | while (++j <= k) { | ||
2554 | u.push(uninterpolate(domain[j - 1], domain[j])); | ||
2555 | i.push(interpolate(range[j - 1], range[j])); | ||
2556 | } | ||
2557 | |||
2558 | return function(x) { | ||
2559 | var j = d3.bisect(domain, x, 1, k) - 1; | ||
2560 | return i[j](u[j](x)); | ||
2561 | }; | ||
2562 | } | ||
2563 | d3.scale.log = function() { | ||
2564 | return d3_scale_log(d3.scale.linear(), d3_scale_logp); | ||
2565 | }; | ||
2566 | |||
2567 | function d3_scale_log(linear, log) { | ||
2568 | var pow = log.pow; | ||
2569 | |||
2570 | function scale(x) { | ||
2571 | return linear(log(x)); | ||
2572 | } | ||
2573 | |||
2574 | scale.invert = function(x) { | ||
2575 | return pow(linear.invert(x)); | ||
2576 | }; | ||
2577 | |||
2578 | scale.domain = function(x) { | ||
2579 | if (!arguments.length) return linear.domain().map(pow); | ||
2580 | log = x[0] < 0 ? d3_scale_logn : d3_scale_logp; | ||
2581 | pow = log.pow; | ||
2582 | linear.domain(x.map(log)); | ||
2583 | return scale; | ||
2584 | }; | ||
2585 | |||
2586 | scale.nice = function() { | ||
2587 | linear.domain(d3_scale_nice(linear.domain(), d3_scale_niceDefault)); | ||
2588 | return scale; | ||
2589 | }; | ||
2590 | |||
2591 | scale.ticks = function() { | ||
2592 | var extent = d3_scaleExtent(linear.domain()), | ||
2593 | ticks = []; | ||
2594 | if (extent.every(isFinite)) { | ||
2595 | var i = Math.floor(extent[0]), | ||
2596 | j = Math.ceil(extent[1]), | ||
2597 | u = pow(extent[0]), | ||
2598 | v = pow(extent[1]); | ||
2599 | if (log === d3_scale_logn) { | ||
2600 | ticks.push(pow(i)); | ||
2601 | for (; i++ < j;) for (var k = 9; k > 0; k--) ticks.push(pow(i) * k); | ||
2602 | } else { | ||
2603 | for (; i < j; i++) for (var k = 1; k < 10; k++) ticks.push(pow(i) * k); | ||
2604 | ticks.push(pow(i)); | ||
2605 | } | ||
2606 | for (i = 0; ticks[i] < u; i++) {} // strip small values | ||
2607 | for (j = ticks.length; ticks[j - 1] > v; j--) {} // strip big values | ||
2608 | ticks = ticks.slice(i, j); | ||
2609 | } | ||
2610 | return ticks; | ||
2611 | }; | ||
2612 | |||
2613 | scale.tickFormat = function(n, format) { | ||
2614 | if (arguments.length < 2) format = d3_scale_logFormat; | ||
2615 | if (arguments.length < 1) return format; | ||
2616 | var k = n / scale.ticks().length, | ||
2617 | f = log === d3_scale_logn ? (e = -1e-12, Math.floor) : (e = 1e-12, Math.ceil), | ||
2618 | e; | ||
2619 | return function(d) { | ||
2620 | return d / pow(f(log(d) + e)) < k ? format(d) : ""; | ||
2621 | }; | ||
2622 | }; | ||
2623 | |||
2624 | scale.copy = function() { | ||
2625 | return d3_scale_log(linear.copy(), log); | ||
2626 | }; | ||
2627 | |||
2628 | return d3_scale_linearRebind(scale, linear); | ||
2629 | }; | ||
2630 | |||
2631 | var d3_scale_logFormat = d3.format(".0e"); | ||
2632 | |||
2633 | function d3_scale_logp(x) { | ||
2634 | return Math.log(x < 0 ? 0 : x) / Math.LN10; | ||
2635 | } | ||
2636 | |||
2637 | function d3_scale_logn(x) { | ||
2638 | return -Math.log(x > 0 ? 0 : -x) / Math.LN10; | ||
2639 | } | ||
2640 | |||
2641 | d3_scale_logp.pow = function(x) { | ||
2642 | return Math.pow(10, x); | ||
2643 | }; | ||
2644 | |||
2645 | d3_scale_logn.pow = function(x) { | ||
2646 | return -Math.pow(10, -x); | ||
2647 | }; | ||
2648 | d3.scale.pow = function() { | ||
2649 | return d3_scale_pow(d3.scale.linear(), 1); | ||
2650 | }; | ||
2651 | |||
2652 | function d3_scale_pow(linear, exponent) { | ||
2653 | var powp = d3_scale_powPow(exponent), | ||
2654 | powb = d3_scale_powPow(1 / exponent); | ||
2655 | |||
2656 | function scale(x) { | ||
2657 | return linear(powp(x)); | ||
2658 | } | ||
2659 | |||
2660 | scale.invert = function(x) { | ||
2661 | return powb(linear.invert(x)); | ||
2662 | }; | ||
2663 | |||
2664 | scale.domain = function(x) { | ||
2665 | if (!arguments.length) return linear.domain().map(powb); | ||
2666 | linear.domain(x.map(powp)); | ||
2667 | return scale; | ||
2668 | }; | ||
2669 | |||
2670 | scale.ticks = function(m) { | ||
2671 | return d3_scale_linearTicks(scale.domain(), m); | ||
2672 | }; | ||
2673 | |||
2674 | scale.tickFormat = function(m) { | ||
2675 | return d3_scale_linearTickFormat(scale.domain(), m); | ||
2676 | }; | ||
2677 | |||
2678 | scale.nice = function() { | ||
2679 | return scale.domain(d3_scale_nice(scale.domain(), d3_scale_linearNice)); | ||
2680 | }; | ||
2681 | |||
2682 | scale.exponent = function(x) { | ||
2683 | if (!arguments.length) return exponent; | ||
2684 | var domain = scale.domain(); | ||
2685 | powp = d3_scale_powPow(exponent = x); | ||
2686 | powb = d3_scale_powPow(1 / exponent); | ||
2687 | return scale.domain(domain); | ||
2688 | }; | ||
2689 | |||
2690 | scale.copy = function() { | ||
2691 | return d3_scale_pow(linear.copy(), exponent); | ||
2692 | }; | ||
2693 | |||
2694 | return d3_scale_linearRebind(scale, linear); | ||
2695 | }; | ||
2696 | |||
2697 | function d3_scale_powPow(e) { | ||
2698 | return function(x) { | ||
2699 | return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e); | ||
2700 | }; | ||
2701 | } | ||
2702 | d3.scale.sqrt = function() { | ||
2703 | return d3.scale.pow().exponent(.5); | ||
2704 | }; | ||
2705 | d3.scale.ordinal = function() { | ||
2706 | return d3_scale_ordinal([], {t: "range", x: []}); | ||
2707 | }; | ||
2708 | |||
2709 | function d3_scale_ordinal(domain, ranger) { | ||
2710 | var index, | ||
2711 | range, | ||
2712 | rangeBand; | ||
2713 | |||
2714 | function scale(x) { | ||
2715 | return range[((index[x] || (index[x] = domain.push(x))) - 1) % range.length]; | ||
2716 | } | ||
2717 | |||
2718 | function steps(start, step) { | ||
2719 | return d3.range(domain.length).map(function(i) { return start + step * i; }); | ||
2720 | } | ||
2721 | |||
2722 | scale.domain = function(x) { | ||
2723 | if (!arguments.length) return domain; | ||
2724 | domain = []; | ||
2725 | index = {}; | ||
2726 | var i = -1, n = x.length, xi; | ||
2727 | while (++i < n) if (!index[xi = x[i]]) index[xi] = domain.push(xi); | ||
2728 | return scale[ranger.t](ranger.x, ranger.p); | ||
2729 | }; | ||
2730 | |||
2731 | scale.range = function(x) { | ||
2732 | if (!arguments.length) return range; | ||
2733 | range = x; | ||
2734 | rangeBand = 0; | ||
2735 | ranger = {t: "range", x: x}; | ||
2736 | return scale; | ||
2737 | }; | ||
2738 | |||
2739 | scale.rangePoints = function(x, padding) { | ||
2740 | if (arguments.length < 2) padding = 0; | ||
2741 | var start = x[0], | ||
2742 | stop = x[1], | ||
2743 | step = (stop - start) / (domain.length - 1 + padding); | ||
2744 | range = steps(domain.length < 2 ? (start + stop) / 2 : start + step * padding / 2, step); | ||
2745 | rangeBand = 0; | ||
2746 | ranger = {t: "rangePoints", x: x, p: padding}; | ||
2747 | return scale; | ||
2748 | }; | ||
2749 | |||
2750 | scale.rangeBands = function(x, padding) { | ||
2751 | if (arguments.length < 2) padding = 0; | ||
2752 | var start = x[0], | ||
2753 | stop = x[1], | ||
2754 | step = (stop - start) / (domain.length + padding); | ||
2755 | range = steps(start + step * padding, step); | ||
2756 | rangeBand = step * (1 - padding); | ||
2757 | ranger = {t: "rangeBands", x: x, p: padding}; | ||
2758 | return scale; | ||
2759 | }; | ||
2760 | |||
2761 | scale.rangeRoundBands = function(x, padding) { | ||
2762 | if (arguments.length < 2) padding = 0; | ||
2763 | var start = x[0], | ||
2764 | stop = x[1], | ||
2765 | step = Math.floor((stop - start) / (domain.length + padding)); | ||
2766 | range = steps(start + Math.round((stop - start - (domain.length - padding) * step) / 2), step); | ||
2767 | rangeBand = Math.round(step * (1 - padding)); | ||
2768 | ranger = {t: "rangeRoundBands", x: x, p: padding}; | ||
2769 | return scale; | ||
2770 | }; | ||
2771 | |||
2772 | scale.rangeBand = function() { | ||
2773 | return rangeBand; | ||
2774 | }; | ||
2775 | |||
2776 | scale.rangeExtent = function() { | ||
2777 | return ranger.t === "range" ? d3_scaleExtent(ranger.x) : ranger.x; | ||
2778 | }; | ||
2779 | |||
2780 | scale.copy = function() { | ||
2781 | return d3_scale_ordinal(domain, ranger); | ||
2782 | }; | ||
2783 | |||
2784 | return scale.domain(domain); | ||
2785 | }; | ||
2786 | /* | ||
2787 | * This product includes color specifications and designs developed by Cynthia | ||
2788 | * Brewer (http://colorbrewer.org/). See lib/colorbrewer for more information. | ||
2789 | */ | ||
2790 | |||
2791 | d3.scale.category10 = function() { | ||
2792 | return d3.scale.ordinal().range(d3_category10); | ||
2793 | }; | ||
2794 | |||
2795 | d3.scale.category20 = function() { | ||
2796 | return d3.scale.ordinal().range(d3_category20); | ||
2797 | }; | ||
2798 | |||
2799 | d3.scale.category20b = function() { | ||
2800 | return d3.scale.ordinal().range(d3_category20b); | ||
2801 | }; | ||
2802 | |||
2803 | d3.scale.category20c = function() { | ||
2804 | return d3.scale.ordinal().range(d3_category20c); | ||
2805 | }; | ||
2806 | |||
2807 | var d3_category10 = [ | ||
2808 | "#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", | ||
2809 | "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf" | ||
2810 | ]; | ||
2811 | |||
2812 | var d3_category20 = [ | ||
2813 | "#1f77b4", "#aec7e8", | ||
2814 | "#ff7f0e", "#ffbb78", | ||
2815 | "#2ca02c", "#98df8a", | ||
2816 | "#d62728", "#ff9896", | ||
2817 | "#9467bd", "#c5b0d5", | ||
2818 | "#8c564b", "#c49c94", | ||
2819 | "#e377c2", "#f7b6d2", | ||
2820 | "#7f7f7f", "#c7c7c7", | ||
2821 | "#bcbd22", "#dbdb8d", | ||
2822 | "#17becf", "#9edae5" | ||
2823 | ]; | ||
2824 | |||
2825 | var d3_category20b = [ | ||
2826 | "#393b79", "#5254a3", "#6b6ecf", "#9c9ede", | ||
2827 | "#637939", "#8ca252", "#b5cf6b", "#cedb9c", | ||
2828 | "#8c6d31", "#bd9e39", "#e7ba52", "#e7cb94", | ||
2829 | "#843c39", "#ad494a", "#d6616b", "#e7969c", | ||
2830 | "#7b4173", "#a55194", "#ce6dbd", "#de9ed6" | ||
2831 | ]; | ||
2832 | |||
2833 | var d3_category20c = [ | ||
2834 | "#3182bd", "#6baed6", "#9ecae1", "#c6dbef", | ||
2835 | "#e6550d", "#fd8d3c", "#fdae6b", "#fdd0a2", | ||
2836 | "#31a354", "#74c476", "#a1d99b", "#c7e9c0", | ||
2837 | "#756bb1", "#9e9ac8", "#bcbddc", "#dadaeb", | ||
2838 | "#636363", "#969696", "#bdbdbd", "#d9d9d9" | ||
2839 | ]; | ||
2840 | d3.scale.quantile = function() { | ||
2841 | return d3_scale_quantile([], []); | ||
2842 | }; | ||
2843 | |||
2844 | function d3_scale_quantile(domain, range) { | ||
2845 | var thresholds; | ||
2846 | |||
2847 | function rescale() { | ||
2848 | var k = 0, | ||
2849 | n = domain.length, | ||
2850 | q = range.length; | ||
2851 | thresholds = []; | ||
2852 | while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q); | ||
2853 | return scale; | ||
2854 | } | ||
2855 | |||
2856 | function scale(x) { | ||
2857 | if (isNaN(x = +x)) return NaN; | ||
2858 | return range[d3.bisect(thresholds, x)]; | ||
2859 | } | ||
2860 | |||
2861 | scale.domain = function(x) { | ||
2862 | if (!arguments.length) return domain; | ||
2863 | domain = x.filter(function(d) { return !isNaN(d); }).sort(d3.ascending); | ||
2864 | return rescale(); | ||
2865 | }; | ||
2866 | |||
2867 | scale.range = function(x) { | ||
2868 | if (!arguments.length) return range; | ||
2869 | range = x; | ||
2870 | return rescale(); | ||
2871 | }; | ||
2872 | |||
2873 | scale.quantiles = function() { | ||
2874 | return thresholds; | ||
2875 | }; | ||
2876 | |||
2877 | scale.copy = function() { | ||
2878 | return d3_scale_quantile(domain, range); // copy on write! | ||
2879 | }; | ||
2880 | |||
2881 | return rescale(); | ||
2882 | }; | ||
2883 | d3.scale.quantize = function() { | ||
2884 | return d3_scale_quantize(0, 1, [0, 1]); | ||
2885 | }; | ||
2886 | |||
2887 | function d3_scale_quantize(x0, x1, range) { | ||
2888 | var kx, i; | ||
2889 | |||
2890 | function scale(x) { | ||
2891 | return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))]; | ||
2892 | } | ||
2893 | |||
2894 | function rescale() { | ||
2895 | kx = range.length / (x1 - x0); | ||
2896 | i = range.length - 1; | ||
2897 | return scale; | ||
2898 | } | ||
2899 | |||
2900 | scale.domain = function(x) { | ||
2901 | if (!arguments.length) return [x0, x1]; | ||
2902 | x0 = +x[0]; | ||
2903 | x1 = +x[x.length - 1]; | ||
2904 | return rescale(); | ||
2905 | }; | ||
2906 | |||
2907 | scale.range = function(x) { | ||
2908 | if (!arguments.length) return range; | ||
2909 | range = x; | ||
2910 | return rescale(); | ||
2911 | }; | ||
2912 | |||
2913 | scale.copy = function() { | ||
2914 | return d3_scale_quantize(x0, x1, range); // copy on write | ||
2915 | }; | ||
2916 | |||
2917 | return rescale(); | ||
2918 | }; | ||
2919 | d3.svg = {}; | ||
2920 | d3.svg.arc = function() { | ||
2921 | var innerRadius = d3_svg_arcInnerRadius, | ||
2922 | outerRadius = d3_svg_arcOuterRadius, | ||
2923 | startAngle = d3_svg_arcStartAngle, | ||
2924 | endAngle = d3_svg_arcEndAngle; | ||
2925 | |||
2926 | function arc() { | ||
2927 | var r0 = innerRadius.apply(this, arguments), | ||
2928 | r1 = outerRadius.apply(this, arguments), | ||
2929 | a0 = startAngle.apply(this, arguments) + d3_svg_arcOffset, | ||
2930 | a1 = endAngle.apply(this, arguments) + d3_svg_arcOffset, | ||
2931 | da = (a1 < a0 && (da = a0, a0 = a1, a1 = da), a1 - a0), | ||
2932 | df = da < Math.PI ? "0" : "1", | ||
2933 | c0 = Math.cos(a0), | ||
2934 | s0 = Math.sin(a0), | ||
2935 | c1 = Math.cos(a1), | ||
2936 | s1 = Math.sin(a1); | ||
2937 | return da >= d3_svg_arcMax | ||
2938 | ? (r0 | ||
2939 | ? "M0," + r1 | ||
2940 | + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1) | ||
2941 | + "A" + r1 + "," + r1 + " 0 1,1 0," + r1 | ||
2942 | + "M0," + r0 | ||
2943 | + "A" + r0 + "," + r0 + " 0 1,0 0," + (-r0) | ||
2944 | + "A" + r0 + "," + r0 + " 0 1,0 0," + r0 | ||
2945 | + "Z" | ||
2946 | : "M0," + r1 | ||
2947 | + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1) | ||
2948 | + "A" + r1 + "," + r1 + " 0 1,1 0," + r1 | ||
2949 | + "Z") | ||
2950 | : (r0 | ||
2951 | ? "M" + r1 * c0 + "," + r1 * s0 | ||
2952 | + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1 | ||
2953 | + "L" + r0 * c1 + "," + r0 * s1 | ||
2954 | + "A" + r0 + "," + r0 + " 0 " + df + ",0 " + r0 * c0 + "," + r0 * s0 | ||
2955 | + "Z" | ||
2956 | : "M" + r1 * c0 + "," + r1 * s0 | ||
2957 | + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1 | ||
2958 | + "L0,0" | ||
2959 | + "Z"); | ||
2960 | } | ||
2961 | |||
2962 | arc.innerRadius = function(v) { | ||
2963 | if (!arguments.length) return innerRadius; | ||
2964 | innerRadius = d3.functor(v); | ||
2965 | return arc; | ||
2966 | }; | ||
2967 | |||
2968 | arc.outerRadius = function(v) { | ||
2969 | if (!arguments.length) return outerRadius; | ||
2970 | outerRadius = d3.functor(v); | ||
2971 | return arc; | ||
2972 | }; | ||
2973 | |||
2974 | arc.startAngle = function(v) { | ||
2975 | if (!arguments.length) return startAngle; | ||
2976 | startAngle = d3.functor(v); | ||
2977 | return arc; | ||
2978 | }; | ||
2979 | |||
2980 | arc.endAngle = function(v) { | ||
2981 | if (!arguments.length) return endAngle; | ||
2982 | endAngle = d3.functor(v); | ||
2983 | return arc; | ||
2984 | }; | ||
2985 | |||
2986 | arc.centroid = function() { | ||
2987 | var r = (innerRadius.apply(this, arguments) | ||
2988 | + outerRadius.apply(this, arguments)) / 2, | ||
2989 | a = (startAngle.apply(this, arguments) | ||
2990 | + endAngle.apply(this, arguments)) / 2 + d3_svg_arcOffset; | ||
2991 | return [Math.cos(a) * r, Math.sin(a) * r]; | ||
2992 | }; | ||
2993 | |||
2994 | return arc; | ||
2995 | }; | ||
2996 | |||
2997 | var d3_svg_arcOffset = -Math.PI / 2, | ||
2998 | d3_svg_arcMax = 2 * Math.PI - 1e-6; | ||
2999 | |||
3000 | function d3_svg_arcInnerRadius(d) { | ||
3001 | return d.innerRadius; | ||
3002 | } | ||
3003 | |||
3004 | function d3_svg_arcOuterRadius(d) { | ||
3005 | return d.outerRadius; | ||
3006 | } | ||
3007 | |||
3008 | function d3_svg_arcStartAngle(d) { | ||
3009 | return d.startAngle; | ||
3010 | } | ||
3011 | |||
3012 | function d3_svg_arcEndAngle(d) { | ||
3013 | return d.endAngle; | ||
3014 | } | ||
3015 | function d3_svg_line(projection) { | ||
3016 | var x = d3_svg_lineX, | ||
3017 | y = d3_svg_lineY, | ||
3018 | interpolate = "linear", | ||
3019 | interpolator = d3_svg_lineInterpolators[interpolate], | ||
3020 | tension = .7; | ||
3021 | |||
3022 | function line(d) { | ||
3023 | return d.length < 1 ? null : "M" + interpolator(projection(d3_svg_linePoints(this, d, x, y)), tension); | ||
3024 | } | ||
3025 | |||
3026 | line.x = function(v) { | ||
3027 | if (!arguments.length) return x; | ||
3028 | x = v; | ||
3029 | return line; | ||
3030 | }; | ||
3031 | |||
3032 | line.y = function(v) { | ||
3033 | if (!arguments.length) return y; | ||
3034 | y = v; | ||
3035 | return line; | ||
3036 | }; | ||
3037 | |||
3038 | line.interpolate = function(v) { | ||
3039 | if (!arguments.length) return interpolate; | ||
3040 | interpolator = d3_svg_lineInterpolators[interpolate = v]; | ||
3041 | return line; | ||
3042 | }; | ||
3043 | |||
3044 | line.tension = function(v) { | ||
3045 | if (!arguments.length) return tension; | ||
3046 | tension = v; | ||
3047 | return line; | ||
3048 | }; | ||
3049 | |||
3050 | return line; | ||
3051 | } | ||
3052 | |||
3053 | d3.svg.line = function() { | ||
3054 | return d3_svg_line(Object); | ||
3055 | }; | ||
3056 | |||
3057 | // Converts the specified array of data into an array of points | ||
3058 | // (x-y tuples), by evaluating the specified `x` and `y` functions on each | ||
3059 | // data point. The `this` context of the evaluated functions is the specified | ||
3060 | // "self" object; each function is passed the current datum and index. | ||
3061 | function d3_svg_linePoints(self, d, x, y) { | ||
3062 | var points = [], | ||
3063 | i = -1, | ||
3064 | n = d.length, | ||
3065 | fx = typeof x === "function", | ||
3066 | fy = typeof y === "function", | ||
3067 | value; | ||
3068 | if (fx && fy) { | ||
3069 | while (++i < n) points.push([ | ||
3070 | x.call(self, value = d[i], i), | ||
3071 | y.call(self, value, i) | ||
3072 | ]); | ||
3073 | } else if (fx) { | ||
3074 | while (++i < n) points.push([x.call(self, d[i], i), y]); | ||
3075 | } else if (fy) { | ||
3076 | while (++i < n) points.push([x, y.call(self, d[i], i)]); | ||
3077 | } else { | ||
3078 | while (++i < n) points.push([x, y]); | ||
3079 | } | ||
3080 | return points; | ||
3081 | } | ||
3082 | |||
3083 | // The default `x` property, which references d[0]. | ||
3084 | function d3_svg_lineX(d) { | ||
3085 | return d[0]; | ||
3086 | } | ||
3087 | |||
3088 | // The default `y` property, which references d[1]. | ||
3089 | function d3_svg_lineY(d) { | ||
3090 | return d[1]; | ||
3091 | } | ||
3092 | |||
3093 | // The various interpolators supported by the `line` class. | ||
3094 | var d3_svg_lineInterpolators = { | ||
3095 | "linear": d3_svg_lineLinear, | ||
3096 | "step-before": d3_svg_lineStepBefore, | ||
3097 | "step-after": d3_svg_lineStepAfter, | ||
3098 | "basis": d3_svg_lineBasis, | ||
3099 | "basis-open": d3_svg_lineBasisOpen, | ||
3100 | "basis-closed": d3_svg_lineBasisClosed, | ||
3101 | "bundle": d3_svg_lineBundle, | ||
3102 | "cardinal": d3_svg_lineCardinal, | ||
3103 | "cardinal-open": d3_svg_lineCardinalOpen, | ||
3104 | "cardinal-closed": d3_svg_lineCardinalClosed, | ||
3105 | "monotone": d3_svg_lineMonotone | ||
3106 | }; | ||
3107 | |||
3108 | // Linear interpolation; generates "L" commands. | ||
3109 | function d3_svg_lineLinear(points) { | ||
3110 | var i = 0, | ||
3111 | n = points.length, | ||
3112 | p = points[0], | ||
3113 | path = [p[0], ",", p[1]]; | ||
3114 | while (++i < n) path.push("L", (p = points[i])[0], ",", p[1]); | ||
3115 | return path.join(""); | ||
3116 | } | ||
3117 | |||
3118 | // Step interpolation; generates "H" and "V" commands. | ||
3119 | function d3_svg_lineStepBefore(points) { | ||
3120 | var i = 0, | ||
3121 | n = points.length, | ||
3122 | p = points[0], | ||
3123 | path = [p[0], ",", p[1]]; | ||
3124 | while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]); | ||
3125 | return path.join(""); | ||
3126 | } | ||
3127 | |||
3128 | // Step interpolation; generates "H" and "V" commands. | ||
3129 | function d3_svg_lineStepAfter(points) { | ||
3130 | var i = 0, | ||
3131 | n = points.length, | ||
3132 | p = points[0], | ||
3133 | path = [p[0], ",", p[1]]; | ||
3134 | while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]); | ||
3135 | return path.join(""); | ||
3136 | } | ||
3137 | |||
3138 | // Open cardinal spline interpolation; generates "C" commands. | ||
3139 | function d3_svg_lineCardinalOpen(points, tension) { | ||
3140 | return points.length < 4 | ||
3141 | ? d3_svg_lineLinear(points) | ||
3142 | : points[1] + d3_svg_lineHermite(points.slice(1, points.length - 1), | ||
3143 | d3_svg_lineCardinalTangents(points, tension)); | ||
3144 | } | ||
3145 | |||
3146 | // Closed cardinal spline interpolation; generates "C" commands. | ||
3147 | function d3_svg_lineCardinalClosed(points, tension) { | ||
3148 | return points.length < 3 | ||
3149 | ? d3_svg_lineLinear(points) | ||
3150 | : points[0] + d3_svg_lineHermite((points.push(points[0]), points), | ||
3151 | d3_svg_lineCardinalTangents([points[points.length - 2]] | ||
3152 | .concat(points, [points[1]]), tension)); | ||
3153 | } | ||
3154 | |||
3155 | // Cardinal spline interpolation; generates "C" commands. | ||
3156 | function d3_svg_lineCardinal(points, tension, closed) { | ||
3157 | return points.length < 3 | ||
3158 | ? d3_svg_lineLinear(points) | ||
3159 | : points[0] + d3_svg_lineHermite(points, | ||
3160 | d3_svg_lineCardinalTangents(points, tension)); | ||
3161 | } | ||
3162 | |||
3163 | // Hermite spline construction; generates "C" commands. | ||
3164 | function d3_svg_lineHermite(points, tangents) { | ||
3165 | if (tangents.length < 1 | ||
3166 | || (points.length != tangents.length | ||
3167 | && points.length != tangents.length + 2)) { | ||
3168 | return d3_svg_lineLinear(points); | ||
3169 | } | ||
3170 | |||
3171 | var quad = points.length != tangents.length, | ||
3172 | path = "", | ||
3173 | p0 = points[0], | ||
3174 | p = points[1], | ||
3175 | t0 = tangents[0], | ||
3176 | t = t0, | ||
3177 | pi = 1; | ||
3178 | |||
3179 | if (quad) { | ||
3180 | path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3) | ||
3181 | + "," + p[0] + "," + p[1]; | ||
3182 | p0 = points[1]; | ||
3183 | pi = 2; | ||
3184 | } | ||
3185 | |||
3186 | if (tangents.length > 1) { | ||
3187 | t = tangents[1]; | ||
3188 | p = points[pi]; | ||
3189 | pi++; | ||
3190 | path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1]) | ||
3191 | + "," + (p[0] - t[0]) + "," + (p[1] - t[1]) | ||
3192 | + "," + p[0] + "," + p[1]; | ||
3193 | for (var i = 2; i < tangents.length; i++, pi++) { | ||
3194 | p = points[pi]; | ||
3195 | t = tangents[i]; | ||
3196 | path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1]) | ||
3197 | + "," + p[0] + "," + p[1]; | ||
3198 | } | ||
3199 | } | ||
3200 | |||
3201 | if (quad) { | ||
3202 | var lp = points[pi]; | ||
3203 | path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3) | ||
3204 | + "," + lp[0] + "," + lp[1]; | ||
3205 | } | ||
3206 | |||
3207 | return path; | ||
3208 | } | ||
3209 | |||
3210 | // Generates tangents for a cardinal spline. | ||
3211 | function d3_svg_lineCardinalTangents(points, tension) { | ||
3212 | var tangents = [], | ||
3213 | a = (1 - tension) / 2, | ||
3214 | p0, | ||
3215 | p1 = points[0], | ||
3216 | p2 = points[1], | ||
3217 | i = 1, | ||
3218 | n = points.length; | ||
3219 | while (++i < n) { | ||
3220 | p0 = p1; | ||
3221 | p1 = p2; | ||
3222 | p2 = points[i]; | ||
3223 | tangents.push([a * (p2[0] - p0[0]), a * (p2[1] - p0[1])]); | ||
3224 | } | ||
3225 | return tangents; | ||
3226 | } | ||
3227 | |||
3228 | // B-spline interpolation; generates "C" commands. | ||
3229 | function d3_svg_lineBasis(points) { | ||
3230 | if (points.length < 3) return d3_svg_lineLinear(points); | ||
3231 | var i = 1, | ||
3232 | n = points.length, | ||
3233 | pi = points[0], | ||
3234 | x0 = pi[0], | ||
3235 | y0 = pi[1], | ||
3236 | px = [x0, x0, x0, (pi = points[1])[0]], | ||
3237 | py = [y0, y0, y0, pi[1]], | ||
3238 | path = [x0, ",", y0]; | ||
3239 | d3_svg_lineBasisBezier(path, px, py); | ||
3240 | while (++i < n) { | ||
3241 | pi = points[i]; | ||
3242 | px.shift(); px.push(pi[0]); | ||
3243 | py.shift(); py.push(pi[1]); | ||
3244 | d3_svg_lineBasisBezier(path, px, py); | ||
3245 | } | ||
3246 | i = -1; | ||
3247 | while (++i < 2) { | ||
3248 | px.shift(); px.push(pi[0]); | ||
3249 | py.shift(); py.push(pi[1]); | ||
3250 | d3_svg_lineBasisBezier(path, px, py); | ||
3251 | } | ||
3252 | return path.join(""); | ||
3253 | } | ||
3254 | |||
3255 | // Open B-spline interpolation; generates "C" commands. | ||
3256 | function d3_svg_lineBasisOpen(points) { | ||
3257 | if (points.length < 4) return d3_svg_lineLinear(points); | ||
3258 | var path = [], | ||
3259 | i = -1, | ||
3260 | n = points.length, | ||
3261 | pi, | ||
3262 | px = [0], | ||
3263 | py = [0]; | ||
3264 | while (++i < 3) { | ||
3265 | pi = points[i]; | ||
3266 | px.push(pi[0]); | ||
3267 | py.push(pi[1]); | ||
3268 | } | ||
3269 | path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px) | ||
3270 | + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)); | ||
3271 | --i; while (++i < n) { | ||
3272 | pi = points[i]; | ||
3273 | px.shift(); px.push(pi[0]); | ||
3274 | py.shift(); py.push(pi[1]); | ||
3275 | d3_svg_lineBasisBezier(path, px, py); | ||
3276 | } | ||
3277 | return path.join(""); | ||
3278 | } | ||
3279 | |||
3280 | // Closed B-spline interpolation; generates "C" commands. | ||
3281 | function d3_svg_lineBasisClosed(points) { | ||
3282 | var path, | ||
3283 | i = -1, | ||
3284 | n = points.length, | ||
3285 | m = n + 4, | ||
3286 | pi, | ||
3287 | px = [], | ||
3288 | py = []; | ||
3289 | while (++i < 4) { | ||
3290 | pi = points[i % n]; | ||
3291 | px.push(pi[0]); | ||
3292 | py.push(pi[1]); | ||
3293 | } | ||
3294 | path = [ | ||
3295 | d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", | ||
3296 | d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) | ||
3297 | ]; | ||
3298 | --i; while (++i < m) { | ||
3299 | pi = points[i % n]; | ||
3300 | px.shift(); px.push(pi[0]); | ||
3301 | py.shift(); py.push(pi[1]); | ||
3302 | d3_svg_lineBasisBezier(path, px, py); | ||
3303 | } | ||
3304 | return path.join(""); | ||
3305 | } | ||
3306 | |||
3307 | function d3_svg_lineBundle(points, tension) { | ||
3308 | var n = points.length - 1, | ||
3309 | x0 = points[0][0], | ||
3310 | y0 = points[0][1], | ||
3311 | dx = points[n][0] - x0, | ||
3312 | dy = points[n][1] - y0, | ||
3313 | i = -1, | ||
3314 | p, | ||
3315 | t; | ||
3316 | while (++i <= n) { | ||
3317 | p = points[i]; | ||
3318 | t = i / n; | ||
3319 | p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx); | ||
3320 | p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy); | ||
3321 | } | ||
3322 | return d3_svg_lineBasis(points); | ||
3323 | } | ||
3324 | |||
3325 | // Returns the dot product of the given four-element vectors. | ||
3326 | function d3_svg_lineDot4(a, b) { | ||
3327 | return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; | ||
3328 | } | ||
3329 | |||
3330 | // Matrix to transform basis (b-spline) control points to bezier | ||
3331 | // control points. Derived from FvD 11.2.8. | ||
3332 | var d3_svg_lineBasisBezier1 = [0, 2/3, 1/3, 0], | ||
3333 | d3_svg_lineBasisBezier2 = [0, 1/3, 2/3, 0], | ||
3334 | d3_svg_lineBasisBezier3 = [0, 1/6, 2/3, 1/6]; | ||
3335 | |||
3336 | // Pushes a "C" Bézier curve onto the specified path array, given the | ||
3337 | // two specified four-element arrays which define the control points. | ||
3338 | function d3_svg_lineBasisBezier(path, x, y) { | ||
3339 | path.push( | ||
3340 | "C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x), | ||
3341 | ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y), | ||
3342 | ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x), | ||
3343 | ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y), | ||
3344 | ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x), | ||
3345 | ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y)); | ||
3346 | } | ||
3347 | |||
3348 | // Computes the slope from points p0 to p1. | ||
3349 | function d3_svg_lineSlope(p0, p1) { | ||
3350 | return (p1[1] - p0[1]) / (p1[0] - p0[0]); | ||
3351 | } | ||
3352 | |||
3353 | // Compute three-point differences for the given points. | ||
3354 | // http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Finite_difference | ||
3355 | function d3_svg_lineFiniteDifferences(points) { | ||
3356 | var i = 0, | ||
3357 | j = points.length - 1, | ||
3358 | m = [], | ||
3359 | p0 = points[0], | ||
3360 | p1 = points[1], | ||
3361 | d = m[0] = d3_svg_lineSlope(p0, p1); | ||
3362 | while (++i < j) { | ||
3363 | m[i] = d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1])); | ||
3364 | } | ||
3365 | m[i] = d; | ||
3366 | return m; | ||
3367 | } | ||
3368 | |||
3369 | // Interpolates the given points using Fritsch-Carlson Monotone cubic Hermite | ||
3370 | // interpolation. Returns an array of tangent vectors. For details, see | ||
3371 | // http://en.wikipedia.org/wiki/Monotone_cubic_interpolation | ||
3372 | function d3_svg_lineMonotoneTangents(points) { | ||
3373 | var tangents = [], | ||
3374 | d, | ||
3375 | a, | ||
3376 | b, | ||
3377 | s, | ||
3378 | m = d3_svg_lineFiniteDifferences(points), | ||
3379 | i = -1, | ||
3380 | j = points.length - 1; | ||
3381 | |||
3382 | // The first two steps are done by computing finite-differences: | ||
3383 | // 1. Compute the slopes of the secant lines between successive points. | ||
3384 | // 2. Initialize the tangents at every point as the average of the secants. | ||
3385 | |||
3386 | // Then, for each segment… | ||
3387 | while (++i < j) { | ||
3388 | d = d3_svg_lineSlope(points[i], points[i + 1]); | ||
3389 | |||
3390 | // 3. If two successive yk = y{k + 1} are equal (i.e., d is zero), then set | ||
3391 | // mk = m{k + 1} = 0 as the spline connecting these points must be flat to | ||
3392 | // preserve monotonicity. Ignore step 4 and 5 for those k. | ||
3393 | |||
3394 | if (Math.abs(d) < 1e-6) { | ||
3395 | m[i] = m[i + 1] = 0; | ||
3396 | } else { | ||
3397 | // 4. Let ak = mk / dk and bk = m{k + 1} / dk. | ||
3398 | a = m[i] / d; | ||
3399 | b = m[i + 1] / d; | ||
3400 | |||
3401 | // 5. Prevent overshoot and ensure monotonicity by restricting the | ||
3402 | // magnitude of vector <ak, bk> to a circle of radius 3. | ||
3403 | s = a * a + b * b; | ||
3404 | if (s > 9) { | ||
3405 | s = d * 3 / Math.sqrt(s); | ||
3406 | m[i] = s * a; | ||
3407 | m[i + 1] = s * b; | ||
3408 | } | ||
3409 | } | ||
3410 | } | ||
3411 | |||
3412 | // Compute the normalized tangent vector from the slopes. Note that if x is | ||
3413 | // not monotonic, it's possible that the slope will be infinite, so we protect | ||
3414 | // against NaN by setting the coordinate to zero. | ||
3415 | i = -1; while (++i <= j) { | ||
3416 | s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0]) | ||
3417 | / (6 * (1 + m[i] * m[i])); | ||
3418 | tangents.push([s || 0, m[i] * s || 0]); | ||
3419 | } | ||
3420 | |||
3421 | return tangents; | ||
3422 | } | ||
3423 | |||
3424 | function d3_svg_lineMonotone(points) { | ||
3425 | return points.length < 3 | ||
3426 | ? d3_svg_lineLinear(points) | ||
3427 | : points[0] + | ||
3428 | d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points)); | ||
3429 | } | ||
3430 | d3.svg.line.radial = function() { | ||
3431 | var line = d3_svg_line(d3_svg_lineRadial); | ||
3432 | line.radius = line.x, delete line.x; | ||
3433 | line.angle = line.y, delete line.y; | ||
3434 | return line; | ||
3435 | }; | ||
3436 | |||
3437 | function d3_svg_lineRadial(points) { | ||
3438 | var point, | ||
3439 | i = -1, | ||
3440 | n = points.length, | ||
3441 | r, | ||
3442 | a; | ||
3443 | while (++i < n) { | ||
3444 | point = points[i]; | ||
3445 | r = point[0]; | ||
3446 | a = point[1] + d3_svg_arcOffset; | ||
3447 | point[0] = r * Math.cos(a); | ||
3448 | point[1] = r * Math.sin(a); | ||
3449 | } | ||
3450 | return points; | ||
3451 | } | ||
3452 | function d3_svg_area(projection) { | ||
3453 | var x0 = d3_svg_lineX, | ||
3454 | x1 = d3_svg_lineX, | ||
3455 | y0 = 0, | ||
3456 | y1 = d3_svg_lineY, | ||
3457 | interpolate, | ||
3458 | i0, | ||
3459 | i1, | ||
3460 | tension = .7; | ||
3461 | |||
3462 | function area(d) { | ||
3463 | if (d.length < 1) return null; | ||
3464 | var points0 = d3_svg_linePoints(this, d, x0, y0), | ||
3465 | points1 = d3_svg_linePoints(this, d, x0 === x1 ? d3_svg_areaX(points0) : x1, y0 === y1 ? d3_svg_areaY(points0) : y1); | ||
3466 | return "M" + i0(projection(points1), tension) | ||
3467 | + "L" + i1(projection(points0.reverse()), tension) | ||
3468 | + "Z"; | ||
3469 | } | ||
3470 | |||
3471 | area.x = function(x) { | ||
3472 | if (!arguments.length) return x1; | ||
3473 | x0 = x1 = x; | ||
3474 | return area; | ||
3475 | }; | ||
3476 | |||
3477 | area.x0 = function(x) { | ||
3478 | if (!arguments.length) return x0; | ||
3479 | x0 = x; | ||
3480 | return area; | ||
3481 | }; | ||
3482 | |||
3483 | area.x1 = function(x) { | ||
3484 | if (!arguments.length) return x1; | ||
3485 | x1 = x; | ||
3486 | return area; | ||
3487 | }; | ||
3488 | |||
3489 | area.y = function(y) { | ||
3490 | if (!arguments.length) return y1; | ||
3491 | y0 = y1 = y; | ||
3492 | return area; | ||
3493 | }; | ||
3494 | |||
3495 | area.y0 = function(y) { | ||
3496 | if (!arguments.length) return y0; | ||
3497 | y0 = y; | ||
3498 | return area; | ||
3499 | }; | ||
3500 | |||
3501 | area.y1 = function(y) { | ||
3502 | if (!arguments.length) return y1; | ||
3503 | y1 = y; | ||
3504 | return area; | ||
3505 | }; | ||
3506 | |||
3507 | area.interpolate = function(x) { | ||
3508 | if (!arguments.length) return interpolate; | ||
3509 | i0 = d3_svg_lineInterpolators[interpolate = x]; | ||
3510 | i1 = i0.reverse || i0; | ||
3511 | return area; | ||
3512 | }; | ||
3513 | |||
3514 | area.tension = function(x) { | ||
3515 | if (!arguments.length) return tension; | ||
3516 | tension = x; | ||
3517 | return area; | ||
3518 | }; | ||
3519 | |||
3520 | return area.interpolate("linear"); | ||
3521 | } | ||
3522 | |||
3523 | d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter; | ||
3524 | d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore; | ||
3525 | |||
3526 | d3.svg.area = function() { | ||
3527 | return d3_svg_area(Object); | ||
3528 | }; | ||
3529 | |||
3530 | function d3_svg_areaX(points) { | ||
3531 | return function(d, i) { | ||
3532 | return points[i][0]; | ||
3533 | }; | ||
3534 | } | ||
3535 | |||
3536 | function d3_svg_areaY(points) { | ||
3537 | return function(d, i) { | ||
3538 | return points[i][1]; | ||
3539 | }; | ||
3540 | } | ||
3541 | d3.svg.area.radial = function() { | ||
3542 | var area = d3_svg_area(d3_svg_lineRadial); | ||
3543 | area.radius = area.x, delete area.x; | ||
3544 | area.innerRadius = area.x0, delete area.x0; | ||
3545 | area.outerRadius = area.x1, delete area.x1; | ||
3546 | area.angle = area.y, delete area.y; | ||
3547 | area.startAngle = area.y0, delete area.y0; | ||
3548 | area.endAngle = area.y1, delete area.y1; | ||
3549 | return area; | ||
3550 | }; | ||
3551 | d3.svg.chord = function() { | ||
3552 | var source = d3_svg_chordSource, | ||
3553 | target = d3_svg_chordTarget, | ||
3554 | radius = d3_svg_chordRadius, | ||
3555 | startAngle = d3_svg_arcStartAngle, | ||
3556 | endAngle = d3_svg_arcEndAngle; | ||
3557 | |||
3558 | // TODO Allow control point to be customized. | ||
3559 | |||
3560 | function chord(d, i) { | ||
3561 | var s = subgroup(this, source, d, i), | ||
3562 | t = subgroup(this, target, d, i); | ||
3563 | return "M" + s.p0 | ||
3564 | + arc(s.r, s.p1, s.a1 - s.a0) + (equals(s, t) | ||
3565 | ? curve(s.r, s.p1, s.r, s.p0) | ||
3566 | : curve(s.r, s.p1, t.r, t.p0) | ||
3567 | + arc(t.r, t.p1, t.a1 - t.a0) | ||
3568 | + curve(t.r, t.p1, s.r, s.p0)) | ||
3569 | + "Z"; | ||
3570 | } | ||
3571 | |||
3572 | function subgroup(self, f, d, i) { | ||
3573 | var subgroup = f.call(self, d, i), | ||
3574 | r = radius.call(self, subgroup, i), | ||
3575 | a0 = startAngle.call(self, subgroup, i) + d3_svg_arcOffset, | ||
3576 | a1 = endAngle.call(self, subgroup, i) + d3_svg_arcOffset; | ||
3577 | return { | ||
3578 | r: r, | ||
3579 | a0: a0, | ||
3580 | a1: a1, | ||
3581 | p0: [r * Math.cos(a0), r * Math.sin(a0)], | ||
3582 | p1: [r * Math.cos(a1), r * Math.sin(a1)] | ||
3583 | }; | ||
3584 | } | ||
3585 | |||
3586 | function equals(a, b) { | ||
3587 | return a.a0 == b.a0 && a.a1 == b.a1; | ||
3588 | } | ||
3589 | |||
3590 | function arc(r, p, a) { | ||
3591 | return "A" + r + "," + r + " 0 " + +(a > Math.PI) + ",1 " + p; | ||
3592 | } | ||
3593 | |||
3594 | function curve(r0, p0, r1, p1) { | ||
3595 | return "Q 0,0 " + p1; | ||
3596 | } | ||
3597 | |||
3598 | chord.radius = function(v) { | ||
3599 | if (!arguments.length) return radius; | ||
3600 | radius = d3.functor(v); | ||
3601 | return chord; | ||
3602 | }; | ||
3603 | |||
3604 | chord.source = function(v) { | ||
3605 | if (!arguments.length) return source; | ||
3606 | source = d3.functor(v); | ||
3607 | return chord; | ||
3608 | }; | ||
3609 | |||
3610 | chord.target = function(v) { | ||
3611 | if (!arguments.length) return target; | ||
3612 | target = d3.functor(v); | ||
3613 | return chord; | ||
3614 | }; | ||
3615 | |||
3616 | chord.startAngle = function(v) { | ||
3617 | if (!arguments.length) return startAngle; | ||
3618 | startAngle = d3.functor(v); | ||
3619 | return chord; | ||
3620 | }; | ||
3621 | |||
3622 | chord.endAngle = function(v) { | ||
3623 | if (!arguments.length) return endAngle; | ||
3624 | endAngle = d3.functor(v); | ||
3625 | return chord; | ||
3626 | }; | ||
3627 | |||
3628 | return chord; | ||
3629 | }; | ||
3630 | |||
3631 | function d3_svg_chordSource(d) { | ||
3632 | return d.source; | ||
3633 | } | ||
3634 | |||
3635 | function d3_svg_chordTarget(d) { | ||
3636 | return d.target; | ||
3637 | } | ||
3638 | |||
3639 | function d3_svg_chordRadius(d) { | ||
3640 | return d.radius; | ||
3641 | } | ||
3642 | |||
3643 | function d3_svg_chordStartAngle(d) { | ||
3644 | return d.startAngle; | ||
3645 | } | ||
3646 | |||
3647 | function d3_svg_chordEndAngle(d) { | ||
3648 | return d.endAngle; | ||
3649 | } | ||
3650 | d3.svg.diagonal = function() { | ||
3651 | var source = d3_svg_chordSource, | ||
3652 | target = d3_svg_chordTarget, | ||
3653 | projection = d3_svg_diagonalProjection; | ||
3654 | |||
3655 | function diagonal(d, i) { | ||
3656 | var p0 = source.call(this, d, i), | ||
3657 | p3 = target.call(this, d, i), | ||
3658 | m = (p0.y + p3.y) / 2, | ||
3659 | p = [p0, {x: p0.x, y: m}, {x: p3.x, y: m}, p3]; | ||
3660 | p = p.map(projection); | ||
3661 | return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3]; | ||
3662 | } | ||
3663 | |||
3664 | diagonal.source = function(x) { | ||
3665 | if (!arguments.length) return source; | ||
3666 | source = d3.functor(x); | ||
3667 | return diagonal; | ||
3668 | }; | ||
3669 | |||
3670 | diagonal.target = function(x) { | ||
3671 | if (!arguments.length) return target; | ||
3672 | target = d3.functor(x); | ||
3673 | return diagonal; | ||
3674 | }; | ||
3675 | |||
3676 | diagonal.projection = function(x) { | ||
3677 | if (!arguments.length) return projection; | ||
3678 | projection = x; | ||
3679 | return diagonal; | ||
3680 | }; | ||
3681 | |||
3682 | return diagonal; | ||
3683 | }; | ||
3684 | |||
3685 | function d3_svg_diagonalProjection(d) { | ||
3686 | return [d.x, d.y]; | ||
3687 | } | ||
3688 | d3.svg.diagonal.radial = function() { | ||
3689 | var diagonal = d3.svg.diagonal(), | ||
3690 | projection = d3_svg_diagonalProjection, | ||
3691 | projection_ = diagonal.projection; | ||
3692 | |||
3693 | diagonal.projection = function(x) { | ||
3694 | return arguments.length | ||
3695 | ? projection_(d3_svg_diagonalRadialProjection(projection = x)) | ||
3696 | : projection; | ||
3697 | }; | ||
3698 | |||
3699 | return diagonal; | ||
3700 | }; | ||
3701 | |||
3702 | function d3_svg_diagonalRadialProjection(projection) { | ||
3703 | return function() { | ||
3704 | var d = projection.apply(this, arguments), | ||
3705 | r = d[0], | ||
3706 | a = d[1] + d3_svg_arcOffset; | ||
3707 | return [r * Math.cos(a), r * Math.sin(a)]; | ||
3708 | }; | ||
3709 | } | ||
3710 | d3.svg.mouse = function(container) { | ||
3711 | return d3_svg_mousePoint(container, d3.event); | ||
3712 | }; | ||
3713 | |||
3714 | // https://bugs.webkit.org/show_bug.cgi?id=44083 | ||
3715 | var d3_mouse_bug44083 = /WebKit/.test(navigator.userAgent) ? -1 : 0; | ||
3716 | |||
3717 | function d3_svg_mousePoint(container, e) { | ||
3718 | var point = (container.ownerSVGElement || container).createSVGPoint(); | ||
3719 | if ((d3_mouse_bug44083 < 0) && (window.scrollX || window.scrollY)) { | ||
3720 | var svg = d3.select(document.body) | ||
3721 | .append("svg") | ||
3722 | .style("position", "absolute") | ||
3723 | .style("top", 0) | ||
3724 | .style("left", 0); | ||
3725 | var ctm = svg[0][0].getScreenCTM(); | ||
3726 | d3_mouse_bug44083 = !(ctm.f || ctm.e); | ||
3727 | svg.remove(); | ||
3728 | } | ||
3729 | if (d3_mouse_bug44083) { | ||
3730 | point.x = e.pageX; | ||
3731 | point.y = e.pageY; | ||
3732 | } else { | ||
3733 | point.x = e.clientX; | ||
3734 | point.y = e.clientY; | ||
3735 | } | ||
3736 | point = point.matrixTransform(container.getScreenCTM().inverse()); | ||
3737 | return [point.x, point.y]; | ||
3738 | }; | ||
3739 | d3.svg.touches = function(container, touches) { | ||
3740 | if (arguments.length < 2) touches = d3.event.touches; | ||
3741 | |||
3742 | return touches ? d3_array(touches).map(function(touch) { | ||
3743 | var point = d3_svg_mousePoint(container, touch); | ||
3744 | point.identifier = touch.identifier; | ||
3745 | return point; | ||
3746 | }) : []; | ||
3747 | }; | ||
3748 | d3.svg.symbol = function() { | ||
3749 | var type = d3_svg_symbolType, | ||
3750 | size = d3_svg_symbolSize; | ||
3751 | |||
3752 | function symbol(d, i) { | ||
3753 | return (d3_svg_symbols[type.call(this, d, i)] | ||
3754 | || d3_svg_symbols.circle) | ||
3755 | (size.call(this, d, i)); | ||
3756 | } | ||
3757 | |||
3758 | symbol.type = function(x) { | ||
3759 | if (!arguments.length) return type; | ||
3760 | type = d3.functor(x); | ||
3761 | return symbol; | ||
3762 | }; | ||
3763 | |||
3764 | // size of symbol in square pixels | ||
3765 | symbol.size = function(x) { | ||
3766 | if (!arguments.length) return size; | ||
3767 | size = d3.functor(x); | ||
3768 | return symbol; | ||
3769 | }; | ||
3770 | |||
3771 | return symbol; | ||
3772 | }; | ||
3773 | |||
3774 | function d3_svg_symbolSize() { | ||
3775 | return 64; | ||
3776 | } | ||
3777 | |||
3778 | function d3_svg_symbolType() { | ||
3779 | return "circle"; | ||
3780 | } | ||
3781 | |||
3782 | // TODO cross-diagonal? | ||
3783 | var d3_svg_symbols = { | ||
3784 | "circle": function(size) { | ||
3785 | var r = Math.sqrt(size / Math.PI); | ||
3786 | return "M0," + r | ||
3787 | + "A" + r + "," + r + " 0 1,1 0," + (-r) | ||
3788 | + "A" + r + "," + r + " 0 1,1 0," + r | ||
3789 | + "Z"; | ||
3790 | }, | ||
3791 | "cross": function(size) { | ||
3792 | var r = Math.sqrt(size / 5) / 2; | ||
3793 | return "M" + -3 * r + "," + -r | ||
3794 | + "H" + -r | ||
3795 | + "V" + -3 * r | ||
3796 | + "H" + r | ||
3797 | + "V" + -r | ||
3798 | + "H" + 3 * r | ||
3799 | + "V" + r | ||
3800 | + "H" + r | ||
3801 | + "V" + 3 * r | ||
3802 | + "H" + -r | ||
3803 | + "V" + r | ||
3804 | + "H" + -3 * r | ||
3805 | + "Z"; | ||
3806 | }, | ||
3807 | "diamond": function(size) { | ||
3808 | var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)), | ||
3809 | rx = ry * d3_svg_symbolTan30; | ||
3810 | return "M0," + -ry | ||
3811 | + "L" + rx + ",0" | ||
3812 | + " 0," + ry | ||
3813 | + " " + -rx + ",0" | ||
3814 | + "Z"; | ||
3815 | }, | ||
3816 | "square": function(size) { | ||
3817 | var r = Math.sqrt(size) / 2; | ||
3818 | return "M" + -r + "," + -r | ||
3819 | + "L" + r + "," + -r | ||
3820 | + " " + r + "," + r | ||
3821 | + " " + -r + "," + r | ||
3822 | + "Z"; | ||
3823 | }, | ||
3824 | "triangle-down": function(size) { | ||
3825 | var rx = Math.sqrt(size / d3_svg_symbolSqrt3), | ||
3826 | ry = rx * d3_svg_symbolSqrt3 / 2; | ||
3827 | return "M0," + ry | ||
3828 | + "L" + rx +"," + -ry | ||
3829 | + " " + -rx + "," + -ry | ||
3830 | + "Z"; | ||
3831 | }, | ||
3832 | "triangle-up": function(size) { | ||
3833 | var rx = Math.sqrt(size / d3_svg_symbolSqrt3), | ||
3834 | ry = rx * d3_svg_symbolSqrt3 / 2; | ||
3835 | return "M0," + -ry | ||
3836 | + "L" + rx +"," + ry | ||
3837 | + " " + -rx + "," + ry | ||
3838 | + "Z"; | ||
3839 | } | ||
3840 | }; | ||
3841 | |||
3842 | d3.svg.symbolTypes = d3.keys(d3_svg_symbols); | ||
3843 | |||
3844 | var d3_svg_symbolSqrt3 = Math.sqrt(3), | ||
3845 | d3_svg_symbolTan30 = Math.tan(30 * Math.PI / 180); | ||
3846 | d3.svg.axis = function() { | ||
3847 | var scale = d3.scale.linear(), | ||
3848 | orient = "bottom", | ||
3849 | tickMajorSize = 6, | ||
3850 | tickMinorSize = 6, | ||
3851 | tickEndSize = 6, | ||
3852 | tickPadding = 3, | ||
3853 | tickArguments_ = [10], | ||
3854 | tickFormat_, | ||
3855 | tickSubdivide = 0; | ||
3856 | |||
3857 | function axis(selection) { | ||
3858 | selection.each(function(d, i, j) { | ||
3859 | var g = d3.select(this); | ||
3860 | |||
3861 | // If selection is a transition, create subtransitions. | ||
3862 | var transition = selection.delay ? function(o) { | ||
3863 | var id = d3_transitionInheritId; | ||
3864 | try { | ||
3865 | d3_transitionInheritId = selection.id; | ||
3866 | return o.transition() | ||
3867 | .delay(selection[j][i].delay) | ||
3868 | .duration(selection[j][i].duration) | ||
3869 | .ease(selection.ease()); | ||
3870 | } finally { | ||
3871 | d3_transitionInheritId = id; | ||
3872 | } | ||
3873 | } : Object; | ||
3874 | |||
3875 | // Ticks, or domain values for ordinal scales. | ||
3876 | var ticks = scale.ticks ? scale.ticks.apply(scale, tickArguments_) : scale.domain(), | ||
3877 | tickFormat = tickFormat_ == null ? (scale.tickFormat ? scale.tickFormat.apply(scale, tickArguments_) : String) : tickFormat_; | ||
3878 | |||
3879 | // Minor ticks. | ||
3880 | var subticks = d3_svg_axisSubdivide(scale, ticks, tickSubdivide), | ||
3881 | subtick = g.selectAll(".minor").data(subticks, String), | ||
3882 | subtickEnter = subtick.enter().insert("line", "g").attr("class", "tick minor").style("opacity", 1e-6), | ||
3883 | subtickExit = transition(subtick.exit()).style("opacity", 1e-6).remove(), | ||
3884 | subtickUpdate = transition(subtick).style("opacity", 1); | ||
3885 | |||
3886 | // Major ticks. | ||
3887 | var tick = g.selectAll("g").data(ticks, String), | ||
3888 | tickEnter = tick.enter().insert("g", "path").style("opacity", 1e-6), | ||
3889 | tickExit = transition(tick.exit()).style("opacity", 1e-6).remove(), | ||
3890 | tickUpdate = transition(tick).style("opacity", 1), | ||
3891 | tickTransform; | ||
3892 | |||
3893 | // Domain. | ||
3894 | var range = d3_scaleRange(scale), | ||
3895 | path = g.selectAll(".domain").data([0]), | ||
3896 | pathEnter = path.enter().append("path").attr("class", "domain"), | ||
3897 | pathUpdate = transition(path); | ||
3898 | |||
3899 | // Stash a snapshot of the new scale, and retrieve the old snapshot. | ||
3900 | var scale1 = scale.copy(), | ||
3901 | scale0 = this.__chart__ || scale1; | ||
3902 | this.__chart__ = scale1; | ||
3903 | |||
3904 | tickEnter.append("line").attr("class", "tick"); | ||
3905 | tickEnter.append("text"); | ||
3906 | tickUpdate.select("text").text(tickFormat); | ||
3907 | |||
3908 | switch (orient) { | ||
3909 | case "bottom": { | ||
3910 | tickTransform = d3_svg_axisX; | ||
3911 | subtickEnter.attr("y2", tickMinorSize); | ||
3912 | subtickUpdate.attr("x2", 0).attr("y2", tickMinorSize); | ||
3913 | tickEnter.select("line").attr("y2", tickMajorSize); | ||
3914 | tickEnter.select("text").attr("y", Math.max(tickMajorSize, 0) + tickPadding); | ||
3915 | tickUpdate.select("line").attr("x2", 0).attr("y2", tickMajorSize); | ||
3916 | tickUpdate.select("text").attr("x", 0).attr("y", Math.max(tickMajorSize, 0) + tickPadding).attr("dy", ".71em").attr("text-anchor", "middle"); | ||
3917 | pathUpdate.attr("d", "M" + range[0] + "," + tickEndSize + "V0H" + range[1] + "V" + tickEndSize); | ||
3918 | break; | ||
3919 | } | ||
3920 | case "top": { | ||
3921 | tickTransform = d3_svg_axisX; | ||
3922 | subtickEnter.attr("y2", -tickMinorSize); | ||
3923 | subtickUpdate.attr("x2", 0).attr("y2", -tickMinorSize); | ||
3924 | tickEnter.select("line").attr("y2", -tickMajorSize); | ||
3925 | tickEnter.select("text").attr("y", -(Math.max(tickMajorSize, 0) + tickPadding)); | ||
3926 | tickUpdate.select("line").attr("x2", 0).attr("y2", -tickMajorSize); | ||
3927 | tickUpdate.select("text").attr("x", 0).attr("y", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("dy", "0em").attr("text-anchor", "middle"); | ||
3928 | pathUpdate.attr("d", "M" + range[0] + "," + -tickEndSize + "V0H" + range[1] + "V" + -tickEndSize); | ||
3929 | break; | ||
3930 | } | ||
3931 | case "left": { | ||
3932 | tickTransform = d3_svg_axisY; | ||
3933 | subtickEnter.attr("x2", -tickMinorSize); | ||
3934 | subtickUpdate.attr("x2", -tickMinorSize).attr("y2", 0); | ||
3935 | tickEnter.select("line").attr("x2", -tickMajorSize); | ||
3936 | tickEnter.select("text").attr("x", -(Math.max(tickMajorSize, 0) + tickPadding)); | ||
3937 | tickUpdate.select("line").attr("x2", -tickMajorSize).attr("y2", 0); | ||
3938 | tickUpdate.select("text").attr("x", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("y", 0).attr("dy", ".32em").attr("text-anchor", "end"); | ||
3939 | pathUpdate.attr("d", "M" + -tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + -tickEndSize); | ||
3940 | break; | ||
3941 | } | ||
3942 | case "right": { | ||
3943 | tickTransform = d3_svg_axisY; | ||
3944 | subtickEnter.attr("x2", tickMinorSize); | ||
3945 | subtickUpdate.attr("x2", tickMinorSize).attr("y2", 0); | ||
3946 | tickEnter.select("line").attr("x2", tickMajorSize); | ||
3947 | tickEnter.select("text").attr("x", Math.max(tickMajorSize, 0) + tickPadding); | ||
3948 | tickUpdate.select("line").attr("x2", tickMajorSize).attr("y2", 0); | ||
3949 | tickUpdate.select("text").attr("x", Math.max(tickMajorSize, 0) + tickPadding).attr("y", 0).attr("dy", ".32em").attr("text-anchor", "start"); | ||
3950 | pathUpdate.attr("d", "M" + tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + tickEndSize); | ||
3951 | break; | ||
3952 | } | ||
3953 | } | ||
3954 | |||
3955 | // For quantitative scales: | ||
3956 | // - enter new ticks from the old scale | ||
3957 | // - exit old ticks to the new scale | ||
3958 | if (scale.ticks) { | ||
3959 | tickEnter.call(tickTransform, scale0); | ||
3960 | tickUpdate.call(tickTransform, scale1); | ||
3961 | tickExit.call(tickTransform, scale1); | ||
3962 | subtickEnter.call(tickTransform, scale0); | ||
3963 | subtickUpdate.call(tickTransform, scale1); | ||
3964 | subtickExit.call(tickTransform, scale1); | ||
3965 | } | ||
3966 | |||
3967 | // For ordinal scales: | ||
3968 | // - any entering ticks are undefined in the old scale | ||
3969 | // - any exiting ticks are undefined in the new scale | ||
3970 | // Therefore, we only need to transition updating ticks. | ||
3971 | else { | ||
3972 | var dx = scale1.rangeBand() / 2, x = function(d) { return scale1(d) + dx; }; | ||
3973 | tickEnter.call(tickTransform, x); | ||
3974 | tickUpdate.call(tickTransform, x); | ||
3975 | } | ||
3976 | }); | ||
3977 | } | ||
3978 | |||
3979 | axis.scale = function(x) { | ||
3980 | if (!arguments.length) return scale; | ||
3981 | scale = x; | ||
3982 | return axis; | ||
3983 | }; | ||
3984 | |||
3985 | axis.orient = function(x) { | ||
3986 | if (!arguments.length) return orient; | ||
3987 | orient = x; | ||
3988 | return axis; | ||
3989 | }; | ||
3990 | |||
3991 | axis.ticks = function() { | ||
3992 | if (!arguments.length) return tickArguments_; | ||
3993 | tickArguments_ = arguments; | ||
3994 | return axis; | ||
3995 | }; | ||
3996 | |||
3997 | axis.tickFormat = function(x) { | ||
3998 | if (!arguments.length) return tickFormat_; | ||
3999 | tickFormat_ = x; | ||
4000 | return axis; | ||
4001 | }; | ||
4002 | |||
4003 | axis.tickSize = function(x, y, z) { | ||
4004 | if (!arguments.length) return tickMajorSize; | ||
4005 | var n = arguments.length - 1; | ||
4006 | tickMajorSize = +x; | ||
4007 | tickMinorSize = n > 1 ? +y : tickMajorSize; | ||
4008 | tickEndSize = n > 0 ? +arguments[n] : tickMajorSize; | ||
4009 | return axis; | ||
4010 | }; | ||
4011 | |||
4012 | axis.tickPadding = function(x) { | ||
4013 | if (!arguments.length) return tickPadding; | ||
4014 | tickPadding = +x; | ||
4015 | return axis; | ||
4016 | }; | ||
4017 | |||
4018 | axis.tickSubdivide = function(x) { | ||
4019 | if (!arguments.length) return tickSubdivide; | ||
4020 | tickSubdivide = +x; | ||
4021 | return axis; | ||
4022 | }; | ||
4023 | |||
4024 | return axis; | ||
4025 | }; | ||
4026 | |||
4027 | function d3_svg_axisX(selection, x) { | ||
4028 | selection.attr("transform", function(d) { return "translate(" + x(d) + ",0)"; }); | ||
4029 | } | ||
4030 | |||
4031 | function d3_svg_axisY(selection, y) { | ||
4032 | selection.attr("transform", function(d) { return "translate(0," + y(d) + ")"; }); | ||
4033 | } | ||
4034 | |||
4035 | function d3_svg_axisSubdivide(scale, ticks, m) { | ||
4036 | subticks = []; | ||
4037 | if (m && ticks.length > 1) { | ||
4038 | var extent = d3_scaleExtent(scale.domain()), | ||
4039 | subticks, | ||
4040 | i = -1, | ||
4041 | n = ticks.length, | ||
4042 | d = (ticks[1] - ticks[0]) / ++m, | ||
4043 | j, | ||
4044 | v; | ||
4045 | while (++i < n) { | ||
4046 | for (j = m; --j > 0;) { | ||
4047 | if ((v = +ticks[i] - j * d) >= extent[0]) { | ||
4048 | subticks.push(v); | ||
4049 | } | ||
4050 | } | ||
4051 | } | ||
4052 | for (--i, j = 0; ++j < m && (v = +ticks[i] + j * d) < extent[1];) { | ||
4053 | subticks.push(v); | ||
4054 | } | ||
4055 | } | ||
4056 | return subticks; | ||
4057 | } | ||
4058 | d3.svg.brush = function() { | ||
4059 | var event = d3.dispatch("brushstart", "brush", "brushend"), | ||
4060 | x, // x-scale, optional | ||
4061 | y, // y-scale, optional | ||
4062 | extent = [[0, 0], [0, 0]]; // [x0, y0], [x1, y1] | ||
4063 | |||
4064 | function brush(g) { | ||
4065 | var resizes = x && y ? ["n", "e", "s", "w", "nw", "ne", "se", "sw"] | ||
4066 | : x ? ["e", "w"] | ||
4067 | : y ? ["n", "s"] | ||
4068 | : []; | ||
4069 | |||
4070 | g.each(function() { | ||
4071 | var g = d3.select(this).on("mousedown.brush", down), | ||
4072 | bg = g.selectAll(".background").data([0]), | ||
4073 | fg = g.selectAll(".extent").data([0]), | ||
4074 | tz = g.selectAll(".resize").data(resizes, String), | ||
4075 | e; | ||
4076 | |||
4077 | // An invisible, mouseable area for starting a new brush. | ||
4078 | bg.enter().append("rect") | ||
4079 | .attr("class", "background") | ||
4080 | .style("visibility", "hidden") | ||
4081 | .style("pointer-events", "all") | ||
4082 | .style("cursor", "crosshair"); | ||
4083 | |||
4084 | // The visible brush extent; style this as you like! | ||
4085 | fg.enter().append("rect") | ||
4086 | .attr("class", "extent") | ||
4087 | .style("cursor", "move"); | ||
4088 | |||
4089 | // More invisible rects for resizing the extent. | ||
4090 | tz.enter().append("rect") | ||
4091 | .attr("class", function(d) { return "resize " + d; }) | ||
4092 | .attr("width", 6) | ||
4093 | .attr("height", 6) | ||
4094 | .style("visibility", "hidden") | ||
4095 | .style("cursor", function(d) { return d3_svg_brushCursor[d]; }); | ||
4096 | |||
4097 | // Update the resizers. | ||
4098 | tz.style("pointer-events", brush.empty() ? "none" : "all"); | ||
4099 | |||
4100 | // Remove any superfluous resizers. | ||
4101 | tz.exit().remove(); | ||
4102 | |||
4103 | // Initialize the background to fill the defined range. | ||
4104 | // If the range isn't defined, you can post-process. | ||
4105 | if (x) { | ||
4106 | e = d3_scaleRange(x); | ||
4107 | bg.attr("x", e[0]).attr("width", e[1] - e[0]); | ||
4108 | d3_svg_brushRedrawX(g, extent); | ||
4109 | } | ||
4110 | if (y) { | ||
4111 | e = d3_scaleRange(y); | ||
4112 | bg.attr("y", e[0]).attr("height", e[1] - e[0]); | ||
4113 | d3_svg_brushRedrawY(g, extent); | ||
4114 | } | ||
4115 | }); | ||
4116 | } | ||
4117 | |||
4118 | function down() { | ||
4119 | var target = d3.select(d3.event.target); | ||
4120 | |||
4121 | // Store some global state for the duration of the brush gesture. | ||
4122 | d3_svg_brush = brush; | ||
4123 | d3_svg_brushTarget = this; | ||
4124 | d3_svg_brushExtent = extent; | ||
4125 | d3_svg_brushOffset = d3.svg.mouse(d3_svg_brushTarget); | ||
4126 | |||
4127 | // If the extent was clicked on, drag rather than brush; | ||
4128 | // store the offset between the mouse and extent origin instead. | ||
4129 | if (d3_svg_brushDrag = target.classed("extent")) { | ||
4130 | d3_svg_brushOffset[0] = extent[0][0] - d3_svg_brushOffset[0]; | ||
4131 | d3_svg_brushOffset[1] = extent[0][1] - d3_svg_brushOffset[1]; | ||
4132 | } | ||
4133 | |||
4134 | // If a resizer was clicked on, record which side is to be resized. | ||
4135 | // Also, set the offset to the opposite side. | ||
4136 | else if (target.classed("resize")) { | ||
4137 | d3_svg_brushResize = d3.event.target.__data__; | ||
4138 | d3_svg_brushOffset[0] = extent[+/w$/.test(d3_svg_brushResize)][0]; | ||
4139 | d3_svg_brushOffset[1] = extent[+/^n/.test(d3_svg_brushResize)][1]; | ||
4140 | } | ||
4141 | |||
4142 | // If the ALT key is down when starting a brush, the center is at the mouse. | ||
4143 | else if (d3.event.altKey) { | ||
4144 | d3_svg_brushCenter = d3_svg_brushOffset.slice(); | ||
4145 | } | ||
4146 | |||
4147 | // Restrict which dimensions are resized. | ||
4148 | d3_svg_brushX = !/^(n|s)$/.test(d3_svg_brushResize) && x; | ||
4149 | d3_svg_brushY = !/^(e|w)$/.test(d3_svg_brushResize) && y; | ||
4150 | |||
4151 | // Notify listeners. | ||
4152 | d3_svg_brushDispatch = dispatcher(this, arguments); | ||
4153 | d3_svg_brushDispatch("brushstart"); | ||
4154 | d3_svg_brushMove(); | ||
4155 | d3_eventCancel(); | ||
4156 | } | ||
4157 | |||
4158 | function dispatcher(that, argumentz) { | ||
4159 | return function(type) { | ||
4160 | var e = d3.event; | ||
4161 | try { | ||
4162 | d3.event = {type: type, target: brush}; | ||
4163 | event[type].apply(that, argumentz); | ||
4164 | } finally { | ||
4165 | d3.event = e; | ||
4166 | } | ||
4167 | }; | ||
4168 | } | ||
4169 | |||
4170 | brush.x = function(z) { | ||
4171 | if (!arguments.length) return x; | ||
4172 | x = z; | ||
4173 | return brush; | ||
4174 | }; | ||
4175 | |||
4176 | brush.y = function(z) { | ||
4177 | if (!arguments.length) return y; | ||
4178 | y = z; | ||
4179 | return brush; | ||
4180 | }; | ||
4181 | |||
4182 | brush.extent = function(z) { | ||
4183 | var x0, x1, y0, y1, t; | ||
4184 | |||
4185 | // Invert the pixel extent to data-space. | ||
4186 | if (!arguments.length) { | ||
4187 | if (x) { | ||
4188 | x0 = extent[0][0], x1 = extent[1][0]; | ||
4189 | if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1); | ||
4190 | if (x1 < x0) t = x0, x0 = x1, x1 = t; | ||
4191 | } | ||
4192 | if (y) { | ||
4193 | y0 = extent[0][1], y1 = extent[1][1]; | ||
4194 | if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1); | ||
4195 | if (y1 < y0) t = y0, y0 = y1, y1 = t; | ||
4196 | } | ||
4197 | return x && y ? [[x0, y0], [x1, y1]] : x ? [x0, x1] : y && [y0, y1]; | ||
4198 | } | ||
4199 | |||
4200 | // Scale the data-space extent to pixels. | ||
4201 | if (x) { | ||
4202 | x0 = z[0], x1 = z[1]; | ||
4203 | if (y) x0 = x0[0], x1 = x1[0]; | ||
4204 | if (x.invert) x0 = x(x0), x1 = x(x1); | ||
4205 | if (x1 < x0) t = x0, x0 = x1, x1 = t; | ||
4206 | extent[0][0] = x0, extent[1][0] = x1; | ||
4207 | } | ||
4208 | if (y) { | ||
4209 | y0 = z[0], y1 = z[1]; | ||
4210 | if (x) y0 = y0[1], y1 = y1[1]; | ||
4211 | if (y.invert) y0 = y(y0), y1 = y(y1); | ||
4212 | if (y1 < y0) t = y0, y0 = y1, y1 = t; | ||
4213 | extent[0][1] = y0, extent[1][1] = y1; | ||
4214 | } | ||
4215 | |||
4216 | return brush; | ||
4217 | }; | ||
4218 | |||
4219 | brush.clear = function() { | ||
4220 | extent[0][0] = | ||
4221 | extent[0][1] = | ||
4222 | extent[1][0] = | ||
4223 | extent[1][1] = 0; | ||
4224 | return brush; | ||
4225 | }; | ||
4226 | |||
4227 | brush.empty = function() { | ||
4228 | return (x && extent[0][0] === extent[1][0]) | ||
4229 | || (y && extent[0][1] === extent[1][1]); | ||
4230 | }; | ||
4231 | |||
4232 | d3.select(window) | ||
4233 | .on("mousemove.brush", d3_svg_brushMove) | ||
4234 | .on("mouseup.brush", d3_svg_brushUp) | ||
4235 | .on("keydown.brush", d3_svg_brushKeydown) | ||
4236 | .on("keyup.brush", d3_svg_brushKeyup); | ||
4237 | |||
4238 | return d3.rebind(brush, event, "on"); | ||
4239 | }; | ||
4240 | |||
4241 | var d3_svg_brush, | ||
4242 | d3_svg_brushDispatch, | ||
4243 | d3_svg_brushTarget, | ||
4244 | d3_svg_brushX, | ||
4245 | d3_svg_brushY, | ||
4246 | d3_svg_brushExtent, | ||
4247 | d3_svg_brushDrag, | ||
4248 | d3_svg_brushResize, | ||
4249 | d3_svg_brushCenter, | ||
4250 | d3_svg_brushOffset; | ||
4251 | |||
4252 | function d3_svg_brushRedrawX(g, extent) { | ||
4253 | g.select(".extent").attr("x", extent[0][0]); | ||
4254 | g.selectAll(".n,.s,.w,.nw,.sw").attr("x", extent[0][0] - 2); | ||
4255 | g.selectAll(".e,.ne,.se").attr("x", extent[1][0] - 3); | ||
4256 | g.selectAll(".extent,.n,.s").attr("width", extent[1][0] - extent[0][0]); | ||
4257 | } | ||
4258 | |||
4259 | function d3_svg_brushRedrawY(g, extent) { | ||
4260 | g.select(".extent").attr("y", extent[0][1]); | ||
4261 | g.selectAll(".n,.e,.w,.nw,.ne").attr("y", extent[0][1] - 3); | ||
4262 | g.selectAll(".s,.se,.sw").attr("y", extent[1][1] - 4); | ||
4263 | g.selectAll(".extent,.e,.w").attr("height", extent[1][1] - extent[0][1]); | ||
4264 | } | ||
4265 | |||
4266 | function d3_svg_brushKeydown() { | ||
4267 | if (d3.event.keyCode == 32 && d3_svg_brushTarget && !d3_svg_brushDrag) { | ||
4268 | d3_svg_brushCenter = null; | ||
4269 | d3_svg_brushOffset[0] -= d3_svg_brushExtent[1][0]; | ||
4270 | d3_svg_brushOffset[1] -= d3_svg_brushExtent[1][1]; | ||
4271 | d3_svg_brushDrag = 2; | ||
4272 | d3_eventCancel(); | ||
4273 | } | ||
4274 | } | ||
4275 | |||
4276 | function d3_svg_brushKeyup() { | ||
4277 | if (d3.event.keyCode == 32 && d3_svg_brushDrag == 2) { | ||
4278 | d3_svg_brushOffset[0] += d3_svg_brushExtent[1][0]; | ||
4279 | d3_svg_brushOffset[1] += d3_svg_brushExtent[1][1]; | ||
4280 | d3_svg_brushDrag = 0; | ||
4281 | d3_eventCancel(); | ||
4282 | } | ||
4283 | } | ||
4284 | |||
4285 | function d3_svg_brushMove() { | ||
4286 | if (d3_svg_brushOffset) { | ||
4287 | var mouse = d3.svg.mouse(d3_svg_brushTarget), | ||
4288 | g = d3.select(d3_svg_brushTarget); | ||
4289 | |||
4290 | if (!d3_svg_brushDrag) { | ||
4291 | |||
4292 | // If needed, determine the center from the current extent. | ||
4293 | if (d3.event.altKey) { | ||
4294 | if (!d3_svg_brushCenter) { | ||
4295 | d3_svg_brushCenter = [ | ||
4296 | (d3_svg_brushExtent[0][0] + d3_svg_brushExtent[1][0]) / 2, | ||
4297 | (d3_svg_brushExtent[0][1] + d3_svg_brushExtent[1][1]) / 2 | ||
4298 | ]; | ||
4299 | } | ||
4300 | |||
4301 | // Update the offset, for when the ALT key is released. | ||
4302 | d3_svg_brushOffset[0] = d3_svg_brushExtent[+(mouse[0] < d3_svg_brushCenter[0])][0]; | ||
4303 | d3_svg_brushOffset[1] = d3_svg_brushExtent[+(mouse[1] < d3_svg_brushCenter[1])][1]; | ||
4304 | } | ||
4305 | |||
4306 | // When the ALT key is released, we clear the center. | ||
4307 | else d3_svg_brushCenter = null; | ||
4308 | } | ||
4309 | |||
4310 | // Update the brush extent for each dimension. | ||
4311 | if (d3_svg_brushX) { | ||
4312 | d3_svg_brushMove1(mouse, d3_svg_brushX, 0); | ||
4313 | d3_svg_brushRedrawX(g, d3_svg_brushExtent); | ||
4314 | } | ||
4315 | if (d3_svg_brushY) { | ||
4316 | d3_svg_brushMove1(mouse, d3_svg_brushY, 1); | ||
4317 | d3_svg_brushRedrawY(g, d3_svg_brushExtent); | ||
4318 | } | ||
4319 | |||
4320 | // Notify listeners. | ||
4321 | d3_svg_brushDispatch("brush"); | ||
4322 | } | ||
4323 | } | ||
4324 | |||
4325 | function d3_svg_brushMove1(mouse, scale, i) { | ||
4326 | var range = d3_scaleRange(scale), | ||
4327 | r0 = range[0], | ||
4328 | r1 = range[1], | ||
4329 | offset = d3_svg_brushOffset[i], | ||
4330 | size = d3_svg_brushExtent[1][i] - d3_svg_brushExtent[0][i], | ||
4331 | min, | ||
4332 | max; | ||
4333 | |||
4334 | // When dragging, reduce the range by the extent size and offset. | ||
4335 | if (d3_svg_brushDrag) { | ||
4336 | r0 -= offset; | ||
4337 | r1 -= size + offset; | ||
4338 | } | ||
4339 | |||
4340 | // Clamp the mouse so that the extent fits within the range extent. | ||
4341 | min = Math.max(r0, Math.min(r1, mouse[i])); | ||
4342 | |||
4343 | // Compute the new extent bounds. | ||
4344 | if (d3_svg_brushDrag) { | ||
4345 | max = (min += offset) + size; | ||
4346 | } else { | ||
4347 | |||
4348 | // If the ALT key is pressed, then preserve the center of the extent. | ||
4349 | if (d3_svg_brushCenter) offset = Math.max(r0, Math.min(r1, 2 * d3_svg_brushCenter[i] - min)); | ||
4350 | |||
4351 | // Compute the min and max of the offset and mouse. | ||
4352 | if (offset < min) { | ||
4353 | max = min; | ||
4354 | min = offset; | ||
4355 | } else { | ||
4356 | max = offset; | ||
4357 | } | ||
4358 | } | ||
4359 | |||
4360 | // Update the stored bounds. | ||
4361 | d3_svg_brushExtent[0][i] = min; | ||
4362 | d3_svg_brushExtent[1][i] = max; | ||
4363 | } | ||
4364 | |||
4365 | function d3_svg_brushUp() { | ||
4366 | if (d3_svg_brushOffset) { | ||
4367 | d3_svg_brushMove(); | ||
4368 | d3.select(d3_svg_brushTarget).selectAll(".resize").style("pointer-events", d3_svg_brush.empty() ? "none" : "all"); | ||
4369 | d3_svg_brushDispatch("brushend"); | ||
4370 | d3_svg_brush = | ||
4371 | d3_svg_brushDispatch = | ||
4372 | d3_svg_brushTarget = | ||
4373 | d3_svg_brushX = | ||
4374 | d3_svg_brushY = | ||
4375 | d3_svg_brushExtent = | ||
4376 | d3_svg_brushDrag = | ||
4377 | d3_svg_brushResize = | ||
4378 | d3_svg_brushCenter = | ||
4379 | d3_svg_brushOffset = null; | ||
4380 | d3_eventCancel(); | ||
4381 | } | ||
4382 | } | ||
4383 | |||
4384 | var d3_svg_brushCursor = { | ||
4385 | n: "ns-resize", | ||
4386 | e: "ew-resize", | ||
4387 | s: "ns-resize", | ||
4388 | w: "ew-resize", | ||
4389 | nw: "nwse-resize", | ||
4390 | ne: "nesw-resize", | ||
4391 | se: "nwse-resize", | ||
4392 | sw: "nesw-resize" | ||
4393 | }; | ||
4394 | d3.behavior = {}; | ||
4395 | // TODO Track touch points by identifier. | ||
4396 | |||
4397 | d3.behavior.drag = function() { | ||
4398 | var event = d3.dispatch("drag", "dragstart", "dragend"), | ||
4399 | origin = null; | ||
4400 | |||
4401 | function drag() { | ||
4402 | this | ||
4403 | .on("mousedown.drag", mousedown) | ||
4404 | .on("touchstart.drag", mousedown); | ||
4405 | |||
4406 | d3.select(window) | ||
4407 | .on("mousemove.drag", d3_behavior_dragMove) | ||
4408 | .on("touchmove.drag", d3_behavior_dragMove) | ||
4409 | .on("mouseup.drag", d3_behavior_dragUp, true) | ||
4410 | .on("touchend.drag", d3_behavior_dragUp, true) | ||
4411 | .on("click.drag", d3_behavior_dragClick, true); | ||
4412 | } | ||
4413 | |||
4414 | // snapshot the local context for subsequent dispatch | ||
4415 | function start() { | ||
4416 | d3_behavior_dragEvent = event; | ||
4417 | d3_behavior_dragEventTarget = d3.event.target; | ||
4418 | d3_behavior_dragTarget = this; | ||
4419 | d3_behavior_dragArguments = arguments; | ||
4420 | d3_behavior_dragOrigin = d3_behavior_dragPoint(); | ||
4421 | if (origin) { | ||
4422 | d3_behavior_dragOffset = origin.apply(d3_behavior_dragTarget, d3_behavior_dragArguments); | ||
4423 | d3_behavior_dragOffset = [d3_behavior_dragOffset.x - d3_behavior_dragOrigin[0], d3_behavior_dragOffset.y - d3_behavior_dragOrigin[1]]; | ||
4424 | } else { | ||
4425 | d3_behavior_dragOffset = [0, 0]; | ||
4426 | } | ||
4427 | d3_behavior_dragMoved = 0; | ||
4428 | } | ||
4429 | |||
4430 | function mousedown() { | ||
4431 | start.apply(this, arguments); | ||
4432 | d3_behavior_dragDispatch("dragstart"); | ||
4433 | } | ||
4434 | |||
4435 | drag.origin = function(x) { | ||
4436 | if (!arguments.length) return origin; | ||
4437 | origin = x; | ||
4438 | return drag; | ||
4439 | }; | ||
4440 | |||
4441 | return d3.rebind(drag, event, "on"); | ||
4442 | }; | ||
4443 | |||
4444 | var d3_behavior_dragEvent, | ||
4445 | d3_behavior_dragEventTarget, | ||
4446 | d3_behavior_dragTarget, | ||
4447 | d3_behavior_dragArguments, | ||
4448 | d3_behavior_dragOffset, | ||
4449 | d3_behavior_dragOrigin, | ||
4450 | d3_behavior_dragMoved; | ||
4451 | |||
4452 | function d3_behavior_dragDispatch(type) { | ||
4453 | var p = d3_behavior_dragPoint(), | ||
4454 | o = d3.event, | ||
4455 | e = d3.event = {type: type}; | ||
4456 | |||
4457 | if (p) { | ||
4458 | e.x = p[0] + d3_behavior_dragOffset[0]; | ||
4459 | e.y = p[1] + d3_behavior_dragOffset[1]; | ||
4460 | e.dx = p[0] - d3_behavior_dragOrigin[0]; | ||
4461 | e.dy = p[1] - d3_behavior_dragOrigin[1]; | ||
4462 | d3_behavior_dragMoved |= e.dx | e.dy; | ||
4463 | d3_behavior_dragOrigin = p; | ||
4464 | } | ||
4465 | |||
4466 | try { | ||
4467 | d3_behavior_dragEvent[type].apply(d3_behavior_dragTarget, d3_behavior_dragArguments); | ||
4468 | } finally { | ||
4469 | d3.event = o; | ||
4470 | } | ||
4471 | |||
4472 | o.stopPropagation(); | ||
4473 | o.preventDefault(); | ||
4474 | } | ||
4475 | |||
4476 | function d3_behavior_dragPoint() { | ||
4477 | var p = d3_behavior_dragTarget.parentNode, | ||
4478 | t = d3.event.changedTouches; | ||
4479 | return p && (t | ||
4480 | ? d3.svg.touches(p, t)[0] | ||
4481 | : d3.svg.mouse(p)); | ||
4482 | } | ||
4483 | |||
4484 | function d3_behavior_dragMove() { | ||
4485 | if (!d3_behavior_dragTarget) return; | ||
4486 | var parent = d3_behavior_dragTarget.parentNode; | ||
4487 | |||
4488 | // O NOES! The drag element was removed from the DOM. | ||
4489 | if (!parent) return d3_behavior_dragUp(); | ||
4490 | |||
4491 | d3_behavior_dragDispatch("drag"); | ||
4492 | d3_eventCancel(); | ||
4493 | } | ||
4494 | |||
4495 | function d3_behavior_dragUp() { | ||
4496 | if (!d3_behavior_dragTarget) return; | ||
4497 | d3_behavior_dragDispatch("dragend"); | ||
4498 | |||
4499 | // If the node was moved, prevent the mouseup from propagating. | ||
4500 | // Also prevent the subsequent click from propagating (e.g., for anchors). | ||
4501 | if (d3_behavior_dragMoved) { | ||
4502 | d3_eventCancel(); | ||
4503 | d3_behavior_dragMoved = d3.event.target === d3_behavior_dragEventTarget; | ||
4504 | } | ||
4505 | |||
4506 | d3_behavior_dragEvent = | ||
4507 | d3_behavior_dragEventTarget = | ||
4508 | d3_behavior_dragTarget = | ||
4509 | d3_behavior_dragArguments = | ||
4510 | d3_behavior_dragOffset = | ||
4511 | d3_behavior_dragOrigin = null; | ||
4512 | } | ||
4513 | |||
4514 | function d3_behavior_dragClick() { | ||
4515 | if (d3_behavior_dragMoved) { | ||
4516 | d3_eventCancel(); | ||
4517 | d3_behavior_dragMoved = 0; | ||
4518 | } | ||
4519 | } | ||
4520 | // TODO unbind zoom behavior? | ||
4521 | d3.behavior.zoom = function() { | ||
4522 | var xyz = [0, 0, 0], | ||
4523 | event = d3.dispatch("zoom"), | ||
4524 | extent = d3_behavior_zoomInfiniteExtent; | ||
4525 | |||
4526 | function zoom() { | ||
4527 | this | ||
4528 | .on("mousedown.zoom", mousedown) | ||
4529 | .on("mousewheel.zoom", mousewheel) | ||
4530 | .on("DOMMouseScroll.zoom", mousewheel) | ||
4531 | .on("dblclick.zoom", dblclick) | ||
4532 | .on("touchstart.zoom", touchstart); | ||
4533 | |||
4534 | d3.select(window) | ||
4535 | .on("mousemove.zoom", d3_behavior_zoomMousemove) | ||
4536 | .on("mouseup.zoom", d3_behavior_zoomMouseup) | ||
4537 | .on("touchmove.zoom", d3_behavior_zoomTouchmove) | ||
4538 | .on("touchend.zoom", d3_behavior_zoomTouchup) | ||
4539 | .on("click.zoom", d3_behavior_zoomClick, true); | ||
4540 | } | ||
4541 | |||
4542 | // snapshot the local context for subsequent dispatch | ||
4543 | function start() { | ||
4544 | d3_behavior_zoomXyz = xyz; | ||
4545 | d3_behavior_zoomExtent = extent; | ||
4546 | d3_behavior_zoomDispatch = event.zoom; | ||
4547 | d3_behavior_zoomEventTarget = d3.event.target; | ||
4548 | d3_behavior_zoomTarget = this; | ||
4549 | d3_behavior_zoomArguments = arguments; | ||
4550 | } | ||
4551 | |||
4552 | function mousedown() { | ||
4553 | start.apply(this, arguments); | ||
4554 | d3_behavior_zoomPanning = d3_behavior_zoomLocation(d3.svg.mouse(d3_behavior_zoomTarget)); | ||
4555 | d3_behavior_zoomMoved = 0; | ||
4556 | d3.event.preventDefault(); | ||
4557 | window.focus(); | ||
4558 | } | ||
4559 | |||
4560 | // store starting mouse location | ||
4561 | function mousewheel() { | ||
4562 | start.apply(this, arguments); | ||
4563 | if (!d3_behavior_zoomZooming) d3_behavior_zoomZooming = d3_behavior_zoomLocation(d3.svg.mouse(d3_behavior_zoomTarget)); | ||
4564 | d3_behavior_zoomTo(d3_behavior_zoomDelta() + xyz[2], d3.svg.mouse(d3_behavior_zoomTarget), d3_behavior_zoomZooming); | ||
4565 | } | ||
4566 | |||
4567 | function dblclick() { | ||
4568 | start.apply(this, arguments); | ||
4569 | var mouse = d3.svg.mouse(d3_behavior_zoomTarget); | ||
4570 | d3_behavior_zoomTo(d3.event.shiftKey ? Math.ceil(xyz[2] - 1) : Math.floor(xyz[2] + 1), mouse, d3_behavior_zoomLocation(mouse)); | ||
4571 | } | ||
4572 | |||
4573 | // doubletap detection | ||
4574 | function touchstart() { | ||
4575 | start.apply(this, arguments); | ||
4576 | var touches = d3_behavior_zoomTouchup(), | ||
4577 | touch, | ||
4578 | now = Date.now(); | ||
4579 | if ((touches.length === 1) && (now - d3_behavior_zoomLast < 300)) { | ||
4580 | d3_behavior_zoomTo(1 + Math.floor(xyz[2]), touch = touches[0], d3_behavior_zoomLocations[touch.identifier]); | ||
4581 | } | ||
4582 | d3_behavior_zoomLast = now; | ||
4583 | } | ||
4584 | |||
4585 | zoom.extent = function(x) { | ||
4586 | if (!arguments.length) return extent; | ||
4587 | extent = x == null ? d3_behavior_zoomInfiniteExtent : x; | ||
4588 | return zoom; | ||
4589 | }; | ||
4590 | |||
4591 | return d3.rebind(zoom, event, "on"); | ||
4592 | }; | ||
4593 | |||
4594 | var d3_behavior_zoomDiv, | ||
4595 | d3_behavior_zoomPanning, | ||
4596 | d3_behavior_zoomZooming, | ||
4597 | d3_behavior_zoomLocations = {}, // identifier -> location | ||
4598 | d3_behavior_zoomLast = 0, | ||
4599 | d3_behavior_zoomXyz, | ||
4600 | d3_behavior_zoomExtent, | ||
4601 | d3_behavior_zoomDispatch, | ||
4602 | d3_behavior_zoomEventTarget, | ||
4603 | d3_behavior_zoomTarget, | ||
4604 | d3_behavior_zoomArguments, | ||
4605 | d3_behavior_zoomMoved; | ||
4606 | |||
4607 | function d3_behavior_zoomLocation(point) { | ||
4608 | return [ | ||
4609 | point[0] - d3_behavior_zoomXyz[0], | ||
4610 | point[1] - d3_behavior_zoomXyz[1], | ||
4611 | d3_behavior_zoomXyz[2] | ||
4612 | ]; | ||
4613 | } | ||
4614 | |||
4615 | // detect the pixels that would be scrolled by this wheel event | ||
4616 | function d3_behavior_zoomDelta() { | ||
4617 | |||
4618 | // mousewheel events are totally broken! | ||
4619 | // https://bugs.webkit.org/show_bug.cgi?id=40441 | ||
4620 | // not only that, but Chrome and Safari differ in re. to acceleration! | ||
4621 | if (!d3_behavior_zoomDiv) { | ||
4622 | d3_behavior_zoomDiv = d3.select("body").append("div") | ||
4623 | .style("visibility", "hidden") | ||
4624 | .style("top", 0) | ||
4625 | .style("height", 0) | ||
4626 | .style("width", 0) | ||
4627 | .style("overflow-y", "scroll") | ||
4628 | .append("div") | ||
4629 | .style("height", "2000px") | ||
4630 | .node().parentNode; | ||
4631 | } | ||
4632 | |||
4633 | var e = d3.event, delta; | ||
4634 | try { | ||
4635 | d3_behavior_zoomDiv.scrollTop = 1000; | ||
4636 | d3_behavior_zoomDiv.dispatchEvent(e); | ||
4637 | delta = 1000 - d3_behavior_zoomDiv.scrollTop; | ||
4638 | } catch (error) { | ||
4639 | delta = e.wheelDelta || (-e.detail * 5); | ||
4640 | } | ||
4641 | |||
4642 | return delta * .005; | ||
4643 | } | ||
4644 | |||
4645 | // Note: Since we don't rotate, it's possible for the touches to become | ||
4646 | // slightly detached from their original positions. Thus, we recompute the | ||
4647 | // touch points on touchend as well as touchstart! | ||
4648 | function d3_behavior_zoomTouchup() { | ||
4649 | var touches = d3.svg.touches(d3_behavior_zoomTarget), | ||
4650 | i = -1, | ||
4651 | n = touches.length, | ||
4652 | touch; | ||
4653 | while (++i < n) d3_behavior_zoomLocations[(touch = touches[i]).identifier] = d3_behavior_zoomLocation(touch); | ||
4654 | return touches; | ||
4655 | } | ||
4656 | |||
4657 | function d3_behavior_zoomTouchmove() { | ||
4658 | var touches = d3.svg.touches(d3_behavior_zoomTarget); | ||
4659 | switch (touches.length) { | ||
4660 | |||
4661 | // single-touch pan | ||
4662 | case 1: { | ||
4663 | var touch = touches[0]; | ||
4664 | d3_behavior_zoomTo(d3_behavior_zoomXyz[2], touch, d3_behavior_zoomLocations[touch.identifier]); | ||
4665 | break; | ||
4666 | } | ||
4667 | |||
4668 | // double-touch pan + zoom | ||
4669 | case 2: { | ||
4670 | var p0 = touches[0], | ||
4671 | p1 = touches[1], | ||
4672 | p2 = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2], | ||
4673 | l0 = d3_behavior_zoomLocations[p0.identifier], | ||
4674 | l1 = d3_behavior_zoomLocations[p1.identifier], | ||
4675 | l2 = [(l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2, l0[2]]; | ||
4676 | d3_behavior_zoomTo(Math.log(d3.event.scale) / Math.LN2 + l0[2], p2, l2); | ||
4677 | break; | ||
4678 | } | ||
4679 | } | ||
4680 | } | ||
4681 | |||
4682 | function d3_behavior_zoomMousemove() { | ||
4683 | d3_behavior_zoomZooming = null; | ||
4684 | if (d3_behavior_zoomPanning) { | ||
4685 | d3_behavior_zoomMoved = 1; | ||
4686 | d3_behavior_zoomTo(d3_behavior_zoomXyz[2], d3.svg.mouse(d3_behavior_zoomTarget), d3_behavior_zoomPanning); | ||
4687 | } | ||
4688 | } | ||
4689 | |||
4690 | function d3_behavior_zoomMouseup() { | ||
4691 | if (d3_behavior_zoomPanning) { | ||
4692 | if (d3_behavior_zoomMoved) { | ||
4693 | d3_eventCancel(); | ||
4694 | d3_behavior_zoomMoved = d3_behavior_zoomEventTarget === d3.event.target; | ||
4695 | } | ||
4696 | |||
4697 | d3_behavior_zoomXyz = | ||
4698 | d3_behavior_zoomExtent = | ||
4699 | d3_behavior_zoomDispatch = | ||
4700 | d3_behavior_zoomEventTarget = | ||
4701 | d3_behavior_zoomTarget = | ||
4702 | d3_behavior_zoomArguments = | ||
4703 | d3_behavior_zoomPanning = null; | ||
4704 | } | ||
4705 | } | ||
4706 | |||
4707 | function d3_behavior_zoomClick() { | ||
4708 | if (d3_behavior_zoomMoved) { | ||
4709 | d3_eventCancel(); | ||
4710 | d3_behavior_zoomMoved = 0; | ||
4711 | } | ||
4712 | } | ||
4713 | |||
4714 | function d3_behavior_zoomTo(z, x0, x1) { | ||
4715 | z = d3_behavior_zoomExtentClamp(z, 2); | ||
4716 | var j = Math.pow(2, d3_behavior_zoomXyz[2]), | ||
4717 | k = Math.pow(2, z), | ||
4718 | K = Math.pow(2, (d3_behavior_zoomXyz[2] = z) - x1[2]), | ||
4719 | x_ = d3_behavior_zoomXyz[0], | ||
4720 | y_ = d3_behavior_zoomXyz[1], | ||
4721 | x = d3_behavior_zoomXyz[0] = d3_behavior_zoomExtentClamp((x0[0] - x1[0] * K), 0, k), | ||
4722 | y = d3_behavior_zoomXyz[1] = d3_behavior_zoomExtentClamp((x0[1] - x1[1] * K), 1, k), | ||
4723 | o = d3.event; // Events can be reentrant (e.g., focus). | ||
4724 | |||
4725 | d3.event = { | ||
4726 | scale: k, | ||
4727 | translate: [x, y], | ||
4728 | transform: function(sx, sy) { | ||
4729 | if (sx) transform(sx, x_, x); | ||
4730 | if (sy) transform(sy, y_, y); | ||
4731 | } | ||
4732 | }; | ||
4733 | |||
4734 | function transform(scale, a, b) { | ||
4735 | scale.domain(scale.range().map(function(v) { return scale.invert(((v - b) * j) / k + a); })); | ||
4736 | } | ||
4737 | |||
4738 | try { | ||
4739 | d3_behavior_zoomDispatch.apply(d3_behavior_zoomTarget, d3_behavior_zoomArguments); | ||
4740 | } finally { | ||
4741 | d3.event = o; | ||
4742 | } | ||
4743 | |||
4744 | o.preventDefault(); | ||
4745 | } | ||
4746 | |||
4747 | var d3_behavior_zoomInfiniteExtent = [ | ||
4748 | [-Infinity, Infinity], | ||
4749 | [-Infinity, Infinity], | ||
4750 | [-Infinity, Infinity] | ||
4751 | ]; | ||
4752 | |||
4753 | function d3_behavior_zoomExtentClamp(x, i, k) { | ||
4754 | var range = d3_behavior_zoomExtent[i], | ||
4755 | r0 = range[0], | ||
4756 | r1 = range[1]; | ||
4757 | return arguments.length === 3 | ||
4758 | ? Math.max(r1 * (r1 === Infinity ? -Infinity : 1 / k - 1), | ||
4759 | Math.min(r0 === -Infinity ? Infinity : r0, x / k)) * k | ||
4760 | : Math.max(r0, Math.min(r1, x)); | ||
4761 | } | ||
4762 | })(); |
  | |||
1 | // Word cloud layout by Jason Davies, http://www.jasondavies.com/word-cloud/ | ||
2 | // Algorithm due to Jonathan Feinberg, http://static.mrfeinberg.com/bv_ch03.pdf | ||
3 | (function() { | ||
4 | function cloud() { | ||
5 | var size = [256, 256], | ||
6 | text = cloudText, | ||
7 | font = cloudFont, | ||
8 | fontSize = cloudFontSize, | ||
9 | fontStyle = cloudFontNormal, | ||
10 | fontWeight = cloudFontNormal, | ||
11 | rotate = cloudRotate, | ||
12 | padding = cloudPadding, | ||
13 | spiral = archimedeanSpiral, | ||
14 | words = [], | ||
15 | timeInterval = Infinity, | ||
16 | event = d3.dispatch("word", "end"), | ||
17 | timer = null, | ||
18 | cloud = {}; | ||
19 | |||
20 | cloud.start = function() { | ||
21 | var board = zeroArray((size[0] >> 5) * size[1]), | ||
22 | bounds = null, | ||
23 | n = words.length, | ||
24 | i = -1, | ||
25 | tags = [], | ||
26 | data = words.map(function(d, i) { | ||
27 | d.text = text.call(this, d, i); | ||
28 | d.font = font.call(this, d, i); | ||
29 | d.style = fontStyle.call(this, d, i); | ||
30 | d.weight = fontWeight.call(this, d, i); | ||
31 | d.rotate = rotate.call(this, d, i); | ||
32 | d.size = ~~fontSize.call(this, d, i); | ||
33 | d.padding = padding.call(this, d, i); | ||
34 | return d; | ||
35 | }).sort(function(a, b) { return b.size - a.size; }); | ||
36 | |||
37 | if (timer) clearInterval(timer); | ||
38 | timer = setInterval(step, 0); | ||
39 | step(); | ||
40 | |||
41 | return cloud; | ||
42 | |||
43 | function step() { | ||
44 | var start = +new Date, | ||
45 | d; | ||
46 | while (+new Date - start < timeInterval && ++i < n && timer) { | ||
47 | d = data[i]; | ||
48 | d.x = (size[0] * (Math.random() + .5)) >> 1; | ||
49 | d.y = (size[1] * (Math.random() + .5)) >> 1; | ||
50 | cloudSprite(d, data, i); | ||
51 | if (d.hasText && place(board, d, bounds)) { | ||
52 | tags.push(d); | ||
53 | event.word(d); | ||
54 | if (bounds) cloudBounds(bounds, d); | ||
55 | else bounds = [{x: d.x + d.x0, y: d.y + d.y0}, {x: d.x + d.x1, y: d.y + d.y1}]; | ||
56 | // Temporary hack | ||
57 | d.x -= size[0] >> 1; | ||
58 | d.y -= size[1] >> 1; | ||
59 | } | ||
60 | } | ||
61 | if (i >= n) { | ||
62 | cloud.stop(); | ||
63 | event.end(tags, bounds); | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | |||
68 | cloud.stop = function() { | ||
69 | if (timer) { | ||
70 | clearInterval(timer); | ||
71 | timer = null; | ||
72 | } | ||
73 | return cloud; | ||
74 | }; | ||
75 | |||
76 | cloud.timeInterval = function(x) { | ||
77 | if (!arguments.length) return timeInterval; | ||
78 | timeInterval = x == null ? Infinity : x; | ||
79 | return cloud; | ||
80 | }; | ||
81 | |||
82 | function place(board, tag, bounds) { | ||
83 | var perimeter = [{x: 0, y: 0}, {x: size[0], y: size[1]}], | ||
84 | startX = tag.x, | ||
85 | startY = tag.y, | ||
86 | maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]), | ||
87 | s = spiral(size), | ||
88 | dt = Math.random() < .5 ? 1 : -1, | ||
89 | t = -dt, | ||
90 | dxdy, | ||
91 | dx, | ||
92 | dy; | ||
93 | |||
94 | while (dxdy = s(t += dt)) { | ||
95 | dx = ~~dxdy[0]; | ||
96 | dy = ~~dxdy[1]; | ||
97 | |||
98 | if (Math.min(dx, dy) > maxDelta) break; | ||
99 | |||
100 | tag.x = startX + dx; | ||
101 | tag.y = startY + dy; | ||
102 | |||
103 | if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 || | ||
104 | tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue; | ||
105 | // TODO only check for collisions within current bounds. | ||
106 | if (!bounds || !cloudCollide(tag, board, size[0])) { | ||
107 | if (!bounds || collideRects(tag, bounds)) { | ||
108 | var sprite = tag.sprite, | ||
109 | w = tag.width >> 5, | ||
110 | sw = size[0] >> 5, | ||
111 | lx = tag.x - (w << 4), | ||
112 | sx = lx & 0x7f, | ||
113 | msx = 32 - sx, | ||
114 | h = tag.y1 - tag.y0, | ||
115 | x = (tag.y + tag.y0) * sw + (lx >> 5), | ||
116 | last; | ||
117 | for (var j = 0; j < h; j++) { | ||
118 | last = 0; | ||
119 | for (var i = 0; i <= w; i++) { | ||
120 | board[x + i] |= (last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0); | ||
121 | } | ||
122 | x += sw; | ||
123 | } | ||
124 | delete tag.sprite; | ||
125 | return true; | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | return false; | ||
130 | } | ||
131 | |||
132 | cloud.words = function(x) { | ||
133 | if (!arguments.length) return words; | ||
134 | words = x; | ||
135 | return cloud; | ||
136 | }; | ||
137 | |||
138 | cloud.size = function(x) { | ||
139 | if (!arguments.length) return size; | ||
140 | size = [+x[0], +x[1]]; | ||
141 | return cloud; | ||
142 | }; | ||
143 | |||
144 | cloud.font = function(x) { | ||
145 | if (!arguments.length) return font; | ||
146 | font = d3.functor(x); | ||
147 | return cloud; | ||
148 | }; | ||
149 | |||
150 | cloud.fontStyle = function(x) { | ||
151 | if (!arguments.length) return fontStyle; | ||
152 | fontStyle = d3.functor(x); | ||
153 | return cloud; | ||
154 | }; | ||
155 | |||
156 | cloud.fontWeight = function(x) { | ||
157 | if (!arguments.length) return fontWeight; | ||
158 | fontWeight = d3.functor(x); | ||
159 | return cloud; | ||
160 | }; | ||
161 | |||
162 | cloud.rotate = function(x) { | ||
163 | if (!arguments.length) return rotate; | ||
164 | rotate = d3.functor(x); | ||
165 | return cloud; | ||
166 | }; | ||
167 | |||
168 | cloud.text = function(x) { | ||
169 | if (!arguments.length) return text; | ||
170 | text = d3.functor(x); | ||
171 | return cloud; | ||
172 | }; | ||
173 | |||
174 | cloud.spiral = function(x) { | ||
175 | if (!arguments.length) return spiral; | ||
176 | spiral = spirals[x + ""] || x; | ||
177 | return cloud; | ||
178 | }; | ||
179 | |||
180 | cloud.fontSize = function(x) { | ||
181 | if (!arguments.length) return fontSize; | ||
182 | fontSize = d3.functor(x); | ||
183 | return cloud; | ||
184 | }; | ||
185 | |||
186 | cloud.padding = function(x) { | ||
187 | if (!arguments.length) return padding; | ||
188 | padding = d3.functor(x); | ||
189 | return cloud; | ||
190 | }; | ||
191 | |||
192 | return d3.rebind(cloud, event, "on"); | ||
193 | } | ||
194 | |||
195 | function cloudText(d) { | ||
196 | return d.text; | ||
197 | } | ||
198 | |||
199 | function cloudFont() { | ||
200 | return "serif"; | ||
201 | } | ||
202 | |||
203 | function cloudFontNormal() { | ||
204 | return "normal"; | ||
205 | } | ||
206 | |||
207 | function cloudFontSize(d) { | ||
208 | return Math.sqrt(d.value); | ||
209 | } | ||
210 | |||
211 | function cloudRotate() { | ||
212 | return (~~(Math.random() * 6) - 3) * 30; | ||
213 | } | ||
214 | |||
215 | function cloudPadding() { | ||
216 | return 1; | ||
217 | } | ||
218 | |||
219 | // Fetches a monochrome sprite bitmap for the specified text. | ||
220 | // Load in batches for speed. | ||
221 | function cloudSprite(d, data, di) { | ||
222 | if (d.sprite) return; | ||
223 | c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio); | ||
224 | var x = 0, | ||
225 | y = 0, | ||
226 | maxh = 0, | ||
227 | n = data.length; | ||
228 | --di; | ||
229 | while (++di < n) { | ||
230 | d = data[di]; | ||
231 | c.save(); | ||
232 | c.font = d.style + " " + d.weight + " " + ~~((d.size + 1) / ratio) + "px " + d.font; | ||
233 | var w = c.measureText(d.text + "m").width * ratio, | ||
234 | h = d.size << 1; | ||
235 | if (d.rotate) { | ||
236 | var sr = Math.sin(d.rotate * cloudRadians), | ||
237 | cr = Math.cos(d.rotate * cloudRadians), | ||
238 | wcr = w * cr, | ||
239 | wsr = w * sr, | ||
240 | hcr = h * cr, | ||
241 | hsr = h * sr; | ||
242 | w = (Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 0x1f) >> 5 << 5; | ||
243 | h = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr)); | ||
244 | } else { | ||
245 | w = (w + 0x1f) >> 5 << 5; | ||
246 | } | ||
247 | if (h > maxh) maxh = h; | ||
248 | if (x + w >= (cw << 5)) { | ||
249 | x = 0; | ||
250 | y += maxh; | ||
251 | maxh = 0; | ||
252 | } | ||
253 | if (y + h >= ch) break; | ||
254 | c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio); | ||
255 | if (d.rotate) c.rotate(d.rotate * cloudRadians); | ||
256 | c.fillText(d.text, 0, 0); | ||
257 | if (d.padding) c.lineWidth = 2 * d.padding, c.strokeText(d.text, 0, 0); | ||
258 | c.restore(); | ||
259 | d.width = w; | ||
260 | d.height = h; | ||
261 | d.xoff = x; | ||
262 | d.yoff = y; | ||
263 | d.x1 = w >> 1; | ||
264 | d.y1 = h >> 1; | ||
265 | d.x0 = -d.x1; | ||
266 | d.y0 = -d.y1; | ||
267 | d.hasText = true; | ||
268 | x += w; | ||
269 | } | ||
270 | var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data, | ||
271 | sprite = []; | ||
272 | while (--di >= 0) { | ||
273 | d = data[di]; | ||
274 | if (!d.hasText) continue; | ||
275 | var w = d.width, | ||
276 | w32 = w >> 5, | ||
277 | h = d.y1 - d.y0; | ||
278 | // Zero the buffer | ||
279 | for (var i = 0; i < h * w32; i++) sprite[i] = 0; | ||
280 | x = d.xoff; | ||
281 | if (x == null) return; | ||
282 | y = d.yoff; | ||
283 | var seen = 0, | ||
284 | seenRow = -1; | ||
285 | for (var j = 0; j < h; j++) { | ||
286 | for (var i = 0; i < w; i++) { | ||
287 | var k = w32 * j + (i >> 5), | ||
288 | m = pixels[((y + j) * (cw << 5) + (x + i)) << 2] ? 1 << (31 - (i % 32)) : 0; | ||
289 | sprite[k] |= m; | ||
290 | seen |= m; | ||
291 | } | ||
292 | if (seen) seenRow = j; | ||
293 | else { | ||
294 | d.y0++; | ||
295 | h--; | ||
296 | j--; | ||
297 | y++; | ||
298 | } | ||
299 | } | ||
300 | d.y1 = d.y0 + seenRow; | ||
301 | d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32); | ||
302 | } | ||
303 | } | ||
304 | |||
305 | // Use mask-based collision detection. | ||
306 | function cloudCollide(tag, board, sw) { | ||
307 | sw >>= 5; | ||
308 | var sprite = tag.sprite, | ||
309 | w = tag.width >> 5, | ||
310 | lx = tag.x - (w << 4), | ||
311 | sx = lx & 0x7f, | ||
312 | msx = 32 - sx, | ||
313 | h = tag.y1 - tag.y0, | ||
314 | x = (tag.y + tag.y0) * sw + (lx >> 5), | ||
315 | last; | ||
316 | for (var j = 0; j < h; j++) { | ||
317 | last = 0; | ||
318 | for (var i = 0; i <= w; i++) { | ||
319 | if (((last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0)) | ||
320 | & board[x + i]) return true; | ||
321 | } | ||
322 | x += sw; | ||
323 | } | ||
324 | return false; | ||
325 | } | ||
326 | |||
327 | function cloudBounds(bounds, d) { | ||
328 | var b0 = bounds[0], | ||
329 | b1 = bounds[1]; | ||
330 | if (d.x + d.x0 < b0.x) b0.x = d.x + d.x0; | ||
331 | if (d.y + d.y0 < b0.y) b0.y = d.y + d.y0; | ||
332 | if (d.x + d.x1 > b1.x) b1.x = d.x + d.x1; | ||
333 | if (d.y + d.y1 > b1.y) b1.y = d.y + d.y1; | ||
334 | } | ||
335 | |||
336 | function collideRects(a, b) { | ||
337 | return a.x + a.x1 > b[0].x && a.x + a.x0 < b[1].x && a.y + a.y1 > b[0].y && a.y + a.y0 < b[1].y; | ||
338 | } | ||
339 | |||
340 | function archimedeanSpiral(size) { | ||
341 | var e = size[0] / size[1]; | ||
342 | return function(t) { | ||
343 | return [e * (t *= .1) * Math.cos(t), t * Math.sin(t)]; | ||
344 | }; | ||
345 | } | ||
346 | |||
347 | function rectangularSpiral(size) { | ||
348 | var dy = 4, | ||
349 | dx = dy * size[0] / size[1], | ||
350 | x = 0, | ||
351 | y = 0; | ||
352 | return function(t) { | ||
353 | var sign = t < 0 ? -1 : 1; | ||
354 | // See triangular numbers: T_n = n * (n + 1) / 2. | ||
355 | switch ((Math.sqrt(1 + 4 * sign * t) - sign) & 3) { | ||
356 | case 0: x += dx; break; | ||
357 | case 1: y += dy; break; | ||
358 | case 2: x -= dx; break; | ||
359 | default: y -= dy; break; | ||
360 | } | ||
361 | return [x, y]; | ||
362 | }; | ||
363 | } | ||
364 | |||
365 | // TODO reuse arrays? | ||
366 | function zeroArray(n) { | ||
367 | var a = [], | ||
368 | i = -1; | ||
369 | while (++i < n) a[i] = 0; | ||
370 | return a; | ||
371 | } | ||
372 | |||
373 | var cloudRadians = Math.PI / 180, | ||
374 | cw = 1 << 11 >> 5, | ||
375 | ch = 1 << 11, | ||
376 | canvas, | ||
377 | ratio = 1; | ||
378 | |||
379 | if (typeof document !== "undefined") { | ||
380 | canvas = document.createElement("canvas"); | ||
381 | canvas.width = 1; | ||
382 | canvas.height = 1; | ||
383 | ratio = Math.sqrt(canvas.getContext("2d").getImageData(0, 0, 1, 1).data.length >> 2); | ||
384 | canvas.width = (cw << 5) / ratio; | ||
385 | canvas.height = ch / ratio; | ||
386 | } else { | ||
387 | // Attempt to use node-canvas. | ||
388 | canvas = new Canvas(cw << 5, ch); | ||
389 | } | ||
390 | |||
391 | var c = canvas.getContext("2d"), | ||
392 | spirals = { | ||
393 | archimedean: archimedeanSpiral, | ||
394 | rectangular: rectangularSpiral | ||
395 | }; | ||
396 | c.fillStyle = c.strokeStyle = "red"; | ||
397 | c.textAlign = "center"; | ||
398 | |||
399 | if (typeof module === "object" && module.exports) module.exports = cloud; | ||
400 | else (d3.layout || (d3.layout = {})).cloud = cloud; | ||
401 | })(); |