billboard.js
Version:
Re-usable easy interface JavaScript chart library, based on D3 v4+
39 lines (38 loc) • 12.3 kB
JavaScript
/*!
* 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.0.1
* @requires billboard.js
* @summary billboard.js plugin
*/(function(a,d){typeof exports=="object"&&typeof module=="object"?module.exports=d():typeof define=="function"&&define.amd?define("bb",[],d):typeof exports=="object"?exports.bb=d():(a.bb=a.bb||{},a.bb.plugin=a.bb.plugin||{},a.bb.plugin.tableview=d())})(this,function(){return(function(){"use strict";var p={};(function(){p.d=function(e,t){for(var n in t)p.o(t,n)&&!p.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})}})(),(function(){p.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)}})();var a={};p.d(a,{default:function(){return Ge}});const d=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"]),Y=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"]),w=new Map;d.forEach(e=>w.set(e.toLowerCase(),e));const v=new Map;Y.forEach(e=>v.set(e.toLowerCase(),e));const J=new Set(["http:","https:","mailto:"]),K=new Set(["href","src","xlink:href"]),ee=/^<\/?([a-zA-Z][a-zA-Z0-9]*)/,te=/^<\/([a-zA-Z][a-zA-Z0-9]*)\s*>$/,ne=/^<([a-zA-Z][a-zA-Z0-9]*)([\s\S]*?)(\/?)>$/,L=/([a-zA-Z][\w:-]*)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g,M=/url\s*\(\s*["']?([^"')]+)["']?\s*\)/gi,re=["expression(","behavior:","binding:","@import","@charset","-moz-binding:"];function A(e){return e.replace(/:/gi,":").replace(/&newline;/gi,`
`).replace(/&tab;/gi," ").replace(/ /gi," ").replace(/</gi,"<").replace(/>/gi,">").replace(/&/gi,"&").replace(/"/gi,'"').replace(/'/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 z(e){const n=A(e).trim().replace(/[\s\u0000-\u001f]/g,"").toLowerCase();if(!n||n.startsWith("#"))return!0;const r=n.match(/^[^/?#]*:/);return r?J.has(r[0]):!0}function oe(e){const n=A(e).replace(/[\u0000-\u001f]/g,"");M.lastIndex=0;let r;for(;(r=M.exec(n))!==null;)if(!z(r[1]))return null;const o=n.toLowerCase().replace(/\s/g,"");for(const i of re)if(o.includes(i))return null;return e}const ie={'"':""","'":"'","`":"`"},se=/["'`]/g;function x(e){return e.replace(se,t=>ie[t])}function ae(e,t,n=!1){if(K.has(e))return z(t)?n?x(t):t:null;if(e==="style"){const o=oe(t);return o===null?null:n?x(o):o}const r=A(t).toLowerCase().replace(/\s/g,"");return/\bon\w+=/.test(r)?null:n?x(t):t}function le(e){const t=e.match(ee);return t?t[1].toLowerCase():null}function ce(e){const t=le(e);return t!==null&&w.has(t)}function fe(e){var t,n,r;const o=e.match(te);if(o){const f=o[1].toLowerCase();return`</${(t=w.get(f))!=null?t:f}>`}const i=e.match(ne);if(!i)return"";const[,s,l,g]=i,c=s.toLowerCase(),u=(n=w.get(c))!=null?n:c,S=[];L.lastIndex=0;let _;for(;(_=L.exec(l))!==null;){const f=_[1].toLowerCase(),H=_[2],X=_[3],D=_[4];if(f.startsWith("on"))continue;const Z=(r=v.get(f))!=null?r:f;let T,y;if(H!==void 0)T=H,y='"';else if(X!==void 0)T=X,y="'";else if(D!==void 0)T=D,y='"';else{v.has(f)&&S.push(Z);continue}if(v.has(f)){const Q=ae(f,T,D!==void 0);Q!==null&&S.push(`${Z}=${y}${Q}${y}`)}}const Ue=S.length>0?` ${S.join(" ")}`:"";return`<${u}${Ue}${g?"/>":">"}`}function ue(e){return typeof e!="string"||!e||e.indexOf("<")===-1?e:e.replace(/<\/?[^>]*>|[^<>\s]+>/g,t=>t.startsWith("<!--")?"":t.startsWith("<")?ce(t)?fe(t):t.replace(/</g,"<"):t.slice(0,-1)+">")}var pe,I,de,$,m,he,R,C,ge=Object.defineProperty,j=Object.getOwnPropertySymbols,me=Object.prototype.hasOwnProperty,be=Object.prototype.propertyIsEnumerable,k=(e,t,n)=>t in e?ge(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,G=(e,t)=>{for(var n in t||(t={}))me.call(t,n)&&k(e,n,t[n]);if(j)for(var n of j(t))be.call(t,n)&&k(e,n,t[n]);return e};function U(e,t){for(let n=0;n<e.length;n++){const r=e[n];r!==null&&I(r)&&t(r,n)}}function Ve(e,t,n){return I(e[t])?e[t]:n}function Fe(e,t){for(const n in e)if(e[n]===t)return!0;return!1}function qe(e,t,...n){const r=de(e);return r&&e.call(t,...n),r}function Be(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 We=e=>e.charAt(0).toUpperCase()+e.slice(1);function He(e,t="-"){return e.split(t).map((n,r)=>r?n.charAt(0).toUpperCase()+n.slice(1).toLowerCase():n.toLowerCase()).join("")}const Xe=e=>[].slice.call(e);function Ze(...e){const t=n=>{if($(n))return n.map(t);if(m(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)=>G(G({},n),r))}function _e(e={},t){$(t)&&t.forEach(n=>_e(e,n));for(const n in t)/^\d+$/.test(n)||n in e||(e[n]=t[n]);return e}function Qe(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 Ye(e){return e&&e.length?e.reduce((t,n)=>t.concat(n)):[]}function V(e,...t){if(!t.length||t.length===1&&!t[0])return e;const n=t.shift();return m(e)&&m(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()):m(o)?(!e[r]&&(e[r]={}),e[r]=V(e[r],o)):e[r]=$(o)?o.concat():o}}),V(e,...t)}function ye(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 Je(e,t){let n=t.filter(r=>he(r));if(n.length)if(R(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=ye(n,e==="min")[0]);else n=void 0;return n}const Ke=(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 we=0;function et(e=!0){const t=++we;return e?String(t):t}function F(e,t,n,r,o){if(n>r)return-1;const i=Math.floor((n+r)/2);let{x:s,w:l=0}=e[i];return o&&(s=e[i].y,l=e[i].h),t>=s&&t<=s+l?i:t<s?F(e,t,n,i-1,o):F(e,t,i+1,r,o)}function E(e,t){return ue(e.replace(/\{=([^}]+)\}/g,(n,r)=>{var o;return(o=t[r])!=null?o:""}))}function tt(e){var t;let n;if(e instanceof Date)n=e;else if(C(e)){const{config:r,format:o}=this;n=(t=o.dataTime(r.data_xFormat)(e))!=null?t:new Date(e)}else R(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 nt(e){if(m(e)&&!C(e)){const s=e;return{top:s.top||0,right:s.right||0,bottom:s.bottom||0,left:s.left||0}}const t=(C(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 ve(e,t){t()===!1?pe(()=>ve(e,t)):e()}function rt(e,t=(n=>n)){const n=new Set;return U(e,(r,o)=>{n.add(t(r,o))}),n}function ot(e,t,n=(r=>r)){const r=new Map;return U(e,(o,i)=>{r.set(t(o,i),n(o,i))}),r}const it=e=>e||e===0,st=e=>typeof e=="function",Ee=e=>typeof e=="string",q=e=>typeof e=="number",Oe=e=>typeof e=="undefined",Se=e=>typeof e!="undefined",at=e=>typeof e=="boolean",lt=e=>Math.ceil(e/10)*10,ct=e=>Math.ceil(e)+.5,ft=e=>e[1]-e[0],P=e=>typeof e=="object",Te=e=>{for(const t in e)return!1;return!0},Ae=e=>Oe(e)||e===null||Ee(e)&&e.length===0||P(e)&&!(e instanceof Date)&&Te(e)||q(e)&&isNaN(e),ut=e=>!Ae(e),xe=e=>Array.isArray(e),pt=e=>e&&!(e!=null&&e.nodeType)&&P(e)&&!xe(e);function $e(e){const t=this.config;let n,r,o;const i=()=>{const s=r.shift();if(s&&n&&P(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(),Se(o)&&(t[s]=o)}),this.api&&(this.state.orgConfig=e)}var Ce=Object.defineProperty,Pe=(e,t,n)=>t in e?Ce(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,O=(e,t,n)=>Pe(e,typeof t!="symbol"?t+"":t,n);class B{constructor(t={}){O(this,"$$"),O(this,"options"),O(this,"config"),this.options=t}loadConfig(){$e.call(this,this.options)}$beforeInit(){}$init(){}$afterInit(){}$redraw(){}$willDestroy(){Object.keys(this).forEach(t=>{this[t]=null,delete this[t]})}}O(B,"version","4.0.1");const h={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;
}`},b={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 Ne{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 De=Object.defineProperty,Le=Object.defineProperties,Me=Object.getOwnPropertyDescriptors,W=Object.getOwnPropertySymbols,ze=Object.prototype.hasOwnProperty,Ie=Object.prototype.propertyIsEnumerable,N=(e,t,n)=>t in e?De(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Re=(e,t)=>{for(var n in t||(t={}))ze.call(t,n)&&N(e,n,t[n]);if(W)for(var n of W(t))Ie.call(t,n)&&N(e,n,t[n]);return e},je=(e,t)=>Le(e,Me(t)),ke=(e,t,n)=>N(e,typeof t!="symbol"?t+"":t,n);class Ge extends B{constructor(t){return super(t),ke(this,"element"),this.config=new Ne,this}$beforeInit(){this.loadConfig()}$init(){const{class:t,selector:n,style:r}=this.config;let o=document.querySelector(n||`.${t||h.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(h.id)){const i=document.createElement("style");i.id=h.id,i.innerHTML=h.rule,(document.head||document.getElementsByTagName("head")[0]).appendChild(i)}o.classList.add(...[r&&h.class,t].filter(Boolean)),this.element=o}generateTable(){const{$$:t,config:n,element:r}=this,o=t.filterTargetsToShow(t.data.targets);let i=E(b.thead,{title:o.length?this.config.categoryTitle:""}),s="";const l=[];o.forEach(g=>{i+=E(b.thead,{title:g.id}),g.values.forEach((c,u)=>{l[u]||(l[u]=[c.x]),l[u].push(c.value)})}),l.forEach(g=>{s+=`<tr>${g.map((c,u)=>E(u?b.tbody:b.tbodyHeader,{value:u===0?n.categoryFormat.bind(this)(c):q(c)?n.numberFormat.bind(this)(c):n.nullString})).join("")}</tr>`}),r.innerHTML=E(b.body,je(Re({},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(h.id);(n=r==null?void 0:r.parentNode)==null||n.removeChild(r)}}}return a=a.default,a})()});