Page 1 of 1

DragPoint Events in Java

Posted: Fri Sep 02, 2016 1:27 pm
by 17478810
Hi,

I am implementing a Dragpoint event for my chart in Java, and now I want to capture the values after the drag point event is done and save them to DB. I see the legend is being updated with the values but I am not able to capture the values. Can you suggest a way to capture the data after the Drag is complete

Re: DragPoint Events in Java

Posted: Mon Sep 05, 2016 9:34 am
by yeray
Hello,

You can use events as in the following SWT example:

Code: Select all

    static int draggingIndex = -1;
    static Series draggingSeries = null;
    static Point mousePos = null;
    
	private static void dragTest() {
		tChart1.getHeader().setVisible(false);
        tChart1.getAspect().setView3D(false);
        //tChart1.getLegend().setVisible(false);
        
        tChart1.getZoom().setActive(false);
        
        Points points1 = new Points(tChart1.getChart());
        points1.fillSampleValues(10);
        
        DragPoint dragPoints1 = new DragPoint(tChart1.getChart());
        
        tChart1.addMouseMoveListener(new MouseMoveListener() {
			
			@Override
			public void mouseMove(MouseEvent arg0) {
				mousePos = new Point(arg0.x, arg0.y);
			}
		});
        
        dragPoints1.addDragListener(new DragListener() {
			
			@Override
			public void dragMoving(ChangeEvent e) {
				if (draggingSeries == null) {
					draggingIndex = ((DragPoint)e.getSource()).getDragPoint();
					for (int t = 0; t < tChart1.getSeriesCount(); t++) {
						Series s = tChart1.getSeries(t);
						if (s.getActive()) {
							int clickedIndex = s.clicked(mousePos);
							if ((draggingIndex != -1) && (clickedIndex != -1) && (clickedIndex == draggingIndex)) {
								draggingSeries = s;
								break;
							}
						}
					}
				}
			}
			
			@Override
			public void dragFinished(ChangeEvent e) {
				if ((draggingSeries != null) && (draggingIndex != -1)) {
					System.out.println(draggingSeries.getYValues().getValue(draggingIndex));
				}
				
				draggingSeries = null;
				draggingIndex = -1;
			}
		});
	}

Re: DragPoint Events in Java

Posted: Wed Sep 07, 2016 1:43 pm
by 17478810
Can we do the same using Swing components. My project is a swing project?

Re: DragPoint Events in Java

Posted: Thu Sep 08, 2016 9:48 am
by yeray
Hello,

It's quite similar in Swing. I've improved the code a bit:

Code: Select all

	static int draggingIndex = -1;
    static Series draggingSeries = null;
    
    private static void dragTest() {
        tChart1.getHeader().setVisible(false);
          tChart1.getAspect().setView3D(false);
          //tChart1.getLegend().setVisible(false);
         
          tChart1.getZoom().setActive(false);
         
          Points points1 = new Points(tChart1.getChart());
          points1.fillSampleValues(10);
         
          DragPoint dragPoints1 = new DragPoint(tChart1.getChart());
         
          tChart1.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mousePressed(MouseEvent e) {
				if (dragPoints1.getSeries() == null) {
	                for (int t = 0; t < tChart1.getSeriesCount(); t++) {
	                    Series s = tChart1.getSeries(t);
	                    if (s.getActive()) {
	                        draggingIndex = s.clicked(e.getX(), e.getY());
	                        if (draggingIndex != -1) {
	                            draggingSeries = s;
	                            break;
	                        }
	                    }
	                }
	            } else {
	                draggingIndex = dragPoints1.getSeries().clicked(e.getX(), e.getY());
	                if (draggingIndex != -1) {
	                    draggingSeries = dragPoints1.getSeries();
	                }
	            }
			}
		
		});
         
          dragPoints1.addDragListener(new DragAdapter() {
           
           @Override
           public void dragFinished(ChangeEvent e) {
              if ((draggingSeries != null) && (draggingIndex != -1)) {
                 System.out.println(draggingSeries.getYValues().getValue(draggingIndex));
              }
              
              draggingSeries = null;
              draggingIndex = -1;
           }
        });
     }

Re: DragPoint Events in Java

Posted: Wed Oct 05, 2016 10:52 am
by 17478810
I have a chart with multiple Lines, Yet I want to enable just one of the lines as draggable. The other lines have to remain static.

Re: DragPoint Events in Java

Posted: Thu Oct 06, 2016 7:47 am
by yeray
Hello,

Have you tried to do it with the code I posted here?
http://www.teechart.net/support/viewtop ... 396#p72396

Re: DragPoint Events in Java

Posted: Mon Oct 10, 2016 3:10 pm
by 17478810
Yes,

In that code I am able to move all the lines, here is what i am talking about.

I have a TChart with 4 lines, however I want to able to move only one of the lines using DragPoint Tool. The other 3 lines should not be moved. I tried to add series to DragPoint class constructor, but I am not able to get the chart to work how i want it to work.

Re: DragPoint Events in Java

Posted: Tue Oct 11, 2016 8:51 am
by yeray
Hello,

Knowing the index of the series you want the dragging to be allowed (ie draggableSeriesIndex), you can change the mousePressed event to fit your needs. Ie:

Code: Select all

         @Override
         public void mousePressed(MouseEvent e) {
             Series s = tChart1.getSeries(draggableSeriesIndex);
             if (s.getActive()) {
                draggingIndex = s.clicked(e.getX(), e.getY());
                if (draggingIndex != -1) {
                   draggingSeries = s;

                   OldXPos = e.getX();
                   OldYPos = e.getY();

                   break;
                }
             }
         }