-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
239 lines (187 loc) · 6.02 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Test</title>
<script type="text/javascript" src="d3/d3.v3.js"></script>
<style type="text/css">
svg {
border-bottom: 1px;
}
.bar {
fill: #ddd;
}
.g-ESPN {
fill: orange;
}
.g-minor-highlight {
fill: #999;
}
.g-text-minor-highlight {
fill: #000;
font-family: arial, sans-serif;
font-size: 11px;
}
.g-label {
font-family: arial, sans-serif;
font-size: 13px;
font-weight:bold;
}
.axis path,
.axis .domain {
display: none;
}
.axis.bottomaxis line {
stroke-width:1px;
stroke:#000;
}
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.questionHeader {
font-family: arial, sans-serif;
font-size: 14px;
font-weight: bold;
}
.questions {
font-family: arial, sans-serif;
font-size: 12px;
}
</style>
</head>
<body>
<script type="text/javascript">
//OVERARCHING Q'S:
//Why is the y-axis scale flipped upside down? I feel like somewhere in the hack we did to get the bars lined up (height - y(d.X2013)) and //defining the scale we lost the ability to create a proper y axis.
//Defining the margins
var margin = {top: 20, right: 100, bottom: 10, left: 0};
//Defining variables for width and height
var width = 950 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
//Y Scale
var y = d3.scale.linear()
.domain([0,5.6])
.range([0,height]);
//Axes
//Q: What is going on here?
var formatCount = d3.format(",.0f");
var f = d3.format("$.0f");
var yAxis = d3.svg.axis()
.scale(y)
.tickFormat(f)
.tickValues([1,2,3,4,5])
.orient("right");
var bottomyAxis = d3.svg.axis()
.scale(y)
.tickValues([0])
.tickSize(width)
.orient("right");
//Q: Why does appending the svg have to be stored as a variable and why isn't the variable being called anywhere?
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
//Q: What are the pluses?
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + width + ",0)" )
.call(yAxis);
svg.append("g")
.attr("class", "bottomaxis")
.attr("transform", "translate(0," + height + ")" )
.call(bottomyAxis)
.attr("class", "axis bottomaxis");
//This section loading data, making data number from string, then joining it to bars for the bar chart.
d3.csv("subscription-prices.csv", function(err, prices) {
//Numbers as string
prices.forEach(function(d) {
// recasts d.2013 as a number, not a string
d.X2013 = +d.X2013;
});
//Sorting the numbers
prices.sort(function(a,b) {
return a.X2013 - b.X2013;
});
//X scale
var x = d3.scale.ordinal()
.domain([0,prices.length])
.range([0,width]);
//Networks highlighted
var networks = ["Comedy Central","TNT", "C-SPAN"];
//Defining the bar variables
var bar = svg.selectAll(".bar")
.data(prices)
.enter().append("rect")
.attr("height", function(d) {
return y(d.X2013);
})
.attr("width", 4)
//4.38 comes from the 850 width of the chart divided by the 192 observations in the data
.attr("x", function(d, i) {
return 4.38 * i;
})
.attr("y", function(d) {
return height - y(d.X2013);
})
.attr("class", "bar")
//Identifying the networks that are identified in the NYT graphic (dark grey)
.classed("g-ESPN", function(d) {
return d.Network == "ESPN"
})
.classed("g-minor-highlight", function(d) {
return networks.indexOf(d.Network) >= 0;
});
console.log(prices)
});
//Labels for highlighted Networks
//ESPN
svg.append("text")
.attr("class", "g-label")
.attr("x", 855)
.attr("y", 15)
.text("ESPN");
//ESPN DESCRIPTION PART 1
svg.append("text")
.attr("class", "g-text-minor-highlight")
.attr("x", 855)
.attr("y", 28)
.text("$5.54");
//ESPN DESCRIPTION PART 2
svg.append("text")
.attr("class", "g-text-minor-highlight")
.attr("x", 855)
.attr("y", 40)
.text("per subscriber");
//TNT
svg.append("text")
.attr("class", "g-text-minor-highlight")
.attr("x", 770)
.attr("y", 435)
.text("TNT ($1.24)");
//Comedy Central
svg.append("text")
.attr("class", "g-text-minor-highlight")
.attr("x", 490)
.attr("y", 545)
.text("Comedy Central ($0.18)");
//C-SPAN
svg.append("text")
.attr("class", "g-text-minor-highlight")
.attr("x", 101)
.attr("y", 559)
.text("C-SPAN ($0.06)");
//Three journalistic questions
d3.select("body").append("span").html("</br>" + "Three Journalistic Questions")
.attr("class", "questionHeader");
d3.select("body").append("p").html("1) How is each share calculated?" + "</br>" + "2) How has the total allocation changed over time and has the money been redistributed? Which channels have seen the biggest gains versus the sharpest drops?" +"</br>" + "3) How big is the lobbying power of each channel?")
.attr("class", "questions");
</script>
</body>
</html>