How do I change the datatip font color?
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 30 de Mzo. de 2020
Editada: MathWorks Support Team
el 7 de Oct. de 2021
I would like to change the color of the data value text in the datatip.
Respuesta aceptada
MathWorks Support Team
el 2 de Sept. de 2021
Editada: MathWorks Support Team
el 7 de Oct. de 2021
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!