【问题标题】:Cannot implement Primefaces chart properly无法正确实现 Primefaces 图表
【发布时间】:2012-07-31 20:04:24
【问题描述】:

我想实现 Primefaces 图表。但是我没有正确设置图表的图例:

这是我用来构建图表的代码:

<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 提出的代码中,我得到了这张图表:

【问题讨论】:

    标签: javascript jsf jsf-2 charts primefaces


    【解决方案1】:

    您应该像这样使用单独的 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);
    
    }
    

    【讨论】:

    • 我测试了您提出的代码。列的名称不正确 - 它们共享一个名称。我想像第一张图片一样单独命名它们。
    • @user1285928,我不知道这是否是正确的做法。但我已经更新它以满足您的要求。如果我得到比这更好的解决方案,我会再次发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2018-02-14
    相关资源
    最近更新 更多