Skip to content

Commit 36c5dec

Browse files
committed
Pass the plot object as the this param when calling onClick, set a class when a tooltip is sticky (https://rbcommons.com/s/pepperdata/r/2322)
The class will make it easier for callers to style the sticky state.
1 parent 7f6166b commit 36c5dec

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

js/jquery.flot.tooltip.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* author: Krzysztof Urbas @krzysu [myviews.pl]
77
* website: https://github.com/krzysu/flot.tooltip
88
*
9-
* build on 2013-12-09
9+
* build on 2013-12-20
1010
* released under MIT License, 2012
1111
*/
1212
(function ($) {
@@ -30,6 +30,7 @@
3030
},
3131
defaultTheme: true,
3232
stickyable: false,
33+
stickyClass: 'flotTipSticky',
3334

3435
// callbacks
3536
onHover: function(flotItem, $tooltipEl) {},
@@ -102,10 +103,9 @@
102103
$tip.html( tipText );
103104
that.updateTooltipPosition({ x: pos.pageX, y: pos.pageY });
104105
$tip.css({
105-
left: that.tipPosition.x + that.tooltipOptions.shifts.x,
106-
top: that.tipPosition.y + that.tooltipOptions.shifts.y
107-
})
108-
.show();
106+
left: that.tipPosition.x + that.tooltipOptions.shifts.x,
107+
top: that.tipPosition.y + that.tooltipOptions.shifts.y
108+
}).show();
109109

110110
// run callback
111111
if(typeof that.tooltipOptions.onHover === 'function') {
@@ -122,17 +122,20 @@
122122
var $tip = that.getDomElement();
123123
if(that.tooltipOptions.stickyable) {
124124
if(item && !that.stickyItem) {
125+
// make sticky
125126
that.stickyItem = item;
126127
that.plot.highlight(item.seriesIndex, item.dataIndex);
127-
if(typeof that.tooltipOptions.onClick === 'function') {
128-
that.tooltipOptions.onClick(item, $tip, true);
128+
$tip.addClass(that.tooltipOptions.stickyClass);
129+
if (typeof that.tooltipOptions.onClick === 'function') {
130+
that.tooltipOptions.onClick.call(that.plot, item, $tip, true);
129131
}
130-
}
131-
else {
132+
} else {
133+
// make unsticky
132134
hideTooltip();
133-
plothover(event,pos,item);
134-
if(typeof that.tooltipOptions.onClick === 'function') {
135-
that.tooltipOptions.onClick(item, $tip, false);
135+
plothover(event, pos, item);
136+
$tip.removeClass(that.tooltipOptions.stickyClass);
137+
if (typeof that.tooltipOptions.onClick === 'function') {
138+
that.tooltipOptions.onClick.call(that.plot, item, $tip, false);
136139
}
137140
}
138141
}

js/jquery.flot.tooltip.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/jquery.flot.tooltip.source.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
defaultTheme: true,
2121
stickyable: false,
22+
stickyClass: 'flotTipSticky',
2223

2324
// callbacks
2425
onHover: function(flotItem, $tooltipEl) {},
@@ -91,10 +92,9 @@
9192
$tip.html( tipText );
9293
that.updateTooltipPosition({ x: pos.pageX, y: pos.pageY });
9394
$tip.css({
94-
left: that.tipPosition.x + that.tooltipOptions.shifts.x,
95-
top: that.tipPosition.y + that.tooltipOptions.shifts.y
96-
})
97-
.show();
95+
left: that.tipPosition.x + that.tooltipOptions.shifts.x,
96+
top: that.tipPosition.y + that.tooltipOptions.shifts.y
97+
}).show();
9898

9999
// run callback
100100
if(typeof that.tooltipOptions.onHover === 'function') {
@@ -111,17 +111,20 @@
111111
var $tip = that.getDomElement();
112112
if(that.tooltipOptions.stickyable) {
113113
if(item && !that.stickyItem) {
114+
// make sticky
114115
that.stickyItem = item;
115116
that.plot.highlight(item.seriesIndex, item.dataIndex);
116-
if(typeof that.tooltipOptions.onClick === 'function') {
117-
that.tooltipOptions.onClick(item, $tip, true);
117+
$tip.addClass(that.tooltipOptions.stickyClass);
118+
if (typeof that.tooltipOptions.onClick === 'function') {
119+
that.tooltipOptions.onClick.call(that.plot, item, $tip, true);
118120
}
119-
}
120-
else {
121+
} else {
122+
// make unsticky
121123
hideTooltip();
122-
plothover(event,pos,item);
123-
if(typeof that.tooltipOptions.onClick === 'function') {
124-
that.tooltipOptions.onClick(item, $tip, false);
124+
plothover(event, pos, item);
125+
$tip.removeClass(that.tooltipOptions.stickyClass);
126+
if (typeof that.tooltipOptions.onClick === 'function') {
127+
that.tooltipOptions.onClick.call(that.plot, item, $tip, false);
125128
}
126129
}
127130
}

0 commit comments

Comments
 (0)