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

11 views (last 30 days)
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
Jan
Jan on 7 Feb 2012
Please post a small example of how you create the bar graph.
Federico
Federico on 7 Feb 2012
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)

Sean de Wolski
Sean de Wolski on 7 Feb 2012
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
Sean de Wolski
Sean de Wolski on 7 Feb 2012
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.
Sean de Wolski
Sean de Wolski on 7 Feb 2012
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.


Jan
Jan on 8 Feb 2012
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

Find more on Graphics Object Properties in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!