capture mouse events

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Tony Young
Posts: 14
Joined: Fri Nov 02, 2007 8:21 am

capture mouse events

Post by Tony Young » Fri Nov 02, 2007 10:56 am

hi!

how can i capture mouse events?
how can i add codes to JFreeCharPanel?

help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

raumaduth
Posts: 9
Joined: Fri Oct 19, 2007 9:13 am

capture mouse events

Post by raumaduth » Fri Nov 02, 2007 2:53 pm

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:

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);
:idea: 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:

Code: Select all

        cPanel.addChartMouseListener(this);
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?
:D
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
    }
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:

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
    }
easy hey!?!? :roll: 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 :wink:

Tony Young
Posts: 14
Joined: Fri Nov 02, 2007 8:21 am

Post by Tony Young » Mon Nov 05, 2007 4:10 am

Hi:raumaduth

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);
Use this I created 2 XYPlot object.Then:

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?!

raumaduth
Posts: 9
Joined: Fri Oct 19, 2007 9:13 am

capture mouse events

Post by raumaduth » Mon Nov 05, 2007 7:35 am

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. 8)

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

Tony Young
Posts: 14
Joined: Fri Nov 02, 2007 8:21 am

Post by Tony Young » Mon Nov 05, 2007 7:59 am

Hi:raumaduth

:idea: I get it!
Thanks for your help :!: :!: :!: :!:

Rudi23
Posts: 7
Joined: Tue Nov 06, 2007 10:00 am

Post by Rudi23 » Fri Nov 09, 2007 9:20 am

Hi Tony Young,

I'm sure you are happy that you got it. But also other people like me would like to be happy :D , so please post the solution you have found.

Thanks a lot,
Rudi

heksemann
Posts: 1
Joined: Fri Jan 11, 2008 3:54 pm

Post by heksemann » Fri Jan 11, 2008 3:58 pm

Turn off catching btn1:

Code: Select all

chartPanel.setMouseZoomable(false, false);
Turn off the popup menu (Allow you to catch btn3)

Code: Select all

chartPanel.setPopupMenu(null);
regards,
--H

Locked