Java tutorial
/* * 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 pe.egcc.eureka.app.view; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.data.JRMapCollectionDataSource; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; import pe.egcc.eureka.app.controller.CuentaController; import pe.egcc.eureka.app.util.MyMessage; import pe.egcc.eureka.app.util.ReportViewInternal; /** * * @author Profesor */ public class RepoResumen extends javax.swing.JInternalFrame { /** * Creates new form RepoResumen */ public RepoResumen() { 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() { jLabel1 = new javax.swing.JLabel(); txtSucursal = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); txtMoneda = new javax.swing.JTextField(); btnEjecutar = new javax.swing.JButton(); panelReporte = new javax.swing.JPanel(); setClosable(true); setIconifiable(true); setMaximizable(true); setResizable(true); setTitle("RESUMEN DE OPERACIONES"); jLabel1.setText("Sucursal:"); jLabel2.setText("Moneda:"); btnEjecutar.setText("Ejecutar"); btnEjecutar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEjecutarActionPerformed(evt); } }); javax.swing.GroupLayout panelReporteLayout = new javax.swing.GroupLayout(panelReporte); panelReporte.setLayout(panelReporteLayout); panelReporteLayout.setHorizontalGroup(panelReporteLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE)); panelReporteLayout.setVerticalGroup(panelReporteLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(panelReporte, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtSucursal, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtMoneda, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnEjecutar).addGap(0, 305, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1) .addComponent(txtSucursal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2) .addComponent(txtMoneda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnEjecutar)).addGap(18, 18, 18) .addComponent(panelReporte, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void btnEjecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEjecutarActionPerformed try { // Datos String sucursal = txtSucursal.getText(); String moneda = txtMoneda.getText(); CuentaController cuentaController; cuentaController = new CuentaController(); List<Map<String, ?>> lista; lista = (List) cuentaController.getResumen(sucursal, moneda); // Usando una coleccin de datos JRMapCollectionDataSource data; data = new JRMapCollectionDataSource(lista); // Los parmetros Map pars = new HashMap(); String logoFile = "/pe/egcc/eureka/app/img/logo.gif"; InputStream logoStrean = getClass().getResourceAsStream(logoFile); pars.put("LOGO", logoStrean); pars.put("RESUMEN", crearImagen(lista)); // Reporte String repoFile = "/pe/egcc/eureka/app/reports/repoResumen2.jrxml"; InputStream repoJRXML = getClass().getResourceAsStream(repoFile); JasperReport repoComp = JasperCompileManager.compileReport(repoJRXML); JasperPrint print = JasperFillManager.fillReport(repoComp, pars, data); // Visualizando el Reporte ReportViewInternal viewer = new ReportViewInternal(); viewer.setReporte(print); viewer.setTitle("RESUMEN POR SUCURSAL"); panelReporte.removeAll(); panelReporte.add(viewer); viewer.setMaximum(true); viewer.setMaximizable(false); viewer.setIconifiable(false); viewer.setClosable(false); viewer.setVisible(true); } catch (Exception e) { MyMessage.showError(this, e.getMessage()); } }//GEN-LAST:event_btnEjecutarActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnEjecutar; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel panelReporte; private javax.swing.JTextField txtMoneda; private javax.swing.JTextField txtSucursal; // End of variables declaration//GEN-END:variables private Object crearImagen(List<Map<String, ?>> lista) { //datos del grfico String moneda = null; DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (Map<String, ?> map : lista) { double importe = Double.parseDouble(map.get("IMPORTE").toString()); moneda = map.get("MONENOMBRE").toString(); String accion = map.get("ACCION").toString(); dataset.addValue(importe, moneda, accion); } JFreeChart graficoBarras = ChartFactory.createBarChart("RESUMEN DE MOVIMIENTOS", //Ttulo de la grfica "TIPOS DE MOVIMIENTOS", //leyenda Eje horizontal "MILES DE " + moneda, //leyenda Eje vertical dataset, //datos PlotOrientation.VERTICAL, //orientacin true, //incluir leyendas true, //mostrar tooltips true); graficoBarras.setBackgroundPaint(Color.LIGHT_GRAY); CategoryPlot plot = (CategoryPlot) graficoBarras.getPlot(); plot.setBackgroundPaint(Color.CYAN); //fondo del grafico plot.setDomainGridlinesVisible(true);//lineas de rangos, visibles plot.setRangeGridlinePaint(Color.BLACK);//color de las lineas de rangos // Crear imagen BufferedImage imagen = graficoBarras.createBufferedImage(500, 300); return imagen; } }