| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- import 'package:flutter/material.dart';
- import '../entity/macd_entity.dart';
- import '../k_chart_widget.dart' show SecondaryState;
- import 'base_chart_renderer.dart';
- class SecondaryRenderer extends BaseChartRenderer<MACDEntity> {
- late double mMACDWidth;
- SecondaryState state;
- final ChartStyle chartStyle;
- final ChartColors chartColors;
- SecondaryRenderer(
- Rect mainRect,
- double maxValue,
- double minValue,
- double topPadding,
- this.state,
- int fixedLength,
- this.chartStyle,
- this.chartColors)
- : super(
- chartRect: mainRect,
- maxValue: maxValue,
- minValue: minValue,
- topPadding: topPadding,
- fixedLength: fixedLength,
- gridColor: chartColors.gridColor,
- ) {
- mMACDWidth = this.chartStyle.macdWidth;
- }
- @override
- void drawChart(MACDEntity lastPoint, MACDEntity curPoint, double lastX,
- double curX, Size size, Canvas canvas) {
- switch (state) {
- case SecondaryState.MACD:
- drawMACD(curPoint, canvas, curX, lastPoint, lastX);
- break;
- case SecondaryState.KDJ:
- drawLine(lastPoint.k, curPoint.k, canvas, lastX, curX,
- this.chartColors.kColor);
- drawLine(lastPoint.d, curPoint.d, canvas, lastX, curX,
- this.chartColors.dColor);
- drawLine(lastPoint.j, curPoint.j, canvas, lastX, curX,
- this.chartColors.jColor);
- break;
- case SecondaryState.RSI:
- drawLine(lastPoint.rsi, curPoint.rsi, canvas, lastX, curX,
- this.chartColors.rsiColor);
- break;
- case SecondaryState.WR:
- drawLine(lastPoint.r, curPoint.r, canvas, lastX, curX,
- this.chartColors.rsiColor);
- break;
- case SecondaryState.CCI:
- drawLine(lastPoint.cci, curPoint.cci, canvas, lastX, curX,
- this.chartColors.rsiColor);
- break;
- }
- }
- void drawMACD(MACDEntity curPoint, Canvas canvas, double curX,
- MACDEntity lastPoint, double lastX) {
- final macd = curPoint.macd ?? 0;
- double macdY = getY(macd);
- double r = mMACDWidth / 2;
- double zeroy = getY(0);
- if (macd > 0) {
- canvas.drawRect(Rect.fromLTRB(curX - r, macdY, curX + r, zeroy),
- chartPaint..color = this.chartColors.upColor);
- } else {
- canvas.drawRect(Rect.fromLTRB(curX - r, zeroy, curX + r, macdY),
- chartPaint..color = this.chartColors.dnColor);
- }
- if (lastPoint.dif != 0) {
- drawLine(lastPoint.dif, curPoint.dif, canvas, lastX, curX,
- this.chartColors.difColor);
- }
- if (lastPoint.dea != 0) {
- drawLine(lastPoint.dea, curPoint.dea, canvas, lastX, curX,
- this.chartColors.deaColor);
- }
- }
- @override
- void drawText(Canvas canvas, MACDEntity data, double x) {
- List<TextSpan>? children;
- switch (state) {
- case SecondaryState.MACD:
- children = [
- TextSpan(
- text: "MACD(12,26,9) ",
- style: getTextStyle(this.chartColors.defaultTextColor)),
- if (data.macd != 0)
- TextSpan(
- text: "MACD:${format(data.macd)} ",
- style: getTextStyle(this.chartColors.macdColor)),
- if (data.dif != 0)
- TextSpan(
- text: "DIF:${format(data.dif)} ",
- style: getTextStyle(this.chartColors.difColor)),
- if (data.dea != 0)
- TextSpan(
- text: "DEA:${format(data.dea)} ",
- style: getTextStyle(this.chartColors.deaColor)),
- ];
- break;
- case SecondaryState.KDJ:
- children = [
- TextSpan(
- text: "KDJ(9,1,3) ",
- style: getTextStyle(this.chartColors.defaultTextColor)),
- if (data.macd != 0)
- TextSpan(
- text: "K:${format(data.k)} ",
- style: getTextStyle(this.chartColors.kColor)),
- if (data.dif != 0)
- TextSpan(
- text: "D:${format(data.d)} ",
- style: getTextStyle(this.chartColors.dColor)),
- if (data.dea != 0)
- TextSpan(
- text: "J:${format(data.j)} ",
- style: getTextStyle(this.chartColors.jColor)),
- ];
- break;
- case SecondaryState.RSI:
- children = [
- TextSpan(
- text: "RSI(14):${format(data.rsi)} ",
- style: getTextStyle(this.chartColors.rsiColor)),
- ];
- break;
- case SecondaryState.WR:
- children = [
- TextSpan(
- text: "WR(14):${format(data.r)} ",
- style: getTextStyle(this.chartColors.rsiColor)),
- ];
- break;
- case SecondaryState.CCI:
- children = [
- TextSpan(
- text: "CCI(14):${format(data.cci)} ",
- style: getTextStyle(this.chartColors.rsiColor)),
- ];
- break;
- }
- TextPainter tp = TextPainter(
- text: TextSpan(children: children), textDirection: TextDirection.ltr);
- tp.layout();
- tp.paint(canvas, Offset(x, chartRect.top - topPadding));
- }
- @override
- void drawVerticalText(canvas, textStyle, int gridRows) {
- TextPainter maxTp = TextPainter(
- text: TextSpan(text: "${format(maxValue)}", style: textStyle),
- textDirection: TextDirection.ltr);
- maxTp.layout();
- TextPainter minTp = TextPainter(
- text: TextSpan(text: "${format(minValue)}", style: textStyle),
- textDirection: TextDirection.ltr);
- minTp.layout();
- maxTp.paint(canvas,
- Offset(chartRect.width - maxTp.width, chartRect.top - topPadding));
- minTp.paint(canvas,
- Offset(chartRect.width - minTp.width, chartRect.bottom - minTp.height));
- }
- @override
- void drawGrid(Canvas canvas, int gridRows, int gridColumns) {
- // canvas.drawLine(Offset(0, chartRect.top), Offset(chartRect.width, chartRect.top), gridPaint); //hidden line
- canvas.drawLine(Offset(0, chartRect.bottom),
- Offset(chartRect.width, chartRect.bottom), gridPaint);
- double columnSpace = chartRect.width / gridColumns;
- for (int i = 0; i <= columnSpace; i++) {
- //mSecondaryRect垂直线
- canvas.drawLine(Offset(columnSpace * i, chartRect.top - topPadding),
- Offset(columnSpace * i, chartRect.bottom), gridPaint);
- }
- }
- }
|