-
Notifications
You must be signed in to change notification settings - Fork 2
/
sensatechart.min-test.js
1 lines (1 loc) · 3.36 KB
/
sensatechart.min-test.js
1
var liveUpdateChartMap=[],runningLiveUpdateScheduler=!1,sensorMap={},canvasSensorMap={},charts={},initChartJS=!1,apiLocation="https://test-api.sensate.io/v1/";async function initChart(e,t,a,n,r,s){for(console.log("initChart(canvasName, accessToken, listKey, liveUpdate, min, max)");!initChartJS;)await sleep(100);var i=[],o=0;n&&liveUpdateChartMap.push({canvas:e,accessToken:t,listKey:a}),loadJSON(apiLocation+"data/list?accessToken="+t+"&listKey="+a,function(t){Object.keys(t).forEach(function(a){for(var n=[],r=t[a],s=(r.shortName,r.name),l=(r.sensorType,r.dataUnit,"#"+r.graphColor),c=r.dataUnitShort,d=0;d<r.data.length;d++){var p=new Date(r.data[d].dateTime);n[d]={x:p,y:r.data[d].value}}i[o]={label:s,data:n,fill:!1,lineTension:0,borderColor:l,backgroundColor:l,unit:c},sensorMap[e+a]=o,o++}),drawChart(e,i,r,s,n)},function(e){console.error(e)})}function drawChart(e,t,a,n,r){var s,i=document.getElementById(e).getContext("2d");canvasSensorMap[e]=t,s=null!=a&&null!=n?{max:n,min:a}:{};var o=new Chart(i,{type:"lineSensate",tension:0,data:{datasets:t},options:{bezierCurve:!0,responsive:!0,title:{display:!1,text:"Sensate Sensor Chart"},tooltips:{mode:"label",enabled:!0,intersect:!1,position:"nearest",callbacks:{label:function(e,a){for(var n=0;n<t.length;n++)if(e.datasetIndex===n){var r=t[n].label,s=t[n].unit;return" "+r+": "+e.yLabel+s}}}},hover:{mode:"nearest",intersect:!0},scales:{xAxes:[{type:"time",distribution:"linear",time:{tooltipFormat:"ll HH:mm:ss",displayFormats:{millisecond:"HH:mm:SS:sss",second:"HH:mm:SS",minute:"HH:mm",hour:"HH",day:"MMM DD",week:"MMM DD",month:"MMM",quarter:"MMM DD",year:"DD.MM.YYYY HH:mm:ss"}}}],yAxes:[{display:!0,scaleLabel:{display:!0,labelString:"Wert"},ticks:s}]}}});charts[e]=o,!runningLiveUpdateScheduler&&r&&(runningLiveUpdateScheduler=!0,setInterval(updateChart,3e4))}function updateChart(){console.log(liveUpdateChartMap),Object.keys(liveUpdateChartMap).forEach(function(e){var t=liveUpdateChartMap[e].accessToken,a=liveUpdateChartMap[e].listKey,n=liveUpdateChartMap[e].canvas;loadJSON(apiLocation+"data/live/list?accessToken="+t+"&listKey="+a,function(e){Object.keys(e).forEach(function(t){var a=e[t].lastData,r={x:a.dateTime,y:a.value};canvasSensorMap[n][sensorMap[n+t]].data.push(r),charts[n].update()})},function(e){console.error(e)})})}function loadJSON(e,t,a){var n=new XMLHttpRequest;n.onreadystatechange=function(){n.readyState===XMLHttpRequest.DONE&&(200===n.status?t&&t(JSON.parse(n.responseText)):a&&a(n))},n.open("GET",e,!0),n.send()}function loadScript(e,t){var a=document.createElement("script");a.type="text/javascript",a.readyState?a.onreadystatechange=function(){"loaded"!=a.readyState&&"complete"!=a.readyState||(a.onreadystatechange=null,t())}:a.onload=function(){t()},a.src=e,document.getElementsByTagName("head")[0].appendChild(a)}function sleep(e){return new Promise(t=>setTimeout(t,e))}loadScript("https://www.chartjs.org/dist/2.9.3/Chart.bundle.min.js",function(){Chart.defaults.lineSensate=Chart.defaults.line,Chart.controllers.lineSensate=Chart.controllers.line.extend({draw:function(e){Chart.controllers.pie.prototype.draw.call(this,e);var t=this.chart.ctx;this.labelIconImage&&t.drawImage(this.labelIconImage,this.chart.width-186,0,186,30)},initialize:function(e,t){Chart.controllers.pie.prototype.initialize.call(this,e,t);var a=new Image;a.onload=(()=>{this.labelIconImage=a,e.render()}),a.src="https://www.sensate.io/res/chart/pbs.png"}}),initChartJS=!0});