-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
radar_chart_painter.dart
388 lines (316 loc) · 11.9 KB
/
radar_chart_painter.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import 'dart:math' show pi, cos, sin, min;
import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart';
import 'package:fl_chart/src/utils/canvas_wrapper.dart';
import 'package:fl_chart/src/utils/utils.dart';
import 'package:flutter/material.dart';
import '../../../fl_chart.dart';
/// Paints [RadarChartData] in the canvas, it can be used in a [CustomPainter]
class RadarChartPainter extends BaseChartPainter<RadarChartData> {
late Paint _borderPaint, _backgroundPaint, _gridPaint, _tickPaint;
late Paint _graphPaint, _graphBorderPaint, _graphPointPaint;
late TextPainter _ticksTextPaint, _titleTextPaint;
List<RadarDataSetsPosition>? dataSetsPosition;
/// Paints [dataList] into canvas, it is the animating [RadarChartData],
/// [targetData] is the animation's target and remains the same
/// during animation, then we should use it when we need to show
/// tooltips or something like that, because [dataList] is changing constantly.
///
/// [textScale] used for scaling texts inside the chart,
/// parent can use [MediaQuery.textScaleFactor] to respect
/// the system's font size.
RadarChartPainter() : super() {
_backgroundPaint = Paint()
..style = PaintingStyle.fill
..isAntiAlias = true;
_borderPaint = Paint()..style = PaintingStyle.stroke;
_gridPaint = Paint()..style = PaintingStyle.stroke;
_tickPaint = Paint()..style = PaintingStyle.stroke;
_graphPaint = Paint();
_graphBorderPaint = Paint();
_graphPointPaint = Paint();
_ticksTextPaint = TextPainter();
_titleTextPaint = TextPainter();
}
/// Paints [RadarChartData] into the provided canvas.
@override
void paint(BuildContext context, CanvasWrapper canvasWrapper,
PaintHolder<RadarChartData> holder) {
super.paint(context, canvasWrapper, holder);
final data = holder.data;
if (data.dataSets.isEmpty) {
return;
}
dataSetsPosition = calculateDataSetsPosition(canvasWrapper.size, holder);
drawGrids(canvasWrapper, holder);
drawTicks(context, canvasWrapper, holder);
drawTitles(context, canvasWrapper, holder);
drawDataSets(canvasWrapper, holder);
}
@visibleForTesting
void drawTicks(BuildContext context, CanvasWrapper canvasWrapper,
PaintHolder<RadarChartData> holder) {
final data = holder.data;
final size = canvasWrapper.size;
final centerX = radarCenterX(size);
final centerY = radarCenterY(size);
final centerOffset = Offset(centerX, centerY);
/// controls Radar chart size
final radius = radarRadius(size);
_backgroundPaint.color = data.radarBackgroundColor;
_borderPaint
..color = data.radarBorderData.color
..strokeWidth = data.radarBorderData.width;
if (data.radarShape == RadarShape.circle) {
/// draw radar background
canvasWrapper.drawCircle(centerOffset, radius, _backgroundPaint);
/// draw radar border
canvasWrapper.drawCircle(centerOffset, radius, _borderPaint);
} else {
final path =
_generatePolygonPath(centerX, centerY, radius, data.titleCount);
/// draw radar background
canvasWrapper.drawPath(path, _backgroundPaint);
/// draw radar border
canvasWrapper.drawPath(path, _borderPaint);
}
final dataSetMaxValue = data.maxEntry.value;
final dataSetMinValue = data.minEntry.value;
final tickSpace = (dataSetMaxValue - dataSetMinValue) / data.tickCount;
final ticks = <double>[];
for (var tick = dataSetMinValue;
tick <= dataSetMaxValue;
tick = tick + tickSpace) {
ticks.add(tick);
}
final tickDistance = radius / (ticks.length);
_tickPaint
..color = data.tickBorderData.color
..strokeWidth = data.tickBorderData.width;
/// draw radar ticks
ticks.sublist(0, ticks.length - 1).asMap().forEach(
(index, tick) {
final tickRadius = tickDistance * (index + 1);
if (data.radarShape == RadarShape.circle) {
canvasWrapper.drawCircle(centerOffset, tickRadius, _tickPaint);
} else {
canvasWrapper.drawPath(
_generatePolygonPath(centerX, centerY, tickRadius, data.titleCount),
_tickPaint,
);
}
_ticksTextPaint
..text = TextSpan(
text: tick.toStringAsFixed(1),
style: Utils().getThemeAwareTextStyle(context, data.ticksTextStyle),
)
..textDirection = TextDirection.ltr;
_ticksTextPaint.layout(minWidth: 0, maxWidth: size.width);
canvasWrapper.drawText(
_ticksTextPaint,
Offset(centerX + 5, centerY - tickRadius - _ticksTextPaint.height),
);
},
);
}
Path _generatePolygonPath(
double centerX, double centerY, double radius, int count) {
final path = Path();
path.moveTo(centerX, centerY - radius);
final angle = (2 * pi) / count;
for (var index = 0; index < count; index++) {
final xAngle = cos(angle * index - pi / 2);
final yAngle = sin(angle * index - pi / 2);
path.lineTo(centerX + radius * xAngle, centerY + radius * yAngle);
}
path.lineTo(centerX, centerY - radius);
return path;
}
void drawGrids(
CanvasWrapper canvasWrapper, PaintHolder<RadarChartData> holder) {
final data = holder.data;
final size = canvasWrapper.size;
final centerX = radarCenterX(size);
final centerY = radarCenterY(size);
final centerOffset = Offset(centerX, centerY);
/// controls Radar chart size
final radius = radarRadius(size);
final angle = (2 * pi) / data.titleCount;
/// drawing grids
for (var index = 0; index < data.titleCount; index++) {
final endX = centerX + radius * cos(angle * index - pi / 2);
final endY = centerY + radius * sin(angle * index - pi / 2);
final gridOffset = Offset(endX, endY);
_gridPaint
..color = data.gridBorderData.color
..strokeWidth = data.gridBorderData.width;
canvasWrapper.drawLine(centerOffset, gridOffset, _gridPaint);
}
}
@visibleForTesting
void drawTitles(BuildContext context, CanvasWrapper canvasWrapper,
PaintHolder<RadarChartData> holder) {
final data = holder.data;
if (data.getTitle == null) return;
final size = canvasWrapper.size;
final centerX = radarCenterX(size);
final centerY = radarCenterY(size);
/// controls Radar chart size
final radius = radarRadius(size);
final diffAngle = (2 * pi) / data.titleCount;
final style = Utils().getThemeAwareTextStyle(context, data.titleTextStyle);
_titleTextPaint
..textAlign = TextAlign.center
..textDirection = TextDirection.ltr
..textScaleFactor = holder.textScale;
for (var index = 0; index < data.titleCount; index++) {
final baseTitleAngle = Utils().degrees(diffAngle * index);
final title = data.getTitle!(index, baseTitleAngle);
final span = TextSpan(text: title.text, style: style);
_titleTextPaint.text = span;
_titleTextPaint.layout();
final angle = diffAngle * index - pi / 2;
final threshold = 1.0 + data.titlePositionPercentageOffset;
final titleX = centerX +
cos(angle) * (radius * threshold + (_titleTextPaint.height / 2));
final titleY = centerY +
sin(angle) * (radius * threshold + (_titleTextPaint.height / 2));
Rect rect = Rect.fromLTWH(
titleX,
titleY,
_titleTextPaint.width,
_titleTextPaint.height,
);
final rectDrawOffset = Offset(rect.left, rect.top);
final drawTitleDegrees = (angle * 180 / pi) + 90;
canvasWrapper.drawRotated(
size: rect.size,
rotationOffset: Offset(
-rect.width / 2,
-rect.height / 2,
),
drawOffset: rectDrawOffset,
angle: drawTitleDegrees,
drawCallback: () {
canvasWrapper.drawText(
_titleTextPaint,
rect.topLeft,
title.angle - baseTitleAngle,
);
},
);
}
}
@visibleForTesting
void drawDataSets(
CanvasWrapper canvasWrapper, PaintHolder<RadarChartData> holder) {
final data = holder.data;
// we will use dataSetsPosition to draw the graphs
dataSetsPosition ??= calculateDataSetsPosition(canvasWrapper.size, holder);
dataSetsPosition!.asMap().forEach((index, dataSetOffset) {
final graph = data.dataSets[index];
_graphPaint
..color = graph.fillColor
..style = PaintingStyle.fill;
_graphBorderPaint
..color = graph.borderColor
..style = PaintingStyle.stroke
..strokeWidth = graph.borderWidth;
_graphPointPaint
..color = _graphBorderPaint.color
..style = PaintingStyle.fill;
final path = Path();
final firstOffset = Offset(
dataSetOffset.entriesOffset.first.dx,
dataSetOffset.entriesOffset.first.dy,
);
path.moveTo(firstOffset.dx, firstOffset.dy);
canvasWrapper.drawCircle(
firstOffset,
graph.entryRadius,
_graphPointPaint,
);
dataSetOffset.entriesOffset.asMap().forEach((index, pointOffset) {
if (index == 0) return;
path.lineTo(pointOffset.dx, pointOffset.dy);
canvasWrapper.drawCircle(
pointOffset,
graph.entryRadius,
_graphPointPaint,
);
});
path.close();
canvasWrapper.drawPath(path, _graphPaint);
canvasWrapper.drawPath(path, _graphBorderPaint);
});
}
RadarTouchedSpot? handleTouch(
Offset touchedPoint, Size viewSize, PaintHolder<RadarChartData> holder) {
final targetData = holder.targetData;
dataSetsPosition ??= calculateDataSetsPosition(viewSize, holder);
for (var i = 0; i < dataSetsPosition!.length; i++) {
final dataSetPosition = dataSetsPosition![i];
for (var j = 0; j < dataSetPosition.entriesOffset.length; j++) {
final entryOffset = dataSetPosition.entriesOffset[j];
if ((touchedPoint.dx - entryOffset.dx).abs() <=
targetData.radarTouchData.touchSpotThreshold &&
(touchedPoint.dy - entryOffset.dy).abs() <=
targetData.radarTouchData.touchSpotThreshold) {
return RadarTouchedSpot(
targetData.dataSets[i],
i,
targetData.dataSets[i].dataEntries[j],
j,
FlSpot(entryOffset.dx, entryOffset.dy),
entryOffset,
);
}
}
}
return null;
}
@visibleForTesting
double radarCenterY(Size size) => size.height / 2.0;
@visibleForTesting
double radarCenterX(Size size) => size.width / 2.0;
@visibleForTesting
double radarRadius(Size size) =>
min(radarCenterX(size), radarCenterY(size)) * 0.8;
@visibleForTesting
List<RadarDataSetsPosition> calculateDataSetsPosition(
Size viewSize,
PaintHolder<RadarChartData> holder,
) {
final data = holder.data;
final centerX = radarCenterX(viewSize);
final centerY = radarCenterY(viewSize);
final radius = radarRadius(viewSize);
final scale = radius / data.maxEntry.value;
final angle = (2 * pi) / data.titleCount;
final dataSetsPosition = List<RadarDataSetsPosition>.filled(
data.dataSets.length,
const RadarDataSetsPosition([]),
);
for (var i = 0; i < data.dataSets.length; i++) {
final dataSet = data.dataSets[i];
final entriesOffset =
List<Offset>.filled(dataSet.dataEntries.length, Offset.zero);
for (var j = 0; j < dataSet.dataEntries.length; j++) {
final point = dataSet.dataEntries[j];
final xAngle = cos(angle * j - pi / 2);
final yAngle = sin(angle * j - pi / 2);
final scaledPoint = scale * point.value;
final entryOffset = Offset(
centerX + scaledPoint * xAngle,
centerY + scaledPoint * yAngle,
);
entriesOffset[j] = entryOffset;
}
dataSetsPosition[i] = RadarDataSetsPosition(entriesOffset);
}
return dataSetsPosition;
}
}
class RadarDataSetsPosition {
final List<Offset> entriesOffset;
const RadarDataSetsPosition(this.entriesOffset);
}