|
1 YUI.add('series-curve-util', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 * Provides functionality for drawing curves in a series. |
|
5 * |
|
6 * @module charts |
|
7 * @submodule series-curve-util |
|
8 */ |
|
9 /** |
|
10 * Utility class used for calculating curve points. |
|
11 * |
|
12 * @class CurveUtil |
|
13 * @constructor |
|
14 * @submodule series-curve-util |
|
15 */ |
|
16 function CurveUtil() |
|
17 { |
|
18 } |
|
19 |
|
20 CurveUtil.prototype = { |
|
21 /** |
|
22 * Creates an array of start, end and control points for splines. |
|
23 * |
|
24 * @method getCurveControlPoints |
|
25 * @param {Array} xcoords Collection of x-coordinates used for calculate the curves |
|
26 * @param {Array} ycoords Collection of y-coordinates used for calculate the curves |
|
27 * @return Object |
|
28 * @protected |
|
29 */ |
|
30 getCurveControlPoints: function(xcoords, ycoords) |
|
31 { |
|
32 var outpoints = [], |
|
33 i = 1, |
|
34 l = xcoords.length - 1, |
|
35 xvals = [], |
|
36 yvals = []; |
|
37 |
|
38 |
|
39 // Too few points, need at least two |
|
40 if (l < 1) |
|
41 { |
|
42 return null; |
|
43 } |
|
44 |
|
45 outpoints[0] = { |
|
46 startx: xcoords[0], |
|
47 starty: ycoords[0], |
|
48 endx: xcoords[1], |
|
49 endy: ycoords[1] |
|
50 }; |
|
51 |
|
52 // Special case, the Bezier should be a straight line |
|
53 if (l === 1) |
|
54 { |
|
55 outpoints[0].ctrlx1 = (2.0*xcoords[0] + xcoords[1])/3.0; |
|
56 outpoints[0].ctrly2 = (2.0*ycoords[0] + ycoords[1])/3.0; |
|
57 outpoints[0].ctrlx2 = 2.0*outpoints[0].ctrlx1 - xcoords[0]; |
|
58 outpoints[0].ctrly2 = 2.0*outpoints[0].ctrly1 - ycoords[0]; |
|
59 return outpoints; |
|
60 } |
|
61 |
|
62 for (; i < l; ++i) |
|
63 { |
|
64 outpoints.push({ |
|
65 startx: Math.round(xcoords[i]), |
|
66 starty: Math.round(ycoords[i]), |
|
67 endx: Math.round(xcoords[i+1]), |
|
68 endy: Math.round(ycoords[i+1]) |
|
69 }); |
|
70 xvals[i] = 4.0 * xcoords[i] + 2*xcoords[i+1]; |
|
71 yvals[i] = 4.0*ycoords[i] + 2*ycoords[i+1]; |
|
72 } |
|
73 |
|
74 xvals[0] = xcoords[0] + (2.0 * xcoords[1]); |
|
75 xvals[l-1] = (8.0 * xcoords[l-1] + xcoords[l]) / 2.0; |
|
76 xvals = this.getControlPoints(xvals.concat()); |
|
77 yvals[0] = ycoords[0] + (2.0 * ycoords[1]); |
|
78 yvals[l-1] = (8.0 * ycoords[l-1] + ycoords[l]) / 2.0; |
|
79 yvals = this.getControlPoints(yvals.concat()); |
|
80 |
|
81 for (i = 0; i < l; ++i) |
|
82 { |
|
83 outpoints[i].ctrlx1 = Math.round(xvals[i]); |
|
84 outpoints[i].ctrly1 = Math.round(yvals[i]); |
|
85 |
|
86 if (i < l-1) |
|
87 { |
|
88 outpoints[i].ctrlx2 = Math.round(2*xcoords[i+1] - xvals[i+1]); |
|
89 outpoints[i].ctrly2 = Math.round(2*ycoords[i+1] - yvals[i+1]); |
|
90 } |
|
91 else |
|
92 { |
|
93 outpoints[i].ctrlx2 = Math.round((xcoords[l] + xvals[l-1])/2); |
|
94 outpoints[i].ctrly2 = Math.round((ycoords[l] + yvals[l-1])/2); |
|
95 } |
|
96 } |
|
97 |
|
98 return outpoints; |
|
99 }, |
|
100 |
|
101 /** |
|
102 * Gets the control points for the curve. |
|
103 * |
|
104 * @method getControlPoints |
|
105 * @param {Array} vals Collection of values coords used to generate control points. |
|
106 * @return Array |
|
107 * @private |
|
108 */ |
|
109 getControlPoints: function(vals) |
|
110 { |
|
111 var l = vals.length, |
|
112 x = [], |
|
113 tmp = [], |
|
114 b = 2.0, |
|
115 i = 1; |
|
116 x[0] = vals[0] / b; |
|
117 for (; i < l; ++i) |
|
118 { |
|
119 tmp[i] = 1/b; |
|
120 b = (i < l-1 ? 4.0 : 3.5) - tmp[i]; |
|
121 x[i] = (vals[i] - x[i-1]) / b; |
|
122 } |
|
123 |
|
124 for (i = 1; i < l; ++i) |
|
125 { |
|
126 x[l-i-1] -= tmp[l-i] * x[l-i]; |
|
127 } |
|
128 |
|
129 return x; |
|
130 } |
|
131 }; |
|
132 Y.CurveUtil = CurveUtil; |
|
133 |
|
134 |
|
135 }, '@VERSION@'); |