Example usage for org.jfree.chart ChartUtilities saveChartAsPNG

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

Introduction

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

Prototype

public static void saveChartAsPNG(File file, JFreeChart chart, int width, int height) throws IOException 

Source Link

Document

Saves a chart to the specified file in PNG format.

Usage

From source file:com.redhat.rhn.frontend.graphing.GraphGenerator.java

/**
 * Generate a graph, store it to the java.io.tmpdir and
 * return a java.io.File object pointer to the file itself.
 *
 * @param chartIn chart we want to save as a File
 * @param height height in pixels of the image generated
 * @param width in pixels of the image generated
 * @return java.io.File representation of the graph
 *//*  ww w  .ja v  a 2  s .  co  m*/
public File generateGraphFile(JFreeChart chartIn, int height, int width) {
    File retval = null;
    try {
        log.debug("Generating graph with title: ");
        String tmpDir = System.getProperty("java.io.tmpdir");
        StringBuilder fname = new StringBuilder();
        fname.append(tmpDir);
        fname.append("/chart");
        fname.append(System.currentTimeMillis());
        fname.append(".png");
        retval = new File(fname.toString());
        ChartUtilities.saveChartAsPNG(retval, chartIn, height, width);
    } catch (IOException ioe) {
        log.error("Error generating graph: " + ioe, ioe);
    }
    return retval;
}

From source file:org.infoglue.deliver.util.charts.ChartHelper.java

/**
 * This method invokes a named chart with the data supplied in xml-format.
 *///from w  ww . ja  va 2  s.c  o  m

public String renderGraph(String chartName, String dataAsXML, int width, int height) {
    String assetUrl = "";

    try {
        XMLDataDiagram demo = null;

        if (chartName.equals("TimeSeriesDiagram")) {
            demo = new TimeSeriesDiagram();
            demo.setDiagramData(dataAsXML);
            demo.renderChart();
        } else {
            //Todo - for now...
            demo = new TimeSeriesDiagram();
            demo.setDiagramData(dataAsXML);
            demo.renderChart();
        }

        String uniqueId = chartName + width + height + dataAsXML.length();
        String fileName = uniqueId + ".png";

        int i = 0;
        String filePath = CmsPropertyHandler.getDigitalAssetPath0();
        File file = null;
        while (filePath != null) {
            file = new File(filePath + java.io.File.separator + fileName);
            ChartUtilities.saveChartAsPNG(file, demo.getChart(), width, height);
            i++;
            filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
        }

        /*
        String filePath = CmsPropertyHandler.getDigitalAssetPath();
        File file = new File(filePath + java.io.File.separator + fileName);
        ChartUtilities.saveChartAsPNG(file, demo.getChart(), width, height);
        */

        assetUrl = this.templateController.getDigitalAssetBaseUrl() + "/" + file.getName();
    } catch (Exception e) {
        logger.warn("An error occurred when we tried to generate a graph:" + e.getMessage(), e);
    }

    return assetUrl;
}

From source file:ntpgraphic.LineChart.java

private JPanel createChartPanel() {
    String chartTitle = "NTP Graphic";
    String xAxisLabel = "Time";
    String yAxisLabel = "Roundtrip delay(ms)";

    XYDataset dataset = createDataset();

    JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, xAxisLabel, yAxisLabel, dataset,
            PlotOrientation.VERTICAL, rootPaneCheckingEnabled, rootPaneCheckingEnabled,
            rootPaneCheckingEnabled);/*from w ww.ja v a 2s  . c o  m*/

    File imageFile = new File("NTPGraphic.png");
    int width = 640;
    int height = 480;

    try {
        ChartUtilities.saveChartAsPNG(imageFile, chart, width, height);
    } catch (IOException ex) {
        System.err.println(ex);
    }

    return new ChartPanel(chart);
}

From source file:Graphics.Barchart.java

public void createImage(String nome, boolean jpg, int larg, int alt) {
    File pieChart = new File(nome);
    if (jpg) {/*from ww w .  ja  v a2  s  . com*/
        try {
            ChartUtilities.saveChartAsJPEG(pieChart, grafico, larg, alt);
        } catch (IOException ex) {
            Logger.getLogger(Piechart.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        try {
            ChartUtilities.saveChartAsPNG(pieChart, grafico, larg, alt);
        } catch (IOException ex) {
            Logger.getLogger(Piechart.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:org.codehaus.mojo.dashboard.report.plugin.chart.AbstractChartRenderer.java

public void saveToFile(String filename) throws IOException {
    report.setBackgroundPaint(getBackgroundColor());
    File imageFile = new File(filename);

    imageFile.getParentFile().mkdirs();//from  www.  j  a v  a  2 s.c  o m

    ChartUtilities.saveChartAsPNG(new File(filename), report, width, height);
}

From source file:org.matsim.analysis.LegHistogramChart.java

/**
* Writes a graphic showing the number of departures, arrivals and vehicles
* en route of all legs/trips to the specified file.
*
* @param legHistogram/*from   www  . j a  v  a2 s  .  com*/
 * @param filename
*
*/
public static void writeGraphic(LegHistogram legHistogram, final String filename) {
    try {
        ChartUtilities.saveChartAsPNG(new File(filename),
                getGraphic(legHistogram.getAllModesData(), "all", legHistogram.getIteration()), 1024, 768);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.jboss.weld.benchmark.charts.Chart.java

/**
 * Saving a chart to hard disk as png image
 *
 * @param path absolute path//from   w ww. j a va  2  s. co m
 * @return true if no errors occurred
 */
public Boolean saveImageTo(String path) {
    try {
        JFreeChart lineChartObject = ChartFactory.createBarChart(NAME, X_AXIS_NAME, Y_AXIS_NAME,
                lineChartDataset, PlotOrientation.VERTICAL, true, true, false);

        CategoryPlot plot = (CategoryPlot) lineChartObject.getPlot();

        plot.setDomainAxis(new CategoryAxis() {
            private static final long serialVersionUID = 1L;

            @Override
            protected TextBlock createLabel(@SuppressWarnings("rawtypes") Comparable category, float width,
                    RectangleEdge edge, Graphics2D g2) {
                TextBlock label = TextUtilities.createTextBlock(category.toString(), getTickLabelFont(category),
                        getTickLabelPaint(category), ONE_PART_WIDTH - 30, 2, new G2TextMeasurer(g2));

                label.setLineAlignment(HorizontalAlignment.LEFT);
                return label;
            }
        });

        File lineChart = new File(path, NAME + ".png");
        ChartUtilities.saveChartAsPNG(lineChart, lineChartObject,
                ONE_PART_WIDTH * lineChartDataset.getColumnCount(), GRAPH_HEIGHT);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:net.anthonypoon.fintech.assignment.one.part2.Plotter.java

void saveAsPNG(String path, int width, int height) throws Exception {
    this.render(false);
    ChartUtilities.saveChartAsPNG(new File(path), chart, width, height);
}

From source file:io.narayana.perf.product.BarChart.java

private void writeImageData(JFreeChart chart, String imageFileName) throws IOException {
    ChartUtilities.saveChartAsPNG(new File(imageFileName), chart, 600, 300);
}

From source file:net.sf.clichart.chart.ChartSaver.java

/**
 * Package private for testing//from   w w  w .  jav  a  2 s  . c om
 */
void saveChart(int chartType, File outputFile) throws IOException {
    if (chartType == TYPE_PNG) {
        ChartUtilities.saveChartAsPNG(new File(outputFile.getPath()), m_chart, m_width, m_height);
    } else {
        ChartUtilities.saveChartAsJPEG(new File(outputFile.getPath()), m_chart, m_width, m_height);
    }
}