How to run function on (double) click event in bar graph
Show older comments
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!!
Answers (2)
Sean de Wolski
on 7 Feb 2012
0 votes
So obviously you'll want the click to be fast, hmm. Pseudo-thoughts-that may or may not make sense.
- figure's windowbuttondownfcn, changes the figure's windowbuttondownfcn to another function that does the new plot.
- 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.
- 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
Federico
on 7 Feb 2012
Sean de Wolski
on 7 Feb 2012
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.
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
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.
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 Data Distribution Plots 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!