Skip to content

Instantly share code, notes, and snippets.

@ashmigelski
Last active December 21, 2015 20:49
Show Gist options
  • Save ashmigelski/6364427 to your computer and use it in GitHub Desktop.
Save ashmigelski/6364427 to your computer and use it in GitHub Desktop.
jquery-flot "plothover" example
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Flot hover bug</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript' src="http://www.flotcharts.org/flot/jquery.flot.js"></script>
<style type='text/css'>
#container{ width:100%; }
#plot{ width:400px; height:300px; float:left;}
#plot-hover{ position:absolute; top:310px; width:30px; height:20px; background:#c6c6c6; display:none; }
#log{ float:left; width:100px; height:300px; overflow-y:scroll; }
#log .in{ color:green; }
#log .out{ color:red; }
</style>
<script type='text/javascript'>
$(document).ready(function(){
var max = 10, K = [], last_flot_hover = null;
for (var i=0; i<max; i++) {
K.push([2*i, 100*Math.random()]);
}
$.plot("#plot",
[{data:K, bars:{show:true, barWidth:1, lineWidth:0}}],
{
colors: ["#7c93b3"],
zoom: {interactive: true}, pan: { interactive: true},
xaxis:{min:0, max:2*max, show:false},
yaxis:{min:0, max:100},
grid:{autoHighlight:true, hoverable:true, clickable:true, axisMargin:10, borderWidth:0}
}
);
$("#plot").bind("plothover", function (event, pos, item) {
if (item){
$("#log").prepend("<div class='in'>in "+item.dataIndex+"</div>");
if (last_flot_hover == item.dataIndex)
return;
last_flot_hover = item.dataIndex;
toggleHover(item);
} else if (last_flot_hover!=null) {
$("#log").prepend("<div class='out'>out "+last_flot_hover+"</div>");
last_flot_hover = null;
toggleHover();
}
});
function toggleHover(item){
if (!item) {
$("#plot").css("cursor","default");
$("#plot-hover").stop(0, 1).fadeOut(200);
} else {
$("#plot").css("cursor","pointer");
$("#plot-hover").html(item.dataIndex).stop(0, 1).fadeIn(100).offset({ left: item.pageX-15});
}
}
});
</script>
</head>
<body>
<div id="container">
<div id="plot"></div>
<div id="log"></div>
</div>
<div id="plot-hover"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment