[Pkg-javascript-commits] [d3-tip.js] 67/277: move directions to methods out of tip.show

bhuvan krishna bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:57:17 UTC 2016


This is an automated email from the git hooks/post-receive script.

bhuvan-guest pushed a commit to branch master
in repository d3-tip.js.

commit 3c1d1756ceeb753d9982459da799132d91ff81a8
Author: Justin Palmer <justin at github.com>
Date:   Sun Apr 7 14:45:27 2013 -0700

    move directions to methods out of tip.show
---
 examples/bars.html    |  2 +-
 examples/circles.html |  3 ++
 src/d3.tip.js         | 76 +++++++++++++++++++++++++++++++++------------------
 3 files changed, 54 insertions(+), 27 deletions(-)

diff --git a/examples/bars.html b/examples/bars.html
index a93bfd0..121ac85 100644
--- a/examples/bars.html
+++ b/examples/bars.html
@@ -64,7 +64,7 @@
       .attr('class', 'd3-tip')
       .text(function(d) { return d.total })
       .direction(function(d) {
-        return d3.shuffle(['n', 's', 'e', 'w'])[0]
+        return d3.shuffle(['s'])[0]
       })
 
     var w = 800,
diff --git a/examples/circles.html b/examples/circles.html
index 9d3dc66..29b2238 100644
--- a/examples/circles.html
+++ b/examples/circles.html
@@ -60,6 +60,9 @@
     var tip = d3.svg.tip()
       .attr('class', 'd3-tip')
       .text(function(d) { return d.toFixed(2) })
+      .direction(function(d) {
+        return d3.shuffle(['s'])[0]
+      })
 
     var w = 1000,
         h = 1000,
diff --git a/src/d3.tip.js b/src/d3.tip.js
index 13ecd75..baeb7fd 100644
--- a/src/d3.tip.js
+++ b/src/d3.tip.js
@@ -30,30 +30,15 @@ d3.svg.tip = function() {
   }
 
   tip.show = function(v) {
-    var bbox = get_screen_bbox(),
-        content = text.apply(this, arguments),
-        dir = direction.apply(this, arguments),
-        top, left;
+      var content = text.apply(this, arguments),
+        dir = direction.apply(this, arguments), coords
 
     node.innerHTML = content
     node.style.display = 'block'
 
-    switch(dir) {
-      case 'bottom':
-        top  = (bbox.s.y - (node.offsetHeight / 2)) + 'px'
-        left = (bbox.s.x - node.offsetWidth / 2) + 'px'
-        break
-      case 'left':
-        top  = (bbox.w.y - node.offsetHeight / 2) + 'px'
-        left = bbox.w.x + 'px'
-      default:
-        top  = (bbox.n.y - node.offsetHeight) + 'px'
-        left = (bbox.n.x - node.offsetWidth / 2) + 'px'
-        break
-    }
-
-    node.style.top = top
-    node.style.left = left
+    coords = direction_callbacks.get(dir).apply(this)
+    node.style.top = coords.top + 'px'
+    node.style.left = coords.left + 'px'
   }
 
   tip.hide = function(v) {
@@ -77,14 +62,14 @@ d3.svg.tip = function() {
     return tip;
   }
 
-  // Public: Set or get the orientation of the tooltip
+  // Public: Set or get the direction of the tooltip
   //
-  // v - One of top, bottom, left, or right
+  // v - One of n(orth), s(outh), e(ast), or w(est)
   //
   // Returns tip or direction
   tip.direction = function(v) {
-    if (!arguments.length) return orient;
-    orient = v == null ? v : d3.functor(v);
+    if (!arguments.length) return direction;
+    direction = v == null ? v : d3.functor(v);
     return tip;
   };
 
@@ -106,11 +91,50 @@ d3.svg.tip = function() {
   // Returns text value or tip
   tip.text = function(v) {
     if (!arguments.length) return text;
-    text = v == null ? v : d3.functor(v);
+    text = v == null ? v : d3.functor(v)
 
-    return tip;
+    return tip
   };
 
+  var direction_callbacks = d3.map({
+    n: direction_n,
+    s: direction_s,
+    e: direction_e,
+    w: direction_w
+  })
+
+  function direction_n() {
+    var bbox = get_screen_bbox()
+    return {
+      top: (bbox.n.y - node.offsetHeight),
+      left: (bbox.n.x - node.offsetWidth / 2)
+    }
+  }
+
+  function direction_s() {
+    var bbox = get_screen_bbox()
+    return {
+      top:  (bbox.s.y),
+      left: (bbox.s.x - node.offsetWidth / 2)
+    }
+  }
+
+  function direction_e() {
+    var bbox = get_screen_bbox()
+    return {
+      top: (bbox.e.y - node.offsetHeight / 2),
+      left: bbox.e.x
+    }
+  }
+
+  function direction_w() {
+    var bbox = get_screen_bbox()
+    return {
+      top: (bbox.w.y - node.offsetHeight / 2),
+      left: bbox.w.x - node.offsetWidth
+    }
+  }
+
   function init_node() {
     var node = document.createElement('div')
     node.style.position = 'absolute'

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/d3-tip.js.git



More information about the Pkg-javascript-commits mailing list