Updates from July, 2007 Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 7:16 am on July 10, 2007 Permalink |
    Tags:   

    Displaying dynamic charts in a web page (using JFreeChart & JSP) 

    This code snippet will tell you how to create a chart (using JFreeChart) on the fly and displaying it in a webpage.

    getChart.jsp

    < %@ page import="java.io.*" %>
    < %@ page import="org.jfree.chart.JFreeChart" %>
    < %@ page import="org.jfree.chart.ChartUtilities" %>
    
    < %
     try
     {
     File image = File.createTempFile("image", "tmp");
    
     // Assume that we have the chart
     ChartUtilities.saveChartAsPNG(image, chart, 500, 300);
    
     FileInputStream fileInStream = new FileInputStream(image);
     OutputStream outStream = response.getOutputStream();   
    
     long fileLength;
     byte&#91;&#93; byteStream;
    
     fileLength = image.length();
     byteStream = new byte&#91;(int)fileLength&#93;;
     fileInStream.read(byteStream, 0, (int)fileLength);
    
     response.setContentType("image/png");
     response.setContentLength((int)fileLength);
     response.setHeader("Cache-Control", 
         "no-store,no-cache, must-revalidate, post-check=0, pre-check=0");
     response.setHeader("Pragma", "no-cache");
    
     fileInStream.close();
     outStream.write(byteStream);
     outStream.flush();
     outStream.close();
    
     }
     catch (IOException e)
     {
     System.err.println("Problem occurred creating chart.");
     }
    
    %>
    

    How to use it in a webpage:

    <img src="getChart.jsp" alt="Progress chart" />
    

    We can make it more dynamic using various parameters. See the sample below.

    <img src="getChart.jsp?type=bar&action=showProfits" alt="Progress chart" />
    

    Useful links: creating charts using JFreeChart
    Creating BarChart with custom colors using JFreeChart
    Creating a 3D BarChart using JFreeChart
    Creating Time Series charts using JFreeChart
    Creating Pie Chart using JFreeChart (Using plot)
    Creating Pie Chart using JFreeChart

    Advertisements
     
    • pchopr3 4:52 am on August 27, 2009 Permalink

      Hey Subin,

      Thanks for posting the code. I have a problem:
      I tried using ur code in eclipse after adding jcommon jar files. but the imported library was not found.

      Can u suggest wht can be the issue.

      Thnks,
      PC

    • pchopr3 4:56 am on August 27, 2009 Permalink

      I am using jcommon 1.0.16.jar

    • Subinkrishna G 9:51 am on August 27, 2009 Permalink

      Hi,

      I wrote it almost 2 years back. Then I was using jcommon-1.0.10.jar along with jfreechart-1.0.6.jar, jfreechart-1.0.6-experimental.jar and jfreechart-1.0.6-swt.jar. And I’m not sure whether they’ve changed anything after that. Hope this helps.

      Subin

    • pchopr3 5:37 am on August 28, 2009 Permalink

      okie, i will try with them…thnx

    • kalmud 11:37 am on August 5, 2011 Permalink

      hi Subinkrishna G,
      ur example is nice.
      can u give me code for ScatterPlot in jsp.

      thank u,

  • Subinkrishna Gopi 10:35 am on June 27, 2007 Permalink |  

    Creating custom downloads using JSP 

    This code snippet will help you to create custom/runtime downloads upon user request. In this code snippet, the code will create a download for an image in the server.

    String fileName = config.getServletContext().getRealPath("") + "image.jpg"; 
    String displayFileName = "myFile.jpg"; 
    long fileLength; 
    byte[] byteStream;
    File fileToDownload = new File(fileName); 
    FileInputStream fileInStream = new FileInputStream(fileToDownload); 
    OutputStream outStream = response.getOutputStream();
    fileLength = fileToDownload.length(); 
    byteStream = new byte[(int)fileLength]; 
    fileInStream.read(byteStream, 0, (int)fileLength);
    response.setContentType("image/jpg"); 
    response.setContentLength((int)fileLength); 
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); 
    response.setHeader("Pragma", "no-cache"); 
    response.setHeader("Content-Disposition", "attachment; filename=" + displayFileName);
    fileInStream.close(); 
    outStream.write(byteStream); 
    outStream.flush();

    Here, evenif the browser can display the JPEG image coming from the the server as the response, will ask the user to either “Open” or “Save” it. B’ coz we are setting the  “Content-Disposition” header to “attachment”.

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel