-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathdepth_chart.dart
422 lines (382 loc) · 12.7 KB
/
depth_chart.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:k_chart/flutter_k_chart.dart';
class DepthChart extends StatefulWidget {
final List<DepthEntity> bids, asks;
final int fixedLength;
final Color? buyPathColor;
final Color? sellPathColor;
final ChartColors chartColors;
DepthChart(
this.bids,
this.asks,
this.chartColors, {
this.fixedLength = 2,
this.buyPathColor,
this.sellPathColor,
});
@override
_DepthChartState createState() => _DepthChartState();
}
class _DepthChartState extends State<DepthChart> {
Offset? pressOffset;
bool isLongPress = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onLongPressStart: (details) {
pressOffset = details.localPosition;
isLongPress = true;
setState(() {});
},
onLongPressMoveUpdate: (details) {
pressOffset = details.localPosition;
isLongPress = true;
setState(() {});
},
onTap: () {
if (isLongPress) {
isLongPress = false;
setState(() {});
}
},
child: CustomPaint(
size: Size(double.infinity, double.infinity),
painter: DepthChartPainter(
widget.bids,
widget.asks,
pressOffset,
isLongPress,
widget.fixedLength,
widget.buyPathColor,
widget.sellPathColor,
widget.chartColors),
),
);
}
}
class DepthChartPainter extends CustomPainter {
//买入//卖出
List<DepthEntity>? mBuyData, mSellData;
Offset? pressOffset;
bool isLongPress;
int? fixedLength;
Color? mBuyPathColor, mSellPathColor;
ChartColors chartColors;
double mPaddingBottom = 18.0;
double mWidth = 0.0, mDrawHeight = 0.0, mDrawWidth = 0.0;
double? mBuyPointWidth, mSellPointWidth;
//最大的委托量
double? mMaxVolume, mMultiple;
//右侧绘制个数
int mLineCount = 4;
Path? mBuyPath, mSellPath;
//买卖出区域边线绘制画笔 //买卖出取悦绘制画笔
Paint? mBuyLinePaint,
mSellLinePaint,
mBuyPathPaint,
mSellPathPaint,
selectPaint,
selectBorderPaint;
DepthChartPainter(
this.mBuyData,
this.mSellData,
this.pressOffset,
this.isLongPress,
this.fixedLength,
this.mBuyPathColor,
this.mSellPathColor,
this.chartColors) {
mBuyLinePaint ??= Paint()
..isAntiAlias = true
..color = this.chartColors.depthBuyColor
..style = PaintingStyle.stroke
..strokeWidth = 1.0;
mSellLinePaint ??= Paint()
..isAntiAlias = true
..color = this.chartColors.depthSellColor
..style = PaintingStyle.stroke
..strokeWidth = 1.0;
mBuyPathPaint ??= Paint()
..isAntiAlias = true
..color = (mBuyPathColor == null
? this.chartColors.depthBuyColor.withOpacity(0.2)
: mBuyPathColor)!;
mSellPathPaint ??= Paint()
..isAntiAlias = true
..color = (mSellPathColor == null
? this.chartColors.depthSellColor.withOpacity(0.2)
: mSellPathColor)!;
mBuyPath ??= Path();
mSellPath ??= Path();
init();
}
void init() {
if (mBuyData == null ||
mBuyData!.isEmpty ||
mSellData == null ||
mSellData!.isEmpty) return;
mMaxVolume = mBuyData![0].vol;
mMaxVolume = max(mMaxVolume!, mSellData!.last.vol);
mMaxVolume = mMaxVolume! * 1.05;
mMultiple = mMaxVolume! / mLineCount;
fixedLength ??= 2;
selectPaint = Paint()
..isAntiAlias = true
..color = chartColors.selectFillColor;
selectBorderPaint = Paint()
..isAntiAlias = true
..color = chartColors.selectBorderColor
..style = PaintingStyle.stroke
..strokeWidth = 0.5;
}
@override
void paint(Canvas canvas, Size size) {
if (mBuyData == null ||
mSellData == null ||
mBuyData!.isEmpty ||
mSellData!.isEmpty) return;
mWidth = size.width;
mDrawWidth = mWidth / 2;
mDrawHeight = size.height - mPaddingBottom;
// canvas.drawColor(Colors.green, BlendMode.srcATop);
canvas.save();
//绘制买入区域
drawBuy(canvas);
//绘制卖出区域
drawSell(canvas);
//绘制界面相关文案
drawText(canvas);
canvas.restore();
}
void drawBuy(Canvas canvas) {
mBuyPointWidth =
(mDrawWidth / (mBuyData!.length - 1 == 0 ? 1 : mBuyData!.length - 1));
mBuyPath!.reset();
double x;
double y;
for (int i = 0; i < mBuyData!.length; i++) {
if (i == 0) {
mBuyPath!.moveTo(0, getY(mBuyData![0].vol));
}
x = mBuyPointWidth! * i;
y = getY(mBuyData![i].vol);
if (i >= 1) {
canvas.drawLine(
Offset(mBuyPointWidth! * (i - 1), getY(mBuyData![i - 1].vol)),
Offset(x, y),
mBuyLinePaint!);
}
if (i != mBuyData!.length - 1) {
mBuyPath!.quadraticBezierTo(
x, y, mBuyPointWidth! * (i + 1), getY(mBuyData![i + 1].vol));
} else {
if (i == 0) {
mBuyPath!.lineTo(mDrawWidth, y);
mBuyPath!.lineTo(mDrawWidth, mDrawHeight);
mBuyPath!.lineTo(0, mDrawHeight);
} else {
mBuyPath!.quadraticBezierTo(x, y, x, mDrawHeight);
mBuyPath!.quadraticBezierTo(x, mDrawHeight, 0, mDrawHeight);
}
mBuyPath!.close();
}
}
canvas.drawPath(mBuyPath!, mBuyPathPaint!);
}
void drawSell(Canvas canvas) {
mSellPointWidth =
(mDrawWidth / (mSellData!.length - 1 == 0 ? 1 : mSellData!.length - 1));
mSellPath!.reset();
double x;
double y;
for (int i = 0; i < mSellData!.length; i++) {
if (i == 0) {
mSellPath!.moveTo(mDrawWidth, getY(mSellData![0].vol));
}
x = (mSellPointWidth! * i) + mDrawWidth;
y = getY(mSellData![i].vol);
if (i >= 1) {
canvas.drawLine(
Offset((mSellPointWidth! * (i - 1)) + mDrawWidth,
getY(mSellData![i - 1].vol)),
Offset(x, y),
mSellLinePaint!);
}
if (i != mSellData!.length - 1) {
mSellPath!.quadraticBezierTo(
x,
y,
(mSellPointWidth! * (i + 1)) + mDrawWidth,
getY(mSellData![i + 1].vol));
} else {
if (i == 0) {
mSellPath!.lineTo(mWidth, y);
mSellPath!.lineTo(mWidth, mDrawHeight);
mSellPath!.lineTo(mDrawWidth, mDrawHeight);
} else {
mSellPath!.quadraticBezierTo(mWidth, y, x, mDrawHeight);
mSellPath!.quadraticBezierTo(x, mDrawHeight, mDrawWidth, mDrawHeight);
}
mSellPath!.close();
}
}
canvas.drawPath(mSellPath!, mSellPathPaint!);
}
// int? mLastPosition;
void drawText(Canvas canvas) {
double value;
String str;
for (int j = 0; j < mLineCount; j++) {
value = mMaxVolume! - mMultiple! * j;
str = value.toStringAsFixed(fixedLength!);
var tp = getTextPainter(str);
tp.layout();
tp.paint(
canvas,
Offset(
mWidth - tp.width, mDrawHeight / mLineCount * j + tp.height / 2));
}
var startText = mBuyData!.first.price.toStringAsFixed(fixedLength!);
TextPainter startTP = getTextPainter(startText);
startTP.layout();
startTP.paint(canvas, Offset(0, getBottomTextY(startTP.height)));
double centerPrice = (mBuyData!.last.price + mSellData!.first.price) / 2;
var center = centerPrice.toStringAsFixed(fixedLength!);
TextPainter centerTP = getTextPainter(center);
centerTP.layout();
centerTP.paint(
canvas,
Offset(
mDrawWidth - centerTP.width / 2, getBottomTextY(centerTP.height)));
var endText = mSellData!.last.price.toStringAsFixed(fixedLength!);
TextPainter endTP = getTextPainter(endText);
endTP.layout();
endTP.paint(
canvas, Offset(mWidth - endTP.width, getBottomTextY(endTP.height)));
var leftHalfText = ((mBuyData!.first.price + centerPrice) / 2)
.toStringAsFixed(fixedLength!);
TextPainter leftHalfTP = getTextPainter(leftHalfText);
leftHalfTP.layout();
leftHalfTP.paint(
canvas,
Offset((mDrawWidth - leftHalfTP.width) / 2,
getBottomTextY(leftHalfTP.height)));
var rightHalfText = ((mSellData!.last.price + centerPrice) / 2)
.toStringAsFixed(fixedLength!);
TextPainter rightHalfTP = getTextPainter(rightHalfText);
rightHalfTP.layout();
rightHalfTP.paint(
canvas,
Offset((mDrawWidth + mWidth - rightHalfTP.width) / 2,
getBottomTextY(rightHalfTP.height)));
if (isLongPress == true) {
if (pressOffset!.dx <= mDrawWidth) {
int index = _indexOfTranslateX(
pressOffset!.dx, 0, mBuyData!.length - 1, getBuyX);
drawSelectView(canvas, index, true);
} else {
int index = _indexOfTranslateX(
pressOffset!.dx, 0, mSellData!.length - 1, getSellX);
drawSelectView(canvas, index, false);
}
}
}
void drawSelectView(Canvas canvas, int index, bool isLeft) {
DepthEntity entity = isLeft ? mBuyData![index] : mSellData![index];
double dx = isLeft ? getBuyX(index) : getSellX(index);
double radius = 8.0;
if (dx < mDrawWidth) {
canvas.drawCircle(Offset(dx, getY(entity.vol)), radius / 3,
mBuyLinePaint!..style = PaintingStyle.fill);
canvas.drawCircle(Offset(dx, getY(entity.vol)), radius,
mBuyLinePaint!..style = PaintingStyle.stroke);
} else {
canvas.drawCircle(Offset(dx, getY(entity.vol)), radius / 3,
mSellLinePaint!..style = PaintingStyle.fill);
canvas.drawCircle(Offset(dx, getY(entity.vol)), radius,
mSellLinePaint!..style = PaintingStyle.stroke);
}
//画底部
TextPainter priceTP =
getTextPainter(entity.price.toStringAsFixed(fixedLength!));
priceTP.layout();
double left;
if (dx <= priceTP.width / 2) {
left = 0;
} else if (dx >= mWidth - priceTP.width / 2) {
left = mWidth - priceTP.width;
} else {
left = dx - priceTP.width / 2;
}
Rect bottomRect = Rect.fromLTRB(left - 3, mDrawHeight + 3,
left + priceTP.width + 3, mDrawHeight + mPaddingBottom);
canvas.drawRect(bottomRect, selectPaint!);
canvas.drawRect(bottomRect, selectBorderPaint!);
priceTP.paint(
canvas,
Offset(bottomRect.left + (bottomRect.width - priceTP.width) / 2,
bottomRect.top + (bottomRect.height - priceTP.height) / 2));
//画左边
TextPainter amountTP =
getTextPainter(entity.vol.toStringAsFixed(fixedLength!));
amountTP.layout();
double y = getY(entity.vol);
double rightRectTop;
if (y <= amountTP.height / 2) {
rightRectTop = 0;
} else if (y >= mDrawHeight - amountTP.height / 2) {
rightRectTop = mDrawHeight - amountTP.height;
} else {
rightRectTop = y - amountTP.height / 2;
}
Rect rightRect = Rect.fromLTRB(mWidth - amountTP.width - 6,
rightRectTop - 3, mWidth, rightRectTop + amountTP.height + 3);
canvas.drawRect(rightRect, selectPaint!);
canvas.drawRect(rightRect, selectBorderPaint!);
amountTP.paint(
canvas,
Offset(rightRect.left + (rightRect.width - amountTP.width) / 2,
rightRect.top + (rightRect.height - amountTP.height) / 2));
}
///二分查找当前值的index
int _indexOfTranslateX(double translateX, int start, int end, Function getX) {
if (end == start || end == -1) {
return start;
}
if (end - start == 1) {
double startValue = getX(start);
double endValue = getX(end);
return (translateX - startValue).abs() < (translateX - endValue).abs()
? start
: end;
}
int mid = start + (end - start) ~/ 2;
double midValue = getX(mid);
if (translateX < midValue) {
return _indexOfTranslateX(translateX, start, mid, getX);
} else if (translateX > midValue) {
return _indexOfTranslateX(translateX, mid, end, getX);
} else {
return mid;
}
}
double getBuyX(int position) => position * mBuyPointWidth!;
double getSellX(int position) => position * mSellPointWidth! + mDrawWidth;
getTextPainter(String text, [Color color = Colors.white]) => TextPainter(
text:
TextSpan(text: "$text", style: TextStyle(color: color, fontSize: 10)),
textDirection: TextDirection.ltr);
double getBottomTextY(double textHeight) =>
(mPaddingBottom - textHeight) / 2 + mDrawHeight;
double getY(double volume) =>
mDrawHeight - (mDrawHeight) * volume / mMaxVolume!;
@override
bool shouldRepaint(DepthChartPainter oldDelegate) {
// return oldDelegate.mBuyData != mBuyData ||
// oldDelegate.mSellData != mSellData ||
// oldDelegate.isLongPress != isLongPress ||
// oldDelegate.pressOffset != pressOffset;
return true;
}
}