Displaying a chart inside a jsp page between HTML code ?

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

Displaying a chart inside a jsp page between HTML code ?

Post by fab » Tue Dec 09, 2003 10:12 am

Hello

I am a beginner with JfreeChart and I have spent a lot of time to read precedent messages in order to understand how to display chart using a servlet and a jsp page.

So this is the result of my work :

Code: Select all

<html>
<head>
<%@ page  import="java.awt.Color"%>
<%@ page  import="java.io.*"%>
<%@ page  import="org.jfree.chart.*"%>
<%@ page  import="org.jfree.chart.plot.XYPlot"%>
<%@ page  import="org.jfree.chart.axis.*"%>
<%@ page  import="org.jfree.chart.renderer.XYDifferenceRenderer"%>
<%@ page  import="org.jfree.data.XYDataset"%>
<%@ page  import="org.jfree.data.time.*"%>
<%@ page  import="org.jfree.ui.*"%>
<%@ page  import="org.jfree.chart.servlet.*"%>
</head>
<body>
<%
	//OutputStream ostream = response.getOutputStream();

    TimeSeries series1 = new TimeSeries("Random 1");
    TimeSeries series2 = new TimeSeries("Random 2");
    double value1 = 0.0;
    double value2 = 0.0;
    Day day = new Day();
    for (int i = 0; i < 200; i++) {
        value1 = value1 + Math.random() - 0.5;
        value2 = value2 + Math.random() - 0.5;
        series1.add(day, value1);
        series2.add(day, value2);
        day = (Day) day.next();
    }

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series1);
    dataset.addSeries(series2);

    JFreeChart chart = ChartFactory.createTimeSeriesChart(
        "Synthese",
        "Time", "Value",
        dataset,
        false,  // legend
        false,  // tool tips
        false  // URLs
    );
    chart.setBackgroundPaint(Color.white);
    XYPlot plot = chart.getXYPlot();
    plot.setRenderer(new XYDifferenceRenderer(Color.green, Color.red, false));
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));

    ValueAxis domainAxis = new DateAxis("Time");
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    plot.setDomainAxis(domainAxis);
    plot.setForegroundAlpha(0.5f);
	//ChartUtilities.writeChartAsPNG(ostream,chart,400,300);
	String filename = ServletUtilities.saveChartAsPNG(chart,300,200,session);
%>
</body>
Voila le graph<p>
<img src="<%=request.getContextPath()%>/servlet/DisplayChart?filename=<%=filename%>" >
Il est bo quand mm
</html>
In this way I generate a chart image in the temporary directory, All is right with this method. But I have not to save the image physicaly, I would like to use a other way (you can see the disabled lines in my code) but when I use the response, HTML code is not displayed. I saw topic related to this problem, how can I do ???

Thank you in advance

Fab a french student :-)

topic already visited :


chart generation and servlet w/o physical file:
http://www.jfree.org/phpBB2/viewtopic.php?t=6399

How to display a JFreeChart in JSP:
http://jfree.org/phpBB2/viewtopic.php?t ... ht=servlet

Displaying Charts:
http://jfree.org/phpBB2/viewtopic.php?t ... ht=servlet

Not able to get tooltips:
http://www.jfree.org/phpBB2/viewtopic.php?t=4673

a few examples using jfreechart with jsp:
http://jfree.org/phpBB2/viewtopic.php?t ... ervlet+jsp

Creating a chart through jsp (Missing chart)
http://www.jfree.org/phpBB2/viewtopic.p ... inside+jsp


Guest

Post by Guest » Wed Dec 10, 2003 6:16 pm

Hi,

Add this line before you write the chart out to the response stream. I'm not sure if thats the soultion but check it out.

response.setContentType("image/png");

lamdor

Hi,

Post by lamdor » Sat Jan 10, 2004 8:21 am

U can include a servlet in JSP page and ask servlet to generate the response for image...

u can use sevletcontext, include extra stuff to achive the same..

Better have a look at the example sources suppiled with JFreeChart.

Have a good time..

akkeri
Posts: 13
Joined: Sat Nov 22, 2003 10:50 pm

Post by akkeri » Mon Jan 12, 2004 2:12 pm

you can do it like this:
in your jsp page you only set the source of the image generated to be your servlet like this:

Code: Select all

<html> 
<head> 
body>
Voila le graph<p> 
<img src="/servlet/your_servlet_name">
</body> 
</html> 
and in your servlet you specify the content type of the file so the browser can read it and you write to the outputstream like this:

Code: Select all

import="java.awt.Color" 
  import="java.io.*" 
  import="org.jfree.chart.*" 
  import="org.jfree.chart.plot.XYPlot" 
  import="org.jfree.chart.axis.*" 
  import="org.jfree.chart.renderer.XYDifferenceRenderer" 
  import="org.jfree.data.XYDataset" 
  import="org.jfree.data.time.*" 
  import="org.jfree.ui.*" 
  import="org.jfree.chart.servlet.*" 
public class your_servlet_name extends HttpServlet {
public your_servlet_name() {
}
 public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = response.getOutputStream();

    TimeSeries series1 = new TimeSeries("Random 1"); 
    TimeSeries series2 = new TimeSeries("Random 2"); 
    double value1 = 0.0; 
    double value2 = 0.0; 
    Day day = new Day(); 
    for (int i = 0; i < 200; i++) { 
        value1 = value1 + Math.random() - 0.5; 
        value2 = value2 + Math.random() - 0.5; 
        series1.add(day, value1); 
        series2.add(day, value2); 
        day = (Day) day.next(); 
    } 

    TimeSeriesCollection dataset = new TimeSeriesCollection(); 
    dataset.addSeries(series1); 
    dataset.addSeries(series2); 

    JFreeChart chart = ChartFactory.createTimeSeriesChart( 
        "Synthese", 
        "Time", "Value", 
        dataset, 
        false,  // legend 
        false,  // tool tips 
        false  // URLs 
    ); 
    chart.setBackgroundPaint(Color.white); 
    XYPlot plot = chart.getXYPlot(); 
    plot.setRenderer(new XYDifferenceRenderer(Color.green, Color.red, false)); 
    plot.setBackgroundPaint(Color.lightGray); 
    plot.setDomainGridlinePaint(Color.white); 
    plot.setRangeGridlinePaint(Color.white); 
    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 

    ValueAxis domainAxis = new DateAxis("Time"); 
    domainAxis.setLowerMargin(0.0); 
    domainAxis.setUpperMargin(0.0); 
    plot.setDomainAxis(domainAxis); 
    plot.setForegroundAlpha(0.5f);
    response.setContentType("image/png");
    ChartUtilities.writeChartAsPNG(out,chart,400,300); 

Chitra

Post by Chitra » Thu Jan 15, 2004 6:32 am

Hi,

I think what u're looking for is displaying the chart without saving the file anywhere. If that's the case, pls try the foll. sample code, which I am using for my application :

First create the JFreeChart object with all the custom parameters.

Then..

Code: Select all

//Create RenderingInfo object
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
//Create a BufferedImage obj(java.awt.img package)
//U can use the createBufferedImage method in JFreeChart to draw the image to the image

BufferedImage chartImage = tChart.createBufferedImage(width, height, info);

PngEncoder encoder = new PngEncoder(chartImage, false, 0, DEFAULT_PNG_COMPRESSION);
byte[] outChar = encoder.pngEncode();

BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
bos.write(outChar);
bos.flush();
bos.close();
I coded the above in a servlet. And called it from the img tag in HTML

Code: Select all

<%
	
	String initPath = request.getContextPath() + "/servlet/CustomChart";


%>

Code: Select all

<img src="<%=initPath%>?id=<%=j%>&index=<%=i%>" border=0>
It is working fine for our application.

Pls try this out.

Suggestions / improvements on the above most welcome !!

TheHaas
Posts: 21
Joined: Sat Feb 07, 2004 9:14 pm
Contact:

Post by TheHaas » Tue Feb 24, 2004 6:06 pm

Code: Select all

<img src="<%=initPath%>?id=<%=j%>&index=<%=i%>" border=0>
In the above, what is i and j??

Guest

How to display image created by createBufferedImage?

Post by Guest » Mon Jul 12, 2004 11:00 am

Can I implement image buffer this way?

Code: Select all

.... produce chart ....
//Create RenderingInfo object 
response.setContentType("text/html"); 
ChartRenderingInfo 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(out); 
ChartUtilities.writeImageMap(writer, "imap", info); 
writer.flush();
With the above buffer, how to call in jsp to display chart?

Code: Select all

<img src="chart.do?method=sendChart&imgid=chartImage" usemap="#imap">
With the above img tag cannot display chart, but I can see the image map when view the source. How can I know the image ID?

poo

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

Post by pmarsollier » Thu Jul 15, 2004 6:52 am

the point with imagemap is that you need 2 acces to your JFreeChart object from 2 diffrent HTTP requests :
one for the HTML which contains imagemap, and one wiht the graph in himself.
There is no way to do it in 1 unqiue HTTP request.

So you have to handle in your server a context beetwen thoses 2 requests.

a good solution is to use a cache / singleton that generatd graphs.

Your JSP will call it to generate the JFreeChart so you can get the ImageMap AND give the graph an unique ID.
Your servlet for displaying the graph will find the JFrreChart by his ID and save it to your response stream.

If I'm not clear enough, you can ask me in french on my mail.

psu

Post by psu » Mon Jul 19, 2004 4:23 am

Hi pmarsollier,

Could you post your example code in both servlet and jsp? or what is your email address?

psu,

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

Post by pmarsollier » Mon Jul 19, 2004 6:19 am

just send me a private message

psu

Post by psu » Tue Jul 20, 2004 10:42 am

Hi pmarsollier,

What is your email address?
Or you can send your example source code in both servlet and jsp to me at psupawa@yahoo.com

Thanks in advance,
psu

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

Post by pmarsollier » Wed Jul 21, 2004 8:51 am


Locked