how to set mouse Events for markstip in TChart

TeeChart for Java (NetBeans, Eclipse, Android Studio, etc)
Post Reply
Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

how to set mouse Events for markstip in TChart

Post by Jonathan » Fri Sep 18, 2009 9:06 pm

Hi,

I have a class called 'glassPane' which encases TChart components and intercepting all event handing for the encased component. Since the 'glassPane' intercepts all mouse events, the labels of points in my scatter chart do not get displayed when mouse moves over the points. Thus, I have to pass the mouse (move) events to TChart from glassPane in order to display the markstip labels. So, I did it. However, it doesn't work as I'd expected. Pls. help me how to set mouse event within TChart correctly to display the labels over the scatter points.

Within TChartRenderer, I set the mouse events as followings:

Code: Select all

@Override
public void mouseMoved(java.awt.event.MouseEvent e) { 
  teeChart.mouseMoved(e); 
}

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: how to set mouse Events for markstip in TChart

Post by Narcís » Mon Sep 21, 2009 8:33 am

Hi Jonathan,

You can do something as in the example I posted here.

Hope this helps!
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: how to set mouse Events for markstip in TChart

Post by Jonathan » Mon Sep 21, 2009 2:32 pm

Hi Narcis,

I'll consider your example.

BTW, Is there a way to re-fire event to TChart? In other words, is it impossible to fire the event directly to TChart? (If so, I don't have to worry about the workaround)

Thanks

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: how to set mouse Events for markstip in TChart

Post by Narcís » Mon Sep 21, 2009 2:45 pm

Hi Jonathan,

Yes, continuing in the example at the thread I pointed you should be able to create MouseEvent variable and pass it onto the mouse event handling method, for example:

Code: Select all

        MouseEvent e = new MouseEvent(/*you'll need to add the params here*/);
        tChartMouseListened(e);
Hope this helps!
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: how to set mouse Events for markstip in TChart

Post by Jonathan » Mon Sep 21, 2009 4:37 pm

Hi Narcis,

I am using Markstip to display custom labels(in string) for each point. Your "tChartMouseListened(e)" function doesn't do what I need since it displays the value of the point, not labels that are stored previously. I need to display the custom labels when mouse moves over the point.

In my code below, I can get both the index and custom label of the point within mouseMove listener. After that, how can I display the label ?

Code: Select all

		
Points p = new Points(teeChart.getChart());
p.add(3,6);
p.add(4,7);
p.add(5,10);

StringList sl = new StringList(3);
sl.setString(0, "one one");
sl.setString(1, "two two");
sl.setString(2, "three three");
p.setLabels(sl);

MarksTip mt = new MarksTip(teeChart.getChart());
mt.setStyle(MarksStyle.LABEL);

@Override
public void mouseMoved(java.awt.event.MouseEvent e) {
	int index = teeChart.getSeries(0).clicked(e.getX(), e.getY());
	if(index != -1){
		String str = teeChart.getSeries(0).getMarkText(index);
	}
}

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: how to set mouse Events for markstip in TChart

Post by Narcís » Tue Sep 22, 2009 8:49 am

Hi Jonathan,

In that case you can try this:

Code: Select all

      String str = teeChart.getSeries(0).getLabels().getString(index);
Hope this helps!
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: how to set mouse Events for markstip in TChart

Post by Jonathan » Tue Sep 22, 2009 2:20 pm

Hi,

You missed my point. I want to know how to display the custom label when MOUSE MOVES OVER THE POINTS.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: how to set mouse Events for markstip in TChart

Post by Narcís » Tue Sep 22, 2009 3:13 pm

Hi Jonathan,

I'm sorry if my explanation was not clear enough I meant that you should just replace that line in the code snippet you posted. You should replace this:

Code: Select all

      String str = teeChart.getSeries(0).getMarkText(index);
with this:

Code: Select all

      String str = teeChart.getSeries(0).getLabels().getString(index);
so that mouseMoved event is implemented like this:

Code: Select all

@Override
public void mouseMoved(java.awt.event.MouseEvent e) {
   int index = teeChart.getSeries(0).clicked(e.getX(), e.getY());
   if(index != -1){
      String str = teeChart.getSeries(0).getLabels().getString(index);
   }
}
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: how to set mouse Events for markstip in TChart

Post by Jonathan » Tue Sep 22, 2009 3:30 pm

Hi Narcis,

I understood your previous answer. But, it wasn't the answer I was looking for. I guess my question wasn't clear.

The previous code within mouseMove event doesn't do anything except having index and label values. I don't want to print the label, but want to DISPLAY the label on the scatter chart when mouse moves over the points. I am asking you how to do this.

In my option, it would be best to pass the event to the marktips.mouseEvent(). However, I don't know how to pass java.awt.event.MouseEvent to FrameworkMouseEvent due to lack of API doc. Pls. guide me how to do this.

Code: Select all

MarksTip mt = new MarksTip(teeChart.getChart());
mt.setStyle(MarksStyle.LABEL);

@Override
public void mouseMoved(java.awt.event.MouseEvent e) {
   int index = teeChart.getSeries(0).clicked(e.getX(), e.getY());
   if(index != -1){
         FrameworkMouseEvent fme = new FrameworkMouseEvent(e.getComponent(),e.getID(),e.getWhen(),e.getModifiers(),
                                                     e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger());
         mt.mouseEvent(fme, Cursor.getDefaultCursor());   // <--- this doesn't work
   }
}

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: how to set mouse Events for markstip in TChart

Post by Narcís » Wed Sep 23, 2009 9:40 am

Hi Jonathan,
The previous code within mouseMove event doesn't do anything except having index and label values. I don't want to print the label, but want to DISPLAY the label on the scatter chart when mouse moves over the points. I am asking you how to do this.
MarksTip tool already does this for me only using this code you posted:

Code: Select all

        Points p = new Points(tChart1.getChart());
        p.add(3,6);
        p.add(4,7);
        p.add(5,10);

        StringList sl = new StringList(3);
        sl.setString(0, "one one");
        sl.setString(1, "two two");
        sl.setString(2, "three three");
        p.setLabels(sl);

        MarksTip mt = new MarksTip(tChart1.getChart());
        mt.setStyle(MarksStyle.LABEL);
Anyway, you can also do this manually using annotation tools or custom drawing on charts canvas, for example:

Code: Select all

    private void initChart() { 
        
        Points p = new Points(tChart1.getChart());
        p.add(3,6);
        p.add(4,7);
        p.add(5,10);

        StringList sl = new StringList(3);
        sl.setString(0, "one one");
        sl.setString(1, "two two");
        sl.setString(2, "three three");
        p.setLabels(sl);
      
        tChart1.addMouseMotionListener(new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {
            }

            public void mouseMoved(MouseEvent e) {
                tChartMouseListened(e);
            }
        });       
        
    }
    
    private void tChartMouseListened(java.awt.event.MouseEvent e) {
               
        Series s = tChart1.getSeries(0);
        int index = s.clicked(e.getX(),e.getY());        
       
        if (index != -1) {
            String yVal = String.valueOf(s.getYValues().getValue(index));
            String label = s.getLabels().getString(index);
            
            tChart1.getTools().clear();
            
            Annotation annotation1 = new Annotation(tChart1.getChart());
            annotation1.getShape().setCustomPosition(true);
            annotation1.getShape().setLeft(e.getX());
            annotation1.getShape().setTop(e.getY());
            annotation1.setText(yVal + " - " + label);
        }
    }
If this doesn't help please attach a simple example project we can run "as-is" to reproduce your problem here.

Thanks in advance.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: how to set mouse Events for markstip in TChart

Post by Jonathan » Fri Sep 25, 2009 9:11 pm

Hi Narcis,

Does this annotation tool work with multiple series on one scatter chart? It seems it doesn't. Say, I have two series of point. I see the tooltip on the 1st series, but not on the 2nd series. Any idea? (BTW, my annotation implementation is same as what you described above.)

thanks,

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: how to set mouse Events for markstip in TChart

Post by Narcís » Mon Sep 28, 2009 7:51 am

Hi Jonathan,

Yes, but you should modify mouse event code a little bit to use all series in the chart, for example:

Code: Select all

    private void tChartMouseListened(java.awt.event.MouseEvent e) {
         
        for (int i = 0; i< tChart1.getSeriesCount(); i++)
        {
            Series s = tChart1.getSeries(i);
            int index = s.clicked(e.getX(),e.getY());        

            if (index != -1) {
                String yVal = String.valueOf(s.getYValues().getValue(index));
                String label = s.getLabels().getString(index);

                tChart1.getTools().clear();

                Annotation annotation1 = new Annotation(tChart1.getChart());
                annotation1.getShape().setCustomPosition(true);
                annotation1.getShape().setLeft(e.getX());
                annotation1.getShape().setTop(e.getY());
                annotation1.setText(yVal + " - " + label);
                break;
            }
        }
    }
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: how to set mouse Events for markstip in TChart

Post by Jonathan » Mon Sep 28, 2009 2:33 pm

Hi Narcis,

In fact, I have done exactly what you said above.

1) When I display two series in different chart( series A in Chart A, series B in Chart B), then I can see tooltips from both charts.

However,

2) When I display the two series on the same chart (series A and B in Chart A), then I can see tooltips only from series A, not series B). Is this a bug? (FYI - When I printed out the label within tChartMouseListened function, I can see the labels printed out from series A and series B. However, the labels from series B do not get displayed.)

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: how to set mouse Events for markstip in TChart

Post by Narcís » Mon Sep 28, 2009 2:36 pm

Hi Jonathan,

Code below works fine for me here. Can you please check if it works fine at your end? Can you please modify the code snippet or send us a simple example project we can run "as-is" to reproduce the problem here?

Code: Select all

    private void initChart() { 
        
        Points p = new Points(tChart1.getChart());
        p.add(3,6);
        p.add(4,7);
        p.add(5,10);

        StringList sl = new StringList(3);
        sl.setString(0, "one one");
        sl.setString(1, "two two");
        sl.setString(2, "three three");
        p.setLabels(sl);
        
        Points p2 = new Points(tChart1.getChart());
        p2.add(0,6);
        p2.add(1,7);
        p2.add(2,10);

        StringList sl2 = new StringList(3);
        sl2.setString(0, "one");
        sl2.setString(1, "two");
        sl2.setString(2, "three");
        p2.setLabels(sl2);
      
        tChart1.addMouseMotionListener(new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {
            }

            public void mouseMoved(MouseEvent e) {
                tChartMouseListened(e);
            }
        });
        
    }
    
    private void tChartMouseListened(java.awt.event.MouseEvent e) {
         
        for (int i = 0; i< tChart1.getSeriesCount(); i++)
        {
            Series s = tChart1.getSeries(i);
            int index = s.clicked(e.getX(),e.getY());        

            if (index != -1) {
                String yVal = String.valueOf(s.getYValues().getValue(index));
                String label = s.getLabels().getString(index);

                tChart1.getTools().clear();

                Annotation annotation1 = new Annotation(tChart1.getChart());
                annotation1.getShape().setCustomPosition(true);
                annotation1.getShape().setLeft(e.getX());
                annotation1.getShape().setTop(e.getY());
                annotation1.setText(yVal + " - " + label);
                break;
            }
        }
    }
Thanks in advance.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: how to set mouse Events for markstip in TChart

Post by Jonathan » Mon Sep 28, 2009 6:27 pm

Interestingly, it works when I added "break;" at the end of the code. I appreciate with all of your support.

Post Reply