-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy path_math.js
206 lines (170 loc) · 6.32 KB
/
_math.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// --------- math.js -----------
var math = {
toRadians: function(degrees) {
return degrees * (Math.PI / 180);
},
toDegrees: function(radians) {
return radians * (180 / Math.PI);
},
computePieRadius: function(pie) {
var size = pie.options.size;
var canvasPadding = pie.options.misc.canvasPadding;
// outer radius is either specified (e.g. through the generator), or omitted altogether
// and calculated based on the canvas dimensions. Right now the estimated version isn't great - it should
// be possible to calculate it to precisely generate the maximum sized pie, but it's fussy as heck. Something
// for the next release.
// first, calculate the default _outerRadius
var w = size.canvasWidth - canvasPadding.left - canvasPadding.right;
var h = size.canvasHeight - canvasPadding.top - canvasPadding.bottom;
// now factor in the footer, title & subtitle
if (pie.options.header.location !== "pie-center") {
h -= pie.textComponents.headerHeight;
}
if (pie.textComponents.footer.exists) {
h -= pie.textComponents.footer.h;
}
// for really teeny pies, h may be < 0. Adjust it back
h = (h < 0) ? 0 : h;
var outerRadius = ((w < h) ? w : h) / 3;
var innerRadius, percent;
// if the user specified something, use that instead
if (size.pieOuterRadius !== null) {
if (/%/.test(size.pieOuterRadius)) {
percent = parseInt(size.pieOuterRadius.replace(/[\D]/, ""), 10);
percent = (percent > 99) ? 99 : percent;
percent = (percent < 0) ? 0 : percent;
var smallestDimension = (w < h) ? w : h;
// now factor in the label line size
if (pie.options.labels.outer.format !== "none") {
var pieDistanceSpace = parseInt(pie.options.labels.outer.pieDistance, 10) * 2;
if (smallestDimension - pieDistanceSpace > 0) {
smallestDimension -= pieDistanceSpace;
}
}
outerRadius = Math.floor((smallestDimension / 100) * percent) / 2;
} else {
outerRadius = parseInt(size.pieOuterRadius, 10);
}
}
// inner radius
if (/%/.test(size.pieInnerRadius)) {
percent = parseInt(size.pieInnerRadius.replace(/[\D]/, ""), 10);
percent = (percent > 99) ? 99 : percent;
percent = (percent < 0) ? 0 : percent;
innerRadius = Math.floor((outerRadius / 100) * percent);
} else {
innerRadius = parseInt(size.pieInnerRadius, 10);
}
pie.innerRadius = innerRadius;
pie.outerRadius = outerRadius;
},
getTotalPieSize: function(data) {
var totalSize = 0;
for (var i=0; i<data.length; i++) {
totalSize += data[i].value;
}
return totalSize;
},
sortPieData: function(pie) {
var data = pie.options.data.content;
var sortOrder = pie.options.data.sortOrder;
switch (sortOrder) {
case "none":
// do nothing
break;
case "random":
data = helpers.shuffleArray(data);
break;
case "value-asc":
data.sort(function(a, b) { return (a.value < b.value) ? -1 : 1; });
break;
case "value-desc":
data.sort(function(a, b) { return (a.value < b.value) ? 1 : -1; });
break;
case "label-asc":
data.sort(function(a, b) { return (a.label.toLowerCase() > b.label.toLowerCase()) ? 1 : -1; });
break;
case "label-desc":
data.sort(function(a, b) { return (a.label.toLowerCase() < b.label.toLowerCase()) ? 1 : -1; });
break;
}
return data;
},
// var pieCenter = math.getPieCenter();
getPieTranslateCenter: function(pieCenter) {
return "translate(" + pieCenter.x + "," + pieCenter.y + ")";
},
/**
* Used to determine where on the canvas the center of the pie chart should be. It takes into account the
* height and position of the title, subtitle and footer, and the various paddings.
* @private
*/
calculatePieCenter: function(pie) {
var pieCenterOffset = pie.options.misc.pieCenterOffset;
var hasTopTitle = (pie.textComponents.title.exists && pie.options.header.location !== "pie-center");
var hasTopSubtitle = (pie.textComponents.subtitle.exists && pie.options.header.location !== "pie-center");
var headerOffset = pie.options.misc.canvasPadding.top;
if (hasTopTitle && hasTopSubtitle) {
headerOffset += pie.textComponents.title.h + pie.options.header.titleSubtitlePadding + pie.textComponents.subtitle.h;
} else if (hasTopTitle) {
headerOffset += pie.textComponents.title.h;
} else if (hasTopSubtitle) {
headerOffset += pie.textComponents.subtitle.h;
}
var footerOffset = 0;
if (pie.textComponents.footer.exists) {
footerOffset = pie.textComponents.footer.h + pie.options.misc.canvasPadding.bottom;
}
var x = ((pie.options.size.canvasWidth - pie.options.misc.canvasPadding.left - pie.options.misc.canvasPadding.right) / 2) + pie.options.misc.canvasPadding.left;
var y = ((pie.options.size.canvasHeight - footerOffset - headerOffset) / 2) + headerOffset;
x += pieCenterOffset.x;
y += pieCenterOffset.y;
pie.pieCenter = { x: x, y: y };
},
/**
* Rotates a point (x, y) around an axis (xm, ym) by degrees (a).
* @param x
* @param y
* @param xm
* @param ym
* @param a angle in degrees
* @returns {Array}
*/
rotate: function(x, y, xm, ym, a) {
a = a * Math.PI / 180; // convert to radians
var cos = Math.cos,
sin = Math.sin,
// subtract midpoints, so that midpoint is translated to origin and add it in the end again
xr = (x - xm) * cos(a) - (y - ym) * sin(a) + xm,
yr = (x - xm) * sin(a) + (y - ym) * cos(a) + ym;
return { x: xr, y: yr };
},
/**
* Translates a point x, y by distance d, and by angle a.
* @param x
* @param y
* @param dist
* @param a angle in degrees
*/
translate: function(x, y, d, a) {
var rads = math.toRadians(a);
return {
x: x + d * Math.sin(rads),
y: y - d * Math.cos(rads)
};
},
// from: http://stackoverflow.com/questions/19792552/d3-put-arc-labels-in-a-pie-chart-if-there-is-enough-space
pointIsInArc: function(pt, ptData, d3Arc) {
// Center of the arc is assumed to be 0,0
// (pt.x, pt.y) are assumed to be relative to the center
var r1 = d3Arc.innerRadius()(ptData), // Note: Using the innerRadius
r2 = d3Arc.outerRadius()(ptData),
theta1 = d3Arc.startAngle()(ptData),
theta2 = d3Arc.endAngle()(ptData);
var dist = pt.x * pt.x + pt.y * pt.y,
angle = Math.atan2(pt.x, -pt.y); // Note: different coordinate system
angle = (angle < 0) ? (angle + Math.PI * 2) : angle;
return (r1 * r1 <= dist) && (dist <= r2 * r2) &&
(theta1 <= angle) && (angle <= theta2);
}
};