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 LineChartPainter extends AbstractPainter { 9 : final List<ChartData> data; 10 : 11 0 : 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 0 : @override 22 : void paint(Canvas canvas, Size size) { 23 0 : if (data.isEmpty) { 24 : return; 25 : } 26 0 : size = this.size ?? size; 27 0 : for (final scope in data) { 28 0 : _paint(canvas, scope.data, size, scope.color, scope.strokeWidth); 29 : } 30 : } 31 : 32 0 : void _paint(Canvas canvas, List<Offset> scope, Size size, Color color, [double strokeWidth = 2]) { 33 0 : for (int i = 1; i < scope.length; i++) { 34 0 : _paintLine(canvas, getValue(scope[i - 1], size), getValue(scope[i], size), color, strokeWidth); 35 : } 36 : } 37 : 38 0 : _paintLine(Canvas canvas, Offset startPoint, Offset endPoint, Color color, double strokeWidth) { 39 0 : final line = Paint() 40 0 : ..color = color 41 0 : ..style = PaintingStyle.stroke 42 0 : ..strokeWidth = strokeWidth; 43 0 : canvas.drawLine(startPoint, endPoint, line); 44 : } 45 : }