-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart_generator.py
More file actions
131 lines (112 loc) · 3.95 KB
/
chart_generator.py
File metadata and controls
131 lines (112 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""Chart generation module using matplotlib."""
import platform
from pathlib import Path
from typing import Dict, List
import matplotlib
matplotlib.use("Agg") # Non-interactive backend for server/CI
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
def _setup_style():
"""Configure matplotlib style for dark-themed Obsidian-friendly charts."""
plt.style.use("dark_background")
plt.rcParams.update({
"figure.facecolor": "#1e1e2e",
"axes.facecolor": "#1e1e2e",
"axes.edgecolor": "#444466",
"axes.grid": True,
"grid.color": "#333355",
"grid.alpha": 0.3,
"text.color": "#cdd6f4",
"xtick.color": "#a6adc8",
"ytick.color": "#a6adc8",
"figure.dpi": 150,
"figure.figsize": (10, 5),
"font.size": 10,
})
# Korean font setup
system = platform.system()
if system == "Darwin":
plt.rcParams["font.family"] = "AppleGothic"
elif system == "Linux":
# GitHub Actions ubuntu - install fonts-nanum
plt.rcParams["font.family"] = "NanumGothic"
plt.rcParams["axes.unicode_minus"] = False
def generate_line_chart(
series_dict: Dict[str, Dict],
title: str,
output_path: Path,
ylabel: str = "",
normalize: bool = False,
):
"""Generate a multi-line trend chart.
Args:
series_dict: {name: {"dates": [...], "values": [...]}}
title: Chart title
output_path: Where to save PNG
ylabel: Y-axis label
normalize: If True, normalize all series to 100 at start (for comparison)
"""
_setup_style()
fig, ax = plt.subplots()
colors = ["#89b4fa", "#f38ba8", "#a6e3a1", "#fab387", "#cba6f7", "#f9e2af", "#94e2d5"]
for i, (name, data) in enumerate(series_dict.items()):
dates = data["dates"]
values = data["values"]
if normalize and len(values) > 0 and values[0] != 0:
values = [v / values[0] * 100 for v in values]
color = colors[i % len(colors)]
ax.plot(dates, values, label=name, color=color, linewidth=2)
ax.set_title(title, fontsize=13, fontweight="bold", pad=12)
if ylabel:
ax.set_ylabel(ylabel)
if normalize:
ax.set_ylabel("기준 = 100")
ax.axhline(y=100, color="#585b70", linestyle="--", alpha=0.5)
ax.legend(loc="upper left", fontsize=9, framealpha=0.3)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%m/%d"))
fig.autofmt_xdate(rotation=30)
plt.tight_layout()
output_path.parent.mkdir(parents=True, exist_ok=True)
fig.savefig(str(output_path), bbox_inches="tight")
plt.close(fig)
def generate_bar_chart(
names: List[str],
values: List[float],
title: str,
output_path: Path,
ylabel: str = "등락률 (%)",
):
"""Generate a horizontal bar chart for comparing changes.
Args:
names: Ticker/asset names
values: Change percentages
title: Chart title
output_path: Where to save PNG
"""
_setup_style()
fig, ax = plt.subplots(figsize=(10, max(4, len(names) * 0.5 + 1)))
colors = ["#a6e3a1" if v >= 0 else "#f38ba8" for v in values]
y_pos = np.arange(len(names))
bars = ax.barh(y_pos, values, color=colors, height=0.6, edgecolor="none")
ax.set_yticks(y_pos)
ax.set_yticklabels(names)
ax.set_xlabel(ylabel)
ax.set_title(title, fontsize=13, fontweight="bold", pad=12)
ax.axvline(x=0, color="#585b70", linewidth=0.8)
# Add value labels
for bar, val in zip(bars, values):
sign = "+" if val >= 0 else ""
ax.text(
bar.get_width() + (0.3 if val >= 0 else -0.3),
bar.get_y() + bar.get_height() / 2,
f"{sign}{val:.1f}%",
va="center",
ha="left" if val >= 0 else "right",
fontsize=9,
color="#cdd6f4",
)
plt.tight_layout()
output_path.parent.mkdir(parents=True, exist_ok=True)
fig.savefig(str(output_path), bbox_inches="tight")
plt.close(fig)