如何使用JSF2 Primefaces请求上下文将JSON发送到Highchart? [英] How to send JSON using JSF2 Primefaces Request Context to Highchart?

查看:156
本文介绍了如何使用JSF2 Primefaces请求上下文将JSON发送到Highchart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经为此工作了几个小时,我想使用Primefaces请求上下文将JSON格式的POJO发送到JSF内的Highchart来更新其值. 基本上我是从他自己的stackoverflow

Hi I've been strugling with this for hours, I want to send my POJO in JSON format using Primefaces Request Context to a Highchart inside my JSF to update its value. Basically I am following this solution of @Bhesh Gurung from his own stackoverflow question but I cant seem to make it work. Right now it is throwing a:

找不到从"j_idt31"引用的标识符为"pieData"的组件.

Cannot find component with identifier "pieData" referenced from "j_idt31".

我想通过Primefaces请求上下文成功地使用JSON数据创建一个图表.请提前帮助.

I want to sucessfully create a highchart using the JSON data through the Primefaces Request Context. Please help Thanks in advance.

这是我的下面的代码

@ManagedBean
@ViewScoped

public class PieDataProvider {

        public void retrievePieData() { 
            List<String> categories = new ArrayList<String>();
            categories.add("Electronic");
            categories.add("Food");
            categories.add("Liguor");
            categories.add("Stationary");
            categories.add("Mechanical");

            List<Integer> itemCounts = new ArrayList<Integer>();

            itemCounts.add(5);
            itemCounts.add(20);
            itemCounts.add(1);
            itemCounts.add(50);
            itemCounts.add(10);

            RequestContext reqCtx = RequestContext.getCurrentInstance();
            reqCtx.addCallbackParam("categories", new Gson().toJson(categories));
            reqCtx.addCallbackParam("itemCounts", new Gson().toJson(itemCounts));
            System.out.println(categories);
        }

 }

我的xhtml page5.xhtml

My xhtml page5.xhtml

<?xml version="1.0" encoding="UTF-8"?>

<ui:composition template="/template/common/commonLayout.xhtml">

    <ui:define name="content">
        <h:head>

            <script type="text/javascript">
                src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"
                src="http://code.highcharts.com/highcharts.js"
                src="http://code.highcharts.com/modules/exporting.js"
            </script>


            <script type="text/javascript">
                function feedPieData(xhr, status, args) {
                    var categories = eval('(' + args.categories + ')');
                    var itemCounts = eval('(' + args.itemCounts + ')');

                    options.xAxis.categories = categories;

                    var series = {
                        data : []
                    };

                    series.name = new Date().toString();
                    series.data = itemCounts;

                    options.series = [ series ];

                    chart = new Highcharts.Chart(options);
                }
            </script>

        </h:head>
        <h:body>
            <p:commandLink action="#{pieDataProvider.retrievePieData}"
                oncomplete="feedPieData(xhr, status, args);" value="Pie chart demo"
                update="pieData" />
        </h:body>
    </ui:define>

    <ui:define name="footer">
        <h2>This is page5 Footer</h2>
    </ui:define>

</ui:composition>

更新:已修改的XHTML

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:body>
    <ui:composition template="/template/common/commonLayout.xhtml">
        <ui:define name="content">
            <h:head>
                <script type="text/javascript"
                    src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
                <script type="text/javascript">
                    $(function() {
                        $('#container')
                                .highcharts(
                                        {
                                            chart : {
                                                plotBackgroundColor : null,
                                                plotBorderWidth : null,
                                                plotShadow : false
                                            },
                                            title : {
                                                text : 'Most used words, 2010'
                                            },
                                            tooltip : {
                                                pointFormat : '{series.name}: {point.percentage}',
                                                percentageDecimals : 1
                                            },
                                            plotOptions : {
                                                pie : {
                                                    allowPointSelect : true,
                                                    cursor : 'pointer',
                                                    dataLabels : {
                                                        enabled : true,
                                                        color : '#000000',
                                                        connectorColor : '#000000',
                                                        formatter : function() {
                                                            return '<b>'
                                                                    + this.point.name
                                                                    + '</b>: '
                                                                    + this.percentage
                                                            //+ ' %'
                                                            ;
                                                        }
                                                    }
                                                }
                                            },
                                            series : [ {
                                                type : 'pie',
                                                name : 'Browser share',
                                                data : [ [ 'Red', 45.0 ],
                                                        [ 'Orange', 26.8 ], {
                                                            name : 'Yellow',
                                                            y : 12.8,
                                                            sliced : true,
                                                            selected : true
                                                        }, [ 'Green', 8.5 ],
                                                        [ 'Blue', 6.2 ],
                                                        [ 'Violet', 0.7 ] ]
                                            } ]
                                        });
                    });
                </script>
                <script type="text/javascript">
                    function feedPieData(xhr, status, args) {
                        var categories = JSON.parse(args.categories);
                        var itemCounts = JSON.parse(args.itemCounts);

                        var series = {
                            data : []
                        };
                        options.series[0].data.length = 0;
                        series.data = categories;
                        series.data = itemCounts;

                        options.series = [ series ];

                        chart = new Highcharts.Chart(options);
                    }
                </script>

                <script src="http://code.highcharts.com/highcharts.js"></script>
                <script src="http://code.highcharts.com/modules/exporting.js"></script>
            </h:head>
            <h:body>

                <div id="container"
                    style="min-width: 400px; height: 400px; margin: 0 auto"></div>
                <h:form>
                    <p:commandLink action="#{pieDataProvider.retrievePieData}"
                        oncomplete="feedPieData(xhr, status, args);"
                        value="Pie chart demo" />
                </h:form>
            </h:body>
        </ui:define>

        <ui:define name="footer">
            <h2>This is page5 Footer</h2>
        </ui:define>

    </ui:composition>

</h:body>

</html>

推荐答案

您似乎无法理解Highcharts是一个JavasScript环境,该环境仅在客户端上运行,而PrimeFaces/JSF在该环境中.服务器端,它们在这种情况下仅充当HTML,CSS和JavaScript代码生成器.

You seem to fail to understand that Highcharts is a JavasScript environment that runs solely on the client, while PrimeFaces/JSF is on the server side and they act in this context simply as HTML, CSS and JavaScript code generator.

在您的特定情况下,PrimeFaces仅用于通过RequestContext 从服务器向客户端发送数据.在AJAX调用有效完成之后,客户端将接收序列化的数据,您可以调用JavaScript函数,该函数根据接收到的数据完全在客户端上创建Highcharts JS组件.

In your particular case, PrimeFaces is simply used to send data from the server to the client via RequestContext. After AJAX call completes and effectively, serialized data is received by the client, you call a JavaScript function that creates a Highcharts JS component entirely on the client from the received data.

总而言之,它为我们提供了以下设置.

All in all, it yields us with the following setup.

**视图元素**

<h:form>
    <p:commandLink action="#{pieDataProvider.retrievePieData}"
                   oncomplete="feedPieData(xhr, status, args);"
                   value="Generate pie chart" />
</h:form>

** JavaScript **

** The JavaScript **

<script type="text/javascript">
    function feedPieData(xhr, status, args) {
        var categories = JSON.parse(args.categories);//serialized data from server
        var itemCounts = JSON.parse(args.itemCounts);//serialized data from server

        //next you create the chart and show it
    }
</script>

**动作方法**

public void retrievePieData() { 
    List<String> categories = generateCategories();
    List<Integer> itemCounts = generateItems();
    RequestContext reqCtx = RequestContext.getCurrentInstance();
    reqCtx.addCallbackParam("categories", new Gson().toJson(categories));//additional serialized data to be sent
    reqCtx.addCallbackParam("itemCounts", new Gson().toJson(itemCounts));//additional serialized data to be sent
}

这篇关于如何使用JSF2 Primefaces请求上下文将JSON发送到Highchart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆