| OLD | NEW |
| (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 | |
| 9 part of charted.charts; | |
| 10 | |
| 11 abstract class LayoutRendererBase implements LayoutRenderer { | |
| 12 static const MAX_SUPPORTED_ROWS = 250; | |
| 13 final SubscriptionsDisposer _disposer = new SubscriptionsDisposer(); | |
| 14 | |
| 15 LayoutArea area; | |
| 16 ChartSeries series; | |
| 17 ChartTheme theme; | |
| 18 ChartState state; | |
| 19 Rect rect; | |
| 20 | |
| 21 List<int> _valueStateCache; | |
| 22 List<Iterable<String>> _valueStylesCache; | |
| 23 | |
| 24 Element host; | |
| 25 Selection root; | |
| 26 SelectionScope scope; | |
| 27 | |
| 28 StreamController<ChartEvent> mouseOverController; | |
| 29 StreamController<ChartEvent> mouseOutController; | |
| 30 StreamController<ChartEvent> mouseClickController; | |
| 31 | |
| 32 void _ensureAreaAndSeries(ChartArea area, ChartSeries series) { | |
| 33 assert(area != null && series != null); | |
| 34 assert(this.area == null || this.area == area); | |
| 35 if (this.area == null) { | |
| 36 if (area.state != null) { | |
| 37 this.state = area.state; | |
| 38 _disposer.add(this.state.changes.listen((changes) { | |
| 39 resetStylesCache(); | |
| 40 handleStateChanges(changes); | |
| 41 })); | |
| 42 } | |
| 43 } | |
| 44 this.area = area; | |
| 45 this.series = series; | |
| 46 } | |
| 47 | |
| 48 void _ensureReadyToDraw(Element element) { | |
| 49 assert(series != null && area != null); | |
| 50 assert(element != null && element is GElement); | |
| 51 | |
| 52 if (scope == null) { | |
| 53 host = element; | |
| 54 scope = new SelectionScope.element(element); | |
| 55 root = scope.selectElements([host]); | |
| 56 } | |
| 57 | |
| 58 theme = area.theme; | |
| 59 rect = area.layout.renderArea; | |
| 60 resetStylesCache(); | |
| 61 } | |
| 62 | |
| 63 void resetStylesCache() { | |
| 64 var length = math.min(area.data.rows.length, MAX_SUPPORTED_ROWS); | |
| 65 _valueStylesCache = new List(length); | |
| 66 _valueStateCache = new List(length); | |
| 67 _computeValueStates(); | |
| 68 } | |
| 69 | |
| 70 void handleStateChanges(List<ChangeRecord> changes); | |
| 71 | |
| 72 @override | |
| 73 void dispose() { | |
| 74 if (root == null) return; | |
| 75 root.selectAll('.row-group').remove(); | |
| 76 } | |
| 77 | |
| 78 @override | |
| 79 Stream<ChartEvent> get onValueMouseOver { | |
| 80 if (mouseOverController == null) { | |
| 81 mouseOverController = new StreamController.broadcast(sync: true); | |
| 82 } | |
| 83 return mouseOverController.stream; | |
| 84 } | |
| 85 | |
| 86 @override | |
| 87 Stream<ChartEvent> get onValueMouseOut { | |
| 88 if (mouseOutController == null) { | |
| 89 mouseOutController = new StreamController.broadcast(sync: true); | |
| 90 } | |
| 91 return mouseOutController.stream; | |
| 92 } | |
| 93 | |
| 94 @override | |
| 95 Stream<ChartEvent> get onValueClick { | |
| 96 if (mouseClickController == null) { | |
| 97 mouseClickController = new StreamController.broadcast(sync: true); | |
| 98 } | |
| 99 return mouseClickController.stream; | |
| 100 } | |
| 101 | |
| 102 void _computeValueStates() { | |
| 103 var length = math.min(area.data.rows.length, MAX_SUPPORTED_ROWS); | |
| 104 for (int i = 0, len = length; i < len; ++i) { | |
| 105 int flags = 0; | |
| 106 if (state != null) { | |
| 107 if (state.selection.isNotEmpty) { | |
| 108 flags |= (state.isSelected(i)) | |
| 109 ? ChartState.VAL_HIGHLIGHTED | |
| 110 : ChartState.VAL_UNHIGHLIGHTED; | |
| 111 } | |
| 112 if (state.preview == i) { | |
| 113 flags |= ChartState.VAL_HOVERED; | |
| 114 } | |
| 115 } | |
| 116 _valueStateCache[i] = flags; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 Iterable<String> stylesForValue(int row, { bool isTail: false }) { | |
| 121 if (isTail == true) return const[]; | |
| 122 if (_valueStylesCache[row] == null) { | |
| 123 if (state == null) { | |
| 124 _valueStylesCache[row] = const[]; | |
| 125 } else { | |
| 126 var styles = [], | |
| 127 flags = _valueStateCache[row]; | |
| 128 | |
| 129 if (flags & ChartState.VAL_HIGHLIGHTED != 0) { | |
| 130 styles.add(ChartState.VAL_HIGHLIGHTED_CLASS); | |
| 131 } else if (flags & ChartState.VAL_UNHIGHLIGHTED != 0) { | |
| 132 styles.add(ChartState.VAL_UNHIGHLIGHTED_CLASS); | |
| 133 } | |
| 134 if (flags & ChartState.VAL_HOVERED != 0) { | |
| 135 styles.add(ChartState.VAL_HOVERED_CLASS); | |
| 136 } | |
| 137 | |
| 138 _valueStylesCache[row] = styles; | |
| 139 } | |
| 140 } | |
| 141 return _valueStylesCache[row]; | |
| 142 } | |
| 143 | |
| 144 String colorForValue(int row, {bool isTail: false}) => isTail | |
| 145 ? theme.getOtherColor() | |
| 146 : theme.getColorForKey(row, _valueStateCache[row]); | |
| 147 } | |
| OLD | NEW |