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/charts/interface/chart_data.dart';
5 : import 'package:app_finance/charts/painter/abstract_painter.dart';
6 : import 'package:flutter/material.dart';
7 :
8 : class BarChartPainter extends AbstractPainter {
9 : final List<ChartData> data;
10 : final Color lineColor;
11 :
12 0 : BarChartPainter({
13 : required super.indent,
14 : required this.data,
15 : required this.lineColor,
16 : super.size,
17 : super.xMax = 1.0,
18 : super.xMin = 0.0,
19 : super.yMax = 1.0,
20 : });
21 :
22 0 : @override
23 : void paint(Canvas canvas, Size size) {
24 0 : if (data.isEmpty) {
25 : return;
26 : }
27 0 : size = this.size ?? size;
28 0 : for (int i = 0; i < data.length; i++) {
29 0 : for (int j = 0; j < data[i].data.length; j++) {
30 0 : final point = data[i].data[j];
31 0 : _draw(canvas, size, point, data[i].color);
32 0 : final txtPoint = getValue(Offset(xMin, _getY(point, 0.1)), size);
33 0 : _paintText(canvas, txtPoint, data[i].helper.title);
34 0 : _paintIcon(canvas, Offset(0.0, txtPoint.dy), data[i].helper.icon);
35 : }
36 : }
37 : }
38 :
39 0 : _getY(Offset point, double shift) {
40 0 : return yMax - point.dx - shift;
41 : }
42 :
43 0 : void _draw(Canvas canvas, Size size, Offset point, Color color) {
44 0 : final paint = Paint()
45 0 : ..color = color
46 0 : ..style = PaintingStyle.fill
47 0 : ..strokeWidth = 1;
48 0 : final rect = Rect.fromPoints(
49 0 : getValue(Offset(xMin, _getY(point, 0.6)), size),
50 0 : getValue(Offset(point.dy, _getY(point, 0.9)), size),
51 : );
52 0 : canvas.drawRect(rect, paint);
53 : }
54 :
55 0 : void _paintText(Canvas canvas, Offset point, String text) {
56 0 : final textPainter = TextPainter(
57 0 : text: TextSpan(
58 : text: text,
59 0 : style: TextStyle(
60 0 : color: lineColor,
61 : fontSize: 11,
62 : fontFamily: 'Abel-Regular',
63 : ),
64 : ),
65 : textDirection: TextDirection.ltr,
66 : );
67 0 : textPainter.layout();
68 0 : textPainter.paint(canvas, point);
69 : }
70 :
71 0 : void _paintIcon(Canvas canvas, Offset point, IconData? icon) {
72 : if (icon == null) {
73 : return;
74 : }
75 0 : final textPainter = TextPainter(
76 0 : text: TextSpan(
77 0 : text: String.fromCharCode(icon.codePoint),
78 0 : style: TextStyle(
79 0 : color: lineColor,
80 : fontSize: 24,
81 0 : fontFamily: Icons.question_mark.fontFamily,
82 : ),
83 : ),
84 : textDirection: TextDirection.ltr,
85 : );
86 0 : textPainter.layout();
87 0 : textPainter.paint(canvas, point);
88 : }
89 : }
|