using JFreeChart with JSP

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Guest

Post by Guest » Tue Aug 24, 2004 7:56 pm

> org.jfree.base.Library exists in jcommon-0.9.5

nice, I guess the web page at http://www.jfree.org/jcommon/index.html needs an update then.

Thank you.

Steve
Posts: 53
Joined: Mon Dec 22, 2003 8:29 pm

Post by Steve » Tue Aug 24, 2004 8:05 pm

Ya it does.
I believe 0.9.5 came with jFreechart 0.9.20, because I can't find it for download anywhere, nor can I find the zip file for it on my computer.

*Edit*
It's present in my /jFreechart-0.9.20/lib directory.

sunonepp

Is Doucment an interface?

Post by sunonepp » Wed Aug 25, 2004 2:33 am

For generating PDF, you use:
Document document = new Document(pagesize, 50, 50, 50, 50);
Is this Document java.swing.text.Document which is an interface?
If not, which pakage should I import?
thanks

sunonepp

one more question

Post by sunonepp » Wed Aug 25, 2004 2:52 am

I know the Doucument is from iText now. But one more question:

Document document = new Document(pagesize, a, b, c, d);
OutputStream os = new BufferedOutputStream(out);

the out argument to create new BufferedOutputStream is OutputStream, but where it is created?

thanks

pmarsollier
Posts: 49
Joined: Thu Jul 08, 2004 8:54 am
Location: France

Post by pmarsollier » Mon Aug 30, 2004 9:02 am

it's just working in memory.

No file is writen, it's just send through the servlet outpoutstream.

khipras
Posts: 15
Joined: Mon Feb 07, 2005 6:41 pm
Location: Mexico D.F.
Contact:

ChartViewer do not refresh chart image

Post by khipras » Tue Mar 01, 2005 10:02 pm

Hi, every one.,
I use this ChartViewer code example to create my image charts.
I pass some parameters to the servlet chart, but when I change some parameters, ChartViewer show me the last chart , not the new that I selected.
But when I reload the page, the refresh my chart.
I put on my jsp page.

Code: Select all

<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
But is the same.

This is the ChartViewer Code.

Code: Select all

package myapp;

import java.io.*;
import java.awt.image.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.keypoint.PngEncoder;

public class ChartViewer extends HttpServlet {

  public void init() throws ServletException {
  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {

   // get the chart from session
   HttpSession session = request.getSession();
   BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage");

   // set the content type so the browser can see this as a picture
   response.setContentType("image/png");

   // send the picture
   PngEncoder encoder = new PngEncoder(chartImage, false, 0, 9);
   response.getOutputStream().write(encoder.pngEncode());
   
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
    doGet(request, response);
  }

  //Process the HTTP Put request
  public void doPut(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
  }

  //Clean up resources
  public void destroy() {
  }

} 

mjit

Problem with multiple chart

Post by mjit » Fri Mar 18, 2005 9:52 am

guys,

I have a problem when i create multiple charts using a loop. I m using the Chartviewer sevlet to buffer the image and render it in the jspm page. But i could see charts with the last parameters only, i mean they are all same. Could this be coz the buffer is overwritten? Any suggestions?

Thanx
mjit

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Re: ChartViewer do not refresh chart image

Post by angel » Fri Mar 18, 2005 10:00 am

khipras wrote:Hi, every one.,
I use this ChartViewer code example to create my image charts.
I pass some parameters to the servlet chart, but when I change some parameters, ChartViewer show me the last chart , not the new that I selected.
But when I reload the page, the refresh my chart.
I put on my jsp page.

Code: Select all

<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
But is the same.
You have to set the cashe control also for your ChartViewer Servlet

Code: Select all

response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");

mjitch

problem with multiple charts

Post by mjitch » Mon Mar 21, 2005 7:24 am

Hi,

I need to create multiple charts with different parameters. I am using the ChartViewer servlet to buffer and encode the images. But i see only the same chart when i display them using the <img> tag in the jsp. Here's my code for the ChartViewer servlet -

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.image.BufferedImage;
import com.keypoint.PngEncoder;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.data.general.DefaultPieDataset;

public class ChartViewerBuf extends HttpServlet
{
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{

// get the chart from storage
HttpSession session = request.getSession(true);

int len = Integer.parseInt((String) session.getAttribute( "totalChart" ));
JFreeChart chart [] = new JFreeChart[len];
// set the content type so the browser can see this as it is
response.setContentType( "image/png" );

// send the picture
BufferedImage buf[] = new BufferedImage[len];
PngEncoder encoder[] = new PngEncoder[len];
for (int i=0; i<len; i++ )
{
chart = (JFreeChart) session.getAttribute( "chart"+i);
buf = chart.createBufferedImage(400, 350, null);
encoder = new PngEncoder( buf, false, 0, 9 );
System.out.println("i= "+i);
response.getOutputStream().write( encoder.pngEncode() );
}

}
}

Any suggestions?

regards,
mjitch

Guest

Post by Guest » Thu Mar 31, 2005 4:35 am

This code will get only one chart.

Code: Select all

int len = Integer.parseInt((String) session.getAttribute( "totalChart" )); 
In your bean code, you need to save different attribute name for each chart and get them accordingly. Be reminded that this may not only one solution, others may have a better way.

mingjade

Post by mingjade » Wed Sep 14, 2005 4:17 am

Hi,

I have tested with the example from psupa's posting from 26-07-04 05:11:32 and the pie chart shown succesffully but the tooltips or URL is not showing. Do you know what could went wrong?

Thanks.
Regards,
mingjade

Guest

Post by Guest » Wed Sep 14, 2005 12:10 pm

I test and it work fine, able to show tooltips and URL. Please try to view the source in a browser. You should see an image map name something like below. This image map will show tooltips and URL. If not show, you need to see in your code.

Code: Select all

<MAP NAME="imageMap">
<AREA SHAPE="POLY" COORDS="320,88,348,89,376,91,402,95,428,101,320,179,320,179" title="Jan = 14.934" href="Bar3DDemo.jsp?section=Jan&pieIndex=0">
<AREA SHAPE="POLY" COORDS="428,101,451,108,472,117,491,126,506,137,320,179,320,179" title="Feb = 15.196" href="Bar3DDemo.jsp?section=Feb&pieIndex=0">
<AREA SHAPE="POLY" COORDS="506,137,519,151,526,165,529,179,526,194,320,179,320,179" title="Mar = 17.588" href="Bar3DDemo.jsp?section=Mar&pieIndex=0">
<AREA SHAPE="POLY" COORDS="526,194,519,207,506,220,490,232,469,242,320,179,320,179" title="Apr = 16.848" href="Bar3DDemo.jsp?section=Apr&pieIndex=0">
<AREA SHAPE="POLY" COORDS="469,242,446,251,420,259,392,264,363,268,320,179,320,179" title="May = 16.143" href="Bar3DDemo.jsp?section=May&pieIndex=0">
<AREA SHAPE="POLY" COORDS="363,268,310,270,283,268,257,266,320,179,320,179" title="Jun = 14.032" href="Bar3DDemo.jsp?section=Jun&pieIndex=0">
<AREA SHAPE="POLY" COORDS="257,266,220,259,187,249,320,179,320,179" title="Jul = 10.583" href="Bar3DDemo.jsp?section=Jul&pieIndex=0">
<AREA SHAPE="POLY" COORDS="187,249,158,237,136,222,320,179,320,179" title="Aug = 10.631" href="Bar3DDemo.jsp?section=Aug&pieIndex=0">
<AREA SHAPE="POLY" COORDS="136,222,127,214,119,206,114,197,111,188,320,179,320,179" title="Sep = 10.863" href="Bar3DDemo.jsp?section=Sep&pieIndex=0">
<AREA SHAPE="POLY" COORDS="111,188,110,177,113,165,118,153,128,142,320,179,320,179" title="Oct = 14.21" href="Bar3DDemo.jsp?section=Oct&pieIndex=0">
<AREA SHAPE="POLY" COORDS="128,142,138,133,151,125,166,117,183,110,320,179,320,179" title="Nov = 12.339" href="Bar3DDemo.jsp?section=Nov&pieIndex=0">
<AREA SHAPE="POLY" COORDS="183,110,213,100,247,94,282,89,320,88,320,179,320,179" title="Dec = 19.542" href="Bar3DDemo.jsp?section=Dec&pieIndex=0">
</MAP>
<html>
<head>
<link rel="stylesheet" href="../sample.css" type="text/css"/>
<title>Pie3D Demo</title>
</head>

 

<body>
<img src="../images/top_bar.png" width=100% height=75 border=0>
<table border=0>
	<tr>
	<td width=170><img src="../images/spacer.png" width=170 height=1></td>
	<td>
	<h2>Pie3D Demo</h2>

	<img src="http://localhost:8080/jfc0921/servlet/ChartViewer" border=0 usemap="#imageMap">

	<p>
	The chart shown above has tooltips and drilldown enabled.<br>
	Chart image save to output stream instead of filesystem.<br>
	</p>

	</td>
	</tr>
</table>
</body>
</html>

mingjade

Post by mingjade » Thu Sep 15, 2005 1:32 am

Hello,

I have view source code on the browser, i can't see the <MAP> on the code. I wonder which part went wrong? Since i'm using jfreechart-1.0.0-rc1 where this part ChartUtilities.writeImageMap different from the example shown because of the version used.

Code: Select all

PrintWriter writer = new PrintWriter(response.getWriter());
ChartUtilities.writeImageMap(writer, "imageMap", info,true);
writer.flush();
Could this cause the tooltips and URL not shown? If yes, please advice on how to solve this.

Thanks.
Regards,
mingjade

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Thu Sep 15, 2005 7:27 am

mingjade wrote: Could this cause the tooltips and URL not shown? If yes, please advice on how to solve this.
You have to embed the imagemap stream into your generated html. You code doesn't show this.

mingjade

Post by mingjade » Thu Sep 15, 2005 11:36 am

Hello,

Can you please show some example? I'm not sure how to do the imagemap embed, because i though the part

Code: Select all

PrintWriter writer = new PrintWriter(response.getWriter());
      ChartUtilities.writeImageMap(writer, "imageMap", info,true);
      writer.flush();
will actually return the imageMap code. Is that correct? Please advice.

Anyhow, the code is getting from the previous post. Please let me know where i did wrongly?

Here is the code:

Code: Select all

import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.image.*;
import org.jfree.data.general.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.entity.*;
import javax.servlet.http.*;

public class Pie3DDemo {

  public Pie3DDemo() {
  }

  private DefaultPieDataset getDataset() {
    // categories...
    String[] section = new String[] {
      "Jan","Feb","Mar","Apr","May","Jun",
      "Jul","Aug","Sep","Oct","Nov","Dec"
   };

    // data...
    double[] data = new double[section.length];
    for (int i = 0; i < data.length; i++) {
        data[i] = 10 + (Math.random() * 10);
    }

    // create the dataset...
    DefaultPieDataset dataset = new DefaultPieDataset();
    for (int i = 0; i < data.length; i++) {
        dataset.setValue(section[i], data[i]);
    }
    return dataset;
  }

  public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {
    DefaultPieDataset dataset = getDataset();
    // create the chart...
    JFreeChart chart = ChartFactory.createPieChart3D(
          "Pie3D Chart Demo",  // chart title
          dataset,             // data
          true,                // include legend
          true,
          false
    );

    // set the background color for the chart...
    chart.setBackgroundPaint(Color.cyan);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setNoDataMessage("No data available");

    // set drilldown capability...
    plot.setURLGenerator(new StandardPieURLGenerator("Bar3DDemo.jsp","section"));
    plot.setLabelGenerator(null);

    // OPTIONAL CUSTOMISATION COMPLETED.

    ChartRenderingInfo info = null;
    HttpSession session = request.getSession();
    try {

      //Create RenderingInfo object
      response.setContentType("text/html");
      info = new ChartRenderingInfo(new StandardEntityCollection());
      BufferedImage chartImage = chart.createBufferedImage(640, 400, info);

      // putting chart as BufferedImage in session,
      // thus making it available for the image reading action Action.
      session.setAttribute("chartImage", chartImage);

      PrintWriter writer = new PrintWriter(response.getWriter());
      ChartUtilities.writeImageMap(writer, "imageMap", info,true);
      writer.flush();

    }
    catch (Exception e) {
       // handel your exception here
    }

    String pathInfo = "http://";
    pathInfo += request.getServerName();
    int port = request.getServerPort();
    pathInfo += ":"+String.valueOf(port);
    pathInfo += request.getContextPath();
    String chartViewer = pathInfo + "/servlet/ChartViewer";
    return chartViewer;
  }
}
JSP:

Code: Select all

<html> 
<head> 
<title>Pie Chart Demo</title> 
</head> 

<jsp:useBean id="myChart" scope="session" class="org.jfree.chart.jade.Pie3DDemo" /> 

<body> 
<h2>Pie Chart Demo</h2> 

<%String chartViewer = myChart.getChartViewer(request, response);%> 

<img src="<%=chartViewer%>" border=0 usemap="#imageMap"> 

</body> 
</html>
Thanks.
Regards,
mingjade

Locked