@ember-joinery/chart 0.0.0-stage → 1.1.0

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 (121) hide show
  1. package/CHANGELOG.md +189 -0
  2. package/LICENSE.md +9 -0
  3. package/NOTICE +532 -0
  4. package/README.md +69 -2
  5. package/addon-main.cjs +4 -0
  6. package/declarations/-private/cartesian-chart.d.ts +413 -0
  7. package/declarations/-private/cartesian-chart.d.ts.map +1 -0
  8. package/declarations/-private/cartesian.d.ts +236 -0
  9. package/declarations/-private/cartesian.d.ts.map +1 -0
  10. package/declarations/-private/config.d.ts +13 -0
  11. package/declarations/-private/config.d.ts.map +1 -0
  12. package/declarations/-private/dots.d.ts +27 -0
  13. package/declarations/-private/dots.d.ts.map +1 -0
  14. package/declarations/-private/nice-ticks.d.ts +17 -0
  15. package/declarations/-private/nice-ticks.d.ts.map +1 -0
  16. package/declarations/-private/parts.d.ts +596 -0
  17. package/declarations/-private/parts.d.ts.map +1 -0
  18. package/declarations/-private/paths.d.ts +26 -0
  19. package/declarations/-private/paths.d.ts.map +1 -0
  20. package/declarations/-private/pie-chart.d.ts +244 -0
  21. package/declarations/-private/pie-chart.d.ts.map +1 -0
  22. package/declarations/-private/polar-chart.d.ts +423 -0
  23. package/declarations/-private/polar-chart.d.ts.map +1 -0
  24. package/declarations/-private/polar-geometry.d.ts +230 -0
  25. package/declarations/-private/polar-geometry.d.ts.map +1 -0
  26. package/declarations/-private/polar-parts.d.ts +315 -0
  27. package/declarations/-private/polar-parts.d.ts.map +1 -0
  28. package/declarations/-private/polar.d.ts +156 -0
  29. package/declarations/-private/polar.d.ts.map +1 -0
  30. package/declarations/-private/registry.d.ts +9 -0
  31. package/declarations/-private/registry.d.ts.map +1 -0
  32. package/declarations/-private/text-size.d.ts +13 -0
  33. package/declarations/-private/text-size.d.ts.map +1 -0
  34. package/declarations/-private/tick-interval.d.ts +35 -0
  35. package/declarations/-private/tick-interval.d.ts.map +1 -0
  36. package/declarations/components/area-chart.d.ts +16 -0
  37. package/declarations/components/area-chart.d.ts.map +1 -0
  38. package/declarations/components/bar-chart.d.ts +16 -0
  39. package/declarations/components/bar-chart.d.ts.map +1 -0
  40. package/declarations/components/chart-container.d.ts +66 -0
  41. package/declarations/components/chart-container.d.ts.map +1 -0
  42. package/declarations/components/chart-legend-content.d.ts +42 -0
  43. package/declarations/components/chart-legend-content.d.ts.map +1 -0
  44. package/declarations/components/chart-style.d.ts +19 -0
  45. package/declarations/components/chart-style.d.ts.map +1 -0
  46. package/declarations/components/chart-tooltip-content.d.ts +94 -0
  47. package/declarations/components/chart-tooltip-content.d.ts.map +1 -0
  48. package/declarations/components/dot.d.ts +22 -0
  49. package/declarations/components/dot.d.ts.map +1 -0
  50. package/declarations/components/line-chart.d.ts +16 -0
  51. package/declarations/components/line-chart.d.ts.map +1 -0
  52. package/declarations/components/pie-chart.d.ts +13 -0
  53. package/declarations/components/pie-chart.d.ts.map +1 -0
  54. package/declarations/components/radar-chart.d.ts +21 -0
  55. package/declarations/components/radar-chart.d.ts.map +1 -0
  56. package/declarations/components/radial-bar-chart.d.ts +17 -0
  57. package/declarations/components/radial-bar-chart.d.ts.map +1 -0
  58. package/declarations/components/rectangle.d.ts +32 -0
  59. package/declarations/components/rectangle.d.ts.map +1 -0
  60. package/declarations/components/sector.d.ts +47 -0
  61. package/declarations/components/sector.d.ts.map +1 -0
  62. package/declarations/index.d.ts +24 -0
  63. package/declarations/index.d.ts.map +1 -0
  64. package/declarations/test-support/chart.d.ts +162 -0
  65. package/declarations/test-support/chart.d.ts.map +1 -0
  66. package/declarations/test-support/index.d.ts +3 -0
  67. package/declarations/test-support/index.d.ts.map +1 -0
  68. package/declarations/test-support/polar-chart.d.ts +62 -0
  69. package/declarations/test-support/polar-chart.d.ts.map +1 -0
  70. package/declarations/test-support.d.ts +2 -0
  71. package/declarations/types.d.ts +138 -0
  72. package/declarations/types.d.ts.map +1 -0
  73. package/dist/cartesian-D9hWDamz.js +1217 -0
  74. package/dist/cartesian-D9hWDamz.js.map +1 -0
  75. package/dist/cartesian-chart-BLj6Q_OB.js +1123 -0
  76. package/dist/cartesian-chart-BLj6Q_OB.js.map +1 -0
  77. package/dist/components/area-chart.js +20 -0
  78. package/dist/components/area-chart.js.map +1 -0
  79. package/dist/components/bar-chart.js +20 -0
  80. package/dist/components/bar-chart.js.map +1 -0
  81. package/dist/components/chart-container.js +54 -0
  82. package/dist/components/chart-container.js.map +1 -0
  83. package/dist/components/chart-legend-content.js +48 -0
  84. package/dist/components/chart-legend-content.js.map +1 -0
  85. package/dist/components/chart-style.js +35 -0
  86. package/dist/components/chart-style.js.map +1 -0
  87. package/dist/components/chart-tooltip-content.js +131 -0
  88. package/dist/components/chart-tooltip-content.js.map +1 -0
  89. package/dist/components/dot.js +18 -0
  90. package/dist/components/dot.js.map +1 -0
  91. package/dist/components/line-chart.js +20 -0
  92. package/dist/components/line-chart.js.map +1 -0
  93. package/dist/components/pie-chart.js +6 -0
  94. package/dist/components/pie-chart.js.map +1 -0
  95. package/dist/components/radar-chart.js +22 -0
  96. package/dist/components/radar-chart.js.map +1 -0
  97. package/dist/components/radial-bar-chart.js +20 -0
  98. package/dist/components/radial-bar-chart.js.map +1 -0
  99. package/dist/components/rectangle.js +5 -0
  100. package/dist/components/rectangle.js.map +1 -0
  101. package/dist/components/sector.js +5 -0
  102. package/dist/components/sector.js.map +1 -0
  103. package/dist/config-DnsUmxOT.js +35 -0
  104. package/dist/config-DnsUmxOT.js.map +1 -0
  105. package/dist/index.js +15 -0
  106. package/dist/index.js.map +1 -0
  107. package/dist/parts-BWLhFFjB.js +355 -0
  108. package/dist/parts-BWLhFFjB.js.map +1 -0
  109. package/dist/pie-chart-CmDtW_Ge.js +646 -0
  110. package/dist/pie-chart-CmDtW_Ge.js.map +1 -0
  111. package/dist/polar-chart-GhgOSsH9.js +2242 -0
  112. package/dist/polar-chart-GhgOSsH9.js.map +1 -0
  113. package/dist/rectangle-Cl7tbXuZ.js +166 -0
  114. package/dist/rectangle-Cl7tbXuZ.js.map +1 -0
  115. package/dist/sector-7shQPJuQ.js +565 -0
  116. package/dist/sector-7shQPJuQ.js.map +1 -0
  117. package/dist/test-support.js +470 -0
  118. package/dist/test-support.js.map +1 -0
  119. package/dist/types.js +56 -0
  120. package/dist/types.js.map +1 -0
  121. package/package.json +117 -3
@@ -0,0 +1,646 @@
1
+ /*!
2
+ * @ember-joinery/chart ports recharts 3.8.0 (https://github.com/recharts/recharts): its tick
3
+ * selection, nice tick values, text measurement, cartesian, pie and polar layout, tooltip
4
+ * placement, and rectangle, sector and polygon paths.
5
+ *
6
+ * The MIT License (MIT). Copyright (c) 2015-present recharts
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
9
+ * and associated documentation files (the "Software"), to deal in the Software without
10
+ * restriction, including without limitation the rights to use, copy, modify, merge, publish,
11
+ * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
12
+ * Software is furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all copies or
15
+ * substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
18
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+ import { isNumber, isString, isFunction } from '@sindresorhus/is';
24
+ import Component from '@glimmer/component';
25
+ import { tracked, cached } from '@glimmer/tracking';
26
+ import { hash } from '@ember/helper';
27
+ import { on } from '@ember/modifier';
28
+ import { htmlSafe } from '@ember/template';
29
+ import { modifier } from 'ember-modifier';
30
+ import { idFor } from '@ember-joinery/primitives/id';
31
+ import { R as Registry, P as Pie, C as ChartTooltip, a as ChartLegend } from './parts-BWLhFFjB.js';
32
+ import { p as plotArea, d as defaultAxis, v as valueOf } from './cartesian-D9hWDamz.js';
33
+ import { o as observeSize, t as tooltipStyleFor, l as legendStyleFor } from './cartesian-chart-BLj6Q_OB.js';
34
+ import { l as linePath } from './rectangle-Cl7tbXuZ.js';
35
+ import { S as Sector, p as pieSectors, P as PIE_DEFAULTS, a as percentOf, d as drawnSectorAngles, b as pieLabelPlacement, c as polarLabelPlacement, i as inSector } from './sector-7shQPJuQ.js';
36
+ import ChartTooltipContent from './components/chart-tooltip-content.js';
37
+ import ChartLegendContent from './components/chart-legend-content.js';
38
+ import { precompileTemplate } from '@ember/template-compilation';
39
+ import { setComponentTemplate } from '@ember/component';
40
+ import templateOnly from '@ember/component/template-only';
41
+ import { g, i, n } from 'decorator-transforms/runtime';
42
+
43
+ const INITIAL_DIMENSION = {
44
+ width: -1,
45
+ height: -1
46
+ };
47
+ const DEFAULT_MARGIN = {
48
+ top: 5,
49
+ right: 5,
50
+ bottom: 5,
51
+ left: 5
52
+ };
53
+ function num(value, fallback) {
54
+ return isNumber(value) && Number.isFinite(value) ? value : fallback;
55
+ }
56
+ function keyOf(dataKey) {
57
+ return isFunction(dataKey) ? '' : String(dataKey ?? '');
58
+ }
59
+ function round(value) {
60
+ return Math.round(value * 1000) / 1000;
61
+ }
62
+ function textOf(value) {
63
+ return value === null || value === undefined ? '' : String(value);
64
+ }
65
+ /**
66
+ * The chart at its size: recharts' wrapper, its `<svg>` with the pies and their labels in
67
+ * recharts' z-index layers, and the legend and tooltip boxes over it.
68
+ */
69
+ const Surface = setComponentTemplate(precompileTemplate("<div class=\"recharts-wrapper\" style={{@chart.wrapperStyle}} data-test-jn={{unless @chart.args.inContainer \"chart\"}} data-test-jn-name={{unless @chart.args.inContainer @chart.args.name}} data-test-jn-part=\"wrapper\" data-layout=\"centric\" {{on \"mousemove\" @chart.onMouseMove}} {{on \"mouseleave\" @chart.onMouseLeave}} ...attributes>\n <svg class=\"recharts-surface\" width={{@chart.width}} height={{@chart.height}} viewBox={{@chart.viewBox}} style=\"width: 100%; height: 100%; display: block;\" role={{if @chart.accessibilityLayer \"application\"}} tabindex={{if @chart.accessibilityLayer \"0\"}} data-test-jn-part=\"surface\" {{on \"focus\" @chart.onFocus}} {{on \"blur\" @chart.onBlur}} {{on \"keydown\" @chart.onKeyDown}}>\n <title>{{@chart.args.title}}</title>\n <desc>{{@chart.args.desc}}</desc>\n {{!-- The block registers the chart's parts. Nothing above it may read them (see Registry). --}}\n {{yield}}\n {{#if @chart.geometry}}\n <defs>\n <clipPath id={{@chart.clipId}}>\n <rect data-test-jn-part=\"plot\" x={{@chart.geometry.offset.left}} y={{@chart.geometry.offset.top}} height={{@chart.geometry.offset.height}} width={{@chart.geometry.offset.width}}></rect>\n </clipPath>\n </defs>\n <g class=\"recharts-zIndex-layer_100\">\n {{#each @chart.pies as |pie|}}\n <g class=\"recharts-layer recharts-pie\" data-test-jn-part=\"series\" data-test-jn-value={{pie.key}} data-series-type=\"pie\" data-test-jn-points={{pie.points}}>\n <g class=\"recharts-layer\">\n {{#each pie.sectors key=\"index\" as |sector|}}\n <g class=\"recharts-layer recharts-pie-sector\">\n <g class={{@chart.sectorClass pie.index sector.index}}>\n {{#if pie.shape}}\n {{#let pie.shape as |Shape|}}\n <Shape @props={{@chart.sectorProps pie.index sector}} @index={{sector.index}} />\n {{/let}}\n {{else}}\n <Sector @props={{@chart.sectorProps pie.index sector}} />\n {{/if}}\n </g>\n </g>\n {{/each}}\n </g>\n {{#if @chart.polarViewBox}}\n {{#each pie.contents as |content|}}\n {{#let content.component as |Content|}}\n <Content @viewBox={{@chart.polarViewBox}} @value={{content.value}} @offset={{content.offset}} @position={{content.position}} />\n {{/let}}\n {{/each}}\n {{/if}}\n </g>\n {{/each}}\n </g>\n <g class=\"recharts-zIndex-layer_2000\">\n {{#each @chart.pies as |pie|}}\n {{#if pie.labels.length}}\n <g class=\"recharts-layer recharts-pie-labels\" data-test-jn-part=\"labels\" data-test-jn-value={{pie.key}}>\n {{#each pie.labels as |label|}}\n <g class=\"recharts-layer\">\n {{#if label.line}}\n <path class=\"recharts-curve recharts-pie-label-line\" fill=\"none\" stroke={{label.line.stroke}} stroke-width={{label.line.strokeWidth}} d={{label.line.d}} data-test-jn-part=\"label-line\"></path>\n {{/if}}\n {{#if label.component}}\n {{#let label.component as |Label|}}\n <Label @props={{label.props}} @index={{label.index}} />\n {{/let}}\n {{else}}\n <text class=\"recharts-text recharts-pie-label-text\" stroke=\"none\" alignment-baseline=\"middle\" x={{label.props.x}} y={{label.props.y}} text-anchor={{label.props.textAnchor}} fill={{label.props.fill}} data-test-jn-part=\"label\"><tspan x={{label.props.x}} dy=\"0em\">{{label.text}}</tspan></text>\n {{/if}}\n </g>\n {{/each}}\n </g>\n {{/if}}\n {{#each pie.labelLists as |list|}}\n <g class=\"recharts-layer recharts-label-list\" data-test-jn-part=\"labels\" data-test-jn-value={{pie.key}}>\n {{#each list.items as |item|}}\n <text class={{list.class}} stroke={{list.stroke}} font-size={{list.fontSize}} fill-opacity={{list.fillOpacity}} x={{item.x}} y={{item.y}} text-anchor={{item.anchor}} fill={{item.fill}} data-test-jn-part=\"label\"><tspan x={{item.x}} dy={{item.dy}}>{{item.text}}</tspan></text>\n {{/each}}\n </g>\n {{/each}}\n {{#each pie.texts as |item|}}\n <text class={{item.class}} font-size={{item.fontSize}} x={{item.x}} y={{item.y}} text-anchor={{item.anchor}} fill={{item.fill}} data-test-jn-part=\"label\"><tspan x={{item.x}} dy={{item.dy}}>{{item.text}}</tspan></text>\n {{/each}}\n {{/each}}\n </g>\n {{/if}}\n </svg>\n {{#if @chart.legendPart}}\n <div class=\"recharts-legend-wrapper\" style={{@chart.legendStyle}} {{@chart.measureLegend}}>\n {{#let @chart.legendContent as |Content|}}\n {{!-- The legend's @class goes to its content only when given (see the cartesian chart). --}}\n {{#if @chart.legendPart.args.class}}\n <Content @payload={{@chart.legendPayload}} @verticalAlign={{@chart.legendVerticalAlign}} @config={{@chart.args.config}} @class={{@chart.legendPart.args.class}} />\n {{else}}\n <Content @payload={{@chart.legendPayload}} @verticalAlign={{@chart.legendVerticalAlign}} @config={{@chart.args.config}} />\n {{/if}}\n {{/let}}\n </div>\n {{/if}}\n {{#if @chart.tooltipPart}}\n <div class=\"recharts-tooltip-wrapper\" tabindex=\"-1\" style={{@chart.tooltipStyle}} {{@chart.measureTooltip @chart.isTooltipActive @chart.tooltipKey}}>\n {{#let @chart.tooltipContent as |Content|}}\n <Content @active={{@chart.isTooltipActive}} @payload={{@chart.tooltipPayload}} @config={{@chart.args.config}} @coordinate={{@chart.tooltipCoordinate}} @tooltip={{@chart.tooltipProps}} />\n {{/let}}\n </div>\n {{/if}}\n</div>", {
70
+ strictMode: true,
71
+ scope: () => ({
72
+ on,
73
+ Sector
74
+ })
75
+ }), templateOnly());
76
+ class PieChart extends Component {
77
+ #parts = new Registry();
78
+ chartId = idFor(this, 'chart');
79
+ static {
80
+ g(this.prototype, "containerSize", [tracked], function () {
81
+ return null;
82
+ });
83
+ }
84
+ #containerSize_ = (i(this, "containerSize"), void 0);
85
+ static {
86
+ g(this.prototype, "legendBox", [tracked], function () {
87
+ return null;
88
+ });
89
+ }
90
+ #legendBox_ = (i(this, "legendBox"), void 0);
91
+ static {
92
+ g(this.prototype, "tooltipBox", [tracked], function () {
93
+ return null;
94
+ });
95
+ }
96
+ #tooltipBox_ = (i(this, "tooltipBox"), void 0);
97
+ static {
98
+ g(this.prototype, "pointerItem", [tracked], function () {
99
+ return null;
100
+ });
101
+ }
102
+ #pointerItem = (i(this, "pointerItem"), void 0);
103
+ /** The sector under the pointer. */
104
+ static {
105
+ g(this.prototype, "keyboardIndex", [tracked], function () {
106
+ return null;
107
+ });
108
+ }
109
+ #keyboardIndex = (i(this, "keyboardIndex"), void 0);
110
+ /** The sector the arrow keys are on, counting every pie's sectors in turn. */
111
+ static {
112
+ g(this.prototype, "keyboardActive", [tracked], function () {
113
+ return false;
114
+ });
115
+ }
116
+ #keyboardActive = (i(this, "keyboardActive"), void 0);
117
+ register(part) {
118
+ this.#parts.add(part);
119
+ }
120
+ unregister(part) {
121
+ this.#parts.remove(part);
122
+ }
123
+ // ---------------------------------------------------------------------------------------
124
+ // Size
125
+ get fixedSize() {
126
+ return isNumber(this.args.width) && isNumber(this.args.height);
127
+ }
128
+ get size() {
129
+ if (this.fixedSize) return {
130
+ width: this.args.width,
131
+ height: this.args.height
132
+ };
133
+ const size = this.containerSize ?? this.args.initialDimension ?? INITIAL_DIMENSION;
134
+ if (!(size.width > 0) || !(size.height > 0)) return null;
135
+ return size;
136
+ }
137
+ #containerSize = null;
138
+ #legendBox = null;
139
+ #tooltipBox = null;
140
+ measureContainer = modifier(element => observeSize(this, element, ({
141
+ width,
142
+ height
143
+ }) => {
144
+ const next = {
145
+ width: Math.round(width),
146
+ height: Math.round(height)
147
+ };
148
+ const current = this.#containerSize;
149
+ if (!current || current.width !== next.width || current.height !== next.height) {
150
+ this.#containerSize = next;
151
+ this.containerSize = next;
152
+ }
153
+ }));
154
+ get width() {
155
+ return this.size?.width ?? 0;
156
+ }
157
+ get height() {
158
+ return this.size?.height ?? 0;
159
+ }
160
+ get viewBox() {
161
+ return `0 0 ${this.width} ${this.height}`;
162
+ }
163
+ get wrapperStyle() {
164
+ return htmlSafe(`position: relative; cursor: default; width: ${this.width}px; height: ${this.height}px;`);
165
+ }
166
+ get clipId() {
167
+ return `${this.chartId}-clip`;
168
+ }
169
+ get accessibilityLayer() {
170
+ return this.args.accessibilityLayer ?? true;
171
+ }
172
+ get margin() {
173
+ const margin = this.args.margin;
174
+ if (!margin) return DEFAULT_MARGIN;
175
+ return {
176
+ top: margin.top ?? 0,
177
+ right: margin.right ?? 0,
178
+ bottom: margin.bottom ?? 0,
179
+ left: margin.left ?? 0
180
+ };
181
+ }
182
+ // ---------------------------------------------------------------------------------------
183
+ // Parts
184
+ get partList() {
185
+ return this.#parts.items;
186
+ }
187
+ get pieParts() {
188
+ return this.partList.filter(part => part instanceof Pie);
189
+ }
190
+ get tooltipPart() {
191
+ return this.partList.find(part => part instanceof ChartTooltip);
192
+ }
193
+ get legendPart() {
194
+ return this.partList.find(part => part instanceof ChartLegend);
195
+ }
196
+ /** A pie's rows: its own `@data`, or the chart's. */
197
+ dataOf(pie) {
198
+ return pie.args.data?.length ? pie.args.data : this.args.data ?? [];
199
+ }
200
+ // ---------------------------------------------------------------------------------------
201
+ // Layout
202
+ get geometry() {
203
+ const size = this.size;
204
+ if (!size) return null;
205
+ const legend = this.legendPart;
206
+ const offset = plotArea({
207
+ width: size.width,
208
+ height: size.height,
209
+ margin: this.margin,
210
+ xAxis: defaultAxis('x', true),
211
+ yAxis: defaultAxis('y', true),
212
+ legend: legend && this.legendBox ? {
213
+ ...this.legendBox,
214
+ verticalAlign: legend.args.verticalAlign ?? 'bottom',
215
+ align: legend.args.align ?? 'center',
216
+ layout: legend.args.layout ?? 'horizontal'
217
+ } : undefined
218
+ });
219
+ const pies = this.pieParts.filter(part => !part.args.hide).map(part => {
220
+ const args = part.args;
221
+ const sectors = pieSectors({
222
+ dataKey: args.dataKey ?? PIE_DEFAULTS.dataKey,
223
+ nameKey: args.nameKey ?? PIE_DEFAULTS.nameKey,
224
+ name: args.name,
225
+ cx: args.cx ?? PIE_DEFAULTS.cx,
226
+ cy: args.cy ?? PIE_DEFAULTS.cy,
227
+ innerRadius: args.innerRadius ?? PIE_DEFAULTS.innerRadius,
228
+ outerRadius: args.outerRadius ?? PIE_DEFAULTS.outerRadius,
229
+ startAngle: args.startAngle ?? PIE_DEFAULTS.startAngle,
230
+ endAngle: args.endAngle ?? PIE_DEFAULTS.endAngle,
231
+ paddingAngle: args.paddingAngle ?? PIE_DEFAULTS.paddingAngle,
232
+ minAngle: args.minAngle ?? PIE_DEFAULTS.minAngle,
233
+ cornerRadius: args.cornerRadius,
234
+ fill: args.fill ?? PIE_DEFAULTS.fill
235
+ }, this.dataOf(part), part.cells.items.map(cell => cell.attributes), offset) ?? [];
236
+ return {
237
+ part,
238
+ key: keyOf(args.dataKey ?? PIE_DEFAULTS.dataKey),
239
+ sectors
240
+ };
241
+ });
242
+ return {
243
+ offset,
244
+ pies
245
+ };
246
+ }
247
+ /** recharts' polar view box: the chart's ring, which a pie's `Label` reads. */
248
+ static {
249
+ n(this.prototype, "geometry", [cached]);
250
+ }
251
+ get polarViewBox() {
252
+ const geometry = this.geometry;
253
+ if (!geometry) return null;
254
+ const {
255
+ width,
256
+ height
257
+ } = this;
258
+ const maxRadius = Math.min(geometry.offset.width, geometry.offset.height) / 2;
259
+ return {
260
+ cx: percentOf(this.args.cx ?? '50%', width, width / 2),
261
+ cy: percentOf(this.args.cy ?? '50%', height, height / 2),
262
+ innerRadius: percentOf(this.args.innerRadius ?? 0, maxRadius, 0),
263
+ outerRadius: percentOf(this.args.outerRadius ?? '80%', maxRadius, maxRadius * 0.8),
264
+ startAngle: this.args.startAngle ?? 0,
265
+ endAngle: this.args.endAngle ?? 360,
266
+ clockWise: false
267
+ };
268
+ }
269
+ // ---------------------------------------------------------------------------------------
270
+ // Drawing
271
+ get pies() {
272
+ const geometry = this.geometry;
273
+ if (!geometry) return [];
274
+ return geometry.pies.map(({
275
+ part,
276
+ key,
277
+ sectors
278
+ }, index) => {
279
+ const args = part.args;
280
+ const dataKey = args.dataKey ?? PIE_DEFAULTS.dataKey;
281
+ const stroke = args.stroke ?? PIE_DEFAULTS.stroke;
282
+ const drawn = drawnSectorAngles(sectors);
283
+ const props = sectors.map((sector, i) => ({
284
+ cx: sector.cx,
285
+ cy: sector.cy,
286
+ innerRadius: sector.innerRadius,
287
+ outerRadius: sector.outerRadius,
288
+ startAngle: drawn[i].startAngle,
289
+ endAngle: drawn[i].endAngle,
290
+ midAngle: sector.midAngle,
291
+ cornerRadius: sector.cornerRadius,
292
+ // The row's attributes (and its Cell's) win over the pie's, as recharts spreads them.
293
+ fill: sector.fill,
294
+ fillOpacity: sector.payload['fillOpacity'] ?? args.fillOpacity,
295
+ stroke: sector.payload['stroke'] ?? stroke,
296
+ strokeWidth: sector.payload['strokeWidth'] ?? args.strokeWidth,
297
+ index: sector.index,
298
+ value: sector.value,
299
+ name: sector.name,
300
+ percent: sector.percent,
301
+ payload: sector.payload,
302
+ dataKey,
303
+ isActive: false
304
+ }));
305
+ const label = args.label;
306
+ const labels = label ? sectors.map((sector, i) => {
307
+ const placement = pieLabelPlacement(sector);
308
+ const {
309
+ isActive: _,
310
+ ...sectorProps
311
+ } = props[i];
312
+ return {
313
+ index: i,
314
+ component: label === true ? undefined : label,
315
+ props: {
316
+ ...sectorProps,
317
+ stroke: 'none',
318
+ x: placement.x,
319
+ y: placement.y,
320
+ textAnchor: placement.textAnchor
321
+ },
322
+ line: args.labelLine === false ? undefined : {
323
+ d: linePath(placement.line, 'linear', 'horizontal') ?? '',
324
+ stroke: sector.fill,
325
+ strokeWidth: args.strokeWidth
326
+ },
327
+ text: textOf(valueOf({
328
+ ...sector.payload,
329
+ value: sector.value
330
+ }, dataKey))
331
+ };
332
+ }) : [];
333
+ const labelLists = part.labels.items.map((list, i) => {
334
+ const listArgs = list.args;
335
+ const offset = num(listArgs.offset, 5);
336
+ const position = listArgs.position ?? 'middle';
337
+ return {
338
+ key: `label-${i}`,
339
+ // recharts' Label is `recharts-label` unless given a class, which replaces it.
340
+ class: `recharts-text ${listArgs.class ?? 'recharts-label'}`,
341
+ fillOpacity: listArgs.fillOpacity,
342
+ stroke: listArgs.stroke,
343
+ fontSize: listArgs.fontSize,
344
+ offset,
345
+ items: sectors.map(sector => {
346
+ const raw = listArgs.dataKey !== undefined ? valueOf(sector.payload, listArgs.dataKey) : sector.value;
347
+ const shown = listArgs.formatter ? listArgs.formatter(raw) : raw;
348
+ const placement = polarLabelPlacement(sector, offset, position);
349
+ return {
350
+ x: placement.x,
351
+ y: placement.y,
352
+ anchor: placement.textAnchor,
353
+ dy: placement.dy,
354
+ text: textOf(shown),
355
+ fill: listArgs.fill ?? sector.fill,
356
+ class: ''
357
+ };
358
+ })
359
+ };
360
+ });
361
+ const viewBox = this.polarViewBox;
362
+ const contents = [];
363
+ const texts = [];
364
+ for (const text of part.textLabels.items) {
365
+ const textArgs = text.args;
366
+ const offset = num(textArgs.offset, 5);
367
+ const position = textArgs.position ?? 'middle';
368
+ if (textArgs.content) {
369
+ contents.push({
370
+ component: textArgs.content,
371
+ value: textArgs.value,
372
+ offset,
373
+ position
374
+ });
375
+ } else if (viewBox && textArgs.value !== undefined && textArgs.value !== null) {
376
+ const placement = polarLabelPlacement(viewBox, offset, position);
377
+ texts.push({
378
+ x: placement.x,
379
+ y: placement.y,
380
+ anchor: placement.textAnchor,
381
+ dy: placement.dy,
382
+ text: textOf(textArgs.value),
383
+ fill: textArgs.fill ?? '#808080',
384
+ class: `recharts-text ${textArgs.class ?? 'recharts-label'}`,
385
+ fontSize: textArgs.fontSize
386
+ });
387
+ }
388
+ }
389
+ return {
390
+ index,
391
+ part,
392
+ key,
393
+ points: sectors.map(sector => `${round(sector.tooltipPosition.x)},${round(sector.tooltipPosition.y)}`).join(' '),
394
+ sectors: props,
395
+ shape: args.shape,
396
+ labels,
397
+ labelLists,
398
+ contents,
399
+ texts
400
+ };
401
+ });
402
+ }
403
+ /** A sector's props for its shape, with whether the tooltip shows it. */
404
+ static {
405
+ n(this.prototype, "pies", [cached]);
406
+ }
407
+ sectorProps = (pie, sector) => {
408
+ const active = this.activeItem;
409
+ const isActive = !!active && active.pie === pie && active.index === sector.index;
410
+ return isActive ? {
411
+ ...sector,
412
+ isActive
413
+ } : sector;
414
+ };
415
+ /** recharts wraps the sector the tooltip shows in `recharts-active-shape`. */
416
+ sectorClass = (pie, index) => {
417
+ const active = this.activeItem;
418
+ return active && active.pie === pie && active.index === index ? 'recharts-layer recharts-active-shape' : 'recharts-layer recharts-shape';
419
+ };
420
+ // ---------------------------------------------------------------------------------------
421
+ // Tooltip
422
+ /** Every sector, pie by pie, in the order the arrow keys step through them. */
423
+ get items() {
424
+ return this.pies.flatMap((pie, p) => pie.sectors.map(sector => ({
425
+ pie: p,
426
+ index: sector.index
427
+ })));
428
+ }
429
+ get activeItem() {
430
+ const tooltip = this.tooltipPart;
431
+ if (!tooltip || tooltip.args.active === false) return null;
432
+ if (this.pointerItem) return this.pointerItem;
433
+ const items = this.items;
434
+ if (this.keyboardActive && this.keyboardIndex !== null) return items[this.keyboardIndex] ?? null;
435
+ if (isNumber(tooltip.args.defaultIndex) && this.keyboardIndex === null) {
436
+ return items.find(item => item.pie === 0 && item.index === tooltip.args.defaultIndex) ?? null;
437
+ }
438
+ if (tooltip.args.active === true) return items[this.keyboardIndex ?? 0] ?? null;
439
+ return null;
440
+ }
441
+ get activeSector() {
442
+ const item = this.activeItem;
443
+ if (!item) return null;
444
+ const pie = this.pies[item.pie];
445
+ const sector = pie?.sectors[item.index];
446
+ const geometry = this.geometry?.pies[item.pie]?.sectors[item.index];
447
+ return pie && sector && geometry ? {
448
+ pie,
449
+ sector,
450
+ geometry
451
+ } : null;
452
+ }
453
+ get isTooltipActive() {
454
+ const active = this.activeSector;
455
+ return !!active && active.pie.part.args.tooltipType !== 'none';
456
+ }
457
+ /** Which sector the tooltip shows, so its box is measured again when that changes. */
458
+ get tooltipKey() {
459
+ const item = this.activeItem;
460
+ return item ? `${item.pie}:${item.index}` : '';
461
+ }
462
+ get tooltipPayload() {
463
+ const active = this.activeSector;
464
+ if (!active || !this.isTooltipActive) return [];
465
+ const {
466
+ pie,
467
+ sector
468
+ } = active;
469
+ const args = pie.part.args;
470
+ return [{
471
+ name: sector.name,
472
+ value: sector.value,
473
+ dataKey: args.dataKey ?? PIE_DEFAULTS.dataKey,
474
+ color: sector.fill,
475
+ fill: sector.fill,
476
+ payload: sector.payload,
477
+ type: args.tooltipType
478
+ }];
479
+ }
480
+ get tooltipCoordinate() {
481
+ return this.activeSector?.geometry.tooltipPosition;
482
+ }
483
+ get tooltipProps() {
484
+ const args = this.tooltipPart?.args;
485
+ return {
486
+ labelClassName: args?.labelClassName,
487
+ labelFormatter: args?.labelFormatter,
488
+ formatter: args?.formatter
489
+ };
490
+ }
491
+ get tooltipContent() {
492
+ return this.tooltipPart?.args.content ?? ChartTooltipContent;
493
+ }
494
+ get tooltipStyle() {
495
+ const box = this.tooltipBox;
496
+ return tooltipStyleFor({
497
+ visible: this.isTooltipActive && this.tooltipPayload.length > 0 && !!box && box.width > 0 && box.height > 0,
498
+ coordinate: this.tooltipCoordinate,
499
+ box,
500
+ plot: this.geometry?.offset,
501
+ tooltip: this.tooltipPart
502
+ });
503
+ }
504
+ /** Measures the tooltip's box each time what it shows changes. */
505
+ measureTooltip = modifier((element, positional) => {
506
+ void [...positional];
507
+ const rect = {
508
+ width: element.offsetWidth,
509
+ height: element.offsetHeight
510
+ };
511
+ const current = this.#tooltipBox;
512
+ if (!current || Math.abs(current.width - rect.width) > 1 || Math.abs(current.height - rect.height) > 1) {
513
+ this.#tooltipBox = rect;
514
+ this.tooltipBox = rect;
515
+ }
516
+ });
517
+ // ---------------------------------------------------------------------------------------
518
+ // Legend
519
+ /** A legend entry for each pie's rows, as recharts' `selectPieLegend` makes them. */
520
+ get legendPayload() {
521
+ const items = [];
522
+ for (const part of this.pieParts) {
523
+ const args = part.args;
524
+ const legendType = args.legendType ?? 'rect';
525
+ const dataKey = args.dataKey ?? PIE_DEFAULTS.dataKey;
526
+ const cells = part.cells.items;
527
+ this.dataOf(part).forEach((datum, i) => {
528
+ const row = datum ?? {};
529
+ const name = valueOf(row, args.nameKey ?? PIE_DEFAULTS.nameKey) ?? args.name;
530
+ const cellFill = cells[i]?.args.fill;
531
+ const color = cellFill ?? (isString(row['fill']) ? row['fill'] : args.fill ?? PIE_DEFAULTS.fill);
532
+ items.push({
533
+ value: name ? String(name) : isString(dataKey) ? dataKey : '',
534
+ color,
535
+ payload: row,
536
+ type: legendType,
537
+ inactive: args.hide ?? false
538
+ });
539
+ });
540
+ }
541
+ // recharts sorts the legend by value (a stable sort).
542
+ return items.sort((a, b) => a.value < b.value ? -1 : a.value > b.value ? 1 : 0);
543
+ }
544
+ get legendVerticalAlign() {
545
+ return this.legendPart?.args.verticalAlign ?? 'bottom';
546
+ }
547
+ get legendContent() {
548
+ return this.legendPart?.args.content ?? ChartLegendContent;
549
+ }
550
+ get legendStyle() {
551
+ return legendStyleFor(this.size, this.margin, this.legendPart, this.legendBox);
552
+ }
553
+ measureLegend = modifier(element => observeSize(this, element, ({
554
+ width,
555
+ height
556
+ }) => {
557
+ const current = this.#legendBox;
558
+ if (!current || Math.abs(current.width - width) > 1 || Math.abs(current.height - height) > 1) {
559
+ this.#legendBox = {
560
+ width,
561
+ height
562
+ };
563
+ this.legendBox = {
564
+ width,
565
+ height
566
+ };
567
+ }
568
+ }));
569
+ // ---------------------------------------------------------------------------------------
570
+ // Events
571
+ /** The sector under a point, the last-drawn pie's first, as the pointer finds the top one. */
572
+ itemAt(point) {
573
+ const pies = this.geometry?.pies ?? [];
574
+ for (let p = pies.length - 1; p >= 0; p--) {
575
+ const sector = pies[p].sectors.find(entry => inSector(point, entry));
576
+ if (sector) return {
577
+ pie: p,
578
+ index: sector.index
579
+ };
580
+ }
581
+ return null;
582
+ }
583
+ onMouseMove = event => {
584
+ if (!this.geometry) return;
585
+ const element = event.currentTarget;
586
+ const rect = element.getBoundingClientRect();
587
+ const scaleX = element.offsetWidth ? rect.width / element.offsetWidth : 1;
588
+ const scaleY = element.offsetHeight ? rect.height / element.offsetHeight : 1;
589
+ const item = this.itemAt({
590
+ x: (event.clientX - rect.left) / scaleX,
591
+ y: (event.clientY - rect.top) / scaleY
592
+ });
593
+ const current = this.pointerItem;
594
+ if (item?.pie !== current?.pie || item?.index !== current?.index) this.pointerItem = item;
595
+ };
596
+ onMouseLeave = () => {
597
+ this.pointerItem = null;
598
+ };
599
+ onFocus = () => {
600
+ if (!this.accessibilityLayer || this.keyboardActive) return;
601
+ if (this.keyboardIndex === null) this.keyboardIndex = 0;
602
+ this.keyboardActive = true;
603
+ };
604
+ onBlur = () => {
605
+ if (!this.accessibilityLayer) return;
606
+ this.keyboardActive = false;
607
+ };
608
+ /**
609
+ * The arrow keys step through the sectors (every pie's, in turn), and Enter hides or shows the
610
+ * tooltip. recharts 3's keyboard steps through a cartesian chart's rows; on a pie chart it
611
+ * marks a sector active but shows no tooltip, so Joinery's goes further.
612
+ */
613
+ onKeyDown = event => {
614
+ if (!this.accessibilityLayer) return;
615
+ const {
616
+ key
617
+ } = event;
618
+ if (key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Enter') return;
619
+ const current = this.keyboardIndex ?? -1;
620
+ if (current < 0) return;
621
+ if (key === 'Enter') {
622
+ this.keyboardActive = !this.keyboardActive;
623
+ return;
624
+ }
625
+ const next = current + (key === 'ArrowRight' ? 1 : -1);
626
+ if (next < 0 || next >= this.items.length) return;
627
+ this.pointerItem = null;
628
+ this.keyboardIndex = next;
629
+ this.keyboardActive = true;
630
+ };
631
+ static {
632
+ setComponentTemplate(precompileTemplate("{{#let (hash Pie=(component Pie chart=this) ChartTooltip=(component ChartTooltip chart=this) ChartLegend=(component ChartLegend chart=this)) as |parts|}}\n {{#if this.fixedSize}}\n {{#if this.size}}\n <Surface @chart={{this}} ...attributes>{{yield parts}}</Surface>\n {{/if}}\n {{else}}\n <div class=\"recharts-responsive-container\" style=\"width: 100%; height: 100%; min-width: 0px;\" {{this.measureContainer}}>\n <div style=\"width: 0px; height: 0px; overflow: visible;\">\n {{#if this.size}}\n <Surface @chart={{this}} ...attributes>{{yield parts}}</Surface>\n {{/if}}\n </div>\n </div>\n {{/if}}\n{{/let}}", {
633
+ strictMode: true,
634
+ scope: () => ({
635
+ hash,
636
+ Pie,
637
+ ChartTooltip,
638
+ ChartLegend,
639
+ Surface
640
+ })
641
+ }), this);
642
+ }
643
+ }
644
+
645
+ export { PieChart as P };
646
+ //# sourceMappingURL=pie-chart-CmDtW_Ge.js.map