/*
 * 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 Calculos_automaticos;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author chuen
 */
public class Graficos {
    

    
    
    public void generargraficocolumna(JTable tbl, JPanel jpanel1, String titulo, String ejeY ){
try{
    
  
    
    
String grupo_artef = (tbl.getColumnName(tbl.getSelectedColumn()));  


DefaultCategoryDataset ds = new DefaultCategoryDataset();

    
    for (int i = 0; i < tbl.getRowCount()-1; i++) {
         int a = Integer.parseInt(tbl.getValueAt(i, tbl.getSelectedColumn()).toString());
         String MP = tbl.getValueAt(i, 0).toString();
         ds.addValue(a, MP, "");
    }



 

    JFreeChart jf =ChartFactory.createBarChart3D(titulo, ejeY, "Cantidad", ds, PlotOrientation.HORIZONTAL, true, true, true);

    
    CategoryPlot barchrt = jf.getCategoryPlot();
    barchrt.setRangeGridlinePaint(Color.ORANGE);
    ChartPanel barpanel = new ChartPanel(jf);
    jpanel1.removeAll();
    jpanel1.add(barpanel, BorderLayout.CENTER);
    jpanel1.validate();
    

}catch (Exception ex){
    JOptionPane.showMessageDialog(null, "No se puede generar gráfico segun col");
}
    
}
    
    
    public void generargraficocolumnaFLOAT(JTable tbl, JPanel jpanel1, String titulo, String ejeY ){
try{
    
  
    
    
String grupo_artef = (tbl.getColumnName(tbl.getSelectedColumn()));  


DefaultCategoryDataset ds = new DefaultCategoryDataset();

    
    for (int i = 0; i < tbl.getRowCount()-1; i++) {
         float a = Float.parseFloat(tbl.getValueAt(i, tbl.getSelectedColumn()).toString());
         String MP = tbl.getValueAt(i, 0).toString();
         ds.addValue(a, MP, "");
    }



 

    JFreeChart jf =ChartFactory.createBarChart3D(titulo, ejeY, "Cantidad", ds, PlotOrientation.HORIZONTAL, true, true, true);

    
    CategoryPlot barchrt = jf.getCategoryPlot();
    barchrt.setRangeGridlinePaint(Color.ORANGE);
    ChartPanel barpanel = new ChartPanel(jf);
    jpanel1.removeAll();
    jpanel1.add(barpanel, BorderLayout.CENTER);
    jpanel1.validate();
    

}catch (Exception ex){
    JOptionPane.showMessageDialog(null, "No se puede generar gráfico segun col");
}
    
}
    
    public void generargraficofila2(JTable tbl, JPanel jpanel, String titulo, String ejeY){
    

String MP = tbl.getValueAt(tbl.getSelectedRow(), 0).toString();


DefaultCategoryDataset ds = new DefaultCategoryDataset();
try{
    for (int i = 1; i < tbl.getColumnCount()-1; i++) {
        int a = Integer.parseInt(tbl.getValueAt(tbl.getSelectedRow(), i).toString());
        ds.addValue(a, tbl.getColumnName(i), "");
    }
    
    JFreeChart jf =ChartFactory.createBarChart3D(titulo, ejeY, "Cantidad", ds, PlotOrientation.HORIZONTAL, true, true, true);

    
    CategoryPlot barchrt = jf.getCategoryPlot();
    barchrt.setRangeGridlinePaint(Color.ORANGE);
    ChartPanel barpanel = new ChartPanel(jf);
    jpanel.removeAll();
    jpanel.add(barpanel, BorderLayout.CENTER);
    jpanel.validate();
    

}catch (Exception ex){
    JOptionPane.showMessageDialog(null, "No se puede generar gráfico por fila");
}
    
}
    public void generargraficofila2FLOAT(JTable tbl, JPanel jpanel, String titulo, String ejeY){
    

String MP = tbl.getValueAt(tbl.getSelectedRow(), 0).toString();


DefaultCategoryDataset ds = new DefaultCategoryDataset();
try{
    for (int i = 1; i < tbl.getColumnCount()-1; i++) {
        float a = Float.parseFloat(tbl.getValueAt(tbl.getSelectedRow(), i).toString());
        ds.addValue(a, tbl.getColumnName(i), "");
    }
    
    JFreeChart jf =ChartFactory.createBarChart3D(titulo, ejeY, "Cantidad", ds, PlotOrientation.HORIZONTAL, true, true, true);

    
    CategoryPlot barchrt = jf.getCategoryPlot();
    barchrt.setRangeGridlinePaint(Color.ORANGE);
    ChartPanel barpanel = new ChartPanel(jf);
    jpanel.removeAll();
    jpanel.add(barpanel, BorderLayout.CENTER);
    jpanel.validate();
    

}catch (Exception ex){
    JOptionPane.showMessageDialog(null, "No se puede generar gráfico por fila");
}
    
}
}


