org.codehaus.mojo.chronos.chart.ChartRendererImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.codehaus.mojo.chronos.chart.ChartRendererImpl.java

Source

/*
 * Chronos - Copyright (C) 2011 National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
 *
 * All source code and information supplied as part of Chronos is
 * copyright to National Board of e-Health.
 *
 * The source code has been released under a dual license - meaning you can
 * use either licensed version of the library with your code.
 *
 * It is released under the LGPL (GNU Lesser General Public License), either
 * version 2.1 of the License, or (at your option) any later version. A copy
 * of which can be found at the link below.
 * http://www.gnu.org/copyleft/lesser.html
 *
 * $HeadURL$
 * $Id$
 */
package org.codehaus.mojo.chronos.chart;

import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

/**
 * Utility class for performing the actual rendering of charts.
 * 
 * @author ksr@lakeside.dk
 */
public final class ChartRendererImpl implements ChartRenderer {

    private static final int HEIGHT = 400;
    private static final int WIDTH = 800;
    private String outputDirectory;

    /**
     * Constructor for the <code>ChartRendererImpl</code> class.
     * 
     * @param outputDir
     *            The directory where generated charts is to be saved.
     */
    public ChartRendererImpl(String outputDir) {
        this.outputDirectory = outputDir;
    }

    /**
     * Save a {@link JFreeChart} to the filesystem.
     * 
     * @param filename
     *            The filename of the chart to save
     * @param chart
     *            the {@link JFreeChart} to save as a file
     * @throws IOException
     *             If the file cannot be saved
     */
    public void renderChart(String filename, JFreeChart chart) throws IOException {
        File parentDir = new File(outputDirectory);

        File imageDir = new File(parentDir, "images");
        if (!imageDir.exists()) {
            imageDir.mkdirs();
        }

        File pngFile = new File(imageDir, filename + ".png");
        ChartUtilities.saveChartAsPNG(pngFile, chart, WIDTH, HEIGHT);
    }
}