From 6fb6b0f1fb910d87669d8c2392440d48bca04c0c Mon Sep 17 00:00:00 2001 From: Masayuki Tanaka Date: Mon, 28 Oct 2013 14:12:35 +0900 Subject: [PATCH] Add unzoom api --- c3.js | 17 ++++++++++---- c3.min.js | 68 +++++++++++++++++++++++++++---------------------------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/c3.js b/c3.js index 00ef300..3aa3240 100644 --- a/c3.js +++ b/c3.js @@ -795,6 +795,12 @@ var brush = d3.svg.brush().on("brush", redrawForBrush); var zoom = d3.behavior.zoom().on("zoom", __zoom_enabled ? redrawForZoom : null); + // define functions for c3 + brush.update = function () { + if (context) context.select('.x.brush').call(this); + return this; + } + /*-- Draw Chart --*/ // for svg elements @@ -1376,8 +1382,7 @@ if (__subchart_show) { // reflect main chart to extent on subchart if zoomed if (d3.event !== null && d3.event.type === 'zoom') { - brush.extent(x.orgDomain()); - context.select('.x.brush').call(brush); + brush.extent(x.orgDomain()).update(); } // update subchart elements if needed if (withSubchart) { @@ -1386,8 +1391,7 @@ context.select('.x.axis').transition().duration(__axis_rotated ? duration : 0).call(__axis_rotated ? yAxis : subXAxis); // extent rect if ( ! brush.empty()) { - brush.extent(x.orgDomain()); - context.select('.x.brush').call(brush); + brush.extent(x.orgDomain()).update(); } // bars barW = getBarW(subXAxis, barTargetsNum, true); @@ -1720,6 +1724,11 @@ .style('opacity', 0); }; + c3.unzoom = function () { + brush.clear().update(); + redraw({withUpdateXDomain:true}); + } + c3.load = function (args) { // check args if (isUndefined(args.done)) { diff --git a/c3.min.js b/c3.min.js index 8afe030..21e316b 100644 --- a/c3.min.js +++ b/c3.min.js @@ -1,24 +1,24 @@ -(function(xb){function jc(){function l(b,h){b.attr("transform",function(b){return"translate("+(h(b)+R)+",0)"})}function s(b,h){b.attr("transform",function(b){return"translate(0,"+h(b)+")"})}function Oa(b){var h=b[0];b=b[b.length-1];return ha?0:a}function ba(a){var c=Da[a.id];a.name=l(c)?c:a.id;return a}function va(a){var c=a[0],d={},m=[],f,b;for(f=1;fa?0:a}function ba(a){var c=Da[a.id];a.name=l(c)?c:a.id;return a}function va(a){var c=a[0],d={},m=[],f,b;for(f=1;fw[d].indexOf(f.id)))for(b=0;b=g;g+=diffx2)p+=b(a[f-1],a[f],g,u);return p}function pb(a){var c=k.data.targets=ca(a),d,b;N();R();r.domain(d3.extent(a.map(function(a){return a.x}))); -y.domain(B(c,"y"));O.domain(B(c,"y2"));U.domain(r.domain());Qa.domain(y.domain());Ra.domain(O.domain());M.ticks(10>a.length?a.length:10);fa.ticks(Ic).outerTickSize(0).tickFormat(Jc);Sa.ticks(Kc).outerTickSize(0).tickFormat(Lc);ya=r.domain();E.x(U);oa&&Ga.x(r);Ha=d3.select(xa).append("svg").attr("width",n+H+C).attr("height",t+v+T);Za=Ha.append("defs");Za.append("clipPath").attr("id",qb).append("rect").attr("y",v).attr("width",n).attr("height",t-v);Za.append("clipPath").attr("id","xaxis-clip").append("rect").attr("x", +y.domain(C(c,"y"));O.domain(C(c,"y2"));U.domain(r.domain());Qa.domain(y.domain());Ra.domain(O.domain());M.ticks(10>a.length?a.length:10);fa.ticks(Ic).outerTickSize(0).tickFormat(Jc);Sa.ticks(Kc).outerTickSize(0).tickFormat(Lc);ya=r.domain();F.x(U);oa&&Ga.x(r);Ha=d3.select(xa).append("svg").attr("width",n+H+D).attr("height",t+v+T);Za=Ha.append("defs");Za.append("clipPath").attr("id",qb).append("rect").attr("y",v).attr("width",n).attr("height",t-v);Za.append("clipPath").attr("id","xaxis-clip").append("rect").attr("x", -1).attr("y",-1).attr("width",n+2).attr("height",40);Za.append("clipPath").attr("id","yaxis-clip").append("rect").attr("x",-H+1).attr("y",v-1).attr("width",H).attr("height",t-v+2);g=Ha.append("g").attr("transform",I.main);x=pa?Ha.append("g").attr("transform",I.context):null;qa=ra?Ha.append("g").attr("transform",I.legend):null;Y=d3.select(xa).style("position","relative").append("div").style("position","absolute").style("width","30%").style("z-index","10").style("visibility","hidden");g.append("g").attr("class", "x axis").attr("clip-path",e?"":"url(#xaxis-clip)").attr("transform",I.x).call(e?fa:M);g.append("g").attr("class","y axis").attr("clip-path",e?"url(#yaxis-clip)":"").call(e?M:fa).append("text").attr("transform","rotate(-90)").attr("dy","1.4em").attr("dx","-.8em").style("text-anchor","end").text(Mc);Gb&&g.append("g").attr("class","y2 axis").attr("transform",I.y2).call(Sa);if(oa)g.append("rect").attr("class","zoom-rect").attr("width",n).attr("height",t).style("opacity",0).call(Ga).on("dblclick.zoom", null);d=g.append("g").attr("clip-path",Ia).attr("class","grid");ac&&d.append("g").attr("class","xgrids");rb&&(b=d.append("g").attr("class","xgrid-lines").selectAll(".xgrid-line").data(rb).enter().append("g").attr("class","xgrid-line"),b.append("line").attr("class",function(a){return""+a["class"]}),b.append("text").attr("class",function(a){return""+a["class"]}).attr("text-anchor","end").attr("transform",e?"":"rotate(-90)").attr("dx",e?0:-v).attr("dy",-6).text(function(a){return a.text}));Nc&&d.append("g").attr("class", @@ -28,37 +28,37 @@ null);d=g.append("g").attr("clip-path",Ia).attr("class","grid");ac&&d.append("g" c).classed($,!1)}).on("mousemove",function(a,c){Z&&!$a&&(ha||g.selectAll(".-shape-"+c).filter(function(a){return ia(a)}).each(function(a){a=d3.select(this).classed($,!0);"circle"===this.nodeName&&a.attr("r",dc);d3.select(".event-rect-"+c).style("cursor",null)}).filter(function(a){a=d3.select(this);if("circle"===this.nodeName)return Vb(this,Fa);if("rect"===this.nodeName)return Wb(this,a.attr("x"),a.attr("y"))}).each(function(a){a=d3.select(this);a.classed($)||(a.classed($,!0),"circle"===this.nodeName&& a.attr("r",Fa));d3.select(".event-rect-"+c).style("cursor","pointer")}))}).on("click",function(a,c){ub?ub=!1:g.selectAll(".-shape-"+c).each(function(a){var d=d3.select(this),b=d.classed(aa),f=!1,e;"circle"===this.nodeName?(f=Vb(this,1.5*Fa),e=Yb):"rect"===this.nodeName&&(f=Wb(this),e=$b);if(ha||f)Z&&ia(a)&&(d.classed(aa,!b),e(!b,d,a,c)),Oc(a,d)})}).call(d3.behavior.drag().origin(Object).on("drag",function(a){if(Z&&!oa){a=vb[0];var c=vb[1],d=d3.mouse(this),b=d[0],d=d[1],e=Math.min(a,b),m=Math.max(a, b),k=ha?v:Math.min(c,d),l=ha?t:Math.max(c,d);g.select(".dragarea").attr("x",e).attr("y",k).attr("width",m-e).attr("height",l-k);g.selectAll(".-shapes").selectAll(".-shape").filter(function(a){return ia(a)}).each(function(a,c){var d=d3.select(this),b=d.classed(aa),f=d.classed(wb),g,p,h,n;g=!1;"circle"===this.nodeName?(g=1*d.attr("cx"),p=1*d.attr("cy"),n=Yb,g=e",b,e,f;for(b=0;b"+f+""+e+"";return c+""}),Pc=b(["tooltip","init","show"],!1),sa=b(["tooltip","init","x"],0),gc=b(["tooltip","init","position"],{top:"0px",left:"50px"}),qb=xa.replace("#","")+"-clip",Ia="url(#"+qb+")",D="timeseries"===ic,na="categorized"=== +0,Nc=b(["point","focus","line","enabled"],!0),cc=b(["point","focus","expand","enabled"],!0),dc=b(["point","focus","expand","r"],cc?4:tb),Fa=b(["point","focus","select","r"],8),Oc=b(["point","onclick"],function(){}),Ec=b(["point","onselected"],function(){}),Fc=b(["point","onunselected"],function(){}),J=b(["regions"],[]),ec=b(["tooltip","contents"],function(a){var c="",b,e,f;for(b=0;b";return c+"
"+(E?a[0].x.getFullYear()+"."+(a[0].x.getMonth()+1)+"."+a[0].x.getDate():na?a[0].x
"+f+""+e+"
"}),Pc=b(["tooltip","init","show"],!1),sa=b(["tooltip","init","x"],0),gc=b(["tooltip","init","position"],{top:"0px",left:"50px"}),qb=xa.replace("#","")+"-clip",Ia="url(#"+qb+")",E="timeseries"===ic,na="categorized"=== ic,vb=null,$a=!1,ub=!1,da=ra?40:0,ga=d3.time.format(Vc).parse,P=function(a,c){var b=[],e=null!==c?c:"#1f77b4 #ff7f0e #2ca02c #d62728 #9467bd #8c564b #e377c2 #7f7f7f #bcbd22 #17becf".split(" ");return function(c){if(c in a)return a[c];0<=b.indexOf(c)||b.push(c);return e[b.indexOf(c)%e.length]}}(Wc,Xc),oc=function(){var a=[[d3.time.format("%Y/%-m/%-d"),function(){return!0}],[d3.time.format("%-m/%-d"),function(a){return a.getMonth()}],[d3.time.format("%-m/%-d"),function(a){return 1!=a.getDate()}],[d3.time.format("%-m/%-d"), function(a){return a.getDay()&&1!=a.getDate()}],[d3.time.format("%I %p"),function(a){return a.getHours()}],[d3.time.format("%I:%M"),function(a){return a.getMinutes()}],[d3.time.format(":%S"),function(a){return a.getSeconds()}],[d3.time.format(".%L"),function(a){return a.getMilliseconds()}]];return function(c){for(var b=a.length-1,e=a[b];!e[1](c);)e=a[--b];return e[0](c)}}(),Db,Ib,Fb,Pa,Hb,Jb,n,t,ea,cb,ma,Kb,Lb,fb,gb,r,y,O,U,Qa,Ra,M,fa,Sa,za,kc=e?"left":"bottom",lc=e?eb?"top":"bottom":eb?"right":"left", mc=e?db?"bottom":"top":db?"left":"right",nc="bottom",I={main:function(){return"translate("+H+","+v+")"},context:function(){return"translate("+G+","+ta+")"},legend:function(){return"translate("+Oa+","+yb+")"},y2:function(){return"translate("+(e?0:n)+","+(e?10:0)+")"},x:function(){return"translate(0,"+t+")"},subx:function(){return"translate(0,"+ea+")"}},Rc=function(){var a=d3.svg.line().x(e?function(a){return h(a.id)(a.value)}:Ya).y(e?Ya:function(a){return h(a.id)(a.value)});return function(c){var b; -if(nb(c))return"spline"===X["string"===typeof c?c:c.id]?a.interpolate("cardinal"):a.interpolate("linear"),0b.classes.indexOf(a)})});return J};k.data.get=function(a){a=k.data.getAsTarget(a);return l(a)?a.values.map(function(a){return a.value}):void 0};k.data.getAsTarget=function(a){var b=Ta(function(b){return b.id==a});return 0b.classes.indexOf(a)})});return J};k.data.get=function(a){a=k.data.getAsTarget(a);return l(a)?a.values.map(function(a){return a.value}):void 0};k.data.getAsTarget=function(a){var b=Ta(function(b){return b.id==a});return 0