Same chart is being displayed every time-ITS urgent!

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
lukkumar
Posts: 17
Joined: Thu Jun 07, 2007 8:10 pm

Same chart is being displayed every time-ITS urgent!

Post by lukkumar » Mon Jul 02, 2007 9:45 pm

Hi,
I have used frames with a left frame containing menu and right frame displaying the chart. When I click the links on the menu the chart would be rendered on the right frame. The chart that I get for the first click, this is the same chart that I am getting on the right frame for every other click on the menu. I have see the 'View Source' of the browser, the map info is that of the new chart but the chart that was displayed for the first click is being displayed again and again.

Please help.

Here is the index.jsp:
<frameset cols="210,*" frameborder=0>
<frame src="xpmenu.jsp">
<frame src="welcome.jsp" name="showframe">
</frameset>

Here is the xpmenu.jsp:
<div style="float: left" class="navbar">
<!-- *********************************Start Menu****************************** -->
<div class="mainDiv" >
<div class="topItem" >call status on X axis</div>
<div class="dropMenu" ><!-- -->
<div class="subMenu" style="display:inline;">
<div class="subItem"><a href="<%=request.getContextPath()%>/displaychart.jsp?type=bar" target ="showframe">3-D Bar View</a></div>
</div>
</div>
</div>
<!-- *********************************End Menu****************************** -->
<br>
<!-- *********************************Start Menu****************************** -->
<div class="mainDiv" >
<div class="topItem" >Date of X axis</div>
<div class="dropMenu" ><!-- -->
<div class="subMenu" style="display:inline;">
<div class="subItem"><a href="<%=request.getContextPath()%>/displaychart.jsp?type=stackedline" target ="showframe">Line View</a></div>
<div class="subItem"><a href="<%=request.getContextPath()%>/displaychart.jsp?type=stackedarea" target ="showframe">Stacked Area View</a></div>
</div>
</div>
</div>
<!-- *********************************End Menu****************************** -->
<br>
<div class="mainDiv" >
<div class="topItem" >Calendar</div>
<div class="dropMenu" ><!-- -->
<div class="subMenu" style="display:inline;">
<div id="calendar-container"></div>
</div>
</div>
</div>
<script type="text/javascript" src="xpmenuv21.js"></script>
</div>

Here is the displaychart.jsp:

<body>
<center>
<%
JFreeChart chartobj = null;
String altname = "";
String chartdesc="";
String chartquery="";
String charttype = request.getParameter("type").toString();
BufferedImage bufimage = null;
// get ImageMap
ChartRenderingInfo info = new ChartRenderingInfo();
String imageMap = null;
if(charttype.equals("bar"))
{
chartobj = CreateChartClass.generateBarChart();
altname = "result cnt order by call status cd";
session.setAttribute("title","call status");
session.setAttribute("cname1","Call Status CD");
session.setAttribute("cname2","Result Count");
}
else if(charttype.equals("stackedline"))
{
chartobj = CreateChartClass.generateStackedAreaLineChart("stackedline");
altname = "Test Application on lead_fact";
}
else if(charttype.equals("stackedarea"))
{
chartobj = CreateChartClass.generateStackedAreaLineChart("stackedarea");
}
//populate the info
bufimage = chartobj.createBufferedImage(600, 300, info);
imageMap = ChartUtilities.getImageMap( "map", info );
session.removeAttribute("chartobj");
session.setAttribute( "chartobj", chartobj );

session.removeAttribute("bufimage");
session.setAttribute( "bufimage", bufimage );

%>

<br>
<%= imageMap%>

<IMG src="<%=request.getContextPath()%>/chartviewer" usemap="#map" alt="<%=altname%>" border=1/>
<%=chartdesc%>
<%=chartquery%>

</center>

</body>
</html>


Here is the ChartViewer servelt that streams the chart to the output:
HttpSession session = request.getSession();

// get the chart from storage
JFreeChart chart = (JFreeChart) session.getAttribute( "chartobj" );
// set the content type so the browser can see this as it is
response.setContentType( "image/png" );
// send the picture
BufferedImage buf = chart.createBufferedImage(600, 300, null);
PngEncoder encoder = new PngEncoder( buf, false, 0, 9 );
response.getOutputStream().write( encoder.pngEncode() );


Please help it is very urgent.

Thanks,
Laks.

joolz
Posts: 56
Joined: Thu Nov 17, 2005 2:38 am
Location: Australia

Post by joolz » Tue Jul 03, 2007 4:36 am

It seems the web browser is caching the image, and not reconising that the image is acutally different. Are you using firefox? When I started with dynamic images, firefox would refuse to download the updated image without some pushing.

Anyway, there a few things you can do:
1. Right click on the right frame, push down Ctrl+Shift and click on Reload (ff) or Refresh (ie) - this forces the browser download the new image

2. Tell the web browser to not cache the image via code:

Code: Select all

JFreeChart chart = (JFreeChart) session.getAttribute( "chartobj" ); 
// set the content type so the browser can see this as it is 
response.setContentType( "image/png" ); 


// Add this:
response.setHeader("Expires","0");
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");


// send the picture 
BufferedImage buf = chart.createBufferedImage(600, 300, null); 
PngEncoder encoder = new PngEncoder( buf, false, 0, 9 ); 
response.getOutputStream().write( encoder.pngEncode() ); 
3. Get the jsp to add a parameter to the image url that is unique so that it thinks it is a different image

If doing 1 fixes your problem, implement 2 or 3 (I recommend 2)

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Post by RichardWest » Tue Jul 03, 2007 7:31 am

joolz wrote:It seems the web browser is caching the image, and not reconising that the image is acutally different. Are you using firefox? When I started with dynamic images, firefox would refuse to download the updated image without some pushing.
While suspect the browser cache as well, another possibility is the proxy server cache. If you are running this applet within a corporate intranet, it is possible the proxy is just caching the image. The suggestions of joolz will certainly work for the browser cache, but they may not work on the proxy depending upon the settings. I would suggest you add something to the image just to be sure if joolz suggestions do not work work (they should).
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

lukkumar
Posts: 17
Joined: Thu Jun 07, 2007 8:10 pm

The chart started reloading

Post by lukkumar » Tue Jul 03, 2007 4:50 pm

Hi,
Yes, you were right that the servlet URL has been same so the browser was rendering the same Chart. I tried the option 3 and it worked. I even tried option 2 but it din't work.
Any idea why the option 2 did not work.
I am using IE6.0 and using a BEA Weblogic Server 8.1

Thanks,
Laks.

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Re: The chart started reloading

Post by RichardWest » Tue Jul 03, 2007 5:21 pm

lukkumar wrote:Any idea why the option 2 did not work.
If option 2 did not work, you probably have a proxy server which is also caching the images. Option 3 tricks the proxy just like it tricks the browser. Irritating behavior for you, but it does allow faster access to common sites.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

joolz
Posts: 56
Joined: Thu Nov 17, 2005 2:38 am
Location: Australia

Post by joolz » Wed Jul 04, 2007 12:37 am

Can you confirm if there is there a proxy server involved?

I'm a bit surprised that #2 doesn't work. I had the same problems, and found that setting those headers did the trick. I am using tomcat6, though that shouldn't make a difference.

I'm not surprised #3 worked, but it will use more disk space in your browser cache, making it a less ideal solution than #2, and is why I mentioned it last.

jwenting
Posts: 157
Joined: Sat Jul 15, 2006 7:46 am

Re: The chart started reloading

Post by jwenting » Wed Jul 04, 2007 9:47 am

RichardWest wrote:
lukkumar wrote:Any idea why the option 2 did not work.
If option 2 did not work, you probably have a proxy server which is also caching the images. Option 3 tricks the proxy just like it tricks the browser. Irritating behavior for you, but it does allow faster access to common sites.
Or should. Not all proxy servers play nice with headers telling them not to cache, I've encountered (though long ago) at least one that cached everything, no-cache headers or no.

aaa
Posts: 19
Joined: Thu Jul 03, 2008 3:12 pm

Post by aaa » Tue Jul 08, 2008 11:06 am

joolz wrote:It seems the web browser is caching the image, and not reconising that the image is acutally different. Are you using firefox? When I started with dynamic images, firefox would refuse to download the updated image without some pushing.

Anyway, there a few things you can do:
1. Right click on the right frame, push down Ctrl+Shift and click on Reload (ff) or Refresh (ie) - this forces the browser download the new image

2. Tell the web browser to not cache the image via code:

Code: Select all

JFreeChart chart = (JFreeChart) session.getAttribute( "chartobj" ); 
// set the content type so the browser can see this as it is 
response.setContentType( "image/png" ); 


// Add this:
response.setHeader("Expires","0");
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");


// send the picture 
BufferedImage buf = chart.createBufferedImage(600, 300, null); 
PngEncoder encoder = new PngEncoder( buf, false, 0, 9 ); 
response.getOutputStream().write( encoder.pngEncode() ); 
3. Get the jsp to add a parameter to the image url that is unique so that it thinks it is a different image

If doing 1 fixes your problem, implement 2 or 3 (I recommend 2)
hi joolz and jfree teams

I am not using response object instead we are using action forward of struts and ajax
so i cannot use 2 method and i dont want to do it manually so i am not preparing to use 1 one also.

but then how to use 3? any example please?

my problem is i created one dash board using charts and graphs which is generated by jfreechart.
after making the data available for the graphs and charts, i saw the dash board still it is showing the old items. but it has created the images its available in the path.
The problem is, its not loaded the latest image
so what i need to do?

Regards
Vaaji

joolz
Posts: 56
Joined: Thu Nov 17, 2005 2:38 am
Location: Australia

Post by joolz » Tue Jul 08, 2008 11:52 pm

In the code the builds/serves the HTML, construct the URL of the image so that the resulting HTML looks like this:

Code: Select all

<img src="chart.png?r=1215556634215" />
where 1215556634215 is a uniquely generated number, which could be generated with something like:

Code: Select all

System.currentTimeMillis()
Every time you refresh the page, the r parameter will be different, causing the browser to always download the image from the server

aaa
Posts: 19
Joined: Thu Jul 03, 2008 3:12 pm

Post by aaa » Wed Jul 09, 2008 12:28 pm

thank you joolz,

but i did it another way by adding r with image name.(this solves one more of my problem, thats why i am using this method)
that is every time when i refresh the page it will generate new image
and i point it in the jsp so that the browser caching problem is solved

but i need to delete the old images, that i am trying to do it now any idea?
(ChartDeleter is trying to delete the charts in tmp only - i think so)

joolz
Posts: 56
Joined: Thu Nov 17, 2005 2:38 am
Location: Australia

Post by joolz » Wed Jul 09, 2008 11:18 pm

I presume you mean delete the old images from the client web browser. It shouldn't be too much of a problem as IE and Firefox both have limits on how big the temporary internet file cache is. I'm not aware of any programatic method of clearing the cache either; the user must clear it manually.

Locked