Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / chart / Tip.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.chart.Tip
17  * Provides tips for Ext.chart.series.Series.
18  */
19 Ext.define('Ext.chart.Tip', {
20
21     /* Begin Definitions */
22
23     requires: ['Ext.tip.ToolTip', 'Ext.chart.TipSurface'],
24
25     /* End Definitions */
26
27     constructor: function(config) {
28         var me = this,
29             surface,
30             sprites,
31             tipSurface;
32         if (config.tips) {
33             me.tipTimeout = null;
34             me.tipConfig = Ext.apply({}, config.tips, {
35                 renderer: Ext.emptyFn,
36                 constrainPosition: false
37             });
38             me.tooltip = Ext.create('Ext.tip.ToolTip', me.tipConfig);
39             me.chart.surface.on('mousemove', me.tooltip.onMouseMove, me.tooltip);
40             me.chart.surface.on('mouseleave', function() {
41                 me.hideTip();
42             });
43             if (me.tipConfig.surface) {
44                 //initialize a surface
45                 surface = me.tipConfig.surface;
46                 sprites = surface.sprites;
47                 tipSurface = Ext.create('Ext.chart.TipSurface', {
48                     id: 'tipSurfaceComponent',
49                     sprites: sprites
50                 });
51                 if (surface.width && surface.height) {
52                     tipSurface.setSize(surface.width, surface.height);
53                 }
54                 me.tooltip.add(tipSurface);
55                 me.spriteTip = tipSurface;
56             }
57         }
58     },
59
60     showTip: function(item) {
61         var me = this;
62         if (!me.tooltip) {
63             return;
64         }
65         clearTimeout(me.tipTimeout);
66         var tooltip = me.tooltip,
67             spriteTip = me.spriteTip,
68             tipConfig = me.tipConfig,
69             trackMouse = tooltip.trackMouse,
70             sprite, surface, surfaceExt, pos, x, y;
71         if (!trackMouse) {
72             tooltip.trackMouse = true;
73             sprite = item.sprite;
74             surface = sprite.surface;
75             surfaceExt = Ext.get(surface.getId());
76             if (surfaceExt) {
77                 pos = surfaceExt.getXY();
78                 x = pos[0] + (sprite.attr.x || 0) + (sprite.attr.translation && sprite.attr.translation.x || 0);
79                 y = pos[1] + (sprite.attr.y || 0) + (sprite.attr.translation && sprite.attr.translation.y || 0);
80                 tooltip.targetXY = [x, y];
81             }
82         }
83         if (spriteTip) {
84             tipConfig.renderer.call(tooltip, item.storeItem, item, spriteTip.surface);
85         } else {
86             tipConfig.renderer.call(tooltip, item.storeItem, item);
87         }
88         tooltip.show();
89         tooltip.trackMouse = trackMouse;
90     },
91
92     hideTip: function(item) {
93         var tooltip = this.tooltip;
94         if (!tooltip) {
95             return;
96         }
97         clearTimeout(this.tipTimeout);
98         this.tipTimeout = setTimeout(function() {
99             tooltip.hide();
100         }, 0);
101     }
102 });