-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathpainter_params.dart
More file actions
138 lines (113 loc) · 4.08 KB
/
Copy pathpainter_params.dart
File metadata and controls
138 lines (113 loc) · 4.08 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
import 'dart:ui';
import 'package:flutter/widgets.dart';
import 'chart_style.dart';
import 'candle_data.dart';
class PainterParams {
final List<CandleData> candles;
final ChartStyle style;
final Size size;
final double candleWidth;
final double startOffset;
final double maxPrice;
final double minPrice;
final double maxVol;
final double minVol;
final double xShift;
final Offset? tapPosition;
final List<double?>? leadingTrends;
final List<double?>? trailingTrends;
PainterParams({
required this.candles,
required this.style,
required this.size,
required this.candleWidth,
required this.startOffset,
required this.maxPrice,
required this.minPrice,
required this.maxVol,
required this.minVol,
required this.xShift,
required this.tapPosition,
required this.leadingTrends,
required this.trailingTrends,
});
double get chartWidth => // width without price labels
size.width - style.priceLabelWidth;
double get chartHeight => // height without time labels
size.height - style.timeLabelHeight;
double get volumeHeight => chartHeight * style.volumeHeightFactor;
double get priceHeight => chartHeight - volumeHeight;
int getCandleIndexFromOffset(double x) {
final adjustedPos = x - xShift + candleWidth / 2;
final i = adjustedPos ~/ candleWidth;
return i;
}
double getPriceFromOffset(double y) =>
(priceHeight - y) / priceHeight * (maxPrice - minPrice) + minPrice;
double fitPrice(double y) =>
priceHeight * (maxPrice - y) / (maxPrice - minPrice);
double fitVolume(double y) {
final gap = 12; // the gap between price bars and volume bars
final baseAmount = 2; // display at least "something" for the lowest volume
if (maxVol == minVol) {
// Apparently max and min values (in the current visible range, at least)
// are the same. It's likely they passed in a bunch of zeroes, because
// they don't have real volume data or don't want to draw volumes.
assert(() {
if (style.volumeHeightFactor != 0) {
print('If you do not want to show volumes, '
'make sure to set `volumeHeightFactor` (ChartStyle) to zero.');
}
return true;
}());
// Since they are equal, we just draw all volume bars as half height.
return priceHeight + volumeHeight / 2;
}
final volGridSize = (volumeHeight - baseAmount - gap) / (maxVol - minVol);
final vol = (y - minVol) * volGridSize;
return volumeHeight - vol + priceHeight - baseAmount;
}
static PainterParams lerp(PainterParams a, PainterParams b, double t) {
double lerpField(double getField(PainterParams p)) =>
lerpDouble(getField(a), getField(b), t)!;
return PainterParams(
candles: b.candles,
style: b.style,
size: b.size,
candleWidth: b.candleWidth,
startOffset: b.startOffset,
maxPrice: lerpField((p) => p.maxPrice),
minPrice: lerpField((p) => p.minPrice),
maxVol: lerpField((p) => p.maxVol),
minVol: lerpField((p) => p.minVol),
xShift: b.xShift,
tapPosition: b.tapPosition,
leadingTrends: b.leadingTrends,
trailingTrends: b.trailingTrends,
);
}
bool shouldRepaint(PainterParams other) {
if (candles.length != other.candles.length) return true;
if (size != other.size ||
candleWidth != other.candleWidth ||
startOffset != other.startOffset ||
xShift != other.xShift) return true;
if (maxPrice != other.maxPrice ||
minPrice != other.minPrice ||
maxVol != other.maxVol ||
minVol != other.minVol) return true;
if (tapPosition != other.tapPosition) return true;
if (leadingTrends != other.leadingTrends ||
trailingTrends != other.trailingTrends) return true;
if (style != other.style) return true;
return false;
}
}
class PainterParamsTween extends Tween<PainterParams> {
PainterParamsTween({
PainterParams? begin,
required PainterParams end,
}) : super(begin: begin, end: end);
@override
PainterParams lerp(double t) => PainterParams.lerp(begin ?? end!, end!, t);
}