๐ย ย Chart.js Chart
Submitted by Lukas Masuch
Summary
Display charts using the Chart.js library with Streamlit theme integration.
Functions
chartjs_chart
Display a chart using Chart.js.
Chart.js is a popular JavaScript charting library known for its simplicity, performance, and smooth animations. This function renders a Chart.js chart from a configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
dict[str, Any]
|
Chart.js configuration object containing |
required |
height
|
Literal['content', 'stretch'] | int
|
Chart height. "content": fit to content (default). "stretch": fill container height. int: fixed pixel height. |
'content'
|
theme
|
Literal['streamlit'] | None
|
Theme for the chart. "streamlit": use Streamlit theme colors for datasets, fonts, and grid (default). None: use Chart.js defaults. |
'streamlit'
|
key
|
str | None
|
Unique key for this chart instance. |
None
|
Raises:
| Type | Description |
|---|---|
StreamlitAPIException
|
If spec is not a dict or missing required fields. |
Example
Source code in src/streamlit_extras/chartjs_chart/__init__.py
Import:
- You should add this to the top of your .py file
Examples
example_bar_chart
def example_bar_chart() -> None:
"""Basic bar chart example."""
st.write("### Bar Chart")
spec = {
"type": "bar",
"data": {
"labels": ["January", "February", "March", "April", "May"],
"datasets": [{"label": "Sales", "data": [65, 59, 80, 81, 56]}],
},
}
chartjs_chart(spec)
example_line_chart
def example_line_chart() -> None:
"""Multi-dataset line chart example."""
st.write("### Line Chart")
spec = {
"type": "line",
"data": {
"labels": ["Q1", "Q2", "Q3", "Q4"],
"datasets": [
{"label": "2024", "data": [10, 20, 15, 25]},
{"label": "2025", "data": [15, 25, 20, 30]},
],
},
"options": {
"plugins": {"title": {"display": True, "text": "Quarterly Revenue"}},
},
}
chartjs_chart(spec)
example_pie_chart
def example_pie_chart() -> None:
"""Pie chart example."""
st.write("### Pie Chart")
spec = {
"type": "pie",
"data": {
"labels": ["Red", "Blue", "Yellow", "Green"],
"datasets": [{"data": [30, 25, 20, 25]}],
},
}
chartjs_chart(spec)
example_radar_chart
def example_radar_chart() -> None:
"""Radar chart comparing multiple datasets."""
st.write("### Radar Chart")
spec = {
"type": "radar",
"data": {
"labels": ["Speed", "Reliability", "Comfort", "Safety", "Efficiency"],
"datasets": [
{"label": "Car A", "data": [65, 59, 90, 81, 56]},
{"label": "Car B", "data": [28, 48, 40, 19, 96]},
],
},
}
chartjs_chart(spec)
example_doughnut_chart
def example_doughnut_chart() -> None:
"""Doughnut chart example."""
st.write("### Doughnut Chart")
spec = {
"type": "doughnut",
"data": {
"labels": ["Desktop", "Mobile", "Tablet"],
"datasets": [{"data": [55, 35, 10]}],
},
"options": {
"plugins": {"title": {"display": True, "text": "Traffic by Device"}},
},
}
chartjs_chart(spec)