mirror of
https://github.com/YunoHost-Apps/linuxdash_ynh.git
synced 2024-09-03 19:36:07 +02:00
1 line
8.8 KiB
JavaScript
1 line
8.8 KiB
JavaScript
(function(e){function n(e){this.options=t.extend({},n.defaultOptions,e);this.clear()}function r(e){this.options=t.extend({},r.defaultChartOptions,e);this.seriesSet=[];this.currentValueRange=1;this.currentVisMinValue=0;this.lastRenderTimeMillis=0}var t={extend:function(){arguments[0]=arguments[0]||{};for(var e=1;e<arguments.length;e++){for(var n in arguments[e]){if(arguments[e].hasOwnProperty(n)){if(typeof arguments[e][n]==="object"){if(arguments[e][n]instanceof Array){arguments[0][n]=arguments[e][n]}else{arguments[0][n]=t.extend(arguments[0][n],arguments[e][n])}}else{arguments[0][n]=arguments[e][n]}}}}return arguments[0]}};n.defaultOptions={resetBoundsInterval:3e3,resetBounds:true};n.prototype.clear=function(){this.data=[];this.maxValue=Number.NaN;this.minValue=Number.NaN};n.prototype.resetBounds=function(){if(this.data.length){this.maxValue=this.data[0][1];this.minValue=this.data[0][1];for(var e=1;e<this.data.length;e++){var t=this.data[e][1];if(t>this.maxValue){this.maxValue=t}if(t<this.minValue){this.minValue=t}}}else{this.maxValue=Number.NaN;this.minValue=Number.NaN}};n.prototype.append=function(e,t,n){var r=this.data.length-1;while(r>=0&&this.data[r][0]>e){r--}if(r===-1){this.data.splice(0,0,[e,t])}else if(this.data.length>0&&this.data[r][0]===e){if(n){this.data[r][1]+=t;t=this.data[r][1]}else{this.data[r][1]=t}}else if(r<this.data.length-1){this.data.splice(r+1,0,[e,t])}else{this.data.push([e,t])}this.maxValue=isNaN(this.maxValue)?t:Math.max(this.maxValue,t);this.minValue=isNaN(this.minValue)?t:Math.min(this.minValue,t)};n.prototype.dropOldData=function(e,t){var n=0;while(this.data.length-n>=t&&this.data[n+1][0]<e){n++}if(n!==0){this.data.splice(0,n)}};r.defaultChartOptions={millisPerPixel:20,enableDpiScaling:true,yMinFormatter:function(e,t){return parseFloat(e).toFixed(t)},yMaxFormatter:function(e,t){return parseFloat(e).toFixed(t)},maxValueScale:1,minValueScale:1,interpolation:"bezier",scaleSmoothing:.125,maxDataSetLength:2,grid:{fillStyle:"#000000",strokeStyle:"#777777",lineWidth:1,sharpLines:false,millisPerLine:1e3,verticalSections:2,borderVisible:true},labels:{fillStyle:"#ffffff",disabled:false,fontSize:10,fontFamily:"monospace",precision:2},horizontalLines:[]};r.AnimateCompatibility=function(){var e=function(e,t){var n=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(e){return window.setTimeout(function(){e((new Date).getTime())},16)};return n.call(window,e,t)},t=function(e){var t=window.cancelAnimationFrame||function(e){clearTimeout(e)};return t.call(window,e)};return{requestAnimationFrame:e,cancelAnimationFrame:t}}();r.defaultSeriesPresentationOptions={lineWidth:1,strokeStyle:"#ffffff"};r.prototype.addTimeSeries=function(e,n){this.seriesSet.push({timeSeries:e,options:t.extend({},r.defaultSeriesPresentationOptions,n)});if(e.options.resetBounds&&e.options.resetBoundsInterval>0){e.resetBoundsTimerId=setInterval(function(){e.resetBounds()},e.options.resetBoundsInterval)}};r.prototype.removeTimeSeries=function(e){var t=this.seriesSet.length;for(var n=0;n<t;n++){if(this.seriesSet[n].timeSeries===e){this.seriesSet.splice(n,1);break}}if(e.resetBoundsTimerId){clearInterval(e.resetBoundsTimerId)}};r.prototype.getTimeSeriesOptions=function(e){var t=this.seriesSet.length;for(var n=0;n<t;n++){if(this.seriesSet[n].timeSeries===e){return this.seriesSet[n].options}}};r.prototype.bringToFront=function(e){var t=this.seriesSet.length;for(var n=0;n<t;n++){if(this.seriesSet[n].timeSeries===e){var r=this.seriesSet.splice(n,1);this.seriesSet.push(r[0]);break}}};r.prototype.streamTo=function(e,t){this.canvas=e;this.delay=t;this.start()};r.prototype.resize=function(){if(!this.options.enableDpiScaling||!window||window.devicePixelRatio===1)return;var e=window.devicePixelRatio;var t=parseInt(this.canvas.getAttribute("width"));var n=parseInt(this.canvas.getAttribute("height"));if(!this.originalWidth||Math.floor(this.originalWidth*e)!==t){this.originalWidth=t;this.canvas.setAttribute("width",Math.floor(t*e).toString());this.canvas.style.width=t+"px";this.canvas.getContext("2d").scale(e,e)}if(!this.originalHeight||Math.floor(this.originalHeight*e)!==n){this.originalHeight=n;this.canvas.setAttribute("height",Math.floor(n*e).toString());this.canvas.style.height=n+"px";this.canvas.getContext("2d").scale(e,e)}};r.prototype.start=function(){if(this.frame){return}var e=function(){this.frame=r.AnimateCompatibility.requestAnimationFrame(function(){this.render();e()}.bind(this))}.bind(this);e()};r.prototype.stop=function(){if(this.frame){r.AnimateCompatibility.cancelAnimationFrame(this.frame);delete this.frame}};r.prototype.updateValueRange=function(){var e=this.options,t=Number.NaN,n=Number.NaN;for(var r=0;r<this.seriesSet.length;r++){var i=this.seriesSet[r].timeSeries;if(!isNaN(i.maxValue)){t=!isNaN(t)?Math.max(t,i.maxValue):i.maxValue}if(!isNaN(i.minValue)){n=!isNaN(n)?Math.min(n,i.minValue):i.minValue}}if(e.maxValue!=null){t=e.maxValue}else{t*=e.maxValueScale}if(e.minValue!=null){n=e.minValue}else{n-=Math.abs(n*e.minValueScale-n)}if(this.options.yRangeFunction){var s=this.options.yRangeFunction({min:n,max:t});n=s.min;t=s.max}if(!isNaN(t)&&!isNaN(n)){var o=t-n;var u=o-this.currentValueRange;var a=n-this.currentVisMinValue;this.isAnimatingScale=Math.abs(u)>.1||Math.abs(a)>.1;this.currentValueRange+=e.scaleSmoothing*u;this.currentVisMinValue+=e.scaleSmoothing*a}this.valueRange={min:n,max:t}};r.prototype.render=function(e,t){var n=(new Date).getTime();if(!this.isAnimatingScale){var r=Math.min(1e3/6,this.options.millisPerPixel);if(n-this.lastRenderTimeMillis<r){return}}this.resize();this.lastRenderTimeMillis=n;e=e||this.canvas;t=t||n-(this.delay||0);t-=t%this.options.millisPerPixel;var i=e.getContext("2d"),s=this.options,o={top:0,left:0,width:e.clientWidth,height:e.clientHeight},u=t-o.width*s.millisPerPixel,a=function(e){var t=e-this.currentVisMinValue;return this.currentValueRange===0?o.height:o.height-Math.round(t/this.currentValueRange*o.height)}.bind(this),f=function(e){return Math.round(o.width-(t-e)/s.millisPerPixel)};this.updateValueRange();i.font=s.labels.fontSize+"px "+s.labels.fontFamily;i.save();i.translate(o.left,o.top);i.beginPath();i.rect(0,0,o.width,o.height);i.clip();i.save();i.fillStyle=s.grid.fillStyle;i.clearRect(0,0,o.width,o.height);i.fillRect(0,0,o.width,o.height);i.restore();i.save();i.lineWidth=s.grid.lineWidth;i.strokeStyle=s.grid.strokeStyle;if(s.grid.millisPerLine>0){i.beginPath();for(var l=t-t%s.grid.millisPerLine;l>=u;l-=s.grid.millisPerLine){var c=f(l);if(s.grid.sharpLines){c-=.5}i.moveTo(c,0);i.lineTo(c,o.height)}i.stroke();i.closePath()}for(var h=1;h<s.grid.verticalSections;h++){var p=Math.round(h*o.height/s.grid.verticalSections);if(s.grid.sharpLines){p-=.5}i.beginPath();i.moveTo(0,p);i.lineTo(o.width,p);i.stroke();i.closePath()}if(s.grid.borderVisible){i.beginPath();i.strokeRect(0,0,o.width,o.height);i.closePath()}i.restore();if(s.horizontalLines&&s.horizontalLines.length){for(var d=0;d<s.horizontalLines.length;d++){var v=s.horizontalLines[d],m=Math.round(a(v.value))-.5;i.strokeStyle=v.color||"#ffffff";i.lineWidth=v.lineWidth||1;i.beginPath();i.moveTo(0,m);i.lineTo(o.width,m);i.stroke();i.closePath()}}for(var g=0;g<this.seriesSet.length;g++){i.save();var y=this.seriesSet[g].timeSeries,b=y.data,w=this.seriesSet[g].options;y.dropOldData(u,s.maxDataSetLength);i.lineWidth=w.lineWidth;i.strokeStyle=w.strokeStyle;i.beginPath();var E=0,S=0,x=0;for(var T=0;T<b.length&&b.length!==1;T++){var N=f(b[T][0]),C=a(b[T][1]);if(T===0){E=N;i.moveTo(N,C)}else{switch(s.interpolation){case"linear":case"line":{i.lineTo(N,C);break};case"bezier":default:{i.bezierCurveTo(Math.round((S+N)/2),x,Math.round(S+N)/2,C,N,C);break};case"step":{i.lineTo(N,x);i.lineTo(N,C);break}}}S=N;x=C}if(b.length>1){if(w.fillStyle){i.lineTo(o.width+w.lineWidth+1,x);i.lineTo(o.width+w.lineWidth+1,o.height+w.lineWidth+1);i.lineTo(E,o.height+w.lineWidth);i.fillStyle=w.fillStyle;i.fill()}if(w.strokeStyle&&w.strokeStyle!=="none"){i.stroke()}i.closePath()}i.restore()}if(!s.labels.disabled&&!isNaN(this.valueRange.min)&&!isNaN(this.valueRange.max)){var k=s.yMaxFormatter(this.valueRange.max,s.labels.precision),L=s.yMinFormatter(this.valueRange.min,s.labels.precision);i.fillStyle=s.labels.fillStyle;i.fillText(k,o.width-i.measureText(k).width-2,s.labels.fontSize);i.fillText(L,o.width-i.measureText(L).width-2,o.height-2)}if(s.timestampFormatter&&s.grid.millisPerLine>0){var A=o.width-i.measureText(L).width+4;for(var l=t-t%s.grid.millisPerLine;l>=u;l-=s.grid.millisPerLine){var c=f(l);if(c<A){var O=new Date(l),M=s.timestampFormatter(O),_=i.measureText(M).width;A=c-_-2;i.fillStyle=s.labels.fillStyle;i.fillText(M,c-_,o.height-2)}}}i.restore()};r.timeFormatter=function(e){function t(e){return(e<10?"0":"")+e}return t(e.getHours())+":"+t(e.getMinutes())+":"+t(e.getSeconds())};e.TimeSeries=n;e.SmoothieChart=r})(typeof exports==="undefined"?this:exports)
|