UNPKG

billboard.js

Version:

Re-usable easy interface JavaScript chart library, based on D3 v4+

39 lines (38 loc) • 12.7 kB
/*! * Copyright (c) 2017 ~ present NAVER Corp. * billboard.js project is licensed under the MIT license * * billboard.js, JavaScript chart library * https://naver.github.io/billboard.js/ * * @version 4.1.1 * @requires billboard.js * @summary billboard.js plugin */(function(l,c){typeof exports=="object"&&typeof module=="object"?module.exports=c():typeof define=="function"&&define.amd?define("bb",[],c):typeof exports=="object"?exports.bb=c():(l.bb=l.bb||{},l.bb.plugin=l.bb.plugin||{},l.bb.plugin.tableview=c())})(this,function(){return(function(){var g={};g.d=function(c,f){for(var a in f)g.o(f,a)&&!g.o(c,a)&&Object.defineProperty(c,a,{enumerable:!0,get:f[a]})},g.o=function(c,f){return Object.prototype.hasOwnProperty.call(c,f)};var l={};return(function(){"use strict";g.d(l,{default:function(){return Be}});const c=e=>e||e===0,f=e=>typeof e=="function",a=e=>typeof e=="string",M=e=>typeof e=="number",te=e=>typeof e=="undefined",ne=e=>typeof e!="undefined",z=e=>typeof e=="boolean",He=e=>Math.ceil(e/10)*10,Xe=e=>Math.ceil(e)+.5,Ze=e=>e[1]-e[0],O=e=>typeof e=="object",re=e=>{for(const t in e)if(Object.prototype.hasOwnProperty.call(e,t))return!1;return!0},oe=e=>te(e)||e===null||a(e)&&e.length===0||O(e)&&!(e instanceof Date)&&re(e)||M(e)&&isNaN(e),Qe=e=>!oe(e),I=e=>Array.isArray(e),Ye=e=>e&&!(e!=null&&e.nodeType)&&O(e)&&!I(e),ie=new Set(["span","div","p","br","b","i","em","small","strong","mark","u","s","sub","sup","h1","h2","h3","h4","h5","h6","ul","ol","li","dl","dt","dd","table","thead","tbody","tfoot","tr","th","td","caption","colgroup","col","hr","pre","code","blockquote","abbr","ins","del","a","img","figure","figcaption","svg","g","path","circle","ellipse","rect","line","polyline","polygon","text","tspan","textPath","use","defs","symbol","clipPath","mask","linearGradient","radialGradient","stop","pattern","marker","title","desc"]),se=new Set(["class","id","style","title","lang","dir","href","src","alt","width","height","colspan","rowspan","scope","headers","d","points","x","y","x1","x2","y1","y2","cx","cy","r","rx","ry","dx","dy","viewBox","preserveAspectRatio","transform","fill","fill-opacity","fill-rule","stroke","stroke-width","stroke-opacity","stroke-linecap","stroke-linejoin","stroke-dasharray","stroke-dashoffset","opacity","clip-path","clip-rule","mask","font-family","font-size","font-weight","font-style","text-anchor","dominant-baseline","offset","stop-color","stop-opacity","gradientUnits","gradientTransform","spreadMethod","patternUnits","patternTransform","marker-start","marker-mid","marker-end","markerWidth","markerHeight","refX","refY","xlink:href"]),E=new Map;ie.forEach(e=>E.set(e.toLowerCase(),e));const T=new Map;se.forEach(e=>T.set(e.toLowerCase(),e));const ae=new Set(["http:","https:","mailto:"]),le=new Set(["href","src","xlink:href"]),ce=/^<\/?([a-zA-Z][a-zA-Z0-9]*)/,fe=/^<\/([a-zA-Z][a-zA-Z0-9]*)\s*>$/,ue=/^<([a-zA-Z][a-zA-Z0-9]*)([\s\S]*?)(\/?)>$/,R=/([a-zA-Z][\w:-]*)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g,V=/url\s*\(\s*["']?([^"')]+)["']?\s*\)/gi,de=["expression(","behavior:","binding:","@import","@charset","-moz-binding:"];function C(e){return e.replace(/&colon;/gi,":").replace(/&newline;/gi,` `).replace(/&tab;/gi," ").replace(/&nbsp;/gi," ").replace(/&lt;/gi,"<").replace(/&gt;/gi,">").replace(/&amp;/gi,"&").replace(/&quot;/gi,'"').replace(/&apos;/gi,"'").replace(/&#(\d+);?/gi,(t,n)=>String.fromCharCode(parseInt(n,10))).replace(/&#x([0-9a-f]+);?/gi,(t,n)=>String.fromCharCode(parseInt(n,16)))}function G(e){const n=C(e).trim().replace(/[\s\u0000-\u001f]/g,"").toLowerCase();if(!n||n.startsWith("#"))return!0;const r=n.match(/^[^/?#]*:/);return r?ae.has(r[0]):!0}function pe(e){const n=C(e).replace(/[\u0000-\u001f]/g,"");V.lastIndex=0;let r;for(;(r=V.exec(n))!==null;)if(!G(r[1]))return null;const o=n.toLowerCase().replace(/\s/g,"");for(const i of de)if(o.includes(i))return null;return e}const he={'"':"&quot;","'":"&#39;","`":"&#96;"},ge=/["'`]/g;function P(e){return e.replace(ge,t=>he[t])}function be(e,t,n=!1){if(le.has(e))return G(t)?n?P(t):t:null;if(e==="style"){const o=pe(t);return o===null?null:n?P(o):o}const r=C(t).toLowerCase().replace(/\s/g,"");return/\bon\w+=/.test(r)?null:n?P(t):t}function me(e){const t=e.match(ce);return t?t[1].toLowerCase():null}function _e(e){const t=me(e);return t!==null&&E.has(t)}function we(e){var t,n,r;const o=e.match(fe);if(o){const p=o[1].toLowerCase();return`</${(t=E.get(p))!=null?t:p}>`}const i=e.match(ue);if(!i)return"";const[,s,u,m]=i,d=s.toLowerCase(),h=(n=E.get(d))!=null?n:d,x=[];R.lastIndex=0;let y;for(;(y=R.exec(u))!==null;){const p=y[1].toLowerCase(),Y=y[2],J=y[3],j=y[4];if(p.startsWith("on"))continue;const K=(r=T.get(p))!=null?r:p;let $,v;if(Y!==void 0)$=Y,v='"';else if(J!==void 0)$=J,v="'";else if(j!==void 0)$=j,v='"';else{T.has(p)&&x.push(K);continue}if(T.has(p)){const ee=be(p,$,j!==void 0);ee!==null&&x.push(`${K}=${v}${ee}${v}`)}}const We=x.length>0?` ${x.join(" ")}`:"";return`<${h}${We}${m?"/>":">"}`}function ye(e){return typeof e!="string"||!e||e.indexOf("<")===-1?e:e.replace(/<\/?[^>]*>|[^<>\s]+>/g,t=>t.startsWith("<!--")?"":t.startsWith("<")?_e(t)?we(t):t.replace(/</g,"&lt;"):t.slice(0,-1)+"&gt;")}var ve,U,Oe,N,_,Ee,F,D,Te=Object.defineProperty,k=Object.getOwnPropertySymbols,Se=Object.prototype.hasOwnProperty,Ae=Object.prototype.propertyIsEnumerable,q=(e,t,n)=>t in e?Te(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,B=(e,t)=>{for(var n in t||(t={}))Se.call(t,n)&&q(e,n,t[n]);if(k)for(var n of k(t))Ae.call(t,n)&&q(e,n,t[n]);return e};function W(e,t){for(let n=0;n<e.length;n++){const r=e[n];r!==null&&U(r)&&t(r,n)}}function Je(e,t,n){return U(e[t])?e[t]:n}function Ke(e,t){for(const n in e)if(e[n]===t)return!0;return!1}function et(e,t,...n){const r=Oe(e);return r&&e.call(t,...n),r}function tt(e,t){let n=0;const r=function(...o){!--n&&t.apply(this,o)};"duration"in e?e.each(()=>++n).on("end",r):(++n,e.call(r))}const nt=e=>e.charAt(0).toUpperCase()+e.slice(1);function rt(e,t="-"){return e.split(t).map((n,r)=>r?n.charAt(0).toUpperCase()+n.slice(1).toLowerCase():n.toLowerCase()).join("")}const ot=e=>[].slice.call(e);function it(...e){const t=n=>{if(N(n))return n.map(t);if(_(n)&&n.constructor){const r=new n.constructor;for(const o in n)r[o]=t(n[o]);return r}return n};return e.map(n=>t(n)).reduce((n,r)=>B(B({},n),r))}function xe(e={},t){N(t)&&t.forEach(n=>xe(e,n));for(const n in t)/^\d+$/.test(n)||n in e||(e[n]=t[n]);return e}function st(e){const t=e[0]instanceof Date,n=Array.from(new Set(t?e.map(Number):e));return t?n.map(r=>new Date(r)):n}function at(e){return e&&e.length?e.reduce((t,n)=>t.concat(n)):[]}function H(e,...t){if(!t.length||t.length===1&&!t[0])return e;const n=t.shift();return _(e)&&_(n)&&Object.keys(n).forEach(r=>{if(!/^(__proto__|constructor|prototype)$/i.test(r)){const o=n[r];o instanceof Date?e[r]=new Date(o.getTime()):_(o)?(!e[r]&&(e[r]={}),e[r]=H(e[r],o)):e[r]=N(o)?o.concat():o}}),H(e,...t)}function $e(e,t=!0){let n;return e[0]instanceof Date?n=t?(r,o)=>r-o:(r,o)=>o-r:t&&!e.every(isNaN)?n=(r,o)=>r-o:t||(n=(r,o)=>r>o&&-1||r<o&&1||r===o&&0),e.concat().sort(n)}function lt(e,t){let n=t.filter(r=>Ee(r));if(n.length)if(F(n[0])){let r=e==="min"?1/0:-1/0;for(const o of n)(e==="min"?o<r:o>r)&&(r=o);n=r}else n[0]instanceof Date&&(n=$e(n,e==="min")[0]);else n=void 0;return n}const ct=(e,t,n=1)=>{const r=[],o=Math.max(0,Math.ceil((t-e)/n))|0;for(let i=0;i<o;i++)r.push(e+i*n);return r};let Ce=0;function ft(e=!0){const t=++Ce;return e?String(t):t}function X(e,t,n,r,o){if(n>r)return-1;const i=Math.floor((n+r)/2);let{x:s,w:u=0}=e[i];return o&&(s=e[i].y,u=e[i].h),t>=s&&t<=s+u?i:t<s?X(e,t,n,i-1,o):X(e,t,i+1,r,o)}function S(e,t){return ye(e.replace(/\{=([^}]+)\}/g,(n,r)=>{var o;return(o=t[r])!=null?o:""}))}function ut(e){var t;let n;if(e instanceof Date)n=e;else if(D(e)){const{config:r,format:o}=this;n=(t=o.dataTime(r.data_xFormat)(e))!=null?t:new Date(e)}else F(e)&&!isNaN(e)&&(n=new Date(+e));return(!n||isNaN(+n))&&console&&console.error&&console.error(`Failed to parse x '${e}' to Date object`),n}function dt(e){if(_(e)&&!D(e)){const s=e;return{top:s.top||0,right:s.right||0,bottom:s.bottom||0,left:s.left||0}}const t=(D(e)?e.trim().split(/\s+/):[e]).map(s=>+s||0),[n,r=n,o=n,i=r]=t;return{top:n,right:r,bottom:o,left:i}}function Pe(e,t){t()===!1?ve(()=>Pe(e,t)):e()}function pt(e,t=(n=>n)){const n=new Set;return W(e,(r,o)=>{n.add(t(r,o))}),n}function ht(e,t,n=(r=>r)){const r=new Map;return W(e,(o,i)=>{r.set(t(o,i),n(o,i))}),r}function Ne(e){const t=this.config;let n,r,o;const i=()=>{const s=r.shift();if(s&&n&&O(n)&&s in n)return n=n[s],i();if(!s)return n};Object.keys(t).forEach(s=>{n=e,r=s.split("_"),o=i(),ne(o)&&(t[s]=o)}),this.api&&(this.state.orgConfig=e)}var De=Object.defineProperty,Le=(e,t,n)=>t in e?De(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,A=(e,t,n)=>Le(e,typeof t!="symbol"?t+"":t,n);class Z{constructor(t={}){A(this,"$$"),A(this,"options"),A(this,"config"),this.options=t}loadConfig(){Ne.call(this,this.options)}$beforeInit(){}$init(){}$afterInit(){}$redraw(){}$willDestroy(){Object.keys(this).forEach(t=>{this[t]=null,delete this[t]})}}A(Z,"version","4.1.1");const b={id:"__tableview-style__",class:"bb-tableview",rule:`.bb-tableview { border-collapse:collapse; border-spacing:0; background:#fff; min-width:100%; margin-top:10px; font-family:sans-serif; font-size:.9em; } .bb-tableview tr:hover { background:#eef7ff; } .bb-tableview thead tr { background:#f8f8f8; } .bb-tableview caption,.bb-tableview td,.bb-tableview th { text-align: center; border:1px solid silver; padding:.5em; } .bb-tableview caption { font-size:1.1em; font-weight:700; margin-bottom: -1px; }`},w={body:`<caption>{=title}</caption> <thead><tr>{=thead}</tr></thead> <tbody>{=tbody}</tbody>`,thead:'<th scope="col">{=title}</th>',tbodyHeader:'<th scope="row">{=value}</th>',tbody:"<td>{=value}</td>"};class je{constructor(){return{selector:void 0,categoryTitle:"Category",categoryFormat:function(t){let n=t;return this.$$.axis.isCategorized()?n=this.$$.categoryName(t):this.$$.axis.isTimeSeries()&&(n=t.toLocaleDateString()),n},class:void 0,style:!0,title:void 0,updateOnToggle:!0,nullString:"-",numberFormat:function(t){return t.toLocaleString()}}}}var Me=Object.defineProperty,ze=Object.defineProperties,Ie=Object.getOwnPropertyDescriptors,Q=Object.getOwnPropertySymbols,Re=Object.prototype.hasOwnProperty,Ve=Object.prototype.propertyIsEnumerable,L=(e,t,n)=>t in e?Me(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Ge=(e,t)=>{for(var n in t||(t={}))Re.call(t,n)&&L(e,n,t[n]);if(Q)for(var n of Q(t))Ve.call(t,n)&&L(e,n,t[n]);return e},Ue=(e,t)=>ze(e,Ie(t)),Fe=(e,t,n)=>L(e,typeof t!="symbol"?t+"":t,n);const ke={selector:a,categoryTitle:a,categoryFormat:f,class:a,style:z,title:a,updateOnToggle:z,nullString:a,numberFormat:f};function qe(e){return!O(e)||I(e)||e===null?!1:Object.entries(e).every(([t,n])=>{const r=ke[t];return!f(r)||n===void 0||r(n)})}class Be extends Z{constructor(t={}){var n;return super(t),Fe(this,"element"),qe(t)||(n=console==null?void 0:console.error)==null||n.call(console,"[billboard.js] TableView plugin received invalid options; unsupported values will be ignored.",t),this.config=new je,this}$beforeInit(){this.loadConfig()}$init(){const{class:t,selector:n,style:r}=this.config;let o=document.querySelector(n||`.${t||b.class}`);if(!o){const i=this.$$.$el.chart.node();o=document.createElement("table"),i.parentNode.insertBefore(o,i.nextSibling)}if(o.tagName!=="TABLE"){const i=document.createElement("table");o.appendChild(i),o=i}if(r&&!document.getElementById(b.id)){const i=document.createElement("style");i.id=b.id,i.innerHTML=b.rule,(document.head||document.getElementsByTagName("head")[0]).appendChild(i)}o.classList.add(...[r&&b.class,t].filter(Boolean)),this.element=o}generateTable(){const{$$:t,config:n,element:r}=this,o=t.filterTargetsToShow(t.data.targets);let i=S(w.thead,{title:o.length?this.config.categoryTitle:""}),s="";const u=[];o.forEach(m=>{i+=S(w.thead,{title:m.id}),m.values.forEach((d,h)=>{u[h]||(u[h]=[d.x]),u[h].push(d.value)})}),u.forEach(m=>{s+=`<tr>${m.map((d,h)=>S(h?w.tbody:w.tbodyHeader,{value:h===0?n.categoryFormat.bind(this)(d):M(d)?n.numberFormat.bind(this)(d):n.nullString})).join("")}</tr>`}),r.innerHTML=S(w.body,Ue(Ge({},n),{title:n.title||t.config.title_text||"",thead:i,tbody:s}))}$redraw(){const{state:t}=this.$$;!(t.resizing||!this.config.updateOnToggle&&t.toggling)&&this.generateTable()}$willDestroy(){var t,n;if((t=this.element.parentNode)==null||t.removeChild(this.element),this.$$.charts.length===1){const r=document.getElementById(b.id);(n=r==null?void 0:r.parentNode)==null||n.removeChild(r)}}}})(),l=l.default,l})()});