使用JSP / servlet在JFreeChart中创建工具提示

时间:2012-06-01 13:07:11

标签: java jsp servlets jfreechart

我正在使用JFreeChart,我想使用servlet / JSP实现工具提示

代码:servlet

doPost() {
    XYDataset xyDataset = Chart.createDataset(chartType, getServletContext());
    JFreeChart chart = Chart.createChart(xyDataset, chartType);
    final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
    final File file1 = new File("D:\\graph\\test.png");
    ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);

    // write an HTML page incorporating the image with an image map
    final File file2 = new File("D:\\graph\\multipiechart100.html");
    final OutputStream out = new BufferedOutputStream(new FileOutputStream(file2));
    final PrintWriter writer = new PrintWriter(out);
    writer.println("<HTML>");
    writer.println("<HEAD><TITLE>JFreeChart Image Map Demo</TITLE></HEAD>");
    writer.println("<BODY>");
    //            ChartUtilities.writeImageMap(writer, "chart", info);
    writer.println("<IMG SRC=\"multipiechart100.png\" "
        + "WIDTH=\"600\" HEIGHT=\"400\" BORDER=\"0\" USEMAP=\"#imageMap\">");
    writer.println("</BODY>");
    writer.println("</HTML>");
    writer.close();

    BufferedImage buf = chart.createBufferedImage(800, 450, info);
    ChartUtilities.writeImageMap(writer, "imageMap", info, true);
    //          ChartUtilities.writeBufferedImageAsPNG(out,buf);
    ChartUtilities.writeChartAsPNG(out, chart, 700, 250, info);
}

JSP:

<img alt="" id="monthchart" usemap="#imageMap"
                            src='<%=url+"/TimeChart/servlet/ChartServlet?CHARTTYPE="+ChartConstant.MONTH_CHART%>'></img>

但我没有得到任何图表,我得到这样的错误:

java.io.IOException: Write error
    at java.io.FileOutputStream.writeBytes(Native Method)
    at java.io.FileOutputStream.write(FileOutputStream.java:260)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
    at javax.imageio.stream.FileCacheImageOutputStream.close(FileCacheImageOutputStream.java:213)
    at javax.imageio.ImageIO.write(ImageIO.java:1567)
    at org.jfree.chart.encoders.SunPNGEncoderAdapter.encode(SunPNGEncoderAdapter.java:127)
    at org.jfree.chart.encoders.EncoderUtil.writeBufferedImage(EncoderUtil.java:134)
    at org.jfree.chart.ChartUtilities.writeChartAsPNG(ChartUtilities.java:185)
    at com.chart.ChartServlet.doPost(ChartServlet.java:138)
    at com.chart.ChartServlet.doGet(ChartServlet.java:60)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
    at java.lang.Thread.run(Thread.java:619)

1 个答案:

答案 0 :(得分:0)

首先,在调用ChartUtilities.writeImageMap(...)之前,您正在关闭编写器;