开发者

need advice regarding datasets flot jquery

开发者 https://www.devze.com 2023-03-12 03:34 出处:网络
Hi I have 2 questions regarding the script below. Question 1: Why is only Test1date, Test1time, Test1Test1modules etc.... getting diffrent colors, while the colors of Test1date, Test2date, Test3date

Hi I have 2 questions regarding the script below.

Question 1: Why is only Test1date, Test1time, Test1Test1modules etc.... getting diffrent colors, while the colors of Test1date, Test2date, Test3date etc... getting the same color, and how can I change this??

Question 2: I Only want to print out checkboxes for "failed" and "cover" below, but how do I check if a key contains anything with "cover" or "failed"?? As for now I check if a key is equal to "Test3failed" or "Test2cover". But I only want to check if key==something containing "cover" or "failed" Thanks in advance =)

<!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) You are getting the same color for the objects because you are setting var i = 0; at the start of every loop, move it to outside of the loop expression.

2) You can check if a variable contains another by using .indexOf. For example, to check if key contains "cover" you can do: if (key.indexOf("cover") > -1) { ... }

0

精彩评论

暂无评论...
验证码 换一张
取 消