Effacer les filtres
Effacer les filtres

Is it possible to change the background color of data tips?

27 vues (au cours des 30 derniers jours)
Roger Breton
Roger Breton le 24 Jan 2022
Modifié(e) : DGM le 18 Nov 2023
I get this result :
You see? I wonder what property controls the background color of the data tip?
  3 commentaires
Massimo Ciacci
Massimo Ciacci le 18 Nov 2023
Modifié(e) : Massimo Ciacci le 18 Nov 2023
It's a pity... In Matlab 2016 there was such a property,
hDatatip = cursorMode.createDatatip(hTarget) ;
set(hDatatip, 'BackgroundColor', tipBckGndColor,'MarkerFaceColor',tipBckGndColor)
Unfortunately this property is gone nowadays.. I also had a tool in ML central add_DataTips where this property has been broken.
However I found another property that does work:
hDatatip.FaceColor = [1 0 0];
hDatatip.EdgeColor = [1 0 0];
will make it all red.
DGM
DGM le 18 Nov 2023
Modifié(e) : DGM le 18 Nov 2023
[x y z] = peaks(100);
hp = pcolor(x,y,z);
hp.EdgeColor = 'none';
cursorMode = datacursormode(gcf);
hDatatip = cursorMode.createDatatip(hp);
hDatatip.FaceColor = [1 0 0];
I don't know when that was added, but it's not in R2019b, and while it's a public property in R2023b, it's apparently not documented anywhere that I can find.
What's interesting is that while 'backgroundcolor' is a valid property still in R2019b, it doesn't actually seem to do anything, whereas it works in R2015b.
Seems like a fine mess.

Connectez-vous pour commenter.

Réponses (1)

DGM
DGM le 25 Jan 2022
Modifié(e) : DGM le 25 Jan 2022
@Ankit is correct. There really aren't any such color properties for datatips.
If you like frustration, you can always craft such a thing out of an annotation object. In this example, the width and height of the annotation box should automatically be adjusted to fit the text. The orientation of the box WRT the data marker depends on the alignment properties of the annotation object. Since annotations are specified in figure coordinates, I included a convenience function to do the conversions.
[x y z] = peaks(100);
hp = pcolor(x,y,z);
hp.EdgeColor = 'none';
hold on;
hdt = datatip(hp,-1.03,-1); % observe datatip behavior (it snaps to data)
% note that for a pcolor plot, datatip reports z = 0
% since a pcolor plot is basically a zero-height surface
% the actual z data is just used for colormapping
annloc = [1.03 1]; % point to sample [x y]
% find nearest actual datapoint (assuming gridded data)
[~,idxx] = min(abs(x(1,:)-annloc(1)));
[~,idxy] = min(abs(y(:,1)-annloc(2)));
sampx = x(1,idxx);
sampy = y(idxy,1);
sampz = z(idxy,idxx); % this is the actual z data
% build annotation object
annstr = sprintf('X: %.3f\nY: %.3f\nZ: %.3f',sampx,sampy,sampz);
boxpos = [sampx sampy 1 1]; % [xoffset yoffset width height] in data coordinates
[xx yy] = datc2figc([boxpos(1) sum(boxpos([1 3]))],[boxpos(2) sum(boxpos([2 4]))]);
boxposf = [xx(1) yy(1) range(xx) range(yy)]; % position in figure coordinates
ha = annotation('textbox',boxposf,'string',annstr,'FitBoxToText','on');
ha.BackgroundColor = [1 0.2 0.8];
ha.VerticalAlignment = 'bottom';
% add marker dot
hp = plot(sampx,sampy,'o');
hp.MarkerFaceColor = 'k';
Note the way datatips behave on pcolor plots. The z-values it reports will be misleading.
clf
[x y z] = peaks(100);
hp = surf(x,y,z);
hp.EdgeColor = 'none';
hold on;
plot3(-1.03,-1,1.8559,'k*'); % zdata is not zero at this point
view(12,25)
For other types of plots (images, lines, scatter), you'll have to come up with corresponding ways to fetch the appropriate values to display.

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by