DONUT
Excel Usage
=DONUT(data, title, color_map, hole_size, legend)
data(list[list], required): Input data (Labels, Values).title(str, optional, default: null): Chart title.color_map(str, optional, default: “viridis”): Color map for slices.hole_size(float, optional, default: 0.5): Size of the donut hole (0-1).legend(str, optional, default: “true”): Show legend.
Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
Examples
Example 1: Simple donut chart with 3 categories
Inputs:
| data | |
|---|---|
| A | 30 |
| B | 50 |
| C | 20 |
Excel formula:
=DONUT({"A",30;"B",50;"C",20})
Expected output:
"chart"
Example 2: Donut chart with title and legend
Inputs:
| data | title | legend | |
|---|---|---|---|
| Q1 | 100 | Quarterly Sales | true |
| Q2 | 150 | ||
| Q3 | 120 | ||
| Q4 | 180 |
Excel formula:
=DONUT({"Q1",100;"Q2",150;"Q3",120;"Q4",180}, "Quarterly Sales", "true")
Expected output:
"chart"
Example 3: Donut with plasma colormap and small hole
Inputs:
| data | color_map | hole_size | |
|---|---|---|---|
| Category A | 25 | plasma | 0.3 |
| Category B | 35 | ||
| Category C | 40 |
Excel formula:
=DONUT({"Category A",25;"Category B",35;"Category C",40}, "plasma", 0.3)
Expected output:
"chart"
Example 4: Donut with large center hole
Inputs:
| data | hole_size | |
|---|---|---|
| Item 1 | 60 | 0.7 |
| Item 2 | 40 |
Excel formula:
=DONUT({"Item 1",60;"Item 2",40}, 0.7)
Expected output:
"chart"
Python Code
import sys
import matplotlib
IS_PYODIDE = sys.platform == "emscripten"
if IS_PYODIDE:
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import io
import base64
import numpy as np
def donut(data, title=None, color_map='viridis', hole_size=0.5, legend='true'):
"""
Create a donut chart from data.
See: https://matplotlib.org/stable/gallery/pie_and_polar_charts/pie_and_donut_labels.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): Input data (Labels, Values).
title (str, optional): Chart title. Default is None.
color_map (str, optional): Color map for slices. Valid options: Viridis, Plasma, Inferno, Magma, Cividis. Default is 'viridis'.
hole_size (float, optional): Size of the donut hole (0-1). Default is 0.5.
legend (str, optional): Show legend. Valid options: True, False. Default is 'true'.
Returns:
object: Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
"""
def to2d(x):
return [[x]] if not isinstance(x, list) else x
try:
data = to2d(data)
if not isinstance(data, list) or len(data) < 1:
return "Error: Data must be a non-empty list"
# Extract labels and values
labels = []
values = []
for row in data:
if not isinstance(row, list) or len(row) < 2:
continue
try:
labels.append(str(row[0]))
values.append(float(row[1]))
except (ValueError, TypeError):
continue
if len(labels) == 0 or len(values) == 0:
return "Error: No valid data rows found"
if any(v < 0 for v in values):
return "Error: Values must be non-negative"
# Create figure
fig, ax = plt.subplots(figsize=(8, 6))
# Create donut chart
wedges, texts, autotexts = ax.pie(
values,
labels=labels,
autopct='%1.1f%%',
startangle=90,
wedgeprops=dict(width=1-hole_size),
colors=plt.cm.get_cmap(color_map)(np.linspace(0, 1, len(values)))
)
# Set title
if title:
ax.set_title(title)
# Handle legend
if legend == "true":
ax.legend(labels, loc="best")
# Equal aspect ratio ensures circular shape
ax.axis('equal')
if IS_PYODIDE:
buf = io.BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight')
plt.close(fig)
buf.seek(0)
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
return f"data:image/png;base64,{img_base64}"
else:
return fig
except Exception as e:
return f"Error: {str(e)}"