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/ohlc_data.dart'; 5 : import 'package:app_finance/charts/painter/abstract_painter.dart'; 6 : import 'package:flutter/material.dart'; 7 : 8 : class OhlcChartPainter extends AbstractPainter { 9 : final List<OhlcData> data; 10 : final Color color; 11 : 12 0 : OhlcChartPainter({ 13 : required super.indent, 14 : required this.data, 15 : required this.color, 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 : _drawLine(canvas, size, data[i]); 30 0 : _drawRectangle(canvas, size, data[i]); 31 : } 32 : } 33 : 34 0 : _drawRectangle(Canvas canvas, Size size, OhlcData value) { 35 0 : final paint = Paint() 36 0 : ..color = value.open > value.close ? Colors.red : Colors.blue 37 0 : ..style = PaintingStyle.fill 38 0 : ..strokeWidth = 1; 39 0 : final time = value.date.millisecondsSinceEpoch.toDouble() - msDay * 8; 40 0 : final rect = Rect.fromPoints( 41 0 : getValue(Offset(time - msDay * 1.5, value.open), size), 42 0 : getValue(Offset(time + msDay * 1.5, value.close), size), 43 : ); 44 0 : canvas.drawRect(rect, paint); 45 : } 46 : 47 0 : _drawLine(Canvas canvas, Size size, OhlcData value) { 48 0 : final line = Paint() 49 0 : ..color = color 50 0 : ..style = PaintingStyle.stroke 51 0 : ..strokeWidth = 1; 52 0 : final time = value.date.millisecondsSinceEpoch.toDouble() - msDay * 8; 53 0 : canvas.drawLine(getValue(Offset(time, value.low), size), getValue(Offset(time, value.high), size), line); 54 : } 55 : }