vol_renderer.dart 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'package:flutter/material.dart';
  2. import 'package:k_chart_plus/k_chart_plus.dart';
  3. class VolRenderer extends BaseChartRenderer<VolumeEntity> {
  4. late double mVolWidth;
  5. final ChartStyle chartStyle;
  6. final ChartColors chartColors;
  7. VolRenderer(Rect mainRect, double maxValue, double minValue,
  8. double topPadding, int fixedLength, this.chartStyle, this.chartColors)
  9. : super(
  10. chartRect: mainRect,
  11. maxValue: maxValue,
  12. minValue: minValue,
  13. topPadding: topPadding,
  14. fixedLength: fixedLength,
  15. gridColor: chartColors.gridColor,
  16. ) {
  17. mVolWidth = this.chartStyle.volWidth;
  18. }
  19. @override
  20. void drawChart(VolumeEntity lastPoint, VolumeEntity curPoint, double lastX,
  21. double curX, Size size, Canvas canvas) {
  22. double r = mVolWidth / 2;
  23. double top = getVolY(curPoint.vol);
  24. double bottom = chartRect.bottom;
  25. if (curPoint.vol != 0) {
  26. canvas.drawRect(
  27. Rect.fromLTRB(curX - r, top, curX + r, bottom),
  28. chartPaint
  29. ..color = curPoint.close > curPoint.open
  30. ? this.chartColors.upColor
  31. : this.chartColors.dnColor);
  32. }
  33. if (lastPoint.MA5Volume != 0) {
  34. drawLine(lastPoint.MA5Volume, curPoint.MA5Volume, canvas, lastX, curX,
  35. this.chartColors.ma5Color);
  36. }
  37. if (lastPoint.MA10Volume != 0) {
  38. drawLine(lastPoint.MA10Volume, curPoint.MA10Volume, canvas, lastX, curX,
  39. this.chartColors.ma10Color);
  40. }
  41. }
  42. double getVolY(double value) =>
  43. (maxValue - value) * (chartRect.height / maxValue) + chartRect.top;
  44. @override
  45. void drawText(Canvas canvas, VolumeEntity data, double x) {
  46. TextSpan span = TextSpan(
  47. children: [
  48. TextSpan(
  49. text: "VOL:${NumberUtil.format(data.vol)} ",
  50. style: getTextStyle(this.chartColors.volColor)),
  51. if (data.MA5Volume.notNullOrZero)
  52. TextSpan(
  53. text: "MA5:${NumberUtil.format(data.MA5Volume!)} ",
  54. style: getTextStyle(this.chartColors.ma5Color)),
  55. if (data.MA10Volume.notNullOrZero)
  56. TextSpan(
  57. text: "MA10:${NumberUtil.format(data.MA10Volume!)} ",
  58. style: getTextStyle(this.chartColors.ma10Color)),
  59. ],
  60. );
  61. TextPainter tp = TextPainter(text: span, textDirection: TextDirection.ltr);
  62. tp.layout();
  63. tp.paint(canvas, Offset(x, chartRect.top - topPadding));
  64. }
  65. @override
  66. void drawVerticalText(canvas, textStyle, int gridRows) {
  67. TextSpan span =
  68. TextSpan(text: "${NumberUtil.format(maxValue)}", style: textStyle);
  69. TextPainter tp = TextPainter(text: span, textDirection: TextDirection.ltr);
  70. tp.layout();
  71. tp.paint(
  72. canvas, Offset(chartRect.width - tp.width, chartRect.top - topPadding));
  73. }
  74. @override
  75. void drawGrid(Canvas canvas, int gridRows, int gridColumns) {
  76. canvas.drawLine(Offset(0, chartRect.bottom),
  77. Offset(chartRect.width, chartRect.bottom), gridPaint);
  78. double columnSpace = chartRect.width / gridColumns;
  79. for (int i = 0; i <= columnSpace; i++) {
  80. //vol垂直线
  81. canvas.drawLine(Offset(columnSpace * i, chartRect.top - topPadding),
  82. Offset(columnSpace * i, chartRect.bottom), gridPaint);
  83. }
  84. }
  85. }