UNPKG

billboard.js

Version:

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

41 lines (40 loc) 16.9 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 3.18.0 * @requires billboard.js * @summary billboard.js plugin */(function(a,p){typeof exports=="object"&&typeof module=="object"?module.exports=p(require("d3-brush"),require("d3-selection")):typeof define=="function"&&define.amd?define("bb",["d3-brush","d3-selection"],p):typeof exports=="object"?exports.bb=p(require("d3-brush"),require("d3-selection")):(a.bb=a.bb||{},a.bb.plugin=a.bb.plugin||{},a.bb.plugin.tableview=p(a.d3,a.d3))})(this,function(N,a){return(function(){"use strict";var p={747:(function(e){e.exports=a}),853:(function(e){e.exports=N})},j={};function u(e){var t=j[e];if(t!==void 0)return t.exports;var n=j[e]={exports:{}};return p[e](n,n.exports,u),n.exports}(function(){u.d=function(e,t){for(var n in t)u.o(t,n)&&!u.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})}})(),(function(){u.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)}})();var y={};u.d(y,{default:function(){return Ve}});var ke=u(853),Ue=u(747);function J(){return typeof globalThis=="object"&&globalThis!==null&&globalThis.Object===Object&&globalThis||typeof global=="object"&&global!==null&&global.Object===Object&&global||typeof self=="object"&&self!==null&&self.Object===Object&&self||Function("return this")()}function ee(e){const t=typeof(e==null?void 0:e.requestAnimationFrame)=="function"&&typeof(e==null?void 0:e.cancelAnimationFrame)=="function",n=typeof(e==null?void 0:e.requestIdleCallback)=="function"&&typeof(e==null?void 0:e.cancelIdleCallback)=="function",o=i=>setTimeout(i,1),r=i=>clearTimeout(i);return[t?e.requestAnimationFrame:o,t?e.cancelAnimationFrame:r,n?e.requestIdleCallback:o,n?e.cancelIdleCallback:r]}const v=J(),te=v==null?void 0:v.document,[Ge,Xe,We,Ye]=ee(v),ne=new Set(["span","div","p","br","b","i","em","strong","u","s","sub","sup","ul","ol","li","dl","dt","dd","table","thead","tbody","tfoot","tr","th","td","caption","colgroup","col","hr","pre","code","blockquote","a","img","svg","g","path","circle","ellipse","rect","line","polyline","polygon","text","tspan","textPath","use","defs","symbol","clipPath","mask","linearGradient","radialGradient","stop","pattern","marker","title","desc"]),I=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"]),oe=new Set(["http:","https:","mailto:"]),re=new Set(["href","src","xlink:href"]),ie=/^<\/?([a-zA-Z][a-zA-Z0-9]*)/,se=/^<\/([a-zA-Z][a-zA-Z0-9]*)\s*>$/,le=/^<([a-zA-Z][a-zA-Z0-9]*)([\s\S]*?)(\/?)>$/,D=/([a-zA-Z][\w:-]*)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g,L=/url\s*\(\s*["']?([^"')]+)["']?\s*\)/gi,ce=["expression(","behavior:","binding:","@import","@charset","-moz-binding:"];function O(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 z(e){const n=O(e).trim().replace(/[\s\u0000-\u001f]/g,"").toLowerCase();if(!n||n.startsWith("#")||n.startsWith("/")||n.startsWith("./")||n.startsWith("../")||!n.includes(":"))return!0;const o=n.indexOf(":");if(o>0){const r=n.substring(0,o+1);return oe.has(r)}return!1}function ae(e){const n=O(e).replace(/[\u0000-\u001f]/g,"");L.lastIndex=0;let o;for(;(o=L.exec(n))!==null;)if(!z(o[1]))return null;const r=n.toLowerCase().replace(/\s/g,"");for(const i of ce)if(r.includes(i))return null;return e}const ue={'"':"&quot;","'":"&#39;","`":"&#96;"},fe=/["'`]/g;function A(e){return e.replace(fe,t=>ue[t])}function de(e,t,n=!1){if(re.has(e))return z(t)?n?A(t):t:null;if(e==="style"){const r=ae(t);return r===null?null:n?A(r):r}const o=O(t).toLowerCase().replace(/\s/g,"");return/\bon\w+=/.test(o)?null:n?A(t):t}function pe(e){const t=e.match(ie);return t?t[1].toLowerCase():null}function he(e){const t=pe(e);return t!==null&&ne.has(t)}function be(e){const t=e.match(se);if(t)return`</${t[1].toLowerCase()}>`;const n=e.match(le);if(!n)return"";const[,o,r,i]=n,s=o.toLowerCase(),l=[];D.lastIndex=0;let c;for(;(c=D.exec(r))!==null;){const b=c[1].toLowerCase(),Z=c[2],Q=c[3],M=c[4];if(b.startsWith("on"))continue;let T,w;if(Z!==void 0)T=Z,w='"';else if(Q!==void 0)T=Q,w="'";else if(M!==void 0)T=M,w='"';else{I.has(b)&&l.push(b);continue}if(I.has(b)){const K=de(b,T,M!==void 0);K!==null&&l.push(`${b}=${w}${K}${w}`)}}const f=l.length>0?` ${l.join(" ")}`:"";return`<${s}${f}${i?"/>":">"}`}function ge(e){return typeof e!="string"||!e||e.indexOf("<")===-1?e:e.replace(/<\/?[^>]*>|[^<>\s]+>/g,t=>t.startsWith("<!--")?"":t.startsWith("<")?he(t)?be(t):t.replace(/</g,"&lt;"):t.slice(0,-1)+"&gt;")}var me=Object.defineProperty,B=Object.getOwnPropertySymbols,_e=Object.prototype.hasOwnProperty,we=Object.prototype.propertyIsEnumerable,q=(e,t,n)=>t in e?me(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,F=(e,t)=>{for(var n in t||(t={}))_e.call(t,n)&&q(e,n,t[n]);if(B)for(var n of B(t))we.call(t,n)&&q(e,n,t[n]);return e};function V(e,t,n=!1){const o=r=>r[e?"getBoundingClientRect":"getBBox"]();return n?o(t):!("rect"in t)||"rect"in t&&t.hasAttribute("width")&&t.rect.width!==+(t.getAttribute("width")||0)?t.rect=o(t):t.rect}function k(e,t){for(let n=0;n<e.length;n++){const o=e[n];o&&t(o,n)}}const He=e=>e||e===0,ye=e=>typeof e=="function",g=e=>typeof e=="string",x=e=>typeof e=="number",ve=e=>typeof e=="undefined",U=e=>typeof e!="undefined",Ze=e=>typeof e=="boolean",Qe=e=>Math.ceil(e/10)*10,Ke=e=>Math.ceil(e)+.5,Je=e=>e[1]-e[0],$=e=>typeof e=="object",xe=e=>{for(const t in e)return!1;return!0},Ee=e=>ve(e)||e===null||g(e)&&e.length===0||$(e)&&!(e instanceof Date)&&xe(e)||x(e)&&isNaN(e),Te=e=>!Ee(e),S=e=>Array.isArray(e),m=e=>e&&!(e!=null&&e.nodeType)&&$(e)&&!S(e);function et(e,t,n){return U(e[t])?e[t]:n}function tt(e,t){let n=!1;return Object.keys(e).forEach(o=>e[o]===t&&(n=!0)),n}function nt(e,t,...n){const o=ye(e);return o&&e.call(t,...n),o}function ot(e,t){let n=0;const o=function(...r){!--n&&t.apply(this,...r)};"duration"in e?e.each(()=>++n).on("end",o):(++n,e.call(o))}function rt(e,t,n=[-1,1],o=!1){if(!(!e||!g(t)))if(t.indexOf(` `)===-1)e.text(t);else{const r=[e.text(),t].map(i=>i.replace(/[\s\n]/g,""));if(r[0]!==r[1]){const i=t.split(` `),s=o?i.length-1:1;e.html(""),i.forEach((l,c)=>{e.append("tspan").attr("x",0).attr("dy",`${c===0?n[0]*s:n[1]}em`).text(l)})}}}function Oe(e){const{x:t,y:n,width:o,height:r}=e.getBBox();return[{x:t,y:n+r},{x:t,y:n},{x:t+o,y:n},{x:t+o,y:n+r}]}function it(e){const{width:t,height:n}=G(e),o=Oe(e),r=o[0].x,i=Math.min(o[0].y,o[1].y);return{x:r,y:i,width:t,height:n}}function st(e,t){var n;const o=e&&((n=e.touches||e.sourceEvent&&e.sourceEvent.touches)==null?void 0:n[0]);let r=[0,0];try{r=d3Pointer(o||e,t)}catch(i){}return r.map(i=>isNaN(i)?0:i)}function Ae(e){const{event:t,$el:n}=e,o=n.subchart.main||n.main;let r;return t&&t.type==="brush"?r=t.selection:o&&(r=o.select(".bb-brush").node())&&(r=d3BrushSelection(r)),r}function G(e,t=!1){return V(!0,e,t)}function lt(e,t=!1){return V(!1,e,t)}function ct(e=!0,t=0,n=1e4){const o=window.crypto||window.msCrypto,r=o?t+o.getRandomValues(new Uint32Array(1))[0]%(n-t+1):Math.floor(Math.random()*(n-t)+t);return e?String(r):r}function X(e,t,n,o,r){if(n>o)return-1;const i=Math.floor((n+o)/2);let{x:s,w:l=0}=e[i];return r&&(s=e[i].y,l=e[i].h),t>=s&&t<=s+l?i:t<s?X(e,t,n,i-1,r):X(e,t,i+1,o,r)}function at(e){const t=Ae(e);return t?t[0]===t[1]:!0}function ut(...e){const t=n=>{if(m(n)&&n.constructor){const o=new n.constructor;for(const r in n)o[r]=t(n[r]);return o}return n};return e.map(n=>t(n)).reduce((n,o)=>F(F({},n),o))}function $e(e={},t){S(t)&&t.forEach(n=>$e(e,n));for(const n in t)/^\d+$/.test(n)||n in e||(e[n]=t[n]);return e}const ft=e=>e.charAt(0).toUpperCase()+e.slice(1);function dt(e,t="-"){return e.split(t).map((n,o)=>o?n.charAt(0).toUpperCase()+n.slice(1).toLowerCase():n.toLowerCase()).join("")}const Se=e=>[].slice.call(e);function pt(e,t,n){const{rootSelector:o="",sheet:r}=e,s=`${o} ${(l=>l.replace(/\s?(bb-)/g,".$1").replace(/\.+/g,"."))(t)} {${n.join(";")}}`;return r[r.insertRule?"insertRule":"addRule"](s,r.cssRules.length)}function ht(e){let t=[];return e.forEach(n=>{var o;try{n.cssRules&&n.cssRules.length&&(t=t.concat(Se(n.cssRules)))}catch(r){(o=window.console)==null||o.warn(`Error while reading rules from ${n.href}: ${r.toString()}`)}}),t}function bt(e){var t,n,o,r,i,s;return{x:((n=(t=window.pageXOffset)!=null?t:window.scrollX)!=null?n:0)+((o=e.scrollLeft)!=null?o:0),y:((i=(r=window.pageYOffset)!=null?r:window.scrollY)!=null?i:0)+((s=e.scrollTop)!=null?s:0)}}function gt(e,t=0,n=0,o=!0){const r=new DOMPoint(t,n),i=e.getScreenCTM(),s=r.matrixTransform(o?i==null?void 0:i.inverse():i);if(o===!1){const l=G(e);s.x-=l.x,s.y-=l.y}return s}function mt(e){const t=e?e.transform:null,n=t&&t.baseVal;return n&&n.numberOfItems?n.getItem(0).matrix:{a:0,b:0,c:0,d:0,e:0,f:0}}function _t(e){const t=e[0]instanceof Date,n=(t?e.map(Number):e).filter((o,r,i)=>i.indexOf(o)===r);return t?n.map(o=>new Date(o)):n}function wt(e){return e&&e.length?e.reduce((t,n)=>t.concat(n)):[]}function C(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(o=>{if(!/^(__proto__|constructor|prototype)$/i.test(o)){const r=n[o];m(r)?(!e[o]&&(e[o]={}),e[o]=C(e[o],r)):e[o]=S(r)?r.concat():r}}),C(e,...t)}function Ce(e,t=!0){let n;return e[0]instanceof Date?n=t?(o,r)=>o-r:(o,r)=>r-o:t&&!e.every(isNaN)?n=(o,r)=>o-r:t||(n=(o,r)=>o>r&&-1||o<r&&1||o===r&&0),e.concat().sort(n)}function yt(e,t){let n=t.filter(o=>Te(o));return n.length?x(n[0])?n=Math[e](...n):n[0]instanceof Date&&(n=Ce(n,e==="min")[0]):n=void 0,n}const vt=(e,t,n=1)=>{const o=[],r=Math.max(0,Math.ceil((t-e)/n))|0;for(let i=e;i<r;i++)o.push(e+i*n);return o},xt={mouse:(()=>{const e=()=>({bubbles:!1,cancelable:!1,screenX:0,screenY:0,clientX:0,clientY:0});try{return new MouseEvent("t"),(t,n,o=e())=>{t.dispatchEvent(new MouseEvent(n,o))}}catch(t){return(n,o,r=e())=>{const i=te.createEvent("MouseEvent");i.initMouseEvent(o,r.bubbles,r.cancelable,v,0,r.screenX,r.screenY,r.clientX,r.clientY,!1,!1,!1,!1,0,null),n.dispatchEvent(i)}}})(),touch:(e,t,n)=>{const o=new Touch(C({identifier:Date.now(),target:e,radiusX:2.5,radiusY:2.5,rotationAngle:10,force:.5},n));e.dispatchEvent(new TouchEvent(t,{cancelable:!0,bubbles:!0,shiftKey:!0,touches:[o],targetTouches:[],changedTouches:[o]}))}};function E(e,t){let n=e;for(const o in t)n=n.replace(new RegExp(`{=${o}}`,"g"),t[o]);return ge(n)}function Et(e){var t;let n;if(e instanceof Date)n=e;else if(g(e)){const{config:o,format:r}=this;n=(t=r.dataTime(o.data_xFormat)(e))!=null?t:new Date(e)}else x(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 Tt(e){const t=e.attr("viewBox");return t?/(\d+(\.\d+)?){3}/.test(t):!1}function Ot(e,t,n=!1){const o=!!e.node;let r=!1;for(const[i,s]of Object.entries(t))if(r=o?e.style(i)===s:e.style[i]===s,n===!1&&r)break;return r}function At(){var e,t;return((e=document)==null?void 0:e.hidden)===!1||((t=document)==null?void 0:t.visibilityState)==="visible"}function $t(e,t){const{DocumentTouch:n,matchMedia:o,navigator:r}=window,i=o==null?void 0:o("(pointer:coarse)").matches;let s=!1;if(t)if(r&&"maxTouchPoints"in r)s=r.maxTouchPoints>0;else if("ontouchmove"in window||n&&document instanceof n)s=!0;else if(i)s=!0;else{const c=r.userAgent;s=/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(c)||/\b(Android|Windows Phone|iPad|iPod)\b/i.test(c)}return e&&!i&&(o==null?void 0:o("(pointer:fine)").matches)&&"mouse"||s&&"touch"||"mouse"}function Pe(e,t){t()===!1?requestAnimationFrame(()=>Pe(e,t)):e()}function St(e){if(m(e)&&!g(e)){const s=e;return{top:s.top||0,right:s.right||0,bottom:s.bottom||0,left:s.left||0}}const t=(g(e)?e.trim().split(/\s+/):[e]).map(s=>+s||0),[n,o=n,r=n,i=o]=t;return{top:n,right:o,bottom:r,left:i}}function Ct(e,t){e.pendingRaf!==null?(window.cancelAnimationFrame(e.pendingRaf),e.pendingRaf=window.requestAnimationFrame(()=>{e.pendingRaf=null,t()})):(e.pendingRaf=window.requestAnimationFrame(()=>{e.pendingRaf=null}),t())}function Pt(e,t=(n=>n)){const n=new Set;return k(e,(o,r)=>{n.add(t(o,r))}),n}function Rt(e,t,n=(o=>o)){const o=new Map;return k(e,(r,i)=>{o.set(t(r,i),n(r,i))}),o}function Re(e){const t=this.config;let n,o,r;const i=()=>{const s=o.shift();if(s&&n&&$(n)&&s in n)return n=n[s],i();if(!s)return n};Object.keys(t).forEach(s=>{n=e,o=s.split("_"),r=i(),U(r)&&(t[s]=r)}),this.api&&(this.state.orgConfig=e)}var Me=Object.defineProperty,Ne=(e,t,n)=>t in e?Me(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,P=(e,t,n)=>Ne(e,typeof t!="symbol"?t+"":t,n);class W{constructor(t={}){P(this,"$$"),P(this,"options"),this.options=t}$beforeInit(){}$init(){}$afterInit(){}$redraw(){}$willDestroy(){Object.keys(this).forEach(t=>{this[t]=null,delete this[t]})}}P(W,"version","3.18.0");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; }`},_={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:"-"}}}var Ie=Object.defineProperty,De=Object.defineProperties,Le=Object.getOwnPropertyDescriptors,Y=Object.getOwnPropertySymbols,ze=Object.prototype.hasOwnProperty,Be=Object.prototype.propertyIsEnumerable,R=(e,t,n)=>t in e?Ie(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,qe=(e,t)=>{for(var n in t||(t={}))ze.call(t,n)&&R(e,n,t[n]);if(Y)for(var n of Y(t))Be.call(t,n)&&R(e,n,t[n]);return e},Fe=(e,t)=>De(e,Le(t)),H=(e,t,n)=>R(e,typeof t!="symbol"?t+"":t,n);class Ve extends W{constructor(t){return super(t),H(this,"config"),H(this,"element"),this.config=new je,this}$beforeInit(){Re.call(this,this.options)}$init(){const{class:t,selector:n,style:o}=this.config;let r=document.querySelector(n||`.${t||h.class}`);if(!r){const i=this.$$.$el.chart.node();r=document.createElement("table"),i.parentNode.insertBefore(r,i.nextSibling)}if(r.tagName!=="TABLE"){const i=document.createElement("table");r.appendChild(i),r=i}if(o&&!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)}r.classList.add(...[o&&h.class,t].filter(Boolean)),this.element=r}generateTable(){const{$$:t,config:n,element:o}=this,r=t.filterTargetsToShow(t.data.targets);let i=E(_.thead,{title:r.length?this.config.categoryTitle:""}),s="";const l=[];r.forEach(c=>{i+=E(_.thead,{title:c.id}),c.values.forEach((f,d)=>{l[d]||(l[d]=[f.x]),l[d].push(f.value)})}),l.forEach(c=>{s+=`<tr>${c.map((f,d)=>E(d?_.tbody:_.tbodyHeader,{value:d===0?n.categoryFormat.bind(this)(f):x(f)?f.toLocaleString():n.nullString})).join("")}</tr>`}),o.innerHTML=E(_.body,Fe(qe({},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 o=document.getElementById(h.id);(n=o==null?void 0:o.parentNode)==null||n.removeChild(o)}}}return y=y.default,y})()});