Example usage for org.jfree.chart ChartUtilities writeChartAsJPEG

List of usage examples for org.jfree.chart ChartUtilities writeChartAsJPEG

Introduction

In this page you can find the example usage for org.jfree.chart ChartUtilities writeChartAsJPEG.

Prototype

public static void writeChartAsJPEG(OutputStream out, JFreeChart chart, int width, int height)
        throws IOException 

Source Link

Document

Writes a chart to an output stream in JPEG format.

Usage

From source file:org.jrecruiter.web.CustomChartResult.java

/**
 * Executes the result. Writes the given chart as a PNG or JPG to the servlet output stream.
 *
 * @param invocation an encapsulation of the action execution state.
 * @throws Exception if an error occurs when creating or writing the chart to the servlet output stream.
 *//*from  w w  w .j a  v a  2 s.c om*/
public void execute(ActionInvocation invocation) throws Exception {
    if (!chartSet) { // if our chart hasn't been set (by the testcase), we'll look it up in the value stack
        chart = (JFreeChart) invocation.getStack().findValue(value, JFreeChart.class);
    }
    if (chart == null) {// we need to have a chart object - if not, blow up
        throw new IllegalArgumentException("No JFreeChart object found on the stack with name " + value);
    }
    // make sure we have some value for the width and height
    if (height == null) {
        throw new IllegalArgumentException("No height parameter was given.");
    }
    if (width == null) {
        throw new IllegalArgumentException("No width parameter was given.");
    }

    // get a reference to the servlet output stream to write our chart image to
    OutputStream os = ServletActionContext.getResponse().getOutputStream();
    try {
        // check the type to see what kind of output we have to produce
        if ("png".equalsIgnoreCase(type)) {
            ChartUtilities.writeChartAsPNG(os, chart, width, height, true, 0);
        } else if ("jpg".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type)) {
            ChartUtilities.writeChartAsJPEG(os, chart, width, height);
        } else {
            throw new IllegalArgumentException(
                    type + " is not a supported render type (only JPG and PNG are).");
        }
    } finally {
        if (os != null)
            os.flush();
    }
}

From source file:org.apache.struts2.dispatcher.ChartResult.java

/**
 * Executes the result. Writes the given chart as a PNG or JPG to the servlet output stream.
 *
 * @param invocation an encapsulation of the action execution state.
 * @throws Exception if an error occurs when creating or writing the chart to the servlet output stream.
 *//*from  w  w  w . j  a v  a2s  .c  o  m*/
public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {

    initializeProperties(invocation);

    if (!chartSet) // if our chart hasn't been set (by the testcase), we'll look it up in the value stack
        chart = (JFreeChart) invocation.getStack().findValue(value, JFreeChart.class);
    if (chart == null) // we need to have a chart object - if not, blow up
        throw new NullPointerException("No JFreeChart object found on the stack with name " + value);
    // make sure we have some value for the width and height
    if (height == null)
        throw new NullPointerException("No height parameter was given.");
    if (width == null)
        throw new NullPointerException("No width parameter was given.");

    // get a reference to the servlet output stream to write our chart image to
    HttpServletResponse response = ServletActionContext.getResponse();
    OutputStream os = response.getOutputStream();
    try {
        // check the type to see what kind of output we have to produce
        if ("png".equalsIgnoreCase(type)) {
            response.setContentType("image/png");
            ChartUtilities.writeChartAsPNG(os, chart, getIntValueFromString(width),
                    getIntValueFromString(height));
        } else if ("jpg".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type)) {
            response.setContentType("image/jpg");
            ChartUtilities.writeChartAsJPEG(os, chart, getIntValueFromString(width),
                    getIntValueFromString(height));
        } else
            throw new IllegalArgumentException(
                    type + " is not a supported render type (only JPG and PNG are).");
    } finally {
        if (os != null)
            os.flush();
    }
}

From source file:net.commerce.zocalo.freechart.ChartTest.java

public void testSimpleBarChart() throws IOException {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    String seriesKey = "prices";
    dataset.addValue(.2, seriesKey, "apples");
    dataset.addValue(.15, seriesKey, "bananas");
    dataset.addValue(.18, seriesKey, "cherries");
    dataset.addValue(.32, seriesKey, "watermelon");
    dataset.addValue(.06, seriesKey, "peaches");
    dataset.addValue(.09, seriesKey, "persimmons");

    int hSize = 300;
    int vSize = 150;
    JFreeChart chart = ChartGenerator.buildBarChart(dataset, PlotOrientation.HORIZONTAL, hSize, vSize);

    File jpgFile = newEmptyFile(".", "BarChart.jpg");
    File pngFile = newEmptyFile(".", "BarChart.png");
    assertFalse(jpgFile.exists());/* w w w  .  ja  va2 s  .c  om*/
    assertFalse(pngFile.exists());

    OutputStream jpgStream = new FileOutputStream(jpgFile);
    OutputStream pngStream = new FileOutputStream(pngFile);

    ChartUtilities.writeChartAsJPEG(jpgStream, chart, hSize, vSize);
    ChartUtilities.writeChartAsPNG(pngStream, chart, hSize, vSize);

    assertTrue(jpgFile.exists());
    assertTrue(pngFile.exists());
    jpgFile.delete();
    pngFile.delete();
}

From source file:com.vectorprint.report.jfree.ChartBuilder.java

public void save(OutputStream img, FILETYPE type, int width, int height) throws IOException {
    switch (type) {
    case JPEG:/*www  .  ja  v  a2s  . c o m*/
        ChartUtilities.writeChartAsJPEG(img, chart, width, height);

        break;

    case PNG:
        ChartUtilities.writeChartAsPNG(img, chart, width, height);

        break;
    }
}

From source file:org.projectforge.web.scripting.ScriptExecutePage.java

private void exportJFreeChart(final ExportJFreeChart exportJFreeChart) {
    final JFreeChart chart = exportJFreeChart.getJFreeChart();
    final int width = exportJFreeChart.getWidth();
    final int height = exportJFreeChart.getHeight();
    final StringBuffer buf = new StringBuffer();
    buf.append("pf_chart_");
    buf.append(DateHelper.getTimestampAsFilenameSuffix(new Date()));
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {//  w  w w. j a  va2 s.co m
        if (exportJFreeChart.getImageType() == JFreeChartImageType.PNG) {
            ChartUtilities.writeChartAsPNG(out, chart, width, height);
            buf.append(".png");
        } else {
            ChartUtilities.writeChartAsJPEG(out, chart, width, height);
            buf.append(".jpg");
        }
    } catch (final IOException ex) {
        log.fatal("Exception encountered " + ex, ex);
    }
    DownloadUtils.setDownloadTarget(out.toByteArray(), buf.toString());
}

From source file:com.tencent.wstt.apt.chart.AbstractRealTimeLineChart.java

/**
 * ?/*from w  w w  .j a  v a  2  s  . c  o  m*/
* @Title: saveToJPEG  
* @Description:   
* @param imageName
* @return 
* boolean 
* @throws
 */
public boolean saveToJPEG(String imageName) {
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imageName);
        ChartUtilities.writeChartAsJPEG(fos, chart, this.getSize().width, this.getSize().height);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:net.commerce.zocalo.freechart.ChartTest.java

public void testXYAreaStepChart() throws IOException {
    Minute now = new Minute();

    TimePeriodValuesCollection aSeries = createBottomValues(now);
    TimePeriodValuesCollection bSeries = createTopValues(now);

    File jpgFile = newEmptyFile(".", "StepAreaChart.jpg");
    File pngFile = newEmptyFile(".", "StepAreaChart.png");
    assertFalse(jpgFile.exists());/*from w ww  . j  a  v  a  2s. com*/
    assertFalse(pngFile.exists());

    OutputStream jpgStream = new FileOutputStream(jpgFile);
    OutputStream pngStream = new FileOutputStream(pngFile);

    JFreeChart chart = ChartGenerator.createCustomXYStepAreaChart(aSeries, bSeries);

    ChartUtilities.writeChartAsJPEG(jpgStream, chart, 500, 500);
    ChartUtilities.writeChartAsPNG(pngStream, chart, 500, 500);

    assertTrue(jpgFile.exists());
    assertTrue(pngFile.exists());
    jpgFile.delete();
    pngFile.delete();
}

From source file:org.opennms.netmgt.charts.ChartUtils.java

/**
 * Helper method that returns the JFreeChart to an output stream written in JPEG format.
 *
 * @param chartName a {@link java.lang.String} object.
 * @param out a {@link java.io.OutputStream} object.
 * @throws org.exolab.castor.xml.MarshalException if any.
 * @throws org.exolab.castor.xml.ValidationException if any.
 * @throws java.io.IOException if any.//w  ww.  j  a  v a  2  s . c o  m
 * @throws java.sql.SQLException if any.
 */
public static void getBarChart(String chartName, OutputStream out)
        throws MarshalException, ValidationException, IOException, SQLException {
    BarChart chartConfig = getBarChartConfigByName(chartName);
    JFreeChart chart = getBarChart(chartName);
    ImageSize imageSize = chartConfig.getImageSize();
    int hzPixels;
    int vtPixels;

    if (imageSize == null) {
        hzPixels = 400;
        vtPixels = 400;
    } else {
        hzPixels = imageSize.getHzSize().getPixels();
        vtPixels = imageSize.getVtSize().getPixels();
    }

    ChartUtilities.writeChartAsJPEG(out, chart, hzPixels, vtPixels);

}

From source file:com.comcast.cqs.test.stress.CqsStressTester.java

private byte[] generateChart(String title, ConcurrentHashMap<Integer, AtomicInteger> metric, String id,
        Date startTime, Date endTime) throws IOException {

    XYSeries series = new XYSeries(title);

    for (Entry<Integer, AtomicInteger> entry : metric.entrySet()) {
        series.add(entry.getKey().intValue(), entry.getValue().intValue());
    }/*  w  ww .  j ava2 s.  c  o m*/

    XYSeriesCollection dataset = new XYSeriesCollection(series);

    JFreeChart chart = ChartFactory.createXYBarChart(
            "Start: " + startTime + " End: " + endTime + " Message Count: " + messageCount, "Test Second",
            false, "Number of Messages", dataset, PlotOrientation.VERTICAL, true, true, false);

    File file = new File("/tmp/" + id + ".jpeg");
    //File file = new File(getServletContext().getRealPath("WEB-INF" + "/" + id + ".jpeg"));
    ChartUtilities.saveChartAsJPEG(file, chart, 1600, 400);
    //byte b[] = Files.toByteArray(file);
    //return b;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ChartUtilities.writeChartAsJPEG(bos, chart, 2400, 400);
    return bos.toByteArray();
}

From source file:net.commerce.zocalo.freechart.ChartTest.java

public void testOverlaidOHLCPlusStepChart() throws IOException {
    Minute now = new Minute();
    File jpgFile = newEmptyFile(".", "OverlaidChart.jpg");
    File pngFile = newEmptyFile(".", "OverlaidChart.png");

    assertFalse(jpgFile.exists());// w  w  w  .  ja  v a  2 s .  c  o  m
    assertFalse(pngFile.exists());

    TimePeriodValuesCollection bottom = createBottomValues(now);
    TimePeriodValuesCollection top = createTopValues(now);

    OHLCDataset OHLCdata = createOHLCDataSet(now);
    JFreeChart chart = ChartGenerator.createOverlaidOHLCAndStepChart(bottom, top, OHLCdata);

    OutputStream jpgStream = new FileOutputStream(jpgFile);
    OutputStream pngStream = new FileOutputStream(pngFile);
    ChartUtilities.writeChartAsJPEG(jpgStream, chart, 500, 500);
    ChartUtilities.writeChartAsPNG(pngStream, chart, 500, 500);

    assertTrue(jpgFile.exists());
    assertTrue(pngFile.exists());
    jpgFile.delete();
    pngFile.delete();
}