Associating object with chart point/finding specific chart p

TeeChart for Java (NetBeans, Eclipse, Android Studio, etc)
Post Reply
plamen
Newbie
Newbie
Posts: 9
Joined: Thu Jan 08, 2015 12:00 am

Associating object with chart point/finding specific chart p

Post by plamen » Mon Jan 26, 2015 10:37 am

Hello,

I was trying to find a way to associate an object with a point in Chart (Line series), because I need to be able to display specific information in the tool tip on mouse hover or alternatively do some specific action on point double click. I tried using the returned value index from the add method, but it does not make sense, because the initial data is not sorted, so any kept index, can be overwritten when the chart rearranges its internal data. Am I missing some obvious way to do this?

Thanks,
Plamen

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Associating object with chart point/finding specific chart p

Post by Yeray » Mon Jan 26, 2015 12:19 pm

Hi Plamen,

You can to use the MarkTextResolver or seriesClicked events provided to change the behaviour according to your needs:

Code: Select all

        tChart1.getAspect().setView3D(false);
        tChart1.getLegend().setVisible(false);

        Points point1 = new Points(tChart1.getChart());
        point1.fillSampleValues();
        
        point1.addSeriesMouseListener(new SeriesMouseAdapter() {
			
			@Override
			public void seriesClicked(SeriesMouseEvent e) {
				
			}
		});
        
        point1.setMarkTextResolver(new MarkTextResolver() {
			
			@Override
			public String getMarkText(int valueIndex, String markText) {
				
				return markText;
			}
		});
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

plamen
Newbie
Newbie
Posts: 9
Joined: Thu Jan 08, 2015 12:00 am

Re: Associating object with chart point/finding specific chart p

Post by plamen » Mon Jan 26, 2015 2:15 pm

Hi Yeray,

thanks for your quick answer, but I am afraid I might have not been clear enough specifying my question.
Suppose I have number of objects, O1 through On.

I want to plot one of their properties vs another, and I also want to be able, when user hovers over a point to retrieve the original object O whose properties this point reflects, and probably to display third property. For example if my object has field members:
double x;
double y;
String name;

I want to be able to retrieve the object whose x and y coordinates this point represents and display name property when the mouse is over this chart point.
I thought that using method add(x,y,text) will do, but it does not work as I expected.

regards,

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Associating object with chart point/finding specific chart p

Post by Yeray » Mon Jan 26, 2015 3:37 pm

Hello,

If I understand you correctly, you have something like this:

Code: Select all

		final ArrayList<MyObject> myObjectsList = new ArrayList<MyObject>();
		for (int i=0; i<10; i++) {
			myObjectsList.add(new MyObject());
			myObjectsList.get(i).label = "Label " + i;
			myObjectsList.get(i).name = "Name " + i;
			myObjectsList.get(i).x = i + 1;
			myObjectsList.get(i).y = i * 2;
		}
		
        tChart1.getAspect().setView3D(false);
        tChart1.getLegend().setVisible(false);

        final Points point1 = new Points(tChart1.getChart());
        for (int i=0; i<myObjectsList.size(); i++) {
        	point1.add(myObjectsList.get(i).x, myObjectsList.get(i).y, myObjectsList.get(i).label);
        }
        
        point1.getMarks().setVisible(true);
        MarksTip marksTip1 = new MarksTip(tChart1.getChart());
        
        tChart1.getAxes().getBottom().getLabels().setStyle(AxisLabelStyle.VALUE);
And you want the MaksTip tool to show the name property, not the label.
In that case, you could use mouseMove event to find the object that is represented under the mouse, if any, and store it in a global variable (ie objectUnderMouse). Then, you can use the object stored in this global property, if any, at the MarksTip tool getText event as follows:

Code: Select all

		tChart1.addMouseMoveListener(new MouseMoveListener() {

			@Override
			public void mouseMove(MouseEvent arg0) {
				int tmpIndex = -1;
				for (int i = 0; i < point1.getCount(); i++) {
					tmpIndex = point1.clicked(arg0.x, arg0.y);
					if (tmpIndex > -1) {
						objectUnderMouse = null;
						for (int j = 0; i < myObjectsList.size(); j++) {
							if (point1.getXValues().getValue(tmpIndex) == myObjectsList
									.get(j).x) {
								objectUnderMouse = myObjectsList.get(j);
								break;
							}
						}
						break;
					}
				}
			}
		});

		marksTip1.setToolTipResolver(new TextResolver() {

			@Override
			public String getText(Object sender, String text) {
				if (objectUnderMouse != null)
					return objectUnderMouse.name;

				return text;
			}
		});
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

plamen
Newbie
Newbie
Posts: 9
Joined: Thu Jan 08, 2015 12:00 am

Re: Associating object with chart point/finding specific chart p

Post by plamen » Mon Jan 26, 2015 3:59 pm

Thanks Yeray,

I think this will work! I will give it a shot and get back to you! I will probably use HashMap with key x_coord_value+"_"+y_coord_value and value the object itself - might be faster than rolling two embedded cycles.

thanks again and regards,
Plamen

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Associating object with chart point/finding specific chart p

Post by Yeray » Tue Jan 27, 2015 8:21 am

Hi Plamen,

Yes, a HashMap will probably be faster. We'll be glad to read your solution if you want to share it here.
And don't hesitate to let us know if you still find problems with it.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply