How to run function on (double) click event in bar graph

Hi,
I would like to know if any of you has figured how to (easily) trigger a function when the user double clicks on a bar in a bar graph?
To get a better understanding of my problem, here's what I'm trying to achieve: I have some processed data which I plot in a bar graph to show the number of events per day over a certain period. Now when I click/double-click on one bar, I would like to plot a new figure to present the distribution of the events within a single day (hour by hour for instance).
It goes without saying that I have no problem in plotting these graphs from a script/function. What I don't know is how to plot the second plot after a click on a bar in the first figure!!
Any ideas on how to achieve that? Thanks!!

2 Comments

Please post a small example of how you create the bar graph.
Here you have it...
edges = firstday:(lastday+1);
[n,bin] = histc(eventdates, edges);
figure
bar(edges, n)
xlim([firstday-1 lastday+1])
Very basic MATLAB instructions as you can see. Now what I would like it to execute a function whenever a bar is clicked, and that function needs to know which bar was clicked (either through its index, or its x-value)

Sign in to comment.

Answers (2)

So obviously you'll want the click to be fast, hmm. Pseudo-thoughts-that may or may not make sense.
  1. figure's windowbuttondownfcn, changes the figure's windowbuttondownfcn to another function that does the new plot.
  2. It then makes a timer that fires once, in a little bit, (1/5 second or whatever you want the threshold for double clicking to be). This timer's function resets the figure's windowbuttondownfcn to the original.
  3. If within that half second its clicked again, the new windowbuttondownfcn gets the currentpoint, currentobject etc. Figures out wher eyou clicked using (maybe the vertex property of the bar graph (patch object) etc) and does some zooming.
Hopefully this helps get you started.

4 Comments

Hi Sean!
First, thanks for taking the time to look at my question!
However, the question is not so much about the double-click (point 1-2 of your answer), but rather about how to know which column/bar was clicked...
I have a DataTip Callback function:
function output_txt = DataTipFcn_PhotoGraph(obj, event_obj, binsize)
% Récupère la position (forme X, Y, Z)
pos = get(event_obj,'Position');
And within that function, it is very easy to know which information I need to display in my datatip (using pos(1) & pos(2)). How can I get the same information for the buttondownfcn event?
The bar graphs is really just a collection of patches whose data you should be able to use to figure out where you clicked:
h = bar(rand(1,5));
pause; %click somewhere, then press a keyboard key!
obj = get(gcf,'currentobject');
pt = get(gcf,'currentpoint');
objc = get(obj,'children');
objc will now be the patch objects. There is probably a way to figure out where they lie based on their vertex property (not positive though). If not, something in there holds the key.
Instead of gettign the figure's currentpoint, get the axes' current point. You can then map that current point to what set of vertices it falls in on a bar.
I would do all of this in the buttondownfcn of the patch object so when the patch is clicked, it figures out where the click was and what to do.

Sign in to comment.

At first get the handles of the BAR graph. Then assign a callback function:
...
H = bar(edges, n);
% I cannot run Matlab currently. Perhaps this is not required:
H = H(strcmpi(get(H, 'Type'), 'Patch'));
set(H, 'ButtonDownFcn', {@myCallback, H});
function myCallback(PatchH, EventData, H)
disp(find(H == PatchH));

Categories

Products

Asked:

on 7 Feb 2012

Edited:

on 3 Oct 2013

Community Treasure Hunt

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

Start Hunting!