为什么这个脚本没有打印出任何图表? Flot Jquery

时间:2011-06-08 12:41:13

标签: javascript jquery html flot

下面这个脚本打印出复选框,但没有在图形窗口中绘制任何内容。

if (key && allDataSets[key])
                data.push(allDataSets[key]); 

                   

    <link href="layout.css" rel="stylesheet" type="text/css">
    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="root/include/excanvas.min.js"></script><![endif]-->
    <script language="javascript" type="text/javascript" src="root/include/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="root/include/jquery.flot.js"></script>
 </head>
    <body>
    <h1>Flot Examples</h1>

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

    <p>Here is an example with real data: military budgets for
        various countries in constant (2005) million US dollars (source: <a href="http://www.sipri.org/">SIPRI</a>).</p>

    <p>Since all data is available client-side, it's pretty easy to
       make the plot interactive. Try turning countries on/off with the
       checkboxes below.</p>

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

<script type="text/javascript">
$(function () {


    var Test1 = {
    "date": {
        label: "Date",
        data: [[1, 20110122], [2, 20110123], [3, 20110124], [4, 20110125]]
    },        
    "time": {
        label: "Time",
          data: [[1, 22.12], [2, 22.12], [3, 22.12], [4, 22.12]]
    },
   "modules": {
        label: "Modules",
         data: [[1, 22], [2, 22], [3, 22], [4, 22]]
    }, 
    "cases": {
        label: "Cases",
         data: [[1, 1312], [2, 1312], [3, 1312], [4, 1312]]
    }, 
    "failed": {
        label: "Failed",
          data: [[1, 75], [2, 77], [3, 64], [4, 55]]
    }, 
    "cover": {
        label: "Cover",
data: [[1, 13.55 ], [2, 23.55], [3, 33.55], [4, 43.55]]
    }}; 

var Test2 = {
    "date": {
        label: "Date",
        data: [[1, 20110122], [2, 20110123], [3, 20110124], [4, 20110125]]
    },        
    "time": {
        label: "Time",
          data: [[1, 22.12], [2, 22.12], [3, 22.12], [4, 22.12]]
    },
   "modules": {
        label: "Modules",
         data: [[1, 22], [2, 22], [3, 22], [4, 22]]
    }, 
    "cases": {
        label: "Cases",
         data: [[1, 1312], [2, 1312], [3, 1312], [4, 1312]]
    }, 
    "failed": {
        label: "Failed",
          data: [[1, 75], [2, 77], [3, 64], [4, 55]]
    }, 
    "cover": {
        label: "Cover",
data: [[1, 13.55 ], [2, 23.55], [3, 33.55], [4, 43.55]]
    }}; 

var Test3 = {
    "date": {
        label: "Date",
        data: [[1, 20110122], [2, 20110123], [3, 20110124], [4, 20110125]]
    },        
    "time": {
        label: "Time",
          data: [[1, 22.12], [2, 22.12], [3, 22.12], [4, 22.12]]
    },
   "modules": {
        label: "Modules",
         data: [[1, 22], [2, 22], [3, 22], [4, 22]]
    }, 
    "cases": {
        label: "Cases",
         data: [[1, 1312], [2, 1312], [3, 1312], [4, 1312]]
    }, 
    "failed": {
        label: "Failed",
          data: [[1, 75], [2, 77], [3, 64], [4, 55]]
    }, 
    "cover": {
        label: "errover",
data: [[1, 13.55 ], [2, 23.55], [3, 33.55], [4, 43.55]]
    }};
       var allDataSets = [Test1,Test2,Test3];

    // hard-code color indices to prevent them from shifting as
    // countries are turned on/off
    for(j=0; j<allDataSets.length; j++){//Going through all datasets, are there any other more simple way to do this???
    var i = 0;
    $.each(allDataSets[j], function(key, val) {
        val.color = i;
        ++i;
    });


    // insert checkboxes 
    var choiceContainer = $("#choices");
    $.each(allDataSets[j], function(key, val) {
        choiceContainer.append('<br/><input type="checkbox" name="' + key +
                               '" checked="checked" id="id' + key + '">' +
                                   '<label for="id' + key + '">'
                                    + val.label + '</label>');
        });


/**************************Here the script stops working???**********************/


        choiceContainer.find("input").click(plotAccordingToChoices);


    function plotAccordingToChoices() {
        var data = [];

        choiceContainer.find("input:checked").each(function () {
            var key = $(this).attr("name");
            if (key && allDataSets[j][key])
                data.push(allDataSets[j][key]);
        });

        if (data.length > 0)
            $.plot($("#placeholder"), data, {
                yaxis: { min: 0 },
                xaxis: { tickDecimals: 0 }
            });
    }

    plotAccordingToChoices();
    }
});

</script>

 </body>
</html>

/ * ** * ** * **** 修改 * ** * ** * ** * ** * ** * / 好的,所以我解决了第一个问题就是用allDataSets替换allDataSets [key] [j] [key]

但是当我现在取消选中一个复选框时,我得到以下错误行134 allDataSets [...]为空或不是对象。为什么我会收到此错误?换句话说,当我取消选中复选框if (key && allDataSets[j][key])

时,此行不正确

1 个答案:

答案 0 :(得分:0)

您需要做的三件事:

  1. 使用var关键字声明循环变量“j”。

    for(var j=0; j<allDataSets.length; j++) {
    
  2. 评论行之后的所有代码 - 点击处理程序等 - 需要移动该循环的

  3. 在click处理程序中,代码需要遍历“allDataSets”,然后在每个代码中查找密钥:

    choiceContainer.find("input:checked").each(function() {
        var key = $(this).attr("name");
        $.each(allDataSets, function(_, set) {
            if (set[key]) data.push(set[key]);
        });
    });
    
  4. Here是证明变化的jsfiddle。你会注意到它看起来很糟糕。那是因为你的“allDataSets”为每个丛重用了相同的键。我不知道你想要实现什么,但这些改变至少会让某些东西出现。