forked from aboutaaron/backbone-d3-viz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf-neighborhoods-geo.d3.js
66 lines (53 loc) · 1.73 KB
/
sf-neighborhoods-geo.d3.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
define([
'js/common/views/geo.d3',
'd3'
], function(GeoD3View,
d3) {
return GeoD3View.extend({
_sfHousingPrices: null,
_dataDeferred: null,
initialize: function(options) {
GeoD3View.prototype.initialize.apply(this, arguments);
_.bindAll(this, 'filter');
this._colors = d3.scale.linear()
.domain(this.constructor.PRICES)
.range(this.constructor.PALETTE);
this._sfNeighborhoodsGeo = options.sfNeighborhoodsGeo;
this._sfHousingPrices = options.sfHousingPrices;
this._sfNeighborhoodsGeo.on('sync', this.reset, this);
this._sfHousingPrices.on('sync', this.reset, this);
},
reset: function() {
this.collection.reset(this._sfNeighborhoodsGeo.models);
},
enter: function(selection) {
this.colorByPrice(selection);
},
update: function(selection) {
this.colorByPrice(selection);
},
colorByPrice: function(selection) {
var self = this;
selection.each(function(data) {
var medianHousingPrice,
housingPrice = self._sfHousingPrices.findWhere({
zip: parseInt(data.id)
});
if (housingPrice) {
medianHousingPrice = housingPrice.get('zpctile50');
d3.select(this).attr('fill', self._colors(medianHousingPrice/10000));
}
});
},
filter: function(percentileValues) {
var zips = this._sfHousingPrices
.filterByPercentileValues(percentileValues),
filteredNeighbsGeo = this._sfNeighborhoodsGeo.filterByZips(zips);
this.collection.reset(filteredNeighbsGeo);
}
}, {
PRICES: _.range(30, 151, 20),
PALETTE: ["#eff3ff", "#c6dbef", "#9ecae1", "#6baed6", "#4292c6", "#2171b5",
"#084594"]
});
});