source: Dev/trunk/d3/src/layout/pie.js @ 76

Last change on this file since 76 was 76, checked in by fpvanagthoven, 14 years ago

d3

File size: 2.6 KB
Line 
1d3.layout.pie = function() {
2  var value = Number,
3      sort = null,
4      startAngle = 0,
5      endAngle = 2 * Math.PI;
6
7  function pie(data, i) {
8
9    // Compute the start angle.
10    var a = +(typeof startAngle === "function"
11        ? startAngle.apply(this, arguments)
12        : startAngle);
13
14    // Compute the angular range (end - start).
15    var k = (typeof endAngle === "function"
16        ? endAngle.apply(this, arguments)
17        : endAngle) - startAngle;
18
19    // Optionally sort the data.
20    var index = d3.range(data.length);
21    if (sort != null) index.sort(function(i, j) {
22      return sort(data[i], data[j]);
23    });
24
25    // Compute the numeric values for each data element.
26    var values = data.map(value);
27
28    // Convert k into a scale factor from value to angle, using the sum.
29    k /= values.reduce(function(p, d) { return p + d; }, 0);
30
31    // Compute the arcs!
32    var arcs = index.map(function(i) {
33      return {
34        data: data[i],
35        value: d = values[i],
36        startAngle: a,
37        endAngle: a += d * k
38      };
39    });
40
41    // Return the arcs in the original data's order.
42    return data.map(function(d, i) {
43      return arcs[index[i]];
44    });
45  }
46
47  /**
48   * Specifies the value function *x*, which returns a nonnegative numeric value
49   * for each datum. The default value function is `Number`. The value function
50   * is passed two arguments: the current datum and the current index.
51   */
52  pie.value = function(x) {
53    if (!arguments.length) return value;
54    value = x;
55    return pie;
56  };
57
58  /**
59   * Specifies a sort comparison operator *x*. The comparator is passed two data
60   * elements from the data array, a and b; it returns a negative value if a is
61   * less than b, a positive value if a is greater than b, and zero if a equals
62   * b.
63   */
64  pie.sort = function(x) {
65    if (!arguments.length) return sort;
66    sort = x;
67    return pie;
68  };
69
70  /**
71   * Specifies the overall start angle of the pie chart. Defaults to 0. The
72   * start angle can be specified either as a constant or as a function; in the
73   * case of a function, it is evaluated once per array (as opposed to per
74   * element).
75   */
76  pie.startAngle = function(x) {
77    if (!arguments.length) return startAngle;
78    startAngle = x;
79    return pie;
80  };
81
82  /**
83   * Specifies the overall end angle of the pie chart. Defaults to 2π. The
84   * end angle can be specified either as a constant or as a function; in the
85   * case of a function, it is evaluated once per array (as opposed to per
86   * element).
87   */
88  pie.endAngle = function(x) {
89    if (!arguments.length) return endAngle;
90    endAngle = x;
91    return pie;
92  };
93
94  return pie;
95};
Note: See TracBrowser for help on using the repository browser.