svacee.form.Grafico.java Source code

Java tutorial

Introduction

Here is the source code for svacee.form.Grafico.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package svacee.form;

import java.text.SimpleDateFormat;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import svacee.ctrl.ConsumoCtrl;
import svacee.model.Consumo;

/**
 *
 * @author ana
 */
public class Grafico extends javax.swing.JFrame {

    private ConsumoCtrl consumoCtrl;

    /**
     * Creates new form Grafico
     */
    public Grafico() {

        initComponents();
        //JPanel painel = createChartPanel();
        //        
        //
        //
        //        setSize(640, 480);
        //        setDefaultCloseOperation(jFrame1.DISPOSE_ON_CLOSE);
        //        setLocationRelativeTo(null);
        //        
        //        myChartPanel.setSize(jPanel2.getWidth(), jPanel2.getHeight());
        //        
        //        myChartPanel.setVisible(true);
        //        
        //        jPanel2.removeAll();
        //     
        //        jPanel2.add(myChartPanel);
        //
        //        jPanel2.revalidate();
        //        
        //        jPanel2.repaint();
    }

    /**
     * @param args the command line arguments
     */
    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(TabelaDados.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TabelaDados.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TabelaDados.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TabelaDados.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new TabelaDados().setVisible(true);

            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jFrame1 = new javax.swing.JFrame();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();

        jMenuItem2.setText("jMenuItem2");

        jMenuItem3.setText("jMenuItem3");

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(jFrame1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE));
        jFrame1Layout.setVerticalGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(java.awt.Color.white);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 558, Short.MAX_VALUE));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 313, Short.MAX_VALUE));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jPanel2,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jPanel2,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(61, 61, 61)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addGap(0, 1, Short.MAX_VALUE).addComponent(jPanel1,
                        javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFrame jFrame1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

    //    private CategoryDataset createDataset() {
    //        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    //       
    //
    //        List<Consumo> listaDados = new ArrayList();
    //        try {
    //            listaDados = consumoCtrl.getListaDados();
    //        } catch (Exception e) {
    //            JOptionPane.showMessageDialog(null, "ERRO" + JOptionPane.ERROR_MESSAGE);
    //        }
    //        if (!listaDados.isEmpty()) {
    //            Iterator it = listaDados.iterator();
    //            while (it.hasNext()) {
    //                dataset = new DefaultCategoryDataset();
    //                Consumo c = (Consumo) it.next();
    //                dataset.addValue(c.getValor(), c.getDataHora(), c.getIdColeta());
    //                
    //
    //            }
    //        }
    //       
    //        return dataset;
    //    }
    //    
    //    public void criaGrafico() {
    //        
    //         CategoryDataset cds = createDataset();
    //        
    //        String titulo = "Grfico de Teste";
    //        
    //        String eixoy = "Valores";
    //        
    //        String txt_legenda = "Ledenda:";
    //        
    //        boolean legenda = true;
    //        
    //        boolean tooltips = true;
    //        
    //        boolean urls = true;
    //        
    //        JFreeChart graf = ChartFactory.createBarChart3D(titulo, txt_legenda, eixoy, cds, PlotOrientation.VERTICAL, legenda, tooltips, urls);
    //        
    //        ChartPanel myChartPanel = new ChartPanel(graf, true);
    //        
    //        myChartPanel.setSize(jPanel2.getWidth(), jPanel2.getHeight());
    //        
    //        myChartPanel.setVisible(true);
    //        
    //        jPanel2.removeAll();
    //     
    //        jPanel2.add(myChartPanel);
    //
    //        jPanel2.revalidate();
    //        
    //        jPanel2.repaint();
    //        
    //    }
    public JPanel createChartPanel() {

        String chartTitle = "Grfico de Consumo";
        String xAxisLabel = "Hora";
        String yAxisLabel = "Kw/h";

        XYDataset dataset = createDataset();

        JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, xAxisLabel, yAxisLabel, dataset);

        XYPlot plot = chart.getXYPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        plot.setRenderer(renderer);

        return new ChartPanel(chart);
    }

    //    private XYDataset createDataset() {
    //        XYSeriesCollection dataset = new XYSeriesCollection();
    //        XYSeries series1 = new XYSeries("Object 1");
    //        XYSeries series2 = new XYSeries("Object 2");
    //        XYSeries series3 = new XYSeries("Object 3");
    //
    //        series1.add(1.0, 2.0);
    //        series1.add(2.0, 3.0);
    //        series1.add(3.0, 2.5);
    //        series1.add(3.5, 2.8);
    //        series1.add(4.2, 6.0);
    //
    //        series2.add(2.0, 1.0);
    //        series2.add(2.5, 2.4);
    //        series2.add(3.2, 1.2);
    //        series2.add(3.9, 2.8);
    //        series2.add(4.6, 3.0);
    //
    //        series3.add(1.2, 4.0);
    //        series3.add(2.5, 4.4);
    //        series3.add(3.8, 4.2);
    //        series3.add(4.3, 3.8);
    //        series3.add(4.5, 4.0);
    //
    //        dataset.addSeries(series1);
    //        dataset.addSeries(series2);
    //        dataset.addSeries(series3);
    //
    //        return dataset;
    //    }

    //    private XYDataset createDataset() {
    //        XYSeriesCollection dataset = new XYSeriesCollection();
    //        XYSeries Robo1 = new XYSeries("Robo 1");
    //        XYSeries Serra1 = new XYSeries("Serra  1");
    //        XYSeries Esteira1 = new XYSeries("Esteira  1");
    //
    //        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH.mm");
    //
    //        for (Consumo c : consumoCtrl.getListaDados()) {
    //            System.out.println(c.getDataHora().toString() + " " + c.getIdColeta() + " " + c.getValor());
    //            if (c.getIdColeta().equalsIgnoreCase("ROB 1")) {
    //                Robo1.add(Double.parseDouble(simpleDateFormat.format(c.getDataHora())), c.getValor());
    //                System.out.println(Robo1.toString());
    //            }
    //            if (c.getIdColeta().equalsIgnoreCase("SERRA 1")) {
    //                Serra1.add(Double.parseDouble(simpleDateFormat.format(c.getDataHora())), c.getValor());
    //            }
    //            if (c.getIdColeta().equalsIgnoreCase("ESTEIRA 1")) {
    //                Esteira1.add(Double.parseDouble(simpleDateFormat.format(c.getDataHora())), c.getValor());
    //            }
    //        }
    //
    //        dataset.addSeries(Robo1);
    //        dataset.addSeries(Serra1);
    //        dataset.addSeries(Esteira1);
    //
    //        return dataset;
    //
    //    }

    private XYDataset createDataset() {
        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries Robo1 = new XYSeries("Robo 1");
        XYSeries Serra1 = new XYSeries("Serra  1");
        XYSeries Esteira1 = new XYSeries("Esteira  1");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH.mm");

        for (Consumo c : consumoCtrl.getListaDados()) {
            System.out.println(c.getDataHora().toString() + " " + c.getIdColeta() + " " + c.getValor());
            if (c.getIdColeta().equalsIgnoreCase(" ROB 1")) {
                Robo1.add(Double.parseDouble(simpleDateFormat.format(c.getDataHora())), c.getValor());
                System.out.println(Robo1.toString());
            }
            if (c.getIdColeta().equalsIgnoreCase(" SERRA 1")) {
                Serra1.add(Double.parseDouble(simpleDateFormat.format(c.getDataHora())), c.getValor());
            }
            if (c.getIdColeta().equalsIgnoreCase(" ESTEIRA 1")) {
                Esteira1.add(Double.parseDouble(simpleDateFormat.format(c.getDataHora())), c.getValor());
            }
        }

        dataset.addSeries(Robo1);
        dataset.addSeries(Serra1);
        dataset.addSeries(Esteira1);

        return dataset;

    }

    /**
     * @return the consumoCtrl
     */
    public ConsumoCtrl getConsumoCtrl() {
        return consumoCtrl;
    }

    /**
     * @param consumoCtrl the consumoCtrl to set
     */
    public void setConsumoCtrl(ConsumoCtrl consumoCtrl) {
        System.out.println("oi");
        this.consumoCtrl = consumoCtrl;

    }
}