-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmicropolar-v0.1.min.js
1 lines (1 loc) · 17.1 KB
/
micropolar-v0.1.min.js
1
var micropolar={version:"0.1.1"},µ=micropolar;µ.Axis=function(){function t(){var i=n.containerSelector;return"string"==typeof i&&(i=d3.select(i)),i.datum(n.data).each(function(t){function i(){var t=d3.mouse(e.node()),a=t[0]-n.width/2,r=t[1]-n.height/2,i=180*((Math.atan2(r,a)+Math.PI)/Math.PI),l=Math.sqrt(a*a+r*r);return{angle:i,radius:l}}"object"!=typeof n.geometry&&(n.geometry=[n.geometry]),"object"!=typeof t[0][0]&&(t=[t]);var l=Math.min(n.width,n.height)/2-n.margin,o=d3.extent(d3.merge(t).map(function(t){return t[1]}));a=d3.scale.linear().domain(n.radialDomain||o).range([0,l]);var c=d3.extent(t[0].map(function(t){return t[0]})),s=n.angularDomain||c,d=n.angularTicksStep||(s[1]-s[0])/n.angularTicksCount;s[2]||(s[2]=d),s[2]/=n.minorTicks+1;var u=d3.range.apply(this,s);u=u.map(function(t){return parseFloat(t.toPrecision(12))}),r=d3.scale.linear().domain(s.slice(0,2)).range(n.flip?[0,360]:[360,0]);var g='<svg xmlns="http://www.w3.org/2000/svg" class="chart-root"> <g class="chart-group"> <circle class="background-circle"></circle> <g class="angular axis-group"></g> <g class="geometry-group"></g> <g class="radial axis-group"> <circle class="outside-circle"></circle> </g> <g class="guides-group"><line></line><circle></circle></g> </g> <g class="legend-group"> </g> </svg>';if("undefined"==typeof e){var f=(new DOMParser).parseFromString(g,"application/xml");this.appendChild(this.ownerDocument.importNode(f.documentElement,!0)),e=d3.select(this).select("svg")}var h={fill:"none",stroke:"silver"},m={"font-size":11,"font-family":"Tahoma, sans-serif"};e.attr({width:n.width,height:n.height}).style({"pointer-events":"none"});var p=e.select(".chart-group").attr("transform","translate("+n.width/2+","+n.height/2+")"),y=e.select(".radial.axis-group");if(n.showRadialCircle){var k=y.selectAll("circle.grid-circle").data(a.ticks(5));k.enter().append("circle").attr({"class":"grid-circle"}).style(h),k.attr("r",a),k.exit().remove()}y.select("circle.outside-circle").attr({r:l}).style(h),e.select("circle.background-circle").attr({r:l}).style({fill:n.backgroundColor,stroke:h.stroke});var v=function(t){return(r(t)+n.originTheta)%360};if(n.showRadialAxis){var x=d3.svg.axis().scale(a).ticks(5).tickSize(5),y=e.select(".radial.axis-group").call(x).attr({transform:"rotate("+n.radialAxisTheta+")"});y.selectAll(".domain").style(h),y.selectAll("g>text").text(function(){return this.textContent+n.radialTicksSuffix}).style(m).style({"text-anchor":"start"}).attr({x:0,y:0,dx:0,dy:0,transform:function(){return"horizontal"===n.radialTickOrientation?"rotate("+-n.radialAxisTheta+") translate("+[0,m["font-size"]]+")":"translate("+[0,m["font-size"]]+")"}}),y.selectAll("g>line").style({stroke:"black"})}var S=e.select(".angular.axis-group").selectAll("g.angular-tick").data(u),T=S.enter().append("g").attr({"class":"angular-tick"});S.attr({transform:function(t,e){return"rotate("+v(t,e)+")"}}),S.exit().remove(),T.append("line").attr({"class":"grid-line"}).classed("major",function(t,e){return 0==e%(n.minorTicks+1)}).classed("minor",function(t,e){return!(0==e%(n.minorTicks+1))}).style(h),T.selectAll(".minor").style({stroke:"#eee"}),S.select("line.grid-line").attr({x1:n.tickLength?l-n.tickLength:0,x2:l}),T.append("text").attr({"class":"axis-text"}).style(m);var w=S.select("text.axis-text").attr({x:l+n.labelOffset,dy:".35em",transform:function(t,e){var a=v(t,e),r=l+n.labelOffset,i=n.angularTickOrientation;return"horizontal"==i?"rotate("+-a+" "+r+" 0)":"radial"==i?270>a&&a>90?"rotate(180 "+r+" 0)":null:"rotate("+(180>=a&&a>0?-90:90)+" "+r+" 0)"}}).style({"text-anchor":"middle"}).text(function(t,e){return 0!=e%(n.minorTicks+1)?"":n.ticks?n.ticks[e/(n.minorTicks+1)]+n.angularTicksSuffix:t+n.angularTicksSuffix}).style(m);n.rewriteTicks&&w.text(function(t,e){return n.rewriteTicks(this.textContent,e)});var b=this;if(n.geometry.forEach(function(e,i){var l="geometry"+i,o=d3.select(b).select("svg g.geometry-group").selectAll("g."+l).data([0]).enter().append("g").classed(l,!0);e.config({data:t[i],axisConfig:n,radialScale:a,angularScale:r,containerSelector:o})()}),n.legend){var A=d3.select(this).select(".legend-group").attr({transform:"translate("+n.width+", 0)"});n.legend.config({containerSelector:A})();var C=A.node().getBBox().width;e.attr({width:n.width+C})}if(n.title){var z=e.append("text").classed("title",!0).attr({x:100,y:100}).style({"font-size":18}).text(n.title),M=z.node().getBBox();z.attr({x:n.width/2-M.width/2,y:M.height}),e.attr({height:n.height+M.height});var p=e.select(".chart-group"),_=d3.transform(p.attr("transform")).translate;p.attr("transform","translate("+_[0]+","+(_[1]+M.height)+")")}e.select(".geometry-group g").style({"pointer-events":"visible"});var P=e.select(".guides-group");p.on("mousemove.angular-guide",function(){var t=i().angle;P.select("line").attr({x1:0,y1:0,x2:-l,y2:0,transform:"rotate("+t+")"}).style({stroke:"grey",opacity:1})}).on("mouseout.angular-guide",function(){P.select("line").style({opacity:0})}),p.on("mousemove.radial-guide",function(){var t=i().radius;P.select("circle").attr({r:t}).style({stroke:"grey",fill:"none",opacity:1})}).on("mouseout.radial-guide",function(){P.select("circle").style({opacity:0})}),e.selectAll(".geometry-group .mark").on("mouseenter.tooltip",function(){})}),t}var e,a,r,n={geometry:[],data:[[0,0],[0,0]],legend:null,title:null,height:300,width:300,radialDomain:null,angularDomain:null,angularTicksStep:null,angularTicksCount:4,flip:!0,originTheta:0,labelOffset:10,radialAxisTheta:-45,ticks:null,radialTicksSuffix:"",angularTicksSuffix:"",angularTicks:null,showRadialAxis:!0,showRadialCircle:!0,minorTicks:0,tickLength:null,rewriteTicks:null,angularTickOrientation:"horizontal",radialTickOrientation:"horizontal",containerSelector:"body",margin:25,backgroundColor:"none"},i=d3.dispatch("hover");return t.config=function(t){return arguments.length?(µ.util._override(t,n),this):n},t.radialScale=function(){return a},t.angularScale=function(){return r},d3.rebind(t,i,"on"),t},µ.util={},µ.util._override=function(t,e){for(var a in t)a in e&&(e[a]=t[a])},µ.util._extend=function(t,e){for(var a in t)e[a]=t[a]},µ.util._rndSnd=function(){return 2*Math.random()-1+(2*Math.random()-1)+(2*Math.random()-1)},µ.util.dataFromEquation=function(t,e){var a=e||6,r=d3.range(0,360+a,a).map(function(e){var a=e*Math.PI/180,r=t(a);return[e,r]});return r},µ.util.ensureArray=function(t,e){if("undefined"==typeof t)return null;var a=[].concat(t);return d3.range(e).map(function(t,e){return a[e]||a[0]})},µ.util.fillArrays=function(t,e,a){return e.forEach(function(e){t[e]=µ.util.ensureArray(t[e],a)}),t},µ.BarChart=function(){function t(){var t=e.containerSelector;"string"==typeof t&&(t=d3.select(t)),t.datum(e.data).each(function(t){var a=e.angularScale.domain(),r=e.angularScale.copy().domain([a[0],a[1]+t[1][0]-t[0][0]]),n={fill:e.fill,stroke:e.stroke},i=12,l=d3.select(this).classed("bar-chart",!0),o=l.selectAll("rect.mark").data(t);o.enter().append("rect").attr({"class":"mark"}),o.attr({x:-i/2,y:e.radialScale(0),width:i,height:function(t){return e.radialScale(t[1])-e.radialScale(0)},transform:function(t){return"rotate("+(e.axisConfig.originTheta-90+r(t[0]))+")"}}).style(n)})}var e={data:null,containerSelector:"body",dotRadius:5,fill:"orange",stroke:"red",radialScale:null,angularScale:null,axisConfig:null},a=d3.dispatch("hover");return t.config=function(t){return arguments.length?(µ.util._override(t,e),this):e},d3.rebind(t,a,"on"),t},µ.Clock=function(){function t(){var t=e.containerSelector;"string"==typeof t&&(t=d3.select(t)),t.datum(e.data).each(function(t){var a=e.radialScale.range()[1],r=[a/1.3,a/1.5,a/1.5],n=[a/15,a/10,a/30];t=[0,4,8],e.angularScale.domain([0,12]);var i={fill:e.fill,stroke:e.stroke},l=d3.select(this).classed("clock",!0),o=l.selectAll("rect.mark").data(t);o.enter().append("rect").attr({"class":"mark"}),o.attr({x:function(t,e){return-n[e]/2},y:function(t,e){return 2==e?-a/5:0},width:function(t,e){return n[e]},height:function(t,e){return r[e]},transform:function(t){return"rotate("+(e.axisConfig.originTheta-90+e.angularScale(t))+")"}}).style(i),l.selectAll("circle.mark").data([0]).enter().append("circle").attr({"class":"mark"}).attr({r:a/10}).style({"fill-opacity":1}).style(i)})}var e={data:null,containerSelector:"body",fill:"orange",stroke:"red",radialScale:null,angularScale:null,axisConfig:null},a=d3.dispatch("hover");return t.config=function(t){return arguments.length?(µ.util._override(t,e),this):e},d3.rebind(t,a,"on"),t},µ.AreaChart=function(){function t(){var t=e.containerSelector;"string"==typeof t&&(t=d3.select(t)),t.datum(e.data).each(function(t){var a=e.angularScale.domain(),r=e.angularScale.copy().domain([a[0],a[1]+t[1][0]-t[0][0]]),n=360/t.length*Math.PI/180/2,i={fill:e.fill,stroke:e.stroke},l=d3.select(this).classed("area-chart",!0),o=l.selectAll("path.mark").data(t);o.enter().append("path").attr({"class":"mark"}),o.attr({d:function(t){var a=e.radialScale(t[1]),r=Math.tan(n)*a;return"M"+[[0,0],[a,r],[a,-r]].join("L")+"Z"},transform:function(t){return"rotate("+(e.axisConfig.originTheta-90+r(t[0]))+")"}}).style(i)})}var e={data:null,containerSelector:"body",dotRadius:5,fill:"orange",stroke:"red",radialScale:null,angularScale:null,axisConfig:null},a=d3.dispatch("hover");return t.config=function(t){return arguments.length?(µ.util._override(t,e),this):e},d3.rebind(t,a,"on"),t},µ.StackedAreaChart=function(){function t(){var t=e.containerSelector;"string"==typeof t&&(t=d3.select(t)),t.datum(e.data).each(function(t){var a=d3.nest().key(function(t){return t[2]}).entries(t);a.forEach(function(t){t.values.forEach(function(t){t.x=t[0],t.y=+t[1]})});var r=d3.layout.stack().values(function(t){return t.values}),n=360/d3.keys(d3.nest().key(function(t){return t[0]}).map(t)).length*Math.PI/180/2,i={fill:function(t){return e.colorScale(t[2])},stroke:"gray"},l=d3.select(this).classed("stacked-area-chart",!0),o=l.selectAll("g.layer").data(r(a)).enter().append("g").classed("layer",!0).selectAll("path.mark").data(function(t){return t.values});o.enter().append("path").attr({"class":"mark"}),o.attr({d:function(t){var a=e.radialScale(t.y+t.y0),r=e.radialScale(t.y0),i=Math.tan(n)*a,l=Math.tan(n)*r;return"M"+[[r,l],[a,i],[a,-i],[r,-l]].join("L")+"Z"},transform:function(t){return"rotate("+(e.axisConfig.originTheta-90+e.angularScale(t[0]))+")"}}).style(i)})}var e={data:null,containerSelector:"body",dotRadius:5,fill:"orange",stroke:"red",radialScale:null,angularScale:null,colorScale:d3.scale.category20(),axisConfig:null},a=d3.dispatch("hover");return t.config=function(t){return arguments.length?(µ.util._override(t,e),this):e},d3.rebind(t,a,"on"),t},µ.DotPlot=function(){function t(){var t=e.containerSelector;"string"==typeof t&&(t=d3.select(t)),t.datum(e.data).each(function(t){var a={fill:e.fill,stroke:e.stroke},r=d3.select(this).classed("dot-plot",!0),n=r.selectAll("circle.mark").data(t);n.enter().append("circle").attr({"class":"mark"}),n.attr({cy:function(t){return e.radialScale(t[1])},r:e.dotRadius,transform:function(t){return"rotate("+(e.axisConfig.originTheta-90+e.angularScale(t[0]))+")"}}).style(a)})}var e={data:null,containerSelector:"body",dotRadius:3,fill:"orange",stroke:"red",radialScale:null,angularScale:null,axisConfig:null},a=d3.dispatch("hover");return t.config=function(t){return arguments.length?(µ.util._override(t,e),this):e},d3.rebind(t,a,"on"),t},µ.LinePlot=function(){function t(){var t=e.containerSelector;"string"==typeof t&&(t=d3.select(t)),t.datum(e.data).each(function(t){var a=d3.svg.line.radial().radius(function(t){return e.radialScale(t[1])}).angle(function(t){return e.angularScale(t[0])*Math.PI/180*(e.axisConfig.flip?1:-1)}),r={fill:"none","stroke-width":e.lineStrokeSize,stroke:e.stroke,"pointer-events":"stroke"},n=d3.select(this).classed("line-plot",!0),i=n.selectAll("path.mark").data([0]);i.enter().append("path").attr({"class":"mark"}),n.select("path.mark").datum(t).attr({d:a,"stroke-width":e.lineStrokeSize+"px"}).style(r)})}var e={data:null,containerSelector:"body",lineStrokeSize:2,stroke:"orange",radialScale:null,angularScale:null,axisConfig:null},a=d3.dispatch("hover");return t.config=function(t){return arguments.length?(µ.util._override(t,e),this):e},d3.rebind(t,a,"on"),t},µ.legend=function(){function t(){var t=e.containerSelector;"string"==typeof t&&(t=d3.select(t));var a=[].concat(e.color),r="number"==typeof e.data[0],n=r?e.height:e.lineHeight*e.data.length,i=t.classed("legend",!0),l=i.append("svg").attr({width:300,height:n+2*e.lineHeight,xmlns:"http://www.w3.org/2000/svg","xmlns:xmlns:xlink":"http://www.w3.org/1999/xlink",version:"1.1"}),o=l.append("g").attr({transform:"translate("+[0,e.lineHeight]+")"}),c=d3.scale[r?"linear":"ordinal"]().domain(e.data).range(a),s=c.copy()[r?"range":"rangePoints"]([0,n]),d=function(t,e){return"line"===t?"M"+[[-e/2,-e/12],[e/2,-e/12],[e/2,e/12],[-e/2,e/12]]+"Z":-1!=d3.svg.symbolTypes.indexOf(t)?d3.svg.symbol().type(t).size(e*e/2)():d3.svg.symbol().type("square").size(e*e/2)()};if(r){var u=o.append("defs").append("linearGradient").attr({id:"grad1",x1:"0%",y1:"0%",x2:"0%",y2:"100%"}).selectAll("stop").data(a);u.enter().append("stop"),u.attr({offset:function(t,e){return 100*(e/(a.length-1))+"%"}}).style({"stop-color":function(t){return t}}),o.append("rect").classed("legend-mark",!0).attr({height:e.height,width:e.colorBandWidth,fill:"url(#grad1)"})}else{var g=o.selectAll("path.legend-mark").data(e.data);g.enter().append("path").classed("legend-mark",!0),g.attr({transform:function(t,a){return"translate("+[e.lineHeight/2,s(t,a)]+")"},d:function(t,a){var r="string"==typeof e.symbol?e.symbol:e.symbol[a];return d(r,e.lineHeight)},fill:function(t,e){return c(t,e)}})}var f=d3.svg.axis().scale(s).orient("right"),h=o.append("g").classed("legend-axis",!0).attr({transform:"translate("+[r?e.colorBandWidth:e.lineHeight,0]+")"}).call(f);h.selectAll(".domain").style({fill:"none",stroke:"none"}),h.selectAll("line").style({fill:"none",stroke:r?e.textColor:"none"}),h.selectAll("text").style({fill:e.textColor,"font-size":e.fontSize}),l.attr({width:l.node().getBBox().width+10})}var e={height:150,lineHeight:20,colorBandWidth:30,fontSize:12,containerSelector:"body",data:["a","b","c"],symbol:"square",color:["red","yellow","limegreen"],textColor:"grey"},a=d3.dispatch("hover");return t.config=function(t){return arguments.length?(µ.util._override(t,e),this):e},d3.rebind(t,a,"on"),t},µ.preset={},µ.preset.linePlot=function(t){t&&t.size&&(t.width=t.height=t.size);var e=µ.LinePlot(),a={geometry:e,data:d3.range(0,721,1).map(function(t,e){return[t,2*(e/720)]}),height:250,width:250,angularDomain:[0,360],angularTicksStep:30,angularTicksSuffix:"º",minorTicks:1,flip:!1,originTheta:0,radialAxisTheta:-30,containerSelector:"body"};µ.util._extend(t,a);var r=µ.Axis().config(a);r()},µ.preset.multiLinePlot=function(t){var e={data:d3.range(0,721,1).map(function(t,e){return[t,2*(e/720)]}),title:"",geometry:"LinePlot",color:"#ffa500",height:250,width:250,isLegendVisible:!1,minorTicks:1,flip:!0,originTheta:-90,radialAxisTheta:-90,radialTicksSuffix:"",containerSelector:"body"};µ.util._extend(t,e),"function"==typeof[].concat(t.data)[0]&&(e.data=e.data.map(function(t){return µ.util.dataFromEquation(t,6)})),"object"!=typeof e.data[0][0]&&(e.data=[e.data]),µ.util.fillArrays(e,["color","geometryName","geometry"],e.data.length),e.geometryName||(e.geometryName=e.data.map(function(t,e){return"Line"+e})),e.geometry=e.geometry.map(function(t,a){return µ[t]().config({stroke:e.color[a]})}),e.isLegendVisible&&(e.legend=µ.legend().config({data:e.geometryName,color:e.color}));var a=µ.Axis().config(e);a()},µ.preset.dotPlot=function(t){t&&t.size&&(t.width=t.height=t.size);var e=µ.DotPlot(),a=d3.scale.linear().domain([-3,3]).range([0,1]),r={geometry:e,data:d3.range(0,100).map(function(){return[~~(1e3*a(µ.util._rndSnd())),~~(100*a(µ.util._rndSnd()))]}),height:250,width:250,angularDomain:[0,1e3],angularTicksStep:100,minorTicks:1,flip:!1,originTheta:0,radialAxisTheta:-15,containerSelector:"body"};µ.util._extend(t,r);var n=µ.Axis().config(r);n()},µ.preset.barChart=function(t){t&&t.size&&(t.width=t.height=t.size);var e=µ.BarChart(),a={geometry:e,data:d3.range(0,16).map(function(t,e){return[50*t,Math.ceil(5*Math.random()*(e+1))]}),height:250,width:250,radialDomain:[-60,100],minorTicks:1,flip:!0,originTheta:0,radialAxisTheta:-10,containerSelector:"body"};µ.util._extend(t,a);var r=µ.Axis().config(a);r()},µ.preset.areaChart=function(t){t&&t.size&&(t.width=t.height=t.size);var e=µ.AreaChart(),a={geometry:e,data:d3.range(0,12).map(function(t){return[50*t+50,~~(10*Math.random()+5)]}),height:250,width:250,radialDomain:[0,20],angularTicksCount:4,ticks:["North","East","South","West"],minorTicks:5,flip:!0,originTheta:-90,radialAxisTheta:-30,radialTicksSuffix:"%",containerSelector:"body"};µ.util._extend(t,a);var r=µ.Axis().config(a);r()},µ.preset.clock=function(t){t&&t.size&&(t.width=t.height=t.size);var e=µ.Clock(),a={geometry:e,data:[12,4,8],height:250,width:250,angularDomain:[0,12],minorTicks:9,flip:!0,originTheta:-90,showRadialAxis:!1,showRadialCircle:!1,rewriteTicks:function(t){return"0"===t?"12":t},labelOffset:-15,tickLength:5,containerSelector:"body"};µ.util._extend(t,a);var r=µ.Axis().config(a);r()};