This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • JFreeChart and wicket example
Skip to end of metadata
Go to start of metadata

This page shows an example of how to use JFreeChart with wicket.

PieChartExamplePage.html

<html>
<body>
<img wicket:id="image">
</body>
</html>

PieChartExamplePage.java

import org.apache.wicket.markup.html.WebPage;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class PieChartExamplePage extends WebPage {

	public PieChartExamplePage() {
		DefaultPieDataset d = new DefaultPieDataset();
		d.setValue("JavaWorld", new Integer(75));
		d.setValue("Other", new Integer(25));
		JFreeChart chart = ChartFactory.createPieChart("Sample Pie Chart", d,
                 true,		// Show legend  
                 true,		// Show tooltips
                 true);		// Show urls
        //chart.setBackgroundPaint(Color.white);
        //chart.setBorderVisible(false);
		add(new JFreeChartImage("image", chart, 300, 300));
	}

}

JFreeChartImage.java

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.request.resource.DynamicImageResource;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.http.WebResponse;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

public class JFreeChartImage extends Image {
	
    private final int width;
    private final int height;

	public JFreeChartImage(String id, JFreeChart chart, int width, int height){
		super(id, new Model(chart));
		this.width = width;
		this.height = height;
	}

	@Override
    protected DynamicImageResource getImageResource() {
        return new DynamicImageResource(){
			@Override
			protected byte[] getImageData() {
		        JFreeChart chart = (JFreeChart)getDefaultModelObject();
		        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);
		        }
		    }
        };
    }

}

No-Cache for Wicket 1.5

    @Override
    protected DynamicImageResource getImageResource() {
        DynamicImageResource resource = new DynamicImageResource() {

            @Override
            protected byte[] getImageData(final Attributes attributes) {
                JFreeChart chart = (JFreeChart) getDefaultModelObject();
                return toImageData(chart.createBufferedImage(width, height));
            }

            @Override
            protected void configureResponse(final ResourceResponse response, final Attributes attributes) {
                super.configureResponse(response, attributes);

                if (isCacheAble() == false) {
                    response.setCacheDuration(Duration.NONE);
                    response.setCacheScope(CacheScope.PRIVATE);
                }
            }

        };

        return resource;
    }
  • No labels