|
@@ -0,0 +1,711 @@
|
|
1
|
+// A D3.js plugin that produces flame graphs from hierarchical data.
|
|
2
|
+// https://github.com/spiermar/d3-flame-graph
|
|
3
|
+// Version 1.0.11
|
|
4
|
+// See LICENSE file for license details
|
|
5
|
+
|
|
6
|
+package d3flamegraph
|
|
7
|
+
|
|
8
|
+// JSSource returns the d3.flameGraph.js file
|
|
9
|
+const JSSource = `
|
|
10
|
+/**!
|
|
11
|
+*
|
|
12
|
+* Copyright 2017 Martin Spier <spiermar@gmail.com>
|
|
13
|
+*
|
|
14
|
+* Licensed under the Apache License, Version 2.0 (the "License");
|
|
15
|
+* you may not use this file except in compliance with the License.
|
|
16
|
+* You may obtain a copy of the License at
|
|
17
|
+*
|
|
18
|
+* http://www.apache.org/licenses/LICENSE-2.0
|
|
19
|
+*
|
|
20
|
+* Unless required by applicable law or agreed to in writing, software
|
|
21
|
+* distributed under the License is distributed on an "AS IS" BASIS,
|
|
22
|
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
23
|
+* See the License for the specific language governing permissions and
|
|
24
|
+* limitations under the License.
|
|
25
|
+*
|
|
26
|
+*/
|
|
27
|
+(function() {
|
|
28
|
+ 'use strict';
|
|
29
|
+
|
|
30
|
+ /*jshint eqnull:true */
|
|
31
|
+ // https://tc39.github.io/ecma262/#sec-array.prototype.find
|
|
32
|
+ if (!Array.prototype.find) {
|
|
33
|
+ Object.defineProperty(Array.prototype, 'find', {
|
|
34
|
+ value: function(predicate) {
|
|
35
|
+ // 1. Let O be ? ToObject(this value).
|
|
36
|
+ if (this == null) {
|
|
37
|
+ throw new TypeError('"this" is null or not defined');
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ var o = Object(this);
|
|
41
|
+
|
|
42
|
+ // 2. Let len be ? ToLength(? Get(O, "length")).
|
|
43
|
+ var len = o.length >>> 0;
|
|
44
|
+
|
|
45
|
+ // 3. If IsCallable(predicate) is false, throw a TypeError exception.
|
|
46
|
+ if (typeof predicate !== 'function') {
|
|
47
|
+ throw new TypeError('predicate must be a function');
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
|
|
51
|
+ var thisArg = arguments[1];
|
|
52
|
+
|
|
53
|
+ // 5. Let k be 0.
|
|
54
|
+ var k = 0;
|
|
55
|
+
|
|
56
|
+ // 6. Repeat, while k < len
|
|
57
|
+ while (k < len) {
|
|
58
|
+ // a. Let Pk be ! ToString(k).
|
|
59
|
+ // b. Let kValue be ? Get(O, Pk).
|
|
60
|
+ // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
|
|
61
|
+ // d. If testResult is true, return kValue.
|
|
62
|
+ var kValue = o[k];
|
|
63
|
+ if (predicate.call(thisArg, kValue, k, o)) {
|
|
64
|
+ return kValue;
|
|
65
|
+ }
|
|
66
|
+ // e. Increase k by 1.
|
|
67
|
+ k++;
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ // 7. Return undefined.
|
|
71
|
+ return undefined;
|
|
72
|
+ }
|
|
73
|
+ });
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ if (!Array.prototype.filter)
|
|
77
|
+ Array.prototype.filter = function(func, thisArg) {
|
|
78
|
+ if ( ! ((typeof func === 'function') && this) )
|
|
79
|
+ throw new TypeError();
|
|
80
|
+
|
|
81
|
+ var len = this.length >>> 0,
|
|
82
|
+ res = new Array(len), // preallocate array
|
|
83
|
+ c = 0, i = -1;
|
|
84
|
+ if (thisArg === undefined)
|
|
85
|
+ while (++i !== len)
|
|
86
|
+ // checks to see if the key was set
|
|
87
|
+ if (i in this)
|
|
88
|
+ if (func(t[i], i, t))
|
|
89
|
+ res[c++] = t[i];
|
|
90
|
+ else
|
|
91
|
+ while (++i !== len)
|
|
92
|
+ // checks to see if the key was set
|
|
93
|
+ if (i in this)
|
|
94
|
+ if (func.call(thisArg, t[i], i, t))
|
|
95
|
+ res[c++] = t[i];
|
|
96
|
+
|
|
97
|
+ res.length = c; // shrink down array to proper size
|
|
98
|
+ return res;
|
|
99
|
+ };
|
|
100
|
+ /*jshint eqnull:false */
|
|
101
|
+
|
|
102
|
+ // Node/CommonJS - require D3
|
|
103
|
+ if (typeof(module) !== 'undefined' && typeof(exports) !== 'undefined' && typeof(d3) == 'undefined') {
|
|
104
|
+ d3 = require('d3');
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ // Node/CommonJS - require d3-tip
|
|
108
|
+ if (typeof(module) !== 'undefined' && typeof(exports) !== 'undefined' && typeof(d3.tip) == 'undefined') {
|
|
109
|
+ d3.tip = require('d3-tip');
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ function flameGraph() {
|
|
113
|
+
|
|
114
|
+ var w = 960, // graph width
|
|
115
|
+ h = null, // graph height
|
|
116
|
+ c = 18, // cell height
|
|
117
|
+ selection = null, // selection
|
|
118
|
+ tooltip = true, // enable tooltip
|
|
119
|
+ title = "", // graph title
|
|
120
|
+ transitionDuration = 750,
|
|
121
|
+ transitionEase = d3.easeCubic, // tooltip offset
|
|
122
|
+ sort = false,
|
|
123
|
+ reversed = false, // reverse the graph direction
|
|
124
|
+ clickHandler = null,
|
|
125
|
+ minFrameSize = 0,
|
|
126
|
+ details = null;
|
|
127
|
+
|
|
128
|
+ var tip = d3.tip()
|
|
129
|
+ .direction("s")
|
|
130
|
+ .offset([8, 0])
|
|
131
|
+ .attr('class', 'd3-flame-graph-tip')
|
|
132
|
+ .html(function(d) { return label(d); });
|
|
133
|
+
|
|
134
|
+ var svg;
|
|
135
|
+
|
|
136
|
+ function name(d) {
|
|
137
|
+ return d.data.n || d.data.name;
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ function children(d) {
|
|
141
|
+ return d.c || d.children;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ function value(d) {
|
|
145
|
+ return d.v || d.value;
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ var label = function(d) {
|
|
149
|
+ return name(d) + " (" + d3.format(".3f")(100 * (d.x1 - d.x0), 3) + "%, " + value(d) + " samples)";
|
|
150
|
+ };
|
|
151
|
+
|
|
152
|
+ function setDetails(t) {
|
|
153
|
+ if (details)
|
|
154
|
+ details.innerHTML = t;
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ var colorMapper = function(d) {
|
|
158
|
+ return d.highlight ? "#E600E6" : colorHash(name(d));
|
|
159
|
+ };
|
|
160
|
+
|
|
161
|
+ function generateHash(name) {
|
|
162
|
+ // Return a vector (0.0->1.0) that is a hash of the input string.
|
|
163
|
+ // The hash is computed to favor early characters over later ones, so
|
|
164
|
+ // that strings with similar starts have similar vectors. Only the first
|
|
165
|
+ // 6 characters are considered.
|
|
166
|
+ var hash = 0, weight = 1, max_hash = 0, mod = 10, max_char = 6;
|
|
167
|
+ if (name) {
|
|
168
|
+ for (var i = 0; i < name.length; i++) {
|
|
169
|
+ if (i > max_char) { break; }
|
|
170
|
+ hash += weight * (name.charCodeAt(i) % mod);
|
|
171
|
+ max_hash += weight * (mod - 1);
|
|
172
|
+ weight *= 0.70;
|
|
173
|
+ }
|
|
174
|
+ if (max_hash > 0) { hash = hash / max_hash; }
|
|
175
|
+ }
|
|
176
|
+ return hash;
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ function colorHash(name) {
|
|
180
|
+ // Return an rgb() color string that is a hash of the provided name,
|
|
181
|
+ // and with a warm palette.
|
|
182
|
+ var vector = 0;
|
|
183
|
+ if (name) {
|
|
184
|
+ var nameArr = name.split('` + "`" + `');
|
|
185
|
+ if (nameArr.length > 1) {
|
|
186
|
+ name = nameArr[nameArr.length -1]; // drop module name if present
|
|
187
|
+ }
|
|
188
|
+ name = name.split('(')[0]; // drop extra info
|
|
189
|
+ vector = generateHash(name);
|
|
190
|
+ }
|
|
191
|
+ var r = 200 + Math.round(55 * vector);
|
|
192
|
+ var g = 0 + Math.round(230 * (1 - vector));
|
|
193
|
+ var b = 0 + Math.round(55 * (1 - vector));
|
|
194
|
+ return "rgb(" + r + "," + g + "," + b + ")";
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ function hide(d) {
|
|
198
|
+ d.data.hide = true;
|
|
199
|
+ if(children(d)) {
|
|
200
|
+ children(d).forEach(hide);
|
|
201
|
+ }
|
|
202
|
+ }
|
|
203
|
+
|
|
204
|
+ function show(d) {
|
|
205
|
+ d.data.fade = false;
|
|
206
|
+ d.data.hide = false;
|
|
207
|
+ if(children(d)) {
|
|
208
|
+ children(d).forEach(show);
|
|
209
|
+ }
|
|
210
|
+ }
|
|
211
|
+
|
|
212
|
+ function getSiblings(d) {
|
|
213
|
+ var siblings = [];
|
|
214
|
+ if (d.parent) {
|
|
215
|
+ var me = d.parent.children.indexOf(d);
|
|
216
|
+ siblings = d.parent.children.slice(0);
|
|
217
|
+ siblings.splice(me, 1);
|
|
218
|
+ }
|
|
219
|
+ return siblings;
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+ function hideSiblings(d) {
|
|
223
|
+ var siblings = getSiblings(d);
|
|
224
|
+ siblings.forEach(function(s) {
|
|
225
|
+ hide(s);
|
|
226
|
+ });
|
|
227
|
+ if(d.parent) {
|
|
228
|
+ hideSiblings(d.parent);
|
|
229
|
+ }
|
|
230
|
+ }
|
|
231
|
+
|
|
232
|
+ function fadeAncestors(d) {
|
|
233
|
+ if(d.parent) {
|
|
234
|
+ d.parent.data.fade = true;
|
|
235
|
+ fadeAncestors(d.parent);
|
|
236
|
+ }
|
|
237
|
+ }
|
|
238
|
+
|
|
239
|
+ function getRoot(d) {
|
|
240
|
+ if(d.parent) {
|
|
241
|
+ return getRoot(d.parent);
|
|
242
|
+ }
|
|
243
|
+ return d;
|
|
244
|
+ }
|
|
245
|
+
|
|
246
|
+ function zoom(d) {
|
|
247
|
+ tip.hide(d);
|
|
248
|
+ hideSiblings(d);
|
|
249
|
+ show(d);
|
|
250
|
+ fadeAncestors(d);
|
|
251
|
+ update();
|
|
252
|
+ if (typeof clickHandler === 'function') {
|
|
253
|
+ clickHandler(d);
|
|
254
|
+ }
|
|
255
|
+ }
|
|
256
|
+
|
|
257
|
+ function searchTree(d, term) {
|
|
258
|
+ var re = new RegExp(term),
|
|
259
|
+ searchResults = [];
|
|
260
|
+
|
|
261
|
+ function searchInner(d) {
|
|
262
|
+ var label = name(d);
|
|
263
|
+
|
|
264
|
+ if (children(d)) {
|
|
265
|
+ children(d).forEach(function (child) {
|
|
266
|
+ searchInner(child);
|
|
267
|
+ });
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ if (label.match(re)) {
|
|
271
|
+ d.highlight = true;
|
|
272
|
+ searchResults.push(d);
|
|
273
|
+ } else {
|
|
274
|
+ d.highlight = false;
|
|
275
|
+ }
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+ searchInner(d);
|
|
279
|
+ return searchResults;
|
|
280
|
+ }
|
|
281
|
+
|
|
282
|
+ function clear(d) {
|
|
283
|
+ d.highlight = false;
|
|
284
|
+ if(children(d)) {
|
|
285
|
+ children(d).forEach(function(child) {
|
|
286
|
+ clear(child);
|
|
287
|
+ });
|
|
288
|
+ }
|
|
289
|
+ }
|
|
290
|
+
|
|
291
|
+ function doSort(a, b) {
|
|
292
|
+ if (typeof sort === 'function') {
|
|
293
|
+ return sort(a, b);
|
|
294
|
+ } else if (sort) {
|
|
295
|
+ return d3.ascending(name(a), name(b));
|
|
296
|
+ }
|
|
297
|
+ }
|
|
298
|
+
|
|
299
|
+ var partition = d3.partition();
|
|
300
|
+
|
|
301
|
+ function filterNodes(root) {
|
|
302
|
+ var nodeList = root.descendants();
|
|
303
|
+ if (minFrameSize > 0) {
|
|
304
|
+ var kx = w / (root.x1 - root.x0);
|
|
305
|
+ nodeList = nodeList.filter(function(el) {
|
|
306
|
+ return ((el.x1 - el.x0) * kx) > minFrameSize;
|
|
307
|
+ });
|
|
308
|
+ }
|
|
309
|
+ return nodeList;
|
|
310
|
+ }
|
|
311
|
+
|
|
312
|
+ function update() {
|
|
313
|
+ selection.each(function(root) {
|
|
314
|
+ var x = d3.scaleLinear().range([0, w]),
|
|
315
|
+ y = d3.scaleLinear().range([0, c]);
|
|
316
|
+
|
|
317
|
+ if (sort) root.sort(doSort);
|
|
318
|
+ root.sum(function(d) {
|
|
319
|
+ if (d.fade || d.hide) {
|
|
320
|
+ return 0;
|
|
321
|
+ }
|
|
322
|
+ // The node's self value is its total value minus all children.
|
|
323
|
+ var v = value(d);
|
|
324
|
+ if (children(d)) {
|
|
325
|
+ var c = children(d);
|
|
326
|
+ for (var i = 0; i < c.length; i++) {
|
|
327
|
+ v -= value(c[i]);
|
|
328
|
+ }
|
|
329
|
+ }
|
|
330
|
+ return v;
|
|
331
|
+ });
|
|
332
|
+ partition(root);
|
|
333
|
+
|
|
334
|
+ var kx = w / (root.x1 - root.x0);
|
|
335
|
+ function width(d) { return (d.x1 - d.x0) * kx; }
|
|
336
|
+
|
|
337
|
+ var descendants = filterNodes(root);
|
|
338
|
+ var g = d3.select(this).select("svg").selectAll("g").data(descendants, function(d) { return d.id; });
|
|
339
|
+
|
|
340
|
+ g.transition()
|
|
341
|
+ .duration(transitionDuration)
|
|
342
|
+ .ease(transitionEase)
|
|
343
|
+ .attr("transform", function(d) { return "translate(" + x(d.x0) + "," + (reversed ? y(d.depth) : (h - y(d.depth) - c)) + ")"; });
|
|
344
|
+
|
|
345
|
+ g.select("rect")
|
|
346
|
+ .attr("width", width);
|
|
347
|
+
|
|
348
|
+ var node = g.enter()
|
|
349
|
+ .append("svg:g")
|
|
350
|
+ .attr("transform", function(d) { return "translate(" + x(d.x0) + "," + (reversed ? y(d.depth) : (h - y(d.depth) - c)) + ")"; });
|
|
351
|
+
|
|
352
|
+ node.append("svg:rect")
|
|
353
|
+ .transition()
|
|
354
|
+ .delay(transitionDuration / 2)
|
|
355
|
+ .attr("width", width);
|
|
356
|
+
|
|
357
|
+ if (!tooltip)
|
|
358
|
+ node.append("svg:title");
|
|
359
|
+
|
|
360
|
+ node.append("foreignObject")
|
|
361
|
+ .append("xhtml:div");
|
|
362
|
+
|
|
363
|
+ // Now we have to re-select to see the new elements (why?).
|
|
364
|
+ g = d3.select(this).select("svg").selectAll("g").data(descendants, function(d) { return d.id; });
|
|
365
|
+
|
|
366
|
+ g.attr("width", width)
|
|
367
|
+ .attr("height", function(d) { return c; })
|
|
368
|
+ .attr("name", function(d) { return name(d); })
|
|
369
|
+ .attr("class", function(d) { return d.data.fade ? "frame fade" : "frame"; });
|
|
370
|
+
|
|
371
|
+ g.select("rect")
|
|
372
|
+ .attr("height", function(d) { return c; })
|
|
373
|
+ .attr("fill", function(d) { return colorMapper(d); });
|
|
374
|
+
|
|
375
|
+ if (!tooltip)
|
|
376
|
+ g.select("title")
|
|
377
|
+ .text(label);
|
|
378
|
+
|
|
379
|
+ g.select("foreignObject")
|
|
380
|
+ .attr("width", width)
|
|
381
|
+ .attr("height", function(d) { return c; })
|
|
382
|
+ .select("div")
|
|
383
|
+ .attr("class", "d3-flame-graph-label")
|
|
384
|
+ .style("display", function(d) { return (width(d) < 35) ? "none" : "block";})
|
|
385
|
+ .transition()
|
|
386
|
+ .delay(transitionDuration)
|
|
387
|
+ .text(name);
|
|
388
|
+
|
|
389
|
+ g.on('click', zoom);
|
|
390
|
+
|
|
391
|
+ g.exit()
|
|
392
|
+ .remove();
|
|
393
|
+
|
|
394
|
+ g.on('mouseover', function(d) {
|
|
395
|
+ if (tooltip) tip.show(d);
|
|
396
|
+ setDetails(label(d));
|
|
397
|
+ }).on('mouseout', function(d) {
|
|
398
|
+ if (tooltip) tip.hide(d);
|
|
399
|
+ setDetails("");
|
|
400
|
+ });
|
|
401
|
+ });
|
|
402
|
+ }
|
|
403
|
+
|
|
404
|
+ function merge(data, samples) {
|
|
405
|
+ samples.forEach(function (sample) {
|
|
406
|
+ var node = data.find(function (element) {
|
|
407
|
+ return (element.name === sample.name);
|
|
408
|
+ });
|
|
409
|
+
|
|
410
|
+ if (node) {
|
|
411
|
+ if (node.original) {
|
|
412
|
+ node.original += sample.value;
|
|
413
|
+ } else {
|
|
414
|
+ node.value += sample.value;
|
|
415
|
+ }
|
|
416
|
+ if (sample.children) {
|
|
417
|
+ if (!node.children) {
|
|
418
|
+ node.children = [];
|
|
419
|
+ }
|
|
420
|
+ merge(node.children, sample.children);
|
|
421
|
+ }
|
|
422
|
+ } else {
|
|
423
|
+ data.push(sample);
|
|
424
|
+ }
|
|
425
|
+ });
|
|
426
|
+ }
|
|
427
|
+
|
|
428
|
+ function s4() {
|
|
429
|
+ return Math.floor((1 + Math.random()) * 0x10000)
|
|
430
|
+ .toString(16)
|
|
431
|
+ .substring(1);
|
|
432
|
+ }
|
|
433
|
+
|
|
434
|
+ function injectIds(node) {
|
|
435
|
+ node.id = s4() + "-" + s4() + "-" + "-" + s4() + "-" + s4();
|
|
436
|
+ var children = node.c || node.children || [];
|
|
437
|
+ for (var i = 0; i < children.length; i++) {
|
|
438
|
+ injectIds(children[i]);
|
|
439
|
+ }
|
|
440
|
+ }
|
|
441
|
+
|
|
442
|
+ function chart(s) {
|
|
443
|
+ var root = d3.hierarchy(
|
|
444
|
+ s.datum(), function(d) { return children(d); }
|
|
445
|
+ );
|
|
446
|
+ injectIds(root);
|
|
447
|
+ selection = s.datum(root);
|
|
448
|
+
|
|
449
|
+ if (!arguments.length) return chart;
|
|
450
|
+
|
|
451
|
+ if (!h) {
|
|
452
|
+ h = (root.height + 2) * c;
|
|
453
|
+ }
|
|
454
|
+
|
|
455
|
+ selection.each(function(data) {
|
|
456
|
+
|
|
457
|
+ if (!svg) {
|
|
458
|
+ svg = d3.select(this)
|
|
459
|
+ .append("svg:svg")
|
|
460
|
+ .attr("width", w)
|
|
461
|
+ .attr("height", h)
|
|
462
|
+ .attr("class", "partition d3-flame-graph")
|
|
463
|
+ .call(tip);
|
|
464
|
+
|
|
465
|
+ svg.append("svg:text")
|
|
466
|
+ .attr("class", "title")
|
|
467
|
+ .attr("text-anchor", "middle")
|
|
468
|
+ .attr("y", "25")
|
|
469
|
+ .attr("x", w/2)
|
|
470
|
+ .attr("fill", "#808080")
|
|
471
|
+ .text(title);
|
|
472
|
+ }
|
|
473
|
+ });
|
|
474
|
+
|
|
475
|
+ // first draw
|
|
476
|
+ update();
|
|
477
|
+ }
|
|
478
|
+
|
|
479
|
+ chart.height = function (_) {
|
|
480
|
+ if (!arguments.length) { return h; }
|
|
481
|
+ h = _;
|
|
482
|
+ return chart;
|
|
483
|
+ };
|
|
484
|
+
|
|
485
|
+ chart.width = function (_) {
|
|
486
|
+ if (!arguments.length) { return w; }
|
|
487
|
+ w = _;
|
|
488
|
+ return chart;
|
|
489
|
+ };
|
|
490
|
+
|
|
491
|
+ chart.cellHeight = function (_) {
|
|
492
|
+ if (!arguments.length) { return c; }
|
|
493
|
+ c = _;
|
|
494
|
+ return chart;
|
|
495
|
+ };
|
|
496
|
+
|
|
497
|
+ chart.tooltip = function (_) {
|
|
498
|
+ if (!arguments.length) { return tooltip; }
|
|
499
|
+ if (typeof _ === "function") {
|
|
500
|
+ tip = _;
|
|
501
|
+ }
|
|
502
|
+ tooltip = !!_;
|
|
503
|
+ return chart;
|
|
504
|
+ };
|
|
505
|
+
|
|
506
|
+ chart.title = function (_) {
|
|
507
|
+ if (!arguments.length) { return title; }
|
|
508
|
+ title = _;
|
|
509
|
+ return chart;
|
|
510
|
+ };
|
|
511
|
+
|
|
512
|
+ chart.transitionDuration = function (_) {
|
|
513
|
+ if (!arguments.length) { return transitionDuration; }
|
|
514
|
+ transitionDuration = _;
|
|
515
|
+ return chart;
|
|
516
|
+ };
|
|
517
|
+
|
|
518
|
+ chart.transitionEase = function (_) {
|
|
519
|
+ if (!arguments.length) { return transitionEase; }
|
|
520
|
+ transitionEase = _;
|
|
521
|
+ return chart;
|
|
522
|
+ };
|
|
523
|
+
|
|
524
|
+ chart.sort = function (_) {
|
|
525
|
+ if (!arguments.length) { return sort; }
|
|
526
|
+ sort = _;
|
|
527
|
+ return chart;
|
|
528
|
+ };
|
|
529
|
+
|
|
530
|
+ chart.reversed = function (_) {
|
|
531
|
+ if (!arguments.length) { return reversed; }
|
|
532
|
+ reversed = _;
|
|
533
|
+ return chart;
|
|
534
|
+ };
|
|
535
|
+
|
|
536
|
+ chart.label = function(_) {
|
|
537
|
+ if (!arguments.length) { return label; }
|
|
538
|
+ label = _;
|
|
539
|
+ return chart;
|
|
540
|
+ };
|
|
541
|
+
|
|
542
|
+ chart.search = function(term) {
|
|
543
|
+ var searchResults = [];
|
|
544
|
+ selection.each(function(data) {
|
|
545
|
+ searchResults = searchTree(data, term);
|
|
546
|
+ update();
|
|
547
|
+ });
|
|
548
|
+ return searchResults;
|
|
549
|
+ };
|
|
550
|
+
|
|
551
|
+ chart.clear = function() {
|
|
552
|
+ selection.each(function(data) {
|
|
553
|
+ clear(data);
|
|
554
|
+ update();
|
|
555
|
+ });
|
|
556
|
+ };
|
|
557
|
+
|
|
558
|
+ chart.zoomTo = function(d) {
|
|
559
|
+ zoom(d);
|
|
560
|
+ };
|
|
561
|
+
|
|
562
|
+ chart.resetZoom = function() {
|
|
563
|
+ selection.each(function (data) {
|
|
564
|
+ zoom(data); // zoom to root
|
|
565
|
+ });
|
|
566
|
+ };
|
|
567
|
+
|
|
568
|
+ chart.onClick = function(_) {
|
|
569
|
+ if (!arguments.length) {
|
|
570
|
+ return clickHandler;
|
|
571
|
+ }
|
|
572
|
+ clickHandler = _;
|
|
573
|
+ return chart;
|
|
574
|
+ };
|
|
575
|
+
|
|
576
|
+ chart.merge = function(samples) {
|
|
577
|
+ var newRoot; // Need to re-create hierarchy after data changes.
|
|
578
|
+ selection.each(function (root) {
|
|
579
|
+ merge([root.data], [samples]);
|
|
580
|
+ newRoot = d3.hierarchy(root.data, function(d) { return children(d); });
|
|
581
|
+ injectIds(newRoot);
|
|
582
|
+ });
|
|
583
|
+ selection = selection.datum(newRoot);
|
|
584
|
+ update();
|
|
585
|
+ };
|
|
586
|
+
|
|
587
|
+ chart.color = function(_) {
|
|
588
|
+ if (!arguments.length) { return colorMapper; }
|
|
589
|
+ colorMapper = _;
|
|
590
|
+ return chart;
|
|
591
|
+ };
|
|
592
|
+
|
|
593
|
+ chart.minFrameSize = function (_) {
|
|
594
|
+ if (!arguments.length) { return minFrameSize; }
|
|
595
|
+ minFrameSize = _;
|
|
596
|
+ return chart;
|
|
597
|
+ };
|
|
598
|
+
|
|
599
|
+ chart.details = function (_) {
|
|
600
|
+ if (!arguments.length) { return details; }
|
|
601
|
+ details = _;
|
|
602
|
+ return chart;
|
|
603
|
+ };
|
|
604
|
+
|
|
605
|
+ return chart;
|
|
606
|
+ }
|
|
607
|
+
|
|
608
|
+ // Node/CommonJS exports
|
|
609
|
+ if (typeof(module) !== 'undefined' && typeof(exports) !== 'undefined') {
|
|
610
|
+ module.exports = flameGraph;
|
|
611
|
+ } else {
|
|
612
|
+ d3.flameGraph = flameGraph;
|
|
613
|
+ }
|
|
614
|
+})();
|
|
615
|
+`
|
|
616
|
+
|
|
617
|
+// CSSSource returns the d3.flameGraph.css file
|
|
618
|
+const CSSSource = `
|
|
619
|
+.d3-flame-graph rect {
|
|
620
|
+ stroke: #EEEEEE;
|
|
621
|
+ fill-opacity: .8;
|
|
622
|
+}
|
|
623
|
+
|
|
624
|
+.d3-flame-graph rect:hover {
|
|
625
|
+ stroke: #474747;
|
|
626
|
+ stroke-width: 0.5;
|
|
627
|
+ cursor: pointer;
|
|
628
|
+}
|
|
629
|
+
|
|
630
|
+.d3-flame-graph-label {
|
|
631
|
+ pointer-events: none;
|
|
632
|
+ white-space: nowrap;
|
|
633
|
+ text-overflow: ellipsis;
|
|
634
|
+ overflow: hidden;
|
|
635
|
+ font-size: 12px;
|
|
636
|
+ font-family: Verdana;
|
|
637
|
+ margin-left: 4px;
|
|
638
|
+ margin-right: 4px;
|
|
639
|
+ line-height: 1.5;
|
|
640
|
+ padding: 0 0 0;
|
|
641
|
+ font-weight: 400;
|
|
642
|
+ color: black;
|
|
643
|
+ text-align: left;
|
|
644
|
+}
|
|
645
|
+
|
|
646
|
+.d3-flame-graph .fade {
|
|
647
|
+ opacity: 0.6 !important;
|
|
648
|
+}
|
|
649
|
+
|
|
650
|
+.d3-flame-graph .title {
|
|
651
|
+ font-size: 20px;
|
|
652
|
+ font-family: Verdana;
|
|
653
|
+}
|
|
654
|
+
|
|
655
|
+.d3-flame-graph-tip {
|
|
656
|
+ line-height: 1;
|
|
657
|
+ font-family: Verdana;
|
|
658
|
+ font-size: 12px;
|
|
659
|
+ padding: 12px;
|
|
660
|
+ background: rgba(0, 0, 0, 0.8);
|
|
661
|
+ color: #fff;
|
|
662
|
+ border-radius: 2px;
|
|
663
|
+ pointer-events: none;
|
|
664
|
+}
|
|
665
|
+
|
|
666
|
+/* Creates a small triangle extender for the tooltip */
|
|
667
|
+.d3-flame-graph-tip:after {
|
|
668
|
+ box-sizing: border-box;
|
|
669
|
+ display: inline;
|
|
670
|
+ font-size: 10px;
|
|
671
|
+ width: 100%;
|
|
672
|
+ line-height: 1;
|
|
673
|
+ color: rgba(0, 0, 0, 0.8);
|
|
674
|
+ position: absolute;
|
|
675
|
+ pointer-events: none;
|
|
676
|
+}
|
|
677
|
+
|
|
678
|
+/* Northward tooltips */
|
|
679
|
+.d3-flame-graph-tip.n:after {
|
|
680
|
+ content: "\25BC";
|
|
681
|
+ margin: -1px 0 0 0;
|
|
682
|
+ top: 100%;
|
|
683
|
+ left: 0;
|
|
684
|
+ text-align: center;
|
|
685
|
+}
|
|
686
|
+
|
|
687
|
+/* Eastward tooltips */
|
|
688
|
+.d3-flame-graph-tip.e:after {
|
|
689
|
+ content: "\25C0";
|
|
690
|
+ margin: -4px 0 0 0;
|
|
691
|
+ top: 50%;
|
|
692
|
+ left: -8px;
|
|
693
|
+}
|
|
694
|
+
|
|
695
|
+/* Southward tooltips */
|
|
696
|
+.d3-flame-graph-tip.s:after {
|
|
697
|
+ content: "\25B2";
|
|
698
|
+ margin: 0 0 1px 0;
|
|
699
|
+ top: -8px;
|
|
700
|
+ left: 0;
|
|
701
|
+ text-align: center;
|
|
702
|
+}
|
|
703
|
+
|
|
704
|
+/* Westward tooltips */
|
|
705
|
+.d3-flame-graph-tip.w:after {
|
|
706
|
+ content: "\25B6";
|
|
707
|
+ margin: -4px 0 0 -1px;
|
|
708
|
+ top: 50%;
|
|
709
|
+ left: 100%;
|
|
710
|
+}
|
|
711
|
+`
|