capture mouse events
-
- Posts: 14
- Joined: Fri Nov 02, 2007 8:21 am
capture mouse events
hi!
how can i capture mouse events?
how can i add codes to JFreeCharPanel?
help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
how can i capture mouse events?
how can i add codes to JFreeCharPanel?
help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
capture mouse events
Hi Tony,
Capturing mouse events is relatively simple in jfreechart. your class should implement the ChartMouseListener interface. So assuming you've created a chart and set the chart panel for example:
At this point the chart is created and a chart panel is created with the chart. now all that is left to do is add the chart mouse listener which is done easily as follows:
this adds a chartlistner to the current class (in my example above im implementing the ChartMouseListener interface).
Now your whenever there is a mouse event on the chart area or panel an event will be fired. how to deal with these events?
easy. the chartmouselistener interface has two methods which must be implemented:
&
that should solve your problem. try looking into the entity class as well. from this you can obtain what the mouse is over. typically you would like to find out if something has happened on the chart area. for example maybe you want to update the value (in some text box or Label displayed to the user) the mouse is pointing to on a chart as it is moved. in this case we would use the the chartMouseMoved with the entity class as follows:
easy hey!?!? this routine will display what is under the mouse to a jLabel called lblDisplay. you can also go further and look at other entities and so make your chart very interactive.
I hope this helps. let me know if you go wrong.
Ridwaan
Capturing mouse events is relatively simple in jfreechart. your class should implement the ChartMouseListener interface. So assuming you've created a chart and set the chart panel for example:
Code: Select all
public JFreeChart chart;
public XYPlot plot;
private XYDataset mydata;
private ChartPanel cPanel;
mydata=createDataset(); // a method to populate a dataset
chart = ChartFactory.createXYAreaChart(
"My Chart",
"X Axis Label",
"Y Axis Label",
mydata,
PlotOrientation.VERTICAL,
true,true,false
);
cPanel = new ChartPanel(chart);
Code: Select all
cPanel.addChartMouseListener(this);
Now your whenever there is a mouse event on the chart area or panel an event will be fired. how to deal with these events?
easy. the chartmouselistener interface has two methods which must be implemented:
Code: Select all
public void chartMouseClicked(ChartMouseEvent chartMouseEvent) {
//put your handling code here to handle click events
return;
}
Code: Select all
public void chartMouseMoved(ChartMouseEvent event) {
//put your handling code here to handle mouse movement events
}
Code: Select all
public void chartMouseMoved(ChartMouseEvent event) {
//put your handling code here to handle mouse movement events
ChartEntity entity = event.getEntity();
//the above line will get the entity that is below the mouse.
if ((entity instanceof XYItemEntity)) {
//now we check what type of entity it is.
//in my case im just checking if the mouse is over
//any Datapoint on my chart. but you should be
//able to extend this to any type of chart entity
XYItemEntity ent = (XYItemEntity) entity;
serindex = ent.getSeriesIndex();
itemindex = ent.getItem();
//now we actually have the series and item index of the data value
//being pointed to by the mouse.
//in a category chart you would get the series key and category key etc
//now you may get the data from your dataset using these indexes
//and do some processing:
lblDisplay.setText("at x = "
+ mydata.getXValue(serindex,itemindex)
+ " the value of y is : "
+ mydata.getYValue(serindex,itemindex));
} //end if
}
I hope this helps. let me know if you go wrong.
Ridwaan
-
- Posts: 14
- Joined: Fri Nov 02, 2007 8:21 am
Hi:raumaduth
Thanks for your help. But I've found something really strange
Here is the fragment of my program:
Use this I created 2 XYPlot object.Then:
The strange thing is I've not set a chartpanel or add any MouseListener to my class, but when I clicked the right button of my mouse, it leads a pop menu! And even more strange, when I drag my mouse, the chart zooms out!
How can it be?!
Thanks for your help. But I've found something really strange
Here is the fragment of my program:
Code: Select all
XYPlot volPlot = new XYPlot(vol_avg_line5, null, y1Axis, xyLineRender1);
XYPlot candlePlot = new XYPlot(k_line, null, y2Axis , candlesRender);
Code: Select all
CombinedDomainXYPlot combineXY = new CombinedDomainXYPlot(dateaxis);
combineXY.add(candlePlot, 3);
combineXY.add(volPlot, 1);
combineXY.setGap(8D);
combineXY.setDomainGridlinesVisible(true);
JFreeChart jfreechart = new JFreeChart(stock_name,JFreeChart.DEFAULT_TITLE_FONT, combineXY, false);
The strange thing is I've not set a chartpanel or add any MouseListener to my class, but when I clicked the right button of my mouse, it leads a pop menu! And even more strange, when I drag my mouse, the chart zooms out!
How can it be?!
capture mouse events
Hi Tony,
Jfreechart as a default when creating a plot has these listeners set up on other classes. so by default dragging zooms and right clicking brings up the popup menu.
there is a way in in the chart or the plot to turn these off so clicking and dragging do not do anything. i cant remember the method but it should be in the api.
Ridwaan
Jfreechart as a default when creating a plot has these listeners set up on other classes. so by default dragging zooms and right clicking brings up the popup menu.
there is a way in in the chart or the plot to turn these off so clicking and dragging do not do anything. i cant remember the method but it should be in the api.
Ridwaan
-
- Posts: 14
- Joined: Fri Nov 02, 2007 8:21 am
Turn off catching btn1:
Turn off the popup menu (Allow you to catch btn3)
regards,
--H
Code: Select all
chartPanel.setMouseZoomable(false, false);
Code: Select all
chartPanel.setPopupMenu(null);
--H