Dynamic Jfreechart

I have a problem with the operation of the GUI. I think that I am doing this in exactly the same way as in the example in the showcase of strokes, but the image does not load on the page. My managed bean is 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>

Do you see what I'm doing wrong?

+3
source share
2 answers

, - . , , ( , , , ), JSF .

+1

, . . - :

    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());
    }

, .

0

Source: https://habr.com/ru/post/1781228/


All Articles