diff --git a/c3.js b/c3.js index 9110506..e3621e9 100644 --- a/c3.js +++ b/c3.js @@ -2069,7 +2069,9 @@ }; function categoryAxis() { - var scale = d3.scale.linear(), orient = "bottom", tickMajorSize = 6, /*tickMinorSize = 6,*/ tickEndSize = 6, tickPadding = 3, tickCentered = false, tickTextNum = 10, tickOffset = 0, categories = []; + var scale = d3.scale.linear(), orient = "bottom"; + var tickMajorSize = 6, /*tickMinorSize = 6,*/ tickEndSize = 6, tickPadding = 3, tickCentered = false, tickTextNum = 10, tickOffset = 0, tickFormat = null; + var categories = []; function axisX(selection, x) { selection.attr("transform", function (d) { return "translate(" + (x(d) + tickOffset) + ", 0)"; @@ -2100,6 +2102,10 @@ function category(i) { return i < categories.length ? categories[i] : i; } + function formattedCategory(i) { + var c = category(i); + return tickFormat ? tickFormat(c) : c; + } function axis(g) { g.each(function () { var g = d3.select(this); @@ -2135,7 +2141,7 @@ lineUpdate.attr("x1", tickX).attr("x2", tickX).attr("y2", tickMajorSize); textUpdate.attr("x", 0).attr("y", Math.max(tickMajorSize, 0) + tickPadding); text.attr("dy", ".71em").style("text-anchor", "middle"); - text.text(function (i) { return shouldShowTickText(ticks, i) ? category(i) : ""; }); + text.text(function (i) { return shouldShowTickText(ticks, i) ? formattedCategory(i) : ""; }); pathUpdate.attr("d", "M" + range[0] + "," + tickEndSize + "V0H" + range[1] + "V" + tickEndSize); } @@ -2161,7 +2167,7 @@ lineUpdate.attr("x2", -tickMajorSize).attr("y2", 0); textUpdate.attr("x", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("y", tickOffset); text.attr("dy", ".32em").style("text-anchor", "end"); - text.text(function (i) { return shouldShowTickText(ticks, i) ? category(i) : ""; }); + text.text(function (i) { return shouldShowTickText(ticks, i) ? formattedCategory(i) : ""; }); pathUpdate.attr("d", "M" + -tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + -tickEndSize); break; } @@ -2217,6 +2223,11 @@ tickTextNum = x; return axis; }; + axis.tickFormat = function (format) { + if (!arguments.length) { return tickFormat; } + tickFormat = format; + return axis; + }; axis.tickOffset = function () { return tickOffset; }; diff --git a/c3.min.js b/c3.min.js index c88992a..2e90c13 100644 --- a/c3.min.js +++ b/c3.min.js @@ -1,68 +1,68 @@ -(function(M){function wc(){function n(e,c){e.attr("transform",function(e){return"translate("+(c(e)+Z)+", 0)"})}function t(e,c){e.attr("transform",function(e){return"translate(0,"+c(e)+")"})}function M(e){var c=e[0];e=e[e.length-1];return ca?0:a}function ob(a){var b=Ia[a.id];a.name=n(b)?b:a.id;return a}function Wb(a){var b= -a[0],d={},g=[],p,e;for(p=1;pw[d].indexOf(e.id)))for(g=0;g=h;h+=v)s+=e(a[c-1],a[c],h,u)}return s}function xb(a){var b,d;cb=e.select(va);if(cb.empty())M.alert('No bind element found. Check the selector specified by "bindto" and existance of that element. Default "bindto" is "#chart".'); -else{cb.html("");h.data.x=void 0;h.data.targets=Ja(a);Q();O();l.domain(e.extent(a.map(function(a){return a.x})));z.domain(ka("y"));S.domain(ka("y2"));ba.domain(l.domain());Va.domain(z.domain());Wa.domain(S.domain());I.ticks(10>a.length?a.length:10);na.ticks(Yc).outerTickSize(0).tickFormat(Zc);Xa.ticks($c).outerTickSize(0).tickFormat(ad);T=l.domain();E.x(ba);pa&&F.x(l);Ma=e.select(va).append("svg").attr("width",r+C+B).attr("height",q+v+aa).on("mouseenter",bd).on("mouseleave",cd);db=Ma.append("defs"); -db.append("clipPath").attr("id",yb).append("rect").attr("y",v).attr("width",r).attr("height",q-v);db.append("clipPath").attr("id","xaxis-clip").append("rect").attr("x",-1-C).attr("y",-20).attr("width",Y).attr("height",hb);db.append("clipPath").attr("id","yaxis-clip").append("rect").attr("x",-C+1).attr("y",v-1).attr("width",xc).attr("height",N);k=Ma.append("g").attr("transform",J.main);x=xa?Ma.append("g").attr("transform",J.context):null;ya=za?Ma.append("g").attr("transform",J.legend):null;W=e.select(va).style("position", -"relative").append("div").style("position","absolute").style("z-index","10").style("display","none");k.append("g").attr("class","x axis").attr("clip-path",f?"":"url(#xaxis-clip)").attr("transform",J.x).call(f?na:I).append("text").attr("class","-axis-x-label").attr("x",r).attr("dy","-.5em").style("text-anchor","end").text(dd);k.append("g").attr("class","y axis").attr("clip-path",f?"url(#yaxis-clip)":"").call(f?I:na).append("text").attr("transform","rotate(-90)").attr("dy","1.2em").attr("dx","-.5em").style("text-anchor", -"end").text(ed);Lb&&k.append("g").attr("class","y2 axis").attr("transform",J.y2).call(Xa);b=k.append("g").attr("clip-path",Na).attr("class","grid");mc&&b.append("g").attr("class","xgrids");zb&&(d=b.append("g").attr("class","xgrid-lines").selectAll(".xgrid-line").data(zb).enter().append("g").attr("class","xgrid-line"),d.append("line").attr("class",function(a){return""+a["class"]}),d.append("text").attr("class",function(a){return""+a["class"]}).attr("text-anchor","end").attr("transform",f?"":"rotate(-90)").attr("dx", -f?0:-v).attr("dy",-6).text(function(a){return a.text}));fd&&b.append("g").attr("class","xgrid-focus").append("line").attr("class","xgrid-focus").attr("x1",f?0:-10).attr("x2",f?r:-10).attr("y1",f?-10:v).attr("y2",f?-10:q);nc&&b.append("g").attr("class","ygrids");Ab&&b.append("g").attr("class","ygrid-lines").selectAll("ygrid-line").data(Ab).enter().append("line").attr("class",function(a){return"ygrid-line "+a["class"]});k.append("g").attr("clip-path",Na).attr("class","regions");k.append("g").attr("clip-path", -Na).attr("class","chart");k.select(".chart").append("g").attr("class","event-rects").style("fill-opacity",0).style("cursor",pa?"ew-resize":null).selectAll(".event-rects").data(a).enter().append("rect").attr("class",function(a,b){return"event-rect event-rect-"+b}).style("cursor",ga&&qa?"pointer":null).on("mouseover",function(a,b){if(!eb){var d=h.data.targets.map(function(a){return ob(a.values[b])}),e,c;if(0",d,c,e;for(d=0;d"+e+""+ -c+"";return b+""}),id=c(["tooltip","init","show"],!1),Aa=c(["tooltip","init","x"],0),sc=c(["tooltip","init","position"],{top:"0px",left:"50px"}),yb=va.replace("#","")+"-clip",Na="url(#"+yb+")",A="timeseries"===vc,ca="categorized"===vc,pb=!A&&U,Db=null,eb=!1,Cb=!1,la=za?40:0,oa=e.time.format(pd).parse,V=function(a,b){var d=[],c=null!==b?b:"#1f77b4 #ff7f0e #2ca02c #d62728 #9467bd #8c564b #e377c2 #7f7f7f #bcbd22 #17becf".split(" ");return function(b){if(b in a)return a[b];-1===d.indexOf(b)&& -d.push(b);return c[d.indexOf(b)%c.length]}}(qd,rd),Ec=function(){var a=[[e.time.format("%Y/%-m/%-d"),function(){return!0}],[e.time.format("%-m/%-d"),function(a){return a.getMonth()}],[e.time.format("%-m/%-d"),function(a){return 1!==a.getDate()}],[e.time.format("%-m/%-d"),function(a){return a.getDay()&&1!==a.getDate()}],[e.time.format("%I %p"),function(a){return a.getHours()}],[e.time.format("%I:%M"),function(a){return a.getMinutes()}],[e.time.format(":%S"),function(a){return a.getSeconds()}],[e.time.format(".%L"), -function(a){return a.getMilliseconds()}]];return function(b){for(var d=a.length-1,c=a[d];!c[1](b);)c=a[--d];return c[0](b)}}(),Hb,Ob,Jb,Ua,Nb,Pb,r,q,ma,ib,ua,Rb,Sb,lb,mb,l,z,S,ba,Va,Wa,I,na,Xa,Da,zc=f?"left":"bottom",Ac=f?kb?"top":"bottom":kb?"right":"left",Bc=f?jb?"bottom":"top":jb?"left":"right",Cc="bottom",J={main:function(){return"translate("+C+","+v+")"},context:function(){return"translate("+G+","+Ba+")"},legend:function(){return"translate("+Gb+","+Fb+")"},y2:function(){return"translate("+(f? -0:r)+","+(f?10:0)+")"},x:function(){return"translate(0,"+q+")"},subx:function(){return"translate(0,"+ma+")"}},ld=function(){var a=e.svg.line().x(f?function(a){return y(a.id)(a.value)}:bb).y(f?bb:function(a){return y(a.id)(a.value)});return function(b){var d=lc(b.values),c;if(vb(b))return"spline"===fa["string"===typeof b?b:b.id]?a.interpolate("cardinal"):a.interpolate("linear"),0b.classes.indexOf(a)})});return K};h.data.get=function(a){a=h.data.getAsTarget(a);return n(a)?a.values.map(function(a){return a.value}):void 0};h.data.getAsTarget=function(a){var b= -Za(function(b){return b.id===a});return 0a?0:a}function nb(a){var b=Ia[a.id];a.name=n(b)?b:a.id;return a}function Wb(a){var b=a[0],d={},k=[],e,c;for(e=1;ew[d].indexOf(e.id)))for(k=0;k=g;g+=u)s+=e(a[c-1],a[c],g,v)}return s} +function wb(a){var b,d;db=e.select(ua);if(db.empty())O.alert('No bind element found. Check the selector specified by "bindto" and existance of that element. Default "bindto" is "#chart".');else{db.html("");g.data.x=void 0;g.data.targets=Ja(a);sa();Eb();l.domain(e.extent(a.map(function(a){return a.x})));y.domain(ka("y"));T.domain(ka("y2"));ba.domain(l.domain());Wa.domain(y.domain());Xa.domain(T.domain());J.ticks(10>a.length?a.length:10);na.ticks(Yc).outerTickSize(0).tickFormat(Zc);Ya.ticks($c).outerTickSize(0).tickFormat(ad); +U=l.domain();F.x(ba);pa&&G.x(l);Ma=e.select(ua).append("svg").attr("width",r+A+aa).attr("height",u+p+E).on("mouseenter",bd).on("mouseleave",cd);eb=Ma.append("defs");eb.append("clipPath").attr("id",xb).append("rect").attr("y",p).attr("width",r).attr("height",u-p);eb.append("clipPath").attr("id","xaxis-clip").append("rect").attr("x",-1-A).attr("y",-20).attr("width",Q).attr("height",P);eb.append("clipPath").attr("id","yaxis-clip").append("rect").attr("x",-A+1).attr("y",p-1).attr("width",ja).attr("height", +hb);h=Ma.append("g").attr("transform",K.main);x=wa?Ma.append("g").attr("transform",K.context):null;xa=ya?Ma.append("g").attr("transform",K.legend):null;X=e.select(ua).style("position","relative").append("div").style("position","absolute").style("z-index","10").style("display","none");h.append("g").attr("class","x axis").attr("clip-path",f?"":"url(#xaxis-clip)").attr("transform",K.x).call(f?na:J).append("text").attr("class","-axis-x-label").attr("x",r).attr("dy","-.5em").style("text-anchor","end").text(dd); +h.append("g").attr("class","y axis").attr("clip-path",f?"url(#yaxis-clip)":"").call(f?J:na).append("text").attr("transform","rotate(-90)").attr("dy","1.2em").attr("dx","-.5em").style("text-anchor","end").text(ed);Mb&&h.append("g").attr("class","y2 axis").attr("transform",K.y2).call(Ya);b=h.append("g").attr("clip-path",Na).attr("class","grid");mc&&b.append("g").attr("class","xgrids");yb&&(d=b.append("g").attr("class","xgrid-lines").selectAll(".xgrid-line").data(yb).enter().append("g").attr("class", +"xgrid-line"),d.append("line").attr("class",function(a){return""+a["class"]}),d.append("text").attr("class",function(a){return""+a["class"]}).attr("text-anchor","end").attr("transform",f?"":"rotate(-90)").attr("dx",f?0:-p).attr("dy",-6).text(function(a){return a.text}));fd&&b.append("g").attr("class","xgrid-focus").append("line").attr("class","xgrid-focus").attr("x1",f?0:-10).attr("x2",f?r:-10).attr("y1",f?-10:p).attr("y2",f?-10:u);nc&&b.append("g").attr("class","ygrids");zb&&b.append("g").attr("class", +"ygrid-lines").selectAll("ygrid-line").data(zb).enter().append("line").attr("class",function(a){return"ygrid-line "+a["class"]});h.append("g").attr("clip-path",Na).attr("class","regions");h.append("g").attr("clip-path",Na).attr("class","chart");h.select(".chart").append("g").attr("class","event-rects").style("fill-opacity",0).style("cursor",pa?"ew-resize":null).selectAll(".event-rects").data(a).enter().append("rect").attr("class",function(a,b){return"event-rect event-rect-"+b}).style("cursor",ga&& +qa?"pointer":null).on("mouseover",function(a,b){if(!fb){var d=g.data.targets.map(function(a){return nb(a.values[b])}),e,c;if(0",d,c,e;for(d=0;d"+ +e+""+c+"";return b+""}),id=c(["tooltip","init","show"],!1),za=c(["tooltip","init","x"],0),sc=c(["tooltip","init","position"],{top:"0px",left:"50px"}),xb=ua.replace("#","")+"-clip",Na="url(#"+xb+")",z="timeseries"===vc,ca="categorized"===vc,ob=!z&&V,Cb=null,fb=!1,Bb=!1,la=ya?40:0,oa=e.time.format(pd).parse,W=function(a,b){var d=[],c=null!==b?b:"#1f77b4 #ff7f0e #2ca02c #d62728 #9467bd #8c564b #e377c2 #7f7f7f #bcbd22 #17becf".split(" ");return function(b){if(b in +a)return a[b];-1===d.indexOf(b)&&d.push(b);return c[d.indexOf(b)%c.length]}}(qd,rd),Ec=function(){var a=[[e.time.format("%Y/%-m/%-d"),function(){return!0}],[e.time.format("%-m/%-d"),function(a){return a.getMonth()}],[e.time.format("%-m/%-d"),function(a){return 1!==a.getDate()}],[e.time.format("%-m/%-d"),function(a){return a.getDay()&&1!==a.getDate()}],[e.time.format("%I %p"),function(a){return a.getHours()}],[e.time.format("%I:%M"),function(a){return a.getMinutes()}],[e.time.format(":%S"),function(a){return a.getSeconds()}], +[e.time.format(".%L"),function(a){return a.getMilliseconds()}]];return function(b){for(var d=a.length-1,c=a[d];!c[1](b);)c=a[--d];return c[0](b)}}(),Ib,Pb,Kb,Va,Ob,Qb,r,u,ma,ib,ta,Sb,Tb,lb,mb,l,y,T,ba,Wa,Xa,J,na,Ya,Da,zc=f?"left":"bottom",Ac=f?kb?"top":"bottom":kb?"right":"left",Bc=f?jb?"bottom":"top":jb?"left":"right",Cc="bottom",K={main:function(){return"translate("+A+","+p+")"},context:function(){return"translate("+m+","+Aa+")"},legend:function(){return"translate("+Gb+","+Fb+")"},y2:function(){return"translate("+ +(f?0:r)+","+(f?10:0)+")"},x:function(){return"translate(0,"+u+")"},subx:function(){return"translate(0,"+ma+")"}},ld=function(){var a=e.svg.line().x(f?function(a){return D(a.id)(a.value)}:cb).y(f?cb:function(a){return D(a.id)(a.value)});return function(b){var d=lc(b.values),c;if(ub(b))return"spline"===fa["string"===typeof b?b:b.id]?a.interpolate("cardinal"):a.interpolate("linear"),0b.classes.indexOf(a)})});return L};g.data.get=function(a){a=g.data.getAsTarget(a);return n(a)?a.values.map(function(a){return a.value}):void 0};g.data.getAsTarget=function(a){var b= +$a(function(b){return b.id===a});return 0