[RESOLVIDO]Duvida sobre o JFreeChart

1 resposta
maurijava

Pessoal,

Como faço para criar um gráfico do tipo TimeSeries com o JFreeChart a partir de um ResultSet?

Ex:

No código abaixo ele usa um Tipo Day com uma entrada bem manual, eu gostaria de pegar estas informações de uma consulta (um ResultSet) e popular esta série, quem puder me ajudar agradeço.

public class TimeSeriesExample {
    public static void main(String[] args) {
    // Create a time series chart

   TimeSeries pop = new TimeSeries("Population", Day.class);
      pop.add(new Day(10, 1, 2004), 100);
      pop.add(new Day(10, 2, 2004), 150);
      pop.add(new Day(10, 3, 2004), 250);
      pop.add(new Day(10, 4, 2004), 275);
      pop.add(new Day(10, 5, 2004), 325);
      pop.add(new Day(10, 6, 2004), 425);

   TimeSeriesCollection dataset = new TimeSeriesCollection();
   dataset.addSeries(pop);
   JFreeChart chart = ChartFactory.createTimeSeriesChart(
       "Population of CSC408 Town",
       "Date",
       "Population",
       dataset,
        true,
        true,
        false);
     try {
           ChartUtilities.saveChartAsJPEG(new File("C:\\chart.jpg"), chart, 500, 300);
          } catch (IOException e) {
           System.err.println("Problem occurred creating chart.");
         }
      }
}

Sds,

Maurijava

1 Resposta

maurijava

Descobri como faz!

A quem interessar possa, segue:

package plotargraficos;

import br.com.mda.connect.ConnectAccess;
import java.sql.Connection;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.jdbc.JDBCXYDataset;


/**
 *
 * @author mda
 */
public class TimeSeriesChart extends JFrame{
    
    private static final long serialVersionUID = 1L;
    static String query = "Select HORA, BQR_RL_78409_VAB, BQR_RL_78409_VBC, BQR_RL_78409_VCA from USA_BQR";

    private TimeSeriesChart(Connection con, String query) {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        JDBCXYDataset dataset;
        try {
            dataset = new JDBCXYDataset(con,query);
            JFreeChart chart = ChartFactory.createTimeSeriesChart("Primeiro Grafico", "Periodo", "Grandezas", dataset, true, true, true);
            ChartPanel cpane = new ChartPanel(chart);
            cpane.setPreferredSize(new java.awt.Dimension(1024, 728));
            setContentPane(cpane);
         
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
    }
    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Tabela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Tabela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Tabela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Tabela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TimeSeriesChart(ConnectAccess.ConnectDBC(),query).setVisible(true);
            }
        });
    }
    
}

Sds,

Maurijava

Criado 11 de fevereiro de 2012
Ultima resposta 12 de fev. de 2012
Respostas 1
Participantes 1