eclipse / swtchart

Eclipse Public License 2.0
44 stars 41 forks source link

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

Closed hadiahmadi277 closed 2 years ago

hadiahmadi277 commented 2 years ago

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?

`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;
}
}`
eselmeister commented 2 years ago

With chart.getAxisSet().adjustRange(); the chart is set to its full x and y range. A good way to explore and play is the demo chart: https://github.com/eclipse/swtchart/blob/develop/org.eclipse.swtchart.extensions.examples/src/org/eclipse/swtchart/extensions/examples/charts/DemoChart.java

hadiahmadi277 commented 2 years ago

Thanks, but some of the parts are not imported when I want to use the above code. Thank you for your help. 1 1

eselmeister commented 2 years ago

Please pull the complete repository and import all projects into your workspace. Have a look at the bundle architecture of SWTChart: https://github.com/eclipse/swtchart/wiki

hadiahmadi277 commented 2 years ago

I receive and importe all the projects into the workspace, but the DemoChart.java gives such an error.

The following ChartType has been set using the auto-detection: LINE Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at org.eclipse.swtchart.extensions.core.ResourceSupport.getImage(ResourceSupport.java:182)
at org.eclipse.swtchart.extensions.core.RangeSelector.createControl(RangeSelector.java:172)
at org.eclipse.swtchart.extensions.core.RangeSelector.<init>(RangeSelector.java:46)
at org.eclipse.swtchart.extensions.core.ScrollableChart.createRangeInfoUI(ScrollableChart.java:1419)
at org.eclipse.swtchart.extensions.core.ScrollableChart.createChart(ScrollableChart.java:1411)
at org.eclipse.swtchart.extensions.core.ScrollableChart.createChartSection(ScrollableChart.java:1350)
at org.eclipse.swtchart.extensions.core.ScrollableChart.initialize(ScrollableChart.java:1334)
at org.eclipse.swtchart.extensions.core.ScrollableChart.<init>(ScrollableChart.java:167)
at org.eclipse.swtchart.extensions.linecharts.LineChart.<init>(LineChart.java:62)
at org.eclipse.swtchart.customcharts.core.ChromatogramChart.<init>(ChromatogramChart.java:40)
at org.eclipse.swtchart.extensions.examples.parts.LineSeries_1_Part.<init>(LineSeries_1_Part.java:41)
at org.eclipse.swtchart.extensions.examples.charts.DemoChart.main(DemoChart.java:42)
eselmeister commented 2 years ago

Did you set the "Target Platform" correctly?

hadiahmadi277 commented 2 years ago

I do not understand what you mean. According to the instructions in this link, I put all the repositories in the workspace.

eselmeister commented 2 years ago

The "Target Platform" contains all necessary dependencies to run the dev environment. You have to load it in Eclipse: https://github.com/eclipse/swtchart/blob/develop/org.eclipse.swtchart.targetplatform/org.eclipse.swtchart.targetplatform.target

hadiahmadi277 commented 2 years ago

Excuse me. I have not loaded the target platform yet. How can I use this code? Thank you

eselmeister commented 2 years ago

If you're using Eclipse, then read: https://www.vogella.com/tutorials/EclipseTargetPlatform/article.html

Otherwise, fetch the dependencies via Maven.

hadiahmadi277 commented 2 years ago

Thank you very much the problem was solved.

hadiahmadi277 commented 2 years ago

Is there an example that has pan capability?

hadiahmadi277 commented 2 years ago

And I did not see the zoom out feature in the demos

eselmeister commented 2 years ago

Have a look at the zoom out handler: public class ZoomOutHandler extends AbstractChartMenuEntry implements IChartMenuEntry {

And I'm not sure what you mean with "pan capability"?

hadiahmadi277 commented 2 years ago

Pan means navigating the chart after zooming.

eselmeister commented 2 years ago

You can use the scrollbars, or the mouse scroll button. Have a look at the constraints to restrict scrolling either to only x or y. Both is activated by default:

https://github.com/eclipse/swtchart/blob/develop/org.eclipse.swtchart.extensions/src/org/eclipse/swtchart/extensions/core/RangeRestriction.java