diff --git a/dist/chartjs-plugin-piechart-outlabels.js b/dist/chartjs-plugin-piechart-outlabels.js index 9c87ef7..b482edb 100644 --- a/dist/chartjs-plugin-piechart-outlabels.js +++ b/dist/chartjs-plugin-piechart-outlabels.js @@ -298,13 +298,13 @@ var outlabeledCharts = { 'use strict'; var positioners = { - center: function(arc, stretch) { + center: function(arc, stretch, invert) { var angle = (arc.startAngle + arc.endAngle) / 2; var cosA = Math.cos(angle); var sinA = Math.sin(angle); var d = arc.outerRadius; - var stretchedD = d + stretch; + var stretchedD = invert ? d - stretch : d + stretch; return { x: arc.x + cosA * stretchedD, y: arc.y + sinA * stretchedD, @@ -436,14 +436,15 @@ var classes = { this.predictedOffset = this.offset; var angle = -((el._model.startAngle + el._model.endAngle) / 2) / (Math.PI); + let innerLabel = el._model.circumference > 0.5; var val = Math.abs(angle - Math.trunc(angle)); if (val > 0.45 && val < 0.55) { this.predictedOffset.x = 0; } else if (angle <= 0.45 && angle >= -0.45) { - this.predictedOffset.x = this.size.width / 2; + this.predictedOffset.x = this.size.width / (innerLabel ? -2 : 2); } else if (angle >= -1.45 && angle <= -0.55) { - this.predictedOffset.x = -this.size.width / 2; + this.predictedOffset.x = -this.size.width / (innerLabel ? -2 : 2); } }; @@ -597,7 +598,8 @@ var classes = { this.update = function(view, elements, max) { - this.center = positioners.center(view, this.stretch); + let innerLabel = el._model.circumference > 0.5; + this.center = positioners.center(view, this.stretch, innerLabel); this.moveLabelToOffset(); this.center.x += this.offset.x; @@ -635,6 +637,7 @@ var classes = { this.center.copy.y += .5; } } + return innerLabel; }; this.moveLabelToOffset = function() { @@ -660,6 +663,7 @@ outlabeledCharts.init(); Chart$1.defaults.global.plugins.outlabels = defaults; var LABEL_KEY = defaults.LABEL_KEY; +let activeTooltip = null; function configure(dataset, options) { var override = dataset.outlabels; @@ -682,6 +686,46 @@ Chart$1.plugins.register({ chart.sizeChanged = true; }, + beforeEvent: function(chart, event, options) { + const { + x, + y + } = event; + + const { + tooltipToggle, + toggleOffDistance + } = options; + + const elems = chart.getDatasetMeta(0).data; + let center, data; + + if (activeTooltip && (Math.abs(activeTooltip.x - x) > toggleOffDistance || Math.abs(activeTooltip.y - y) > toggleOffDistance)) { + tooltipToggle(null, null); + activeTooltip = null; + } + for (let i = 0; i < elems.length; i++) { + if (!elems[i].$outlabels) { + continue; + } + const within = elems[i].$outlabels.containsPoint({ + x: event.x, + y: event.y, + }); + if (within) { + center = elems[i].$outlabels.center; + const index = elems[i]._index; + data = chart.data.labels[index]; + tooltipToggle(center, data); + activeTooltip = center; + return; + } + } + + return; + }, + + afterDatasetUpdate: function(chart, args, options) { var labels = chart.config.data.labels; var dataset = chart.data.datasets[args.index]; @@ -699,7 +743,7 @@ Chart$1.plugins.register({ percent = dataset.data[i] / args.meta.total; newLabel = null; - if (display && el && !el.hidden) { + if (display && el && !el.hidden && el._model.circumference > options.tooltipCutoff) { try { context = { chart: chart, @@ -746,8 +790,8 @@ Chart$1.plugins.register({ } if (i < elements.length) { - label.update(el._view, elements, i); - label.drawLine(ctx); + const innerLabel = label.update(el._view, elements, i); + innerLabel || label.drawLine(ctx); } else { label.draw(ctx); } diff --git a/dist/chartjs-plugin-piechart-outlabels.min.js b/dist/chartjs-plugin-piechart-outlabels.min.js deleted file mode 100644 index 155b386..0000000 --- a/dist/chartjs-plugin-piechart-outlabels.min.js +++ /dev/null @@ -1,10 +0,0 @@ -/*! - * chartjs-plugin-piechart-outlabels - * http://chartjs.org/ - * Version: 0.1.3 - * - * Copyright 2018 Neckster - * Released under the MIT license - * https://github.com/Neckster/chartjs-plugin-piechart-outlabels/blob/master/LICENSE - */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(require("chart.js")):"function"==typeof define&&define.amd?define(["chart.js"],e):e(t.Chart)}(this,function(t){"use strict";function e(t,e){var i=t.outlabels,r={};return i===!1?null:(i===!0&&(i={}),s.merge(r,[e,i]))}t=t&&t.hasOwnProperty("default")?t["default"]:t;var i=t.helpers,s=i.merge(i,{toFontString:function(t){return!t||i.isNullOrUndef(t.size)||i.isNullOrUndef(t.family)?null:(t.style?t.style+" ":"")+(t.weight?t.weight+" ":"")+t.size+"px "+t.family},textSize:function(t,e,i){var s,r=[].concat(e),h=r.length,n=t.font,o=0;for(t.font=i.string,s=0;si?i:s}}),r={LABEL_KEY:"$outlabels",backgroundColor:function(t){return t.dataset.backgroundColor},borderColor:function(t){return t.dataset.backgroundColor},lineColor:function(t){return t.dataset.backgroundColor},borderRadius:0,borderWidth:0,lineWidth:2,color:"white",display:!0,font:{family:void 0,size:void 0,style:void 0,weight:null,maxSize:null,minSize:null,resizable:!0},lineHeight:1.2,padding:{top:4,right:4,bottom:4,left:4},textAlign:"center",stretch:40,text:"%l %p",zoomOutPercentage:50,percentPrecision:1,valuePrecision:3},h={init:function(){Math.trunc||(Math.trunc=function(t){return t=+t,t-t%1||(isFinite(t)&&0!==t?t<0?-0:0:t)}),Chart.defaults.outlabeledDoughnut=Chart.defaults.doughnut,Chart.defaults.outlabeledPie=Chart.defaults.pie;var t=function(t){Chart.controllers.doughnut.prototype.update.call(this);var e=this,i=e.getMeta(),s=e.chart.options.zoomOutPercentage||r.zoomOutPercentage;e.outerRadius*=1-s/100,e.innerRadius*=1-s/100,Chart.helpers.each(i.data,function(i,s){e.updateElement(i,s,t)})},e=Chart.controllers.doughnut.extend({update:t}),i=Chart.controllers.pie.extend({update:t});Chart.controllers.outlabeledPie=i,Chart.controllers.outlabeledDoughnut=e}},n={center:function(t,e){var i=(t.startAngle+t.endAngle)/2,s=Math.cos(i),r=Math.sin(i),h=t.outerRadius,n=h+e;return{x:t.x+s*n,y:t.y+r*n,d:n,arc:t,anchor:{x:t.x+s*h,y:t.y+r*h},copy:{x:t.x+s*n,y:t.y+r*n}}},moveFromAnchor:function(t,e){var i=t.arc,s=t.d,r=(i.startAngle+i.endAngle)/2,h=Math.cos(r),n=Math.sin(r);return s+=e,{x:i.x+h*s,y:i.y+n*s,d:s,arc:i,anchor:t.anchor,copy:{x:i.x+h*s,y:i.y+n*s}}}},o=t.helpers,l=r.LABEL_KEY,a={OutLabel:function(e,i,s,h,a){var c=t.helpers.options.resolve;if(!c([h.display,!0],a,i))throw new Error("Label display property is set to false.");var d=a.dataset.data[i],f=a.labels[i],u=c([h.text,r.text],a,i);u=u.replace(/%l/gi,f),(u.match(/%v\.?(\d*)/gi)||[]).map(function(t){var e=t.replace(/%v\./gi,"");return e.length?+e:h.valuePrecision||r.valuePrecision}).forEach(function(t){u=u.replace(/%v\.?(\d*)/i,d.toFixed(t))}),(u.match(/%p\.?(\d*)/gi)||[]).map(function(t){var e=t.replace(/%p\./gi,"");return e.length?+e:h.percentPrecision||r.percentPrecision}).forEach(function(t){u=u.replace(/%p\.?(\d*)/i,(100*a.percent).toFixed(t)+"%")});var x=u.match(/[^\r\n]+/g);if(!x||!x.length)throw new Error("No text to show.");for(var g=0;g.45&&u<.55?this.predictedOffset.x=0:l<=.45&&l>=-.45?this.predictedOffset.x=this.size.width/2:l>=-1.45&&l<=-.55&&(this.predictedOffset.x=-this.size.width/2)},this.init(u,x),this.computeLabelRect=function(){var t=this.textRect.width+2*this.style.borderWidth,e=this.textRect.height+2*this.style.borderWidth,i=this.textRect.x-this.style.padding.left-this.style.borderWidth,s=this.textRect.y-this.style.padding.top-this.style.borderWidth;return t+=this.style.padding.width,e+=this.style.padding.height,{x:i,y:s,width:t,height:e}},this.computeTextRect=function(){return{x:this.center.x-this.size.width/2,y:this.center.y-this.size.height/2,width:this.size.width,height:this.size.height}},this.getPoints=function(){return[{x:this.labelRect.x,y:this.labelRect.y},{x:this.labelRect.x+this.labelRect.width,y:this.labelRect.y},{x:this.labelRect.x+this.labelRect.width,y:this.labelRect.y+this.labelRect.height},{x:this.labelRect.x,y:this.labelRect.y+this.labelRect.height}]},this.containsPoint=function(t,e){return e||(e=5),this.textRect.x-e<=t.x&&t.x<=this.textRect.x+this.textRect.width+e&&this.textRect.y-e<=t.y&&t.y<=this.textRect.y+this.textRect.height+e},this.drawText=function(){var t,e,i,s=this.style.textAlign,r=this.style.font,h=r.lineHeight,n=this.style.color,o=this.lines.length;if(o&&n)for(t=this.textRect.x,e=this.textRect.y+h/2,"center"===s?t+=this.textRect.width/2:"end"!==s&&"right"!==s||(t+=this.textRect.width),this.ctx.font=this.style.font.string,this.ctx.fillStyle=n,this.ctx.textAlign=s,this.ctx.textBaseline="middle",i=0;ithis.predictedOffset.x?(this.offset.x-=this.offsetStep,this.offset.x<=this.predictedOffset.x&&(this.offset.x=this.predictedOffset.x)):this.predictedOffset.x>=0&&this.offset.x=this.predictedOffset.x&&(this.offset.x=this.predictedOffset.x))}}};h.init(),t.defaults.global.plugins.outlabels=r;var c=r.LABEL_KEY;t.plugins.register({id:"outlabels",resize:function(t,e,i){t.sizeChanged=!0},afterDatasetUpdate:function(t,i,s){var r,h,n,o,l,d,f=t.config.data.labels,u=t.data.datasets[i.index],x=e(u,s),g=x&&x.display,y=i.meta.data||[],p=t.ctx;for(p.save(),d=0;d0&&f<=1?f:2-f,f/=2,g>1&&(g=1),{h:isNaN(e)?0:e,s:isNaN(g)?0:g,l:isNaN(f)?0:f,a:isNaN(d)?0:d}},toAlias:function(a,b,c,d){var e,f=0===arguments.length?this.toHex(!0):this.toHex(!0,a,b,c,d),g="alias"===this.origFormat?f:this.toString(!1,this.origFormat);for(var h in this.colors)if(e=this.colors[h].toLowerCase().trim(),e===f||e===g)return h;return!1},RGBtoHSB:function(a,b,c,d){a/=255,b/=255,c/=255;var e,f,g,h;return g=Math.max(a,b,c),h=g-Math.min(a,b,c),e=0===h?null:g===a?(b-c)/h:g===b?(c-a)/h+2:(a-b)/h+4,e=(e+360)%6*60/360,f=0===h?0:h/g,{h:this._sanitizeNumber(e),s:f,b:g,a:this._sanitizeNumber(d)}},HueToRGB:function(a,b,c){return c<0?c+=1:c>1&&(c-=1),6*c<1?a+(b-a)*c*6:2*c<1?b:3*c<2?a+(b-a)*(2/3-c)*6:a},HSLtoRGB:function(a,b,c,d){b<0&&(b=0);var e;e=c<=.5?c*(1+b):c+b-c*b;var f=2*c-e,g=a+1/3,h=a,i=a-1/3,j=Math.round(255*this.HueToRGB(f,e,g)),k=Math.round(255*this.HueToRGB(f,e,h)),l=Math.round(255*this.HueToRGB(f,e,i));return[j,k,l,this._sanitizeNumber(d)]},parse:function(b){if(0===arguments.length)return!1;var c,d,e=this,f=!1,g="undefined"!=typeof this.colors[b];return g&&(b=this.colors[b].toLowerCase().trim()),a.each(this.stringParsers,function(a,h){var i=h.re.exec(b);return c=i&&h.parse.apply(e,[i]),!c||(f={},d=g?"alias":h.format?h.format:e.getValidFallbackFormat(),f=d.match(/hsla?/)?e.RGBtoHSB.apply(e,e.HSLtoRGB.apply(e,c)):e.RGBtoHSB.apply(e,c),f instanceof Object&&(f.format=d),!1)}),f},getValidFallbackFormat:function(){var a=["rgba","rgb","hex","hsla","hsl"];return this.origFormat&&a.indexOf(this.origFormat)!==-1?this.origFormat:this.fallbackFormat&&a.indexOf(this.fallbackFormat)!==-1?this.fallbackFormat:"rgba"},toString:function(a,c,d){c=c||this.origFormat||this.fallbackFormat,d=d||!1;var e=!1;switch(c){case"rgb":return e=this.toRGB(),this.rgbaIsTransparent(e)?"transparent":"rgb("+e.r+","+e.g+","+e.b+")";case"rgba":return e=this.toRGB(),"rgba("+e.r+","+e.g+","+e.b+","+e.a+")";case"hsl":return e=this.toHSL(),"hsl("+Math.round(360*e.h)+","+Math.round(100*e.s)+"%,"+Math.round(100*e.l)+"%)";case"hsla":return e=this.toHSL(),"hsla("+Math.round(360*e.h)+","+Math.round(100*e.s)+"%,"+Math.round(100*e.l)+"%,"+e.a+")";case"hex":return this.toHex(a);case"alias":return e=this.toAlias(),e===!1?this.toString(a,this.getValidFallbackFormat()):d&&!(e in b.webColors)&&e in this.predefinedColors?this.predefinedColors[e]:e;default:return e}},stringParsers:[{re:/rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*?\)/,format:"rgb",parse:function(a){return[a[1],a[2],a[3],1]}},{re:/rgb\(\s*(\d*(?:\.\d+)?)\%\s*,\s*(\d*(?:\.\d+)?)\%\s*,\s*(\d*(?:\.\d+)?)\%\s*?\)/,format:"rgb",parse:function(a){return[2.55*a[1],2.55*a[2],2.55*a[3],1]}},{re:/rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(\d*(?:\.\d+)?)\s*)?\)/,format:"rgba",parse:function(a){return[a[1],a[2],a[3],a[4]]}},{re:/rgba\(\s*(\d*(?:\.\d+)?)\%\s*,\s*(\d*(?:\.\d+)?)\%\s*,\s*(\d*(?:\.\d+)?)\%\s*(?:,\s*(\d*(?:\.\d+)?)\s*)?\)/,format:"rgba",parse:function(a){return[2.55*a[1],2.55*a[2],2.55*a[3],a[4]]}},{re:/hsl\(\s*(\d*(?:\.\d+)?)\s*,\s*(\d*(?:\.\d+)?)\%\s*,\s*(\d*(?:\.\d+)?)\%\s*?\)/,format:"hsl",parse:function(a){return[a[1]/360,a[2]/100,a[3]/100,a[4]]}},{re:/hsla\(\s*(\d*(?:\.\d+)?)\s*,\s*(\d*(?:\.\d+)?)\%\s*,\s*(\d*(?:\.\d+)?)\%\s*(?:,\s*(\d*(?:\.\d+)?)\s*)?\)/,format:"hsla",parse:function(a){return[a[1]/360,a[2]/100,a[3]/100,a[4]]}},{re:/#?([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,format:"hex",parse:function(a){return[parseInt(a[1],16),parseInt(a[2],16),parseInt(a[3],16),1]}},{re:/#?([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/,format:"hex",parse:function(a){return[parseInt(a[1]+a[1],16),parseInt(a[2]+a[2],16),parseInt(a[3]+a[3],16),1]}}],colorNameToHex:function(a){return"undefined"!=typeof this.colors[a.toLowerCase()]&&this.colors[a.toLowerCase()]}};var c={horizontal:!1,inline:!1,color:!1,format:!1,input:"input",container:!1,component:".add-on, .input-group-addon",fallbackColor:!1,fallbackFormat:"hex",hexNumberSignPrefix:!0,sliders:{saturation:{maxLeft:100,maxTop:100,callLeft:"setSaturation",callTop:"setBrightness"},hue:{maxLeft:0,maxTop:100,callLeft:!1,callTop:"setHue"},alpha:{maxLeft:0,maxTop:100,callLeft:!1,callTop:"setAlpha"}},slidersHorz:{saturation:{maxLeft:100,maxTop:100,callLeft:"setSaturation",callTop:"setBrightness"},hue:{maxLeft:100,maxTop:0,callLeft:"setHue",callTop:!1},alpha:{maxLeft:100,maxTop:0,callLeft:"setAlpha",callTop:!1}},template:'