Código
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 */ /** Creates new form VentanaGrafica */ public VentanaGrafica() { super("Ejemplos usando JFreeChart."); initComponents(); } // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { exitForm(evt); } }); pack(); } // </editor-fold> /** Exit the Application */ } // Variables declaration - do not modify // End of variables declaration try{ 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; } return image; } 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:
Código:
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)