/*
* JavaScript code for drawing FLOT graph. Still testing this!!!
*/
$(function () {
var placeholder = $('#placeholder');
var options = {
series: { lines: { show: true }, shadowSize: 1 },
xaxis: { ticks: 10},
xaxis: { mode: 'time'},
yaxis: { ticks: 10 },
zoom: { interactive: true },
pan: { interactive: true },
grid: { hoverable: true, clickable: true },
legend: { position: 'nw' }
};
// hard-code color indices to prevent them from shifting as
// countries are turned on/off
var i = 0;
$.each(datasets, function(key, val) {
val.color = i;
++i;
});
// insert checkboxes in page
var choiceContainer = $("#choices");
$.each(datasets, function(key, val) {
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");
if (key && datasets[key])
data.push(datasets[key]);
});
if (data.length > 0)
$.plot($("#placeholder"), data, options);
}
function showTooltip(x, y, contents) {
$('<div id="tooltip">' + contents + '</div>').css( {
position: 'absolute',
display: 'none',
top: y + 2,
left: x + 5,
border: '2px solid #fcc',
padding: '0px',
'background-color': '#fee',
opacity: 0.70
}).appendTo("body").fadeIn(500);
}
var previousPoint = null;
var click = 0;
$("#placeholder").bind( "plotclick", function (event, pos, item) {
$("#x").text(pos.x.toFixed(2));
$("#y").text(pos.y.toFixed(2));
if (item) {
$("#tooltip").remove();
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(2);
if (previousPoint === null) {
previousPoint = item;
showTooltip(item.pageX, item.pageY,
item.series.label + ": " + x + "= " + y);
click = 1;
}
else if (previousPoint.datapoint[0] != item.datapoint[0]||
previousPoint.datapoint[1] != item.datapoint[1]||
click===0) {
showTooltip(item.pageX, item.pageY,
item.series.label + ": " + x + "= " + y);
click = 1;
previousPoint = item;
}
else
click=0;
}
});
plotAccordingToChoices();
$('#whole').click(function () {
plotAccordingToChoices();
});
});