using JFreeChart with JSP

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

Re: still need help

Post by Mutia » Tue Dec 27, 2005 7:31 am

Mutia wrote:I still need help here,

Is it possible to use this method to create tooltips? when there are 2 chart in 1? What I mean that I draw bar chart and also the line chart .. but I only want it for the line chart.
this one also already manage to do it :lol:

tomkast

fantastic - two updates

Post by tomkast » Mon Jan 02, 2006 8:08 am

unreal - worked ont he first try adding it to www.ussrp.com

we need this for drill downs.

i had to make two simple updates to Pie3DDemo which others will need to make to compile under jfreecharts 1.0:
1) line 53 ChartFactory.createPieChart3D(...) //new method name
2) line 83 ChartUtilities.writeImageMap() //has a fourth arg - add false

thanks!

tom

James.Cheng
Posts: 42
Joined: Fri Dec 02, 2005 9:43 pm
Location: China

Post by James.Cheng » Tue Jan 03, 2006 6:03 pm

I think this is not a good solution,you can reference Cerolf or WebGem,
Store chart definition in session not a chart image with chart Id and session id

JIron
Posts: 20
Joined: Mon May 15, 2006 10:42 pm

Post by JIron » Mon May 15, 2006 10:51 pm

psupa, i´m newbie on JFreeChart, and iam receiving a NullPointer at this lines:

Code: Select all

response.getOutputStream().write(encoder.pngEncode()); 

Code: Select all

BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage");
My Pie3DChart.java it´s de same of yours and using JFreeChart 1.01!Libs and dependencies are OK!
Tomcat Stack Error:

Code: Select all

13/05/2006 18:12:30 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet ChartServlet threw exception
java.lang.NullPointerException
	at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:82)
	at ChartServlet.doGet(ChartServlet.java:33)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
	at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
	at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
	at java.lang.Thread.run(Unknown Source)
13/05/2006 18:13:52 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet ChartServlet threw exception
java.lang.NullPointerException
	at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:82)
	at ChartServlet.doGet(ChartServlet.java:32)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
	at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
	at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
	at java.lang.Thread.run(Unknown Source)

psupa
Posts: 13
Joined: Fri Jun 18, 2004 9:32 am

Post by psupa » Wed May 17, 2006 5:06 am

Please try to debug your code in the servlet which object is null and fix from that point.

milsum
Posts: 2
Joined: Fri Aug 25, 2006 12:37 pm

Post by milsum » Mon Aug 28, 2006 2:06 pm

hello ,
i'm usin jfree1.0.1, when i tryed the psupa's example i ha a blank screen with the following source vue

Code: Select all

<map id="imageMap" name="imageMap">
<area shape="poly" coords="259,89,273,81,288,76,304,73,320,72,320,186,320,186" title="Dec = 16,227" alt="" href="Bar3DDemo.jsp?section=Dec&pieIndex=0"/>
<area shape="poly" coords="223,124,239,105,259,89,320,186,320,186" title="Nov = 12,91" alt="" href="Bar3DDemo.jsp?section=Nov&pieIndex=0"/>
<area shape="poly" coords="205,193,206,175,209,157,215,140,223,124,320,186,320,186" title="Oct = 18,299" alt="" href="Bar3DDemo.jsp?section=Oct&pieIndex=0"/>
<area shape="poly" coords="226,251,218,238,211,223,207,208,205,193,320,186,320,186" title="Sep = 16,074" alt="" href="Bar3DDemo.jsp?section=Sep&pieIndex=0"/>
<area shape="poly" coords="273,290,259,283,247,274,235,263,226,251,320,186,320,186" title="Aug = 15,773" alt="" href="Bar3DDemo.jsp?section=Aug&pieIndex=0"/>
<area shape="poly" coords="348,297,329,300,310,300,291,297,273,290,320,186,320,186" title="Jul = 19,715" alt="" href="Bar3DDemo.jsp?section=Jul&pieIndex=0"/>
<area shape="poly" coords="405,262,393,274,379,283,364,291,348,297,320,186,320,186" title="Jun = 17,16" alt="" href="Bar3DDemo.jsp?section=Jun&pieIndex=0"/>
<area shape="poly" coords="427,226,417,245,405,262,320,186,320,186" title="May = 10,666" alt="" href="Bar3DDemo.jsp?section=May&pieIndex=0"/>
<area shape="poly" coords="434,188,432,207,427,226,320,186,320,186" title="Apr = 10,111" alt="" href="Bar3DDemo.jsp?section=Apr&pieIndex=0"/>
<area shape="poly" coords="423,137,428,149,431,162,433,174,434,188,320,186,320,186" title="Mar = 13,34" alt="" href="Bar3DDemo.jsp?section=Mar&pieIndex=0"/>
<area shape="poly" coords="384,92,396,101,407,112,415,124,423,137,320,186,320,186" title="Feb = 15,24" alt="" href="Bar3DDemo.jsp?section=Feb&pieIndex=0"/>
<area shape="poly" coords="320,72,337,73,353,77,369,83,384,92,320,186,320,186" title="Jan = 17,578" alt="" href="Bar3DDemo.jsp?section=Jan&pieIndex=0"/>
</map>
<html>
<head>
<title>Pie Chart Demo</title>
</head>



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



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

</body>
</html>


kemalcakmak
Posts: 5
Joined: Wed Jun 14, 2006 1:15 pm
Location: Germany

same blank screen

Post by kemalcakmak » Thu Oct 19, 2006 10:55 am

I get also the blank screen when I try to debug the jsp page. How can I check if the servlet does the expected job? May without an image map?

greatdonking
Posts: 4
Joined: Mon May 15, 2006 2:05 pm

Post by greatdonking » Fri Dec 22, 2006 5:21 pm

Hi every body.

I would know if instead writing this in the response :

Code: Select all

response.getOutputStream().write(encoder.pngEncode());
we could return something like this "<img src=").append(new String(encoder.pngEncode())).append(" />".


In this case, we could return different kind of HTML elements in the response...

puppinoo
Posts: 2
Joined: Mon Jan 15, 2007 3:30 pm

Post by puppinoo » Mon Jan 15, 2007 3:44 pm

Hi,
Im sorry if the question im about to ask is too simple but im a totally newbie in java and servlets.

Thanks to psupa post Ibve been able to create a chart (<IMG> tag pointing to a servlet which stream a bufferedimage to the response).


Unfortunately I need to put 2 or more different charts on the same jsp.

I created different beans for each type (pie, Timeseries etc) but when i try to put create more than 2 bean instance pointing to different <IMG>, I always get the LAST bufferedimage created bu the servlet.

This is my simple jsp:

Code: Select all

<%@page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.net.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*, java.util.Calendar" %>

<%@ page import="org.jfree.chart.demo.*" %>

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

<%

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



<html>
<meta http-equiv="Cache-Control" CONTENT="no-cache"> 
<meta http-equiv="Pragma" CONTENT="no-cache">
<head> 
<title>Demo JFreeChart</title> 
</head> 


<body> 
<h2>Prova JFreeChart </h2> 

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

<jsp:useBean id="myChart2" scope="session" class="org.jfree.chart.demo.Pie3DDemo" /> 
<%

   String chartViewer2 = myChart2.getChartViewer(request, response);
%>
<img src="<%=chartViewer2%>" border=0 usemap="#imageMapPie" NAME="immagine2">


</body> 
</html> 
The code of the servlet is this:

Code: Select all

/*
 * ChartViewer.java
 *
 * Created on 8 gennaio 2007, 16.38
 */

package org.jfree.chart.demo;

/** 
 * 
 * Need to produce some chart prior to this action call in a Java bean 
 * Need a session attribute named "chartImage"; 
 * 
 */ 

import java.io.*; 
import java.awt.image.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import com.keypoint.PngEncoder;
import org.jfree.chart.*;
import org.jfree.chart.JFreeChart;

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.setHeader("Pragma", "No-cache"); 
   response.setDateHeader("Expires", 0); 
   response.setHeader("Cache-Control", "no-cache");
   
   response.setContentType("image/png"); 

   
   // send the picture 
   PngEncoder encoder = new PngEncoder(chartImage, false, 0, 9);
   response.getOutputStream().write(encoder.pngEncode());
   
   response.getOutputStream().flush();
   response.getOutputStream().close();
   
   // necessario?
   session.removeAttribute("chartImage");

    
  } 

  //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() { 
  } 

} 
and this is one of my beans (they are all similars as structure):

Code: Select all

/*
 * Pie3DDemo.java
 *
 * Created on 8 gennaio 2007, 16.58
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jfree.chart.demo;

/** 
 * 
 * JFreeChart version 0.9.20 
 * Called by Pie3DDemo.jsp 
 * 
 */ 

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

public class Pie3DDemo { 
    
  private BufferedImage BufImg;

  public Pie3DDemo() { 
  } 
  
  // ----------------TEMP
  public void setBufImage(BufferedImage BufImg) {
      this.BufImg = BufImg;
  }
  
  public BufferedImage getBufImage() {
      return this.BufImg;
  }
  // FINE TEMP --------------------------------------

  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( 
          "Prova JFreeChart",  // 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); 
      
      this.BufImg = chartImage;

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

//      session.setAttribute("jfChart", chart);

      PrintWriter writer = new PrintWriter(response.getWriter()); 
      org.jfree.chart.ChartUtilities.writeImageMap(writer, "imageMapPie", info, false); 
      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 + "/ChartViewer";
    return chartViewer; 
  } 
} 
Sorry again if the question is stupid but please help.

Thanks a lot.
Pino

psupa
Posts: 13
Joined: Fri Jun 18, 2004 9:32 am

Post by psupa » Mon Feb 12, 2007 11:35 am

1. Create another chartviewer.

Code: Select all

BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage2"); 
2. In the bean code.

Code: Select all

...
session.setAttribute("chartImage2", chartImage);
... 
ChartUtilities.writeImageMap(writer, "imageMap2", info); 
...
String chartViewer = pathInfo + "/servlet/ChartViewer2";
3. Add more servlet mapping in web.xml

puppinoo
Posts: 2
Joined: Mon Jan 15, 2007 3:30 pm

Post by puppinoo » Mon Feb 12, 2007 8:26 pm

Thanks a lot for your help.
I started using Cewolf but Ill try tomorrow.

Bye.
Pino.

yannick
Posts: 1
Joined: Mon Feb 12, 2007 12:15 pm

Cannot drill down to a specific jsp

Post by yannick » Wed Feb 14, 2007 9:54 am

Hi guys,

I used the code from psupa and managed to display category bar chart but when I specify this line below it still drills down to index.html with the value parameters instead of xy_chart.jsp

renderer.setItemURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp","series","section"));

I'm quite new with these charts so excuse my simple question but can someone help me to drilldown to xy_chart.jsp ?

Thanks in advance,

Yannick

Here is the full code for getChartViewer:

public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {
CategoryDataset dataset = getDataset();

// set drilldown capability...
CategoryAxis categoryAxis = new CategoryAxis("");
ValueAxis valueAxis = new NumberAxis("");
BarRenderer renderer = new BarRenderer();
renderer.setItemURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp","series","section"));
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());

Plot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);

// create the chart...
JFreeChart chart = ChartFactory.createBarChart3D(
"3D Bar Chart Demo", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
true // urls
);

plot.setNoDataMessage("No data available");
chart.setBackgroundPaint(java.awt.Color.white);

// OPTIONAL CUSTOMISATION COMPLETED.

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
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, false);

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 + "/chart/ChartViewer";
System.out.println("path:" + chartViewer);
return chartViewer;
}

psupa
Posts: 13
Joined: Fri Jun 18, 2004 9:32 am

Post by psupa » Thu Feb 15, 2007 7:05 am

Example war file jfcdemo.war http://hosted.filefront.com/psupa/

NY
Posts: 3
Joined: Tue Mar 20, 2007 7:35 am

Post by NY » Tue Mar 20, 2007 7:54 am

Hi...I used ur code and could successfully display the pie chart in my jsp......thnx a ton for dat....the only problem i am facing now is to add the rotating effect to my 3D pie chart......i am unable to display the rotating effect in my jsp.......
Here is my class:

public class Pie3DDemo {

public Pie3DDemo() {
}

private static PieDataset createDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("UPCL OBS", 35);
dataset.setValue("PGCIL", 25);
dataset.setValue("UPCIL MIS", 15);
dataset.setValue("THDC", 12.5);
dataset.setValue("JVVNL", 30);
return dataset;
}

//to create the chart
private static JFreeChart createChart(PieDataset dataset) {

JFreeChart chart = ChartFactory.createPieChart3D(
"ASTIN PROJECTS", // chart title
dataset, // data
true, // include legend
true,
false
);
final PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setStartAngle(270);
plot.setDirection(Rotation.ANTICLOCKWISE);
plot.setForegroundAlpha(0.60f);
plot.setInteriorGap(0.33);

final Rotator rotator = new Rotator(plot);
rotator.start();
//set Chart Background Colour
chart.setBackgroundPaint(Color.gray);
return chart;
}


public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {
Pie3DDemo demo=new Pie3DDemo();
PieDataset dataset=demo.createDataset();
JFreeChart chart=demo.createChart(dataset);

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 + "/ChartViewer";
return chartViewer;
}
}

Please help me how to show this effect......
thanks....

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Mar 20, 2007 6:24 pm

The rotating pie chart works in Swing because the ChartPanel class picks up ChartChangeEvent notifications (which are triggered by a chain of events that starts when the dataset is modified) and repaints the chart. You can't do the same thing in a web app.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked