View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
20   */
21  package com.ebay.carad.os.vitalsigns.util;
22  
23  import java.awt.Rectangle;
24  import java.awt.image.BufferedImage;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.util.Arrays;
29  import java.util.Collections;
30  import java.util.List;
31  
32  import org.apache.commons.lang.exception.ExceptionUtils;
33  import org.apache.log4j.Logger;
34  import org.jfree.chart.JFreeChart;
35  
36  import com.sun.image.codec.jpeg.JPEGCodec;
37  import com.sun.image.codec.jpeg.JPEGEncodeParam;
38  import com.sun.image.codec.jpeg.JPEGImageEncoder;
39  
40  /***
41   * This class produces a time-series chart, and saves it as a JPEG to the specified location.
42   * 
43   * @author Jeremy Thomerson
44   * @author Jeremy Kraybill
45   * @version $Id$
46   */
47  public abstract class ChartUtil {
48  
49  	/*** Class logger. */
50  	private static final Logger LOGGER = Logger.getLogger(ChartUtil.class);
51  	
52      /*** Label string for legends. */
53      private static final String CURRENT_HOUR_LABEL = "Current Hour";
54      /*** Label string for legends. */
55      private static final String CURRENT_DAY_LABEL = "Current Day";
56      /*** Label string for legends. */
57      private static final String CURRENT_WEEK_LABEL = "Current Week";
58      /*** Label string for legends. */
59      private static final String CURRENT_MONTH_LABEL = "Current Month";
60      /*** Label string for legends. */
61      private static final String CURRENT_YEAR_LABEL = "Current Year";
62      /*** Label string for legends. */
63      private static final String ONE_DAY_AGO_LABEL = "One Day Ago";
64      /*** Label string for legends. */
65      private static final String ONE_WEEK_AGO_LABEL = "One Week Ago";
66      /*** Label string for legends. */
67      private static final String ONE_MONTH_AGO_LABEL = "One Month Ago";
68      /*** Label string for legends. */
69      private static final String ONE_YEAR_AGO_LABEL = "One Year Ago";
70      
71      /*** Legend for hour charts. */
72  	public static final List HOURLEGEND = Collections.unmodifiableList(Arrays.asList(new String[] { CURRENT_HOUR_LABEL, ONE_WEEK_AGO_LABEL, ONE_MONTH_AGO_LABEL }));
73      /*** Legend for day charts. */
74  	public static final List DAYLEGEND = Collections.unmodifiableList(Arrays.asList(new String[] {CURRENT_DAY_LABEL, ONE_DAY_AGO_LABEL, ONE_WEEK_AGO_LABEL, ONE_MONTH_AGO_LABEL, ONE_YEAR_AGO_LABEL }));
75      /*** Legend for week charts. */
76  	public static final List WEEKLEGEND = Collections.unmodifiableList(Arrays.asList(new String[] {CURRENT_WEEK_LABEL, ONE_WEEK_AGO_LABEL, ONE_MONTH_AGO_LABEL, ONE_YEAR_AGO_LABEL }));
77      /*** Legend for month charts. */
78  	public static final List MONTHLEGEND = Collections.unmodifiableList(Arrays.asList(new String[] {CURRENT_MONTH_LABEL, ONE_MONTH_AGO_LABEL, ONE_YEAR_AGO_LABEL }));
79      /*** Legend for year charts. */
80  	public static final List YEARLEGEND = Collections.unmodifiableList(Arrays.asList(new String[] {CURRENT_YEAR_LABEL, ONE_YEAR_AGO_LABEL }));
81  	
82  	private static void encode(BufferedImage img, OutputStream out, float quality) throws IOException {
83  		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
84  		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
85  		param.setQuality(quality,true);
86  		encoder.encode(img,param);
87  	}
88      
89      /***
90       * Saves the given chart to the given destination JPG file at 85% quality.
91       * 
92       * @param chart the chart to save
93       * @param path the path of the image to save to
94       * @param width the width of the image in pixels
95       * @param height the height of the image in pixes
96       */
97      public static void saveChart(JFreeChart chart, String path, int width, int height) {
98          saveChart(chart, path, width, height, 0.85f);
99      }
100 
101     /***
102      * Saves the given chart to the given destination JPG file.
103      * 
104      * @param chart the chart to save
105      * @param path the path of the image to save to
106      * @param width the width of the image in pixels
107      * @param height the height of the image in pixes
108      * @param quality the quality of the JPG, from 0 to 1.
109      */
110     public static void saveChart(JFreeChart chart, String path, int width, int height, float quality) {
111     	FileOutputStream fos = null;
112 		try {
113     		fos = new FileOutputStream(path);
114     		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
115     		chart.draw(bi.createGraphics(), new Rectangle(0, 0, width, height));
116     		encode(bi, fos, quality);
117     	} catch (IOException ioe) {
118     		LOGGER.error("exception encountered: " + ExceptionUtils.getStackTrace(ioe));
119     	} finally {
120     		try {
121     			fos.close();
122         	} catch (IOException ioe) {
123         		LOGGER.error("exception encountered: " +  ExceptionUtils.getStackTrace(ioe));
124         	}
125     	}
126     }
127 }