swtchart doesn't display in dialog

282 Views Asked by At

I use swtchart (http://www.swtchart.org/) and try to display it in the dialog but it is always failing.

SWT Dialog doesn't support swtchart, does it?

public final class TestDialog extends
        Dialog {

    private Chart chart;

    private static double[] ySeries1 = {1,2,3,4,5,6,7,8,9,0};

    public TestDialog(Shell shell) {
        super(shell);
    }

    protected Control createDialogArea(Composite parent) {
        Composite composite = (Composite) super.createDialogArea(parent);
        createChart(composite);

        return composite;
    }


    static public Chart createChart(Composite parent) {

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

        // set titles
        chart.getTitle().setText("Large Series");
        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(ySeries1);
        lineSeries.setSymbolSize(2);
        // adjust the axis range
        chart.getAxisSet().adjustRange();

        return chart;
    }
}
1

There are 1 best solutions below

2
On

I don't think this issue is related to SWTChart, rather to your knowledge of SWT layouts.

Also, there's no reason for the chart to not work in a SWT dialog.

Please modify your overridden createDialogArea accordingly:

@Override
protected void createDialogArea(final Composite parent)
{
    final Composite dialogArea = (Composite) super.createDialogArea(parent);

    final Composite container = new Composite(dialogArea, SWT.NULL);
    container.setLayout(new GridLayout());
    container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    createChart(container);

    return dialogArea;
} 

Don't create your contents directly under dialogArea, rather create an intermediate container. I happen to know that dialogArea has a GridLayout layout, hence I set the GridData on the container.

You may want to read this article very carefully.


Edit 1

So apparently, your chart is being drawn, but the dialog packs and you get the impression that it didn't draw. Try the code below, and resize the dialog to see the chart.

To fit the dialog to the chart is a whole different question (regarding SWT shells and layouts).

/**
 * 
 * @author ggrec
 *
 */
public class ChartDialog extends Dialog
{

    // ====================== 2. Instance Fields =============================

    private static double[] ySeries1 = {1,2,3,4,5,6,7,8,9,0};


    // ==================== 3. Static Methods ====================

    public static void main(final String[] args)
    {
        final Display display = new Display();

        new ChartDialog(new Shell(display)).open();

        display.sleep();
        display.dispose();
    }


    // ==================== 4. Constructors ====================

    public ChartDialog(final Shell parent)
    {
        super(parent);
    }


    // ==================== 5. Creators ====================

    @Override
    protected Composite createDialogArea(final Composite parent)
    {
        final Composite dialogArea = (Composite) super.createDialogArea(parent);

        final Composite container = new Composite(dialogArea, SWT.NULL);
        container.setLayout(new FillLayout());
        container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        createChart(container);

        return dialogArea;
    } 


    @Override
    protected void setShellStyle(final int newShellStyle)
    {
        super.setShellStyle(newShellStyle | SWT.RESIZE);    
    }


    static private Chart createChart(final Composite parent)
    {
        // create a chart
        final Chart chart = new Chart(parent, SWT.NONE);

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

        // create line series
        final ILineSeries lineSeries = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, "line series");
        lineSeries.setYSeries(ySeries1);
        lineSeries.setSymbolSize(2);
        // adjust the axis range
        chart.getAxisSet().adjustRange();

        return chart;
    }

}