Coverage Report - net.sf.statcvs.charts.ChartConfigUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ChartConfigUtil
0%
0/77
0%
0/28
2.111
 
 1  
 /**
 2  
  * 
 3  
  */
 4  
 package net.sf.statcvs.charts;
 5  
 
 6  
 import java.awt.BasicStroke;
 7  
 import java.awt.Color;
 8  
 import java.awt.Dimension;
 9  
 import java.awt.Font;
 10  
 import java.awt.Image;
 11  
 import java.net.MalformedURLException;
 12  
 import java.net.URL;
 13  
 
 14  
 import javax.swing.ImageIcon;
 15  
 
 16  
 import net.sf.statcvs.output.ConfigurationOptions;
 17  
 import net.sf.statcvs.util.StringUtils;
 18  
 
 19  
 import org.jfree.chart.JFreeChart;
 20  
 import org.jfree.chart.plot.Plot;
 21  
 import org.jfree.chart.renderer.xy.XYItemRenderer;
 22  
 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
 23  
 import org.jfree.chart.title.TextTitle;
 24  
 import org.jfree.data.xy.XYDataset;
 25  
 import org.jfree.ui.Align;
 26  
 import org.jfree.ui.HorizontalAlignment;
 27  0
 import org.jfree.ui.RectangleEdge;
 28  0
 
 29  
 /**
 30  
  * @author benoitx
 31  
  *
 32  
  */
 33  
 public final class ChartConfigUtil {
 34  0
     private ChartConfigUtil() {
 35  0
     }
 36  
 
 37  
     /**
 38  
      * returns the background color from the config file, tries properties:
 39  0
      * <pre>
 40  
      * chart.&lt;chartName&gt;.backgroundColor
 41  
      * chart.backgroundColor
 42  
      * </pre>
 43  
      * @param chartName
 44  
      */
 45  
     public static Color getBackgroundColor(final String chartName) {
 46  0
         return ConfigurationOptions.getConfigColorProperty("chart." + chartName + ".backgroundColor", "chart.backgroundColor", ChartImage.BACKGROUND_COLOR);
 47  
     }
 48  
 
 49  
     /**
 50  
      * returns the PLOT color from the config file, tries properties:
 51  0
      * <pre>
 52  
      * chart.&lt;chartName&gt;.plotColor
 53  
      * chart.plotColor
 54  
      * </pre>
 55  
      * @param chartName
 56  
      */
 57  
     public static Color getPlotColor(final String chartName) {
 58  0
         return ConfigurationOptions.getConfigColorProperty("chart." + chartName + ".plotColor", "chart.plotColor", ChartImage.BACKGROUND_COLOR);
 59  
     }
 60  
 
 61  
     /**
 62  
      * returns the PLOT color from the config file, tries properties:
 63  
      * <pre>
 64  
      * chart.&lt;chartName&gt;.width
 65  0
      * chart.width
 66  
      * chart.&lt;chartName&gt;.height
 67  
      * chart.height
 68  0
      * </pre>
 69  
      * @param chartName
 70  
      */
 71  0
     public static Dimension getDimension(final String chartName, final Dimension defaultDimension) {
 72  0
         final Integer width = ConfigurationOptions
 73  
                 .getConfigIntegerProperty("chart." + chartName + ".width", "chart.width", new Integer(defaultDimension.width));
 74  
 
 75  0
         final Integer height = ConfigurationOptions.getConfigIntegerProperty("chart." + chartName + ".height", "chart.height", new Integer(
 76  
                 defaultDimension.height));
 77  
 
 78  0
         return new Dimension(width.intValue(), height.intValue());
 79  
     }
 80  
 
 81  
     /**
 82  
      * configure the lines for the chart, tries properties:
 83  0
      * <pre>
 84  0
      * chart.&lt;chartName&gt;.lineStroke
 85  0
      * chart.lineStroke
 86  0
      * </pre>
 87  
      * @param chartName
 88  
      */
 89  0
     public static void configureStroke(final String chartName, final XYItemRenderer renderer, final XYDataset data) {
 90  0
         final Float stroke = ConfigurationOptions.getConfigFloatProperty("chart." + chartName + ".lineStroke", "chart.lineStroke", null);
 91  0
         if (stroke != null) {
 92  0
             for (int i = 0; i < data.getSeriesCount(); i++) {
 93  0
                 renderer.setSeriesStroke(i, new BasicStroke(stroke.floatValue()));
 94  
             }
 95  
         }
 96  0
     }
 97  
 
 98  
     /**
 99  
      * configure the shapes for the chart (if renderer is of type XYLineAndShapeRenderer) , tries properties:
 100  0
      * <pre>
 101  0
      * chart.&lt;chartName&gt;.showShapes
 102  0
      * chart.filledShapes
 103  
      * </pre>
 104  
      * @param chartName
 105  0
      */
 106  0
     public static void configureShapes(final String chartName, final XYItemRenderer renderer) {
 107  0
         if (renderer instanceof XYLineAndShapeRenderer) {
 108  0
             final Boolean showShapes = ConfigurationOptions.getConfigBooleanProperty("chart." + chartName + ".showShapes", "chart.showShapes", Boolean.FALSE);
 109  0
             final Boolean filledShapes = ConfigurationOptions.getConfigBooleanProperty("chart." + chartName + ".filledShapes", "chart.filledShapes",
 110  
                     Boolean.FALSE);
 111  
 
 112  0
             ((XYLineAndShapeRenderer) renderer).setBaseShapesVisible(showShapes.booleanValue());
 113  0
             ((XYLineAndShapeRenderer) renderer).setBaseShapesFilled(filledShapes.booleanValue());
 114  
         }
 115  0
     }
 116  
 
 117  
     /**
 118  
      * Add a copyright notice on the bottom right part of the chart.
 119  
      * <pre>
 120  0
      * chart.&lt;chartName&gt;.chartBackgroundImage.url
 121  0
      * chart.chartBackgroundImage.url
 122  
      * chart.&lt;chartName&gt;.chartBackgroundImage.transparency
 123  0
      * chart.chartBackgroundImage.transparency (0-1, defaulted to 0.35)
 124  0
      * </pre>
 125  
      * @param chartName
 126  0
      * @param chart
 127  0
      */
 128  0
     public static void configureChartBackgroungImage(final String chartName, final JFreeChart chart) {
 129  0
         final String imageLocation = ConfigurationOptions.getConfigStringProperty("chart." + chartName + ".chartBackgroundImage.url",
 130  0
                 "chart.chartBackgroundImage.url", null);
 131  0
         final Float alpha = ConfigurationOptions.getConfigFloatProperty("chart." + chartName + ".chartBackgroundImage.transparency",
 132  
                 "chart.chartBackgroundImage.transparency", new Float(0.35));
 133  0
         if (StringUtils.isNotEmpty(imageLocation) && alpha != null) {
 134  0
             Image image = null;
 135  
             try {
 136  0
                 image = new ImageIcon(new URL(imageLocation)).getImage();
 137  0
             } catch (MalformedURLException e) {
 138  0
                 e.printStackTrace();
 139  0
             }
 140  0
             if (image != null) {
 141  0
                 chart.setBackgroundImageAlpha(alpha.floatValue());
 142  0
                 chart.setBackgroundImageAlignment(Align.BOTTOM_LEFT);
 143  0
                 chart.setBackgroundImage(image);
 144  
             }
 145  
         }
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Add a copyright notice on the bottom right part of the chart.
 150  
      * <pre>
 151  
      * chart.&lt;chartName&gt;.plotImage.url eg file:///C:/project/statcvs/site/images/statcvslogo.gif
 152  
      * chart.plotImage.url
 153  
      * chart.&lt;chartName&gt;.plotImage.transparency
 154  
      * chart.plotImage.transparency (0-1, default to 0.35)
 155  
      * </pre>
 156  
      * @param chartName
 157  
      * @param chart
 158  
      */
 159  
     public static void configurePlotImage(final String chartName, final JFreeChart chart) {
 160  0
         final String imageLocation = ConfigurationOptions.getConfigStringProperty("chart." + chartName + ".plotImage.url", "chart.plotImage.url",
 161  
                 null);
 162  0
         final Float alpha = ConfigurationOptions.getConfigFloatProperty("chart." + chartName + ".plotImage.transparency", "chart.plotImage.transparency",
 163  
                 new Float(0.35));
 164  0
         if (StringUtils.isNotEmpty(imageLocation) && alpha != null) {
 165  0
             Image image = null;
 166  
             try {
 167  0
                 image = new ImageIcon(new URL(imageLocation)).getImage();
 168  0
             } catch (MalformedURLException e) {
 169  0
                 e.printStackTrace();
 170  0
             }
 171  0
             if (image != null) {
 172  0
                 final Plot plot = chart.getPlot();
 173  0
                 plot.setBackgroundImageAlpha(alpha.floatValue());
 174  0
                 plot.setBackgroundImage(image);
 175  
             }
 176  
         }
 177  0
     }
 178  
 
 179  
     /**
 180  
      * Add a copyright notice on the bottom right part of the chart.
 181  
      * <pre>
 182  
      * chart.&lt;chartName&gt;.copyright
 183  
      * chart.copyright
 184  
      * </pre>
 185  
      * @param chartName
 186  
      * @param chart
 187  
      */
 188  
     public static void configureCopyrightNotice(final String chartName, final JFreeChart chart) {
 189  0
         final String copyrightTxt = ConfigurationOptions.getConfigStringProperty("chart." + chartName + ".copyright", "chart.copyright", null);
 190  0
         final Integer copyrightTxtSize = ConfigurationOptions.getConfigIntegerProperty("chart." + chartName + ".copyrightTextSize", "chart.copyrightTextSize",
 191  
                 new Integer(9));
 192  0
         if (StringUtils.isNotEmpty(copyrightTxt)) {
 193  0
             final TextTitle copyright = new TextTitle(copyrightTxt, new Font("SansSerif", Font.PLAIN, copyrightTxtSize.intValue()));
 194  
 
 195  0
             copyright.setPosition(RectangleEdge.BOTTOM);
 196  0
             copyright.setHorizontalAlignment(HorizontalAlignment.RIGHT);
 197  0
             chart.addSubtitle(copyright);
 198  
         }
 199  0
     }
 200  
 }