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 | 8 |
9 part of charted.charts; | 9 part of charted.charts; |
10 | 10 |
11 class DefaultChartDataImpl extends ChangeNotifier implements ChartData { | 11 class DefaultChartDataImpl extends ChangeNotifier implements ChartData { |
12 Iterable<ChartColumnSpec> _columns; | 12 Iterable<ChartColumnSpec> _columns; |
13 Iterable<Iterable> _rows; | 13 Iterable<Iterable> _rows; |
14 | 14 |
15 bool _hasObservableRows = false; | 15 bool _hasObservableRows = false; |
16 SubscriptionsDisposer _disposer = new SubscriptionsDisposer(); | 16 SubscriptionsDisposer _disposer = new SubscriptionsDisposer(); |
17 | 17 |
18 DefaultChartDataImpl(Iterable<ChartColumnSpec> columns, Iterable<Iterable> row
s) { | 18 DefaultChartDataImpl( |
| 19 Iterable<ChartColumnSpec> columns, Iterable<Iterable> rows) { |
19 this.columns = columns; | 20 this.columns = columns; |
20 this.rows = rows; | 21 this.rows = rows; |
21 } | 22 } |
22 | 23 |
23 set columns(Iterable<ChartColumnSpec> value) { | 24 set columns(Iterable<ChartColumnSpec> value) { |
24 assert(value != null); | 25 assert(value != null); |
25 | 26 |
26 // Create a copy of columns. We do not currently support | 27 // Create a copy of columns. We do not currently support |
27 // changes to the list of columns. Any changes to the spec | 28 // changes to the list of columns. Any changes to the spec |
28 // will be applied at the next ChartBase.draw(); | 29 // will be applied at the next ChartBase.draw(); |
29 this._columns = new List<ChartColumnSpec>.from(value); | 30 this._columns = new List<ChartColumnSpec>.from(value); |
30 } | 31 } |
31 | 32 |
32 Iterable<ChartColumnSpec> get columns => _columns; | 33 Iterable<ChartColumnSpec> get columns => _columns; |
33 | 34 |
34 set rows(Iterable<Iterable> value) { | 35 set rows(Iterable<Iterable> value) { |
35 assert(value != null); | 36 assert(value != null); |
36 | 37 |
37 _rows = value; | 38 _rows = value; |
38 if (_rows is ObservableList) { | 39 if (_rows is ObservableList) { |
39 _disposer.add( | 40 _disposer.add((_rows as ObservableList).listChanges.listen(rowsChanged)); |
40 (_rows as ObservableList).listChanges.listen(rowsChanged)); | |
41 } | 41 } |
42 | 42 |
43 if (_rows.every((row) => row is ObservableList)) { | 43 if (_rows.every((row) => row is ObservableList)) { |
44 _hasObservableRows = true; | 44 _hasObservableRows = true; |
45 for (int i = 0; i < _rows.length; i++) { | 45 for (int i = 0; i < _rows.length; i++) { |
46 var row = _rows.elementAt(i); | 46 var row = _rows.elementAt(i); |
47 _disposer.add(row.listChanges.listen((changes) | 47 _disposer.add( |
48 => _valuesChanged(i, changes)), row); | 48 row.listChanges.listen((changes) => _valuesChanged(i, changes)), |
49 }; | 49 row); |
| 50 } |
| 51 ; |
50 } else if (_rows is Observable) { | 52 } else if (_rows is Observable) { |
51 logger.info('List of rows is Observable, but not rows themselves!'); | 53 logger.info('List of rows is Observable, but not rows themselves!'); |
52 } | 54 } |
53 } | 55 } |
54 | 56 |
55 Iterable<Iterable> get rows => _rows; | 57 Iterable<Iterable> get rows => _rows; |
56 | 58 |
57 rowsChanged(List<ListChangeRecord> changes) { | 59 rowsChanged(List<ListChangeRecord> changes) { |
58 if (_rows is! ObservableList) return; | 60 if (_rows is! ObservableList) return; |
59 notifyChange(new ChartRowChangeRecord(changes)); | 61 notifyChange(new ChartRowChangeRecord(changes)); |
60 | 62 |
61 if (!_hasObservableRows) return; | 63 if (!_hasObservableRows) return; |
62 changes.forEach((ListChangeRecord change) { | 64 changes.forEach((ListChangeRecord change) { |
63 change.removed.forEach((item) => _disposer.unsubscribe(item)); | 65 change.removed.forEach((item) => _disposer.unsubscribe(item)); |
64 | 66 |
65 for(int i = 0; i < change.addedCount; i++) { | 67 for (int i = 0; i < change.addedCount; i++) { |
66 var index = change.index + i, | 68 var index = change.index + i, row = _rows.elementAt(index); |
67 row = _rows.elementAt(index); | |
68 | 69 |
69 if (row is! ObservableList) { | 70 if (row is! ObservableList) { |
70 logger.severe('A non-observable row was added! ' | 71 logger.severe('A non-observable row was added! ' |
71 'Changes on this row will not be monitored'); | 72 'Changes on this row will not be monitored'); |
72 } else { | 73 } else { |
73 _disposer.add(row.listChanges.listen((changes) | 74 _disposer.add( |
74 => _valuesChanged(index, changes)), row); | 75 row.listChanges |
| 76 .listen((changes) => _valuesChanged(index, changes)), |
| 77 row); |
75 } | 78 } |
76 } | 79 } |
77 }); | 80 }); |
78 } | 81 } |
79 | 82 |
80 _valuesChanged(int index, List<ListChangeRecord> changes) { | 83 _valuesChanged(int index, List<ListChangeRecord> changes) { |
81 if (!_hasObservableRows) return; | 84 if (!_hasObservableRows) return; |
82 notifyChange(new ChartValueChangeRecord(index, changes)); | 85 notifyChange(new ChartValueChangeRecord(index, changes)); |
83 } | 86 } |
84 | 87 |
85 @override | 88 @override |
86 String toString() { | 89 String toString() { |
87 var cellDataLength = new List.filled(rows.elementAt(0).length, 0); | 90 var cellDataLength = new List.filled(rows.elementAt(0).length, 0); |
88 for (var i = 0; i < columns.length; i++) { | 91 for (var i = 0; i < columns.length; i++) { |
89 if (cellDataLength[i] < columns.elementAt(i).label.toString().length) { | 92 if (cellDataLength[i] < columns.elementAt(i).label.toString().length) { |
90 cellDataLength[i] = columns.elementAt(i).label.toString().length; | 93 cellDataLength[i] = columns.elementAt(i).label.toString().length; |
91 } | 94 } |
92 } | 95 } |
93 for (var row in rows) { | 96 for (var row in rows) { |
94 for (var i = 0; i < row.length; i++) { | 97 for (var i = 0; i < row.length; i++) { |
95 if (cellDataLength[i] < row.elementAt(i).toString().length) { | 98 if (cellDataLength[i] < row.elementAt(i).toString().length) { |
96 cellDataLength[i] = row.elementAt(i).toString().length; | 99 cellDataLength[i] = row.elementAt(i).toString().length; |
97 } | 100 } |
98 } | 101 } |
99 } | 102 } |
100 | 103 |
101 var totalLength = 1; // 1 for the leading '|'. | 104 var totalLength = 1; // 1 for the leading '|'. |
102 for (var length in cellDataLength) { | 105 for (var length in cellDataLength) { |
103 // 3 for the leading and trailing ' ' padding and trailing '|'. | 106 // 3 for the leading and trailing ' ' padding and trailing '|'. |
104 totalLength += length + 3; | 107 totalLength += length + 3; |
105 } | 108 } |
106 | 109 |
107 // Second pass for building the string buffer and pad each cell with space | 110 // Second pass for building the string buffer and pad each cell with space |
108 // according to the difference between cell string length and max length. | 111 // according to the difference between cell string length and max length. |
109 var strBuffer = new StringBuffer(); | 112 var strBuffer = new StringBuffer(); |
110 strBuffer.write('-' * totalLength + '\n'); | 113 strBuffer.write('-' * totalLength + '\n'); |
111 strBuffer.write('|'); | 114 strBuffer.write('|'); |
(...skipping 15 matching lines...) Expand all Loading... |
127 strBuffer.write(' ' * lengthDiff + ' ${data} |'); | 130 strBuffer.write(' ' * lengthDiff + ' ${data} |'); |
128 | 131 |
129 if (i == row.length - 1) { | 132 if (i == row.length - 1) { |
130 strBuffer.write('\n' + '-' * totalLength + '\n'); | 133 strBuffer.write('\n' + '-' * totalLength + '\n'); |
131 } | 134 } |
132 } | 135 } |
133 } | 136 } |
134 return strBuffer.toString(); | 137 return strBuffer.toString(); |
135 } | 138 } |
136 } | 139 } |
OLD | NEW |