2012-05-11 64 views
1

我是jfreechart的新手。我已經創建了一個條形圖並將其作爲圖像存儲在java.io.tmpdir的服務器上。 它的一切工作正常。現在我想要的是在我的jsp頁面上顯示這個圖像。 在我的控制器
String imageFile = System.getProperty("java.io.tmpdir") + "barChart.jpeg";如何顯示Jsp服務器上生成的Jfreechart圖像

File file = new File (imageFile); 

    uiModel.addAttribute("path", file.getAbsolutePath()); 

和jsp我在做< img src=${path} /> 但這是行不通的。

請幫我一下,我該如何在jsp上顯示該圖像。 謝謝!

回答

1
<%-- 
    Document : s1 
    Created on : 23.07.2012, 15:36:31 
    Author  : r.khamidullin 
--%> 

<%@page import="org.jfree.chart.renderer.xy.XYStepAreaRenderer"%> 
<%@page import="org.jfree.chart.renderer.xy.XYShapeRenderer"%> 
<%@page import="org.jfree.chart.renderer.xy.XYBarRenderer"%> 
<%@page import="org.jfree.chart.renderer.xy.XYDotRenderer"%> 
<%@page import="org.jfree.chart.renderer.xy.XYItemRenderer"%> 
<%@page import="org.jfree.chart.plot.XYPlot"%> 
<%@page import="org.jfree.data.time.TimeSeriesCollection"%> 
<%@page import="org.jfree.data.time.TimeSeries"%> 
<%@page import="java.io.OutputStream"%> 
<%@page import="org.jfree.chart.JFreeChart"%> 
<%@page import="org.jfree.chart.ChartFactory"%> 

<%@page import="org.jfree.chart.ChartUtilities"%> 
<%@page import="java.sql.ResultSet"%> 
<%@page import="java.sql.PreparedStatement"%> 
<%@page import="java.sql.SQLException"%> 
<%@page import="java.sql.DriverManager"%> 
<%@page import="java.sql.Connection"%> 
<%@page contentType="image/png" pageEncoding="UTF-8"%> 
     <% 

      Connection conn = null; 
      try { 

       Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      } catch (Exception ex) { 
      } 
      try { 
       conn = 
         DriverManager.getConnection("jdbc:mysql://localhost:3306/xxx?" 
         + "user=xxxz"); 


      } catch (SQLException ex) { 

       System.out.println("SQLException: " + ex.getMessage()); 
       System.out.println("SQLState: " + ex.getSQLState()); 
       System.out.println("VendorError: " + ex.getErrorCode()); 

      } 

      PreparedStatement statement = null; 
      ResultSet resultSet = null; 


      statement = conn.prepareStatement("SELECT * FROM XXX where CURRENT_TIMESTAMP - ddatetime < 12000 and XXX is not null and XXX < 100000"); 
      resultSet = statement.executeQuery(); 
     TimeSeries s1; 
     s1 = new TimeSeries("Delays, sec"); 

     TimeSeriesCollection dataset = new TimeSeriesCollection(); 
     dataset.addSeries(s1); 
      while (resultSet.next()) { 
       s1.addOrUpdate(org.jfree.data.time.Second.parseSecond(resultSet.getString("ddatetime").substring(0, 19)), resultSet.getInt("XXX")); 

      } 

     JFreeChart mychart = ChartFactory.createTimeSeriesChart("Delays", "T, sec", "DateTime", dataset, true, true, true); 


     XYPlot plot = (XYPlot) mychart.getPlot(); // график 
     //plot.setRenderer(new XYDotRenderer()); // точки 
     plot.setRenderer(new XYBarRenderer()); //столбики 
     //plot.setRenderer(new XYShapeRenderer()); // кружки - shapes 
     //plot.setRenderer(new XYStepAreaRenderer()); 
     ChartUtilities.writeChartAsPNG(response.getOutputStream(), mychart, 800, 200); 
     out.close(); 
      statement.close(); 

     %> 
相關問題