Skip to content

Commit

Permalink
using quantiles for color scale
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasalmeida committed Oct 7, 2014
1 parent 04ad935 commit afc4059
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
14 changes: 9 additions & 5 deletions jobs/pqiSuffolk.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,30 @@ openHealth.getScript(["//cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js","ht
// ini
function(){return 0}
)

G_zips.n=G_zips.all().length;

C_Map.width(800)
.height(500)
.dimension(zips)
.projection(d3.geo.albersUsa().scale(28000).translate([-8350,2400]))
.group(G_zips)
.colors(d3.scale.quantize().range(["#E2F2FF", "#C4E4FF", "#9ED2FF", "#81C5FF", "#6BBAFF", "#51AEFF", "#36A2FF", "#1E96FF", "#0089FF", "#0061B5"]))
//.colors(d3.scale.quantize().range(["#E2F2FF", "#C4E4FF", "#9ED2FF", "#81C5FF", "#6BBAFF", "#51AEFF", "#36A2FF", "#1E96FF", "#0089FF", "#0061B5"]))
//.colors(d3.scale.quantize().range([d3.rgb(255,0,0).toString(), d3.rgb(255,255,0).toString()]))
//.colors(d3.scale.quantile().range(["#E2F2FF", "#C4E4FF", "#9ED2FF", "#81C5FF", "#6BBAFF", "#51AEFF", "#36A2FF", "#1E96FF", "#0089FF", "#0061B5"]))
.colorDomain([-100, 1000])
//.colorDomain([-100, 1000])
.colors(d3.scale.linear().domain([-1,0,0.75,1]).range(["silver","green","yellow","red"]))
.overlayGeoJson(zipMap.features, "zip", function (d) {
return d.properties.ZCTA5CE10;
})
.title(function(d) {
return "zip: " + d.patient_zipcode;
})
.colorAccessor(function(d, i){
//console.log(i,d)
if(d){return d}
else{return 0}
if(i==0){G_zips.dst=openHealth.memb(G_zips.all().map(function(d){return d.value}))} // update distribution
console.log(i,d)
if(d){return openHealth.memb([d],G_zips.dst)[0]}
else{return -1}
})

C_Pie
Expand Down
37 changes: 37 additions & 0 deletions openHealth.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,43 @@ this.min=function(x){ //return maximum value of array
return x.reduce(function(a,b){if(a<b){return a}else{return b}})
}

this.sum=function(x){
if(Array.isArray(x[0])){return x.map(function(xi){return openHealth.sum(xi)})}
else{return x.reduce(function(a,b){return a+b})};
}

this.sort=function(x){ // [y,I]=sort(x), where y is the sorted array and I contains the indexes
x=x.map(function(xi,i){return [xi,i]});
x.sort(function(a,b){return a[0]-b[0]});
return this.transpose(x)
}

this.interp1=function(X,Y,XI){ // linear interpolation, remember X is supposed to be sorted
var n = X.length;
var YI = XI.map(function(XIi){
var i=openHealth.sum(X.map(function(Xi){if (Xi<XIi){return 1}else{return 0}}));
if (i==0){return Y[0]} // lower bound
else if (i==n){return Y[n-1]} // upper bound
else{return (Y[i-1]+(XIi-X[i-1])*(Y[i]-Y[i-1])/(X[i]-X[i-1]))}
});
return YI
}

this.memb=function(x,dst){ // builds membership function
var n = x.length-1;
if(!dst){
dst = this.sort(x);
Ind=dst[1];
dst[1]=dst[1].map(function(z,i){return i/(n)});
var y = x.map(function(z,i){return dst[1][Ind[i]]});
return dst;
}
else{ // interpolate y from distributions, dst
var y = this.interp1(dst[0],dst[1],x);
return y;
}

}

this.crossdoc2html=function(d){ // create table from cross-document
var html = '<table>';
Expand Down

0 comments on commit afc4059

Please sign in to comment.