feat: K线图改用k_chart库,支持完整技术指标(MA/BOLL/MACD/KDJ/RSI/VOL)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:interactive_chart/interactive_chart.dart';
|
||||
import 'package:k_chart/entity/k_line_entity.dart';
|
||||
import '../models/candle.dart';
|
||||
|
||||
/// 时间周期
|
||||
@@ -16,26 +16,24 @@ enum ChartInterval {
|
||||
const ChartInterval(this.label, this.value);
|
||||
}
|
||||
|
||||
/// 技术指标开关
|
||||
/// 技术指标设置
|
||||
class IndicatorSettings {
|
||||
final bool showMA;
|
||||
final bool showEMA;
|
||||
final bool showBOLL;
|
||||
final bool showVOL;
|
||||
final bool showMACD;
|
||||
final int maPeriod;
|
||||
final int emaPeriod;
|
||||
final int bollPeriod;
|
||||
final bool showKDJ;
|
||||
final bool showRSI;
|
||||
|
||||
const IndicatorSettings({
|
||||
this.showMA = true,
|
||||
this.showEMA = false,
|
||||
this.showBOLL = false,
|
||||
this.showVOL = true,
|
||||
this.showMACD = false,
|
||||
this.maPeriod = 7,
|
||||
this.emaPeriod = 14,
|
||||
this.bollPeriod = 20,
|
||||
this.showMACD = true,
|
||||
this.showKDJ = false,
|
||||
this.showRSI = false,
|
||||
});
|
||||
|
||||
IndicatorSettings copyWith({
|
||||
@@ -44,9 +42,8 @@ class IndicatorSettings {
|
||||
bool? showBOLL,
|
||||
bool? showVOL,
|
||||
bool? showMACD,
|
||||
int? maPeriod,
|
||||
int? emaPeriod,
|
||||
int? bollPeriod,
|
||||
bool? showKDJ,
|
||||
bool? showRSI,
|
||||
}) {
|
||||
return IndicatorSettings(
|
||||
showMA: showMA ?? this.showMA,
|
||||
@@ -54,16 +51,14 @@ class IndicatorSettings {
|
||||
showBOLL: showBOLL ?? this.showBOLL,
|
||||
showVOL: showVOL ?? this.showVOL,
|
||||
showMACD: showMACD ?? this.showMACD,
|
||||
maPeriod: maPeriod ?? this.maPeriod,
|
||||
emaPeriod: emaPeriod ?? this.emaPeriod,
|
||||
bollPeriod: bollPeriod ?? this.bollPeriod,
|
||||
showKDJ: showKDJ ?? this.showKDJ,
|
||||
showRSI: showRSI ?? this.showRSI,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// K 线数据 Provider
|
||||
class ChartProvider extends ChangeNotifier {
|
||||
|
||||
// 当前币种
|
||||
String _symbol = 'BTC';
|
||||
String get symbol => _symbol;
|
||||
@@ -76,42 +71,22 @@ class ChartProvider extends ChangeNotifier {
|
||||
IndicatorSettings _indicators = const IndicatorSettings();
|
||||
IndicatorSettings get indicators => _indicators;
|
||||
|
||||
// K 线数据
|
||||
// K 线数据(原始)
|
||||
List<Candle> _candles = [];
|
||||
List<Candle> get candles => _candles;
|
||||
|
||||
List<CandleData> get candleData {
|
||||
final baseData = _candles
|
||||
.map((c) => CandleData(
|
||||
timestamp: c.timestamp,
|
||||
open: c.open,
|
||||
high: c.high,
|
||||
low: c.low,
|
||||
close: c.close,
|
||||
volume: c.volume,
|
||||
))
|
||||
.toList();
|
||||
|
||||
// 如果开启MA,计算均线并添加到趋势线
|
||||
if (_indicators.showMA && baseData.isNotEmpty) {
|
||||
final ma7 = CandleData.computeMA(baseData, 7);
|
||||
final ma14 = CandleData.computeMA(baseData, 14);
|
||||
final ma30 = CandleData.computeMA(baseData, 30);
|
||||
|
||||
return List.generate(baseData.length, (i) {
|
||||
return CandleData(
|
||||
timestamp: baseData[i].timestamp,
|
||||
open: baseData[i].open,
|
||||
high: baseData[i].high,
|
||||
low: baseData[i].low,
|
||||
close: baseData[i].close,
|
||||
volume: baseData[i].volume,
|
||||
trends: [ma7[i], ma14[i], ma30[i]],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
return baseData;
|
||||
// k_chart 需要的数据格式
|
||||
List<KLineEntity> get klineData {
|
||||
return _candles.map((c) {
|
||||
return KLineEntity.fromCustom(
|
||||
open: c.open,
|
||||
high: c.high,
|
||||
low: c.low,
|
||||
close: c.close,
|
||||
vol: c.volume,
|
||||
time: c.timestamp,
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
// 状态
|
||||
@@ -125,7 +100,7 @@ class ChartProvider extends ChangeNotifier {
|
||||
void setSymbol(String symbol) {
|
||||
if (_symbol != symbol) {
|
||||
_symbol = symbol;
|
||||
loadCandles(); // 切换币种显示loading
|
||||
loadCandles();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +108,7 @@ class ChartProvider extends ChangeNotifier {
|
||||
void setInterval(ChartInterval interval) {
|
||||
if (_interval != interval) {
|
||||
_interval = interval;
|
||||
loadCandles(showLoading: false); // 切换时间周期不显示loading
|
||||
loadCandles(showLoading: false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,11 +128,6 @@ class ChartProvider extends ChangeNotifier {
|
||||
|
||||
try {
|
||||
// TODO: 对接真实 API
|
||||
// final response = await _api.getKlineData(
|
||||
// symbol: _symbol,
|
||||
// interval: _interval.value,
|
||||
// );
|
||||
|
||||
// 临时使用模拟数据
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
_candles = _generateMockCandles();
|
||||
|
||||
Reference in New Issue
Block a user