UNPKG

chartjs-plugin-stanford-diagram

Version:
3 lines (2 loc) 13 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).stanfordDiagramPlugin={})}(this,(function(t){"use strict";function e(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var n=[],a=!0,o=!1,r=void 0;try{for(var i,l=t[Symbol.iterator]();!(a=(i=l.next()).done)&&(n.push(i.value),!e||n.length!==e);a=!0);}catch(t){o=!0,r=t}finally{try{a||null==l.return||l.return()}finally{if(o)throw r}}return n}(t,e)||function(t,e){if(!t)return;if("string"==typeof t)return n(t,e);var a=Object.prototype.toString.call(t).slice(8,-1);"Object"===a&&t.constructor&&(a=t.constructor.name);if("Map"===a||"Set"===a)return Array.from(t);if("Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a))return n(t,e)}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,a=new Array(e);n<e;n++)a[n]=t[n];return a}function a(t,e,n){var a=t.length,o=Array(a);n>1?n=1:n<0&&(n=0);for(var r=0;r<a;r++){var i=t[r];o[r]=i+(e[r]-i)*n}return function(t,e,n){var a,o,r;if(0===e)a=n,o=n,r=n;else{var i=function(t,e,n){return n<0&&(n+=1),n>1&&(n-=1),n<1/6?t+6*(e-t)*n:n<.5?e:n<2/3?t+(e-t)*(2/3-n)*6:t},l=n<.5?n*(1+e):n+e-n*e,s=2*n-l;a=i(s,l,t+1/3),o=i(s,l,t),r=i(s,l,t-1/3)}return"rgb(".concat(Math.round(255*a),", ").concat(Math.round(255*o),", ").concat(Math.round(255*r),")")}(o[0],o[1],o[2])}function o(){var t=m(g()).domain([1,10]);return t.copy=function(){return copy$1(t,o()).base(t.base())},h.apply(t,arguments)}function r(t){return t}function i(t){return Math.log(t)}function l(t){return Math.exp(t)}function s(t){return-Math.log(-t)}function u(t){return-Math.exp(-t)}function c(t){return function(e){return-t(-e)}}function f(t){return isFinite(t)?+("1e"+t):t<0?0:t}function h(t,e){switch(arguments.length){case 0:break;case 1:this.interpolator(t);break;default:this.interpolator(e).domain(t)}return this}function d(){var t,e,n,a,o=0,i=1,l=r;function s(e){return isNaN(e=+e)?undefined:l(0===n?.5:e=(a(e)-t)*n)}return s.domain=function(r){return arguments.length?(t=a(o=+r[0]),e=a(i=+r[1]),n=t===e?0:1/(e-t),s):[o,i]},s.interpolator=function(t){return arguments.length?(l=t,s):l},function(r){return a=r,t=r(o),e=r(i),n=t===e?0:1/(e-t),s}}function p(t){return t}function g(){var t,n,a,o,r,i=0,l=1,s=p,u=!1;function c(e){return isNaN(e=+e)?r:s(0===a?.5:(e=(o(e)-t)*a,u?Math.max(0,Math.min(1,e)):e))}return c.domain=function(r){var s;return arguments.length?(s=e(r,2),i=s[0],l=s[1],t=o(i=+i),n=o(l=+l),a=t===n?0:1/(n-t),c):[i,l]},c.clamp=function(t){return arguments.length?(u=!!t,c):u},c.interpolator=function(t){return arguments.length?(s=t,c):s},c.unknown=function(t){return arguments.length?(r=t,c):r},function(e){return o=e,t=e(i),n=e(l),a=t===n?0:1/(n-t),c}}function y(t,e,n){var a,o,r,i,l=-1;if(n=+n,(t=+t)===(e=+e)&&n>0)return[t];if((a=e<t)&&(o=t,t=e,e=o),0===(i=function(t,e,n){var a=(e-t)/Math.max(0,n),o=Math.floor(Math.log(a)/Math.LN10),r=a/Math.pow(10,o);return o>=0?(r>=e10?10:r>=e5?5:r>=e2?2:1)*Math.pow(10,o):-Math.pow(10,-o)/(r>=e10?10:r>=e5?5:r>=e2?2:1)}(t,e,n))||!isFinite(i))return[];if(i>0)for(t=Math.ceil(t/i),e=Math.floor(e/i),r=new Array(o=Math.ceil(e-t+1));++l<o;)r[l]=(t+l)*i;else for(t=Math.floor(t*i),e=Math.ceil(e*i),r=new Array(o=Math.ceil(t-e+1));++l<o;)r[l]=(t-l)/i;return a&&r.reverse(),r}function m(e){var n,a,o=e(i,l),r=o.domain,h=10;function d(){return n=function(t){return t===Math.E?Math.log:10===t&&Math.log10||2===t&&Math.log2||(t=Math.log(t),function(e){return Math.log(e)/t})}(h),a=function(t){return 10===t?f:t===Math.E?Math.exp:function(e){return Math.pow(t,e)}}(h),r()[0]<0?(n=c(n),a=c(a),e(s,u)):e(i,l),o}return o.base=function(t){return arguments.length?(h=+t,d()):h},o.domain=function(t){return arguments.length?(r(t),d()):r()},o.ticks=function(t){var e,o=r(),i=o[0],l=o[o.length-1];(e=l<i)&&(f=i,i=l,l=f);var s,u,c,f=n(i),d=n(l),p=null==t?10:+t,g=[];if(!(h%1)&&d-f<p){if(f=Math.round(f)-1,d=Math.round(d)+1,i>0){for(;f<d;++f)for(u=1,s=a(f);u<h;++u)if(!((c=s*u)<i)){if(c>l)break;g.push(c)}}else for(;f<d;++f)for(u=h-1,s=a(f);u>=1;--u)if(!((c=s*u)<i)){if(c>l)break;g.push(c)}}else g=y(f,d,Math.min(d-f,p)).map(a);return e?g.reverse():g},o.tickFormat=function(e,r){if(null==r&&(r=10===h?".0e":","),"function"!=typeof r&&(r=t.format(r)),e===1/0)return r;null==e&&(e=10);var i=Math.max(1,h*e/o.ticks().length);return function(t){var e=t/a(Math.round(n(t)));return e*h<h-.5&&(e*=h),e<=i?r(t):""}},o.nice=function(){return r(function(t,e){t=t.slice();var n,a=0,o=t.length-1,r=t[a],i=t[o];i<r&&(n=a,a=o,o=n,n=r,r=i,i=n);return t[a]=e.floor(r),t[o]=e.ceil(i),t}(r(),{floor:function(t){return a(Math.floor(n(t)))},ceil:function(t){return a(Math.ceil(n(t)))}}))},o}function x(t){var e=t.plugins;return(e&&e.stanfordDiagram?e.stanfordDiagram:null)||t.stanfordDiagram||{}}function b(t,e,n){var a=t.ctx;e&&e.forEach((function(e){a.polygon(function(t,e){var n=t.scales["x-axis-1"],a=t.scales["y-axis-1"];return e.map((function(t){return{x:n.getPixelForValue(F(t.x,n,a)),y:a.getPixelForValue(F(t.y,n,a))}}))}(t,e.points),e.fillColor,e.strokeColor),e.text&&function(t,e,n,a){var o=t.ctx,r=t.scales["x-axis-1"],i=t.scales["y-axis-1"],l=v(t,n,a),s=l.count,u=l.percentage,c=e.format?e.format(s,u):"".concat(s," (").concat(u,")");o.save(),o.font=e.font?e.font:"".concat(t.options.defaultFontSize,"px ").concat(Chart.defaults.global.defaultFontFamily),o.fillStyle=e.color?e.color:Chart.defaults.global.defaultFontColor,o.textBaseline="middle",o.fillText(c,r.getPixelForValue(e.x),i.getPixelForValue(e.y)),o.restore()}(t,e.text,e.points,n)}))}function v(t,e,n){var a=t.data.datasets[0].data.reduce((function(t,e){return t+Number(e.epochs)}),0),o=t.data.datasets[0].data.reduce((function(a,o){return k(t,o,e,n)?a+Number(o.epochs):a}),0);return{count:o,percentage:M(t,a,o)}}function M(t,e,n){var a=t.options.plugins.stanfordDiagram&&t.options.plugins.stanfordDiagram.percentage?t.options.plugins.stanfordDiagram.percentage:{};if(a instanceof Intl.NumberFormat)return a.format(0===n?0:n/e);var o,r,i,l,s,u=isNaN(+a.decimalPlaces)?1:a.decimalPlaces,c=0===n?0:n/e*100;if(0===c)return c.toFixed(u);switch(a.roundingMethod){case"ceil":o=Math.ceil;break;case"floor":o=Math.floor;break;case"round":default:o=Math.round}return(r=o,i=c,l=u,s=Math.pow(10,l||0),r(i*s)/s).toFixed(u)}function F(t,e,n){switch(t){case"MAX_XY":return Math.min(e.max,n.max);case"MAX_X":return e.max;case"MAX_Y":return n.max;default:return t}}function S(t,e,n,a,o){switch(t){case"MAX_XY":return"x"===o&&a>e.max?Math.ceil(a*n.max/e.max)+1:"y"===o&&a>n.max?Math.ceil(a*e.max/n.max)+1:Math.min(e.max,n.max)+1;case"MAX_X":return"x"===o&&a>e.max?a+1:e.max+1;case"MAX_Y":return"y"===o&&a>n.max?a+1:n.max+1;default:return t}}function k(t,e,n,a){for(var o,r,i,l,s=t.scales["x-axis-1"],u=t.scales["y-axis-1"],c=!1,f=e.x,h=e.y,d=0,p=n.length-1;d<n.length;p=d++)a?(o=F(n[d].x,s,u),r=F(n[d].y,s,u),l=F(n[p].x,s,u),i=F(n[p].y,s,u),"number"!=typeof n[d].x&&(o+=1),"number"!=typeof n[d].y&&(r+=1),"number"!=typeof n[p].x&&(l+=1),"number"!=typeof n[p].y&&(i+=1)):(o=S(n[d].x,s,u,f,"x"),l=S(n[p].x,s,u,f,"x"),r=S(n[d].y,s,u,h,"y"),i=S(n[p].y,s,u,h,"y")),r>h!=i>h&&f<(l-o)*(h-r)/(i-r)+o&&(c=!c);return c}function P(t,e){var n=t.ctx,o=t.chartArea.top+5,i=t.chartArea.bottom,l=function(){var t=d()(r);return h.apply(t,arguments)}((function(t){return a([.7,1,.5],[0,1,.5],t)})).domain([o,i]),s=t.chartArea.right+25,u=function(t,e,n){t=+t,e=+e,n=(o=arguments.length)<2?(e=t,t=0,1):o<3?1:+n;for(var a=-1,o=0|Math.max(0,Math.ceil((e-t)/n)),r=new Array(o);++a<o;)r[a]=t+a*n;return r}(o,i,5),c=t.scales["x-axis-1"],f=t.scales["y-axis-1"];n.save(),u.forEach((function(t){n.fillStyle=l(i-t),n.fillRect(s,t,25,5)})),i=u[u.length-1]+5,function(t,e,n,a,o,r){e.lineWidth=1,e.strokeStyle="#000000",e.fillStyle="#000000",e.beginPath(),e.moveTo(a+.5,o),e.lineTo(a+.5,r),e.stroke(),e.textAlign="start",e.textBaseline="middle";for(var i=function(t){return+t.toExponential().split("e",2)[1]}(n),l=0;l<=i;l++){var s=r-(r-o)/i*l;e.beginPath(),e.moveTo(a,s),e.lineTo(a+6,s),e.stroke(),e.font="".concat(t.options.defaultFontSize-1,"px ").concat(Chart.defaults.global.defaultFontFamily),e.fillText("10 ",a+9,s),e.font="".concat(t.options.defaultFontSize-2,"px ").concat(Chart.defaults.global.defaultFontFamily),e.fillText("".concat(l),a+9+t.options.defaultFontSize,s-7)}}(t,n,e,s+25,o,i);var p=Math.min(c.max,f.max);n.lineWidth=1,n.strokeStyle="rgba(0, 0, 0, 0.2)",n.beginPath(),n.moveTo(c.getPixelForValue(c.min),f.getPixelForValue(f.min)),n.lineTo(c.getPixelForValue(p),f.getPixelForValue(p)),n.stroke(),n.restore();var g=s+25+9+t.options.defaultFontSize+22;!function(t,e,n,a,o){var r=t.options.plugins.stanfordDiagram.legendLabel||"Number of samples (epochs) per point";e.save(),e.font="".concat(t.options.defaultFontSize,"px ").concat(Chart.defaults.global.defaultFontFamily),e.strokeStyle="#000000",e.fillStyle="#000000";var i=e.measureText(r);e.translate(n,(o-a)/2+i.width/2),e.rotate(-Math.PI/2),e.fillText(r,0,0),e.restore()}(t,n,g,o,i)}CanvasRenderingContext2D.prototype.polygon=function(t,e,n){if(!(t.length<=0)){this.save(),this.globalCompositeOperation="destination-over",this.beginPath(),this.moveTo(t[0].x,t[0].y);for(var a=1;a<t.length;a++)this.lineTo(t[a].x,t[a].y);this.closePath(),n&&(this.lineWidth=1,this.strokeStyle=n,this.stroke()),e&&(this.fillStyle=e,this.fill()),this.restore()}},Chart.controllers.stanford=Chart.controllers.line.extend({update:function(){this.chart.options.elements.point.radius=Math.max(Math.round(this.chart.height/200),1),Chart.controllers.line.prototype.update.apply(this,arguments)}}),Chart.defaults._set("stanford",{animation:!1,aspectRatio:1.12,elements:{point:{radius:2.5,pointStyle:"rect"}},hover:{mode:null},legend:{display:!1},scales:{xAxes:[{id:"x-axis-1",type:"linear",position:"bottom",ticks:{suggestedMax:60,beginAtZero:!0}}],yAxes:[{id:"y-axis-1",type:"linear",position:"left",ticks:{suggestedMax:60,beginAtZero:!0}}]},showLines:!1,tooltips:{enabled:!1,custom:function(t){var e=document.getElementById("chartjs-tooltip");if(e||((e=document.createElement("div")).id="chartjs-tooltip",e.innerHTML="<table></table>",document.body.appendChild(e)),0!==t.opacity){if(e.classList.remove("above","below","no-transform"),t.yAlign?e.classList.add(t.yAlign):e.classList.add("no-transform"),t.body){var n=t.title||[],a=t.body.map((function(t){return t.lines})),o="<thead>";n.forEach((function(t){o+='<tr><th style="text-align: left">'+t.label+'</th><th style="text-align: right">'+t.value+"</th></tr>"})),o+="</thead><tbody>",a.forEach((function(e,n){var a="background: "+t.labelColors[n].backgroundColor+";";o+="<tr><td>"+('<span style="'+(a+="width: 10px; height: 10px; display: inline-block; margin-right: 5px;")+'"></span>')+e[0].label+'</td><td style="text-align: right">'+e[0].value+"</td></tr>"})),o+="</tbody>",e.querySelector("table").innerHTML=o}var r=this._chart.canvas.getBoundingClientRect();e.style.opacity="1",e.style.position="absolute",e.style.left=r.left+window.pageXOffset+t.caretX+"px",e.style.top=r.top+window.pageYOffset+t.caretY+"px",e.style.fontFamily=t._bodyFontFamily,e.style.fontSize=t.bodyFontSize+"px",e.style.fontStyle=t._bodyFontStyle,e.style.padding=t.yPadding+"px "+t.xPadding+"px",e.style.pointerEvents="none",e.style.background=t.backgroundColor,e.style.color=t.bodyFontColor,e.style.borderRadius=t.cornerRadius+"px"}else e.style.opacity="0"},callbacks:{title:function(t){return[{label:this._chart.scales["x-axis-1"].options.scaleLabel.labelString,value:t[0].xLabel},{label:this._chart.scales["y-axis-1"].options.scaleLabel.labelString,value:t[0].yLabel}]},label:function(t,e){return{label:this._chart.options.plugins.stanfordDiagram.epochsLabel||"Epochs",value:e.datasets[0].data[t.index].epochs}}}}});var A={beforeInit:function(t){var e=t.stanfordDiagramPlugin={options:x(t.options.plugins)};e.options.maxEpochs||(e.options.maxEpochs=1e4),e.colorScale=o((function(t){return a([.7,1,.5],[0,1,.5],t)})).domain([1,e.options.maxEpochs]),t.options.layout.padding.right+=100},beforeDatasetsUpdate:function(t){var e=t.stanfordDiagramPlugin;if(e.colorScale){t.data.datasets[0].pointBackgroundColor=[];for(var n=0;n<t.data.datasets[0].data.length;n++)t.data.datasets[0].pointBackgroundColor[n]=e.colorScale(t.data.datasets[0].data[n].epochs)}},beforeUpdate:function(t){t.chart.options.defaultFontSize=Math.max(Math.round(t.chart.height/50),8)},afterRender:function(t){P(t,t.stanfordDiagramPlugin.options.maxEpochs),b(t,t.stanfordDiagramPlugin.options.regions,!!t.stanfordDiagramPlugin.options.countOnlyVisible)}};t.calculatePercentageValue=M,t.countEpochsInRegion=v,t.getStanfordConfig=x,t.pointInPolygon=k,t.stanfordDiagramPlugin=A})); //# sourceMappingURL=chartjs-plugin-stanford-diagram.min.js.map