|
79 | 79 | range=["#306998", "#2E8B57", "#DC3545"], # Blue for totals, green for positive, red for negative |
80 | 80 | ) |
81 | 81 |
|
| 82 | +# Explicit x-axis scale with category sort order |
| 83 | +x_scale = alt.Scale(domain=categories) |
| 84 | + |
82 | 85 | # Create bars |
83 | 86 | bars = ( |
84 | 87 | alt.Chart(data) |
|
89 | 92 | x=alt.X( |
90 | 93 | "category:N", |
91 | 94 | title="Category", |
92 | | - sort=alt.EncodingSortField(field="order", order="ascending"), |
| 95 | + scale=x_scale, |
93 | 96 | axis=alt.Axis(labelAngle=-30, labelFontSize=16, titleFontSize=20), |
94 | 97 | ), |
95 | 98 | y=alt.Y("start:Q", title="Amount ($)", axis=alt.Axis(labelFontSize=16, titleFontSize=20)), |
|
111 | 114 | alt.Chart(data) |
112 | 115 | .mark_text(fontSize=14, fontWeight="bold") |
113 | 116 | .encode( |
114 | | - x=alt.X("category:N", sort=alt.EncodingSortField(field="order", order="ascending")), |
115 | | - y=alt.Y("label_y:Q"), |
116 | | - text=alt.Text("label:N"), |
117 | | - color=alt.value("#333333"), |
| 117 | + x=alt.X("category:N", scale=x_scale), y=alt.Y("label_y:Q"), text=alt.Text("label:N"), color=alt.value("#333333") |
118 | 118 | ) |
119 | 119 | ) |
120 | 120 |
|
|
135 | 135 | connectors = ( |
136 | 136 | alt.Chart(connector_df) |
137 | 137 | .mark_rule(color="#666666", strokeWidth=1.5, strokeDash=[4, 4]) |
138 | | - .encode(x=alt.X("cat_start:N", sort=categories), x2=alt.X2("cat_end:N"), y=alt.Y("y:Q")) |
| 138 | + .encode(x=alt.X("cat_start:N", scale=x_scale), x2=alt.X2("cat_end:N"), y=alt.Y("y:Q")) |
139 | 139 | ) |
140 | 140 |
|
141 | | -# Combine chart layers |
| 141 | +# Combine chart layers (bars, connectors for cumulative flow, and labels) |
142 | 142 | chart = ( |
143 | | - alt.layer(bars, labels) |
| 143 | + alt.layer(bars, connectors, labels) |
144 | 144 | .properties( |
145 | | - width=1500, |
146 | | - height=800, |
| 145 | + width=1600, |
| 146 | + height=900, |
147 | 147 | title=alt.Title(text="Quarterly P&L Breakdown · waterfall-basic · altair · pyplots.ai", fontSize=28), |
148 | 148 | ) |
149 | 149 | .configure_view(strokeWidth=0) |
|
0 commit comments