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