| Create a rectangle of selection [message #899647] |
Wed, 01 August 2012 12:29  |
Pierrick LE DAY Messages: 2 Registered: August 2012 |
Junior Member |
|
|
hello,
in a chart created with the SWTChart API, i have to implement a selection rectangle to zoom on the created selection. The zoom party isn't a problem, my problem is on the creation of the rectangle with the GC API.
Here is my code
Listener listener = new Listener() {
boolean mouseDown=false;
boolean mouseMoved=false;
private Integer oldX, oldY;
final Cursor cursorCross = new Cursor(parent.getDisplay(), SWT.CURSOR_CROSS);
final Cursor cursorNormal = new Cursor(parent.getDisplay(), SWT.CURSOR_ARROW);
public void handleEvent(Event event) {
switch (event.type) {
case SWT.MouseDown:
mouseDown=true;
break;
case SWT.MouseMove:
if(oldX!=null && oldY!=null && mouseDown) {
mouseMoved=true;
parent.setCursor(cursorCross);
GC gc= new GC(chart.getPlotArea());
gc.setLineStyle(SWT.LINE_DOT);
gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_DARK_GRAY));
gc.drawRectangle(oldX, oldY, event.x-oldX, event.y-oldY);
gc.dispose();
} else {
oldX=event.x;
oldY=event.y;
}
break;
case SWT.MouseUp:
if(mouseMoved) {
Range range= new Range(getXAxis().getDataCoordinate(oldX),getXAxis().getDataCoordinate(event.x));
getXAxis().setRange(range);
range= new Range(getYAxis().getDataCoordinate(oldY),getYAxis().getDataCoordinate(event.y));
getYAxis().setRange(range);
chart.redraw();
oldX=null;
oldY=null;
parent.setCursor(cursorNormal);
}
mouseMoved=false;
mouseDown=false;
break;
}
}
};
chart.getPlotArea().addListener(SWT.MouseDown, listener);
chart.getPlotArea().addListener(SWT.MouseUp, listener);
chart.getPlotArea().addListener(SWT.MouseMove, listener);
I draw a rectangle every time my mouse moves, but I want the old rectangle disappears once the new one is drawn.
Thx for the help.
|
|
|
| Re: Create a rectangle of selection [message #899849 is a reply to message #899647] |
Thu, 02 August 2012 11:01   |
Grant Gayed Messages: 2132 Registered: July 2009 |
Senior Member |
|
|
Hi, two answers:
1. You need to do all of your drawing from an SWT.Paint callback. To
trigger one of these from your MouseMove callback invoke
redraw(theBoundsToRedraw) on chart.getPlotArea().
2. The Tracker widget may save you from having to handle these details,
see the example snippets at http://www.eclipse.org/swt/snippets/#tracker .
Grant
On 8/1/2012 12:29 PM, Pierrick LE DAY wrote:
> hello,
>
> in a chart created with the SWTChart API, i have to implement a
> selection rectangle to zoom on the created selection. The zoom party
> isn't a problem, my problem is on the creation of the rectangle with the
> GC API.
>
> Here is my code
>
> Listener listener = new Listener() {
> boolean mouseDown=false;
> boolean mouseMoved=false;
> private Integer oldX, oldY;
> final Cursor cursorCross = new Cursor(parent.getDisplay(),
> SWT.CURSOR_CROSS);
> final Cursor cursorNormal = new Cursor(parent.getDisplay(),
> SWT.CURSOR_ARROW);
>
> public void handleEvent(Event event) {
> switch (event.type) {
> case SWT.MouseDown:
> mouseDown=true;
> break;
> case SWT.MouseMove:
> if(oldX!=null && oldY!=null && mouseDown) {
> mouseMoved=true;
> parent.setCursor(cursorCross);
> GC gc= new GC(chart.getPlotArea());
> gc.setLineStyle(SWT.LINE_DOT);
>
> gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_DARK_GRAY));
> gc.drawRectangle(oldX, oldY, event.x-oldX, event.y-oldY);
> gc.dispose();
> } else {
> oldX=event.x;
> oldY=event.y;
> }
> break;
> case SWT.MouseUp:
> if(mouseMoved) {
> Range range= new
> Range(getXAxis().getDataCoordinate(oldX),getXAxis().getDataCoordinate(event.x));
>
> getXAxis().setRange(range);
> range= new
> Range(getYAxis().getDataCoordinate(oldY),getYAxis().getDataCoordinate(event.y));
>
> getYAxis().setRange(range);
> chart.redraw();
> oldX=null;
> oldY=null;
> parent.setCursor(cursorNormal);
> }
> mouseMoved=false;
> mouseDown=false;
> break;
> }
> }
> };
> chart.getPlotArea().addListener(SWT.MouseDown, listener);
> chart.getPlotArea().addListener(SWT.MouseUp, listener);
> chart.getPlotArea().addListener(SWT.MouseMove, listener);
>
>
> I draw a rectangle every time my mouse moves, but I want the old
> rectangle disappears once the new one is drawn.
>
> Thx for the help.
|
|
|
|
| Re: Create a rectangle of selection [message #1015671 is a reply to message #900014] |
Fri, 01 March 2013 11:00  |
txas kun Messages: 5 Registered: March 2013 |
Junior Member |
|
|
Hello Pierrick
I am very interested in your post. I have problems to get my chart information from a selection. My purpose is to do a zoom to a LineChart but I am becoming crazy!! Do you mind sending me your code source? With the tracker I am visualizing the rectangle, but I have no idea how to get the points that are in the selection.(I have been searching some example/information to get the information of the dataset).
Thanks in advance!
|
|
|
Powered by
FUDForum. Page generated in 0.04063 seconds