简体   繁体   中英

JFreeChart Pie Chart not expanding in RCP viewpart

The JFreeChart Pie Chart doesn't expand to fill the composite panel when using GridLayout. I am trying to use it in a viewpart with Eclipse Indigo but it only seems to work when using FillLayout in a shell...

public class SWTPieChart {  
    public static void main(String[] args) {        
        JFreeChart chart = createChart(createDataset());
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setSize(600, 400);
        shell.setLayout(new FillLayout());
        shell.setText("JFreeChart with GridLayout");

        Composite panel = new Composite(shell, SWT.BORDER);
        panel.setLayout(new GridLayout(1, true));
        panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final ChartComposite frame = new ChartComposite(panel, SWT.NONE, chart, true);
        frame.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

    private static PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("One", new Double(43.2));
        dataset.setValue("Two", new Double(10.0));
        dataset.setValue("Three", new Double(27.5));
        dataset.setValue("Four", new Double(17.5));
        dataset.setValue("Five", new Double(11.0));
        dataset.setValue("Six", new Double(19.4));
        return dataset;        
    }

    private static JFreeChart createChart(PieDataset dataset) {
        JFreeChart chart = ChartFactory.createPieChart3D("3D Pie Chart", dataset, true, true, false);

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setSectionOutlinesVisible(true);
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(true);
        return chart;
    }
}

You incorrectly set the layout data to the composite ( panel ) instead of the chart ( frame ).

Since the composite is inside a shell with FillLayout it doesn't have to set any layout data.

The chart instead is inside the composite with GridLayout so it has to specify a layout data for it:

 Composite panel = new Composite(shell, SWT.BORDER);
 panel.setLayout(new GridLayout(1, true));

 final ChartComposite frame = new ChartComposite(panel, SWT.NONE, chart, true);
 frame.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM