How can I add zoom and Scroll in swtchart in Java?

186 Views Asked by At

I used the zoom and scroll chart codes based on the description of the following swtchart document, but it does not work. Is there a solution?

Description for zoom:

Zoom of axis is an operation to zoom in or out the range of axis.

enter image description here

The following example code zooms in and out the range of Y axis.

IAxis yAxis = axisSet.getYAxis(0);
yAxis.zoomIn();
yAxis.zoomOut();

Description for Scroll:

Scroll of axis is an operation to scroll up or down the range of axis.

enter image description here

The following example code scrolls up or down the range of Y axis.

IAxis yAxis = axisSet.getYAxis(0);
yAxis.scrollUp();
yAxis.scrollDown();

My code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtchart.Chart;
import org.eclipse.swtchart.IAxis;
import org.eclipse.swtchart.IAxisSet;
import org.eclipse.swtchart.ILineSeries;
import org.eclipse.swtchart.ISeries.SeriesType;
public class LineChartExample {

private static final double[] ySeries = { 0.0, 0.38, 0.71, 0.92, 1.0, 0.92,
        0.71, 0.38, 0.0, -0.38, -0.71, -0.92, -1.0, -0.92, -0.71, -0.38 };

/**
 * The main method.
 * 
 * @param args
 *            the arguments
 */
public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Line Chart");
    shell.setSize(500, 400);
    shell.setLayout(new FillLayout());

    createChart(shell);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

/**
 * create the chart.
 * 
 * @param parent
 *            The parent composite
 * @return The created chart
 */
static public Chart createChart(Composite parent) {

    // create a chart
    Chart chart = new Chart(parent, SWT.NONE);

    // set titles
    chart.getTitle().setText("Line Chart");
    chart.getAxisSet().getXAxis(0).getTitle().setText("Data Points");
    chart.getAxisSet().getYAxis(0).getTitle().setText("Amplitude");

    // create line series
    ILineSeries lineSeries = (ILineSeries) chart.getSeriesSet()
            .createSeries(SeriesType.LINE, "line series");
    lineSeries.setYSeries(ySeries);
    IAxisSet axisSet = chart.getAxisSet();
    IAxis yAxis = axisSet.getYAxis(0);
    yAxis.zoomIn();
    yAxis.zoomOut();
    yAxis.scrollUp();
    yAxis.scrollDown();

    // adjust the axis range
    chart.getAxisSet().adjustRange();

    return chart;
}
}
1

There are 1 best solutions below

1
On

The following are example from Github that could help you:

Handling MouseWheelListener MouseEvent for ZoomIn/ZoomOut:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseWheelListener;
import org.eclipse.swt.graphics.Rectangle;
import org.swtchart.Chart;
import org.swtchart.IAxis;
import org.swtchart.Range;

import cz.cuni.mff.respefo.ReSpefo;

/**
 * Adds zooming capabilities.
 * Hold ctrl and scroll mouse wheel to zoom in or out.
 */
public class MouseWheelZoomListener implements MouseWheelListener {
    private boolean zoomXCentred;
    private boolean zoomYCentred;
    
    public MouseWheelZoomListener() {
        this(false, false);
    }
    
    public MouseWheelZoomListener(boolean zoomXCentred, boolean zoomYCentred) {
        this.zoomXCentred = zoomXCentred;
        this.zoomYCentred = zoomYCentred;
    }
    
    @Override
    public void mouseScrolled(MouseEvent event) {
        if ((event.stateMask & SWT.CTRL) == SWT.CTRL) {
            Chart chart = ReSpefo.getChart();
            
            if (event.count > 0) {
                if (zoomXCentred && zoomYCentred) {
                    chart.getAxisSet().zoomIn();
                } else {
                    Rectangle bounds = chart.getPlotArea().getBounds();
                    if (zoomXCentred) {
                        for (IAxis axis : ReSpefo.getChart().getAxisSet().getXAxes()) {
                            axis.zoomIn();
                        }
                    } else {
                        Range chartXRange = chart.getAxisSet().getXAxis(0).getRange();
                        double realX = chartXRange.lower + ((chartXRange.upper - chartXRange.lower) * ((double)event.x / bounds.width));
                        
                        for (IAxis axis : ReSpefo.getChart().getAxisSet().getXAxes()) {
                            axis.zoomIn(realX);
                        }
                    }
    
                    if (zoomYCentred) {
                        for (IAxis axis : ReSpefo.getChart().getAxisSet().getYAxes()) {
                            axis.zoomIn();
                        }
                    } else {
                        Range chartYRange = chart.getAxisSet().getYAxis(0).getRange();
                        double realY = chartYRange.upper - ((chartYRange.upper - chartYRange.lower) * ((double)event.y / bounds.height));
                        
                        for (IAxis axis : ReSpefo.getChart().getAxisSet().getYAxes()) {
                            axis.zoomIn(realY);
                        }
                    }
                }
                
                
            } else if (event.count < 0) {
                chart.getAxisSet().zoomOut();
            }
            
            chart.redraw();
        }
    }

}

Reference: https://github.com/harmanea/reSpefo/blob/master/reSpefo/src/cz/cuni/mff/respefo/listeners/MouseWheelZoomListener.java

https://javadoc.scijava.org/Eclipse/org/eclipse/swt/events/MouseWheelListener.html


KeyAdapter

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.swtchart.IAxis;

import cz.cuni.mff.respefo.ReSpefo;
import cz.cuni.mff.respefo.util.ChartUtils;

/**
 * Adds the default movement capabilities
 * 
 * Listens for:
 * 'w' / Arrow Up = Move the view up
 * 'a' / Arrow Left = Move the view left
 * 's' / Arrow Down = Move the view down
 * 'd' / Arrow Right = Move the view right
 * 
 * '+' = Zoom in
 * '-' = Zoom out
 * 
 * '8' / Numpad 8 = Zoom in Y Axis
 * '2' / Numpad 2 = Zoom Out Y Axis
 * '6' / Numpad 6 = Zoom in X Axis
 * '4' / Numpad 4 = Zoom Out X Axis
 * 
 * Space = Adjust the view so that all series are visible
 */
public class DefaultKeyListener extends KeyAdapter {
    protected Map<Integer, Consumer<KeyEvent>> handlers;
    
    public DefaultKeyListener() {
        handlers = new HashMap<>();
        
        handlers.put((int) 'w', DefaultKeyListener::moveUp);
        handlers.put(SWT.ARROW_UP, DefaultKeyListener::moveUp);
        
        handlers.put((int) 'a', DefaultKeyListener::moveLeft);
        handlers.put(SWT.ARROW_LEFT, DefaultKeyListener::moveLeft);
        
        handlers.put((int) 's', DefaultKeyListener::moveDown);
        handlers.put(SWT.ARROW_DOWN, DefaultKeyListener::moveDown);
        
        handlers.put((int) 'd', DefaultKeyListener::moveRight);
        handlers.put(SWT.ARROW_RIGHT, DefaultKeyListener::moveRight);
        
        handlers.put((int) '+', DefaultKeyListener::zoomIn);
        handlers.put(16777259, DefaultKeyListener::zoomIn);
        handlers.put(49, DefaultKeyListener::zoomIn);
        
        handlers.put((int) '-', DefaultKeyListener::zoomOut);
        handlers.put(16777261, DefaultKeyListener::zoomOut);
        handlers.put(47, DefaultKeyListener::zoomOut);
        
        handlers.put((int) '8', DefaultKeyListener::zoomInYAxis);
        handlers.put(SWT.KEYPAD_8, DefaultKeyListener::zoomInYAxis);
        
        handlers.put((int) '2', DefaultKeyListener::zoomOutYAxis);
        handlers.put(SWT.KEYPAD_2, DefaultKeyListener::zoomOutYAxis);
        
        handlers.put((int) '6', DefaultKeyListener::zoomInXAxis);
        handlers.put(SWT.KEYPAD_6, DefaultKeyListener::zoomInXAxis);
        
        handlers.put((int) '4', DefaultKeyListener::zoomOutXAxis);
        handlers.put(SWT.KEYPAD_4, DefaultKeyListener::zoomOutXAxis);
        
        handlers.put((int) SWT.SPACE, DefaultKeyListener::adjustView);
    }
    
    @Override
    public void keyPressed(KeyEvent event) {
        Consumer<KeyEvent> handler = handlers.get(event.keyCode);
        if (handler != null) {
            handler.accept(event);
            
            if (ReSpefo.getChart() != null && !ReSpefo.getChart().isDisposed()) {
                ReSpefo.getChart().redraw();
            }
        }
    }

    private static void moveUp(KeyEvent event) {
        for (IAxis axis : ReSpefo.getChart().getAxisSet().getYAxes()) {
            axis.scrollUp();
        }
    }
    
    private static void moveDown(KeyEvent event) {
        for (IAxis axis : ReSpefo.getChart().getAxisSet().getYAxes()) {
            axis.scrollDown();
        }
    }
    
    private static void moveRight(KeyEvent event) {
        for (IAxis axis : ReSpefo.getChart().getAxisSet().getXAxes()) {
            axis.scrollUp();
        }
    }
    
    private static void moveLeft(KeyEvent event) {
        for (IAxis axis : ReSpefo.getChart().getAxisSet().getXAxes()) {
            axis.scrollDown();
        }
    }
    
    private static void zoomIn(KeyEvent event) {
        ReSpefo.getChart().getAxisSet().zoomIn();
    }
    
    private static void zoomOut(KeyEvent event) {
        ReSpefo.getChart().getAxisSet().zoomOut();
    }
    
    private static void zoomInYAxis(KeyEvent event) {
        for (IAxis axis : ReSpefo.getChart().getAxisSet().getYAxes()) {
            axis.zoomIn();
        }
    }
    
    private static void zoomOutYAxis(KeyEvent event) {
        for (IAxis axis : ReSpefo.getChart().getAxisSet().getYAxes()) {
            axis.zoomOut();
        }
    }
    
    private static void zoomInXAxis(KeyEvent event) {
        for (IAxis axis : ReSpefo.getChart().getAxisSet().getXAxes()) {
            axis.zoomIn();
        }
    }
    
    private static void zoomOutXAxis(KeyEvent event) {
        for (IAxis axis : ReSpefo.getChart().getAxisSet().getXAxes()) {
            axis.zoomOut();
        }
    }
    
    private static void adjustView(KeyEvent event) {
        ChartUtils.adjustRange(ReSpefo.getChart());
    }
}

Reference: https://github.com/harmanea/reSpefo/blob/master/reSpefo/src/cz/cuni/mff/respefo/listeners/DefaultKeyListener.java

https://download.eclipse.org/rt/rap/doc/3.13/guide/reference/api/org/eclipse/swt/events/KeyAdapter.html

As you can see in the examples above, it is not simply just calling zoomIn/zoomOut, and scrollUp/scrollDown, you need to make it aligned with Key Listeners, and/or MouseWheel events.

In your code, you zoomedIn/Zoomed out...so basically they cancelled each other.