-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.min.js
7 lines (7 loc) · 3.05 KB
/
plot.min.js
1
2
3
4
5
6
7
window.jsPlot=function(f,j,k){var i={extend:k,createCanvas:function(b,a){var d=j.getElementById(b),c=d.getElementsByTagName("canvas")[0];c||(c=j.createElement("canvas"),d.appendChild(c));c.width=a.canvasWidth;c.height=a.canvasHeight;return c.getContext("2d")},findNiceRoundStep:function(){var b=Math.log(10);return function(a,d){var c=a/d,e=Math.floor(Math.log(c)/b),g=Math.pow(10,e),c=c/g;1.5>c?c=1:3.5>c?c=2:7.5>c?c=5:(g=Math.pow(10,e+1),c=1);return c*g}}(),drawAxes:function(b,a){b.save();b.strokeStyle=
"#888";b.beginPath();b.moveTo(a.Xmin*a.xscale,0);b.lineTo(a.Xmax*a.xscale,0);b.moveTo(0,a.Ymin*a.yscale);b.lineTo(0,a.Ymax*a.yscale);b.stroke();b.fillStyle="black";b.beginPath();b.moveTo(a.Xmax*a.xscale,0);b.lineTo(a.Xmax*a.xscale-10,6);b.lineTo(a.Xmax*a.xscale-10,-6);b.lineTo(a.Xmax*a.xscale,0);b.moveTo(0,a.Ymax*a.yscale);b.lineTo(-6,a.Ymax*a.yscale-10);b.lineTo(6,a.Ymax*a.yscale-10);b.lineTo(0,a.Ymax*a.yscale);b.fill();b.scale(-1,1);b.font="13px helvetica";b.textAlign="end";b.textBaseline="bottom";
b.rotate(Math.PI);b.save();b.translate(a.Xmax*a.xscale-13,-2);b.fillText(a.xLabel,0,0);b.restore();b.rotate(-Math.PI/2);b.translate(a.Ymax*a.yscale-13,-2);b.fillText(a.yLabel,0,0);b.restore()},drawGrid:function(b,a){b.save();b.font="15px helvetica";b.strokeStyle="#CCF";b.lineWidth=1;b.beginPath();for(var d=i.findNiceRoundStep(a.Xmax-a.Xmin,15),c=a.Xmin-a.Xmin%d;c<a.Xmax;c+=d)b.moveTo(c*a.xscale,a.Ymin*a.yscale),b.lineTo(c*a.xscale,a.Ymax*a.yscale);b.stroke();b.beginPath();for(c=a.Ymin-a.Ymin%d;c<
a.Ymax;c+=d)b.moveTo(a.Xmin*a.xscale,c*a.yscale),b.lineTo(a.Xmax*a.xscale,c*a.yscale);b.stroke();b.save();b.rotate(-Math.PI);b.scale(-1,1);for(c=a.Xmin-a.Xmin%(2*d);c<a.Xmax;c+=2*d)b.fillText(Math.round(1E6*c)/1E6,c*a.xscale,0);b.rotate(-Math.PI/2);for(c=a.Ymin-a.Ymin%(2*d);c<a.Ymax;c+=2*d){var e=Math.round(1E6*c)/1E6;0!==e&&b.fillText(e,c*a.yscale,0)}b.restore();b.restore()},drawFunction:function(b,a,d){try{var c=a.Xmin*a.xscale,e=a.Xmax*a.xscale,g=b.strokeStyle,f=d.color?d.color:"#000";b.lineWidth=
d.width?d.width:1;b.strokeStyle=f;b.moveTo(c,d(c));for(b.beginPath();c<=e;c++){var h=d(c/a.xscale),h=h<a.Ymin?a.Ymin-1:h,h=h>a.Ymax?a.Ymax+1:h;b.lineTo(c,h*a.yscale)}b.stroke()}catch(l){console.log("fonction en erreur",d,l)}finally{b.stokeStyle=g}}},m={Xmin:0,Xmax:10,Ymin:0,Ymax:3,xLabel:"x",yLabel:"y",canvasHeight:500,canvasWidth:500,gridDensity:0,gridVisible:!0},f=function(b,a,d){var a=i.extend({},m,a),c=a.X=a.Xmax-a.Xmin,e=a.Y=a.Ymax-a.Ymin,c=a.xscale=a.canvasWidth/c,e=a.yscale=a.canvasHeight/
e,b=i.createCanvas(b,a);b.scale(1,-1);b.translate(0,-a.canvasHeight);b.translate(-(a.Xmin*c),-(a.Ymin*e));a.gridVisible&&i.drawGrid(b,a);i.drawAxes(b,a);for(c=0;c<d.length;c++)i.drawFunction(b,a,d[c])};f.tools={datasetToFunc:function(b){return b.reduce(function(a,b,c,e){c=e[c+1];if(void 0===c)return function(c){if(!(c>b[0]))return a(c)};var g=(c[1]-b[1])/(c[0]-b[0]),f=b[1]-g*b[0],h=b[0];console.log(g+"x + "+f);return function(b){return b>=h?g*b+f:a(b)}},function(){})}};return f}(window,document,function(f){[].slice.call(arguments,
1).forEach(function(j){for(key in j)f[key]=j[key]});return f});