Preview only show first 10 pages with watermark. For full document please download

Grafico De Linha, Barra Circular Java

grafico de linha, barra, circular em java

   EMBED

  • Rating

  • Date

    December 2018
  • Size

    14KB
  • Views

    3,022
  • Categories


Share

Transcript

/*,,, * 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 grafico;,,, ,,, import java.util.Locale;,,, import javax.swing.JOptionPane;,,, /*****************/,,, import org.jfree.chart.*; // contem todos os metodos para os tres,,, /*****************/,,, import org.jfree.chart.renderer.xy.XYSplineRenderer;,,, import org.jfree.chart.plot.XYPlot; // linha,,, import org.jfree.chart.axis.*;,,, import org.jfree.data.xy.*;,,, ,,, import org.jfree.chart.plot.CategoryPlot;,,, import org.jfree.chart.plot.PlotOrientation;,,, import org.jfree.data.category.DefaultCategoryDataset; // barra,,, ,,, import org.jfree.data.general.DefaultPieDataset; // circular,,, ,,, public class Grafico extends javax.swing.JFrame {,,, ,,, /**,,, * Creates new form Grafico,,, */,,, public Grafico() {,,, initComponents();,,, this.setLocationRelativeTo(null);,,, ,,, this.barra.setVisible(false);,,, this.linha.setVisible(false);,,, this.circular.setVisible(false);,,, ,,, this.setVisible(true);,,, this.setSize(1500,900);,, },,, ,,, " @SuppressWarnings(""unchecked"")",,, " // //GEN-BEGIN:initComponents",,, private void initComponents() {,,, ,,, buttonGroup1 = new javax.swing.ButtonGroup();,,, jLabel1 = new javax.swing.JLabel();,,, l = new javax.swing.JRadioButton();,,, b = new javax.swing.JRadioButton();,,, c = new javax.swing.JRadioButton();,,, jScrollPane1 = new javax.swing.JScrollPane();,,, dados = new javax.swing.JTable();,,, jButton1 = new javax.swing.JButton();,,, capas = new javax.swing.JLayeredPane();,,, linha = new javax.swing.JPanel();,,, barra = new javax.swing.JPanel();,,, circular = new javax.swing.JPanel();,,, ,,, setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);,,, getContentPane().setLayout(null);,,, ,,, " jLabel1.setText(""Tipos de Grafico"");",,, getContentPane().add(jLabel1);,,, jLabel1.setBounds(10,11,97, 32); ,,, buttonGroup1.add(l);,,, " l.setText(""Linha"");",,, l.addActionListener(new java.awt.event.ActionListener() {,,, public void actionPerformed(java.awt.event.ActionEvent evt) {,,, lActionPerformed(evt);,,, },,, });,,, getContentPane().add(l);,,, l.setBounds(10,45,51, 23); ,,, buttonGroup1.add(b);,,, " b.setText(""Barra"");",,, b.addActionListener(new java.awt.event.ActionListener() {,,, public void actionPerformed(java.awt.event.ActionEvent evt) {,,, bActionPerformed(evt);,,, },,, });,,, getContentPane().add(b);,,, b.setBounds(63,45,51, 23); ,,, buttonGroup1.add(c);,,, " c.setText(""Circular"");",,, c.addActionListener(new java.awt.event.ActionListener() {,,, public void actionPerformed(java.awt.event.ActionEvent evt) {,,, cActionPerformed(evt);,,, },,, });,,, getContentPane().add(c);,,, c.setBounds(116,45,61, 23); ,,, dados.setModel(new javax.swing.table.DefaultTableModel(,,, new Object [][] {,,, {null, null},, {null, null},, {null, null},, {null, null},, {null, null},, {null, null},, },,, new String [] {,,, " ""X, ""Y",,, },,, ));,,, jScrollPane1.setViewportView(dados);,,, ,,, getContentPane().add(jScrollPane1);,,, jScrollPane1.setBounds(10,70,362, 90); ,,, " jButton1.setText(""Criar Graficos"");",,, jButton1.addActionListener(new java.awt.event.ActionListener() {,,, public void actionPerformed(java.awt.event.ActionEvent evt) {,,, jButton1ActionPerformed(evt);,,, },,, });,,, getContentPane().add(jButton1);,,, jButton1.setBounds(10,246,176, 84); ,,, capas.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));,,, ,,, javax.swing.GroupLayout linhaLayout = new javax.swing.GroupLayout(linha);,,, linha.setLayout(linhaLayout);,,, linhaLayout.setHorizontalGroup(,,, linhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addGap(0,646, Short.MAX_VALUE), );,,, linhaLayout.setVerticalGroup(,,, linhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addGap(0,556, Short.MAX_VALUE), );,,, ,,, javax.swing.GroupLayout barraLayout = new javax.swing.GroupLayout(barra);,,, barra.setLayout(barraLayout);,,, barraLayout.setHorizontalGroup(,,, barraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addGap(0,646, Short.MAX_VALUE), );,,, barraLayout.setVerticalGroup(,,, barraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addGap(0,556, Short.MAX_VALUE), );,,, ,,, javax.swing.GroupLayout circularLayout = new javax.swing.GroupLayout(circular);,,, circular.setLayout(circularLayout);,,, circularLayout.setHorizontalGroup(,,, circularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addGap(0,646, Short.MAX_VALUE), );,,, circularLayout.setVerticalGroup(,,, circularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addGap(0,556, Short.MAX_VALUE), );,,, ,,, javax.swing.GroupLayout capasLayout = new javax.swing.GroupLayout(capas);,,, capas.setLayout(capasLayout);,,, capasLayout.setHorizontalGroup(,,, capasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addComponent(linha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(capasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addComponent(barra, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(capasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addComponent(circular, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) );,,, capasLayout.setVerticalGroup(,,, capasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addComponent(linha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(capasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addComponent(barra, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(capasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING),,, .addComponent(circular, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) );,,, capas.setLayer(linha, javax.swing.JLayeredPane.DEFAULT_LAYER);,, capas.setLayer(barra, javax.swing.JLayeredPane.DEFAULT_LAYER);,, capas.setLayer(circular, javax.swing.JLayeredPane.DEFAULT_LAYER);,, ,,, getContentPane().add(capas);,,, capas.setBounds(370,0,650, 560); ,,, pack();,,, }// //GEN-END:initComponents,,, ,,, private void lActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lActionPerformed,,, linha.setVisible(true);,,, capas.setLayer(linha,0,0); // posicao na referia janel , }//GEN-LAST:event_lActionPerformed,,, ,,, private void bActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bActionPerformed,,, barra.setVisible(true);,,, capas.setLayer(barra,0,0);, }//GEN-LAST:event_bActionPerformed,,, ,,, private void cActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cActionPerformed,,, circular.setVisible(true);,,, capas.setLayer(circular,0,0);, }//GEN-LAST:event_cActionPerformed,,, ,,, private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed,,, ChartPanel panel;,,, JFreeChart chart= null;,,, ,,, ,,, if(l.isSelected()){,,, // grafica linha,,, int validar=1;,,, XYSplineRenderer renderer= new XYSplineRenderer();,,, XYSeriesCollection datasert= new XYSeriesCollection();,,, ,,, ValueAxis x= new NumberAxis();,,, ValueAxis y=new NumberAxis();,,, ,,, " XYSeries serie= new XYSeries(""Datos"");",,, XYPlot plot;,,, linha.removeAll();,,, ,,, try{ ,,, for(int i=0; i<6; i++){,,, serie.add(Float.parseFloat(String.valueOf(dados.getValueAt(i, 0))),, Float.parseFloat(String.valueOf(dados.getValueAt(i, 1))));,, },,, }catch(Exception ja){,,, JOptionPane.showMessageDialog(this,"ERRO EM TABELA""); validar=0; } if(validar==1){ datasert.addSeries(serie); // carega x.setLabel(""eixo X""); y.setLabel(""eixo Y""); plot=new XYPlot(datasert, x, y, renderer); chart=new JFreeChart(plot); chart.setTitle(""Grafico de linha""); } else{ JOptionPane.showMessageDialog(this,""ERRO""); } }else if(b.isSelected()){ // grafica barra DefaultCategoryDataset data=new DefaultCategoryDataset(); String produto1=""sopas""; String produto2=""ARROZ""; String dia1=""DIA 1""; String dia2=""DIA 1""; String dia3=""DIA 1""; String dia4=""DIA 1""; data.addValue(18,produto1, dia1); data.addValue(15,produto1, dia2); data.addValue(14,produto1, dia3); data.addValue(1,produto1, dia4); data.addValue(50,produto2, dia1); data.addValue(45,produto2, dia2); data.addValue(31,produto2, dia3); data.addValue(10,produto2, dia4); chart= ChartFactory.createBarChart(""Grafico de Barras"," ""Dias,""Quantidade"" , data, PlotOrientation.VERTICAL,true, true, true); CategoryPlot plot= (CategoryPlot) chart.getPlot(); plot.setDomainGridlinesVisible(true); }else{ // grafica circular int d=Integer.parseInt(String.valueOf(dados.getModel().getValueAt(0, 0))); System.out.print(d); DefaultPieDataset data= new DefaultPieDataset(); data.setValue(""Categoria 1", 80); " data.setValue(""Categoria 2, 60); data.setValue(""Categoria 3", 20);,, ,,, " chart=ChartFactory.createPieChart3D(""Grafico Circular, data, true, true, true); } panel=new ChartPanel(chart); panel.setBounds(5,10,444,596 ); if(l.isSelected()){ linha.add(panel); linha.repaint(); }else if(b.isSelected()){ barra.add(panel); barra.repaint(); } else if(c.isSelected()){ circular.add(panel); circular.repaint(); } }//GEN-LAST:event_jButton1ActionPerformed public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* 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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Grafico().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JRadioButton b; private javax.swing.JPanel barra; private javax.swing.ButtonGroup buttonGroup1; private javax.swing.JRadioButton c; private javax.swing.JLayeredPane capas; private javax.swing.JPanel circular; private javax.swing.JTable dados; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JRadioButton l; private javax.swing.JPanel linha; // End of variables declaration//GEN-END:variables } ",,,