@konce-pt/chart 0.18.0 → 0.18.1

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.
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Szacowana szerokość etykiety w jednostkach viewBoxa. Bez pomiaru w DOM (rdzeń nie ma przeglądarki):
3
+ * średni znak kroju bezszeryfowego to ok. 0,6 wysokości fontu.
4
+ */
5
+ export declare function estimateLabelWidth(text: string, fontSize?: number): number;
6
+ /**
7
+ * Które etykiety osi kategorii pokazać. Wszystkie, jeśli się mieszczą; inaczej najgęstszy podział,
8
+ * który się mieści: 9 etykiet (ósemki osi), 5 (pierwsza, 1/4, środek, 3/4, ostatnia), 3, a na
9
+ * końcu same skrajne - te zwracane są zawsze, nawet gdy i one się stykają.
10
+ *
11
+ * `positions` - środki kategorii wzdłuż osi, `sizes` - rozmiar etykiet wzdłuż tej samej osi
12
+ * (szerokość na osi poziomej, wysokość linii na pionowej).
13
+ */
14
+ export declare function axisLabelIndices(positions: readonly number[], sizes: readonly number[], gap?: number): number[];
15
+ /**
16
+ * Środek etykiety przesunięty tak, żeby cała (`size` wzdłuż osi) zmieściła się w `[min, max]`.
17
+ * Etykieta szersza niż cały zakres zostaje na środku zakresu.
18
+ */
19
+ export declare function clampLabel(center: number, size: number, min: number, max: number): number;
@@ -0,0 +1,66 @@
1
+ /*
2
+ * Oś kategorii: które etykiety pokazać, gdy wszystkie się nie mieszczą, i jak nie wyjść
3
+ * poza viewBox. Jednostki to jednostki viewBoxa - tekst osi ma w nim stały rozmiar, więc
4
+ * wybór nie zależy od tego, jak duży jest wykres na ekranie.
5
+ */
6
+ /** Podziały osi próbowane po kolei, gdy wszystkie etykiety się nie mieszczą: ósemki, ćwiartki, połowy, końce. */
7
+ const SEGMENTS = [8, 4, 2, 1];
8
+ /**
9
+ * Szacowana szerokość etykiety w jednostkach viewBoxa. Bez pomiaru w DOM (rdzeń nie ma przeglądarki):
10
+ * średni znak kroju bezszeryfowego to ok. 0,6 wysokości fontu.
11
+ */
12
+ export function estimateLabelWidth(text, fontSize = 11) {
13
+ return text.length * fontSize * 0.6;
14
+ }
15
+ /** Indeksy `count` kategorii dzielących oś na `segments` równych odcinków, bez powtórzeń. */
16
+ function evenIndices(count, segments) {
17
+ const out = [];
18
+ for (let k = 0; k <= segments; k++) {
19
+ const idx = Math.round((k * (count - 1)) / segments);
20
+ if (out[out.length - 1] !== idx)
21
+ out.push(idx);
22
+ }
23
+ return out;
24
+ }
25
+ /** Czy etykiety pod tymi indeksami nie nachodzą na siebie (z odstępem `gap`). */
26
+ function fits(indices, positions, sizes, gap) {
27
+ for (let k = 1; k < indices.length; k++) {
28
+ const a = indices[k - 1];
29
+ const b = indices[k];
30
+ const room = Math.abs(positions[b] - positions[a]);
31
+ if (room < (sizes[a] + sizes[b]) / 2 + gap)
32
+ return false;
33
+ }
34
+ return true;
35
+ }
36
+ /**
37
+ * Które etykiety osi kategorii pokazać. Wszystkie, jeśli się mieszczą; inaczej najgęstszy podział,
38
+ * który się mieści: 9 etykiet (ósemki osi), 5 (pierwsza, 1/4, środek, 3/4, ostatnia), 3, a na
39
+ * końcu same skrajne - te zwracane są zawsze, nawet gdy i one się stykają.
40
+ *
41
+ * `positions` - środki kategorii wzdłuż osi, `sizes` - rozmiar etykiet wzdłuż tej samej osi
42
+ * (szerokość na osi poziomej, wysokość linii na pionowej).
43
+ */
44
+ export function axisLabelIndices(positions, sizes, gap = 6) {
45
+ const count = positions.length;
46
+ const all = positions.map((_, i) => i);
47
+ if (count <= 2 || fits(all, positions, sizes, gap))
48
+ return all;
49
+ for (const segments of SEGMENTS) {
50
+ if (count - 1 <= segments)
51
+ continue;
52
+ const indices = evenIndices(count, segments);
53
+ if (segments === 1 || fits(indices, positions, sizes, gap))
54
+ return indices;
55
+ }
56
+ return [0, count - 1];
57
+ }
58
+ /**
59
+ * Środek etykiety przesunięty tak, żeby cała (`size` wzdłuż osi) zmieściła się w `[min, max]`.
60
+ * Etykieta szersza niż cały zakres zostaje na środku zakresu.
61
+ */
62
+ export function clampLabel(center, size, min, max) {
63
+ if (size >= max - min)
64
+ return (min + max) / 2;
65
+ return Math.min(max - size / 2, Math.max(min + size / 2, center));
66
+ }
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from './bar.ts';
5
5
  export * from './arc.ts';
6
6
  export * from './radar.ts';
7
7
  export * from './layout.ts';
8
+ export * from './category-axis.ts';
8
9
  export * from './color.ts';
9
10
  export * from './format.ts';
10
11
  export * from './accessible.ts';
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export * from "./bar.js";
6
6
  export * from "./arc.js";
7
7
  export * from "./radar.js";
8
8
  export * from "./layout.js";
9
+ export * from "./category-axis.js";
9
10
  export * from "./color.js";
10
11
  export * from "./format.js";
11
12
  export * from "./accessible.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konce-pt/chart",
3
- "version": "0.18.0",
3
+ "version": "0.18.1",
4
4
  "description": "Chart geometry core for Koncept UI - scales, line and area paths, bar layout, pie and radar geometry. Framework-free TypeScript, zero runtime dependencies.",
5
5
  "license": "MIT",
6
6
  "author": "konce.pt",