2011-06-10 64 views
1

嗨,我有兩個關於下面的腳本的問題。需要關於數據集的建議flot jquery

問題1:爲什麼只Test1date,Test1time,Test1Test1modules等等....讓不同勢的色彩,而Test1date,Test2date,Test3date等的顏色......得到相同的顏色,我該怎麼改變這個??

問題2:我只想打印出下面「失敗」和「封面」的複選框,但是如何檢查一個鍵是否含有「封面」或「失敗」?至於現在我檢查一個密鑰是否等於「Test3failed」或「Test2cover」。但我只是想檢查關鍵==一些含提前「捂」或「失敗」 感謝=)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Flot Examples</title> 
    <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> 
    <script language="javascript" type="text/javascript" src="root/include/jquery.flot.crosshair.js"></script> 
</head> 
    <body> 
    <h1>Flot Examples</h1> 

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


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

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

     var Name1 = { 
    "Test1date": { 
     label: "COSDate", 
     data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    },   
    "Test1time": { 
     label: "Time", 
      data: [[1, 209], [2, 201], [3, 201], [4, 134]] 
    }, 
    "Test1Test1modules": { 
     label: "Modules", 
     data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }, 
    "Test1cases": { 
     label: "Cases", 
     data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }, 
    "Test1failed": { 
     label: "Failed", 
      data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }, 
    "Test1cover": { 
     label: "Cover", 
data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }}; 

var Name2 = { 
    "Test2date": { 
     label: "CASDate", 
     data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    },   
    "Test2time": { 
     label: "Time", 
      data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }, 
    "Test2modules": { 
     label: "Modules", 
     data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }, 
    "Test2cases": { 
     label: "Cases", 
     data: [[1, 101], [2, 201], [3, 201], [4, 45]] 
    }, 
    "Test2failed": { 
     label: "Failed", 
      data: [[1, 301], [2, 454], [3, 43], [4, 125]] 
    }, 
    "Test2cover": { 
     label: "Cover", 
data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }}; 

var Name3 = { 
    "Test3date": { 
     label: "MVSDate", 
     data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    },   
    "Test3time": { 
     label: "Time", 
      data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }, 
    "Test3modules": { 
     label: "Modules", 
     data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }, 
    "Test3cases": { 
     label: "Cases", 
     data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }, 
    "Test3failed": { 
     label: "Failed", 
      data: [[1, 245], [2, 501], [3, 432], [4, 195]] 
    }, 
    "Test3cover": { 
     label: "errover", 
data: [[1, 201], [2, 201], [3, 201], [4, 125]] 
    }}; 
     var allDataSets = [Name1,Name2,Name3]; 

    // hard-code color indices to prevent them from shifting as 
    // countries are turned on/off 
    for(var j=0; j<allDataSets.length; j++){ 
    var i = 0; 
    $.each(allDataSets[j], function(key, val) {**//Here is where the colours are set for `diffrent data`** 
     val.color = i; //The same object of the 3 diffrent datasets are getting the `same color, why, and how can I solve this?` 
     ++i; 

    }); 


    var choiceContainer = $("#choices"); 

    $.each(allDataSets[j], function(key, val) { 
    if(key=="Test3failed" || key=="Test2cover"){ 

     choiceContainer.append('<br/><input type="checkbox" name="' + key + 
           '" checked="checked" id="id' + key + '">' + 
           '<label for="id' + key + '">' 
           + val.label + '</label>'); 


    } 
    }); 

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


    function plotAccordingToChoices() { 
     var data = []; 

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

     if (data.length > 0) 
      $.plot($("#placeholder"), data, { 
      series: { 
        lines: { show: true }, 
        points: { show: true } 
       }, 
       crosshair: { mode: "x" }, 
       grid: { hoverable: true, clickable: true}, 
       yaxis: { min: 0 }, 
       xaxis: { tickDecimals: 0 } 
      }); 
    } 


    plotAccordingToChoices(); 
}); 

</script> 

</body> 
</html> 

回答

1

1)你所得到的相同顏色的對象,因爲你是在設置var i = 0;每個循環的開始,將其移到循環表達式的外部。

2)您可以通過使用.indexOf來檢查變量是否包含另一個變量。例如,要檢查key是否包含「封面」,您可以這樣做:if (key.indexOf("cover") > -1) { ... }