Data Tip properties do not persist

6 Ansichten (letzte 30 Tage)
AMM
AMM am 23 Jul. 2020
Kommentiert: AMM am 17 Aug. 2020
I made a custom data tip function to show in-depth info about points in a plot. It works as expected. However, I'm having trouble getting the properties of the text in the data tip popups to stick. Currently, I set up these tips as follows (following Walter Roberson's suggestion regarding hggroup objects):
dcm = datacursormode(h_fig);
set(dcm, 'UpdateFcn', {@myTooltip, myData});
% Set tooltip font to make it readable (lots of numbers!)
alldatacursors = findall(h_fig, 'type', 'hggroup', '-property', 'FontSize');
set(alldatacursors, 'FontSize', 14, 'FontName', 'Consolas');
Unfortunately, when I create a data tip by clicking on a point in the plot, the result is rendered in a default font and size (Helvetica 10?), not Consolas 14. Only if I manually execute the statements above a second time (either from the Command Window, or by highlighting them in the Editor and choosing Evaluate Selection from the context menu) does the font style/size get updated. Maddeningly, if I then create a second data tip, it comes up in the default font again.
Can anyone spot my error(s)? Am I simply mis-applying the datacursormode mechanism and/or hggroup properties?

Akzeptierte Antwort

Alan Moses
Alan Moses am 14 Aug. 2020
Hi,
You can try pasting both the lines of code where the font size is initialized, inside the ‘UpdateFcn’ of the datacursormode function. This ensures the data tip properties persist. For example:
x = 1:10;
y = x.^2;
h_fig = figure;
scatter(x,y)
dcm = datacursormode;
dcm.Enable = 'on';
dcm.UpdateFcn = {@displayCoordinates,h_fig};
function txt = displayCoordinates(~,info,h_fig)
x = info.Position(1);
y = info.Position(2);
txt = ['(' num2str(x) ', ' num2str(y) ')'];
alldatacursors = findall(h_fig, 'type', 'hggroup', '-property', 'FontSize');
set(alldatacursors, 'FontSize', 20, 'FontName', 'Rockwell');
end
Hope this helps!
  1 Kommentar
AMM
AMM am 17 Aug. 2020
This worked perfectly. Thanks Alan!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by