UNPKG

chrt-object

Version:

Universal Object components of Chrt

3 lines (2 loc) 5.73 kB
// chrt-object v0.0.19 Copyright 2020-2024 chrt chrt.io !function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).chrt=t.chrt||{})}(this,(function(t){"use strict";function n(t){return function(t){if(Array.isArray(t))return e(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,n){if(!t)return;if("string"==typeof t)return e(t,n);var i=Object.prototype.toString.call(t).slice(8,-1);"Object"===i&&t.constructor&&(i=t.constructor.name);if("Map"===i||"Set"===i)return Array.from(t);if("Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i))return e(t,n)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function e(t,n){(null==n||n>t.length)&&(n=t.length);for(var e=0,i=new Array(n);e<n;e++)i[e]=t[e];return i}var i=["chrt","series"];function r(t){return null===t||null==t||void 0===t}function s(){return"c"+Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15)}function o(t){return!r(t.type)&&i.indexOf(t.type)>-1}function a(t){return!isFinite(t)}function c(t){return document.createElementNS("http://www.w3.org/2000/svg",t)}function u(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1],i=t._id||s();return t.id(i).parent(this),e?this.objects=[t].concat(n(this.objects)):this.objects.push(t),this}var l=Object.freeze({__proto__:null,add:u,create:function(t){return document.createElement(t)},createSVG:c,getCoords:function(t){var n=this;return null!=this&&this.fields?t.map((function(t){var e=r(t[n.fields.x])||a(t[n.fields.x])?null:n.parentNode.scales.x[n.scales.x](t[n.fields.x]),i=r(t[n.fields.y])||a(t[n.fields.y])?null:n.parentNode.scales.y[n.scales.y](t[n.fields.y]);return[a(e)?0:e,a(i)?0:i]})):[]},getStrokeStyle:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:4,i=null;switch(t){case"dashed":i="".concat(n*e," ").concat(n*e);break;case"dotted":i="".concat(n," ").concat(n);break;default:i=null}return i},hasData:o,isInfinity:a,isNull:r,oppositeSigns:function(t,n){return(t^n)<0},uuid:s});function h(t,n){var e,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(t){return t};return r(t)?(console.warn("name missing: attr method requires a name for the attribute"),this):r(t)||void 0!==n?(this.attrs[t]="function"==typeof n?n:function(){return i(n)},this):(null===(e=this.attrs)||void 0===e?void 0:e[t])||function(){}}function d(t){return t?(this.root=t,this.currentNode=this.root,this):this.g||this.root}function f(t){return h.call(this,"display",t)}function p(){var t=this;this._id=null,this.objects=[],this.type="chrt-object",this.fields={x:null,y:null},this.scales={x:"x",y:"y"},this._classNames=[],this._accessor=function(t,n){return{x:!r(t)&&Object.prototype.hasOwnProperty.call(t,"x")?t.x:n,y:r(t)?null:Object.prototype.hasOwnProperty.call(t,"y")?t.y:t}},this.interpolationFunction=null,this.attrs=[],h.call(this,"display",!0),this.id=function(n){return r(n)?t._id:(t._id=n||t._id,t.g&&t.g.setAttribute("id",t._id),t)},this.aria=function(n){return h.call(t,"aria",n),t},this.class=function(e){if(r(e))return t._classNames;var i,s,o=(Array.isArray(e)?e:e.split(" ")).map((function(t){return t.replace(/\s\s+/g," ").trim()})).filter((function(t){return""!==t}));(t.original_classNames=t.original_classNames||t._classNames,t._classNames=n(new Set([].concat(n(t.original_classNames),n(o)))),t.g)&&((i=t.g.classList).remove.apply(i,n(t.g.classList)),(s=t.g.classList).add.apply(s,n(t._classNames)));return t},this.hasData=function(){return o(t)};var e=function(n,e){r(e)||(t.scales[n]=e)};return this.x=function(n){return r(n)?t.scales.x:(e("x",n||"x"),t)},this.y=function(n){return r(n)?t.scales.y:(e("y",n||"y"),t)},this.getObjectTypeIndex=function(){var n,e;return(null!==(n=null===(e=t.parent())||void 0===e?void 0:e.objects)&&void 0!==n?n:[]).filter((function(n){return n.constructor.name===t.constructor.name})).indexOf(t)},this}function y(){return new p}p.prototype=Object.create(y.prototype),p.prototype=Object.assign(y.prototype,{node:d,data:function(t,n){if(!t)return o(this)?this._data||null:this;if(!o(this))return this;if("function"==typeof arguments[0])return this._accessor=arguments[0],this;this._orginalData=this._orginalData||t;var e=n||this._accessor;return this._accessor=e,this._data=e&&!this._accessorApplied?t.map((function(t,n,i){return t instanceof Object?Object.assign({},t,e(t,n,i)):e(t,n,i)})):t,this._accessorApplied=!0,this},add:u,snap:u,parent:function(t){return t?(this.parentNode=t,this):this.parentNode},render:function(t){return this.g||(this.g=c("g"),this._id&&this.g.setAttribute("id",this._id),o(this)&&t?"stack"===t.type||"group"===t.group?t.g.prepend(this.g):t.g.append(this.g):this.currentNode.append(this.g),this.update()),this},update:function(){return this.parentNode&&this.parentNode.scales.x[this.scales.x]&&this.parentNode.scales.y[this.scales.y]&&this.draw(),this},curve:function(t){return r(t)?this.interpolationFunction:(this.interpolationFunction=t,this)},attr:h,show:function(){return f.call(this,!0)},hide:function(){return f.call(this,!1)}}),t.cssDisplay=function(t){var n=d.call(this);if(!n)return this;var e=n.style.display;return this._display=void 0!==this._display?this._display:n.style.display,t?"none"===e&&t&&(n.style.display=this._display):n.style.display="none",this},t.default=p,t.node=d,t.utils=l,Object.defineProperty(t,"__esModule",{value:!0})}));