mirror of
https://github.com/medialab-prado/poblados-colonizacion-colonias-penitenciarias.git
synced 2024-12-26 20:41:23 +01:00
6 lines
96 KiB
JavaScript
6 lines
96 KiB
JavaScript
|
/*! grafica.js (0.1.0): a library for p5.js. Author: Javier Graciá Carpio, license: LGPL-3.0, 2015-04-12 */ function GPoint(){var a,b,c;if(3===arguments.length)a=arguments[0],b=arguments[1],c=arguments[2];else if(2===arguments.length&&arguments[0]instanceof p5.Vector)a=arguments[0].x,b=arguments[0].y,c=arguments[1];else if(2===arguments.length)a=arguments[0],b=arguments[1],c="";else if(1===arguments.length&&arguments[0]instanceof GPoint)a=arguments[0].getX(),b=arguments[0].getY(),c=arguments[0].getLabel();else if(1===arguments.length&&arguments[0]instanceof p5.Vector)a=arguments[0].x,b=arguments[0].y,c="";else{if(0!==arguments.length)throw new Error("GPoint constructor: signature not supported");a=0,b=0,c=""}this.x=a,this.y=b,this.label=c,this.valid=this.isValidNumber(this.x)&&this.isValidNumber(this.y)}function GTitle(a,b){this.parent=a,this.dim=b.slice(),this.relativePos=.5,this.plotPos=this.relativePos*this.dim[0],this.offset=10,this.text="",this.textAlignment=this.parent.CENTER,this.fontName="Helvetica",this.fontColor=this.parent.color(100),this.fontStyle=this.parent.BOLD,this.fontSize=13}function GAxisLabel(a,b,c){this.parent=a,this.type=b===this.parent.BOTTOM||b===this.parent.TOP||b===this.parent.LEFT||b===this.parent.RIGHT?b:this.parent.BOTTOM,this.dim=c.slice(),this.relativePos=.5,this.plotPos=this.type===this.parent.BOTTOM||this.type===this.parent.TOP?this.relativePos*this.dim[0]:-this.relativePos*this.dim[1],this.offset=35,this.rotate=this.type===this.parent.BOTTOM||this.type===this.parent.TOP?!1:!0,this.text="",this.textAlignment=this.parent.CENTER,this.fontName="Helvetica",this.fontColor=this.parent.color(0),this.fontSize=13}function GAxis(a,b,c,d,e){this.parent=a,this.type=b===this.parent.BOTTOM||b===this.parent.TOP||b===this.parent.LEFT||b===this.parent.RIGHT?b:this.parent.BOTTOM,this.dim=c.slice(),this.lim=d.slice(),this.log=e,this.log&&(this.lim[0]<=0||this.lim[1]<=0)&&(console.log("The limits are negative. This is not allowed in logarithmic scale."),console.log("Will set them to (0.1, 10)"),this.lim[1]>this.lim[0]?(this.lim[0]=.1,this.lim[1]=10):(this.lim[0]=10,this.lim[1]=.1)),this.offset=5,this.lineColor=this.parent.color(0),this.lineWidth=1,this.nTicks=5,this.ticksSeparation=-1,this.ticks=[],this.plotTicks=[],this.ticksInside=[],this.tickLabels=[],this.fixedTicks=!1,this.tickLength=3,this.smallTickLength=2,this.expTickLabels=!1,this.rotateTickLabels=this.type===this.parent.BOTTOM||this.type===this.parent.TOP?!1:!0,this.drawTickLabels=this.type===this.parent.BOTTOM||this.type===this.parent.LEFT?!0:!1,this.tickLabelOffset=7,this.ticksPrecision=void 0,this.lab=new GAxisLabel(this.parent,this.type,this.dim),this.drawAxisLabel=!0,this.fontName="Helvetica",this.fontColor=this.parent.color(0),this.fontSize=11,this.updateTicks(),this.updatePlotTicks(),this.updateTicksInside(),this.updateTickLabels()}function GHistogram(a,b,c,d){this.parent=a,this.type=b===GPlot.VERTICAL||b===GPlot.HORIZONTAL?b:GPlot.VERTICAL,this.dim=c.slice(),this.plotPoints=[];for(var e=0;e<d.length;e++)this.plotPoints[e]=new GPoint(d[e]);this.visible=!0,this.separations=[2],this.bgColors=[this.parent.color(150,150,255)],this.lineColors=[this.parent.color(100,100,255)],this.lineWidths=[1],this.differences=[],this.leftSides=[],this.rightSides=[],this.updateArrays(),this.labelsOffset=8,this.drawLabels=!1,this.rotateLabels=!1,this.fontName="Helvetica",this.fontColor=this.parent.color(0),this.fontSize=11}function GLayer(a,b,c,d,e,f,g){this.parent=a,this.id=b,this.dim=c.slice(),this.xLim=d.slice(),this.yLim=e.slice(),this.xLog=f,this.yLog=g,this.xLog&&(this.xLim[0]<=0||this.xLim[1]<=0)&&(console.log("One of the limits is negative. This is not allowed in logarithmic scale."),console.log("Will set horizontal limits to (0.1, 10)"),this.xLim[0]=.1,this.xLim[1]=10),this.yLog&&(this.yLim[0]<=0||this.yLim[1]<=0)&&(console.log("One of the limits is negative. This is not allowed in logarithmic scale."),console.log("Will set vertical limits to (0.1, 10)"),this.yLim[0]=.1,this.yLim[1]=10),this.points=[],this.plotPoints=[],this.inside=[],this.pointColors=
|
||
|
|
||
|
a=arguments[0][0],b=arguments[0][1]}a>0&&b>0&&(this.dim[0]=a,this.dim[1]=b,this.updateArrays())},GHistogram.prototype.setPlotPoints=function(a){var b,c=a.length;if(this.plotPoints.length===c)for(b=0;c>b;b++)this.plotPoints[b].set(a[b]);else if(this.plotPoints.length>c){for(b=0;c>b;b++)this.plotPoints[b].set(a[b]);this.plotPoints.splice(c,Number.MAX_VALUE)}else{for(b=0;b<this.plotPoints.length;b++)this.plotPoints[b].set(a[b]);for(b=this.plotPoints.lengh;c>b;b++)this.plotPoints[b]=new GPoint(a[b])}this.updateArrays()},GHistogram.prototype.setPlotPoint=function(a,b){if(a<this.plotPoints.length)this.plotPoints[a].set(b);else{if(a!==this.plotPoints.length)throw new Error("GHistogram.setPlotPoint(): the index position is outside the array size");this.plotPoints[a]=new GPoint(b)}this.updateArrays()},GHistogram.prototype.addPlotPoint=function(){if(2===arguments.length)this.plotPoints.push(new GPoint(arguments[0],arguments[1]));else{if(1!==arguments.length)throw new Error("GHistogram.addPlotPoint(): signature not supported");this.plotPoints.push(new GPoint(arguments[0]))}this.updateArrays()},GHistogram.prototype.removePlotPoint=function(a){if(!(a<this.plotPoints.length))throw new Error("GHistogram.removePlotPoint(): the index position is outside the array size");this.plotPoints.splice(a,1),this.updateArrays()},GHistogram.prototype.setSeparations=function(a){this.separations=a.slice(),this.updateArrays()},GHistogram.prototype.setBgColors=function(a){this.bgColors=a.slice()},GHistogram.prototype.setLineColors=function(a){this.lineColors=a.slice()},GHistogram.prototype.setLineWidths=function(a){this.lineWidths=a.slice()},GHistogram.prototype.setVisible=function(a){this.visible=a},GHistogram.prototype.setLabelsOffset=function(a){this.labelsOffset=a},GHistogram.prototype.setDrawLabels=function(a){this.drawLabels=a},GHistogram.prototype.setRotateLabels=function(a){this.rotateLabels=a},GHistogram.prototype.setFontName=function(a){this.fontName=a},GHistogram.prototype.setFontColor=function(a){this.fontColor=a},GHistogram.prototype.setFontSize=function(a){a>0&&(this.fontSize=a)},GHistogram.prototype.setFontProperties=function(a,b,c){c>0&&(this.fontName=a,this.fontColor=b,this.fontSize=c)},GLayer.prototype.isValidNumber=function(a){return!isNaN(a)&&isFinite(a)},GLayer.prototype.isId=function(a){return this.id===a},GLayer.prototype.valueToXPlot=function(a){return this.xLog?this.dim[0]*Math.log(a/this.xLim[0])/Math.log(this.xLim[1]/this.xLim[0]):this.dim[0]*(a-this.xLim[0])/(this.xLim[1]-this.xLim[0])},GLayer.prototype.valueToYPlot=function(a){return this.yLog?-this.dim[1]*Math.log(a/this.yLim[0])/Math.log(this.yLim[1]/this.yLim[0]):-this.dim[1]*(a-this.yLim[0])/(this.yLim[1]-this.yLim[0])},GLayer.prototype.valueToPlot=function(){if(2===arguments.length)return[this.valueToXPlot(arguments[0]),this.valueToYPlot(arguments[1])];if(1===arguments.length&&arguments[0]instanceof GPoint)return new GPoint(this.valueToXPlot(arguments[0].getX()),this.valueToYPlot(arguments[0].getY()),arguments[0].getLabel());if(1===arguments.length&&arguments[0]instanceof Array&&arguments[0][0]instanceof GPoint){var a,b,c,d,e,f,g=arguments[0].length,h=[];if(this.xLog&&this.yLog)for(a=this.dim[0]/Math.log(this.xLim[1]/this.xLim[0]),b=-this.dim[1]/Math.log(this.yLim[1]/this.yLim[0]),f=0;g>f;f++)c=arguments[0][f],d=Math.log(c.getX()/this.xLim[0])*a,e=Math.log(c.getY()/this.yLim[0])*b,h[f]=new GPoint(d,e,c.getLabel());else if(this.xLog)for(a=this.dim[0]/Math.log(this.xLim[1]/this.xLim[0]),b=-this.dim[1]/(this.yLim[1]-this.yLim[0]),f=0;g>f;f++)c=arguments[0][f],d=Math.log(c.getX()/this.xLim[0])*a,e=(c.getY()-this.yLim[0])*b,h[f]=new GPoint(d,e,c.getLabel());else if(this.yLog)for(a=this.dim[0]/(this.xLim[1]-this.xLim[0]),b=-this.dim[1]/Math.log(this.yLim[1]/this.yLim[0]),f=0;g>f;f++)c=arguments[0][f],d=(c.getX()-this.xLim[0])*a,e=Math.log(c.getY()/this.yLim[0])*b,h[f]=new GPoint(d,e,c.getLabel());else for(a=this.dim[0]/(this.xLim[1]-this.xLim[0]),b=-this.dim[1]/(this.yLim[1]-this.yLim[0]),f=0;g>f;f++)c=arguments[0][f],d=(c.getX()-this.xLim[0])*a,e=(c.getY()-this.yLim[0])*b,h
|
||
|
c=!0;break}c?console.log("A layer with the same id exists. Please change the id and try to add it again."):(b.setDim(this.dim),b.setLimAndLog(this.xLim,this.yLim,this.xLog,this.yLog),this.layerList.push(b),this.includeAllLayersInLim&&this.updateLimits())},GPlot.prototype.removeLayer=function(a){for(var b,c=0;c<this.layerList.length;c++)if(this.layerList[c].isId(a)){b=c;break}"undefined"!=typeof b?(this.layerList.splice(b,1),this.includeAllLayersInLim&&this.updateLimits()):console.log("Couldn't find a layer in the plot with id = "+a)},GPlot.prototype.getPlotPosAt=function(a,b){var c=a-(this.pos[0]+this.mar[1]),d=b-(this.pos[1]+this.mar[2]+this.dim[1]);return[c,d]},GPlot.prototype.getScreenPosAtValue=function(a,b){var c=this.mainLayer.valueToXPlot(a)+(this.pos[0]+this.mar[1]),d=this.mainLayer.valueToYPlot(b)+(this.pos[1]+this.mar[2]+this.dim[1]);return[c,d]},GPlot.prototype.getPointAt=function(){var a,b,c;if(3===arguments.length)a=arguments[0],b=arguments[1],c=this.getLayer(arguments[2]);else{if(2!==arguments.length)throw new Error("GPlot.getPointAt(): signature not supported");a=arguments[0],b=arguments[1],c=this.mainLayer}var d=this.getPlotPosAt(a,b);return c.getPointAtPlotPos(d[0],d[1])},GPlot.prototype.addPointAt=function(){var a,b,c;if(3===arguments.length)a=arguments[0],b=arguments[1],c=arguments[2];else{if(2!==arguments.length)throw new Error("GPlot.addPointAt(): signature not supported");a=arguments[0],b=arguments[1],c=GPlot.MAINLAYERID}var d=this.getValueAt(a,b);this.addPoint(d[0],d[1],"",c)},GPlot.prototype.removePointAt=function(){var a,b,c;if(3===arguments.length)a=arguments[0],b=arguments[1],c=arguments[2];else{if(2!==arguments.length)throw new Error("GPlot.removePointAt(): signature not supported");a=arguments[0],b=arguments[1],c=GPlot.MAINLAYERID}var d=this.getPlotPosAt(a,b),e=this.getLayer(c).getPointIndexAtPlotPos(d[0],d[1]);"undefined"!=typeof e&&this.removePoint(e,c)},GPlot.prototype.getValueAt=function(a,b){var c=this.getPlotPosAt(a,b);return this.mainLayer.plotToValue(c[0],c[1])},GPlot.prototype.getRelativePlotPosAt=function(a,b){var c=this.getPlotPosAt(a,b);return[c[0]/this.dim[0],-c[1]/this.dim[1]]},GPlot.prototype.isOverPlot=function(){var a,b;if(2===arguments.length)a=arguments[0],b=arguments[1];else{if(0!==arguments.length)throw new Error("GPlot.isOverPlot(): signature not supported");a=this.parent.mouseX,b=this.parent.mouseY}return a>=this.pos[0]&&a<=this.pos[0]+this.outerDim[0]&&b>=this.pos[1]&&b<=this.pos[1]+this.outerDim[1]},GPlot.prototype.isOverBox=function(){var a,b;if(2===arguments.length)a=arguments[0],b=arguments[1];else{if(0!==arguments.length)throw new Error("GPlot.isOverBox(): signature not supported");a=this.parent.mouseX,b=this.parent.mouseY}return a>=this.pos[0]+this.mar[1]&&a<=this.pos[0]+this.outerDim[0]-this.mar[3]&&b>=this.pos[1]+this.mar[2]&&b<=this.pos[1]+this.outerDim[1]-this.mar[0]},GPlot.prototype.updateLimits=function(){this.fixedXLim||(this.xLim=this.calculatePlotXLim(),this.xAxis.setLim(this.xLim),this.topAxis.setLim(this.xLim)),this.fixedYLim||(this.yLim=this.calculatePlotYLim(),this.yAxis.setLim(this.yLim),this.rightAxis.setLim(this.yLim)),this.mainLayer.setXYLim(this.xLim,this.yLim);for(var a=0;a<this.layerList.length;a++)this.layerList[a].setXYLim(this.xLim,this.yLim)},GPlot.prototype.calculatePlotXLim=function(){var a=this.calculatePointsXLim(this.mainLayer.getPointsRef());if(this.includeAllLayersInLim)for(var b=0;b<this.layerList.length;b++){var c=this.calculatePointsXLim(this.layerList[b].getPointsRef());"undefined"!=typeof c&&("undefined"!=typeof a?(a[0]=Math.min(a[0],c[0]),a[1]=Math.max(a[1],c[1])):a=c)}if("undefined"!=typeof a){var d=0===a[0]?.1:.1*a[0];this.xLog?(a[0]!==a[1]&&(d=Math.exp(this.expandLimFactor*Math.log(a[1]/a[0]))),a[0]=a[0]/d,a[1]=a[1]*d):(a[0]!==a[1]&&(d=this.expandLimFactor*(a[1]-a[0])),a[0]=a[0]-d,a[1]=a[1]+d)}else a=this.xLog&&(this.xLim[0]<=0||this.xLim[1]<=0)?[.1,10]:this.xLim.slice();return this.invertedXScale&&a[0]<a[1]&&(a=[a[1],a[0]]),a},GPlot.prototype.calculatePlotYLim=function(){var a=this.calculatePointsYLim(this.mainLayer.getPo
|
||
|
this.center(this.parent.mouseX,this.parent.mouseY)),this.resetIsActive&&b===this.resetButton&&c===this.resetKeyModifier&&"undefined"!=typeof this.xLimReset&&"undefined"!=typeof this.yLimReset&&(this.setXLim(this.xLimReset),this.setYLim(this.yLimReset),this.xLimReset=void 0,this.yLimReset=void 0)}},GPlot.prototype.mouseDownEvent=function(a){if(e=a||window.event,this.isOverBox()){var b=this.getButton(e),c=this.getModifier(e);this.panningIsActive&&b===this.panningButton&&c===this.panningKeyModifier&&(this.resetIsActive&&this.saveResetLimits(),this.panningReferencePoint=this.getValueAt(this.parent.mouseX,this.parent.mouseY),this.panningIntervalId=setInterval(this.panningFunction.bind(this),100)),this.labelingIsActive&&b===this.labelingButton&&c===this.labelingKeyModifier&&(this.mousePos=[this.parent.mouseX,this.parent.mouseY])}},GPlot.prototype.mouseUpEvent=function(a){e=a||window.event;{var b=this.getButton(e);this.getModifier(e)}this.panningIsActive&&b===this.panningButton&&"undefined"!=typeof this.panningIntervalId&&(clearInterval(this.panningIntervalId),this.panningIntervalId=void 0,this.panningReferencePoint=void 0),this.labelingIsActive&&b===this.labelingButton&&(this.mousePos=void 0),b===this.parent.RIGHT&&this.clickEvent(e)},GPlot.prototype.wheelEvent=function(a){if(e=a||window.event,this.isOverBox()){var b=e.deltaY,c=this.parent.CENTER,d=this.getModifier(e);this.zoomingIsActive&&(c===this.increaseZoomButton&&d===this.increaseZoomKeyModifier&&b>0?(this.resetIsActive&&this.saveResetLimits(),this.zoom(this.zoomFactor,this.parent.mouseX,this.parent.mouseY)):c===this.decreaseZoomButton&&d===this.decreaseZoomKeyModifier&&0>b&&(this.resetIsActive&&this.saveResetLimits(),this.zoom(1/this.zoomFactor,this.parent.mouseX,this.parent.mouseY)))}},GPlot.prototype.preventDefaultEvent=function(a){e=a||window.event,this.isOverBox()&&(e.preventDefault?e.preventDefault():e.returnValue=!1)},GPlot.prototype.preventWheelDefault=function(){window.addEventListener("wheel",this.preventDefaultEvent.bind(this),!1)},GPlot.prototype.preventRightClickDefault=function(){window.addEventListener("contextmenu",this.preventDefaultEvent.bind(this),!1)};
|