需要帮助在flot jquery中编写两个javascript函数

时间:2011-06-08 14:27:29

标签: javascript jquery html flot

编辑*的 * **

我想要帮助编写下面的两个函数“plotSingleCoverage”和plotAllCoverage。我遇到的主要问题是我不知道如何搜索页面上的所有现有数据集,然后在数据集中写出单个数组,或者在不同数据集中写出相同类型的所有数组。因此,如果有人能够把我推向正确的方向,我会非常高兴=)

                            剧情示例                                                   

    <div id="placeholder" style="width:600px;height:300px;"></div>

    <p id="choices">Show:</p>

<a href="javascript:plotSingleCoverage('Test1')">Show coverage of first test</a>
<a href="javascript:plotAllCoverage()">Show coverage of all tests</a>


<script type="text/javascript">
$(function () {
    var Test1 = {
        "date": {
            label: "Date",
            data: [[1, 201], [2, 201], [3, 201], [4, 125]]
        },        
        "time": {
            label: "Time",
              data: [[1, 209], [2, 201], [3, 201], [4, 134]]
        },
       "modules": {
            label: "Modules",
             data: [[1, 201], [2, 201], [3, 201], [4, 125]]
        }, 
        "cases": {
            label: "Cases",
             data: [[1, 201], [2, 201], [3, 201], [4, 125]]
        }, 
        "failed": {
            label: "Failed",
              data: [[1, 201], [2, 201], [3, 201], [4, 125]]
        }, 
        "cover": {
            label: "Cover",
data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }
}; 

var Test2 = {
        "date": {
            label: "Date",
            data: [[1, 201], [2, 201], [3, 201], [4, 125]]
        },        
        "time": {
            label: "Time",
              data: [[1, 201], [2, 201], [3, 201], [4, 125]]
        },
       "modules": {
            label: "Modules",
             data: [[1, 201], [2, 201], [3, 201], [4, 125]]
        }, 
        "cases": {
            label: "Cases",
             data: [[1, 101], [2, 201], [3, 201], [4, 45]]
        }, 
        "failed": {
            label: "Failed",
              data: [[1, 301], [2, 454], [3, 43], [4, 125]]
        }, 
        "cover": {
            label: "Cover",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
        }
}; 

var Test3 = {
    "date": {
        label: "Date",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    },        
    "time": {
        label: "Time",
          data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    },
   "modules": {
        label: "Modules",
         data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }, 
    "cases": {
        label: "Cases",
         data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }, 
    "failed": {
        label: "Failed",
          data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }, 
    "cover": {
        label: "errover",
    data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }
};

/*Need help to write this function, it shall go to that dataset that its argument is, for example in this case it shall go to Test1 and read that cover and plot it in the #placeholder*    
function plotSingleCoverage(coverageToPlot){
    $.plot($("#placeholder"), [???]);
}
/*This function shall simply go through all avaible datasets and plot all cover array's*/
function plotAllCoverage(){
    $.plot($("#placeholder"), [???]);
}

});
</script>

 </body>
</html>

提前致谢=)

1 个答案:

答案 0 :(得分:0)

您有一些无法以方便的方式访问的变量。如何将它们保存在一个数组中,以便您可以轻松地循环它们并搜索正确的数据集以对其进行操作? (来自我上面的评论)

您可以创建一个可用数据集数组,如下所示:var datasets = new Array();。 然后,在定义新数据集时,请datasets[] = { ...... }而不是每个var Test1 = { ... };

var datasets = new Array();
datasets[0] = {
    "date": {
        label: "Date",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    },        
    "time": {
        label: "Time",
        data: [[1, 209], [2, 201], [3, 201], [4, 134]]
    },
   "modules": {
        label: "Modules",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }, 
    "cases": {
        label: "Cases",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }, 
    "failed": {
        label: "Failed",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }, 
    "cover": {
        label: "Cover",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }
}; 

datasets[1] = {
    "date": {
        label: "Date",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    },        
    "time": {
        label: "Time",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    },
   "modules": {
        label: "Modules",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }, 
    "cases": {
        label: "Cases",
        data: [[1, 101], [2, 201], [3, 201], [4, 45]]
    }, 
    "failed": {
        label: "Failed",
        data: [[1, 301], [2, 454], [3, 43], [4, 125]]
    }, 
    "cover": {
        label: "Cover",
        data: [[1, 201], [2, 201], [3, 201], [4, 125]]
    }
}; 

然后,您可以像这样访问数据集:

alert(datasets[1]);
// datasets[0] holds the first test data, datasets[1] the second, and so on...
$.plot($("#placeholder"), datasets[0]);

你的plotSingleCoverage可以是:

function plotSingleCoverage(dateset){
    $.plot($("#placeholder"), dataset);
}

你可以像这样使用它:

plotSingleCoverage(datasets[0]);