Skip to content

Commit

Permalink
color scale done
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasalmeida committed Oct 9, 2014
1 parent 58a463b commit 1448b29
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
24 changes: 23 additions & 1 deletion jobs/paintSuffolk.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ openHealth.getScript(["//cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js","ht

)

// Average Key Values

res.avgKeyVals=openHealth.avgKeyValues(res.tab,res.parms.parmKey,res.U_keys,res.parms.parmValue);





Expand All @@ -87,11 +92,17 @@ openHealth.getScript(["//cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js","ht
.dimension(res.D_Zip)
.projection(d3.geo.albersUsa().scale(28000).translate([-8350,2400]))
.group(res.G_Zip)
//.colors(d3.scale.linear().domain([-1,0,0.95,1.1,1.75,10]).range(["silver","green","green","yellow","red","black"]))
.colors(d3.scale.linear().domain([0,0.5,1]).range(["green","yellow","red"]))
.overlayGeoJson(res.zipMap.features, "zip", function (d) {
//console.log(d.properties.ZCTA5CE10)
return d.properties.ZCTA5CE10;
})
.colorAccessor(function(d, i){
if (i==0){ // capture the distribution
res.C_MapMembs=openHealth.memb(res.G_Zip.all().map(function(x){return x.value}));
}
return openHealth.memb([d],res.C_MapMembs)
})

res.C_Row
.width(400)
Expand All @@ -100,6 +111,17 @@ openHealth.getScript(["//cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js","ht
.margins({top: 5, left: 10, right: 10, bottom: 20})
.dimension(res.D_Key)
.group(res.G_Key)
.colors(d3.scale.linear().domain([0,0.9,1.5,2]).range(["white","green","yellow","red"]))
.colorAccessor(function(d, i){
var avg = res.avgKeyVals[d.key];
if(avg==0){avg=d.value/100}
return d.value/avg;
})
.title(function(d){
return Math.round(d.value)+' observed, which is '+Math.round(100*d.value/res.avgKeyVals[d.key])+'% of average '+Math.round(res.avgKeyVals[d.key])
})



4
dc.renderAll();
Expand Down
18 changes: 18 additions & 0 deletions openHealth.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,24 @@ this.unique=function (x){ // x should be an Array
return u;
}

this.avgKeyValue=function(tab,key,val,x){ // find avverage value of a key value pair
// i.e. openHealth.avgKeyValue(paintSuffolk.tab,"Disease state","Septicemia","Rate per 1000")
var tabKey = tab[key];
tabKeyVal=[];
tabKey.map(function(k,i){
if(k===val){tabKeyVal.push(tab[x][i])}
})
return this.sum(tabKeyVal)/tabKeyVal.length
}

this.avgKeyValues=function(tab,key,vals,x){ // aplies .avgKeyValue to all values
var y={}
vals.map(function(v){
y[v]=openHealth.avgKeyValue(tab,key,v,x)
})
return y
}

this.saveFile=function(x,fileName) { // x is the content of the file
// var bb = new Blob([x], {type: 'application/octet-binary'});
// see also https://github.com/eligrey/FileSaver.js
Expand Down

0 comments on commit 1448b29

Please sign in to comment.