-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathchart_painter.dart
More file actions
322 lines (297 loc) · 10.3 KB
/
chart_painter.dart
File metadata and controls
322 lines (297 loc) · 10.3 KB
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
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'candle_data.dart';
import 'painter_params.dart';
typedef TimeLabelGetter = String Function(int timestamp, int visibleDataCount);
typedef PriceLabelGetter = String Function(double price);
typedef OverlayInfoGetter = Map<String, String> Function(CandleData candle);
class ChartPainter extends CustomPainter {
final PainterParams params;
final TimeLabelGetter getTimeLabel;
final PriceLabelGetter getPriceLabel;
final OverlayInfoGetter getOverlayInfo;
ChartPainter({
required this.params,
required this.getTimeLabel,
required this.getPriceLabel,
required this.getOverlayInfo,
});
@override
void paint(Canvas canvas, Size size) {
// Draw time labels (dates) & price labels
_drawTimeLabels(canvas, params);
_drawPriceGridAndLabels(canvas, params);
// Draw prices, volumes & trend line
canvas.save();
canvas.clipRect(Offset.zero & Size(params.chartWidth, params.chartHeight));
// canvas.drawRect(
// // apply yellow tint to clipped area (for debugging)
// Offset.zero & Size(params.chartWidth, params.chartHeight),
// Paint()..color = Colors.yellow[100]!,
// );
canvas.translate(params.xShift, 0);
for (int i = 0; i < params.candles.length; i++) {
_drawSingleDay(canvas, params, i);
}
canvas.restore();
// Draw tap highlight & overlay
if (params.tapPosition != null) {
if (params.tapPosition!.dx < params.chartWidth) {
_drawTapHighlightAndOverlay(canvas, params);
}
}
}
void _drawTimeLabels(canvas, PainterParams params) {
// We draw one time label per 90 pixels of screen width
final lineCount = params.chartWidth ~/ 90;
final gap = 1 / (lineCount + 1);
for (int i = 1; i <= lineCount; i++) {
double x = i * gap * params.chartWidth;
final index = params.getCandleIndexFromOffset(x);
if (index < params.candles.length) {
final candle = params.candles[index];
final visibleDataCount = params.candles.length;
final timeTp = TextPainter(
text: TextSpan(
text: getTimeLabel(candle.timestamp, visibleDataCount),
style: params.style.timeLabelStyle,
),
)
..textDirection = TextDirection.ltr
..layout();
// Align texts towards vertical bottom
final topPadding = params.style.timeLabelHeight - timeTp.height;
timeTp.paint(
canvas,
Offset(x - timeTp.width / 2, params.chartHeight + topPadding),
);
}
}
}
void _drawPriceGridAndLabels(canvas, PainterParams params) {
[0.0, 0.25, 0.5, 0.75, 1.0]
.map((v) => ((params.maxPrice - params.minPrice) * v) + params.minPrice)
.forEach((y) {
canvas.drawLine(
Offset(0, params.fitPrice(y)),
Offset(params.chartWidth, params.fitPrice(y)),
Paint()
..strokeWidth = 0.5
..color = params.style.priceGridLineColor,
);
final priceTp = TextPainter(
text: TextSpan(
text: getPriceLabel(y),
style: params.style.priceLabelStyle,
),
)
..textDirection = TextDirection.ltr
..layout();
priceTp.paint(
canvas,
Offset(
params.chartWidth + 4,
params.fitPrice(y) - priceTp.height / 2,
));
});
}
void _drawSingleDay(canvas, PainterParams params, int i) {
final candle = params.candles[i];
final x = i * params.candleWidth;
final thickWidth = max(params.candleWidth * 0.8, 0.8);
final thinWidth = max(params.candleWidth * 0.2, 0.2);
// Draw price bar
final open = candle.open;
final close = candle.close;
final high = candle.high;
final low = candle.low;
if (open != null && close != null) {
final color = open > close
? params.style.priceLossColor
: params.style.priceGainColor;
canvas.drawLine(
Offset(x, params.fitPrice(open)),
Offset(x, params.fitPrice(close)),
Paint()
..strokeWidth = thickWidth
..color = color,
);
if (high != null && low != null) {
canvas.drawLine(
Offset(x, params.fitPrice(high)),
Offset(x, params.fitPrice(low)),
Paint()
..strokeWidth = thinWidth
..color = color,
);
}
}
// Draw volume bar
final volume = candle.volume;
if (volume != null) {
canvas.drawLine(
Offset(x, params.chartHeight),
Offset(x, params.fitVolume(volume)),
Paint()
..strokeWidth = thickWidth
..color = params.style.volumeColor,
);
}
// Draw trend line
for (int j = 0; j < candle.trends.length; j++) {
final trendLinePaint = params.style.trendLineStyles.at(j) ??
(Paint()
..strokeWidth = 2.0
..strokeCap = StrokeCap.round
..color = Colors.blue);
final pt = candle.trends.at(j); // current data point
final prevPt = params.candles.at(i - 1)?.trends.at(j);
if (pt != null && prevPt != null) {
canvas.drawLine(
Offset(x - params.candleWidth, params.fitPrice(prevPt)),
Offset(x, params.fitPrice(pt)),
trendLinePaint,
);
}
if (i == 0) {
// In the front, draw an extra line connecting to out-of-window data
if (pt != null && params.leadingTrends?.at(j) != null) {
canvas.drawLine(
Offset(x - params.candleWidth,
params.fitPrice(params.leadingTrends!.at(j)!)),
Offset(x, params.fitPrice(pt)),
trendLinePaint,
);
}
} else if (i == params.candles.length - 1) {
// At the end, draw an extra line connecting to out-of-window data
if (pt != null && params.trailingTrends?.at(j) != null) {
canvas.drawLine(
Offset(x, params.fitPrice(pt)),
Offset(
x + params.candleWidth,
params.fitPrice(params.trailingTrends!.at(j)!),
),
trendLinePaint,
);
}
}
}
}
void _drawTapHighlightAndOverlay(canvas, PainterParams params) {
final pos = params.tapPosition!;
final i = params.getCandleIndexFromOffset(pos.dx);
final candle = params.candles[i];
canvas.save();
canvas.translate(params.xShift, 0.0);
// Draw highlight bar (selection box)
canvas.drawLine(
Offset(i * params.candleWidth, 0.0),
Offset(i * params.candleWidth, params.chartHeight),
Paint()
..strokeWidth = max(params.candleWidth * 0.88, 1.0)
..color = params.style.selectionHighlightColor);
canvas.restore();
// Draw price line
canvas.drawLine(
Offset(0, pos.dy),
Offset(params.chartWidth, pos.dy),
Paint()
..strokeWidth = 1
..color = params.style.selectionHighlightColor);
// Draw price label
final priceTp = TextPainter(
text: TextSpan(
text: getPriceLabel(params.getPriceFromOffset(pos.dy)),
style: params.style.priceLabelStyle,
),
)
..textDirection = TextDirection.ltr
..layout();
priceTp.paint(
canvas,
Offset(
params.chartWidth + 4,
pos.dy - priceTp.height / 2,
));
// Draw info pane
_drawTapInfoOverlay(canvas, params, candle);
}
void _drawTapInfoOverlay(canvas, PainterParams params, CandleData candle) {
final xGap = 8.0;
final yGap = 4.0;
TextPainter makeTP(String text) => TextPainter(
text: TextSpan(
text: text,
style: params.style.overlayTextStyle,
),
)
..textDirection = TextDirection.ltr
..layout();
final info = getOverlayInfo(candle);
if (info.isEmpty) return;
final labels = info.keys.map((text) => makeTP(text)).toList();
final values = info.values.map((text) => makeTP(text)).toList();
final labelsMaxWidth = labels.map((tp) => tp.width).reduce(max);
final valuesMaxWidth = values.map((tp) => tp.width).reduce(max);
final panelWidth = labelsMaxWidth + valuesMaxWidth + xGap * 3;
final panelHeight = max(
labels.map((tp) => tp.height).reduce((a, b) => a + b),
values.map((tp) => tp.height).reduce((a, b) => a + b),
) +
yGap * (values.length + 1);
// Shift the canvas, so the overlay panel can appear near touch position.
canvas.save();
final pos = params.tapPosition!;
final fingerSize = 32.0; // leave some margin around user's finger
double dx, dy;
assert(params.size.width >= panelWidth, "Overlay panel is too wide.");
if (pos.dx <= params.size.width / 2) {
// If user touches the left-half of the screen,
// we show the overlay panel near finger touch position, on the right.
dx = pos.dx + fingerSize;
} else {
// Otherwise we show panel on the left of the finger touch position.
dx = pos.dx - panelWidth - fingerSize;
}
dx = dx.clamp(0, params.size.width - panelWidth);
dy = pos.dy - panelHeight - fingerSize;
if (dy < 0) dy = 0.0;
canvas.translate(dx, dy);
// Draw the background for overlay panel
canvas.drawRRect(
RRect.fromRectAndRadius(
Offset.zero & Size(panelWidth, panelHeight),
Radius.circular(8),
),
Paint()..color = params.style.overlayBackgroundColor);
// Draw texts
var y = 0.0;
for (int i = 0; i < labels.length; i++) {
y += yGap;
final rowHeight = max(labels[i].height, values[i].height);
// Draw labels (left align, vertical center)
final labelY = y + (rowHeight - labels[i].height) / 2; // vertical center
labels[i].paint(canvas, Offset(xGap, labelY));
// Draw values (right align, vertical center)
final leading = valuesMaxWidth - values[i].width; // right align
final valueY = y + (rowHeight - values[i].height) / 2; // vertical center
values[i].paint(
canvas,
Offset(labelsMaxWidth + xGap * 2 + leading, valueY),
);
y += rowHeight;
}
canvas.restore();
}
@override
bool shouldRepaint(ChartPainter oldDelegate) =>
params.shouldRepaint(oldDelegate.params);
}
extension ElementAtOrNull<E> on List<E> {
E? at(int index) {
if (index < 0 || index >= length) return null;
return elementAt(index);
}
}