# datasette-charts-pie
[](https://pypi.org/project/datasette-charts-pie/)
[](https://github.com/datasette/datasette-charts-pie/releases)
[](https://github.com/datasette/datasette-charts-pie/actions/workflows/test.yml)
[](https://github.com/datasette/datasette-charts-pie/blob/main/LICENSE)
Pie, donut, sunburst and rose charts for [datasette-charts](https://charts.datasette.io/).
## Installation
Install this plugin in the same environment as Datasette and datasette-charts.
```bash
datasette install datasette-charts-pie
```
## Chart types
| Type | Shows | Suggested when |
|------|-------|----------------|
| `pie` | Each value of a category as a share of the whole: the top slices plus "Other" | A category with 3 to 6 values, none of them too dominant or too rare. Always ranked below the bar chart of the same column |
| `donut` | The same, with the total in the hole | Not suggested |
| `sunburst` | Two categories as two rings: each inner slice broken down by a second column | Not suggested |
| `rose` | A count or an aggregate around a cycle: month of the year, day of the week or hour of the day | A date column that spans at least 2 years (months), 8 weeks (weekdays) or 7 days (hours, not for dates with no time of day) |
All of these chart types can be constructed by hand in the chart builder. Pie and Rose charts will be suggested if the data matches the above criteria.
### `pie` and `donut`
[Live demo](https://lite.datasette.io/?install=datasette-charts-pie#/content/pypi_packages#chart=%7B%22v%22%3A1%2C%22type%22%3A%22pie%22%2C%22encoding%22%3A%7B%22x%22%3A%7B%22column%22%3A%22license%22%2C%22limit%22%3A10%2C%22other%22%3Atrue%7D%2C%22y%22%3A%7B%22aggregate%22%3A%22count%22%7D%7D%2C%22options%22%3A%7B%22labels%22%3A%22percent%22%2C%22sweep%22%3A%22full%22%7D%7D): PyPI packages by license, in Datasette Lite.
[Live demo](https://lite.datasette.io/?install=datasette-charts-pie#/content/stats#chart=%7B%22v%22%3A1%2C%22type%22%3A%22donut%22%2C%22encoding%22%3A%7B%22x%22%3A%7B%22column%22%3A%22package%22%2C%22limit%22%3A6%2C%22other%22%3Atrue%7D%2C%22y%22%3A%7B%22column%22%3A%22downloads%22%2C%22aggregate%22%3A%22sum%22%7D%7D%2C%22options%22%3A%7B%22labels%22%3A%22percent%22%2C%22sweep%22%3A%22full%22%2C%22center%22%3A%22total%22%7D%7D): total PyPI downloads by package.
```json
{
"v": 1,
"type": "donut",
"encoding": {
"x": {"column": "primary_fuel", "limit": 6, "other": true},
"y": {"column": "capacity_mw", "aggregate": "sum"}
},
"options": {"labels": "percent", "sweep": "full", "center": "total"}
}
```
- `x`: the slices, a category. `limit` defaults to 6. At most 8 slices are drawn, as core has eight series colors: a larger `limit` is treated as 8. The rest are always summed into an "Other" slice, and `other: false` is ignored, since leaving out the small slices would make the rest look like the whole.
- `sort`: slices run clockwise from 12 o'clock, largest first, with "Other" last. `key` or `-key` orders them by value instead, for categories with a natural order.
- `y`: optional, a count of rows (the default) or `sum` of a column. Averages and the other aggregates are not shares of anything, so they are not offered.
- `labels`: `percent` (the default), `value` or `none`. A label goes inside its slice when it fits, outside with a short leader when the slice is too thin but at least 4% of the whole, and nowhere below that. The legend always has every name, value and share.
- `sweep`: `full` (the default) or `half`, for a half circle from 9 o'clock to 3 o'clock.
- `center`, on a donut only: `total` (the default) writes the sum of the slices in the hole, and `none` leaves it empty.
- A slice whose value is zero or null is left out. If any group's sum is below zero there is no whole to take shares of, and the chart says so.
### `sunburst`
[Live demo](https://lite.datasette.io/?install=datasette-charts-pie#/content/releases#chart=%7B%22v%22%3A1%2C%22type%22%3A%22sunburst%22%2C%22encoding%22%3A%7B%22x%22%3A%7B%22column%22%3A%22author%22%2C%22limit%22%3A6%7D%2C%22series%22%3A%7B%22column%22%3A%22repo%22%2C%22limit%22%3A5%7D%2C%22y%22%3A%7B%22aggregate%22%3A%22count%22%7D%7D%7D): GitHub releases by author, then repository.
```json
{
"v": 1,
"type": "sunburst",
"encoding": {
"x": {"column": "country", "limit": 6},
"series": {"column": "city", "limit": 5},
"y": {"aggregate": "count"}
}
}
```
- `x`: the inner ring, a category. `limit` defaults to 6, at most 8, with the rest in one "Other" slice that is not broken down.
- `series`: the outer ring, a different column. Its values are ranked within each inner slice, so each country gets its own largest cities. `limit` defaults to 5 for each inner slice, at most 8, with the rest of each in a pale "Other".
- `y`: optional, a count of rows (the default) or `sum` of a column. Cells whose sum is zero or below are left out, so a sum of a column with negative values is not supported.
- The legend lists the inner ring. The outer ring is named by labels where the arcs are long enough, and by tooltips.
- It is best for columns that really nest, such as city inside country. For two columns that cut across each other, a stacked bar chart is easier to read.
### `rose`
[Live demo](https://lite.datasette.io/?install=datasette-charts-pie#/content/releases#chart=%7B%22v%22%3A1%2C%22type%22%3A%22rose%22%2C%22encoding%22%3A%7B%22x%22%3A%7B%22column%22%3A%22published_at%22%7D%2C%22y%22%3A%7B%22aggregate%22%3A%22count%22%7D%7D%2C%22options%22%3A%7B%22cycle%22%3A%22hour%22%7D%7D): GitHub releases by hour of the day, in UTC.
```json
{
"v": 1,
"type": "rose",
"encoding": {
"x": {"column": "started_at"},
"y": {"aggregate": "count"}
},
"options": {"cycle": "month"}
}
```
- `x`: a date or time column: ISO dates or datetimes, or Unix seconds or milliseconds, as the column's profile says. Times are read as UTC, as in core.
- `cycle`: `month` (the default), `weekday` (Monday first) or `hour`. A column of dates has no hours, and a column of years has none of these, so those are errors.
- `y`: optional, a count of rows (the default) or any aggregate of a column: `sum`, `avg`, `min`, `max` or `count_distinct`. A rose shows magnitude, not shares, so an average for each month is fine. A chart with any value below zero shows a message instead.
- Every step of the cycle gets a wedge of the same angle, so a month with no rows is an empty wedge. A wedge's radius grows with the square root of its value, so the area it fills is in proportion to it.
## Development
This plugin is developed against a checkout of datasette-charts next to it, set in `pyproject.toml`:
```toml
[tool.uv.sources]
datasette-charts = { path = "../datasette-charts", editable = true }
```
To run the tests:
```bash
cd datasette-charts-pie
uv run pytest
```
The tests of the drawing geometry in `static/arcs.js` run in node, and are skipped if node is not installed.
To try the charts on made-up data:
```bash
uv run python demo/build_demo.py
uv run datasette demo/demo.db
```
The `screenshots/` folder has the screenshots in this README, taken from the demo database.