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.2k
/
series_renderer.dart
198 lines (158 loc) · 6.75 KB
/
series_renderer.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// 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 'dart:math' show Point, Rectangle, max;
import 'package:meta/meta.dart';
import 'base_chart.dart' show BaseChart;
import 'chart_canvas.dart' show ChartCanvas;
import 'datum_details.dart' show DatumDetails;
import 'processed_series.dart' show ImmutableSeries, MutableSeries;
import '../layout/layout_view.dart'
show LayoutView, LayoutPosition, LayoutViewConfig, ViewMeasuredSizes;
import '../../common/color.dart' show Color;
import '../../common/graphics_factory.dart' show GraphicsFactory;
import '../../common/symbol_renderer.dart' show SymbolRenderer;
import '../../common/style/style_factory.dart' show StyleFactory;
import '../../data/series.dart' show AttributeKey;
const AttributeKey<String> rendererIdKey =
const AttributeKey<String>('SeriesRenderer.rendererId');
const AttributeKey<SeriesRenderer> rendererKey =
const AttributeKey<SeriesRenderer>('SeriesRenderer.renderer');
abstract class SeriesRenderer<T, D> extends LayoutView {
static const defaultRendererId = 'default';
SymbolRenderer get symbolRenderer;
String get rendererId;
set rendererId(String rendererId);
void onAttach(BaseChart<T, D> chart);
void onDetach(BaseChart<T, D> chart);
void preprocessSeries(List<MutableSeries<T, D>> seriesList);
void configureDomainAxes(List<MutableSeries<T, D>> seriesList);
void configureMeasureAxes(List<MutableSeries<T, D>> seriesList);
void update(List<ImmutableSeries<T, D>> seriesList, bool isAnimating);
void paint(ChartCanvas canvas, double animationPercent);
List<DatumDetails<T, D>> getNearestDatumDetailPerSeries(
Point<double> chartPoint);
}
abstract class BaseSeriesRenderer<T, D> implements SeriesRenderer<T, D> {
final LayoutViewConfig layoutConfig;
String rendererId;
Rectangle<int> _drawAreaBounds;
Rectangle<int> get drawBounds => _drawAreaBounds;
GraphicsFactory _graphicsFactory;
BaseSeriesRenderer({
@required this.rendererId,
@required int layoutPositionOrder,
}) : this.layoutConfig = new LayoutViewConfig(
position: LayoutPosition.DrawArea,
positionOrder: layoutPositionOrder);
@override
GraphicsFactory get graphicsFactory => _graphicsFactory;
@override
set graphicsFactory(GraphicsFactory value) {
_graphicsFactory = value;
}
@override
void onAttach(BaseChart<T, D> chart) {}
@override
void onDetach(BaseChart<T, D> chart) {}
/// Assigns colors to series that are missing their colorFn.
///
/// [emptyCategoryUsesSinglePalette] Flag indicating whether having all
/// series with no categories will use the same or separate palettes.
/// Setting it to true uses various Blues for each series.
/// Setting it to false used different palettes (ie: s1 uses Blue500,
/// s2 uses Red500),
@protected
assignMissingColors(Iterable<MutableSeries<T, D>> seriesList,
{@required bool emptyCategoryUsesSinglePalette}) {
const defaultCategory = '__default__';
// Count up the number of missing series per category, keeping a max across
// categories.
final missingColorCountPerCategory = <String, int>{};
int maxMissing = 0;
bool hasSpecifiedCategory = false;
seriesList.forEach((MutableSeries<T, D> series) {
if (series.colorFn == null) {
// If there is no category, give it a default category to match logic.
String category = series.seriesCategory;
if (category == null) {
category = defaultCategory;
} else {
hasSpecifiedCategory = true;
}
// Increment the missing counts for the category.
final missingCnt = (missingColorCountPerCategory[category] ?? 0) + 1;
missingColorCountPerCategory[category] = missingCnt;
maxMissing = max(maxMissing, missingCnt);
}
});
if (maxMissing > 0) {
// Special handling of only series with empty categories when we want
// to use different palettes.
if (!emptyCategoryUsesSinglePalette && !hasSpecifiedCategory) {
final palettes = StyleFactory.style.getOrderedPalettes(maxMissing);
int index = 0;
seriesList.forEach((MutableSeries series) {
if (series.colorFn == null) {
final color = palettes[index % palettes.length].shadeDefault;
index++;
series.colorFn = (_, __) => color;
}
});
return;
}
// Get a list of palettes to use given the number of categories we've
// seen. One palette per category (but might need to repeat).
final colorPalettes = StyleFactory.style
.getOrderedPalettes(missingColorCountPerCategory.length);
// Create a map of Color palettes for each category. Each Palette uses
// the max for any category to ensure that the gradients look appropriate.
final colorsByCategory = <String, List<Color>>{};
int index = 0;
missingColorCountPerCategory.keys.forEach((String category) {
colorsByCategory[category] =
colorPalettes[index % colorPalettes.length].makeShades(maxMissing);
index++;
// Reset the count so we can use it to count as we set the colorFn.
missingColorCountPerCategory[category] = 0;
});
seriesList.forEach((MutableSeries series) {
if (series.colorFn == null) {
final category = series.seriesCategory ?? defaultCategory;
// Get the current index into the color list.
final colorIndex = missingColorCountPerCategory[category];
missingColorCountPerCategory[category] = colorIndex + 1;
final color = colorsByCategory[category][colorIndex];
series.colorFn = (_, __) => color;
}
});
}
}
@override
ViewMeasuredSizes measure(int maxWidth, int maxHeight) {
return null;
}
@override
void layout(Rectangle<int> componentBounds, Rectangle<int> drawAreaBounds) {
this._drawAreaBounds = drawAreaBounds;
}
@override
Rectangle<int> get componentBounds => this._drawAreaBounds;
@override
void preprocessSeries(List<MutableSeries<T, D>> seriesList) {}
@override
void configureDomainAxes(List<MutableSeries<T, D>> seriesList) {}
@override
void configureMeasureAxes(List<MutableSeries<T, D>> seriesList) {}
}