-
Notifications
You must be signed in to change notification settings - Fork 3
/
D3-memmaps-v2.html
562 lines (512 loc) · 17.1 KB
/
D3-memmaps-v2.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.machine{
fill: #444;
font-size: 22px;
font-weight: bold;
}
.label{
fill: gray;
font-size: 12px;
font-weight: bold;
}
.info{
font-size: 10px;
}
.title {
overflow: hidden;
width:100%;
font-style: bold;
font-size: 2em;
padding: 2px 0px 8px 2px;
}
.units{
fill: gray;
font-size: 12px;
}
.value{
font-size: 12px;
font-weight: bold;
}
svg {
padding: 10px 0px 0px 10px;
}
.legend {
float:left;
padding: 0px 2px 10px 0px
}
.pie {
padding: 10px 2px 10px 2px;
}
.arc {
stroke: #fff;
}
</style>
<body>
<script src="d3.min.js"></script>
<script>
// Thumbnail variables
var thumb_radius = 90,
thumb_ring = 30,
legend_box_width = 22, // pixel width of sq colour box
legend_max_rows = 32, // max rows in a legend col
legend_min_rows = 8, // min rows before going multi-col
legend_max_text = 120; // width allocated to a legend text entry
// Main donut chart variables
var iwidth = 960,
iheight = 450,
iringwidth = 66,
iradius = Math.min(iwidth, iheight) / 3,
tweenDuration = 1000,
// tick lines
tick_length = 20,
tick_offset = 8,
tick_width = 2,
// text
textOffset = 34;
//--------------------------------
// Thumbnail Donut Charts
var color = d3.scale.ordinal()
.range(["#999", "#0A0", "#A33", "#46B", "#DA6", "#D6A", "#0DD"]);
var thumb_arc = d3.svg.arc()
.outerRadius(thumb_radius)
.innerRadius(thumb_radius - thumb_ring);
var thumb_pie = d3.layout.pie()
.value(function(d) { return d.memsize; })
.sort(null);
//--------------------------------
// read the csv file.
// - rest in this scope
d3.csv("mappings.csv", function(error, data) {
// make color set from line of names (ignoring the system item)
color.domain(d3.keys(data[0]).filter(function(key){ return key !== "system"; }));
// convert data and create totals
data.forEach(function(d) {
// set d.sizes to hold numbers instead of strings
d.sizes = color.domain().map(function(name) {
var msize = +d[name];
return {label: name,
memsize: msize};
});
// calc total of memsizes
d.total = d3.sum(d.sizes, function(d) { return d.memsize; });
});
var dsize = data.length,
legend_size = data[0].sizes.length,
legend_row_count = Math.max(legend_min_rows, Math.min(Math.floor(legend_size/2) + 1, legend_max_rows)),
legend_width = Math.ceil(legend_size / legend_row_count),
index = 0;
// Screen Title
var legtitle = d3.select("body").append("div")
.attr("class", "title")
.text("Found "+dsize+" systems");
// Legend boxes
var legend = d3.select("body").append("svg")
.attr("class", "legend")
.attr("width", legend_width*legend_max_text)
.attr("height", (legend_box_width+2)*legend_row_count)
.selectAll("g")
.data(color.domain().slice())
.enter().append("g")
.attr("transform", function(d, i){
xOff = Math.floor(i / legend_row_count) * legend_max_text
yOff = (i % legend_row_count) * (legend_box_width+2)
return "translate(" + xOff + "," + yOff + ")"; });
legend.append("rect")
.attr("width", legend_box_width)
.attr("height", legend_box_width) // square
.style("fill", color)
legend.append("text")
.attr("x", legend_box_width+6)
.attr("y", legend_box_width/2)
//.on("click", update_main)
.attr("dy", ".35em")
.text(function(d) { return d; });
// Thumbnail Ring Charts
var pie_thumb = d3.select("body").append("div")
.attr("class", "thumbs");
var mypie = pie_thumb.selectAll(".thumbs")
.data(data)
.enter().append("svg")
.attr("class", "pie")
.attr("width", thumb_radius*2)
.attr("height", thumb_radius*2.2)
.append("g")
.attr("transform", "translate(" + thumb_radius + "," + thumb_radius*1.1 + ")")
mypie.append("svg:circle")
.attr("fill", "ivory")
.attr("r", thumb_radius)
.on("click", update_main) // refocus main onto this thumb data
mypie.append("text")
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) { return d.system; })
mypie.selectAll(".arc")
.data(function(d) { return thumb_pie(d.sizes); })
.enter().append("path")
.attr("class", "arc")
.attr("d", thumb_arc)
//.on("click", update_main)
.attr("fill", function(d){ return color(d.data.label); })
mypie.append("line")
.attr("x1", 0)
.attr("x2", 0)
.attr("y1", -thumb_radius)
.attr("y2", -thumb_radius-thumb_ring/6)
.attr("stroke", "black")
.attr("stroke-width", 2)
//
var imain_title = d3.select("body").append("div")
.text("Interactive")
.attr("class", "title");
// switch focus of main pie chart to one of the sets
function update_main(a,i) {
index = i;
render();
};
//--------------------------------
// draw and append the SVG container
var svg = d3.select("body").append("svg")
.attr("width", iwidth)
.attr("height", iheight)
.on("click", render);
// create the pie chart container
var ig = svg.append('g')
.attr('transform', function(){
if ( window.innerWidth >= iwidth ) var shiftWidth = iwidth / 2;
if ( window.innerWidth < iwidth ) var shiftWidth = iwidth / 3;
return 'translate(' + shiftWidth + ',' + iheight / 2 + ')';
});
// create group for region labels(percent and label)
var label_group = svg.append('g')
.attr("class", "label_group")
.attr("transform", function(){
return 'translate(' + iwidth/2 + ',' + iheight/2 + ')';
});
// create group for center text
var center_group = svg.append('g')
.attr("class", "center_group")
.attr("transform", function(){
return 'translate(' + iwidth/2 + ',' + iheight/2 + ')';
});
//--------------------------------
// generate random data
var mydata = data[index];
console.log("mydata ",mydata);
//mydata = mydata.sizes.filter(function(key){ return !(key == "system" || key.memsize == 0); });
mydata = mydata.sizes.filter(function(key){ return !(key == "system"); });
var system = data[index].system;
var totalRAM = data[index].total;
//console.log("initial",mydata);
function makeData(size){
return d3.range(size).map(function(item){
val = Math.floor(Math.random()*100000); // simulate kB
totalRAM += val
return { memsize: val,
label: ".bss"
};
});
};
//--------------------------------
//Helper functions to position elements
// percentages are above labels
percent_dy = function(d){
if ((d.startAngle+d.endAngle)/2 > Math.PI/2 && (d.startAngle+d.endAngle)/2 < Math.PI*1.5 ) {
return 5;
} else {
return -7; }};
// labels are below percentages (dy)
label_dy = function(d){
if ((d.startAngle+d.endAngle)/2 > Math.PI/2 && (d.startAngle+d.endAngle)/2 < Math.PI*1.5 ) {
return 17;
} else {
return 5; }};
// all text is aligned based on which side of circle
text_anchor = function(d){
if ((d.startAngle+d.endAngle)/2 < Math.PI ) {
return "beginning";
} else {
return "end"; }};
// all text main position (dy separates different fields)
text_position = function(d){
return "translate(" + Math.cos(((d.startAngle+d.endAngle - Math.PI)/2)) * (iradius+textOffset) + "," +
Math.sin((d.startAngle+d.endAngle - Math.PI)/2) * (iradius+textOffset) + ")"; };
// text percent and size
percent_label = function(d){
var percentage = (d.data.memsize/totalRAM)*100;
// don't print anything if value==0
return (percentage==0)? "" : d.data.memsize + " bytes (" + percentage.toFixed(1) + "%)";
}
//--------------------------------
// shape and style of D3 graph
// construct default pie layout
var main_pie = d3.layout.pie()
.value(function(d){ return d.memsize; })
.sort(null);
// construct arc generator
var iarc = d3.svg.arc()
.outerRadius(iradius)
.innerRadius(iradius - iringwidth);
//--------------------------------
// initial types of things
// make initial paths
var ipath = ig.datum(mydata).selectAll("path")
.data(main_pie)
.enter().append("path")
.attr("class","arc")
.attr("fill", function(d,i){ return color(d.data.label); })
.attr("d", iarc)
//.on("click", function() {d3.select(this).attr("fill", "#5F6");})
.each(function(d){ this._current = d; });
// make initial tickmarks
var itick = label_group.datum(mydata).selectAll("line")
.data(main_pie)
.enter().append("svg:line")
.attr("class", "tick")
.attr("x1", 0)
.attr("x2", 0)
// don't draw tick marks if value==0
.attr("y1", function(d) {return (d.value==0) ? 0 : -iradius-tick_offset})
.attr("y2", function(d) {return (d.value==0) ? 0 : -iradius-(tick_length+tick_offset) })
.attr("stroke", "gray")
.attr("transform", function(d) {
return "rotate(" + (d.startAngle+d.endAngle)/2 * (180/Math.PI) + ")";
})
.each(function(d){ this._current = d; });
// make initial text labels
var ilabel = label_group.datum(mydata).selectAll("text.units")
.data(main_pie)
.enter().append("svg:text")
.attr("class", "units")
.attr("transform", text_position)
.attr("dy", label_dy)
.attr("text-anchor", text_anchor)
.text(function(d){
// don't draw percents if value==0
return (d.value==0) ? "" : d.data.label;})
.each(function(d){ this._current = d; });
// make initial percentages
var ipercent = label_group.datum(mydata).selectAll("text.value")
.data(main_pie)
.enter().append("svg:text")
.attr("class", "value")
.attr("transform", text_position)
.attr("dy", percent_dy)
.attr("text-anchor", text_anchor)
.text(percent_label)
.each(function(d){ this._current = d; });
// make central text
// - White BG circle behind text
var whiteCircle = center_group.append("svg:circle")
.attr("fill", "ivory")
.attr("r", iradius-iringwidth);
// - machine
var machineName = center_group.append("svg:text")
.attr("class", "machine")
.attr("dy", -15)
.attr("text-anchor", "middle")
.text(system);
// - how much RAM
var totalLabel = center_group.append("svg:text")
.attr("class", "label")
.attr("dy", 7)
.attr("text-anchor", "middle")
.text("TOTAL");
// - for selection info or something...
var field_info = center_group.append("svg:text")
.attr("class", "info")
.attr("dy", 33)
.attr("text-anchor", "middle")
.text("............");
// vertical start mem marker
var izeromarker = center_group.append("line")
.attr("x1", 0)
.attr("x2", 0)
.attr("y1", -iradius)
.attr("y2", -iradius-tick_length)
.attr("stroke", "black")
.attr("stroke-width", 2)
//--------------------------------
// render function for displaying pie chart and all components
function render(){
// generate new random data
//mydata = data[index].sizes.filter(function(key){ return !(key == "system" || key.memsize == 0); });
mydata = data[index].sizes.filter(function(key){ return !(key == "system"); });
system = data[index].system;
totalRAM = data[index].total;
//console.log("mydata ",mydata);
// Update central region
machineName.text(function(){
return system; })
totalLabel.text(function(){
var kb = totalRAM/1024;
//return "Total: " + kb.toFixed(1) + " kB"; })
return "Total: " + totalRAM + " bytes"; })
//Draw Paths
// new - add any new paths
ig.datum(mydata).selectAll("path")
.data(main_pie)
.enter().append("path")
.attr("class","arc")
.attr("fill", function(d,i){return color(d.data.label); })
.attr("d", iarc)
.each(function(d){ this._current = d; })
// transition - add transition to new path
ig.datum(mydata).selectAll("path")
.data(main_pie)
.transition()
.duration(tweenDuration)
.attrTween("d", arcTween)
// exit - remove data not being used
ig.datum(mydata).selectAll("path")
.data(main_pie)
.exit().remove()
//Draw Tick lines for Labels
// new
label_group.datum(mydata).selectAll("line")
.data(main_pie)
.enter().append("svg:line")
.attr("x1", 0)
.attr("x2", 0)
// don't draw tick marks if value==0
.attr("y1", function(d) {return (d.value==0) ? 0 : -iradius-tick_offset})
.attr("y2", function(d) {return (d.value==0) ? 0 : -iradius-(tick_length+tick_offset) })
.attr("stroke", "gray")
.attr("stroke-width", tick_width)
.attr("transform", function(d) {
return "rotate(" + (d.startAngle+d.endAngle)/2 * (180/Math.PI) + ")";
})
//.each(function(d){ this._current = d; })
// transition
label_group.datum(mydata).selectAll("line")
.data(main_pie)
.transition()
.duration(tweenDuration)
.attrTween("transform", lineTween)
.attrTween("stroke-width", strokeTween)
// exit
label_group.datum(mydata).selectAll("line")
.data(main_pie)
.exit().remove()
//Draw Label Names
label_group.datum(mydata).selectAll("text.units")
.data(main_pie)
.attr("dy", label_dy)
.attr("text-anchor", text_anchor)
.text(function(d){
// don't draw Names if value==0
return (d.value==0) ? "" : d.data.label;
})
// new
label_group.datum(mydata).selectAll("text.units")
.data(main_pie)
.enter().append("svg:text")
.attr("class", "units")
.attr("transform", text_position)
.attr("dy", label_dy)
.attr("text-anchor", text_anchor)
// don't draw label if value==0
.text(function(d){return (d.value==0) ? "" : d.data.label; })
.each(function(d){ this._current = d; })
// transition
label_group.datum(mydata).selectAll("text.units")
.data(main_pie)
.transition()
.duration(tweenDuration)
.attrTween("transform", textTween)
// exit
label_group.datum(mydata).selectAll("text.units")
.data(main_pie)
.exit().remove()
//Draw section percentages
label_group.datum(mydata).selectAll("text.value")
.data(main_pie)
.attr("dy", percent_dy)
.attr("text-anchor", text_anchor)
.text(percent_label)
// new
label_group.datum(mydata).selectAll("text.value")
.data(main_pie)
.enter().append("svg:text")
.attr("class", "value")
.attr("transform", text_position)
.attr("dy", percent_dy)
.attr("text-anchor", text_anchor)
.text(percent_label)
.each(function(d){ this._current = d; })
//--------------------------------
// in case text needs a BG
//
//var text = d3.select("text");
//var bbox = text.node().getBBox();
//var padding = 2;
//var rect = self.svg.insert("rect", "text")
// .attr("x", bbox.x - padding)
// .attr("y", bbox.y - padding)
// .attr("width", bbox.width + (padding*2))
// .attr("height", bbox.height + (padding*2))
// .style("fill", "red");
//--------------------------------
// transition
label_group.datum(mydata).selectAll("text.value")
.data(main_pie)
.transition()
.duration(tweenDuration)
.attrTween("transform", textTween)
// exit
label_group.datum(mydata).selectAll("text.value")
.data(main_pie)
.exit().remove()
};
render();
//--------------------------------
// Tween functions
// Store the displayed angles in this._current (Object, not just a number)
// Then, interpolate from _current to the new angles
// During the transition, _current is updated in-place by d3.interpolate
function arcTween(a) {
//console.log(a.value,a);
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return iarc(i(t));
}
}
//
function lineTween(a) {
//console.log(a);
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
//console.log(i(t).startAngle,i(t).endAngle,(i(t).startAngle+i(t).endAngle)/2 * (180/Math.PI));
return "rotate(" + (i(t).startAngle+i(t).endAngle)/2 * (180/Math.PI) + ")";
}
}
function strokeTween(a) { // don't need to interpolate, just check dest value
//console.log(a);
return function(t) {
// if value==0 then hide else visible
return (a.value==0) ? 0 : tick_width;
}
}
function textTween(a) {
//console.log(a);
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
var val = (i(t).startAngle + i(t).endAngle - Math.PI)/2;
//console.log("at ", val, val*360);
return "translate(" + Math.cos(val) * (iradius+textOffset) + "," + Math.sin(val) * (iradius+textOffset) + ")";
}
}
//--------------------------------
// Notes:
// try with all fields found in map program
// separate crowding (BG behind text, vertically stack, keep track :()
// add table LHS of Main chart. list all fields, hex, dec
});
</script>