]> git.proxmox.com Git - sencha-touch.git/blob - src/src/chart/label/Callout.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / src / chart / label / Callout.js
1 /**
2 * @class Ext.chart.label.Callout
3 * @extends Ext.draw.modifier.Modifier
4 *
5 * This is a modifier to place labels and callouts by additional attributes.
6 */
7 Ext.define("Ext.chart.label.Callout", {
8 extend: 'Ext.draw.modifier.Modifier',
9
10 prepareAttributes: function (attr) {
11 if (!attr.hasOwnProperty('calloutOriginal')) {
12 attr.calloutOriginal = Ext.Object.chain(attr);
13 }
14 if (this._previous) {
15 this._previous.prepareAttributes(attr.calloutOriginal);
16 }
17 },
18
19 setAttrs: function (attr, changes) {
20 var callout = attr.callout,
21 origin = attr.calloutOriginal,
22 bbox = attr.bbox.plain,
23 width = (bbox.width || 0) + attr.labelOverflowPadding,
24 height = (bbox.height || 0) + attr.labelOverflowPadding,
25 dx, dy;
26
27 if ('callout' in changes) {
28 callout = changes.callout;
29 }
30
31 if ('callout' in changes || 'calloutPlaceX' in changes || 'calloutPlaceY' in changes || 'x' in changes || 'y' in changes) {
32 var rotationRads = 'rotationRads' in changes ? origin.rotationRads = changes.rotationRads : origin.rotationRads,
33 x = 'x' in changes ? (origin.x = changes.x) : origin.x,
34 y = 'y' in changes ? (origin.y = changes.y) : origin.y,
35 calloutPlaceX = 'calloutPlaceX' in changes ? changes.calloutPlaceX : attr.calloutPlaceX,
36 calloutPlaceY = 'calloutPlaceY' in changes ? changes.calloutPlaceY : attr.calloutPlaceY,
37 calloutVertical = 'calloutVertical' in changes ? changes.calloutVertical : attr.calloutVertical,
38 temp;
39
40 // Normalize Rotations
41 rotationRads %= Math.PI * 2;
42 if (Math.cos(rotationRads) < 0) {
43 rotationRads = (rotationRads + Math.PI) % (Math.PI * 2);
44 }
45
46 if (rotationRads > Math.PI) {
47 rotationRads -= Math.PI * 2;
48 }
49
50 if (calloutVertical) {
51 rotationRads = rotationRads * (1 - callout) - Math.PI / 2 * callout;
52 temp = width;
53 width = height;
54 height = temp;
55 } else {
56 rotationRads = rotationRads * (1 - callout);
57 }
58 changes.rotationRads = rotationRads;
59
60
61 // Placing label.
62 changes.x = x * (1 - callout) + calloutPlaceX * callout;
63 changes.y = y * (1 - callout) + calloutPlaceY * callout;
64
65
66 // Placing the end of the callout line.
67 dx = calloutPlaceX - x;
68 dy = calloutPlaceY - y;
69 if (Math.abs(dy * width) > Math.abs(height * dx)) {
70 // on top/bottom
71 if (dy > 0) {
72 changes.calloutEndX = changes.x - (height / (dy * 2) * dx) * callout;
73 changes.calloutEndY = changes.y - height / 2 * callout;
74 } else {
75 changes.calloutEndX = changes.x + (height / (dy * 2) * dx) * callout;
76 changes.calloutEndY = changes.y + height / 2 * callout;
77 }
78 } else {
79 // on left/right
80 if (dx > 0) {
81 changes.calloutEndX = changes.x - width / 2;
82 changes.calloutEndY = changes.y - (width / (dx * 2) * dy) * callout;
83 } else {
84 changes.calloutEndX = changes.x + width / 2;
85 changes.calloutEndY = changes.y + (width / (dx * 2) * dy) * callout;
86 }
87 }
88 }
89
90 return changes;
91 },
92
93 pushDown: function (attr, changes) {
94 changes = Ext.draw.modifier.Modifier.prototype.pushDown.call(this, attr.calloutOriginal, changes);
95 return this.setAttrs(attr, changes);
96 },
97
98 popUp: function (attr, changes) {
99 attr = Object.getPrototypeOf(attr);
100 changes = this.setAttrs(attr, changes);
101 if (this._next) {
102 return this._next.popUp(attr, changes);
103 } else {
104 return Ext.apply(attr, changes);
105 }
106 }
107 });