如何在JFrame JLabel中显示JFreeChart

时间:2010-10-18 21:09:36

标签: java jfreechart

我大部分时间都在阅读“The JFreeChart Class Library v1.0.13开发人员指南”及相关示例。 我相信我知道如何创建图表,我只是不清楚如何在JFrame JLabel中显示它。 我试过这个时

LineChartDemo1 chart = new LineChartDemo1("Chart title");
jLabelChart.add(chart);
chart.setVisible(true);

我得到了

  

线程“AWT-EventQueue-0”中的异常   java.lang.IllegalArgumentException异常:   向容器添加窗口

我看到我正在尝试的是非法的,但我应该用什么方法来显示图表?我不应该使用JLabel吗?

/**
 * A simple demonstration application showing how to create a line chart using
 * data from a {@link CategoryDataset}.
 */
public class LineChartDemo1 extends ApplicationFrame {

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */
    public LineChartDemo1(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * Creates a sample dataset.
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(212, "Classes", "JDK 1.0");
        dataset.addValue(504, "Classes", "JDK 1.1");
        dataset.addValue(1520, "Classes", "JDK 1.2");
        dataset.addValue(1842, "Classes", "JDK 1.3");
        dataset.addValue(2991, "Classes", "JDK 1.4");
        dataset.addValue(3500, "Classes", "JDK 1.5");
        return dataset;
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset  a dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(CategoryDataset dataset) {

        // create the chart...
        JFreeChart chart = ChartFactory.createLineChart(
            "Java Standard Class Library",   // chart title
            null,                       // domain axis label
            "Class Count",                   // range axis label
            dataset,                         // data
            PlotOrientation.VERTICAL,        // orientation
            false,                           // include legend
            true,                            // tooltips
            false                            // urls
        );

        chart.addSubtitle(new TextTitle("Number of Classes By Release"));
        TextTitle source = new TextTitle(
                "Source: Java In A Nutshell (5th Edition) "
                + "by David Flanagan (O'Reilly)");
        source.setFont(new Font("SansSerif", Font.PLAIN, 10));
        source.setPosition(RectangleEdge.BOTTOM);
        source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        chart.addSubtitle(source);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setRangePannable(true);
        plot.setRangeGridlinesVisible(false);
        URL imageURL = LineChartDemo1.class.getClassLoader().getResource(
            "OnBridge11small.png");
        if (imageURL != null) {
            ImageIcon temp = new ImageIcon(imageURL);
            // use ImageIcon because it waits for the image to load...
            chart.setBackgroundImage(temp.getImage());
            plot.setBackgroundPaint(null);
        }
        // customise the range axis...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        ChartUtilities.applyCurrentTheme(chart);

        // customise the renderer...
        LineAndShapeRenderer renderer
                = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseShapesVisible(true);
        renderer.setDrawOutlines(true);
        renderer.setUseFillPaint(true);
        renderer.setBaseFillPaint(Color.white);
        renderer.setSeriesStroke(0, new BasicStroke(3.0f));
        renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f));
        renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0));
        return chart;
    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        ChartPanel panel = new ChartPanel(chart);
        panel.setMouseWheelEnabled(true);
        return panel;
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        LineChartDemo1 demo = new LineChartDemo1(
                "JFreeChart: LineChartDemo1.java");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:3)

您的示例代码效果很好,但我认为将ChartPanelJPanel的子类)放在JLabel中没有任何意义。请注意LineChartDemo1 extends ApplicationFrameJFrame的子类),它继承了后者的默认BorderLayout。您可以将图表添加到中心并相应地放置其他组件。

public LineChartDemo1(String title) {
    super(title);
    JPanel chartPanel = createDemoPanel();
    chartPanel.setPreferredSize(new Dimension(500, 270));
    this.add(chartPanel, BorderLayout.CENTER);
    this.add(new JLabel("Mouse wheel!", JLabel.CENTER), BorderLayout.SOUTH);
}

以下是相关的demo,不要忽略org.jfree.chart.demo中的示例。为方便起见,每个API文档都链接到相应的源代码。