-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankingVis.js
executable file
·361 lines (302 loc) · 10.5 KB
/
rankingVis.js
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* RankingVis object for the Sales Focus Chart visualization.
* @constructor
* @param {number} _visId -- the ID for this visualization instantiation
* @param {object} _parentElement -- the HTML or SVG element to which to attach
* this visualization object
* @param {object} _dataObject -- the object containing the data
* @param {object} _colorMap -- map of music formats to colors
* @param {object} _eventHandler -- the Event Handling object to emit data to
* @returns {RankingVis}
*/
RankingVis = function(_visId, _parentElement, _dataObject, _colorMap, _eventHandler) {
this.visId = _visId;
this.parentElement = _parentElement;
this.dataObject = _dataObject;
this.colorMap = _colorMap;
this.eventHandler = _eventHandler;
this.displayData = [];
this.filterOptions = {};
// define all "constants" here
this.margin = {top: 20, right: 70, bottom: 85, left: 80};
this.width = getInnerWidth(this.parentElement) - this.margin.left -
this.margin.right;
this.height = 265 - this.margin.top - this.margin.bottom;
this.initVis();
};
/**
* Method that sets up the visualization.
*/
RankingVis.prototype.initVis = function() {
var that = this;
// bind to the eventHandler
$(this.eventHandler).bind("dataChanged" + this.visId,
function(event, dataObject) {
that.onDataChange(dataObject);
}
);
$(this.eventHandler).bind("selectionChanged" + this.visId,
function(event, selectStart, selectEnd, autoSelected) {
that.onSelectionChange(selectStart, selectEnd, autoSelected);
});
$(this.eventHandler).bind("highlightChanged",
function(event, highlight) {
that.onHighlightChange(highlight);
}
);
$(this.eventHandler).bind("scaleChanged" + this.visId,
function(event, scale) {
that.onScaleChange(scale);
});
$(this.eventHandler).bind("formatsChanged",
function(event, formats) {
that.onFormatsChange(formats);
}
);
// constructs SVG layout
this.svg = this.parentElement.append("svg")
.attr("width", this.width + this.margin.left + this.margin.right)
.attr("height", this.height+ this.margin.top +this.margin.bottom)
.append("g")
.attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")");
// creates axis and scales
this.yScale = d3.scale.pow()
.range([this.height,0]);
this.xScale = d3.scale.ordinal()
.rangeRoundBands([0, this.width], .1);
this.xAxis = d3.svg.axis()
.scale(this.xScale)
// .ticks(6)
.orient("bottom");
this.yAxis = d3.svg.axis()
.scale(this.yScale)
.tickFormat(function(d) {
// determine formatting string
var formatting = "";
var d_abs = Math.abs(d);
// check if dollar formatting is needed
if (that.dataObject.name.indexOf("Price") >= 0 ||
that.dataObject.name.indexOf("Dollar") >= 0) {
formatting = "$" + formatting;
if (0 < d_abs && d_abs < .05) {
formatting = formatting + ".3f";
}
}
// check if SI/metric formmating is needed
if (1e3 <= d_abs) {
formatting = formatting + "s";
}
// replace 'G' with 'B' for billions for SI/metric formatting
return d3.format(formatting)(d).replace("G", "B");
})
.orient("left");
// Add axes visual elements
this.svg.append("g")
.attr("class", "axis x_axis")
.attr("transform", "translate(0," + this.height + ")")
// .call(this.xAxis)
this.svg.append("g")
.attr("class", "y axis")
.append("g")
.attr("class", "label")
.append("text")
.attr("dy", "-.7em");
// .call(this.yAxis)
// filter, aggregate, modify data
this.wrangleData();
// call the update method
this.updateVis();
}
/**
* Method to wrangle the data.
* @param {function} _filterFunction -- filter function to apply on the data
*/
RankingVis.prototype.wrangleData = function(_filterFunction) {
var selectStart = this.filterOptions.selectStart;
var selectEnd = this.filterOptions.selectEnd;
var formats = this.filterOptions.formats;
var filterFunction = function(d) {
// filter for data within range and contained in formats
return (selectStart ? selectStart <= d.year : true) &&
(selectEnd ? d.year <= selectEnd : true) &&
(formats && formats.length ? formats.indexOf(d.format) >= 0 : true);
};
// displayData holds the data which is visualized
this.displayData = this.filterAndAggregate(this.dataObject.data,
filterFunction, this.dataObject.name);
};
/**
* Method to update the visualization.
* @param {object} _options -- update option parameters
*/
RankingVis.prototype.updateVis = function(_options){
var tDuration = _options && _options.tDuration ? _options.tDuration : 0;
var that = this;
// update scales
{
this.xScale.domain(this.displayData.map(function(d){
return d.key
}))
}
// else{
// this.xScale.domain(d3.keys(this.colorMap2))
// }
yMin = Math.min(0, d3.min(this.displayData.map(function(d){
return d.value
})))
// d3.min(this.displayData, function(d){
// return d.value;
// })
yMax = d3.max(this.displayData, function(d){
return d.value;
});
// if (yMin == yMax) {
// yMin = 0
// }
this.yScale.domain([yMin, yMax]);
// update axis
this.svg.select(".x_axis")
.transition().duration(tDuration)
.call(this.xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("transform", function(d) {
return "rotate(-60)";
})
.attr("dy", -1)
.attr("dx", -8);
this.svg.select(".y.axis")
.transition().duration(tDuration)
.call(this.yAxis)
.select(".label text")
.text(this.dataObject.name);
this.svg.select(".x_axis")
.call(this.xAxis)
.selectAll("text")
.on("mouseover", function(d) {
// trigger highlightChanged event
$(that.eventHandler).trigger("highlightChanged", d);
})
.on("mouseout", function(d) {
// trigger highlightChanged event with no arguments to clear highlight
$(that.eventHandler).trigger("highlightChanged");
});
// Remove the extra bars
// Data join
var bar = that.svg.selectAll(".bar")
.data(this.displayData, function(d) { return d.key; }); // BOUND COUNT DATA IS CORRECTLY FILTERED HERE AFTER BRUSH
// Append new bar groups, if required
var bar_enter = bar.enter().append("g")
.attr("class", "bar");
// Append a rect and a text only for the Enter set (new g)
bar_enter.append("rect")
.style("fill", function(d,i) {
color = that.colorMap[d.key]
return color;
});
bar_enter.on("mouseover", function(d) {
// trigger highlightChanged event
$(that.eventHandler).trigger("highlightChanged", d.key);
});
bar_enter.on("mouseout", function(d) {
// trigger highlightChanged event with no arguments to clear highlight
$(that.eventHandler).trigger("highlightChanged");
});
// .transition()
// .attr("transform", function(d, i) { return "translate(0," + that.yScale(d) + ")"; })
// Add attributes (position) to all bars
// Update all inner rects and texts (both update and enter sets)
bar.select("rect")
.attr("y", function(d) {
return that.yScale(d.value);})
.attr("width", that.xScale.rangeBand())
.attr("height", function(d) {
var barheight = that.height-that.yScale(d.value);
return barheight;
})
.transition().duration(tDuration)
.attr("x", function(d,i){
x = that.xScale(d.key);
return x;
})
bar.exit().remove();
}
RankingVis.prototype.filterAndAggregate = function(_data, _filterFunction, _metricname) {
// set filterFunction to a function that accepts all items
// ONLY if the parameter _filterFunction is null
var filterFunction = _filterFunction || function() { return true; };
// filter the data
var filteredData = _data.filter(filterFunction);
// aggregate the data
var aggregatedData = {}
var aggregatedData_count = {}
{filteredData.forEach(function(d){
if (!(d.format in aggregatedData)){
aggregatedData[d.format] = 0;
aggregatedData_count[d.format] = 0;
}
aggregatedData[d.format] += d.value;
aggregatedData_count[d.format] += 1;
})}
if(_metricname.indexOf("Price") >= 0){
for(var format in aggregatedData){
aggregatedData[format] = aggregatedData[format]/aggregatedData_count[format]
}
}
aggregatedData = d3.entries(aggregatedData);
aggregatedData.sort(function(a,b){
if(b.value > a.value){
return 1;
}
if(b.value < a.value){
return -1;
}
return 0;
})
// }
// return an array of filtered and aggregated data
return (aggregatedData);
};
/**
* Gets called by the Event Handler on a "dataChanged" event,
* re-wrangles the data, and updates the visualization.
* @param {array} newData
*/
RankingVis.prototype.onDataChange = function(newDataObject) {
this.dataObject = newDataObject;
this.wrangleData();
this.updateVis({tDuration: 500});
};
/**
* Gets called by the Event Handler on a "selectionChanged" event,
* re-wrangles the data, and updates the visualization.
* @param {number} selectStart
* @param {number} selectEnd
*/
RankingVis.prototype.onSelectionChange = function(selectStart, selectEnd, autoSelected) {
// save off selection range
this.filterOptions.selectStart = selectStart;
this.filterOptions.selectEnd = selectEnd;
this.wrangleData();
this.updateVis(autoSelected ? {tDuration: 500} : {});
};
RankingVis.prototype.onHighlightChange = function(highlight) {
var formats = this.svg.selectAll(".bar");
formats.classed("faded", function(d) {
return highlight && d.key !== highlight;
});
formats.classed("highlighted", function(d) {
return highlight && d.key === highlight;
});
};
RankingVis.prototype.onScaleChange = function(scale) {
// deform the y scale
this.yScale.exponent(scale);
this.updateVis();
};
RankingVis.prototype.onFormatsChange = function(formats) {
// set format filter options and wrangle data
this.filterOptions.formats = formats;
this.wrangleData();
this.updateVis({tDuration: 500});
};