1

我想使用 jfree 图表在 jsp 中创建一个饼图,我正在使用此代码

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ page  import="java.awt.*" %>
<%@ page  import="java.io.*" %>
<%@ page  import="org.jfree.chart.*" %>
<%@ page  import="org.jfree.chart.entity.*" %>
<%@ page  import ="org.jfree.data.general.*"%>
<%
  final DefaultPieDataset data = new DefaultPieDataset();
  data.setValue("One", new Double(43.2));
  data.setValue("Two", new Double(10.0));
  data.setValue("Three", new Double(27.5));
  data.setValue("Four", new Double(17.5));
  data.setValue("Five", new Double(11.0));
  data.setValue("Six", new Double(19.4));

  JFreeChart chart = ChartFactory.createPieChart
  ("Pie Chart ", data, true, true, false);

 try {
 final ChartRenderingInfo info = new 
  ChartRenderingInfo(new StandardEntityCollection());

  final File file1 = new File("../webapps/jspchart/
  web/piechart.png");
  ChartUtilities.saveChartAsPNG(
   file1, chart, 600, 400, info);
  } catch (Exception e) {
  out.println(e);
  }
%>
<html>
  <head>
  <meta http-equiv="Content-Type" 
  content="text/html; charset=UTF-8">
  <title>JSP Page</title>
  </head>
  <body>
  <IMG SRC="piechart.png" WIDTH="600" HEIGHT="400" 
   BORDER="0" USEMAP="#chart">
  </body>
</html> 

问题是我收到此异常“java.io.FileNotFoundException:../webapps/jspchart/web/piechart.png(没有这样的文件或目录)”

有什么想法吗??

4

3 回答 3

1

异常清楚地说“ java.io.FileNotFoundException: ../webapps/jspchart/web/piechart.png (No such file or directory)

这里piechart.png ( ../webapps/jspchart/web/piechart.png) 或web目录不存在。

验证这些信息并修复它。

修复可以是:-

  1. Web在文件夹下创建文件jspchart夹或
  2. piechart.png文件放在Web文件夹下

然后尝试再次编译并运行应用程序。

于 2012-07-03T09:59:59.843 回答
1

我明白了。实际上我需要饼图从数据库中获取值。数据库的第一列是名称,第二列是它的值。代码是:表名是chart,数据库是maj

<%@ page import="java.io.*"%>

<<%@ page  import="java.awt.*" %>
<%@ page  import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.jfree.data.jdbc.JDBCPieDataset" %>
<%@ page import="org.jfree.chart.plot.PlotOrientation" %>
<%@ page import="org.jfree.chart.JFreeChart" %>
<%@ page import="org.jfree.chart.ChartUtilities" %>
<%@ page import="org.jfree.chart.ChartFactory" %>
<%@ page import="org.jfree.data.general.DefaultPieDataset" %>
<%@ page import="org.jfree.chart.*"%>
<%@ page import="org.jfree.chart.entity.*"%>
<%@ page import="org.jfree.data.general.*"%>
<%@ page import="org.jfree.chart.plot.PiePlot;" %>

<%

                String query = "SELECT * from chart";
                JDBCPieDataset dataset = new JDBCPieDataset("jdbc:mysql://localhost:3306/maj", "com.mysql.jdbc.Driver","root", "password");
                dataset.executeQuery(query);

            JFreeChart chart = ChartFactory.createPieChart("File System",dataset, true, true, false);
            //chart.setBackgroundPaint(new Color(222, 222, 255));
                final PiePlot plot = (PiePlot) chart.getPlot();
                plot.setBackgroundPaint(Color.white);
                plot.setCircular(true);

            try {

                final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
                final File file1 = new File(getServletContext().getRealPath(".") + "/piechart.png");

                ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
            } catch (Exception e) {
                System.out.println(e);

            }




%>
<html>
    <body>
        Heading
        <IMG SRC="piechart.png" WIDTH="500" HEIGHT="400" style="border:4px solid orange;" USEMAP="#chart" alt="image">
    </body>
</html>
于 2012-07-03T15:55:05.333 回答
0

你检查过这条路吗?文件在那里吗?(我打赌不会)。

复制该目录下的文件,您的问题将得到解决。

于 2012-07-01T11:49:31.213 回答