Draw a pie diagram.
(
title: str,
charts: list,
max_cols: int=3,
colormap: Dict[str,str]=None,
do_show: bool=True,
)
| 371 | |
| 372 | |
| 373 | def plotly_pie2( |
| 374 | title: str, |
| 375 | charts: list, |
| 376 | max_cols: int=3, |
| 377 | colormap: Dict[str,str]=None, |
| 378 | do_show: bool=True, |
| 379 | ): |
| 380 | """Draw a pie diagram.""" |
| 381 | |
| 382 | main_title = title |
| 383 | n_charts = len(charts) |
| 384 | n_cols = min(max_cols, n_charts) |
| 385 | n_rows = math.ceil(n_charts/n_cols) |
| 386 | specs = [[{'type':'domain'}] * n_cols] * n_rows |
| 387 | |
| 388 | subtitles = [chart[1] for chart in charts] |
| 389 | fig = make_subplots(rows=n_rows, cols=n_cols, specs=specs, |
| 390 | subplot_titles=subtitles) |
| 391 | |
| 392 | pie_pallette = None |
| 393 | for i, chart in enumerate(charts): |
| 394 | df, title, values, names = chart |
| 395 | row, col = i // n_cols, i % n_cols |
| 396 | texttemplate = "%{value:.1f}" if df[values].dtype == float else '%{value}' |
| 397 | if colormap is not None: |
| 398 | pie_pallette = [colormap[key] for key in df[names]] |
| 399 | marker = dict(colors=pie_pallette, line=dict(color=NVDA_GREEN, width=1)) |
| 400 | |
| 401 | fig.add_trace(go.Pie( |
| 402 | labels=df[names], |
| 403 | values=df[values], |
| 404 | name=title, |
| 405 | marker=marker, |
| 406 | texttemplate=texttemplate,), row+1, col+1) |
| 407 | |
| 408 | if pie_pallette is None: |
| 409 | pie_pallette = default_pallete |
| 410 | |
| 411 | fig.update_traces( |
| 412 | hoverinfo='label+percent', |
| 413 | textinfo='value', |
| 414 | textfont_size=20, |
| 415 | hole=0.4, |
| 416 | textposition='inside',) |
| 417 | fig.update_layout(title=main_title, title_x=0.5, font_size=20,) |
| 418 | if do_show: |
| 419 | fig.show() |
| 420 | return fig |
no test coverage detected