não mostra gráfico?

1 resposta
juniorsatanas

Pessoal boa noite...

Estou tentando fazer um gráfico igual a este :
http://www.primefaces.org/showcase-labs/ui/dynamicImage.jsf

Mas não aparece o gráfico, alguém usa outra coisa para fazer gráfico alem do Prime ?

segue meu codigo:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Utils;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;


import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

public class DynamicImageController {

    private StreamedContent graphicText;

    private StreamedContent barcode;

    private StreamedContent chart;

    public DynamicImageController() {
        try {
            //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) {
        }
    }

    public StreamedContent getBarcode() {
        return barcode;
    }

    public StreamedContent getGraphicText() {
        return graphicText;
    }

    public StreamedContent getChart() {
        return chart;
    }

    private PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("Istanbul", new Double(45.0));
        dataset.setValue("Ankara", new Double(15.0));
        dataset.setValue("Izmir", new Double(25.2));
        dataset.setValue("Antalya", new Double(14.8));

        return dataset;
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Utils;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;


import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

public class DynamicImageController {

    private StreamedContent graphicText;

    private StreamedContent barcode;

    private StreamedContent chart;

    public DynamicImageController() {
        try {
            //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) {
        }
    }

    public StreamedContent getBarcode() {
        return barcode;
    }

    public StreamedContent getGraphicText() {
        return graphicText;
    }

    public StreamedContent getChart() {
        return chart;
    }

    private PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("Istanbul", new Double(45.0));
        dataset.setValue("Ankara", new Double(15.0));
        dataset.setValue("Izmir", new Double(25.2));
        dataset.setValue("Antalya", new Double(14.8));

        return dataset;
    }
}

xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
        <title>Gerenciador de Livros</title>
        <link type="text/css" rel="stylesheet" href="dot-luv/skin.css"/>
    </h:head>
    <h:body>
        <p:layout fullPage="true">
            <p:layoutUnit position="left" width="200" header="Atividades" resizable="true" closable="true" collapsible="true">
                <h:form prependId="false">
                    <p:commandLink value="Novo Livro" actionListener="#{livroController.prepararAdicionarLivro}" update="infosLivro" oncomplete="dialogGerLivro.show()"/>
                </h:form>
            </p:layoutUnit>
            <p:layoutUnit position="center">
                <h1>Gerenciador de Livros</h1>
                <br/>
                &lt;h:form prependId="false"&gt;

                    &lt;h3&gt;JFreeChart on-the-fly&lt;/h3&gt;
&lt;p:graphicImage value="#{dynamicImageController.chart}" /&gt;

&lt;h3&gt;Barcode on-the-fly&lt;/h3&gt;
&lt;p:graphicImage value="#{dynamicImageController.barcode}" /&gt;

&lt;h3&gt;GraphicText on-the-fly&lt;/h3&gt;
&lt;p:graphicImage value="#{dynamicImageController.graphicText}" /&gt;



                    &lt;p:dataTable id="tabela" var="livro" value="#{livroController.listarLivros}"&gt;
                        &lt;p:column&gt;
                            &lt;f:facet name="header"&gt;
                                &lt;h:outputText value="Título"/&gt;
                            &lt;/f:facet&gt;
                            &lt;h:outputText value="#{livro.titulo}" /&gt;
                        &lt;/p:column&gt;
                        &lt;p:column&gt;
                            &lt;f:facet name="header"&gt;
                                &lt;h:outputText value="Autor"/&gt;
                            &lt;/f:facet&gt;
                            &lt;h:outputText value="#{livro.autor}"/&gt;
                        &lt;/p:column&gt;
                        &lt;p:column&gt;
                            &lt;f:facet name="header"&gt;
                                &lt;h:outputText value="Páginas"/&gt;
                            &lt;/f:facet&gt;
                            &lt;h:outputText value="#{livro.paginas}"/&gt;
                        &lt;/p:column&gt;
                        &lt;p:column&gt;
                            &lt;f:facet name="header"&gt;
                                &lt;h:outputText value="Editora"/&gt;
                            &lt;/f:facet&gt;
                            &lt;h:outputText value="#{livro.editora}"/&gt;
                        &lt;/p:column&gt;
                        &lt;p:column&gt;
                            &lt;f:facet name="header"&gt;
                                &lt;h:outputText value="Isbn"/&gt;
                            &lt;/f:facet&gt;
                            &lt;h:outputText value="#{livro.isbn}"/&gt;
                        &lt;/p:column&gt;
                        &lt;p:column&gt;
                            &lt;f:facet name="header"&gt;
                                &lt;h:outputText value="Avaliação"/&gt;
                            &lt;/f:facet&gt;
                            &lt;h:outputText value="#{livro.avaliacao}"/&gt;
                        &lt;/p:column&gt;
                        &lt;p:column&gt;
                            &lt;f:facet name="header"&gt;
                                &lt;h:outputText value="Alterar"/&gt;
                            &lt;/f:facet&gt;
                            &lt;p:commandButton actionListener="#{livroController.prepararAlterarLivro}" value="Alterar" update="infosLivro" oncomplete="dialogGerLivro.show()"/&gt;
                        &lt;/p:column&gt;
                        &lt;p:column&gt;
                            &lt;f:facet name="header"&gt;
                                &lt;h:outputText value="Excluir"/&gt;
                            &lt;/f:facet&gt;
                            &lt;h:commandLink action="#{livroController.excluirLivro}" value="Excluir"/&gt;
                        &lt;/p:column&gt;
                    &lt;/p:dataTable&gt;
                &lt;/h:form&gt;
            &lt;/p:layoutUnit&gt;
        &lt;/p:layout&gt;

        &lt;p:dialog header="Gerencia de Livro" widgetVar="dialogGerLivro"  resizable="false" modal="true" showEffect="slide" width="500"&gt;
            &lt;h:form prependId="false"&gt;
                &lt;h:panelGrid id="infosLivro" columns="2" style="margin-bottom:10px"&gt;

                    &lt;h:outputLabel for="titulo" value="Título:" /&gt;
                    &lt;h:inputText id="titulo" value="#{livroController.livro.titulo}"/&gt;

                    &lt;h:outputLabel for="autor" value="Autor:" /&gt;
                    &lt;h:inputText id="autor" value="#{livroController.livro.autor}"/&gt;

                    &lt;h:outputLabel for="paginas" value="Páginas:" /&gt;
                    &lt;h:inputText id="paginas" value="#{livroController.livro.paginas}"/&gt;

                    &lt;h:outputLabel for="editora" value="Editora:" /&gt;
                    &lt;h:inputText id="editora" value="#{livroController.livro.editora}"/&gt;

                    &lt;h:outputLabel for="isbn" value="ISBN:" /&gt;
                    &lt;h:inputText id="isbn" value="#{livroController.livro.isbn}"/&gt;

                    &lt;h:outputLabel for="avaliacao" value="Avaliação:" /&gt;
                    &lt;h:selectOneMenu id="avaliacao" value="#{livroController.livro.avaliacao}"&gt;
                        &lt;f:selectItem itemLabel="1" itemValue="1"/&gt;
                        &lt;f:selectItem itemLabel="2" itemValue="2"/&gt;
                        &lt;f:selectItem itemLabel="3" itemValue="3"/&gt;
                        &lt;f:selectItem itemLabel="4" itemValue="4"/&gt;
                        &lt;f:selectItem itemLabel="5" itemValue="5"/&gt;
                    &lt;/h:selectOneMenu&gt;

                    &lt;p:commandButton update="tabela" oncomplete="dialogGerLivro.hide();" actionListener="#{livroController.adicionarLivro}" value="Inserir Livro"/&gt;
                    &lt;p:commandButton update="tabela" oncomplete="dialogGerLivro.hide();" actionListener="#{livroController.alterarLivro}" value="Alterar Livro"/&gt;

                &lt;/h:panelGrid&gt;
            &lt;/h:form&gt;
        &lt;/p:dialog&gt;

    &lt;/h:body&gt;
&lt;/html&gt;

1 Resposta

guilherme.dio

Porque você não usa o Google Chart API ?

Ele é bom pra caramba.

Criado 18 de maio de 2012
Ultima resposta 19 de mai. de 2012
Respostas 1
Participantes 2