Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: charted/lib/layout/src/pie_layout.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://developers.google.com/open-source/licenses/bsd
7 */
8 part of charted.layout;
9
10 /**
11 * Utility class to create arc definitions that can be used by the SvgArc
12 * to generate arcs for pie and donut charts
13 */
14 class PieLayout {
15 /**
16 * Callback to convert datum to values used for layout
17 * Defaults to [defaultValueAccessor]
18 */
19 SelectionValueAccessor<num> accessor = defaultValueAccessor;
20
21 /**
22 * Callback to get the start angle for the pie. This callback is
23 * called once per list of value (i.e once per call to [layout])
24 * Defaults to [defaultStartAngleCallback]
25 */
26 SelectionCallback<num> startAngleCallback = defaultStartAngleCallback;
27
28 /**
29 * Callback to get the start angle for the pie. This callback is
30 * called once per list of value (i.e once per call to [layout])
31 * Defaults to [defaultEndAngleCallback]
32 */
33 SelectionCallback<num> endAngleCallback = defaultEndAngleCallback;
34
35 /**
36 * Comparator that is used to set the sort order of values. If not
37 * specified, the input order is used.
38 */
39 Comparator<num> compare = null;
40
41 /**
42 * Return a list of SvgArcData objects that could be used to create
43 * arcs in a pie-chart or donut-chart.
44 */
45 List layout(List data, [int ei, Element e]) {
46 var values = new List.generate(data.length,
47 (int i) => accessor(data[i], i)),
48 startAngle = startAngleCallback(data, ei, e),
49 endAngle = endAngleCallback(data, ei, e),
50 total = sum(values),
51 scaleFactor = (endAngle - startAngle) / (total > 0 ? total : 1),
52 index = new Range.integers(values.length).toList(),
53 arcs = new List(data.length);
54
55 if (compare != null) {
56 index.sort((left, right) => compare(data[left], data[right]));
57 }
58
59 int count = 0;
60 index.forEach((i) {
61 endAngle = startAngle + values[i] * scaleFactor;
62 arcs[count++] = new SvgArcData(data[i], values[i], startAngle, endAngle);
63 startAngle = endAngle;
64 });
65
66 return arcs;
67 }
68
69 /** Sets a constant value to start angle of the layout */
70 set startAngle(num value) =>
71 startAngleCallback = toCallback(value);
72
73 /** Sets a constant value to end angle of the layout */
74 set endAngle(num value) =>
75 endAngleCallback = toCallback(value);
76
77 /** Default value accessor */
78 static num defaultValueAccessor(num d, i) => d;
79
80 /** Default start angle callback - returns 0 */
81 static num defaultStartAngleCallback(d, i, _) => 0;
82
83 /** Default end angle callback - returns 2 * PI */
84 static num defaultEndAngleCallback(d, i, _) => 2 * PI;
85 }
OLDNEW
« no previous file with comments | « charted/lib/layout/src/hierarchy_layout.dart ('k') | charted/lib/layout/src/treemap_layout.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698