create interactive legends in AppDesigner (UIAxes) to change the visibility of a line in a chart

9 visualizaciones (últimos 30 días)
Dear Matlab community,
Does anybody know how to create interactive legends in AppDesigner (2018b) to change the visibility of a line in a chart? It is perfectly working using Axes (outside AppDesigner) following the script below. But in AppDesigner I cannot get it working.
Thanks!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ItemHitFcn Callback that executes when you click legend items
@defaultItemHitCallback (default) | function handle | cell array | character vector
Callback that executes when you click legend items, specified as one of these values:
  • Function handle. For example, @myCallback.
  • Cell array containing a function handle and additional arguments. For example, {@myCallback,arg3}.
  • Character vector that is a valid MATLAB command or function, which is evaluated in the base workspace (not recommended).
If you specify this property using a function handle, then MATLAB passes the Legend object and an event data structure as the first and second input arguments to the function. This table describes the fields in the event data structure.
Event Data Structure Fields
FieldDescription
PeerChart object associated with the clicked legend item.
RegionRegion of legend item clicked, returned as either 'icon' or 'label'.
SelectionType
Type of click, returned as one of these values:
  • 'normal' — Single-click left mouse button
  • 'extend' Shift + single-click left mouse button
  • 'open' — Double-click any mouse button
  • 'alt' — Single-click right mouse button, both mouse buttons (Windows and Mac), or middle mouse button (Mac and Linux). If the UIContextMenu property contains a valid context menu (which is the default), then this type of click opens the context menu instead of triggering theItemHitFcn callback.
SourceLegend object.
EventNameEvent name, 'ItemHit'.
Note
If you set the ButtonDownFcn property, then the ItemHitFcn property is disabled.
Example
You can create interactive legends so that when you click an item in the legend, the associated chart updates in some way. For example, you can toggle the visibility of the chart or change its line width. Set the ItemHitFcn property of the legend to a callback function that controls how the charts change. This example shows how to toggle the visibility of a chart when you click the chart icon or label in a legend. It creates a callback function that changes the Visible property of the chart to either 'on' or 'off'.
Copy the following code to a new function file and save it as hitcallback_ex1.m either in the current folder or in a folder on the MATLAB search path. The two input arguments, src and evnt, are the legend object and an event data structure. MATLAB automatically passes these inputs to the callback function when you click an item in the legend. Use the Peer field of the event data structure to access properties of the chart object associated with the clicked legend item.
function hitcallback_ex1(src,evnt)
if strcmp(evnt.Peer.Visible,'on')
evnt.Peer.Visible = 'off';
else
evnt.Peer.Visible = 'on';
end
end
Then, plot four lines, create a legend, and assign the legend object to a variable. Set the ItemHitFcn property of the legend object to the callback function. Click items in the legend to show or hide the associated chart. The legend label changes to gray when you hide a chart.
plot(rand(4));
l = legend('Line 1','Line 2','Line 3','Line 4');
l.ItemHitFcn = @hitcallback_ex1;

Respuestas (1)

Pascal Bauerfeind
Pascal Bauerfeind el 3 de Mayo de 2023
I do not know if this works in v2018b but I just made an interactive legend in my app using AppDesigner in v2023a.
Definition of the hitcallback in the methods block:
methods (Access = private, Static)
function hitcallbackLegend(~, evt)
% Callback that executes when you click legend items. The
% visibility of the clicked item will be turned to the opposite
% state before the clicking event occurs (on/off).
if strcmp(evt.Peer.Visible,'on')
evt.Peer.Visible = 'off';
else
evt.Peer.Visible = 'on';
end
end
end
Somewhere in the app (within a button callback) the code for the plot creation on UIAxes with a legend:
hUIAxes = app.UIAxes1;
plot(hUIAxes, time, magnitude);
lg = legend(hUIAxes, myLegendEntries, 'location', 'northeast');
lg.ItemHitFcn = @app.hitcallbackLegend;
This worked for me.
  1 comentario
Christian Strotkötter
Christian Strotkötter el 28 de En. de 2024
This worked for me as well, but only with a small adaptation.
The "hitcallbackLegend"-function needs to have three arguments:
function hitcallbackLegend(~,~,evt)
because
  • the arguments of (non-static) functions in MATLAB apps (and classes) need to include the app itself (first argument = "app" = "~", because not needed)
  • copied from legend properties -> "Callbacks" -> "ItemHitFcn": If you specify this property using a function handle, then MATLAB passes the Legend object and an event data structure as the first and second input arguments to the function. ("~" & "evt")

Iniciar sesión para comentar.

Categorías

Más información sobre Legend 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!

Translated by