Skip to content

Commit 5ca488a

Browse files
Merge branch 'main' into implementation/slope-basic/highcharts
2 parents 20c9113 + 18a89f3 commit 5ca488a

14 files changed

Lines changed: 1751 additions & 1027 deletions

File tree

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1-
""" pyplots.ai
1+
""" anyplot.ai
22
slope-basic: Basic Slope Chart (Slopegraph)
3-
Library: altair 6.0.0 | Python 3.13.11
4-
Quality: 94/100 | Created: 2025-12-17
3+
Library: altair 6.1.0 | Python 3.13.13
4+
Quality: 86/100 | Created: 2026-04-30
55
"""
66

7-
import altair as alt
7+
import os
8+
import sys
9+
810
import pandas as pd
911

1012

11-
# Data - Product sales comparing Q1 vs Q4 (10 products)
13+
_script_dir = os.path.dirname(os.path.abspath(__file__))
14+
if _script_dir in sys.path:
15+
sys.path.remove(_script_dir)
16+
17+
import altair as alt # noqa: E402
18+
19+
20+
# Theme tokens
21+
THEME = os.getenv("ANYPLOT_THEME", "light")
22+
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
23+
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
24+
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
25+
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
26+
27+
# Okabe-Ito: position 1 = Increase, position 2 = Decrease
28+
COLOR_INCREASE = "#009E73"
29+
COLOR_DECREASE = "#D55E00"
30+
31+
# Data
1232
data = pd.DataFrame(
1333
{
1434
"Product": [
@@ -28,84 +48,67 @@
2848
}
2949
)
3050

31-
# Reshape data to long format for slope chart
3251
df_long = pd.melt(data, id_vars=["Product"], value_vars=["Q1 Sales", "Q4 Sales"], var_name="Period", value_name="Sales")
33-
34-
# Determine direction of change for color coding
3552
data["Direction"] = data.apply(lambda row: "Increase" if row["Q4 Sales"] > row["Q1 Sales"] else "Decrease", axis=1)
3653
df_long = df_long.merge(data[["Product", "Direction"]], on="Product")
3754

38-
# Create slope chart
55+
color_scale = alt.Scale(domain=["Increase", "Decrease"], range=[COLOR_INCREASE, COLOR_DECREASE])
56+
57+
# Plot
3958
lines = (
4059
alt.Chart(df_long)
4160
.mark_line(strokeWidth=3, opacity=0.8)
4261
.encode(
43-
x=alt.X("Period:N", axis=alt.Axis(labelFontSize=20, titleFontSize=24, title=None, labelAngle=0)),
62+
x=alt.X("Period:N", axis=alt.Axis(labelFontSize=20, title=None, labelAngle=0)),
4463
y=alt.Y(
4564
"Sales:Q",
4665
axis=alt.Axis(labelFontSize=18, titleFontSize=22, title="Sales (units)"),
4766
scale=alt.Scale(zero=False),
4867
),
4968
color=alt.Color(
50-
"Direction:N",
51-
scale=alt.Scale(domain=["Increase", "Decrease"], range=["#306998", "#FFD43B"]),
52-
legend=alt.Legend(titleFontSize=20, labelFontSize=18, orient="top-right"),
69+
"Direction:N", scale=color_scale, legend=alt.Legend(titleFontSize=20, labelFontSize=18, orient="top-right")
5370
),
5471
detail="Product:N",
5572
)
5673
)
5774

58-
# Add points at endpoints
5975
points = (
6076
alt.Chart(df_long)
6177
.mark_circle(size=200, opacity=0.9)
62-
.encode(
63-
x="Period:N",
64-
y="Sales:Q",
65-
color=alt.Color(
66-
"Direction:N", scale=alt.Scale(domain=["Increase", "Decrease"], range=["#306998", "#FFD43B"]), legend=None
67-
),
68-
)
78+
.encode(x="Period:N", y="Sales:Q", color=alt.Color("Direction:N", scale=color_scale, legend=None))
6979
)
7080

71-
# Add labels at left endpoint (Q1)
7281
labels_left = (
7382
alt.Chart(df_long[df_long["Period"] == "Q1 Sales"])
7483
.mark_text(align="right", dx=-15, fontSize=16)
75-
.encode(
76-
x="Period:N",
77-
y="Sales:Q",
78-
text="Product:N",
79-
color=alt.Color(
80-
"Direction:N", scale=alt.Scale(domain=["Increase", "Decrease"], range=["#306998", "#FFD43B"]), legend=None
81-
),
82-
)
84+
.encode(x="Period:N", y="Sales:Q", text="Product:N", color=alt.Color("Direction:N", scale=color_scale, legend=None))
8385
)
8486

85-
# Add labels at right endpoint (Q4)
8687
labels_right = (
8788
alt.Chart(df_long[df_long["Period"] == "Q4 Sales"])
8889
.mark_text(align="left", dx=15, fontSize=16)
89-
.encode(
90-
x="Period:N",
91-
y="Sales:Q",
92-
text="Product:N",
93-
color=alt.Color(
94-
"Direction:N", scale=alt.Scale(domain=["Increase", "Decrease"], range=["#306998", "#FFD43B"]), legend=None
95-
),
96-
)
90+
.encode(x="Period:N", y="Sales:Q", text="Product:N", color=alt.Color("Direction:N", scale=color_scale, legend=None))
9791
)
9892

99-
# Combine layers
93+
# Style
10094
chart = (
10195
(lines + points + labels_left + labels_right)
102-
.properties(
103-
width=1400, height=850, title=alt.Title(text="slope-basic · altair · pyplots.ai", fontSize=28, anchor="middle")
96+
.properties(width=1400, height=850, background=PAGE_BG, title="slope-basic · altair · anyplot.ai")
97+
.configure_title(color=INK, fontSize=28, anchor="middle")
98+
.configure_axis(
99+
domainColor=INK_SOFT,
100+
tickColor=INK_SOFT,
101+
grid=True,
102+
gridColor=INK,
103+
gridOpacity=0.10,
104+
gridDash=[4, 4],
105+
labelColor=INK_SOFT,
106+
titleColor=INK,
104107
)
105-
.configure_axis(grid=True, gridOpacity=0.3, gridDash=[4, 4])
106-
.configure_view(strokeWidth=0)
108+
.configure_view(fill=PAGE_BG, stroke=INK_SOFT)
109+
.configure_legend(fillColor=ELEVATED_BG, strokeColor=INK_SOFT, labelColor=INK_SOFT, titleColor=INK)
107110
)
108111

109-
# Save as PNG (4800 × 2700 px) and HTML
110-
chart.save("plot.png", scale_factor=3.0)
111-
chart.save("plot.html")
112+
# Save
113+
chart.save(f"plot-{THEME}.png", scale_factor=3.0)
114+
chart.save(f"plot-{THEME}.html")
Lines changed: 100 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
""" pyplots.ai
1+
""" anyplot.ai
22
slope-basic: Basic Slope Chart (Slopegraph)
3-
Library: bokeh 3.8.1 | Python 3.13.11
4-
Quality: 91/100 | Created: 2025-12-23
3+
Library: bokeh 3.9.0 | Python 3.13.13
4+
Quality: 84/100 | Updated: 2026-04-30
55
"""
66

7+
import os
8+
79
from bokeh.io import export_png, save
8-
from bokeh.models import Label
10+
from bokeh.models import ColumnDataSource, HoverTool, Label
911
from bokeh.plotting import figure
1012
from bokeh.resources import CDN
1113

1214

13-
# Data - Product sales comparison Q1 vs Q4 (10 products)
15+
THEME = os.getenv("ANYPLOT_THEME", "light")
16+
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
17+
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
18+
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
19+
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
20+
21+
INCREASE_COLOR = "#009E73" # Okabe-Ito position 1 (brand green)
22+
DECREASE_COLOR = "#D55E00" # Okabe-Ito position 2 (vermillion)
23+
1424
products = [
1525
"Product A",
1626
"Product B",
@@ -26,66 +36,115 @@
2636
q1_sales = [85, 72, 91, 45, 68, 53, 78, 62, 40, 88]
2737
q4_sales = [92, 65, 88, 71, 74, 48, 95, 58, 67, 82]
2838

29-
# Determine direction for color coding
30-
colors = []
31-
for start, end in zip(q1_sales, q4_sales, strict=True):
32-
if end > start:
33-
colors.append("#306998") # Python Blue - increase
34-
elif end < start:
35-
colors.append("#FFD43B") # Python Yellow - decrease
36-
else:
37-
colors.append("#888888") # Gray - no change
38-
39-
# Create figure
39+
colors = [INCREASE_COLOR if end > start else DECREASE_COLOR for start, end in zip(q1_sales, q4_sales, strict=True)]
40+
41+
42+
def spread_labels(ys, min_gap=4.5):
43+
"""Shift label y-positions apart so dense clusters don't overlap."""
44+
n = len(ys)
45+
order = sorted(range(n), key=lambda i: ys[i])
46+
adjusted = [float(ys[i]) for i in order]
47+
for _ in range(30):
48+
changed = False
49+
for i in range(1, n):
50+
if adjusted[i] - adjusted[i - 1] < min_gap:
51+
mid = (adjusted[i] + adjusted[i - 1]) / 2
52+
adjusted[i - 1] = mid - min_gap / 2
53+
adjusted[i] = mid + min_gap / 2
54+
changed = True
55+
if not changed:
56+
break
57+
result = [0.0] * n
58+
for new_i, orig_i in enumerate(order):
59+
result[orig_i] = adjusted[new_i]
60+
return result
61+
62+
63+
left_y = spread_labels(q1_sales)
64+
right_y = spread_labels(q4_sales)
65+
4066
p = figure(
4167
width=4800,
4268
height=2700,
43-
title="slope-basic · bokeh · pyplots.ai",
69+
title="slope-basic · bokeh · anyplot.ai",
4470
x_range=(-0.5, 1.5),
45-
y_range=(25, 105),
71+
y_range=(25, 112),
4672
toolbar_location=None,
4773
)
4874

49-
# Style title and axes
75+
p.background_fill_color = PAGE_BG
76+
p.border_fill_color = PAGE_BG
77+
p.outline_line_color = None
78+
5079
p.title.text_font_size = "32pt"
5180
p.title.align = "center"
81+
p.title.text_color = INK
5282

53-
# Remove x axis tick labels and set custom labels for time points
5483
p.xaxis.visible = False
5584
p.yaxis.axis_label = "Sales (thousands)"
5685
p.yaxis.axis_label_text_font_size = "22pt"
86+
p.yaxis.axis_label_text_color = INK
5787
p.yaxis.major_label_text_font_size = "18pt"
88+
p.yaxis.major_label_text_color = INK_SOFT
89+
p.yaxis.axis_line_color = INK_SOFT
90+
p.yaxis.major_tick_line_color = INK_SOFT
91+
92+
p.xgrid.grid_line_color = None
93+
p.ygrid.grid_line_color = INK_SOFT
94+
p.ygrid.grid_line_alpha = 0.10
5895

59-
# Add time point labels at bottom
60-
p.add_layout(Label(x=0, y=32, text="Q1", text_font_size="28pt", text_align="center", text_baseline="top"))
61-
p.add_layout(Label(x=1, y=32, text="Q4", text_font_size="28pt", text_align="center", text_baseline="top"))
96+
# Time point labels
97+
for x_pos, label in [(0, "Q1"), (1, "Q4")]:
98+
p.add_layout(
99+
Label(
100+
x=x_pos, y=27, text=label, text_font_size="28pt", text_align="center", text_baseline="top", text_color=INK
101+
)
102+
)
103+
104+
# Direction legend in upper-center (above data range)
105+
for y_pos, legend_text, color in [(109, "— Increase", INCREASE_COLOR), (103, "— Decrease", DECREASE_COLOR)]:
106+
p.add_layout(
107+
Label(
108+
x=0.5,
109+
y=y_pos,
110+
text=legend_text,
111+
text_font_size="20pt",
112+
text_align="center",
113+
text_baseline="middle",
114+
text_color=color,
115+
)
116+
)
62117

63-
# Draw slope lines connecting Q1 to Q4 for each product
118+
# ColumnDataSource for scatter enables HoverTool
119+
scatter_data: dict[str, list] = {"x": [], "y": [], "color": [], "product": [], "period": [], "value": []}
64120
for product, start, end, color in zip(products, q1_sales, q4_sales, colors, strict=True):
65-
# Draw the connecting line
66-
p.line(x=[0, 1], y=[start, end], line_width=4, line_color=color, line_alpha=0.8)
121+
scatter_data["x"].extend([0, 1])
122+
scatter_data["y"].extend([start, end])
123+
scatter_data["color"].extend([color, color])
124+
scatter_data["product"].extend([product, product])
125+
scatter_data["period"].extend(["Q1", "Q4"])
126+
scatter_data["value"].extend([start, end])
67127

68-
# Add markers at both endpoints
69-
p.scatter(x=[0, 1], y=[start, end], size=18, color=color, alpha=0.9)
128+
source = ColumnDataSource(data=scatter_data)
70129

71-
# Add labels at start (Q1) - left aligned
130+
# Draw slope lines and endpoint labels
131+
for i, (product, start, end, color) in enumerate(zip(products, q1_sales, q4_sales, colors, strict=True)):
132+
p.line(x=[0, 1], y=[start, end], line_width=4, line_color=color, line_alpha=0.8)
72133
p.add_layout(
73134
Label(
74135
x=-0.05,
75-
y=start,
136+
y=left_y[i],
76137
text=f"{product}: {start}",
77138
text_font_size="18pt",
78139
text_align="right",
79140
text_baseline="middle",
80141
text_color=color,
81142
)
82143
)
83-
84-
# Add labels at end (Q4) - right aligned
85144
p.add_layout(
86145
Label(
87146
x=1.05,
88-
y=end,
147+
y=right_y[i],
89148
text=f"{end}: {product}",
90149
text_font_size="18pt",
91150
text_align="left",
@@ -94,16 +153,12 @@
94153
)
95154
)
96155

97-
# Style grid
98-
p.xgrid.grid_line_color = None
99-
p.ygrid.grid_line_alpha = 0.3
100-
p.ygrid.grid_line_dash = "dashed"
101-
102-
# Style outline
103-
p.outline_line_color = None
104-
105-
# Save as PNG
106-
export_png(p, filename="plot.png")
156+
dots = p.scatter(x="x", y="y", size=18, color="color", source=source, alpha=0.9)
157+
p.add_tools(
158+
HoverTool(
159+
renderers=[dots], tooltips=[("Product", "@product"), ("Period", "@period"), ("Sales", "@value{0} thousand")]
160+
)
161+
)
107162

108-
# Also save as HTML for interactive viewing
109-
save(p, filename="plot.html", resources=CDN, title="slope-basic · bokeh · pyplots.ai")
163+
export_png(p, filename=f"plot-{THEME}.png")
164+
save(p, filename=f"plot-{THEME}.html", resources=CDN, title="slope-basic · bokeh · anyplot.ai")

0 commit comments

Comments
 (0)