Mouseover highlighting of plotted line?
40 次查看(过去 30 天)
显示 更早的评论
The 'parallelplot' function has a very handy mouseover function that highlights the proximate line in the plot and displays its parameters automatically in a datatip. I had to write my own parallel coordinate plot function due to parallelplot's limited color control, line sorting and label control but the mouseover feature is a bridge too far for me. Can anyone please give me direction to accomplish this for the plain 'plot' function?
Thanks!
0 个评论
采纳的回答
Abhilash Padma
2019-7-19
Hi,
I understand that you wanted to implement the mouseover functionality for plotted lines.
MATLAB's pointer manager lets you assign functions to be called whenever the mouse pointer enters, traverses or exits any graphics object. I included an example here where a line changes to another color when mouse passes through that line.
% ======================================================================= %
% Your code with a few additions:
% Save the figure handle for later
hf = figure;
x = linspace(-3,3);
y = (x/5-x.^3).*exp(-2*x.^2);
hp=plot(x,y, 'tag', 'rollover','color','black');
% ======================================================================= %
% Define a pointer behavior struct containing the fields 'enterFcn',
% 'exitFcn', and 'traverseFcn'. These fields should contain function
% handles or an empty matrix ([]). MATLAB will call these functions when
% the mouse pointer first moves over an object ('enterFcn'), when the mouse
% pointer moves off of an object ('exitFcn') and when the mouse pointer
% moves across and object ('traverseFcn'). MATLAB passes two arguments when
% it calls these functions: the figure handle, and the cursor position.
% I've defined the enterFcn to find the object with the 'rollover' tag (the
% text box) and change it's string value to something more informative...
pointerBehavior.enterFcn =@(hfig, cpp)set(findobj(hfig, 'tag', 'rollover'), ...
'color', 'red');
% I don't care about what happens when the mouse moves across the text, so
% I'll leave the traverseFcn blank...
pointerBehavior.traverseFcn = [];
% The exitFcn is similar to the enterFcn, but it changes the string back to
% the shorter version...
pointerBehavior.exitFcn = ...
@(hfig, cpp)set(findobj(hfig, 'tag', 'rollover'), ...
'color', 'black');
% Now, I need to link the pointer behavior to the object (the text box):
iptSetPointerBehavior(hp, pointerBehavior);
% Now, I need to enable pointer management for the figure:
iptPointerManager(hf, 'enable');
% function changeColor(hfig, cpp)
% set(findobj(hfig, 'tag', 'rollover'), ...
% 'color', 'red');
% end
2 个评论
cliffy
2019-8-3
Hi Abhilash,
I'm using the pointerBehavior function. It works, it can show some more text when I move my cursor to a textbox in the figure. I wonder if you have encounter the problem of several objects i.e. when move the cursor over different objects, different text will show up. Thank you! And does the object have to be a textbox, or could it be some plots?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!