-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsmokechart.min.js
10 lines (10 loc) · 4.7 KB
/
smokechart.min.js
1
2
3
4
5
6
7
8
9
10
function SmokeChart(g,j,e,i,k){this.parentNode=d3.select(g);var g=this.parentNode.attr("id")||g.replace(/[^a-zA-Z]+/g,""),d=this.parentNode.append("div").attr("class","svg").append("svg").attr("id",g+"-svg").attr("width",j).attr("height",e),l=d3.scale.linear().domain([0,i]).range([0,j]),m=d3.scale.linear().domain([0,k+10]).range([e,0]),o=l(1),g=function(a){return l(a.x)},p=function(a){return isNaN(a.y)?5*e:m(a.y+a.y0)};this.selectAll=function(a){return d.selectAll(a)};this.height=function(a){var b=
d.attr("height");void 0!=typeof a&&d.attr("height",a);return b};var n;this.yaxis=function(a,b,c){l=d3.scale.linear().domain([0,i]).range([b,j]);n=d3.svg.axis().scale(m).ticks(c).tickSubdivide(1).orient(a);d.append("svg:g").attr("class","y axis").attr("transform","translate("+b+",0)").call(n);return this};this.stretch=function(a,b){var c=d.attr("height")-e;a||(a=e);if(k!=b||a!=e)d.attr("height",a+c),m.domain([0,b+10]).range([a,0]),n&&this.selectAll(".y.axis").call(n),e=a,k=b;return this};this._intensity=
0.6;this._layout=function(a){var a=d3.transpose(a),b=[],c=this._intensity,f=a.length/2;a.forEach(function(a,e){var d=[];a.forEach(function(a,b){d.push({x:b,y:a},{x:b+0.9,y:a})});if(e){var q=(Math.abs(e-f)+1)/f;d[0].c=q;d[0].o=(1-q/2)*c}else d[0].c=0,d[0].o=0;b.push(d)});return d3.layout.stack()(b)};this._line_data=function(a){var b=Math.floor(a.length/2),c=a[b];0.01>Math.abs(b-a.length/2)&&a[b-1].forEach(function(a,b){c[b].y0=(c[b].y0+a.y0)/2});return c};var h=d3.svg.area().x(g).y0(function(a){return isNaN(a.y)?
5*e:m(a.y0)}).y1(p),r=d3.svg.line().x(g).y(p);this.addLine=function(a,b,c,f){"undefined"==typeof f&&(f="#999");c=d3.interpolateRgb(c,f);b=this._layout(b);2<b.length&&(f=a+"-area",this.selectAll("path.area."+f).data(b).enter().append("path").attr("class","area "+f).style("fill",function(a){return c(a[0].c)}).style("opacity",function(a){return a[0].o}).attr("d",h));this.selectAll("path.line."+a).data([this._line_data(b)]).enter().append("path").attr("class","line "+a).style("stroke",c(0)).attr("d",
r);return this};this.updateLine=function(a,b){var c=this._layout(b),f=a+"-area";2<c.length?d3.selectAll("path.area."+f).data(c).attr("d",h):d3.selectAll("path.area."+f).style("opacity","0");this.selectAll("path.line."+a).data([this._line_data(c)]).attr("d",r);return this};this.addGuide=function(a,b,c){var f=d3.range(i).map(function(){return[b]});this.addLine(a,f,c);d3.selectAll("path.line."+a).attr("class","guide");return this};this.updateGuide=function(a,b){var c=d3.range(i).map(function(){return[b]});
this.updateLine(a,c);return this};this.labelCount=0;this.showLabels=function(a){var b=o/2,c=a.length;if(c!=this.labelCount){this.selectAll("text.label").remove();this.labelCount=0;if(!c)return;this.labelCount||this.height(e+20)}this.labelCount?this.selectAll("text.label").data(a).text(String):this.selectAll("text.label").data(a).enter().append("text").attr("class","label").attr("x",function(a,e){return b+e*j/c}).attr("y",e+20).attr("text-anchor","middle").text(String);this.labelCount=c;return this};
this.errors=this.selectAll("circle.errors");this.showMarkers=function(a){this.errors.empty()||this.errors.remove();var b=[],c=-1,f=0;a.forEach(function(a){c!=a&&(f=0,c=a);b.push([a,f++])});var d=e/500+1.1;this.errors=this.selectAll("circle.errors").data(b).enter().append("circle").attr("class","errors").attr("cx",o/2).attr("r",d).style("fill","red").style("stroke","none").attr("transform",function(a){return"translate("+l(a[0])+","+(e/300+1+(d+1.5)*a[1])+")"});return this};this.avlCount=0;this.showAvailability=
function(a,b){"undefined"==typeof b&&(b=50);var c=b/100,d=this.avlCount=a.length,g=j/d,h=o*i/d-5,d=function(a,b){return b*g},l=function(a){return Math.floor((100-a)*c+0.9)};this.selectAll("rect.unavailable").data(a).enter().append("rect").attr("class","unavailable").attr("x",d).attr("width",h).attr("height",function(a){a=l(a);return 0<a&&3>a?3:a}).attr("y",e+2);this.selectAll("rect.available").data(a).enter().append("rect").attr("class","available").attr("x",d).attr("width",h).attr("height",function(a){return l(99-
a)}).attr("y",function(a){a=l(a);0<a&&1.5>a&&(a=1.5);return e+a+2}).append("text");this.selectAll("text.avlpercent").data(a).enter().append("text").attr("class","avlpercent").attr("x",function(a,b){return b*g+h/2}).attr("y",function(){return e+b/2+2}).attr("text-anchor","middle").text(String);return this};return this}
FlameChart=function(g,j,e,i,k){fc=SmokeChart(g,j,e,i,k);fc._intensity=0.8;fc._line_data=function(d){return d[0]};fc._layout=function(d){var d=d3.transpose(d),e=[],g=this._intensity,j=d.length;d.forEach(function(d,i){var h=[];d.forEach(function(a,b){h.push({x:b,y:a},{x:b+0.9,y:a})});if(i){var k=(i+1)/j;h[0].c=k;h[0].o=Math.pow(k,1.5)*g}else h[0].c=0,h[0].o=0;e.push(h)});return d3.layout.stack()(e)};return fc};