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:material_ui/material_ui.dart';
7 :
8 : class LineChartPainter extends AbstractPainter {
9 : final List<ChartData> data;
10 :
11 1 : LineChartPainter({
12 : required super.indent,
13 : required this.data,
14 : super.size,
15 : super.xMin = 0.0,
16 : super.xMax = 1.0,
17 : super.yMin = 1.0,
18 : super.yMax = 1.0,
19 : });
20 :
21 1 : @override
22 : void paint(Canvas canvas, Size size) {
23 2 : if (data.isEmpty) {
24 : return;
25 : }
26 1 : size = this.size ?? size;
27 2 : for (final scope in data) {
28 4 : _paint(canvas, scope.data, size, scope.color, scope.strokeWidth);
29 : }
30 : }
31 :
32 1 : void _paint(Canvas canvas, List<Offset> scope, Size size, Color color, [double strokeWidth = 2]) {
33 3 : for (int i = 1; i < scope.length; i++) {
34 6 : _paintLine(canvas, getValue(scope[i - 1], size), getValue(scope[i], size), color, strokeWidth);
35 : }
36 : }
37 :
38 1 : _paintLine(Canvas canvas, Offset startPoint, Offset endPoint, Color color, double strokeWidth) {
39 1 : final line = Paint()
40 1 : ..color = color
41 1 : ..style = PaintingStyle.stroke
42 1 : ..strokeWidth = strokeWidth;
43 1 : canvas.drawLine(startPoint, endPoint, line);
44 : }
45 : }
|