datasette-charts-pie 0.1a0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. datasette_charts_pie-0.1a0/LICENSE +201 -0
  2. datasette_charts_pie-0.1a0/PKG-INFO +143 -0
  3. datasette_charts_pie-0.1a0/README.md +125 -0
  4. datasette_charts_pie-0.1a0/datasette_charts_pie/__init__.py +19 -0
  5. datasette_charts_pie-0.1a0/datasette_charts_pie/base.py +173 -0
  6. datasette_charts_pie-0.1a0/datasette_charts_pie/pie.py +213 -0
  7. datasette_charts_pie-0.1a0/datasette_charts_pie/rose.py +196 -0
  8. datasette_charts_pie-0.1a0/datasette_charts_pie/static/arcs.js +334 -0
  9. datasette_charts_pie-0.1a0/datasette_charts_pie/static/draw.js +316 -0
  10. datasette_charts_pie-0.1a0/datasette_charts_pie/static/pie.js +166 -0
  11. datasette_charts_pie-0.1a0/datasette_charts_pie/static/rose.js +170 -0
  12. datasette_charts_pie-0.1a0/datasette_charts_pie/static/sunburst.js +172 -0
  13. datasette_charts_pie-0.1a0/datasette_charts_pie/sunburst.py +167 -0
  14. datasette_charts_pie-0.1a0/datasette_charts_pie.egg-info/PKG-INFO +143 -0
  15. datasette_charts_pie-0.1a0/datasette_charts_pie.egg-info/SOURCES.txt +23 -0
  16. datasette_charts_pie-0.1a0/datasette_charts_pie.egg-info/dependency_links.txt +1 -0
  17. datasette_charts_pie-0.1a0/datasette_charts_pie.egg-info/entry_points.txt +2 -0
  18. datasette_charts_pie-0.1a0/datasette_charts_pie.egg-info/requires.txt +2 -0
  19. datasette_charts_pie-0.1a0/datasette_charts_pie.egg-info/top_level.txt +1 -0
  20. datasette_charts_pie-0.1a0/pyproject.toml +49 -0
  21. datasette_charts_pie-0.1a0/setup.cfg +4 -0
  22. datasette_charts_pie-0.1a0/tests/test_arcs.py +284 -0
  23. datasette_charts_pie-0.1a0/tests/test_charts_pie.py +382 -0
  24. datasette_charts_pie-0.1a0/tests/test_rose.py +225 -0
  25. datasette_charts_pie-0.1a0/tests/test_sunburst.py +162 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,143 @@
1
+ Metadata-Version: 2.4
2
+ Name: datasette-charts-pie
3
+ Version: 0.1a0
4
+ Summary: Pie, donut, sunburst and rose charts for datasette-charts
5
+ Author: Datasette
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/datasette/datasette-charts-pie
8
+ Project-URL: Changelog, https://github.com/datasette/datasette-charts-pie/releases
9
+ Project-URL: Issues, https://github.com/datasette/datasette-charts-pie/issues
10
+ Project-URL: CI, https://github.com/datasette/datasette-charts-pie/actions
11
+ Classifier: Framework :: Datasette
12
+ Requires-Python: >=3.11
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: datasette>=1.0a44
16
+ Requires-Dist: datasette-charts>=0.4a0
17
+ Dynamic: license-file
18
+
19
+ # datasette-charts-pie
20
+
21
+ [![PyPI](https://img.shields.io/pypi/v/datasette-charts-pie.svg)](https://pypi.org/project/datasette-charts-pie/)
22
+ [![Changelog](https://img.shields.io/github/v/release/datasette/datasette-charts-pie?include_prereleases&label=changelog)](https://github.com/datasette/datasette-charts-pie/releases)
23
+ [![Tests](https://github.com/datasette/datasette-charts-pie/actions/workflows/test.yml/badge.svg)](https://github.com/datasette/datasette-charts-pie/actions/workflows/test.yml)
24
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/datasette/datasette-charts-pie/blob/main/LICENSE)
25
+
26
+ Pie, donut, sunburst and rose charts for [datasette-charts](https://charts.datasette.io/).
27
+
28
+ ## Installation
29
+
30
+ Install this plugin in the same environment as Datasette and datasette-charts.
31
+ ```bash
32
+ datasette install datasette-charts-pie
33
+ ```
34
+ ## Chart types
35
+
36
+ | Type | Shows | Suggested when |
37
+ |------|-------|----------------|
38
+ | `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 |
39
+ | `donut` | The same, with the total in the hole | Not suggested |
40
+ | `sunburst` | Two categories as two rings: each inner slice broken down by a second column | Not suggested |
41
+ | `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) |
42
+
43
+ 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.
44
+
45
+ ### `pie` and `donut`
46
+
47
+ <img src="screenshots/pie.png" width="580" alt="A pie chart of orders by status. Delivered is the largest slice, at 69%, running clockwise from 12 o'clock, then Shipped at 17%, Cancelled at 9% and Returned at 5%, whose share is written outside its thin slice with a short leader line. A legend on the right lists each status with its count and share: Delivered 2,079, Shipped 505, Cancelled 265 and Returned 151.">
48
+
49
+ [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.
50
+
51
+ <img src="screenshots/donut.png" width="580" alt="A donut chart of the total amount of orders by sales channel, with 164.9K and Total amount written in the hole. Website is 45%, Mobile app 28%, Marketplace 15%, Phone 8% and In store 4%, which is too thin for a label. The legend on the right gives each channel's total, from 74,313.87 for Website down to 6,248.85 for In store, and its share.">
52
+
53
+ [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.
54
+
55
+ ```json
56
+ {
57
+ "v": 1,
58
+ "type": "donut",
59
+ "encoding": {
60
+ "x": {"column": "primary_fuel", "limit": 6, "other": true},
61
+ "y": {"column": "capacity_mw", "aggregate": "sum"}
62
+ },
63
+ "options": {"labels": "percent", "sweep": "full", "center": "total"}
64
+ }
65
+ ```
66
+
67
+ - `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.
68
+ - `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.
69
+ - `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.
70
+ - `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.
71
+ - `sweep`: `full` (the default) or `half`, for a half circle from 9 o'clock to 3 o'clock.
72
+ - `center`, on a donut only: `total` (the default) writes the sum of the slices in the hole, and `none` leaves it empty.
73
+ - 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.
74
+
75
+ ### `sunburst`
76
+
77
+ <img src="screenshots/sunburst.png" width="580" alt="A sunburst chart of customers by country, then city. The inner ring has a slice for each country, in the series colors: United Kingdom 33%, United States 24%, Germany 14%, France 9%, Japan 9%, Brazil 5% and a gray Other. The outer ring breaks each country down by city, in paler tints of the country's color, largest first, with the names written along the radius where they fit: London, Manchester shortened to Manc…, Leeds and Bristol for the United Kingdom, New York, Chicago, Austin and Seattle for the United States, and so on. The legend on the right lists the countries with their counts and shares.">
78
+
79
+ [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.
80
+
81
+ ```json
82
+ {
83
+ "v": 1,
84
+ "type": "sunburst",
85
+ "encoding": {
86
+ "x": {"column": "country", "limit": 6},
87
+ "series": {"column": "city", "limit": 5},
88
+ "y": {"aggregate": "count"}
89
+ }
90
+ }
91
+ ```
92
+
93
+ - `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.
94
+ - `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".
95
+ - `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.
96
+ - The legend lists the inner ring. The outer ring is named by labels where the arcs are long enough, and by tooltips.
97
+ - 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.
98
+
99
+ ### `rose`
100
+
101
+ <img src="screenshots/rose.png" width="580" alt="A rose chart of orders by hour of the day. Twenty-four wedges run clockwise from midnight at the top, each reaching out as far as its number of orders, with hours labelled 00, 03, 06 and so on around the outside like a clock. The wedges are short overnight, from about 01:00 to 06:00, and longest around 12:00 and again from 19:00 to 21:00. Rings at 50, 100 and 150 orders are labelled up the 12 o'clock line, and Rows is written under the chart.">
102
+
103
+ [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.
104
+
105
+ ```json
106
+ {
107
+ "v": 1,
108
+ "type": "rose",
109
+ "encoding": {
110
+ "x": {"column": "started_at"},
111
+ "y": {"aggregate": "count"}
112
+ },
113
+ "options": {"cycle": "month"}
114
+ }
115
+ ```
116
+
117
+ - `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.
118
+ - `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.
119
+ - `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.
120
+ - 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.
121
+
122
+ ## Development
123
+
124
+ This plugin is developed against a checkout of datasette-charts next to it, set in `pyproject.toml`:
125
+
126
+ ```toml
127
+ [tool.uv.sources]
128
+ datasette-charts = { path = "../datasette-charts", editable = true }
129
+ ```
130
+
131
+ To run the tests:
132
+ ```bash
133
+ cd datasette-charts-pie
134
+ uv run pytest
135
+ ```
136
+ The tests of the drawing geometry in `static/arcs.js` run in node, and are skipped if node is not installed.
137
+
138
+ To try the charts on made-up data:
139
+ ```bash
140
+ uv run python demo/build_demo.py
141
+ uv run datasette demo/demo.db
142
+ ```
143
+ The `screenshots/` folder has the screenshots in this README, taken from the demo database.
@@ -0,0 +1,125 @@
1
+ # datasette-charts-pie
2
+
3
+ [![PyPI](https://img.shields.io/pypi/v/datasette-charts-pie.svg)](https://pypi.org/project/datasette-charts-pie/)
4
+ [![Changelog](https://img.shields.io/github/v/release/datasette/datasette-charts-pie?include_prereleases&label=changelog)](https://github.com/datasette/datasette-charts-pie/releases)
5
+ [![Tests](https://github.com/datasette/datasette-charts-pie/actions/workflows/test.yml/badge.svg)](https://github.com/datasette/datasette-charts-pie/actions/workflows/test.yml)
6
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/datasette/datasette-charts-pie/blob/main/LICENSE)
7
+
8
+ Pie, donut, sunburst and rose charts for [datasette-charts](https://charts.datasette.io/).
9
+
10
+ ## Installation
11
+
12
+ Install this plugin in the same environment as Datasette and datasette-charts.
13
+ ```bash
14
+ datasette install datasette-charts-pie
15
+ ```
16
+ ## Chart types
17
+
18
+ | Type | Shows | Suggested when |
19
+ |------|-------|----------------|
20
+ | `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 |
21
+ | `donut` | The same, with the total in the hole | Not suggested |
22
+ | `sunburst` | Two categories as two rings: each inner slice broken down by a second column | Not suggested |
23
+ | `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) |
24
+
25
+ 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.
26
+
27
+ ### `pie` and `donut`
28
+
29
+ <img src="screenshots/pie.png" width="580" alt="A pie chart of orders by status. Delivered is the largest slice, at 69%, running clockwise from 12 o'clock, then Shipped at 17%, Cancelled at 9% and Returned at 5%, whose share is written outside its thin slice with a short leader line. A legend on the right lists each status with its count and share: Delivered 2,079, Shipped 505, Cancelled 265 and Returned 151.">
30
+
31
+ [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.
32
+
33
+ <img src="screenshots/donut.png" width="580" alt="A donut chart of the total amount of orders by sales channel, with 164.9K and Total amount written in the hole. Website is 45%, Mobile app 28%, Marketplace 15%, Phone 8% and In store 4%, which is too thin for a label. The legend on the right gives each channel's total, from 74,313.87 for Website down to 6,248.85 for In store, and its share.">
34
+
35
+ [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.
36
+
37
+ ```json
38
+ {
39
+ "v": 1,
40
+ "type": "donut",
41
+ "encoding": {
42
+ "x": {"column": "primary_fuel", "limit": 6, "other": true},
43
+ "y": {"column": "capacity_mw", "aggregate": "sum"}
44
+ },
45
+ "options": {"labels": "percent", "sweep": "full", "center": "total"}
46
+ }
47
+ ```
48
+
49
+ - `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.
50
+ - `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.
51
+ - `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.
52
+ - `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.
53
+ - `sweep`: `full` (the default) or `half`, for a half circle from 9 o'clock to 3 o'clock.
54
+ - `center`, on a donut only: `total` (the default) writes the sum of the slices in the hole, and `none` leaves it empty.
55
+ - 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.
56
+
57
+ ### `sunburst`
58
+
59
+ <img src="screenshots/sunburst.png" width="580" alt="A sunburst chart of customers by country, then city. The inner ring has a slice for each country, in the series colors: United Kingdom 33%, United States 24%, Germany 14%, France 9%, Japan 9%, Brazil 5% and a gray Other. The outer ring breaks each country down by city, in paler tints of the country's color, largest first, with the names written along the radius where they fit: London, Manchester shortened to Manc…, Leeds and Bristol for the United Kingdom, New York, Chicago, Austin and Seattle for the United States, and so on. The legend on the right lists the countries with their counts and shares.">
60
+
61
+ [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.
62
+
63
+ ```json
64
+ {
65
+ "v": 1,
66
+ "type": "sunburst",
67
+ "encoding": {
68
+ "x": {"column": "country", "limit": 6},
69
+ "series": {"column": "city", "limit": 5},
70
+ "y": {"aggregate": "count"}
71
+ }
72
+ }
73
+ ```
74
+
75
+ - `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.
76
+ - `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".
77
+ - `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.
78
+ - The legend lists the inner ring. The outer ring is named by labels where the arcs are long enough, and by tooltips.
79
+ - 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.
80
+
81
+ ### `rose`
82
+
83
+ <img src="screenshots/rose.png" width="580" alt="A rose chart of orders by hour of the day. Twenty-four wedges run clockwise from midnight at the top, each reaching out as far as its number of orders, with hours labelled 00, 03, 06 and so on around the outside like a clock. The wedges are short overnight, from about 01:00 to 06:00, and longest around 12:00 and again from 19:00 to 21:00. Rings at 50, 100 and 150 orders are labelled up the 12 o'clock line, and Rows is written under the chart.">
84
+
85
+ [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.
86
+
87
+ ```json
88
+ {
89
+ "v": 1,
90
+ "type": "rose",
91
+ "encoding": {
92
+ "x": {"column": "started_at"},
93
+ "y": {"aggregate": "count"}
94
+ },
95
+ "options": {"cycle": "month"}
96
+ }
97
+ ```
98
+
99
+ - `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.
100
+ - `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.
101
+ - `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.
102
+ - 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.
103
+
104
+ ## Development
105
+
106
+ This plugin is developed against a checkout of datasette-charts next to it, set in `pyproject.toml`:
107
+
108
+ ```toml
109
+ [tool.uv.sources]
110
+ datasette-charts = { path = "../datasette-charts", editable = true }
111
+ ```
112
+
113
+ To run the tests:
114
+ ```bash
115
+ cd datasette-charts-pie
116
+ uv run pytest
117
+ ```
118
+ The tests of the drawing geometry in `static/arcs.js` run in node, and are skipped if node is not installed.
119
+
120
+ To try the charts on made-up data:
121
+ ```bash
122
+ uv run python demo/build_demo.py
123
+ uv run datasette demo/demo.db
124
+ ```
125
+ The `screenshots/` folder has the screenshots in this README, taken from the demo database.
@@ -0,0 +1,19 @@
1
+ # Importing datasette_charts first registers its register_chart_types hookspec
2
+ import datasette_charts # noqa: F401
3
+ from datasette import hookimpl
4
+
5
+ from .base import PLUGIN_NAME, configured
6
+ from .pie import DonutChart, PieChart
7
+ from .rose import RoseChart
8
+ from .sunburst import SunburstChart
9
+
10
+ CHART_TYPES = [PieChart, DonutChart, SunburstChart, RoseChart]
11
+
12
+ __all__ = ["CHART_TYPES", "DonutChart", "PieChart", "RoseChart", "SunburstChart"]
13
+
14
+
15
+ @hookimpl
16
+ def register_chart_types(datasette):
17
+ config = datasette.plugin_config(PLUGIN_NAME) or {}
18
+ suggest = bool(config.get("suggest", True))
19
+ return [configured(cls, suggest) for cls in CHART_TYPES]