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:Servizi.GraficoJ.java

/**
 * Costruttore che permette la visualizzazione di un grafico 
 * @param titolo Targhetta che descrive il grafico
 * @param datiFitness array che contiene la media della fitness di ogni generazione
 * @param fitnessLinea costante che rappresenta il valore di fitness della linea da ottenere
 * @throws IOException /*from ww  w. ja v  a 2  s  .c  o  m*/
 */
public GraficoJ(final String titolo, Double[] datiFitness, Double fitnessLinea, String name)
        throws IOException {

    super(titolo);

    final XYDataset dataset = createDataset(datiFitness, fitnessLinea);

    final JFreeChart chart = createChart(dataset);

    final ChartPanel chartPanel = new ChartPanel(chart);

    chartPanel.setPreferredSize(new java.awt.Dimension(700, 470));

    setContentPane(chartPanel);

    ChartUtilities.saveChartAsPNG(new File("/home/gianni/Documenti/output/" + name + ".png"), chart, 1024, 768);

}

From source file:org.codehaus.mojo.chronos.chart.ChartRendererImpl.java

/**
 * Save a {@link JFreeChart} to the filesystem.
 * /* w  w w  .  java  2 s .  com*/
 * @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);
}

From source file:org.primefaces.showcase.view.multimedia.GraphicImageView.java

@PostConstruct
public void init() {
    try {/*  w  ww  . j  a  v a  2  s  . com*/
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.primefaces.examples.view.DynamicImageController.java

public DynamicImageController() {
    try {// w w w. j  a  v a 2  s  . co  m
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createPieChart("Turkish Cities", createDataset(), true, true,
                false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");

        //Barcode
        File barcodeFile = new File("dynamicbarcode");
        BarcodeImageHandler.saveJPEG(BarcodeFactory.createCode128("PRIMEFACES"), barcodeFile);
        barcode = new DefaultStreamedContent(new FileInputStream(barcodeFile), "image/jpeg");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:playground.artemc.socialCost.MeanTravelTimeWriter.java

public void writeGraphic(final String filename, String legMode, double[] data) {
    try {/*w  ww  .j a  v  a  2 s  .c om*/
        ChartUtilities.saveChartAsPNG(new File(filename), getGraphic(legMode, data), 1024, 768);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:lifnetwork.ChartSave.java

private void saveFile(File f, int width, int height, String title) throws Throwable {
    setTitle(title);/*from  ww  w  .  java2  s . com*/
    ChartUtilities.saveChartAsPNG(f, fireChart, width, height);
}

From source file:uk.ac.ed.epcc.webapp.charts.jfreechart.JFreeChartData.java

public void createPNG(File pngfile) throws IOException {
    ChartUtilities.saveChartAsPNG(pngfile, getCustomisedJFreeChart(), DEFAULT_X_SIZE, DEFAULT_Y_SIZE);
}

From source file:com.pureinfo.srm.reports.impl.STSimpleBase.java

/**
 * @throws PureException/*from   w  w w.j  av a  2 s . c o  m*/
 * @see com.pureinfo.srm.reports.IStatisticable#statistic()
 */
public final String statistic() throws PureException {
    IChartBuilder builder = getChartBuilder();
    JFreeChart chart = builder.buildChart();
    setChartInfo(builder.getChartInfo());
    String sFileName = ReportConfigHelper.makeImageFileName("png");
    try {
        String sPath = ReportConfigHelper.getImageLocalPath();
        File file = new File(sPath, sFileName);
        if (logger.isDebugEnabled()) {
            logger.debug("to save report chart as " + file.getAbsolutePath());
        }
        ChartUtilities.saveChartAsPNG(file, chart, m_chartInfo.getChartSize().x, m_chartInfo.getChartSize().y);
        return sFileName;
    } catch (IOException ex) {
        throw new PureException(SRMExceptionTypes.REPORT_SAVE_CHART, sFileName, ex);
    }
}

From source file:Servlets.servletGraficas.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  ww  w.java 2 s  . com
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    String SITIO_1 = "prueba 1";
    String SITIO_2 = "prueba 2";
    // Visitas del sitio web 1
    dataset.setValue(100, SITIO_1, "Lunes");
    dataset.setValue(120, SITIO_1, "Martes");
    dataset.setValue(110, SITIO_1, "Mircoles");
    dataset.setValue(103, SITIO_1, "Jueves");
    dataset.setValue(106, SITIO_1, "Viernes");

    // Visitas del sitio web 2
    dataset.setValue(60, SITIO_2, "Lunes");
    dataset.setValue(62, SITIO_2, "Martes");
    dataset.setValue(61, SITIO_2, "Mircoles");
    dataset.setValue(63, SITIO_2, "Jueves");
    dataset.setValue(66, SITIO_2, "Viernes");
    JFreeChart chart = ChartFactory.createBarChart3D("Aspirantes", "", "Nmero visitas", dataset,
            PlotOrientation.VERTICAL, true, true, false);
    try {
        File image = File.createTempFile("image", "tmp");

        // Assume that we have the chart
        ChartUtilities.saveChartAsPNG(image, chart, 500, 300);

        FileInputStream fileInStream = new FileInputStream(image);
        OutputStream outStream = response.getOutputStream();

        long fileLength;
        byte[] byteStream;

        fileLength = image.length();
        byteStream = new byte[(int) fileLength];
        fileInStream.read(byteStream, 0, (int) fileLength);

        response.setContentType("image/png");
        response.setContentLength((int) fileLength);
        response.setHeader("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");

        fileInStream.close();
        outStream.write(byteStream);
        outStream.flush();
        outStream.close();

    } catch (IOException e) {
        System.err.println("Problem occurred creating chart.");
    }
}

From source file:edu.msu.cme.rdp.rarefaction.RarefactionPlotter.java

private static void plotToFile(List<RarefactionResult> resultsToPlot, int numSeqs, String title,
        File outputFile) throws IOException {
    XYSeriesCollection dataset = new XYSeriesCollection();

    for (RarefactionResult result : resultsToPlot) {
        XYSeries series = new XYSeries(RarefactionWriter.dformat.format(result.distance) + " distance");

        for (int plotIndex : RarefactionWriter.getIndiciesToPlot(numSeqs)) {
            series.add(result.geteArray()[plotIndex], plotIndex);
        }//from www. j  a v a2 s.  c  o  m

        dataset.addSeries(series);
    }

    JFreeChart chart = ChartFactory.createXYLineChart(title, "", "", dataset, PlotOrientation.HORIZONTAL, true,
            false, false);
    ChartUtilities.saveChartAsPNG(outputFile, chart, 1280, 1024);
}