Commit 808993ce authored by Eric CHARPENTIER's avatar Eric CHARPENTIER 🐍
Browse files

reduced the size of html report file

parent 571c4207
......@@ -270,11 +270,11 @@ ecrire(rbind(DE.sel$up,DE.sel$down),paste(paste(outputDir,comp,sep="/"),"DEseqRe
#Volcano-plot
p <- ggvulcano(DE, cond1, cond2, logFCthreshold=logFCthreshold, AdjPValthreshold=AdjPValthreshold, GenesInFig=GenesInFig)
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 17, height_svg = 14), paste(outputDir, comp, "Volcano-plot.html", sep="/"))
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 17, height_svg = 14), paste(outputDir, comp, "Volcano-plot.html", sep="/"), selfcontained = FALSE)
#MA-plot
p <- ggMA(DE, cond1, cond2, meanInCompThreshold=0.5, GenesInFig=GenesInFig)
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 17, height_svg = 14), paste(outputDir, comp, "MA-plot.html", sep="/"))
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 17, height_svg = 14), paste(outputDir, comp, "MA-plot.html", sep="/"), selfcontained = FALSE)
#DE genes clustering
if(nrow(DE.sel$isDE)>=10){
......
......@@ -203,7 +203,7 @@ if(batchEffect || plateBatch){
}
p <- acp2d(acpTa,comp=c(1,2),group = sampleAnnot[,condCol],main="PCA on normalized/transformed/adjusted data")
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 6, height_svg = 4), paste(outputDir, "PCA.html", sep="/"))
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 6, height_svg = 4), paste(outputDir, "PCA.html", sep="/"), selfcontained = FALSE)
png(paste(outputDir,"/PCA.png",sep=""),width=15,height=10,units="cm",res=300,pointsize=6)
print(p)
......@@ -219,7 +219,7 @@ if(batchEffect || plateBatch){
plotAllACP(compo,acpT,sampleAnnot,condCol)
p <- acp2d(acpT,comp=c(1,2),group = sampleAnnot[,condCol],main="PCA on normalized/transformed data")
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 6, height_svg = 4), paste(outputDir, "PCA.html", sep="/"))
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 6, height_svg = 4), paste(outputDir, "PCA.html", sep="/"), selfcontained = FALSE)
png(paste(outputDir,"/PCA.png",sep=""),width=15,height=10,units="cm",res=300,pointsize=6)
print(p)
......
.girafe {
margin-left: auto;
margin-right: auto;
}
.girafe_container_std {
margin-left: auto;
margin-right: auto;
position: relative;
text-align: center;
}
.girafe_container_ie {
height: 0;
}
.girafe_svg_ie {
position: absolute;
top: 0;
left: 0;
}
.ggiraph-toolbar {
position: absolute;
background-color: rgb(255, 255, 255);
background-color: rgba(255, 255, 255,.3);
border-radius: 5px;
padding-top:2px;
opacity: 0;
transition: opacity 0.3s ease 0s;
}
.ggiraph-toolbar-top {
text-align:center;
top: 3px;
right: 0px;
left: 0px;
}
.ggiraph-toolbar-topleft {
text-align:left;
top: 3px;
left: 3px;
}
.ggiraph-toolbar-topright {
text-align:right;
top: 3px;
right: 3px;
}
.ggiraph-toolbar-bottom {
text-align:center;
bottom: 3px;
right: 0px;
left: 0px;
}
.ggiraph-toolbar-bottomleft {
text-align:left;
left: 3px;
bottom: 3px;
}
.ggiraph-toolbar-bottomright {
text-align:right;
right: 3px;
bottom: 3px;
}
.ggiraph-toolbar-block {
display: inline-block;
padding-right: 10px;
}
.ggiraph-toolbar-icon {
position: relative;
cursor: pointer;
box-sizing: border-box;
padding-right: 3px;
}
.drop {
stroke: #E30C37;
fill: #E30C37;
}
.drop:hover{
stroke: #333333;
fill: #333333;
}
.neutral {
stroke: #006699;
fill: #006699;
}
.neutral:hover{
stroke: #333333;
fill: #333333;
}
.lasso path {
stroke: rgb(80,80,80);
stroke-width:2px;
}
.lasso .drawn {
fill-opacity:.05 ;
}
.lasso .loop_close {
fill:none;
stroke-dasharray: 4,4;
}
.lasso .origin {
fill:#333333;
fill-opacity:.5;
}
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("d3-selection"),require("d3-drag")):"function"==typeof define&&define.amd?define(["exports","d3-selection","d3-drag"],n):n(t.d3=t.d3||{},t.d3,t.d3)}(this,function(t,n,r){"use strict";function e(t,n){return n={exports:{}},t(n,n.exports),n.exports}function o(){function t(t){function u(){p=[],h="",_.attr("d",null),m.attr("d",null),r.nodes().forEach(function(t){t.__lasso.possible=!1,t.__lasso.selected=!1,t.__lasso.hoverSelect=!1,t.__lasso.loopSelect=!1;var n=t.getBoundingClientRect();t.__lasso.lassoPoint=[Math.round(n.left+n.width/2),Math.round(n.top+n.height/2)]}),s&&r.on("mouseover.lasso",function(){this.__lasso.hoverSelect=!0}),i.start()}function l(){var t,n;"touchmove"===d3.event.sourceEvent.type?(t=d3.event.sourceEvent.touches[0].clientX,n=d3.event.sourceEvent.touches[0].clientY):(t=d3.event.sourceEvent.clientX,n=d3.event.sourceEvent.clientY);var s=d3.mouse(this)[0],u=d3.mouse(this)[1];""===h?(h=h+"M "+s+" "+u,v=[t,n],d=[s,u],b.attr("cx",s).attr("cy",u).attr("r",7).attr("display",null)):h=h+" L "+s+" "+u,p.push([t,n]);var l=Math.sqrt(Math.pow(t-v[0],2)+Math.pow(n-v[1],2)),f="M "+s+" "+u+" L "+d[0]+" "+d[1];_.attr("d",h),m.attr("d",f),a=l<=e,a&&o?m.attr("display",null):m.attr("display","none"),r.nodes().forEach(function(t){t.__lasso.loopSelect=!(!a||!o)&&c(p,t.__lasso.lassoPoint)<1,t.__lasso.possible=t.__lasso.hoverSelect||t.__lasso.loopSelect}),i.draw()}function f(){r.on("mouseover.lasso",null),r.nodes().forEach(function(t){t.__lasso.selected=t.__lasso.possible,t.__lasso.possible=!1}),_.attr("d",null),m.attr("d",null),b.attr("display","none"),i.end()}var h,v,d,p,g=t.append("g").attr("class","lasso"),_=g.append("path").attr("class","drawn"),m=g.append("path").attr("class","loop_close"),b=g.append("circle").attr("class","origin"),M=d3.drag().on("start",u).on("drag",l).on("end",f);n.call(M)}var n,r=[],e=75,o=!0,a=!1,s=!0,i={start:function(){},draw:function(){},end:function(){}};return t.items=function(n){if(!arguments.length)return r;r=n;var e=r.nodes();return e.forEach(function(t){t.__lasso={possible:!1,selected:!1}}),t},t.possibleItems=function(){return r.filter(function(){return this.__lasso.possible})},t.selectedItems=function(){return r.filter(function(){return this.__lasso.selected})},t.notPossibleItems=function(){return r.filter(function(){return!this.__lasso.possible})},t.notSelectedItems=function(){return r.filter(function(){return!this.__lasso.selected})},t.closePathDistance=function(n){return arguments.length?(e=n,t):e},t.closePathSelect=function(n){return arguments.length?(o=n===!0,t):o},t.isPathClosed=function(n){return arguments.length?(a=n===!0,t):a},t.hoverSelect=function(n){return arguments.length?(s=n===!0,t):s},t.on=function(n,r){if(!arguments.length)return i;if(1===arguments.length)return i[n];var e=["start","draw","end"];return e.indexOf(n)>-1&&(i[n]=r),t},t.targetArea=function(r){return arguments.length?(n=r,t):n},t}var a=e(function(t){function n(t,n,e){var o=t*n,a=r*t,s=a-t,i=a-s,u=t-i,l=r*n,f=l-n,c=l-f,h=n-c,v=o-i*c,d=v-u*c,p=d-i*h,g=u*h-p;return e?(e[0]=g,e[1]=o,e):[g,o]}t.exports=n;var r=+(Math.pow(2,27)+1)}),s=e(function(t){function n(t,n){var r=t+n,e=r-t,o=r-e,a=n-e,s=t-o,i=s+a;return i?[i,r]:[r]}function r(t,r){var e=0|t.length,o=0|r.length;if(1===e&&1===o)return n(t[0],r[0]);var a,s,i=e+o,u=new Array(i),l=0,f=0,c=0,h=Math.abs,v=t[f],d=h(v),p=r[c],g=h(p);d<g?(s=v,f+=1,f<e&&(v=t[f],d=h(v))):(s=p,c+=1,c<o&&(p=r[c],g=h(p))),f<e&&d<g||c>=o?(a=v,f+=1,f<e&&(v=t[f],d=h(v))):(a=p,c+=1,c<o&&(p=r[c],g=h(p)));for(var _,m,b,M,y,w=a+s,x=w-a,j=s-x,E=j,A=w;f<e&&c<o;)d<g?(a=v,f+=1,f<e&&(v=t[f],d=h(v))):(a=p,c+=1,c<o&&(p=r[c],g=h(p))),s=E,w=a+s,x=w-a,j=s-x,j&&(u[l++]=j),_=A+w,m=_-A,b=_-m,M=w-m,y=A-b,E=y+M,A=_;for(;f<e;)a=v,s=E,w=a+s,x=w-a,j=s-x,j&&(u[l++]=j),_=A+w,m=_-A,b=_-m,M=w-m,y=A-b,E=y+M,A=_,f+=1,f<e&&(v=t[f]);for(;c<o;)a=p,s=E,w=a+s,x=w-a,j=s-x,j&&(u[l++]=j),_=A+w,m=_-A,b=_-m,M=w-m,y=A-b,E=y+M,A=_,c+=1,c<o&&(p=r[c]);return E&&(u[l++]=E),A&&(u[l++]=A),l||(u[l++]=0),u.length=l,u}t.exports=r}),i=e(function(t){function n(t,n,r){var e=t+n,o=e-t,a=e-o,s=n-o,i=t-a;return r?(r[0]=i+s,r[1]=e,r):[i+s,e]}t.exports=n}),u=e(function(t){function n(t,n){var o=t.length;if(1===o){var a=r(t[0],n);return a[0]?a:[a[1]]}var s=new Array(2*o),i=[.1,.1],u=[.1,.1],l=0;r(t[0],n,i),i[0]&&(s[l++]=i[0]);for(var f=1;f<o;++f){r(t[f],n,u);var c=i[1];e(c,u[0],i),i[0]&&(s[l++]=i[0]);var h=u[1],v=i[1],d=h+v,p=d-h,g=v-p;i[1]=d,g&&(s[l++]=g)}return i[1]&&(s[l++]=i[1]),0===l&&(s[l++]=0),s.length=l,s}var r=a,e=i;t.exports=n}),l=e(function(t){function n(t,n){var r=t+n,e=r-t,o=r-e,a=n-e,s=t-o,i=s+a;return i?[i,r]:[r]}function r(t,r){var e=0|t.length,o=0|r.length;if(1===e&&1===o)return n(t[0],-r[0]);var a,s,i=e+o,u=new Array(i),l=0,f=0,c=0,h=Math.abs,v=t[f],d=h(v),p=-r[c],g=h(p);d<g?(s=v,f+=1,f<e&&(v=t[f],d=h(v))):(s=p,c+=1,c<o&&(p=-r[c],g=h(p))),f<e&&d<g||c>=o?(a=v,f+=1,f<e&&(v=t[f],d=h(v))):(a=p,c+=1,c<o&&(p=-r[c],g=h(p)));for(var _,m,b,M,y,w=a+s,x=w-a,j=s-x,E=j,A=w;f<e&&c<o;)d<g?(a=v,f+=1,f<e&&(v=t[f],d=h(v))):(a=p,c+=1,c<o&&(p=-r[c],g=h(p))),s=E,w=a+s,x=w-a,j=s-x,j&&(u[l++]=j),_=A+w,m=_-A,b=_-m,M=w-m,y=A-b,E=y+M,A=_;for(;f<e;)a=v,s=E,w=a+s,x=w-a,j=s-x,j&&(u[l++]=j),_=A+w,m=_-A,b=_-m,M=w-m,y=A-b,E=y+M,A=_,f+=1,f<e&&(v=t[f]);for(;c<o;)a=p,s=E,w=a+s,x=w-a,j=s-x,j&&(u[l++]=j),_=A+w,m=_-A,b=_-m,M=w-m,y=A-b,E=y+M,A=_,c+=1,c<o&&(p=-r[c]);return E&&(u[l++]=E),A&&(u[l++]=A),l||(u[l++]=0),u.length=l,u}t.exports=r}),f=e(function(t){function n(t,n){for(var r=new Array(t.length-1),e=1;e<t.length;++e)for(var o=r[e-1]=new Array(t.length-1),a=0,s=0;a<t.length;++a)a!==n&&(o[s++]=t[e][a]);return r}function r(t){for(var n=new Array(t),r=0;r<t;++r){n[r]=new Array(t);for(var e=0;e<t;++e)n[r][e]=["m",e,"[",t-r-1,"]"].join("")}return n}function e(t){return 1&t?"-":""}function o(t){if(1===t.length)return t[0];if(2===t.length)return["sum(",t[0],",",t[1],")"].join("");var n=t.length>>1;return["sum(",o(t.slice(0,n)),",",o(t.slice(n)),")"].join("")}function i(t){if(2===t.length)return[["sum(prod(",t[0][0],",",t[1][1],"),prod(-",t[0][1],",",t[1][0],"))"].join("")];for(var r=[],a=0;a<t.length;++a)r.push(["scale(",o(i(n(t,a))),",",e(a),t[0][a],")"].join(""));return r}function f(t){for(var e=[],a=[],s=r(t),u=[],l=0;l<t;++l)0===(1&l)?e.push.apply(e,i(n(s,l))):a.push.apply(a,i(n(s,l))),u.push("m"+l);var f=o(e),c=o(a),h="orientation"+t+"Exact",_=["function ",h,"(",u.join(),"){var p=",f,",n=",c,",d=sub(p,n);return d[d.length-1];};return ",h].join(""),m=new Function("sum","prod","scale","sub",_);return m(d,v,p,g)}function c(t){var n=x[t.length];return n||(n=x[t.length]=f(t.length)),n.apply(void 0,t)}function h(){for(;x.length<=_;)x.push(f(x.length));for(var n=[],r=["slow"],e=0;e<=_;++e)n.push("a"+e),r.push("o"+e);for(var o=["function getOrientation(",n.join(),"){switch(arguments.length){case 0:case 1:return 0;"],e=2;e<=_;++e)o.push("case ",e,":return o",e,"(",n.slice(0,e).join(),");");o.push("}var s=new Array(arguments.length);for(var i=0;i<arguments.length;++i){s[i]=arguments[i]};return slow(s);}return getOrientation"),r.push(o.join(""));var a=Function.apply(void 0,r);t.exports=a.apply(void 0,[c].concat(x));for(var e=0;e<=_;++e)t.exports[e]=x[e]}var v=a,d=s,p=u,g=l,_=5,m=1.1102230246251565e-16,b=(3+16*m)*m,M=(7+56*m)*m,y=f(3),w=f(4),x=[function(){return 0},function(){return 0},function(t,n){return n[0]-t[0]},function(t,n,r){var e,o=(t[1]-r[1])*(n[0]-r[0]),a=(t[0]-r[0])*(n[1]-r[1]),s=o-a;if(o>0){if(a<=0)return s;e=o+a}else{if(!(o<0))return s;if(a>=0)return s;e=-(o+a)}var i=b*e;return s>=i||s<=-i?s:y(t,n,r)},function(t,n,r,e){var o=t[0]-e[0],a=n[0]-e[0],s=r[0]-e[0],i=t[1]-e[1],u=n[1]-e[1],l=r[1]-e[1],f=t[2]-e[2],c=n[2]-e[2],h=r[2]-e[2],v=a*l,d=s*u,p=s*i,g=o*l,_=o*u,m=a*i,b=f*(v-d)+c*(p-g)+h*(_-m),y=(Math.abs(v)+Math.abs(d))*Math.abs(f)+(Math.abs(p)+Math.abs(g))*Math.abs(c)+(Math.abs(_)+Math.abs(m))*Math.abs(h),x=M*y;return b>x||-b>x?b:w(t,n,r,e)}];h()}),c=e(function(t){function n(t,n){for(var e=n[0],o=n[1],a=t.length,s=1,i=a,u=0,l=a-1;u<i;l=u++){var f=t[u],c=t[l],h=f[1],v=c[1];if(v<h){if(v<o&&o<h){var d=r(f,c,n);if(0===d)return 0;s^=0<d|0}else if(o===h){var p=t[(u+1)%a],g=p[1];if(h<g){var d=r(f,c,n);if(0===d)return 0;s^=0<d|0}}}else if(h<v){if(h<o&&o<v){var d=r(f,c,n);if(0===d)return 0;s^=d<0|0}else if(o===h){var p=t[(u+1)%a],g=p[1];if(g<h){var d=r(f,c,n);if(0===d)return 0;s^=d<0|0}}}else if(o===h){var _=Math.min(f[0],c[0]),m=Math.max(f[0],c[0]);if(0===u){for(;l>0;){var b=(l+a-1)%a,M=t[b];if(M[1]!==o)break;var y=M[0];_=Math.min(_,y),m=Math.max(m,y),l=b}if(0===l)return _<=e&&e<=m?0:1;i=l+1}for(var w=t[(l+a-1)%a][1];u+1<i;){var M=t[u+1];if(M[1]!==o)break;var y=M[0];_=Math.min(_,y),m=Math.max(m,y),u+=1}if(_<=e&&e<=m)return 0;var x=t[(u+1)%a][1];e<_&&w<o!=x<o&&(s^=1)}}return 2*s-1}t.exports=n;var r=f});t.lasso=o,Object.defineProperty(t,"__esModule",{value:!0})});
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
function isArray(x) {
return x.constructor.toString().indexOf("Array") > -1;
}
function set_reactive(x, id ){
Shiny.addCustomMessageHandler(id + '_set', function(message) {
if( typeof message === 'string' ) {
x.setSelected([message]);
} else if( isArray(message) ){
x.setSelected(message);
}
});
Shiny.addCustomMessageHandler(id + '_key_set', function(message) {
if( typeof message === 'string' ) {
x.setKeySelected([message]);
} else if( isArray(message) ){
x.setKeySelected(message);
}
});
Shiny.addCustomMessageHandler(id + '_theme_set', function(message) {
if( typeof message === 'string' ) {
x.setThemeSelected([message]);
} else if( isArray(message) ){
x.setThemeSelected(message);
}
});
}
HTMLWidgets.widget({
name: "girafe",
type: "output",
factory: function(el, width, height) {
var ggobj = ggiraphjs.newgi(el.id);
return {
renderValue: function(x) {
ggobj.setSvgId(x.uid);
ggobj.addStyle(x.settings.tooltip.css,
x.settings.hover.css, x.settings.hoverkey.css, x.settings.hovertheme.css,
x.settings.capture.css, x.settings.capturekey.css, x.settings.capturetheme.css);
ggobj.setZoomer(x.settings.zoom.min, x.settings.zoom.max);
ggobj.addSvg(x.html, x.js);
ggobj.animateGElements(x.settings.tooltip.opacity,
x.settings.tooltip.offx, x.settings.tooltip.offy,
x.settings.tooltip.use_cursor_pos,
x.settings.tooltip.delay.over, x.settings.tooltip.delay.out,
x.settings.tooltip.usefill, x.settings.tooltip.usestroke);
ggobj.animateToolbar();
if( !x.settings.sizing.rescale ){
var width_ = d3.select(el).style("width");
var height_ = d3.select(el).style("height");
ggobj.fixSize(width_, height_);
} else if( HTMLWidgets.shinyMode ){
ggobj.autoScale("100%");
ggobj.IEFixResize(1, 1/x.ratio);
var maxWidth = width;
var maxHeight = height;
try {
const box = d3.select("#" + ggobj.svgid).property("viewBox").baseVal;
maxWidth = box.width;
maxHeight = box.height;
} catch (e) {}
ggobj.setSizeLimits(maxWidth+'px', 0, maxHeight+'px', 0);
ggobj.removeContainerLimits();
} else {
ggobj.autoScale(Math.round(x.settings.sizing.width * 100) + "%");
ggobj.IEFixResize(x.settings.sizing.width, 1/x.ratio);
ggobj.setSizeLimits("unset", "unset", "unset", "unset");
ggobj.removeContainerLimits();
}
var addSelection = ggobj.isSelectable() &&
(( HTMLWidgets.shinyMode &&
( x.settings.capture.only_shiny ||
x.settings.capturekey.only_shiny ||
x.settings.capturetheme.only_shiny)
) ||
( !HTMLWidgets.shinyMode &&
( !x.settings.capture.only_shiny ||
!x.settings.capturekey.only_shiny ||
!x.settings.capturetheme.only_shiny)
));
var addZoom = true;
if( x.settings.zoom.min === 1 && x.settings.zoom.max <= 1 ){
addZoom = false;
}
if( addSelection && x.settings.capturetheme.type == "single" ){
ggobj.selectizeThemeSingle();
if( typeof x.settings.capturetheme.selected === 'string' ) {
ggobj.setThemeSelected([x.settings.capturetheme.selected]);
}
} else if( addSelection && x.settings.capturetheme.type == "multiple" ){
ggobj.selectizeThemeMultiple();
if( typeof x.settings.capturetheme.selected === 'string' ) {
ggobj.setThemeSelected([x.settings.capturetheme.selected]);
} else if( isArray(x.settings.capturetheme.selected) ){
ggobj.setThemeSelected(x.settings.capturetheme.selected);
}
} else {
ggobj.selectizeThemeNone();
}
if( addSelection && x.settings.capturekey.type == "single" ){
ggobj.selectizeKeySingle();
if( typeof x.settings.capturekey.selected === 'string' ) {
ggobj.setKeySelected([x.settings.capturekey.selected]);
}
} else if( addSelection && x.settings.capturekey.type == "multiple" ){
ggobj.selectizeKeyMultiple();
if( typeof x.settings.capturekey.selected === 'string' ) {
ggobj.setKeySelected([x.settings.capturekey.selected]);
} else if( isArray(x.settings.capturekey.selected) ){
ggobj.setKeySelected(x.settings.capturekey.selected);
}
} else {
ggobj.selectizeKeyNone();
}
if( addSelection && x.settings.capture.type == "single" ){
ggobj.selectizeSingle();
addSelection = false;
if( typeof x.settings.capture.selected === 'string' ) {
ggobj.setSelected([x.settings.capture.selected]);
}
} else if( addSelection && x.settings.capture.type == "multiple" ){
ggobj.selectizeMultiple();
if( typeof x.settings.capture.selected === 'string' ) {
ggobj.setSelected([x.settings.capture.selected]);
} else if( isArray(x.settings.capture.selected) ){
ggobj.setSelected(x.settings.capture.selected);
}
} else {
ggobj.selectizeNone();
addSelection = false;
}
ggobj.addUI(addSelection, addZoom,
x.settings.toolbar.saveaspng,
'ggiraph-toolbar-' + x.settings.toolbar.position);
if( HTMLWidgets.shinyMode ){
ggobj.setInputId(el.id + "_selected");
ggobj.setInputKeyId(el.id + "_key_selected");
ggobj.setInputThemeId(el.id + "_theme_selected");
set_reactive(ggobj, el.id );
}
},
resize: function(width, height) {
//ggobj.setSize(width, height);
}
};
}
});
This diff is collapsed.
import * as selection from "d3-selection";
import * as drag from "d3-drag";
import classifyPoint from "robust-point-in-polygon";
export default function() {
var items =[],
closePathDistance = 75,
closePathSelect = true,
isPathClosed = false,
hoverSelect = true,
targetArea,
on = {start:function(){}, draw: function(){}, end: function(){}};
// Function to execute on call
function lasso(_this) {
// add a new group for the lasso
var g = _this.append("g")
.attr("class","lasso");
// add the drawn path for the lasso
var dyn_path = g.append("path")
.attr("class","drawn");
// add a closed path
var close_path = g.append("path")
.attr("class","loop_close");
// add an origin node
var origin_node = g.append("circle")
.attr("class","origin");
// The transformed lasso path for rendering
var tpath;
// The lasso origin for calculations
var origin;
// The transformed lasso origin for rendering
var torigin;
// Store off coordinates drawn
var drawnCoords;
// Apply drag behaviors
var drag = d3.drag()
.on("start",dragstart)
.on("drag",dragmove)
.on("end",dragend);
// Call drag
targetArea.call(drag);
function dragstart() {
// Init coordinates
drawnCoords = [];
// Initialize paths
tpath = "";
dyn_path.attr("d",null);
close_path.attr("d",null);
// Set every item to have a false selection and reset their center point and counters
items.nodes().forEach(function(e) {
e.__lasso.possible = false;
e.__lasso.selected = false;
e.__lasso.hoverSelect = false;
e.__lasso.loopSelect = false;
var box = e.getBoundingClientRect();
e.__lasso.lassoPoint = [Math.round(box.left + box.width/2),Math.round(box.top + box.height/2)];
});
// if hover is on, add hover function
if(hoverSelect) {
items.on("mouseover.lasso",function() {
// if hovered, change lasso selection attribute to true
this.__lasso.hoverSelect = true;
});
}
// Run user defined start function
on.start();
}
function dragmove() {
// Get mouse position within body, used for calculations
var x,y;
if(d3.event.sourceEvent.type === "touchmove") {
x = d3.event.sourceEvent.touches[0].clientX;
y = d3.event.sourceEvent.touches[0].clientY;
}
else {
x = d3.event.sourceEvent.clientX;
y = d3.event.sourceEvent.clientY;
}
// Get mouse position within drawing area, used for rendering
var tx = d3.mouse(this)[0];
var ty = d3.mouse(this)[1];
// Initialize the path or add the latest point to it
if (tpath==="") {
tpath = tpath + "M " + tx + " " + ty;
origin = [x,y];
torigin = [tx,ty];
// Draw origin node
origin_node
.attr("cx",tx)
.attr("cy",ty)
.attr("r",7)
.attr("display",null);
}
else {
tpath = tpath + " L " + tx + " " + ty;
}
drawnCoords.push([x,y]);
// Calculate the current distance from the lasso origin
var distance = Math.sqrt(Math.pow(x-origin[0],2)+Math.pow(y-origin[1],2));
// Set the closed path line
var close_draw_path = "M " + tx + " " + ty + " L " + torigin[0] + " " + torigin[1];
// Draw the lines
dyn_path.attr("d",tpath);
close_path.attr("d",close_draw_path);
// Check if the path is closed
isPathClosed = distance<=closePathDistance ? true : false;
// If within the closed path distance parameter, show the closed path. otherwise, hide it
if(isPathClosed && closePathSelect) {
close_path.attr("display",null);
}
else {
close_path.attr("display","none");
}
items.nodes().forEach(function(n) {
n.__lasso.loopSelect = (isPathClosed && closePathSelect) ? (classifyPoint(drawnCoords,n.__lasso.lassoPoint) < 1) : false;
n.__lasso.possible = n.__lasso.hoverSelect || n.__lasso.loopSelect;
});
on.draw();
}
function dragend() {
// Remove mouseover tagging function
items.on("mouseover.lasso",null);
items.nodes().forEach(function(n) {
n.__lasso.selected = n.__lasso.possible;
n.__lasso.possible = false;
});
// Clear lasso
dyn_path.attr("d",null);
close_path.attr("d",null);
origin_node.attr("display","none");
// Run user defined end function
on.end();
}
}
// Set or get list of items for lasso to select
lasso.items = function(_) {
if (!arguments.length) return items;
items = _;
var nodes = items.nodes();
nodes.forEach(function(n) {
n.__lasso = {
"possible": false,
"selected": false
};
});
return lasso;
};
// Return possible items
lasso.possibleItems = function() {
return items.filter(function() {