1 34 35 package org.krysalis.jcharts; 36 37 38 import org.krysalis.jcharts.chartData.ChartDataException; 39 import org.krysalis.jcharts.imageMap.ImageMap; 40 import org.krysalis.jcharts.properties.ChartProperties; 41 import org.krysalis.jcharts.properties.LegendProperties; 42 import org.krysalis.jcharts.properties.PropertyException; 43 import org.krysalis.jcharts.test.HTMLGenerator; 44 45 import java.awt.*; 46 import java.awt.font.FontRenderContext ; 47 import java.awt.font.LineBreakMeasurer ; 48 import java.awt.font.TextAttribute ; 49 import java.awt.font.TextLayout ; 50 import java.awt.geom.Rectangle2D ; 51 import java.awt.image.BufferedImage ; 52 import java.io.Serializable ; 53 import java.text.AttributedString ; 54 55 56 62 public abstract class Chart implements Serializable 63 { 64 private Graphics2D graphics2D; 67 68 private int width; 69 private int height; 70 71 private Legend legend; 72 73 private ChartProperties chartProperties; 75 76 77 private boolean generateImageMap = false; 79 private ImageMap imageMap = null; 80 private BufferedImage bufferedImage = null; 81 82 83 91 public Chart( LegendProperties legendProperties, ChartProperties chartProperties, int pixelWidth, int pixelHeight ) 92 { 93 this.width = pixelWidth; 94 this.height = pixelHeight; 95 this.chartProperties = chartProperties; 96 97 if( legendProperties != null ) 98 { 99 this.legend = new Legend( this, legendProperties ); 100 legendProperties.setSize( new Dimension( width, height ) ); 101 } 102 } 103 104 105 110 public boolean getGenerateImageMapFlag() 111 { 112 return this.generateImageMap; 113 } 114 115 116 124 public BufferedImage getBufferedImage() 125 { 126 return this.bufferedImage; 127 } 128 129 130 137 public void renderWithImageMap() throws ChartDataException, PropertyException 138 { 139 this.bufferedImage = new BufferedImage ( this.getImageWidth(), this.getImageHeight(), BufferedImage.TYPE_INT_RGB ); 140 this.setGraphics2D( this.bufferedImage.createGraphics() ); 141 this.generateImageMap = true; 142 143 this.render(); 144 } 145 146 147 153 public void render() throws ChartDataException, PropertyException 154 { 155 if( this.chartProperties.useAntiAliasing() ) 156 { 157 this.graphics2D.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); 158 } 159 else 160 { 161 this.graphics2D.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF ); 162 } 163 164 165 Rectangle2D.Float rectangle = new Rectangle2D.Float ( 0, 0, this.width, this.height ); 166 167 this.graphics2D.setPaint( this.chartProperties.getBackgroundPaint() ); 169 this.graphics2D.fill( rectangle ); 170 171 if( this.chartProperties.getBorderStroke() != null ) 173 { 174 rectangle.width -= 1; 176 rectangle.height -= 1; 177 this.chartProperties.getBorderStroke().draw( this.graphics2D, rectangle ); 178 } 179 180 this.renderChart(); 182 } 183 184 185 192 protected float renderChartTitle( String chartTitle, FontRenderContext fontRenderContext ) 193 { 194 float height = 0; 195 196 if( chartTitle != null ) 197 { 198 float currentLine = this.getChartProperties().getEdgePadding(); 200 201 AttributedString s = new AttributedString ( chartTitle ); 203 s.addAttribute( TextAttribute.FONT, this.getChartProperties().getTitleFont().getFont() ); 204 205 LineBreakMeasurer measurer = new LineBreakMeasurer ( s.getIterator(), fontRenderContext ); 207 208 this.getGraphics2D().setPaint( this.getChartProperties().getTitleFont().getPaint() ); 210 211 float wrappingWidth= this.getImageWidth() - ( this.getChartProperties().getEdgePadding() * 2 ); 213 214 TextLayout titleTextLayout= null; 215 while( ( titleTextLayout = measurer.nextLayout( wrappingWidth ) ) != null ) 216 { 217 currentLine += titleTextLayout.getAscent(); 219 220 titleTextLayout.draw( this.getGraphics2D(), 221 ( ( this.getImageWidth() - titleTextLayout.getAdvance() ) / 2 ), 222 currentLine ); 223 224 height += titleTextLayout.getAscent() + titleTextLayout.getDescent(); 226 } 227 228 height += this.getChartProperties().getTitlePadding(); 230 } 231 232 return height; 233 } 234 235 236 241 abstract protected void renderChart() throws ChartDataException, PropertyException; 242 243 244 249 public final int getImageWidth() 250 { 251 return this.width; 252 } 253 254 255 260 public final int getImageHeight() 261 { 262 return this.height; 263 } 264 265 266 271 public final ChartProperties getChartProperties() 272 { 273 return this.chartProperties; 274 } 275 276 277 282 protected final Legend getLegend() 283 { 284 return this.legend; 285 } 286 287 288 293 public final boolean hasLegend() 294 { 295 return this.legend != null; 296 } 297 298 299 304 public final void setGraphics2D( Graphics2D graphics2D ) 305 { 306 this.graphics2D = graphics2D; 307 } 308 309 310 318 public final Graphics2D getGraphics2D() 319 { 320 return this.graphics2D; 321 } 322 323 324 330 public final void setImageMap( ImageMap imageMap ) 331 { 332 this.imageMap = imageMap; 333 } 334 335 336 340 public final ImageMap getImageMap() 341 { 342 return this.imageMap; 343 } 344 345 346 353 public void toHTML( HTMLGenerator htmlGenerator, String imageFileName, ImageMap imageMap ) 354 { 355 htmlGenerator.chartTableStart( this.getClass().getName(), imageFileName, imageMap ); 356 357 htmlGenerator.chartTableRowStart(); 358 this.chartProperties.toHTML( htmlGenerator ); 359 htmlGenerator.chartTableRowEnd(); 360 361 if( this.legend != null ) 362 { 363 htmlGenerator.chartTableRowStart(); 364 this.getLegend().toHTML( htmlGenerator ); 365 htmlGenerator.chartTableRowEnd(); 366 } 367 368 htmlGenerator.chartTableEnd(); 369 } 370 371 372 } 373 | Popular Tags |