ngx-mira-charts 0.2.0 → 0.2.1-dev.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.
@@ -1,5 +1,5 @@
1
1
  import * as i0 from '@angular/core';
2
- import { viewChild, TemplateRef, Directive, input, model, computed, booleanAttribute, contentChild, inject, ElementRef, linkedSignal, DestroyRef, afterRenderEffect, afterNextRender, ChangeDetectionStrategy, Component, numberAttribute, forwardRef, LOCALE_ID, contentChildren, signal } from '@angular/core';
2
+ import { viewChild, TemplateRef, Directive, input, model, computed, numberAttribute, inject, forwardRef, ChangeDetectionStrategy, Component, LOCALE_ID, contentChildren, contentChild, signal, ElementRef, DestroyRef, afterNextRender, booleanAttribute, afterRenderEffect, linkedSignal } from '@angular/core';
3
3
  import { NgTemplateOutlet } from '@angular/common';
4
4
 
5
5
  /**
@@ -303,446 +303,128 @@ const DEFAULT_TICK_COUNT = 5;
303
303
  * scales, the labels along x, and the pointer and the datum under it. Every
304
304
  * kind of chart provides itself under it, with `provideCartesianChart`, and
305
305
  * the library's own parts inject it: they can't import the chart, which
306
- * imports the axes and the legend to leave room for them. The way Angular
307
- * Material's tabs inject `MAT_TAB_GROUP`.
306
+ * imports the axes to leave room for them. The way Angular Material's tabs
307
+ * inject `MAT_TAB_GROUP`.
308
308
  *
309
309
  * @internal
310
310
  */
311
311
  class CartesianContext {
312
312
  }
313
-
314
313
  /**
315
- * Marks a template of your own for what each item of `<mira-legend>` shows,
316
- * in place of its icon and its name. It flows as text inside the item, which
317
- * stays the legend's: in a toggleable legend, a button, marked `aria-pressed`,
318
- * that hides or shows the series. A hidden series' item takes a gray color,
319
- * which your template can keep or set its own from `item.hidden`. Its context
320
- * is a `LegendItemContext`, and typed: `let-item` holds the series' `name`,
321
- * `color`, `icon` and `hidden`.
322
- *
323
- * Screen readers read the template's text out, as the button's name in a
324
- * toggleable legend:
325
- *
326
- * - keep the series' name in it, the same whether the series is hidden or
327
- * not, and show the state in the look: the button already tells it;
328
- * - mark what only decorates, such as a swatch or an emoji,
329
- * `aria-hidden="true"`;
330
- * - in a toggleable legend, put no buttons or links in it, as it's inside a
331
- * button.
314
+ * The legend as the chart sees it: the edge it sits along, and the room it
315
+ * takes there. The legend provides itself under it, so the chart finds it
316
+ * without importing it, and an app without a legend doesn't ship one.
332
317
  *
333
- * Forced colors replace the colors you set, and print drops backgrounds by
334
- * default: give what shows the series' color `forced-color-adjust: none` and
335
- * `print-color-adjust: exact`, as the legend's own icons have.
318
+ * @internal
319
+ */
320
+ class ChartLegend {
321
+ }
322
+
323
+ /**
324
+ * The category axis under the plot: a tick per datum, labelled with the
325
+ * datum's value under `dataKey`. Its line runs at zero, where values rise and
326
+ * fall from; the ticks and labels stay under the plot, clear of values below
327
+ * zero.
336
328
  *
337
329
  * @example
338
330
  * ```html
339
- * <mira-legend>
340
- * <ng-template miraLegendItemContent let-item>
341
- * <span
342
- * aria-hidden="true"
343
- * [style.color]="item.hidden ? null : item.color"
344
- * >●</span>
345
- * <b>{{ item.name }}</b>
346
- * </ng-template>
347
- * </mira-legend>
331
+ * <mira-x-axis dataKey="month" />
348
332
  * ```
349
333
  */
350
- class MiraLegendItemContent {
351
- /**
352
- * Types the template's context, for Angular's template checks.
353
- *
354
- * @internal
355
- */
356
- static ngTemplateContextGuard(directive, context) {
357
- return true;
358
- }
359
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraLegendItemContent, deps: [], target: i0.ɵɵFactoryTarget.Directive });
360
- static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.3.35", type: MiraLegendItemContent, isStandalone: true, selector: "ng-template[miraLegendItemContent]", ngImport: i0 });
334
+ class MiraXAxis extends MiraChartLayer {
335
+ /** The key of each datum's category. Without one, the ticks show indexes. */
336
+ dataKey = input(...(ngDevMode ? [undefined, { debugName: "dataKey" }] : []));
337
+ /** The room the axis takes under the plot, in px: 30 by default. */
338
+ height = input(30, ...(ngDevMode ? [{ debugName: "height", transform: numberAttribute }] : [{ transform: numberAttribute }]));
339
+ chart = inject(CartesianContext);
340
+ // Where the ticks and labels hang from: the bottom of the plot.
341
+ bottom = computed(() => this.chart.plot().top + this.chart.plot().height, ...(ngDevMode ? [{ debugName: "bottom" }] : []));
342
+ // Where the axis line runs: at zero, kept on the plot.
343
+ baseline = computed(() => {
344
+ const zero = this.chart.yScale()(0);
345
+ return Math.min(this.bottom(), Math.max(this.chart.plot().top, zero));
346
+ }, ...(ngDevMode ? [{ debugName: "baseline" }] : []));
347
+ // A tick at each datum, with its label: the chart makes the labels from
348
+ // the `dataKey` of its x axis.
349
+ ticks = computed(() => {
350
+ const xs = this.chart.xPositions();
351
+ return this.chart
352
+ .xLabels()
353
+ .map((label, index) => ({ offset: xs[index], label }));
354
+ }, ...(ngDevMode ? [{ debugName: "ticks" }] : []));
355
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraXAxis, deps: null, target: i0.ɵɵFactoryTarget.Component });
356
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.35", type: MiraXAxis, isStandalone: true, selector: "mira-x-axis", inputs: { dataKey: { classPropertyName: "dataKey", publicName: "dataKey", isSignal: true, isRequired: false, transformFunction: null }, height: { classPropertyName: "height", publicName: "height", isSignal: true, isRequired: false, transformFunction: null } }, providers: [
357
+ { provide: MiraChartLayer, useExisting: forwardRef(() => MiraXAxis) },
358
+ ], usesInheritance: true, ngImport: i0, template: `
359
+ <ng-template>
360
+ <svg:g class="mira-x-axis">
361
+ <svg:line
362
+ [attr.x1]="chart.plot().left"
363
+ [attr.x2]="chart.plot().left + chart.plot().width"
364
+ [attr.y1]="baseline()"
365
+ [attr.y2]="baseline()"
366
+ />
367
+ @for (tick of ticks(); track $index) {
368
+ <svg:g
369
+ [attr.transform]="'translate(' + tick.offset + ',' + bottom() + ')'"
370
+ >
371
+ <svg:line y2="6" />
372
+ <svg:text y="9" dy="0.71em" text-anchor="middle">
373
+ {{ tick.label }}
374
+ </svg:text>
375
+ </svg:g>
376
+ }
377
+ </svg:g>
378
+ </ng-template>
379
+ `, isInline: true, styles: [".mira-x-axis{font-size:12px}line{stroke:var(--_mira-axis-color)}text{fill:var(--_mira-axis-color)}\n"], changeDetection: i0.ChangeDetectionStrategy.OnPush });
361
380
  }
362
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraLegendItemContent, decorators: [{
363
- type: Directive,
364
- args: [{ selector: 'ng-template[miraLegendItemContent]' }]
365
- }] });
366
- // The height of a legend of one row along the top or the bottom, as its
367
- // styles lay it out in the browser's default font size, in px: the row and
368
- // the room beside it for the focus ring. The plot leaves it until the legend
369
- // is measured, on the server.
370
- const ROW_HEIGHT = 22;
381
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraXAxis, decorators: [{
382
+ type: Component,
383
+ args: [{ selector: 'mira-x-axis', changeDetection: ChangeDetectionStrategy.OnPush, providers: [
384
+ { provide: MiraChartLayer, useExisting: forwardRef(() => MiraXAxis) },
385
+ ], template: `
386
+ <ng-template>
387
+ <svg:g class="mira-x-axis">
388
+ <svg:line
389
+ [attr.x1]="chart.plot().left"
390
+ [attr.x2]="chart.plot().left + chart.plot().width"
391
+ [attr.y1]="baseline()"
392
+ [attr.y2]="baseline()"
393
+ />
394
+ @for (tick of ticks(); track $index) {
395
+ <svg:g
396
+ [attr.transform]="'translate(' + tick.offset + ',' + bottom() + ')'"
397
+ >
398
+ <svg:line y2="6" />
399
+ <svg:text y="9" dy="0.71em" text-anchor="middle">
400
+ {{ tick.label }}
401
+ </svg:text>
402
+ </svg:g>
403
+ }
404
+ </svg:g>
405
+ </ng-template>
406
+ `, styles: [".mira-x-axis{font-size:12px}line{stroke:var(--_mira-axis-color)}text{fill:var(--_mira-axis-color)}\n"] }]
407
+ }], propDecorators: { dataKey: [{ type: i0.Input, args: [{ isSignal: true, alias: "dataKey", required: false }] }], height: [{ type: i0.Input, args: [{ isSignal: true, alias: "height", required: false }] }] } });
408
+
371
409
  /**
372
- * Lists the chart's series along one of its edges, in the order they're
373
- * written, each with its icon in its color and its name: a stretch of line
374
- * marks a line, a square marks bars, and a circle any other series (the
375
- * series' `legendIcon`). A series is called by its `name`, or its `dataKey`
376
- * without one. The plot leaves room for the legend, as it does for the axes,
377
- * and the chart stays as big as it is.
378
- *
379
- * Along the bottom, by default, or the top, the items go in rows, centered,
380
- * as many as they take. Up the right or the left side, they go in a column in
381
- * the middle of the chart's height, at most 40% of its width wide, where a
382
- * longer name wraps. Left and right are as the svg is drawn, in a page that
383
- * runs right to left too, where the y axis stays on the left. A legend on a
384
- * side leaves the plot no room on the server: its width is known only once
385
- * the browser lays it out.
386
- *
387
- * It's a list to read, unless it's `toggleable`: then each item is a button
388
- * that hides its series, or shows it again, and the chart fits its y axis to
389
- * the series that are left. The state is the series' own `hidden`, to bind
390
- * as `[(hidden)]`. A hidden series' item stays, grayed and struck through.
391
- * In a toggleable legend it's marked `aria-pressed="false"` for screen
392
- * readers; in a list to read, screen readers and the keyboard skip it, as the
393
- * chart doesn't draw it.
394
- *
395
- * The legend's text takes the `color` around it, the chart's in a chart. The
396
- * rest are CSS custom properties, to set anywhere above the legend:
397
- *
398
- * - `--mira-font-size`: the size of the text, `0.75rem` by default; the room
399
- * around the text follows it;
400
- * - `--mira-legend-inactive-color`: a hidden series' item,
401
- * `--mira-axis-color` by default.
402
- *
403
- * For items that show something of your own, write their template inside, as
404
- * an `<ng-template miraLegendItemContent>`, and import `MiraLegendItemContent`
405
- * along with `MiraLegend`: without it, the template goes unnoticed, and the
406
- * legend shows its own icon and name.
407
- *
408
- * Anywhere else on the page, name the chart with `for`. There the legend sits
409
- * where the page puts it, as a block, and the chart leaves it no room; its
410
- * `position` only picks its layout, rows for the top or the bottom, a column
411
- * for a side. Its colors come from above it, as in a chart: set the custom
412
- * properties above both to change them alike.
410
+ * The value axis left of the plot. It fits the values of every series in the
411
+ * chart that isn't hidden, and zero, rounded out to round ticks. The ticks
412
+ * read as numbers in the app's locale (`LOCALE_ID`).
413
413
  *
414
414
  * @example
415
415
  * ```html
416
- * <mira-line-chart [data]="day">
417
- * <mira-x-axis dataKey="hour" />
418
- * <mira-y-axis />
419
- * <mira-line dataKey="solar" name="Solar" stroke="#d97706" />
420
- * <mira-line dataKey="usage" name="Usage" stroke="#3b82f6" />
421
- * <mira-legend position="right" toggleable />
422
- * </mira-line-chart>
423
- * ```
424
- *
425
- * ```html
426
- * <header>
427
- * <h2>A sunny day</h2>
428
- * <mira-legend [for]="chart" />
429
- * </header>
430
- * <mira-line-chart #chart [data]="day">…</mira-line-chart>
416
+ * <mira-y-axis [tickCount]="4" />
431
417
  * ```
432
418
  */
433
- class MiraLegend {
434
- /** The edge of the chart the legend sits along: `'bottom'` by default. */
435
- position = input('bottom', ...(ngDevMode ? [{ debugName: "position" }] : []));
436
- /**
437
- * Whether a click on an item, or Enter or Space on it, hides its series or
438
- * shows it again. Off by default: the legend is then a list to read.
439
- */
440
- toggleable = input(false, ...(ngDevMode ? [{ debugName: "toggleable", transform: booleanAttribute }] : [{ transform: booleanAttribute }]));
441
- /**
442
- * The chart whose series the legend lists, for a legend out of it: give
443
- * the chart a reference, `<mira-line-chart #chart>`, and name it,
444
- * `<mira-legend [for]="chart" />`. Unset, the chart the legend is in. With
445
- * no chart, as while a chart named through a query is in an `@if` that's
446
- * off, the legend lists nothing.
447
- */
448
- for = input(...(ngDevMode ? [undefined, { debugName: "for" }] : []));
419
+ class MiraYAxis extends MiraChartLayer {
420
+ /** The room the axis takes left of the plot, in px: 60 by default. */
421
+ width = input(60, ...(ngDevMode ? [{ debugName: "width", transform: numberAttribute }] : [{ transform: numberAttribute }]));
449
422
  /**
450
- * The room the legend takes from the chart along its edge, in px: its
451
- * height along the top or the bottom, its width up a side, as the browser
452
- * last laid it out there. Until then, a row's height along the top or the
453
- * bottom, and none on a side.
454
- *
455
- * @internal
423
+ * About how many ticks to show, 5 by default. The axis keeps its ticks 1, 2
424
+ * or 5 times a power of ten apart, so it may show a few more or a few fewer.
456
425
  */
457
- room = computed(() => this.#measured() ?? (inRows(this.position()) ? ROW_HEIGHT : 0), ...(ngDevMode ? [{ debugName: "room" }] : []));
458
- // Where the legend sits: at an edge of the chart it's in. Out of a chart,
459
- // where the page puts it.
460
- placement = computed(() => this.#parent ? this.position() : null, ...(ngDevMode ? [{ debugName: "placement" }] : []));
461
- // How it lays out its items: in rows, or in a column.
462
- layout = computed(() => inRows(this.position()) ? 'rows' : 'column', ...(ngDevMode ? [{ debugName: "layout" }] : []));
463
- // What the legend lists: each series of the chart it names, or of the one
464
- // it's in, hidden ones too.
465
- entries = computed(() => {
466
- const all = (this.for() ?? this.#parent)?.series() ?? [];
467
- return all.flatMap((series) => {
468
- const name = nameOf(series);
469
- if (name === null)
470
- return [];
471
- const item = {
472
- name,
473
- color: series.color(),
474
- icon: series.legendIcon,
475
- hidden: series.hidden(),
476
- };
477
- return { series, item };
478
- });
479
- }, ...(ngDevMode ? [{ debugName: "entries" }] : []));
480
- // A template of your own for the items, if there's one.
481
- template = contentChild(MiraLegendItemContent, ...(ngDevMode ? [{ debugName: "template", read: TemplateRef }] : [{
482
- read: TemplateRef,
483
- }]));
484
- #host = inject(ElementRef).nativeElement;
485
- // The chart the legend is in, if it's in one.
486
- #parent = inject(CartesianContext, { optional: true });
487
- // The room the legend took, as the browser last laid it out: none yet on a
488
- // side it has just moved to, which the old room says nothing about.
489
- #measured = linkedSignal(...(ngDevMode ? [{ debugName: "#measured", source: this.position,
490
- computation: () => null }] : [{
491
- source: this.position,
492
- computation: () => null,
493
- }]));
494
- /** @internal */
495
- constructor() {
496
- const parent = this.#parent;
497
- const destroyRef = inject(DestroyRef);
498
- // Out of a chart, there's no room to leave the legend, and nothing to
499
- // measure.
500
- if (!parent)
501
- return;
502
- // The legend is measured after a render that changes what it lists, how
503
- // it shows it, its side, or the chart's size, which can wrap it onto more
504
- // rows or fewer; and the plot leaves it room before the page is painted.
505
- afterRenderEffect({
506
- earlyRead: () => {
507
- this.entries();
508
- this.template();
509
- parent.size();
510
- return this.#measure();
511
- },
512
- write: (room) => this.#measured.set(room()),
513
- });
514
- // And again whenever it changes size on its own, as when a font loads,
515
- // or an item of your own that changes, padding and border included: then
516
- // the plot follows a frame later.
517
- afterNextRender(() => {
518
- const observer = new ResizeObserver(() => this.#measured.set(this.#measure()));
519
- observer.observe(this.#host, { box: 'border-box' });
520
- destroyRef.onDestroy(() => observer.disconnect());
521
- });
522
- }
523
- // Hides the series, or shows it again.
524
- toggle(series) {
525
- series.hidden.update((hidden) => !hidden);
526
- }
527
- // The room the legend takes along its edge as laid out, in the svg's px: a
528
- // transform on the page around the chart scales both alike.
529
- #measure() {
530
- const { offsetWidth, offsetHeight } = this.#host;
531
- return inRows(this.position()) ? offsetHeight : offsetWidth;
532
- }
533
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraLegend, deps: [], target: i0.ɵɵFactoryTarget.Component });
534
- static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.35", type: MiraLegend, isStandalone: true, selector: "mira-legend", inputs: { position: { classPropertyName: "position", publicName: "position", isSignal: true, isRequired: false, transformFunction: null }, toggleable: { classPropertyName: "toggleable", publicName: "toggleable", isSignal: true, isRequired: false, transformFunction: null }, for: { classPropertyName: "for", publicName: "for", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "attr.data-position": "placement()", "attr.data-layout": "layout()" } }, queries: [{ propertyName: "template", first: true, predicate: MiraLegendItemContent, descendants: true, read: TemplateRef, isSignal: true }], ngImport: i0, template: `
535
- <ul class="mira-legend" role="list">
536
- @for (entry of entries(); track entry.series) {
537
- <li
538
- [attr.data-hidden]="entry.item.hidden || null"
539
- [attr.inert]="entry.item.hidden && !toggleable() ? '' : null"
540
- >
541
- @if (toggleable()) {
542
- <button
543
- type="button"
544
- class="mira-legend-item"
545
- [attr.aria-pressed]="!entry.item.hidden"
546
- (click)="toggle(entry.series)"
547
- >
548
- <ng-container
549
- [ngTemplateOutlet]="template() ?? own"
550
- [ngTemplateOutletContext]="{ $implicit: entry.item }"
551
- />
552
- </button>
553
- } @else {
554
- <ng-container
555
- [ngTemplateOutlet]="template() ?? own"
556
- [ngTemplateOutletContext]="{ $implicit: entry.item }"
557
- />
558
- }
559
- </li>
560
- }
561
- </ul>
562
-
563
- <ng-template #own let-item>
564
- <span class="mira-legend-label">
565
- <span
566
- class="mira-legend-icon"
567
- [attr.data-icon]="item.icon"
568
- [style.color]="item.hidden ? null : item.color"
569
- ></span>
570
- <span class="mira-legend-name">{{ item.name }}</span>
571
- </span>
572
- </ng-template>
573
- `, isInline: true, styles: [":host{--_mira-axis-color: var(--mira-axis-color, #666);--_mira-grid-color: var(--mira-grid-color, #ccc);--_mira-background: var(--mira-background, #fff);--_mira-cursor-color: var(--mira-cursor-color, #999)}@supports (color: light-dark(#000,#fff)){:host{--_mira-axis-color: var(--mira-axis-color, light-dark(#666, #999));--_mira-grid-color: var(--mira-grid-color, light-dark(#ccc, #333));--_mira-background: var(--mira-background, Canvas);--_mira-cursor-color: var(--mira-cursor-color, light-dark(#999, #666))}}\n", ":host{display:block}:host([data-position]){position:absolute}:host([data-position=\"top\"]){inset:0 0 auto}:host([data-position=\"bottom\"]){inset:auto 0 0}:host([data-position=\"left\"]){inset:0 auto 0 0}:host([data-position=\"right\"]){inset:0 0 0 auto}:host([data-position=\"left\"]),:host([data-position=\"right\"]){display:flex;align-items:center;align-items:safe center;max-width:40%}.mira-legend{display:flex;flex-wrap:wrap;justify-content:center;justify-content:safe center;gap:max(.5em,24px - 1.5em) 1.25em;margin:0;padding:0 .75em;list-style:none;font-size:var(--mira-font-size, .75rem);line-height:1.5;overflow-wrap:anywhere}:host([data-position=\"top\"]) .mira-legend{padding-top:4px}:host([data-position=\"bottom\"]) .mira-legend{padding-bottom:4px}:host([data-layout=\"column\"]) .mira-legend{flex-direction:column;align-items:flex-start}:host([data-position]) li{pointer-events:auto}.mira-legend-item{all:unset;display:block;cursor:pointer}.mira-legend-item:focus-visible{outline:revert;outline-offset:2px}li[data-hidden]{color:var(--mira-legend-inactive-color, var(--_mira-axis-color))}li[data-hidden] .mira-legend-name{text-decoration:line-through}@media(forced-colors:active){li[data-hidden]{color:GrayText}}.mira-legend-label{display:flex;gap:.5em}.mira-legend-icon{display:grid;flex:none;place-items:center;width:1em;height:1.5em;forced-color-adjust:none;-webkit-print-color-adjust:exact;print-color-adjust:exact}.mira-legend-icon:before{content:\"\";width:.75em;height:.75em;border-radius:50%;background:currentColor}.mira-legend-icon[data-icon=square]:before{border-radius:.125em}.mira-legend-icon[data-icon=line]:before{width:100%;height:2px;border-radius:0}\n"], dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
574
- }
575
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraLegend, decorators: [{
576
- type: Component,
577
- args: [{ selector: 'mira-legend', changeDetection: ChangeDetectionStrategy.OnPush, imports: [NgTemplateOutlet], host: {
578
- '[attr.data-position]': 'placement()',
579
- '[attr.data-layout]': 'layout()',
580
- }, template: `
581
- <ul class="mira-legend" role="list">
582
- @for (entry of entries(); track entry.series) {
583
- <li
584
- [attr.data-hidden]="entry.item.hidden || null"
585
- [attr.inert]="entry.item.hidden && !toggleable() ? '' : null"
586
- >
587
- @if (toggleable()) {
588
- <button
589
- type="button"
590
- class="mira-legend-item"
591
- [attr.aria-pressed]="!entry.item.hidden"
592
- (click)="toggle(entry.series)"
593
- >
594
- <ng-container
595
- [ngTemplateOutlet]="template() ?? own"
596
- [ngTemplateOutletContext]="{ $implicit: entry.item }"
597
- />
598
- </button>
599
- } @else {
600
- <ng-container
601
- [ngTemplateOutlet]="template() ?? own"
602
- [ngTemplateOutletContext]="{ $implicit: entry.item }"
603
- />
604
- }
605
- </li>
606
- }
607
- </ul>
608
-
609
- <ng-template #own let-item>
610
- <span class="mira-legend-label">
611
- <span
612
- class="mira-legend-icon"
613
- [attr.data-icon]="item.icon"
614
- [style.color]="item.hidden ? null : item.color"
615
- ></span>
616
- <span class="mira-legend-name">{{ item.name }}</span>
617
- </span>
618
- </ng-template>
619
- `, styles: [":host{--_mira-axis-color: var(--mira-axis-color, #666);--_mira-grid-color: var(--mira-grid-color, #ccc);--_mira-background: var(--mira-background, #fff);--_mira-cursor-color: var(--mira-cursor-color, #999)}@supports (color: light-dark(#000,#fff)){:host{--_mira-axis-color: var(--mira-axis-color, light-dark(#666, #999));--_mira-grid-color: var(--mira-grid-color, light-dark(#ccc, #333));--_mira-background: var(--mira-background, Canvas);--_mira-cursor-color: var(--mira-cursor-color, light-dark(#999, #666))}}\n", ":host{display:block}:host([data-position]){position:absolute}:host([data-position=\"top\"]){inset:0 0 auto}:host([data-position=\"bottom\"]){inset:auto 0 0}:host([data-position=\"left\"]){inset:0 auto 0 0}:host([data-position=\"right\"]){inset:0 0 0 auto}:host([data-position=\"left\"]),:host([data-position=\"right\"]){display:flex;align-items:center;align-items:safe center;max-width:40%}.mira-legend{display:flex;flex-wrap:wrap;justify-content:center;justify-content:safe center;gap:max(.5em,24px - 1.5em) 1.25em;margin:0;padding:0 .75em;list-style:none;font-size:var(--mira-font-size, .75rem);line-height:1.5;overflow-wrap:anywhere}:host([data-position=\"top\"]) .mira-legend{padding-top:4px}:host([data-position=\"bottom\"]) .mira-legend{padding-bottom:4px}:host([data-layout=\"column\"]) .mira-legend{flex-direction:column;align-items:flex-start}:host([data-position]) li{pointer-events:auto}.mira-legend-item{all:unset;display:block;cursor:pointer}.mira-legend-item:focus-visible{outline:revert;outline-offset:2px}li[data-hidden]{color:var(--mira-legend-inactive-color, var(--_mira-axis-color))}li[data-hidden] .mira-legend-name{text-decoration:line-through}@media(forced-colors:active){li[data-hidden]{color:GrayText}}.mira-legend-label{display:flex;gap:.5em}.mira-legend-icon{display:grid;flex:none;place-items:center;width:1em;height:1.5em;forced-color-adjust:none;-webkit-print-color-adjust:exact;print-color-adjust:exact}.mira-legend-icon:before{content:\"\";width:.75em;height:.75em;border-radius:50%;background:currentColor}.mira-legend-icon[data-icon=square]:before{border-radius:.125em}.mira-legend-icon[data-icon=line]:before{width:100%;height:2px;border-radius:0}\n"] }]
620
- }], ctorParameters: () => [], propDecorators: { position: [{ type: i0.Input, args: [{ isSignal: true, alias: "position", required: false }] }], toggleable: [{ type: i0.Input, args: [{ isSignal: true, alias: "toggleable", required: false }] }], for: [{ type: i0.Input, args: [{ isSignal: true, alias: "for", required: false }] }], template: [{ type: i0.ContentChild, args: [i0.forwardRef(() => MiraLegendItemContent), { ...{
621
- read: TemplateRef,
622
- }, isSignal: true }] }] } });
623
- // What the series is called, or null while it has no `dataKey` yet. Angular
624
- // binds a series' inputs as it checks the block the series is written in,
625
- // and a legend in a block it checks before reads the series first: such a
626
- // series joins the list as soon as its inputs are bound, in the same pass.
627
- function nameOf(series) {
628
- try {
629
- return series.displayName();
630
- }
631
- catch {
632
- return null;
633
- }
634
- }
635
- // Whether a legend on this side lists its items in rows, rather than in a
636
- // column.
637
- function inRows(side) {
638
- return side === 'top' || side === 'bottom';
639
- }
640
-
641
- /**
642
- * The category axis under the plot: a tick per datum, labelled with the
643
- * datum's value under `dataKey`. Its line runs at zero, where values rise and
644
- * fall from; the ticks and labels stay under the plot, clear of values below
645
- * zero.
646
- *
647
- * @example
648
- * ```html
649
- * <mira-x-axis dataKey="month" />
650
- * ```
651
- */
652
- class MiraXAxis extends MiraChartLayer {
653
- /** The key of each datum's category. Without one, the ticks show indexes. */
654
- dataKey = input(...(ngDevMode ? [undefined, { debugName: "dataKey" }] : []));
655
- /** The room the axis takes under the plot, in px: 30 by default. */
656
- height = input(30, ...(ngDevMode ? [{ debugName: "height", transform: numberAttribute }] : [{ transform: numberAttribute }]));
657
- chart = inject(CartesianContext);
658
- // Where the ticks and labels hang from: the bottom of the plot.
659
- bottom = computed(() => this.chart.plot().top + this.chart.plot().height, ...(ngDevMode ? [{ debugName: "bottom" }] : []));
660
- // Where the axis line runs: at zero, kept on the plot.
661
- baseline = computed(() => {
662
- const zero = this.chart.yScale()(0);
663
- return Math.min(this.bottom(), Math.max(this.chart.plot().top, zero));
664
- }, ...(ngDevMode ? [{ debugName: "baseline" }] : []));
665
- // A tick at each datum, with its label: the chart makes the labels from
666
- // the `dataKey` of its x axis.
667
- ticks = computed(() => {
668
- const xs = this.chart.xPositions();
669
- return this.chart
670
- .xLabels()
671
- .map((label, index) => ({ offset: xs[index], label }));
672
- }, ...(ngDevMode ? [{ debugName: "ticks" }] : []));
673
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraXAxis, deps: null, target: i0.ɵɵFactoryTarget.Component });
674
- static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.35", type: MiraXAxis, isStandalone: true, selector: "mira-x-axis", inputs: { dataKey: { classPropertyName: "dataKey", publicName: "dataKey", isSignal: true, isRequired: false, transformFunction: null }, height: { classPropertyName: "height", publicName: "height", isSignal: true, isRequired: false, transformFunction: null } }, providers: [
675
- { provide: MiraChartLayer, useExisting: forwardRef(() => MiraXAxis) },
676
- ], usesInheritance: true, ngImport: i0, template: `
677
- <ng-template>
678
- <svg:g class="mira-x-axis">
679
- <svg:line
680
- [attr.x1]="chart.plot().left"
681
- [attr.x2]="chart.plot().left + chart.plot().width"
682
- [attr.y1]="baseline()"
683
- [attr.y2]="baseline()"
684
- />
685
- @for (tick of ticks(); track $index) {
686
- <svg:g
687
- [attr.transform]="'translate(' + tick.offset + ',' + bottom() + ')'"
688
- >
689
- <svg:line y2="6" />
690
- <svg:text y="9" dy="0.71em" text-anchor="middle">
691
- {{ tick.label }}
692
- </svg:text>
693
- </svg:g>
694
- }
695
- </svg:g>
696
- </ng-template>
697
- `, isInline: true, styles: [".mira-x-axis{font-size:12px}line{stroke:var(--_mira-axis-color)}text{fill:var(--_mira-axis-color)}\n"], changeDetection: i0.ChangeDetectionStrategy.OnPush });
698
- }
699
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraXAxis, decorators: [{
700
- type: Component,
701
- args: [{ selector: 'mira-x-axis', changeDetection: ChangeDetectionStrategy.OnPush, providers: [
702
- { provide: MiraChartLayer, useExisting: forwardRef(() => MiraXAxis) },
703
- ], template: `
704
- <ng-template>
705
- <svg:g class="mira-x-axis">
706
- <svg:line
707
- [attr.x1]="chart.plot().left"
708
- [attr.x2]="chart.plot().left + chart.plot().width"
709
- [attr.y1]="baseline()"
710
- [attr.y2]="baseline()"
711
- />
712
- @for (tick of ticks(); track $index) {
713
- <svg:g
714
- [attr.transform]="'translate(' + tick.offset + ',' + bottom() + ')'"
715
- >
716
- <svg:line y2="6" />
717
- <svg:text y="9" dy="0.71em" text-anchor="middle">
718
- {{ tick.label }}
719
- </svg:text>
720
- </svg:g>
721
- }
722
- </svg:g>
723
- </ng-template>
724
- `, styles: [".mira-x-axis{font-size:12px}line{stroke:var(--_mira-axis-color)}text{fill:var(--_mira-axis-color)}\n"] }]
725
- }], propDecorators: { dataKey: [{ type: i0.Input, args: [{ isSignal: true, alias: "dataKey", required: false }] }], height: [{ type: i0.Input, args: [{ isSignal: true, alias: "height", required: false }] }] } });
726
-
727
- /**
728
- * The value axis left of the plot. It fits the values of every series in the
729
- * chart that isn't hidden, and zero, rounded out to round ticks. The ticks
730
- * read as numbers in the app's locale (`LOCALE_ID`).
731
- *
732
- * @example
733
- * ```html
734
- * <mira-y-axis [tickCount]="4" />
735
- * ```
736
- */
737
- class MiraYAxis extends MiraChartLayer {
738
- /** The room the axis takes left of the plot, in px: 60 by default. */
739
- width = input(60, ...(ngDevMode ? [{ debugName: "width", transform: numberAttribute }] : [{ transform: numberAttribute }]));
740
- /**
741
- * About how many ticks to show, 5 by default. The axis keeps its ticks 1, 2
742
- * or 5 times a power of ten apart, so it may show a few more or a few fewer.
743
- */
744
- tickCount = input(DEFAULT_TICK_COUNT, ...(ngDevMode ? [{ debugName: "tickCount", transform: numberAttribute }] : [{
745
- transform: numberAttribute,
426
+ tickCount = input(DEFAULT_TICK_COUNT, ...(ngDevMode ? [{ debugName: "tickCount", transform: numberAttribute }] : [{
427
+ transform: numberAttribute,
746
428
  }]));
747
429
  chart = inject(CartesianContext);
748
430
  x = computed(() => this.chart.plot().left, ...(ngDevMode ? [{ debugName: "x" }] : []));
@@ -1035,7 +717,7 @@ class MiraCartesianChart {
1035
717
  xAxis = contentChild(MiraXAxis, ...(ngDevMode ? [{ debugName: "xAxis", descendants: false }] : [{ descendants: false }]));
1036
718
  yAxis = contentChild(MiraYAxis, ...(ngDevMode ? [{ debugName: "yAxis", descendants: false }] : [{ descendants: false }]));
1037
719
  /** The chart's legend, if it has one: the plot leaves room for it too. */
1038
- legend = contentChild(MiraLegend, ...(ngDevMode ? [{ debugName: "legend", descendants: false }] : [{ descendants: false }]));
720
+ legend = contentChild(ChartLegend, ...(ngDevMode ? [{ debugName: "legend", descendants: false }] : [{ descendants: false }]));
1039
721
  /** The chart's svg, where the pointer is tracked. */
1040
722
  svg = viewChild.required('svg');
1041
723
  viewBox = computed(() => {
@@ -1106,7 +788,7 @@ class MiraCartesianChart {
1106
788
  return { x, y };
1107
789
  }
1108
790
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraCartesianChart, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1109
- static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.2.0", version: "20.3.35", type: MiraCartesianChart, isStandalone: true, inputs: { data: { classPropertyName: "data", publicName: "data", isSignal: true, isRequired: false, transformFunction: null }, width: { classPropertyName: "width", publicName: "width", isSignal: true, isRequired: false, transformFunction: null }, height: { classPropertyName: "height", publicName: "height", isSignal: true, isRequired: false, transformFunction: null }, margin: { classPropertyName: "margin", publicName: "margin", isSignal: true, isRequired: false, transformFunction: null }, ariaLabel: { classPropertyName: "ariaLabel", publicName: "ariaLabel", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "style.display": "\"block\"", "style.height": "height() === undefined ? \"100%\" : null" } }, queries: [{ propertyName: "series", predicate: MiraSeries, isSignal: true }, { propertyName: "layers", predicate: MiraChartLayer, isSignal: true }, { propertyName: "xAxis", first: true, predicate: MiraXAxis, isSignal: true }, { propertyName: "yAxis", first: true, predicate: MiraYAxis, isSignal: true }, { propertyName: "legend", first: true, predicate: MiraLegend, isSignal: true }], viewQueries: [{ propertyName: "svg", first: true, predicate: ["svg"], descendants: true, isSignal: true }], ngImport: i0 });
791
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.2.0", version: "20.3.35", type: MiraCartesianChart, isStandalone: true, inputs: { data: { classPropertyName: "data", publicName: "data", isSignal: true, isRequired: false, transformFunction: null }, width: { classPropertyName: "width", publicName: "width", isSignal: true, isRequired: false, transformFunction: null }, height: { classPropertyName: "height", publicName: "height", isSignal: true, isRequired: false, transformFunction: null }, margin: { classPropertyName: "margin", publicName: "margin", isSignal: true, isRequired: false, transformFunction: null }, ariaLabel: { classPropertyName: "ariaLabel", publicName: "ariaLabel", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "style.display": "\"block\"", "style.height": "height() === undefined ? \"100%\" : null" } }, queries: [{ propertyName: "series", predicate: MiraSeries, isSignal: true }, { propertyName: "layers", predicate: MiraChartLayer, isSignal: true }, { propertyName: "xAxis", first: true, predicate: MiraXAxis, isSignal: true }, { propertyName: "yAxis", first: true, predicate: MiraYAxis, isSignal: true }, { propertyName: "legend", first: true, predicate: ChartLegend, isSignal: true }], viewQueries: [{ propertyName: "svg", first: true, predicate: ["svg"], descendants: true, isSignal: true }], ngImport: i0 });
1110
792
  }
1111
793
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraCartesianChart, decorators: [{
1112
794
  type: Directive,
@@ -1118,7 +800,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImpo
1118
800
  '[style.height]': 'height() === undefined ? "100%" : null',
1119
801
  },
1120
802
  }]
1121
- }], ctorParameters: () => [], propDecorators: { data: [{ type: i0.Input, args: [{ isSignal: true, alias: "data", required: false }] }], width: [{ type: i0.Input, args: [{ isSignal: true, alias: "width", required: false }] }], height: [{ type: i0.Input, args: [{ isSignal: true, alias: "height", required: false }] }], margin: [{ type: i0.Input, args: [{ isSignal: true, alias: "margin", required: false }] }], ariaLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "ariaLabel", required: false }] }], series: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => MiraSeries), { isSignal: true }] }], layers: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => MiraChartLayer), { isSignal: true }] }], xAxis: [{ type: i0.ContentChild, args: [i0.forwardRef(() => MiraXAxis), { ...{ descendants: false }, isSignal: true }] }], yAxis: [{ type: i0.ContentChild, args: [i0.forwardRef(() => MiraYAxis), { ...{ descendants: false }, isSignal: true }] }], legend: [{ type: i0.ContentChild, args: [i0.forwardRef(() => MiraLegend), { ...{ descendants: false }, isSignal: true }] }], svg: [{ type: i0.ViewChild, args: ['svg', { isSignal: true }] }] } });
803
+ }], ctorParameters: () => [], propDecorators: { data: [{ type: i0.Input, args: [{ isSignal: true, alias: "data", required: false }] }], width: [{ type: i0.Input, args: [{ isSignal: true, alias: "width", required: false }] }], height: [{ type: i0.Input, args: [{ isSignal: true, alias: "height", required: false }] }], margin: [{ type: i0.Input, args: [{ isSignal: true, alias: "margin", required: false }] }], ariaLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "ariaLabel", required: false }] }], series: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => MiraSeries), { isSignal: true }] }], layers: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => MiraChartLayer), { isSignal: true }] }], xAxis: [{ type: i0.ContentChild, args: [i0.forwardRef(() => MiraXAxis), { ...{ descendants: false }, isSignal: true }] }], yAxis: [{ type: i0.ContentChild, args: [i0.forwardRef(() => MiraYAxis), { ...{ descendants: false }, isSignal: true }] }], legend: [{ type: i0.ContentChild, args: [i0.forwardRef(() => ChartLegend), { ...{ descendants: false }, isSignal: true }] }], svg: [{ type: i0.ViewChild, args: ['svg', { isSignal: true }] }] } });
1122
804
  function sameSize(a, b) {
1123
805
  return a?.width === b?.width && a?.height === b?.height;
1124
806
  }
@@ -1567,6 +1249,337 @@ function beside(before, after, size, start, length) {
1567
1249
  return Math.max(start, Math.min(after, end - size));
1568
1250
  }
1569
1251
 
1252
+ /**
1253
+ * Marks a template of your own for what each item of `<mira-legend>` shows,
1254
+ * in place of its icon and its name. It flows as text inside the item, which
1255
+ * stays the legend's: in a toggleable legend, a button, marked `aria-pressed`,
1256
+ * that hides or shows the series. A hidden series' item takes a gray color,
1257
+ * which your template can keep or set its own from `item.hidden`. Its context
1258
+ * is a `LegendItemContext`, and typed: `let-item` holds the series' `name`,
1259
+ * `color`, `icon` and `hidden`.
1260
+ *
1261
+ * Screen readers read the template's text out, as the button's name in a
1262
+ * toggleable legend:
1263
+ *
1264
+ * - keep the series' name in it, the same whether the series is hidden or
1265
+ * not, and show the state in the look: the button already tells it;
1266
+ * - mark what only decorates, such as a swatch or an emoji,
1267
+ * `aria-hidden="true"`;
1268
+ * - in a toggleable legend, put no buttons or links in it, as it's inside a
1269
+ * button.
1270
+ *
1271
+ * Forced colors replace the colors you set, and print drops backgrounds by
1272
+ * default: give what shows the series' color `forced-color-adjust: none` and
1273
+ * `print-color-adjust: exact`, as the legend's own icons have.
1274
+ *
1275
+ * @example
1276
+ * ```html
1277
+ * <mira-legend>
1278
+ * <ng-template miraLegendItemContent let-item>
1279
+ * <span
1280
+ * aria-hidden="true"
1281
+ * [style.color]="item.hidden ? null : item.color"
1282
+ * >●</span>
1283
+ * <b>{{ item.name }}</b>
1284
+ * </ng-template>
1285
+ * </mira-legend>
1286
+ * ```
1287
+ */
1288
+ class MiraLegendItemContent {
1289
+ /**
1290
+ * Types the template's context, for Angular's template checks.
1291
+ *
1292
+ * @internal
1293
+ */
1294
+ static ngTemplateContextGuard(directive, context) {
1295
+ return true;
1296
+ }
1297
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraLegendItemContent, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1298
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.3.35", type: MiraLegendItemContent, isStandalone: true, selector: "ng-template[miraLegendItemContent]", ngImport: i0 });
1299
+ }
1300
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraLegendItemContent, decorators: [{
1301
+ type: Directive,
1302
+ args: [{ selector: 'ng-template[miraLegendItemContent]' }]
1303
+ }] });
1304
+ // The height of a legend of one row along the top or the bottom, as its
1305
+ // styles lay it out in the browser's default font size, in px: the row and
1306
+ // the room beside it for the focus ring. The plot leaves it until the legend
1307
+ // is measured, on the server.
1308
+ const ROW_HEIGHT = 22;
1309
+ /**
1310
+ * Lists the chart's series along one of its edges, in the order they're
1311
+ * written, each with its icon in its color and its name: a stretch of line
1312
+ * marks a line, a square marks bars, and a circle any other series (the
1313
+ * series' `legendIcon`). A series is called by its `name`, or its `dataKey`
1314
+ * without one. The plot leaves room for the legend, as it does for the axes,
1315
+ * and the chart stays as big as it is.
1316
+ *
1317
+ * Along the bottom, by default, or the top, the items go in rows, centered,
1318
+ * as many as they take. Up the right or the left side, they go in a column in
1319
+ * the middle of the chart's height, at most 40% of its width wide, where a
1320
+ * longer name wraps. Left and right are as the svg is drawn, in a page that
1321
+ * runs right to left too, where the y axis stays on the left. A legend on a
1322
+ * side leaves the plot no room on the server: its width is known only once
1323
+ * the browser lays it out.
1324
+ *
1325
+ * It's a list to read, unless it's `toggleable`: then each item is a button
1326
+ * that hides its series, or shows it again, and the chart fits its y axis to
1327
+ * the series that are left. The state is the series' own `hidden`, to bind
1328
+ * as `[(hidden)]`. A hidden series' item stays, grayed and struck through.
1329
+ * In a toggleable legend it's marked `aria-pressed="false"` for screen
1330
+ * readers; in a list to read, screen readers and the keyboard skip it, as the
1331
+ * chart doesn't draw it.
1332
+ *
1333
+ * The legend's text takes the `color` around it, the chart's in a chart. The
1334
+ * rest are CSS custom properties, to set anywhere above the legend:
1335
+ *
1336
+ * - `--mira-font-size`: the size of the text, `0.75rem` by default; the room
1337
+ * around the text follows it;
1338
+ * - `--mira-legend-inactive-color`: a hidden series' item,
1339
+ * `--mira-axis-color` by default.
1340
+ *
1341
+ * For items that show something of your own, write their template inside, as
1342
+ * an `<ng-template miraLegendItemContent>`, and import `MiraLegendItemContent`
1343
+ * along with `MiraLegend`: without it, the template goes unnoticed, and the
1344
+ * legend shows its own icon and name.
1345
+ *
1346
+ * Anywhere else on the page, name the chart with `for`. There the legend sits
1347
+ * where the page puts it, as a block, and the chart leaves it no room; its
1348
+ * `position` only picks its layout, rows for the top or the bottom, a column
1349
+ * for a side. Its colors come from above it, as in a chart: set the custom
1350
+ * properties above both to change them alike.
1351
+ *
1352
+ * @example
1353
+ * ```html
1354
+ * <mira-line-chart [data]="day">
1355
+ * <mira-x-axis dataKey="hour" />
1356
+ * <mira-y-axis />
1357
+ * <mira-line dataKey="solar" name="Solar" stroke="#d97706" />
1358
+ * <mira-line dataKey="usage" name="Usage" stroke="#3b82f6" />
1359
+ * <mira-legend position="right" toggleable />
1360
+ * </mira-line-chart>
1361
+ * ```
1362
+ *
1363
+ * ```html
1364
+ * <header>
1365
+ * <h2>A sunny day</h2>
1366
+ * <mira-legend [for]="chart" />
1367
+ * </header>
1368
+ * <mira-line-chart #chart [data]="day">…</mira-line-chart>
1369
+ * ```
1370
+ */
1371
+ class MiraLegend {
1372
+ /** The edge of the chart the legend sits along: `'bottom'` by default. */
1373
+ position = input('bottom', ...(ngDevMode ? [{ debugName: "position" }] : []));
1374
+ /**
1375
+ * Whether a click on an item, or Enter or Space on it, hides its series or
1376
+ * shows it again. Off by default: the legend is then a list to read.
1377
+ */
1378
+ toggleable = input(false, ...(ngDevMode ? [{ debugName: "toggleable", transform: booleanAttribute }] : [{ transform: booleanAttribute }]));
1379
+ /**
1380
+ * The chart whose series the legend lists, for a legend out of it: give
1381
+ * the chart a reference, `<mira-line-chart #chart>`, and name it,
1382
+ * `<mira-legend [for]="chart" />`. Unset, the chart the legend is in. With
1383
+ * no chart, as while a chart named through a query is in an `@if` that's
1384
+ * off, the legend lists nothing.
1385
+ */
1386
+ for = input(...(ngDevMode ? [undefined, { debugName: "for" }] : []));
1387
+ /**
1388
+ * The room the legend takes from the chart along its edge, in px: its
1389
+ * height along the top or the bottom, its width up a side, as the browser
1390
+ * last laid it out there. Until then, a row's height along the top or the
1391
+ * bottom, and none on a side.
1392
+ *
1393
+ * @internal
1394
+ */
1395
+ room = computed(() => this.#measured() ?? (inRows(this.position()) ? ROW_HEIGHT : 0), ...(ngDevMode ? [{ debugName: "room" }] : []));
1396
+ // Where the legend sits: at an edge of the chart it's in. Out of a chart,
1397
+ // where the page puts it.
1398
+ placement = computed(() => this.#parent ? this.position() : null, ...(ngDevMode ? [{ debugName: "placement" }] : []));
1399
+ // How it lays out its items: in rows, or in a column.
1400
+ layout = computed(() => inRows(this.position()) ? 'rows' : 'column', ...(ngDevMode ? [{ debugName: "layout" }] : []));
1401
+ // What the legend lists: each series of the chart it names, or of the one
1402
+ // it's in, hidden ones too.
1403
+ entries = computed(() => {
1404
+ const all = (this.for() ?? this.#parent)?.series() ?? [];
1405
+ return all.flatMap((series) => {
1406
+ const name = nameOf(series);
1407
+ if (name === null)
1408
+ return [];
1409
+ const item = {
1410
+ name,
1411
+ color: series.color(),
1412
+ icon: series.legendIcon,
1413
+ hidden: series.hidden(),
1414
+ };
1415
+ return { series, item };
1416
+ });
1417
+ }, ...(ngDevMode ? [{ debugName: "entries" }] : []));
1418
+ // A template of your own for the items, if there's one.
1419
+ template = contentChild(MiraLegendItemContent, ...(ngDevMode ? [{ debugName: "template", read: TemplateRef }] : [{
1420
+ read: TemplateRef,
1421
+ }]));
1422
+ #host = inject(ElementRef).nativeElement;
1423
+ // The chart the legend is in, if it's in one.
1424
+ #parent = inject(CartesianContext, { optional: true });
1425
+ // The room the legend took, as the browser last laid it out: none yet on a
1426
+ // side it has just moved to, which the old room says nothing about.
1427
+ #measured = linkedSignal(...(ngDevMode ? [{ debugName: "#measured", source: this.position,
1428
+ computation: () => null }] : [{
1429
+ source: this.position,
1430
+ computation: () => null,
1431
+ }]));
1432
+ /** @internal */
1433
+ constructor() {
1434
+ const parent = this.#parent;
1435
+ const destroyRef = inject(DestroyRef);
1436
+ // Out of a chart, there's no room to leave the legend, and nothing to
1437
+ // measure.
1438
+ if (!parent)
1439
+ return;
1440
+ // The legend is measured after a render that changes what it lists, how
1441
+ // it shows it, its side, or the chart's size, which can wrap it onto more
1442
+ // rows or fewer; and the plot leaves it room before the page is painted.
1443
+ afterRenderEffect({
1444
+ earlyRead: () => {
1445
+ this.entries();
1446
+ this.template();
1447
+ parent.size();
1448
+ return this.#measure();
1449
+ },
1450
+ write: (room) => this.#measured.set(room()),
1451
+ });
1452
+ // And again whenever it changes size on its own, as when a font loads,
1453
+ // or an item of your own that changes, padding and border included: then
1454
+ // the plot follows a frame later.
1455
+ afterNextRender(() => {
1456
+ const observer = new ResizeObserver(() => this.#measured.set(this.#measure()));
1457
+ observer.observe(this.#host, { box: 'border-box' });
1458
+ destroyRef.onDestroy(() => observer.disconnect());
1459
+ });
1460
+ }
1461
+ // Hides the series, or shows it again.
1462
+ toggle(series) {
1463
+ series.hidden.update((hidden) => !hidden);
1464
+ }
1465
+ // The room the legend takes along its edge as laid out, in the svg's px: a
1466
+ // transform on the page around the chart scales both alike.
1467
+ #measure() {
1468
+ const { offsetWidth, offsetHeight } = this.#host;
1469
+ return inRows(this.position()) ? offsetHeight : offsetWidth;
1470
+ }
1471
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraLegend, deps: [], target: i0.ɵɵFactoryTarget.Component });
1472
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.35", type: MiraLegend, isStandalone: true, selector: "mira-legend", inputs: { position: { classPropertyName: "position", publicName: "position", isSignal: true, isRequired: false, transformFunction: null }, toggleable: { classPropertyName: "toggleable", publicName: "toggleable", isSignal: true, isRequired: false, transformFunction: null }, for: { classPropertyName: "for", publicName: "for", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "attr.data-position": "placement()", "attr.data-layout": "layout()" } }, providers: [
1473
+ { provide: ChartLegend, useExisting: forwardRef(() => MiraLegend) },
1474
+ ], queries: [{ propertyName: "template", first: true, predicate: MiraLegendItemContent, descendants: true, read: TemplateRef, isSignal: true }], ngImport: i0, template: `
1475
+ <ul class="mira-legend" role="list">
1476
+ @for (entry of entries(); track entry.series) {
1477
+ <li
1478
+ [attr.data-hidden]="entry.item.hidden || null"
1479
+ [attr.inert]="entry.item.hidden && !toggleable() ? '' : null"
1480
+ >
1481
+ @if (toggleable()) {
1482
+ <button
1483
+ type="button"
1484
+ class="mira-legend-item"
1485
+ [attr.aria-pressed]="!entry.item.hidden"
1486
+ (click)="toggle(entry.series)"
1487
+ >
1488
+ <ng-container
1489
+ [ngTemplateOutlet]="template() ?? own"
1490
+ [ngTemplateOutletContext]="{ $implicit: entry.item }"
1491
+ />
1492
+ </button>
1493
+ } @else {
1494
+ <ng-container
1495
+ [ngTemplateOutlet]="template() ?? own"
1496
+ [ngTemplateOutletContext]="{ $implicit: entry.item }"
1497
+ />
1498
+ }
1499
+ </li>
1500
+ }
1501
+ </ul>
1502
+
1503
+ <ng-template #own let-item>
1504
+ <span class="mira-legend-label">
1505
+ <span
1506
+ class="mira-legend-icon"
1507
+ [attr.data-icon]="item.icon"
1508
+ [style.color]="item.hidden ? null : item.color"
1509
+ ></span>
1510
+ <span class="mira-legend-name">{{ item.name }}</span>
1511
+ </span>
1512
+ </ng-template>
1513
+ `, isInline: true, styles: [":host{--_mira-axis-color: var(--mira-axis-color, #666);--_mira-grid-color: var(--mira-grid-color, #ccc);--_mira-background: var(--mira-background, #fff);--_mira-cursor-color: var(--mira-cursor-color, #999)}@supports (color: light-dark(#000,#fff)){:host{--_mira-axis-color: var(--mira-axis-color, light-dark(#666, #999));--_mira-grid-color: var(--mira-grid-color, light-dark(#ccc, #333));--_mira-background: var(--mira-background, Canvas);--_mira-cursor-color: var(--mira-cursor-color, light-dark(#999, #666))}}\n", ":host{display:block}:host([data-position]){position:absolute}:host([data-position=\"top\"]){inset:0 0 auto}:host([data-position=\"bottom\"]){inset:auto 0 0}:host([data-position=\"left\"]){inset:0 auto 0 0}:host([data-position=\"right\"]){inset:0 0 0 auto}:host([data-position=\"left\"]),:host([data-position=\"right\"]){display:flex;align-items:center;align-items:safe center;max-width:40%}.mira-legend{display:flex;flex-wrap:wrap;justify-content:center;justify-content:safe center;gap:max(.5em,24px - 1.5em) 1.25em;margin:0;padding:0 .75em;list-style:none;font-size:var(--mira-font-size, .75rem);line-height:1.5;overflow-wrap:anywhere}:host([data-position=\"top\"]) .mira-legend{padding-top:4px}:host([data-position=\"bottom\"]) .mira-legend{padding-bottom:4px}:host([data-layout=\"column\"]) .mira-legend{flex-direction:column;align-items:flex-start}:host([data-position]) li{pointer-events:auto}.mira-legend-item{all:unset;display:block;cursor:pointer}.mira-legend-item:focus-visible{outline:revert;outline-offset:2px}li[data-hidden]{color:var(--mira-legend-inactive-color, var(--_mira-axis-color))}li[data-hidden] .mira-legend-name{text-decoration:line-through}@media(forced-colors:active){li[data-hidden]{color:GrayText}}.mira-legend-label{display:flex;gap:.5em}.mira-legend-icon{display:grid;flex:none;place-items:center;width:1em;height:1.5em;forced-color-adjust:none;-webkit-print-color-adjust:exact;print-color-adjust:exact}.mira-legend-icon:before{content:\"\";width:.75em;height:.75em;border-radius:50%;background:currentColor}.mira-legend-icon[data-icon=square]:before{border-radius:.125em}.mira-legend-icon[data-icon=line]:before{width:100%;height:2px;border-radius:0}\n"], dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
1514
+ }
1515
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraLegend, decorators: [{
1516
+ type: Component,
1517
+ args: [{ selector: 'mira-legend', changeDetection: ChangeDetectionStrategy.OnPush, imports: [NgTemplateOutlet], providers: [
1518
+ { provide: ChartLegend, useExisting: forwardRef(() => MiraLegend) },
1519
+ ], host: {
1520
+ '[attr.data-position]': 'placement()',
1521
+ '[attr.data-layout]': 'layout()',
1522
+ }, template: `
1523
+ <ul class="mira-legend" role="list">
1524
+ @for (entry of entries(); track entry.series) {
1525
+ <li
1526
+ [attr.data-hidden]="entry.item.hidden || null"
1527
+ [attr.inert]="entry.item.hidden && !toggleable() ? '' : null"
1528
+ >
1529
+ @if (toggleable()) {
1530
+ <button
1531
+ type="button"
1532
+ class="mira-legend-item"
1533
+ [attr.aria-pressed]="!entry.item.hidden"
1534
+ (click)="toggle(entry.series)"
1535
+ >
1536
+ <ng-container
1537
+ [ngTemplateOutlet]="template() ?? own"
1538
+ [ngTemplateOutletContext]="{ $implicit: entry.item }"
1539
+ />
1540
+ </button>
1541
+ } @else {
1542
+ <ng-container
1543
+ [ngTemplateOutlet]="template() ?? own"
1544
+ [ngTemplateOutletContext]="{ $implicit: entry.item }"
1545
+ />
1546
+ }
1547
+ </li>
1548
+ }
1549
+ </ul>
1550
+
1551
+ <ng-template #own let-item>
1552
+ <span class="mira-legend-label">
1553
+ <span
1554
+ class="mira-legend-icon"
1555
+ [attr.data-icon]="item.icon"
1556
+ [style.color]="item.hidden ? null : item.color"
1557
+ ></span>
1558
+ <span class="mira-legend-name">{{ item.name }}</span>
1559
+ </span>
1560
+ </ng-template>
1561
+ `, styles: [":host{--_mira-axis-color: var(--mira-axis-color, #666);--_mira-grid-color: var(--mira-grid-color, #ccc);--_mira-background: var(--mira-background, #fff);--_mira-cursor-color: var(--mira-cursor-color, #999)}@supports (color: light-dark(#000,#fff)){:host{--_mira-axis-color: var(--mira-axis-color, light-dark(#666, #999));--_mira-grid-color: var(--mira-grid-color, light-dark(#ccc, #333));--_mira-background: var(--mira-background, Canvas);--_mira-cursor-color: var(--mira-cursor-color, light-dark(#999, #666))}}\n", ":host{display:block}:host([data-position]){position:absolute}:host([data-position=\"top\"]){inset:0 0 auto}:host([data-position=\"bottom\"]){inset:auto 0 0}:host([data-position=\"left\"]){inset:0 auto 0 0}:host([data-position=\"right\"]){inset:0 0 0 auto}:host([data-position=\"left\"]),:host([data-position=\"right\"]){display:flex;align-items:center;align-items:safe center;max-width:40%}.mira-legend{display:flex;flex-wrap:wrap;justify-content:center;justify-content:safe center;gap:max(.5em,24px - 1.5em) 1.25em;margin:0;padding:0 .75em;list-style:none;font-size:var(--mira-font-size, .75rem);line-height:1.5;overflow-wrap:anywhere}:host([data-position=\"top\"]) .mira-legend{padding-top:4px}:host([data-position=\"bottom\"]) .mira-legend{padding-bottom:4px}:host([data-layout=\"column\"]) .mira-legend{flex-direction:column;align-items:flex-start}:host([data-position]) li{pointer-events:auto}.mira-legend-item{all:unset;display:block;cursor:pointer}.mira-legend-item:focus-visible{outline:revert;outline-offset:2px}li[data-hidden]{color:var(--mira-legend-inactive-color, var(--_mira-axis-color))}li[data-hidden] .mira-legend-name{text-decoration:line-through}@media(forced-colors:active){li[data-hidden]{color:GrayText}}.mira-legend-label{display:flex;gap:.5em}.mira-legend-icon{display:grid;flex:none;place-items:center;width:1em;height:1.5em;forced-color-adjust:none;-webkit-print-color-adjust:exact;print-color-adjust:exact}.mira-legend-icon:before{content:\"\";width:.75em;height:.75em;border-radius:50%;background:currentColor}.mira-legend-icon[data-icon=square]:before{border-radius:.125em}.mira-legend-icon[data-icon=line]:before{width:100%;height:2px;border-radius:0}\n"] }]
1562
+ }], ctorParameters: () => [], propDecorators: { position: [{ type: i0.Input, args: [{ isSignal: true, alias: "position", required: false }] }], toggleable: [{ type: i0.Input, args: [{ isSignal: true, alias: "toggleable", required: false }] }], for: [{ type: i0.Input, args: [{ isSignal: true, alias: "for", required: false }] }], template: [{ type: i0.ContentChild, args: [i0.forwardRef(() => MiraLegendItemContent), { ...{
1563
+ read: TemplateRef,
1564
+ }, isSignal: true }] }] } });
1565
+ // What the series is called, or null while it has no `dataKey` yet. Angular
1566
+ // binds a series' inputs as it checks the block the series is written in,
1567
+ // and a legend in a block it checks before reads the series first: such a
1568
+ // series joins the list as soon as its inputs are bound, in the same pass.
1569
+ function nameOf(series) {
1570
+ try {
1571
+ return series.displayName();
1572
+ }
1573
+ catch {
1574
+ return null;
1575
+ }
1576
+ }
1577
+ // Whether a legend on this side lists its items in rows, rather than in a
1578
+ // column.
1579
+ function inRows(side) {
1580
+ return side === 'top' || side === 'bottom';
1581
+ }
1582
+
1570
1583
  /**
1571
1584
  * The providers of a kind of cartesian chart: under `MiraCartesianChart`, for
1572
1585
  * layers of your own, and under the context the library's layers inject.