components.py
Composable building blocks for ESM chart classes.
Each class encapsulates a single cross-cutting concern (file I/O, layout styling, bar trace styling, total-sum annotation, or time-series styling) that was previously inherited from ESMChart. Chart classes instantiate the components they need rather than inheriting from a shared base.
BarTraceStyler
Apply consistent styling to bar traces in a Plotly figure.
Merges the formerly duplicated _style_bars (width=0.6) and
_style_grouped_bars (width=0.8) methods into one class
parameterised by bar width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
float
|
Bar width passed to |
required |
Source code in evals/plots/components.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
apply(fig, unit)
Apply bar-trace styling to all bar traces in fig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
plotly.graph_objects.Figure
|
Target figure. |
required |
unit
|
str
|
Unit string appended to the hover template. |
required |
Source code in evals/plots/components.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
FileExporter
Serialise a Plotly figure to HTML and JSON files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
typing.Any
|
Plot configuration object with |
required |
metric_name
|
str
|
Name of the metric being exported; used to fill the
|
required |
Source code in evals/plots/components.py
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
construct_file_name(groupby, idx)
Construct the file name based on the provided template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
groupby
|
list[str]
|
List of groupby keys used to fill the template. |
required |
idx
|
typing.Hashable
|
The data-frame index from the groupby clause. Scalars of any type (str, int, etc.) are wrapped in a tuple so they can be zipped with groupby. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The constructed filename string (without extension). |
Source code in evals/plots/components.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
to_html(fig, output_path, groupby, idx)
Serialise fig to an HTML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
plotly.graph_objects.Figure
|
Plotly figure to serialise. |
required |
output_path
|
pathlib.Path
|
Folder to save the file under (the |
required |
groupby
|
list[str]
|
Groupby keys needed to fill the file-name template. |
required |
idx
|
typing.Hashable
|
Data-frame index used to fill the file-name template. |
required |
Returns:
| Type | Description |
|---|---|
pathlib.Path
|
Path of the written file. |
Source code in evals/plots/components.py
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 132 133 134 135 136 137 138 | |
to_json(fig, location, year, output_path, groupby, idx)
Serialise fig to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
plotly.graph_objects.Figure
|
Plotly figure to serialise. |
required |
location
|
str
|
Geographic location label stored in the JSON payload. |
required |
year
|
typing.Any
|
Year label stored in the JSON payload ( |
required |
output_path
|
pathlib.Path
|
Folder to save the file under. |
required |
groupby
|
list[str]
|
Groupby keys needed to fill the file-name template. |
required |
idx
|
typing.Hashable
|
Data-frame index used to fill the file-name template. |
required |
Returns:
| Type | Description |
|---|---|
pathlib.Path
|
Path of the written file. |
Source code in evals/plots/components.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
LayoutStyler
Apply common Plotly layout, title, legend, and footnote styling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
typing.Any
|
Plot configuration object with |
required |
Source code in evals/plots/components.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
append_footnote(fig, footnote_text, y=-0.17, align=None)
Append a single footnote annotation at the bottom of fig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
plotly.graph_objects.Figure
|
Target figure. |
required |
footnote_text
|
str
|
Text to display. |
required |
y
|
float
|
Vertical position (negative values move the footnote down). |
-0.17
|
align
|
str
|
Text alignment mode. |
None
|
Source code in evals/plots/components.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
append_footnotes(fig)
Append both configured footnotes and adjust the bottom margin.
Source code in evals/plots/components.py
289 290 291 292 293 294 | |
apply(fig, unit='', location='')
Apply the full standard layout sequence to fig.
Calls :meth:set_base_layout,
:meth:style_title_and_legend_and_xaxis_label, and
:meth:append_footnotes in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
plotly.graph_objects.Figure
|
Target figure. |
required |
unit
|
str
|
Display unit (currently unused here; available for subclasses). |
''
|
location
|
str
|
Geographic location (currently unused here; available for subclasses). |
''
|
Source code in evals/plots/components.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
count_footnote_lines()
Return the number of <br> line-breaks across all footnote texts.
Source code in evals/plots/components.py
250 251 252 | |
set_base_layout(fig)
Apply base figure properties (size, fonts, grid, zero-line).
Source code in evals/plots/components.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
style_title_and_legend_and_xaxis_label(fig)
Update figure title font, legend position, and x-axis label.
Source code in evals/plots/components.py
236 237 238 239 240 241 242 243 244 245 246 247 248 | |
TimeSeriesStyler
Apply time-series-specific Plotly styling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
typing.Any
|
Plot configuration object with |
required |
Source code in evals/plots/components.py
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
style_axes_and_layout(fig, title, unit)
Update axes and layout for time-series figures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
plotly.graph_objects.Figure
|
Target figure. |
required |
title
|
str
|
Figure title string. |
required |
unit
|
str
|
Unit label for the y-axis title. |
required |
Source code in evals/plots/components.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
style_inflexible_demand(fig)
Override trace style for the 'Inflexible Demand' series.
Source code in evals/plots/components.py
530 531 532 533 534 535 536 537 538 | |
TotalSumRenderer
Add total-sum scatter annotations on top of stacked bar charts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col_values
|
str
|
Name of the column containing numeric values (e.g. the metric
column returned by the chart's |
required |
plot_xaxis
|
str
|
Name of the column used as the x-axis (typically |
required |
unit
|
str
|
Unit string appended to the text template. |
required |
Source code in evals/plots/components.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
add_subplot_traces(fig, df, facet_column)
Add total-sum scatter traces for every subplot in a faceted bar chart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
plotly.graph_objects.Figure
|
Target figure (must have been built with |
required |
df
|
pandas.DataFrame
|
Data frame in the same format as |
required |
facet_column
|
str
|
Column name used for faceting (e.g. |
required |
Source code in evals/plots/components.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
add_sum_trace(fig, df, orientation=None, name_trace='Sum')
Add a scatter trace for total-sum labels on a plain bar chart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
plotly.graph_objects.Figure
|
Target figure. |
required |
df
|
pandas.DataFrame
|
Data frame in the same format as |
required |
orientation
|
str
|
|
None
|
name_trace
|
str
|
Trace name (used for identification in the JSON output). |
'Sum'
|
Source code in evals/plots/components.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
empty_figure(title)
Return an empty graph with explanation text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The figure title displayed at the top of the graph. |
required |
Returns:
| Type | Description |
|---|---|
plotly.graph_objects.Figure
|
The plotly figure with a text that explains that there is no data available for this view. |
Source code in evals/plots/components.py
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
empty_input(df)
Return True if df is empty or contains only NaN values.
Source code in evals/plots/components.py
32 33 34 | |