필터 지우기
필터 지우기

How do I change the datatip font color?

조회 수: 34 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2020년 3월 30일
편집: MathWorks Support Team 2021년 10월 7일
I would like to change the color of the data value text in the datatip.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2021년 9월 2일
편집: 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