LCOV - code coverage report
Current view: top level - lib/charts - trade_chart.dart Coverage Total Hit
Test: lcov.info Lines: 100.0 % 51 51
Test Date: 2026-11-14 15:08:11 Functions: - 0 0

            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 'dart:collection';
       5              : 
       6              : import 'package:app_finance/_classes/herald/app_locale.dart';
       7              : import 'package:app_finance/_classes/structure/transaction_log_data.dart';
       8              : import 'package:app_finance/_ext/build_context_ext.dart';
       9              : import 'package:app_finance/charts/interface/chart_data.dart';
      10              : import 'package:app_finance/charts/interface/data_scope.dart';
      11              : import 'package:app_finance/charts/painter/foreground_chart_painter.dart';
      12              : import 'package:app_finance/charts/painter/line_chart_painter.dart';
      13              : import 'package:material_ui/material_ui.dart';
      14              : import 'package:intl/intl.dart';
      15              : 
      16              : class TradeChart extends StatelessWidget {
      17              :   final double width;
      18              :   final double height;
      19              :   final double indent;
      20              :   final String tooltip;
      21              :   final List<TransactionLogData> data;
      22              : 
      23            1 :   const TradeChart({
      24              :     super.key,
      25              :     required this.data,
      26              :     required this.width,
      27              :     required this.height,
      28              :     this.indent = 0.0,
      29              :     this.tooltip = '',
      30              :   });
      31              : 
      32            1 :   DataScope _prepareData() {
      33            1 :     final result = SplayTreeMap<double, Offset>();
      34            3 :     final xMax = DateTime.now().millisecondsSinceEpoch.toDouble();
      35            3 :     double yMin = data.firstOrNull?.changedTo ?? 0.0;
      36              :     double yMax = 0.0;
      37            4 :     for (int i = 0; i < data.length; i++) {
      38            3 :       final time = data[i].timestamp;
      39            6 :       final x = DateTime(time.year, time.month, time.day).millisecondsSinceEpoch.toDouble();
      40            7 :       if (!result.containsKey(x) || result[x]!.dy < data[i].changedTo) {
      41            5 :         result[x] = Offset(x, data[i].changedTo ?? 0.0);
      42              :       }
      43            4 :       if (data[i].changedTo > yMax) {
      44            3 :         yMax = data[i].changedTo;
      45              :       }
      46            4 :       if (data[i].changedTo < yMin) {
      47            3 :         yMin = data[i].changedTo;
      48              :       }
      49              :     }
      50            2 :     final scope = result.values.toList();
      51            1 :     return DataScope(
      52              :       xMax: xMax,
      53            2 :       xMin: scope.firstOrNull?.dx ?? xMax,
      54            1 :       yMin: yMin * 0.8,
      55            1 :       yMax: yMax * 1.2,
      56            1 :       data: [
      57            1 :         ChartData(
      58              :           scope,
      59            6 :           color: scope.isNotEmpty && scope.first.dy > scope.last.dy ? Colors.orange : Colors.blue,
      60              :         ),
      61            1 :         if (scope.isNotEmpty)
      62            2 :           ChartData([
      63            1 :             scope.first,
      64            3 :             Offset(xMax, scope.first.dy),
      65              :           ], color: Colors.grey, strokeWidth: 1)
      66              :       ],
      67              :     );
      68              :   }
      69              : 
      70            1 :   @override
      71              :   Widget build(BuildContext context) {
      72            3 :     final size = Size(width, height);
      73            2 :     final bgColor = context.colorScheme.onSurface;
      74            1 :     final scope = _prepareData();
      75              : 
      76            1 :     final bg = ForegroundChartPainter(
      77              :       size: size,
      78              :       color: bgColor,
      79              :       lineColor: bgColor,
      80            1 :       background: Colors.white.withValues(alpha: 0.0),
      81            1 :       yMin: scope.yMin,
      82            1 :       yMax: scope.yMax,
      83              :       xType: DateTime,
      84            1 :       xMin: scope.xMin,
      85            1 :       xMax: scope.xMax,
      86              :       xDivider: 16,
      87            1 :       xTpl: DateFormat.Md(AppLocale.code),
      88              :     );
      89              : 
      90            1 :     return SizedBox(
      91            1 :       height: size.height,
      92            1 :       width: size.width,
      93            1 :       child: CustomPaint(
      94              :         size: size,
      95            1 :         painter: LineChartPainter(
      96            1 :           indent: bg.shift,
      97              :           size: size,
      98            1 :           data: scope.data,
      99            1 :           yMin: scope.yMin,
     100            1 :           yMax: scope.yMax,
     101            1 :           xMin: bg.xMin,
     102            1 :           xMax: bg.xMax,
     103              :         ),
     104              :         foregroundPainter: bg,
     105              :         willChange: false,
     106            1 :         child: Padding(
     107            3 :           padding: EdgeInsets.only(top: indent / 4),
     108            2 :           child: Text(tooltip),
     109              :         ),
     110              :       ),
     111              :     );
     112              :   }
     113              : }
        

Generated by: LCOV version 2.0-1