UNPKG

billboard.js

Version:

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

14 lines (13 loc) 10.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.16.0 * @requires billboard.js * @summary billboard.js plugin */(function(l,u){typeof exports=="object"&&typeof module=="object"?module.exports=u(require("d3-delaunay"),require("d3-polygon"),require("d3-brush"),require("d3-selection")):typeof define=="function"&&define.amd?define("bb",["d3-delaunay","d3-polygon","d3-brush","d3-selection"],u):typeof exports=="object"?exports.bb=u(require("d3-delaunay"),require("d3-polygon"),require("d3-brush"),require("d3-selection")):(l.bb=l.bb||{},l.bb.plugin=l.bb.plugin||{},l.bb.plugin.textoverlap=u(l.d3,l.d3,l.d3,l.d3))})(this,function(w,l,u,q){return function(){"use strict";var R={263:function(e){e.exports=l},348:function(e){e.exports=w},747:function(e){e.exports=q},853:function(e){e.exports=u}},O={};function a(e){var t=O[e];if(t!==void 0)return t.exports;var n=O[e]={exports:{}};return R[e](n,n.exports,a),n.exports}(function(){a.d=function(e,t){for(var n in t)a.o(t,n)&&!a.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})}})(),function(){a.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)}}();var p={};a.d(p,{default:function(){return ie}});var B=a(348),P=a(263),pe=a(853),he=a(747);function F(){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 S(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 h=F(),I=h==null?void 0:h.document,[be,me,_e,ge]=S(h);var N=Object.defineProperty,E=Object.getOwnPropertySymbols,V=Object.prototype.hasOwnProperty,U=Object.prototype.propertyIsEnumerable,M=(e,t,n)=>t in e?N(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,T=(e,t)=>{for(var n in t||(t={}))V.call(t,n)&&M(e,n,t[n]);if(E)for(var n of E(t))U.call(t,n)&&M(e,n,t[n]);return e};const ye=e=>e||e===0,X=e=>typeof e=="function",b=e=>typeof e=="string",_=e=>typeof e=="number",Y=e=>typeof e=="undefined",$=e=>typeof e!="undefined",ve=e=>typeof e=="boolean",xe=e=>Math.ceil(e/10)*10,we=e=>Math.ceil(e)+.5,Oe=e=>e[1]-e[0],g=e=>typeof e=="object",L=e=>Y(e)||e===null||b(e)&&e.length===0||g(e)&&!(e instanceof Date)&&Object.keys(e).length===0||_(e)&&isNaN(e),z=e=>!L(e),y=e=>Array.isArray(e),m=e=>e&&!(e!=null&&e.nodeType)&&g(e)&&!y(e);function Pe(e,t,n){return $(e[t])?e[t]:n}function Ee(e,t){let n=!1;return Object.keys(e).forEach(o=>e[o]===t&&(n=!0)),n}function Me(e,t,...n){const o=X(e);return o&&e.call(t,...n),o}function Te(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 $e(e){return b(e)?e.replace(/<(script|img)?/ig,"&lt;").replace(/(script)?>/ig,"&gt;"):e}function je(e,t,n=[-1,1],o=!1){if(!(!e||!b(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((c,f)=>{e.append("tspan").attr("x",0).attr("dy",`${f===0?n[0]*s:n[1]}em`).text(c)})}}}function k(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 Ae(e){const{width:t,height:n}=A(e),o=k(e),r=o[0].x,i=Math.min(o[0].y,o[1].y);return{x:r,y:i,width:t,height:n}}function Ce(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 G(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 j(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 A(e,t=!1){return j(!0,e,t)}function De(e,t=!1){return j(!1,e,t)}function qe(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 C(e,t,n,o,r){if(n>o)return-1;const i=Math.floor((n+o)/2);let{x:s,w:c=0}=e[i];return r&&(s=e[i].y,c=e[i].h),t>=s&&t<=s+c?i:t<s?C(e,t,n,i-1,r):C(e,t,i+1,o,r)}function Re(e){const t=G(e);return t?t[0]===t[1]:!0}function Be(...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)=>T(T({},n),o))}function H(e={},t){y(t)&&t.forEach(n=>H(e,n));for(const n in t)/^\d+$/.test(n)||n in e||(e[n]=t[n]);return e}const Fe=e=>e.charAt(0).toUpperCase()+e.slice(1);function Se(e,t="-"){return e.split(t).map((n,o)=>o?n.charAt(0).toUpperCase()+n.slice(1).toLowerCase():n.toLowerCase()).join("")}const K=e=>[].slice.call(e);function Ie(e,t,n){const{rootSelector:o="",sheet:r}=e,s=`${o} ${(c=>c.replace(/\s?(bb-)/g,".$1").replace(/\.+/g,"."))(t)} {${n.join(";")}}`;return r[r.insertRule?"insertRule":"addRule"](s,r.cssRules.length)}function Ne(e){let t=[];return e.forEach(n=>{var o;try{n.cssRules&&n.cssRules.length&&(t=t.concat(K(n.cssRules)))}catch(r){(o=window.console)==null||o.warn(`Error while reading rules from ${n.href}: ${r.toString()}`)}}),t}function Ve(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 Ue(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 c=A(e);s.x-=c.x,s.y-=c.y}return s}function Xe(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 Ye(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 Le(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(o=>{if(!/^(__proto__|constructor|prototype)$/i.test(o)){const r=n[o];m(r)?(!e[o]&&(e[o]={}),e[o]=v(e[o],r)):e[o]=y(r)?r.concat():r}}),v(e,...t)}function W(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 ze(e,t){let n=t.filter(o=>z(o));return n.length?_(n[0])?n=Math[e](...n):n[0]instanceof Date&&(n=W(n,e==="min")[0]):n=void 0,n}const ke=(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},Ge={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=I.createEvent("MouseEvent");i.initMouseEvent(o,r.bubbles,r.cancelable,h,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(v({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 He(e,t){let n=e;for(const o in t)n=n.replace(new RegExp(`{=${o}}`,"g"),t[o]);return n}function Ke(e){var t;let n;if(e instanceof Date)n=e;else if(b(e)){const{config:o,format:r}=this;n=(t=r.dataTime(o.data_xFormat)(e))!=null?t:new Date(e)}else _(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 We(e){const t=e.attr("viewBox");return t?/(\d+(\.\d+)?){3}/.test(t):!1}function Je(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 Qe(){var e,t;return((e=document)==null?void 0:e.hidden)===!1||((t=document)==null?void 0:t.visibilityState)==="visible"}function Ze(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 f=r.userAgent;s=/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(f)||/\b(Android|Windows Phone|iPad|iPod)\b/i.test(f)}return e&&!i&&(o==null?void 0:o("(pointer:fine)").matches)&&"mouse"||s&&"touch"||"mouse"}function J(e,t){t()===!1?requestAnimationFrame(()=>J(e,t)):e()}function Q(e){const t=this.config;let n,o,r;const i=()=>{const s=o.shift();if(s&&n&&g(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(),$(r)&&(t[s]=r)}),this.api&&(this.state.orgConfig=e)}var Z=Object.defineProperty,ee=(e,t,n)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,x=(e,t,n)=>ee(e,typeof t!="symbol"?t+"":t,n);class D{constructor(t={}){x(this,"$$"),x(this,"options"),this.options=t}$beforeInit(){}$init(){}$afterInit(){}$redraw(){}$willDestroy(){Object.keys(this).forEach(t=>{this[t]=null,delete this[t]})}}x(D,"version","3.16.0");class te{constructor(){return{selector:void 0,extent:1,area:0}}}var ne=Object.defineProperty,oe=(e,t,n)=>t in e?ne(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,re=(e,t,n)=>oe(e,typeof t!="symbol"?t+"":t,n);class ie extends D{constructor(t){return super(t),re(this,"config"),this.config=new te,this}$init(){Q.call(this,this.options)}$redraw(){const{$$:{$el:t},config:{selector:n}}=this,o=n?t.main.selectAll(n):t.text;!o.empty()&&this.preventLabelOverlap(o)}generateVoronoi(t){const{$$:n}=this,{scale:o}=n,[r,i]=["x","y"].map(s=>o[s].domain());return[r[1],i[0]]=[i[0],r[1]],B.Delaunay.from(t).voronoi([...r,...i])}preventLabelOverlap(t){const{extent:n,area:o}=this.config,r=t.data().map(c=>[c.index,c.value]),i=this.generateVoronoi(r);let s=0;t.each(function(){const c=i.cellPolygon(s);if(c&&this){const[f,se]=r[s],[ce,le]=(0,P.polygonCentroid)(c),ae=Math.abs((0,P.polygonArea)(c)),d=Math.round(Math.atan2(le-se,ce-f)/Math.PI*2),ue=n*(d===0?1:-1),fe=d===-1?-n:n+5,de=Math.abs(d)===1?"middle":d===0?"start":"end";this.style.display=ae<o?"none":"",this.setAttribute("text-anchor",de),this.setAttribute("dy",`0.${d===1?71:35}em`),this.setAttribute("transform",`translate(${ue}, ${fe})`)}s++})}}return p=p.default,p}()});