bonjour,
voila depuis un bon moment que je cherche à résoudre ce problème :(
je veux ,à partir d'une page web, obtenir une courbe avec Jfreechart
est ce que je dois écrire mon code (pour le graph) dans la JSP ou Servlet
:?
RQ:
le graphe sera dans un fenetre indépendante (non une page web) dès que je fais une selection dans ma page web :?
voici mon code
import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.AxisLocation; import org.jfree.chart.axis.NumberAxis; //import org.jfree.chart.labels.ItemLabelPosition; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.LineAndShapeRenderer; //import org.jfree.chart.renderer.xy.StandardXYItemRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RectangleInsets; import org.jfree.ui.RefineryUtilities; //import java.awt.BasicStroke; import java.awt.Color; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; //import java.text.DecimalFormat; public class LineChartTests extends ApplicationFrame { /** * */ private static final long serialVersionUID = 1L; public LineChartTests(String title) { super(title); CategoryDataset dataset = createDataset(); CategoryDataset dataset1 = createDataset1(); CategoryDataset dataset2 = createDataset2(); JFreeChart chart = createChart(dataset,dataset1,dataset2); // add the chart to a panel... ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(chartPanel); } private String url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/pfe.mdb"; private String user = "user"; private String pass = "pass"; private String []temps = new String [100]; private float[] Traf= new float [100]; private float[] SRVLINES= new float [100]; private float[] CULC= new float [100]; private int y=0; private CategoryDataset createDataset() { // row keys... String series1 = "TGRP:TRAFCARR"; // String series2 = "TGRP:SRVLINES"; // String series3 = "TGRP:CULC"; // --connection à la base donnée try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; } catch(ClassNotFoundException e) { System.err.println("Erreur de chargement du driver : + e") ; } try { Connection connect = DriverManager.getConnection(url,user,pass) ; Statement statement = connect.createStatement() ; String Str="2007/05/26"; String Str01="2007/05/21"; String Str02="MSC01:TCLIP1"; String query = "select \"TGRP:TRAFCARR\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') "; //String query = "select \"TGRP:TRAFCARR\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') "; ResultSet resultset = statement.executeQuery(query); while (resultset.next()!= false){ String Str1 = resultset.getString(1); String Str2 = resultset.getString(2); if (Str1.indexOf("-")==-1){ Traf[y]= Float.parseFloat(Str1); } temps[y]=Str2; y++; } System.out.println(y); connect.close();// fermuture de la connection à la BD } catch(SQLException sqle) { System.err.println("Erreur lors de la connexion : " + sqle) ; } DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i=0;i<y;i++){ dataset.addValue(Traf[i], series1, temps[i]); // dataset.addValue(SRVLINES[i], series2, temps[i]); // dataset.addValue(CULC[i], series3, temps[i]); } return dataset; } private CategoryDataset createDataset1() { // row keys... //String series1 = "TGRP:TRAFCARR"; String series2 = "TGRP:SRVLINES"; // String series3 = "TGRP:CULC"; // --connection à la base donnée try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; } catch(ClassNotFoundException e) { System.err.println("Erreur de chargement du driver : + e") ; } try { Connection connect = DriverManager.getConnection(url,user,pass) ; Statement statement = connect.createStatement() ; String Str="2007/05/26"; String Str01="2007/05/21"; String Str02="MSC01:TCLIP1"; y=0; String query = "select \"TGRP:SRVLINES\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') "; //String query = "select \"TGRP:TRAFCARR\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') "; ResultSet resultset = statement.executeQuery(query); while (resultset.next()!= false){ String Str1 = resultset.getString(1); String Str2 = resultset.getString(2); if (Str1.indexOf("-")==-1){ SRVLINES[y]= Float.parseFloat(Str1); } // temps[y]=Str2; y++; } System.out.println(y); connect.close();// fermuture de la connection à la BD } catch(SQLException sqle) { System.err.println("Erreur lors de la connexion : " + sqle) ; } DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i=0;i<y;i++){ // dataset.addValue(Traf[i], series1, temps[i]); dataset.addValue(SRVLINES[i], series2, temps[i]); // dataset.addValue(CULC[i], series3, temps[i]); } return dataset; } private CategoryDataset createDataset2() { // row keys... //String series1 = "TGRP:TRAFCARR"; //String series2 = "TGRP:SRVLINES"; String series3 = "TGRP:CULC"; // --connection à la base donnée try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; } catch(ClassNotFoundException e) { System.err.println("Erreur de chargement du driver : + e") ; } try { Connection connect = DriverManager.getConnection(url,user,pass) ; Statement statement = connect.createStatement() ; String Str="2007/05/26"; String Str01="2007/05/21"; String Str02="MSC01:TCLIP1"; y=0; String query = "select \"TGRP:CULC\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') "; //String query = "select \"TGRP:TRAFCARR\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') "; ResultSet resultset = statement.executeQuery(query); while (resultset.next()!= false){ String Str1 = resultset.getString(1); String Str2 = resultset.getString(2); if (Str1.indexOf("-")==-1){ CULC[y]= Float.parseFloat(Str1); } // temps[y]=Str2; y++; } System.out.println(y); connect.close();// fermuture de la connection à la BD } catch(SQLException sqle) { System.err.println("Erreur lors de la connexion : " + sqle) ; } DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i=0;i<y;i++){ // dataset.addValue(Traf[i], series1, temps[i]); // dataset.addValue(SRVLINES[i], series2, temps[i]); dataset.addValue(CULC[i], series3, temps[i]); } return dataset; } private JFreeChart createChart(CategoryDataset dataset, CategoryDataset dataset12, CategoryDataset dataset22) { // create the chart... JFreeChart jfreechart = ChartFactory.createLineChart( "Line Chart Demo 1", // chart title "Time", // domain axis label "Value", // range axis label dataset, // data //dataset12, // data //dataset22, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); jfreechart.setBackgroundPaint(Color.white); CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot(); categoryplot.setOrientation(PlotOrientation.VERTICAL); categoryplot.setBackgroundPaint(Color.lightGray); categoryplot.setDomainGridlinePaint(Color.white); categoryplot.setRangeGridlinePaint(Color.white); categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); categoryplot.getRangeAxis().setFixedDimension(15D); LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer() ; lineandshaperenderer.setSeriesPaint(0, Color.black); categoryplot.setRenderer(0, lineandshaperenderer); CategoryDataset dataset1 = createDataset1(); categoryplot.setDataset(1,dataset1) ; categoryplot.mapDatasetToRangeAxis(1, 1); //LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer(); LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer(true,false); lineandshaperenderer1.setSeriesPaint(1, Color.red); categoryplot.setRenderer(1, lineandshaperenderer1); NumberAxis numberaxis = new NumberAxis("TGRP:SRVLINES"); numberaxis.setFixedDimension(10D); numberaxis.setAutoRangeIncludesZero(false); numberaxis.setLabelPaint(Color.red); numberaxis.setTickLabelPaint(Color.red); categoryplot.setRangeAxis(1, numberaxis); categoryplot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT); //StandardXYItemRenderer standardxyitemrenderer1 = new StandardXYItemRenderer(); CategoryDataset dataset2 = createDataset2(); categoryplot.setDataset(2,dataset2) ; categoryplot.mapDatasetToRangeAxis(2, 2); LineAndShapeRenderer lineandshaperenderer2 = new LineAndShapeRenderer(true,false); lineandshaperenderer2.setSeriesPaint(2, Color.blue);// couleur de la courbe categoryplot.setRenderer(2, lineandshaperenderer2); NumberAxis numberaxis1 = new NumberAxis("TGRP:CULC"); numberaxis1.setLabelPaint(Color.blue);// couleur indexe de l'axe numberaxis1.setTickLabelPaint(Color.blue);// couleur des value categoryplot.setRangeAxis(2, numberaxis1); categoryplot.setRangeAxisLocation(2, AxisLocation.BOTTOM_OR_RIGHT); return jfreechart; } public static void main(String[] args) { LineChartTests demo = new LineChartTests("Line Chart Demo"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } }