-
Notifications
You must be signed in to change notification settings - Fork 90
/
flutter_candlesticks.dart
304 lines (261 loc) · 9.99 KB
/
flutter_candlesticks.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
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class OHLCVGraph extends StatelessWidget {
OHLCVGraph({
Key key,
@required this.data,
this.lineWidth = 1.0,
this.fallbackHeight = 100.0,
this.fallbackWidth = 300.0,
this.gridLineColor = Colors.grey,
this.gridLineAmount = 5,
this.gridLineWidth = 0.5,
this.gridLineLabelColor = Colors.grey,
this.labelPrefix = "\$",
@required this.enableGridLines,
@required this.volumeProp,
this.increaseColor = Colors.green,
this.decreaseColor = Colors.red,
}) : assert(data != null),
super(key: key);
/// OHLCV data to graph /// List of Maps containing open, high, low, close and volumeto
/// Example: [["open" : 40.0, "high" : 75.0, "low" : 25.0, "close" : 50.0, "volumeto" : 5000.0}, {...}]
final List data;
/// All lines in chart are drawn with this width
final double lineWidth;
/// Enable or disable grid lines
final bool enableGridLines;
/// Color of grid lines and label text
final Color gridLineColor;
final Color gridLineLabelColor;
/// Number of grid lines
final int gridLineAmount;
/// Width of grid lines
final double gridLineWidth;
/// Proportion of paint to be given to volume bar graph
final double volumeProp;
/// If graph is given unbounded space,
/// it will default to given fallback height and width
final double fallbackHeight;
final double fallbackWidth;
/// Symbol prefix for grid line labels
final String labelPrefix;
/// Increase color
final Color increaseColor;
/// Decrease color
final Color decreaseColor;
@override
Widget build(BuildContext context) {
return new LimitedBox(
maxHeight: fallbackHeight,
maxWidth: fallbackWidth,
child: new CustomPaint(
size: Size.infinite,
painter: new _OHLCVPainter(data,
lineWidth: lineWidth,
gridLineColor: gridLineColor,
gridLineAmount: gridLineAmount,
gridLineWidth: gridLineWidth,
gridLineLabelColor: gridLineLabelColor,
enableGridLines: enableGridLines,
volumeProp: volumeProp,
labelPrefix: labelPrefix,
increaseColor: increaseColor,
decreaseColor: decreaseColor),
),
);
}
}
class _OHLCVPainter extends CustomPainter {
_OHLCVPainter(this.data,
{@required this.lineWidth,
@required this.enableGridLines,
@required this.gridLineColor,
@required this.gridLineAmount,
@required this.gridLineWidth,
@required this.gridLineLabelColor,
@required this.volumeProp,
@required this.labelPrefix,
@required this.increaseColor,
@required this.decreaseColor});
final List data;
final double lineWidth;
final bool enableGridLines;
final Color gridLineColor;
final int gridLineAmount;
final double gridLineWidth;
final Color gridLineLabelColor;
final String labelPrefix;
final double volumeProp;
final Color increaseColor;
final Color decreaseColor;
double _min;
double _max;
double _maxVolume;
List<TextPainter> gridLineTextPainters = [];
TextPainter maxVolumePainter;
numCommaParse(number) {
return number.round().toString().replaceAllMapped(
new RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => "${m[1]},");
}
update() {
_min = double.infinity;
_max = -double.infinity;
_maxVolume = -double.infinity;
for (var i in data) {
if (i["high"] > _max) {
_max = i["high"].toDouble();
}
if (i["low"] < _min) {
_min = i["low"].toDouble();
}
if (i["volumeto"] > _maxVolume) {
_maxVolume = i["volumeto"].toDouble();
}
}
if (enableGridLines) {
double gridLineValue;
for (int i = 0; i < gridLineAmount; i++) {
// Label grid lines
gridLineValue = _max - (((_max - _min) / (gridLineAmount - 1)) * i);
String gridLineText;
if (gridLineValue < 1) {
gridLineText = gridLineValue.toStringAsPrecision(4);
} else if (gridLineValue < 999) {
gridLineText = gridLineValue.toStringAsFixed(2);
} else {
gridLineText = gridLineValue.round().toString().replaceAllMapped(
new RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'),
(Match m) => "${m[1]},");
}
gridLineTextPainters.add(new TextPainter(
text: new TextSpan(
text: labelPrefix + gridLineText,
style: new TextStyle(
color: gridLineLabelColor,
fontSize: 10.0,
fontWeight: FontWeight.bold)),
textDirection: TextDirection.ltr));
gridLineTextPainters[i].layout();
}
// Label volume line
maxVolumePainter = new TextPainter(
text: new TextSpan(
text: labelPrefix + numCommaParse(_maxVolume),
style: new TextStyle(
color: gridLineLabelColor,
fontSize: 10.0,
fontWeight: FontWeight.bold)),
textDirection: TextDirection.ltr);
maxVolumePainter.layout();
}
}
@override
void paint(Canvas canvas, Size size) {
if (_min == null || _max == null || _maxVolume == null) {
update();
}
final double volumeHeight = size.height * volumeProp;
final double volumeNormalizer = volumeHeight / _maxVolume;
double width = size.width;
final double height = size.height * (1 - volumeProp);
if (enableGridLines) {
InlineSpan inlineSpan = gridLineTextPainters[0].text;
width = size.width - inlineSpan.toPlainText().length * 6;
Paint gridPaint = new Paint()
..color = gridLineColor
..strokeWidth = gridLineWidth;
double gridLineDist = height / (gridLineAmount - 1);
double gridLineY;
// Draw grid lines
for (int i = 0; i < gridLineAmount; i++) {
gridLineY = (gridLineDist * i).round().toDouble();
canvas.drawLine(new Offset(0.0, gridLineY),
new Offset(width, gridLineY), gridPaint);
// Label grid lines
gridLineTextPainters[i]
.paint(canvas, new Offset(width + 2.0, gridLineY - 6.0));
}
// Label volume line
maxVolumePainter.paint(canvas, new Offset(0.0, gridLineY + 2.0));
}
final double heightNormalizer = height / (_max - _min);
final double rectWidth = width / data.length;
double rectLeft;
double rectTop;
double rectRight;
double rectBottom;
Paint rectPaint;
// Loop through all data
for (int i = 0; i < data.length; i++) {
rectLeft = (i * rectWidth) + lineWidth / 2;
rectRight = ((i + 1) * rectWidth) - lineWidth / 2;
double volumeBarTop = (height + volumeHeight) -
(data[i]["volumeto"] * volumeNormalizer - lineWidth / 2);
double volumeBarBottom = height + volumeHeight + lineWidth / 2;
if (data[i]["open"] > data[i]["close"]) {
// Draw candlestick if decrease
rectTop = height - (data[i]["open"] - _min) * heightNormalizer;
rectBottom = height - (data[i]["close"] - _min) * heightNormalizer;
rectPaint = new Paint()
..color = decreaseColor
..strokeWidth = lineWidth;
Rect ocRect =
new Rect.fromLTRB(rectLeft, rectTop, rectRight, rectBottom);
canvas.drawRect(ocRect, rectPaint);
// Draw volume bars
Rect volumeRect = new Rect.fromLTRB(
rectLeft, volumeBarTop, rectRight, volumeBarBottom);
canvas.drawRect(volumeRect, rectPaint);
} else {
// Draw candlestick if increase
rectTop = (height - (data[i]["close"] - _min) * heightNormalizer) +
lineWidth / 2;
rectBottom = (height - (data[i]["open"] - _min) * heightNormalizer) -
lineWidth / 2;
rectPaint = new Paint()
..color = increaseColor
..strokeWidth = lineWidth;
canvas.drawLine(new Offset(rectLeft, rectBottom - lineWidth / 2),
new Offset(rectRight, rectBottom - lineWidth / 2), rectPaint);
canvas.drawLine(new Offset(rectLeft, rectTop + lineWidth / 2),
new Offset(rectRight, rectTop + lineWidth / 2), rectPaint);
canvas.drawLine(new Offset(rectLeft + lineWidth / 2, rectBottom),
new Offset(rectLeft + lineWidth / 2, rectTop), rectPaint);
canvas.drawLine(new Offset(rectRight - lineWidth / 2, rectBottom),
new Offset(rectRight - lineWidth / 2, rectTop), rectPaint);
// Draw volume bars
canvas.drawLine(new Offset(rectLeft, volumeBarBottom - lineWidth / 2),
new Offset(rectRight, volumeBarBottom - lineWidth / 2), rectPaint);
canvas.drawLine(new Offset(rectLeft, volumeBarTop + lineWidth / 2),
new Offset(rectRight, volumeBarTop + lineWidth / 2), rectPaint);
canvas.drawLine(new Offset(rectLeft + lineWidth / 2, volumeBarBottom),
new Offset(rectLeft + lineWidth / 2, volumeBarTop), rectPaint);
canvas.drawLine(new Offset(rectRight - lineWidth / 2, volumeBarBottom),
new Offset(rectRight - lineWidth / 2, volumeBarTop), rectPaint);
}
// Draw low/high candlestick wicks
double low = height - (data[i]["low"] - _min) * heightNormalizer;
double high = height - (data[i]["high"] - _min) * heightNormalizer;
canvas.drawLine(
new Offset(rectLeft + rectWidth / 2 - lineWidth / 2, rectBottom),
new Offset(rectLeft + rectWidth / 2 - lineWidth / 2, low),
rectPaint);
canvas.drawLine(
new Offset(rectLeft + rectWidth / 2 - lineWidth / 2, rectTop),
new Offset(rectLeft + rectWidth / 2 - lineWidth / 2, high),
rectPaint);
}
}
@override
bool shouldRepaint(_OHLCVPainter old) {
return data != old.data ||
lineWidth != old.lineWidth ||
enableGridLines != old.enableGridLines ||
gridLineColor != old.gridLineColor ||
gridLineAmount != old.gridLineAmount ||
gridLineWidth != old.gridLineWidth ||
volumeProp != old.volumeProp ||
gridLineLabelColor != old.gridLineLabelColor;
}
}