using JFreeChart with JSP
Is Doucment an interface?
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
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
one more question
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
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
-
- Posts: 49
- Joined: Thu Jul 08, 2004 8:54 am
- Location: France
ChartViewer do not refresh chart image
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.
But is the same.
This is the ChartViewer Code.
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">
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() {
}
}
Problem with multiple chart
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
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
Re: ChartViewer do not refresh chart image
You have to set the cashe control also for your ChartViewer Servletkhipras 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.
But is the same.Code: Select all
<meta http-equiv="Cache-Control" CONTENT="no-cache"> <meta http-equiv="Pragma" CONTENT="no-cache">
Code: Select all
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
problem with multiple charts
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
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
This code will get only one chart.
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.
Code: Select all
int len = Integer.parseInt((String) session.getAttribute( "totalChart" ));
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>
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.
Could this cause the tooltips and URL not shown? If yes, please advice on how to solve this.
Thanks.
Regards,
mingjade
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();
Thanks.
Regards,
mingjade
Hello,
Can you please show some example? I'm not sure how to do the imagemap embed, because i though the part
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:
JSP:
Thanks.
Regards,
mingjade
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();
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;
}
}
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>
Regards,
mingjade