1

我是 jfreechart 的新手。我已经制作了一个条形图并将其作为图像存储在 java.io.tmpdir 的服务器上。它一切正常。现在我想要在我的 jsp 页面上显示这个图像。在我的控制器上
String imageFile = System.getProperty("java.io.tmpdir") + "barChart.jpeg";

    File file = new File (imageFile);

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

在 jsp 上我正在做 < img src=${path} /> ,但这不起作用。

请帮助我如何在 jsp 上显示该图像。谢谢!

4

1 回答 1

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();

        %>  
于 2012-07-24T06:38:04.760 回答