From d62c4bbfc236ab26f1de18dcb24373b060d8759b Mon Sep 17 00:00:00 2001 From: Justin Palmer Date: Tue, 5 Nov 2013 14:05:59 -0800 Subject: [PATCH 1/4] allow explict passing of target element --- index.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index e84ca7f..5a27756 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,8 @@ d3.tip = function() { html = d3_tip_html, node = initNode(), svg = null, - point = null + point = null, + target = null function tip(vis) { svg = getSVGNode(vis) @@ -24,9 +25,12 @@ d3.tip = function() { // // Returns a tip tip.show = function() { - var content = html.apply(this, arguments), - poffset = offset.apply(this, arguments), - dir = direction.apply(this, arguments), + var args = Array.prototype.slice.call(arguments) + if(args.length > 1) target = args.pop() + + var content = html.apply(this, args), + poffset = offset.apply(this, args), + dir = direction.apply(this, args), nodel = d3.select(node), i = 0, coords @@ -238,10 +242,10 @@ d3.tip = function() { // // Returns an Object {n, s, e, w, nw, sw, ne, se} function getScreenBBox() { - var target = d3.event.target, + var targetel = target || d3.event.target, bbox = {}, - matrix = target.getScreenCTM(), - tbbox = target.getBBox(), + matrix = targetel.getScreenCTM(), + tbbox = targetel.getBBox(), width = tbbox.width, height = tbbox.height, x = tbbox.x, From 3a2620d4aa2f75ea798b2ccc0a105387fdaf3e7d Mon Sep 17 00:00:00 2001 From: Justin Palmer Date: Tue, 5 Nov 2013 16:13:15 -0800 Subject: [PATCH 2/4] check if the last item passed to ship show is an svgelement before we pop the array --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 1ea31db..6cfa3cb 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,7 @@ d3.tip = function() { // Returns a tip tip.show = function() { var args = Array.prototype.slice.call(arguments) - if(args.length > 1) target = args.pop() + if(args[args.length - 1] instanceof SVGElement) target = args.pop() var content = html.apply(this, args), poffset = offset.apply(this, args), From e294f73fc9c11244426b442cbd01933bcdef9671 Mon Sep 17 00:00:00 2001 From: Justin Palmer Date: Tue, 5 Nov 2013 16:14:34 -0800 Subject: [PATCH 3/4] bump version to 0.6.3 --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index f192e85..d5d2043 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "d3-tip", - "version": "0.6.2", + "version": "0.6.3", "main": "index.js", "ignore": [ "**/.*", From b520932e2a0207f693f7625294f5484ba12cbab5 Mon Sep 17 00:00:00 2001 From: Justin Palmer Date: Sat, 7 Dec 2013 11:24:20 -0800 Subject: [PATCH 4/4] add documentation and examples for explicity targets --- docs/initializing-tooltips.md | 2 +- docs/showing-and-hiding-tooltips.md | 13 ++- examples/explicit-target.html | 145 ++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 examples/explicit-target.html diff --git a/docs/initializing-tooltips.md b/docs/initializing-tooltips.md index a3c75d2..4e88ef9 100644 --- a/docs/initializing-tooltips.md +++ b/docs/initializing-tooltips.md @@ -24,4 +24,4 @@ var vis = d3.select(document.body) // Show and hide the tooltip .on('mouseover', tip.show) .on('mouseout', tip.hide) -``` \ No newline at end of file +``` diff --git a/docs/showing-and-hiding-tooltips.md b/docs/showing-and-hiding-tooltips.md index 031f7f8..64fdee4 100644 --- a/docs/showing-and-hiding-tooltips.md +++ b/docs/showing-and-hiding-tooltips.md @@ -15,6 +15,17 @@ rect.on('mouseover', function(d) { }) ``` +#### Explicit targets +Sometimes you need to manually specify a target to act on. For instance, maybe +you want the tooltip to appear over a different element than the one that triggered +a `mouseover` event. You can specify an explicit target by passing an `SVGElement` +as the last argument. + +``` javascript +tip.show(data, target) +``` + + ### tip.hide Hide a tooltip @@ -26,4 +37,4 @@ rect.on('mouseout', tip.hide) rect.on('mouseout', function(d) { tip.hide(d) }) -``` \ No newline at end of file +``` diff --git a/examples/explicit-target.html b/examples/explicit-target.html new file mode 100644 index 0000000..2c2b45c --- /dev/null +++ b/examples/explicit-target.html @@ -0,0 +1,145 @@ + + + + d3.tip.js - Tooltips for D3 + + Explicit Target + + + + + + +
+ +
+ + +