Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unbind events #39

Merged
merged 2 commits into from
Sep 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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