To: [email protected]
From: [email protected]
Subject: Re: Updating a Dynamic Image with AJAX (and JFreeChart)
Date: Fri, 13 Nov 2009 23:31:39 -0500
Look at the source of the DynamicImageResource class. The getResourceState
method does something like (sorry for the lame pseudocode) 'if image data is
null then save and return value of getImageData else return the previous image
data'. So its gonna call your getImageData() method once and save the value.
This falls in line with the super class's (DynamicWebResource) javadoc that
says:
very useful for things you generate dynamically, but reuse for a while after
that. If you need resources that stream directly and are not cached, extend
WebResource directly and implement Resouce.getResourceStream() yourself.
It has nothing to do with HTTP caching, which it looks like you're trying to
solve with your headers, but server-side caching. Anyways... in this case,
getResourceStream of WebResource ends up calling getResourceState of
DynamicImageResource. All you need to do is is override getResourceState (and
therefore kind of overrides the behavior of getResourceStream) of your
DynamicImageResource and ensure that it doesn't cache the result of
getImageData() and you should be set.
Craig
_____
From: wicketnewuser [mailto:[email protected]]
To: [email protected]
Sent: Fri, 13 Nov 2009 20:32:05 -0500
Subject: Re: Updating a Dynamic Image with AJAX (and JFreeChart)
I have same situation. I'm not able to refresh my image. But if i view the
image i do get refreshed image
Here is my code. Based on my dropdownchoice it should make new Jfreechart
and the image should refresh. I couldn't get it working so i wrapped the
image with in a span but it still doesn't work. TugboatChartImage extends
NonCachingImage . Can any one point out what I'm doing wrong
Chart chart1 = new Chart(this.getString(column1.toString()), "Date",
"Dollars");
final String yAxisType = "linear";
final int smallChartWidth=400;
final int smallChartHeight=200;
JFreeChart jfChartOne = chart1.render(chartOneCollection, null, yAxisType,
smallChartWidth, smallChartHeight);
// make an image
final TugboatChartImage imageOne = new TugboatChartImage("chart1image",
jfChartOne, smallChartWidth, smallChartHeight);
final WebMarkupContainer chart1Span = new
WebMarkupContainer("chart1Span");
chart1Span.add(imageOne);
add(chart1Span);
// draw chart 2
Chart chart2 = new Chart(this.getString(column2.toString()), "Date",
"Count");
JFreeChart jfChartTwo = chart2.render(chartTwoCollection, null, yAxisType,
smallChartWidth, smallChartHeight);
// make an image
TugboatChartImage imageTwo = new TugboatChartImage("chart2image",
jfChartTwo, smallChartWidth, smallChartHeight);
add(imageTwo);
String filterStringList ="";
if (filterStringList!= null){
filterStringList =
report.getFilterParameterList().toString().replaceAll("\\[", "");
filterStringList = filterStringList.replaceAll("\\]", "");
}
final DropDownChoice<TugboatReportData.ColumnName> chart1Select = new
DropDownChoice<TugboatReportData.ColumnName>("chart1Select" ,new
PropertyModel(this, "column1"), TugboatReportData.trafficColumns,new
IChoiceRenderer() {
public Object getDisplayValue(Object obj) {
//this.getString give you value from the propertyfile
return
ReportResultsPage.this.getString(((TugboatReportData.ColumnName)
obj).toString());
}
public String getIdValue(Object obj, int index) {
return obj.toString();
}
});
chart1Select.add(new AjaxFormComponentUpdatingBehavior("onchange") {
protected void onUpdate(AjaxRequestTarget target) {
sortByColumn(displayRowSetFinal, column1);
Chart chart1R = new
Chart(ReportResultsPage.this.getString(column1.toString()), "Date",
"Dollars");
SwishTimeSeriesCollection chartOneCollectionR =
createChartCollection(report, originalRowSet, displayRowSetFinal.subList(0,
(originalRowSet.size() > 10) ? 9 : originalRowSet.size() - 1), column1);
logger.error(displayRowSetFinal);
JFreeChart jfChartOneR = chart1R.render(chartOneCollectionR, null,
yAxisType, smallChartWidth, smallChartHeight);
imageOne.setDefaultModelObject(jfChartOneR);
imageOne.modelChanged();
chart1Span.modelChanged();
chart1Span.setOutputMarkupId(true);
imageOne.setOutputMarkupId(true);
target.addComponent(imageOne);
target.addComponent(chart1Span);
}
});
Jason Mihalick wrote:
>
> I am integrating JFreeChart into my application and I've followed the
> JFreeChart example on the wiki. That works very nicely. I was impressed.
> What I want to do now is update the graph that I display based on the
> selection from a drop down list. I've successfully added the
> DropDownChoice component and added a AjaxFormComponentUpdatingBehavior to
> it. I've verified that my onUpdate( AjaxRequestTarget ) method is being
> invoked and I'm receiving the updated value, which is very cool. Here is
> my onUpdate implementation for the DropDownChoice control:
>
> protected void onUpdate( AjaxRequestTarget target ) {
> log.debug( "graphStyles onUpdate invoked! SelectedGraphStyle = " +
> FieldResultsPanel.this.getSelectedGraphStyle() );
> FieldResultsPanel.this.updateGraph(); // Update the model and re-create
> the JFreeChart Image
>
> // fieldGraph is my JFreeChartImage instance (derived from Image)
> // This is supposed to indicate back to the browser that the image needs
> updated. I think this
> // is working properly
> target.addComponent( FieldResultsPanel.this.fieldGraph );
> }
>
> Even though I am updating the model of my JFreeChartImage, it appears that
> the getImageData method is never invoked afterwards.
>
> What do I need to do in order to get the image data to be re-read?
>
> Here is my JFreeChartImage class:
>
> public class JFreeChartImage extends Image {
>
> private static final Logger log = LoggerFactory.getLogger(
> JFreeChartImage.class );
>
> private int width;
> private int height;
>
> public JFreeChartImage( String id ) {
> super( id );
> }
>
> public JFreeChartImage( String id, int width, int height ) {
> this( id );
> this.width = width;
> this.height = height;
> }
>
> public JFreeChartImage( String id, JFreeChart chart, int width, int
> height ) {
> super(id, new Model( chart ) );
> this.width = width;
> this.height = height;
> }
>
> public Component setModel( JFreeChart chart ) {
> log.debug( "setModel invoked with chart: " + chart );
> return super.setModel( new Model( chart ) );
> }
>
> @Override
> protected Resource getImageResource() {
>
> return new DynamicImageResource(){
> private static final long serialVersionUID = 1L;
>
> @Override
> protected byte[] getImageData() {
> log.debug( "Invoking getImageData..." );
> JFreeChart chart = (JFreeChart)getModelObject();
> log.debug( "Chart object: " + chart );
> return toImageData( chart.createBufferedImage( width, height ) );
> }
>
> @Override
> protected void setHeaders( WebResponse response ) {
> if ( isCacheable() ) {
> super.setHeaders(response);
> } else {
> response.setHeader( "Pragma", "no-cache" );
> response.setHeader( "Cache-Control", "no-cache" );
> response.setDateHeader( "Expires", 0 );
> }
> }
> };
> }
>
> Any help is much appreciated!
>
> --
> Jason
>
--
View this message in context: http://old.nabble.com/Updating-a-Dynamic-Image-with-AJAX-%28and-JFreeChart%29-tp12884455p26346217.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]