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_start_of_month.dart';
5 : import 'package:app_finance/_ext/build_context_ext.dart';
6 : import 'package:app_finance/_ext/date_time_ext.dart';
7 : import 'package:app_finance/charts/interface/chart_data.dart';
8 : import 'package:app_finance/charts/painter/forecast_chart_painter.dart';
9 : import 'package:app_finance/charts/painter/foreground_chart_painter.dart';
10 : import 'package:app_finance/design/generic/empty_widget.dart';
11 : import 'package:material_ui/material_ui.dart';
12 :
13 : class ForecastChart extends StatelessWidget {
14 : final double width;
15 : final double height;
16 : final double indent;
17 : final String tooltip;
18 : final List<ChartData> data;
19 : final double yMax;
20 :
21 0 : const ForecastChart({
22 : super.key,
23 : required this.data,
24 : required this.yMax,
25 : required this.width,
26 : required this.height,
27 : this.indent = 0.0,
28 : this.tooltip = '',
29 : });
30 :
31 0 : @override
32 : Widget build(BuildContext context) {
33 0 : if (data.isEmpty || data.first.data.isEmpty) {
34 : return const EmptyWidget();
35 : }
36 0 : final now = DateTime.now();
37 0 : final size = Size(width, height);
38 0 : final bgColor = context.colorScheme.onSurface;
39 0 : final startingDay = AppStartOfMonth.get();
40 0 : final xMin = now.getStartingDay(startingDay).millisecondsSinceEpoch.toDouble();
41 : final xMax =
42 0 : DateTime(now.year, now.month + 1, now.day).getStartingDay(startingDay).millisecondsSinceEpoch.toDouble();
43 0 : final bg = ForegroundChartPainter(
44 : size: size,
45 : color: bgColor,
46 : lineColor: bgColor,
47 0 : background: bgColor.withValues(alpha: 0.1),
48 : yMin: 0.0,
49 : yMax: 140,
50 0 : yArea: [80, 120],
51 : xType: DateTime,
52 : yType: Percentage,
53 : xMin: xMin,
54 : xMax: xMax,
55 : );
56 0 : return SizedBox(
57 0 : height: size.height,
58 0 : width: size.width,
59 0 : child: CustomPaint(
60 : size: size,
61 0 : painter: ForecastChartPainter(
62 0 : indent: bg.shift,
63 : size: size,
64 0 : data: data,
65 0 : yMax: yMax * bg.yMax / 100,
66 : xMin: xMin,
67 : xMax: xMax,
68 : ),
69 : foregroundPainter: bg,
70 : willChange: false,
71 0 : child: Padding(
72 0 : padding: EdgeInsets.only(top: indent / 4),
73 0 : child: Text(tooltip),
74 : ),
75 : ),
76 : );
77 : }
78 : }
|