Example usage for org.jfree.chart ChartUtilities saveChartAsJPEG

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

Introduction

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

Prototype

public static void saveChartAsJPEG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info)
        throws IOException 

Source Link

Document

Saves a chart to a file in JPEG format.

Usage

From source file:org.posterita.core.AbstractChart.java

public String saveChartAsJPEG(String filePath, int width, int height) throws OperationException {
    try {/*from   w w w  . j  a v  a2  s  .  co  m*/
        ChartUtilities.saveChartAsJPEG(new File(filePath), getChart(), width, height, renderingInfo);
        return filePath;
    } catch (IOException e) {
        throw new OperationException("Problem occured while saving chart.", e);
    }
}

From source file:msec.org.Tools.java

public static String generateFullDayChart(String filename, OneDayValue[] data, String title) {
    if (data[0].getValues().length != 1440) {
        return "data size invalid";
    }/*from   w  ww. j a va  2  s  .co  m*/
    if (data.length > 1) {
        if (data[1].getValues() == null || data[1].getValues().length != 1440) {
            return "data 1 invalid";
        }
    }

    XYDataset xydataset = createDataset(data);
    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, "time", "", xydataset, true, true, true);

    try {
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();

        //
        DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
        dateaxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
        dateaxis.setLabelFont(new Font("", Font.PLAIN, 16)); //
        dateaxis.setLabelPaint(ChartColor.gray);
        dateaxis.setTickLabelFont(new Font("", Font.PLAIN, 16));
        dateaxis.setTickLabelPaint(ChartColor.GRAY);

        GregorianCalendar endgc = (GregorianCalendar) gc.clone();
        endgc.add(GregorianCalendar.DATE, 1);
        dateaxis.setMaximumDate(endgc.getTime());

        dateaxis.setTickMarksVisible(true);
        dateaxis.setTickMarkInsideLength(5);
        dateaxis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 2));
        dateaxis.setVerticalTickLabels(true);
        dateaxis.setLabel("");

        //
        ValueAxis rangeAxis = xyplot.getRangeAxis();//?
        rangeAxis.setLabelFont(new Font("", Font.PLAIN, 16));
        rangeAxis.setLabelPaint(ChartColor.gray);
        rangeAxis.setTickLabelFont(new Font("", Font.PLAIN, 16));
        rangeAxis.setTickLabelPaint(ChartColor.gray);
        rangeAxis.setLowerBound(0);

        //
        jfreechart.getLegend().setItemFont(new Font("", Font.PLAIN, 12));
        jfreechart.getLegend().setItemPaint(ChartColor.gray);
        jfreechart.getLegend().setBorder(0, 0, 0, 0);//

        //
        jfreechart.getTitle().setFont(new Font("", Font.PLAIN, 18));//
        jfreechart.getTitle().setPaint(ChartColor.gray);

        //?
        xyplot.setRangeGridlinePaint(ChartColor.GRAY);
        xyplot.setBackgroundPaint(ChartColor.WHITE);
        xyplot.setOutlinePaint(null);//

        int w = 500;
        int h = 300;

        // ChartUtilities.saveChartAsPNG(new File(filename), jfreechart, w, h);
        ChartUtilities.saveChartAsJPEG(new File(filename), 0.8f, jfreechart, w, h);

        return "success";

    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }
}

From source file:msec.org.Tools.java

public static String generateDaysChart(String filename, ArrayList<OneDayValue> data, OneAttrDaysChart chart,
        String title, int duration) {
    if (data.size() == 0) {
        return "data size invalid";
    }/*from   w ww  . j  a  v  a  2s .  c o  m*/

    int date = Integer.parseInt(data.get(0).getDate());
    GregorianCalendar startgc = new GregorianCalendar(date / 10000, date % 10000 / 100 - 1, date % 100);

    XYDataset xydataset = createDaysDataset(data, startgc, chart);
    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, "time", "", xydataset, true, true, true);

    try {
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();

        //
        DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
        dateaxis.setDateFormatOverride(new SimpleDateFormat("MM/dd"));
        dateaxis.setLabelFont(new Font("", Font.PLAIN, 16)); //
        dateaxis.setLabelPaint(ChartColor.gray);
        dateaxis.setTickLabelFont(new Font("", Font.PLAIN, 16));
        dateaxis.setTickLabelPaint(ChartColor.GRAY);

        dateaxis.setMinimumDate(startgc.getTime());
        GregorianCalendar endgc = (GregorianCalendar) startgc.clone();
        endgc.add(GregorianCalendar.DATE, duration);
        dateaxis.setMaximumDate(endgc.getTime());

        dateaxis.setTickMarksVisible(true);
        dateaxis.setTickMarkInsideLength(5);
        dateaxis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1));
        dateaxis.setVerticalTickLabels(true);
        dateaxis.setLabel("");

        //
        ValueAxis rangeAxis = xyplot.getRangeAxis();//?
        rangeAxis.setLabelFont(new Font("", Font.PLAIN, 16));
        rangeAxis.setLabelPaint(ChartColor.gray);
        rangeAxis.setTickLabelFont(new Font("", Font.PLAIN, 16));
        rangeAxis.setTickLabelPaint(ChartColor.gray);
        rangeAxis.setLowerBound(0);

        //
        jfreechart.getLegend().setItemFont(new Font("", Font.PLAIN, 12));
        jfreechart.getLegend().setItemPaint(ChartColor.gray);
        jfreechart.getLegend().setBorder(0, 0, 0, 0);//

        //
        jfreechart.getTitle().setFont(new Font("", Font.PLAIN, 18));//
        jfreechart.getTitle().setPaint(ChartColor.gray);

        //?
        xyplot.setRangeGridlinePaint(ChartColor.GRAY);
        xyplot.setBackgroundPaint(ChartColor.WHITE);
        xyplot.setOutlinePaint(null);//

        int w = 500;
        int h = 300;

        // ChartUtilities.saveChartAsPNG(new File(filename), jfreechart, w, h);
        ChartUtilities.saveChartAsJPEG(new File(filename), 0.8f, jfreechart, w, h);

        return "success";

    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }
}

From source file:com.bt.aloha.sipstone.GenGraph.java

public void saveCharts(File file1, File file2) {
    try {/*w w  w . j  a  v  a  2s  . c om*/
        ChartUtilities.saveChartAsJPEG(file1, .95f, createCombinedChart(), 1024, 768);
    } catch (IOException e) {
        System.out.println("UNABLE to store chart: " + e.getMessage());
    }
    try {
        ChartUtilities.saveChartAsJPEG(file2, .95f, createSingleChart(), 1024, 768);
    } catch (IOException e) {
        System.out.println("UNABLE to store chart: " + e.getMessage());
    }
}

From source file:userinterface.graph.Graph.java

/**
 * Renders the current graph to a JPEG file.
 * //from w w w .  jav a2 s  . c  om
 * @param file
 *             The file to export the JPEG data to.
 * @throws GraphException, IOException
 *             If file cannot be written to.
 */
public void exportToJPEG(File file, int width, int height) throws GraphException, IOException {
    ChartUtilities.saveChartAsJPEG(file, 1.0f, this.chart, width, height);
}

From source file:com.zimbra.perf.chart.ChartUtil.java

private void writeChart(JFreeChart chart, ChartSettings cs) throws IOException {
    String filename = cs.getOutfile();
    System.out.println("Writing " + filename);
    File file = new File(mDestDir, filename);
    if (cs.getImageType() == ImageType.PNG) {
        ChartUtilities.saveChartAsPNG(file, chart, cs.getWidth(), cs.getHeight());
    } else {//w w w  . j a  va2 s. c o  m
        ChartUtilities.saveChartAsJPEG(file, 90, chart, cs.getWidth(), cs.getHeight());
    }
}

From source file:com.naryx.tagfusion.cfm.tag.awt.cfCHART.java

private String saveChartToFile(JFreeChart chart, byte _format, int width, int height, ChartRenderingInfo info)
        throws IOException {
    File tempFile;//from   www  . j ava  2  s. c o m
    if (_format == FORMAT_JPG) {
        tempFile = File.createTempFile("cfchart", ".jpeg", cfchartDirectory);
        ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
    } else {
        tempFile = File.createTempFile("cfchart", ".png", cfchartDirectory);
        ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
    }
    String filename = tempFile.getName();

    // Check if charts are being cached
    if (storageCacheSize > 0) {
        synchronized (storageCache) {
            // If we've reached the cache limit then delete the oldest cached chart.
            if (storageCache.size() == storageCacheSize) {
                String oldestChart = storageCache.remove(0);
                File fileToDelete = new File(cfchartDirectory, oldestChart);
                fileToDelete.delete();
            }

            // Add the new chart to the end of the list of cached charts
            storageCache.add(filename);
        }
    }

    return filename;
}