AnAmuser
AnAmuser

Reputation: 1895

Dynamic Jfreechart

I have a problem getting primefaces dynamic image working. I think I am doing it exactly as in the example on primefaces showcase but the image isn't loaded on the page. My managed bean looks as follows.

@ManagedBean(name = "statisticsBean")
@NoneScoped
public class StatisticsBean {

    private WMCUser user;
    private BasicGroup group;
    private List<GameProgressStatistics> lineChartStats;
    private List<UserStatistics> userStatistics;
    private static final Logger logger = Logger.getLogger(StatisticsBean.class.getName());

public StatisticsBean() {
        ...
        createUserStatistics();
}

private void createUserStatistics() {
    List<WMCUser> users = GroupHelper.getNonDeletedMembers(group);
    for (WMCUser wmcUser : users) {
        userStatistics.add(new UserStatistics(
                wmcUser.getStatUser().getMakeCount(),
                wmcUser.getStatUser().getGamesPlayed(),
                wmcUser.getFirstName(),
                wmcUser.getLastName()));
    }
}

      public class UserStatistics {

        private int makeCount;
        private int gameCount;
        private String firstName;
        private String lastName;
        private StreamedContent thermoChart;

        public UserStatistics(int makeCount, int gameCount, String firstName, String lastName) {
            this.makeCount = makeCount;
            this.gameCount = gameCount;
            this.firstName = firstName;
            this.lastName = lastName;
            try {
                ThermometerPlot plot = new ThermometerPlot(createThermometerDataset());
                File chartFile = new File("dynamichart");
                JFreeChart chart = new JFreeChart(plot);
                ChartUtilities.saveChartAsPNG(chartFile, chart, 50, 50);
                thermoChart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
            } catch (Exception e) {
                logger.severe(e.getMessage());
            }
            .....
        }
        .....
    }

The JSF page looks like this

<h:form id="stats">
                    <p:dataTable  id="statsTable" var="user" value="#{statisticsBean.userStatistics}">
                        <p:column styleClass="textColumn">
                            <f:facet name="header">
                                <h:outputText value="Name" />
                            </f:facet>
                            <h:outputText value="#{user.firstName} #{user.lastName}"/>
                        </p:column>
                        ...
                        <p:column styleClass="imageColumn">
                            <f:facet name="header">
                                <h:outputText value="Luck Barometer"/>
                            </f:facet>
                            <p:graphicImage value="#{user.thermoChart}"/>
                        </p:column>
                    </p:dataTable>
   </h:form>

Can you see what i am doing wrong?

Upvotes: 3

Views: 1963

Answers (2)

jwenting
jwenting

Reputation: 5663

best not to save the image to the server, then link to it in some way. The preferred solution would have the image generated on demand and streamed to the client from a servlet, with a link to the servlet (with parameters from which to generate the image of course) being injected in the JSF instead of a link to a pregenerated image file.

Upvotes: 1

zinan.yumak
zinan.yumak

Reputation: 1590

I think, writing the chart image to disk is causing the problem. Use output stream instead. Try something like,

    try {
        ThermometerPlot plot = new ThermometerPlot(createThermometerDataset());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

        JFreeChart chart = new JFreeChart(plot);
        ChartUtilities.writeChartAsPNG(baos, chart, 50, 50);
        thermoChart = new DefaultStreamedContent( new ByteArrayInputStream(baos.toByteArray()), "image/png");
    } catch (Exception e) {
        logger.severe(e.getMessage());
    }

It will probably solve your problem.

Upvotes: 0

Related Questions