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 part of charted.layout; | 8 part of charted.layout; |
9 | 9 |
10 /** | 10 /** |
(...skipping 25 matching lines...) Expand all Loading... |
36 * Comparator that is used to set the sort order of values. If not | 36 * Comparator that is used to set the sort order of values. If not |
37 * specified, the input order is used. | 37 * specified, the input order is used. |
38 */ | 38 */ |
39 Comparator<num> compare = null; | 39 Comparator<num> compare = null; |
40 | 40 |
41 /** | 41 /** |
42 * Return a list of SvgArcData objects that could be used to create | 42 * Return a list of SvgArcData objects that could be used to create |
43 * arcs in a pie-chart or donut-chart. | 43 * arcs in a pie-chart or donut-chart. |
44 */ | 44 */ |
45 List layout(List data, [int ei, Element e]) { | 45 List layout(List data, [int ei, Element e]) { |
46 var values = new List.generate(data.length, | 46 var values = |
47 (int i) => accessor(data[i], i)), | 47 new List.generate(data.length, (int i) => accessor(data[i], i)), |
48 startAngle = startAngleCallback(data, ei, e), | 48 startAngle = startAngleCallback(data, ei, e), |
49 endAngle = endAngleCallback(data, ei, e), | 49 endAngle = endAngleCallback(data, ei, e), |
50 total = sum(values), | 50 total = sum(values), |
51 scaleFactor = (endAngle - startAngle) / (total > 0 ? total : 1), | 51 scaleFactor = (endAngle - startAngle) / (total > 0 ? total : 1), |
52 index = new Range.integers(values.length).toList(), | 52 index = new Range.integers(values.length).toList(), |
53 arcs = new List(data.length); | 53 arcs = new List(data.length); |
54 | 54 |
55 if (compare != null) { | 55 if (compare != null) { |
56 index.sort((left, right) => compare(data[left], data[right])); | 56 index.sort((left, right) => compare(data[left], data[right])); |
57 } | 57 } |
58 | 58 |
59 int count = 0; | 59 int count = 0; |
60 index.forEach((i) { | 60 index.forEach((i) { |
61 endAngle = startAngle + values[i] * scaleFactor; | 61 endAngle = startAngle + values[i] * scaleFactor; |
62 arcs[count++] = new SvgArcData(data[i], values[i], startAngle, endAngle); | 62 arcs[count++] = new SvgArcData(data[i], values[i], startAngle, endAngle); |
63 startAngle = endAngle; | 63 startAngle = endAngle; |
64 }); | 64 }); |
65 | 65 |
66 return arcs; | 66 return arcs; |
67 } | 67 } |
68 | 68 |
69 /** Sets a constant value to start angle of the layout */ | 69 /** Sets a constant value to start angle of the layout */ |
70 set startAngle(num value) => | 70 set startAngle(num value) => startAngleCallback = toCallback(value); |
71 startAngleCallback = toCallback(value); | |
72 | 71 |
73 /** Sets a constant value to end angle of the layout */ | 72 /** Sets a constant value to end angle of the layout */ |
74 set endAngle(num value) => | 73 set endAngle(num value) => endAngleCallback = toCallback(value); |
75 endAngleCallback = toCallback(value); | |
76 | 74 |
77 /** Default value accessor */ | 75 /** Default value accessor */ |
78 static num defaultValueAccessor(num d, i) => d; | 76 static num defaultValueAccessor(num d, i) => d; |
79 | 77 |
80 /** Default start angle callback - returns 0 */ | 78 /** Default start angle callback - returns 0 */ |
81 static num defaultStartAngleCallback(d, i, _) => 0; | 79 static num defaultStartAngleCallback(d, i, _) => 0; |
82 | 80 |
83 /** Default end angle callback - returns 2 * PI */ | 81 /** Default end angle callback - returns 2 * PI */ |
84 static num defaultEndAngleCallback(d, i, _) => 2 * PI; | 82 static num defaultEndAngleCallback(d, i, _) => 2 * PI; |
85 } | 83 } |
OLD | NEW |