Debian Codegraph @

dojox/charting/action2d/Tooltip.js

raw symbols SCIP
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 defaultPosition: ["after-centered", "before-centered"]
46 },
47 optionalParams: {}, // no optional parameters
48
49 constructor: function(chart, plot, kwArgs){
50 // summary:
51 // Create the tooltip action and connect it to the plot.
52 // chart: dojox/charting/Chart
53 // The chart this action belongs to.
54 // plot: String?
55 // The plot this action is attached to. If not passed, "default" is assumed.
56 // kwArgs: __TooltipCtorArgs?
57 // Optional keyword arguments object for setting parameters.
58 this.text = kwArgs && kwArgs.text ? kwArgs.text : DEFAULT_TEXT;
59 this.mouseOver = kwArgs && kwArgs.mouseOver != undefined ? kwArgs.mouseOver : true;
60 this.defaultPosition = kwArgs && kwArgs.defaultPosition != undefined ? kwArgs.defaultPosition : ["after-centered", "before-centered"];
61 this.connect();
62 },
63
64 process: function(o){
65 // summary:
66 // Process the action on the given object.
67 // o: dojox/gfx/shape.Shape
68 // The object on which to process the highlighting action.
69 if(o.type === "onplotreset" || o.type === "onmouseout"){
70 DijitTooltip.hide(this.aroundRect);
71 this.aroundRect = null;
72 if(o.type === "onplotreset"){
73 delete this.angles;
74 }
75 return;
76 }
77
78 if(!o.shape || (this.mouseOver && o.type !== "onmouseover") || (!this.mouseOver && o.type !== "onclick")){ return; }
79
80 // calculate relative coordinates and the position
81 var aroundRect = {type: "rect"}, position = this.defaultPosition;
82 switch(o.element){
83 case "marker":
84 aroundRect.x = o.cx;
85 aroundRect.y = o.cy;
86 aroundRect.w = aroundRect.h = 1;
87 break;
88 case "circle":
89 aroundRect.x = o.cx - o.cr;
90 aroundRect.y = o.cy - o.cr;
91 aroundRect.w = aroundRect.h = 2 * o.cr;
92 break;
93 case "spider_circle":
94 aroundRect.x = o.cx;
95 aroundRect.y = o.cy ;
96 aroundRect.w = aroundRect.h = 1;
97 break;
98 case "spider_plot":
99 return;
100 case "column":
101 position = ["above-centered", "below-centered"];
102 // intentional fall down
103 case "bar":
104 aroundRect = lang.clone(o.shape.getShape());
105 aroundRect.w = aroundRect.width;
106 aroundRect.h = aroundRect.height;
107 break;
108 case "candlestick":
109 aroundRect.x = o.x;
110 aroundRect.y = o.y;
111 aroundRect.w = o.width;
112 aroundRect.h = o.height;
113 break;
114 default:
115 //case "slice":
116 if(!this.angles){
117 // calculate the running total of slice angles
118 var filteredRun = typeof o.run.data[0] == "number" ?
119 df.map(o.run.data, "x ? Math.max(x, 0) : 0") : df.map(o.run.data, "x ? Math.max(x.y, 0) : 0");
120 this.angles = df.map(df.scanl(filteredRun, "+", 0),
121 "* 2 * Math.PI / this", df.foldl(filteredRun, "+", 0));
122 }
123 var startAngle = m._degToRad(o.plot.opt.startAngle),
124 angle = (this.angles[o.index] + this.angles[o.index + 1]) / 2 + startAngle;
125 aroundRect.x = o.cx + o.cr * Math.cos(angle);
126 aroundRect.y = o.cy + o.cr * Math.sin(angle);
127 aroundRect.w = aroundRect.h = 1;
128 // depending on startAngle we might go out of the 0-2*PI range, normalize that
129 if(startAngle && (angle < 0 || angle > 2 * Math.PI)){
130 angle = Math.abs(2 * Math.PI - Math.abs(angle));
131 }
132 // calculate the position
133 if(angle < pi4){
134 // do nothing: the position is right
135 }else if(angle < pi2 + pi4){
136 position = ["below-centered", "above-centered"];
137 }else if(angle < Math.PI + pi4){
138 position = ["before-centered", "after-centered"];
139 }else if(angle < 2 * Math.PI - pi4){
140 position = ["above-centered", "below-centered"];
141 }
142 /*
143 else{
144 // do nothing: the position is right
145 }
146 */
147 break;
148 }
149 if(has("dojo-bidi")){
150 this._recheckPosition(o,aroundRect,position);
151 }
152 // adjust relative coordinates to absolute, and remove fractions
153 var lt = this.chart.getCoords();
154 aroundRect.x += lt.x;
155 aroundRect.y += lt.y;
156 aroundRect.x = Math.round(aroundRect.x);
157 aroundRect.y = Math.round(aroundRect.y);
158 aroundRect.w = Math.ceil(aroundRect.w);
159 aroundRect.h = Math.ceil(aroundRect.h);
160 this.aroundRect = aroundRect;
161
162 var tooltipText = this.text(o, this.plot);
163 if(tooltipText){
164 DijitTooltip.show(this._format(tooltipText), this.aroundRect, position);
165 }
166 if(!this.mouseOver){
167 this._handle = hub.connect(win.doc, "onclick", this, "onClick");
168 }
169 },
170 onClick: function(){
171 this.process({ type: "onmouseout"});
172 },
173 _recheckPosition: function(obj,rect,position){
174 },
175 _format: function(tooltipText){
176 return tooltipText;
177 }
178 });
179 return has("dojo-bidi")? declare("dojox.charting.action2d.Tooltip", [Tooltip, BidiTooltip]) : Tooltip;
180 });