Repository navigation
Expand file tree
/
Copy pathyear_slider.js
More file actions
257 lines (242 loc) · 7.94 KB
/
Copy pathyear_slider.js
File metadata and controls
257 lines (242 loc) · 7.94 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
function YearSlider(){
var id = "year-slider";
var height = 10
, width = 770
var rectWidth = 30
, rectHeight = 25;
//var years = [1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010];
var years = [1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010];
var parseDate = d3.timeParse("%Y")
var consumption_extent
, choroplethScale;
var filterYear;
//var title = "Year-End Energy Consumption & Feature Efficiency Ratings for a YEAR's Home | by Weather Region".split("|");
var title = "50 Years of Consumption & Efficiency"
var slider_heading_title = "Home Vintage: YEAR";
var change;
var previous_year = filterYear;
function chart(selection) {
selection.each(function(map_data) {
var slider = d3.select(this)
.attr("transform","translate(20,650)");
var dates = years.map(d => parseDate(d));
var date_range = d3.extent(dates);
// create axis
var xScale = d3.scaleTime()
.domain([parseDate("1946"),parseDate("2004")])
.range([0,width]);
var ticks = xScale.ticks(5);
var xStart = xScale(parseDate(filterYear))-14;
var xAxis = d3.axisBottom(xScale)
.tickValues(ticks)
slider.call(xAxis)
// create some definitions
var defs = slider.append("defs");
// create right arrow marker
defs.append("marker")
.attr("id", "slider_arrow_right")
.attr("viewBox", "0 -5 10 10")
.attr("refX", 0)
.attr("refY", 0)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5")
.attr("stroke-width","2px")
.style("fill",d3.rgb(255, 255, 255))
.style("stroke",d3.rgb(255, 255, 255))
// create right arrow
slider.append("line")// attach a line
.attr("id","arrow_right")
.style("opacity",1)
.style("fill","black")
.style("stroke", "black")
.attr("x1", xScale(parseDate(filterYear))+13.2)
.attr("y1", -11.3)
.attr("x2", xScale(parseDate(filterYear))+14.3)
.attr("y2", -11.3)
.attr("marker-end","url(#slider_arrow_right)");
// create left arrow marker
defs.append("marker")
.attr("id", "slider_arrow_left")
.attr("viewBox", "0 -5 10 10")
.attr("refX", 0)
.attr("refY", 0)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5")
.attr("fill","black")
.attr("stroke","black")
.style("fill",d3.rgb(255, 255, 255))
.style("stroke",d3.rgb(255, 255, 255)) // create arrow
slider.append("line")// attach a line
.attr("id","arrow_left")
.style("opacity",1)
.style("fill","black")
.style("stroke", "black")
.attr("x1", xScale(parseDate(filterYear))-11.5)
.attr("y1", -11.3)
.attr("x2", xScale(parseDate(filterYear))-12.5)
.attr("y2", -11.3)
.attr("marker-end","url(#slider_arrow_left)");
// create rect for dragging
slider.append("rect")
.classed("dragger",true)
.attr("x",xStart)
.attr("y",-rectHeight)
.attr("height",rectHeight)
.attr("width",rectWidth)
.style("fill","blue")
.style("opacity",0.0)
.call(d3.drag()
.on("start",dragstarted)
.on("drag", dragged)
.on("end", dragended))
// create small inner circle.
slider.insert("circle",":first-child")
.attr("id","slider_highlight")
.attr("fill",d3.rgb(255, 255, 255))
.attr("stroke","black")
.attr("stroke-width",1)
.attr("r",5)
.attr("cx",xStart+15)
.attr("cy",-11);
// create larger outer circle
slider.insert("circle",":first-child")
.attr("id","slider_bug")
.attr("r",9)
.attr("cx",xStart+15)
.attr("cy",-11);
slider.insert("circle",":first-child")
.attr("id","slider_bug_background")
.attr("r",9)
.attr("cx",xStart+15)
.attr("cy",-11);
// insert image (doesn't seem to work in some browsers)
/*
slider.insert("image",":first-child")
.attr("id","slider_img")
.attr("xlink:href","img/slider.png")
.attr("x",xStart)
.attr("y",-15)
.attr("opacity",1)
.attr("width","30px")
*/
// create main title
slider.append("text")
.attr("id","chart-title")
.attr("x",0)
.attr("y",-580)
.text(title);
//.text(title.replace("YEAR",filterYear));
/*
// create sub title
slider.append("text")
.attr("id","chart-title")
.classed("slider text",true)
.attr("x",440)
.attr("y",-460)
.text(title[1]);
*/
slider.append("text")
.attr("id","slider-heading")
.classed("heading",true)
.attr("x",0)
.attr("y",-35)
.text(slider_heading_title.replace("YEAR",filterYear));
// label slider
/*
slider.append("text")
.attr("id","ball-label")
.classed("slider text",true)
.attr("x",xScale(parseDate(filterYear))+1)
.attr("y",-23)
.text("Choose Vintage");
*/
function dragstarted(d) {
d3.select("#slider_highlight").classed("active", true);
}
function dragged(d) {
var x_value = d3.max([0, d3.min([d3.event.x,width - rectWidth])]);
// change value of invisible slider rect
d3.select(this).attr("x", d.x = x_value);
// change xValue of slider image
d3.selectAll("#slider_img").attr("x", x_value);
// change xValue of slider label
d3.selectAll("#ball-label").attr("x", x_value+16);
// change xValue of circle behind image
d3.selectAll("#slider_highlight").attr("cx", 15 + x_value);
// change xValue of large center circle
d3.selectAll("#slider_bug").attr("cx", 15 + x_value);
d3.selectAll("#slider_bug_background").attr("cx", 15 + x_value);
// change slider arrows
d3.select("#arrow_right")
.attr("x1", x_value+27)
.attr("x2", x_value+28)
d3.select("#arrow_left")
.attr("x1", x_value+3)
.attr("x2", x_value+2)
// change title
var year = xScale.invert(x_value).getFullYear();
var rounded_year = Math.round(year/10)*10;
//var rounded_year = (year>2000)? 2000:Math.round(year/10)*10;
d3.select("#slider-heading")
.text(slider_heading_title.replace("YEAR",rounded_year));
if (previous_year != rounded_year){
change.call("year_change",this,rounded_year)
previous_year = rounded_year;
}
}
function dragended(d) {
d3.select("#slider_highlight").classed("active", false);
}
//end selection
})
// end chart
}
chart.id = function(i) {
if (!arguments.length) { return id; }
id = i;
return chart;
};
chart.width = function(w) {
if (!arguments.length) { return width; }
width = w;
return chart;
};
chart.height = function(h) {
if (!arguments.length) { return height; }
height = h;
return chart;
};
chart.consumption_extent = function(c) {
if (!arguments.length) { return consumption_extent; }
consumption_extent = c;
return chart;
};
chart.choroplethScale= function(c) {
if (!arguments.length) { return choroplethScale; }
choroplethScale = c;
return chart;
};
chart.years = function(y) {
if (!arguments.length) { return years; }
years = y;
return chart;
};
chart.filterYear = function(y) {
if (!arguments.length) { return filterYear; }
filterYear = y;
return chart;
};
chart.change = function(c) {
if (!arguments.length) { return change; }
change = c;
return chart;
};
// end YearSlider
return chart
}