Skip to content

Commit

Permalink
Merge pull request #39 from maplemuse/unbind-events
Browse files Browse the repository at this point in the history
Unbind events
  • Loading branch information
Kris Urbas committed Sep 30, 2013
2 parents b36576d + 497dc8b commit 33610a0
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 96 deletions.
70 changes: 42 additions & 28 deletions examples/many_series.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,53 @@
<h1>flot.tooltip plugin example page</h1>

<div id="placeholder" style="width: 825px; height: 150px;"></div>

<a href="javascript:void(0);" class="button" id="replot">Plot</a>

<script type="text/javascript">
$(function () {
var sin = [], cos = [];
for (var i = 0; i < 12; i += 0.2) {
sin.push([i, Math.sin(i)]);
cos.push([i, Math.cos(i)]);
}

var options = {
series: {
lines: { show: true },
points: { show: true }
},
grid: {
hoverable: true //IMPORTANT! this is needed for tooltip to work
},
yaxis: { min: -1.2, max: 1.2 },
tooltip: true,
tooltipOpts: {
content: "'%s' of %x.1 is %y.4",
shifts: {
x: -60,
y: 25
$(document).ready(function(){
console.log("document ready");
var offset = 0;
$('#replot').bind('click', function(){
offset = 0;
var intervalId = setInterval(function(){
plot();
offset = offset + 0.1;
if (offset > 10){
clearInterval(intervalId);
}
});
});
plot();
function plot(){
var sin = [], cos = [];
for (var i = 0; i < 12; i += 0.2) {
sin.push([i, Math.sin(i + offset)]);
cos.push([i, Math.cos(i + offset)]);
}
};

var plotObj = $.plot( $("#placeholder"),
[ { data: sin, label: "sin(x)"}, { data: cos, label: "cos(x)" } ],
options );

var options = {
series: {
lines: { show: true },
points: { show: true }
},
grid: {
hoverable: true //IMPORTANT! this is needed for tooltip to work
},
yaxis: { min: -1.2, max: 1.2 },
tooltip: true,
tooltipOpts: {
content: "'%s' of %x.1 is %y.4",
shifts: {
x: -60,
y: 25
}
}
};

var plotObj = $.plot( $("#placeholder"),
[ { data: sin, label: "sin(x)"}, { data: cos, label: "cos(x)" } ],
options );
}
});
</script>

Expand Down
74 changes: 41 additions & 33 deletions js/jquery.flot.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* jquery.flot.tooltip
*
* description: easy-to-use tooltips for Flot charts
* version: 0.6.1
* version: 0.6.2
* author: Krzysztof Urbas @krzysu [myviews.pl]
* website: https://github.com/krzysu/flot.tooltip
*
* build on 2013-07-10
* build on 2013-08-19
* released under MIT License, 2012
*/
(function ($) {
Expand Down Expand Up @@ -64,38 +64,46 @@
var $tip = that.getDomElement();

// bind event
$( plot.getPlaceholder() ).bind("plothover", function (event, pos, item) {
if (item) {
var tipText;

// convert tooltip content template to real tipText
tipText = that.stringFormat(that.tooltipOptions.content, item);

$tip.html( tipText );
that.updateTooltipPosition({ x: pos.pageX, y: pos.pageY });
$tip.css({
left: that.tipPosition.x + that.tooltipOptions.shifts.x,
top: that.tipPosition.y + that.tooltipOptions.shifts.y
})
.show();

// run callback
if(typeof that.tooltipOptions.onHover === 'function') {
that.tooltipOptions.onHover(item, $tip);
}
}
else {
$tip.hide().html('');
}
});

eventHolder.mousemove( function(e) {
var pos = {};
pos.x = e.pageX;
pos.y = e.pageY;
that.updateTooltipPosition(pos);
});
$( plot.getPlaceholder() ).bind("plothover", plothover);

$(eventHolder).bind('mousemove', mouseMove);

});
plot.hooks.shutdown.push(function (plot, eventHolder){
$(plot.getPlaceholder()).unbind("plothover", plothover);
$(eventHolder).unbind("mousemove", mouseMove);
});
function mouseMove(e){
var pos = {};
pos.x = e.pageX;
pos.y = e.pageY;
that.updateTooltipPosition(pos);
}
function plothover(event, pos, item) {
var $tip = that.getDomElement();
if (item) {
var tipText;

// convert tooltip content template to real tipText
tipText = that.stringFormat(that.tooltipOptions.content, item);

$tip.html( tipText );
that.updateTooltipPosition({ x: pos.pageX, y: pos.pageY });
$tip.css({
left: that.tipPosition.x + that.tooltipOptions.shifts.x,
top: that.tipPosition.y + that.tooltipOptions.shifts.y
})
.show();

// run callback
if(typeof that.tooltipOptions.onHover === 'function') {
that.tooltipOptions.onHover(item, $tip);
}
}
else {
$tip.hide().html('');
}
}
};

/**
Expand Down
6 changes: 3 additions & 3 deletions js/jquery.flot.tooltip.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 39 additions & 31 deletions js/jquery.flot.tooltip.source.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,46 @@
var $tip = that.getDomElement();

// bind event
$( plot.getPlaceholder() ).bind("plothover", function (event, pos, item) {
if (item) {
var tipText;

// convert tooltip content template to real tipText
tipText = that.stringFormat(that.tooltipOptions.content, item);

$tip.html( tipText );
that.updateTooltipPosition({ x: pos.pageX, y: pos.pageY });
$tip.css({
left: that.tipPosition.x + that.tooltipOptions.shifts.x,
top: that.tipPosition.y + that.tooltipOptions.shifts.y
})
.show();

// run callback
if(typeof that.tooltipOptions.onHover === 'function') {
that.tooltipOptions.onHover(item, $tip);
}
}
else {
$tip.hide().html('');
}
});

eventHolder.mousemove( function(e) {
var pos = {};
pos.x = e.pageX;
pos.y = e.pageY;
that.updateTooltipPosition(pos);
});
$( plot.getPlaceholder() ).bind("plothover", plothover);

$(eventHolder).bind('mousemove', mouseMove);

});
plot.hooks.shutdown.push(function (plot, eventHolder){
$(plot.getPlaceholder()).unbind("plothover", plothover);
$(eventHolder).unbind("mousemove", mouseMove);
});
function mouseMove(e){
var pos = {};
pos.x = e.pageX;
pos.y = e.pageY;
that.updateTooltipPosition(pos);
}
function plothover(event, pos, item) {
var $tip = that.getDomElement();
if (item) {
var tipText;

// convert tooltip content template to real tipText
tipText = that.stringFormat(that.tooltipOptions.content, item);

$tip.html( tipText );
that.updateTooltipPosition({ x: pos.pageX, y: pos.pageY });
$tip.css({
left: that.tipPosition.x + that.tooltipOptions.shifts.x,
top: that.tipPosition.y + that.tooltipOptions.shifts.y
})
.show();

// run callback
if(typeof that.tooltipOptions.onHover === 'function') {
that.tooltipOptions.onHover(item, $tip);
}
}
else {
$tip.hide().html('');
}
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery.flot.tooltip",
"version": "0.6.1",
"version": "0.6.2",
"description": "easy-to-use tooltips for Flot charts",
"website": "https://github.com/krzysu/flot.tooltip",
"directories": {
Expand Down

0 comments on commit 33610a0

Please sign in to comment.