-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherc_treemap.html
900 lines (780 loc) · 28.7 KB
/
erc_treemap.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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
<!DOCTYPE html>
<html>
<head></head>
<style type="text/css">
.projectDetails {
font : 15px sans-serif;
border: 2px solid white;
vertical-align : top;
}
.projectDetailsHeaders {
background: #aaf;
font-weight : bold;
text-align: right;
vertical-align: top;
padding: 5px;
border: 2px solid white;
width: 90px;
}
.projectDetailsData {
vertical-align : top;
border: 2px solid white;
padding: 5px;
}
#tree {
width: 960px;
height: 520px;
background: #ddd;
}
text {
pointer-events: none;
}
.grandparent text {
font-weight: bold;
}
.barchart rect {
fill : steelblue;
}
.barchart text {
fill : #555;
font : 10px sans-serif;
text-anchor: end;
}
rect {
fill: none;
stroke: #fff;
}
rect.parent,
.grandparent rect {
stroke-width: 2px;
}
.grandparent rect {
fill: orange;
}
.grandparent:hover rect {
fill: #ee9700;
}
.children rect.parent,
.grandparent rect {
cursor: pointer;
}
.children rect.parent {
fill: #bbb;
fill-opacity: 0.5;
}
.children:hover rect.child {
stroke: #aaa;
}
.ui-state-default {
font-size : 11px;
}
</style>
<body>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="css/bootstrap-multiselect.css" type="text/css"/>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
<script type="text/javascript" src="http://www.billdwhite.com/wordpress/wp-content/js/browserdetect/browserdetect.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap-multiselect.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="pourover.js"></script>
<!-- <script charset="utf-8" src="d3.js" type="text/javascript"></script> -->
<style>
#sortable { list-style-type: none; margin: 0; padding: 0; width: 100%; }
#sortable li { margin: 3px 3px 3px 3px; padding: 1.0em; padding-left: 1.5em; font-size: 1.0em; height: 12px; }
#sortable li span { position: absolute; margin-left: -1.3em; }
</style>
<div style="background:#aaf">
<h2>The ERC Data Browser</h2><p>
The European Research Council is responsible for disbursing billions of euros of research funding; this is an effort to understand that mass of data.<p>
<h4>Click the orange bar to zoom out, any colored rectangle to zoom in; hover over a rectangle for more data</h4><p>
Leaf nodes will display full summaries of a grant. In some cases the display may get "wedged" and look erroneous after changing filters or reordering the tree; either reload or zoom in/out once and that should sort things out.
<hr>
</div>
<div id="loadingdiv">
<h1>Loading data, please wait...</h1>
</div>
<div style="background:#eeeeff">
<table>
<tr>
<td style="vertical-align: top;">
<div id="hierarchy">
<b>Drag to sort:</b><p>
<ul id="sortable">
<li class="ui-state-default" value='category'><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Category</li>
<li class="ui-state-default" value='call_year'><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Call Year</li>
<li class="ui-state-default" value='call_domain'><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Call Domain</li>
<li class="ui-state-default" value='country'><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Country</li>
<li class="ui-state-default" value='STOP'><span class="ui-icon ui-icon-arrowthick-2-n-s"></span><b>STOP BREAKDOWN</b></li>
</ul>
</div>
<b>Year:</b><p>
<div id="yeardiv"></div>
<select id="year" multiple="multiple">
</select>
<p>
<b>Country:</b><p>
<select id="country" multiple="multiple">
</select><p>
<b>Domain:</b><p>
<select id="domain" multiple="multiple">
</select><p>
<b>Category:</b><p>
<select id="category" multiple="multiple">
</select>
<p>
<b>Substring Search</b>
<p>(summaries)<p>
<input type="text" id="search"></input>
<p>
</td>
<td >
<div id="tree">
</div>
</td>
<td>
<div id="bargraphDiv">
</div>
</td>
</tr>
</table>
</div>
<div id="projectdetailsdiv">
<h3>Last Selected Project Details</h3>
<table class="projectDetails" cellpadding="10">
<tr>
<td class="projectDetailsHeaders">
Project
</td>
<td id="projectDetailsProject">
</td>
</tr>
<tr>
<td class="projectDetailsHeaders">
Acronym
</td>
<td id="projectDetailsAcronym" class="projectDetailsData">
</td>
</tr>
<tr>
<td class="projectDetailsHeaders">
Participants
</td>
<td id="projectDetailsParticipants" class="projectDetailsData">
</td>
</tr>
<tr>
<td class="projectDetailsHeaders">
Call Details
</td>
<td id="projectDetailsCallDetails" class="projectDetailsData">
</td>
</tr>
<tr>
<td class="projectDetailsHeaders">
Country
</td>
<td id="projectDetailsCountry" class="projectDetailsData">
</td>
</tr>
<tr>
<td class="projectDetailsHeaders">
Funding
</td>
<td id="projectDetailsFunding" class="projectDetailsData">
</td>
</tr>
<tr>
<td class="projectDetailsHeaders">
Duration
</td>
<td id="projectDetailsDuration" class="projectDetailsData">
</td>
</tr>
<tr>
<td class="projectDetailsHeaders">
Category
</td>
<td id="projectDetailsCategory" class="projectDetailsData">
</td>
</tr>
<tr>
<td class="projectDetailsHeaders">
Summary
</td>
<td id="projectDetailsSummary" class="projectDetailsData">
</td>
</tr>
</table>
</div>
<script>
"use strict";
var arr = [
{ 'a' : 'a', 'b' : 1, 'c' : '5', 'size' : 2000, 'name' : 'adam' },
{ 'a' : 'b', 'b' : 2, 'c' : '6', 'size' : 4000, 'name' : 'bob' },
{ 'a' : 'a', 'b' : 2, 'c' : '6', 'size' : 6000, 'name' : 'charlie' },
{ 'a' : 'b', 'b' : 1, 'c' : '5', 'size' : 10000, 'name' : 'david' }
];
var collection;
var years;
var countries;
var domains;
var categories;
var yearFilter;
var countryFilter;
var domainFilter;
var categoryFilter;
var summaryFilter;
var dataView;
var SubstringFilter = PourOver.Filter.extend({
// cacheResults specifies how to take a set of items and assign them to possibilities
// see nytimes.github.io/pourover/examples/examples_build/advanced_filters.html
// looks like this actually filters the items, but it also sounds like
// we can refuse to cache by setting these to false (PourOver.manualFilter)
// this is inefficient and in time it may be worth adding a query and translating that into
// a cacheable possibility
cacheResults : function(items){
var possibilities = this.possibilities,
attribute = this.attr;
_(items).each(function(i){
var value = i[attribute].toLowerCase();
_(possibilities).each(function(p){
var index = value.indexOf( p.value.toLowerCase() );
if (( index > -1 ) || (p.value === "*") || (p.value === '')) {
p.matching_cids = PourOver.insert_sorted(p.matching_cids,i.cid)
}
});
});
},
addCacheResults : function( items ) {
this.cachResults.call( this, new_items );
},
// getFn specifies how to translate a query into some combination of possibility caches
// so this requests a cache. For this one-shot search filter, we have one cache (the query)
// so this should not only match the single possibility but return it.
// but we can have getFn just return a MatchSet by manually constructing one
// starting point : PourOver.manualFilter (line 1480)
getFn : function( query ) {
var query_lc = query.toLowerCase();
var matching_possibility = _(this.possibilities).find( function(p) {
var value_lc = p.value.toLowerCase();
return value_lc === query_lc;
});
// new PourOver.MatchSet seems to take ([cids], collection, [[filter, query]] )
// this.makeQueryMatchSet takes cids,query and calls the above with this.getCollection()
// so how to page through this.getCollection() and match cids?
// Collection has items()
return this.makeQueryMatchSet( matching_possibility.matching_cids, query );
},
addPossibility : function( query ) {
this.possibilities[query] = {
value : query,
matching_cids : []
};
this.cacheResults( this.getCollection().items );
}
});
var makeSubstringFilter = function( name, values, attr ) {
var values = _(values).map(function(i){return {value:i}}),
opts = {associated_attrs: [attr], attr: attr},
filter = new SubstringFilter(name,values,opts);
return filter;
};
d3.json( "projects.json", function (d) {
arr = d;
collection = new PourOver.Collection( arr );
years = _.sortBy(_.uniq(_.pluck( arr, "call_year" )));
yearFilter = PourOver.makeExactFilter( "call_year", years );
countries = _.sortBy(_.uniq(_.pluck( arr, "country" ) ));
countryFilter = PourOver.makeExactFilter( "country", countries );
domains = _.sortBy(_.uniq(_.pluck( arr, "call_domain" )));
domainFilter = PourOver.makeExactFilter( "call_domain", domains );
categories = _.sortBy(_.uniq(_.pluck( arr, "category" )));
categoryFilter = PourOver.makeExactFilter( "category", categories );
summaryFilter = makeSubstringFilter("summary", [], 'summary');
//populate the UI
var populateMultiSelect = function( id, data, key ) {
d3.select(id).selectAll("option")
.data( data ).enter()
.append( "option" )
.attr( "value", function(d) { return d } )
.attr("selected", "selected")
.text( function(d) {
if (key) {
return key[d] || d;
} else {
return d;
}
} )
}
populateMultiSelect( "select#year", years )
populateMultiSelect( "select#country", countries )
populateMultiSelect( "select#domain", domains, {
'CoG' : 'CoG: Consolidator Grant',
'ID1' : 'ID1: Interdisciplinary Grants',
'PoC' : 'PoC: Proof of Concept Grant',
'SG-' : 'SG-: (special?)',
'SyG' : 'Synergy Grant',
'PE1' : 'PE1: Mathematical Foundations (all areas of mathematics and foundations of comp. sci, phys, stats)',
'PE2' : 'PE2: Fundamental constituents of matter: particle, nuclear, plasma, atomic, molecular, gas, optical physics',
'PE3' : 'PE3: Condensed matter physics: structure, electronic properties, fluids, nanosciences',
'PE4' : 'PE4: Physical and analytical chemical sciences: analytical chemistry, chemical theory, physical chemistry/chemical physics',
'PE5' : 'PE5: Materials and synthesis: materials synthesis, structure-properties relations, functional and advanced materials, molecular architecture, organic chemistry',
'PE6' : 'PE6: Computer science and informatics: informatics and information systems, computer science, scientific computing, intelligent systems',
'PE7' : 'PE7: Systems and communication engineering: electronic, communication, optical and systems engineering',
'PE8' : 'PE8: Products and processes engineering: product design, process design and control, construction methods, civil engineering, energy systems, material engineering',
'PE9' : 'PE9: Universe sciences: astro-physics/chemistry/biology; solar system; stellar, galactic and extragalactic astronomy, planetary systems, cosmology, space science, instrumentation',
'PE10' : 'PE10: Earth system science: physical geography, geology, geophysics, meteorology, oceanography, climatology, ecology, global environmental change, biogeochemical cycles, natural resources management.',
'SH1' : 'SH1: Individuals, institutions and markets: economics, finance and management',
'SH2' : 'SH2: Institutions, values and beliefs and behaviour: sociology, social anthropology, political science, law, communication, social studies of science and technology',
'SH3' : 'SH3: Environment and society: environmental studies, demography, social geography, urban and regional studies',
'SH4' : 'SH4: The Human Mind and its complexity: cognition, psychology, linguistics, philosophy and education',
'SH5' : 'SH5: Cultures and cultural production: literature, visual and performing arts, music, cultural and comparative studies',
'SH6' : 'SH6: The study of the human past: archaeology, history and memory.',
'LS1' : 'LS1: Molecular and Structural Biology and Biochemistry: molecular biology, biochemistry, biophysics, structural biology, biochemistry of signal transduction',
'LS2' : 'LS2: Genetics, Genomics, Bioinformatics and Systems Biology: genetics, population genetics, molecular genetics, genomics, transcriptomics, proteomics, metabolomics, bioinformatics, computational biology, biostatistics, biological modelling and simulation, systems biology, genetic epidemiology',
'LS3' : 'LS3: Cellular and Developmental Biology: cell biology, cell physiology, signal transduction, organogenesis, developmental genetics, pattern formation in plants and animals',
'LS4' : 'LS4: Physiology, Pathophysiology and Endocrinology: organ physiology, pathophysiology, endocrinology, metabolism, ageing, regeneration, tumorigenesis, cardiovascular disease, metabolic syndrome',
'LS5' : 'LS5: Neurosciences and neural disorders: neurobiology, neuroanatomy, neurophysiology, neurochemistry, neuropharmacology, neuroimaging, systems neuroscience, neurological disorders, psychiatry',
'LS6' : 'LS6: Immunity and infection: immunobiology, aetiology of immune disorders, microbiology, virology, parasitology, global and other infectious diseases, population dynamics of infectious diseases, veterinary medicine',
'LS7' : 'LS7: Diagnostic tools, therapies and public health: aetiology, diagnosis and treatment of disease, public health, epidemiology, pharmacology, clinical medicine, regenerative medicine, medical ethics',
'LS8' : 'LS8: Evolutionary, population and environmental biology: evolution, ecology, animal behaviour, population biology, biodiversity, biogeography, marine biology, ecotoxicology, prokaryotic biology',
'LS9' : 'LS9: Applied life sciences and biotechnology: agricultural, animal, fishery, forestry and food sciences; biotechnology, chemical biology, genetic engineering, synthetic biology, industrial biosciences; environmental biotechnology and remediation.'
} );
populateMultiSelect( "select#category", categories )
collection.addFilters([yearFilter, countryFilter, domainFilter, categoryFilter, summaryFilter]);
var DataView = PourOver.View.extend({
selectionFn : function() {
return collection.filters.call_year.current_query
.and( collection.filters.country.current_query )
.and( collection.filters.call_domain.current_query )
.and( collection.filters.category.current_query )
.and( collection.filters.summary.current_query );
}
});
var queryAll = function( filter, arr ) {
// make a union query of all the items in array
if ( arr.length > 0 ) {
filter.query( arr[0] );
arr.slice(1,arr.length).forEach( function(a) {
filter.unionQuery(a);
});
} else {
filter.query();
}
}
//have to set all current queries first
dataView = new DataView( "dataView", collection );
queryAll( collection.filters.call_year, years )
queryAll( collection.filters.country, countries )
queryAll( collection.filters.call_domain, domains )
queryAll( collection.filters.category, categories )
setData( tree_from_array( dataView.getCurrentItems(), ['category', 'call_year','call_domain', 'country', ] ) )
var resetData = function() {
var a = $("#sortable > li").map( function() { return $(this).attr("value") }).get();
//trim everything after "stop"
a = a.slice(0, a.indexOf("STOP"));
setData( tree_from_array( dataView.getCurrentItems(), a ) );
}
var multiSelectOnChangeFunction = function( id, filter ) {
var result = function( element, checked ) {
var a = []
$(id).each( function() {
if ((this.value !== "multiselect-all") && this.selected) {
a.push( this.value );
} } );
queryAll( filter, a );
resetData();
}
return result
}
$(function() {
$( "#sortable" ).sortable( {
update : function( event, ui ) {
resetData();
}
});
$( "#sortable" ).disableSelection();
$( "#year" ).multiselect({
numberDisplayed : 0,
includeSelectAllOption : true,
onChange : multiSelectOnChangeFunction( "select#year > option", collection.filters.call_year )
});
$( "#country" ).multiselect({
numberDisplayed : 0,
includeSelectAllOption : true,
onChange : multiSelectOnChangeFunction( "select#country > option", collection.filters.country )
});
$( "#domain" ).multiselect({
numberDisplayed : 0,
includeSelectAllOption : true,
onChange : multiSelectOnChangeFunction( "select#domain > option", collection.filters.call_domain )
});
$( "#category" ).multiselect({
numberDisplayed : 0,
includeSelectAllOption : true,
onChange : multiSelectOnChangeFunction( "select#category > option", collection.filters.category )
});
$( "#search" ).change( function() {
var query = $(this).val();
if (! (collection.filters.summary.possibilities[query]) ) {
collection.filters.summary.addPossibility( query );
}
collection.filters.summary.query( query );
resetData();
});
});
$("#loadingdiv").hide();
} );
function children_has_key( n, key ) {
result = false;
n.children.forEach( function(c) {
if (c.name == key) {
result = true;
}
})
return result;
}
function child_with_name( n, key ) {
result = null;
n.children.forEach( function(c) {
if (c.name == key) {
result = c;
}
})
return result;
}
//test for tree from http://bl.ocks.org/mbostock/4063550
function tree1_from_array( a, keys ) {
var id = 1;
var result = { "name" : "All ERC", "children" : [] }
a.forEach( function( i ) {
var ks = keys.slice(0, keys.length)
var n = result;
while (ks.length > 0) {
var this_key_name = ks[0]
var this_key = i[ks[0]]
if (children_has_key(n, this_key)) {
n = child_with_name( n, this_key );
} else {
n.children.push( {
"id" : id,
"name" : this_key,
"children" : []
})
id = id +1;
n = child_with_name(n, this_key)
}
ks = ks.slice(1, ks.length);
}
n.children.push( {
"id" : id,
"name" : i.project + "\n" + i.erc_funding,
"size" : i.erc_funding, //size
"project" : i.project,
"acronym" : i.acronym,
"pi" : i.pi,
"hi" : i.hi,
"country" : i.country,
"call_details": i.call_details,
"hi_website" : i.hi_website,
"erc_funding" : i.erc_funding,
"duration" : i.startDate + "-" + i.endDate,
"category" : i.category,
"summary" : i.summary
});
id = id + 1;
})
return result
}
var children_from_array = function( arr, keys ) {
var _children_from_array = function( carr, ckey ) {
return { name : ckey, children : children_from_array( carr, keys.slice(1,keys.length) ) }
};
var result;
if (keys.length === 0) {
// if keys.length == 0, this is an array of children; assume the name attribute exists
result = arr;
} else {
// otherwise, group by the first key, and children are the remaining keys
result = _.map( _.groupBy( arr, keys[0] ), _children_from_array )
}
return result;
}
var paint_tree = function( node, starting_id ) {
// walks a tree and paints its nodes with an ID and additional data
node.id = starting_id
starting_id = starting_id + 1
if (node.hasOwnProperty( "children" )) {
node.children.forEach( function(c) {
starting_id = paint_tree(c, starting_id)
})
} else {
node.name = node.acronym
node.size = node.erc_funding
}
return starting_id
}
var tree_from_array = function( arr, keys ) {
var result = { name : "All ERC", children : children_from_array( arr, keys ) }
paint_tree( result, 0 );
return result;
}
var margin = { top: 20, right:0, bottom:0, left:0 };
var formatNumber = d3.format(",d");
var svg_width = 960;
var svg_height = 500;
var svg_id = 0;
var transitioning = false;
var colorScale = d3.scale.category20();
var formatMoney = function( d ) {
if ( d > 1000000000 ) {
return "\u20AC" + (d/1000000000).toFixed(2) + "B";
} else if ( d > 1000000 ) {
return "\u20AC" + (d/1000000).toFixed(2) + "M";
} else {
return "\u20AC" + d.toFixed(2);
}
}
var make_bargraph = function(d) {
d._children.sort( function(a,b) { return a.name > b.name; } )
var bargraphDelay = 250;
var bargraphDuration = 750;
var width = 300;
var numChildren = d._children.length;
var maxCostHeight = 25;
var maxLabelHeight = 10;
var barheight = svg_height / d._children.length;
var costHeight = Math.min( Math.max(barheight-5, 1), maxCostHeight );
var labelHeight = Math.min( Math.max( barheight-5, 1), maxLabelHeight );
var dataMax = d3.max(_.pluck( d._children, "size" ))
var barX = d3.scale.linear()
.domain([0, dataMax])
.range([0,width])
var chart = d3.select("div#bargraphDiv").append("svg")
.attr("class", "barchart")
.attr("id", "bargraph")
.attr("width", width )
.attr("height", svg_height )
var bar = chart.selectAll("g")
.data(d._children)
.enter().append("g")
.attr("transform", function(d,i) { return "translate(0, " +i*barheight + ")"; })
bar.append("rect")
.attr("width", 0)
.attr("height", barheight - 1)
.style("fill", function(d) { return colorScale( d.name ); } )
.transition()
.delay( function(d,i) { return i/numChildren*bargraphDelay; } )
.duration( bargraphDuration )
.attr("width", function(d) { return barX(d.size); })
bar.append("rect")
.attr("width", 0)
.attr("height", barheight - 1)
.style("fill", function(d) { return colorScale( d.parent.name ); } )
.style("fill-opacity", 0.5 )
.transition()
.delay( function(d,i) { return i/numChildren*bargraphDelay; } )
.duration( bargraphDuration )
.attr("width", function(d) { return barX(d.size); });
bar.append("text")
.attr("x", 3 )
.attr("y", (barheight + labelHeight)/2 )
.style( "font-size", labelHeight )
.style("text-anchor", "start")
.text( function(d) { return d.name; });
var textwidths = [];
bar.select("text")
.each( function() { textwidths.push( this.getBBox().width ); } );
bar.append("text")
.attr("x", function(d,i) { return Math.max( textwidths[i]+costHeight*5, barX(d.size) - 3 ); } )
.attr("y", ( barheight + costHeight ) / 2 )
.style( "font-size", costHeight + "px" )
.style("text-anchor", function(d,i) { return "end" })
.text( function(d) { return formatMoney(d.size); } );
}
function make_svg() {
return d3.select("div#tree").append("svg")
.attr("id", "treeMap"+svg_id)
.attr("width", svg_width + margin.left + margin.right )
.attr("height", svg_height + margin.top + margin.bottom)
.style("margin.left", -margin.left + "px" )
.style("margin.right", -margin.right + "px" )
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.style("shape-rendering", "crispEdges");
// .attr("transform","translate(" + svg_width/2 + "," + svg_width/2 + ")" );
}
var svg;// = make_svg();
var grandparent;
var make_grandparent = function( svg ) {
var result = svg.append("g")
.attr("class", "grandparent" );
result.append( "rect" )
.attr("y", -margin.top )
.attr("width", svg_width )
.attr("height", margin.top )
result.append( "text" )
.attr("x", 6)
.attr("y", 6-margin.top)
.attr("dy", ".75em" )
return result;
}
/*
var tree = d3.layout.tree()
.size([360, svg_width / 2 - 120])
.separation( function(a,b) { return (a.parent == b.parent ? 1 : 2 ) / a.depth; });
*/
var x = d3.scale.linear()
.domain([0,svg_width])
.range([0, svg_width]);
var y = d3.scale.linear()
.domain([0, svg_height])
.range([0, svg_height]);
function makeTreemap() {
return d3.layout.treemap()
.children( function( d, depth ) { return depth ? null : d._children; } )
.sort( function(a,b) { return a.value - b.value; } )
.ratio( svg_height / svg_width * 0.5 * (1 + Math.sqrt(5)))
.round(false)
.value( function(d) { return d.size; } );
}
var treemap = makeTreemap();
//borrowing heavily from bost.ocks.org/mike/treemap
function setData( data ) {
var initialize = function( root ) {
root.x = root.y = 0;
root.dx = svg_width;
root.dy = svg_height;
root.depth = 0;
}
var accumulate = function(d) {
// aggregate values; "normally done by treemap layout but not here because of our
// custom implementation" -- not sure what this means
return (d._children = d.children) ?
d.size = d.children.reduce( function(p, v) { return p + accumulate(v); }, 0 )
: d.size
}
var layout = function( d ) {
if (d._children) {
treemap.nodes( {_children : d._children} );
d._children.forEach( function(c) {
c.x = d.x + c.x * d.dx;
c.y = d.y + c.y * d.dy;
c.dx *= d.dx;
c.dy *= d.dy;
c.parent = d;
layout(c)
});
}
}
var display = function( d ) {
var transition = function( d) {
if ( transitioning || !d) return;
transitioning = true;
var g2 = display(d),
t1 = g1.transition().duration(750),
t2 = g2.transition().duration(750);
//update the domain only after entering new elements
x.domain([d.x, d.x + d.dx]);
y.domain([d.y, d.y + d.dy]);
//enable antialiasing during transition
svg.style("shape-rendering", null);
//draw child nodes atop parent nodes
svg.selectAll(".depth").sort( function(a,b) { return a.depth - b.depth; } );
// fade-in entering text
g2.selectAll("text").style("fill-opacity", 0)
//transitioning to new view
t1.selectAll("text").call(text).style("fill-opacity", 0);
t2.selectAll("text").call(text).style("fill-opacity", 1);
t1.selectAll("rect").call(rect);
t2.selectAll("rect").call(rect);
//remove old node when transition finished
t1.remove().each("end", function() {
svg.style("shape-rendering", "crispEdges" );
transitioning = false;
});
}
grandparent.datum( d.parent )
.on("click", transition)
.select("text")
.text( name(d) );
var g1 = svg.insert("g", ".grandparent" )
.datum(d)
.attr("class", "depth" );
var g = g1.selectAll("g")
.data(d._children)
.enter().append("g");
g.filter( function(d) { return d._children; })
.classed("children", true)
.on("click", transition);
g.filter( function(d) { return !(d._children); })
.classed("children", false )
.on("click", function(d2) {
$("#projectDetailsProject").text(d2.project);
$("#projectDetailsAcronym").text(d2.acronym);
$("#projectDetailsParticipants").text(d2.print_participants);
$("#projectDetailsCallDetails").text(d2.call_details);
$("#projectDetailsCountry").text(d2.country);
$("#projectDetailsFunding").text(formatMoney(d2.erc_funding));
$("#projectDetailsDuration").text(d2.duration);
$("#projectDetailsCategory").text(d2.category);
$("#projectDetailsSummary").text(d2.summary);
});
g.selectAll(".child")
.data( function(d) { return d._children || [d]; } )
.enter()
.append("rect")
.attr("class", "child" )
.call(rect);
g.append("rect")
.attr("class", "parent")
.call(rect)
.append("title")
.text(function(d) {
if ( d._children ) {
return d.name + "\nSubprojects: " + d._children.length + "\n" + formatMoney(d.value);
} else {
return "Project: " + d.project + "\nAcronym: " + d.acronym
+ "\nPI: " + d.pi + "\nHI: " + d.hi + "\nCountry: " + d.country
+ "\nFunding: " + formatMoney( d.erc_funding ) + "\nDuration: " + d.duration
+ "\nCategory: " + d.category + "\n\nSummary: " + d.summary;
}
});
g.append("text")
.attr("dy", ".75em")
.text( function(d) { return d.name; } )
.call(text)
d3.select("#bargraph").remove();
make_bargraph(d);
return g;
}
var text = function( text ) {
text.attr("x", function(d) { return x(d.x) + 6; } )
.attr("y", function(d) { return y(d.y) + 6; });
}
var rect = function( rect ) {
rect.attr("x", function(d) { return x(d.x); } )
.attr("y", function(d) { return y(d.y); } )
.attr("width", function(d) { return x(d.x + d.dx ) - x(d.x); })
.attr("height", function(d) { return y(d.y + d.dy) - y(d.y); })
.style("fill", function(d) { return colorScale( d.parent.name ); } );
}
var name = function(d) {
return d.parent ? name(d.parent) + "." + d.name : d.name
}
d3.select("#treeMap0").remove();
treemap = makeTreemap();
svg = make_svg();
grandparent = make_grandparent(svg);
initialize( data );
accumulate( data );
layout( data );
display( data );
}
</script>
</body>
</html>