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 View; import Controller.Cuenta; import Controller.EdoRes; import Controller.Historico; import Controller.Indicador; import Controller.Indicadores; import Controller.TablaOp; import Controller.Utilidades; import java.awt.BasicStroke; import java.awt.Color; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.Stroke; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.StringTokenizer; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.DefaultComboBoxModel; import javax.swing.JFrame; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.*; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.data.category.*; import org.jfree.data.general.*; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.jfree.ui.RefineryUtilities; /** * * @author toshiba */ public class DialogoEstadisticas extends javax.swing.JDialog { Indicadores i; Utilidades uts; JFreeChart chart; Historico h; private boolean utilidad; private List<Cuenta> cuentas; private ChartPanel chartPanel; private boolean clientes; private boolean ventas; private boolean costos; private boolean gastosAdmin; private boolean almacen; private boolean deudoresDiv; private boolean gastosOp; private final List<Indicador> indicadores; private boolean intervalo; private String fechaIntervaloI; private String fechaIntervaloF; private boolean listaInd; private boolean listaHis; /** * Creates new form DialogoEstadisticas */ public DialogoEstadisticas(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); i = new Indicadores(); uts = new Utilidades(); h = new Historico(); cuentas = h.obtenerHistorico(); indicadores = h.obtenerHistoricoInd(); llenarCmbPeriodos(); cmbPeriodoF.setEnabled(false); cmbPeriodoI.setEnabled(false); intervalo = false; lbPeriodoF.setText(""); lbPeriodoI.setText(""); listaIndicadores.setSelectedIndex(0); } private void llenarCmbPeriodos() { h = new Historico(); h.buscaPeriodos(); /*String[] periodos = new String[h.getNumeroPeriodos()]; int i = 0; for (String p : h.getPeriodos()) { periodos[i] = formatPeriodo(p); i++; } cmbPeriodoF.setModel(new DefaultComboBoxModel((Object[]) periodos)); cmbPeriodoF.setSelectedIndex(h.getNumeroPeriodos() - 1); cmbPeriodoI.setModel(new DefaultComboBoxModel((Object[]) periodos)); cmbPeriodoF.setSelectedIndex(h.getNumeroPeriodos() - 1); */ String[] periodos = h.getPeriodos(); cmbPeriodoI.setModel(new DefaultComboBoxModel((Object[]) periodos)); cmbPeriodoI.setSelectedIndex(0); cmbPeriodoF.setModel(new DefaultComboBoxModel((Object[]) periodos)); cmbPeriodoF.setSelectedIndex(h.getNumeroPeriodos() - 1); fechaIntervaloI = periodos[0]; fechaIntervaloF = periodos[h.getNumeroPeriodos() - 1]; } /** * 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() { panelG = new javax.swing.JPanel(); jButton8 = new javax.swing.JButton(); jScrollPane3 = new javax.swing.JScrollPane(); listaHistorico = new javax.swing.JList<>(); jScrollPane2 = new javax.swing.JScrollPane(); listaIndicadores = new javax.swing.JList<>(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); cmbPeriodoI = new javax.swing.JComboBox<>(); cmbPeriodoF = new javax.swing.JComboBox<>(); rdIntervalo = new javax.swing.JRadioButton(); lbPeriodoI = new javax.swing.JLabel(); lbPeriodoF = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); javax.swing.GroupLayout panelGLayout = new javax.swing.GroupLayout(panelG); panelG.setLayout(panelGLayout); panelGLayout.setHorizontalGroup(panelGLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 723, Short.MAX_VALUE)); panelGLayout.setVerticalGroup(panelGLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 464, Short.MAX_VALUE)); jButton8.setText("Guardar"); jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton8ActionPerformed(evt); } }); listaHistorico.setModel(new javax.swing.AbstractListModel<String>() { String[] strings = { "Ventas", "Clientes", "Gastos de Admin", "Costo Venta", "Almacen", "Efectivo", "Utilidad", "Gastos de Op", "Deudores Div", "Pagos", "Utilidad N", "Utilidad B" }; public int getSize() { return strings.length; } public String getElementAt(int i) { return strings[i]; } }); listaHistorico.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { listaHistoricoValueChanged(evt); } }); jScrollPane3.setViewportView(listaHistorico); listaIndicadores.setModel(new javax.swing.AbstractListModel<String>() { String[] strings = { "Activo", "Efectivo", "Margen" }; public int getSize() { return strings.length; } public String getElementAt(int i) { return strings[i]; } }); listaIndicadores.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); listaIndicadores.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { listaIndicadoresValueChanged(evt); } }); jScrollPane2.setViewportView(listaIndicadores); jLabel1.setText("Indicadores:"); jLabel2.setText("Histrico de cuentas:"); cmbPeriodoI.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "periodo inicial" })); cmbPeriodoI.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cmbPeriodoIItemStateChanged(evt); } }); cmbPeriodoI.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { cmbPeriodoIMouseEntered(evt); } }); cmbPeriodoI.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmbPeriodoIActionPerformed(evt); } }); cmbPeriodoF.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "periodo final" })); cmbPeriodoF.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cmbPeriodoFItemStateChanged(evt); } }); cmbPeriodoF.addPopupMenuListener(new javax.swing.event.PopupMenuListener() { public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) { cmbPeriodoFPopupMenuWillBecomeVisible(evt); } public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) { } public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) { } }); cmbPeriodoF.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmbPeriodoFActionPerformed(evt); } }); rdIntervalo.setText("Intervalo:"); rdIntervalo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { rdIntervaloActionPerformed(evt); } }); lbPeriodoI.setText("jLabel3"); lbPeriodoF.setText("jLabel3"); jLabel3.setText("De:"); jLabel4.setText("a"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(18, 18, 18).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(jLabel1) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane3).addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(panelG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup().addComponent(rdIntervalo) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(cmbPeriodoI, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(lbPeriodoI) .addGap(104, 104, 104))) .addGap(29, 29, 29).addComponent(jLabel4).addGap(18, 18, 18) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(cmbPeriodoF, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(84, 84, 84).addComponent(jButton8)) .addGroup(layout.createSequentialGroup().addGap(35, 35, 35) .addComponent(lbPeriodoF))))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addGap(18, 18, 18).addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent( jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addComponent( panelG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cmbPeriodoI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(rdIntervalo) .addComponent(cmbPeriodoF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3).addComponent(jLabel4)) .addComponent(jButton8)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lbPeriodoI).addComponent(lbPeriodoF)))) .addGap(19, 19, 19))); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed try { ChartUtilities.saveChartAsJPEG(new File("grafico.jpg"), chart, 700, 700); } catch (IOException ex) { Logger.getLogger(DialogoEstadisticas.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton8ActionPerformed private void listaHistoricoValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listaHistoricoValueChanged if (chartPanel != null) { panelG.remove(chartPanel); } listaInd = false; listaHis = true; creaGraficaHistorico(listaHistorico.getSelectedIndices()); listaIndicadores.removeSelectionInterval(0, 2); }//GEN-LAST:event_listaHistoricoValueChanged private void listaIndicadoresValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listaIndicadoresValueChanged if (chartPanel != null) { panelG.remove(chartPanel); } listaInd = true; listaHis = false; creaGraficaIndicadores(listaIndicadores.getSelectedIndex()); listaHistorico.removeSelectionInterval(0, 7); }//GEN-LAST:event_listaIndicadoresValueChanged private void rdIntervaloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdIntervaloActionPerformed if (rdIntervalo.isSelected()) { cmbPeriodoF.setEnabled(true); cmbPeriodoI.setEnabled(true); lbPeriodoF.setText(formatPeriodo(cmbPeriodoF.getSelectedItem().toString())); lbPeriodoI.setText(formatPeriodo(cmbPeriodoI.getSelectedItem().toString())); fechaIntervaloF = cmbPeriodoF.getSelectedItem().toString(); fechaIntervaloI = cmbPeriodoI.getSelectedItem().toString(); intervalo = true; } else { cmbPeriodoF.setEnabled(false); cmbPeriodoI.setEnabled(false); lbPeriodoF.setText(""); lbPeriodoI.setText(""); intervalo = false; } }//GEN-LAST:event_rdIntervaloActionPerformed private void cmbPeriodoIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbPeriodoIActionPerformed if (intervalo) { if (validarIntervalo(cmbPeriodoI.getSelectedItem().toString(), "i")) { lbPeriodoI.setText(formatPeriodo(cmbPeriodoI.getSelectedItem().toString())); lbPeriodoF.setText(formatPeriodo(cmbPeriodoF.getSelectedItem().toString())); if (chartPanel != null) { panelG.remove(chartPanel); } intervalo = true; fechaIntervaloI = cmbPeriodoI.getSelectedItem().toString(); if (listaInd) { creaGraficaIndicadores(listaIndicadores.getSelectedIndex()); } else if (listaHis) { creaGraficaHistorico(listaHistorico.getSelectedIndices()); } } else { uts.ventanaInfo(new JFrame(), "intervalo no vlido", "Aviso"); cmbPeriodoI.setSelectedItem(fechaIntervaloI); } } }//GEN-LAST:event_cmbPeriodoIActionPerformed private void cmbPeriodoFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbPeriodoFActionPerformed if (intervalo) { if (validarIntervalo(cmbPeriodoF.getSelectedItem().toString(), "f")) { lbPeriodoF.setText(formatPeriodo(cmbPeriodoF.getSelectedItem().toString())); lbPeriodoI.setText(formatPeriodo(cmbPeriodoI.getSelectedItem().toString())); if (chartPanel != null) { panelG.remove(chartPanel); } fechaIntervaloF = cmbPeriodoF.getSelectedItem().toString(); intervalo = true; creaGraficaIndicadores(listaIndicadores.getSelectedIndex()); } else { uts.ventanaInfo(new JFrame(), "intervalo no vlido", "Aviso"); } } }//GEN-LAST:event_cmbPeriodoFActionPerformed private void cmbPeriodoIItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbPeriodoIItemStateChanged if (intervalo) { } }//GEN-LAST:event_cmbPeriodoIItemStateChanged private void cmbPeriodoFItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbPeriodoFItemStateChanged if (intervalo) { } }//GEN-LAST:event_cmbPeriodoFItemStateChanged private void cmbPeriodoIMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmbPeriodoIMouseEntered // TODO add your handling code here: }//GEN-LAST:event_cmbPeriodoIMouseEntered private void cmbPeriodoFPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cmbPeriodoFPopupMenuWillBecomeVisible }//GEN-LAST:event_cmbPeriodoFPopupMenuWillBecomeVisible /** * @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(DialogoEstadisticas.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(DialogoEstadisticas.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(DialogoEstadisticas.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(DialogoEstadisticas.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the dialog */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { DialogoEstadisticas dialog = new DialogoEstadisticas(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox<String> cmbPeriodoF; private javax.swing.JComboBox<String> cmbPeriodoI; private javax.swing.JButton jButton8; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JLabel lbPeriodoF; private javax.swing.JLabel lbPeriodoI; private javax.swing.JList<String> listaHistorico; private javax.swing.JList<String> listaIndicadores; private javax.swing.JPanel panelG; private javax.swing.JRadioButton rdIntervalo; // End of variables declaration//GEN-END:variables private void creaGraficaHistorico(int[] selectedIndices) { // Fuente de Datos DefaultCategoryDataset line_chart_dataset = new DefaultCategoryDataset(); String[] periodos; EdoRes e; for (int j = 0; j < selectedIndices.length; j++) { switch (selectedIndices[j]) { case 0://ventas for (Cuenta c : cuentas) { if (c.getId().equals("IN000")) { if (combrobarIntervalo(c.getPeriodo())) { line_chart_dataset.addValue(c.getSaldo(), c.getNombre(), formatPeriodo(c.getPeriodo())); } } } break; case 1: //clientes for (Cuenta c : cuentas) { if (c.getId().equals("AC003")) { if (combrobarIntervalo(c.getPeriodo())) { line_chart_dataset.addValue(c.getSaldo(), c.getNombre(), formatPeriodo(c.getPeriodo())); } } } break; case 2: //gastosadmin for (Cuenta c : cuentas) { if (c.getId().equals("GA001")) { if (combrobarIntervalo(c.getPeriodo())) { line_chart_dataset.addValue(c.getSaldo(), c.getNombre(), formatPeriodo(c.getPeriodo())); } } } break; case 3://costos for (Cuenta c : cuentas) { if (c.getId().equals("CO000")) { if (combrobarIntervalo(c.getPeriodo())) { line_chart_dataset.addValue(c.getSaldo(), c.getNombre(), formatPeriodo(c.getPeriodo())); } } } break; case 4://almacen for (Cuenta c : cuentas) { if (c.getId().equals("AC002")) { if (combrobarIntervalo(c.getPeriodo())) { line_chart_dataset.addValue(c.getSaldo(), c.getNombre(), formatPeriodo(c.getPeriodo())); } } } break; case 5://efectivo for (Indicador i : indicadores) { if (i.getNombre().equals("Efectivo")) { if (combrobarIntervalo(i.getPeriodo())) { line_chart_dataset.addValue(i.getValor(), i.getNombre(), formatPeriodo(i.getPeriodo())); } } } break; case 6://utilidad for (Cuenta c : cuentas) { if (c.getId().equals("CC001")) { if (combrobarIntervalo(c.getPeriodo())) { line_chart_dataset.addValue(c.getSaldo(), c.getNombre(), formatPeriodo(c.getPeriodo())); } } } break; case 7://gastosop for (Cuenta c : cuentas) { if (c.getId().equals("GA002")) { if (combrobarIntervalo(c.getPeriodo())) { line_chart_dataset.addValue(c.getSaldo(), c.getNombre(), formatPeriodo(c.getPeriodo())); } } } break; case 8://deudiv for (Cuenta c : cuentas) { if (c.getId().equals("AC004")) { if (combrobarIntervalo(c.getPeriodo())) { line_chart_dataset.addValue(c.getSaldo(), c.getNombre(), formatPeriodo(c.getPeriodo())); } } } break; case 9://pagos periodo for (Indicador i : indicadores) { if (i.getNombre().equals("Pagos del periodo")) { if (combrobarIntervalo(i.getPeriodo())) { line_chart_dataset.addValue(i.getValor(), i.getNombre(), formatPeriodo(i.getPeriodo())); } } } break; case 10://utilidadNeta periodo h.buscaPeriodos(); periodos = h.getPeriodos(); for (String p : periodos) { if (combrobarIntervalo(p)) { e = new EdoRes(p); line_chart_dataset.addValue(e.getUtN(), "Utilidad N", formatPeriodo(p)); } } break; case 11://utilidadBruta periodo h.buscaPeriodos(); periodos = h.getPeriodos(); for (String p : periodos) { if (combrobarIntervalo(p)) { e = new EdoRes(p); line_chart_dataset.addValue(e.getUtB(), "Utilidad B", formatPeriodo(p)); } } break; } } JFreeChart chart = ChartFactory.createLineChart("Estadsticas", "periodos", "monto ($)", line_chart_dataset, PlotOrientation.VERTICAL, true, true, false); if (chartPanel != null) { panelG.remove(chartPanel); } //grosor de linea de fx CategoryPlot p = chart.getCategoryPlot(); for (int j = 0; j < selectedIndices.length; j++) { p.getRenderer().setSeriesStroke(j, new BasicStroke(4)); } //color y grosor de grid chart.getCategoryPlot().setDomainGridlinesVisible(true); //grid visible p.setRangeGridlinePaint(Color.black); p.setDomainGridlinePaint(Color.black); p.setRangeGridlineStroke(new BasicStroke(1)); p.setDomainGridlineStroke(new BasicStroke(1)); //titulos de eje y en vertical CategoryAxis domainAxis = ((CategoryPlot) p).getDomainAxis(); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90); chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(723, 456)); chartPanel.setVisible(true); chartPanel.setSize(723, 456); panelG.add(chartPanel); panelG.revalidate(); panelG.repaint(); } private void creaGraficaIndicadores(int selectedIndex) { EdoRes e; DefaultCategoryDataset dataset; CategoryPlot p; switch (selectedIndex) { case 0: if (rdIntervalo.isSelected()) { rdIntervalo.doClick(); } DefaultPieDataset data = new DefaultPieDataset(); data.setValue("Efectivo: " + uts.formatPorcentaje(i.getPorLiq()) + " (" + uts.formatDinero(i.getEfectivo()) + ")", i.getPorLiq()); data.setValue("Clientes: " + uts.formatPorcentaje(i.getPorDebClientes()) + " (" + uts.formatDinero(i.getSaldoClientes()) + ")", i.getPorDebClientes()); data.setValue("Deudores Div: " + uts.formatPorcentaje(i.getPorDebDiv()) + " (" + uts.formatDinero(i.getSaldoDeuDiv()) + ")", i.getPorDebDiv()); data.setValue("Inversion: " + uts.formatPorcentaje(i.getPorInv()) + " (" + uts.formatDinero(i.getInversion()) + ")", i.getPorInv()); // Creando el Grafico chart = ChartFactory.createPieChart("Distribucin de Activo", data, true, true, false); chart.setBackgroundPaint(Color.lightGray); break; case 1: //Efectivo // Fuente de Datos dataset = new DefaultCategoryDataset(); for (Indicador ind : indicadores) { if (ind.getNombre().equals("Efectivo")) { if (combrobarIntervalo(ind.getPeriodo())) { dataset.setValue(ind.getValor(), "Efectivo", formatPeriodo(ind.getPeriodo())); } } } // Creando el Grafico chart = ChartFactory.createBarChart3D("Flujo de Efectivo", "Efectivo", "Periodos", dataset, PlotOrientation.VERTICAL, true, true, false); chart.setBackgroundPaint(Color.lightGray); chart.getTitle().setPaint(Color.black); p = chart.getCategoryPlot(); p.setRangeGridlinePaint(Color.blue); //titulos de eje y en vertical p = chart.getCategoryPlot(); CategoryAxis domainAxis = ((CategoryPlot) p).getDomainAxis(); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90); break; case 2: //Margen // Fuente de Datos dataset = new DefaultCategoryDataset(); h.buscaPeriodos(); String[] periodos = h.getPeriodos(); for (String per : periodos) { if (combrobarIntervalo(per)) { System.out.println("------------------SI"); e = new EdoRes(per); dataset.setValue(e.getMargen(), formatPeriodo(per) + ": %" + uts.format2(e.getMargen()), formatPeriodo(per)); } else { System.out.println("-" + per + "-----NO"); } } // Creando el Grafico chart = ChartFactory.createBarChart3D("Margen de ganancia", "margen (%)", "periodos", dataset, PlotOrientation.VERTICAL, true, true, false); chart.setBackgroundPaint(Color.lightGray); chart.getTitle().setPaint(Color.black); p = chart.getCategoryPlot(); p.setRangeGridlinePaint(Color.blue); //titulos de eje y en vertical p = chart.getCategoryPlot(); CategoryAxis domainAxis2 = ((CategoryPlot) p).getDomainAxis(); domainAxis2.setCategoryLabelPositions(CategoryLabelPositions.UP_90); break; } chartPanel = new ChartPanel(chart); chartPanel.setVisible(true); chartPanel.setSize(723, 456); panelG.add(chartPanel); panelG.revalidate(); panelG.repaint(); } private String formatPeriodo(String periodo) { StringTokenizer st = new StringTokenizer(periodo, "/"); String mesI = ""; String diaI = ""; String anioI = ""; String diaF = ""; String mesF = ""; String anioF = ""; String fechaI = null; String fechaF = null; String mesLetraI; String mesLetraF; while (st.hasMoreTokens()) { fechaI = st.nextToken(); fechaF = st.nextToken(); } if (fechaI != null) { st = new StringTokenizer(fechaI, "-"); while (st.hasMoreTokens()) { diaI = st.nextToken(); mesI = st.nextToken(); anioI = st.nextToken(); } } if (fechaF != null) { st = new StringTokenizer(fechaF, "-"); while (st.hasMoreTokens()) { diaF = st.nextToken(); mesF = st.nextToken(); anioF = st.nextToken(); } } mesLetraI = obtenerMesLetra(mesI); mesLetraF = obtenerMesLetra(mesF); return mesLetraI + "-" + mesLetraF + "-" + anioF.substring(2); } private String obtenerMesLetra(String mes) { switch (mes) { case "01": return "ENE"; case "02": return "FEB"; case "03": return "MAR"; case "04": return "ABR"; case "05": return "MAY"; case "06": return "JUN"; case "07": return "JUL"; case "08": return "AGO"; case "09": return "SEP"; case "10": return "OCT"; case "11": return "NOV"; case "12": return "DIC"; } return "No valido"; } private boolean combrobarIntervalo(String periodo) { if (intervalo) { String fechaI = null; String fechaF = null; String periodoI = null; String periodoF = null; SimpleDateFormat dt1 = new SimpleDateFormat("dd-MM-yyyy"); StringTokenizer st = new StringTokenizer(periodo, "/"); while (st.hasMoreTokens()) { fechaI = st.nextToken(); fechaF = st.nextToken(); } st = new StringTokenizer(fechaIntervaloI, "/"); periodoI = st.nextToken(); st = new StringTokenizer(fechaIntervaloF, "/"); while (st.hasMoreTokens()) { st.nextToken(); periodoF = st.nextToken(); } try { Date dI = dt1.parse(fechaI); Date dF = dt1.parse(fechaF); Date dIntevaloI = dt1.parse(periodoI); Date dIntevaloF = dt1.parse(periodoF); return (dI.after(dIntevaloI) && dF.before(dIntevaloF)) || (dI.compareTo(dIntevaloI) == 0) || (dF.compareTo(dIntevaloF) == 0); } catch (ParseException ex) { Logger.getLogger(DialogoEstadisticas.class.getName()).log(Level.SEVERE, null, ex); return false; } } else { return true; } } private boolean validarIntervalo(String periodo, String opc) { String fechaFinI = null; String fechaFinF = null; String fechaInI = null; String fechaInF = null; SimpleDateFormat dt1 = new SimpleDateFormat("dd-MM-yyyy"); StringTokenizer st = new StringTokenizer(periodo, "/"); while (st.hasMoreTokens()) { fechaFinI = st.nextToken(); fechaFinF = st.nextToken(); } switch (opc) { case "f": st = new StringTokenizer(fechaIntervaloI, "/"); break; case "i": st = new StringTokenizer(fechaIntervaloF, "/"); break; } fechaInI = st.nextToken(); fechaInF = st.nextToken(); try { Date dI = dt1.parse(fechaInF); Date dF = dt1.parse(fechaFinF); switch (opc) { case "f": System.out.println("Fecha intervalo Inicial: " + fechaInF); System.out.println("Fecha que me dan final: " + fechaFinF); System.out.println("Resultado: " + dF.after(dI)); if (!dF.after(dI)) { cmbPeriodoF.setSelectedItem(fechaIntervaloF); } return (dF.after(dI)); case "i": System.out.println("Fecha intervalo Fin: " + fechaInF); System.out.println("Fecha que me dan inicial: " + fechaFinF); System.out.println("Resultado: " + dF.before(dI)); if (!dF.before(dI)) { cmbPeriodoI.setSelectedItem(fechaIntervaloI); } return (dF.before(dI)); } return false; } catch (ParseException ex) { Logger.getLogger(DialogoEstadisticas.class.getName()).log(Level.SEVERE, null, ex); return false; } } }