angular-chrts 0.1.0-beta.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.
package/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Lib
2
+
3
+ This project was generated using [Angular CLI](https://github.com/angular/angular-cli) version 20.3.0.
4
+
5
+ ## Code scaffolding
6
+
7
+ Angular CLI includes powerful code scaffolding tools. To generate a new component, run:
8
+
9
+ ```bash
10
+ ng generate component component-name
11
+ ```
12
+
13
+ For a complete list of available schematics (such as `components`, `directives`, or `pipes`), run:
14
+
15
+ ```bash
16
+ ng generate --help
17
+ ```
18
+
19
+ ## Building
20
+
21
+ To build the library, run:
22
+
23
+ ```bash
24
+ ng build lib
25
+ ```
26
+
27
+ This command will compile your project, and the build artifacts will be placed in the `dist/` directory.
28
+
29
+ ### Publishing the Library
30
+
31
+ Once the project is built, you can publish your library by following these steps:
32
+
33
+ 1. Navigate to the `dist` directory:
34
+ ```bash
35
+ cd dist/lib
36
+ ```
37
+
38
+ 2. Run the `npm publish` command to publish your library to the npm registry:
39
+ ```bash
40
+ npm publish
41
+ ```
42
+
43
+ ## Running unit tests
44
+
45
+ To execute unit tests with the [Karma](https://karma-runner.github.io) test runner, use the following command:
46
+
47
+ ```bash
48
+ ng test
49
+ ```
50
+
51
+ ## Running end-to-end tests
52
+
53
+ For end-to-end (e2e) testing, run:
54
+
55
+ ```bash
56
+ ng e2e
57
+ ```
58
+
59
+ Angular CLI does not come with an end-to-end testing framework by default. You can choose one that suits your needs.
60
+
61
+ ## Additional Resources
62
+
63
+ For more information on using the Angular CLI, including detailed command references, visit the [Angular CLI Overview and Command Reference](https://angular.dev/tools/cli) page.
@@ -0,0 +1,7 @@
1
+ {
2
+ "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
3
+ "dest": "../../dist/angular-chrts",
4
+ "lib": {
5
+ "entryFile": "src/public-api.ts"
6
+ }
7
+ }
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "angular-chrts",
3
+ "version": "0.1.0-beta.1",
4
+ "peerDependencies": {
5
+ "@angular/common": "^20.3.0",
6
+ "@angular/core": "^20.3.0",
7
+ "@unovis/angular": "^1.6.2",
8
+ "@unovis/ts": "^1.6.2"
9
+ },
10
+ "dependencies": {
11
+ "tslib": "^2.3.0"
12
+ },
13
+ "sideEffects": false
14
+ }
@@ -0,0 +1,393 @@
1
+ import {
2
+ ChangeDetectionStrategy,
3
+ ChangeDetectorRef,
4
+ Component,
5
+ computed,
6
+ ElementRef,
7
+ inject,
8
+ input,
9
+ output,
10
+ signal,
11
+ viewChild,
12
+ } from '@angular/core';
13
+ import { CommonModule } from '@angular/common';
14
+ import {
15
+ VisXYContainerModule,
16
+ VisAreaModule,
17
+ VisAxisModule,
18
+ VisLineModule,
19
+ VisTooltipModule,
20
+ VisCrosshairModule,
21
+ VisBulletLegendModule,
22
+ } from '@unovis/angular';
23
+ import { CurveType, Position } from '@unovis/ts';
24
+ import {
25
+ BulletLegendItemInterface,
26
+ LegendPosition,
27
+ MarkerConfig,
28
+ axisFormatter,
29
+ } from '../types';
30
+ import { TooltipComponent } from '../tooltip';
31
+ import { createMarkers } from '../utils';
32
+
33
+ @Component({
34
+ selector: 'ngx-area-chart',
35
+ standalone: true,
36
+ imports: [
37
+ CommonModule,
38
+ VisXYContainerModule,
39
+ VisAreaModule,
40
+ VisAxisModule,
41
+ VisLineModule,
42
+ VisTooltipModule,
43
+ VisCrosshairModule,
44
+ VisBulletLegendModule,
45
+ TooltipComponent,
46
+ ],
47
+ template: `
48
+ <div
49
+ [style.display]="'flex'"
50
+ [style.flexDirection]="isLegendTop() ? 'column-reverse' : 'column'"
51
+ [style.gap]="'var(--vis-legend-spacing)'"
52
+ [style]="markerCssVars()"
53
+ [class.stacked-area-chart]="stacked()"
54
+ [id]="markerConfig()?.id"
55
+ (click)="onClick($event)"
56
+ >
57
+ <vis-xy-container
58
+ [data]="data()"
59
+ [height]="height()"
60
+ [padding]="padding()"
61
+ [yDomain]="yDomain()"
62
+ [xDomain]="xDomain()"
63
+ >
64
+ @if (!hideTooltip()) {
65
+ <vis-tooltip
66
+ [horizontalPlacement]="Position.Right"
67
+ [verticalPlacement]="Position.Top"
68
+ ></vis-tooltip>
69
+ }
70
+
71
+ @if (stacked()) {
72
+ <vis-area
73
+ [x]="_x"
74
+ [y]="stackedYAccessors()"
75
+ [color]="stackedColorAccessor"
76
+ [opacity]="hideArea() ? 0 : DEFAULT_OPACITY"
77
+ [curveType]="curveType() || CurveType.MonotoneX"
78
+ ></vis-area>
79
+ <vis-line
80
+ [x]="_x"
81
+ [y]="stackedLineYAccessors()"
82
+ [color]="stackedColorAccessor"
83
+ [curveType]="curveType() || CurveType.MonotoneX"
84
+ [lineWidth]="lineWidth()"
85
+ ></vis-line>
86
+ } @else {
87
+ @for (categoryId of categoryKeys(); track categoryId; let i = $index) {
88
+ <vis-area
89
+ [x]="_x"
90
+ [y]="getYAccessor(categoryId)"
91
+ [color]="getGradientSelector(i)"
92
+ [opacity]="hideArea() ? 0 : DEFAULT_OPACITY"
93
+ [curveType]="curveType() || CurveType.MonotoneX"
94
+ ></vis-area>
95
+ <vis-line
96
+ [x]="_x"
97
+ [y]="getYAccessor(categoryId)"
98
+ [color]="colors()[i]"
99
+ [curveType]="curveType() || CurveType.MonotoneX"
100
+ [lineWidth]="lineWidth()"
101
+ [lineDashArray]="lineDashArray() ? lineDashArray()![i] : undefined"
102
+ ></vis-line>
103
+ }
104
+ }
105
+
106
+ @if (!hideXAxis()) {
107
+ <vis-axis
108
+ type="x"
109
+ [label]="xLabel()"
110
+ [labelMargin]="8"
111
+ [numTicks]="xNumTicks()"
112
+ [tickFormat]="xFormatter()"
113
+ [tickValues]="$any(xExplicitTicks())"
114
+ [gridLine]="xGridLine()"
115
+ [domainLine]="xDomainLine()"
116
+ [tickLine]="xTickLine()"
117
+ [minMaxTicksOnly]="minMaxTicksOnly()"
118
+ ></vis-axis>
119
+ }
120
+
121
+ @if (!hideYAxis()) {
122
+ <vis-axis
123
+ type="y"
124
+ [label]="yLabel()"
125
+ [numTicks]="yNumTicks()"
126
+ [tickFormat]="yFormatter()"
127
+ [gridLine]="yGridLine()"
128
+ [domainLine]="yDomainLine()"
129
+ [tickLine]="yTickLine()"
130
+ ></vis-axis>
131
+ }
132
+
133
+ @if (!hideTooltip()) {
134
+ <vis-crosshair
135
+ [template]="onCrosshairUpdate"
136
+ ></vis-crosshair>
137
+ }
138
+ </vis-xy-container>
139
+
140
+ @if (!hideLegend()) {
141
+ <div
142
+ [style.display]="'flex'"
143
+ [style.justifyContent]="legendAlignment()"
144
+ >
145
+ <vis-bullet-legend
146
+ [style]="legendStyle()"
147
+ [items]="legendItems()"
148
+ ></vis-bullet-legend>
149
+ </div>
150
+ }
151
+
152
+ <div #tooltipWrapper style="display: none">
153
+ @if (hoverValues()) {
154
+ <ngx-tooltip
155
+ [data]="hoverValues()!"
156
+ [categories]="categories()"
157
+ [titleFormatter]="tooltipTitleFormatter()"
158
+ [yFormatter]="yFormatter()"
159
+ ></ngx-tooltip>
160
+ }
161
+ </div>
162
+ </div>
163
+ `,
164
+ changeDetection: ChangeDetectionStrategy.OnPush,
165
+ })
166
+ export class AreaChartComponent<T extends Record<string, any>> {
167
+ /** The data to be displayed in the chart. */
168
+ readonly data = input.required<T[]>();
169
+
170
+ /** The height of the chart in pixels. Default is 400. */
171
+ readonly height = input<number>(400);
172
+
173
+ /** Padding around the chart area. */
174
+ readonly padding = input<{ top: number; right: number; bottom: number; left: number }>({
175
+ top: 5,
176
+ right: 5,
177
+ bottom: 5,
178
+ left: 5,
179
+ });
180
+
181
+ /** Configuration for each category (line/area) in the chart. Keyed by category property name. */
182
+ readonly categories = input.required<Record<string, BulletLegendItemInterface>>();
183
+
184
+ /** Whether to stack the areas on top of each other. */
185
+ readonly stacked = input<boolean>(false);
186
+
187
+ /** Whether to hide the filled area and only show lines. */
188
+ readonly hideArea = input<boolean>(false);
189
+
190
+ /** The type of curve to use for the lines/areas. */
191
+ readonly curveType = input<CurveType>();
192
+
193
+ /** Thickness of the lines. Default is 2. */
194
+ readonly lineWidth = input<number>(2);
195
+ /** Array of dash patterns for each line. */
196
+ readonly lineDashArray = input<number[][]>();
197
+
198
+ /** Label for the X axis. */
199
+ readonly xLabel = input<string>();
200
+
201
+ /** Label for the Y axis. */
202
+ readonly yLabel = input<string>();
203
+
204
+ /** Formatter function for X axis tick labels. */
205
+ readonly xFormatter = input<axisFormatter>();
206
+
207
+ /** Formatter function for Y axis tick labels. */
208
+ readonly yFormatter = input<axisFormatter>();
209
+
210
+ /** Number of ticks to show on the X axis. */
211
+ readonly xNumTicks = input<number>();
212
+
213
+ /** Specific values to show on the X axis. */
214
+ readonly xExplicitTicks = input<Array<number | string | Date>>();
215
+
216
+ /** If true, only shows the first and last tick labels on the X axis. */
217
+ readonly minMaxTicksOnly = input<boolean>(false);
218
+
219
+ /** Number of ticks to show on the Y axis. */
220
+ readonly yNumTicks = input<number>();
221
+
222
+ /** Whether to hide the X axis entirely. */
223
+ readonly hideXAxis = input<boolean>(false);
224
+
225
+ /** Whether to hide the Y axis entirely. */
226
+ readonly hideYAxis = input<boolean>(false);
227
+ /** Whether to show grid lines for the X axis. */
228
+ readonly xGridLine = input<boolean>(false);
229
+
230
+ /** Whether to show grid lines for the Y axis. */
231
+ readonly yGridLine = input<boolean>(false);
232
+
233
+ /** Whether to show the domain line for the X axis. */
234
+ readonly xDomainLine = input<boolean>(false);
235
+
236
+ /** Whether to show the domain line for the Y axis. */
237
+ readonly yDomainLine = input<boolean>(false);
238
+
239
+ /** Whether to show tick lines for the X axis. */
240
+ readonly xTickLine = input<boolean>(false);
241
+
242
+ /** Whether to show tick lines for the Y axis. */
243
+ readonly yTickLine = input<boolean>(false);
244
+
245
+ /** Whether to hide the tooltip. */
246
+ readonly hideTooltip = input<boolean>(false);
247
+
248
+ /** Whether to hide the legend. */
249
+ readonly hideLegend = input<boolean>(false);
250
+
251
+ /** Position of the legend relative to the chart. */
252
+ readonly legendPosition = input<LegendPosition>(LegendPosition.BottomCenter);
253
+
254
+ /** Custom styles for the legend. */
255
+ readonly legendStyle = input<Record<string, string>>();
256
+
257
+ /** Custom styles for the tooltip. */
258
+ readonly tooltipStyle = input<Record<string, string>>({});
259
+
260
+ /** Formatter for the tooltip title. */
261
+ readonly tooltipTitleFormatter = input<(data: T) => string | number>();
262
+
263
+ /** Configuration for svg markers (dots) on the lines. */
264
+ readonly markerConfig = input<MarkerConfig>();
265
+
266
+ /** Gradient stop configuration for area charts. */
267
+ readonly gradientStops = input<Array<{ offset: string; stopOpacity: number }>>([
268
+ { offset: '0%', stopOpacity: 1 },
269
+ { offset: '75%', stopOpacity: 0 },
270
+ ]);
271
+
272
+ /** Manual Y domain [min, max]. */
273
+ readonly yDomain = input<[number, number]>();
274
+
275
+ /** Manual X domain [min, max]. */
276
+ readonly xDomain = input<[number, number]>();
277
+
278
+ /** Event emitted when the chart or a segment is clicked. */
279
+ readonly click = output<{ event: MouseEvent; values?: T }>();
280
+
281
+ readonly tooltipWrapper = viewChild<ElementRef<HTMLDivElement>>('tooltipWrapper');
282
+ readonly hoverValues = signal<T | undefined>(undefined);
283
+
284
+ readonly DEFAULT_OPACITY = 0.5;
285
+ readonly DEFAULT_COLOR = '#3b82f6';
286
+ readonly Position = Position;
287
+ readonly CurveType = CurveType;
288
+
289
+ readonly categoryKeys = computed(() => Object.keys(this.categories()));
290
+
291
+ readonly colors = computed(() => {
292
+ const cats = this.categories();
293
+ return Object.keys(cats).map((key, index) => {
294
+ const color = cats[key].color;
295
+ if (Array.isArray(color)) return color[0] ?? `var(--vis-color${index})`;
296
+ return color ?? `var(--vis-color${index})`;
297
+ });
298
+ });
299
+
300
+ readonly isLegendTop = computed(() => this.legendPosition().startsWith('top'));
301
+
302
+ readonly legendAlignment = computed(() => {
303
+ const pos = this.legendPosition();
304
+ if (pos.includes('left')) return 'flex-start';
305
+ if (pos.includes('right')) return 'flex-end';
306
+ return 'center';
307
+ });
308
+
309
+ readonly legendItems = computed(() => {
310
+ return Object.values(this.categories()).map((item) => ({
311
+ ...item,
312
+ color: Array.isArray(item.color) ? item.color[0] : item.color,
313
+ }));
314
+ });
315
+
316
+ readonly svgDefs = computed(() => {
317
+ const stops = this.gradientStops();
318
+ const colors = this.colors();
319
+
320
+ return colors.map((color, index) => {
321
+ const id = `gradient-${index}-${color.replace(/#/g, '')}`;
322
+ return `
323
+ <linearGradient id="${id}" gradientTransform="rotate(90)">
324
+ ${stops.map(stop => `
325
+ <stop offset="${stop.offset}" stop-color="${color}" stop-opacity="${stop.stopOpacity}" />
326
+ `).join('')}
327
+ <stop offset="100%" stop-color="${color}" stop-opacity="0" />
328
+ </linearGradient>
329
+ `;
330
+ }).join('');
331
+ });
332
+
333
+ readonly stackedYAccessors = computed(() => {
334
+ const keys = this.categoryKeys();
335
+ return keys.map(key => (d: T) => Number(d[key]));
336
+ });
337
+
338
+ readonly stackedLineYAccessors = computed(() => {
339
+ const keys = this.categoryKeys();
340
+ return keys.map((_, index) => {
341
+ return (d: T) => {
342
+ let sum = 0;
343
+ for (let i = 0; i <= index; i++) {
344
+ sum += Number(d[keys[i]]) || 0;
345
+ }
346
+ return sum;
347
+ };
348
+ });
349
+ });
350
+
351
+ readonly markerSvgDefs = computed(() => {
352
+ const config = this.markerConfig();
353
+ if (!config?.config) return '';
354
+ return createMarkers(config);
355
+ });
356
+
357
+ readonly markerCssVars = computed(() => {
358
+ const config = this.markerConfig();
359
+ if (!config?.config) return {};
360
+
361
+ const vars: Record<string, string> = {};
362
+ for (const key of Object.keys(config.config)) {
363
+ vars[`--vis-marker-${key}`] = `url("#${config.id}-${key}")`;
364
+ }
365
+ return vars;
366
+ });
367
+
368
+ readonly stackedColorAccessor: any = (_d: any, i: number) => this.colors()[i] ?? this.DEFAULT_COLOR;
369
+
370
+ _x: any = (_: any, i: number) => i;
371
+
372
+ getYAccessor(categoryId: string): any {
373
+ return (d: T) => Number(d[categoryId]);
374
+ }
375
+
376
+ getGradientSelector(index: number): any {
377
+ const color = this.colors()[index];
378
+ const id = `gradient-${index}-${color.replace(/#/g, '')}`;
379
+ return `url(#${id})`;
380
+ }
381
+
382
+ onCrosshairUpdate = (d: T): string => {
383
+ this.hoverValues.set(d);
384
+ this.cdr.detectChanges();
385
+ return this.tooltipWrapper()?.nativeElement.innerHTML || '';
386
+ };
387
+
388
+ onClick(event: MouseEvent) {
389
+ this.click.emit({ event, values: this.hoverValues() });
390
+ }
391
+
392
+ private cdr = inject(ChangeDetectorRef);
393
+ }
@@ -0,0 +1 @@
1
+ export * from './area-chart.component';