UNPKG

pxt-core

Version:

Microsoft MakeCode provides Blocks / JavaScript / Python tools and editors

27 lines • 15.2 kB
!function(t){ /** * @license * MIT License: * * Copyright (c) 2010-2013, Joe Walnes * 2013-2018, Drew Noakes * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ Date.now=Date.now||function(){return(new Date).getTime()};var e={extend:function(){arguments[0]=arguments[0]||{};for(var t=1;t<arguments.length;t++)for(var i in arguments[t])arguments[t].hasOwnProperty(i)&&("object"==typeof arguments[t][i]?arguments[t][i]instanceof Array?arguments[0][i]=arguments[t][i]:arguments[0][i]=e.extend(arguments[0][i],arguments[t][i]):arguments[0][i]=arguments[t][i]);return arguments[0]},binarySearch:function(t,e){for(var i=0,s=t.length;i<s;){var a=i+s>>1;e<t[a][0]?s=a:i=a+1}return i},pixelSnap:function(t,e){return e%2==0?Math.round(t):Math.floor(t)+.5}};function i(t){this.options=e.extend({},i.defaultOptions,t),this.disabled=!1,this.clear()}function s(t){this.options=e.extend({},s.defaultChartOptions,t),this.seriesSet=[],this.currentValueRange=1,this.currentVisMinValue=0,this.lastRenderTimeMillis=0,this.lastChartTimestamp=0,this.mousemove=this.mousemove.bind(this),this.mouseout=this.mouseout.bind(this)}i.defaultOptions={resetBoundsInterval:3e3,resetBounds:!0},i.prototype.clear=function(){this.data=[],this.maxValue=Number.NaN,this.minValue=Number.NaN},i.prototype.resetBounds=function(){if(this.data.length){this.maxValue=this.data[0][1],this.minValue=this.data[0][1];for(var t=1;t<this.data.length;t++){var e=this.data[t][1];e>this.maxValue&&(this.maxValue=e),e<this.minValue&&(this.minValue=e)}}else this.maxValue=Number.NaN,this.minValue=Number.NaN},i.prototype.append=function(t,e,i){if(!isNaN(t)&&!isNaN(e)){var s=this.data.length-1;if(s>=0)for(var a=s;;){var n=this.data[a];if(t>=n[0]){t===n[0]?i?(n[1]+=e,e=n[1]):n[1]=e:this.data.splice(a+1,0,[t,e]);break}if(--a<0){this.data.splice(0,0,[t,e]);break}}else this.data.push([t,e]);this.maxValue=isNaN(this.maxValue)?e:Math.max(this.maxValue,e),this.minValue=isNaN(this.minValue)?e:Math.min(this.minValue,e)}},i.prototype.dropOldData=function(t,e){for(var i=0;this.data.length-i>=e&&this.data[i+1][0]<t;)i++;0!==i&&this.data.splice(0,i)},s.tooltipFormatter=function(t,e){var i,a=this.options.timestampFormatter||s.timeFormatter,n=document.createElement("div");n.appendChild(document.createTextNode(a(new Date(t))));for(var o=0;o<e.length;++o){""!==(i=e[o].series.options.tooltipLabel||"")&&(i+=" ");var r=document.createElement("span");r.style.color=e[o].series.options.strokeStyle,r.appendChild(document.createTextNode(i+this.options.yMaxFormatter(e[o].value,this.options.labels.precision))),n.appendChild(document.createElement("br")),n.appendChild(r)}return n.innerHTML},s.defaultChartOptions={millisPerPixel:20,enableDpiScaling:!0,yMinFormatter:function(t,e){return parseFloat(t).toFixed(e)},yMaxFormatter:function(t,e){return parseFloat(t).toFixed(e)},yIntermediateFormatter:function(t,e){return parseFloat(t).toFixed(e)},maxValueScale:1,minValueScale:1,interpolation:"bezier",scaleSmoothing:.125,maxDataSetLength:2,scrollBackwards:!1,displayDataFromPercentile:1,grid:{fillStyle:"#000000",strokeStyle:"#777777",lineWidth:2,millisPerLine:1e3,verticalSections:2,borderVisible:!0},labels:{fillStyle:"#ffffff",disabled:!1,fontSize:10,fontFamily:"monospace",precision:2,showIntermediateLabels:!1,intermediateLabelSameAxis:!0},title:{text:"",fillStyle:"#ffffff",fontSize:15,fontFamily:"monospace",verticalAlign:"middle"},horizontalLines:[],tooltip:!1,tooltipLine:{lineWidth:1,strokeStyle:"#BBBBBB"},tooltipFormatter:s.tooltipFormatter,nonRealtimeData:!1,responsive:!1,limitFPS:0},s.AnimateCompatibility={requestAnimationFrame:function(t,e){var i=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(t){return window.setTimeout(function(){t(Date.now())},16)};return i.call(window,t,e)},cancelAnimationFrame:function(t){var e=window.cancelAnimationFrame||function(t){clearTimeout(t)};return e.call(window,t)}},s.defaultSeriesPresentationOptions={lineWidth:1,strokeStyle:"#ffffff"},s.prototype.addTimeSeries=function(t,i){this.seriesSet.push({timeSeries:t,options:e.extend({},s.defaultSeriesPresentationOptions,i)}),t.options.resetBounds&&t.options.resetBoundsInterval>0&&(t.resetBoundsTimerId=setInterval(function(){t.resetBounds()},t.options.resetBoundsInterval))},s.prototype.removeTimeSeries=function(t){for(var e=this.seriesSet.length,i=0;i<e;i++)if(this.seriesSet[i].timeSeries===t){this.seriesSet.splice(i,1);break}t.resetBoundsTimerId&&clearInterval(t.resetBoundsTimerId)},s.prototype.getTimeSeriesOptions=function(t){for(var e=this.seriesSet.length,i=0;i<e;i++)if(this.seriesSet[i].timeSeries===t)return this.seriesSet[i].options},s.prototype.bringToFront=function(t){for(var e=this.seriesSet.length,i=0;i<e;i++)if(this.seriesSet[i].timeSeries===t){var s=this.seriesSet.splice(i,1);this.seriesSet.push(s[0]);break}},s.prototype.streamTo=function(t,e){this.canvas=t,this.clientWidth=parseInt(this.canvas.getAttribute("width")),this.clientHeight=parseInt(this.canvas.getAttribute("height")),this.delay=e,this.start()},s.prototype.getTooltipEl=function(){return this.tooltipEl||(this.tooltipEl=document.createElement("div"),this.tooltipEl.className="smoothie-chart-tooltip",this.tooltipEl.style.pointerEvents="none",this.tooltipEl.style.position="absolute",this.tooltipEl.style.display="none",document.body.appendChild(this.tooltipEl)),this.tooltipEl},s.prototype.updateTooltip=function(){if(this.options.tooltip){var t=this.getTooltipEl();if(this.mouseover&&this.options.tooltip){for(var i=this.lastChartTimestamp,s=this.options.scrollBackwards?i-this.mouseX*this.options.millisPerPixel:i-(this.clientWidth-this.mouseX)*this.options.millisPerPixel,a=[],n=0;n<this.seriesSet.length;n++){var o=this.seriesSet[n].timeSeries;if(!o.disabled){var r=e.binarySearch(o.data,s);r>0&&r<o.data.length&&a.push({series:this.seriesSet[n],index:r,value:o.data[r][1]})}}a.length?(t.innerHTML=this.options.tooltipFormatter.call(this,s,a),t.style.display="block"):t.style.display="none"}else t.style.display="none"}},s.prototype.mousemove=function(t){if(this.mouseover=!0,this.mouseX=t.offsetX,this.mouseY=t.offsetY,this.mousePageX=t.pageX,this.mousePageY=t.pageY,this.options.tooltip){var e=this.getTooltipEl();e.style.top=Math.round(this.mousePageY)+"px",e.style.left=Math.round(this.mousePageX)+"px",this.updateTooltip()}},s.prototype.mouseout=function(){this.mouseover=!1,this.mouseX=this.mouseY=-1,this.tooltipEl&&(this.tooltipEl.style.display="none")},s.prototype.resize=function(){var t,e,i=this.options.enableDpiScaling&&window?window.devicePixelRatio:1;this.options.responsive?(t=this.canvas.offsetWidth,e=this.canvas.offsetHeight,t!==this.lastWidth&&(this.lastWidth=t,this.canvas.setAttribute("width",Math.floor(t*i).toString()),this.canvas.getContext("2d").scale(i,i)),e!==this.lastHeight&&(this.lastHeight=e,this.canvas.setAttribute("height",Math.floor(e*i).toString()),this.canvas.getContext("2d").scale(i,i)),this.clientWidth=t,this.clientHeight=e):(t=parseInt(this.canvas.getAttribute("width")),e=parseInt(this.canvas.getAttribute("height")),1!==i?(Math.floor(this.clientWidth*i)!==t&&(this.canvas.setAttribute("width",Math.floor(t*i).toString()),this.canvas.style.width=t+"px",this.clientWidth=t,this.canvas.getContext("2d").scale(i,i)),Math.floor(this.clientHeight*i)!==e&&(this.canvas.setAttribute("height",Math.floor(e*i).toString()),this.canvas.style.height=e+"px",this.clientHeight=e,this.canvas.getContext("2d").scale(i,i))):(this.clientWidth=t,this.clientHeight=e))},s.prototype.start=function(){if(!this.frame){this.canvas.addEventListener("mousemove",this.mousemove),this.canvas.addEventListener("mouseout",this.mouseout);var t=function(){this.frame=s.AnimateCompatibility.requestAnimationFrame(function(){if(this.options.nonRealtimeData){var e=new Date(0),i=this.seriesSet.reduce(function(t,e){var i=e.timeSeries.data,s=Math.round(this.options.displayDataFromPercentile*i.length)-1;if(s=(s=s>=0?s:0)<=i.length-1?s:i.length-1,i&&i.length>0){var a=i[s][0];t=t>a?t:a}return t}.bind(this),e);this.render(this.canvas,i>e?i:null)}else this.render();t()}.bind(this))}.bind(this);t()}},s.prototype.stop=function(){this.frame&&(s.AnimateCompatibility.cancelAnimationFrame(this.frame),delete this.frame,this.canvas.removeEventListener("mousemove",this.mousemove),this.canvas.removeEventListener("mouseout",this.mouseout))},s.prototype.updateValueRange=function(){for(var t=this.options,e=Number.NaN,i=Number.NaN,s=0;s<this.seriesSet.length;s++){var a=this.seriesSet[s].timeSeries;a.disabled||(isNaN(a.maxValue)||(e=isNaN(e)?a.maxValue:Math.max(e,a.maxValue)),isNaN(a.minValue)||(i=isNaN(i)?a.minValue:Math.min(i,a.minValue)))}if(null!=t.maxValue?e=t.maxValue:e*=t.maxValueScale,null!=t.minValue?i=t.minValue:i-=Math.abs(i*t.minValueScale-i),this.options.yRangeFunction){var n=this.options.yRangeFunction({min:i,max:e});i=n.min,e=n.max}if(!isNaN(e)&&!isNaN(i)){var o=e-i-this.currentValueRange,r=i-this.currentVisMinValue;this.isAnimatingScale=Math.abs(o)>.1||Math.abs(r)>.1,this.currentValueRange+=t.scaleSmoothing*o,this.currentVisMinValue+=t.scaleSmoothing*r}this.valueRange={min:i,max:e}},s.prototype.render=function(t,i){var s=Date.now();if(!(this.options.limitFPS>0&&s-this.lastRenderTimeMillis<1e3/this.options.limitFPS)){if(i=(i||s)-(this.delay||0),i-=i%this.options.millisPerPixel,!this.isAnimatingScale)if(this.lastChartTimestamp===i)if(!(s-this.lastRenderTimeMillis>1e3/6))return;this.lastRenderTimeMillis=s,this.lastChartTimestamp=i,this.resize();var a=(t=t||this.canvas).getContext("2d"),n=this.options,o={top:0,left:0,width:this.clientWidth,height:this.clientHeight},r=i-o.width*n.millisPerPixel,l=function(t,i){var s=t-this.currentVisMinValue,a=0===this.currentValueRange?o.height:o.height*(1-s/this.currentValueRange);return e.pixelSnap(a,i)}.bind(this),h=function(t,s){var a=n.scrollBackwards?(i-t)/n.millisPerPixel:o.width-(i-t)/n.millisPerPixel;return e.pixelSnap(a,s)};if(this.updateValueRange(),a.font=n.labels.fontSize+"px "+n.labels.fontFamily,a.save(),a.translate(o.left,o.top),a.beginPath(),a.rect(0,0,o.width,o.height),a.clip(),a.save(),a.fillStyle=n.grid.fillStyle,a.clearRect(0,0,o.width,o.height),a.fillRect(0,0,o.width,o.height),a.restore(),a.save(),a.lineWidth=n.grid.lineWidth,a.strokeStyle=n.grid.strokeStyle,n.grid.millisPerLine>0){a.beginPath();for(var m=i-i%n.grid.millisPerLine;m>=r;m-=n.grid.millisPerLine){var d=h(m,n.grid.lineWidth);a.moveTo(d,0),a.lineTo(d,o.height)}a.stroke(),a.closePath()}for(var u=1;u<n.grid.verticalSections;u++){var c=e.pixelSnap(u*o.height/n.grid.verticalSections,n.grid.lineWidth);a.beginPath(),a.moveTo(0,c),a.lineTo(o.width,c),a.stroke(),a.closePath()}if(n.grid.borderVisible&&(a.beginPath(),a.strokeRect(0,0,o.width,o.height),a.closePath()),a.restore(),n.horizontalLines&&n.horizontalLines.length)for(var p=0;p<n.horizontalLines.length;p++){var f=n.horizontalLines[p],g=f.lineWidth||1,v=l(f.value,g);a.strokeStyle=f.color||"#ffffff",a.lineWidth=g,a.beginPath(),a.moveTo(0,v),a.lineTo(o.width,v),a.stroke(),a.closePath()}for(var S=0;S<this.seriesSet.length;S++){var y=this.seriesSet[S].timeSeries,x=y.data;if(y.dropOldData(r,n.maxDataSetLength),!(x.length<=1||y.disabled)){a.save();var b=this.seriesSet[S].options,w=b.strokeStyle&&"none"!==b.strokeStyle,T=w?b.lineWidth:0;a.beginPath();var N,V=h(x[0][0],T),P=l(x[0][1],T),F=V,k=P;switch(a.moveTo(V,P),b.interpolation||n.interpolation){case"linear":case"line":N=function(t,e,i,s){a.lineTo(t,e)};break;case"bezier":default:N=function(t,e,i,s){a.bezierCurveTo(Math.round((i+t)/2),s,Math.round(i+t)/2,e,t,e)};break;case"step":N=function(t,e,i,s){a.lineTo(t,s),a.lineTo(t,e)}}for(var M=1;M<x.length;M++){var R=x[M],B=h(R[0],T),A=l(R[1],T);N(B,A,F,k),F=B,k=A}w&&(a.lineWidth=b.lineWidth,a.strokeStyle=b.strokeStyle,a.stroke()),b.fillStyle&&(a.lineTo(F,o.height+T+1),a.lineTo(V,o.height+T+1),a.fillStyle=b.fillStyle,a.fill()),a.restore()}}n.tooltip&&this.mouseX>=0&&(a.lineWidth=n.tooltipLine.lineWidth,a.strokeStyle=n.tooltipLine.strokeStyle,a.beginPath(),a.moveTo(this.mouseX,0),a.lineTo(this.mouseX,o.height),a.closePath(),a.stroke()),this.updateTooltip();var L=n.labels;if(!L.disabled&&!isNaN(this.valueRange.min)&&!isNaN(this.valueRange.max)){var W=n.yMaxFormatter(this.valueRange.max,L.precision),E=n.yMinFormatter(this.valueRange.min,L.precision),C=n.scrollBackwards?0:o.width-a.measureText(W).width-2,D=n.scrollBackwards?0:o.width-a.measureText(E).width-2;a.fillStyle=L.fillStyle,a.fillText(W,C,L.fontSize),a.fillText(E,D,o.height-2)}if(L.showIntermediateLabels&&!isNaN(this.valueRange.min)&&!isNaN(this.valueRange.max)&&n.grid.verticalSections>0){var z=(this.valueRange.max-this.valueRange.min)/n.grid.verticalSections,I=o.height/n.grid.verticalSections;for(u=1;u<n.grid.verticalSections;u++){c=o.height-Math.round(u*I);var H=n.yIntermediateFormatter(this.valueRange.min+u*z,L.precision),X=L.intermediateLabelSameAxis?n.scrollBackwards?0:o.width-a.measureText(H).width-2:n.scrollBackwards?o.width-a.measureText(H).width-2:0;a.fillText(H,X,c-n.grid.lineWidth)}}if(n.timestampFormatter&&n.grid.millisPerLine>0){var O=n.scrollBackwards?a.measureText(E).width:o.width-a.measureText(E).width+4;for(m=i-i%n.grid.millisPerLine;m>=r;m-=n.grid.millisPerLine){d=h(m,0);if(!n.scrollBackwards&&d<O||n.scrollBackwards&&d>O){var q=new Date(m),Y=n.timestampFormatter(q),j=a.measureText(Y).width;O=n.scrollBackwards?d+j+2:d-j-2,a.fillStyle=n.labels.fillStyle,n.scrollBackwards?a.fillText(Y,d,o.height-2):a.fillText(Y,d-j,o.height-2)}}}if(""!==n.title.text){a.font=n.title.fontSize+"px "+n.title.fontFamily;var G=n.scrollBackwards?o.width-a.measureText(n.title.text).width-2:2;if("bottom"==n.title.verticalAlign){a.textBaseline="bottom";var J=o.height}else if("middle"==n.title.verticalAlign){a.textBaseline="middle";J=o.height/2}else{a.textBaseline="top";J=0}a.fillStyle=n.title.fillStyle,a.fillText(n.title.text,G,J)}a.restore()}},s.timeFormatter=function(t){function e(t){return(t<10?"0":"")+t}return e(t.getHours())+":"+e(t.getMinutes())+":"+e(t.getSeconds())},t.TimeSeries=i,t.SmoothieChart=s}("undefined"==typeof exports?this:exports);