Java tutorial
/* This file is part of eGorilla. eGorilla is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. eGorilla 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 General Public License for more details. You should have received a copy of the GNU General Public License along with eGorilla. If not, see <http://www.gnu.org/licenses/>. */ /* * GUIPanelGrafica.java * * Created on 21 de abril de 2009, 0:14 */ package gui.grafica.estadisticas; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Date; import org.apache.log4j.Logger; import org.jfree.chart.ChartFactory; 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; /** * * @author Qiang */ public class GUIPanelGrafica extends javax.swing.JPanel { BufferedImage buffer; ArrayList<Date> listaEjeX; ArrayList<Double> listaEjeY; public void setLeyendaEjeX(String leyendaEjeX) { this.leyendaEjeX = leyendaEjeX; } public void setLeyendaEjeY(String leyendaEjeY) { this.leyendaEjeY = leyendaEjeY; } public void setTitulo(String titulo) { this.titulo = titulo; } String titulo; String leyendaEjeX; String leyendaEjeY; private static Logger log = Logger.getLogger(GUIPanelGrafica.class); /** Creates new form GUIPanelGrafica */ public GUIPanelGrafica() { listaEjeX = new ArrayList(); listaEjeY = new ArrayList(); this.setDoubleBuffered(true); initComponents(); } /** 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() { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300, Short.MAX_VALUE)); }// </editor-fold>//GEN-END:initComponents @Override public void paint(java.awt.Graphics g) { //super.paint(g); if (buffer == null) { buffer = creaImagen(); } float fancho = this.getAlignmentX(); float falto = this.getAlignmentY(); int alto = Math.round(falto); int ancho = Math.round(fancho); if (g == null) { // g = new Graphics(); } g.drawImage(buffer, alto, ancho, null); } public void setBufferImage(BufferedImage buff) { buffer = buff; } public BufferedImage creaImagen() { XYSeries series = new XYSeries("Evolucion"); for (int i = 0; i < listaEjeY.size(); i++) { Date date = listaEjeX.get(i); date.getTime(); series.add(date.getTime(), listaEjeY.get(i)); } // series.add(4, 23); // series.add(2, 34); // series.add(3, 51); // series.add(4, 67); // series.add(5, 89); // series.add(6, 121); // series.add(7, 137); XYDataset juegoDatos = new XYSeriesCollection(series); log.info(listaEjeX.toString()); JFreeChart chart = ChartFactory.createXYLineChart(titulo, leyendaEjeX, leyendaEjeY, juegoDatos, PlotOrientation.VERTICAL, false, false, true // Show legend ); log.info("Valor de Width:" + getWidth()); log.info("Valor de Heigth:" + getHeight()); BufferedImage image = chart.createBufferedImage(getWidth(), getHeight()); return image; } public void setEjeX(ArrayList valores) { listaEjeX.clear(); listaEjeX.addAll(valores); } public void setEjeY(ArrayList<Double> valores) { listaEjeY.clear(); listaEjeY.addAll(valores); } public void changeImage() { buffer = null; } public void setInicioX(ArrayList valores) { } // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables }