001package bradleyross.opensource.jfreechart;
002import org.jfree.chart.ChartFactory;
003import org.jfree.chart.ChartUtilities;
004import org.jfree.chart.JFreeChart;
005import org.jfree.chart.ChartFrame;
006import org.jfree.data.xy.XYSeries;
007import org.jfree.data.xy.XYSeriesCollection;
008import org.jfree.chart.plot.PlotOrientation;
009import java.io.File;
010/**
011 * Example of generating XY chart.
012 * <p>This example was taken from
013 *    <a href="http://www.screaming-penguin.com/node/4005" target="_blank">
014 *    http://www.screaming-penguin.com/node/4005</a>.  I added some
015 *    additional comments and completed the code sample.  (Primarily adding package
016 *    and import statements.)</p>
017 * <p>When the program is run from within Eclipse, there doesn't seem to be
018 *    any way to shut it down withut shutting down eclipse.  When it is started
019 *    from the command line, it is resizable and has its own menu bar which
020 *    allows it to be closed.  The problem may be a combination of Swing and
021 *    SWT graphics.</p>
022 * @author Bradley Ross
023 * @see ChartFactory
024 * @see ChartFrame
025 * @see ChartUtilities
026 * @see JFreeChart
027 * @see PlotOrientation
028 * @see XYSeries
029 * @see XYSeriesCollection
030 *
031 */
032public class XYChartExample {
033
034        /**
035         * Creates and displays the chart.
036         * @param args If a parameter is specified, it is used as the file name to be
037         *    generated.  If there are parameters in the array args, the chart is
038         *    displayed in a window.
039         */
040        public static void main(String[] args) {
041                XYSeries series = new XYSeries("XYGraph");
042                series.add(1, 1);
043                series.add(1, 2);
044                series.add(2, 1);
045                series.add(3, 9);
046                series.add(4, 10);
047                XYSeriesCollection dataset = new XYSeriesCollection();
048                dataset.addSeries(series);
049                JFreeChart chart = ChartFactory.createXYLineChart("XY Chart",
050                                "x-axis",
051                                "y-axis",
052                                dataset,
053                                PlotOrientation.VERTICAL,
054                                true,
055                                true,
056                                false);
057                try
058                {
059                        if (args.length > 0)
060                        { ChartUtilities.saveChartAsJPEG(new File(args[0]), chart, 500, 300); }
061                        else
062                        {
063                                javax.swing.JMenuBar menuBar = new javax.swing.JMenuBar();
064                                ChartFrame frame = new ChartFrame("sample", chart);
065                                frame.setJMenuBar(menuBar);
066                                frame.pack();
067                                frame.setVisible(true);
068                        }
069                }
070                catch (Exception e)
071                {
072                        System.out.println("Problem occurred creating chart");
073                }
074        }
075}