@pioneer-code/pioneer-charts 22.2.12 → 22.2.14
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 +3 -2
- package/fesm2022/pioneer-code-pioneer-charts.mjs +608 -233
- package/fesm2022/pioneer-code-pioneer-charts.mjs.map +1 -1
- package/package.json +12 -3
- package/scss/pioneer-charts.scss +12 -1
- package/themes/pioneer-charts.css +9 -0
- package/themes/pioneer-charts.min.css +1 -1
- package/types/pioneer-code-pioneer-charts.d.ts +133 -613
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { Injectable, inject, ApplicationRef, DestroyRef, afterNextRender, TemplateRef, Directive, input, viewChild, contentChild, effect, ViewEncapsulation, Component, model, output, computed } from '@angular/core';
|
|
2
|
+
import { Injectable, inject, ApplicationRef, DOCUMENT, DestroyRef, afterNextRender, TemplateRef, Directive, input, viewChild, contentChild, effect, ViewEncapsulation, Component, model, output, computed } from '@angular/core';
|
|
3
|
+
import { takeUntilDestroyed, outputFromObservable } from '@angular/core/rxjs-interop';
|
|
3
4
|
import { color } from 'd3-color';
|
|
4
|
-
import { scaleLinear, scaleBand } from 'd3-scale';
|
|
5
|
-
import { select
|
|
5
|
+
import { scaleLinear, scaleBand, scaleTime } from 'd3-scale';
|
|
6
|
+
import { select } from 'd3-selection';
|
|
7
|
+
import { transition } from 'd3-transition';
|
|
6
8
|
import { Subject } from 'rxjs';
|
|
7
9
|
import { axisLeft, axisBottom } from 'd3-axis';
|
|
8
10
|
import { format } from 'd3-format';
|
|
9
|
-
import { select, scaleTime, zoom } from 'd3';
|
|
10
|
-
import { takeUntilDestroyed, outputFromObservable } from '@angular/core/rxjs-interop';
|
|
11
11
|
import { debounceTime } from 'rxjs/operators';
|
|
12
|
-
import { transition } from 'd3-transition';
|
|
13
12
|
import { range } from 'd3-array';
|
|
14
13
|
import { line, area, arc, pie } from 'd3-shape';
|
|
14
|
+
import { zoom } from 'd3-zoom';
|
|
15
15
|
import { interpolate } from 'd3-interpolate';
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -39,6 +39,12 @@ class PcacChartConfig {
|
|
|
39
39
|
* initializer still applies to anything constructed via `new`.
|
|
40
40
|
*/
|
|
41
41
|
heightFull = false;
|
|
42
|
+
/**
|
|
43
|
+
* What the chart shows, for screen readers - e.g. `'Sales by month, 2026'`. The chart's `<svg>` is
|
|
44
|
+
* announced as one image with this name; when it isn't set, the chart's type is used (`'Bar
|
|
45
|
+
* chart'`, `'Pie chart'`, ...). Optional for the same reason as `heightFull`.
|
|
46
|
+
*/
|
|
47
|
+
ariaLabel;
|
|
42
48
|
}
|
|
43
49
|
/**
|
|
44
50
|
* Up to three short labels placed along an axis by position rather than by value: `min` at its
|
|
@@ -210,6 +216,12 @@ class PcacAxisChartConfig extends PcacChartConfig {
|
|
|
210
216
|
class PcacData {
|
|
211
217
|
key = null;
|
|
212
218
|
value = null;
|
|
219
|
+
/**
|
|
220
|
+
* Hides the item while keeping its place in the color order, so a `<pcac-legend>` can toggle it
|
|
221
|
+
* without the other items changing color. A line/area/plot series draws no line, area, points or
|
|
222
|
+
* hover effects; a bar (or every bar of a group) is drawn at zero size, keeping its slot; a pie
|
|
223
|
+
* slice takes no angle.
|
|
224
|
+
*/
|
|
213
225
|
hide = false;
|
|
214
226
|
data = [];
|
|
215
227
|
/**
|
|
@@ -261,13 +273,58 @@ var PcacFormatEnum;
|
|
|
261
273
|
PcacFormatEnum["DateTime"] = "dateTime";
|
|
262
274
|
PcacFormatEnum["Fahrenheit"] = "fahrenheit";
|
|
263
275
|
/**
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
* e.g. 0 = 12am, 1 = 1am, 13 = 1pm, 23 = 11pm
|
|
276
|
+
* Hours of the day, 0-24, labelled on a 12 hour clock: 0 = 12am, 1 = 1am, 13 = 1pm, 24 = 12am.
|
|
277
|
+
* A value between hours shows its minutes (2.5 = 2:30am).
|
|
267
278
|
*/
|
|
268
279
|
PcacFormatEnum["OneDayHours"] = "oneDayHours";
|
|
269
280
|
})(PcacFormatEnum || (PcacFormatEnum = {}));
|
|
270
281
|
|
|
282
|
+
/**
|
|
283
|
+
* `Percentage` values are fractions - `0.25` is 25% - on the axis and in the tooltip alike.
|
|
284
|
+
* `~` trims trailing zeros, so 0.07 reads `7%` rather than `7.000000%`.
|
|
285
|
+
*/
|
|
286
|
+
const formatPercent = format('~%');
|
|
287
|
+
/**
|
|
288
|
+
* An hour of the day, 0-24, on a 12-hour clock: 0 and 24 are `12am`, 13 is `1pm`, and a
|
|
289
|
+
* fractional hour gets its minutes (`2.5` is `2:30am`) rather than reading `2.5am`.
|
|
290
|
+
*/
|
|
291
|
+
function formatHourOfDay(value) {
|
|
292
|
+
let totalMinutes = Math.round(value * 60);
|
|
293
|
+
totalMinutes = ((totalMinutes % 1440) + 1440) % 1440;
|
|
294
|
+
const hour24 = Math.floor(totalMinutes / 60);
|
|
295
|
+
const minutes = totalMinutes % 60;
|
|
296
|
+
const hour = hour24 % 12 === 0 ? 12 : hour24 % 12;
|
|
297
|
+
const period = hour24 < 12 ? 'am' : 'pm';
|
|
298
|
+
return minutes === 0 ? `${hour}${period}` : `${hour}:${String(minutes).padStart(2, '0')}${period}`;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* The tick label format for an axis in `format`, shared by both axes (so a format means the same
|
|
302
|
+
* thing on x and y) and by anything that labels a value on that axis, like the line/area hover
|
|
303
|
+
* crosshair. Returns null for the formats that keep D3's default labels.
|
|
304
|
+
*
|
|
305
|
+
* Numeric formats take their precision from the scale's tick step where the scale has one: a
|
|
306
|
+
* fixed precision is fine for a whole domain but breaks down once zoom narrows it to a few units.
|
|
307
|
+
*/
|
|
308
|
+
function axisTickFormat(pcacFormat, scale, ticks) {
|
|
309
|
+
const withTicks = scale;
|
|
310
|
+
switch (pcacFormat) {
|
|
311
|
+
case PcacFormatEnum.Percentage:
|
|
312
|
+
return withTicks.tickFormat ? withTicks.tickFormat(ticks, '~%') : (d) => formatPercent(Number(d));
|
|
313
|
+
case PcacFormatEnum.Decimal:
|
|
314
|
+
// Plain numbers with thousands separators - `1,500`, `0.05` - rather than SI prefixes, which
|
|
315
|
+
// turned a differential of 0.05 into `50m` (reading as minutes, like the Minutes format).
|
|
316
|
+
return withTicks.tickFormat ? withTicks.tickFormat(ticks, ',~f') : (d) => format(',~f')(Number(d));
|
|
317
|
+
case PcacFormatEnum.Minutes:
|
|
318
|
+
return (d) => d + 'm';
|
|
319
|
+
case PcacFormatEnum.Fahrenheit:
|
|
320
|
+
return (d) => d + ' F';
|
|
321
|
+
case PcacFormatEnum.OneDayHours:
|
|
322
|
+
return (d) => formatHourOfDay(Number(d));
|
|
323
|
+
default:
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
271
328
|
class PcacAxisBuilder {
|
|
272
329
|
drawAxis(config) {
|
|
273
330
|
this.drawXAxis(config);
|
|
@@ -293,16 +350,9 @@ class PcacAxisBuilder {
|
|
|
293
350
|
if (config.yAxis.tickSize !== undefined) {
|
|
294
351
|
yAxis.tickSizeInner(config.yAxis.tickSize);
|
|
295
352
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
break;
|
|
300
|
-
case PcacFormatEnum.Minutes:
|
|
301
|
-
yAxis.tickFormat((d) => d + 'm');
|
|
302
|
-
break;
|
|
303
|
-
case PcacFormatEnum.Fahrenheit:
|
|
304
|
-
yAxis.tickFormat((d) => d + ' F');
|
|
305
|
-
break;
|
|
353
|
+
const yFormat = axisTickFormat(config.yAxis.format, config.yScale, config.yAxis.ticks);
|
|
354
|
+
if (yFormat) {
|
|
355
|
+
yAxis.tickFormat(yFormat);
|
|
306
356
|
}
|
|
307
357
|
const group = config.svg.append('g')
|
|
308
358
|
.attr('class', 'pcac-y-axis')
|
|
@@ -322,27 +372,9 @@ class PcacAxisBuilder {
|
|
|
322
372
|
if (config.xAxis.tickSize !== undefined) {
|
|
323
373
|
xAxis.tickSizeInner(config.xAxis.tickSize);
|
|
324
374
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
break;
|
|
329
|
-
case PcacFormatEnum.Minutes:
|
|
330
|
-
xAxis.tickFormat((d) => d + 'm');
|
|
331
|
-
break;
|
|
332
|
-
case PcacFormatEnum.Fahrenheit:
|
|
333
|
-
xAxis.tickFormat((d) => d + ' F');
|
|
334
|
-
break;
|
|
335
|
-
case PcacFormatEnum.OneDayHours:
|
|
336
|
-
xAxis.tickFormat((d) => {
|
|
337
|
-
const h = d;
|
|
338
|
-
const hour = h % 12 === 0 ? 12 : h % 12;
|
|
339
|
-
const period = h < 12 ? 'am' : 'pm';
|
|
340
|
-
return `${hour}${period}`;
|
|
341
|
-
});
|
|
342
|
-
break;
|
|
343
|
-
case PcacFormatEnum.Decimal:
|
|
344
|
-
xAxis.tickFormat(decimalTickFormat(config.xScale, config.xAxis.ticks));
|
|
345
|
-
break;
|
|
375
|
+
const xFormat = axisTickFormat(config.xAxis.format, config.xScale, config.xAxis.ticks);
|
|
376
|
+
if (xFormat) {
|
|
377
|
+
xAxis.tickFormat(xFormat);
|
|
346
378
|
}
|
|
347
379
|
const group = config.svg.append('g')
|
|
348
380
|
.attr('class', 'pcac-x-axis')
|
|
@@ -413,20 +445,6 @@ function applyColors(group, axis) {
|
|
|
413
445
|
.style('--pcac-axis-label-color', () => axis.labelColor ?? null)
|
|
414
446
|
.style('--pcac-axis-sub-label-color', () => axis.subLabelColor ?? null);
|
|
415
447
|
}
|
|
416
|
-
/**
|
|
417
|
-
* Tick labels for a `Decimal` axis: SI-prefixed (`1.5k`), with the precision taken from the
|
|
418
|
-
* scale's own tick step rather than fixed. A fixed two significant digits (`.2s`) is fine for a
|
|
419
|
-
* whole domain but breaks down once zoom narrows it to a few units - ticks at 10.5, 11, 11.5, 12
|
|
420
|
-
* all rounded to `10 11 12 12`. `~` trims trailing zeros so a coarse step still reads `5 10 15`,
|
|
421
|
-
* not `5.0 10.0 15.0`. Falls back to the fixed format for a scale without `tickFormat` (a
|
|
422
|
-
* `Decimal` axis is always linear in practice, but the config type only promises `AxisScale`).
|
|
423
|
-
*/
|
|
424
|
-
function decimalTickFormat(scale, ticks) {
|
|
425
|
-
const withTicks = scale;
|
|
426
|
-
const formatTick = withTicks.tickFormat ? withTicks.tickFormat(ticks, '~s') : (d) => format('.2s')(d);
|
|
427
|
-
// The prefix comes from the domain's largest value, which would label the origin `0k`.
|
|
428
|
-
return (d) => (Number(d) === 0 ? '0' : formatTick(d));
|
|
429
|
-
}
|
|
430
448
|
|
|
431
449
|
/** Where along its axis each grid line sits, in pixels. */
|
|
432
450
|
function gridLines(scale, numberOfTicks) {
|
|
@@ -493,15 +511,24 @@ class PcacColorService {
|
|
|
493
511
|
warning = '#fec163';
|
|
494
512
|
orange = '#ffa177';
|
|
495
513
|
blue = '#55d8fe';
|
|
496
|
-
scale = [
|
|
497
|
-
this.primaryLight,
|
|
498
|
-
this.success,
|
|
499
|
-
this.danger,
|
|
500
|
-
this.warning,
|
|
501
|
-
this.orange,
|
|
502
|
-
this.blue
|
|
503
|
-
];
|
|
504
514
|
alert = 'red';
|
|
515
|
+
/** A consumer's own scale from `setScale()`; replaces the default palette while set. */
|
|
516
|
+
customScale = null;
|
|
517
|
+
/**
|
|
518
|
+
* The palette charts cycle through: the `setScale()` one when given, else the default colors
|
|
519
|
+
* built from the current fields - so a setter like `setSuccess()` changes the charts drawn after
|
|
520
|
+
* it, rather than a copy of the colors taken when the service was created.
|
|
521
|
+
*/
|
|
522
|
+
get scale() {
|
|
523
|
+
return this.customScale ?? [
|
|
524
|
+
this.primaryLight,
|
|
525
|
+
this.success,
|
|
526
|
+
this.danger,
|
|
527
|
+
this.warning,
|
|
528
|
+
this.orange,
|
|
529
|
+
this.blue,
|
|
530
|
+
];
|
|
531
|
+
}
|
|
505
532
|
/**
|
|
506
533
|
* Get a scale of colors dynamically
|
|
507
534
|
* @param numberOfNodes Number of items in dynamically build collection of colors
|
|
@@ -518,12 +545,15 @@ class PcacColorService {
|
|
|
518
545
|
return this.alert;
|
|
519
546
|
}
|
|
520
547
|
/**
|
|
521
|
-
* Set the scale that charts use to set colors of data[x] on the UI
|
|
548
|
+
* Set the scale that charts use to set colors of data[x] on the UI, in place of the default
|
|
549
|
+
* palette (and of the individual color setters below, which only shape the default). An empty
|
|
550
|
+
* array goes back to the default palette rather than leaving charts with no colors.
|
|
522
551
|
* @param colors A collection of hex values
|
|
523
552
|
*/
|
|
524
553
|
setScale(colors) {
|
|
525
|
-
this.
|
|
554
|
+
this.customScale = colors.length ? [...colors] : null;
|
|
526
555
|
}
|
|
556
|
+
/** @deprecated No chart reads the primary color; it isn't part of the palette. */
|
|
527
557
|
setPrimary(color) {
|
|
528
558
|
this.primary = color;
|
|
529
559
|
}
|
|
@@ -536,6 +566,9 @@ class PcacColorService {
|
|
|
536
566
|
setDanger(color) {
|
|
537
567
|
this.danger = color;
|
|
538
568
|
}
|
|
569
|
+
setWarning(color) {
|
|
570
|
+
this.warning = color;
|
|
571
|
+
}
|
|
539
572
|
setOrange(color) {
|
|
540
573
|
this.orange = color;
|
|
541
574
|
}
|
|
@@ -572,15 +605,26 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.7", ngImpor
|
|
|
572
605
|
|
|
573
606
|
class PcacTooltipBuilder {
|
|
574
607
|
appRef = inject(ApplicationRef);
|
|
608
|
+
document = inject(DOCUMENT);
|
|
609
|
+
shell = null;
|
|
575
610
|
/**
|
|
576
611
|
* One tooltip element shared by every chart on the page, appended to `<body>` so it can float
|
|
577
612
|
* over anything. It's only ever a positioning shell: the default look (`.pcac-d3-tooltip-default`)
|
|
578
613
|
* is toggled on when the library renders its own key/value content and off when a consumer's
|
|
579
614
|
* `<ng-template pcacTooltip>` is rendered into it instead, so a custom template owns the whole box.
|
|
615
|
+
*
|
|
616
|
+
* Created on first use rather than with the service, which every chart builder injects: that
|
|
617
|
+
* way nothing touches the DOM until a chart actually shows a tooltip, so building a chart
|
|
618
|
+
* where there's no browser `document` (server-side rendering) doesn't throw. Removed again in
|
|
619
|
+
* `ngOnDestroy()`, when the app that owns this root service is destroyed.
|
|
580
620
|
*/
|
|
581
|
-
tooltip
|
|
582
|
-
.
|
|
583
|
-
|
|
621
|
+
get tooltip() {
|
|
622
|
+
this.shell ??= select(this.document.body)
|
|
623
|
+
.append('div')
|
|
624
|
+
.attr('class', 'pcac-d3-tooltip')
|
|
625
|
+
.attr('role', 'tooltip');
|
|
626
|
+
return this.shell;
|
|
627
|
+
}
|
|
584
628
|
/**
|
|
585
629
|
* The consumer template currently rendered into the shell, if any. Created on the first show
|
|
586
630
|
* of a hover and torn down in `hideTooltip()` - not kept around between hovers - so a view is
|
|
@@ -589,14 +633,22 @@ class PcacTooltipBuilder {
|
|
|
589
633
|
*/
|
|
590
634
|
view = null;
|
|
591
635
|
viewTemplate = null;
|
|
636
|
+
/**
|
|
637
|
+
* Whatever showed the tooltip last (a chart passes itself), so `hideTooltip(owner)` from any
|
|
638
|
+
* other chart leaves it alone. The element is shared by every chart on the page: without this, a
|
|
639
|
+
* chart rebuilding or being destroyed would close the tooltip another chart is showing.
|
|
640
|
+
*/
|
|
641
|
+
owner = null;
|
|
592
642
|
/** Space left between the tooltip and whatever it's placed next to (the cursor or an anchor). */
|
|
593
643
|
static GAP = 8;
|
|
594
644
|
/**
|
|
595
645
|
* Shows the tooltip for a hovered datum. Renders `template` when the chart has one projected;
|
|
596
646
|
* otherwise falls back to the default key/value content, formatted per `valueFormat`/`keyFormat`.
|
|
597
647
|
* Placed beside `anchor` when one is given (see `positionBeside`), else above the event's position.
|
|
648
|
+
* `owner` is what's showing it (see `hideTooltip`).
|
|
598
649
|
*/
|
|
599
|
-
showTooltip(event, template, context, valueFormat, keyFormat, anchor) {
|
|
650
|
+
showTooltip(event, template, context, valueFormat, keyFormat, anchor, owner) {
|
|
651
|
+
this.owner = owner ?? null;
|
|
600
652
|
if (template) {
|
|
601
653
|
this.renderTemplate(template, context);
|
|
602
654
|
}
|
|
@@ -618,15 +670,43 @@ class PcacTooltipBuilder {
|
|
|
618
670
|
showBarTooltip(event, data, valueFormat, keyFormat) {
|
|
619
671
|
this.showTooltip(event, undefined, { $implicit: data, parent: null, isThreshold: false, index: 0, parentIndex: null, coincident: [] }, valueFormat, keyFormat);
|
|
620
672
|
}
|
|
621
|
-
|
|
622
|
-
|
|
673
|
+
/**
|
|
674
|
+
* Hides the tooltip. Given an `owner`, only if that owner is the one showing it - so a chart can
|
|
675
|
+
* release the tooltip on rebuild or destroy without closing one another chart has open. Without
|
|
676
|
+
* an owner it always hides.
|
|
677
|
+
*/
|
|
678
|
+
hideTooltip(owner) {
|
|
679
|
+
if (owner !== undefined && owner !== this.owner) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
this.owner = null;
|
|
683
|
+
if (!this.shell) {
|
|
684
|
+
// Never shown, so there's nothing to hide - and no reason to create the element just to.
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
this.shell.style('display', 'none');
|
|
688
|
+
this.destroyView();
|
|
689
|
+
}
|
|
690
|
+
ngOnDestroy() {
|
|
623
691
|
this.destroyView();
|
|
692
|
+
this.shell?.remove();
|
|
693
|
+
this.shell = null;
|
|
624
694
|
}
|
|
695
|
+
/**
|
|
696
|
+
* The library's own key/value content. Built from text nodes, never parsed as HTML: `key` and
|
|
697
|
+
* `value` are consumer data, and markup in them must show as text rather than run.
|
|
698
|
+
*/
|
|
625
699
|
renderDefault(data, valueFormat, keyFormat) {
|
|
626
700
|
this.destroyView();
|
|
627
|
-
this.
|
|
701
|
+
const { key, value } = this.getBarTipData(data, valueFormat, keyFormat);
|
|
702
|
+
const shell = this.tooltip
|
|
628
703
|
.classed('pcac-d3-tooltip-default', true)
|
|
629
|
-
.html(
|
|
704
|
+
.html(null)
|
|
705
|
+
.node();
|
|
706
|
+
if (key !== null) {
|
|
707
|
+
shell.append(key, this.document.createElement('br'));
|
|
708
|
+
}
|
|
709
|
+
shell.append(value);
|
|
630
710
|
}
|
|
631
711
|
renderTemplate(template, context) {
|
|
632
712
|
if (this.view && this.viewTemplate === template) {
|
|
@@ -663,7 +743,7 @@ class PcacTooltipBuilder {
|
|
|
663
743
|
this.view.destroy();
|
|
664
744
|
this.view = null;
|
|
665
745
|
this.viewTemplate = null;
|
|
666
|
-
this.
|
|
746
|
+
this.shell?.html(null);
|
|
667
747
|
}
|
|
668
748
|
/**
|
|
669
749
|
* Shows the (already rendered) shell at the page's top-left so it can be measured. Left where the
|
|
@@ -699,7 +779,8 @@ class PcacTooltipBuilder {
|
|
|
699
779
|
const width = shell.offsetWidth;
|
|
700
780
|
const height = shell.offsetHeight;
|
|
701
781
|
// `clientWidth`/`clientHeight` of the root, unlike `innerWidth`/`innerHeight`, exclude scrollbars.
|
|
702
|
-
const viewport = document.documentElement;
|
|
782
|
+
const viewport = this.document.documentElement;
|
|
783
|
+
const view = this.document.defaultView;
|
|
703
784
|
const right = viewport.clientWidth - box.right - gap;
|
|
704
785
|
const left = box.left - gap;
|
|
705
786
|
const above = box.top - gap;
|
|
@@ -710,16 +791,19 @@ class PcacTooltipBuilder {
|
|
|
710
791
|
const y = toTop ? box.top - gap - height : box.bottom + gap;
|
|
711
792
|
// The rect is in viewport coordinates; the shell is absolutely positioned against the page.
|
|
712
793
|
this.tooltip
|
|
713
|
-
.style('left', x +
|
|
714
|
-
.style('top', y +
|
|
794
|
+
.style('left', x + (view?.scrollX ?? 0) + 'px')
|
|
795
|
+
.style('top', y + (view?.scrollY ?? 0) + 'px');
|
|
715
796
|
}
|
|
797
|
+
/** The default tooltip's two lines as plain text; `key` is null when the datum has none. */
|
|
716
798
|
getBarTipData(data, valueFormat, keyFormat) {
|
|
717
799
|
let value = data.value;
|
|
718
800
|
let key = data.key;
|
|
719
|
-
|
|
801
|
+
// A missing value stays blank rather than being formatted as `0%` / `null F`.
|
|
802
|
+
if (valueFormat && value !== null && value !== undefined) {
|
|
720
803
|
switch (valueFormat) {
|
|
721
804
|
case PcacFormatEnum.Percentage:
|
|
722
|
-
|
|
805
|
+
// Same fraction rule, and the same formatting, as a Percentage axis's ticks.
|
|
806
|
+
value = formatPercent(Number(value));
|
|
723
807
|
break;
|
|
724
808
|
case PcacFormatEnum.Fahrenheit:
|
|
725
809
|
value = `${value} F`;
|
|
@@ -740,8 +824,11 @@ class PcacTooltipBuilder {
|
|
|
740
824
|
break;
|
|
741
825
|
}
|
|
742
826
|
}
|
|
743
|
-
|
|
744
|
-
return
|
|
827
|
+
// A key of 0 (e.g. hour 0) is a real key; only a missing or empty one leaves the line out.
|
|
828
|
+
return {
|
|
829
|
+
key: key === null || key === undefined || key === '' ? null : String(key),
|
|
830
|
+
value: value === null || value === undefined ? '' : String(value),
|
|
831
|
+
};
|
|
745
832
|
}
|
|
746
833
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.7", ngImport: i0, type: PcacTooltipBuilder, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
747
834
|
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.1.7", ngImport: i0, type: PcacTooltipBuilder, providedIn: 'root' });
|
|
@@ -753,6 +840,8 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.7", ngImpor
|
|
|
753
840
|
}]
|
|
754
841
|
}] });
|
|
755
842
|
|
|
843
|
+
// Decorated only so Angular accepts its `ngOnDestroy` hook, which the builders extending it inherit;
|
|
844
|
+
// never provided itself - each builder is provided by its own chart component.
|
|
756
845
|
class PcacChart {
|
|
757
846
|
axisBuilder = inject(PcacAxisBuilder);
|
|
758
847
|
gridBuilder = inject(PcacGridBuilder);
|
|
@@ -778,6 +867,22 @@ class PcacChart {
|
|
|
778
867
|
height = 400;
|
|
779
868
|
colors = [];
|
|
780
869
|
startData = [];
|
|
870
|
+
/** The chart's accessible name when its config gives no `ariaLabel`; each builder names its type. */
|
|
871
|
+
chartTypeLabel = 'Chart';
|
|
872
|
+
/**
|
|
873
|
+
* Swaps a consumer's `colorOverride` in for the theme palette `initializeChartState` set, in
|
|
874
|
+
* order. Checks `length` rather than truthiness: every config class defaults its override to
|
|
875
|
+
* `[]`, and an empty array is truthy - taken as an override, it would leave every mark with no
|
|
876
|
+
* color. Cycled out to at least as many entries as the theme palette, the same way that palette
|
|
877
|
+
* cycles its own colors, so an override shorter than the data repeats rather than leaving the
|
|
878
|
+
* marks past its end with an `undefined` color. Call after `initializeChartState()`.
|
|
879
|
+
*/
|
|
880
|
+
applyColorOverride(override) {
|
|
881
|
+
if (!override?.length) {
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
this.colors = Array.from({ length: Math.max(this.colors.length, override.length) }, (_, i) => override[i % override.length]);
|
|
885
|
+
}
|
|
781
886
|
/**
|
|
782
887
|
* Raw, unadjusted container width measured by the last successful `initializeChartState()`
|
|
783
888
|
* call, or `null` if none has succeeded yet. Deliberately tracked separately from `width`:
|
|
@@ -972,10 +1077,19 @@ class PcacChart {
|
|
|
972
1077
|
index: options.index,
|
|
973
1078
|
parentIndex: options.parentIndex ?? null,
|
|
974
1079
|
coincident: options.coincident ?? [],
|
|
975
|
-
}, options.valueFormat, options.keyFormat, options.anchor);
|
|
1080
|
+
}, options.valueFormat, options.keyFormat, options.anchor, this);
|
|
976
1081
|
}
|
|
1082
|
+
/** Hides the tooltip if this chart is the one showing it; another chart's is left alone. */
|
|
977
1083
|
hideTooltip() {
|
|
978
|
-
this.tooltipBuilder.hideTooltip();
|
|
1084
|
+
this.tooltipBuilder.hideTooltip(this);
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Builders are provided per chart component, so Angular calls this when the component is
|
|
1088
|
+
* destroyed. A chart removed while hovered (an `@if` flip, a route change) gets no `mouseout`,
|
|
1089
|
+
* so without this its tooltip - and any consumer template view in it - would stay on screen.
|
|
1090
|
+
*/
|
|
1091
|
+
ngOnDestroy() {
|
|
1092
|
+
this.hideTooltip();
|
|
979
1093
|
}
|
|
980
1094
|
/**
|
|
981
1095
|
* Prior to building a chart, we need to initialize the state of the chart.
|
|
@@ -989,17 +1103,38 @@ class PcacChart {
|
|
|
989
1103
|
* @param chartElm Reference to SVG on dom
|
|
990
1104
|
* @param config Chart specific configuration
|
|
991
1105
|
*/
|
|
1106
|
+
/**
|
|
1107
|
+
* Removes whatever the chart last drew, for a build with no data to draw. Builders call it
|
|
1108
|
+
* rather than just returning, which left the previous data on screen when a consumer emptied
|
|
1109
|
+
* the chart (e.g. a filter that now matches nothing).
|
|
1110
|
+
*/
|
|
1111
|
+
clearChart(chartElm) {
|
|
1112
|
+
this.hideTooltip();
|
|
1113
|
+
select(chartElm.nativeElement).select('g').remove();
|
|
1114
|
+
}
|
|
992
1115
|
initializeChartState(chartElm, config) {
|
|
993
|
-
|
|
1116
|
+
// The rebuild removes the hovered element, which gets no mouseout to close its tooltip.
|
|
1117
|
+
this.hideTooltip();
|
|
1118
|
+
select(chartElm.nativeElement).select('g').remove();
|
|
994
1119
|
const container = chartElm.nativeElement.parentNode;
|
|
995
1120
|
const containerWidth = container.clientWidth;
|
|
996
1121
|
const measuredWidth = containerWidth - this.margin.left - this.margin.right;
|
|
997
1122
|
if (measuredWidth <= 0) {
|
|
1123
|
+
// The previous drawing was just removed above, so nothing is on screen any more. Forget the
|
|
1124
|
+
// size it was drawn at, or a container that comes back at that same width (a hidden tab
|
|
1125
|
+
// shown again) would read as unchanged to `containerSizeChanged()` and never be redrawn.
|
|
1126
|
+
this.lastContainerWidth = null;
|
|
998
1127
|
return false;
|
|
999
1128
|
}
|
|
1000
1129
|
this.width = measuredWidth;
|
|
1130
|
+
// Announced as a single image with a name, rather than as a pile of unlabelled shapes.
|
|
1131
|
+
select(chartElm.nativeElement)
|
|
1132
|
+
.attr('role', 'img')
|
|
1133
|
+
.attr('aria-label', config.ariaLabel || this.chartTypeLabel);
|
|
1001
1134
|
this.height = this.resolveHeight(container, config);
|
|
1002
|
-
|
|
1135
|
+
// One color per group or per series within a group, whichever needs more - sized by the
|
|
1136
|
+
// largest group, since bars index colors by their position within their own group.
|
|
1137
|
+
this.colors = this.colorService.getColorScale(Math.max(config.data.length, ...config.data.map((d) => d.data?.length ?? 0)));
|
|
1003
1138
|
this.lastContainerWidth = containerWidth;
|
|
1004
1139
|
this.lastContainerHeight = container.clientHeight;
|
|
1005
1140
|
this.lastHeightFull = config.heightFull === true;
|
|
@@ -1069,7 +1204,7 @@ class PcacChart {
|
|
|
1069
1204
|
buildContainer(chartElm, center = false) {
|
|
1070
1205
|
const combinedHeight = this.height + this.margin.top + this.margin.bottom;
|
|
1071
1206
|
const combinedWidth = this.width + this.margin.left + this.margin.right;
|
|
1072
|
-
this.svg = select
|
|
1207
|
+
this.svg = select(chartElm.nativeElement)
|
|
1073
1208
|
.attr('width', combinedWidth)
|
|
1074
1209
|
.attr('height', combinedHeight);
|
|
1075
1210
|
if (center) {
|
|
@@ -1101,7 +1236,7 @@ class PcacChart {
|
|
|
1101
1236
|
axisY.tickSizeInner(this.yAxis.tickSize);
|
|
1102
1237
|
}
|
|
1103
1238
|
let max = 0;
|
|
1104
|
-
select
|
|
1239
|
+
select(chartElm.nativeElement).append('g')
|
|
1105
1240
|
.call(axisY)
|
|
1106
1241
|
.each((d, i, n) => {
|
|
1107
1242
|
const width = n[i].getBBox().width;
|
|
@@ -1119,28 +1254,112 @@ class PcacChart {
|
|
|
1119
1254
|
this.width = this.width + this.margin.left - left;
|
|
1120
1255
|
this.margin.left = left;
|
|
1121
1256
|
}
|
|
1257
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.7", ngImport: i0, type: PcacChart, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
1258
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.1.7", ngImport: i0, type: PcacChart });
|
|
1122
1259
|
}
|
|
1260
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.7", ngImport: i0, type: PcacChart, decorators: [{
|
|
1261
|
+
type: Injectable
|
|
1262
|
+
}] });
|
|
1123
1263
|
|
|
1124
1264
|
/**
|
|
1125
|
-
*
|
|
1126
|
-
*
|
|
1127
|
-
*
|
|
1265
|
+
* How long each bar is drawn: its value, or 0 when it can't be drawn - a null or non-numeric
|
|
1266
|
+
* value, or `hide` set on the bar or on its whole group. A hidden bar keeps its slot (and so its
|
|
1267
|
+
* color) and just draws nothing, the same way a hidden line/area/plot series keeps its place.
|
|
1268
|
+
* Negative values are kept as they are; the bar charts only draw upward from 0.
|
|
1128
1269
|
*
|
|
1129
1270
|
* Keyed by the bar's own `PcacData` object so D3 callbacks (which only get the datum) can look
|
|
1130
|
-
* their
|
|
1271
|
+
* their size up directly.
|
|
1272
|
+
*/
|
|
1273
|
+
function barSizes(groups) {
|
|
1274
|
+
const sizes = new Map();
|
|
1275
|
+
for (const group of groups) {
|
|
1276
|
+
for (const bar of group.data ?? []) {
|
|
1277
|
+
const value = Number(bar.value ?? 0);
|
|
1278
|
+
sizes.set(bar, group.hide || bar.hide || !Number.isFinite(value) ? 0 : value);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
return sizes;
|
|
1282
|
+
}
|
|
1283
|
+
/**
|
|
1284
|
+
* Where each bar of a stacked group starts: the running total of the bars before it, in data
|
|
1285
|
+
* order, so the first bar sits on the baseline and each later one on top of the previous. Sizes
|
|
1286
|
+
* are the bar's *own* (segment), not a pre-accumulated total, taken from `barSizes` - so a null,
|
|
1287
|
+
* non-numeric or hidden bar adds nothing to the stack.
|
|
1131
1288
|
*/
|
|
1132
|
-
function stackStarts(groups) {
|
|
1289
|
+
function stackStarts(groups, sizes = barSizes(groups)) {
|
|
1133
1290
|
const starts = new Map();
|
|
1134
1291
|
for (const group of groups) {
|
|
1135
1292
|
let total = 0;
|
|
1136
|
-
for (const bar of group.data) {
|
|
1293
|
+
for (const bar of group.data ?? []) {
|
|
1137
1294
|
starts.set(bar, total);
|
|
1138
|
-
total +=
|
|
1295
|
+
total += sizes.get(bar) ?? 0;
|
|
1139
1296
|
}
|
|
1140
1297
|
}
|
|
1141
1298
|
return starts;
|
|
1142
1299
|
}
|
|
1143
1300
|
|
|
1301
|
+
/** Whether a threshold entry carries per-bar values in its `data`. */
|
|
1302
|
+
function hasBars(entry) {
|
|
1303
|
+
return (entry.data?.length ?? 0) > 0;
|
|
1304
|
+
}
|
|
1305
|
+
/** A threshold is only drawn when its value is a finite number. */
|
|
1306
|
+
function drawable(threshold) {
|
|
1307
|
+
if (!threshold || threshold.value === null || threshold.value === undefined) {
|
|
1308
|
+
return null;
|
|
1309
|
+
}
|
|
1310
|
+
return Number.isFinite(Number(threshold.value)) ? threshold : null;
|
|
1311
|
+
}
|
|
1312
|
+
/**
|
|
1313
|
+
* Reads the shape of `thresholds` the way the docs describe it:
|
|
1314
|
+
* - one entry with an empty (or no) `data` - a single threshold across the whole chart;
|
|
1315
|
+
* - one entry per group, each with an empty `data` - a threshold per group;
|
|
1316
|
+
* - one entry per group, each with an entry per bar in its `data` - a threshold per bar.
|
|
1317
|
+
*
|
|
1318
|
+
* A stacked group is drawn as one bar, so on a stacked chart per-bar entries give per-group
|
|
1319
|
+
* thresholds, each group's taken from its entry's first `data` item. Checks `data.length` rather
|
|
1320
|
+
* than `data`'s truthiness: `PcacData.data` defaults to `[]`, which is truthy.
|
|
1321
|
+
*/
|
|
1322
|
+
function barThresholdLayout(thresholds, isStacked) {
|
|
1323
|
+
if (!thresholds?.length) {
|
|
1324
|
+
return 'none';
|
|
1325
|
+
}
|
|
1326
|
+
if (!thresholds.some(hasBars)) {
|
|
1327
|
+
return thresholds.length === 1 ? 'chart' : 'group';
|
|
1328
|
+
}
|
|
1329
|
+
return isStacked ? 'group' : 'bar';
|
|
1330
|
+
}
|
|
1331
|
+
/**
|
|
1332
|
+
* The threshold drawn across the group at `groupIndex` (or across the chart, for index 0 of a
|
|
1333
|
+
* `'chart'` layout): the entry itself, or its first `data` item when it has per-bar values. Null
|
|
1334
|
+
* when there's no entry for that group or no numeric value to draw.
|
|
1335
|
+
*/
|
|
1336
|
+
function groupThreshold(thresholds, groupIndex) {
|
|
1337
|
+
const entry = thresholds[groupIndex];
|
|
1338
|
+
if (!entry) {
|
|
1339
|
+
return null;
|
|
1340
|
+
}
|
|
1341
|
+
return drawable(hasBars(entry) ? entry.data[0] : entry);
|
|
1342
|
+
}
|
|
1343
|
+
/** The threshold drawn across one bar, or null when that bar has none (or no numeric value). */
|
|
1344
|
+
function barThreshold(thresholds, groupIndex, barIndex) {
|
|
1345
|
+
return drawable(thresholds[groupIndex]?.data?.[barIndex]);
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
/**
|
|
1349
|
+
* Every series key across all groups, in first-seen order: the domain of a grouped bar chart's
|
|
1350
|
+
* inner band, so each series has its own slot in every group. Taken from the first group alone,
|
|
1351
|
+
* a series that only later groups have fell outside the band and was drawn over the first slot.
|
|
1352
|
+
*/
|
|
1353
|
+
function seriesKeys(groups) {
|
|
1354
|
+
const keys = new Set();
|
|
1355
|
+
for (const group of groups) {
|
|
1356
|
+
for (const bar of group.data ?? []) {
|
|
1357
|
+
keys.add(bar.key);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
return [...keys];
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1144
1363
|
/**
|
|
1145
1364
|
* Provided per-component (see PcacBarHorizontalChartComponent's `providers`), not root-scoped:
|
|
1146
1365
|
* this builder extends PcacChart, which holds mutable per-chart-instance state (margin, width,
|
|
@@ -1154,8 +1373,10 @@ class BarHorizontalChartBuilder extends PcacChart {
|
|
|
1154
1373
|
barClickedSource = new Subject();
|
|
1155
1374
|
config;
|
|
1156
1375
|
barClicked$ = this.barClickedSource.asObservable();
|
|
1376
|
+
chartTypeLabel = 'Bar chart';
|
|
1157
1377
|
buildChart(chartElm, config) {
|
|
1158
1378
|
if (!config?.data?.length) {
|
|
1379
|
+
this.clearChart(chartElm);
|
|
1159
1380
|
return;
|
|
1160
1381
|
}
|
|
1161
1382
|
this.config = JSON.parse(JSON.stringify(config));
|
|
@@ -1163,18 +1384,11 @@ class BarHorizontalChartBuilder extends PcacChart {
|
|
|
1163
1384
|
if (!this.initializeChartState(chartElm, this.config)) {
|
|
1164
1385
|
return;
|
|
1165
1386
|
}
|
|
1166
|
-
|
|
1167
|
-
// reversing it directly flipped their palette on every rebuild (i.e. every resize).
|
|
1168
|
-
if (config.colorOverride && config.colorOverride.colors) {
|
|
1169
|
-
this.colors = [...config.colorOverride.colors].reverse();
|
|
1170
|
-
}
|
|
1387
|
+
this.applyColorOverride(config.colorOverride?.colors);
|
|
1171
1388
|
this.buildScales(chartElm, this.config);
|
|
1172
1389
|
this.drawChart(chartElm, this.config);
|
|
1173
1390
|
}
|
|
1174
1391
|
buildScales(chartElm, config) {
|
|
1175
|
-
config.data[0].data.map((d) => {
|
|
1176
|
-
return d.value;
|
|
1177
|
-
});
|
|
1178
1392
|
// Bars grow from 0, so only `domainMax` is read; `xAxis.domainMin` is ignored.
|
|
1179
1393
|
this.xScale = scaleLinear()
|
|
1180
1394
|
.domain([0, this.xAxis.domainMax]);
|
|
@@ -1185,7 +1399,7 @@ class BarHorizontalChartBuilder extends PcacChart {
|
|
|
1185
1399
|
this.yScaleGrouped = scaleBand()
|
|
1186
1400
|
.padding(0.05)
|
|
1187
1401
|
.rangeRound([0, this.yScaleStacked.bandwidth()])
|
|
1188
|
-
.domain(config.data
|
|
1402
|
+
.domain(seriesKeys(config.data));
|
|
1189
1403
|
// The left margin is sized to the y axis's labels - unless there is no y axis to size it to
|
|
1190
1404
|
if (!this.yAxis.hide) {
|
|
1191
1405
|
this.setHorizontalMarginsBasedOnContent(chartElm, this.yScaleStacked);
|
|
@@ -1212,33 +1426,31 @@ class BarHorizontalChartBuilder extends PcacChart {
|
|
|
1212
1426
|
.attr('transform', (d) => 'translate(0,' + this.yScaleStacked(d.key) + ')');
|
|
1213
1427
|
const group = groupsContainer.selectAll('rect')
|
|
1214
1428
|
.data((d) => {
|
|
1215
|
-
return d.data;
|
|
1429
|
+
return d.data ?? [];
|
|
1216
1430
|
});
|
|
1217
1431
|
this.drawBarsPerGroup(group, config);
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
this.drawThresholdsPerGroup(group, config);
|
|
1229
|
-
}
|
|
1230
|
-
// Draw threshold across each bar in group
|
|
1231
|
-
if (config.thresholds.length > 1 && config.thresholds[0].data && !config.isStacked) {
|
|
1232
|
-
this.drawThresholdsPerBarInGroup(group, config);
|
|
1432
|
+
switch (barThresholdLayout(config.thresholds, config.isStacked)) {
|
|
1433
|
+
case 'chart':
|
|
1434
|
+
this.drawThresholdAcrossChart(config);
|
|
1435
|
+
break;
|
|
1436
|
+
case 'group':
|
|
1437
|
+
this.drawThresholdsPerGroup(config);
|
|
1438
|
+
break;
|
|
1439
|
+
case 'bar':
|
|
1440
|
+
this.drawThresholdsPerBarInGroup(group, config);
|
|
1441
|
+
break;
|
|
1233
1442
|
}
|
|
1234
1443
|
}
|
|
1235
1444
|
drawBarsPerGroup(groups, config) {
|
|
1236
1445
|
const self = this;
|
|
1237
1446
|
// Stacked bars start where the previous bar in their group ends; every other layout draws
|
|
1238
1447
|
// from the baseline (start 0).
|
|
1239
|
-
|
|
1448
|
+
// Sizes honor `hide` (and treat a null or non-numeric value as 0), so a hidden bar keeps its
|
|
1449
|
+
// slot and color but draws nothing - see `barSizes`.
|
|
1450
|
+
const sizes = barSizes(config.data);
|
|
1451
|
+
const starts = config.isStacked ? stackStarts(config.data, sizes) : new Map();
|
|
1240
1452
|
const startOf = (d) => starts.get(d) ?? 0;
|
|
1241
|
-
const endOf = (d) => startOf(d) +
|
|
1453
|
+
const endOf = (d) => startOf(d) + (sizes.get(d) ?? 0);
|
|
1242
1454
|
groups.enter().append('rect')
|
|
1243
1455
|
.attr('class', 'pcac-bar')
|
|
1244
1456
|
.attr('x', (d) => this.xScale(startOf(d)))
|
|
@@ -1259,7 +1471,7 @@ class BarHorizontalChartBuilder extends PcacChart {
|
|
|
1259
1471
|
})
|
|
1260
1472
|
.attr('width', 0)
|
|
1261
1473
|
.on('mouseover', function () {
|
|
1262
|
-
select
|
|
1474
|
+
select(this)
|
|
1263
1475
|
.transition()
|
|
1264
1476
|
.duration(self.transitionService.getTransitionDuration() / 7.5)
|
|
1265
1477
|
.style('fill', () => {
|
|
@@ -1282,7 +1494,7 @@ class BarHorizontalChartBuilder extends PcacChart {
|
|
|
1282
1494
|
})
|
|
1283
1495
|
.on('mouseout', function () {
|
|
1284
1496
|
self.hideTooltip();
|
|
1285
|
-
select
|
|
1497
|
+
select(this)
|
|
1286
1498
|
.transition()
|
|
1287
1499
|
.duration(self.transitionService.getTransitionDuration() / 5)
|
|
1288
1500
|
.style('fill', () => {
|
|
@@ -1304,53 +1516,63 @@ class BarHorizontalChartBuilder extends PcacChart {
|
|
|
1304
1516
|
});
|
|
1305
1517
|
}
|
|
1306
1518
|
drawThresholdAcrossChart(config) {
|
|
1519
|
+
const threshold = groupThreshold(config.thresholds, 0);
|
|
1520
|
+
if (!threshold) {
|
|
1521
|
+
return;
|
|
1522
|
+
}
|
|
1307
1523
|
this.applyPreTransitionThresholdStyles(this.svg.select('.pcac-bars').append('rect'))
|
|
1308
1524
|
.attr('height', this.height)
|
|
1309
|
-
.attr('data-group-threshold-id',
|
|
1310
|
-
return i;
|
|
1311
|
-
})
|
|
1525
|
+
.attr('data-group-threshold-id', 0)
|
|
1312
1526
|
.on('mousemove', (event) => {
|
|
1313
|
-
this.showTooltip(event,
|
|
1527
|
+
this.showTooltip(event, threshold, { index: 0, isThreshold: true, valueFormat: this.xAxis.format });
|
|
1314
1528
|
})
|
|
1315
1529
|
.transition()
|
|
1316
1530
|
.duration(this.transitionService.getTransitionDuration())
|
|
1317
|
-
.attr('x', (
|
|
1318
|
-
return this.xScale(config.thresholds[i].value);
|
|
1319
|
-
});
|
|
1531
|
+
.attr('x', this.xScale(Number(threshold.value)));
|
|
1320
1532
|
}
|
|
1321
|
-
|
|
1533
|
+
/** One threshold across each group that has one; groups without an entry are skipped. */
|
|
1534
|
+
drawThresholdsPerGroup(config) {
|
|
1322
1535
|
const self = this;
|
|
1323
|
-
|
|
1536
|
+
const groupIndexOf = (rect) => Number(rect.parentElement.dataset['groupId']);
|
|
1537
|
+
const groups = this.svg.selectAll('.pcac-bar-group')
|
|
1538
|
+
.filter((_, i) => groupThreshold(config.thresholds, i) !== null);
|
|
1539
|
+
this.applyPreTransitionThresholdStyles(groups.append('rect'))
|
|
1324
1540
|
.attr('height', this.yScaleStacked.bandwidth())
|
|
1325
|
-
.attr('data-group-threshold-id', (
|
|
1326
|
-
return
|
|
1541
|
+
.attr('data-group-threshold-id', function () {
|
|
1542
|
+
return groupIndexOf(this);
|
|
1327
1543
|
})
|
|
1328
1544
|
.on('mousemove', function (event) {
|
|
1329
|
-
const index =
|
|
1330
|
-
const threshold = config.thresholds[index];
|
|
1545
|
+
const index = groupIndexOf(this);
|
|
1331
1546
|
// A threshold's own PcacData is typically just a value, so its parent is the *data* group
|
|
1332
1547
|
// it's drawn against (which has the key) rather than the threshold entry it came from.
|
|
1333
|
-
self.showTooltip(event, config.
|
|
1548
|
+
self.showTooltip(event, groupThreshold(config.thresholds, index), { index, parent: config.data[index], parentIndex: index, isThreshold: true, valueFormat: self.xAxis.format });
|
|
1334
1549
|
})
|
|
1335
1550
|
.transition()
|
|
1336
1551
|
.duration(this.transitionService.getTransitionDuration())
|
|
1337
|
-
.attr('x', (_, i) => {
|
|
1338
|
-
return this.xScale(config.
|
|
1552
|
+
.attr('x', (_, i, nodes) => {
|
|
1553
|
+
return this.xScale(Number(groupThreshold(config.thresholds, groupIndexOf(nodes[i]))?.value));
|
|
1339
1554
|
});
|
|
1340
1555
|
}
|
|
1556
|
+
/** One threshold across each bar that has one; bars without an entry are skipped. */
|
|
1341
1557
|
drawThresholdsPerBarInGroup(group, config) {
|
|
1342
1558
|
const self = this;
|
|
1343
|
-
|
|
1559
|
+
const thresholdOf = (rect) => barThreshold(config.thresholds, Number(rect.parentElement.dataset['groupId']), Number(rect.getAttribute('data-group-threshold-id')));
|
|
1560
|
+
const rects = this.applyPreTransitionThresholdStyles(group.enter().append('rect'))
|
|
1344
1561
|
.attr('data-group-threshold-id', (_, i) => {
|
|
1345
1562
|
return i;
|
|
1346
1563
|
})
|
|
1347
|
-
.attr('height', this.yScaleGrouped.bandwidth())
|
|
1564
|
+
.attr('height', this.yScaleGrouped.bandwidth());
|
|
1565
|
+
rects.filter(function () {
|
|
1566
|
+
return thresholdOf(this) === null;
|
|
1567
|
+
}).remove();
|
|
1568
|
+
rects.filter(function () {
|
|
1569
|
+
return thresholdOf(this) !== null;
|
|
1570
|
+
})
|
|
1348
1571
|
.on('mousemove', function (event) {
|
|
1349
|
-
const
|
|
1350
|
-
const index = Number(target.getAttribute("data-group-threshold-id"));
|
|
1572
|
+
const index = Number(this.getAttribute('data-group-threshold-id'));
|
|
1351
1573
|
const groupIndex = Number(this.parentElement.dataset['groupId']);
|
|
1352
1574
|
// Parent is the data group (see drawThresholdsPerGroup), not the threshold entry itself.
|
|
1353
|
-
self.showTooltip(event,
|
|
1575
|
+
self.showTooltip(event, thresholdOf(this), { index, parent: config.data[groupIndex], parentIndex: groupIndex, isThreshold: true, valueFormat: self.xAxis.format });
|
|
1354
1576
|
})
|
|
1355
1577
|
.transition()
|
|
1356
1578
|
.duration(this.transitionService.getTransitionDuration())
|
|
@@ -1358,8 +1580,8 @@ class BarHorizontalChartBuilder extends PcacChart {
|
|
|
1358
1580
|
// ScaleBand can return undefined for a key outside its domain; `.attr()` needs null, not undefined.
|
|
1359
1581
|
return this.yScaleGrouped(d.key) ?? null;
|
|
1360
1582
|
})
|
|
1361
|
-
.attr('x', (
|
|
1362
|
-
return this.xScale(
|
|
1583
|
+
.attr('x', (_, i, nodes) => {
|
|
1584
|
+
return this.xScale(Number(thresholdOf(nodes[i])?.value));
|
|
1363
1585
|
});
|
|
1364
1586
|
}
|
|
1365
1587
|
applyPreTransitionThresholdStyles(elm) {
|
|
@@ -1529,7 +1751,8 @@ class PcacBarHorizontalChartComponent {
|
|
|
1529
1751
|
}
|
|
1530
1752
|
buildChart() {
|
|
1531
1753
|
const config = this.config();
|
|
1532
|
-
|
|
1754
|
+
// Handed over even when empty: the builder clears what it last drew rather than leaving it.
|
|
1755
|
+
if (config) {
|
|
1533
1756
|
this.chartBuilder.buildChart(this.chartElm(), config);
|
|
1534
1757
|
}
|
|
1535
1758
|
}
|
|
@@ -1563,8 +1786,10 @@ class BarVerticalChartBuilder extends PcacChart {
|
|
|
1563
1786
|
yScale;
|
|
1564
1787
|
barClickedSource = new Subject();
|
|
1565
1788
|
barClicked$ = this.barClickedSource.asObservable();
|
|
1789
|
+
chartTypeLabel = 'Bar chart';
|
|
1566
1790
|
buildChart(chartElm, config) {
|
|
1567
1791
|
if (!config?.data?.length) {
|
|
1792
|
+
this.clearChart(chartElm);
|
|
1568
1793
|
return;
|
|
1569
1794
|
}
|
|
1570
1795
|
// Shallow copy: initializeAxisState() rewrites `height` for hidden axes, and that must not
|
|
@@ -1575,11 +1800,7 @@ class BarVerticalChartBuilder extends PcacChart {
|
|
|
1575
1800
|
if (!this.initializeChartState(chartElm, config)) {
|
|
1576
1801
|
return;
|
|
1577
1802
|
}
|
|
1578
|
-
|
|
1579
|
-
// reversing it directly flipped their palette on every rebuild (i.e. every resize).
|
|
1580
|
-
if (config.colorOverride && config.colorOverride.colors) {
|
|
1581
|
-
this.colors = [...config.colorOverride.colors].reverse();
|
|
1582
|
-
}
|
|
1803
|
+
this.applyColorOverride(config.colorOverride?.colors);
|
|
1583
1804
|
this.buildScales(config);
|
|
1584
1805
|
this.drawChart(chartElm, config);
|
|
1585
1806
|
}
|
|
@@ -1597,7 +1818,7 @@ class BarVerticalChartBuilder extends PcacChart {
|
|
|
1597
1818
|
this.xScaleGrouped = scaleBand()
|
|
1598
1819
|
.padding(0.2)
|
|
1599
1820
|
.rangeRound([0, this.xScaleStacked.bandwidth()])
|
|
1600
|
-
.domain(config.data
|
|
1821
|
+
.domain(seriesKeys(config.data));
|
|
1601
1822
|
}
|
|
1602
1823
|
drawChart(chartElm, config) {
|
|
1603
1824
|
this.buildContainer(chartElm);
|
|
@@ -1621,33 +1842,31 @@ class BarVerticalChartBuilder extends PcacChart {
|
|
|
1621
1842
|
});
|
|
1622
1843
|
const group = groupsContainer.selectAll('rect')
|
|
1623
1844
|
.data((d) => {
|
|
1624
|
-
return d.data;
|
|
1845
|
+
return d.data ?? [];
|
|
1625
1846
|
});
|
|
1626
1847
|
this.drawBarsPerGroup(group, config);
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
this.drawThresholdsPerGroup(group, config);
|
|
1638
|
-
}
|
|
1639
|
-
// Draw threshold across each bar in group
|
|
1640
|
-
if (config.thresholds.length > 1 && config.thresholds[0].data && !config.isStacked) {
|
|
1641
|
-
this.drawThresholdsPerBarInGroup(group, config);
|
|
1848
|
+
switch (barThresholdLayout(config.thresholds, config.isStacked)) {
|
|
1849
|
+
case 'chart':
|
|
1850
|
+
this.drawThresholdAcrossChart(config);
|
|
1851
|
+
break;
|
|
1852
|
+
case 'group':
|
|
1853
|
+
this.drawThresholdsPerGroup(config);
|
|
1854
|
+
break;
|
|
1855
|
+
case 'bar':
|
|
1856
|
+
this.drawThresholdsPerBarInGroup(group, config);
|
|
1857
|
+
break;
|
|
1642
1858
|
}
|
|
1643
1859
|
}
|
|
1644
1860
|
drawBarsPerGroup(group, config) {
|
|
1645
1861
|
const self = this;
|
|
1646
1862
|
// Stacked bars sit on top of the ones before them in their group; every other layout draws
|
|
1647
1863
|
// from the baseline (start 0).
|
|
1648
|
-
|
|
1864
|
+
// Sizes honor `hide` (and treat a null or non-numeric value as 0), so a hidden bar keeps its
|
|
1865
|
+
// slot and color but draws nothing - see `barSizes`.
|
|
1866
|
+
const sizes = barSizes(config.data);
|
|
1867
|
+
const starts = config.isStacked ? stackStarts(config.data, sizes) : new Map();
|
|
1649
1868
|
const startOf = (d) => starts.get(d) ?? 0;
|
|
1650
|
-
const endOf = (d) => startOf(d) +
|
|
1869
|
+
const endOf = (d) => startOf(d) + (sizes.get(d) ?? 0);
|
|
1651
1870
|
group.enter().append('rect')
|
|
1652
1871
|
.attr('class', 'pcac-bar')
|
|
1653
1872
|
.attr('x', (d) => {
|
|
@@ -1669,7 +1888,7 @@ class BarVerticalChartBuilder extends PcacChart {
|
|
|
1669
1888
|
})
|
|
1670
1889
|
.attr('height', 0)
|
|
1671
1890
|
.on('mouseover', function () {
|
|
1672
|
-
select
|
|
1891
|
+
select(this)
|
|
1673
1892
|
.transition()
|
|
1674
1893
|
.duration(self.transitionService.getTransitionDuration() / 5)
|
|
1675
1894
|
.style('fill', () => {
|
|
@@ -1692,7 +1911,7 @@ class BarVerticalChartBuilder extends PcacChart {
|
|
|
1692
1911
|
})
|
|
1693
1912
|
.on('mouseout', function () {
|
|
1694
1913
|
self.hideTooltip();
|
|
1695
|
-
select
|
|
1914
|
+
select(this)
|
|
1696
1915
|
.transition()
|
|
1697
1916
|
.duration(self.transitionService.getTransitionDuration() / 5)
|
|
1698
1917
|
.style('fill', () => {
|
|
@@ -1721,58 +1940,71 @@ class BarVerticalChartBuilder extends PcacChart {
|
|
|
1721
1940
|
});
|
|
1722
1941
|
}
|
|
1723
1942
|
drawThresholdAcrossChart(config) {
|
|
1943
|
+
const threshold = groupThreshold(config.thresholds, 0);
|
|
1944
|
+
if (!threshold) {
|
|
1945
|
+
return;
|
|
1946
|
+
}
|
|
1724
1947
|
this.applyPreTransitionThresholdStyles(this.svg.select('.pcac-bars').append('rect'))
|
|
1725
1948
|
.attr('width', this.width)
|
|
1726
|
-
.attr('data-group-threshold-id',
|
|
1727
|
-
return i;
|
|
1728
|
-
})
|
|
1949
|
+
.attr('data-group-threshold-id', 0)
|
|
1729
1950
|
.on('mousemove', (event) => {
|
|
1730
|
-
this.showTooltip(event,
|
|
1951
|
+
this.showTooltip(event, threshold, { index: 0, isThreshold: true, valueFormat: this.yAxis.format });
|
|
1731
1952
|
})
|
|
1732
1953
|
.transition()
|
|
1733
1954
|
.duration(this.transitionService.getTransitionDuration())
|
|
1734
|
-
.attr('y', (
|
|
1735
|
-
return this.yScale(config.thresholds[i].value);
|
|
1736
|
-
});
|
|
1955
|
+
.attr('y', this.yScale(Number(threshold.value)));
|
|
1737
1956
|
}
|
|
1738
|
-
|
|
1957
|
+
/** One threshold across each group that has one; groups without an entry are skipped. */
|
|
1958
|
+
drawThresholdsPerGroup(config) {
|
|
1739
1959
|
const self = this;
|
|
1740
|
-
|
|
1960
|
+
const groupIndexOf = (rect) => Number(rect.parentElement.dataset['groupId']);
|
|
1961
|
+
const groups = this.svg.selectAll('.pcac-bar-group')
|
|
1962
|
+
.filter((_, i) => groupThreshold(config.thresholds, i) !== null);
|
|
1963
|
+
this.applyPreTransitionThresholdStyles(groups.append('rect'))
|
|
1964
|
+
// Spans its whole group: the pre-transition styles place a rect by bar key, which a group
|
|
1965
|
+
// key can collide with.
|
|
1966
|
+
.attr('x', 0)
|
|
1741
1967
|
.attr('width', this.xScaleStacked.bandwidth())
|
|
1742
|
-
.attr('data-group-threshold-id', (
|
|
1743
|
-
return
|
|
1968
|
+
.attr('data-group-threshold-id', function () {
|
|
1969
|
+
return groupIndexOf(this);
|
|
1744
1970
|
})
|
|
1745
1971
|
.on('mousemove', function (event) {
|
|
1746
|
-
const index =
|
|
1747
|
-
const threshold = config.thresholds[index];
|
|
1972
|
+
const index = groupIndexOf(this);
|
|
1748
1973
|
// A threshold's own PcacData is typically just a value, so its parent is the *data* group
|
|
1749
1974
|
// it's drawn against (which has the key) rather than the threshold entry it came from.
|
|
1750
|
-
self.showTooltip(event, config.
|
|
1975
|
+
self.showTooltip(event, groupThreshold(config.thresholds, index), { index, parent: config.data[index], parentIndex: index, isThreshold: true, valueFormat: self.yAxis.format });
|
|
1751
1976
|
})
|
|
1752
1977
|
.transition()
|
|
1753
1978
|
.duration(this.transitionService.getTransitionDuration())
|
|
1754
|
-
.attr('y', (_, i) => {
|
|
1755
|
-
return this.yScale(config.
|
|
1979
|
+
.attr('y', (_, i, nodes) => {
|
|
1980
|
+
return this.yScale(Number(groupThreshold(config.thresholds, groupIndexOf(nodes[i]))?.value));
|
|
1756
1981
|
});
|
|
1757
1982
|
}
|
|
1983
|
+
/** One threshold across each bar that has one; bars without an entry are skipped. */
|
|
1758
1984
|
drawThresholdsPerBarInGroup(group, config) {
|
|
1759
1985
|
const self = this;
|
|
1760
|
-
|
|
1986
|
+
const thresholdOf = (rect) => barThreshold(config.thresholds, Number(rect.parentElement.dataset['groupId']), Number(rect.getAttribute('data-group-threshold-id')));
|
|
1987
|
+
const rects = this.applyPreTransitionThresholdStyles(group.enter().append('rect'))
|
|
1761
1988
|
.attr('data-group-threshold-id', (_, i) => {
|
|
1762
1989
|
return i;
|
|
1990
|
+
})
|
|
1991
|
+
.attr('width', this.xScaleGrouped.bandwidth());
|
|
1992
|
+
rects.filter(function () {
|
|
1993
|
+
return thresholdOf(this) === null;
|
|
1994
|
+
}).remove();
|
|
1995
|
+
rects.filter(function () {
|
|
1996
|
+
return thresholdOf(this) !== null;
|
|
1763
1997
|
})
|
|
1764
1998
|
.on('mousemove', function (event) {
|
|
1765
|
-
const
|
|
1766
|
-
const index = Number(target.getAttribute("data-group-threshold-id"));
|
|
1999
|
+
const index = Number(this.getAttribute('data-group-threshold-id'));
|
|
1767
2000
|
const groupIndex = Number(this.parentElement.dataset['groupId']);
|
|
1768
2001
|
// Parent is the data group (see drawThresholdsPerGroup), not the threshold entry itself.
|
|
1769
|
-
self.showTooltip(event,
|
|
2002
|
+
self.showTooltip(event, thresholdOf(this), { index, parent: config.data[groupIndex], parentIndex: groupIndex, isThreshold: true, valueFormat: self.yAxis.format });
|
|
1770
2003
|
})
|
|
1771
|
-
.attr('width', this.xScaleGrouped.bandwidth())
|
|
1772
2004
|
.transition()
|
|
1773
2005
|
.duration(this.transitionService.getTransitionDuration())
|
|
1774
|
-
.attr('y', (
|
|
1775
|
-
return this.yScale(
|
|
2006
|
+
.attr('y', (_, i, nodes) => {
|
|
2007
|
+
return this.yScale(Number(thresholdOf(nodes[i])?.value));
|
|
1776
2008
|
});
|
|
1777
2009
|
}
|
|
1778
2010
|
applyPreTransitionThresholdStyles(elm) {
|
|
@@ -1843,7 +2075,8 @@ class PcacBarVerticalChartComponent {
|
|
|
1843
2075
|
}
|
|
1844
2076
|
buildChart() {
|
|
1845
2077
|
const config = this.config();
|
|
1846
|
-
|
|
2078
|
+
// Handed over even when empty: the builder clears what it last drew rather than leaving it.
|
|
2079
|
+
if (config) {
|
|
1847
2080
|
this.chartBuilder.buildChart(this.chartElm(), config);
|
|
1848
2081
|
}
|
|
1849
2082
|
}
|
|
@@ -1865,6 +2098,10 @@ class PcacBarVerticalChartColorOverrideConfig {
|
|
|
1865
2098
|
colors = [];
|
|
1866
2099
|
}
|
|
1867
2100
|
|
|
2101
|
+
// Only what a consumer configures, binds or styles with. The chart plumbing - the `PcacChart`
|
|
2102
|
+
// base class and the builders, the axis/grid/tooltip/transition/resize services, and the
|
|
2103
|
+
// resolved-config helpers - is internal, so it can change without breaking anyone.
|
|
2104
|
+
|
|
1868
2105
|
class PcacLegendConfigItem {
|
|
1869
2106
|
label;
|
|
1870
2107
|
checked = true;
|
|
@@ -2127,12 +2364,22 @@ class PlaChartEffectsBuilder {
|
|
|
2127
2364
|
break;
|
|
2128
2365
|
}
|
|
2129
2366
|
}
|
|
2130
|
-
const
|
|
2131
|
-
select
|
|
2132
|
-
.text(
|
|
2367
|
+
const value = this.config.y.invert(pos.y);
|
|
2368
|
+
select(nodes[index]).select('text')
|
|
2369
|
+
.text(this.formatValue(value));
|
|
2133
2370
|
return 'translate(' + mousePos[0] + ',' + pos.y + ')';
|
|
2134
2371
|
});
|
|
2135
2372
|
}
|
|
2373
|
+
/**
|
|
2374
|
+
* The crosshair's value, formatted the way the y axis labels its ticks - same format, same
|
|
2375
|
+
* step-based precision - rather than rounded to a whole number, which read a 0-1 axis as 0 or 1.
|
|
2376
|
+
*/
|
|
2377
|
+
formatValue(value) {
|
|
2378
|
+
const y = this.config.y;
|
|
2379
|
+
const formatter = axisTickFormat(this.config.yFormat, y, this.config.yTicks)
|
|
2380
|
+
?? y.tickFormat(this.config.yTicks);
|
|
2381
|
+
return formatter(value);
|
|
2382
|
+
}
|
|
2136
2383
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.7", ngImport: i0, type: PlaChartEffectsBuilder, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
2137
2384
|
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.1.7", ngImport: i0, type: PlaChartEffectsBuilder });
|
|
2138
2385
|
}
|
|
@@ -2254,8 +2501,16 @@ function getXFormat(type, data, index, xScale) {
|
|
|
2254
2501
|
}
|
|
2255
2502
|
}
|
|
2256
2503
|
|
|
2504
|
+
/** Whether a point has a numeric value to draw at. */
|
|
2505
|
+
function hasValue(d) {
|
|
2506
|
+
return d.value !== null && d.value !== undefined && d.value !== '' && Number.isFinite(Number(d.value));
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2257
2509
|
function buildLineGenerator(format, scales) {
|
|
2258
2510
|
return line()
|
|
2511
|
+
// A point with no numeric value leaves a gap rather than a NaN in the path, which browsers stop
|
|
2512
|
+
// drawing at - hiding the rest of the line.
|
|
2513
|
+
.defined(hasValue)
|
|
2259
2514
|
.x((d, i) => {
|
|
2260
2515
|
return getXFormat(format, d, i, scales.x);
|
|
2261
2516
|
})
|
|
@@ -2266,6 +2521,9 @@ function buildLineGenerator(format, scales) {
|
|
|
2266
2521
|
|
|
2267
2522
|
function buildAreaGenerator(format, scales, chartHeight) {
|
|
2268
2523
|
return area()
|
|
2524
|
+
// A point with no numeric value leaves a gap rather than a NaN in the path, which browsers stop
|
|
2525
|
+
// drawing at - hiding the rest of the area.
|
|
2526
|
+
.defined(hasValue)
|
|
2269
2527
|
.x((d, i) => {
|
|
2270
2528
|
return getXFormat(format, d, i, scales.x);
|
|
2271
2529
|
})
|
|
@@ -2573,6 +2831,12 @@ const DOT_GROW_TRANSITION = 'pcac-dot-grow';
|
|
|
2573
2831
|
const DOT_HOVER_TRANSITION = 'pcac-dot-hover';
|
|
2574
2832
|
/** See `revealAfterEntry`. */
|
|
2575
2833
|
const REVEAL_TRANSITION = 'pcac-reveal';
|
|
2834
|
+
/** Each chart type's accessible name when its config gives no `ariaLabel`. */
|
|
2835
|
+
const CHART_TYPE_LABELS = {
|
|
2836
|
+
[PcacLineAreaPlotChartConfigType.Line]: 'Line chart',
|
|
2837
|
+
[PcacLineAreaPlotChartConfigType.Area]: 'Area chart',
|
|
2838
|
+
[PcacLineAreaPlotChartConfigType.Plot]: 'Plot chart',
|
|
2839
|
+
};
|
|
2576
2840
|
class PlaChartBuilder extends PcacChart {
|
|
2577
2841
|
effectsBuilder = inject(PlaChartEffectsBuilder);
|
|
2578
2842
|
scales;
|
|
@@ -2620,6 +2884,8 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2620
2884
|
dotClicked$ = this.dotClickedSource.asObservable();
|
|
2621
2885
|
buildChart(chartElm, config, type) {
|
|
2622
2886
|
if (!config?.data?.length) {
|
|
2887
|
+
this.leavePoint();
|
|
2888
|
+
this.clearChart(chartElm);
|
|
2623
2889
|
return;
|
|
2624
2890
|
}
|
|
2625
2891
|
// A rebuild throws the hovered point away with everything else, and a removed element gets
|
|
@@ -2634,22 +2900,14 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2634
2900
|
}));
|
|
2635
2901
|
// A hidden axis keeps 8px rather than 0 so a dot on the edge of the plot isn't clipped
|
|
2636
2902
|
this.initializeAxisState(this.config, 'y', 8);
|
|
2903
|
+
this.chartTypeLabel = CHART_TYPE_LABELS[type];
|
|
2637
2904
|
this.resolvePointLayout(type);
|
|
2638
2905
|
this.reservePointSpace();
|
|
2639
2906
|
this.pointRange = this.config.pointRange ? { ...new PcacPointRangeConfig(), ...this.config.pointRange } : null;
|
|
2640
2907
|
if (!this.initializeChartState(chartElm, this.config)) {
|
|
2641
2908
|
return;
|
|
2642
2909
|
}
|
|
2643
|
-
|
|
2644
|
-
// `[]`, and an empty array is truthy - a config built with `new` would otherwise replace the
|
|
2645
|
-
// theme palette with nothing and every series would render with no color at all. Cycled out to
|
|
2646
|
-
// as many entries as `initializeChartState` gave the theme palette, the same way that palette
|
|
2647
|
-
// cycles its own colors, so an override shorter than the data repeats rather than leaving every
|
|
2648
|
-
// series past its end with an `undefined` color (and so no line, dot stroke or range).
|
|
2649
|
-
const override = this.config.colorOverride;
|
|
2650
|
-
if (override?.length) {
|
|
2651
|
-
this.colors = Array.from({ length: Math.max(this.colors.length, override.length) }, (_, i) => override[i % override.length]);
|
|
2652
|
-
}
|
|
2910
|
+
this.applyColorOverride(this.config.colorOverride);
|
|
2653
2911
|
this.scales = new PlaChartScalesBuilder().build(this.xAxis, this.yAxis, this.config.data, this.width, this.height);
|
|
2654
2912
|
this.lineGenerator = buildLineGenerator(this.xAxis.format, this.scales);
|
|
2655
2913
|
this.areaGenerator = buildAreaGenerator(this.xAxis.format, this.scales, this.height);
|
|
@@ -2735,7 +2993,9 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2735
2993
|
data: config.data,
|
|
2736
2994
|
colors: this.colors,
|
|
2737
2995
|
x: this.scales.x,
|
|
2738
|
-
y: this.scales.y
|
|
2996
|
+
y: this.scales.y,
|
|
2997
|
+
yFormat: this.yAxis.format,
|
|
2998
|
+
yTicks: this.yAxis.ticks,
|
|
2739
2999
|
});
|
|
2740
3000
|
}
|
|
2741
3001
|
// Axes above the lines/areas (so an axis line isn't covered by a series sitting at x = 0),
|
|
@@ -2945,7 +3205,7 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2945
3205
|
if (!node) {
|
|
2946
3206
|
return;
|
|
2947
3207
|
}
|
|
2948
|
-
const group = select
|
|
3208
|
+
const group = select(node);
|
|
2949
3209
|
const { x, y } = this.pointPosition(point, index, scales);
|
|
2950
3210
|
const extent = rangeExtent(point, x, y, this.xAxis.format, scales);
|
|
2951
3211
|
if (!extent) {
|
|
@@ -2964,7 +3224,7 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2964
3224
|
this.svg.selectAll('.point-range.is-focused').classed('is-focused', false);
|
|
2965
3225
|
const node = d ? this.rangeGroupOf.get(d) : undefined;
|
|
2966
3226
|
if (node) {
|
|
2967
|
-
select
|
|
3227
|
+
select(node).classed('is-focused', true);
|
|
2968
3228
|
}
|
|
2969
3229
|
this.svg.select('.point-ranges').classed('has-focus', !!node);
|
|
2970
3230
|
}
|
|
@@ -3062,12 +3322,12 @@ class PlaChartBuilder extends PcacChart {
|
|
|
3062
3322
|
valueFormat: self.yAxis.format,
|
|
3063
3323
|
keyFormat: self.xAxis.format,
|
|
3064
3324
|
// The dot or image itself rather than the group, so the tooltip clears what's drawn.
|
|
3065
|
-
anchor: select
|
|
3325
|
+
anchor: select(this).select('.dot, .dot-image').node(),
|
|
3066
3326
|
});
|
|
3067
3327
|
// No-op for an image point (no circle inside to grow). Its own transition name, so it
|
|
3068
3328
|
// runs alongside the enter rise rather than cancelling it (see DOT_HOVER_TRANSITION);
|
|
3069
3329
|
// it only takes over the entry's size tween, which it would otherwise fight over `r`.
|
|
3070
|
-
select
|
|
3330
|
+
select(this).select('.dot')
|
|
3071
3331
|
.interrupt(DOT_GROW_TRANSITION)
|
|
3072
3332
|
.transition(DOT_HOVER_TRANSITION)
|
|
3073
3333
|
.duration(duration / 3)
|
|
@@ -3123,7 +3383,7 @@ class PlaChartBuilder extends PcacChart {
|
|
|
3123
3383
|
this.hoveredPoint = null;
|
|
3124
3384
|
this.hideTooltip();
|
|
3125
3385
|
this.focusPointRange(null);
|
|
3126
|
-
select
|
|
3386
|
+
select(point).select('.dot')
|
|
3127
3387
|
.interrupt(DOT_GROW_TRANSITION)
|
|
3128
3388
|
.transition(DOT_HOVER_TRANSITION)
|
|
3129
3389
|
.duration(this.transitionService.getTransitionDuration() / 3)
|
|
@@ -3257,7 +3517,8 @@ class PcacLineAreaChartComponent {
|
|
|
3257
3517
|
}
|
|
3258
3518
|
buildChart() {
|
|
3259
3519
|
const config = this.config();
|
|
3260
|
-
|
|
3520
|
+
// Handed over even when empty: the builder clears what it last drew rather than leaving it.
|
|
3521
|
+
if (config) {
|
|
3261
3522
|
this.chartBuilder.buildChart(this.chartElm(), config, this.type());
|
|
3262
3523
|
}
|
|
3263
3524
|
}
|
|
@@ -3340,6 +3601,57 @@ class PcacAreaChartConfig extends PcacLineAreaChartConfig {
|
|
|
3340
3601
|
// the point-image and point-range types are part of those configs' public shape, so they're
|
|
3341
3602
|
// exported alone.
|
|
3342
3603
|
|
|
3604
|
+
/**
|
|
3605
|
+
* Turns the pie into a donut: slices drawn as a ring around an empty center, which can hold a
|
|
3606
|
+
* short label - typically the total the slices add up to.
|
|
3607
|
+
*/
|
|
3608
|
+
class PcacPieDonutConfig {
|
|
3609
|
+
/**
|
|
3610
|
+
* Radius of the hole as a fraction of the pie's radius, from 0 (a plain pie) to 0.9. Values
|
|
3611
|
+
* outside that range are clamped.
|
|
3612
|
+
*/
|
|
3613
|
+
innerRadius = 0.6;
|
|
3614
|
+
/**
|
|
3615
|
+
* Large text centered in the hole, e.g. `'67'`. Sized to the hole and shrunk to fit across it;
|
|
3616
|
+
* left out if it would have to go below 8px. Not drawn when not set.
|
|
3617
|
+
*/
|
|
3618
|
+
label;
|
|
3619
|
+
/** Smaller line under `label`, e.g. `'balls'`, fitted the same way. Not drawn when not set. */
|
|
3620
|
+
subLabel;
|
|
3621
|
+
/**
|
|
3622
|
+
* `label` color, any CSS color; theme default `$gray-800`. Applied as
|
|
3623
|
+
* `--pcac-pie-center-label-color` on the chart's `.pcac-pie-center` group, so a stylesheet can
|
|
3624
|
+
* set it on an ancestor instead (a config value wins).
|
|
3625
|
+
*/
|
|
3626
|
+
labelColor;
|
|
3627
|
+
/** `subLabel` color, likewise (`--pcac-pie-center-sub-label-color`); theme default `$gray-600`. */
|
|
3628
|
+
subLabelColor;
|
|
3629
|
+
}
|
|
3630
|
+
class PcacPieChartConfig extends PcacChartConfig {
|
|
3631
|
+
/**
|
|
3632
|
+
* Draw the pie as a donut. Off when not set; `{}` turns it on with every `PcacPieDonutConfig`
|
|
3633
|
+
* default. Slices, hover and `sliceClicked` behave the same either way.
|
|
3634
|
+
*/
|
|
3635
|
+
donut;
|
|
3636
|
+
/**
|
|
3637
|
+
* Slice colors, any CSS color, in data order - the first slice takes the first color - in place
|
|
3638
|
+
* of the theme palette. Repeated when there are more slices than colors. Omitted or empty, the
|
|
3639
|
+
* theme palette is used. Match a `<pcac-legend>` to it with each item's `colorOverride`.
|
|
3640
|
+
*/
|
|
3641
|
+
colorOverride;
|
|
3642
|
+
}
|
|
3643
|
+
|
|
3644
|
+
/** The largest `PcacPieDonutConfig.innerRadius` honored, so a ring is always left to draw. */
|
|
3645
|
+
const MAX_DONUT_INNER_RADIUS = 0.9;
|
|
3646
|
+
/** Smallest font a center line is drawn at; a line that can't fit the hole at this size is left out. */
|
|
3647
|
+
const MIN_CENTER_FONT_SIZE = 8;
|
|
3648
|
+
/** Share of the hole's diameter the center text may span, leaving a margin inside the ring. */
|
|
3649
|
+
const CENTER_TEXT_WIDTH = 1.6;
|
|
3650
|
+
/**
|
|
3651
|
+
* Average glyph width as a fraction of font size. Only used where text can't be measured (no
|
|
3652
|
+
* layout, e.g. jsdom); in a browser each line is measured and scaled to fit instead.
|
|
3653
|
+
*/
|
|
3654
|
+
const ESTIMATED_GLYPH_WIDTH = 0.6;
|
|
3343
3655
|
/**
|
|
3344
3656
|
* Provided per-component (see PcacPieChartComponent's `providers`), not root-scoped:
|
|
3345
3657
|
* this builder extends PcacChart, which holds mutable per-chart-instance state (margin, width,
|
|
@@ -3348,13 +3660,16 @@ class PcacAreaChartConfig extends PcacLineAreaChartConfig {
|
|
|
3348
3660
|
*/
|
|
3349
3661
|
class PieChartBuilder extends PcacChart {
|
|
3350
3662
|
radius;
|
|
3663
|
+
innerRadius = 0;
|
|
3351
3664
|
arcShape;
|
|
3352
3665
|
arcOverShape;
|
|
3353
3666
|
pieAngles;
|
|
3354
3667
|
sliceClickedSource = new Subject();
|
|
3355
3668
|
sliceClicked$ = this.sliceClickedSource.asObservable();
|
|
3669
|
+
chartTypeLabel = 'Pie chart';
|
|
3356
3670
|
buildChart(chartElm, config) {
|
|
3357
3671
|
if (!config?.data?.length) {
|
|
3672
|
+
this.clearChart(chartElm);
|
|
3358
3673
|
return;
|
|
3359
3674
|
}
|
|
3360
3675
|
// The pie deliberately opts out of `heightFull`: `height` here feeds the radius rather than a
|
|
@@ -3365,21 +3680,31 @@ class PieChartBuilder extends PcacChart {
|
|
|
3365
3680
|
if (!this.initializeChartState(chartElm, { ...config, heightFull: false })) {
|
|
3366
3681
|
return;
|
|
3367
3682
|
}
|
|
3683
|
+
this.applyColorOverride(config.colorOverride);
|
|
3368
3684
|
this.radius = Math.min(Math.min(this.height, this.width), Math.min(this.height, this.width)) / 2;
|
|
3369
|
-
this.buildShapes();
|
|
3685
|
+
this.buildShapes(config.donut);
|
|
3370
3686
|
this.drawChart(chartElm, config);
|
|
3687
|
+
if (config.donut && this.innerRadius > 0) {
|
|
3688
|
+
this.drawCenter(config.donut);
|
|
3689
|
+
}
|
|
3371
3690
|
}
|
|
3372
|
-
buildShapes() {
|
|
3691
|
+
buildShapes(donut) {
|
|
3373
3692
|
const radiusOffset = 10;
|
|
3693
|
+
const outerRadius = this.radius - radiusOffset;
|
|
3694
|
+
const ratio = donut ? (donut.innerRadius ?? new PcacPieDonutConfig().innerRadius) : 0;
|
|
3695
|
+
// Sized against the resting slice, so the hole stays put when a hovered slice grows outward.
|
|
3696
|
+
this.innerRadius = Math.max(0, outerRadius * Math.min(Math.max(ratio, 0), MAX_DONUT_INNER_RADIUS));
|
|
3374
3697
|
this.arcShape = arc()
|
|
3375
|
-
.innerRadius(
|
|
3376
|
-
.outerRadius(
|
|
3698
|
+
.innerRadius(this.innerRadius)
|
|
3699
|
+
.outerRadius(outerRadius);
|
|
3377
3700
|
this.arcOverShape = arc()
|
|
3378
|
-
.innerRadius(
|
|
3379
|
-
.outerRadius(
|
|
3701
|
+
.innerRadius(this.innerRadius)
|
|
3702
|
+
.outerRadius(outerRadius + radiusOffset);
|
|
3380
3703
|
this.pieAngles = pie()
|
|
3381
3704
|
.sort(null)
|
|
3382
|
-
|
|
3705
|
+
// A hidden slice takes no angle but keeps its index - and so its color, matching a legend -
|
|
3706
|
+
// the same way a hidden line/area/plot series keeps its place.
|
|
3707
|
+
.value((d) => (d.hide ? 0 : Number(d.value ?? 0)));
|
|
3383
3708
|
}
|
|
3384
3709
|
drawChart(chartElm, config) {
|
|
3385
3710
|
this.buildContainer(chartElm, true);
|
|
@@ -3396,7 +3721,7 @@ class PieChartBuilder extends PcacChart {
|
|
|
3396
3721
|
const t = transition().duration(self.transitionService.getTransitionDuration() / 3);
|
|
3397
3722
|
const c = color(self.colors[d.index]);
|
|
3398
3723
|
const ct = c ? c.darker(1).toString() : self.colors[d.index];
|
|
3399
|
-
select
|
|
3724
|
+
select(this).transition(t)
|
|
3400
3725
|
.attr('d', self.arcOverShape)
|
|
3401
3726
|
.style('fill', ct);
|
|
3402
3727
|
})
|
|
@@ -3405,7 +3730,7 @@ class PieChartBuilder extends PcacChart {
|
|
|
3405
3730
|
})
|
|
3406
3731
|
.on('mouseout', function (_, d) {
|
|
3407
3732
|
self.hideTooltip();
|
|
3408
|
-
select
|
|
3733
|
+
select(this)
|
|
3409
3734
|
.transition()
|
|
3410
3735
|
.duration(self.transitionService.getTransitionDuration() / 3)
|
|
3411
3736
|
.attr('d', self.arcShape)
|
|
@@ -3420,10 +3745,62 @@ class PieChartBuilder extends PcacChart {
|
|
|
3420
3745
|
return this.tweenChart(b);
|
|
3421
3746
|
});
|
|
3422
3747
|
}
|
|
3748
|
+
/**
|
|
3749
|
+
* The donut's `label` / `subLabel`, stacked around the center of the hole. Each line starts at a
|
|
3750
|
+
* size proportional to the hole and shrinks until it fits across it; a line that would have to
|
|
3751
|
+
* drop below `MIN_CENTER_FONT_SIZE` is left out rather than spill over the slices. Drawn after
|
|
3752
|
+
* the slices and ignoring the pointer, so it never blocks a slice's hover or click.
|
|
3753
|
+
*/
|
|
3754
|
+
drawCenter(donut) {
|
|
3755
|
+
if (!donut.label && !donut.subLabel) {
|
|
3756
|
+
return;
|
|
3757
|
+
}
|
|
3758
|
+
const maxWidth = this.innerRadius * CENTER_TEXT_WIDTH;
|
|
3759
|
+
const center = this.svg.append('g')
|
|
3760
|
+
.attr('class', 'pcac-pie-center')
|
|
3761
|
+
.attr('pointer-events', 'none')
|
|
3762
|
+
.style('--pcac-pie-center-label-color', () => donut.labelColor ?? null)
|
|
3763
|
+
.style('--pcac-pie-center-sub-label-color', () => donut.subLabelColor ?? null);
|
|
3764
|
+
const label = this.drawCenterLine(center, 'pcac-pie-center-label', donut.label, Math.min(this.innerRadius * 0.5, 40), maxWidth);
|
|
3765
|
+
const subLabel = this.drawCenterLine(center, 'pcac-pie-center-sub-label', donut.subLabel, Math.min(this.innerRadius * 0.2, 16), maxWidth);
|
|
3766
|
+
if (!label && !subLabel) {
|
|
3767
|
+
center.remove();
|
|
3768
|
+
return;
|
|
3769
|
+
}
|
|
3770
|
+
// Stacked as a pair about the center; a line on its own sits on it.
|
|
3771
|
+
if (label && subLabel) {
|
|
3772
|
+
label.element.attr('y', -subLabel.size * 0.6);
|
|
3773
|
+
subLabel.element.attr('y', label.size * 0.55);
|
|
3774
|
+
}
|
|
3775
|
+
}
|
|
3776
|
+
/**
|
|
3777
|
+
* One centered line of the donut's center text at the largest size up to `preferredSize` that
|
|
3778
|
+
* fits `maxWidth`, or nothing (null) when there's no text or it can't fit at a readable size.
|
|
3779
|
+
*/
|
|
3780
|
+
drawCenterLine(center, className, text, preferredSize, maxWidth) {
|
|
3781
|
+
if (!text || preferredSize < MIN_CENTER_FONT_SIZE) {
|
|
3782
|
+
return null;
|
|
3783
|
+
}
|
|
3784
|
+
const element = center.append('text')
|
|
3785
|
+
.attr('class', className)
|
|
3786
|
+
.attr('text-anchor', 'middle')
|
|
3787
|
+
.attr('dominant-baseline', 'central')
|
|
3788
|
+
.style('font-size', `${preferredSize}px`)
|
|
3789
|
+
.text(text);
|
|
3790
|
+
const node = element.node();
|
|
3791
|
+
const measured = node && typeof node.getComputedTextLength === 'function'
|
|
3792
|
+
? node.getComputedTextLength()
|
|
3793
|
+
: 0;
|
|
3794
|
+
const width = measured > 0 ? measured : text.length * preferredSize * ESTIMATED_GLYPH_WIDTH;
|
|
3795
|
+
const size = width > maxWidth ? preferredSize * (maxWidth / width) : preferredSize;
|
|
3796
|
+
if (size < MIN_CENTER_FONT_SIZE) {
|
|
3797
|
+
element.remove();
|
|
3798
|
+
return null;
|
|
3799
|
+
}
|
|
3800
|
+
element.style('font-size', `${size}px`);
|
|
3801
|
+
return { element, size };
|
|
3802
|
+
}
|
|
3423
3803
|
tweenChart(b) {
|
|
3424
|
-
// `innerRadius`/`outerRadius` aren't part of PieArcDatum - arcShape's own .innerRadius()/.outerRadius()
|
|
3425
|
-
// accessors are fixed constants and never read them from the datum, so this is a legacy no-op kept for parity.
|
|
3426
|
-
b.innerRadius = 0;
|
|
3427
3804
|
const i = interpolate({ startAngle: 0, endAngle: 0 }, b);
|
|
3428
3805
|
return (t) => this.arcShape(i(t)) ?? '';
|
|
3429
3806
|
}
|
|
@@ -3466,7 +3843,8 @@ class PcacPieChartComponent {
|
|
|
3466
3843
|
}
|
|
3467
3844
|
buildChart() {
|
|
3468
3845
|
const config = this.config();
|
|
3469
|
-
|
|
3846
|
+
// Handed over even when empty: the builder clears what it last drew rather than leaving it.
|
|
3847
|
+
if (config) {
|
|
3470
3848
|
this.chartBuilder.buildChart(this.chartElm(), config);
|
|
3471
3849
|
}
|
|
3472
3850
|
}
|
|
@@ -3478,9 +3856,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.7", ngImpor
|
|
|
3478
3856
|
args: [{ selector: 'pcac-pie-chart', encapsulation: ViewEncapsulation.None, providers: [PieChartBuilder], template: "<section class=\"pcac-pie-chart\">\n <svg #chart></svg>\n</section>\n\n", styles: [".pcac-pie-chart svg{width:100%;display:block}.pcac-pie-chart .dot{fill:none;stroke-width:1px}\n"] }]
|
|
3479
3857
|
}], ctorParameters: () => [], propDecorators: { config: [{ type: i0.Input, args: [{ isSignal: true, alias: "config", required: true }] }], chartElm: [{ type: i0.ViewChild, args: ['chart', { isSignal: true }] }], sliceClicked: [{ type: i0.Output, args: ["sliceClicked"] }], tooltipTemplate: [{ type: i0.ContentChild, args: [i0.forwardRef(() => PcacTooltipDirective), { isSignal: true }] }] } });
|
|
3480
3858
|
|
|
3481
|
-
class PcacPieChartConfig extends PcacChartConfig {
|
|
3482
|
-
}
|
|
3483
|
-
|
|
3484
3859
|
/*
|
|
3485
3860
|
* Public API Surface of pioneer-charts
|
|
3486
3861
|
*/
|
|
@@ -3489,5 +3864,5 @@ class PcacPieChartConfig extends PcacChartConfig {
|
|
|
3489
3864
|
* Generated bundle index. Do not edit.
|
|
3490
3865
|
*/
|
|
3491
3866
|
|
|
3492
|
-
export {
|
|
3867
|
+
export { PcacAreaChart, PcacAreaChartConfig, PcacAxisChartConfig, PcacAxisConfig, PcacAxisSubLabels, PcacBarHorizontalChartColorOverrideConfig, PcacBarHorizontalChartComponent, PcacBarHorizontalChartConfig, PcacBarVerticalChartColorOverrideConfig, PcacBarVerticalChartComponent, PcacBarVerticalChartConfig, PcacChartConfig, PcacColorService, PcacData, PcacDataRange, PcacFormatEnum, PcacLegend, PcacLegendConfig, PcacLegendConfigItem, PcacLineChart, PcacLineChartConfig, PcacPieChartComponent, PcacPieChartConfig, PcacPieDonutConfig, PcacPlotChart, PcacPlotChartConfig, PcacPointFanOutConfig, PcacPointImageConfig, PcacPointRangeConfig, PcacPointRangeShow, PcacPointRangeStyle, PcacRangeBounds, PcacTooltipDirective };
|
|
3493
3868
|
//# sourceMappingURL=pioneer-code-pioneer-charts.mjs.map
|