UNPKG

billboard.js

Version:

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

12 lines (11 loc) 3.5 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.0.1 * @requires billboard.js * @summary billboard.js plugin */(function(c,l){typeof exports=="object"&&typeof module=="object"?module.exports=l(require("d3-delaunay")):typeof define=="function"&&define.amd?define("bb",["d3-delaunay"],l):typeof exports=="object"?exports.bb=l(require("d3-delaunay")):(c.bb=c.bb||{},c.bb.plugin=c.bb.plugin||{},c.bb.plugin.textoverlap=l(c.d3))})(this,function(_){return(function(){"use strict";var c={348:(function(t){t.exports=_})},l={};function u(t){var e=l[t];if(e!==void 0)return e.exports;var n=l[t]={exports:{}};return c[t](n,n.exports,u),n.exports}(function(){u.d=function(t,e){for(var n in e)u.o(e,n)&&!u.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})}})(),(function(){u.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)}})();var p={};u.d(p,{default:function(){return T}});var m=u(348);function x(t){const e=t.length;let n=0,s=t[e-1];for(let i=0;i<e;i++){const o=s;s=t[i],n+=o[1]*s[0]-o[0]*s[1]}return n/2}function g(t){const e=t.length;let n=0,s=0,i=0,o=t[e-1];for(let r=0;r<e;r++){const a=o;o=t[r];const f=a[0]*o[1]-o[0]*a[1];i+=f,n+=(a[0]+o[0])*f,s+=(a[1]+o[1])*f}return i*=3,[n/i,s/i]}const B=t=>t||t===0,H=t=>typeof t=="function",v=t=>typeof t=="string",$=t=>typeof t=="number",O=t=>typeof t=="undefined",w=t=>typeof t!="undefined",I=t=>typeof t=="boolean",S=t=>Math.ceil(t/10)*10,z=t=>Math.ceil(t)+.5,G=t=>t[1]-t[0],b=t=>typeof t=="object",A=t=>{for(const e in t)return!1;return!0},M=t=>O(t)||t===null||v(t)&&t.length===0||b(t)&&!(t instanceof Date)&&A(t)||$(t)&&isNaN(t),J=t=>!M(t),P=t=>Array.isArray(t),K=t=>t&&!(t!=null&&t.nodeType)&&b(t)&&!P(t);function j(t){const e=this.config;let n,s,i;const o=()=>{const r=s.shift();if(r&&n&&b(n)&&r in n)return n=n[r],o();if(!r)return n};Object.keys(e).forEach(r=>{n=t,s=r.split("_"),i=o(),w(i)&&(e[r]=i)}),this.api&&(this.state.orgConfig=t)}var C=Object.defineProperty,D=(t,e,n)=>e in t?C(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,h=(t,e,n)=>D(t,typeof e!="symbol"?e+"":e,n);class y{constructor(e={}){h(this,"$$"),h(this,"options"),h(this,"config"),this.options=e}loadConfig(){j.call(this,this.options)}$beforeInit(){}$init(){}$afterInit(){}$redraw(){}$willDestroy(){Object.keys(this).forEach(e=>{this[e]=null,delete this[e]})}}h(y,"version","4.0.1");class E{constructor(){return{selector:void 0,extent:1,area:0}}}class T extends y{constructor(e){return super(e),this.config=new E,this}$init(){this.loadConfig()}$redraw(){const{$$:{$el:e},config:{selector:n}}=this,s=n?e.main.selectAll(n):e.text;!s.empty()&&this.preventLabelOverlap(s)}generateVoronoi(e){const{$$:n}=this,{scale:s}=n,[i,o]=["x","y"].map(r=>s[r].domain());return[i[1],o[0]]=[o[0],i[1]],m.Delaunay.from(e).voronoi([...i,...o])}preventLabelOverlap(e){const{extent:n,area:s}=this.config,i=e.data().map(a=>[a.index,a.value]),o=this.generateVoronoi(i);let r=0;e.each(function(){const a=o.cellPolygon(r);if(a&&this){const[f,N]=i[r],[q,V]=g(a),F=Math.abs(x(a)),d=Math.round(Math.atan2(V-N,q-f)/Math.PI*2),L=n*(d===0?1:-1),U=d===-1?-n:n+5,k=Math.abs(d)===1?"middle":d===0?"start":"end";this.style.display=F<s?"none":"",this.setAttribute("text-anchor",k),this.setAttribute("dy",`0.${d===1?71:35}em`),this.setAttribute("transform",`translate(${L}, ${U})`)}r++})}}return p=p.default,p})()});