diff --git a/README.md b/README.md index 300306e..6c88e4c 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ Container( height: 450, width: double.infinity, child: KChartWidget( + chartStyle, // Required for styling purposes + chartColors,// Required for styling purposes datas,// Required,Data must be an ordered list,(history=>now) isLine: isLine,// Decide whether it is k-line or time-sharing mainState: _mainState,// Decide what the main view shows @@ -59,7 +61,7 @@ if you want change DepthChart color, you can use just like this: height: 230, width: double.infinity, color: Colors.grey, //background color - child: DepthChart(_bids, _asks,buyPathColor: Colors.blue, sellPathColor: Colors.green), + child: DepthChart(_bids, _asks, chartColors, buyPathColor: Colors.blue, sellPathColor: Colors.green), ) ``` diff --git a/example/lib/main.dart b/example/lib/main.dart index 50f9578..211639f 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:k_chart/chart_style.dart'; import 'package:k_chart/flutter_k_chart.dart'; import 'package:k_chart/k_chart_widget.dart'; import 'package:http/http.dart' as http; @@ -40,6 +41,9 @@ class _MyHomePageState extends State { bool isChinese = true; List _bids, _asks; + ChartStyle chartStyle = new ChartStyle(); + ChartColors chartColors = new ChartColors(); + @override void initState() { super.initState(); @@ -96,6 +100,8 @@ class _MyHomePageState extends State { width: double.infinity, child: KChartWidget( datas, + chartStyle, + chartColors, isLine: isLine, mainState: _mainState, volHidden: _volHidden, @@ -116,7 +122,7 @@ class _MyHomePageState extends State { Container( height: 230, width: double.infinity, - child: DepthChart(_bids, _asks), + child: DepthChart(_bids, _asks, this.chartColors), ) ], ), @@ -140,6 +146,14 @@ class _MyHomePageState extends State { button("隐藏副视图", onPressed: () => _secondaryState = SecondaryState.NONE), button(_volHidden ? "显示成交量" : "隐藏成交量", onPressed: () => _volHidden = !_volHidden), button("切换中英文", onPressed: () => isChinese = !isChinese), + button("Customize UI", onPressed: () { + setState(() { + chartColors.selectBorderColor = Colors.red; + chartColors.selectFillColor = Colors.red; + chartColors.lineFillColor = Colors.red; + chartColors.kLineColor = Colors.yellow; + }); + }), ], ); } diff --git a/example/pubspec.lock b/example/pubspec.lock index bcae77e..2e839d9 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,69 +1,62 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.4.1" + version: "2.5.0" boolean_selector: dependency: transitive description: name: boolean_selector - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" - charcode: + version: "2.1.0" + characters: dependency: transitive description: - name: charcode - url: "https://pub.flutter-io.cn" + name: characters + url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" - collection: + version: "1.1.0" + charcode: dependency: transitive description: - name: collection - url: "https://pub.flutter-io.cn" + name: charcode + url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" - convert: + version: "1.2.0" + clock: dependency: transitive description: - name: convert - url: "https://pub.flutter-io.cn" + name: clock + url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" - crypto: + version: "1.1.0" + collection: dependency: transitive description: - name: crypto - url: "https://pub.flutter-io.cn" + name: collection + url: "https://pub.dartlang.org" source: hosted - version: "2.1.4" + version: "1.15.0" cupertino_icons: dependency: "direct main" description: name: cupertino_icons - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "0.1.3" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -78,72 +71,51 @@ packages: dependency: "direct main" description: name: http - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "0.12.2" http_parser: dependency: transitive description: name: http_parser - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "3.1.4" - image: - dependency: transitive - description: - name: image - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.1.12" k_chart: dependency: "direct main" description: path: ".." relative: true source: path - version: "0.1.1" + version: "0.1.4" matcher: dependency: transitive description: name: matcher - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10" meta: dependency: transitive description: name: meta - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0" path: dependency: transitive description: name: path - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.6.4" + version: "1.8.0" pedantic: dependency: transitive description: name: pedantic - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.9.0" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.1.3" + version: "1.11.0" sky_engine: dependency: transitive description: flutter @@ -153,64 +125,57 @@ packages: dependency: transitive description: name: source_span - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0" stack_trace: dependency: transitive description: name: stack_trace - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" test_api: dependency: transitive description: name: test_api - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "0.2.15" + version: "0.2.19" typed_data: dependency: transitive description: name: typed_data - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "3.6.1" + version: "2.1.0" sdks: - dart: ">=2.6.0 <3.0.0" + dart: ">=2.12.0-0.0 <3.0.0" diff --git a/lib/chart_style.dart b/lib/chart_style.dart index 6f3f14b..42a2100 100644 --- a/lib/chart_style.dart +++ b/lib/chart_style.dart @@ -1,40 +1,39 @@ import 'package:flutter/material.dart' show Color; class ChartColors { - ChartColors._(); - static const Color kLineColor = Color(0xff4C86CD); - static const Color lineFillColor = Color(0x554C86CD); - static const Color ma5Color = Color(0xffC9B885); - static const Color ma10Color = Color(0xff6CB0A6); - static const Color ma30Color = Color(0xff9979C6); - static const Color upColor = Color(0xff4DAA90); - static const Color dnColor = Color(0xffC15466); - static const Color volColor = Color(0xff4729AE); + Color kLineColor = Color(0xff4C86CD); + Color lineFillColor = Color(0x554C86CD); + Color ma5Color = Color(0xffC9B885); + Color ma10Color = Color(0xff6CB0A6); + Color ma30Color = Color(0xff9979C6); + Color upColor = Color(0xff4DAA90); + Color dnColor = Color(0xffC15466); + Color volColor = Color(0xff4729AE); - static const Color macdColor = Color(0xff4729AE); - static const Color difColor = Color(0xffC9B885); - static const Color deaColor = Color(0xff6CB0A6); + Color macdColor = Color(0xff4729AE); + Color difColor = Color(0xffC9B885); + Color deaColor = Color(0xff6CB0A6); - static const Color kColor = Color(0xffC9B885); - static const Color dColor = Color(0xff6CB0A6); - static const Color jColor = Color(0xff9979C6); - static const Color rsiColor = Color(0xffC9B885); + Color kColor = Color(0xffC9B885); + Color dColor = Color(0xff6CB0A6); + Color jColor = Color(0xff9979C6); + Color rsiColor = Color(0xffC9B885); - static const Color defaultTextColor = Color(0xff60738E); + Color defaultTextColor = Color(0xff60738E); - static const Color nowPriceColor = Color(0xffC9B885); + Color nowPriceColor = Color(0xffC9B885); //深度颜色 - static const Color depthBuyColor = Color(0xff60A893); - static const Color depthSellColor = Color(0xffC15866); + Color depthBuyColor = Color(0xff60A893); + Color depthSellColor = Color(0xffC15866); //选中后显示值边框颜色 - static const Color selectBorderColor = Color(0xff6C7A86); + Color selectBorderColor = Color(0xff6C7A86); //选中后显示值背景的填充颜色 - static const Color selectFillColor = Color(0xff0D1722); + Color selectFillColor = Color(0xff0D1722); - static Color getMAColor(int index) { + Color getMAColor(int index) { Color maColor = ma5Color; switch (index % 3) { case 0: @@ -52,26 +51,24 @@ class ChartColors { } class ChartStyle { - ChartStyle._(); - //点与点的距离 - static const double pointWidth = 11.0; + double pointWidth = 11.0; //蜡烛宽度 - static const double candleWidth = 8.5; + double candleWidth = 8.5; //蜡烛中间线的宽度 - static const double candleLineWidth = 1.5; + double candleLineWidth = 1.5; //vol柱子宽度 - static const double volWidth = 8.5; + double volWidth = 8.5; //macd柱子宽度 - static const double macdWidth = 3.0; + double macdWidth = 3.0; //垂直交叉线宽度 - static const double vCrossWidth = 8.5; + double vCrossWidth = 8.5; //水平交叉线宽度 - static const double hCrossWidth = 0.5; + double hCrossWidth = 0.5; } diff --git a/lib/depth_chart.dart b/lib/depth_chart.dart index 3d0d966..93d854d 100644 --- a/lib/depth_chart.dart +++ b/lib/depth_chart.dart @@ -9,8 +9,9 @@ class DepthChart extends StatefulWidget { final int fixedLength; final Color buyPathColor; final Color sellPathColor; + final ChartColors chartColors; - DepthChart(this.bids, this.asks, + DepthChart(this.bids, this.asks, this.chartColors, {this.fixedLength = 2, this.buyPathColor, this.sellPathColor}); @override @@ -49,7 +50,8 @@ class _DepthChartState extends State { isLongPress, widget.fixedLength, widget.buyPathColor, - widget.sellPathColor), + widget.sellPathColor, + widget.chartColors), ), ); } @@ -62,6 +64,7 @@ class DepthChartPainter extends CustomPainter { bool isLongPress; int fixedLength; Color mBuyPathColor, mSellPathColor; + ChartColors chartColors; double mPaddingBottom = 18.0; double mWidth = 0.0, @@ -79,7 +82,7 @@ class DepthChartPainter extends CustomPainter { Path mBuyPath, mSellPath; //买卖出区域边线绘制画笔 //买卖出取悦绘制画笔 - Paint mBuyLinePaint, mSellLinePaint, mBuyPathPaint, mSellPathPaint; + Paint mBuyLinePaint, mSellLinePaint, mBuyPathPaint, mSellPathPaint, selectPaint, selectBorderPaint; DepthChartPainter( this.mBuyData, @@ -88,27 +91,28 @@ class DepthChartPainter extends CustomPainter { this.isLongPress, this.fixedLength, this.mBuyPathColor, - this.mSellPathColor) { + this.mSellPathColor, + this.chartColors) { mBuyLinePaint ??= Paint() ..isAntiAlias = true - ..color = ChartColors.depthBuyColor + ..color = this.chartColors.depthBuyColor ..style = PaintingStyle.stroke ..strokeWidth = 1.0; mSellLinePaint ??= Paint() ..isAntiAlias = true - ..color = ChartColors.depthSellColor + ..color = this.chartColors.depthSellColor ..style = PaintingStyle.stroke ..strokeWidth = 1.0; mBuyPathPaint ??= Paint() ..isAntiAlias = true ..color = mBuyPathColor == null - ? ChartColors.depthBuyColor.withOpacity(0.2) + ? this.chartColors.depthBuyColor.withOpacity(0.2) : mBuyPathColor; mSellPathPaint ??= Paint() ..isAntiAlias = true ..color = mSellPathColor == null - ? ChartColors.depthSellColor.withOpacity(0.2) + ? this.chartColors.depthSellColor.withOpacity(0.2) : mSellPathColor; mBuyPath ??= Path(); mSellPath ??= Path(); @@ -125,6 +129,15 @@ class DepthChartPainter extends CustomPainter { mMaxVolume = mMaxVolume * 1.05; mMultiple = mMaxVolume / mLineCount; fixedLength ??= 2; + + selectPaint = Paint() + ..isAntiAlias = true + ..color = chartColors.selectFillColor; + selectBorderPaint = Paint() + ..isAntiAlias = true + ..color = chartColors.selectBorderColor + ..style = PaintingStyle.stroke + ..strokeWidth = 0.5; } @override @@ -292,14 +305,6 @@ class DepthChartPainter extends CustomPainter { } } - Paint selectPaint = Paint() - ..isAntiAlias = true - ..color = ChartColors.selectFillColor; - Paint selectBorderPaint = Paint() - ..isAntiAlias = true - ..color = ChartColors.selectBorderColor - ..style = PaintingStyle.stroke - ..strokeWidth = 0.5; void drawSelectView(Canvas canvas, int index, bool isLeft) { DepthEntity entity = isLeft ? mBuyData[index] : mSellData[index]; diff --git a/lib/k_chart_widget.dart b/lib/k_chart_widget.dart index 8bd228b..f911a1d 100644 --- a/lib/k_chart_widget.dart +++ b/lib/k_chart_widget.dart @@ -44,9 +44,13 @@ class KChartWidget extends StatefulWidget { final double flingRatio; final Curve flingCurve; final Function(bool) isOnDrag; + final ChartColors chartColors; + final ChartStyle chartStyle; KChartWidget( - this.datas, { + this.datas, + this.chartStyle, + this.chartColors, { this.mainState = MainState.MA, this.secondaryState = SecondaryState.MACD, this.onSecondaryTap, @@ -61,7 +65,7 @@ class KChartWidget extends StatefulWidget { this.flingTime = 600, this.flingRatio = 0.5, this.flingCurve = Curves.decelerate, - this.isOnDrag, + this.isOnDrag }) : assert(maDayList != null); @override @@ -109,6 +113,8 @@ class _KChartWidgetState extends State mScaleX = 1.0; } final _painter = ChartPainter( + widget.chartStyle, + widget.chartColors, datas: widget.datas, scaleX: mScaleX, scrollX: mScrollX, @@ -121,7 +127,8 @@ class _KChartWidgetState extends State sink: mInfoWindowStream?.sink, bgColor: widget.bgColor, fixedLength: widget.fixedLength, - maDayList: widget.maDayList); + maDayList: widget.maDayList, + ); return GestureDetector( onTapUp: (details) { if(widget.onSecondaryTap != null && _painter.isInSecondaryRect(details.localPosition)) { @@ -287,9 +294,9 @@ class _KChartWidgetState extends State top: 25), width: mWidth / 3, decoration: BoxDecoration( - color: ChartColors.selectFillColor, + color: widget.chartColors.selectFillColor, border: Border.all( - color: ChartColors.selectBorderColor, width: 0.5)), + color: widget.chartColors.selectBorderColor, width: 0.5)), child: ListView.builder( padding: EdgeInsets.all(4), itemCount: infoNamesCN.length, diff --git a/lib/renderer/base_chart_painter.dart b/lib/renderer/base_chart_painter.dart index b7f5835..53f0ef8 100644 --- a/lib/renderer/base_chart_painter.dart +++ b/lib/renderer/base_chart_painter.dart @@ -34,10 +34,12 @@ abstract class BaseChartPainter extends CustomPainter { mMainLowMinValue = double.maxFinite; int mItemCount = 0; double mDataLen = 0.0; //数据占屏幕总长度 - double mPointWidth = ChartStyle.pointWidth; + final ChartStyle chartStyle; + double mPointWidth; List mFormats = [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn]; //格式化时间 BaseChartPainter( + this.chartStyle, {@required this.datas, @required this.scaleX, @required this.scrollX, @@ -48,6 +50,7 @@ abstract class BaseChartPainter extends CustomPainter { this.secondaryState, this.isLine}) { mItemCount = datas?.length ?? 0; + mPointWidth = this.chartStyle.pointWidth; mDataLen = mItemCount * mPointWidth; initFormats(); } diff --git a/lib/renderer/chart_painter.dart b/lib/renderer/chart_painter.dart index 34da159..9b25646 100644 --- a/lib/renderer/chart_painter.dart +++ b/lib/renderer/chart_painter.dart @@ -23,8 +23,13 @@ class ChartPainter extends BaseChartPainter { List bgColor; int fixedLength; List maDayList; + final ChartColors chartColors; + Paint selectPointPaint, selectorBorderPaint; + final ChartStyle chartStyle; ChartPainter( + this.chartStyle, + this.chartColors, {@required datas, @required scaleX, @required scrollX, @@ -40,6 +45,7 @@ class ChartPainter extends BaseChartPainter { this.maDayList}) : assert(bgColor == null || bgColor.length >= 2), super( + chartStyle, datas: datas, scaleX: scaleX, scrollX: scrollX, @@ -48,7 +54,18 @@ class ChartPainter extends BaseChartPainter { mainState: mainState, volHidden: volHidden, secondaryState: secondaryState, - isLine: isLine); + isLine: isLine + ) { + selectPointPaint = Paint() + ..isAntiAlias = true + ..strokeWidth = 0.5 + ..color = this.chartColors.selectFillColor; + selectorBorderPaint = Paint() + ..isAntiAlias = true + ..strokeWidth = 0.5 + ..style = PaintingStyle.stroke + ..color = this.chartColors.selectBorderColor; + } @override void initChartRenderer() { @@ -62,10 +79,10 @@ class ChartPainter extends BaseChartPainter { } } mMainRenderer ??= MainRenderer(mMainRect, mMainMaxValue, mMainMinValue, - mTopPadding, mainState, isLine, fixedLength, maDayList); + mTopPadding, mainState, isLine, fixedLength, this.chartStyle, this.chartColors, maDayList); if (mVolRect != null) { mVolRenderer ??= VolRenderer( - mVolRect, mVolMaxValue, mVolMinValue, mChildPadding, fixedLength); + mVolRect, mVolMaxValue, mVolMinValue, mChildPadding, fixedLength, this.chartStyle, this.chartColors); } if (mSecondaryRect != null) mSecondaryRenderer ??= SecondaryRenderer( @@ -74,7 +91,9 @@ class ChartPainter extends BaseChartPainter { mSecondaryMinValue, mChildPadding, secondaryState, - fixedLength); + fixedLength, + chartStyle, + chartColors); } @override @@ -142,7 +161,7 @@ class ChartPainter extends BaseChartPainter { @override void drawRightText(canvas) { - var textStyle = getTextStyle(ChartColors.defaultTextColor); + var textStyle = getTextStyle(this.chartColors.defaultTextColor); mMainRenderer?.drawRightText(canvas, textStyle, mGridRows); mVolRenderer?.drawRightText(canvas, textStyle, mGridRows); mSecondaryRenderer?.drawRightText(canvas, textStyle, mGridRows); @@ -159,7 +178,7 @@ class ChartPainter extends BaseChartPainter { if (translateX >= startX && translateX <= stopX) { int index = indexOfTranslateX(translateX); if (datas[index] == null) continue; - TextPainter tp = getTextPainter(getDate(datas[index].time)); + TextPainter tp = getTextPainter(getDate(datas[index].time), null); y = size.height - (mBottomPadding - tp.height) / 2 - tp.height; tp.paint(canvas, Offset(columnSpace * i - tp.width / 2, y)); } @@ -177,16 +196,6 @@ class ChartPainter extends BaseChartPainter { // } } - Paint selectPointPaint = Paint() - ..isAntiAlias = true - ..strokeWidth = 0.5 - ..color = ChartColors.selectFillColor; - Paint selectorBorderPaint = Paint() - ..isAntiAlias = true - ..strokeWidth = 0.5 - ..style = PaintingStyle.stroke - ..color = ChartColors.selectBorderColor; - @override void drawCrossLineText(Canvas canvas, Size size) { var index = calculateSelectedX(selectX); @@ -308,11 +317,11 @@ class ChartPainter extends BaseChartPainter { if (x < mWidth / 2) { //画右边 TextPainter tp = getTextPainter( - "------ " + value.toStringAsFixed(fixedLength), ChartColors.nowPriceColor); + "------ " + value.toStringAsFixed(fixedLength), this.chartColors.nowPriceColor); tp.paint(canvas, Offset(x, y - tp.height / 2)); } else { TextPainter tp = getTextPainter( - value.toStringAsFixed(fixedLength) + " ------", ChartColors.nowPriceColor); + value.toStringAsFixed(fixedLength) + " ------", this.chartColors.nowPriceColor); tp.paint(canvas, Offset(x - tp.width, y - tp.height / 2)); } } @@ -323,7 +332,7 @@ class ChartPainter extends BaseChartPainter { KLineEntity point = getItem(index); Paint paintY = Paint() ..color = Colors.white12 - ..strokeWidth = ChartStyle.vCrossWidth + ..strokeWidth = this.chartStyle.vCrossWidth ..isAntiAlias = true; double x = getX(index); double y = getMainY(point.close); @@ -333,7 +342,7 @@ class ChartPainter extends BaseChartPainter { Paint paintX = Paint() ..color = Colors.white - ..strokeWidth = ChartStyle.hCrossWidth + ..strokeWidth = this.chartStyle.hCrossWidth ..isAntiAlias = true; // k线图横线 canvas.drawLine(Offset(-mTranslateX, y), @@ -341,7 +350,10 @@ class ChartPainter extends BaseChartPainter { canvas.drawCircle(Offset(x, y), 2.0, paintX); } - TextPainter getTextPainter(text, [color = ChartColors.defaultTextColor]) { + TextPainter getTextPainter(text, color) { + if (color == null) { + color = this.chartColors.defaultTextColor; + } TextSpan span = TextSpan(text: "$text", style: getTextStyle(color)); TextPainter tp = TextPainter(text: span, textDirection: TextDirection.ltr); tp.layout(); diff --git a/lib/renderer/main_renderer.dart b/lib/renderer/main_renderer.dart index f57534d..4484eec 100644 --- a/lib/renderer/main_renderer.dart +++ b/lib/renderer/main_renderer.dart @@ -4,17 +4,20 @@ import '../k_chart_widget.dart' show MainState; import 'base_chart_renderer.dart'; class MainRenderer extends BaseChartRenderer { - double mCandleWidth = ChartStyle.candleWidth; - double mCandleLineWidth = ChartStyle.candleLineWidth; + double mCandleWidth; + double mCandleLineWidth; MainState state; bool isLine; //绘制的内容区域 Rect _contentRect; double _contentPadding = 5.0; List maDayList; + final ChartStyle chartStyle; + final ChartColors chartColors; + Paint mLinePaint; MainRenderer(Rect mainRect, double maxValue, double minValue, - double topPadding, this.state, this.isLine, int fixedLength, + double topPadding, this.state, this.isLine, int fixedLength, this.chartStyle, this.chartColors, [this.maDayList = const [5, 10, 20]]) : super( chartRect: mainRect, @@ -22,6 +25,13 @@ class MainRenderer extends BaseChartRenderer { minValue: minValue, topPadding: topPadding, fixedLength: fixedLength) { + mCandleWidth = this.chartStyle.candleWidth; + mCandleLineWidth = this.chartStyle.candleLineWidth; + mLinePaint = Paint() + ..isAntiAlias = true + ..style = PaintingStyle.stroke + ..strokeWidth = 1.0 + ..color = this.chartColors.kLineColor; _contentRect = Rect.fromLTRB( chartRect.left, chartRect.top + _contentPadding, @@ -48,15 +58,15 @@ class MainRenderer extends BaseChartRenderer { if (data.up != 0) TextSpan( text: "BOLL:${format(data.mb)} ", - style: getTextStyle(ChartColors.ma5Color)), + style: getTextStyle(this.chartColors.ma5Color)), if (data.mb != 0) TextSpan( text: "UB:${format(data.up)} ", - style: getTextStyle(ChartColors.ma10Color)), + style: getTextStyle(this.chartColors.ma10Color)), if (data.dn != 0) TextSpan( text: "LB:${format(data.dn)} ", - style: getTextStyle(ChartColors.ma30Color)), + style: getTextStyle(this.chartColors.ma30Color)), ], ); } @@ -72,7 +82,7 @@ class MainRenderer extends BaseChartRenderer { if (data.maValueList[i] != 0) { var item = TextSpan( text: "MA${maDayList[i]}:${format(data.maValueList[i])} ", - style: getTextStyle(ChartColors.getMAColor(i))); + style: getTextStyle(this.chartColors.getMAColor(i))); result.add(item); } } @@ -96,11 +106,6 @@ class MainRenderer extends BaseChartRenderer { Shader mLineFillShader; Path mLinePath, mLineFillPath; - Paint mLinePaint = Paint() - ..isAntiAlias = true - ..style = PaintingStyle.stroke - ..strokeWidth = 1.0 - ..color = ChartColors.kLineColor; Paint mLineFillPaint = Paint() ..style = PaintingStyle.fill ..isAntiAlias = true; @@ -128,7 +133,7 @@ class MainRenderer extends BaseChartRenderer { begin: Alignment.topCenter, end: Alignment.bottomCenter, tileMode: TileMode.clamp, - colors: [ChartColors.lineFillColor, Colors.transparent], + colors: [this.chartColors.lineFillColor, Colors.transparent], ).createShader(Rect.fromLTRB( chartRect.left, chartRect.top, chartRect.right, chartRect.bottom)); mLineFillPaint..shader = mLineFillShader; @@ -157,7 +162,7 @@ class MainRenderer extends BaseChartRenderer { } if (lastPoint.maValueList[i] != 0) { drawLine(lastPoint.maValueList[i], curPoint.maValueList[i], canvas, - lastX, curX, ChartColors.getMAColor(i)); + lastX, curX, this.chartColors.getMAColor(i)); } } } @@ -166,15 +171,15 @@ class MainRenderer extends BaseChartRenderer { Canvas canvas, double lastX, double curX) { if (lastPoint.up != 0) { drawLine(lastPoint.up, curPoint.up, canvas, lastX, curX, - ChartColors.ma10Color); + this.chartColors.ma10Color); } if (lastPoint.mb != 0) { drawLine( - lastPoint.mb, curPoint.mb, canvas, lastX, curX, ChartColors.ma5Color); + lastPoint.mb, curPoint.mb, canvas, lastX, curX, this.chartColors.ma5Color); } if (lastPoint.dn != 0) { drawLine(lastPoint.dn, curPoint.dn, canvas, lastX, curX, - ChartColors.ma30Color); + this.chartColors.ma30Color); } } @@ -190,7 +195,7 @@ class MainRenderer extends BaseChartRenderer { if (open - close < mCandleLineWidth) { open = close + mCandleLineWidth; } - chartPaint.color = ChartColors.upColor; + chartPaint.color = this.chartColors.upColor; canvas.drawRect( Rect.fromLTRB(curX - r, close, curX + r, open), chartPaint); canvas.drawRect( @@ -200,7 +205,7 @@ class MainRenderer extends BaseChartRenderer { if (close - open < mCandleLineWidth) { open = close - mCandleLineWidth; } - chartPaint.color = ChartColors.dnColor; + chartPaint.color = this.chartColors.dnColor; canvas.drawRect( Rect.fromLTRB(curX - r, open, curX + r, close), chartPaint); canvas.drawRect( diff --git a/lib/renderer/secondary_renderer.dart b/lib/renderer/secondary_renderer.dart index f921a5d..0f663a4 100644 --- a/lib/renderer/secondary_renderer.dart +++ b/lib/renderer/secondary_renderer.dart @@ -7,17 +7,21 @@ import '../k_chart_widget.dart' show SecondaryState; import 'base_chart_renderer.dart'; class SecondaryRenderer extends BaseChartRenderer { - double mMACDWidth = ChartStyle.macdWidth; + double mMACDWidth; SecondaryState state; + final ChartStyle chartStyle; + final ChartColors chartColors; SecondaryRenderer(Rect mainRect, double maxValue, double minValue, - double topPadding, this.state, int fixedLength) + double topPadding, this.state, int fixedLength, this.chartStyle, this.chartColors) : super( chartRect: mainRect, maxValue: maxValue, minValue: minValue, topPadding: topPadding, - fixedLength: fixedLength); + fixedLength: fixedLength) { + mMACDWidth = this.chartStyle.macdWidth; + } @override void drawChart(MACDEntity lastPoint, MACDEntity curPoint, double lastX, @@ -28,22 +32,22 @@ class SecondaryRenderer extends BaseChartRenderer { break; case SecondaryState.KDJ: drawLine( - lastPoint.k, curPoint.k, canvas, lastX, curX, ChartColors.kColor); + lastPoint.k, curPoint.k, canvas, lastX, curX, this.chartColors.kColor); drawLine( - lastPoint.d, curPoint.d, canvas, lastX, curX, ChartColors.dColor); + lastPoint.d, curPoint.d, canvas, lastX, curX, this.chartColors.dColor); drawLine( - lastPoint.j, curPoint.j, canvas, lastX, curX, ChartColors.jColor); + lastPoint.j, curPoint.j, canvas, lastX, curX, this.chartColors.jColor); break; case SecondaryState.RSI: drawLine(lastPoint.rsi, curPoint.rsi, canvas, lastX, curX, - ChartColors.rsiColor); + this.chartColors.rsiColor); break; case SecondaryState.WR: drawLine( - lastPoint.r, curPoint.r, canvas, lastX, curX, ChartColors.rsiColor); + lastPoint.r, curPoint.r, canvas, lastX, curX, this.chartColors.rsiColor); break; case SecondaryState.CCI: - drawLine(lastPoint.cci, curPoint.cci, canvas, lastX, curX, ChartColors.rsiColor); + drawLine(lastPoint.cci, curPoint.cci, canvas, lastX, curX, this.chartColors.rsiColor); break; default: break; @@ -57,18 +61,18 @@ class SecondaryRenderer extends BaseChartRenderer { double zeroy = getY(0); if (curPoint.macd > 0) { canvas.drawRect(Rect.fromLTRB(curX - r, macdY, curX + r, zeroy), - chartPaint..color = ChartColors.upColor); + chartPaint..color = this.chartColors.upColor); } else { canvas.drawRect(Rect.fromLTRB(curX - r, zeroy, curX + r, macdY), - chartPaint..color = ChartColors.dnColor); + chartPaint..color = this.chartColors.dnColor); } if (lastPoint.dif != 0) { drawLine(lastPoint.dif, curPoint.dif, canvas, lastX, curX, - ChartColors.difColor); + this.chartColors.difColor); } if (lastPoint.dea != 0) { drawLine(lastPoint.dea, curPoint.dea, canvas, lastX, curX, - ChartColors.deaColor); + this.chartColors.deaColor); } } @@ -80,59 +84,59 @@ class SecondaryRenderer extends BaseChartRenderer { children = [ TextSpan( text: "MACD(12,26,9) ", - style: getTextStyle(ChartColors.defaultTextColor)), + style: getTextStyle(this.chartColors.defaultTextColor)), if (data.macd != 0) TextSpan( text: "MACD:${format(data.macd)} ", - style: getTextStyle(ChartColors.macdColor)), + style: getTextStyle(this.chartColors.macdColor)), if (data.dif != 0) TextSpan( text: "DIF:${format(data.dif)} ", - style: getTextStyle(ChartColors.difColor)), + style: getTextStyle(this.chartColors.difColor)), if (data.dea != 0) TextSpan( text: "DEA:${format(data.dea)} ", - style: getTextStyle(ChartColors.deaColor)), + style: getTextStyle(this.chartColors.deaColor)), ]; break; case SecondaryState.KDJ: children = [ TextSpan( text: "KDJ(14,1,3) ", - style: getTextStyle(ChartColors.defaultTextColor)), + style: getTextStyle(this.chartColors.defaultTextColor)), if (data.macd != 0) TextSpan( text: "K:${format(data.k)} ", - style: getTextStyle(ChartColors.kColor)), + style: getTextStyle(this.chartColors.kColor)), if (data.dif != 0) TextSpan( text: "D:${format(data.d)} ", - style: getTextStyle(ChartColors.dColor)), + style: getTextStyle(this.chartColors.dColor)), if (data.dea != 0) TextSpan( text: "J:${format(data.j)} ", - style: getTextStyle(ChartColors.jColor)), + style: getTextStyle(this.chartColors.jColor)), ]; break; case SecondaryState.RSI: children = [ TextSpan( text: "RSI(14):${format(data.rsi)} ", - style: getTextStyle(ChartColors.rsiColor)), + style: getTextStyle(this.chartColors.rsiColor)), ]; break; case SecondaryState.WR: children = [ TextSpan( text: "WR(14):${format(data.r)} ", - style: getTextStyle(ChartColors.rsiColor)), + style: getTextStyle(this.chartColors.rsiColor)), ]; break; case SecondaryState.CCI: children = [ TextSpan( text: "CCI(14):${format(data.cci)} ", - style: getTextStyle(ChartColors.rsiColor)), + style: getTextStyle(this.chartColors.rsiColor)), ]; break; default: diff --git a/lib/renderer/vol_renderer.dart b/lib/renderer/vol_renderer.dart index 93c295a..77dfd6c 100644 --- a/lib/renderer/vol_renderer.dart +++ b/lib/renderer/vol_renderer.dart @@ -6,16 +6,20 @@ import '../renderer/base_chart_renderer.dart'; import '../utils/number_util.dart'; class VolRenderer extends BaseChartRenderer { - double mVolWidth = ChartStyle.volWidth; + double mVolWidth; + final ChartStyle chartStyle; + final ChartColors chartColors; VolRenderer(Rect mainRect, double maxValue, double minValue, - double topPadding, int fixedLength) + double topPadding, int fixedLength, this.chartStyle, this.chartColors) : super( chartRect: mainRect, maxValue: maxValue, minValue: minValue, topPadding: topPadding, - fixedLength: fixedLength); + fixedLength: fixedLength){ + mVolWidth = this.chartStyle.volWidth; + } @override void drawChart(VolumeEntity lastPoint, VolumeEntity curPoint, double lastX, @@ -28,18 +32,18 @@ class VolRenderer extends BaseChartRenderer { Rect.fromLTRB(curX - r, top, curX + r, bottom), chartPaint ..color = curPoint.close > curPoint.open - ? ChartColors.upColor - : ChartColors.dnColor); + ? this.chartColors.upColor + : this.chartColors.dnColor); } if (lastPoint.MA5Volume != 0) { drawLine(lastPoint.MA5Volume, curPoint.MA5Volume, canvas, lastX, curX, - ChartColors.ma5Color); + this.chartColors.ma5Color); } if (lastPoint.MA10Volume != 0) { drawLine(lastPoint.MA10Volume, curPoint.MA10Volume, canvas, lastX, curX, - ChartColors.ma10Color); + this.chartColors.ma10Color); } } @@ -52,15 +56,15 @@ class VolRenderer extends BaseChartRenderer { children: [ TextSpan( text: "VOL:${NumberUtil.format(data.vol)} ", - style: getTextStyle(ChartColors.volColor)), + style: getTextStyle(this.chartColors.volColor)), if (NumberUtil.checkNotNullOrZero(data.MA5Volume)) TextSpan( text: "MA5:${NumberUtil.format(data.MA5Volume)} ", - style: getTextStyle(ChartColors.ma5Color)), + style: getTextStyle(this.chartColors.ma5Color)), if (NumberUtil.checkNotNullOrZero(data.MA10Volume)) TextSpan( text: "MA10:${NumberUtil.format(data.MA10Volume)} ", - style: getTextStyle(ChartColors.ma10Color)), + style: getTextStyle(this.chartColors.ma10Color)), ], ); TextPainter tp = TextPainter(text: span, textDirection: TextDirection.ltr); diff --git a/pubspec.lock b/pubspec.lock index 414a5e8..19b13d0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,62 +1,55 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.4.1" + version: "2.5.0" boolean_selector: dependency: transitive description: name: boolean_selector - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" charcode: dependency: transitive description: name: charcode - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" - collection: + version: "1.2.0" + clock: dependency: transitive description: - name: collection - url: "https://pub.flutter-io.cn" + name: clock + url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" - convert: + version: "1.1.0" + collection: dependency: transitive description: - name: convert - url: "https://pub.flutter-io.cn" + name: collection + url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" - crypto: + version: "1.15.0" + fake_async: dependency: transitive description: - name: crypto - url: "https://pub.flutter-io.cn" + name: fake_async + url: "https://pub.dartlang.org" source: hosted - version: "2.1.4" + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -67,48 +60,27 @@ packages: description: flutter source: sdk version: "0.0.0" - image: - dependency: transitive - description: - name: image - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.1.12" matcher: dependency: transitive description: name: matcher - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10" meta: dependency: transitive description: name: meta - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0" path: dependency: transitive description: name: path - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.1.3" + version: "1.8.0" sky_engine: dependency: transitive description: flutter @@ -118,64 +90,57 @@ packages: dependency: transitive description: name: source_span - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0" stack_trace: dependency: transitive description: name: stack_trace - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" test_api: dependency: transitive description: name: test_api - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "0.2.15" + version: "0.2.19" typed_data: dependency: transitive description: name: typed_data - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "3.6.1" + version: "2.1.0" sdks: - dart: ">=2.6.0 <3.0.0" + dart: ">=2.12.0-0.0 <3.0.0"