-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
174 lines (146 loc) · 4.26 KB
/
index.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
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>
Star Date
</title>
</head>
<style>
body {
font: 14px sans-serif;
background: #272822;
color: #d3d3d3;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.circle {
stroke: #000;
}
.svg {
width: 100%;
height: 100%;
}
h1{
text-align: center;
}
.header{
position: fixed;
}
.logo {
text-align: left;
}
#content {
margin-left: 20%;
}
#infobox {
float: left;
width: 15%;
}
</style>
<body>
<h1 class='logo'>Star-Date</h1>
<!-- <h1 class='intro'>We see light coming from stars that left (X) light years ago. In the same way, those stars RIGHT NOW will see light (including radio waves) that left Earth (x) light years ago. Cick on a star, and see what was happening on earth when that star light we see now left that star</h1> -->
<div id="container">
<div id="infobox"></div>
<div id="content"></div>
</div>
<script src="http://d3js.org/d3.v2.js?2.9.6"></script>
<script>
function getMonths(f) {
return (f % 1) * 12;
}
function parse_date(str) {
var y = str.substr(0,4),
m = str.substr(4,2) - 1,
d = str.substr(6,2);
var D = new Date(y,m,d);
return (D.getFullYear() == y && D.getMonth() == m && D.getDate() == d) ? D : 'invalid date';
}
var dateArray = {};
var x = d3.scale.linear()
.range([0, 1000]);
var y = d3.scale.linear()
.range([500, 0]);
var infobox = d3.select("#infobox"); // select our p tag
infobox.style("visibility", "hidden");
var svg = d3.select("#content").append("svg")
.attr('class', 'svg')
.attr("width", 1000)
.attr("height",500)
.append("g")
d3.csv(("headlines.csv"), function(nyt_data) {
nyt_data.forEach(function(d) {
d.date = parse_date(d.date);
d.date.setHours(0,0,0,0); //ignore time fields
d.headline = d.headline;
d.snippet = d.snippet;
d.link = d.link;
dateArray[d.date] = d;
});
d3.csv(("amnh_data.csv"), function (data) {
data.forEach(function(d) {
d.x = +d.x;
d.y = +d.y;
d.color = d.color;
d.lum = d.lum;
d.distly = d.distly;
d.appmag = d.appmag;
var headlineDate = new Date();
headlineDate.setHours(0,0,0,0);
//console.log(headlineDate.toDateString() );
dist_months = getMonths(d.distly);
headlineDate.setMonth(headlineDate.getMonth() - dist_months);
headlineDate.setYear(headlineDate.getYear() - Math.floor(d.distly));
headlineDate.setDate(1); //only one headline per month
//console.log(headlineDate.toDateString());
d.date = headlineDate;
d.headline = dateArray[headlineDate];
if ( d.headline == undefined ){
//data missing?
headlineDate.setMonth(headlineDate.getMonth() - 1);
d.headline = dateArray[headlineDate];
}
});
// Compute the scales’ domains.
x.domain(d3.extent(data, function(d) { return d.x; })).nice();
y.domain(d3.extent(data, function(d) { return d.y; })).nice();
// Add the points!
svg.selectAll(".circle")
.data(data)
.enter().append("circle")
.attr("class", "circle")
.attr("d", d3.svg.symbol().type("circle"))
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; })
.attr("r", function(d) {
return d.appmag; })
.attr("fill", function(d){return d.color;})
.attr("opacity", .65)
.on("mouseover", function(d) {
var circle = d3.select(this);
var infobox = d3.select("#infobox"); // select our p tag
infobox.style("visibility", "visible"); // on mouse over, it should be visible
if (d.x == 0)
infobox.html("<strong>Our Sun</strong><BR/> 0 light years away");
else
infobox.html(
"<strong>Distance: </strong>" + d.distly + " light years </span><BR /><BR /><strong>"
+ d.headline.date.toDateString() + " " + d.headline.headline + "</strong><BR />" + d.headline.snippet
+ "<BR /> <A HREF=\"" + d.headline.link + "\">link to story</a>");
console.log(d.date);
d3.select(this).transition().duration(500).attr("r", d.appmag * 1.5)
})
.on("mouseout", function(d) {
var circle = d3.select(this);
d3.select(this).transition().duration(500).attr("r", d.appmag)
})
.filter(function(d) { return d.x ==0; })
.style("fill", "#FFF5F2")
});
})
</script>
</div>
</body>
</html>