This script below is printing out the checkboxes but isn’t plotting anything in the graph window.
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>
/***********EDIT****************/
OK so I solved the first issue with was simply replacing allDataSets[key] with allDataSets[j][key]
But as i now uncheck a checkbox I get the following error line 134 allDataSets[…] is null or not an object. Why do I get this error? In other words its this line that is incorrect when I uncheck a checkbox if (key && allDataSets[j][key])
Three things you need to do:
Declare that loop variable “j” with the
varkeyword.All that code after your comment line – the click handler etc – needs to move out of that loop.
Inside the click handler, the code needs to iterate over “allDataSets” and then look for the key in each one:
Here is the jsfiddle demonstrating the changes. You’ll note that it looks terrible. That’s because your “allDataSets” re-uses the same keys for each clump. I have no idea what you’re trying to achieve, but those changes will at least make something show up.