Tooltip.js 5.8 KB

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