UNPKG

sitespeed.io

Version:

sitespeed.io is an open-source tool for comprehensive web performance analysis, enabling you to test, monitor, and optimize your website’s speed using real browsers in various environments.

1 lines 2.94 kB
!function(e,t){"function"==typeof define&&define.amd?define(["chartist"],function(n){return e.returnExportsGlobal=t(n)}):"object"==typeof exports?module.exports=t(require("chartist")):e["Chartist.plugins.legend"]=t(e.Chartist)}(this,function(e){"use strict";var t={className:"",classNames:!1,removeAll:!1,legendNames:!1,clickable:!0,onClick:null,position:"top"};return e.plugins=e.plugins||{},e.plugins.legend=function(n){if(n&&n.position){if(!("top"===n.position||"bottom"===n.position||n.position instanceof HTMLElement))throw Error("The position you entered is not a valid position");if(n.position instanceof HTMLElement){var a=n.position;delete n.position}}return n=e.extend({},t,n),a&&(n.position=a),function(t){!function(){var e=t.container.querySelector(".ct-legend");e&&e.parentNode.removeChild(e)}();var a=function(){var a=document.createElement("ul");return a.className="ct-legend",t instanceof e.Pie&&a.classList.add("ct-legend-inside"),"string"==typeof n.className&&n.className.length>0&&a.classList.add(n.className),t.options.width&&(a.style.cssText="width: "+t.options.width+"px;margin: 0 auto;"),a}(),i=t instanceof e.Pie&&t.data.labels&&t.data.labels.length,s=function(e){return n.legendNames||(e?t.data.labels:t.data.series)}(i),o=function(e){for(var n=new Array(t.data.series.length),a=0;a<t.data.series.length;a++)n[a]={data:t.data.series[a],label:e?t.data.labels[a]:null,legend:-1};return n}(i),l=[],r=Array.isArray(n.classNames)&&n.classNames.length===s.length;s.forEach(function(e,t){var i=e.name||e,s=e.series||[t],c=function(e,t,a){var i=document.createElement("li");return i.classList.add("ct-series-"+e),a&&i.classList.add(n.classNames[e]),i.setAttribute("data-legend",e),i.textContent=t,i}(t,i,r);a.appendChild(c),s.forEach(function(e){o[e].legend=t}),l.push({text:i,series:s,active:!0})}),t.on("created",function(e){!function(e){if(n.position instanceof HTMLElement)n.position.insertBefore(e,null);else switch(n.position){case"top":t.container.insertBefore(e,t.container.childNodes[0]);break;case"bottom":t.container.insertBefore(e,null)}}(a)}),n.clickable&&(t.data.series=t.data.series.map(function(n,a){return"object"!=typeof n&&(n={value:n}),n.className=n.className||t.options.classNames.series+"-"+e.alphaNumerate(a),n}),function(e,a,i,s){e.addEventListener("click",function(o){var l=o.target;if(l.parentNode===e&&l.hasAttribute("data-legend")){o.preventDefault();var r=parseInt(l.getAttribute("data-legend")),c=a[r];if(c.active){c.active=!1,l.classList.add("inactive");var d=a.filter(function(e){return e.active}).length;if(!n.removeAll&&0==d)for(var u=0;u<a.length;u++)a[u].active=!0,e.childNodes[u].classList.remove("inactive")}else c.active=!0,l.classList.remove("inactive");var f=[],p=[];for(u=0;u<i.length;u++)-1!=i[u].legend&&a[i[u].legend].active&&(f.push(i[u].data),p.push(i[u].label));t.data.series=f,s&&(t.data.labels=p),t.update(),n.onClick&&n.onClick(t,o)}})}(a,l,o,i))}},e.plugins.legend});