user1445209
user1445209

Reputation: 9

how to create chart in java web application

I am new to Java Web. I am using netbeans 7.0.1 and glassfish 3 to develop a web application. I am almost through with all data input and Now I want to create reports & graph. Requirement is to create charts (pie/bar/line based on user selection) with dynamic data (from database / datatable user can apply filters on data). I tried using Primefaces but it gives below error with simple example given in primeface show case:

javax.faces.event.AbortProcessingException: /pages/analyst/analyse.xhtml @65,151 actionListener="#{analysis.bar()}": java.lang.NoClassDefFoundError: org/primefaces/component/chart/series/ChartSeries

I have a jsf page that holds datatable backed by managedbean of viewscope. below is my code:

page

<html   xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui" 
        xmlns:f="http://java.sun.com/jsf/core">

    <body>
        <ui:composition template="./../../WEB-INF/Templates/VUTemplate.xhtml">
            <ui:define name="menu">
                <h:form id="home">
                    <p:menu>
                        <p:menuitem value="Home Page" url="../home.xhtml" />
                    </p:menu>
                </h:form>
            </ui:define>
            <ui:define name="content">
                <h:form id="form" prependId="false">
                    <p:growl id="growl" showDetail="true"/>  

                    <p:barChart id="basic" value="#{analysis.categoryModel}" legendPosition="ne"
                                title="Basic Bar Chart" min="0" max="200" style="height:300px"/>

                    <p:dataTable id="table" var="var" widgetVar="wbTable"
                                 value="#{analysis.list}" 
                                 paginator="true" rows="10" 
                                 paginatorPosition="bottom"
                                 emptyMessage="No data found with given criteria"
                                 >                                

                        <f:facet name="header">  
                            World Bank Open Data Analysis
                        </f:facet>  

                        <p:column filterBy="#{var.regionFullName}" sortBy="#{var.regionFullName}"
                                  headerText="Region" footerText="Enter starting characters" >  
                            <h:outputText   value="#{var.regionFullName}" />
                        </p:column>

                        <p:column filterBy="#{var.countryFullName}" sortBy ="#{var.countryFullName}"
                                  headerText="Country" footerText="Enter starting characters" >
                            <h:outputText   value="#{var.countryFullName}" />
                        </p:column>

                        <p:column  filterBy="#{var.indcatorTypeName}" sortBy ="#{var.indcatorTypeName}"
                                   headerText="Indicator Type" footerText="Enter starting characters" >
                            <h:outputText   value="#{var.indcatorTypeName}" />
                        </p:column>

                        <p:column  filterBy="#{var.indicatorName}" sortBy ="#{var.indicatorName}"
                                   headerText="Indicator" footerText="contains" filterMatchMode="contains">
                            <h:outputText   value="#{var.indicatorName}" />
                        </p:column>

                        <p:column  filterBy="#{var.year}" sortBy ="#{var.year}" 
                                   headerText="Year" footerText="starts with">
                            <h:outputText value="#{var.year}" />
                        </p:column>

                        <p:column  filterBy="#{var.dataValue}" sortBy ="#{var.dataValue}"
                                   headerText="Data Value" footerText="starts with" >
                            <h:outputText value="#{var.dataValue}" />
                        </p:column>
                    </p:dataTable>

                    <h:panelGrid id="command" columns="6"  cellpadding="4" >
                        <p:commandButton id="line" value="Line" actionListener="#{analysis.line()}"  process="@this :form:table"  update="result  table" >                                
                        </p:commandButton>
                        <p:commandButton id="bar" value="Bar" actionListener="#{analysis.bar()}" process="@this :form:table"  update="result  table" >                                
                        </p:commandButton>
                    </h:panelGrid>
                    <p:messages id="result" showDetail="true" autoUpdate="true"/>  

                </h:form>
            </ui:define>
        </ui:composition>
    </body>
</html>

backbean

import ejb.VwdataFacade;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import model.Vwdata;
import org.primefaces.component.chart.series.ChartSeries;
import org.primefaces.model.chart.CartesianChartModel;

@ManagedBean
@ViewScoped
public class Analysis implements Serializable{
    private List<Vwdata> list;

    private @EJB VwdataFacade wsvc;  //entity services    
    private CartesianChartModel categoryModel;

    public Analysis() {
    }   

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    @PostConstruct
    public void init() { 
        list = wsvc.findAll();
     }

    public List<Vwdata> getList() {
//        list = wsvc.findAll();
        return list;
    }
    public void line(){

    }

    public void bar(){
        createCategoryModel();
    }

    private void createCategoryModel() { 
        categoryModel = new CartesianChartModel();  
        ChartSeries yaxis = new ChartSeries();
        yaxis.setLabel("Label");          
        yaxis.set("2004",120);
        yaxis.set("2005",130);
        yaxis.set("2006",140);
        yaxis.set("2007",110);
        categoryModel.addSeries(yaxis); 
    }

}

If I place call createCategoryModel in the constructor or init method, I cant even view the page which otherwise was working perfectly without chart code. Any help will be highly appreciated. Thanks

Upvotes: 0

Views: 2129

Answers (1)

BalusC
BalusC

Reputation: 1108682

java.lang.NoClassDefFoundError: org/primefaces/component/chart/series/ChartSeries

The mentioned class was introduced in PrimeFaces 3.0 and was thus not available in PrimeFaces 2.x and older.

Given the fact that the PrimeFaces 3.0 specific XML namespace http://primefaces.org/ui and all the <p:xxx> tags in your code did apparently not result in a Facelets compile error, that can only mean that you've both PrimeFaces 2.x and 3.x libraries in your webapp's runtime classpath wherein the 2.x one got precedence in classloading.

Get rid of the offending old PrimeFaces 2.x library from your webapp's runtime classpath and this problem should then disappear.

Upvotes: 1

Related Questions