开发者

Pass Dynamic value from backend to jQplot piechart?

I am using jQplot and JavaScript. I want to generate piechart from backing bean values in jQplot instead of static array.

<f:view>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <a4j:loadStyle src="../../resources/chart/css/jquery.jqplot.css"/>
        <a4j:loadScript src="../../resources/chart/jqplot-javascript/jquery-1.3.2.min.js"/>
        <a4j:loadScript src="../../resources/chart/jqplot-javascript/jquery.jqplot.min.js"/>
        <a4j:loadScript src="../../resources/chart/jqplot-javascript/jqplot.pieRenderer.min.js"/>

  <script type="text/javascript">

            var jsonPieObj = {                    
                "pageHits": [
                    ['JAN 2009', 120],
                    ['Feb 2009',60],
                    ['Mar 2009',22],
                    ['Apr 2009',5],
                    ['May 2009',60],
                    ['June 2009',30],
                    ['Jul 2009',22]]
            };

            $(function() {

                $('#pieChartButton1').click(function() {
                    $('#chartDiv').html('');                      
                    $.jqplot('chartDiv', [jsonPieObj.pageHits], CreatePieChartOptions1());
                });

            });

            function CreatePieChartOptions1()
            {
                var optionsObj = {
                    title: 'Blog Statistics',
                    legend: {
                        show: true,
                        location: 'nw'
                    },
                    seriesDefaults:{
                        shadow: true,
                        renderer:$.jqplot.PieRenderer,
                        rendererOptions:{
                            sliceMargin:10,
                            shadowOffset:1,
                            shadowAlpha:0.5,
                            shadowDepth:5
                        }
                    },
                    highlighter: {
                        showTooltip: true,
                        tooltipFade: true
                    }
                };
                return optionsObj;
            }

</script>
</head>
<body>


<h:form id="pieChartForm">
 <rich:panel id="pieChartRichPanel">

                <div>
                    <rich:panel>
                        <div id="chartDiv" style="width:600px; height:400px;" />
                    </rich:panel>
                </div>

                <div>
                    <input id="pieChartButton1" type="button" value="Pie Chart for Page Hits" />                        
                </div>                   
            </rich:panel>
</h:form>
</body>
</html>
</f:view>

My static content :

var jsonPieObj = {                    
            "pageHits": [
                ['JAN 2009', 120],
                ['Feb 2009',60],
                ['Mar 2009',22],
                ['Apr 2009',5],
                ['May 2开发者_如何转开发009',60],
                ['June 2009',30],
                ['Jul 2009',22]]
        };

Help me about this. Thanks in advance.


Create a model object representing a page hit.

public class PageHit {
    private String period;
    private Integer hits;

    // Add/generate the usual c'tor/getter/setter boilerplate.
}

Populate it in your bean somehow as a List<PageHit>.

public class Bean {
    private List<PageHit> pageHits;

    public Bean() {
        pageHits = loadItSomehow();
    }

    // Add/generate getters, etc.
}

Import JSTL core in your JSP (put jstl-1.2.jar in /WEB-INF/lib for the case you don't have it yet):

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Use JSTL <c:forEach> tag to iterate over List<PageHit> and print it as if it's a JS array:

var jsonPieObj = {                    
    "pageHits": [
    <c:forEach items="#{bean.pageHits}" var="pageHit" varStatus="loop">
        ['${pageHit.period}', ${pageHit.hits}]${!loop.last ? ',' : ''}
    </c:forEach>
]};

That's it.

Open page in webbrowser, rightclick and choose View Source to check if it has done its job properly. I.e., the generated JS code has no syntax errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜