Struts: Generating images on the fly

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jfreeuser
Posts: 1
Joined: Fri Mar 30, 2007 3:10 pm

Struts: Generating images on the fly

Post by jfreeuser » Fri Mar 30, 2007 5:50 pm

Hi,

I'm new to JFreeChart and need some help on how to render the png image generated by action class on to jsp. There are options of saving the image in a temp dir and them reading from there, but my requirement is not to involve in any io operations and generate the chart on the fly.

method in my action class
TestAction
------------------------------------------------------------------------------
JFreeChart chart = new JFreeChart("Title", JFreeChart.DEFAULT_TITLE_FONT, plot, false);

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
response.setHeader("Content-Type", "image/png");
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 500, 300, info);
response.getOutputStream().flush();
response.getOutputStream().close();
------------------------------------------------------------------------------
img tag in jsp callign above action

<img src="<%= contextPath %>/testChart.do?dispatch=generateChart" width=500 height=300 border=0>

I think the image is being generating, as I can see some encoded data on the calling jsp page. However this is not helpful :(.

Please let me know if I'm missing anything? Any help is much appreciated.

Thanks,
NJ

ceysin
Posts: 1
Joined: Fri Apr 06, 2007 2:40 pm

Post by ceysin » Fri Apr 06, 2007 2:54 pm

Hi,
I'm new in jfreechart too :) . I need same requirement. I mean, generate the chart on the fly and use it without save, as a source.


my generator code (jsp) can generate jpeg on the fly but i can't use it as a source.

Is there any way?

Thanx

Here is my sample generator jsp


<%@ page contentType="text/html;charset=windows-1254"%>
<%@ page import="tr.com.tofas.bayiprofili.reports.*"%>
<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page import="org.jfree.chart.JFreeChart"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="org.jfree.chart.ChartUtilities"%>
<%@ page import="org.jfree.chart.ChartRenderingInfo"%>
<%@ page import="org.jfree.chart.entity.StandardEntityCollection"%>


<%
// create a dataset...
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Category 1", 43.2);
dataset.setValue("Category 2", 27.9);
dataset.setValue("Category 3", 79.5);

// create a chart...
JFreeChart chart = ChartFactory.createPieChart(
"Sample Pie Chart",
dataset,
true, // legend?
true, // tooltips?
false // URLs?
);


ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
response.setHeader("Content-Type", "image/jpeg");
response.setContentType("image/jpeg");

try{
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart, 500, 300,info);
}
catch(Exception e) {
System.out.println("Problem for creating chart");
}

response.getOutputStream().flush();
response.getOutputStream().close();

%>




And That is my sample jsp which couldn't use generator jsp



<%@ page contentType="text/html;charset=windows-1254"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1254"/>
<title>chartMakerMain</title>
</head>
<body>
<br>
<img src="chartmaker.jsp" width=500 height=300 border=0>
</body>
</html>

jfreeuser2006
Posts: 59
Joined: Mon Nov 20, 2006 1:00 pm

Post by jfreeuser2006 » Mon Apr 30, 2007 3:01 am

I think this thread can be of help.

http://www.jfree.org/phpBB2/viewtopic.p ... sc&start=0

Locked