bonjour,
j'ai un petit problème que j'arrive pas à solutionner. je veux développer une petite appli qui permet à l'utilisateur via un formulaire de rentrer des paramètres. Ensuite ces paramètres servent à tracer un graphe ( via ma classe MonGraphe) grâce à la biblio JFreeChart. j'obtiens donc un graphe de type JFreeChart dans une fenêtre. mon objectif est enfin de pouvoir afficher ce graphique dans ma jsp. mais c'est la que ça coince. j'ai pensé à sauvegarder ce graphe dans un dossier de mon appli (WebContent/images) en tant qu'image (jpeg ou png) afin de n'avoir plus qu' afficher l'image dans la jsp. Mais ça ne marche pas. Besoin de votre aide. Merci d'avance.
le code de ma classe MonGraphe.
le main() juste pour vérifier que je trace bien le graphe
la méthode doGet() de ma servlet
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public MonGraphe( String applicationTitle, String chartTitle ) { super( applicationTitle ); JFreeChart xylineChart = ChartFactory.createXYLineChart( chartTitle, "Axe des X", "F(x)=Y", createDataset(), PlotOrientation.VERTICAL, true, true, false ); File image = new File( "/WebContent/images/chart.jpeg" ); try { ChartUtilities.saveChartAsJPEG( image, xylineChart, 600, 400 ); } catch ( Exception e ) { System.out.println( "probleme rencontré " + e.getMessage() ); } System.out.println( image.getName() ); ChartPanel chartPanel = new ChartPanel( xylineChart ); chartPanel.setPreferredSize( new java.awt.Dimension( 560, 367 ) ); final XYPlot plot = xylineChart.getXYPlot(); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesPaint( 0, Color.RED ); renderer.setSeriesStroke( 0, new BasicStroke( 4.0f ) ); plot.setRenderer( renderer ); setContentPane( chartPanel ); } private XYDataset createDataset() { final XYSeries maFonction = new XYSeries( "Ma_Fonction" ); maFonction.add( 1.0, 4.0 ); maFonction.add( 2.0, 5.0 ); maFonction.add( 3.0, 4.0 ); maFonction.add( 4.0, 4.0 ); maFonction.add( 5.0, 6.0 ); maFonction.add( 2.0, 1.0 ); maFonction.add( 2.0, 3.0 ); final XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries( maFonction ); return dataset; } public static void main( String[] args ) { MonGraphe chart = new MonGraphe( "TEST", "test!" ); chart.pack(); RefineryUtilities.centerFrameOnScreen( chart ); chart.setVisible( true ); } }
N'hésitez pas à me proposer une autre manière de faire.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class dessine extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public static final String VUE = "/WEB-INF/affiche_input.jsp"; public static final String VUE_INSCRIPTION = "/WEB-INF/inscription.jsp"; public static final String ATT_FONCTION = "maFonction"; public static final String ATT_TRAITEMENT = "monTraitement"; public static final String ATT_GRAPHE = "chart"; public static final String VUE_CHART = "/WEB-INF/jfreechart.jsp"; public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { Graphe monGraphe = new Graphe(); JFreeChart lineChart = monGraphe.CreeJFeeChart(); OutputStream out = response.getOutputStream(); response.setContentType( "image/png" ); File fichier = new File( "images/chart.png" ); try { ChartUtilities.writeChartAsPNG( out, lineChart, 400, 300 ); } catch ( Exception e ) { e.printStackTrace(); } this.getServletContext().getRequestDispatcher( VUE_CHART ).forward( request, response ); }
Partager