How do I change the datatip font color?

53 次查看(过去 30 天)
I would like to change the color of the data value text in the datatip.

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2021-10-7
It does not appear to be possible for datatips created using the 'datatip' command. However, it is possible with datatips created interactively, e.g. mouse clicks, but it will require a small bit of knowledge of Tex/Latex.
The trick is to use a 'data cursor mode' object:
See the code snippet below:
% create random scatter plot
X = rand(100,1);
Y = rand(100,1);
f = figure();
a = axes(f);
s = scatter(a, X, Y);
% retreive datacursormode object
d = datacursormode(f);
% set callback to handle displaying datatip
d.UpdateFcn = @myfunction;
% Notes:
% - The callback function below was obtained from
% right clicking a datatip and selecting Update Function > Edit...
% - I had to append closing 'end' lines to clear syntax errors.
% - The callback takes the event object which contains the data values
% and returns the text to display.
% - See line 50 and 51 for text coloring using Latex.
function output_txt = myfunction(obj,event_obj)
% Display data cursor position in a data tip
% obj Currently not used
% event_obj Handle to event object
% output_txt Data tip text, returned as a character vector or a cell array of character vectors
pos = event_obj.Position;
%********* Define the content of the data tip here *********%
% Display the x and y values:
output_txt = {['X',formatValue(pos(1),event_obj)],...
['Y',formatValue(pos(2),event_obj)]};
%***********************************************************%
% If there is a z value, display it:
if length(pos) > 2
output_txt{end+1} = ['Z',formatValue(pos(3),event_obj)];
end
%***********************************************************%
function formattedValue = formatValue(value,event_obj)
% If you do not want TeX formatting in the data tip, uncomment the line below.
% event_obj.Interpreter = 'none';
if strcmpi(event_obj.Interpreter,'tex')
valueFormat = ' \color[rgb]{0.1 0.8 0.1}\bf'; % set green color and bold font
removeValueFormat = '\color[rgb]{.25 .25 .25}\rm'; % set grey and reset font
else
valueFormat = ': ';
removeValueFormat = '';
end
formattedValue = [valueFormat num2str(value,4) removeValueFormat];
end
end
An enhancement request has been made to make this possible for datatips created using the 'datatip' command.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Visual Exploration 的更多信息

标签

尚未输入任何标签。

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by