forked from OpenDataDayChicago2017/3D-chart-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinished.html
More file actions
278 lines (216 loc) · 8.39 KB
/
finished.html
File metadata and controls
278 lines (216 loc) · 8.39 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>3D chart!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0px;
overflow: hidden;
}
#container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="js/third-party/threejs/three.js"></script>
<!--<script src="js/third-party/threejs/StereoEffect.js"></script>
<script src="js/third-party/threejs/DeviceOrientationControls.js"></script>-->
<script src="js/third-party/threejs/OrbitControls.js"></script>
<script src="js/optimer_bold.typeface.js"></script>
<script src="js/optimer_regular.typeface.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
var camera, scene, renderer;
var effect, controls;
var element, container;
var marginbottom = 4, chartwidth = 90, chartheight = 30; //3D units
var xscale = d3.scale.linear().range([-chartwidth/2, chartwidth/2]),
yscale = d3.scale.linear().range([0, chartheight]);
var clock = new THREE.Clock();
init();
animate();
function init() {
renderer = new THREE.WebGLRenderer();
element = renderer.domElement;
container = document.getElementById('container');
container.appendChild(element);
// effect = new THREE.StereoEffect(renderer);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(90, 1, 0.001, 700);
camera.position.set(0, 15, 30);
scene.add(camera);
controls = new THREE.OrbitControls(camera, element);
controls.rotateUp(Math.PI / 4);
controls.target.set(
camera.position.x,
camera.position.y + 0.1,
camera.position.z - 0.1
);
controls.noZoom = true;
controls.noPan = true;
function setOrientationControls(e) {
if (!e.alpha) {
return;
}
controls = new THREE.DeviceOrientationControls(camera, true);
controls.connect();
controls.update();
element.addEventListener('click', fullscreen, false);
window.removeEventListener('deviceorientation', setOrientationControls, true);
}
window.addEventListener('deviceorientation', setOrientationControls, true);
var light = new THREE.HemisphereLight(0x777777, 0x000000, 0.6);
scene.add(light);
var objlight = new THREE.PointLight(0xffffff, 0.7);
objlight.position.set(0, 50, 70);
scene.add(objlight);
var texture = THREE.ImageUtils.loadTexture(
'textures/patterns/yellowchecker.png'
);
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat = new THREE.Vector2(50, 50);
texture.anisotropy = renderer.getMaxAnisotropy();
spectexture = THREE.ImageUtils.loadTexture(
'textures/patterns/blacklinegrid.png'
);
spectexture.wrapS = THREE.RepeatWrapping;
spectexture.wrapT = THREE.RepeatWrapping;
spectexture.repeat = new THREE.Vector2(50, 50);
spectexture.anisotropy = renderer.getMaxAnisotropy();
var material = new THREE.MeshPhongMaterial({
color: 0xffffff,
specular: 0xffffff,
shininess: 5,
shading: THREE.FlatShading,
map: texture,
specularMap: spectexture
});
var geometry = new THREE.PlaneGeometry(1000, 1000);
var mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = -Math.PI / 2;
scene.add(mesh);
window.addEventListener('resize', resize, false);
setTimeout(resize, 1);
/////// START CONTENT HERE ////////
d3.csv('data.csv', population, function(data){
xscale.domain([0, data.length - 1]);
yscale.domain([0, d3.max(data, function(d){ return d.all; })]);
var columnwidth = (chartwidth / data.length);
columnwidth -= columnwidth * 0.1;
var columnmaterial = new THREE.MeshPhongMaterial({
color: "#0000ff",
emissive: "#000000"
});
data.forEach(function(d, i, a){
var colheight = yscale(d.all);
var columngeo = new THREE.BoxGeometry(columnwidth, colheight, columnwidth);
var columnmesh = new THREE.Mesh(columngeo, columnmaterial);
columnmesh.position.set(xscale(i), colheight/2 + marginbottom, 0); //Box geometry is positioned at its’ center, so we need to move it up by half the height
scene.add(columnmesh);
});
yscale.ticks(5).forEach(function(t, i, a){
//Draw label
var label = createType({text: "" + (t/1000000), size: 1.5});
var xOffset = ( label.geometry.boundingBox.max.x - label.geometry.boundingBox.min.x );
label.position.set(-chartwidth/2 - xOffset - 2.5, yscale(t) + marginbottom - 0.5, 0);
scene.add(label);
//Draw line
var lineGeometry = new THREE.Geometry();
var vertArray = lineGeometry.vertices;
vertArray.push( new THREE.Vector3(-chartwidth/2 - 1.5, yscale(t) + marginbottom, 0),
new THREE.Vector3(chartwidth/2, yscale(t) + marginbottom, 0) );
lineGeometry.computeLineDistances();
var lineMaterial = new THREE.LineBasicMaterial( { color: 0xaaaaaa } );
var line = new THREE.Line( lineGeometry, lineMaterial );
scene.add(line);
});
});
function population(d) {
d.all = +d.all;
d.male = +d.male;
d.female = +d.female;
return d;
}
var title = createType({text: "U.S. Population By Age, January 2000 in millions"});
title.position.set(-chartwidth/2, chartheight + 10, -5);
scene.add(title);
var xzerolabel = createType({text: "0 years old", size: 1.5});
xzerolabel.position.set(-chartwidth/2 - 1, 2, 0);
scene.add(xzerolabel);
var x100label = createType({text: "100+ years old", size: 1.5});
var xOffset = ( x100label.geometry.boundingBox.max.x - x100label.geometry.boundingBox.min.x );
x100label.position.set(chartwidth/2 - xOffset, 2, 0);
scene.add(x100label);
var srctext = createType({text: "Sauce: U.S. Census", size: 1});
var xOffset = 0.5 * ( x100label.geometry.boundingBox.max.x - x100label.geometry.boundingBox.min.x );
srctext.position.set(0 - xOffset, 1, 0);
scene.add(srctext);
}
function resize() {
var width = container.offsetWidth;
var height = container.offsetHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
// effect.setSize(width, height);
}
function update(dt) {
resize();
camera.updateProjectionMatrix();
controls.update(dt);
}
function render(dt) {
renderer.render(scene, camera);
// effect.render(scene, camera);
}
function animate(t) {
requestAnimationFrame(animate);
update(clock.getDelta());
render(clock.getDelta());
}
function fullscreen() {
if (container.requestFullscreen) {
container.requestFullscreen();
} else if (container.msRequestFullscreen) {
container.msRequestFullscreen();
} else if (container.mozRequestFullScreen) {
container.mozRequestFullScreen();
} else if (container.webkitRequestFullscreen) {
container.webkitRequestFullscreen();
}
}
function createType(options) {
var height = options.height || 0,
size = options.size || 3,
material = new THREE.MeshFaceMaterial( [
new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } ), // front
new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ) // side
] ),
text = options.text || "Test";
var textGeo = new THREE.TextGeometry( text, {
size: size,
height: height,
curveSegments: 4,
font: "optimer",
weight: "normal",
style: "normal",
bevelEnabled: false,
material: 0,
extrudeMaterial: 1
});
textGeo.computeBoundingBox();
var textMesh1 = new THREE.Mesh( textGeo, material );
return textMesh1;
}
</script>
</body>
</html>