source: Dev/branches/rest-dojo-ui/client/dojox/charting/action2d/Tooltip.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 5.7 KB
Line 
1define(["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        /*=====
6        dojo.declare("dojox.charting.action2d.__TooltipCtorArgs", dojox.charting.action2d.__PlotActionCtorArgs, {
7                //      summary:
8                //              Additional arguments for tooltip actions.
9       
10                //      text: Function?
11                //              The function that produces the text to be shown within a tooltip.  By default this will be
12                //              set by the plot in question, by returning the value of the element.
13                text: null
14        });
15        var PlotAction = dojox.charting.action2d.PlotAction;
16        =====*/
17
18        var DEFAULT_TEXT = function(o){
19                var t = o.run && o.run.data && o.run.data[o.index];
20                if(t && typeof t != "number" && (t.tooltip || t.text)){
21                        return t.tooltip || t.text;
22                }
23                if(o.element == "candlestick"){
24                        return '<table cellpadding="1" cellspacing="0" border="0" style="font-size:0.9em;">'
25                                + '<tr><td>Open:</td><td align="right"><strong>' + o.data.open + '</strong></td></tr>'
26                                + '<tr><td>High:</td><td align="right"><strong>' + o.data.high + '</strong></td></tr>'
27                                + '<tr><td>Low:</td><td align="right"><strong>' + o.data.low + '</strong></td></tr>'
28                                + '<tr><td>Close:</td><td align="right"><strong>' + o.data.close + '</strong></td></tr>'
29                                + (o.data.mid !== undefined ? '<tr><td>Mid:</td><td align="right"><strong>' + o.data.mid + '</strong></td></tr>' : '')
30                                + '</table>';
31                }
32                return o.element == "bar" ? o.x : o.y;
33        };
34
35        var pi4 = Math.PI / 4, pi2 = Math.PI / 2;
36       
37        return declare("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                },
45                optionalParams: {},     // no optional parameters
46
47                constructor: function(chart, plot, kwArgs){
48                        //      summary:
49                        //              Create the tooltip action and connect it to the plot.
50                        //      chart: dojox.charting.Chart
51                        //              The chart this action belongs to.
52                        //      plot: String?
53                        //              The plot this action is attached to.  If not passed, "default" is assumed.
54                        //      kwArgs: dojox.charting.action2d.__TooltipCtorArgs?
55                        //              Optional keyword arguments object for setting parameters.
56                        this.text = kwArgs && kwArgs.text ? kwArgs.text : DEFAULT_TEXT;
57                       
58                        this.connect();
59                },
60               
61                process: function(o){
62                        //      summary:
63                        //              Process the action on the given object.
64                        //      o: dojox.gfx.Shape
65                        //              The object on which to process the highlighting action.
66                        if(o.type === "onplotreset" || o.type === "onmouseout"){
67                Tooltip.hide(this.aroundRect);
68                                this.aroundRect = null;
69                                if(o.type === "onplotreset"){
70                                        delete this.angles;
71                                }
72                                return;
73                        }
74                       
75                        if(!o.shape || o.type !== "onmouseover"){ return; }
76                       
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.w = aroundRect.h = 1;
84                                        break;
85                                case "circle":
86                                        aroundRect.x = o.cx - o.cr;
87                                        aroundRect.y = o.cy - o.cr;
88                                        aroundRect.w = aroundRect.h = 2 * o.cr;
89                                        break;
90                                case "column":
91                                        position = ["above", "below"];
92                                        // intentional fall down
93                                case "bar":
94                                        aroundRect = lang.clone(o.shape.getShape());
95                                        break;
96                                case "candlestick":
97                                        aroundRect.x = o.x;
98                                        aroundRect.y = o.y;
99                                        aroundRect.w = o.width;
100                                        aroundRect.h = 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.w = aroundRect.h = 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                       
137                        // adjust relative coordinates to absolute, and remove fractions
138                        var lt = this.chart.getCoords();
139                        aroundRect.x += lt.x;
140                        aroundRect.y += lt.y;
141                        aroundRect.x = Math.round(aroundRect.x);
142                        aroundRect.y = Math.round(aroundRect.y);
143                        aroundRect.w = Math.ceil(aroundRect.w);
144                        aroundRect.h = Math.ceil(aroundRect.h);
145                        this.aroundRect = aroundRect;
146
147                        var tooltip = this.text(o);
148                        if(this.chart.getTextDir){
149                                var isChartDirectionRtl = (html.style(this.chart.node,"direction") == "rtl");
150                                var isBaseTextDirRtl = (this.chart.getTextDir(tooltip) == "rtl");
151                        }
152                        if(tooltip){
153                                if(isBaseTextDirRtl && !isChartDirectionRtl){
154                                        Tooltip.show("<span dir = 'rtl'>" + tooltip +"</span>", this.aroundRect, position);
155                                }
156                                else if(!isBaseTextDirRtl && isChartDirectionRtl){
157                                        Tooltip.show("<span dir = 'ltr'>" + tooltip +"</span>", this.aroundRect, position);
158                                }else{
159                                        Tooltip.show(tooltip, this.aroundRect, position);
160                                }
161                        }
162                }
163        });
164});
Note: See TracBrowser for help on using the repository browser.