You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

This page details a component that integrates a JFreeChart chart and wicket producing a clickable imagemap integrated with a wicket AjaxLink, and tooltips specified via the tooltip generator of the JFreeChart.

The major component used by simply constructing and adding to your parent panel is MappedChart:

Markup:

MappedChart.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmnls="http://www.w3.org/1999/xhtml" xmnls:wicket="http://wicket.apache.org">
	<wicket:panel>
		<img wicket:id="image" />
		<map wicket:id="imageMap" >
			<area wicket:id = "areas" />
		</map>
	</wicket:panel>
</html>

Java Code:

MappedChart.java

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.EntityCollection;

/**
 * Component that produces an image and associated image map from
 * the given JFreeChart chart. Uses the JFreeChart tooltip generator
 * to provide tooltips for the chart entities but does not use
 * the JFreeChart URL generator, but instead calls an Ajax callback
 * function/
 * 
 * @author Jonny Wray
 *
 */
public abstract class MappedChart extends Panel{
	private static final long serialVersionUID = 4137002187344769160L;

	public MappedChart(String panelId, JFreeChart chart, int width, int height){
		super(panelId);
		ChartImage image = new ChartImage("image", chart, width, height);
		String mapName = getPath();
		image.add(new AttributeModifier("usemap", true, new Model("#"+mapName)));
		add(image);
		DynamicImageMap imageMap = constructImageMap(image, mapName);
		add(imageMap);
	}
	
	/**
	 * The callback method that is called when a specific image map entity is 
	 * clicked on. 
	 * 
	 * @param target
	 * @param entity
	 */
	protected abstract void onClickCallback(AjaxRequestTarget target, ChartEntity entity);
	
	private DynamicImageMap constructImageMap(ChartImage image, String mapName){
		DynamicImageMap imageMap = new DynamicImageMap("imageMap", mapName);
        EntityCollection entities = image.getRenderingInfo().getEntityCollection();
        if (entities != null) {
            int count = entities.getEntityCount();
            for (int i = count - 1; i >= 0; i--) {
                final ChartEntity entity = entities.getEntity(i);
                imageMap.addArea(entity.getShapeType(), entity.getShapeCoords(), entity.getToolTipText(), new AjaxLink("link"){
					private static final long serialVersionUID = -7982198051678987986L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						onClickCallback(target, entity);
					}
                });
            }
        }
        return imageMap;
	}
}

  • No labels