forked from rbkreisberg/visquick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvq.scatterplot.min.js
1 lines (1 loc) · 14.1 KB
/
vq.scatterplot.min.js
1
vq.ScatterPlot=function(){vq.Vis.call(this)},vq.ScatterPlot.prototype=vq.extend(vq.Vis),vq.ScatterPlot.prototype.setRegression=function(a){this.data._regression=a||"none",this._render()},vq.ScatterPlot.prototype.getScales=function(a){var b=this.data,c,d,e,f,g=[],h=[],i=b.COLUMNID.x,j=b.COLUMNID.y;if(b.xScale){var e=b.xScale,g=d3.extent(e);c=d3.scale.linear().domain(g).range([0,b._plot.width])}if(b.yScale){var f=b.yScale,h=d3.extent(f);d=d3.scale.linear().domain(h).range([b._plot.height,0])}var k=g[0]||a.reduce(function(a,b){return b[i]!=null&&b[i]<a?b[i]:a},999999),l=g[1]||a.reduce(function(a,b){return b[i]!=null&&b[i]>a?b[i]:a},-999999),m=h[0]||a.reduce(function(a,b){return b[j]!=null&&b[j]<a?b[j]:a},999999),n=h[1]||a.reduce(function(a,b){return b[j]!=null&&b[j]>a?b[j]:a},-999999),o=k-Math.abs(l-k)*.03,p=l+Math.abs(l-k)*.03,q=m-Math.abs(n-m)*.03,r=n+Math.abs(n-m)*.03;c=c||d3.scale.linear().domain([o,p]).range([0,b._plot.width]),d=d||d3.scale.linear().domain([q,r]).range([b._plot.height,0]);return{x:c,y:d,showMaxX:p,showMinX:o,showMaxY:r,showMinY:q}},vq.ScatterPlot.prototype.draw=function(a){var b=this;this.data=new vq.models.ScatterPlotData(a);if(!!this.data.isDataReady()){var c=this.data,d=c.COLUMNID.x,e=c.COLUMNID.y,f=c.COLUMNID.value,g=c._plot.width,h=c._plot.height;this.x=d,this.y=e,this.value=f,this.data_array=c.data;var i=this.getScales(this.data_array);this.xScale=i.x,this.yScale=i.y,this.regressData=this.getRegressData(i),c._plot.svg_id=vq.utils.VisUtils.guid(),this.vis=d3.select(c._plot.container).append("svg").attr("id",c._plot.svg_id).attr("width",g+2*c.horizontal_padding).attr("height",h+2*c.vertical_padding).on("selectstart",function(){d3.event.preventDefault()}),this.data_area=this.vis.append("g").attr("transform","translate("+c.horizontal_padding+","+c.vertical_padding+")").attr("width",g).attr("height",h),this.brush_layer=this.data_area.append("g").attr("class","plot_brush"),this.xAxis=d3.svg.axis().scale(i.x).orient("bottom").tickSize(-1*h).ticks(5),x_ticks=this.data_area.append("g").attr("class","x axis").attr("transform","translate(0,"+h+")").call(this.xAxis),this.yAxis=d3.svg.axis().scale(i.y).orient("left").tickSize(-1*g).ticks(5),y_ticks=this.data_area.append("g").attr("class","y axis").attr("transform","translate(0,0)").call(this.yAxis),this.data_area.append("g").attr("class","axis").append("text").text(c.COLUMNLABEL.y).style("text-anchor","middle").attr("transform","translate("+c.yLabelDisplacement+","+h/2+") rotate(-90)"),this.data_area.append("text").attr("x",g/2).attr("y",h+c.xLabelDisplacement).style("text-anchor","middle").text(c.COLUMNLABEL.x);var j=this.data_area.append("svg").attr("left",0).attr("top",0).attr("width",g).attr("height",h).attr("viewBox","0 0 "+g+" "+h).attr("class","symbols");this.brush=d3.svg.brush(),this._render(),this.enableZoom(),this.disableZoom(),this.enableBrush(),this.enableZoom()}},vq.ScatterPlot.prototype.getRegressData=function(a){var b=this.data,c=this.x,d=this.y,e=b._regression;if(e=="none")return{type:e};if(e=="linear"){var f=this.data.data.filter(function(a,b,e){return a[d]&&a[c]}),g=d3.sum(f,function(a){return a[c]}),h=d3.sum(f,function(a){return a[d]}),i=d3.sum(f,function(a){return a[c]*a[c]}),j=d3.sum(f,function(a){return a[c]*a[d]}),k=(f.length*j-g*h)/(f.length*i-g*g),l=(h-k*g)/f.length,m=a.showMinX*.95,n=a.showMaxX*1.05,o=k*n+l,p=k*m+l,q=d3.scale.linear().domain([m,n]).range([p,o]);return{type:e,minX:m,maxX:n,scale:q}}},vq.ScatterPlot.prototype._render=function(){this.updateScales({x:this.xScale,y:this.yScale},!0)},vq.ScatterPlot.prototype.updateScales=function(a,b){var c=this,d=this.data,e=d.enableTransitions&&!b,f,g,h,i,j,k,l,m,n,o,p=d.transitionDuration,q,r,s,t,u=d._plot.width,v=d._plot.height;q=a.x,r=(q.domain()[0]+q.domain()[1])/2,s=a.y,t=(s.domain()[0]+s.domain()[1])/2,this.yAxis.scale(s),this.xAxis.scale(q),this.data_area.select("g.y.axis").transition().duration(300).call(this.yAxis),this.data_area.select("g.x.axis").transition().duration(300).call(this.xAxis),this.xScale=q,this.yScale=s,c.updateData(b)},vq.ScatterPlot.prototype.updateData=function(a){var b=this,c=this.data.data,d=this.data,e=this.data.transitionDuration,f=d.enableTransitions&&!a,g=vq.hovercard({canvas_id:d._plot.svg_id,include_header:!1,include_footer:!0,self_hover:!0,offset:{top:20,left:20},timeout:d.tooltipTimeout,data_config:d.tooltipItems,tool_config:d.tooltipLinks}),h=this.data_area.select("svg.symbols").selectAll("circle").data(c,function(a){return""+a[b.x]+a[b.y]}),i=h.enter().append("circle").attr("class","data_point").attr("cx",function(a){return b.xScale(a[b.x])}).attr("cy",function(a){return b.yScale(a[b.y])}).attr("r",d._radius).style("cursor","pointer").call(_.bind(this.setDefaultSymbolStyle,this)).style("opacity",1e-6).on("mouseover",function(a){g.call(this,a)}).on("click",function(a){b.data_area.select("svg.symbols").selectAll("circle").attr("class","bg").style("fill",d._unselectedStrokeStyle).style("stroke",d._unselectedStrokeStyle).style("stroke-width",d._strokeWidth).style("opacity",.5),d3.select(this).attr("class","fg").style("fill",d._fillStyle).style("stroke",d._strokeStyle).style("stroke-width",d._strokeWidth).style("opacity",1),d._clickHandler(a)});i.append("title").text(function(a){return d.COLUMNLABEL.value+" "+a[b.value]}),f&&(i=i.transition().duration(e)),i.style("opacity",1),h.attr("cx",function(a){return b.xScale(a[b.x])}).attr("cy",function(a){return b.yScale(a[b.y])});var j=h.exit();f&&(j=h.exit().transition().duration(e).style("opacity",1e-6)),j.remove();var k=b.regressData;if(k.type=="linear"){var l=this.data_area.select("svg.symbols").selectAll("line.regression").data([{x1:b.xScale(k.minX),y1:b.yScale(k.scale(k.minX)),x2:b.xScale(k.maxX),y2:b.yScale(k.scale(k.maxX))}],function(a){return""+a.x1+a.x2+a.y1+a.y2}),m=l.enter().append("line").attr("class","regression").attr("x1",function(a){return a.x1}).attr("y1",function(a){return a.y1}).attr("x2",function(a){return a.x2}).attr("y2",function(a){return a.y2}).attr("stroke",d._regressionStrokeStyle).style("opacity",1e-6);f&&(m=m.transition().duration(e)),m.style("opacity",1);var n=l.exit();f&&(n=l.exit().transition().duration(e).style("opacity",1e-6)),n.remove()}},vq.ScatterPlot.prototype.setDefaultSymbolStyle=function(a){var b=this.data;a.style("fill",b._fillStyle).style("stroke",b._strokeStyle).style("stroke-width",b._strokeWidth).style("opacity",1)},vq.ScatterPlot.prototype.resetData=function(a){this.data.data=a;var b=this.getScales(this.data.data);this.regressData=this.getRegressData(b),this.data_area.select("g.plot_brush").remove(),this.updateScales(b,!1)},vq.ScatterPlot.prototype.removeListeners=function(){this.brush_layer.on("mousedown.zoom",null).on("mousewheel.zoom",null).on("mousemove.zoom",null).on("DOMMouseScroll.zoom",null).on("dblclick.zoom",null).on("touchstart.zoom",null).on("touchmove.zoom",null).on("touchend.zoom",null)},vq.ScatterPlot.prototype.enableZoom=function(){var a=this;this.brush_layer.select(".extent").style("display","none"),this.brush_layer.style("cursor","move"),this.ignoreBrush=!0,this.data_area.select("svg.symbols").selectAll("circle").attr("class","fg").call(_.bind(this.setDefaultSymbolStyle,this)),this.zoom=d3.behavior.zoom().x(this.xScale).y(this.yScale).on("zoom",function(){_.bind(a.updateScales,a,{x:a.xScale,y:a.yScale},!0)()}),this.brush_layer.call(this.zoom)},vq.ScatterPlot.prototype.disableZoom=function(){var a=this;this.ignoreBrush=!1,this.brush.clear(),this.brush_layer.style("cursor","crosshair").call(this.brush),this.brush_layer.select(".extent").style("display",null),this.zoom.on("zoom",null),this.removeListeners()},vq.ScatterPlot.prototype.enableBrush=function(){this.ignoreBrush=!1,this.brush.clear(),this.brush.x(this.xScale).y(this.yScale).on("brushstart",_.bind(this.brushStart,this)).on("brush",_.bind(this.brushHandler,this)).on("brushend",_.bind(this.brushEnd,this)),this.brush_layer.call(this.brush)},vq.ScatterPlot.prototype.brushStart=function(){d3.event&&!d3.event.sourceEvent.shiftKey?(this.data_area.select("svg.symbols").selectAll("circle").style("pointer-events","none"),this.brush.empty()&&this.disableZoom()):this.data_area.select("svg.symbols").selectAll("circle").style("pointer-events","all")},vq.ScatterPlot.prototype.highlight=function(a){var b=this.data;this.data_area.select("svg.symbols").selectAll("circle").attr("class","bg").style("fill",b._unselectedStrokeStyle).style("stroke",b._unselectedStrokeStyle).style("stroke-width",b._strokeWidth).style("opacity",.5),this.data_area.select("svg.symbols").selectAll("circle").filter(function(b){return _.every(a,function(a,c){return b[c]===a})}).attr("class","fg").style("fill",b._fillStyle).style("stroke",b._strokeStyle).style("stroke-width",b._strokeWidth).style("opacity",1)},vq.ScatterPlot.prototype.brushHandler=function(){if(this.ignoreBrush)this.brush.clear();else{var a=this,b=this.brush.extent(),c=this.data,d=function(c){return b[0][0]<=c[a.x]&&c[a.x]<=b[1][0]&&b[0][1]<=c[a.y]&&c[a.y]<=b[1][1]},e=function(a){return!d(a)};this.data_area.select("svg.symbols").selectAll("circle").filter(d).attr("class","fg").style("fill",c._fillStyle).style("stroke",c._strokeStyle).style("stroke-width",c._strokeWidth).style("opacity",1),this.data_area.select("svg.symbols").selectAll("circle").filter(e).attr("class","bg").style("fill",c._unselectedStrokeStyle).style("stroke",c._unselectedStrokeStyle).style("stroke-width",c._strokeWidth).style("opacity",.5)}},vq.ScatterPlot.prototype.brushEnd=function(){var a=this,b=this.brush.extent(),c=this.data,d=c._brushHandler,e=function(c){return b[0][0]<=c[a.x]&&c[a.x]<=b[1][0]&&b[0][1]<=c[a.y]&&c[a.y]<=b[1][1]};this.brush.empty()&&(this.data_area.select("svg.symbols").selectAll("circle").attr("class","fg").call(_.bind(this.setDefaultSymbolStyle,this)),this.data_area.select("g.plot_brush").html("")),d(this.data_area.select("svg.symbols").selectAll("circle").filter(e).data()),!this.ignoreBrush&&this.brush.empty()&&(this.enableZoom(),this.data_area.select("svg.symbols").selectAll("circle").style("pointer-events","all"))},vq.models.ScatterPlotData=function(a){vq.models.VisData.call(this,a),this.setDataModel(),this.getDataType()=="vq.models.ScatterPlotData"?this._build_data(this.getContents()):console.warn("Unrecognized JSON object. Expected vq.models.ScatterPlotData object.")},vq.models.ScatterPlotData.prototype=vq.extend(vq.models.VisData),vq.models.ScatterPlotData.prototype.setDataModel=function(){this._dataModel=[{label:"_plot.width",id:"PLOT.width",cast:Number,defaultValue:512},{label:"_plot.height",id:"PLOT.height",cast:Number,defaultValue:512},{label:"_plot.container",id:"PLOT.container",optional:!0},{label:"xTickDisplacement",id:"PLOT.x_tick_displacement",cast:Number,defaultValue:15},{label:"yTickDisplacement",id:"PLOT.y_tick_displacement",cast:Number,defaultValue:-10},{label:"xLabelDisplacement",id:"PLOT.x_label_displacement",cast:Number,defaultValue:30},{label:"yLabelDisplacement",id:"PLOT.y_label_displacement",cast:Number,defaultValue:-50},{label:"vertical_padding",id:"PLOT.vertical_padding",cast:Number,defaultValue:40},{label:"horizontal_padding",id:"PLOT.horizontal_padding",cast:Number,defaultValue:60},{label:"enableTransitions",id:"PLOT.enable_transitions",cast:Boolean,defaultValue:!1},{label:"transitionDuration",id:"PLOT.transition_duration",cast:Number,defaultValue:1e3},{label:"data",id:"data_array",defaultValue:[]},{label:"COLUMNID.x",id:"xcolumnid",cast:String,defaultValue:"X"},{label:"COLUMNID.y",id:"ycolumnid",cast:String,defaultValue:"Y"},{label:"xScale",id:"xscale",defaultValue:[],optional:!0},{label:"yScale",id:"yscale",defaultValue:[],optional:!0},{label:"COLUMNID.value",id:"valuecolumnid",cast:String,defaultValue:"VALUE"},{label:"COLUMNLABEL.x",id:"xcolumnlabel",cast:String,defaultValue:""},{label:"COLUMNLABEL.y",id:"ycolumnlabel",cast:String,defaultValue:""},{label:"COLUMNLABEL.value",id:"valuecolumnlabel",cast:String,defaultValue:""},{label:"tooltipTimeout",id:"tooltip_timeout",defaultValue:200},{label:"tooltipItems",id:"tooltip_items",defaultValue:{X:"X",Y:"Y",Value:"VALUE"}},{label:"tooltipLinks",id:"tooltip_links",defaultValue:{}},{label:"_fillStyle",id:"fill_style",cast:vq.utils.VisUtils.wrapProperty,defaultValue:function(){return"rgba(70,130,180,0.2)"}},{label:"_strokeStyle",id:"stroke_style",cast:vq.utils.VisUtils.wrapProperty,defaultValue:function(){return"steelblue"}},{label:"_unselectedStrokeStyle",id:"unselected_stroke_style",cast:vq.utils.VisUtils.wrapProperty,defaultValue:function(){return"lightgray"}},{label:"_strokeWidth",id:"stroke_width",cast:vq.utils.VisUtils.wrapProperty,defaultValue:function(){return 2}},{label:"_regressionStrokeStyle",id:"regression_stroke_style",cast:vq.utils.VisUtils.wrapProperty,defaultValue:function(){return"green"}},{label:"_radius",id:"radius",cast:vq.utils.VisUtils.wrapProperty,defaultValue:function(){return 2}},{label:"_shape",id:"shape",cast:vq.utils.VisUtils.wrapProperty,defaultValue:function(){return"dot"}},{label:"_regression",id:"regression",cast:String,defaultValue:"none"},{label:"_notifier",id:"notifier",cast:Function,defaultValue:function(){return null}},{label:"_brushHandler",id:"brush_handler",cast:Function,defaultValue:function(){return null}},{label:"_clickHandler",id:"click_handler",cast:Function,defaultValue:function(){return null}}]},vq.models.ScatterPlotData.prototype._build_data=function(a){this._processData(a),this.COLUMNLABEL.x==""&&(this.COLUMNLABEL.x=this.COLUMNID.x),this.COLUMNLABEL.y==""&&(this.COLUMNLABEL.y=this.COLUMNID.y),this.COLUMNLABEL.value==""&&(this.COLUMNLABEL.value=this.COLUMNID.value),this.data.length>0&&this.setDataReady(!0)},function(a){var b={init:function(b){return this.each(function(){var c=a(this),d=a(this).data("visquick.d3.scatterplot");d||(b.CONTENTS.PLOT.container=c.get(0),c.data("visquick.d3.scatterplot",d=new vq.ScatterPlot),d.draw(b))})},reset_data:function(b){return this.each(function(){var c=a(this).data("visquick.d3.scatterplot");c&&c.resetData(b)})},enable_zoom:function(){return this.each(function(){var b=a(this).data("visquick.d3.scatterplot");b&&b.enableZoom()})},enable_brush:function(){return this.each(function(){var b=a(this).data("visquick.d3.scatterplot");b&&b.enableBrush()})}};a.fn.scatterplot=function(c){if(b[c])return b[c].apply(this,Array.prototype.slice.call(arguments,1));if(typeof c=="object"||!c)return b.init.apply(this,arguments);a.error("Method "+c+" does not exist on jQuery.scatterplot")}}(window.jQuery)