-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchart.min.js
1 lines (1 loc) · 12.4 KB
/
chart.min.js
1
function buildDrop(){var b=(d3.select("#drop").append("label").attr("for","college-list").text("Select a college."),d3.select("#drop").append("select").attr("name","college-list").attr("class","form-control").attr("id","college-select")),c=b.selectAll("option").data(d3.keys(graphic_data).sort(function(a,b){return a=a.toLowerCase(),b=b.toLowerCase(),a<b?-1:a>b?1:0})).enter().append("option");c.text(function(a){return a}).attr("value",function(a){return a}).attr("id",function(a){return classify(a)});var d=d3.select("#university-at-buffalo");d.attr("selected","selected")}function buildDropSport(a){var c=(d3.select("#drop-sport").append("label").attr("for","sport-list").text("Select a sport."),d3.select("#drop-sport").append("select").attr("name","sport-list").attr("class","form-control").attr("id","sport-list")),d=c.selectAll("option").data(a.sort(function(a,b){return a=a.sport.toLowerCase(),b=b.sport.toLowerCase(),a<b?-1:a>b?1:0})).enter().append("option");d.text(function(a){return a.sport}).attr("value",function(a){return a.sport})}function formatXLabels(a){var b=a.indexOf("Community College"),d=a.indexOf("University"),e="University".length,f=a.indexOf("College"),g="College".length,h=a.indexOf("New York"),i="New York".length,j=a.indexOf("Track & Field"),k="Track & Field".length,l=a.indexOf("Combined"),n=("Combined".length,a.indexOf("Borough"));"Borough".length;if(b!=-1){if(n!=-1){var p=a.slice(0,b),q=p+"C.C.",r=q.indexOf("Borough");q.slice(0,r);return"CUNY Boro. of MHTN C.C."}var p=a.slice(0,b);return p+"C.C."}if(d!=-1){if(h!=-1){var p=a.slice(0,d),t=p+"U."+a.slice(d+e),u=t.indexOf("New York"),v=t.slice(0,u);return v+"N.Y."+t.slice(u+i)}var p=a.slice(0,d);return p+"U."+a.slice(d+e)}if(f!=-1){if(h!=-1){var p=a.slice(0,f),w=p+"Coll."+a.slice(f+g),u=w.indexOf("New York"),v=w.slice(0,u);return v+"N.Y."+w.slice(u+i)}var p=a.slice(0,f);return p+"Coll."+a.slice(f+g)}if(h!=-1){var p=a.slice(0,h);return p+"N.Y."+a.slice(h+i)}if(isMobile&&j!=-1){var p=a.slice(0,j);return p+"T&F"+a.slice(j+k)}return l!=-1?"All Track":a}function formatYLabels(a){var b=d3.format(".2s")(a);return isMobile?0==b?0:d3.format(".2s")(a):0==b?0:d3.format(".2s")(a)}function drawMain(a,b){function h(){var b=0;for(p in graphic_data[a].sports)null!=graphic_data[a].sports[p].exp_per_female&&null!=graphic_data[a].sports[p].exp_per_male?Math.min(graphic_data[a].sports[p].exp_per_male,graphic_data[a].sports[p].exp_per_female)<b&&(b=Math.min(graphic_data[a].sports[p].exp_per_male,graphic_data[a].sports[p].exp_per_female)):null!=graphic_data[a].sports[p].exp_per_female&&graphic_data[a].sports[p].exp_per_female<b?b=graphic_data[a].sports[p].exp_per_female:null!=graphic_data[a].sports[p].exp_per_male&&graphic_data[a].sports[p].exp_per_male<b&&(b=graphic_data[a].sports[p].exp_per_male);return b}function i(){var b=0;for(p in graphic_data[a].sports)null!=graphic_data[a].sports[p].exp_per_female&&null!=graphic_data[a].sports[p].exp_per_male?Math.max(graphic_data[a].sports[p].exp_per_male,graphic_data[a].sports[p].exp_per_female)>b&&(b=Math.max(graphic_data[a].sports[p].exp_per_male,graphic_data[a].sports[p].exp_per_female)):null!=graphic_data[a].sports[p].exp_per_female&&graphic_data[a].sports[p].exp_per_female>b?b=graphic_data[a].sports[p].exp_per_female:null!=graphic_data[a].sports[p].exp_per_male&&graphic_data[a].sports[p].exp_per_male>b&&(b=graphic_data[a].sports[p].exp_per_male);return b}if(isMobile)var c={top:20,right:19,bottom:105,left:36};else var c={top:20,right:20,bottom:120,left:50};var d=b-c.left-c.right,e=Math.ceil(d*graphic_aspect_height/graphic_aspect_width)-c.top-c.bottom,j=(d3.select("#main-graphic").append("h2").text(a),d3.select("#main-graphic").append("p").text("Located in "+graphic_data[a].city+", this "+graphic_data[a].college_type.toLowerCase()+" school is in the "+graphic_data[a].division+" and has "+d3.format("0,000")(graphic_data[a].enrollment_total)+" students."),d3.scale.ordinal().rangeRoundBands([0,d],.1).domain(d3.keys(graphic_data[a].sports).sort(function(a,b){return a=a.toLowerCase(),b=b.toLowerCase(),a<b?-1:a>b?1:0}))),k=d3.scale.linear().range([e,0]).domain([h(),i()]),l=d3.svg.axis().scale(j).orient("bottom").tickFormat(formatXLabels),m=d3.svg.axis().scale(k).orient("left").tickFormat(formatYLabels);isMobile?m.ticks(6):m.ticks(13);var n=graphic_data[a].sports,o=[];for(var p in n)o.push({sport:p,sportsInfo:n[p]});var q=d3.select("#main-graphic").append("svg").attr("width",d+c.left+c.right).attr("height",e+c.top+c.bottom).append("g").attr("transform","translate("+c.left+","+c.top+")");q.append("g").attr("class","x axis").attr("transform","translate("+j.rangeBand()/4+","+e+")").call(l).selectAll("text").attr("transform",function(){return o.length<3?"translate(0, 0)":isMobile&&o.length>15?"rotate(-75) translate(-7, -13)":"rotate(-45)"}).style("text-anchor",function(){return o.length<3?"beginning":"end"}).style("font-size",function(){return isMobile&&o.length>15?"11px":"12px"}),q.append("g").attr("class","y axis").call(m).append("text").attr("transform",function(){return isMobile?"translate(0,0)":"rotate(-90)"}).attr("y",function(){return isMobile?-20:4}).attr("dy",".71em").style("text-anchor",function(){return isMobile?"beginning":"end"}).text("$ per athlete");var r=q.append("g").attr("class","male bars").attr("transform","translate("+j.rangeBand()/4+", 0)");r.selectAll("rect").data(o).enter().append("rect").attr("class","male bar").attr("fill","#29606b").attr("x",function(a){return j(a.sport)}).attr("width",j.rangeBand()/2).attr("y",function(a){return null==a.sportsInfo.exp_per_male?0:k(a.sportsInfo.exp_per_male)}).attr("height",function(a){return null==a.sportsInfo.exp_per_male?0:e-k(a.sportsInfo.exp_per_male)}).attr("transform",function(a){return null==a.sportsInfo.exp_per_female?"translate("+j.rangeBand()/4+", 0)":"translate(0,0)"});var s=q.append("g").attr("class","female bars").attr("transform","translate("+j.rangeBand()/4+", 0)");s.selectAll("rect").data(o).enter().append("rect").attr("class","female bar").attr("x",function(a){return j(a.sport)}).attr("fill","#f98c60").attr("width",j.rangeBand()/2).attr("transform",function(a){return null==a.sportsInfo.exp_per_male?"translate("+j.rangeBand()/4+", 0)":"translate("+j.rangeBand()/2+", 0)"}).attr("y",function(a){return null==a.sportsInfo.exp_per_female?0:k(a.sportsInfo.exp_per_female)}).attr("height",function(a){return null==a.sportsInfo.exp_per_female?0:e-k(a.sportsInfo.exp_per_female)}),buildDropSport(o),pymChild.sendHeight()}function drawSportChart(a,b,c){function k(){var a=0,c=j.length;for(i=0;i<c;i++){var d=j[i];null!=graphic_data[d].sports[b].exp_per_female_team&&null!=graphic_data[d].sports[b].exp_per_male_team?Math.min(graphic_data[d].sports[b].exp_per_male_team,graphic_data[d].sports[b].exp_per_female_team)<a&&(a=Math.min(graphic_data[d].sports[b].exp_per_male_team,graphic_data[d].sports[b].exp_per_female_team)):null!=graphic_data[d].sports[b].exp_per_female_team&&graphic_data[d].sports[b].exp_per_female_team<a?a=graphic_data[d].sports[b].exp_per_female_team:null!=graphic_data[d].sports[b].exp_per_male_team&&graphic_data[d].sports[b].exp_per_male_team<a&&(a=graphic_data[d].sports[b].exp_per_male_team)}return a}function l(){var a=0,c=j.length;for(i=0;i<c;i++){var d=j[i];null!=graphic_data[d].sports[b].exp_per_female_team&&null!=graphic_data[d].sports[b].exp_per_male_team?Math.max(graphic_data[d].sports[b].exp_per_male_team,graphic_data[d].sports[b].exp_per_female_team)>a&&(a=Math.max(graphic_data[d].sports[b].exp_per_male_team,graphic_data[d].sports[b].exp_per_female_team)):null!=graphic_data[d].sports[b].exp_per_female_team&&graphic_data[d].sports[b].exp_per_female_team>a?a=graphic_data[d].sports[b].exp_per_female_team:null!=graphic_data[d].sports[b].exp_per_male_team&&graphic_data[d].sports[b].exp_per_male_team>a&&(a=graphic_data[d].sports[b].exp_per_male_team)}return a}if(isMobile)var d={top:20,right:20,bottom:150,left:45};else var d={top:20,right:20,bottom:175,left:70};isMobile?(graphic_aspect_width=6,graphic_aspect_height=10):(graphic_aspect_width=5,graphic_aspect_height=5);var e=c-d.left-d.right,f=Math.ceil(e*graphic_aspect_height/graphic_aspect_width)-d.top-d.bottom,h=(d3.select("#secondary-graphics").append("h3").text(b),sports_data[b].length),j=[];for(i=0;i<h;i++)"Public, 2-year"!=graphic_data[a].college_type?graphic_data[sports_data[b][i]].division==graphic_data[a].division&&j.push(sports_data[b][i]):graphic_data[sports_data[b][i]].cc_classification==graphic_data[a].cc_classification&&j.push(sports_data[b][i]);var m=d3.scale.ordinal().rangeRoundBands([0,e],.1).domain(j.sort(function(a,b){return a=a.toLowerCase(),b=b.toLowerCase(),a<b?-1:a>b?1:0})),n=d3.scale.linear().range([f,0]).domain([k(),l()]),o=d3.svg.axis().scale(m).orient("bottom").tickFormat(formatXLabels);isMobile?o.ticks(6):o.ticks(13);var p=d3.svg.axis().scale(n).orient("left").tickFormat(formatYLabels);isMobile?p.ticks(6):p.ticks(13);var q=d3.select("#secondary-graphics").append("svg").attr("width",e+d.left+d.right).attr("height",f+d.top+d.bottom).append("g").attr("transform","translate("+d.left+","+d.top+")");q.append("g").attr("class","x axis").attr("transform","translate("+m.rangeBand()/4+","+f+")").call(o).selectAll("text").attr("transform",function(){return j.length<3?"translate(0, 0)":isMobile&&j.length>15?"rotate(-75) translate(-5, -10)":"rotate(-45)"}).style("text-anchor",function(){return j.length<3?"beginning":"end"}).style("font-size",function(){return isMobile&&j.length>15?"11px":"12px"}),q.append("g").attr("class","y axis").call(p).append("text").attr("transform",function(){return isMobile?"translate(0,0)":"rotate(-90)"}).attr("y",function(){return isMobile?-20:4}).attr("dy",".71em").style("text-anchor",function(){return isMobile?"beginning":"end"}).text("$ per athletic team");var r=[],h=j.length;for(i=0;i<h;i++)r.push({college:j[i],sportsInfo:graphic_data[j[i]].sports[b]});var s=q.append("g").attr("class","male bars").attr("transform","translate("+m.rangeBand()/4+", 0)");s.selectAll("rect").data(r).enter().append("rect").attr("class","male bar").attr("fill","#29606b").attr("x",function(a){return m(a.college)}).attr("width",m.rangeBand()/2).attr("y",function(a){return null==a.sportsInfo.exp_per_male_team?0:n(a.sportsInfo.exp_per_male_team)}).attr("height",function(a){return null==a.sportsInfo.exp_per_male_team?0:f-n(a.sportsInfo.exp_per_male_team)}).attr("transform",function(a){return null==a.sportsInfo.exp_per_female_team?"translate("+m.rangeBand()/4+", 0)":"translate(0,0)"});var t=q.append("g").attr("class","female bars").attr("transform","translate("+m.rangeBand()/4+", 0)");t.selectAll("rect").data(r).enter().append("rect").attr("class","female bar").attr("x",function(a){return m(a.college)}).attr("fill","#f98c60").attr("width",m.rangeBand()/2).attr("transform",function(a){return null==a.sportsInfo.exp_per_male_team?"translate("+m.rangeBand()/4+", 0)":"translate("+m.rangeBand()/2+", 0)"}).attr("y",function(a){return null==a.sportsInfo.exp_per_female_team?0:n(a.sportsInfo.exp_per_female_team)}).attr("height",function(a){return null==a.sportsInfo.exp_per_female_team?0:f-n(a.sportsInfo.exp_per_female_team)}),pymChild.sendHeight()}function drawGraphic(a){function g(){var b=d3.event.target.selectedIndex,c=d3.event.target.children[b].value;drawMain(c,a)}function h(){$secondary_graphics.empty(),$main_graphic.empty(),$dropsports.empty()}function i(){var b=d3.event.target.selectedIndex,c=d3.event.target.children[b].value,d=d3.select("#college-select").node().value;drawSportChart(d,c,a)}function j(){$secondary_graphics.empty()}(void 0==a||isNaN(a))&&(a=600),a<=mobile_threshold?isMobile=!0:isMobile=!1,h();var b={top:20,right:20,bottom:175,left:70};isMobile?(graphic_aspect_width=6,graphic_aspect_height=7):(graphic_aspect_width=5,graphic_aspect_height=5);var c=a-b.left-b.right,f=(Math.ceil(c*graphic_aspect_height/graphic_aspect_width)-b.top-b.bottom,d3.select("#college-select").node().value);drawMain(f,a),d3.select("#drop").on("change",function(){h(),g()}),d3.select("#drop-sport").on("change",function(){j(),i()})}var $main_graphic=$("#main-graphic"),$drop=$("#drop"),$secondary_graphics=$("#secondary-graphics"),$dropsports=$("#drop-sport"),graphic_data,sports_data,graphic_aspect_width,graphic_aspect_height,mobile_threshold=500,pymChild=pym.Child(),graphic_data_url="clean_sports.json",classify=function(a){return a.toLowerCase().replace(/\s+/g,"-").replace(/[^\w\-]+/g,"").replace(/\-\-+/g,"-").replace(/^-+/,"").replace(/-+$/,"")};$(window).load(function(){Modernizr.svg&&d3.json(graphic_data_url,function(a,b){graphic_data=b,buildDrop(),d3.json("sports.json",function(a,b){sports_data=b,pymChild=new pym.Child({renderCallback:drawGraphic})})})});