jqplot多个数组作为参数

时间:2014-04-11 07:13:53

标签: jquery ajax servlets jqplot

您好我使用servlet响应为jqplot提供barchart的参数

servlet响应语句是这样的:

out.println("725,759,2032,171,2761,133175,166163,409,28630,3754@0,375,1482,0,2051,125857,167524,0,125406,4022@0,0,0,1,0,125406,0,0,0,0");

如果是成功响应,ajax将使用dataRenderer: dataRenderer(dataArr)作为数组参数调用绘图函数。 但是,错误表示"没有指定数据"找到了

我尝试记录dataRenderer返回

"725,759,2032,171,2761,133175,166163,409,28630,3754,0,375,1482,0,2051,125857,167524,0,125406,4022,0,0,0,1,0,125406,0,0,0,0"

但预期的答案应该是

[725,759,2032,171,2761,133175,166163,409,28630,3754],[0,375,1482,0,2051,125857,167524,0,125406,4022],[0,0,0,1,0,125406,0,0,0,0]

非常感谢任何帮助!感谢

function log(msg) {
setTimeout(function() {
    throw new Error(msg);
}, 0);
}

var form = $('#form1');
var storedData = [3, 7];
var plot1;

//$( "#plot" ).click(function(){
form.submit(function(){
 $.ajax({
 type: form.attr('method'),
 url: form.attr('action'),
 data: {n1:n1.value,n2:n2.value},
 success: function (data) {
 var result=data;
 //$('#result').attr("value",result);
 renderGraph(result);
 }
 });
 return false;
 });


 var dataRenderer = function(userData){

    var result = new Array();


    var seperator1 = /\s*@\s*/;
    var seperator2 = /\s*,\s*/;

    storedData = userData.split(seperator1);
    result.push(storedData[0].split(seperator2));
    result.push(storedData[1].split(seperator2));
    result.push(storedData[2].split(seperator2));

    return result;
 };

 function renderGraph(dataArr) {

if (plot1) {
    plot1.destroy();

}


 log(dataRenderer(dataArr));



var ticks = ['Pub1', 'Pub101', 'Pub121', 'Pub141', 'Pub161', 'Pub2', 'Pub3', 'Pub61', 'Pub81', 'Pub82'];

    var plot1 = $.jqplot('chart1', [], {

                    dataRenderer: dataRenderer(dataArr),
        seriesColors : ['#1cbd17', '#e44e23', '#23b9e4', '#C7754C', '#17BDB8', '#b817bd'],
        seriesDefaults : {
            renderer : $.jqplot.BarRenderer,
            rendererOptions : {
                fillToZero : true
            }
        },
        series : [{
                label : 'delivered'
            }, {
                label : 'filtered out'
            }, {
                label : 'incomplete'
            }, {
                label : ''
            }
        ],
        legend : {
            show : true,
            placement : 'outsideGrid'
        },
        axes : {
            xaxis : {
                renderer : $.jqplot.CategoryAxisRenderer,
                ticks : ticks
            },
            yaxis : {
                pad : 1.05,
                tickOptions : {
                    formatString : '%d'
                }
            }
        }

    });  

    plot1.resetAxesScale(); 
    plot1.replot();
}
 </script>

1 个答案:

答案 0 :(得分:0)

好的,解决了! jqplot假设采用一个int数组,而我正在提供一个str数组。 这就是我收到错误的原因。

这是修复:

        result.push(storedData[0].split(seperator2).map(Number));
        result.push(storedData[1].split(seperator2).map(Number));
        result.push(storedData[2].split(seperator2).map(Number));

希望如果其他人遇到同样的问题,这会有所帮助。