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/painter/gauge_painter.dart';
5 : import 'package:flutter/material.dart';
6 :
7 : class GaugeLinearPainter extends GaugePainter {
8 0 : GaugeLinearPainter({
9 : required super.value,
10 : required super.max,
11 : required super.min,
12 : super.color = Colors.grey,
13 0 : }) : super(fontSize: 9);
14 :
15 0 : @override
16 : void paint(Canvas canvas, Size size) {
17 : const indent = 2.0;
18 0 : final height = size.height / 2;
19 0 : final xStart = 0.10 * size.width;
20 0 : _drawRect(canvas, size, const Offset(0, indent), Offset(xStart, height), Colors.red.shade900);
21 0 : final xMiddle = 0.35 * size.width;
22 0 : _drawRect(canvas, size, Offset(xStart + indent, indent), Offset(xMiddle, height), Colors.orange);
23 0 : _drawRect(canvas, size, Offset(xMiddle + indent, indent), Offset(size.width, height), Colors.green.shade500);
24 :
25 0 : final paint = Paint()
26 0 : ..color = color
27 0 : ..strokeWidth = 2
28 0 : ..style = PaintingStyle.fill;
29 0 : final spread = size.width / threshold - 1;
30 0 : final step = (max - min) / spread;
31 0 : fnZero(double _, TextPainter __) => 0.0;
32 0 : for (double i = 0; i < spread; i += 1) {
33 0 : final pos = Offset(i * threshold + 3, size.height - 3);
34 0 : canvas.drawCircle(pos, 2.0, paint);
35 0 : paintText(
36 0 : canvas, size, fnZero, Offset(pos.dx + 6, size.height / 2 + 2), '${(min + step * i).toStringAsFixed(2)}%');
37 : }
38 :
39 0 : final pos = value * size.width;
40 0 : final strokePath = Path()
41 0 : ..moveTo(pos - 6, 0)
42 0 : ..lineTo(pos, size.height / 2 + 2)
43 0 : ..lineTo(pos + 6, 0);
44 0 : canvas.drawPath(strokePath, paint..invertColors = true);
45 0 : final path = Path()
46 0 : ..moveTo(pos - 3, 0)
47 0 : ..lineTo(pos, size.height / 3)
48 0 : ..lineTo(pos + 3, 0);
49 0 : canvas.drawPath(path, paint..invertColors = false);
50 : }
51 :
52 0 : void _drawRect(Canvas canvas, Size size, Offset start, Offset end, Color color) {
53 0 : final paint = Paint()
54 0 : ..color = color
55 0 : ..style = PaintingStyle.fill;
56 0 : canvas.drawRect(Rect.fromPoints(start, end), paint);
57 : }
58 : }
|