OLD | NEW |
1 // | 1 // |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // | 3 // |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file or at | 5 // license that can be found in the LICENSE file or at |
6 // https://developers.google.com/open-source/licenses/bsd | 6 // https://developers.google.com/open-source/licenses/bsd |
7 // | 7 // |
8 | 8 |
9 part of charted.svg.shapes; | 9 part of charted.svg.shapes; |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 /// [startAngleCallback] is called to get the start angle of the arc. | 29 /// [startAngleCallback] is called to get the start angle of the arc. |
30 /// As with other callbacks, [startAngleCallback] is passed data, index | 30 /// As with other callbacks, [startAngleCallback] is passed data, index |
31 /// and element in the context. | 31 /// and element in the context. |
32 final SelectionCallback<num> startAngleCallback; | 32 final SelectionCallback<num> startAngleCallback; |
33 | 33 |
34 /// [endAngleCallback] is called to get the start angle of the arc. | 34 /// [endAngleCallback] is called to get the start angle of the arc. |
35 /// As with other callbacks, [endAngleCallback] is passed data, index | 35 /// As with other callbacks, [endAngleCallback] is passed data, index |
36 /// and element in the context. | 36 /// and element in the context. |
37 final SelectionCallback<num> endAngleCallback; | 37 final SelectionCallback<num> endAngleCallback; |
38 | 38 |
39 SvgArc({ | 39 SvgArc( |
40 this.innerRadiusCallback : defaultInnerRadiusCallback, | 40 {this.innerRadiusCallback: defaultInnerRadiusCallback, |
41 this.outerRadiusCallback: defaultOuterRadiusCallback, | 41 this.outerRadiusCallback: defaultOuterRadiusCallback, |
42 this.startAngleCallback: defaultStartAngleCallback, | 42 this.startAngleCallback: defaultStartAngleCallback, |
43 this.endAngleCallback: defaultEndAngleCallback | 43 this.endAngleCallback: defaultEndAngleCallback}); |
44 }); | |
45 | 44 |
46 String path(d, int i, Element e) { | 45 String path(d, int i, Element e) { |
47 var ir = innerRadiusCallback(d, i, e), | 46 var ir = innerRadiusCallback(d, i, e), |
48 or = outerRadiusCallback(d, i, e), | 47 or = outerRadiusCallback(d, i, e), |
49 start = startAngleCallback(d, i, e) + _OFFSET, | 48 start = startAngleCallback(d, i, e) + _OFFSET, |
50 end = endAngleCallback(d, i, e) + _OFFSET, | 49 end = endAngleCallback(d, i, e) + _OFFSET, |
51 sa = math.min(start, end), | 50 sa = math.min(start, end), |
52 ea = math.max(start, end), | 51 ea = math.max(start, end), |
53 delta = ea - sa; | 52 delta = ea - sa; |
54 | 53 |
55 if (delta > _MAX) { | 54 if (delta > _MAX) { |
56 return ir > 0 | 55 return ir > 0 |
57 ? "M0,$or" "A$or,$or 0 1,1 0,-$or" "A$or,$or 0 1,1 0,$or" | 56 ? "M0,$or" |
58 "M0,$ir" "A$ir,$ir 0 1,0 0,-$ir" "A$ir,$ir 0 1,0 0,$ir" "Z" | 57 "A$or,$or 0 1,1 0,-$or" |
| 58 "A$or,$or 0 1,1 0,$or" |
| 59 "M0,$ir" |
| 60 "A$ir,$ir 0 1,0 0,-$ir" |
| 61 "A$ir,$ir 0 1,0 0,$ir" |
| 62 "Z" |
59 : "M0,$or" "A$or,$or 0 1,1 0,-$or" "A$or,$or 0 1,1 0,$or" "Z"; | 63 : "M0,$or" "A$or,$or 0 1,1 0,-$or" "A$or,$or 0 1,1 0,$or" "Z"; |
60 } | 64 } |
61 | 65 |
62 var ss = math.sin(sa), | 66 var ss = math.sin(sa), |
63 se = math.sin(ea), | 67 se = math.sin(ea), |
64 cs = math.cos(sa), | 68 cs = math.cos(sa), |
65 ce = math.cos(ea), | 69 ce = math.cos(ea), |
66 df = delta < PI ? 0 : 1; | 70 df = delta < PI ? 0 : 1; |
67 | 71 |
68 return ir > 0 | 72 return ir > 0 |
69 ? "M${or * cs},${or * ss}" "A$or,$or 0 $df,1 ${or * ce},${or * se}" | 73 ? "M${or * cs},${or * ss}" |
70 "L${ir * ce},${ir * se}" "A$ir,$ir 0 $df,0 ${ir * cs},${ir * ss}" | 74 "A$or,$or 0 $df,1 ${or * ce},${or * se}" |
71 "Z" | 75 "L${ir * ce},${ir * se}" |
72 : "M${or * cs},${or * ss}" "A$or,$or 0 $df,1 ${or * ce},${or * se}" | 76 "A$ir,$ir 0 $df,0 ${ir * cs},${ir * ss}" |
73 "L0,0" "Z"; | 77 "Z" |
| 78 : "M${or * cs},${or * ss}" |
| 79 "A$or,$or 0 $df,1 ${or * ce},${or * se}" |
| 80 "L0,0" |
| 81 "Z"; |
74 } | 82 } |
75 | 83 |
76 List centroid(d, int i, Element e) { | 84 List centroid(d, int i, Element e) { |
77 var r = (innerRadiusCallback(d, i, e) + outerRadiusCallback(d, i, e)) / 2, | 85 var r = (innerRadiusCallback(d, i, e) + outerRadiusCallback(d, i, e)) / 2, |
78 a = (startAngleCallback(d, i, e) + endAngleCallback(d, i, e)) / 2 - | 86 a = (startAngleCallback(d, i, e) + endAngleCallback(d, i, e)) / 2 - |
79 math.PI / 2; | 87 math.PI / 2; |
80 return [math.cos(a) * r, math.sin(a) * r]; | 88 return [math.cos(a) * r, math.sin(a) * r]; |
81 } | 89 } |
82 | 90 |
83 /// Default [innerRadiusCallback] returns data.innerRadius | 91 /// Default [innerRadiusCallback] returns data.innerRadius |
(...skipping 15 matching lines...) Expand all Loading... |
99 | 107 |
100 /// Value type for SvgArc as used by default property accessors in SvgArc | 108 /// Value type for SvgArc as used by default property accessors in SvgArc |
101 class SvgArcData { | 109 class SvgArcData { |
102 dynamic data; | 110 dynamic data; |
103 num value; | 111 num value; |
104 num innerRadius; | 112 num innerRadius; |
105 num outerRadius; | 113 num outerRadius; |
106 num startAngle; | 114 num startAngle; |
107 num endAngle; | 115 num endAngle; |
108 | 116 |
109 SvgArcData(this.data, this.value, | 117 SvgArcData(this.data, this.value, this.startAngle, this.endAngle, |
110 this.startAngle, this.endAngle, [ | 118 [this.innerRadius = 0, this.outerRadius = 100]); |
111 this.innerRadius = 0, this.outerRadius = 100 ]); | |
112 } | 119 } |
113 | 120 |
114 | |
115 /// Returns the interpolator between two [SvgArcData] [a] and [b]. | 121 /// Returns the interpolator between two [SvgArcData] [a] and [b]. |
116 /// | 122 /// |
117 /// The interpolator will interpolate the older innerRadius and outerRadius with | 123 /// The interpolator will interpolate the older innerRadius and outerRadius with |
118 /// newer ones, as well as older startAngle and endAngle with newer ones. | 124 /// newer ones, as well as older startAngle and endAngle with newer ones. |
119 Interpolator interpolateSvgArcData(SvgArcData a, SvgArcData b) { | 125 Interpolator interpolateSvgArcData(SvgArcData a, SvgArcData b) { |
120 var ast = a.startAngle, | 126 var ast = a.startAngle, |
121 aen = a.endAngle, | 127 aen = a.endAngle, |
122 ai = a.innerRadius, | 128 ai = a.innerRadius, |
123 ao = a.outerRadius, | 129 ao = a.outerRadius, |
124 bst = b.startAngle - ast, | 130 bst = b.startAngle - ast, |
125 ben = b.endAngle - aen, | 131 ben = b.endAngle - aen, |
126 bi = b.innerRadius - ai, | 132 bi = b.innerRadius - ai, |
127 bo = b.outerRadius - ao; | 133 bo = b.outerRadius - ao; |
128 | 134 |
129 return (t) => new SvgArcData(b.data, b.value, | 135 return (t) => new SvgArcData(b.data, b.value, (ast + bst * t), |
130 (ast + bst * t), (aen + ben * t), (ai + bi * t), (ao + bo * t)); | 136 (aen + ben * t), (ai + bi * t), (ao + bo * t)); |
131 } | 137 } |
OLD | NEW |