This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathinitial_selection.dart
132 lines (108 loc) · 4.2 KB
/
initial_selection.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2018 the Charts project authors. Please see the AUTHORS file
// for details.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import '../base_chart.dart' show BaseChart, LifecycleListener;
import '../processed_series.dart' show MutableSeries, SeriesDatum;
import '../selection_model/selection_model.dart' show SelectionModelType;
import 'chart_behavior.dart' show ChartBehavior;
/// Behavior that sets initial selection.
class InitialSelection<D> implements ChartBehavior<D> {
final SelectionModelType selectionModelType;
/// List of series id of initially selected series.
final List<String> selectedSeriesConfig;
/// List of [SeriesDatumConfig] that represents the initially selected datums.
final List<SeriesDatumConfig> selectedDataConfig;
BaseChart<D> _chart;
LifecycleListener<D> _lifecycleListener;
bool _firstDraw = true;
// TODO : When the series changes, if the user does not also
// change the index the wrong item could be highlighted.
InitialSelection(
{this.selectionModelType = SelectionModelType.info,
this.selectedDataConfig,
this.selectedSeriesConfig}) {
_lifecycleListener = new LifecycleListener<D>(onData: _setInitialSelection);
}
void _setInitialSelection(List<MutableSeries<D>> seriesList) {
if (!_firstDraw) {
return;
}
_firstDraw = false;
final selectionModel = _chart.getSelectionModel(selectionModelType);
final selectedData = <SeriesDatum<D>>[];
final selectedSeries = <MutableSeries<D>>[];
final selectedDataMap = <String, List<D>>{};
if (selectedDataConfig != null) {
for (SeriesDatumConfig config in selectedDataConfig) {
selectedDataMap[config.seriesId] ??= <D>[];
selectedDataMap[config.seriesId].add(config.domainValue);
}
// Add to list of selected series.
selectedSeries.addAll(seriesList.where((MutableSeries<D> series) =>
selectedDataMap.keys.contains(series.id)));
// Add to list of selected data.
for (MutableSeries<D> series in seriesList) {
if (selectedDataMap.containsKey(series.id)) {
final domainFn = series.domainFn;
for (var i = 0; i < series.data.length; i++) {
final datum = series.data[i];
if (selectedDataMap[series.id].contains(domainFn(i))) {
selectedData.add(new SeriesDatum(series, datum));
}
}
}
}
}
// Add to list of selected series, if it does not already exist.
if (selectedSeriesConfig != null) {
final remainingSeriesToAdd = selectedSeriesConfig
.where((String seriesId) => !selectedSeries.contains(seriesId))
.toList();
selectedSeries.addAll(seriesList.where((MutableSeries<D> series) =>
remainingSeriesToAdd.contains(series.id)));
}
selectionModel.updateSelection(selectedData, selectedSeries,
notifyListeners: false);
}
@override
void attachTo(BaseChart<D> chart) {
_chart = chart;
chart.addLifecycleListener(_lifecycleListener);
}
@override
void removeFrom(BaseChart<D> chart) {
chart.removeLifecycleListener(_lifecycleListener);
_chart = null;
}
@override
String get role => 'InitialSelection-${selectionModelType.toString()}}';
}
/// Represents a series datum based on series id and datum index.
class SeriesDatumConfig<D> {
final String seriesId;
final D domainValue;
SeriesDatumConfig(this.seriesId, this.domainValue);
@override
bool operator ==(Object o) {
return o is SeriesDatumConfig &&
seriesId == o.seriesId &&
domainValue == o.domainValue;
}
@override
int get hashCode {
int hashcode = seriesId.hashCode;
hashcode = hashcode * 37 + domainValue.hashCode;
return hashcode;
}
}