Somye
Somye

Reputation: 87

jfreechart in jsp

i want to create a pie chart in jsp using jfree chart, i am using this code

<%@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> 

The problem is that i am getting this exception "java.io.FileNotFoundException: ../webapps/jspchart/web/piechart.png (No such file or directory) "

any thoughts??

Upvotes: 1

Views: 5635

Answers (3)

Somye
Somye

Reputation: 87

i got this.Actually i needed the pie chart to fetch values from a database. The database's first column is the name and second its value. The code is: Table name is chart and database is 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>

Upvotes: 1

Siva Charan
Siva Charan

Reputation: 18064

Exception clearly says "java.io.FileNotFoundException: ../webapps/jspchart/web/piechart.png (No such file or directory)"

Here the piechart.png(../webapps/jspchart/web/piechart.png) or web directory is not exists.

Verify these info and fix it.

Fix can be:-

  1. Creating a Web folder under jspchart folder OR
  2. Placing a piechart.png file under Web folder

Then try to compile and run application once again.

Upvotes: 1

Tomer
Tomer

Reputation: 17930

Did you check this path? is the file there? (i bet not).

Copy the file under that directory and your problem will be solved.

Upvotes: 0

Related Questions