Data Tip properties do not persist

5 ビュー (過去 30 日間)
AMM
AMM 2020 年 7 月 23 日
コメント済み: AMM 2020 年 8 月 17 日
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?

採用された回答

Alan Moses
Alan Moses 2020 年 8 月 14 日
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 件のコメント
AMM
AMM 2020 年 8 月 17 日
This worked perfectly. Thanks Alan!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by