You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-user@portals.apache.org by Prajesh Bhattacharya <pb...@lbl.gov> on 2010/08/24 00:51:30 UTC

Problem showing graphs in a portlet

Hello All,

I am very new to portlet development. I have no experience with servlet 
development either. I am trying to show a graph in a portlet. I am using 
JFreeChart library for charting.

My portlet container is JetSpeed2 (2.2.1). As a test case, first I 
tested my code with a line of text. It worked. Then I tried to move on 
to graphics, but my graphics never showed up in the portlet. I decided 
to test if my portlet could show a static image stored on the disk. I 
did not have any luck even with that. I have followed methodology/code 
provided at
http://blogs.sun.com/satya/entry/new_feature_resource_serving_in

Can someone please point out the problem here? If you need more 
information, please let me know. I really appreciate your help.

Thanks.
Prajesh

Following is my portlet code. Please note that I have tested the line
ChartUtilities.writeChartAsJPEG(image_out, quality, demo.chart, 400,300);
inside a stand-alone app in which image_out was a FileOutputStream.

********************************************************
import java.io.IOException;
import java.io.OutputStream;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceURL;
import java.io.PrintWriter;

import org.jfree.chart.ChartUtilities;
// import org.jfree.ui.RefineryUtilities;
// import org.jfree.util.Log;
// import org.jfree.util.PrintStreamLogTarget;


public class ScatterPlot extends GenericPortlet {


public void doView(RenderRequest request, RenderResponse response)
                          throws PortletException, IOException {

//        Log.getInstance().addTarget(new PrintStreamLogTarget());

    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();

    ResourceURL resURL = response.createResourceURL();
    resURL.setResourceID("image");
    writer.println("<IMG src=\"" + resURL + "\" >");

}




public void serveResource(ResourceRequest resRequest, ResourceResponse 
resResp)
                throws PortletException, IOException {

    resResp.setContentType("image/jpeg");
    OutputStream image_out = resResp.getPortletOutputStream();

        TimeSeriesDemo demo = new TimeSeriesDemo("Time Series");
    float quality = (float)(0.75);
    ChartUtilities.writeChartAsJPEG(image_out, quality, demo.chart, 
400,300);
    image_out.flush();
    image_out.close();
}

} // end of class
**********************************************************************


Following is my portlet.xml.
*********************************************************************
<?xml version="1.0" encoding="UTF-8"?>

<portlet-app id="ScatterPlot" version="1.0">

  <portlet id="ScatterPlot_ID">
    <description>Plot as many variables on the Y axis against the ONLY 
variable on the X axis</description>
    <portlet-name>ScatterPlot_Name</portlet-name>
    <display-name>Scatter Plot</display-name>
    <portlet-class>ScatterPlot</portlet-class>

    <expiration-cache>-1</expiration-cache>
    <supports>
      <mime-type>*/*</mime-type>
      <portlet-mode>VIEW</portlet-mode>
      <portlet-mode>HELP</portlet-mode>
      <portlet-mode>EDIT</portlet-mode>
      <!--  support custom about mode -->
      <portlet-mode>about</portlet-mode>
      <!--  support custom edit_defaults mode -->
      <portlet-mode>edit_defaults</portlet-mode>
      <!--  support custom preview mode -->
      <portlet-mode>preview</portlet-mode>
      <!--  support custom print mode -->
      <portlet-mode>print</portlet-mode>
    </supports>
    <supported-locale>en</supported-locale>

    <portlet-info>
      <title>Scatter Plot</title>
    </portlet-info>

  </portlet>

</portlet-app>
*********************************************************************


Following is my web.xml.
*********************************************************************
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 
2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">


<web-app>
   
  <display-name>ScatterPlot</display-name>
  <description>Scatter Plot Portlet</description>

</web-app>
***********************************************************************
-- 
/ *Prajesh Bhattacharya, PhD | Lawrence Berkeley National Laboratory | 
One Cyclotron Road, MS 90R3111, Berkeley, CA 94720-8134 | 510-486-7857 
(W) | 480-586-1387 (M) | 510-486-4089 (Fax)* /

Re: Problem showing graphs in a portlet

Posted by Woonsan Ko <wo...@yahoo.com>.
Hi Prajesh,

You should use 2.0 in the portlet descriptor if you want to use Portlet 2.0 features.

<portlet-app id="ScatterPlot" version="2.0">

-Woonsan

--- On Tue, 8/24/10, Prajesh Bhattacharya <pb...@lbl.gov> wrote:

> From: Prajesh Bhattacharya <pb...@lbl.gov>
> Subject: Problem showing graphs in a portlet
> To: jetspeed-user@portals.apache.org
> Date: Tuesday, August 24, 2010, 12:51 AM
> Hello All,
> 
> I am very new to portlet development. I have no experience
> with servlet development either. I am trying to show a graph
> in a portlet. I am using JFreeChart library for charting.
> 
> My portlet container is JetSpeed2 (2.2.1). As a test case,
> first I tested my code with a line of text. It worked. Then
> I tried to move on to graphics, but my graphics never showed
> up in the portlet. I decided to test if my portlet could
> show a static image stored on the disk. I did not have any
> luck even with that. I have followed methodology/code
> provided at
> http://blogs.sun.com/satya/entry/new_feature_resource_serving_in
> 
> Can someone please point out the problem here? If you need
> more information, please let me know. I really appreciate
> your help.
> 
> Thanks.
> Prajesh
> 
> Following is my portlet code. Please note that I have
> tested the line
> ChartUtilities.writeChartAsJPEG(image_out, quality,
> demo.chart, 400,300);
> inside a stand-alone app in which image_out was a
> FileOutputStream.
> 
> ********************************************************
> import java.io.IOException;
> import java.io.OutputStream;
> import javax.portlet.GenericPortlet;
> import javax.portlet.PortletException;
> import javax.portlet.RenderRequest;
> import javax.portlet.RenderResponse;
> import javax.portlet.ResourceRequest;
> import javax.portlet.ResourceResponse;
> import javax.portlet.ResourceURL;
> import java.io.PrintWriter;
> 
> import org.jfree.chart.ChartUtilities;
> // import org.jfree.ui.RefineryUtilities;
> // import org.jfree.util.Log;
> // import org.jfree.util.PrintStreamLogTarget;
> 
> 
> public class ScatterPlot extends GenericPortlet {
> 
> 
> public void doView(RenderRequest request, RenderResponse
> response)
>                
>          throws
> PortletException, IOException {
> 
> //       
> Log.getInstance().addTarget(new PrintStreamLogTarget());
> 
>    response.setContentType("text/html");
>    PrintWriter writer =
> response.getWriter();
> 
>    ResourceURL resURL =
> response.createResourceURL();
>    resURL.setResourceID("image");
>    writer.println("<IMG src=\"" + resURL
> + "\" >");
> 
> }
> 
> 
> 
> 
> public void serveResource(ResourceRequest resRequest,
> ResourceResponse resResp)
>            
>    throws PortletException, IOException {
> 
>    resResp.setContentType("image/jpeg");
>    OutputStream image_out =
> resResp.getPortletOutputStream();
> 
>        TimeSeriesDemo demo = new
> TimeSeriesDemo("Time Series");
>    float quality = (float)(0.75);
>    ChartUtilities.writeChartAsJPEG(image_out,
> quality, demo.chart, 400,300);
>    image_out.flush();
>    image_out.close();
> }
> 
> } // end of class
> **********************************************************************
> 
> 
> Following is my portlet.xml.
> *********************************************************************
> <?xml version="1.0" encoding="UTF-8"?>
> 
> <portlet-app id="ScatterPlot" version="1.0">
> 
>  <portlet id="ScatterPlot_ID">
>    <description>Plot as many variables
> on the Y axis against the ONLY variable on the X
> axis</description>
>    <portlet-name>ScatterPlot_Name</portlet-name>
>    <display-name>Scatter
> Plot</display-name>
>    <portlet-class>ScatterPlot</portlet-class>
> 
>    <expiration-cache>-1</expiration-cache>
>    <supports>
>  
>    <mime-type>*/*</mime-type>
>  
>    <portlet-mode>VIEW</portlet-mode>
>  
>    <portlet-mode>HELP</portlet-mode>
>  
>    <portlet-mode>EDIT</portlet-mode>
>      <!--  support custom about
> mode -->
>  
>    <portlet-mode>about</portlet-mode>
>      <!--  support custom
> edit_defaults mode -->
>  
>    <portlet-mode>edit_defaults</portlet-mode>
>      <!--  support custom
> preview mode -->
>  
>    <portlet-mode>preview</portlet-mode>
>      <!--  support custom print
> mode -->
>  
>    <portlet-mode>print</portlet-mode>
>    </supports>
>    <supported-locale>en</supported-locale>
> 
>    <portlet-info>
>      <title>Scatter
> Plot</title>
>    </portlet-info>
> 
>  </portlet>
> 
> </portlet-app>
> *********************************************************************
> 
> 
> Following is my web.xml.
> *********************************************************************
> <?xml version="1.0" encoding="UTF-8"?>
> 
> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD
> Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
> 
> 
> <web-app>
>    <display-name>ScatterPlot</display-name>
>  <description>Scatter Plot
> Portlet</description>
> 
> </web-app>
> ***********************************************************************
> -- / *Prajesh Bhattacharya, PhD | Lawrence Berkeley
> National Laboratory | One Cyclotron Road, MS 90R3111,
> Berkeley, CA 94720-8134 | 510-486-7857 (W) | 480-586-1387
> (M) | 510-486-4089 (Fax)* /
> 


      

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-user-help@portals.apache.org