UNPKG

zingchart

Version:

Our JavaScript charting library is a commercial product. But the full branded version is free to try, forever.

8 lines (7 loc) 7.69 kB
/* All of the code within the ZingChart software is developed and copyrighted by ZingChart, Inc., and may not be copied, replicated, or used in any other software or application without prior permission from ZingChart. All usage must coincide with the ZingChart End User License Agreement which can be requested by email at support@zingchart.com. Build 2.9.16-hf1 */ zingchart.setModule("color-scale"),zingchart.plugins.colorscale={},zingchart.plugins.colorscale.info={},zingchart.plugins.colorscale.defaults={box:{},item:{placement:"default","font-size":10,color:"#33"},label:{"font-size":10,color:"#33"},guide:{"line-color":"#fff","line-width":1},cursor:{type:"diamond",size:4,"background-color":"#000"}},zingchart.bind(null,"complete",function(a){zingchart.plugins.colorscale.paint(a)}),zingchart.plugins.colorscale.paint=function(a){var e=zingchart.getLoader(a.id),i=zingchart.getModules(e);if(-1!==ZC.AV(i,"color-scale"))for(var l=0,r=e.AG.length;l<r;l++){var t=e.AG[l];if(t.o["color-scale"]){var c=zingchart.plugins.colorscale.defaults,o=t.o["color-scale"]||{};o.guide=o.guide||{};var n,h=o.aspect||"gradient",s=o.intervals||4,g=o.layout||"v",p=o.color||"#369",u=o["max-percent"]||50,d=o.effect||"lighten",f=o.progression||"lin",_=ZC._f_(o["min-value"]||"0"),z=ZC._f_(o["max-value"]||"10"),m=o.steps||[],v=o.colors||[],x=o.labels||[],C=ZC._b_(o.mirrored);ZC._b_(o.map)&&(n=zingchart.maps.getMapByGraphIndex(a.id,t.L)._INFO_.choropleth)&&(h=n.aspect,s=n.intervals,p=n.color,u=n.maxPercent,d=n.effect,f=n.progression,_=n.minValue,z=n.maxValue,m=n.steps,v=n.colors,x=n.labels,C=n.mirrored),"v"===g?(o.margin=o.margin||"auto 10 auto auto",o.width=o.width||15,o.height=o.height||200,o["fill-angle"]=270):(o.margin=o.margin||"auto auto 35 auto",o.width=o.width||200,o.height=o.height||15,o["fill-angle"]=0);var Z=zingchart.getInstance(t,"box");Z.append(c.box),Z.append(o),Z.parse();var y,b,F,I,M,w="v"===g?Z.F:Z.I,A=[],k=null;if("gradient"===h){var X=Math.max(2,Math.ceil(w/("v"===g?30:60)));if("log"===f){for((I=Math.floor(ZC.FV(_,10)))===(M=Math.ceil(ZC.FV(z,10)))&&M++,y=I;y<=M;y+=1)A.push(Math.pow(10,y));if(A.length>X&&A.length%2==1)for(y=A.length-1;y>=0;y--)y%2==1&&A.splice(y,1)}else{F=ZC.AQ.WI(_,z);for(var Y=0;(F[1]-z)/F[2]>.5&&Y<10;)F=ZC.AQ.WI(_,z,Math.round(F[2]/2)),Y++;for(k=F[3]<0?-F[3]:null,0===F[3]&&F[2]<1&&(k=1),y=F[0];y<=F[1];y+=F[2])A.push(y);if(A.length>1.5*X){if(A.length%2==1)for(y=A.length-1;y>=0;y--)y%2==1&&A.splice(y,1);if(A.length>1.5*X)for(var V=!1;!V;){for(V=!0,A=[],y=(F=ZC.AQ.WI(_,z,2*F[2]))[0];y<=F[1];y+=F[2])A.push(y);A.length>1.5*X&&(V=!1)}}}}else{var L;if(m.length)s=m.length-1;else if("lin"===f){var P=(z-_)/s;for(b=0;b<s;b+=1)m.push(_+b*P);m.push(z)}else{for((I=Math.floor(ZC.FV(_,10)))===(M=Math.ceil(ZC.FV(z,10)))&&M++,s=Math.max(1,M-I-1),b=I;b<=M;b+=1)m.push(Math.pow(10,b));L=u/s}if(!v.length)for(b=0;b<=s;b+=1)v.push("lighten"===d?ZC.AO.PP(p,b*L):ZC.AO.JQ(p,b*L));A=[].concat(m)}var G=a.id+"_"+t.L;zingchart.plugins.colorscale.info[G]={aspect:h,min:A[0],max:A[A.length-1],progression:f,layout:g,x:Z.iX,y:Z.iY,width:Z.I,height:Z.F,steps:m};var K=0,j=0,O=zingchart.getInstance(t,"textbox");for(y=0;y<A.length;y++)O.append(c.item),O.append(o.item),O.o.text=A[y],O.parse(),K=Math.max(K,O.I),j=Math.max(j,O.F);var Q,B,W=[],J=[];if(zingchart.exec(a.id,"removeobject",{graphid:t.K,type:"shape",cls:"zc-colorscale"}),zingchart.exec(a.id,"removeobject",{graphid:t.K,type:"label",cls:"zc-colorscale"}),"gradient"===h){Q={flat:!0,cls:"zc-colorscale"};var N=p,q="lighten"===d?ZC.AO.PP(p,u):ZC.AO.JQ(p,u);Q["background-color"]=N+" "+q,C&&(Q["background-color"]=q+" "+N),ZC._cp_(Z.o,Q),Q.id="colorscale_box",W.push(Q)}else for(b=0;b<s;b+=1)Q={flat:!0,cls:"zc-colorscale"},ZC._cp_(Z.o,Q),Q["background-color"]=v[b],Q.id="colorscale_box_"+b,"h"===g?(Q.x=Z.iX+b*Z.I/s,Q.y=Z.iY,Q.width=Z.I/s,Q.height=Z.F):(Q.x=Z.iX,Q.y=Z.iY+Z.F-(b+1)*Z.F/s,Q.width=Z.I,Q.height=Z.F/s),ZC._cp_(o.marker,Q),0===b&&(zingchart.plugins.colorscale.info[G].marker=[Q.width,Q.height]),Q.margin=null,W.push(Q),x[b]&&(B={flat:!0,cls:"zc-colorscale"},ZC._cp_(c.label,B),ZC._cp_(o.label,B),B.text=x[b],"h"===g?(B.width=Z.I/s,B.x=Z.iX+b*Z.I/s,B.y=Z.iY):(B.height=Z.F/s,B.x=Z.iX+Z.I+5,B.y=Z.iY+Z.F-(b+1)*Z.F/s),B.id="colorscale_label_"+b,W.push(B));var D,E,H,R,S=w/(A.length-1),T=[];for(y=0;y<A.length;y++){if(E={flat:!0,cls:"zc-colorscale"},ZC._cp_(c.item,E),ZC._cp_(o.item,E),H={decimals:k,short:!0,"thousands-separator":",","decimals-separator":"."},"log"===f){var U=Math.floor(ZC.FV(A[y],10));H.decimals=U<0?-U:0}ZC._cp_(o.item,H),R=zingchart.formatNumber(A[y],H);var $=ZC._i_(o.guide.size||("v"===g?Z.I:Z.F));"v"===g?(D=Z.iY+Z.F-S*y,T.push([Z.iX,D],[Z.iX+$,D],null),ZC._cp_({anchor:"l",width:K,align:"right",x:Z.iX-K-5,y:D,text:R},E)):(D=Z.iX+S*y,T.push([D,Z.iY],[D,Z.iY+$],null),ZC._cp_({anchor:"t",height:j,align:"center","vertical-align":"top",x:D,y:Z.iY+Z.F+5,text:R},E)),E.x-=t.iX,E.y-=t.iY,E.id="colorscale_item_"+y,W.push(E)}var aa={type:"line",flat:!0,cls:"zc-colorscale"};ZC._cp_(c.guide,aa),ZC._cp_(o.guide,aa),aa.id="colorscale_guide",aa.points=T,J.push(aa);var ea={flat:!0,visible:!1,cls:"zc-colorscale"};ZC._cp_(c.cursor,ea),ZC._cp_(o.cursor,ea),ea.id="colorscale_cursor";var ia=zingchart.plugins.colorscale.info[G].marker?zingchart.plugins.colorscale.info[G].marker[0]:Z.I,la=zingchart.plugins.colorscale.info[G].marker?zingchart.plugins.colorscale.info[G].marker[1]:Z.F;ea.x=Z.iX+ia/2-t.iX,ea.y=Z.iY+la/2-t.iY,J.push(ea),zingchart.exec(a.id,"addobject",{graphid:t.K,type:"label",data:W,dynamic:!0,update:!1}),zingchart.exec(a.id,"addobject",{graphid:t.K,type:"shape",dynamic:!0,data:J})}}},zingchart.plugins.colorscale.clear=function(a,e){a=a||"zc";var i=zingchart.getLoader(a);if(i){var l=zingchart.getGraph(i,e.graphid||"0");if(l){if(!zingchart.plugins.colorscale.info[a+"_"+l.L])return;zingchart.exec(a,"updateobject",{graphid:l.K,type:"shape",id:"colorscale_cursor",dynamic:!0,data:{visible:!1}})}}},zingchart.plugins.colorscale.AGM=function(a,e){if(null!==ZC._n_(e.value)){a=a||"zc";var i=zingchart.getLoader(a);if(i){var l=zingchart.getGraph(i,e.graphid||"0");if(l){if(!zingchart.plugins.colorscale.info[a+"_"+l.L])return;var r,t,c,o=ZC._f_(e.value),n=zingchart.plugins.colorscale.info[a+"_"+l.L];if("gradient"===n.aspect)"lin"===n.progression?(r=(o-n.min)/(n.max-n.min),t="h"===n.layout?n.x+r*n.width:n.y+n.height-r*n.height):(r=(ZC.FV(o,10)-ZC.FV(n.min,10))/(ZC.FV(n.max,10)-ZC.FV(n.min,10)),t="h"===n.layout?n.x+r*n.width:n.y+n.height-r*n.height);else{var h=0;if(o===n.steps[n.steps.length-1])h=n.steps.length-2;else for(var s=0;s<n.steps.length-1;s++)o>=n.steps[s]&&o<n.steps[s+1]&&(h=s);r=("h"===n.layout?n.width:n.height)/(n.steps.length-1),t=n.marker?"h"===n.layout?n.x+r/2+r*h:n.y+n.height-r*h-r+n.marker[1]/2:"h"===n.layout?n.x+r/2+r*h:n.y+n.height-r*h+n.marker[0]/2}(c="h"===n.layout?{x:t-l.iX}:{y:t-l.iY}).visible=!0,("h"===n.layout&&(t<n.x||t>n.x+n.width)||"v"===n.layout&&(t<n.y||t>n.y+n.height))&&(c.visible=!1),zingchart.exec(a,"updateobject",{graphid:l.K,type:"shape",id:"colorscale_cursor",dynamic:!0,data:c})}}}},zingchart.extendAPI("colorscale.setvalue",function(a,e){zingchart.plugins.colorscale.AGM(a,e)}),zingchart.extendAPI("colorscale.update",function(a,e){if(e.data){var i=zingchart.getLoader(a);if(i){var l=i.CB(e.graphid);l&&l.o["color-scale"]&&(ZC._todash_(e.data),ZC._cp_(e.data,l.o["color-scale"]),zingchart.plugins.colorscale.paint({id:a,graphid:l.K}))}}}),zingchart.extendAPI("colorscale.clear",function(a,e){zingchart.plugins.colorscale.clear(a,e)}),zingchart.extendAPI("colorscale.getinfo",function(a,e){var i=zingchart.getLoader(a);if(i){var l=i.CB(e.graphid);if(l)return zingchart.plugins.colorscale.info[a+"_"+l.L]}return null});