Hola todos, he compuesto el siguiente codigo:
package ventana;
import java.awt.Frame;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import java.awt.image.*;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.jdbc.JDBCCategoryDataset;
import org.jfree.data.jdbc.JDBCXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import java.sql.*;
/**
*
* @author Administrador
*/
public class VentanaGrafica extends java.awt.Frame {
BufferedImage grafica = null;
static BufferedImage image;
/** Creates new form VentanaGrafica */
public VentanaGrafica() {
super("Ejemplos usando JFreeChart.");
initComponents();
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
pack();
}
// </editor-fold>
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(1);
}
// Variables declaration - do not modify
// End of variables declaration
public static BufferedImage creaImagen(Connection con) {
try{
String sql= "Select x as meses, y as ventas from sesiones";
JDBCXYDataset xyDataset = new JDBCXYDataset(con,sql);
JFreeChart chart = ChartFactory.createLineChart("Grafica de Ventas",
"Meses","Ventas %",(CategoryDataset) xyDataset,PlotOrientation.VERTICAL,
true,
true,
true // Show legend
);
//JFreeChart chart1 = ChartFactory.
image = chart.createBufferedImage(600,400);
return image;
}catch(SQLException e){
System.err.println("Error al obtener datos de la BDD: "+e);
}
return image;
}
public void paint(java.awt.Graphics g) {
if(grafica == null) {
grafica = this.creaImagen(Conexion.con);
}
g.drawImage(grafica,40,80,null);
}
}
Pero al ejecutarlo, me salta un error, y no me crea la grafica, asi que os pongo el error que tira:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.jfree.data.jdbc.JDBCXYDataset cannot be cast to org.jfree.data.category.CategoryDataset
at ventana.VentanaGrafica.creaImagen(VentanaGrafica.java:65)
at ventana.VentanaGrafica.paint(VentanaGrafica.java:82)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.X11.XRepaintArea.paintComponent(XRepaintArea.java:56)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.X11.XComponentPeer.handleEvent(XComponentPeer.java:688)
at java.awt.Component.dispatchEventImpl(Component.java:4706)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Java Result: 1
GENERACIÓN CORRECTA (total time: 29 seconds)
el problema esta el el casting pero como manejo esto no digo mas.
JFreeChart chart = ChartFactory.createLineChart("Grafica de Ventas",
"Meses","Ventas %",(CategoryDataset) xyDataset,PlotOrientation.VERTICAL,
Saludos.
Problema solventado.
Era que no habia includio la libreria en el classpath de ejecucion xDD
Saludos