什么'这是错误的自动创建图表花在网页上的时间

what's wrong with this automatic creation of charts for time spent on webpages?

本文关键字:时间 网页 创建 什么 错误      更新时间:2023-09-26

!解决了

我想自动创建图表,包括用户及其在网站页面上花费的时间

我有一个文件——"log.xml",我在其中保存用户(客户(的信息、访问的页面、日期和他们花费的时间;在我用Ajax"获得"这个Xml文件后,我想解析它,并用JqPlot创建带有值的"提取"图表。

我的问题是我不能遍历多个客户,而且它不能为单个客户构建图表。

如果我在初始化变量plot时删除代码块,我<strong]可以通过Xml中的所有客户循环>。

请,如果有人能告诉我哪里出了问题,以及如何为所有客户创建图表。。。

以下是文件">log.xml"的代码:

<?xml version="1.0"?>
<log>
  <customer id="14" name="Florin Virdol">
    <page name="/mobilestore/index.php">
      <date_ts on="2011-12-02" timeSpent="205"/>
    </page>
    <page name="/mobilestore/products_all.php">
      <date_ts on="2011-12-02" timeSpent="15"/>
    </page>
  </customer>
  <customer id="0" name="guest">
   <page name="/mobilestore/services.php">
      <date_ts on="2011-12-02" timeSpent="50"/>
    </page>
  </customer>
</log>

以下是"操作"的javascript代码:

$(document).ready(function()
{
    //read from xml
    $.ajax({
        type: "GET",
        url: "log.xml",
        dataType: "xml",
        success: parseXml
    });
});//ready          
//parse xml          
function parseXml(xml) {
    var i = 0;
    $(xml).find("customer").each(function() {
        $('<div class = "jqplot graph" id = "chart' + i + '"></div>').appendTo('#content');
        var customerName = $(this).attr("name");                                     
        var line_inside = [];  // declare as array               
        $(this).find("page").each(function() {
          var pageName = $(this).attr("name"); 
          $(this).find("date_ts").each(function() {
            var timeSpent_ = $(this).attr("timeSpent");//if mai multe timespent, sa faca totalul, else singuru; timespent
            line_inside.push([pageName,timeSpent_]); //do not string cat, push onto array
          });                        
        });                    
        var line = '[' + line_inside + ']';
        //--------jqplot----!!! if i remove this block, will loop through customers------------
        var plot = $.jqplot('chart' + i, [line_inside], 
        {                            
            title: customerName,
            series:[{renderer:$.jqplot.BarRenderer}],
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    label: 'Web Page',
                    labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
                    tickRenderer: $.jqplot.CanvasAxisTickRenderer,
                    tickOptions: { labelPosition:'middle',  angle: -30 }  
                },
                yaxis: {
                    autoscale:true,
                    label: 'Total Time Spent',
                    labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
                    tickRenderer: $.jqplot.CanvasAxisTickRenderer,
                    tickOptions: { labelPosition:'middle', angle: -30 }
                }
            }
        });
        //-------jqplot----!!! if i remove this block, will loop through customers------------                                
        i++;
    });//find customer            
}//parse xml    

解决方案:按照标记的建议进行了修改,并且有效。(现在,上面的代码工作!(

您正在将看起来像数组而不是实际数组的字符串传递给jqplot。

尝试:

var line_inside = [];  // declare as array               
$(this).find("page").each(function() {
  var pageName = $(this).attr("name"); 
  $(this).find("date_ts").each(function() {
    var timeSpent_ = $(this).attr("timeSpent");//if mai multe timespent, sa faca totalul, else singuru; timespent
    line_inside.push([pageName,timeSpent_]); //do not string cat, push onto array
  });                        
});
alert(line_inside); // this is an array of arrays                                   
var plot = $.jqplot('chart' + i, [line_inside], 

因为您希望每个客户都有不同的绘图,所以您需要将plot作为内联函数的本地var:"var plot"而不仅仅是"plot"——"line"也是如此。当前,您正在分配一个全局作用域变量并每次覆盖它。