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