-
Notifications
You must be signed in to change notification settings - Fork 3
/
fm_fact_label.js
761 lines (696 loc) · 28 KB
/
fm_fact_label.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
const POINT_TO_PIXEL = 1.3281472327365;
//const TITLE_FONT_FAMILY = "Franklin Gothic Heavy";
const TITLE_FONT_FAMILY = "'Libre Franklin', sans-serif";
const TITLE_FONT_SIZE = "24pt";
const DESCRIPTION_FONT_FAMILY = "Helvetica";
const DESCRIPTION_FONT_SIZE = "8pt";
const PROPERTY_FONT_FAMILY = "Helvetica";
const PROPERTY_FONT_SIZE = "12pt";
const VALUES_FONT_FAMILY = "Helvetica";
const VALUES_FONT_SIZE = "10pt";
const COLLAPSEICON_FONT_SIZE = "8pt";
const PROPERTY_INDENTATION = 2;
const TOP_MARGING = 20;
const LEFT_MARGING = 5;
const MAIN_RULE_HEIGHT = 7 * POINT_TO_PIXEL;
const SECONDARY_RULE_HEIGHT = .25 * POINT_TO_PIXEL;
const MARGING_BETWEEN_PROPERTIES = 3;
const PROPERTIES_VALUES_SPACE = 10;
const PROPERTIES_RATIO_SPACE = 3;
const EXPANDED_ICON = '\uf150';
const COLLAPSED_ICON = '\uf152';
const HREF_ICON = '\uf0ac';
// GLOBAL VARIABLES
var maxWidth;
var currentHeight;
var maxIndentationWidth;
var maxNameWidth
var maxValueWidth;
var maxRatioWidth;
var PROPERTY_HEIGHT;
var x;
var yRule1;
var yMetrics;
var tooltip;
var contentDetail;
var VISIBLE_PROPERTIES = {};
var ALL_DATA;
var chart;
var IMPORTS = ['https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@900',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css'];
function drawFMFactLabel(data) {
chart = d3.select(".chart"); // The svg
// chart.append('defs')
// .append('style')
// .attr('type', 'text/css')
// .text("@import url('https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@900');");
chart.selectAll('defs')
.data(IMPORTS, d => d)
.join("style")
.attr('type', 'text/css')
.text(function (d) { return "@import url('" + d + "');"; });
// Create a div for mouse hover effect
tooltip = d3
.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0)
.style("position", "absolute")
.style("text-align", "left")
.style("padding", "0.1rem")
.style("background", "#FFFFFF")
.style("color", "#313639")
.style("border", "1px solid #313639")
.style("border-radius", "8px")
.style("pointer-events", "none")
.style("font-size", "0.8rem");
// Create a div to show the content detail on mouse click
contentDetail = d3
.select("body")
.append("div")
.attr("class", "contentDetail")
.style("opacity", 0)
.style("position", "absolute")
.style("text-align", "left")
.style("padding", "0.1rem")
.style("background", "#FFFFFF")
.style("color", "#313639")
.style("border", "1px solid #313639")
.style("border-radius", "8px")
.style("font-size", "0.8rem")
//.style("width", "400px")
.on("mouseout", function (event, d) {
d3.select(this).transition().duration("50").style("opacity", 0);
});
ALL_DATA = data
// Initialize visible properties
for (let p of data.metadata) { VISIBLE_PROPERTIES[p.name] = true; }
for (let p of data.metrics) { VISIBLE_PROPERTIES[p.name] = true; }
for (let p of data.analysis) { VISIBLE_PROPERTIES[p.name] = true; }
// Calculate maximum width for the label.
//var maxWidth = Math.max(calculateTotalMaxWidth(data.metrics), calculateTotalMaxWidth(data.analysis));
PROPERTY_HEIGHT = textSize("Any text", PROPERTY_FONT_FAMILY, PROPERTY_FONT_SIZE, "bold").height;// + MARGING_BETWEEN_PROPERTIES;
maxIndentationWidth = Math.max(calculateMaxIndentationWidth(data.metrics), calculateMaxIndentationWidth(data.analysis));
maxNameWidth = Math.max(calculateMaxNameWidth(data.metrics), calculateMaxNameWidth(data.analysis));
maxValueWidth = Math.max(calculateMaxValueWidth(data.metrics), calculateMaxValueWidth(data.analysis));
maxRatioWidth = Math.max(calculateMaxRatioWidth(data.metrics), calculateMaxRatioWidth(data.analysis));
maxWidth = maxIndentationWidth + maxNameWidth + PROPERTIES_VALUES_SPACE + maxValueWidth + PROPERTIES_RATIO_SPACE + maxRatioWidth + LEFT_MARGING; //textSize("-".repeat(PROPERTY_INDENTATION), PROPERTY_FONT_FAMILY, PROPERTY_FONT_SIZE).width;
chart.attr("width", maxWidth);
//.attr("height", BAR_HEIGHT * 10 + BAR_HEIGHT * data.metadata.length + BAR_HEIGHT * data.metrics.length); // CAMBIAR EL *10 AJUSTANDOLO BIEN
x = d3.scaleLinear().domain([0, maxWidth]).range([0, maxWidth]);
// Title
var titleSize = textSize(get_property(data, "Name").value, TITLE_FONT_FAMILY, TITLE_FONT_SIZE);
var yTitle = TOP_MARGING;
var title = chart.append("g").attr("transform", "translate(0," + yTitle + ")");
title.append("text")
.text(get_property(data, 'Name').value)
.attr("x", function (d) { return x(maxWidth / 2); })
//.attr("y", 3)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "central")
.attr("font-family", TITLE_FONT_FAMILY)
.attr("font-size", TITLE_FONT_SIZE)
.attr("font-weight", "bold");
// Description
var yDescription = yTitle + titleSize.height + 1;
var indentationDescription = textSize("-".repeat(PROPERTY_INDENTATION), DESCRIPTION_FONT_FAMILY, DESCRIPTION_FONT_SIZE).width;
var description = chart.append("g").attr("transform", "translate(0," + yDescription + ")");
description.append("text")
.text(get_property(data, 'Description').value)
.attr("x", function (d) { return x(indentationDescription) })
//.attr("y", BAR_HEIGHT / 2)
.attr("font-family", DESCRIPTION_FONT_FAMILY)
.attr("font-size", DESCRIPTION_FONT_SIZE)
.call(wrap, maxWidth - indentationDescription);
var descriptionSize = description.node().getBBox();
// Keywords
var keywordHeight = yDescription + descriptionSize.height + 1;
if (get_property(data, 'Tags').value === null) {
var keywordsSize = descriptionSize;
} else {
var keywords = chart.append("g").attr("transform", "translate(0," + keywordHeight + ")");
addMetadata(keywords, "Tags:", get_property(data, 'Tags').value);
var keywordsSize = keywords.node().getBBox();
}
// Author
var authorHeight = keywordHeight + keywordsSize.height;
if (get_property(data, 'Author').value === null) {
var authorSize = { "width": 0, "height": 0 };
} else {
var author = chart.append("g").attr("transform", "translate(0," + authorHeight + ")");
addMetadata(author, "Author:", get_property(data, 'Author').value);
var authorSize = author.node().getBBox();
}
// Year
var yearHeight = authorHeight + authorSize.height;
if (get_property(data, 'Year').value === null) {
var yearSize = { "width": 0, "height": 0 };
} else {
var year = chart.append("g").attr("transform", "translate(0," + yearHeight + ")");
addMetadata(year, "Year:", get_property(data, 'Year').value);
var yearSize = year.node().getBBox();
}
// Domain
var domainHeight = yearHeight + yearSize.height;
if (get_property(data, 'Domain').value === null) {
var domainSize = { "width": 0, "height": 0 };
} else {
var domain = chart.append("g").attr("transform", "translate(0," + domainHeight + ")");
addMetadata(domain, "Domain:", get_property(data, 'Domain').value);
var domainSize = domain.node().getBBox();
}
// Reference
if (!get_property(data, 'Reference').value == "") {
var reference = chart.append("g").attr("transform", "translate(0," + (domainHeight + domainSize.height - MAIN_RULE_HEIGHT - 10) + ")");
reference.append('a')
.attr("id", "hrefIcon")
.attr("href", get_property(data, 'Reference').value)
.append("text")
.attr("text-anchor", "end")
.attr("x", maxWidth - 5)
.attr("dy", ".35em")
.attr("y", PROPERTY_HEIGHT / 2)
.attr('font-family', 'FontAwesome')
.attr('font-size', "12pt")
.text(HREF_ICON)
.attr("cursor", "pointer");
}
// Middle rule 1
yRule1 = domainHeight + domainSize.height;
chart.append("g").attr("id", "rule1");
drawRule("rule1", yRule1);
// Metrics
yMetrics = yRule1 + MAIN_RULE_HEIGHT;
chart.append("g").attr("id", "metrics").attr("transform", "translate(0," + yMetrics + ")");
updateProperties(data.metrics, "metrics");
// Middle rule 2
var yRule2 = yMetrics + PROPERTY_HEIGHT * data.metrics.length;
chart.append("g").attr("id", "rule2");
drawRule("rule2", yRule2);
// Analysis
var yAnalysis = yRule2 + MAIN_RULE_HEIGHT;
chart.append("g").attr("id", "analysis").attr("transform", "translate(0," + yAnalysis + ")");
updateProperties(data.analysis, "analysis");
// Border of the label
var maxHeight = yAnalysis + MARGING_BETWEEN_PROPERTIES + PROPERTY_HEIGHT * data.analysis.length;
chart.append("rect").attr("id", "border");
drawBorders(maxWidth, maxHeight);
chart.attr("height", maxHeight);
// Set the configuration options
d3.select("#collapseZeroValues").on("change", function () { collapseZeroValues(data); });
d3.select("#collapseSubProperties").on("change", function () { collapseSubProperties(data); });
//d3.selectAll("#collapse").on("click", function (d) { collapseProperty(data, d); });
collapseSubProperties(data);
}
function addMetadata(element, key, value) {
element.append("text")
.text(key)
.attr("x", function (d) { return x(PROPERTY_INDENTATION * 3) })
.attr("font-family", DESCRIPTION_FONT_FAMILY)
.attr("font-size", DESCRIPTION_FONT_SIZE)
.attr("font-weight", "bold");
element.append("text")
.text(value)
.attr("x", function (d) { return x(6 * PROPERTY_INDENTATION + textSize(key, DESCRIPTION_FONT_FAMILY, DESCRIPTION_FONT_SIZE).width); })
.attr("font-family", DESCRIPTION_FONT_FAMILY)
.attr("font-size", DESCRIPTION_FONT_SIZE)
.call(wrap, maxWidth - (6 * PROPERTY_INDENTATION + textSize(key, DESCRIPTION_FONT_FAMILY, DESCRIPTION_FONT_SIZE).width));
}
function updateProperties(data, id) {
d3.select("#" + id)
.selectAll("g")
.data(data, d => d)
.join(
function (enter) {
// Indentation
var property = enter.append("g").attr("id", function (d) { return d.name; }).attr("transform", function (d, i) { return "translate(0," + i * PROPERTY_HEIGHT + ")"; });
property.append("rect")
.attr("id", "indentation")
.attr("x", function (d) { return x(0); })
.attr("y", PROPERTY_HEIGHT)
.attr("dy", ".35em")
.attr("width", function (d) { return get_indentation(d); })
.attr("height", PROPERTY_HEIGHT)
.attr("fill", "white");
var collapseIcon = property.append('text')
.attr("id", "collapseIcon")
.attr("x", function (d) { return get_indentation(d); })
.attr("dy", ".35em")
.attr("y", PROPERTY_HEIGHT / 2)
.attr('font-family', 'FontAwesome')
.attr('font-size', COLLAPSEICON_FONT_SIZE)
.text(function (d) { return hasChildrenProperties(d) && getChildrenProperties(data, d, false).length == 0 ? COLLAPSED_ICON : EXPANDED_ICON; })
.attr("visibility", function (d) { return hasChildrenProperties(d) ? "visible" : "hidden"; })
.attr("cursor", "pointer")
.on("click", function (p, d) { hasChildrenProperties(d) && getChildrenProperties(data, d, false).length == 0 ? expandProperty(ALL_DATA, d) : collapseProperty(ALL_DATA, d); });
var collapseIconWidth = collapseIcon.node() === null ? 0 : collapseIcon.node().getBBox().width;
// Property name
property
.append("text")
.attr("id", "propertyName")
.attr("text-anchor", "start")
.attr("x", function (d) {
return (
get_indentation(d) + collapseIconWidth + PROPERTY_INDENTATION
);
})
.attr("y", PROPERTY_HEIGHT / 2)
.attr("dy", ".35em")
.attr("font-family", PROPERTY_FONT_FAMILY)
.attr("font-size", PROPERTY_FONT_SIZE)
.attr("font-weight", function (d) {
return parseInt(d.level, 10) == 0 ? "bold" : "normal";
})
.attr("cursor", "pointer")
.text(function (d) {
return d.name;
})
.on("mouseover", function (event, d) {
d3.select(this).transition().duration("50").attr("opacity", 0.85);
//Makes the new div appear on hover:
tooltip.transition().duration(50).style("opacity", 1);
tooltip
.html(d.description)
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY - 15 + "px");
})
.on("mouseout", function (event, d) {
d3.select(this).transition().duration("50").attr("opacity", 1);
//Makes the new div disappear:
tooltip.transition().duration("50").style("opacity", 0);
})
.on("click", function (event, d) {
// Hide the tooltip
tooltip.transition().duration("50").style("opacity", 0);
// Show the modal
showMetricModal(d);
});
// Property value (size)
property.append("text")
.attr("id", "value")
.attr("text-anchor", "end")
.attr("x", function (d) { return x(maxIndentationWidth + maxNameWidth + PROPERTIES_VALUES_SPACE + maxValueWidth); })
.attr("y", PROPERTY_HEIGHT / 2)
.attr("dy", ".35em")
.attr("font-family", PROPERTY_FONT_FAMILY)
.attr("font-size", VALUES_FONT_SIZE)
.attr("font-weight", "bold")
.text(function (d) { return get_value(d); });
// Property ratio
property.append("text")
.attr("id", "ratio")
.attr("text-anchor", "end")
.attr("x", function (d) { return x(maxWidth - LEFT_MARGING); })
.attr("y", PROPERTY_HEIGHT / 2)
.attr("dy", ".35em")
.attr("font-family", PROPERTY_FONT_FAMILY)
.attr("font-size", VALUES_FONT_SIZE)
.attr("font-weight", "bold")
.text(function (d) { return get_ratio(d); });
return property;
},
function (update) {
update.select("#collapseIcon")
.text(function (d) { return hasChildrenProperties(d) && getChildrenProperties(data, d).length == 0 ? COLLAPSED_ICON : EXPANDED_ICON; })
.attr("visibility", function (d) { return hasChildrenProperties(d) ? "visible" : "hidden"; })
.on("click", function (p, d) { hasChildrenProperties(d) && getChildrenProperties(data, d).length == 0 ? expandProperty(ALL_DATA, d) : collapseProperty(ALL_DATA, d); });
return update;
},
function (exit) {
return exit.remove();
}
);
drawSecondaryRules(data)
}
function drawRule(id, yPosition) {
d3.select("#" + id)
.attr("transform", "translate(0," + yPosition + ")")
.append("rect")
.attr("height", MAIN_RULE_HEIGHT)
.attr("width", maxWidth);
}
function drawBorders(width, height) {
d3.select("#border")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height)
.style("stroke", "black")
.style("fill", "none")
.style("stroke-width", "3pt");
}
/**
*
* @param {any} d A FM property.
* @returns The value for the property.
*/
function get_value(d) {
return d.size === null ? d.value : d.size;
}
/**
*
* @param {any} d A FM property.
* @returns The percentage for the property.
*/
function get_ratio(d) {
return d.ratio === null ? "" : "(" + Math.round((d.ratio + Number.EPSILON) * 100) + "%)";
}
/**
*
* @param {any} d A FM property.
* @returns The indentation width.
*/
function get_indentation(d) {
return textSize("-".repeat(1 + PROPERTY_INDENTATION * parseInt(d.level, 10)), PROPERTY_FONT_FAMILY, PROPERTY_FONT_SIZE).width;
}
/**
*
* @param {Array} data Dataset.
* @param {String} propertyName Property's name.
* @returns The property by its name.
*/
function get_property(data, propertyName) {
for (let p of data.metadata) {
if (p.name == propertyName) {
return p;
}
}
for (let p of data.metrics) {
if (p.name == propertyName) {
return p;
}
}
for (let p of data.analysis) {
if (p.name == propertyName) {
return p;
}
}
}
function get_property_in_data(data, propertyName) {
for (let p of data) {
if (p.name == propertyName) {
return p;
}
}
return null;
}
/**
* Wrap the text D3 Object to the given width.
*
* @param {Object} text Text to be wrapped.
* @param {String} width Width used for wrapping.
*/
function wrap(text, width) {
text.each(function () {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0, //<-- 0!
lineHeight = 1.2, // ems
x = text.attr("x"), //<-- include the x!
y = text.attr("y"),
dy = text.attr("dy") ? text.attr("dy") : 0; //<-- null check
tspan = text.text(null).append("tspan").attr("x", x).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", x).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
/**
* Measure text size in pixels with D3.js
*
* Usage: textSize("This is a very long text");
* => Return: Object {width: 140, height: 15.453125}
*
* @param {String} text
* @param {String} fontFamily
* @param {String} fontSize
* @param {String} fontWeight
* @returns Object including width and height.
*/
function textSize(text, fontFamily, fontSize, fontWeight = "normal") {
var container = d3.select('body').append('svg');
container.append('text').text(text).attr("font-family", fontFamily).attr("font-size", fontSize).attr("font-weight", fontWeight);
var size = container.node().getBBox();
container.remove();
return { width: size.width, height: size.height };
}
/**
*
* @param {Array} data Array of properties.
* @param {any} property Property.
* @returns List of children of the property.
*/
function getChildrenProperties(data, property, recursively) {
var children = [];
for (let p of data) {
if (p.parent == property.name) {
children.push(p);
}
}
if (recursively) {
for (let c of children) {
subChildren = getChildrenProperties(data, c);
for (let sb of subChildren) {
children.push(sb);
}
}
}
return children;
}
function hasChildrenProperties(property) {
for (let p of ALL_DATA.metrics) {
if (p.parent == property.name) {
return true;
}
}
for (let p of ALL_DATA.analysis) {
if (p.parent == property.name) {
return true;
}
}
return false;
}
function calculateMaxLevel(data) {
return Math.max.apply(Math, data.map(function (d) {
return parseInt(d.level, 10);
}))
}
/**
*
* @param {Array} data Dataset.
* @returns Maximum width for a property's name in the dataset.
*/
function calculateMaxNameWidth(data) {
return Math.max.apply(Math, data.map(function (d) {
return textSize(d.name, PROPERTY_FONT_FAMILY, PROPERTY_FONT_SIZE).width;
}))
}
/**
*
* @param {Array} data Dataset.
* @returns Maximum width for a property's value in the dataset.
*/
function calculateMaxValueWidth(data) {
return Math.max.apply(Math, data.map(function (d) {
return textSize(String(get_value(d)), VALUES_FONT_FAMILY, VALUES_FONT_SIZE).width;
}))
}
/**
*
* @param {Array} data Dataset.
* @returns Maximum width for a property's ratio in the dataset.
*/
function calculateMaxRatioWidth(data) {
return Math.max.apply(Math, data.map(function (d) {
return textSize(String(get_ratio(d)), VALUES_FONT_FAMILY, VALUES_FONT_SIZE).width;
}))
}
/**
*
* @param {Array} data Dataset.
* @returns Maximum width for a property's indentation in the dataset.
*/
function calculateMaxIndentationWidth(data) {
return Math.max.apply(Math, data.map(function (d) {
return textSize("-".repeat(1 + PROPERTY_INDENTATION * parseInt(d.level, 10)), PROPERTY_FONT_FAMILY, PROPERTY_FONT_SIZE).width;
}))
}
function filterData(data) {
metrics = data.metrics;
analysis = data.analysis;
if (d3.select("#collapseZeroValues").property("checked")) {
metrics = metrics.filter(function (d, i) { return get_value(d) != '0'; });
analysis = analysis.filter(function (d, i) { return get_value(d) != '0'; });
}
metrics = metrics.filter(function (d, i) { return VISIBLE_PROPERTIES[d.name]; });
analysis = analysis.filter(function (d, i) { return VISIBLE_PROPERTIES[d.name]; });
return { "metadata": data.metadata, "metrics": metrics, "analysis": analysis };
}
function redrawLabel(data) {
var height = 0;
updateProperties(data.metrics, "metrics");
height = yMetrics + PROPERTY_HEIGHT * data.metrics.length;
drawRule("rule2", height);
height += MAIN_RULE_HEIGHT;
d3.select("#analysis").attr("transform", "translate(0," + height + ")")
updateProperties(data.analysis, "analysis");
height += MARGING_BETWEEN_PROPERTIES + PROPERTY_HEIGHT * data.analysis.length;
drawBorders(maxWidth, height);
d3.select("chart").attr("height", height);
}
/**
* Set-up the collapse zero values option.
*/
function collapseZeroValues(data) {
var newData = filterData(data);
redrawLabel(newData);
}
function collapseSubProperties(data) {
if (d3.select("#collapseSubProperties").property("checked")) {
for (let p of data.metrics) {
var children = getChildrenProperties(data.metrics, p, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = false; }
var children = getChildrenProperties(data.analysis, p, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = false; }
}
for (let p of data.analysis) {
var children = getChildrenProperties(data.metrics, p, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = false; }
var children = getChildrenProperties(data.analysis, p, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = false; }
}
} else {
for (let p of data.metrics) {
var children = getChildrenProperties(data.metrics, p, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = true; }
var children = getChildrenProperties(data.analysis, p, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = true; }
}
for (let p of data.analysis) {
var children = getChildrenProperties(data.metrics, p, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = true; }
var children = getChildrenProperties(data.analysis, p, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = true; }
}
}
newData = filterData(data);
redrawLabel(newData);
}
function collapseProperty(data, property) {
var children = getChildrenProperties(data.metrics, property, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = false; }
var children = getChildrenProperties(data.analysis, property, true);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = false; }
newData = filterData(data);
redrawLabel(newData);
}
function expandProperty(data, property) {
var children = getChildrenProperties(data.metrics, property, false);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = true; }
var children = getChildrenProperties(data.analysis, property, false);
for (let c of children) { VISIBLE_PROPERTIES[c.name] = true; }
newData = filterData(data);
redrawLabel(newData);
}
function drawSecondaryRules(data) {
if (get_property_in_data(data, 'Compound features') !== null) drawSecondaryRule("Compound features");
if (get_property_in_data(data, 'Root feature') !== null) drawSecondaryRule("Root feature");
if (get_property_in_data(data, 'Features in constraints') !== null) drawSecondaryRule("Features in constraints");
if (get_property_in_data(data, 'Configurations') !== null) drawSecondaryRule("Configurations");
// var translate = d3.select("g[id='Compound features']").node() // get the node
// .transform // get the animated transform list
// .baseVal // get its base value
// .getItem(0) // get the first transformation from the list, i.e. your translate
// .matrix // get the matrix containing the values
// // console.log(`Translate x: ${translate.e}, y: ${translate.f}`);
// var property = d3.select("g[id='Compound features']");
// //.append("g").attr("class", "secondaryRule").attr("transform", "translate(0," + translate.f - 3 + ")");
// property.append("rect")
// .attr("x", property.select("#propertyName").attr("x"))
// .attr("y", 1)
// .attr("height", SECONDARY_RULE_HEIGHT)
// .attr("width", maxWidth - property.select("#propertyName").attr("x"));
}
function drawSecondaryRule(propertyName) {
var property = d3.select("g[id='" + propertyName + "']");
property.append("rect")
.attr("x", property.select("#propertyName").attr("x"))
.attr("y", 1)
.attr("height", SECONDARY_RULE_HEIGHT)
.attr("width", maxWidth - property.select("#propertyName").attr("x"));
}
// function calculateTotalMaxWidth(data) {
// return Math.max.apply(Math, data.map(function(d) {
// indentationWidth = textSize("-".repeat(1 + PROPERTY_INDENTATION * parseInt(d.level, 10)), PROPERTY_FONT_FAMILY, PROPERTY_FONT_SIZE).width;
// nameWidth = textSize(d.name, PROPERTY_FONT_FAMILY, PROPERTY_FONT_SIZE).width;
// valueWidth = textSize(String(get_value(d)), VALUES_FONT_FAMILY, VALUES_FONT_SIZE).width;
// ratioWidth = textSize(String(get_ratio(d)), VALUES_FONT_FAMILY, VALUES_FONT_SIZE).width;
// return indentationWidth + nameWidth + valueWidth + ratioWidth;
// })) + PROPERTIES_VALUES_SPACE + PROPERTIES_RATIO_SPACE + LEFT_MARGING;
// };
document.addEventListener("DOMContentLoaded", function () {
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(
function () {
const copyButton = document.getElementById("copyButton");
copyButton.textContent = "Copied!";
copyButton.classList.remove("btn-primary");
copyButton.classList.add("btn-success");
copyButton.disabled = true;
setTimeout(() => {
copyButton.textContent = "Copy";
copyButton.classList.remove("btn-success");
copyButton.classList.add("btn-primary");
copyButton.disabled = false;
}, 2000);
},
function (err) {
console.error("Could not copy text: ", err);
}
);
}
// Event listener for the copy button
document.getElementById("copyButton").addEventListener("click", function () {
const modalBodyText = document.querySelector(
"#metricModal .modal-body"
).textContent;
text = modalBodyText.split(", ").join("\n");
copyToClipboard(text);
});
});
function showMetricModal(metric) {
const modalTitle = document.getElementById("metricModalLabel");
const modalBody = document.querySelector("#metricModal .modal-body");
modalTitle.innerHTML = `<b>${metric.name} </b><br><small>${metric.description}</small>`;
if (metric.stats) {
modalBody.innerHTML = `
<p><strong>Mean:</strong> ${
metric.stats.mean !== null ? metric.stats.mean : "N/A"
}</p>
<p><strong>Median:</strong> ${
metric.stats.median !== null ? metric.stats.median : "N/A"
}</p>
<p><strong>Min:</strong> ${
metric.stats.min !== null ? metric.stats.min : "N/A"
}</p>
<p><strong>Max:</strong> ${
metric.stats.max !== null ? metric.stats.max : "N/A"
}</p>
`;
} else {
modalBody.innerHTML = metric.value.join(", ");
}
const metricModal = new bootstrap.Modal(
document.getElementById("metricModal")
);
metricModal.show();
}