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