Tooltip.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.charting.action2d.Tooltip"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.action2d.Tooltip"] = true;
  8. dojo.provide("dojox.charting.action2d.Tooltip");
  9. dojo.require("dijit.Tooltip");
  10. dojo.require("dojox.charting.action2d.Base");
  11. dojo.require("dojox.gfx.matrix");
  12. dojo.require("dojox.lang.functional");
  13. dojo.require("dojox.lang.functional.scan");
  14. dojo.require("dojox.lang.functional.fold");
  15. /*=====
  16. dojo.declare("dojox.charting.action2d.__TooltipCtorArgs", dojox.charting.action2d.__BaseCtorArgs, {
  17. // summary:
  18. // Additional arguments for tooltip actions.
  19. // text: Function?
  20. // The function that produces the text to be shown within a tooltip. By default this will be
  21. // set by the plot in question, by returning the value of the element.
  22. text: null
  23. });
  24. =====*/
  25. (function(){
  26. var DEFAULT_TEXT = function(o){
  27. var t = o.run && o.run.data && o.run.data[o.index];
  28. if(t && typeof t != "number" && (t.tooltip || t.text)){
  29. return t.tooltip || t.text;
  30. }
  31. if(o.element == "candlestick"){
  32. return '<table cellpadding="1" cellspacing="0" border="0" style="font-size:0.9em;">'
  33. + '<tr><td>Open:</td><td align="right"><strong>' + o.data.open + '</strong></td></tr>'
  34. + '<tr><td>High:</td><td align="right"><strong>' + o.data.high + '</strong></td></tr>'
  35. + '<tr><td>Low:</td><td align="right"><strong>' + o.data.low + '</strong></td></tr>'
  36. + '<tr><td>Close:</td><td align="right"><strong>' + o.data.close + '</strong></td></tr>'
  37. + (o.data.mid !== undefined ? '<tr><td>Mid:</td><td align="right"><strong>' + o.data.mid + '</strong></td></tr>' : '')
  38. + '</table>';
  39. }
  40. return o.element == "bar" ? o.x : o.y;
  41. };
  42. var df = dojox.lang.functional, m = dojox.gfx.matrix, pi4 = Math.PI / 4, pi2 = Math.PI / 2;
  43. dojo.declare("dojox.charting.action2d.Tooltip", dojox.charting.action2d.Base, {
  44. // summary:
  45. // Create an action on a plot where a tooltip is shown when hovering over an element.
  46. // the data description block for the widget parser
  47. defaultParams: {
  48. text: DEFAULT_TEXT // the function to produce a tooltip from the object
  49. },
  50. optionalParams: {}, // no optional parameters
  51. constructor: function(chart, plot, kwArgs){
  52. // summary:
  53. // Create the tooltip action and connect it to the plot.
  54. // chart: dojox.charting.Chart2D
  55. // The chart this action belongs to.
  56. // plot: String?
  57. // The plot this action is attached to. If not passed, "default" is assumed.
  58. // kwArgs: dojox.charting.action2d.__TooltipCtorArgs?
  59. // Optional keyword arguments object for setting parameters.
  60. this.text = kwArgs && kwArgs.text ? kwArgs.text : DEFAULT_TEXT;
  61. this.connect();
  62. },
  63. process: function(o){
  64. // summary:
  65. // Process the action on the given object.
  66. // o: dojox.gfx.Shape
  67. // The object on which to process the highlighting action.
  68. if(o.type === "onplotreset" || o.type === "onmouseout"){
  69. dijit.hideTooltip(this.aroundRect);
  70. this.aroundRect = null;
  71. if(o.type === "onplotreset"){
  72. delete this.angles;
  73. }
  74. return;
  75. }
  76. if(!o.shape || o.type !== "onmouseover"){ return; }
  77. // calculate relative coordinates and the position
  78. var aroundRect = {type: "rect"}, position = ["after", "before"];
  79. switch(o.element){
  80. case "marker":
  81. aroundRect.x = o.cx;
  82. aroundRect.y = o.cy;
  83. aroundRect.width = aroundRect.height = 1;
  84. break;
  85. case "circle":
  86. aroundRect.x = o.cx - o.cr;
  87. aroundRect.y = o.cy - o.cr;
  88. aroundRect.width = aroundRect.height = 2 * o.cr;
  89. break;
  90. case "column":
  91. position = ["above", "below"];
  92. // intentional fall down
  93. case "bar":
  94. aroundRect = dojo.clone(o.shape.getShape());
  95. break;
  96. case "candlestick":
  97. aroundRect.x = o.x;
  98. aroundRect.y = o.y;
  99. aroundRect.width = o.width;
  100. aroundRect.height = o.height;
  101. break;
  102. default:
  103. //case "slice":
  104. if(!this.angles){
  105. // calculate the running total of slice angles
  106. if(typeof o.run.data[0] == "number"){
  107. this.angles = df.map(df.scanl(o.run.data, "+", 0),
  108. "* 2 * Math.PI / this", df.foldl(o.run.data, "+", 0));
  109. }else{
  110. this.angles = df.map(df.scanl(o.run.data, "a + b.y", 0),
  111. "* 2 * Math.PI / this", df.foldl(o.run.data, "a + b.y", 0));
  112. }
  113. }
  114. var startAngle = m._degToRad(o.plot.opt.startAngle),
  115. angle = (this.angles[o.index] + this.angles[o.index + 1]) / 2 + startAngle;
  116. aroundRect.x = o.cx + o.cr * Math.cos(angle);
  117. aroundRect.y = o.cy + o.cr * Math.sin(angle);
  118. aroundRect.width = aroundRect.height = 1;
  119. // calculate the position
  120. if(angle < pi4){
  121. // do nothing: the position is right
  122. }else if(angle < pi2 + pi4){
  123. position = ["below", "above"];
  124. }else if(angle < Math.PI + pi4){
  125. position = ["before", "after"];
  126. }else if(angle < 2 * Math.PI - pi4){
  127. position = ["above", "below"];
  128. }
  129. /*
  130. else{
  131. // do nothing: the position is right
  132. }
  133. */
  134. break;
  135. }
  136. // adjust relative coordinates to absolute, and remove fractions
  137. var lt = dojo.coords(this.chart.node, true);
  138. aroundRect.x += lt.x;
  139. aroundRect.y += lt.y;
  140. aroundRect.x = Math.round(aroundRect.x);
  141. aroundRect.y = Math.round(aroundRect.y);
  142. aroundRect.width = Math.ceil(aroundRect.width);
  143. aroundRect.height = Math.ceil(aroundRect.height);
  144. this.aroundRect = aroundRect;
  145. var tooltip = this.text(o);
  146. if(tooltip){
  147. dijit.showTooltip(tooltip, this.aroundRect, position);
  148. }
  149. }
  150. });
  151. })();
  152. }