1
|
|
|
function buildCharts(){sugarloaf.ndx=crossfilter(sugarloaf.data.trails),sugarloaf.openDim=sugarloaf.ndx.dimension(function(a){return a.open?"Open":"Closed"}),sugarloaf.openGroup=sugarloaf.openDim.group().reduceCount(function(a){return a.open}),sugarloaf.openChart=dc.rowChart("#chart-row-open"),sugarloaf.openChart.width(WIDTH).height(HEIGHT/2).margins(MARGINS).dimension(sugarloaf.openDim).group(sugarloaf.openGroup).elasticX(!0),sugarloaf.groomedDim=sugarloaf.ndx.dimension(function(a){return a.groomed?"Groomed":"Ungroomed"}),sugarloaf.groomedGroup=sugarloaf.groomedDim.group().reduceCount(function(a){return a.groomed}),sugarloaf.groomedChart=dc.rowChart("#chart-row-groomed"),sugarloaf.groomedChart.width(WIDTH).height(HEIGHT/2).margins(MARGINS).dimension(sugarloaf.groomedDim).group(sugarloaf.groomedGroup).elasticX(!0),sugarloaf.snowmakingDim=sugarloaf.ndx.dimension(function(a){return a.snowmaking?"Snowmaking in progress":"Not snowmaking"}),sugarloaf.snowmakingGroup=sugarloaf.snowmakingDim.group().reduceCount(function(a){return a.snowmaking}),sugarloaf.snowmakingChart=dc.rowChart("#chart-row-snowmaking"),sugarloaf.snowmakingChart.width(WIDTH).height(HEIGHT/2).margins(MARGINS).dimension(sugarloaf.snowmakingDim).group(sugarloaf.snowmakingGroup).elasticX(!0),sugarloaf.difficultyDim=sugarloaf.ndx.dimension(function(a){return a.difficulty}),sugarloaf.difficultyGroup=sugarloaf.difficultyDim.group().reduceCount(function(a){return a.difficulty}),sugarloaf.difficultyChart=dc.rowChart("#chart-row-difficulty"),sugarloaf.difficultyChart.width(WIDTH).height(HEIGHT).margins(MARGINS).dimension(sugarloaf.difficultyDim).group(sugarloaf.difficultyGroup).ordering(function(a){return sugarloaf.difficulty_order[a.key]}).elasticX(!0),sugarloaf.areaDim=sugarloaf.ndx.dimension(function(a){return a.area}),sugarloaf.areaGroup=sugarloaf.areaDim.group().reduceCount(function(a){return a.area}),sugarloaf.areaChart=dc.rowChart("#chart-row-area"),sugarloaf.areaChart.width(WIDTH).height(2*HEIGHT).margins(MARGINS).dimension(sugarloaf.areaDim).group(sugarloaf.areaGroup).elasticX(!0),dc.renderAll()}function summaryToDates(a){var r={};a.conditions.forEach(function(a){void 0===r[a.datetime]&&(r[a.datetime]={}),a.open&&void 0===r[a.datetime][a.difficulty]?r[a.datetime][a.difficulty]=a.trail_count:a.open?r[a.datetime][a.difficulty]+=a.trail_count:void 0===r[a.datetime].closed?r[a.datetime].closed=a.trail_count:r[a.datetime].closed+=a.trail_count});var o=[];return Object.keys(r).forEach(function(a){var t=r[a];t.datetime=sugarloaf.parseDate.parse(a),o.push(t)}),o.sort(function(a,r){return a.datetime<r.datetime?-1:1}),o}function datesToDataset(a){var r=[];return Object.keys(sugarloaf.difficulty_order).forEach(function(o){var t=[];a.forEach(function(a){var r={x:a.datetime};void 0===a[o]?r.y=0:r.y=a[o],t.push(r)}),r.push(t)}),r}function countDate(a){var r=0;for(var o in a)a.hasOwnProperty(o)&&"datetime"!==o&&(r+=a[o]);return r}function buildSummaryChart(a){sugarloaf.dates=summaryToDates(a),width=800-MARGINS.left-MARGINS.right,height=200-MARGINS.top-MARGINS.bottom,sugarloaf.dataset=datesToDataset(sugarloaf.dates),sugarloaf.summary_x=d3.time.scale().range([0,width]).domain(d3.extent(sugarloaf.dates.map(function(a){return a.datetime}))),sugarloaf.summary_y=d3.scale.linear().rangeRound([height,0]).domain([0,d3.max(sugarloaf.dates,function(a){return countDate(a)})]),sugarloaf.summary_stack=d3.layout.stack(),sugarloaf.summary_stack_layers=sugarloaf.summary_stack(sugarloaf.dataset),sugarloaf.summary_area=d3.svg.area().interpolate("cardinal").x(function(a){return sugarloaf.summary_x(a.x)}).y0(function(a){return sugarloaf.summary_y(a.y0)}).y1(function(a){return sugarloaf.summary_y(a.y0+a.y)}),sugarloaf.summary_color=d3.scale.ordinal().range(["#00A64B","#2D2D94","#6D6D6D","#000","#F6AE3B","#FFF"]).domain(["green","blue","black","double-black","terrain-park","closed"]),sugarloaf.summary_xAxis=d3.svg.axis().scale(sugarloaf.summary_x).orient("bottom").ticks(10).tickFormat(d3.time.format("%b %e")),sugarloaf.summary_yAxis=d3.svg.axis().scale(sugarloaf.summary_y).orient("left");var r=d3.select("#chart-summary").append("svg").attr("width",width+MARGINS.left+MARGINS.right).attr("height",height+MARGINS.top+MARGINS.bottom).append("g").attr("transform","translate("+MARGINS.left+","+MARGINS.top+")"),o=r.selectAll(".series").data(sugarloaf.summary_stack_layers).enter().append("path").attr("class","layer").attr("d",function(a){return sugarloaf.summary_area(a)}).style("fill",function(a,r){return sugarloaf.summary_color(Object.keys(sugarloaf.difficulty_order)[r])});r.append("g").attr("class","x axis").attr("transform","translate(0,"+height+")").call(sugarloaf.summary_xAxis),r.append("g").attr("class","y axis").call(sugarloaf.summary_yAxis)}var filename_status="/api/current",filename_summary="/api/summary",WIDTH=300,HEIGHT=200,MARGINS={top:10,left:30,right:20,bottom:20},sugarloaf={};sugarloaf.difficulty_order={beginner:1,intermediate:2,black:3,"double-black":4,"terrain-park":5},sugarloaf.parseDate=d3.time.format("%Y-%m-%dT%H:%M:%S"),d3.json(filename_status,function(a){sugarloaf.data=a,buildCharts()}),d3.json(filename_summary,function(a){sugarloaf.summary=a,buildSummaryChart(sugarloaf.summary)}); |