English 中文(简体)
如何展示在服务器上生成的Jfreechart形象
原标题:how to Show Jfreechart image generated at server on jsp

I am new to jfreechart. I have crated a bar chart and stored it as image on server at java.io.tmpdir. Its all working fine. Now wat I want is to show this image on my jsp page. on my controller
String imageFile = System.getProperty("java.io.tmpdir") + "barChart.jpeg";

    File file = new File (imageFile);

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

and on jsp I m doing < img src=${path} /> but this is not working.

Please help me how do I show that image on jsp. Thank you!

最佳回答
<%-- 
    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();

        %>  
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签