squarified 0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Kanno
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Squarified
2
+
3
+ `squarified treemap` is a mini treemap component.
4
+
5
+ ![display](./data.gif)
6
+
7
+ ## Usage
8
+
9
+ ```ts
10
+ import { createTreemap, presetDecorator } from 'squarified'
11
+
12
+ const root = document.querySelector('#app')
13
+
14
+ const treemap = createTreemap()
15
+ treemap.use('decorator', presetDecorator)
16
+
17
+ treemap.init(root)
18
+
19
+ treemap.setOptions({ data: [] })
20
+ ```
21
+
22
+ ### Auth
23
+
24
+ Kanno
25
+
26
+ ### LICENSE
27
+
28
+ [MIT](./LICENSE)
@@ -0,0 +1,283 @@
1
+ interface MatrixLoc {
2
+ a: number;
3
+ b: number;
4
+ c: number;
5
+ d: number;
6
+ e: number;
7
+ f: number;
8
+ }
9
+ declare class Matrix2D {
10
+ a: number;
11
+ b: number;
12
+ c: number;
13
+ d: number;
14
+ e: number;
15
+ f: number;
16
+ constructor(loc?: Partial<MatrixLoc>);
17
+ create(loc: MatrixLoc): this;
18
+ transform(x: number, y: number, scaleX: number, scaleY: number, rotation: number, skewX: number, skewY: number): this;
19
+ private translation;
20
+ private scale;
21
+ private skew;
22
+ private roate;
23
+ }
24
+
25
+ declare class Display {
26
+ parent: Display | null;
27
+ id: number;
28
+ matrix: Matrix2D;
29
+ constructor();
30
+ destory(): void;
31
+ }
32
+ interface LocOptions {
33
+ width: number;
34
+ height: number;
35
+ x: number;
36
+ y: number;
37
+ scaleX: number;
38
+ scaleY: number;
39
+ rotation: number;
40
+ skewX: number;
41
+ skewY: number;
42
+ }
43
+ interface GraphOptions extends LocOptions {
44
+ }
45
+ type Mod = [string, ...any[]];
46
+ interface Instruction {
47
+ mods: Mod[];
48
+ fillStyle(...args: any[]): void;
49
+ fillRect(...args: any[]): void;
50
+ strokeStyle(...args: any[]): void;
51
+ lineWidth(...args: any[]): void;
52
+ strokeRect(...args: any[]): void;
53
+ fillText(...args: any[]): void;
54
+ font(...args: any[]): void;
55
+ textBaseline(...args: any[]): void;
56
+ textAlign(...args: any[]): void;
57
+ }
58
+ declare function createInstruction(): Instruction;
59
+ declare class S extends Display {
60
+ width: number;
61
+ height: number;
62
+ x: number;
63
+ y: number;
64
+ scaleX: number;
65
+ scaleY: number;
66
+ rotation: number;
67
+ skewX: number;
68
+ skewY: number;
69
+ constructor(options?: Partial<LocOptions>);
70
+ }
71
+ declare abstract class Graph extends S {
72
+ instruction: ReturnType<typeof createInstruction>;
73
+ constructor(options?: Partial<GraphOptions>);
74
+ abstract create(): void;
75
+ render(ctx: CanvasRenderingContext2D): void;
76
+ }
77
+
78
+ declare class Box extends Display {
79
+ elements: Display[];
80
+ constructor();
81
+ add(...elements: Display[]): void;
82
+ remove(...elements: Display[]): void;
83
+ destory(): void;
84
+ }
85
+
86
+ interface RGBColor {
87
+ r: number;
88
+ g: number;
89
+ b: number;
90
+ a?: number;
91
+ }
92
+ interface HLSColor {
93
+ h: number;
94
+ l: number;
95
+ s: number;
96
+ a?: number;
97
+ }
98
+ interface ColorDecoratorResultHLS {
99
+ mode?: 'hsl';
100
+ desc: HLSColor;
101
+ }
102
+ interface ColorDecoratorResultRGB {
103
+ mode: 'rgb';
104
+ desc: RGBColor;
105
+ }
106
+ type ColorDecoratorResult = ColorDecoratorResultHLS | ColorDecoratorResultRGB;
107
+
108
+ type EventCallback<P = any[]> = P extends any[] ? (...args: P) => any : never;
109
+ type DefaultEventDefinition = Record<string, EventCallback>;
110
+ type BindThisParameter<T, C = unknown> = T extends (...args: infer P) => infer R ? (this: C, ...args: P) => R : never;
111
+ interface EventCollectionData<EvtDefinition extends DefaultEventDefinition, C = unknown> {
112
+ name: string;
113
+ handler: BindThisParameter<EvtDefinition[keyof EvtDefinition], C>;
114
+ ctx: C;
115
+ }
116
+ type EventCollections<EvtDefinition extends DefaultEventDefinition> = Record<keyof EvtDefinition, EventCollectionData<EvtDefinition>[]>;
117
+ declare class Event<EvtDefinition extends DefaultEventDefinition = DefaultEventDefinition> {
118
+ eventCollections: EventCollections<EvtDefinition>;
119
+ constructor();
120
+ on<C, Evt extends keyof EvtDefinition>(evt: Evt, handler: BindThisParameter<EvtDefinition[Evt], unknown extends C ? this : C>, c?: C): void;
121
+ off(evt: keyof EvtDefinition, handler?: BindThisParameter<EvtDefinition[keyof EvtDefinition], unknown>): void;
122
+ emit(evt: keyof EvtDefinition, ...args: Parameters<EvtDefinition[keyof EvtDefinition]>): void;
123
+ bindWithContext<C>(c: C): (evt: keyof EvtDefinition, handler: BindThisParameter<EvtDefinition[keyof EvtDefinition], unknown extends C ? this : C>) => void;
124
+ }
125
+
126
+ interface RenderViewportOptions {
127
+ width: number;
128
+ height: number;
129
+ devicePixelRatio: number;
130
+ }
131
+ declare class Render {
132
+ canvas: HTMLCanvasElement;
133
+ ctx: CanvasRenderingContext2D;
134
+ options: RenderViewportOptions;
135
+ constructor(to: Element, options: RenderViewportOptions);
136
+ clear(width: number, height: number): void;
137
+ initOptions(userOptions?: Partial<RenderViewportOptions>): void;
138
+ update(schedule: Schedule$1): void;
139
+ destory(): void;
140
+ }
141
+
142
+ type ApplyTo = string | Element;
143
+ declare class Schedule$1 extends Box {
144
+ render: Render;
145
+ to: Element;
146
+ event: Event;
147
+ constructor(to: ApplyTo, renderOptions?: Partial<RenderViewportOptions>);
148
+ applyTransform(matrix: Matrix2D): void;
149
+ update(): void;
150
+ execute(render: Render, graph?: Display): void;
151
+ }
152
+
153
+ declare function traverse(graphs: Display[], handler: (graph: Graph) => void): void;
154
+ declare const etoile: {
155
+ Schedule: typeof Schedule$1;
156
+ traverse: typeof traverse;
157
+ };
158
+
159
+ type ColorMappings = Record<string, ColorDecoratorResult>;
160
+ type Rect = {
161
+ w: number;
162
+ h: number;
163
+ };
164
+ type Series<T> = {
165
+ max: T;
166
+ min: T;
167
+ };
168
+ interface RenderColor {
169
+ mappings: ColorMappings;
170
+ }
171
+ interface RenderLayout {
172
+ titleAreaHeight: Series<number>;
173
+ rectBorderRadius: number;
174
+ rectBorderWidth: number;
175
+ rectGap: number;
176
+ }
177
+ interface RenderFont {
178
+ color: string;
179
+ fontSize: Series<number>;
180
+ fontFamily: string;
181
+ }
182
+ interface RenderDecorator {
183
+ color: RenderColor;
184
+ layout: RenderLayout;
185
+ font: RenderFont;
186
+ }
187
+ declare const defaultLayoutOptions: {
188
+ titleAreaHeight: {
189
+ max: number;
190
+ min: number;
191
+ };
192
+ rectGap: number;
193
+ rectBorderRadius: number;
194
+ rectBorderWidth: number;
195
+ };
196
+ declare const defaultFontOptions: {
197
+ color: string;
198
+ fontSize: {
199
+ max: number;
200
+ min: number;
201
+ };
202
+ fontFamily: string;
203
+ };
204
+ declare function presetDecorator(app: TreemapLayout): void;
205
+
206
+ type AnyObject = Record<keyof any, any>;
207
+ declare function sortChildrenByKey<T extends AnyObject, K extends keyof T = 'weight'>(data: T[], ...keys: K[]): T[];
208
+ declare function c2m<T extends AnyObject & {
209
+ groups: any[];
210
+ }, K extends keyof T>(data: T, key: K, modifier?: (data: T) => T): T & {
211
+ weight: number;
212
+ };
213
+ declare function flatten<T extends AnyObject & {
214
+ groups: T[];
215
+ }>(data: T[]): Omit<T, "groups">[];
216
+ type Module = ReturnType<typeof c2m>;
217
+ declare function bindParentForModule<T extends Module & {
218
+ parent: Module;
219
+ }>(modules: Module[], parent?: Module): T[];
220
+ type NativeModule = ReturnType<typeof bindParentForModule>[number] & {
221
+ id: string;
222
+ parent: NativeModule;
223
+ groups: NativeModule[];
224
+ };
225
+
226
+ type LayoutModule = {
227
+ node: NativeModule;
228
+ layout: [number, number, number, number];
229
+ children: LayoutModule[];
230
+ decorator: {
231
+ titleHeight: number;
232
+ rectBorderRadius: number;
233
+ rectGap: number;
234
+ rectBorderWidth: number;
235
+ };
236
+ };
237
+
238
+ declare const primitiveEvents: readonly ["click", "mousedown", "mousemove", "mouseup", "mouseover", "mouseout"];
239
+ type PrimitiveEvent = typeof primitiveEvents[number];
240
+ interface PrimitiveEventMetadata<T extends keyof HTMLElementEventMap> {
241
+ native: HTMLElementEventMap[T];
242
+ module: LayoutModule;
243
+ }
244
+ type PrimitiveEventCallback<T extends PrimitiveEvent> = (metadata: PrimitiveEventMetadata<T>) => void;
245
+ type PrimitiveEventDefinition = {
246
+ [key in PrimitiveEvent]: BindThisParameter<PrimitiveEventCallback<key>, TreemapInstanceAPI>;
247
+ };
248
+ interface EventMethods<C = TreemapInstanceAPI, D = PrimitiveEventDefinition> {
249
+ on<Evt extends keyof D>(evt: Evt, handler: BindThisParameter<D[Evt], unknown extends C ? this : C>): void;
250
+ off<Evt extends keyof D>(evt: keyof D, handler?: BindThisParameter<D[Evt], unknown extends C ? this : C>): void;
251
+ }
252
+
253
+ interface TreemapOptions {
254
+ data: Module[];
255
+ }
256
+ type Using = 'decorator';
257
+ interface App {
258
+ init: (el: Element) => void;
259
+ dispose: () => void;
260
+ setOptions: (options: TreemapOptions) => void;
261
+ resize: () => void;
262
+ use: (using: Using, register: (app: TreemapLayout) => void) => void;
263
+ }
264
+ declare class Schedule extends etoile.Schedule {
265
+ }
266
+ declare class TreemapLayout extends Schedule {
267
+ data: NativeModule[];
268
+ layoutNodes: LayoutModule[];
269
+ decorator: RenderDecorator;
270
+ private bgBox;
271
+ private fgBox;
272
+ constructor(...args: ConstructorParameters<typeof Schedule>);
273
+ drawBackgroundNode(node: LayoutModule): void;
274
+ drawForegroundNode(node: LayoutModule): void;
275
+ reset(): void;
276
+ get api(): {
277
+ zoom: (node: LayoutModule) => void;
278
+ };
279
+ }
280
+ declare function createTreemap(): App & EventMethods;
281
+ type TreemapInstanceAPI = TreemapLayout['api'];
282
+
283
+ export { type App, type ColorMappings, type Rect, type RenderColor, type RenderDecorator, type RenderFont, type RenderLayout, type Series, type TreemapInstanceAPI, c2m, createTreemap, defaultFontOptions, defaultLayoutOptions, flatten as flattenModule, presetDecorator, sortChildrenByKey };