Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Prevent tooltip overlap when they're always showing

Open tlaredo opened this issue 9 years ago • 8 comments

This is an extension from #1861 , but how can I prevent tooltips from overlapping if they're always showing? See example below. Thanks!

screen shot 2017-03-19 at 3 49 42 pm

tlaredo avatar Mar 19 '17 23:03 tlaredo

@tlaredo What you'd need to do is adjust the position of the tooltips, but I think that is going to get complex. It might be easier to use an HTML tooltip and let the browser figure it out if possible

etimberg avatar Mar 20 '17 11:03 etimberg

Is it possible to position the tooltips in a way, that they always point outside and they are additionally rotated, that they show away from the middle (so not horizontal)? That would solve the overlapping issue.

faulix avatar Mar 23 '17 11:03 faulix

@faulix @tlaredo I actually wanted the same behavior a few months ago (back when the latest chart.js version was v2.3.0) and I implemented Chart.TooltipReversed. It basically overwrites the tooltip positioning logic such that the tooltip point out (instead of in). Check it out.

https://gist.github.com/jordanwillis/5f61d263c56439b65f3b89b2d89ccd9f

Unfortunately, a lot of the Chart.Tooltip functions that I overwrite to get this working were switched to private functions within Chart.Tooltip in v2.4.0. Therefore, this code will not work beyond v2.3.0. I thought I would share to see if you wanted to get something similar for v2.5.0 and also to show that it is indeed complex. I've since switched to using custom tooltips instead.

jordanwillis avatar Mar 24 '17 17:03 jordanwillis

@tlaredo I solved it in the end by deactivating the color in the toolbox, reducing the size of the tooltip, moving the tooltip closer to the outer border and hiding all tooltips, which represent less than 2%. Example looks like that: oem_history 5

I used for that the following code:

Chart.Tooltip.positioners.outer = function(elements) {
	if (!elements.length) {
		return false;
	}

	var i, len;
	var x = 0;
	var y = 0;

	for (i = 0, len = elements.length; i < len; ++i) {
		var el = elements[i];
		if (el && el.hasValue()) {
			var elPosX = el._view.x+0.95*el._view.outerRadius*Math.cos((el._view.endAngle-el._view.startAngle)/2+el._view.startAngle);
			var elPosY = el._view.y+0.95*el._view.outerRadius*Math.sin((el._view.endAngle-el._view.startAngle)/2+el._view.startAngle);
			if (x < elPosX) {
				x = elPosX;
			}
			if (y < elPosY) {
				y = elPosY;
			}
		}
	}

	return {
		x: Math.round(x),
		y: Math.round(y)
	};
},

Chart.pluginService.register({
	  beforeRender: function (chart) {
	    if (chart.config.options.showAllTooltips) {
	        // create an array of tooltips
	        // we can't use the chart tooltip because there is only one tooltip per chart
	        chart.pluginTooltips = [];
	        chart.config.data.datasets.forEach(function (dataset, i) {
	            chart.getDatasetMeta(i).data.forEach(function (sector, j) {
	                if ((sector._view.endAngle-sector._view.startAngle) > 2*Math.PI*0.02) {
		            	chart.pluginTooltips.push(
		            			new Chart.Tooltip({
		                    _chart: chart.chart,
		                    _chartInstance: chart,
		                    _data: chart.data,
		                    _options: chart.options.tooltips,
		                    _active: [sector]
		                }, chart)
		            	);
	                }
	            });
	        });

	        // turn off normal tooltips
	        chart.options.tooltips.enabled = false;
	    }
	},
	  afterDraw: function (chart, easing) {
	    if (chart.config.options.showAllTooltips) {
	        // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
	        if (!chart.allTooltipsOnce) {
	            if (easing !== 1)
	                return;
	            chart.allTooltipsOnce = true;
	        }

	        // turn on tooltips
	        chart.options.tooltips.enabled = true;
	        Chart.helpers.each(chart.pluginTooltips, function (tooltip) {
	        	tooltip.initialize();
	        	tooltip._options.position = "outer";
	            tooltip._options.displayColors = false;
	            tooltip._options.bodyFontSize = tooltip._chart.height*0.025;
	            tooltip._options.yPadding = tooltip._options.bodyFontSize*0.30;
	            tooltip._options.xPadding = tooltip._options.bodyFontSize*0.30;
	            tooltip._options.caretSize = tooltip._options.bodyFontSize*0.5;
	            tooltip._options.cornerRadius = tooltip._options.bodyFontSize*0.50;
	            tooltip.update();
	            // we don't actually need this since we are not animating tooltips
	            tooltip.pivot();
	            tooltip.transition(easing).draw();
	        });
	        chart.options.tooltips.enabled = false;
	    }
	  }
	});

faulix avatar Mar 24 '17 18:03 faulix

Closing since there are a bunch of good answers here and this is a very specific request

etimberg avatar May 02 '17 00:05 etimberg

Has anyone come across a different solution for this? I run into this issue a lot when two slices of the pie have a zero value, or when values are similar. There are cases where I need to show the zero value tooltip, so hiding it is not an option. It would be nice if the tooltips could stack somehow instead of overlap.

adidas-jordan avatar Dec 04 '17 20:12 adidas-jordan

download

download-2

How do I rotate the tooltip text like the second picture?

sumanengbd avatar May 20 '19 08:05 sumanengbd

@sumanengbd use datalabels plugin

kurkle avatar May 20 '19 09:05 kurkle