[483] | 1 | define(["dijit/Tooltip", "dojo/_base/lang", "dojo/_base/declare", "dojo/_base/window", "dojo/_base/connect", "dojo/dom-style", |
---|
| 2 | "./PlotAction", "dojox/gfx/matrix", "dojo/has", "dojo/has!dojo-bidi?../bidi/action2d/Tooltip", |
---|
| 3 | "dojox/lang/functional", "dojox/lang/functional/scan", "dojox/lang/functional/fold"], |
---|
| 4 | function(DijitTooltip, lang, declare, win, hub, domStyle, PlotAction, m, has, BidiTooltip, df){ |
---|
| 5 | |
---|
| 6 | /*===== |
---|
| 7 | var __TooltipCtorArgs = { |
---|
| 8 | // summary: |
---|
| 9 | // Additional arguments for tooltip actions. |
---|
| 10 | // duration: Number? |
---|
| 11 | // The amount of time in milliseconds for an animation to last. Default is 400. |
---|
| 12 | // easing: dojo/fx/easing/*? |
---|
| 13 | // An easing object (see dojo.fx.easing) for use in an animation. The |
---|
| 14 | // default is dojo.fx.easing.backOut. |
---|
| 15 | // text: Function? |
---|
| 16 | // The function that produces the text to be shown within a tooltip. By default this will be |
---|
| 17 | // set by the plot in question, by returning the value of the element. |
---|
| 18 | // mouseOver: Boolean? |
---|
| 19 | // Whether the tooltip is enabled on mouse over or on mouse click / touch down. Default is true. |
---|
| 20 | }; |
---|
| 21 | =====*/ |
---|
| 22 | |
---|
| 23 | var DEFAULT_TEXT = function(o, plot){ |
---|
| 24 | var t = o.run && o.run.data && o.run.data[o.index]; |
---|
| 25 | if(t && typeof t != "number" && (t.tooltip || t.text)){ |
---|
| 26 | return t.tooltip || t.text; |
---|
| 27 | } |
---|
| 28 | if(plot.tooltipFunc){ |
---|
| 29 | return plot.tooltipFunc(o); |
---|
| 30 | }else{ |
---|
| 31 | return o.y; |
---|
| 32 | } |
---|
| 33 | }; |
---|
| 34 | |
---|
| 35 | var pi4 = Math.PI / 4, pi2 = Math.PI / 2; |
---|
| 36 | |
---|
| 37 | var Tooltip = declare(has("dojo-bidi")? "dojox.charting.action2d.NonBidiTooltip" : "dojox.charting.action2d.Tooltip", PlotAction, { |
---|
| 38 | // summary: |
---|
| 39 | // Create an action on a plot where a tooltip is shown when hovering over an element. |
---|
| 40 | |
---|
| 41 | // the data description block for the widget parser |
---|
| 42 | defaultParams: { |
---|
| 43 | text: DEFAULT_TEXT, // the function to produce a tooltip from the object |
---|
| 44 | mouseOver: true |
---|
| 45 | }, |
---|
| 46 | optionalParams: {}, // no optional parameters |
---|
| 47 | |
---|
| 48 | constructor: function(chart, plot, kwArgs){ |
---|
| 49 | // summary: |
---|
| 50 | // Create the tooltip action and connect it to the plot. |
---|
| 51 | // chart: dojox/charting/Chart |
---|
| 52 | // The chart this action belongs to. |
---|
| 53 | // plot: String? |
---|
| 54 | // The plot this action is attached to. If not passed, "default" is assumed. |
---|
| 55 | // kwArgs: __TooltipCtorArgs? |
---|
| 56 | // Optional keyword arguments object for setting parameters. |
---|
| 57 | this.text = kwArgs && kwArgs.text ? kwArgs.text : DEFAULT_TEXT; |
---|
| 58 | this.mouseOver = kwArgs && kwArgs.mouseOver != undefined ? kwArgs.mouseOver : true; |
---|
| 59 | this.connect(); |
---|
| 60 | }, |
---|
| 61 | |
---|
| 62 | process: function(o){ |
---|
| 63 | // summary: |
---|
| 64 | // Process the action on the given object. |
---|
| 65 | // o: dojox/gfx/shape.Shape |
---|
| 66 | // The object on which to process the highlighting action. |
---|
| 67 | if(o.type === "onplotreset" || o.type === "onmouseout"){ |
---|
| 68 | DijitTooltip.hide(this.aroundRect); |
---|
| 69 | this.aroundRect = null; |
---|
| 70 | if(o.type === "onplotreset"){ |
---|
| 71 | delete this.angles; |
---|
| 72 | } |
---|
| 73 | return; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | if(!o.shape || (this.mouseOver && o.type !== "onmouseover") || (!this.mouseOver && o.type !== "onclick")){ return; } |
---|
| 77 | |
---|
| 78 | // calculate relative coordinates and the position |
---|
| 79 | var aroundRect = {type: "rect"}, position = ["after-centered", "before-centered"]; |
---|
| 80 | switch(o.element){ |
---|
| 81 | case "marker": |
---|
| 82 | aroundRect.x = o.cx; |
---|
| 83 | aroundRect.y = o.cy; |
---|
| 84 | aroundRect.w = aroundRect.h = 1; |
---|
| 85 | break; |
---|
| 86 | case "circle": |
---|
| 87 | aroundRect.x = o.cx - o.cr; |
---|
| 88 | aroundRect.y = o.cy - o.cr; |
---|
| 89 | aroundRect.w = aroundRect.h = 2 * o.cr; |
---|
| 90 | break; |
---|
| 91 | case "spider_circle": |
---|
| 92 | aroundRect.x = o.cx; |
---|
| 93 | aroundRect.y = o.cy ; |
---|
| 94 | aroundRect.w = aroundRect.h = 1; |
---|
| 95 | break; |
---|
| 96 | case "spider_plot": |
---|
| 97 | return; |
---|
| 98 | case "column": |
---|
| 99 | position = ["above-centered", "below-centered"]; |
---|
| 100 | // intentional fall down |
---|
| 101 | case "bar": |
---|
| 102 | aroundRect = lang.clone(o.shape.getShape()); |
---|
| 103 | aroundRect.w = aroundRect.width; |
---|
| 104 | aroundRect.h = aroundRect.height; |
---|
| 105 | break; |
---|
| 106 | case "candlestick": |
---|
| 107 | aroundRect.x = o.x; |
---|
| 108 | aroundRect.y = o.y; |
---|
| 109 | aroundRect.w = o.width; |
---|
| 110 | aroundRect.h = o.height; |
---|
| 111 | break; |
---|
| 112 | default: |
---|
| 113 | //case "slice": |
---|
| 114 | if(!this.angles){ |
---|
| 115 | // calculate the running total of slice angles |
---|
| 116 | if(typeof o.run.data[0] == "number"){ |
---|
| 117 | this.angles = df.map(df.scanl(o.run.data, "+", 0), |
---|
| 118 | "* 2 * Math.PI / this", df.foldl(o.run.data, "+", 0)); |
---|
| 119 | }else{ |
---|
| 120 | this.angles = df.map(df.scanl(o.run.data, "a + b.y", 0), |
---|
| 121 | "* 2 * Math.PI / this", df.foldl(o.run.data, "a + b.y", 0)); |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | var startAngle = m._degToRad(o.plot.opt.startAngle), |
---|
| 125 | angle = (this.angles[o.index] + this.angles[o.index + 1]) / 2 + startAngle; |
---|
| 126 | aroundRect.x = o.cx + o.cr * Math.cos(angle); |
---|
| 127 | aroundRect.y = o.cy + o.cr * Math.sin(angle); |
---|
| 128 | aroundRect.w = aroundRect.h = 1; |
---|
| 129 | // depending on startAngle we might go out of the 0-2*PI range, normalize that |
---|
| 130 | if(startAngle && (angle < 0 || angle > 2 * Math.PI)){ |
---|
| 131 | angle = Math.abs(2 * Math.PI - Math.abs(angle)); |
---|
| 132 | } |
---|
| 133 | // calculate the position |
---|
| 134 | if(angle < pi4){ |
---|
| 135 | // do nothing: the position is right |
---|
| 136 | }else if(angle < pi2 + pi4){ |
---|
| 137 | position = ["below-centered", "above-centered"]; |
---|
| 138 | }else if(angle < Math.PI + pi4){ |
---|
| 139 | position = ["before-centered", "after-centered"]; |
---|
| 140 | }else if(angle < 2 * Math.PI - pi4){ |
---|
| 141 | position = ["above-centered", "below-centered"]; |
---|
| 142 | } |
---|
| 143 | /* |
---|
| 144 | else{ |
---|
| 145 | // do nothing: the position is right |
---|
| 146 | } |
---|
| 147 | */ |
---|
| 148 | break; |
---|
| 149 | } |
---|
| 150 | if(has("dojo-bidi")){ |
---|
| 151 | this._recheckPosition(o,aroundRect,position); |
---|
| 152 | } |
---|
| 153 | // adjust relative coordinates to absolute, and remove fractions |
---|
| 154 | var lt = this.chart.getCoords(); |
---|
| 155 | aroundRect.x += lt.x; |
---|
| 156 | aroundRect.y += lt.y; |
---|
| 157 | aroundRect.x = Math.round(aroundRect.x); |
---|
| 158 | aroundRect.y = Math.round(aroundRect.y); |
---|
| 159 | aroundRect.w = Math.ceil(aroundRect.w); |
---|
| 160 | aroundRect.h = Math.ceil(aroundRect.h); |
---|
| 161 | this.aroundRect = aroundRect; |
---|
| 162 | |
---|
| 163 | var tooltipText = this.text(o, this.plot); |
---|
| 164 | if(tooltipText){ |
---|
| 165 | DijitTooltip.show(this._format(tooltipText), this.aroundRect, position); |
---|
| 166 | } |
---|
| 167 | if(!this.mouseOver){ |
---|
| 168 | this._handle = hub.connect(win.doc, "onclick", this, "onClick"); |
---|
| 169 | } |
---|
| 170 | }, |
---|
| 171 | onClick: function(){ |
---|
| 172 | this.process({ type: "onmouseout"}); |
---|
| 173 | }, |
---|
| 174 | _recheckPosition: function(obj,rect,position){ |
---|
| 175 | }, |
---|
| 176 | _format: function(tooltipText){ |
---|
| 177 | return tooltipText; |
---|
| 178 | } |
---|
| 179 | }); |
---|
| 180 | return has("dojo-bidi")? declare("dojox.charting.action2d.Tooltip", [Tooltip, BidiTooltip]) : Tooltip; |
---|
| 181 | }); |
---|