001    /*
002    @license.text@
003     */
004    package biz.hammurapi.metrics.persistent;
005    
006    import java.io.File;
007    import java.io.IOException;
008    import java.io.OutputStream;
009    
010    import org.jfree.chart.ChartUtilities;
011    import org.jfree.chart.JFreeChart;
012    
013    /**
014     * @author Pavel Vlasov
015     * @version $Revision: 1.2 $
016     */
017    public class ChartHelper {
018            
019        /**
020         * Output JPG.
021         */
022        public static final byte FORMAT_JPG=0;
023        
024        /**
025         * Output PNG
026         */
027        public static final byte FORMAT_PNG=1;
028    
029        private byte outputFormat;
030    
031            private int height;
032    
033            private int width;
034    
035    
036            /**
037             * Helper class.
038             * @param outputFormat FORMAT_JPG or FORMAT_PNG.
039             * @param width chart width
040             * @param height chart height
041             */
042        public ChartHelper(
043                            byte outputFormat,
044                            int width,
045                            int height) {
046            this.outputFormat=outputFormat;
047            this.height=height;
048            this.width=width;
049        }
050    
051        /**
052         * Outputs chart to file.
053         * @param chart
054         * @param outputFile
055         * @throws IOException
056         */
057        public void output(JFreeChart chart, File outputFile) throws IOException {
058            if (chart!=null) {
059                    switch (outputFormat) {
060                            case FORMAT_PNG:
061                                    ChartUtilities.saveChartAsPNG(outputFile, chart, width, height );
062                                    break;
063                            case FORMAT_JPG:
064                                    ChartUtilities.saveChartAsJPEG(outputFile, chart, width, height );
065                                    break;
066                            default:
067                                    throw new IllegalStateException("Invalid output format: "+outputFormat);
068                    }
069            }
070        }
071    
072        /**
073         * Outputs chart to stream
074         * @param chart
075         * @param out
076         * @throws IOException
077         */
078        public void output(JFreeChart chart, OutputStream out) throws IOException {
079            if (chart!=null) {
080                    switch (outputFormat) {
081                            case FORMAT_PNG:
082                                    ChartUtilities.writeChartAsPNG(out, chart, width, height );
083                                    break;
084                            case FORMAT_JPG:
085                                    ChartUtilities.writeChartAsJPEG(out, chart, width, height );
086                                    break;
087                            default:
088                                    throw new IllegalStateException("Invalid output format: "+outputFormat);
089                    }
090            }
091        }
092    }