| 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 /// | |
| 12 /// Model that maintains state of each visualization item. Each entry in the | |
| 13 /// legend is considered one visualization item. | |
| 14 /// - In [CartesianArea] it is always a column. | |
| 15 /// - In [LayoutArea] renders choose either columns or rows. | |
| 16 /// | |
| 17 class DefaultChartStateImpl extends ChangeNotifier implements ChartState { | |
| 18 final bool isMultiSelect; | |
| 19 final bool isMultiHighlight; | |
| 20 final bool isSelectOrHighlight; | |
| 21 final bool supportColumnSelection; | |
| 22 final bool supportColumnPreview; | |
| 23 final bool supportValueHighlight; | |
| 24 final bool supportValueHover; | |
| 25 | |
| 26 LinkedHashSet<int> hidden = new LinkedHashSet<int>(); | |
| 27 | |
| 28 LinkedHashSet<int> selection = new LinkedHashSet<int>(); | |
| 29 LinkedHashSet<Pair<int,int>> highlights = new LinkedHashSet<Pair<int,int>>(); | |
| 30 | |
| 31 Pair<int,int> _hovered; | |
| 32 int _preview; | |
| 33 | |
| 34 DefaultChartStateImpl({ | |
| 35 this.supportColumnSelection: true, | |
| 36 this.supportColumnPreview: true, | |
| 37 this.supportValueHighlight: true, | |
| 38 this.supportValueHover: true, | |
| 39 this.isMultiSelect: false, | |
| 40 this.isMultiHighlight: false, | |
| 41 this.isSelectOrHighlight: true}); | |
| 42 | |
| 43 set hovered(Pair<int,int> value) { | |
| 44 if (!this.supportValueHover) return null; | |
| 45 if (value != _hovered) { | |
| 46 _hovered = value; | |
| 47 notifyChange(new ChartHoverChangeRecord(_hovered)); | |
| 48 } | |
| 49 return value; | |
| 50 } | |
| 51 Pair<int,int> get hovered => _hovered; | |
| 52 | |
| 53 set preview(int value) { | |
| 54 if (!this.supportColumnPreview) return null; | |
| 55 if (value != _preview) { | |
| 56 _preview = value; | |
| 57 notifyChange(new ChartPreviewChangeRecord(_preview)); | |
| 58 } | |
| 59 return value; | |
| 60 } | |
| 61 int get preview => _preview; | |
| 62 | |
| 63 bool unhide(int id) { | |
| 64 if (hidden.contains(id)) { | |
| 65 hidden.remove(id); | |
| 66 notifyChange(new ChartVisibilityChangeRecord(unhide:id)); | |
| 67 } | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 bool hide(int id) { | |
| 72 if (!hidden.contains(id)) { | |
| 73 hidden.add(id); | |
| 74 notifyChange(new ChartVisibilityChangeRecord(hide:id)); | |
| 75 } | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 bool isVisible(int id) => !hidden.contains(id); | |
| 80 | |
| 81 bool select(int id) { | |
| 82 if (!this.supportColumnSelection) return false; | |
| 83 if (!selection.contains(id)) { | |
| 84 if (!isMultiSelect) { | |
| 85 selection.clear(); | |
| 86 } | |
| 87 if (isSelectOrHighlight) { | |
| 88 highlights.clear(); | |
| 89 } | |
| 90 selection.add(id); | |
| 91 notifyChange(new ChartSelectionChangeRecord(add:id)); | |
| 92 } | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 bool unselect(int id) { | |
| 97 if (selection.contains(id)) { | |
| 98 selection.remove(id); | |
| 99 notifyChange(new ChartSelectionChangeRecord(remove:id)); | |
| 100 } | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 bool isSelected(int id) => selection.contains(id); | |
| 105 | |
| 106 bool highlight(int column, int row) { | |
| 107 if (!this.supportValueHighlight) return false; | |
| 108 if (!isHighlighted(column, row)) { | |
| 109 if (!isMultiHighlight) { | |
| 110 highlights.clear(); | |
| 111 } | |
| 112 if (isSelectOrHighlight) { | |
| 113 selection.clear(); | |
| 114 } | |
| 115 var item = new Pair(column, row); | |
| 116 highlights.add(item); | |
| 117 notifyChange(new ChartHighlightChangeRecord(add: item)); | |
| 118 } | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 bool unhighlight(int column, int row) { | |
| 123 if (isHighlighted(column, row)) { | |
| 124 var item = new Pair(column, row); | |
| 125 highlights.remove(item); | |
| 126 notifyChange(new ChartHighlightChangeRecord(remove: item)); | |
| 127 } | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 bool isHighlighted(int column, int row) => | |
| 132 highlights.any((x) => x.first == column && x.last == row); | |
| 133 } | |
| OLD | NEW |