2012-07-31 73 views
0

我想實現Primefaces圖表。但我無法正確設置圖表的傳說:無法正確實現Primefaces圖表

enter image description here

這是我用它來構建圖表代碼:

<h:form> 

    <p:barChart id="basic" value="#{DashboardController.categoryModel}" legendPosition="ne" 
       title="Accounts and Groups" min="0" max="#{DashboardController.chartMaxSize}" style="height:400px" 
       shadow="true" barPadding="90" seriesColors="4D94FF, 1975FF, 005CE6, 0047B2" 
       yaxisLabel="Size"/> 
</h:form> 

Java代碼:

private void createCategoryModel() throws SQLException, Exception 
    { 
     categoryModel = new CartesianChartModel(); 

     // Active Accounts 

     ChartSeries chart = new ChartSeries(); 
     chart.setLabel("Active Accounts"); 

     chart.set("Active Accounts", countDBActiveAccounts()); 

     // Blocked Accounts 

     chart.setLabel("Blocked Accounts"); 

     chart.set("Blocked Accounts", countDBBlockedAccounts()); 


     // Active Groups 

     chart.setLabel("Active Groups"); 

     chart.set("Active Groups", countDBActiveGroups()); 


     // Blocked Groups 

     chart.setLabel("Blocked Groups"); 

     chart.set("Blocked Groups", countDBBlockedGroups()); 

     categoryModel.addSeries(chart); 



    } 

你能幫我正確構建圖表嗎?

P.S從@Ravi提出的代碼我得到這個圖表:

enter image description here

回答

2

您應該使用ChartSeries中的不同實例這樣

private void createCategoryModel() throws SQLException, Exception 
{ 
    categoryModel = new CartesianChartModel(); 

    // Active Accounts 

    ChartSeries actAcc = new ChartSeries(); 
    actAcc .setLabel("Active Accounts"); 

    actAcc.set("Active Accounts", countDBActiveAccounts()); 
    actAcc.set("Blocked Accounts", 0); 
    actAcc.set("Active Groups", 0); 
    actAcc.set("Blocked Groups", 0); 

    // Blocked Accounts 

    ChartSeries blocAcc = new ChartSeries(); 
    blocAcc.setLabel("Blocked Accounts"); 

    blocAcc.set("Active Accounts", 0); 
    blocAcc.set("Blocked Accounts", countDBBlockedAccounts()); 
    blocAcc.set("Active Groups", 0); 
    blocAcc.set("Blocked Groups", 0); 


    // Active Groups 

    ChartSeries actGrp = new ChartSeries(); 
    actGrp.setLabel("Active Groups"); 

    actGrp.set("Active Accounts", 0); 
    actGrp.set("Blocked Accounts", 0); 
    actGrp.set("Active Groups", countDBActiveGroups()); 
    actGrp.set("Blocked Groups", 0); 


    // Blocked Groups 

    ChartSeries blocGrp = new ChartSeries(); 
    blocGrp.setLabel("Blocked Groups"); 

    blocGrp.set("Active Accounts", 0); 
    blocGrp.set("Blocked Accounts", 0); 
    blocGrp.set("Active Groups", 0); 
    blocGrp.set("Blocked Groups", countDBBlockedGroups()); 

    categoryModel.addSeries(actAcc); 
    categoryModel.addSeries(blocAcc); 
    categoryModel.addSeries(actGrp); 
    categoryModel.addSeries(blocGrp); 

} 
+1

我測試了你提出的代碼。列的名稱不正確 - 它們共享一個名稱。我想像第一張照片一樣單獨命名它們。 – user1285928 2012-08-01 09:23:50

+1

@ user1285928,我不知道這是否是正確的做法。但我已經更新它來滿足您的要求。如果我得到比這更好的解決方案,我會再次發佈。 – Ravi 2012-08-02 14:18:32