-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygal.py
More file actions
99 lines (83 loc) · 2.48 KB
/
pygal.py
File metadata and controls
99 lines (83 loc) · 2.48 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
""" anyplot.ai
line-timeseries: Time Series Line Plot
Library: pygal 3.1.0 | Python 3.13.13
Quality: 85/100 | Updated: 2026-05-09
"""
import os
import random
import sys
from datetime import datetime, timedelta
# Ensure site-packages is in path before current directory to avoid shadowing
site_packages = next((p for p in sys.path if "site-packages" in p), None)
if site_packages and sys.path[0] == os.path.dirname(__file__):
sys.path.remove(sys.path[0])
sys.path.insert(0, site_packages)
import pygal # noqa: E402
from pygal.style import Style # noqa: E402
# Theme tokens
THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F"
# Okabe-Ito palette
OKABE_ITO = ("#009E73", "#D55E00", "#0072B2", "#CC79A7", "#E69F00", "#56B4E9", "#F0E442")
# Seed for reproducibility
random.seed(42)
# Generate realistic daily stock price data for one year
start_date = datetime(2024, 1, 1)
dates = [start_date + timedelta(days=i) for i in range(365)]
# Simulate stock price with trend and volatility
price = 150.0
prices = []
for _ in range(365):
change = random.gauss(0.1, 2.5)
price = max(100, price + change)
prices.append(round(price, 2))
# Custom style for 4800x2700 canvas
custom_style = Style(
background=PAGE_BG,
plot_background=PAGE_BG,
foreground=INK,
foreground_strong=INK,
foreground_subtle=INK_MUTED,
colors=OKABE_ITO,
title_font_size=28,
label_font_size=22,
major_label_font_size=18,
legend_font_size=16,
value_font_size=14,
stroke_width=6,
)
# Create line chart
chart = pygal.Line(
width=4800,
height=2700,
style=custom_style,
title="line-timeseries · pygal · anyplot.ai",
x_title="Date",
y_title="Stock Price (USD)",
show_x_guides=True,
show_y_guides=True,
x_label_rotation=45,
show_legend=True,
legend_at_bottom=True,
truncate_legend=-1,
show_dots=False,
margin=100,
)
# Add data series
chart.add("ACME Corp Stock", prices)
# Set x-axis labels - show first of each month only
x_labels = []
x_labels_major = []
for d in dates:
if d.day == 1:
x_labels.append(d.strftime("%b %Y"))
x_labels_major.append(d.strftime("%b %Y"))
else:
x_labels.append("")
chart.x_labels = x_labels
chart.x_labels_major = x_labels_major
# Save as PNG and HTML
chart.render_to_file(f"plot-{THEME}.html")
chart.render_to_png(f"plot-{THEME}.png")