-
Notifications
You must be signed in to change notification settings - Fork 34
/
gallery.html
220 lines (185 loc) · 8.47 KB
/
gallery.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
<!doctype html>
<html>
<head>
<title></title>
<meta charset=utf-8>
<script type="text/javascript" src="d3.v3.js"></script>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" title="no title" charset="utf-8">
<link href='http://fonts.googleapis.com/css?family=Monda:400,700' rel='stylesheet' type='text/css'>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-38721366-3', 'christopheviau.com');
ga('send', 'pageview');
</script>
</head>
<body>
<div class="info">D3.js Gallery <span class="example-count"></span> | <a href="./index.html">Static list</a> | <a class="about-link" href="">About</a></div>
<div class="about-box">
<div class="about-close" style="cursor: pointer;">x</div>
<div class="about-content">
You can contribute to this gallery on the <a href="https://docs.google.com/spreadsheet/ccc?key=0AqMEGBUNwXeHdDJ5N0cwUHp4RWVGWkUyVUZrZWVTX2c&usp=drive_web#gid=0">Google Spreadsheet</a> or improving it on <a href="https://github.com/biovisualize/d3visualization">GitHub</a>. <br>
Every entry in this gallery is copyrighted by its author. Feel free to <a href="https://github.com/biovisualize/d3visualization/issues">file a request</a> for correcting errors.<br>
This gallery is developed by Christophe Viau <a href="https://twitter.com/d3visualization">@d3visualization</a> with the help of the community and support from <a href="http://www.datameer.com/">Datameer</a>. Special thanks to <a href="https://twitter.com/mrejfox">@mrejfox</a> for styling.
</div>
</div>
<div id="nav">
<div class="menu1-container"></div>
<div class="menu2-container">
<input type="text" class="menu2-search" />
</div>
</div>
<div id="list">
<div class="menu3-container"></div>
</div>
<script type="text/javascript">
d3.select('.about-link').on('click', function(d, i){
d3.event.preventDefault();
d3.select('.about-box').style({display: 'block'});
})
d3.select('.about-close').on('click', function(d, i){ d3.select('.about-box').style({display: 'none'}); });
var vizTypeScale = d3.scale.category20c();
var splitChar = '; ';
var json;
var data1 = {
href: function(d, i){return '#'+ d.dimension+'=all';},
html: function(d, i){ return d.displayName;}
};
var data2 = {
href: function(d, i){ return '#'+ d.dimension+'='+d.key; },
html: function(d, i){ return d.key+' <span class="value">'+ d.value+'</span>';}
};
var data3 = {
href: function(d, i){return d.url;},
html: function(d, i){
var content = d.title;
if(d.author) content += ' '+'<span class="author">'+d.author+'</span>';
if(d.thumbnail) content += "<br /> <img src='"+d.thumbnail+"' class='thumbnail'/><br />";
return content;}
};
function getDocHeight() {
var doc = document;
return Math.max(
doc.body.scrollHeight, doc.documentElement.scrollHeight,
doc.body.offsetHeight, doc.documentElement.offsetHeight,
doc.body.clientHeight, doc.documentElement.clientHeight
);
}
var thumbSize = 250;
var menu3Width = parseInt(d3.select('#list').style('width'));
var numInOneScreen = Math.ceil(menu3Width / thumbSize) * Math.ceil(getDocHeight() / thumbSize);
var numInView = numInOneScreen;
// d3.csv('https://docs.google.com/spreadsheet/pub?key=0AqMEGBUNwXeHdHpQNlVuY29SUE5BSXVtS3JueGlNYVE&single=true&gid=0&output=csv', function(error, _json){
d3.csv('d3_gallery.csv', function(error, _json){
// d3.csv('d3gallery_feb282013.csv', function(error, _json){
json = _json;
d3.select('.example-count').text('(' + json.length + 'examples!)');
buildList(dataTransformMenu1(), '.menu1-container', data1);
buildList(dataTransformMenu2('visualizationType'), '.menu2-container', data2);
// buildList(dataView(dataTransformMenu3('title', 'all'), numInView), '.menu3-container', data3);
processLocationHash();
});
function dataTransformMenu1(){
//TODO: change in spreadsheet
var dimensionsMap = {author: 'Author', visualizationType: 'Chart type', title: 'Title'};
return d3.keys(dimensionsMap).map(function(d, i){return {dimension: d, displayName: dimensionsMap[d]};});
}
function dataTransformMenu2(dimension, filter){
var splitted = d3.merge(json.map(function(d, i){
return splitter(d[dimension]);
}));
var filtered = (filter) ? splitted.filter(function(d, i){ return d.toLowerCase().indexOf(filter.toLowerCase()) != -1; }) : splitted;
var unemptied = filtered.map(function(d, i){return (d == '') ? 'untagged' : d;});
var grouped = groupCount(unemptied);
var prepared = d3.entries(grouped).map(function(d, i){
d.dimension = dimension;
return d;
});
var sorted = prepared.sort(function(a, b){return b.value - a.value});
return sorted;
}
function splitter(data){
return (data.indexOf(splitChar) !== -1) ? data.split(splitChar) : data;
}
function groupCount(arr) {
var uniques = {}, val;
var dups = {};
for (var i = 0, len = arr.length; i < len; i++) {
val = arr[i];
if (val in uniques) {
uniques[val]++;
dups[val] = uniques[val];
} else uniques[val] = 1;
}
// return(dups);
return(uniques);
}
var cachedData;
function dataTransformMenu3(dimension, value){
var filtered = json.filter(function(d, i){
var splitted = splitter(d[dimension]);
if (value == 'all') return true;
else if(value == 'untagged') return (splitted == '');
//TODO: use the grouped data instead
else if(splitted.indexOf(value) >= 0) return true;
else return false;
});
var sorted = filtered.sort(function(a, b){
return (a.thumbnail == '') - (b.thumbnail == '');
});
cachedData = sorted;
return sorted;
}
function dataView(data, num){
return data.slice(0, num);
}
function buildList(data, containerSelector, content){
d3.select(containerSelector).selectAll("li.entry").remove();
updateList(data, containerSelector, content);
}
function updateList(data, containerSelector, content){
var entries = d3.select(containerSelector)
.selectAll("li.entry")
.data(data)
.enter().append("li")
.attr('class', 'entry')
.append('a')
.attr('href', content.href)
.html(content.html);
}
d3.select('.menu2-search')
.on('change', function(d, i){ filterList(this.value); })
.on('keyup', function(d, i){ filterList(this.value); });
d3.select('body').on('mouseover', function(){
if (d3.event.target.nodeName == 'A') d3.select('.menu2-search').node().blur();
});
function filterList(value){
var dimension = window.location.hash.substring(1).split('=')[0] || 'visualizationType';
buildList(dataTransformMenu2(dimension, value), '.menu2-container', data2);
}
function processLocationHash(){
d3.select('#list').node().scrollTop = 0;
numInView = numInOneScreen;
if(window.location.hash) {
var hash = window.location.hash.substring(1);
var hashSplit = hash.split('=');
if(hashSplit[1] == 'all') buildList(dataTransformMenu2(hashSplit[0]), '.menu2-container', data2);
buildList(dataView(dataTransformMenu3(hashSplit[0], hashSplit[1]), numInView), '.menu3-container', data3);
}else{
buildList(dataView(dataTransformMenu3('title', 'all'), numInView), '.menu3-container', data3);
}
}
d3.select(window).on('hashchange', function () {
processLocationHash();
});
d3.select('#list').on('scroll', function(d, i){
if(this.scrollHeight - this.scrollTop <= getDocHeight()){
numInView += numInOneScreen;
updateList(dataView(cachedData, numInView), '.menu3-container', data3);
}
});
</script>
</body>
</html>