source: node_modules/d3-shape/src/pie.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 2.3 KB
Line 
1import array from "./array.js";
2import constant from "./constant.js";
3import descending from "./descending.js";
4import identity from "./identity.js";
5import {tau} from "./math.js";
6
7export default function() {
8 var value = identity,
9 sortValues = descending,
10 sort = null,
11 startAngle = constant(0),
12 endAngle = constant(tau),
13 padAngle = constant(0);
14
15 function pie(data) {
16 var i,
17 n = (data = array(data)).length,
18 j,
19 k,
20 sum = 0,
21 index = new Array(n),
22 arcs = new Array(n),
23 a0 = +startAngle.apply(this, arguments),
24 da = Math.min(tau, Math.max(-tau, endAngle.apply(this, arguments) - a0)),
25 a1,
26 p = Math.min(Math.abs(da) / n, padAngle.apply(this, arguments)),
27 pa = p * (da < 0 ? -1 : 1),
28 v;
29
30 for (i = 0; i < n; ++i) {
31 if ((v = arcs[index[i] = i] = +value(data[i], i, data)) > 0) {
32 sum += v;
33 }
34 }
35
36 // Optionally sort the arcs by previously-computed values or by data.
37 if (sortValues != null) index.sort(function(i, j) { return sortValues(arcs[i], arcs[j]); });
38 else if (sort != null) index.sort(function(i, j) { return sort(data[i], data[j]); });
39
40 // Compute the arcs! They are stored in the original data's order.
41 for (i = 0, k = sum ? (da - n * pa) / sum : 0; i < n; ++i, a0 = a1) {
42 j = index[i], v = arcs[j], a1 = a0 + (v > 0 ? v * k : 0) + pa, arcs[j] = {
43 data: data[j],
44 index: i,
45 value: v,
46 startAngle: a0,
47 endAngle: a1,
48 padAngle: p
49 };
50 }
51
52 return arcs;
53 }
54
55 pie.value = function(_) {
56 return arguments.length ? (value = typeof _ === "function" ? _ : constant(+_), pie) : value;
57 };
58
59 pie.sortValues = function(_) {
60 return arguments.length ? (sortValues = _, sort = null, pie) : sortValues;
61 };
62
63 pie.sort = function(_) {
64 return arguments.length ? (sort = _, sortValues = null, pie) : sort;
65 };
66
67 pie.startAngle = function(_) {
68 return arguments.length ? (startAngle = typeof _ === "function" ? _ : constant(+_), pie) : startAngle;
69 };
70
71 pie.endAngle = function(_) {
72 return arguments.length ? (endAngle = typeof _ === "function" ? _ : constant(+_), pie) : endAngle;
73 };
74
75 pie.padAngle = function(_) {
76 return arguments.length ? (padAngle = typeof _ === "function" ? _ : constant(+_), pie) : padAngle;
77 };
78
79 return pie;
80}
Note: See TracBrowser for help on using the repository browser.