Part-to-whole at a glance. Capped at a handful of slices by the API's `limit`.
(data, { scale = 'categorical' } = {})
| 201 | |
| 202 | /** Part-to-whole at a glance. Capped at a handful of slices by the API's `limit`. */ |
| 203 | function pieChart(data, { scale = 'categorical' } = {}) { |
| 204 | const total = data.datasets[0].data.reduce((n, v) => n + v, 0); |
| 205 | return { |
| 206 | type: 'pie', |
| 207 | data: { |
| 208 | labels: data.labels, |
| 209 | datasets: [ |
| 210 | { |
| 211 | label: data.datasets[0].label, |
| 212 | data: data.datasets[0].data, |
| 213 | backgroundColor: paletteFor(data.labels, scale === 'ordinal' ? 'ordinal' : 'categorical'), |
| 214 | }, |
| 215 | ], |
| 216 | }, |
| 217 | options: { |
| 218 | plugins: { |
| 219 | legend: { display: true }, |
| 220 | tooltip: { |
| 221 | callbacks: { |
| 222 | label: (ctx) => |
| 223 | `${ctx.label}: ${number(ctx.parsed)} (${total > 0 ? percent(ctx.parsed / total, 1) : '—'})`, |
| 224 | }, |
| 225 | }, |
| 226 | }, |
| 227 | }, |
| 228 | }; |
| 229 | } |
| 230 | |
| 231 | // --------------------------------------------------------------------------- |
| 232 | // Table twins |
no test coverage detected