Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:app_finance/_classes/herald/app_locale.dart';
5 : import 'package:app_finance/_ext/build_context_ext.dart';
6 : import 'package:app_finance/charts/interface/ohlc_data.dart';
7 : import 'package:app_finance/charts/painter/foreground_chart_painter.dart';
8 : import 'package:app_finance/charts/painter/ohlc_chart_painter.dart';
9 : import 'package:app_finance/design/generic/empty_widget.dart';
10 : import 'package:flutter/material.dart';
11 : import 'package:intl/intl.dart';
12 :
13 : class OhlcChart extends StatelessWidget {
14 : final double width;
15 : final double height;
16 : final double indent;
17 : final DateTime xMin;
18 : final String tooltip;
19 : final List<OhlcData> data;
20 :
21 0 : const OhlcChart({
22 : super.key,
23 : required this.data,
24 : required this.width,
25 : required this.height,
26 : required this.xMin,
27 : this.indent = 0.0,
28 : this.tooltip = '',
29 : });
30 :
31 0 : @override
32 : Widget build(BuildContext context) {
33 : double yMin = 0.0;
34 : double yMax = 0.0;
35 0 : for (int i = 0; i < data.length; i++) {
36 0 : if (data[i].low < yMin) yMin = data[i].low;
37 0 : if (data[i].high > yMax) yMax = data[i].high;
38 : }
39 0 : if (data.isEmpty || yMax == 0.0) {
40 : return const EmptyWidget();
41 : }
42 0 : yMax *= 1.25;
43 0 : final now = DateTime.now();
44 0 : final size = Size(width, height);
45 0 : final bgColor = context.colorScheme.onSurface;
46 0 : final xMax = DateTime(now.year, now.month + 1);
47 0 : final bg = ForegroundChartPainter(
48 : size: size,
49 : color: bgColor,
50 : lineColor: bgColor,
51 0 : background: bgColor.withOpacity(0.1),
52 : yMin: yMin,
53 : yMax: yMax,
54 0 : yTpl: NumberFormat.compact(locale: AppLocale.code),
55 : xType: DateTime,
56 0 : xMin: xMin.millisecondsSinceEpoch.toDouble(),
57 0 : xMax: xMax.millisecondsSinceEpoch.toDouble(),
58 : xDivider: 6,
59 0 : xTpl: DateFormat.Md(AppLocale.code),
60 : );
61 0 : return SizedBox(
62 0 : height: size.height,
63 0 : width: size.width,
64 0 : child: CustomPaint(
65 : size: size,
66 0 : painter: OhlcChartPainter(
67 0 : indent: bg.shift,
68 : color: bgColor,
69 : size: size,
70 0 : data: data,
71 : yMax: yMax,
72 0 : xMin: xMin.millisecondsSinceEpoch.toDouble(),
73 0 : xMax: xMax.millisecondsSinceEpoch.toDouble(),
74 : ),
75 : foregroundPainter: bg,
76 : willChange: false,
77 0 : child: Padding(
78 0 : padding: EdgeInsets.only(top: indent / 4),
79 0 : child: Text(tooltip),
80 : ),
81 : ),
82 : );
83 : }
84 : }
|