-
Notifications
You must be signed in to change notification settings - Fork 16
/
catcorr.min.js
1 lines (1 loc) · 8.79 KB
/
catcorr.min.js
1
!function(t){function e(t,e){function r(t){d3.select(this).call(t)}function a(){g.each(r),d3.select("aside.catcorr #active").text(s(d.value()))}function n(t){function e(e){function a(t){for(var e,a=[],n=-1,l=t.length;++n<l;)e=t[n],a.push("M",r(e.key-.5)+y,",",b,"V",i(e.value),"h",_-2*y,"V",b);return a.join("")}function n(){var t=d3.sum(l.__all__),e=d.value(),r=[],a=1;l.__all__.forEach(function(e){r.push((e+a)/(t+a*l.__all__.length))});var n,c=[];return r.forEach(function(t){n=1.96*Math.sqrt(t*(1-t)/e),c.push([e*Math.max(t-n,0),e*Math.min(t+n,1)])}),console.log(c),c}function o(t){return"M"+(t-_/2)+","+-s.top+"h"+_+"v"+(s.top+i.range()+s.bottom)+"h"+-_+"Z"}function g(t){var e=d3.select(this.parentNode);e.selectAll(".asterisk").remove();var a,s,u,h,p,f=[],g=-1,v=t.length,m=d.value();if(m!=c.length)var p=n();for(;++g<v;)a=t[g],s=m/c.length*l.__all__[g],f.push("M",r(a.key-.5)+y,",",i(s),"h",_-2*y),p&&(u=p[g][0],h=p[g][1],f.push("M",r(a.key),",",i(u),"v",i(h)-i(u)),(a.value<u||h<a.value)&&e.insert("path","path.catcorr.all_bar").attr("class","catcorr asterisk").attr("d",o(r(a.key))));return f.join("")}function v(t){var e=+("e"==t),r=e?1:-1,a=b/3;return"M"+.5*r+","+a+"A6,6 0 0 "+e+" "+6.5*r+","+(a+6)+"V"+(2*a-6)+"A6,6 0 0 "+e+" "+.5*r+","+2*a+"Z"+"M"+2.5*r+","+(a+8)+"V"+(2*a-8)+"M"+4.5*r+","+(a+8)+"V"+(2*a-8)}var m=d3.max(r.range()),b=d3.max(i.range());p.tickValues(d3.range(0,d3.keys(r.labels).length)),e.each(function(){var e=d3.select(this),n=e.select("g");if(n.empty()){e.select(".title").append("a").attr("href","javascript:reset("+h+")").attr("class","catcorr reset").text("reset").style("display","none"),n=e.append("svg").attr("width",m+s.left+s.right).attr("height",b+s.top+s.bottom).append("g").attr("transform","translate("+s.left+","+s.top+")");var c=e.select("svg").insert("pattern","g").attr("id","diagonalHatch").attr("patternUnits","userSpaceOnUse").attr("width",10).attr("height",10);c.append("path").attr("class","catcorr hatching").attr("d","M-1,1l2,-2M0,10l10,-10M9,11l2,-2"),u&&n.call(u),n.append("clipPath").attr("id","clip-"+h).append("rect").attr("width",m).attr("height",b),n.selectAll(".bar").data(["all_background","background","foreground","all_proportion"]).enter().append("path").attr("class",function(e,r){return 0===r?"catcorr "+e+" all_bar "+t.type:3===r?"catcorr "+e+" all_bar "+t.type:"catcorr "+e+" bar "+t.type}).datum(l.all()),n.selectAll(".foreground.bar").attr("clip-path","url(#clip-"+h+")"),n.append("g").attr("class","catcorr axis").attr("transform","translate(0,"+b+")").call(p);var o=n.selectAll("g.axis text").text(function(t){var e=20,a=r.labels[t];if(void 0===a)return"";if(a.length>e){var n=a.substring(0,e-3).split(" ");a=n.slice(0,n.length-1).join(" "),a+="..."}return a});u&&(u.html(function(t){return r.labels[t]}),o.on("mouseover",u.show).on("mouseout",u.hide));var i=n.append("g").attr("class","catcorr brush").call(f);i.selectAll("rect").attr("fill","url(#diagonalHatch)").attr("height",b),i.selectAll(".resize").append("path").attr("d",v)}n.selectAll(".bar").attr("d",a),n.selectAll(".all_background.all_bar").attr("d",function(t,e){var r=d3.select(this).attr("d");return null===r?a(t,e):r}),f.empty()?n.selectAll(".all_proportion.all_bar").attr("d",g):n.selectAll(".asterisk").remove()})}n.id||(n.id=0);var r,a,l,o,s={top:10,right:10,bottom:20,left:10},i=m,u=b[n.id],h=n.id++,p=d3.svg.axis().orient("bottom").tickSize(6,0,0),f=d3.svg.brush();return f.on("brushstart.chart",function(){var t=d3.select(this.parentNode.parentNode.parentNode);t.select(".title a").style("display",null)}),f.on("brush.chart",function(){var t=d3.select(this.parentNode),e=f.extent();o&&t.select(".brush").call(f.extent(e=e.map(o))).selectAll(".resize").style("display",null),t.select("#clip-"+h+" rect").attr("x",r(e[0])).attr("width",r(e[1])-r(e[0])),a.filterRange(e)}),f.on("brushend.chart",function(){if(f.empty()){var t=d3.select(this.parentNode.parentNode.parentNode);t.select(".title a").style("display","none"),t.select("#clip-"+h+" rect").attr("x",null).attr("width","100%"),a.filterAll()}else{if(d3.version<"3.3")return;if(!d3.event.sourceEvent)return;var e=f.extent(),r=e.map(function(t){return d3.round(t+.5)-.5});r[0]>=r[1]&&(r[0]=Math.floor(e[0]+.5)-.5,r[1]=Math.ceil(e[1]+.5)-.5),d3.select(this).transition().call(f.extent(r)).call(f.event)}}),e.margin=function(t){return arguments.length?(s=t,e):s},e.x=function(t){return arguments.length?(r=t,p.scale(r),f.x(r),e):r},e.y=function(t){return arguments.length?(i=t,e):i},e.dimension=function(t){return arguments.length?(a=t,e):a},e.filter=function(t){return t?(f.extent(t),a.filterRange(t)):(f.clear(),a.filterAll()),e},e.group=function(t){return arguments.length?(l=t,e):l},e.round=function(t){return arguments.length?(o=t,e):o},d3.rebind(e,f,"on")}var l=e.questions,c=e.responses,o={};l.forEach(function(t){o[t.number]={},t.choices.forEach(function(e,r){o[t.number][e]=r})}),c.forEach(function(t){l.forEach(function(e){t[e.number]=o[e.number][t[e.number]]})}),l.forEach(function(e){d3.select(t).append("div").attr("id",e.number+"-chart").attr("class","catcorr chart").append("div").attr("class","title").text(e.number+". "+e.text)});var s=d3.format(",d"),i=crossfilter(c),d=i.groupAll(),u=[],h=[];l.forEach(function(t,e){u.push(i.dimension(function(e){return e[t.number]})),h.push(u[e].group())}),h.forEach(function(t){t.__all__=t.all().map(function(t){return t.value})});var p,f,g,v=[],m=d3.scale.linear().range([100,0]),b=[],x=[],_=80,y=3;l.forEach(function(t,e){var r={};t.choices.forEach(function(t,e){r[e]=t}),d3.tip&&(f=d3.tip().attr("class","d3-tip").direction("s").html(function(t){return"awesome "+t}),b.push(f));var a=0,l=t.choices.length-1;p=d3.scale.linear().domain([-.5,l+.5]).rangeRound([0,_*(l-a+1)]),p.labels=r,v.push(p),m.domain([0,d3.max([m.domain()[1],h[e].top(1)[0].value])]),g=n(t).dimension(u[e]).group(h[e]).x(p),x.push(g)});var g=d3.selectAll(".catcorr.chart").data(x).each(function(t){t.on("brush",a).on("brushend",a)}),M=d3.select(t).append("aside").attr("id","legend").attr("class","catcorr").html("<div style='clear:both;margin-top:20px'></div><span id='active'>-</span> <span>/</span> <span id='total'>-</span> <br/> selected respondents"),A=300,w=120,j=M.insert("svg","div").attr("width",A).attr("height",w).append("g").attr("transform","translate(0,0)");d3.select(t).append("div").style("clear","both"),j.selectAll(".bar").data(["all_background","background","foreground","all_proportion"]).enter().append("path").attr("class",function(t,e){return 0===e?"catcorr "+t+" all_bar outcome":3===e?"catcorr "+t+" all_bar outcome":"catcorr "+t+" bar outcome"}),j.select(".all_background.all_bar").attr("d",["M",(A-(_-2*y))/2,",",10,"v",100,"h",_-2*y,"v",-100].join("")),j.select(".foreground.bar").attr("d",["M",(A-(_-2*y))/2,",",80,"v",30,"h",_-2*y,"v",-30].join("")),j.select(".all_proportion.all_bar").attr("d",["M",(A-(_-2*y))/2,",",40,"h",_-2*y,"M",A/2,",",15,"v",44].join("")),j.append("foreignObject").attr("class","catcorr legend").attr("width",(A-_)/2).attr("height","3em").attr("x",A/2+_/2+y).attr("y",0).text("all respondents"),j.append("path").attr("class","catcorr legend").attr("d",["M",A/2+_/2,",",7,"h",-15,"l",-7,",",7].join("")),j.append("foreignObject").attr("class","catcorr legend").attr("width",(A-_)/2).attr("height","3em").attr("x",A/2+_/2+y).attr("y",106).text("selected respondents"),j.append("path").attr("class","catcorr legend").attr("d",["M",A/2+_/2,",",113,"h",-15,"l",-7,",",-7].join("")),j.append("foreignObject").attr("class","catcorr legend").attr("width",(A-_)/2).attr("height","3em").attr("x",A/2+_/2+y).attr("y",35).text("expected number of selected respondents"),j.append("path").attr("class","catcorr legend").attr("d",["M",A/2+_/2,",",47,"h",-15,"l",-7,",",-7].join("")),j.append("foreignObject").attr("class","catcorr legend right").attr("width",(A-_)/2-20).attr("height","5em").attr("x",0).attr("y",12).attr("text-align","right").text("variation in expected number of selected respondents"),j.append("path").attr("class","catcorr legend").attr("d",["M",A/2-_/2-18,",",36,"h",15,"v",22,"h",42,"M",A/2-_/2-3,",",36,"v",-22,"h",42].join(""));var k=d3.set();if(l.forEach(function(t){k.add(t.type)}),k=k.values(),k.length>1){var E=20,N=5;M.insert("div","svg").style("clear","both");var V=M.insert("svg","div").attr("width",A).attr("height",k.length*(E+N)+N).style("margin-bottom",20).append("g").attr("transform","translate(0,0)");V.selectAll().data(k).enter().append("path").attr("class",function(t){return"catcorr foreground bar "+t}).attr("d",function(t,e){return["M",A/2-E/2,",",N+e*(E+N),"h",E,"v",E,"h",-E].join("")}),V.selectAll().data(k).enter().append("text").attr("class","catcorr legend").attr("x",A/2+E/2+y).attr("y",function(t,e){return N+e*(E+N)+E/2}).attr("dy","0.35em").text(function(t){return t})}d3.selectAll("aside.catcorr #total").text(s(i.size())),a(),window.filter=function(t){t.forEach(function(t,e){x[e].filter(t)}),a()},window.reset=function(t){x[t].filter(null),a()}}e.version="0.2.0",t.catcorr=e}(this);