Example usage for org.jfree.chart ChartUtilities writeChartAsPNG

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

Introduction

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

Prototype

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

Source Link

Document

Writes a chart to an output stream in PNG format.

Usage

From source file:Interface.Teste.java

public void criacao() {
    DefaultCategoryDataset ds = new DefaultCategoryDataset();
    ds.addValue(40.5, "maximo", "dia 1");
    ds.addValue(38.2, "maximo", "dia 2");
    ds.addValue(37.3, "maximo", "dia 3");
    ds.addValue(31.5, "maximo", "dia 4");
    ds.addValue(35.7, "maximo", "dia 5");
    ds.addValue(42.5, "maximo", "dia 6");

    // cria o grfico
    JFreeChart grafico = ChartFactory.createLineChart("Meu Grafico", "Dia", "Valor", ds,
            PlotOrientation.VERTICAL, true, true, false);
    try {/*  ww w. ja va2 s  . c o m*/
        OutputStream arquivo = new FileOutputStream("grafico.png");
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 550, 400);
        arquivo.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    JPopupMenu pop = new JPopupMenu();
    pop.add(new JMenuItem("1"));
    pop.add(new JMenuItem("2"));
    pop.add(new JMenuItem("3"));
    pop.add(new JMenuItem("4"));
    JPanel painel = new JPanel();
    JButton botao = new JButton("teste");
    JCalendar cal = new JCalendar();
    botao.add(pop);
    painel.add(cal);
    painel.add(new ChartPanel(grafico));
    //Calendar c = cal.getDate();
    Date data = cal.getDate();
    int dia = data.getDay();
    System.out.println("" + dia);

    add(painel);
}

From source file:de.tudarmstadt.ukp.clarin.webanno.monitoring.support.ChartImageResource.java

@Override
protected byte[] getImageData(Attributes aAttributes) {
    try {// w  w w  . ja  v  a  2s  .  c  o  m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ChartUtilities.writeChartAsPNG(bos, chart, width, height);
        return bos.toByteArray();
    } catch (IOException e) {
        return new byte[0];
    }
}

From source file:com.sun.portal.os.portlets.PortletChartUtilities.java

public static void writeChartImageData(ResourceRequest request, ResourceResponse response,
        String defaultImagePath) throws IOException {
    response.setContentType(CONTENT_TYPE_IMG_PNG);
    OutputStream out = response.getPortletOutputStream();

    try {//from   w w  w .ja v a  2s  .co  m
        JFreeChart chart = createChart(request);
        if (chart != null) {
            ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
        } else {
            // A problem occurred - Stream out the default image
            writeDefaultImage(out, defaultImagePath);
        }
    } catch (Exception e) {
        System.err.println(e.toString());
    } finally {
        out.close();
    }
}

From source file:Controlador.ChartServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("image/png");
    OutputStream outputStream = response.getOutputStream();
    JFreeChart chart = null;/*from w  w w.  j  a va 2s. c  om*/

    try {
        chart = getChart();
    } catch (URISyntaxException ex) {
        Logger.getLogger(ChartServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    int width = 500;
    int height = 350;
    ChartUtilities.writeChartAsPNG(outputStream, chart, width, height);

}

From source file:org.usip.osp.graphs.GraphServer.java

/**
 * Attempts to print out png file and close output stream.
 * //  w w  w .  j  a  v  a 2  s .  c  o  m
 * @param out
 * @param cci
 * @throws IOException
 */
public static void writeChartAsPNG(OutputStream out, Chart cci) throws IOException {

    try {

        ChartUtilities.writeChartAsPNG(out, cci.getThis_chart(), cci.getWidth(), cci.getHeight());

    } catch (Exception e) {

        out.write(e.getMessage().getBytes());

    } finally {
        out.close();
    }
}

From source file:ch.goodsolutions.olat.jfreechart.JFreeChartMediaResource.java

/**
 * @see org.olat.core.gui.media.MediaResource#getInputStream()
 *///www.j a  v  a2  s  . c o m
@Override
public InputStream getInputStream() {
    ByteArrayInputStream pIn = null;
    try {
        final ByteArrayOutputStream pOut = new ByteArrayOutputStream();
        ChartUtilities.writeChartAsPNG(pOut, chart, width.intValue(), height.intValue());
        pIn = new ByteArrayInputStream(pOut.toByteArray());
    } catch (final IOException e) {
        // bummer...
    }
    return pIn;
}

From source file:net.bioclipse.model.ImageWriter.java

public static void saveImagePNG(String path, JFreeChart chart) throws IOException {
    FileOutputStream fos = new FileOutputStream(path);
    ChartUtilities.writeChartAsPNG(fos, chart, 640, 480);
    fos.close();/*from ww  w.ja va 2 s.c o  m*/
}

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

public void createPNG(OutputStream res) throws IOException {
    ChartUtilities.writeChartAsPNG(res, getCustomisedJFreeChart(), DEFAULT_X_SIZE, DEFAULT_Y_SIZE);
}

From source file:edu.emory.library.tast.database.graphs.GraphsServlet.java

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

    HttpSession session = request.getSession();
    OutputStream stream = response.getOutputStream();

    int width = Integer.parseInt(request.getParameter("width"));
    int height = Integer.parseInt(request.getParameter("height"));

    JFreeChart chart = (JFreeChart) session.getAttribute(GraphsBean.SESSION_KEY_GRAPH);

    if (chart == null) {
        response.encodeRedirectURL("../../images/blank.png");
        return;//from w ww .  j  a  v  a2 s. c  o  m
    }

    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Cache-Control", "max-age=0");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);

    response.setContentType("image/png");
    ChartUtilities.writeChartAsPNG(stream, chart, width, height);

}

From source file:filtros.histograma.Histograma.java

public static void Apply(BufferedImage image, String savePath) {
    //Chamada do metodo que gera os dados do histograma
    GetData(image);//  www .  j a  v a2  s .c  o m

    //Dataset que gera o grafico
    DefaultCategoryDataset ds = new DefaultCategoryDataset();

    //Loop que percorre o array de dados do histograma
    for (int k = 0; k < data.length; k++) {
        //Adiciona o valor ao dataset
        ds.setValue(data[k], "Imagem", Integer.toString(data[k]));
    }

    //Gera o grafico
    JFreeChart grafico = ChartFactory.createLineChart("Histograma", "Tons de cinza", "Valor", ds,
            PlotOrientation.VERTICAL, true, true, false);

    //Salva o grafico em um arquivo de png
    try {
        OutputStream arquivo = new FileOutputStream(savePath);
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 550, 400);
        arquivo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}