Coverage Report - net.sf.statcvs.charts.ChartImage
 
Classes in this File Line Coverage Branch Coverage Complexity
ChartImage
0%
0/25
0%
0/2
1.5
 
 1  
 package net.sf.statcvs.charts;
 2  
 
 3  
 import java.awt.Color;
 4  
 import java.awt.Dimension;
 5  
 import java.io.File;
 6  
 import java.io.IOException;
 7  
 import java.util.logging.Logger;
 8  
 
 9  
 import org.jfree.chart.ChartUtilities;
 10  
 import org.jfree.chart.JFreeChart;
 11  
 
 12  
 /**
 13  
  * An image file for a chart.
 14  
  * 
 15  
  * TODO: Better integrate all charts with ReportConfig 
 16  
  * @author jentzsch
 17  
  * @author Richard Cyganiak (richard@cyganiak.de)
 18  
  * @version $Id: ChartImage.java,v 1.6 2008/04/02 11:22:15 benoitx Exp $
 19  
  */
 20  
 public class ChartImage {
 21  0
     private final static Logger logger = Logger.getLogger("sf.net.statcvs");
 22  
 
 23  0
     private final static Color BACKGROUND_COLOR = new Color(204, 204, 187);
 24  
 
 25  
     private final String rootDirectory;
 26  
     private JFreeChart chart;
 27  
     private final String fileName;
 28  
     private final Dimension size;
 29  
     private final String title;
 30  0
     private boolean written = false;
 31  
 
 32  
     /**
 33  
      * Creates a new ChartFile.
 34  
      * @param rootDirectory The report root directory with trailing slash
 35  
      * @param fileName The relative file name for the chart, with .png extension
 36  
      * @param title The chart's title
 37  
      * @param chart The JFreeChart object to save as a file
 38  
      * @param size width and height of the chart in pixels
 39  
      */
 40  0
     public ChartImage(final String rootDirectory, final String fileName, final String title, final JFreeChart chart, final Dimension size) {
 41  0
         this.rootDirectory = rootDirectory;
 42  0
         this.fileName = fileName;
 43  0
         this.title = title;
 44  0
         this.chart = chart;
 45  0
         this.size = size;
 46  0
         chart.setBackgroundPaint(BACKGROUND_COLOR);
 47  0
     }
 48  
 
 49  
     /**
 50  
      * Writes the chart to disk as a PNG file.
 51  
      */
 52  
     public void write() {
 53  0
         if (this.written) {
 54  0
             return;
 55  
         }
 56  0
         logger.info("writing chart '" + this.title + "' to " + this.fileName);
 57  
         try {
 58  0
             ChartUtilities.saveChartAsPNG(new File(rootDirectory + fileName), chart, size.width, size.height);
 59  0
         } catch (final IOException e) {
 60  0
             logger.warning("could not write chart '" + fileName + "': " + e);
 61  0
         }
 62  0
         this.written = true;
 63  0
         this.chart = null; // Free memory? Not sure if this has any effect ... 
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Returns the chart's URL, relative to the report root.
 68  
      */
 69  
     public String getURL() {
 70  0
         return this.fileName;
 71  
     }
 72  
 
 73  
     /**
 74  
      * Returns the chart's title.
 75  
      */
 76  
     public String getFullTitle() {
 77  0
         return this.title;
 78  
     }
 79  
 
 80  
     /**
 81  
      * Returns the chart's width in pixels.
 82  
      */
 83  
     public int getWidth() {
 84  0
         return this.size.width;
 85  
     }
 86  
 
 87  
     /**
 88  
      * Returns the chart's height in pixels.
 89  
      */
 90  
     public int getHeight() {
 91  0
         return this.size.height;
 92  
     }
 93  
 }