base_chart_renderer.dart 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:flutter/material.dart';
  2. export '../chart_style.dart';
  3. abstract class BaseChartRenderer<T> {
  4. double maxValue, minValue;
  5. late double scaleY;
  6. double topPadding;
  7. Rect chartRect;
  8. int fixedLength;
  9. Paint chartPaint = Paint()
  10. ..isAntiAlias = true
  11. ..filterQuality = FilterQuality.high
  12. ..strokeWidth = 1.0
  13. ..color = Colors.red;
  14. Paint gridPaint = Paint()
  15. ..isAntiAlias = true
  16. ..filterQuality = FilterQuality.high
  17. ..strokeWidth = 0.5
  18. ..color = Color(0xff4c5c74);
  19. BaseChartRenderer({
  20. required this.chartRect,
  21. required this.maxValue,
  22. required this.minValue,
  23. required this.topPadding,
  24. required this.fixedLength,
  25. required Color gridColor,
  26. }) {
  27. if (maxValue == minValue) {
  28. maxValue *= 1.5;
  29. minValue /= 2;
  30. }
  31. scaleY = chartRect.height / (maxValue - minValue);
  32. gridPaint.color = gridColor;
  33. // print("maxValue=====" + maxValue.toString() + "====minValue===" + minValue.toString() + "==scaleY==" + scaleY.toString());
  34. }
  35. double getY(double y) => (maxValue - y) * scaleY + chartRect.top;
  36. String format(double? n) {
  37. if (n == null || n.isNaN) {
  38. return "0.00";
  39. } else {
  40. return n.toStringAsFixed(fixedLength);
  41. }
  42. }
  43. void drawGrid(Canvas canvas, int gridRows, int gridColumns);
  44. void drawText(Canvas canvas, T data, double x);
  45. void drawVerticalText(canvas, textStyle, int gridRows);
  46. void drawChart(T lastPoint, T curPoint, double lastX, double curX, Size size,
  47. Canvas canvas);
  48. void drawLine(double? lastPrice, double? curPrice, Canvas canvas,
  49. double lastX, double curX, Color color) {
  50. if (lastPrice == null || curPrice == null) {
  51. return;
  52. }
  53. double lastY = getY(lastPrice);
  54. double curY = getY(curPrice);
  55. canvas.drawLine(
  56. Offset(lastX, lastY), Offset(curX, curY), chartPaint..color = color);
  57. }
  58. void drawCircle(Canvas canvas, double curX, double curY, Color color) {
  59. canvas.drawCircle(
  60. Offset(curX, getY(curY)),
  61. 2.0,
  62. Paint()
  63. ..style = PaintingStyle.stroke
  64. ..strokeWidth = .8
  65. ..color = color,
  66. );
  67. }
  68. TextStyle getTextStyle(Color color) {
  69. return TextStyle(fontSize: 10.0, color: color);
  70. }
  71. }