@torpor/ui 1.2.2 → 1.3.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.
@@ -4,14 +4,19 @@ import { chartColors } from "../utils/chartColors";
4
4
  interface PieData {
5
5
  name: string;
6
6
  value: number;
7
- color: string;
7
+ /** The color to draw the slice with (default: the next palette color) */
8
+ color?: string;
8
9
  }
9
10
 
10
11
  interface SegmentData {
11
12
  name: string;
12
13
  value: number;
13
- color: string;
14
+ color?: string;
14
15
  path: string;
16
+ /** The angle the slice spans, in degrees */
17
+ angle: number;
18
+ /** The angle the slice starts at, in degrees */
19
+ startAngle: number;
15
20
  }
16
21
 
17
22
  interface PieChartProps {
@@ -27,6 +32,10 @@ interface PieChartProps {
27
32
  size?: number;
28
33
  radius?: number;
29
34
  cutout?: number;
35
+ /** Draws each slice's name and value on the slice (skips thin slices) */
36
+ labels?: boolean;
37
+ /** Draws an inset key down the right of the chart, with the pie shifted left */
38
+ legend?: boolean;
30
39
  }
31
40
 
32
41
  export default function PieChart($props: PieChartProps) {
@@ -37,25 +46,53 @@ export default function PieChart($props: PieChartProps) {
37
46
  // NOTE: Take two pixels off the radius so that the stroke doesn't go outside the border
38
47
  let $state = $watch({
39
48
  get radius() {
40
- return ($props.radius! / 100) * $props.size! - 2;
49
+ // With a legend, keep the pie clear of the key
50
+ const percent = $props.legend ? Math.min($props.radius!, 26) : $props.radius!;
51
+ return (percent / 100) * $props.size! - 2;
41
52
  },
42
53
  get cutout() {
43
54
  return ($props.cutout! / 100) * $props.size! - 2;
44
55
  },
56
+ get centerX() {
57
+ return $props.legend ? $props.size! * 0.32 : $props.size! / 2;
58
+ },
59
+ get centerY() {
60
+ return $props.size! / 2;
61
+ },
45
62
  get segments() {
46
- return buildSegments($props.size!, $props.data, $props.radius!, $props.cutout!);
63
+ return buildSegments(this.centerX, this.centerY, $props.data, this.radius, this.cutout);
64
+ },
65
+ get labels() {
66
+ // The name and value at the middle of each slice, when the slice
67
+ // is big enough to carry a label
68
+ return this.segments
69
+ .filter((s: SegmentData) => s.angle >= 20)
70
+ .map((s: SegmentData) => {
71
+ const r = this.radius * 0.62;
72
+ const a = (Math.PI * (s.startAngle - s.angle / 2)) / 180;
73
+ return {
74
+ x: +(this.centerX + r * Math.sin(a)).toFixed(2),
75
+ y: +(this.centerY + r * Math.cos(a)).toFixed(2),
76
+ text: `${s.name} ${s.value}`,
77
+ };
78
+ });
79
+ },
80
+ get key() {
81
+ return $props.data.map((d, i) => ({
82
+ name: d.name,
83
+ color: d.color || chartColors[i % chartColors.length],
84
+ }));
47
85
  },
48
86
  });
49
87
 
50
88
  function buildSegments(
51
- theSize: number,
89
+ centerX: number,
90
+ centerY: number,
52
91
  theData: PieData[],
53
92
  theRadiusSize: number,
54
93
  theCutoutSize: number,
55
94
  ): SegmentData[] {
56
95
  const segments = [];
57
- const x = theSize / 2;
58
- const y = theSize / 2;
59
96
  const totalValue = theData.reduce((a, b) => a + b.value, 0);
60
97
 
61
98
  // Start at the top of the chart, at 180 + 360 so that we don't have to
@@ -69,7 +106,9 @@ export default function PieChart($props: PieChartProps) {
69
106
  name: theData[i].name,
70
107
  value: theData[i].value,
71
108
  color: theData[i].color,
72
- path: buildPath(x, y, theRadiusSize, theCutoutSize, startAngle, endAngle, largeArc),
109
+ startAngle,
110
+ angle: startAngle - endAngle,
111
+ path: buildPath(centerX, centerY, theRadiusSize, theCutoutSize, startAngle, endAngle, largeArc),
73
112
  });
74
113
  startAngle = endAngle;
75
114
  }
@@ -92,7 +131,7 @@ export default function PieChart($props: PieChartProps) {
92
131
 
93
132
  // Use some trig to find the points on the circumference at the start and end angles
94
133
  const startx = x + theRadiusSize * Math.sin((Math.PI * startAngle) / 180);
95
- const starty = x + theRadiusSize * Math.cos((Math.PI * startAngle) / 180);
134
+ const starty = y + theRadiusSize * Math.cos((Math.PI * startAngle) / 180);
96
135
 
97
136
  const endx = x + theRadiusSize * Math.sin((Math.PI * endAngle) / 180);
98
137
  const endy = y + theRadiusSize * Math.cos((Math.PI * endAngle) / 180);
@@ -100,7 +139,7 @@ export default function PieChart($props: PieChartProps) {
100
139
  if ($props.cutout) {
101
140
  // Use some trig to find the points on the cutout at the start and end angles
102
141
  const cutoutstartx = x + theCutoutSize * Math.sin((Math.PI * startAngle) / 180);
103
- const cutoutstarty = x + theCutoutSize * Math.cos((Math.PI * startAngle) / 180);
142
+ const cutoutstarty = y + theCutoutSize * Math.cos((Math.PI * startAngle) / 180);
104
143
 
105
144
  const cutoutendx = x + theCutoutSize * Math.sin((Math.PI * endAngle) / 180);
106
145
  const cutoutendy = y + theCutoutSize * Math.cos((Math.PI * endAngle) / 180);
@@ -123,6 +162,15 @@ A${theCutoutSize},${theCutoutSize} 0 ${largeArc} 0 ${cutoutstartx},${cutoutstart
123
162
  }
124
163
  }
125
164
 
165
+ function keyX(): number {
166
+ return $props.size! * 0.58;
167
+ }
168
+
169
+ function keyRowY(i: number): number {
170
+ const rowHeight = 18;
171
+ return $props.size! / 2 - ($props.data.length * rowHeight) / 2 + i * rowHeight;
172
+ }
173
+
126
174
  @render {
127
175
  <div {$props?.id} class={["torp-pie-chart", $props?.class]} {$props?.style}>
128
176
  <svg role={$props?.ariaLabel ? "img" : undefined} aria-label={$props?.ariaLabel} version="1.1" width={$props.size} height={$props.size}>
@@ -132,6 +180,28 @@ A${theCutoutSize},${theCutoutSize} 0 ${largeArc} 0 ${cutoutstartx},${cutoutstart
132
180
  <title>{`${segment.name}\n${segment.value}`}</title>
133
181
  </path>
134
182
  }
183
+ @if ($props.labels) {
184
+ @for (let label of $state.labels) {
185
+ <text
186
+ x={label.x}
187
+ y={label.y}
188
+ text-anchor="middle"
189
+ dominant-baseline="middle"
190
+ fill="white"
191
+ style="paint-order: stroke; stroke: currentcolor; stroke-width: 3px; stroke-opacity: 0.2;"
192
+ >
193
+ {label.text}
194
+ </text>
195
+ }
196
+ }
197
+ @if ($props.legend) {
198
+ @for (let [i, entry] of $state.key.entries()) {
199
+ <rect x={keyX()} y={keyRowY(i) + 4} width={10} height={10} rx="2" fill={entry.color} />
200
+ <text fill="currentcolor" dominant-baseline="middle" x={keyX() + 14} y={keyRowY(i) + 9}>
201
+ {entry.name}
202
+ </text>
203
+ }
204
+ }
135
205
  </g>
136
206
  </svg>
137
207
  </div>
@@ -14,12 +14,13 @@ import YAxis from "./YAxis.torp";
14
14
  interface Pair {
15
15
  x: number;
16
16
  y: number;
17
- label: string;
17
+ /** An extra label for the point's tooltip */
18
+ label?: string;
18
19
  }
19
20
 
20
21
  interface DataSeries {
21
22
  name: string;
22
- color: string;
23
+ color?: string;
23
24
  data: Pair[];
24
25
  }
25
26
 
@@ -1,6 +1,60 @@
1
1
  import { type SlotRender } from "@torpor/view";
2
2
 
3
3
 
4
+ interface AreaChartProps {
5
+ /** An ID for the root element */
6
+ id?: string;
7
+ /** Classes for the root element */
8
+ class?: ClassValue;
9
+ /** Styles for the root element */
10
+ style?: StyleValue;
11
+ /** An ARIA label describing the chart for screen readers */
12
+ ariaLabel?: string;
13
+ series: DataSeries[];
14
+ height?: number;
15
+ width?: number;
16
+ xLabel?: string;
17
+ yLabel?: string;
18
+ maxValue?: number;
19
+ stepCount?: number;
20
+ stepValue?: number;
21
+ showHLines?: boolean;
22
+ showVLines?: boolean;
23
+ /** The opacity of each area's fill (default 0.25) */
24
+ opacity?: number;
25
+ /** Stacks each series on top of the one below it */
26
+ stacked?: boolean;
27
+ }
28
+
29
+ /**
30
+ * An area chart: each series drawn as its line with the region under it
31
+ * filled.
32
+ *
33
+ * This is a preset over the layered chart components:
34
+ *
35
+ * ```
36
+ * <Chart>
37
+ * <ChartYAxis />
38
+ * <ChartGrid />
39
+ * <ChartXAxis />
40
+ * <ChartArea seriesIndex={0} />
41
+ * <ChartLines seriesIndex={0} />
42
+ * </Chart>
43
+ * ```
44
+ *
45
+ * Compose those directly to customize -- e.g. a line for a second series
46
+ * overlaid on the first's area, or `stacked` to pile the series up.
47
+ */
48
+ declare function AreaChart(
49
+ $parent: ParentNode,
50
+ $anchor: Node | null,
51
+ $props: AreaChartProps,
52
+ $context?: Record<PropertyKey, any>,
53
+ $slots?: Record<string, SlotRender>,
54
+ ): void;
55
+
56
+
57
+
4
58
  interface BarChartProps {
5
59
  /** An ID for the root element */
6
60
  id?: string;
@@ -20,12 +74,14 @@ interface BarChartProps {
20
74
  stepValue?: number;
21
75
  showHLines?: boolean;
22
76
  showVLines?: boolean;
77
+ /** Stacks the values in each band into one bar per series */
78
+ stacked?: boolean;
23
79
  }
24
80
 
25
81
  /**
26
- * A bar chart: one thick horizontal bar per series, drawn from the left
27
- * edge towards its first value, with category labels down the side and the
28
- * value scale along the bottom.
82
+ * A bar chart: horizontal bars drawn from the left edge towards their
83
+ * values, with category labels down the side and the value scale along the
84
+ * bottom.
29
85
  *
30
86
  * This is a preset over the layered chart components:
31
87
  *
@@ -65,9 +121,9 @@ interface ChartProps {
65
121
  height?: number;
66
122
  /** Fixed width in pixels (default: fills the container, minimum 400) */
67
123
  width?: number;
68
- /** Label under the x axis */
124
+ /** Label under the x axis, drawn by ChartXAxis */
69
125
  xLabel?: string;
70
- /** Label beside the y axis */
126
+ /** Label beside the y axis, drawn by ChartYAxis */
71
127
  yLabel?: string;
72
128
  /** The value at the top of the value axis, instead of the series' maximum */
73
129
  maxValue?: number;
@@ -79,8 +135,8 @@ interface ChartProps {
79
135
 
80
136
  /**
81
137
  * A container for layered chart components. It measures its text, works out
82
- * the plot geometry and provides it to whatever axes, grid lines and marks
83
- * are slotted inside:
138
+ * the scales, and provides them to whatever axes, grid lines and marks are
139
+ * slotted inside:
84
140
  *
85
141
  * ```
86
142
  * <Chart ariaLabel="Revenue" series={revenue}>
@@ -92,12 +148,20 @@ interface ChartProps {
92
148
  * </Chart>
93
149
  * ```
94
150
  *
151
+ * Layers **reserve space** through the Chart's layout: adding a ChartXAxis
152
+ * claims a strip along the bottom, a ChartYAxis claims a strip on its side,
153
+ * a ChartLegend claims a strip for itself, and so on. The plot area is
154
+ * whatever space is left over, and the mark layers -- columns, bars, lines,
155
+ * areas, points -- draw only into that, overlapping each other freely. A
156
+ * Chart with no axes uses the whole svg; take one away and the marks grow
157
+ * into the space it gives back.
158
+ *
95
159
  * Layers can be mixed freely -- columns with a line overlaid, an axis on
96
160
  * either side, only a y axis with no x axis -- so a preset like LineChart is
97
161
  * just one such combination.
98
162
  *
99
- * See also the ready-made presets: BarChart, ColumnChart, LineChart,
100
- * ScatterChart.
163
+ * See also the ready-made presets: AreaChart, BarChart, ColumnChart,
164
+ * LineChart, ScatterChart.
101
165
  */
102
166
  declare function Chart(
103
167
  $parent: ParentNode,
@@ -110,8 +174,31 @@ declare function Chart(
110
174
 
111
175
 
112
176
  /**
113
- * Horizontal bar marks for a Chart: one thick horizontal bar per series,
114
- * drawn from the left edge of the plot towards its value.
177
+ * Area marks for a Chart: fills the region between the chosen series' line
178
+ * and the bottom of the plot. Add a ChartLines layer with the same
179
+ * seriesIndex to draw the line itself.
180
+ *
181
+ * With `stacked`, each area sits on top of the stacked series below it --
182
+ * give every stacked area the same treatment and the Chart's value domain
183
+ * grows to fit the largest total.
184
+ */
185
+ declare function ChartArea(
186
+ $parent: ParentNode,
187
+ $anchor: Node | null,
188
+ $props?: ChartAreaProps,
189
+ $context?: Record<PropertyKey, any>,
190
+ $slots?: Record<string, SlotRender>,
191
+ ): void;
192
+
193
+
194
+
195
+ /**
196
+ * Horizontal bar marks for a Chart: every value in the chosen series is
197
+ * drawn as a horizontal bar, grouped within its band down the left.
198
+ *
199
+ * With `stacked`, the values in each band are laid end to end into a
200
+ * single bar instead, and the Chart's value domain grows to fit the
201
+ * largest total.
115
202
  *
116
203
  * For a column per value grouped under each label, use ChartColumns --
117
204
  * those run vertically.
@@ -130,6 +217,9 @@ declare function ChartBars(
130
217
  * Vertical column marks for a Chart: every value in the chosen series is
131
218
  * drawn as a vertical column, grouped under its label.
132
219
  *
220
+ * With `stacked`, the values in each slot are piled into a single column
221
+ * instead, and the Chart's value domain grows to fit the largest total.
222
+ *
133
223
  * For one thick bar per series, use ChartBars -- those run horizontally.
134
224
  */
135
225
  declare function ChartColumns(
@@ -144,7 +234,9 @@ declare function ChartColumns(
144
234
 
145
235
  /**
146
236
  * Grid lines for a Chart: horizontal lines at each value step and/or
147
- * vertical lines through each category slot.
237
+ * vertical lines through each category slot. Drawn only within the plot
238
+ * area, inside the space reserved by the axes. The baseline (value 0) is
239
+ * left to the axis line.
148
240
  */
149
241
  declare function ChartGrid(
150
242
  $parent: ParentNode,
@@ -156,12 +248,33 @@ declare function ChartGrid(
156
248
 
157
249
 
158
250
 
251
+ /**
252
+ * A legend for a Chart: one color swatch and name per series.
253
+ *
254
+ * Like the axes, it reserves a strip on its side of the plot -- wide enough
255
+ * for the names, wrapping onto more rows when they don't fit one row --
256
+ * and marks and other layers draw only in the space left over.
257
+ */
258
+ declare function ChartLegend(
259
+ $parent: ParentNode,
260
+ $anchor: Node | null,
261
+ $props?: ChartLegendProps,
262
+ $context?: Record<PropertyKey, any>,
263
+ $slots?: Record<string, SlotRender>,
264
+ ): void;
265
+
266
+
267
+
159
268
  /**
160
269
  * Line marks for a Chart: draws the chosen series as a connected line
161
- * across the category slots.
270
+ * across the plot.
162
271
  *
163
272
  * Add alongside ChartColumns or ChartBars for overlaid layers -- or use two
164
273
  * ChartLines with different `seriesIndex` to compare series.
274
+ *
275
+ * With `stacked`, each line sits on top of the stacked series below it --
276
+ * give every stacked line the same treatment and the Chart's value domain
277
+ * grows to fit the largest total.
165
278
  */
166
279
  declare function ChartLines(
167
280
  $parent: ParentNode,
@@ -173,11 +286,30 @@ declare function ChartLines(
173
286
 
174
287
 
175
288
 
289
+ /**
290
+ * Point marks for a Chart: one circle per value, centered in its category
291
+ * slot. On its own it is a scatter-style layer; with ChartLines it draws
292
+ * the line's markers.
293
+ */
294
+ declare function ChartPoints(
295
+ $parent: ParentNode,
296
+ $anchor: Node | null,
297
+ $props?: ChartPointsProps,
298
+ $context?: Record<PropertyKey, any>,
299
+ $slots?: Record<string, SlotRender>,
300
+ ): void;
301
+
302
+
303
+
176
304
  /**
177
305
  * The x axis for a chart. For vertical marks (columns, lines) it runs along
178
306
  * the bottom with each series' label centered under its slot; for
179
307
  * horizontal marks (bars) it becomes the value scale, with a tick for each
180
308
  * step along the bottom.
309
+ *
310
+ * Adding it to a Chart reserves a strip along the bottom for its labels
311
+ * (and its title, when there is one): marks and other layers draw only in
312
+ * the space left over.
181
313
  */
182
314
  declare function ChartXAxis(
183
315
  $parent: ParentNode,
@@ -195,8 +327,9 @@ declare function ChartXAxis(
195
327
  * between 0 and the maximum value. For horizontal marks (bars) it carries
196
328
  * the category labels, one per band down the left.
197
329
  *
198
- * Drawing it on either side does not change the plot geometry; overlaying
199
- * an axis is purely presentational.
330
+ * Adding it to a Chart reserves a strip on its side of the plot: wide enough
331
+ * for its ticks (or the category names), plus the title when one is drawn.
332
+ * Marks and other layers draw only in the space left over.
200
333
  */
201
334
  declare function ChartYAxis(
202
335
  $parent: ParentNode,
@@ -227,6 +360,8 @@ interface ColumnChartProps {
227
360
  stepValue?: number;
228
361
  showHLines?: boolean;
229
362
  showVLines?: boolean;
363
+ /** Stacks the values in each slot into one column per label */
364
+ stacked?: boolean;
230
365
  }
231
366
 
232
367
  /**
@@ -309,6 +444,8 @@ interface LineChartProps {
309
444
  showPoints?: boolean;
310
445
  showHLines?: boolean;
311
446
  showVLines?: boolean;
447
+ /** Stacks each series on top of the one below it */
448
+ stacked?: boolean;
312
449
  }
313
450
 
314
451
  /**
@@ -352,6 +489,10 @@ interface PieChartProps {
352
489
  size?: number;
353
490
  radius?: number;
354
491
  cutout?: number;
492
+ /** Draws each slice's name and value on the slice (skips thin slices) */
493
+ labels?: boolean;
494
+ /** Draws an inset key down the right of the chart, with the pie shifted left */
495
+ legend?: boolean;
355
496
  }
356
497
 
357
498
  declare function PieChart(
@@ -466,4 +607,4 @@ declare function YAxis(
466
607
  $slots?: Record<string, SlotRender>,
467
608
  ): void;
468
609
 
469
- export { BarChart, Chart, ChartBars, ChartColumns, ChartGrid, ChartLines, ChartXAxis, ChartYAxis, ColumnChart, GridLines, LineChart, PieChart, ScatterChart, SparkLine, XAxis, YAxis };
610
+ export { AreaChart, BarChart, Chart, ChartArea, ChartBars, ChartColumns, ChartGrid, ChartLegend, ChartLines, ChartPoints, ChartXAxis, ChartYAxis, ColumnChart, GridLines, LineChart, PieChart, ScatterChart, SparkLine, XAxis, YAxis };
@@ -1,9 +1,13 @@
1
+ import AreaChart from "./AreaChart.torp";
1
2
  import BarChart from "./BarChart.torp";
2
3
  import Chart from "./Chart.torp";
4
+ import ChartArea from "./ChartArea.torp";
3
5
  import ChartBars from "./ChartBars.torp";
4
6
  import ChartColumns from "./ChartColumns.torp";
5
7
  import ChartGrid from "./ChartGrid.torp";
8
+ import ChartLegend from "./ChartLegend.torp";
6
9
  import ChartLines from "./ChartLines.torp";
10
+ import ChartPoints from "./ChartPoints.torp";
7
11
  import ChartXAxis from "./ChartXAxis.torp";
8
12
  import ChartYAxis from "./ChartYAxis.torp";
9
13
  import ColumnChart from "./ColumnChart.torp";
@@ -14,4 +18,4 @@ import ScatterChart from "./ScatterChart.torp";
14
18
  import SparkLine from "./SparkLine.torp";
15
19
  import XAxis from "./XAxis.torp";
16
20
  import YAxis from "./YAxis.torp";
17
- export { BarChart, Chart, ChartBars, ChartColumns, ChartGrid, ChartLines, ChartXAxis, ChartYAxis, ColumnChart, GridLines, LineChart, PieChart, ScatterChart, SparkLine, XAxis, YAxis };
21
+ export { AreaChart, BarChart, Chart, ChartArea, ChartBars, ChartColumns, ChartGrid, ChartLegend, ChartLines, ChartPoints, ChartXAxis, ChartYAxis, ColumnChart, GridLines, LineChart, PieChart, ScatterChart, SparkLine, XAxis, YAxis };
@@ -27,6 +27,8 @@ export default function Option($props: OptionProps | undefined) {
27
27
  class={["torp-option", $props?.class]}
28
28
  {$props?.style}
29
29
  &value={$state.value}
30
- />
30
+ >
31
+ <slot />
32
+ </option>
31
33
  }
32
34
  }
@@ -1,2 +1 @@
1
- import MaskedInput from "./MaskedInput.torp";
2
- export { MaskedInput };
1
+ export { default as MaskedInput } from "./MaskedInput.torp";
@@ -1,2 +1 @@
1
- import NumberInput from "./NumberInput.torp";
2
- export { NumberInput };
1
+ export { default as NumberInput } from "./NumberInput.torp";