A dashboard is easier to scan when its metrics become charts rather than tables of numbers. Plotly is a Python visualization library for making interactive charts; Plotly Graph Objects (`go`) lets you build and customize each chart incrementally. Prepare the data with pandas, create a Plotly figure, then display it in Streamlit. section 6.5 section 6.5.1
Choose the chart to match the question:
- For change over time, group a metric by day. Add a `go.Scatter` trace to a `go.Figure`, using days for x-values and metric values for y-values; `mode='lines+markers'` shows both the trend and its data points. section 6.5.1
- For a breakdown, group the metric by a chosen dimension and add a `go.Pie` trace. Its `labels` identify the groups, `values` set their sizes, and `hole=0.4` makes a donut chart. section 6.5.2
- Pass the finished figure to `st.plotly_chart(chart, use_container_width=True)`. Streamlit tabs can show different metrics, while a select box lets users choose the pie chart’s grouping dimension. section 6.5.1 section 6.5.2
Sales over time and by product
Daily sales become a line chart after sales are aggregated by day. Grouping the same sales by product instead produces values for a pie chart. The change is in the pandas grouping and the Plotly trace, not in how Streamlit displays the figure. section 6.5.1 section 6.5.2
Listing 6.10 A time series chart
import plotly.graph_objs as go
from data_wrangling import get_metric_time_series
def get_time_series_chart(df, metric):
data = get_metric_time_series(df, metric)
fig = go.Figure()
fig.add_trace(
go.Scatter(x=data['Day'], y=data['Value'], mode='lines+markers')
)
fig.update_layout(
title=f"{metric.title}",
xaxis_title='Day',
yaxis_title=metric.title
)
return fig
A complete line-chart function shows the prepared data, `go.Figure`, `go.Scatter`, and axis labels together. section 6.5.1
Listing 6.11 Displaying a pie chart
import plotly.graph_objects as go
from data_wrangling import get_metric_grouped_by_dimension
def get_pie_chart(df, metric, dimension):
data = get_metric_grouped_by_dimension(df, metric, dimension)
fig = go.Figure()
fig.add_trace(
go.Pie(labels=data[dimension], values=data['Value'], hole=0.4)
)
return fig
Compare the pie-chart function with the line chart: it keeps the figure-building pattern but substitutes dimension labels and `go.Pie`. section 6.5.2

