From 00d158e1d0a39e311f8f99f5b3778caf15b898ac Mon Sep 17 00:00:00 2001 From: Jeffrey Heer Date: Sat, 6 Apr 2013 16:30:45 -0700 Subject: [PATCH] Add explicit import of d3. --- index.js | 8 ++++++-- package.json | 2 +- src/_package.js | 2 +- src/core/_end.js | 2 +- src/core/_start.js | 2 +- vega.js | 6 +++--- vega.min.js | 4 ++-- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 97e283b59d..3b80b0c338 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,20 @@ -var globals = ["vg"], +// cache pre-existing global values +var globals = ["vg", "d3"], globalValues = {}; globals.forEach(function(g) { if (g in global) globalValues[g] = global[g]; }); -// Namespace pollution! +// ensure availability of d3 in global namespace +// NOTE: will "pollute" namespace with jsdom window, etc d3 = require("d3"); +// load and export vega require("./vega"); module.exports = vg; +// restore pre-existing global values globals.forEach(function(g) { if (g in globalValues) global[g] = globalValues[g]; else delete global[g]; diff --git a/package.json b/package.json index 54f789de34..b16424a412 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vega", - "version": "1.1.0", + "version": "1.1.1", "description": "Vega Runtime", "keywords": [ "vega", diff --git a/src/_package.js b/src/_package.js index 7b0fe03e9b..e28843e765 100644 --- a/src/_package.js +++ b/src/_package.js @@ -1,7 +1,7 @@ var vg = {}; // semantic versioning -vg.version = '1.1.0'; +vg.version = '1.1.1'; // type checking functions var toString = Object.prototype.toString; diff --git a/src/core/_end.js b/src/core/_end.js index 8005ad0545..d65675266e 100644 --- a/src/core/_end.js +++ b/src/core/_end.js @@ -1,2 +1,2 @@ return vg; -})(); +})(d3); // assumes availability of D3 in global namespace diff --git a/src/core/_start.js b/src/core/_start.js index 9c6f3979fa..a406deda41 100644 --- a/src/core/_start.js +++ b/src/core/_start.js @@ -1 +1 @@ -vg = (function(){ +vg = (function(d3){ // take d3 instance as sole import diff --git a/vega.js b/vega.js index 46f55372e4..245c9b88fa 100644 --- a/vega.js +++ b/vega.js @@ -1,8 +1,8 @@ -vg = (function(){ +vg = (function(d3){ // take d3 instance as sole import var vg = {}; // semantic versioning -vg.version = '1.1.0'; +vg.version = '1.1.1'; // type checking functions var toString = Object.prototype.toString; @@ -4141,4 +4141,4 @@ vg.spec = function(s) { return new vg.Spec(s); }; return vg; -})(); +})(d3); // assumes availability of D3 in global namespace diff --git a/vega.min.js b/vega.min.js index e409f92393..0a5555ce95 100644 --- a/vega.min.js +++ b/vega.min.js @@ -1,2 +1,2 @@ -vg=function(){var e={};e.version="1.1.0";var t=Object.prototype.toString;return e.isObject=function(e){return e===Object(e)},e.isFunction=function(e){return t.call(e)=="[object Function]"},e.isString=function(e){return t.call(e)=="[object String]"},e.isArray=Array.isArray||function(e){return t.call(e)=="[object Array]"},e.isNumber=function(e){return t.call(e)=="[object Number]"},e.isBoolean=function(e){return t.call(e)=="[object Boolean]"},e.number=function(e){return+e},e.boolean=function(e){return!!e},e.identity=function(e){return e},e.extend=function(e){for(var t,n,r=1,i=arguments.length;r1?function(e){return n.reduce(function(e,t){return e[t]},e)}:function(e){return e[t]}},e.comparator=function(t){var n=[];return t===undefined&&(t=[]),t=e.array(t).map(function(t){var r=1;return t[0]==="-"?(r=-1,t=t.slice(1)):t[0]==="+"&&(r=1,t=t.slice(1)),n.push(r),e.accessor(t)}),function(e,r){var i,s,o,u,a;for(i=0,s=t.length;ia)return n[i]}return 0}},e.numcmp=function(e,t){return e-t},e.array=function(t){return t!=null?e.isArray(t)?t:[t]:[]},e.values=function(t){return e.isObject(t)&&!e.isArray(t)&&t.values?t.values:t},e.str=function(t){return e.isArray(t)?"["+t.map(e.str)+"]":e.isString(t)?"'"+t+"'":t},e.keys=function(e){var t=[];for(var n in e)t.push(n);return t},e.unique=function(t,n){n=n||e.identity;var r=[],i;for(var s=0;sthis.x2&&(this.x2=e),t>this.y2&&(this.y2=t),this},t.expand=function(e){return this.x1-=e,this.y1-=e,this.x2+=e,this.y2+=e,this},t.round=function(){return this.x1=Math.floor(this.x1),this.y1=Math.floor(this.y1),this.x2=Math.ceil(this.x2),this.y2=Math.ceil(this.y2),this},t.translate=function(e,t){return this.x1+=e,this.x2+=e,this.y1+=t,this.y2+=t,this},t.rotate=function(e,t,n){var r=Math.cos(e),i=Math.sin(e),s=t-t*r+n*i,o=n-t*i-n*r,u=this.x1,a=this.x2,f=this.y1,l=this.y2;return this.clear().add(r*u-i*f+s,i*u+r*f+o).add(r*u-i*l+s,i*u+r*l+o).add(r*a-i*f+s,i*a+r*f+o).add(r*a-i*l+s,i*a+r*l+o)},t.union=function(e){return e.x1this.x2&&(this.x2=e.x2),e.y2>this.y2&&(this.y2=e.y2),this},t.intersects=function(e){return e&&!(this.x2e.x2||this.y2e.y2)},t.contains=function(e,t){return!(ethis.x2||tthis.y2)},t.width=function(){return this.x2-this.x1},t.height=function(){return this.y2-this.y1},e}(),e.canvas={},e.canvas.path=function(){function r(e){var r=[],i,s,o;e=e.slice().replace(n[0],"###$1").split(n[1]).slice(1);for(var u=0,a,f,l=e.length;up)for(var d=1,v=f.length;d1&&(g=Math.sqrt(g),n*=g,r*=g);var y=d/n,b=p/n,w=-p/r,E=d/r,S=y*l+b*c,x=w*l+E*c,T=y*e+b*t,N=w*e+E*t,C=(T-S)*(T-S)+(N-x)*(N-x),k=1/C-.25;k<0&&(k=0);var L=Math.sqrt(k);o==i&&(L=-L);var A=.5*(S+T)-L*(N-x),O=.5*(x+N)+L*(T-S),M=Math.atan2(x-O,S-A),_=Math.atan2(N-O,T-A),D=_-M;D<0&&o==1?D+=2*Math.PI:D>0&&o==0&&(D-=2*Math.PI);var P=Math.ceil(Math.abs(D/(Math.PI*.5+.001))),H=[];for(var B=0;B0&&(t.globalAlpha=o*(n.strokeOpacity==null?1:n.strokeOpacity),t.strokeStyle=s,t.lineWidth=a,t.lineCap=(u=n.strokeCap)!=undefined?u:"butt",t.stroke(),n.bounds.expand(a)))}function h(e,t,n,r){var i,s,o;for(i=0,s=n.items.length;i0&&(t.globalAlpha=f*(s.strokeOpacity==null?1:s.strokeOpacity),t.strokeStyle=a,t.lineWidth=c,t.lineCap=(l=s.strokeCap)!=undefined?l:"butt",t.strokeRect(h,p,s.width,s.height),s.bounds.expand(c))}}function d(t,n,r){if(!n.items.length)return;var i=this,s=n.items,o;for(var u=0,a=s.length;u0&&(t.globalAlpha=f*(s.strokeOpacity==null?1:s.strokeOpacity),t.strokeStyle=a,t.lineWidth=l,t.strokeText(s.text,x,y));s.angle&&t.restore()}}function g(e,t,n,r){var i=t.x+(t.dx||0),s=t.y+(t.dy||0),o=e.measureText(t.text).width,u=t.fontSize,a=t.align,f=t.baseline,l,c,h,p,d;return a==="center"?i-=o/2:a==="right"&&(i-=o),f==="top"?s+=u/5:f==="bottom"?s-=u:f==="middle"?s=s-u/2+u/10:s-=4*u/5,n.set(i,s,i+o,s+u),!r&&t.angle&&n.rotate(t.angle*Math.PI/180,t.x,t.y),n}function b(e){return function(t,n,r){h(e,t,n,r)}}function w(e){return function(t,n,r){if(!n.items.length)return;if(r&&!r.intersects(n.items[0].bounds))return;c(e,t,n.items[0],n.items)}}function E(e,t,n){if(!t.items.length)return;var r=t.items,i,s=this,o,u;p(e,t,n);for(var a=0,f=r.length;a=0;){u=n.items[f],a=u.bounds;if(a&&!a.contains(s,o)||!a)continue;if(e(t,u,r,i,s,o))return u}return!1}function N(e,t,n,r,i,s){if(!t.items.length)return!1;var o=t.items,u,a,f,l,c,h,p,d;return a=o[0].bounds,a&&!a.contains(i,s)?!1:L.area(e,o,n,r)?o[0]:!1}function C(e,t,n,r,i,s){return!1}function k(e){return function(t,n,r,i,s,o){return T(e,t,n,r,i,s,o)}}function A(e,t,n,r,i,o){if(!t.fontSize)return!1;if(!t.angle)return!0;e.font=v(t);var u=g(e,t,s,!0),a=-t.angle*Math.PI/180,f=Math.cos(a),l=Math.sin(a),n=t.x,r=t.y,c=f*i-l*o+(n-n*f+r*l),h=l*i+f*o+(r-n*l-r*f);return u.contains(c,h)}var t=e.canvas.path.parse,n=e.canvas.path.render,r=Math.sqrt(3),i=Math.tan(30*Math.PI/180),s=new e.Bounds,L={text:A,rect:function(e,t,n,r){return!0},image:function(e,t,n,r){return!0},arc:function(e,t,n,r){return o(e,t),e.isPointInPath(n,r)},area:function(e,t,n,r){return f(e,t),e.isPointInPath(n,r)},path:function(e,t,n,r){return u(e,t),e.isPointInPath(n,r)},symbol:function(e,t,n,r){return a(e,t),e.isPointInPath(n,r)}};return{draw:{group:E,area:w(f),line:w(l),arc:b(o),path:b(u),symbol:b(a),rect:p,text:m,image:d,drawOne:w,drawAll:b},pick:{group:S,area:N,line:C,arc:k(L.arc),path:k(L.path),symbol:k(L.symbol),rect:k(L.rect),text:k(L.text),image:k(L.image),pickAll:T}}}(),e.canvas.Renderer=function(){function r(t){var n=new e.Bounds(t.bounds);while((t=t.mark.group)!=null)n.translate(t.x||0,t.y||0);return n}function i(t){return t?e.array(t).reduce(function(e,t){return e.union(r(t))},new e.Bounds):null}function s(t,n){var r=null;return n&&(r=(new e.Bounds(n)).round(),t.beginPath(),t.rect(r.x1,r.y1,r.width(),r.height()),t.clip()),r}var t=function(){this._ctx=null,this._el=null},n=t.prototype;return n.initialize=function(e,t,n,r){this._el=e,this._width=t,this._height=n,this._padding=r;if(!e)return this;var i=d3.select(e).selectAll("canvas.marks").data([1]);return i.enter().append("canvas").attr("class","marks"),i.attr("width",t+r.left+r.right).attr("height",n+r.top+r.bottom),this._ctx=i.node().getContext("2d"),this._ctx.setTransform(1,0,0,1,r.left,r.top),this},n.context=function(e){return e?(this._ctx=e,this):this._ctx},n.element=function(){return this._el},n.render=function(e,t){var n=this._ctx,r=this._padding,o=this._width+r.left+r.right,u=this._width+r.top+r.bottom,a=null;this._scene=e,n.save(),a=s(n,i(t)),n.clearRect(-r.left,-r.top,o,u),this.draw(n,e,a),t&&(n.restore(),n.save(),a=s(n,i(t)),n.clearRect(-r.left,-r.top,o,u),this.draw(n,e,a)),n.restore(),this._scene=null},n.draw=function(t,n,r){var i=n.marktype,s=e.canvas.marks.draw[i];s.call(this,t,n,r)},n.renderAsync=function(e){var t=this;t._async_id&&clearTimeout(t._async_id),t._async_id=setTimeout(function(){t.render(e),delete t._async_id},50)},n.loadImage=function(t){var n=this,r=this._scene,i=new Image;return i.onload=function(){e.log("LOAD IMAGE: "+this.src),n.renderAsync(r)},i.src=t,i},t}(),e.canvas.Handler=function(){function i(e){var t=e.indexOf(".");return t<0?e:e.slice(0,t)}var t=function(e,t){this._active=null,this._handlers={},e&&this.initialize(e),t&&this.model(t)},n=t.prototype;n.initialize=function(e,t,i){this._el=d3.select(e).node(),this._canvas=d3.select(e).select("canvas.marks").node(),this._padding=t,this._obj=i||null;var s=this._canvas,o=this;return r.forEach(function(e){s.addEventListener(e,function(t){n[e].call(o,t)})}),this},n.model=function(e){return arguments.length?(this._model=e,this):this._model},n.handlers=function(){var t=this._handlers;return e.keys(t).reduce(function(e,n){return t[n].reduce(function(e,t){return e.push(t),e},e)},[])};var r=["mousedown","mouseup","click","dblclick","wheel","keydown","keypress","keyup","mousewheel"];return r.forEach(function(e){n[e]=function(t){this.fire(e,t)}}),r.push("mousemove"),r.push("mouseout"),n.mousemove=function(e){var t=this._padding,n=e.target.getBoundingClientRect(),r=e.clientX-n.left,i=e.clientY-n.top,s=this._active,o=this.pick(this._model.scene(),r,i,r-t.left,i-t.top);if(o===s){this.fire("mousemove",e);return}s&&this.fire("mouseout",e),this._active=o,o&&this.fire("mouseover",e)},n.mouseout=function(e){this._active&&this.fire("mouseout",e),this._active=null},n.DOMMouseScroll=function(e){this.fire("mousewheel",e)},n.fire=function(e,t){var n=this._active,r=this._handlers[e];if(n&&r)for(var i=0,s=r.length;i=0;){if(r[s].type!==e)continue;(!t||r[s].handler===t)&&r.splice(s,1)}return this},n.context=function(){return this._canvas.getContext("2d")},n.pick=function(t,n,r,i,s){var o=this.context(),u=t.marktype,a=e.canvas.marks.pick[u];return a.call(this,o,t,n,r,i,s)},t}(),e.svg={},e.svg.marks=function(){function t(e){return e.x||0}function n(e){return e.y||0}function r(e){return e.y+e.height||0}function i(e){return e.key}function s(e){return e.size==null?100:e.size}function o(e){return e.shape||"circle"}function d(e){var t=e.mark?e:e[0],n,r,i,s,o;for(n=0,r=p.length;n "+i).data(a),h=c.enter().append(i);i!=="g"&&(f.style("pointer-events",t.interactive===!1?"none":null),h.each(function(e){(e.mark?e:e[0])._svg=this})),c.exit().remove(),c.each(s),i!=="g"&&c.each(d)}function L(e,t,n){var r=this;k(e,t,n,"mark_","rect",b),k(e,t,n,"group_","g",T);var i=e.select(".group_"+n).node(),s,o,u,a;for(var s=0,o=i.childNodes.length;s=0;){if(i[o].type!==e)continue;if(!t||i[o].handler===t)s.removeEventListener(n,i[o].svg),i.splice(o,1)}return this},t}(),e.data={},e.data.ingest=function(e,t){return{data:e,index:t}},e.data.mapper=function(e){return function(t){return t.forEach(e),t}},e.data.size=function(e,t){return e=Array.isArray(e)?e:[0,e],e=e.map(function(e){return typeof e=="string"?t[e]:e}),e},e.data.read=function(){function r(e,n){var r=n&&n.type||"json";return e=t[r](e,n),n&&n.parse&&i(e,n.parse),e}function i(t,r){var i=e.keys(r),s=i.map(function(e){return n[r[e]]}),o,u,a,f,l;for(u=0,f=t.length;u0?"|":"")+String(p);o=i[a],o===undefined&&s.push(o=i[a]={key:a,keys:u,index:s.length,values:[]}),o.values.push(e[l])}if(n)for(l=0,f=s.length;lo&&(o=h),u+=h,p=h-a,a+=p/(l+1),f+=p*(h-a);f/=c-1;var v=e.isArray(i)?{}:i;return n&&(d.sort(e.numcmp),l=d.length>>1,v[r.median]=d.length%2?d[l]:(d[l-1]+d[l])/2),v[r.count]=c,v[r.min]=s,v[r.max]=o,v[r.sum]=u,v[r.mean]=a,v[r.variance]=f,v[r.stdev]=Math.sqrt(f),v}function s(e){return(Array.isArray(e)?[e]:e.values||[]).map(i)}var t=e.accessor("data"),n=!1,r={count:"count",min:"min",max:"max",sum:"sum",mean:"mean",variance:"variance",stdev:"stdev",median:"median"};return s.median=function(e){return n=e||!1,s},s.value=function(n){return t=e.accessor(n),s},s.output=function(t){return e.keys(r).forEach(function(e){t[e]!==undefined&&(r[e]=t[e])}),s},s},e.data.treemap=function(){function o(i,o,u){i=t.size(e.data.size(r,u)).value(n).nodes(i);var a=e.keys(s),f=a.length;return i.forEach(function(e){var t,n;for(var r=0;r0?"\n ":" ",r+="o."+u+" = "+n(a)+";",f[u]=!0;return f.x2&&(r+="\n if (o.x > o.x2) { var t = o.x; o.x = o.x2; o.x2 = t; };",r+="\n o.width = (o.x2 - o.x);"),f.y2&&(r+="\n if (o.y > o.y2) { var t = o.y; o.y = o.y2; o.y2 = t; };",r+="\n o.height = (o.y2 - o.y);"),r+="if (trans) trans.interpolate(item, o);",Function("item","group","trans",r)}function n(t){if(t==null)return null;var n=t.value!==undefined?e.str(t.value):"item.datum.data";t.field!==undefined&&(n="item.datum["+e.field(t.field).map(e.str).join("][")+"]");if(t.scale!==undefined){var r="group.scales['"+t.scale+"']";t.band?n=r+".rangeBand()":n=r+"("+n+")"}return"("+(t.mult?t.mult+" * ":"")+n+")"+(t.offset?" + "+t.offset:"")}return t}(),e.parse.scales=function(){function f(e,t,n,r){return(e||[]).reduce(function(e,t){return e[t.name]=l(t,e[t.name],n,r),e},t||{})}function l(r,i,s,o){var u=r.type||t,a=d(r,o),f=c(u,i),l=u===n?h:p,v=e.values(o.datum);return l(r,f,a,s,v),f}function c(t,n){if(!n||t!==n.type){var r=a[t]||d3.scale[t];r||e.error("Unrecognized scale type: "+t),(n=r()).type=t}return n}function h(t,n,r,i,s){var o,u,a,f;o=t.domain,Array.isArray(o)?n.domain(o):e.isObject(o)&&(u=i[o.data]||s,a=e.accessor(o.field),n.domain(e.unique(u,a))),f=typeof r[0]=="string",f||r.length>2?n.range(r):t.points?n.rangePoints(r,t.padding||0):t.round||t.round===undefined?n.rangeRoundBands(r,t.padding||0):n.rangeBands(r,t.padding||0)}function p(t,n,o,u,a){var f,l,c;f=[null,null],t.domain!==undefined&&(e.isArray(t.domain)?f=t.domain.slice():e.isObject(t.domain)?(l=u[t.domain.data]||a,e.array(t.domain.field).forEach(function(t,n){t=e.accessor(t),f[0]=d3.min([f[0],d3.min(l,t)]),f[1]=d3.max([f[1],d3.max(l,t)])})):f=t.domain),t.domainMin!==undefined&&(e.isObject(t.domainMin)?(f[0]=null,l=u[t.domainMin.data]||a,e.array(t.domainMin.field).forEach(function(t,n){t=e.accessor(t),f[0]=d3.min([f[0],d3.min(l,t)])})):f[0]=t.domainMin),t.domainMax!==undefined&&(e.isObject(t.domainMax)?(f[1]=null,l=u[t.domainMax.data]||a,e.array(t.domainMax.field).forEach(function(t,n){t=e.accessor(t),f[1]=d3.max([f[1],d3.max(l,t)])})):f[1]=t.domainMax),t.type!==r&&t.type!==s&&(t.zero||t.zero===undefined)&&(f[0]=Math.min(0,f[0]),f[1]=Math.max(0,f[1])),n.domain(f),t.range=="height"&&(o=o.reverse()),n[t.round?"rangeRound":"range"](o),t.exponent&&t.type===i&&n.exponent(t.exponent),t.clamp&&n.clamp(!0),t.nice&&(t.type===s?(c=d3.time[t.nice],c||e.error("Unrecognized interval: "+c),n.nice(c)):n.nice())}function d(t,n){var r=[null,null];if(t.range!==undefined)if(typeof t.range=="string")if(o[t.range])r=[0,n[t.range]];else{if(!u[t.range])return e.error("Unrecogized range: "+t.range),r;r=e[t.range]}else Array.isArray(t.range)?r=t.range:r=[0,t.range];t.rangeMin!==undefined&&(r[0]=t.rangeMin),t.rangeMax!==undefined&&(r[1]=t.rangeMax);if(t.reverse!==undefined){var i=t.reverse;e.isObject(i)&&(i=e.accessor(i.field)(n.datum)),i&&(r=r.reverse())}return r}var t="linear",n="ordinal",r="log",i="pow",s="time",o={width:1,height:1},u={category10:1,category20:1,shapes:1},a={time:d3.time.scale,utc:d3.time.scale.utc};return f}(),e.parse.spec=function(t,n,r){function i(t){var i=t.width||500,s=t.height||500,o=t.viewport||null,u={width:i,height:s,viewport:o,padding:e.parse.padding(t.padding),marks:e.parse.marks(t,i,s),data:e.parse.data(t.data,function(){n(a)})},a=r(u)}r=r||e.ViewFactory,e.isObject(t)?i(t):d3.json(t,function(t,n){t?e.error(t):i(n)})},e.parse.transform=function(t){var n=e.data[t.type]();return e.keys(t).forEach(function(e){if(e==="type")return;n[e](t[e])}),n},e.scene={},e.scene.GROUP="group",e.scene.ENTER=0,e.scene.UPDATE=1,e.scene.EXIT=2,e.scene.DEFAULT_DATA={sentinel:1},e.scene.data=function(t,n){var r=e.scene.DEFAULT_DATA;return t=e.values(t||n||[r]),t===r&&(t=[r]),t},e.scene.Item=function(){function e(e){this.mark=e}var t=e.prototype;return t.cousin=function(e,t){if(e===0)return this;e=e||-1;var n=this.mark,r=n.group,i=t==null?n.items.indexOf(this):t,s=r.items.indexOf(n)+e;return r.items[s].items[i]},t.sibling=function(e){if(e===0)return this;e=e||-1;var t=this.mark,n=t.items.indexOf(this)+e;return t.items[n]},t.remove=function(){var e=this,t=e.mark.items,n=t.indexOf(e);return n>=0&&(n===t.length-1?t.pop():t.splice(n,1)),e},e}(),e.scene.item=function(t){return new e.scene.Item(t)},e.scene.build=function(){function o(n,r,i,s){var o=e.scene.data(n.from?n.from(r,i,s):null,s);return i=u(n,i),i.items=a(n,o,i),l(n,i),n.type===t&&f(n,r,i),i}function u(e,t){return t=t||{},t.def=e,t.marktype=e.type,t.interactive=e.interactive!==!1,t}function a(t,s,o){var u=c(t.key),a=o.items||[],f=[],l={},h,p,d,v,m,g;for(h=0,d=a.length;h1&&(u=1),a=r.ease(u);for(f=0,l=r.length;f1?function(e){return n.reduce(function(e,t){return e[t]},e)}:function(t){return t[e]}},t.comparator=function(e){var n=[];return e===undefined&&(e=[]),e=t.array(e).map(function(e){var r=1;return e[0]==="-"?(r=-1,e=e.slice(1)):e[0]==="+"&&(r=1,e=e.slice(1)),n.push(r),t.accessor(e)}),function(t,r){var i,s,o,u,a;for(i=0,s=e.length;ia)return n[i]}return 0}},t.numcmp=function(e,t){return e-t},t.array=function(e){return e!=null?t.isArray(e)?e:[e]:[]},t.values=function(e){return t.isObject(e)&&!t.isArray(e)&&e.values?e.values:e},t.str=function(e){return t.isArray(e)?"["+e.map(t.str)+"]":t.isString(e)?"'"+e+"'":e},t.keys=function(e){var t=[];for(var n in e)t.push(n);return t},t.unique=function(e,n){n=n||t.identity;var r=[],i;for(var s=0;sthis.x2&&(this.x2=e),t>this.y2&&(this.y2=t),this},t.expand=function(e){return this.x1-=e,this.y1-=e,this.x2+=e,this.y2+=e,this},t.round=function(){return this.x1=Math.floor(this.x1),this.y1=Math.floor(this.y1),this.x2=Math.ceil(this.x2),this.y2=Math.ceil(this.y2),this},t.translate=function(e,t){return this.x1+=e,this.x2+=e,this.y1+=t,this.y2+=t,this},t.rotate=function(e,t,n){var r=Math.cos(e),i=Math.sin(e),s=t-t*r+n*i,o=n-t*i-n*r,u=this.x1,a=this.x2,f=this.y1,l=this.y2;return this.clear().add(r*u-i*f+s,i*u+r*f+o).add(r*u-i*l+s,i*u+r*l+o).add(r*a-i*f+s,i*a+r*f+o).add(r*a-i*l+s,i*a+r*l+o)},t.union=function(e){return e.x1this.x2&&(this.x2=e.x2),e.y2>this.y2&&(this.y2=e.y2),this},t.intersects=function(e){return e&&!(this.x2e.x2||this.y2e.y2)},t.contains=function(e,t){return!(ethis.x2||tthis.y2)},t.width=function(){return this.x2-this.x1},t.height=function(){return this.y2-this.y1},e}(),t.canvas={},t.canvas.path=function(){function r(t){var r=[],i,s,o;t=t.slice().replace(n[0],"###$1").split(n[1]).slice(1);for(var u=0,a,f,l=t.length;up)for(var d=1,v=f.length;d1&&(g=Math.sqrt(g),n*=g,r*=g);var y=d/n,b=p/n,w=-p/r,E=d/r,S=y*l+b*c,x=w*l+E*c,T=y*e+b*t,N=w*e+E*t,C=(T-S)*(T-S)+(N-x)*(N-x),k=1/C-.25;k<0&&(k=0);var L=Math.sqrt(k);o==i&&(L=-L);var A=.5*(S+T)-L*(N-x),O=.5*(x+N)+L*(T-S),M=Math.atan2(x-O,S-A),_=Math.atan2(N-O,T-A),D=_-M;D<0&&o==1?D+=2*Math.PI:D>0&&o==0&&(D-=2*Math.PI);var P=Math.ceil(Math.abs(D/(Math.PI*.5+.001))),H=[];for(var B=0;B0&&(t.globalAlpha=o*(n.strokeOpacity==null?1:n.strokeOpacity),t.strokeStyle=s,t.lineWidth=a,t.lineCap=(u=n.strokeCap)!=undefined?u:"butt",t.stroke(),n.bounds.expand(a)))}function p(e,t,n,r){var i,s,o;for(i=0,s=n.items.length;i0&&(e.globalAlpha=f*(s.strokeOpacity==null?1:s.strokeOpacity),e.strokeStyle=a,e.lineWidth=c,e.lineCap=(l=s.strokeCap)!=undefined?l:"butt",e.strokeRect(h,p,s.width,s.height),s.bounds.expand(c))}}function v(e,n,r){if(!n.items.length)return;var i=this,s=n.items,o;for(var u=0,a=s.length;u0&&(e.globalAlpha=f*(s.strokeOpacity==null?1:s.strokeOpacity),e.strokeStyle=a,e.lineWidth=l,e.strokeText(s.text,x,y));s.angle&&e.restore()}}function b(e,t,n,r){var i=t.x+(t.dx||0),s=t.y+(t.dy||0),o=e.measureText(t.text).width,u=t.fontSize,a=t.align,f=t.baseline,l,c,h,p,d;return a==="center"?i-=o/2:a==="right"&&(i-=o),f==="top"?s+=u/5:f==="bottom"?s-=u:f==="middle"?s=s-u/2+u/10:s-=4*u/5,n.set(i,s,i+o,s+u),!r&&t.angle&&n.rotate(t.angle*Math.PI/180,t.x,t.y),n}function w(e){return function(t,n,r){p(e,t,n,r)}}function E(e){return function(t,n,r){if(!n.items.length)return;if(r&&!r.intersects(n.items[0].bounds))return;h(e,t,n.items[0],n.items)}}function S(e,t,n){if(!t.items.length)return;var r=t.items,i,s=this,o,u;d(e,t,n);for(var a=0,f=r.length;a=0;){u=n.items[f],a=u.bounds;if(a&&!a.contains(s,o)||!a)continue;if(e(t,u,r,i,s,o))return u}return!1}function C(e,t,n,r,i,s){if(!t.items.length)return!1;var o=t.items,u,a,f,l,c,h,p,d;return a=o[0].bounds,a&&!a.contains(i,s)?!1:A.area(e,o,n,r)?o[0]:!1}function k(e,t,n,r,i,s){return!1}function L(e){return function(t,n,r,i,s,o){return N(e,t,n,r,i,s,o)}}function O(e,t,n,r,i,s){if(!t.fontSize)return!1;if(!t.angle)return!0;e.font=m(t);var u=b(e,t,o,!0),a=-t.angle*Math.PI/180,f=Math.cos(a),l=Math.sin(a),n=t.x,r=t.y,c=f*i-l*s+(n-n*f+r*l),h=l*i+f*s+(r-n*l-r*f);return u.contains(c,h)}var n=t.canvas.path.parse,r=t.canvas.path.render,i=Math.sqrt(3),s=Math.tan(30*Math.PI/180),o=new t.Bounds,A={text:O,rect:function(e,t,n,r){return!0},image:function(e,t,n,r){return!0},arc:function(e,t,n,r){return u(e,t),e.isPointInPath(n,r)},area:function(e,t,n,r){return l(e,t),e.isPointInPath(n,r)},path:function(e,t,n,r){return a(e,t),e.isPointInPath(n,r)},symbol:function(e,t,n,r){return f(e,t),e.isPointInPath(n,r)}};return{draw:{group:S,area:E(l),line:E(c),arc:w(u),path:w(a),symbol:w(f),rect:d,text:g,image:v,drawOne:E,drawAll:w},pick:{group:T,area:C,line:k,arc:L(A.arc),path:L(A.path),symbol:L(A.symbol),rect:L(A.rect),text:L(A.text),image:L(A.image),pickAll:N}}}(),t.canvas.Renderer=function(){function i(e){var n=new t.Bounds(e.bounds);while((e=e.mark.group)!=null)n.translate(e.x||0,e.y||0);return n}function s(e){return e?t.array(e).reduce(function(e,t){return e.union(i(t))},new t.Bounds):null}function o(e,n){var r=null;return n&&(r=(new t.Bounds(n)).round(),e.beginPath(),e.rect(r.x1,r.y1,r.width(),r.height()),e.clip()),r}var n=function(){this._ctx=null,this._el=null},r=n.prototype;return r.initialize=function(t,n,r,i){this._el=t,this._width=n,this._height=r,this._padding=i;if(!t)return this;var s=e.select(t).selectAll("canvas.marks").data([1]);return s.enter().append("canvas").attr("class","marks"),s.attr("width",n+i.left+i.right).attr("height",r+i.top+i.bottom),this._ctx=s.node().getContext("2d"),this._ctx.setTransform(1,0,0,1,i.left,i.top),this},r.context=function(e){return e?(this._ctx=e,this):this._ctx},r.element=function(){return this._el},r.render=function(e,t){var n=this._ctx,r=this._padding,i=this._width+r.left+r.right,u=this._width+r.top+r.bottom,a=null;this._scene=e,n.save(),a=o(n,s(t)),n.clearRect(-r.left,-r.top,i,u),this.draw(n,e,a),t&&(n.restore(),n.save(),a=o(n,s(t)),n.clearRect(-r.left,-r.top,i,u),this.draw(n,e,a)),n.restore(),this._scene=null},r.draw=function(e,n,r){var i=n.marktype,s=t.canvas.marks.draw[i];s.call(this,e,n,r)},r.renderAsync=function(e){var t=this;t._async_id&&clearTimeout(t._async_id),t._async_id=setTimeout(function(){t.render(e),delete t._async_id},50)},r.loadImage=function(e){var n=this,r=this._scene,i=new Image;return i.onload=function(){t.log("LOAD IMAGE: "+this.src),n.renderAsync(r)},i.src=e,i},n}(),t.canvas.Handler=function(){function s(e){var t=e.indexOf(".");return t<0?e:e.slice(0,t)}var n=function(e,t){this._active=null,this._handlers={},e&&this.initialize(e),t&&this.model(t)},r=n.prototype;r.initialize=function(t,n,s){this._el=e.select(t).node(),this._canvas=e.select(t).select("canvas.marks").node(),this._padding=n,this._obj=s||null;var o=this._canvas,u=this;return i.forEach(function(e){o.addEventListener(e,function(t){r[e].call(u,t)})}),this},r.model=function(e){return arguments.length?(this._model=e,this):this._model},r.handlers=function(){var e=this._handlers;return t.keys(e).reduce(function(t,n){return e[n].reduce(function(e,t){return e.push(t),e},t)},[])};var i=["mousedown","mouseup","click","dblclick","wheel","keydown","keypress","keyup","mousewheel"];return i.forEach(function(e){r[e]=function(t){this.fire(e,t)}}),i.push("mousemove"),i.push("mouseout"),r.mousemove=function(e){var t=this._padding,n=e.target.getBoundingClientRect(),r=e.clientX-n.left,i=e.clientY-n.top,s=this._active,o=this.pick(this._model.scene(),r,i,r-t.left,i-t.top);if(o===s){this.fire("mousemove",e);return}s&&this.fire("mouseout",e),this._active=o,o&&this.fire("mouseover",e)},r.mouseout=function(e){this._active&&this.fire("mouseout",e),this._active=null},r.DOMMouseScroll=function(e){this.fire("mousewheel",e)},r.fire=function(e,t){var n=this._active,r=this._handlers[e];if(n&&r)for(var i=0,s=r.length;i=0;){if(r[i].type!==e)continue;(!t||r[i].handler===t)&&r.splice(i,1)}return this},r.context=function(){return this._canvas.getContext("2d")},r.pick=function(e,n,r,i,s){var o=this.context(),u=e.marktype,a=t.canvas.marks.pick[u];return a.call(this,o,e,n,r,i,s)},n}(),t.svg={},t.svg.marks=function(){function n(e){return e.x||0}function r(e){return e.y||0}function i(e){return e.y+e.height||0}function s(e){return e.key}function o(e){return e.size==null?100:e.size}function u(e){return e.shape||"circle"}function v(e){var t=e.mark?e:e[0],n,r,i,s,o;for(n=0,r=d.length;n "+i).data(a),h=c.enter().append(i);i!=="g"&&(f.style("pointer-events",t.interactive===!1?"none":null),h.each(function(e){(e.mark?e:e[0])._svg=this})),c.exit().remove(),c.each(s),i!=="g"&&c.each(v)}function A(t,n,r){var i=this;L(t,n,r,"mark_","rect",w),L(t,n,r,"group_","g",N);var s=t.select(".group_"+r).node(),o,u,a,f;for(var o=0,u=s.childNodes.length;o=0;){if(s[u].type!==t)continue;if(!n||s[u].handler===n)o.removeEventListener(r,s[u].svg),s.splice(u,1)}return this},n}(),t.data={},t.data.ingest=function(e,t){return{data:e,index:t}},t.data.mapper=function(e){return function(t){return t.forEach(e),t}},t.data.size=function(e,t){return e=Array.isArray(e)?e:[0,e],e=e.map(function(e){return typeof e=="string"?t[e]:e}),e},t.data.read=function(){function i(e,t){var r=t&&t.type||"json";return e=n[r](e,t),t&&t.parse&&s(e,t.parse),e}function s(e,n){var i=t.keys(n),s=i.map(function(e){return r[n[e]]}),o,u,a,f,l;for(u=0,f=e.length;u0?"|":"")+String(p);o=i[a],o===undefined&&s.push(o=i[a]={key:a,keys:u,index:s.length,values:[]}),o.values.push(t[l])}if(n)for(l=0,f=s.length;lo&&(o=h),u+=h,p=h-a,a+=p/(l+1),f+=p*(h-a);f/=c-1;var v=t.isArray(i)?{}:i;return n&&(d.sort(t.numcmp),l=d.length>>1,v[r.median]=d.length%2?d[l]:(d[l-1]+d[l])/2),v[r.count]=c,v[r.min]=s,v[r.max]=o,v[r.sum]=u,v[r.mean]=a,v[r.variance]=f,v[r.stdev]=Math.sqrt(f),v}function s(e){return(Array.isArray(e)?[e]:e.values||[]).map(i)}var e=t.accessor("data"),n=!1,r={count:"count",min:"min",max:"max",sum:"sum",mean:"mean",variance:"variance",stdev:"stdev",median:"median"};return s.median=function(e){return n=e||!1,s},s.value=function(n){return e=t.accessor(n),s},s.output=function(e){return t.keys(r).forEach(function(t){e[t]!==undefined&&(r[t]=e[t])}),s},s},t.data.treemap=function(){function u(e,s,u){e=n.size(t.data.size(i,u)).value(r).nodes(e);var a=t.keys(o),f=a.length;return e.forEach(function(e){var t,n;for(var r=0;r0?"\n ":" ",r+="o."+u+" = "+n(a)+";",f[u]=!0;return f.x2&&(r+="\n if (o.x > o.x2) { var t = o.x; o.x = o.x2; o.x2 = t; };",r+="\n o.width = (o.x2 - o.x);"),f.y2&&(r+="\n if (o.y > o.y2) { var t = o.y; o.y = o.y2; o.y2 = t; };",r+="\n o.height = (o.y2 - o.y);"),r+="if (trans) trans.interpolate(item, o);",Function("item","group","trans",r)}function n(e){if(e==null)return null;var n=e.value!==undefined?t.str(e.value):"item.datum.data";e.field!==undefined&&(n="item.datum["+t.field(e.field).map(t.str).join("][")+"]");if(e.scale!==undefined){var r="group.scales['"+e.scale+"']";e.band?n=r+".rangeBand()":n=r+"("+n+")"}return"("+(e.mult?e.mult+" * ":"")+n+")"+(e.offset?" + "+e.offset:"")}return e}(),t.parse.scales=function(){function l(e,t,n,r){return(e||[]).reduce(function(e,t){return e[t.name]=c(t,e[t.name],n,r),e},t||{})}function c(e,i,s,o){var u=e.type||n,a=v(e,o),f=h(u,i),l=u===r?p:d,c=t.values(o.datum);return l(e,f,a,s,c),f}function h(n,r){if(!r||n!==r.type){var i=f[n]||e.scale[n];i||t.error("Unrecognized scale type: "+n),(r=i()).type=n}return r}function p(e,n,r,i,s){var o,u,a,f;o=e.domain,Array.isArray(o)?n.domain(o):t.isObject(o)&&(u=i[o.data]||s,a=t.accessor(o.field),n.domain(t.unique(u,a))),f=typeof r[0]=="string",f||r.length>2?n.range(r):e.points?n.rangePoints(r,e.padding||0):e.round||e.round===undefined?n.rangeRoundBands(r,e.padding||0):n.rangeBands(r,e.padding||0)}function d(n,r,u,a,f){var l,c,h;l=[null,null],n.domain!==undefined&&(t.isArray(n.domain)?l=n.domain.slice():t.isObject(n.domain)?(c=a[n.domain.data]||f,t.array(n.domain.field).forEach(function(n,r){n=t.accessor(n),l[0]=e.min([l[0],e.min(c,n)]),l[1]=e.max([l[1],e.max(c,n)])})):l=n.domain),n.domainMin!==undefined&&(t.isObject(n.domainMin)?(l[0]=null,c=a[n.domainMin.data]||f,t.array(n.domainMin.field).forEach(function(n,r){n=t.accessor(n),l[0]=e.min([l[0],e.min(c,n)])})):l[0]=n.domainMin),n.domainMax!==undefined&&(t.isObject(n.domainMax)?(l[1]=null,c=a[n.domainMax.data]||f,t.array(n.domainMax.field).forEach(function(n,r){n=t.accessor(n),l[1]=e.max([l[1],e.max(c,n)])})):l[1]=n.domainMax),n.type!==i&&n.type!==o&&(n.zero||n.zero===undefined)&&(l[0]=Math.min(0,l[0]),l[1]=Math.max(0,l[1])),r.domain(l),n.range=="height"&&(u=u.reverse()),r[n.round?"rangeRound":"range"](u),n.exponent&&n.type===s&&r.exponent(n.exponent),n.clamp&&r.clamp(!0),n.nice&&(n.type===o?(h=e.time[n.nice],h||t.error("Unrecognized interval: "+h),r.nice(h)):r.nice())}function v(e,n){var r=[null,null];if(e.range!==undefined)if(typeof e.range=="string")if(u[e.range])r=[0,n[e.range]];else{if(!a[e.range])return t.error("Unrecogized range: "+e.range),r;r=t[e.range]}else Array.isArray(e.range)?r=e.range:r=[0,e.range];e.rangeMin!==undefined&&(r[0]=e.rangeMin),e.rangeMax!==undefined&&(r[1]=e.rangeMax);if(e.reverse!==undefined){var i=e.reverse;t.isObject(i)&&(i=t.accessor(i.field)(n.datum)),i&&(r=r.reverse())}return r}var n="linear",r="ordinal",i="log",s="pow",o="time",u={width:1,height:1},a={category10:1,category20:1,shapes:1},f={time:e.time.scale,utc:e.time.scale.utc};return l}(),t.parse.spec=function(n,r,i){function s(e){var n=e.width||500,s=e.height||500,o=e.viewport||null,u={width:n,height:s,viewport:o,padding:t.parse.padding(e.padding),marks:t.parse.marks(e,n,s),data:t.parse.data(e.data,function(){r(a)})},a=i(u)}i=i||t.ViewFactory,t.isObject(n)?s(n):e.json(n,function(e,n){e?t.error(e):s(n)})},t.parse.transform=function(e){var n=t.data[e.type]();return t.keys(e).forEach(function(t){if(t==="type")return;n[t](e[t])}),n},t.scene={},t.scene.GROUP="group",t.scene.ENTER=0,t.scene.UPDATE=1,t.scene.EXIT=2,t.scene.DEFAULT_DATA={sentinel:1},t.scene.data=function(e,n){var r=t.scene.DEFAULT_DATA;return e=t.values(e||n||[r]),e===r&&(e=[r]),e},t.scene.Item=function(){function e(e){this.mark=e}var t=e.prototype;return t.cousin=function(e,t){if(e===0)return this;e=e||-1;var n=this.mark,r=n.group,i=t==null?n.items.indexOf(this):t,s=r.items.indexOf(n)+e;return r.items[s].items[i]},t.sibling=function(e){if(e===0)return this;e=e||-1;var t=this.mark,n=t.items.indexOf(this)+e;return t.items[n]},t.remove=function(){var e=this,t=e.mark.items,n=t.indexOf(e);return n>=0&&(n===t.length-1?t.pop():t.splice(n,1)),e},e}(),t.scene.item=function(e){return new t.scene.Item(e)},t.scene.build=function(){function u(e,r,i,s){var o=t.scene.data(e.from?e.from(r,i,s):null,s);return i=a(e,i),i.items=f(e,o,i),c(e,i),e.type===n&&l(e,r,i),i}function a(e,t){return t=t||{},t.def=e,t.marktype=e.type,t.interactive=e.interactive!==!1,t}function f(e,n,o){var u=h(e.key),a=o.items||[],f=[],l={},c,p,d,v,m,g;for(c=0,d=a.length;c1&&(u=1),a=r.ease(u);for(f=0,l=r.length;f