1 28 package net.sf.jasperreports.renderers; 29 30 import java.awt.geom.Rectangle2D ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.StringTokenizer ; 35 36 import net.sf.jasperreports.engine.JRConstants; 37 import net.sf.jasperreports.engine.JRException; 38 import net.sf.jasperreports.engine.JRImageMapRenderer; 39 import net.sf.jasperreports.engine.JRPrintHyperlink; 40 import net.sf.jasperreports.engine.JRPrintImageArea; 41 import net.sf.jasperreports.engine.JRPrintImageAreaHyperlink; 42 43 import org.jfree.chart.ChartRenderingInfo; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.entity.ChartEntity; 46 import org.jfree.chart.entity.EntityCollection; 47 48 49 55 public abstract class JRAbstractChartImageMapRenderer extends JFreeChartRenderer implements JRImageMapRenderer 56 { 57 58 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 59 60 public JRAbstractChartImageMapRenderer(JFreeChart chart) 61 { 62 super(chart); 63 } 64 65 66 public List getImageAreaHyperlinks(Rectangle2D renderingArea) throws JRException 67 { 68 ChartRenderingInfo renderingInfo = new ChartRenderingInfo(); 70 getChart().createBufferedImage((int) renderingArea.getWidth(), (int) renderingArea.getHeight(), renderingInfo); 71 72 EntityCollection entityCollection = renderingInfo.getEntityCollection(); 73 List areaHyperlinks = null; 74 if (entityCollection != null && entityCollection.getEntityCount() > 0) 75 { 76 areaHyperlinks = new ArrayList (entityCollection.getEntityCount()); 77 78 for (Iterator it = entityCollection.iterator(); it.hasNext();) 79 { 80 ChartEntity entity = (ChartEntity) it.next(); 81 JRPrintHyperlink printHyperlink = getEntityHyperlink(entity); 82 if (printHyperlink != null) 83 { 84 JRPrintImageArea area = getImageArea(entity); 85 86 JRPrintImageAreaHyperlink areaHyperlink = new JRPrintImageAreaHyperlink(); 87 areaHyperlink.setArea(area); 88 areaHyperlink.setHyperlink(printHyperlink); 89 areaHyperlinks.add(areaHyperlink); 90 } 91 } 92 } 93 94 return areaHyperlinks; 95 } 96 97 protected JRPrintImageArea getImageArea(ChartEntity entity) 98 { 99 JRPrintImageArea area = new JRPrintImageArea(); 100 area.setShape(JRPrintImageArea.getShape(entity.getShapeType())); 101 102 int[] coordinates = getCoordinates(entity); 103 if (coordinates != null) 104 { 105 area.setCoordinates(coordinates); 106 } 107 return area; 108 } 109 110 protected int[] getCoordinates(ChartEntity entity) 111 { 112 int[] coordinates = null; 113 String shapeCoords = entity.getShapeCoords(); 114 if (shapeCoords != null && shapeCoords.length() > 0) 115 { 116 StringTokenizer tokens = new StringTokenizer (shapeCoords, ","); 117 coordinates = new int[tokens.countTokens()]; 118 int idx = 0; 119 while (tokens.hasMoreTokens()) 120 { 121 String coord = tokens.nextToken(); 122 coordinates[idx] = Integer.parseInt(coord); 123 ++idx; 124 } 125 } 126 return coordinates; 127 } 128 129 protected abstract JRPrintHyperlink getEntityHyperlink(ChartEntity entity); 130 } 131 | Popular Tags |