br.com.una.apa.p02e01.Graph.java Source code

Java tutorial

Introduction

Here is the source code for br.com.una.apa.p02e01.Graph.java

Source

package br.com.una.apa.p02e01;

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * -------------------
 * Graph.java
 * -------------------
 * (C) Copyright 2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: Graph.java,v 1.5 2004/04/26 19:11:55 taqua Exp $
 *
 * Changes
 * -------
 * 27-Jan-2004 : Version 1 (DG);
 * 
 */

import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

public class Graph extends ApplicationFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Creates a new chart.
     *
     * @param title
     *            the frame title.
     */
    public Graph(final String title, int[] cordenadas) {

        super(title);

        final XYDataset dataset = createDataset(LenghtVector.cordenadas(cordenadas));
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * Creates a sample dataset.
     * 
     * @return a sample dataset.
     */
    private XYDataset createDataset(ArrayList<Grafico> lista) {
        final XYSeriesCollection dataset = new XYSeriesCollection();

        for (int i = 0; i < lista.size(); i++) {
            XYSeries series = new XYSeries(lista.get(i).getNome());
            series.add(0, lista.get(i).getY());
            series.add(lista.get(i).getX(), lista.get(i).getY());
            dataset.addSeries(series);
        }

        return dataset;

    }

    /**
     * Creates a chart.
     * 
     * @param dataset
     *            the data for the chart.
     * 
     * @return a chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {

        // create the chart...
        final JFreeChart chart = ChartFactory.createXYLineChart("Tam Vetor (N) X Numero de operacoes (OP)",
                // chart title
                "TAM VETOR (N)", // x axis label
                "OPERACOES OP", // y axis label
                dataset, // data
                PlotOrientation.VERTICAL, true, // include legend
                true, // tool tips
                false // urls
        );

        return chart;
    }
}