优化K线图表显示效果:增强均线配置和黑色主题

新增功能:
- 新增30日和60日移动平均线支持
- 完整的5条均线配置:MA5, MA10, MA20, MA30, MA60

 视觉优化:
- 实现专业黑色主题:图表背景、坐标轴、文本全部适配
- 优化均线颜色方案:5日白色,10日黄色,20日粉色,30日荧光绿,60日荧光蓝
- 增强MACD显示:快线白色,信号线黄色,零轴线白色
- 提升网格线和文本在黑色背景下的可读性

 技术改进:
- 采用字典映射实现颜色配置,更易维护和扩展
- 统一所有图表元素的黑色主题适配
- 保持专业金融软件的视觉风格
This commit is contained in:
cuizhan 2025-07-28 16:44:44 +08:00
parent a5ffb830a6
commit 4223c4b799

View File

@ -50,6 +50,17 @@ class KlineChartRenderer:
plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False plt.rcParams['axes.unicode_minus'] = False
# 设置黑色背景主题
plt.rcParams['figure.facecolor'] = 'black' # 图表整体背景
plt.rcParams['axes.facecolor'] = 'black' # 子图背景
plt.rcParams['axes.edgecolor'] = 'white' # 坐标轴边框颜色
plt.rcParams['axes.labelcolor'] = 'white' # 坐标轴标签颜色
plt.rcParams['xtick.color'] = 'white' # x轴刻度颜色
plt.rcParams['ytick.color'] = 'white' # y轴刻度颜色
plt.rcParams['text.color'] = 'white' # 文本颜色
plt.rcParams['grid.color'] = 'gray' # 网格线颜色
plt.rcParams['grid.alpha'] = 0.3 # 网格线透明度
# 图表配置 # 图表配置
self.figure_size = (15, 10) self.figure_size = (15, 10)
@ -222,13 +233,21 @@ class KlineChartRenderer:
if not ma_dict: if not ma_dict:
return return
colors = ['orange', 'purple', 'brown', 'pink', 'gray'] # MA线颜色配置:5日白色,10日黄色,20日粉色,30日荧光绿,60日荧光蓝
ma_periods = params.ma_periods if params else [5, 10, 20] ma_colors = {
5: 'white', # 5日线:白色
10: 'yellow', # 10日线:黄色
20: 'magenta', # 20日线:粉色
30: 'lime', # 30日线:荧光绿
60: 'cyan' # 60日线:荧光蓝
}
ma_periods = params.ma_periods if params else [5, 10, 20, 30, 60]
for i, period_val in enumerate(ma_periods): for i, period_val in enumerate(ma_periods):
ma_key = f'ma{period_val}' ma_key = f'ma{period_val}'
if ma_key in ma_dict: if ma_key in ma_dict:
color = colors[i % len(colors)] # 使用预定义的颜色,如果没有则使用默认颜色
color = ma_colors.get(period_val, 'gray')
ma_series = ma_dict[ma_key] ma_series = ma_dict[ma_key]
# 过滤掉NaN值 # 过滤掉NaN值
@ -263,15 +282,15 @@ class KlineChartRenderer:
x_range = range(len(macd_line)) x_range = range(len(macd_line))
# 绘制MACD线和信号线 # 绘制MACD线和信号线
ax.plot(x_range, macd_line, label='MACD', color='blue', linewidth=1.5) ax.plot(x_range, macd_line, label='MACD', color='white', linewidth=1.5)
ax.plot(x_range, signal_line, label='Signal', color='red', linewidth=1.5) ax.plot(x_range, signal_line, label='Signal', color='yellow', linewidth=1.5)
# 绘制MACD柱状图 # 绘制MACD柱状图
colors = ['red' if h >= 0 else 'green' for h in histogram] colors = ['red' if h >= 0 else 'green' for h in histogram]
ax.bar(x_range, histogram, color=colors, alpha=0.6, width=0.8, label='Histogram') ax.bar(x_range, histogram, color=colors, alpha=0.6, width=0.8, label='Histogram')
# 添加零轴线 # 添加零轴线
ax.axhline(y=0, color='black', linestyle='-', alpha=0.3) ax.axhline(y=0, color='white', linestyle='-', alpha=0.5)
# 设置图例 # 设置图例
if params: if params:
@ -424,7 +443,7 @@ def main():
# 测试股票代码列表 # 测试股票代码列表
test_stocks = ['601658.SH', '601398.SH'] test_stocks = ['601658.SH', '601398.SH']
test_period = '1d' test_period = '60m'
# 方式1: 设置具体的起止时间 # 方式1: 设置具体的起止时间
start_date, end_date = '2025-01-01', '2025-07-28' start_date, end_date = '2025-01-01', '2025-07-28'
@ -440,14 +459,14 @@ def main():
params = None params = None
if IndicatorParams is not None: if IndicatorParams is not None:
params = IndicatorParams( params = IndicatorParams(
ma_periods=[5, 10, 20], # 移动平均线周期:适合日线 ma_periods=[5, 10, 20, 30, 60], # 移动平均线周期:5日、10日、20日、30日、60日
macd_fast=6, # MACD快线 macd_fast=6, # MACD快线
macd_slow=13, # MACD慢线 macd_slow=13, # MACD慢线
macd_signal=5 # MACD信号线 macd_signal=5 # MACD信号线
) )
print(f"📅 数据时间范围: {start_date} ~ {end_date if end_date else '最新数据'}") print(f"📅 数据时间范围: {start_date} ~ {end_date if end_date else '最新数据'}")
print(f"📊 技术指标配置: MA{params.ma_periods if params else [5,10,20]}, MACD(12,26,9)") print(f"📊 技术指标配置: MA{params.ma_periods if params else [5,10,20,30,60]}, MACD(6,13,5)")
for stock_code in test_stocks: for stock_code in test_stocks:
print(f"\n🔍 查看股票: {stock_code} ({test_period})") print(f"\n🔍 查看股票: {stock_code} ({test_period})")