UNPKG

d3-parliament-chart

Version:

parliament / congress / legislature charts simplified

1 lines 3.29 kB
!function(t,a){"object"==typeof exports&&"undefined"!=typeof module?a(exports):"function"==typeof define&&define.amd?define(["exports"],a):a((t="undefined"!=typeof globalThis?globalThis:t||self).d3=t.d3||{})}(this,function(t){"use strict";var r=(t,{sections:a,sectionGap:m,seatRadius:x,rowHeight:k},e)=>{const v=e/2;e=Math.min(t,a);const r=Math.PI/e,s=Array(e).fill({seats:Math.floor(t/e)}).map((t,a)=>({...t,startRad:a*r,endRad:(a+1)*r}));let n=t%e;if(0!==n){const o=Array(e).fill(null).map((t,a)=>a),i=o.splice(0,Math.floor(e/2)).reverse();for(;0<n;)i.length>=o.length?s[i.shift()].seats+=1:s[o.shift()].seats+=1,--n}return s.map(t=>{{var{seats:r,startRad:s,endRad:n,seatRadius:o,rowHeight:i,graphicHeight:l,sectionGap:c}={...t,seatRadius:x,rowHeight:k,graphicHeight:v,sectionGap:m},h=l-o;let a=[],e=0;for(;a.length<r;){var d=h-i*e,p=Math.atan((o+c/2)/d),f=s+p,g=n-p;if(g<=f||d<=0)break;var p=Math.atan(2.5*o/d),u=Math.min(Math.floor((g-f)/p),r-a.length-1),y=u?(g-f)/u:0;for(let t=0;t<=u;t+=1){var M=u?t*y+f:(f+g)/2;a=a.concat([{x:Math.cos(M)*(h-i*e)+l,y:l-(Math.sin(M)*(h-i*e)+o)+o+c/4,angle:M}])}e+=1}return a}}).reduce((t,a)=>[...t,...a],[]).sort((t,a)=>a.angle-t.angle).map(t=>{const{angle:a,...e}=t;return e})};const M="#1abc9c";t.getParliamentPoints=r,t.parliamentChart=(t=[],a=0)=>{let f=parseFloat(a),e=t.map(({x:t,y:a,...e})=>e);const g={sections:4,sectionGap:60,seatRadius:12,rowHeight:42};let u=!1;const y=t=>{0===f&&(f=t.node().getBoundingClientRect().width);var a=y.data().filter(t=>t.x&&t.y);t.select("g.parliament-chart").remove();const e=t.append("g").attr("class","parliament-chart");if(e.select("g.debug").remove(),u){var t=e,r=f,s=g,n=a.length,o=Math.min(n,s.sections);const p=t.append("g").attr("class","debug").attr("transform",`translate(0, ${s.sectionGap/4})`);var i=r/2,l=(p.append("path").attr("d",`M${r},${i} a1,1 0 0,0 -${r},0 m 0 -${s.sectionGap/2} l ${r} 0`).attr("stroke",M).attr("stroke-width",2).attr("stroke-dasharray","5 5").attr("fill","none"),Math.PI/o);for(let t=1;t<o;t+=1){var c,h,d=l*t;s.sectionGap<=0?p.append("line").attr("x1",i).attr("y1",i).attr("x2",Math.cos(d)*i+i).attr("y2",i-Math.sin(d)*i).attr("stroke",M).attr("stroke-width",2).attr("stroke-dasharray","5 5"):(c=Math.sin(d)*(s.sectionGap/2),h=Math.cos(d)*(s.sectionGap/2),p.append("line").attr("x1",i-c).attr("y1",i-h).attr("x2",Math.cos(d)*i+i-c).attr("y2",i-Math.sin(d)*i-h).attr("stroke",M).attr("stroke-width",2).attr("stroke-dasharray","5 5"),p.append("line").attr("x1",i+c).attr("y1",i+h).attr("x2",Math.cos(d)*i+i+c).attr("y2",i-Math.sin(d)*i+h).attr("stroke",M).attr("stroke-width",2).attr("stroke-dasharray","5 5"))}}return e.selectAll("circle").data(a).enter().insert("circle").attr("cx",t=>t.x).attr("cy",t=>t.y).attr("r",g.seatRadius).attr("fill",t=>t.color||"#AAA")};return y.width=t=>(isNaN(t)||(f=parseFloat(t)),y),Object.keys(g).forEach(a=>{y[a]=t=>isNaN(t)?g[a]:(g[a]=parseInt(t,10),y)}),y.debug=t=>(u=!!t,y),y.data=t=>{if(t)return e=t.map(({x:t,y:a,...e})=>e),y;if(f<=0||e.length<=0)return e;if(e.every(t=>t.x&&t.y))return e;t=e.length;const a=r(t,g,f);return a.forEach((t,a)=>e[a]={...e[a],...t}),e},y.aggregatedData=t=>(e=t.reduce((t,a)=>{const{seats:e=0,x:r,y:s,...n}=a;return[...t,...Array(e).fill(n)]},[]),y),y},Object.defineProperty(t,"__esModule",{value:!0})});