admin_dashboard/static/plugins/chartjs-plugin-streaming/chartjs-plugin-streaming.mi...

11 lines
12 KiB
JavaScript

/*
* @license
* chartjs-plugin-streaming
* https://github.com/nagix/chartjs-plugin-streaming/
* Version: 1.7.0
*
* Copyright 2018 Akihiko Kusanagi
* Released under the MIT license
* https://github.com/nagix/chartjs-plugin-streaming/blob/master/LICENSE.md
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("chart.js"),require("moment")):"function"==typeof define&&define.amd?define(["chart.js","moment"],t):e["chartjs-plugin-streaming"]=t(e.Chart,e.moment)}(this,function(e,t){"use strict";!function(e,h){var w=e.helpers;w.cancelAnimFrame=function(){if("undefined"!=typeof window)return window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.oCancelAnimationFrame||window.msCancelAnimationFrame||function(e){return window.clearTimeout(e)}}();var v=Number.MAX_SAFE_INTEGER||9007199254740991,g={millisecond:{common:!0,size:1,steps:[1,2,5,10,20,50,100,250,500]},second:{common:!0,size:1e3,steps:[1,2,5,10,30]},minute:{common:!0,size:6e4,steps:[1,2,5,10,30]},hour:{common:!0,size:36e5,steps:[1,2,3,6,12]},day:{common:!0,size:864e5,steps:[1,2,5]},week:{common:!1,size:6048e5,steps:[1,2,3,4]},month:{common:!0,size:2628e6,steps:[1,2,3]},quarter:{common:!1,size:7884e6,steps:[1,2,3,4]},year:{common:!0,size:3154e7}},y=Object.keys(g);function x(e){for(var t=y.indexOf(e)+1,a=y.length;t<a;++t)if(g[y[t]].common)return y[t]}function u(e,t,a,n,i){var o,r=n.time,s=r.unit||function(e,t,a,n){var i,o,r,s=y.length;for(i=y.indexOf(e);i<s-1;++i)if(r=(o=g[y[i]]).steps?o.steps[o.steps.length-1]:v,o.common&&Math.ceil((a-t)/(r*o.size))<=n)return y[i];return y[s-1]}(r.minUnit,e,t,a),l=x(s),u=w.valueOrDefault(r.stepSize,r.unitStepSize),c="week"===s&&r.isoWeekday,m=g[s],p=h(e),d=h(t+i),f=[];for(u||(u=function(e,t,a,n){var i,o,r,s=t-e,l=g[a],u=l.size,c=l.steps;if(!c)return Math.ceil(s/(n*u));for(i=0,o=c.length;i<o&&(r=c[i],!(Math.ceil(s/(u*r))<=n));++i);return r}(e,t,s,a)),c&&(p=p.isoWeekday(c),d=d.isoWeekday(c)),p=p.startOf(c?"day":s),(d=d.startOf(c?"day":s))<t+i&&d.add(1,s),o=h(p),!l||c||r.round||(o.startOf(l),o.add(~~((p-o)/(m.size*u))*u,s));o<d;o.add(u,s))f.push(+o);return f.push(+o),f}function D(e,t){var a=e.options.realtime,n=e.chart.options.plugins.streaming;return w.valueOrDefault(a[t],n[t])}var b=["pointBackgroundColor","pointBorderColor","pointBorderWidth","pointRadius","pointStyle","pointHitRadius","pointHoverBackgroundColor","pointHoverBorderColor","pointHoverBorderWidth","pointHoverRadius","backgroundColor","borderColor","borderWidth","hoverBackgroundColor","hoverBorderColor","hoverBorderWidth","hoverRadius","hitRadius","radius"];function M(e){var t=e.realtime,a=t.refreshTimerID;a&&(clearInterval(a),delete t.refreshTimerID,delete t.refreshInterval)}function A(y){var x=y.realtime,e=D(y,"refresh");x.refreshTimerID=setInterval(function(){var a,n,i,o,r,s,l,u,c,m,e,t,p,d,f,h,v,g=D(y,"refresh");c=(a=y).chart,m=a.id,e=D(a,"duration"),t=D(a,"delay"),p=D(a,"ttl"),d=D(a,"pause"),f=D(a,"onRefresh"),h=a.getPixelForValue(a.max),v=a.getPixelForValue(Date.now()-(isNaN(p)?e+t:p)),f&&f(c),c.data.datasets.forEach(function(t,e){if(n=c.getDatasetMeta(e),m===n.xAxisID||m===n.yAxisID){if(i=t.data,o=i.length,d){for(r=0;r<o&&a.getPixelForValue(null,r,e)<h;++r);s=r+2}else s=0;for(r=s;r<o&&a.getPixelForValue(null,r,e)<=v;++r);l=r-s,isNaN(p)&&(l=Math.max(l-2,0)),i.splice(s,l),b.forEach(function(e){t.hasOwnProperty(e)&&w.isArray(t[e])&&t[e].splice(s,l)}),"object"!=typeof i[0]&&(u={start:s,count:l})}}),u&&c.data.labels.splice(u.start,u.count),c.update({preservation:!0}),x.refreshInterval===g||isNaN(g)||(M(y),A(y))},e),x.refreshInterval=e}var I={x:{data:["x","controlPointPreviousX","controlPointNextX"],dataset:["x"],tooltip:["x","caretX"]},y:{data:["y","controlPointPreviousY","controlPointNextY"],dataset:["y"],tooltip:["y","caretY"]}};function O(e,t,a){var n,i,o=e._start||{},r=e._view||{},s=e._model||{};for(n=0,i=t.length;n<i;++n){var l=t[n];o.hasOwnProperty(l)&&(o[l]-=a),r.hasOwnProperty(l)&&r!==o&&(r[l]-=a),s.hasOwnProperty(l)&&s!==r&&(s[l]-=a)}}function t(v){var g=v.realtime;if(!g.frameRequestID){var y=function(){var e,t,a,n,i,o,r,s,l,u,c,m,p,d,f,h;l=(e=v).chart,u=e.realtime,c=D(e,"duration"),m=D(e,"delay"),p=e.id,d=l.tooltip,f=d._active,h=Date.now(),a=e.isHorizontal()?(t=e.width,I.x):(t=e.height,I.y),n=t*(h-u.head)/c,w.each(l.data.datasets,function(e,t){if(i=l.getDatasetMeta(t),p===i.xAxisID||p===i.yAxisID){for(o=i.data||[],r=0,s=o.length;r<s;++r)O(o[r],a.data,n);i.dataset&&O(i.dataset,a.dataset,n)}}),f&&f[0]&&(i=l.getDatasetMeta(f[0]._datasetIndex),p!==i.xAxisID&&p!==i.yAxisID||O(d,a.tooltip,n)),e.max=e._table[1].time=h-m,e.min=e._table[0].time=e.max-c,u.head=h,g.frameRequestID=w.requestAnimFrame.call(window,y)};g.head=Date.now(),g.frameRequestID=w.requestAnimFrame.call(window,y)}}function a(e){var t=e.realtime,a=t.frameRequestID;a&&(w.cancelAnimFrame.call(window,a),delete t.frameRequestID)}var c=e.scaleService.getScaleConstructor("time");e.scaleService.getScaleConstructor=function(e){return"time"===e&&(e="realtime"),this.constructors.hasOwnProperty(e)?this.constructors[e]:void 0};var n=c.extend({initialize:function(){var e=this;c.prototype.initialize.apply(e,arguments),("time"!==e.options.type||e.chart.options.plugins.streaming)&&(e.realtime=e.realtime||{},t(e),A(e))},update:function(){var e=this;return("time"!==e.options.type||e.chart.options.plugins.streaming)&&(D(e,"pause")?a(e):(t(e),e.realtime.head=Date.now())),c.prototype.update.apply(e,arguments)},buildTicks:function(){var e=this,t=e.options;if("time"===t.type&&!e.chart.options.plugins.streaming)return c.prototype.buildTicks.apply(e,arguments);var a=t.time,n=D(e,"duration"),i=D(e,"delay"),o=D(e,"refresh"),r=e.realtime.head-i,s=r-n,l=[];switch(t.ticks.source){case"data":l=e._timestamps.data;break;case"labels":l=e._timestamps.labels;break;case"auto":default:l=u(s,r,e.getLabelCapacity(s),t,o)}return e.min=s,e.max=r,e._unit=a.unit||function(e,t,a,n){var i,o,r=h.duration(h(n).diff(h(a)));for(i=y.length-1;i>=y.indexOf(t);i--)if(o=y[i],g[o].common&&r.as(o)>=e.length)return o;return y[t?y.indexOf(t):0]}(l,a.minUnit,e.min,e.max),e._majorUnit=x(e._unit),e._table=[{time:s,pos:0},{time:r,pos:1}],e._offsets={left:0,right:0},e._labelFormat=function(e,t){var a,n,i,o,r,s,l,u=e.length;for(a=0;a<u;a++){if(o=e[a],l=s=void 0,s=(r=t).parser,l=r.parser||r.format,0!==(n="function"==typeof s?s(o):"string"==typeof o&&"string"==typeof l?h(o,l):(o instanceof h||(o=h(o)),o.isValid()?o:"function"==typeof l?l(o):o)).millisecond())return"MMM D, YYYY h:mm:ss.SSS a";0===n.second()&&0===n.minute()&&0===n.hour()||(i=!0)}return i?"MMM D, YYYY h:mm:ss a":"MMM D, YYYY"}(e._timestamps.data,a),function(e,t){var a,n,i,o,r=[];for(a=0,n=e.length;a<n;++a)i=e[a],o=!!t&&i===+h(i).startOf(t),r.push({value:i,major:o});return r}(l,e._majorUnit)},fit:function(){var e=this,t=e.options;c.prototype.fit.apply(e,arguments),("time"!==t.type||e.chart.options.plugins.streaming)&&t.ticks.display&&t.display&&e.isHorizontal()&&(e.paddingLeft=3,e.paddingRight=3,e.handleMargins())},draw:function(e){var t=this,a=t.chart;if("time"!==t.options.type||a.options.plugins.streaming){var n=t.ctx,i=t.isHorizontal()?{left:e.left,top:0,right:e.right,bottom:a.height}:{left:0,top:e.top,right:a.width,bottom:e.bottom};w.canvas.clipArea(n,i),c.prototype.draw.apply(t,arguments),w.canvas.unclipArea(n)}else c.prototype.draw.apply(t,arguments)},destroy:function(){("time"!==this.options.type||this.chart.options.plugins.streaming)&&(a(this),M(this))}});e.scaleService.registerScaleType("realtime",n,{position:"bottom",distribution:"linear",bounds:"data",time:{parser:!1,format:!1,unit:!1,round:!1,displayFormat:!1,isoWeekday:!1,minUnit:"millisecond",displayFormats:{millisecond:"h:mm:ss.SSS a",second:"h:mm:ss a",minute:"h:mm a",hour:"hA",day:"MMM D",week:"ll",month:"MMM YYYY",quarter:"[Q]Q - YYYY",year:"YYYY"}},realtime:{},ticks:{autoSkip:!1,source:"auto",major:{enabled:!0}}})}(e=e&&e.hasOwnProperty("default")?e.default:e,t=t&&t.hasOwnProperty("default")?t.default:t);var a=function(o){var s=o.helpers;o.defaults.global.plugins.streaming={duration:1e4,delay:0,frameRate:30,refresh:1e3,onRefresh:null,pause:!1,ttl:void 0};var r=o.scaleService.getScaleConstructor("realtime");function l(e){var t,a=e.streaming.lastMouseEvent;a&&("function"==typeof MouseEvent?t=new MouseEvent("mousemove",a):(t=document.createEvent("MouseEvents")).initMouseEvent("mousemove",a.bubbles,a.cancelable,a.view,a.detail,a.screenX,a.screenY,a.clientX,a.clientY,a.ctrlKey,a.altKey,a.shiftKey,a.metaKey,a.button,a.relatedTarget),e.canvas.dispatchEvent(t))}var u=o.prototype.update;function n(n){var i=n.streaming;if(!i.frameRequestID){var o=0,r=function(){var e=n.options.plugins.streaming.frameRate,t=1e3/(Math.max(e,0)||30),a=Date.now();o+t<=a&&(n.animating||n.tooltip._start||n.draw(),l(n),(o+=t)+t<=a&&(o=a)),i.frameRequestID=s.requestAnimFrame.call(window,r)};i.frameRequestID=s.requestAnimFrame.call(window,r)}}function i(e){var t=e.streaming,a=t.frameRequestID;a&&(s.cancelAnimFrame.call(window,a),delete t.frameRequestID)}return o.prototype.update=function(e){var a,t,n,i;e&&e.preservation?(t=(a=this).options.animation,n=a.data.datasets,i=a.buildOrUpdateControllers(),n.forEach(function(e,t){a.getDatasetMeta(t).controller.buildOrUpdateElements()}),a.updateLayout(),t&&t.duration&&s.each(i,function(e){e.reset()}),a.updateDatasets(),a.animating?o.animationService.animations.forEach(function(e){e.chart===a&&a.render({duration:16.66*(e.numSteps-e.currentStep)})}):n.forEach(function(e,t){a.getDatasetMeta(t).controller.transition(1)}),a.tooltip._active&&a.tooltip.update(!0),l(a)):u.apply(this,arguments)},{id:"streaming",beforeInit:function(e){var t=e.streaming=e.streaming||{},a=t.canvas=e.canvas,n=t.mouseEventListener=function(e){t.lastMouseEvent=e};a.addEventListener("mousedown",n),a.addEventListener("mouseup",n)},afterInit:function(e){e.resetZoom&&o.Zoom.updateResetZoom(e),n(e)},beforeUpdate:function(e){var t=e.options,a=t.scales;return a&&a.xAxes.concat(a.yAxes).forEach(function(e){"realtime"!==e.type&&"time"!==e.type||(t.elements.line.capBezierPoints=!1)}),!0},afterUpdate:function(e,t){var a=!0;s.each(e.scales,function(e){e instanceof r&&(a&=s.valueOrDefault(e.options.realtime.pause,t.pause))}),a?i(e):n(e)},beforeDatasetDraw:function(e,t){var a=t.meta,n=e.chartArea,i={left:0,top:0,right:e.width,bottom:e.height};return a.xAxisID&&a.controller.getScaleForId(a.xAxisID)instanceof r&&(i.left=n.left,i.right=n.right),a.yAxisID&&a.controller.getScaleForId(a.yAxisID)instanceof r&&(i.top=n.top,i.bottom=n.bottom),s.canvas.clipArea(e.ctx,i),!0},afterDatasetDraw:function(e){s.canvas.unclipArea(e.ctx)},beforeEvent:function(e,t){var a=e.streaming;return"mousemove"===t.type?a.lastMouseEvent=t.native:"mouseout"===t.type&&delete a.lastMouseEvent,!0},destroy:function(e){var t=e.streaming,a=t.canvas,n=t.mouseEventListener;i(e),a.removeEventListener("mousedown",n),a.removeEventListener("mouseup",n),s.each(e.scales,function(e){e instanceof r&&e.destroy()})}}}(e);return e.plugins.register(a),function(e){var m=e.helpers,t=e.Zoom=e.Zoom||{};function p(e,t){if(e.scaleAxes&&e.rangeMax&&!m.isNullOrUndef(e.rangeMax[e.scaleAxes])){var a=e.rangeMax[e.scaleAxes];a<t&&(t=a)}return t}function d(e,t){if(e.scaleAxes&&e.rangeMin&&!m.isNullOrUndef(e.rangeMin[e.scaleAxes])){var a=e.rangeMin[e.scaleAxes];t<a&&(t=a)}return t}t.zoomFunctions=t.zoomFunctions||{},t.panFunctions=t.panFunctions||{},t.zoomFunctions.realtime=function(e,t,a,n){var i,o,r=e.options.realtime,s=e.chart.options.plugins.streaming,l=m.valueOrDefault(r.duration,s.duration),u=m.valueOrDefault(r.delay,s.delay),c=l*(2-t);i=e.isHorizontal()?(e.right-a.x)/(e.right-e.left):(e.bottom-a.y)/(e.bottom-e.top),o=t<1?p(n,c):d(n,c),r.duration=o,r.delay=u+i*(l-o)},t.panFunctions.realtime=function(e,t,a){var n=e.options.realtime,i=e.chart.options.plugins.streaming,o=m.valueOrDefault(n.delay,i.delay)+(e.getValueForPixel(t)-e.getValueForPixel(0));n.delay=0<t?p(a,o):d(a,o)},t.updateResetZoom=function(e){e.resetZoom=function(){m.each(e.scales,function(e){var t=e.options.time,a=e.options.realtime,n=e.options.ticks;t&&(t.min=e.originalOptions.time.min,t.max=e.originalOptions.time.max),a&&(a.duration=e.originalOptions.realtime.duration,a.delay=e.originalOptions.realtime.delay),n&&(n.min=e.originalOptions.ticks.min,n.max=e.originalOptions.ticks.max)}),e.update({duration:0})}}}(e),a});