ngx-mira-charts 0.4.0-dev.1 → 0.4.0-dev.2
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/ngx-mira-charts.mjs +220 -90
- package/fesm2022/ngx-mira-charts.mjs.map +1 -1
- package/index.d.ts +114 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Composable SVG charts for Angular, built on signals. A chart is put together in
|
|
|
8
8
|
|
|
9
9
|
## Why this one
|
|
10
10
|
|
|
11
|
-
- 🧩 **Composed in the template.** Write the parts you need and leave out the rest. A second `<mira-bar>` makes grouped bars, with nothing to set on the chart.
|
|
11
|
+
- 🧩 **Composed in the template.** Write the parts you need and leave out the rest. A second `<mira-bar>` makes grouped bars, and a `stackId` on each stacks them, with nothing to set on the chart.
|
|
12
12
|
- 📋 **Your rows, in their own shape.** A chart takes an array of plain objects, and each series reads its values by `dataKey`. The values must be numbers: a string, even `'42'`, is left out.
|
|
13
13
|
- 🅰️ **Angular all the way down.** Standalone components with signal inputs, OnPush throughout, with zone.js or without it.
|
|
14
14
|
- 🪶 **Small.** No d3 and no chart engine, and no runtime dependencies beyond `tslib`.
|
|
@@ -18,6 +18,7 @@ Composable SVG charts for Angular, built on signals. A chart is put together in
|
|
|
18
18
|
- 📈 Line charts: straight or smooth lines, with a dot on each value
|
|
19
19
|
- 📊 Bar charts: one series, or several side by side
|
|
20
20
|
- ⛰️ Area charts: filled to zero, with a gradient if you like, and lines among them
|
|
21
|
+
- 🥞 Stacks: bars, areas or lines that share a `stackId` stand on each other, above zero and below
|
|
21
22
|
- ↕️ Values below zero, and gaps where a value is missing
|
|
22
23
|
- 🧭 Axes, a grid and a cursor at the datum under the pointer
|
|
23
24
|
- 💬 A tooltip with each series' value there, or a box of your own
|
|
@@ -29,7 +30,7 @@ Composable SVG charts for Angular, built on signals. A chart is put together in
|
|
|
29
30
|
|
|
30
31
|
## Not here (yet)
|
|
31
32
|
|
|
32
|
-
Pie charts,
|
|
33
|
+
Pie charts, stacks that add up to 100%, horizontal bars, a palette of series colors, animations and keyboard access to the data. The plan is in the [issues](https://github.com/dhutaryan/ngx-mira-charts/issues).
|
|
33
34
|
|
|
34
35
|
## Installation
|
|
35
36
|
|
|
@@ -115,10 +115,14 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImpo
|
|
|
115
115
|
/**
|
|
116
116
|
* A layer that plots the values under a key of the chart's data: a line,
|
|
117
117
|
* bars or an area. The chart fits its y axis to the values of all its series
|
|
118
|
-
* that aren't hidden, whatever kind they are
|
|
118
|
+
* that aren't hidden, whatever kind they are, stacked where they share a
|
|
119
|
+
* `stackId`.
|
|
119
120
|
*
|
|
120
121
|
* A series extends this class, and provides itself under it as well as under
|
|
121
|
-
* `MiraChartLayer`. It also implements `color`, the color it's drawn in.
|
|
122
|
+
* `MiraChartLayer`. It also implements `color`, the color it's drawn in. It
|
|
123
|
+
* draws its values where the chart puts them, its own entry of
|
|
124
|
+
* `MiraCartesianContext.spans`, so it stacks along with the rest when it's
|
|
125
|
+
* given a `stackId`.
|
|
122
126
|
*/
|
|
123
127
|
class MiraSeries extends MiraChartLayer {
|
|
124
128
|
/** The key of the values to plot. */
|
|
@@ -128,15 +132,30 @@ class MiraSeries extends MiraChartLayer {
|
|
|
128
132
|
* its `dataKey`.
|
|
129
133
|
*/
|
|
130
134
|
name = input(...(ngDevMode ? [undefined, { debugName: "name" }] : []));
|
|
135
|
+
/**
|
|
136
|
+
* The stack the series goes in. Series with the same `stackId` stack on
|
|
137
|
+
* each other, in the order they're written, the first nearest zero, and
|
|
138
|
+
* the y axis fits the stacks: values above zero stack up from zero, and
|
|
139
|
+
* values below zero down from it, so the two never overlap. A datum where a
|
|
140
|
+
* series in a stack has no value counts as zero for the series after it,
|
|
141
|
+
* while the series itself shows none there: no bar, a break in a line, an
|
|
142
|
+
* area thinned to nothing, no dot, and no row in the tooltip. A series with
|
|
143
|
+
* no value at all draws nothing. Unset, `''` or null, the series rests on
|
|
144
|
+
* zero.
|
|
145
|
+
*/
|
|
146
|
+
stackId = input(undefined, ...(ngDevMode ? [{ debugName: "stackId", transform: (value) => value || undefined }] : [{
|
|
147
|
+
transform: (value) => value || undefined,
|
|
148
|
+
}]));
|
|
131
149
|
/**
|
|
132
150
|
* Whether the series is hidden: the chart doesn't draw it, fits its y axis
|
|
133
151
|
* to the series that aren't, and leaves it out of the tooltip; the bars of
|
|
134
|
-
* the visible series share its room in the band
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
152
|
+
* the visible series share its room in the band, and the series after it
|
|
153
|
+
* in its stack close up. Shown by default. A click on its item in a
|
|
154
|
+
* toggleable legend toggles it. Write `[hidden]="true"`, not a bare
|
|
155
|
+
* `hidden`, to start hidden, or `[(hidden)]` to keep the state in your app.
|
|
156
|
+
* An app that sets the state after a click binds it both ways: a one-way
|
|
157
|
+
* binding takes effect only when its own value changes, so setting the
|
|
158
|
+
* value it last set does nothing.
|
|
140
159
|
*/
|
|
141
160
|
hidden = model(false, ...(ngDevMode ? [{ debugName: "hidden" }] : []));
|
|
142
161
|
/** What the series is called: its `name`, or its `dataKey` without one. */
|
|
@@ -149,11 +168,11 @@ class MiraSeries extends MiraChartLayer {
|
|
|
149
168
|
*/
|
|
150
169
|
legendIcon = 'circle';
|
|
151
170
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraSeries, deps: null, target: i0.ɵɵFactoryTarget.Directive });
|
|
152
|
-
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "20.3.35", type: MiraSeries, isStandalone: true, inputs: { dataKey: { classPropertyName: "dataKey", publicName: "dataKey", isSignal: true, isRequired: true, transformFunction: null }, name: { classPropertyName: "name", publicName: "name", isSignal: true, isRequired: false, transformFunction: null }, hidden: { classPropertyName: "hidden", publicName: "hidden", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { hidden: "hiddenChange" }, usesInheritance: true, ngImport: i0 });
|
|
171
|
+
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "20.3.35", type: MiraSeries, isStandalone: true, inputs: { dataKey: { classPropertyName: "dataKey", publicName: "dataKey", isSignal: true, isRequired: true, transformFunction: null }, name: { classPropertyName: "name", publicName: "name", isSignal: true, isRequired: false, transformFunction: null }, stackId: { classPropertyName: "stackId", publicName: "stackId", isSignal: true, isRequired: false, transformFunction: null }, hidden: { classPropertyName: "hidden", publicName: "hidden", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { hidden: "hiddenChange" }, usesInheritance: true, ngImport: i0 });
|
|
153
172
|
}
|
|
154
173
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraSeries, decorators: [{
|
|
155
174
|
type: Directive
|
|
156
|
-
}], propDecorators: { dataKey: [{ type: i0.Input, args: [{ isSignal: true, alias: "dataKey", required: true }] }], name: [{ type: i0.Input, args: [{ isSignal: true, alias: "name", required: false }] }], hidden: [{ type: i0.Input, args: [{ isSignal: true, alias: "hidden", required: false }] }, { type: i0.Output, args: ["hiddenChange"] }] } });
|
|
175
|
+
}], propDecorators: { dataKey: [{ type: i0.Input, args: [{ isSignal: true, alias: "dataKey", required: true }] }], name: [{ type: i0.Input, args: [{ isSignal: true, alias: "name", required: false }] }], stackId: [{ type: i0.Input, args: [{ isSignal: true, alias: "stackId", required: false }] }], hidden: [{ type: i0.Input, args: [{ isSignal: true, alias: "hidden", required: false }] }, { type: i0.Output, args: ["hiddenChange"] }] } });
|
|
157
176
|
|
|
158
177
|
/**
|
|
159
178
|
* Styles every chart takes: the colors its layers draw with, for a light page
|
|
@@ -198,26 +217,6 @@ function numberAt(datum, key) {
|
|
|
198
217
|
const value = valueAt(datum, key);
|
|
199
218
|
return typeof value === 'number' && Number.isFinite(value) ? value : null;
|
|
200
219
|
}
|
|
201
|
-
/**
|
|
202
|
-
* The smallest and the largest number under the keys, across the data, or
|
|
203
|
-
* null when there's no number at all.
|
|
204
|
-
*
|
|
205
|
-
* @internal
|
|
206
|
-
*/
|
|
207
|
-
function extentOf(data, keys) {
|
|
208
|
-
let min = Infinity;
|
|
209
|
-
let max = -Infinity;
|
|
210
|
-
for (const datum of data) {
|
|
211
|
-
for (const key of keys) {
|
|
212
|
-
const value = numberAt(datum, key);
|
|
213
|
-
if (value === null)
|
|
214
|
-
continue;
|
|
215
|
-
min = Math.min(min, value);
|
|
216
|
-
max = Math.max(max, value);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return min <= max ? [min, max] : null;
|
|
220
|
-
}
|
|
221
220
|
|
|
222
221
|
// Where a raw step moves up to the next round one: 1, 2, 5 or 10 times a
|
|
223
222
|
// power of ten, whichever is closest. The thresholds of d3-array's ticks().
|
|
@@ -354,6 +353,87 @@ function ticksBetween([min, max], increment) {
|
|
|
354
353
|
return ticks;
|
|
355
354
|
}
|
|
356
355
|
|
|
356
|
+
// What a stack gives is rounded to this many digits: past them, sums of
|
|
357
|
+
// decimals carry the noise of floating point. 0.2 + 0.4 is
|
|
358
|
+
// 0.6000000000000001, and the y axis would round that out to 0.7. The sums
|
|
359
|
+
// themselves run unrounded, so the rounding doesn't build up: rounded on the
|
|
360
|
+
// way, three thirds would come to 0.999999999999.
|
|
361
|
+
const DIGITS = 12;
|
|
362
|
+
/**
|
|
363
|
+
* Where the values of each series sit along y: a span per datum, for each
|
|
364
|
+
* series in the order they come. What d3-shape's `stack` does, with the
|
|
365
|
+
* values split by sign, as its `stackOffsetDiverging` splits them.
|
|
366
|
+
*
|
|
367
|
+
* - A series without a `stackId` rests on zero: it runs from zero to each
|
|
368
|
+
* value, and is null where it has none, so it breaks there.
|
|
369
|
+
* - Series with the same `stackId` stack, in the order they come. A value
|
|
370
|
+
* above zero rests on the end of the last value above zero before it, and
|
|
371
|
+
* a value below zero on the end of the last one below zero, so the two
|
|
372
|
+
* sides never overlap.
|
|
373
|
+
* - A zero, and a datum where a series in a stack has no value, rest on the
|
|
374
|
+
* side the series goes: above zero for a series with a value above it,
|
|
375
|
+
* below for one with values below zero only, and for a series of zeros, on
|
|
376
|
+
* its stack's side, above zero if any series of the stack goes there. A
|
|
377
|
+
* datum with no value has no height there, so the values after it rest
|
|
378
|
+
* where they would on a zero. A series in a stack with no value at all is
|
|
379
|
+
* null throughout, as it would be alone.
|
|
380
|
+
* - The starts and ends in a stack are rounded to 12 significant digits,
|
|
381
|
+
* past which sums of decimals carry the noise of floating point.
|
|
382
|
+
*
|
|
383
|
+
* @internal
|
|
384
|
+
*/
|
|
385
|
+
function stackSpans(data, series) {
|
|
386
|
+
const valuesOf = series.map(({ dataKey }) => data.map((datum) => numberAt(datum, dataKey)));
|
|
387
|
+
// The stacks with a value above zero: a series of zeros, with no side of
|
|
388
|
+
// its own, rests on that side of its stack.
|
|
389
|
+
const rising = new Set();
|
|
390
|
+
series.forEach(({ stackId }, rank) => {
|
|
391
|
+
if (stackId !== undefined && valuesOf[rank].some(isAboveZero)) {
|
|
392
|
+
rising.add(stackId);
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
// How far each stack reaches at each datum so far, above zero and below.
|
|
396
|
+
const stacks = new Map();
|
|
397
|
+
return series.map(({ stackId }, rank) => {
|
|
398
|
+
const values = valuesOf[rank];
|
|
399
|
+
if (stackId === undefined) {
|
|
400
|
+
return values.map((value) => value === null ? null : { value, start: 0, end: value });
|
|
401
|
+
}
|
|
402
|
+
// With no value at all, it has nothing to show: rather than an area that
|
|
403
|
+
// lies along the series before it, as a dataKey with a typo would give,
|
|
404
|
+
// it breaks throughout, as it would alone.
|
|
405
|
+
if (values.every((value) => value === null))
|
|
406
|
+
return values.map(() => null);
|
|
407
|
+
const reached = stacks.get(stackId) ?? {
|
|
408
|
+
up: new Array(data.length).fill(0),
|
|
409
|
+
down: new Array(data.length).fill(0),
|
|
410
|
+
};
|
|
411
|
+
stacks.set(stackId, reached);
|
|
412
|
+
// The side the series goes, where its zeros and missing values rest:
|
|
413
|
+
// above zero if it has a value there, below if it has values only below,
|
|
414
|
+
// and with zeros only, its stack's side.
|
|
415
|
+
const rises = values.some(isAboveZero) ||
|
|
416
|
+
(!values.some(isBelowZero) && rising.has(stackId));
|
|
417
|
+
return values.map((value, index) => {
|
|
418
|
+
const up = value === null || value === 0 ? rises : value > 0;
|
|
419
|
+
const side = up ? reached.up : reached.down;
|
|
420
|
+
const start = side[index];
|
|
421
|
+
const end = start + (value ?? 0);
|
|
422
|
+
side[index] = end;
|
|
423
|
+
return { value, start: round(start), end: round(end) };
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
function isAboveZero(value) {
|
|
428
|
+
return value !== null && value > 0;
|
|
429
|
+
}
|
|
430
|
+
function isBelowZero(value) {
|
|
431
|
+
return value !== null && value < 0;
|
|
432
|
+
}
|
|
433
|
+
function round(value) {
|
|
434
|
+
return Number(value.toPrecision(DIGITS));
|
|
435
|
+
}
|
|
436
|
+
|
|
357
437
|
/**
|
|
358
438
|
* The category axis under the plot: a tick per datum, labelled with the
|
|
359
439
|
* datum's value under `dataKey`. Its line runs at zero, where values rise and
|
|
@@ -602,10 +682,10 @@ const FALLBACK_SIZE = { width: 600, height: 300 };
|
|
|
602
682
|
/**
|
|
603
683
|
* What every chart with an x and a y axis has, whatever its series: data, a
|
|
604
684
|
* size, a margin, the plot area they leave, a y scale that fits the values of
|
|
605
|
-
* its visible series,
|
|
606
|
-
* under it. Nothing about a kind of
|
|
607
|
-
*
|
|
608
|
-
* datum.
|
|
685
|
+
* its visible series, stacked where they share a `stackId`, a label for each
|
|
686
|
+
* datum, and the pointer and the datum under it. Nothing about a kind of
|
|
687
|
+
* chart: a line chart and an area chart extend it with a point per datum
|
|
688
|
+
* along x, and a bar chart with a band per datum.
|
|
609
689
|
*
|
|
610
690
|
* Parts of your own, a layer (`MiraChartLayer`) that draws in the svg or a
|
|
611
691
|
* component that shows something in HTML over it, inject
|
|
@@ -728,6 +808,16 @@ class MiraCartesianChart {
|
|
|
728
808
|
// The y axis fits their values, the bars share their bands, and the tooltip
|
|
729
809
|
// shows them.
|
|
730
810
|
visibleSeries = computed(() => this.series().filter((series) => !series.hidden()), ...(ngDevMode ? [{ debugName: "visibleSeries" }] : []));
|
|
811
|
+
// Worked out once for every visible series, stacked or not: the y axis
|
|
812
|
+
// fits them, and each series draws its own.
|
|
813
|
+
spans = computed(() => {
|
|
814
|
+
const series = this.visibleSeries();
|
|
815
|
+
const spans = stackSpans(this.data(), series.map((item) => ({
|
|
816
|
+
dataKey: item.dataKey(),
|
|
817
|
+
stackId: item.stackId(),
|
|
818
|
+
})));
|
|
819
|
+
return new Map(series.map((item, index) => [item, spans[index]]));
|
|
820
|
+
}, ...(ngDevMode ? [{ debugName: "spans" }] : []));
|
|
731
821
|
/** The chart's layers, in the order they're written. */
|
|
732
822
|
layers = contentChildren(MiraChartLayer, ...(ngDevMode ? [{ debugName: "layers" }] : []));
|
|
733
823
|
/** What the chart draws: its layers, less the hidden series. */
|
|
@@ -747,12 +837,21 @@ class MiraCartesianChart {
|
|
|
747
837
|
#measured = signal(null, ...(ngDevMode ? [{ debugName: "#measured", equal: sameSize }] : [{ equal: sameSize }]));
|
|
748
838
|
// The pointer in the svg's own px, while it's over the svg.
|
|
749
839
|
#pointer = signal(null, ...(ngDevMode ? [{ debugName: "#pointer" }] : []));
|
|
750
|
-
// The
|
|
840
|
+
// The ends of the visible series' spans, and zero, rounded out to round
|
|
841
|
+
// ticks. Their starts can't reach further: each is zero, or another end.
|
|
751
842
|
#yNice = computed(() => {
|
|
752
|
-
|
|
753
|
-
|
|
843
|
+
let min = 0;
|
|
844
|
+
let max = 0;
|
|
845
|
+
for (const spans of this.spans().values()) {
|
|
846
|
+
for (const span of spans) {
|
|
847
|
+
if (span === null)
|
|
848
|
+
continue;
|
|
849
|
+
min = Math.min(min, span.end);
|
|
850
|
+
max = Math.max(max, span.end);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
754
853
|
const count = this.yAxis()?.tickCount() ?? DEFAULT_TICK_COUNT;
|
|
755
|
-
return linearTicks([
|
|
854
|
+
return linearTicks([min, max], count);
|
|
756
855
|
}, ...(ngDevMode ? [{ debugName: "#yNice" }] : []));
|
|
757
856
|
/** @internal */
|
|
758
857
|
constructor() {
|
|
@@ -1698,22 +1797,22 @@ function linePath(points, curve) {
|
|
|
1698
1797
|
.join('');
|
|
1699
1798
|
}
|
|
1700
1799
|
/**
|
|
1701
|
-
* The svg path of the area between a line through the points and
|
|
1702
|
-
*
|
|
1703
|
-
*
|
|
1704
|
-
*
|
|
1705
|
-
*
|
|
1800
|
+
* The svg path of the area between a line through the points and their
|
|
1801
|
+
* bases: for each stretch of points between the nulls, the line, as
|
|
1802
|
+
* `linePath` draws it, then on to the base of its last point, back through
|
|
1803
|
+
* the bases on the same curve, and closed. Where a point is null, the area
|
|
1804
|
+
* breaks, as the line does. What d3-shape's `area` draws, with a `y0` per
|
|
1805
|
+
* point.
|
|
1706
1806
|
*
|
|
1707
1807
|
* @internal
|
|
1708
1808
|
*/
|
|
1709
|
-
function areaPath(points,
|
|
1809
|
+
function areaPath(points, curve) {
|
|
1710
1810
|
return runsOf(points)
|
|
1711
1811
|
.map((run) => {
|
|
1712
|
-
const
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
`L${xy({ x: first.x, y: base })}Z`);
|
|
1812
|
+
const bases = run.map(({ x, base }) => ({ x, y: base })).reverse();
|
|
1813
|
+
// The bases go on from the end of the line, rather than start a path
|
|
1814
|
+
// of their own: their M becomes an L.
|
|
1815
|
+
return `${lineThrough(run, curve)}L${lineThrough(bases, curve).slice(1)}Z`;
|
|
1717
1816
|
})
|
|
1718
1817
|
.join('');
|
|
1719
1818
|
}
|
|
@@ -1794,7 +1893,10 @@ function xy(point) {
|
|
|
1794
1893
|
* A line through the values under `dataKey`, a point per datum, with a dot on
|
|
1795
1894
|
* each value and a bigger one, in a halo, on the value under the pointer.
|
|
1796
1895
|
* Where a datum has no number under the key, the line breaks; a value alone
|
|
1797
|
-
* between two gaps shows only as its dot.
|
|
1896
|
+
* between two gaps shows only as its dot. With a `stackId`, it's drawn on the
|
|
1897
|
+
* series before it in the stack, on its side of zero: each point at its value
|
|
1898
|
+
* added to theirs. Where it has no value, it still breaks, and counts as zero
|
|
1899
|
+
* for the series after it.
|
|
1798
1900
|
*
|
|
1799
1901
|
* @example
|
|
1800
1902
|
* ```html
|
|
@@ -1829,15 +1931,15 @@ class MiraLine extends MiraSeries {
|
|
|
1829
1931
|
return this.#points()[index] ?? null;
|
|
1830
1932
|
}, ...(ngDevMode ? [{ debugName: "activePoint" }] : []));
|
|
1831
1933
|
#chart = inject(MiraCartesianContext);
|
|
1832
|
-
// A point per datum, or null where the datum has
|
|
1934
|
+
// A point per datum, at the end of its span, or null where the datum has
|
|
1935
|
+
// no value to plot: a line breaks there, in a stack too.
|
|
1833
1936
|
#points = computed(() => {
|
|
1834
|
-
const
|
|
1937
|
+
const spans = this.#chart.spans().get(this) ?? [];
|
|
1835
1938
|
const xs = this.#chart.xPositions();
|
|
1836
1939
|
const y = this.#chart.yScale();
|
|
1837
|
-
return
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
});
|
|
1940
|
+
return spans.map((span, index) => span === null || span.value === null
|
|
1941
|
+
? null
|
|
1942
|
+
: { x: xs[index], y: y(span.end) });
|
|
1841
1943
|
}, ...(ngDevMode ? [{ debugName: "#points" }] : []));
|
|
1842
1944
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraLine, deps: null, target: i0.ɵɵFactoryTarget.Component });
|
|
1843
1945
|
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.35", type: MiraLine, isStandalone: true, selector: "mira-line", inputs: { stroke: { classPropertyName: "stroke", publicName: "stroke", isSignal: true, isRequired: false, transformFunction: null }, curve: { classPropertyName: "curve", publicName: "curve", isSignal: true, isRequired: false, transformFunction: null }, dot: { classPropertyName: "dot", publicName: "dot", isSignal: true, isRequired: false, transformFunction: null }, activeDot: { classPropertyName: "activeDot", publicName: "activeDot", isSignal: true, isRequired: false, transformFunction: null } }, providers: [
|
|
@@ -1982,9 +2084,10 @@ const BAR_GAP = 4;
|
|
|
1982
2084
|
* the datum's value under `dataKey`, up for a value above zero and down for
|
|
1983
2085
|
* one below. Write one per key: the bars of several share each band, side by
|
|
1984
2086
|
* side in the order they're written, and a hidden one leaves its room to the
|
|
1985
|
-
* others.
|
|
1986
|
-
*
|
|
1987
|
-
*
|
|
2087
|
+
* others. Bars with the same `stackId` share one place in the band instead:
|
|
2088
|
+
* each rests on the end of the last bar before it on its side of zero. A
|
|
2089
|
+
* datum with no number under the key has no bar. It goes in a chart with a
|
|
2090
|
+
* band per datum, such as `<mira-bar-chart>`.
|
|
1988
2091
|
*
|
|
1989
2092
|
* @example
|
|
1990
2093
|
* ```html
|
|
@@ -2000,26 +2103,25 @@ class MiraBar extends MiraSeries {
|
|
|
2000
2103
|
/** A square marks the bars in the legend. */
|
|
2001
2104
|
legendIcon = 'square';
|
|
2002
2105
|
// A rect per datum with a value: at the bar's place in the datum's band,
|
|
2003
|
-
// and from
|
|
2106
|
+
// and from where the value rests to where it ends, whichever way that goes.
|
|
2004
2107
|
bars = computed(() => {
|
|
2005
|
-
const
|
|
2108
|
+
const spans = this.#chart.spans().get(this) ?? [];
|
|
2006
2109
|
const xs = this.#chart.xPositions();
|
|
2007
2110
|
const y = this.#chart.yScale();
|
|
2008
2111
|
const { offset, width } = this.#slot();
|
|
2009
2112
|
// The bar's left edge, from the middle of its band.
|
|
2010
2113
|
const fromMiddle = offset - this.#bandwidth() / 2;
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
const value = numberAt(datum, key);
|
|
2014
|
-
if (value === null)
|
|
2114
|
+
return spans.flatMap((span, index) => {
|
|
2115
|
+
if (span === null || span.value === null)
|
|
2015
2116
|
return [];
|
|
2016
|
-
const
|
|
2117
|
+
const start = y(span.start);
|
|
2118
|
+
const end = y(span.end);
|
|
2017
2119
|
return {
|
|
2018
2120
|
index,
|
|
2019
2121
|
x: xs[index] + fromMiddle,
|
|
2020
|
-
y: Math.min(
|
|
2122
|
+
y: Math.min(start, end),
|
|
2021
2123
|
width,
|
|
2022
|
-
height: Math.abs(end -
|
|
2124
|
+
height: Math.abs(end - start),
|
|
2023
2125
|
};
|
|
2024
2126
|
});
|
|
2025
2127
|
}, ...(ngDevMode ? [{ debugName: "bars" }] : []));
|
|
@@ -2027,11 +2129,16 @@ class MiraBar extends MiraSeries {
|
|
|
2027
2129
|
// further out.
|
|
2028
2130
|
#chart = inject(MiraCartesianContext);
|
|
2029
2131
|
#bandwidth = bandwidthOf(this.#chart);
|
|
2030
|
-
// The bar's place in each band
|
|
2031
|
-
// side in the order
|
|
2132
|
+
// The bar's place in each band. A stack takes one place, and a bar without
|
|
2133
|
+
// a stack one of its own, side by side in the order their first bars are
|
|
2134
|
+
// written. Hidden bars count toward that order, so a place stays where it
|
|
2135
|
+
// is as bars are hidden; but only the places with a bar shown share the
|
|
2136
|
+
// band.
|
|
2032
2137
|
#slot = computed(() => {
|
|
2033
|
-
const
|
|
2034
|
-
|
|
2138
|
+
const places = new Set(this.#chart.series().filter(isBar).map(placeOf));
|
|
2139
|
+
const shown = new Set(this.#chart.visibleSeries().filter(isBar).map(placeOf));
|
|
2140
|
+
const sharing = [...places].filter((place) => shown.has(place));
|
|
2141
|
+
return slotOf(sharing.indexOf(placeOf(this)), sharing.length, this.#bandwidth());
|
|
2035
2142
|
}, ...(ngDevMode ? [{ debugName: "#slot" }] : []));
|
|
2036
2143
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraBar, deps: null, target: i0.ɵɵFactoryTarget.Component });
|
|
2037
2144
|
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.35", type: MiraBar, isStandalone: true, selector: "mira-bar", inputs: { fill: { classPropertyName: "fill", publicName: "fill", isSignal: true, isRequired: false, transformFunction: null } }, providers: [
|
|
@@ -2084,6 +2191,10 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImpo
|
|
|
2084
2191
|
function isBar(series) {
|
|
2085
2192
|
return series instanceof MiraBar;
|
|
2086
2193
|
}
|
|
2194
|
+
// What a bar's place in the band goes by: its stack, or the bar itself.
|
|
2195
|
+
function placeOf(bar) {
|
|
2196
|
+
return bar.stackId() ?? bar;
|
|
2197
|
+
}
|
|
2087
2198
|
// The width of the chart's bands. A chart of points has none, and a bar there
|
|
2088
2199
|
// has nowhere to go.
|
|
2089
2200
|
function bandwidthOf(chart) {
|
|
@@ -2166,6 +2277,16 @@ let gradients = 0;
|
|
|
2166
2277
|
* goes in an area chart, or in a line chart, along with lines: write it
|
|
2167
2278
|
* before a line it shouldn't cover.
|
|
2168
2279
|
*
|
|
2280
|
+
* With a `stackId`, the area is filled down to the area before it in the
|
|
2281
|
+
* stack, on its side of zero, rather than to zero, and where it has no value
|
|
2282
|
+
* it thins to nothing instead of breaking, so the areas after it stay on it.
|
|
2283
|
+
* Its edges both take its own curve: give the areas of a stack one curve, or
|
|
2284
|
+
* the edges where they meet part. A monotone curve bends each line by its own
|
|
2285
|
+
* neighbours, so where an area is zero beside values that turn, its line can
|
|
2286
|
+
* dip a little under the area before it; straight lines can't. Keep each area
|
|
2287
|
+
* of a stack on one side of zero: one that crosses it moves to the other
|
|
2288
|
+
* side's stack there, and leaves a hole between the two values.
|
|
2289
|
+
*
|
|
2169
2290
|
* @example
|
|
2170
2291
|
* ```html
|
|
2171
2292
|
* <mira-area dataKey="solar" stroke="#d97706" />
|
|
@@ -2209,11 +2330,11 @@ class MiraArea extends MiraSeries {
|
|
|
2209
2330
|
*/
|
|
2210
2331
|
activeDot = input(true, ...(ngDevMode ? [{ debugName: "activeDot", transform: booleanAttribute }] : [{ transform: booleanAttribute }]));
|
|
2211
2332
|
/**
|
|
2212
|
-
* Whether the area fades out toward zero: from its
|
|
2213
|
-
* zero, at `fillOpacity`, to nothing at zero, as
|
|
2214
|
-
* Off by default. It fades the
|
|
2215
|
-
* which has to be a color then:
|
|
2216
|
-
* it and leave this off.
|
|
2333
|
+
* Whether the area fades out toward zero: from where its line is farthest
|
|
2334
|
+
* from zero at a value of its own, at `fillOpacity`, to nothing at zero, as
|
|
2335
|
+
* fast on either side of it, in a stack too. Off by default. It fades the
|
|
2336
|
+
* area's color, its `fill` or its `stroke`, which has to be a color then:
|
|
2337
|
+
* for a gradient of your own, set `fill` to it and leave this off.
|
|
2217
2338
|
*/
|
|
2218
2339
|
gradient = input(false, ...(ngDevMode ? [{ debugName: "gradient", transform: booleanAttribute }] : [{ transform: booleanAttribute }]));
|
|
2219
2340
|
/**
|
|
@@ -2225,23 +2346,26 @@ class MiraArea extends MiraSeries {
|
|
|
2225
2346
|
legendIcon = 'square';
|
|
2226
2347
|
gradientId = `mira-area-gradient-${gradients++}`;
|
|
2227
2348
|
line = computed(() => linePath(this.#points(), this.curve()), ...(ngDevMode ? [{ debugName: "line" }] : []));
|
|
2228
|
-
area = computed(() => areaPath(this.#points(), this
|
|
2349
|
+
area = computed(() => areaPath(this.#points(), this.curve()), ...(ngDevMode ? [{ debugName: "area" }] : []));
|
|
2229
2350
|
fillColor = computed(() => this.fill() || this.stroke(), ...(ngDevMode ? [{ debugName: "fillColor" }] : []));
|
|
2230
2351
|
// What the area is painted with: its color, or its gradient.
|
|
2231
2352
|
paint = computed(() => this.gradient() ? `url(#${this.gradientId})` : this.fillColor(), ...(ngDevMode ? [{ debugName: "paint" }] : []));
|
|
2232
2353
|
opacity = computed(() => this.fillOpacity() ??
|
|
2233
2354
|
(this.gradient() ? DEFAULT_GRADIENT_OPACITY : DEFAULT_FILL_OPACITY), ...(ngDevMode ? [{ debugName: "opacity" }] : []));
|
|
2234
2355
|
// Where the gradient runs along y: from as far above zero as the area
|
|
2235
|
-
// reaches on either side of it, to as far below. Zero is in
|
|
2236
|
-
// the area fades as fast both ways.
|
|
2356
|
+
// reaches with its values on either side of it, to as far below. Zero is in
|
|
2357
|
+
// its middle, so the area fades as fast both ways. Where it thins to
|
|
2358
|
+
// nothing, in a stack, it reaches nowhere of its own.
|
|
2237
2359
|
fade = computed(() => {
|
|
2238
2360
|
const zero = this.#zero();
|
|
2239
|
-
const reach = this.#points().reduce((far, point) => point === null
|
|
2361
|
+
const reach = this.#points().reduce((far, point) => point === null || point.value === null
|
|
2362
|
+
? far
|
|
2363
|
+
: Math.max(far, Math.abs(point.y - zero)), 0);
|
|
2240
2364
|
return { y1: zero - reach, y2: zero + reach };
|
|
2241
2365
|
}, ...(ngDevMode ? [{ debugName: "fade" }] : []));
|
|
2242
2366
|
// A dot per value, each with its datum's index, so its element stays with
|
|
2243
2367
|
// its datum as the values around it come and go.
|
|
2244
|
-
dots = computed(() => this.#points().flatMap((point, index) => point === null ? [] : [{ ...point, index }]), ...(ngDevMode ? [{ debugName: "dots" }] : []));
|
|
2368
|
+
dots = computed(() => this.#points().flatMap((point, index) => point === null || point.value === null ? [] : [{ ...point, index }]), ...(ngDevMode ? [{ debugName: "dots" }] : []));
|
|
2245
2369
|
// The point under the pointer, if the area has a value there.
|
|
2246
2370
|
activePoint = computed(() => {
|
|
2247
2371
|
if (!this.activeDot())
|
|
@@ -2249,20 +2373,26 @@ class MiraArea extends MiraSeries {
|
|
|
2249
2373
|
const index = this.#chart.activeIndex();
|
|
2250
2374
|
if (index === null)
|
|
2251
2375
|
return null;
|
|
2252
|
-
|
|
2376
|
+
const point = this.#points()[index];
|
|
2377
|
+
return point && point.value !== null ? point : null;
|
|
2253
2378
|
}, ...(ngDevMode ? [{ debugName: "activePoint" }] : []));
|
|
2254
2379
|
#chart = inject(MiraCartesianContext);
|
|
2255
|
-
// A point per datum,
|
|
2380
|
+
// A point per datum, at the end of its span, over the start it rests on;
|
|
2381
|
+
// or null where the area breaks.
|
|
2256
2382
|
#points = computed(() => {
|
|
2257
|
-
const
|
|
2383
|
+
const spans = this.#chart.spans().get(this) ?? [];
|
|
2258
2384
|
const xs = this.#chart.xPositions();
|
|
2259
2385
|
const y = this.#chart.yScale();
|
|
2260
|
-
return
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2386
|
+
return spans.map((span, index) => span === null
|
|
2387
|
+
? null
|
|
2388
|
+
: {
|
|
2389
|
+
x: xs[index],
|
|
2390
|
+
y: y(span.end),
|
|
2391
|
+
base: y(span.start),
|
|
2392
|
+
value: span.value,
|
|
2393
|
+
});
|
|
2264
2394
|
}, ...(ngDevMode ? [{ debugName: "#points" }] : []));
|
|
2265
|
-
// The y of zero, which the
|
|
2395
|
+
// The y of zero, which the gradient fades to: the y axis always takes it in.
|
|
2266
2396
|
#zero = computed(() => this.#chart.yScale()(0), ...(ngDevMode ? [{ debugName: "#zero" }] : []));
|
|
2267
2397
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: MiraArea, deps: null, target: i0.ɵɵFactoryTarget.Component });
|
|
2268
2398
|
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.35", type: MiraArea, isStandalone: true, selector: "mira-area", inputs: { stroke: { classPropertyName: "stroke", publicName: "stroke", isSignal: true, isRequired: false, transformFunction: null }, fill: { classPropertyName: "fill", publicName: "fill", isSignal: true, isRequired: false, transformFunction: null }, fillOpacity: { classPropertyName: "fillOpacity", publicName: "fillOpacity", isSignal: true, isRequired: false, transformFunction: null }, curve: { classPropertyName: "curve", publicName: "curve", isSignal: true, isRequired: false, transformFunction: null }, dot: { classPropertyName: "dot", publicName: "dot", isSignal: true, isRequired: false, transformFunction: null }, activeDot: { classPropertyName: "activeDot", publicName: "activeDot", isSignal: true, isRequired: false, transformFunction: null }, gradient: { classPropertyName: "gradient", publicName: "gradient", isSignal: true, isRequired: false, transformFunction: null } }, providers: [
|