ml-time-graph 1.7.2 → 1.8.3
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/CHANGELOG.md +214 -8
- package/README.de.md +104 -11
- package/README.md +101 -11
- package/SKILLS.md +157 -4
- package/USAGE.md +326 -3
- package/dist/analyze/index.d.ts +54 -1
- package/dist/analyze/index.js +2 -2
- package/dist/index.d.ts +107 -7
- package/dist/index.js +27 -26
- package/dist/interaction/index.d.ts +1 -1
- package/dist/interaction/index.js +1 -1
- package/dist/internals.d.ts +104 -26
- package/dist/internals.js +15 -15
- package/dist/{scale-D6NtEaUB.d.ts → scale-CoTgJ27U.d.ts} +71 -12
- package/dist/{series_renderer-COVNvq2x.d.ts → slug-0lFk9pY7.d.ts} +18 -2
- package/package.json +17 -13
package/SKILLS.md
CHANGED
|
@@ -26,6 +26,38 @@ mount('#chart', {
|
|
|
26
26
|
});
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
### The data does not look like that
|
|
30
|
+
|
|
31
|
+
`{ time, value }` is what the chart wants, not what you have. Convert instead of rebuilding —
|
|
32
|
+
tuples, a REST payload in Unix seconds, a CSV export of strings, a nested document:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { toSeries } from 'ml-time-graph';
|
|
36
|
+
|
|
37
|
+
toSeries('Temperature', tuples) // [[1704067200000, 21.5], …]
|
|
38
|
+
toSeries('Temperature', rows, { time: 'ts', value: 'val', timeUnit: 's' })
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Missing readings (`null`, `undefined`, `NaN`, `''`) become gaps. A string that is not a number
|
|
42
|
+
throws and names the row — silently dropping it would turn a broken export into a plausible
|
|
43
|
+
chart.
|
|
44
|
+
|
|
45
|
+
### The same options in every chart
|
|
46
|
+
|
|
47
|
+
Dimensions, axes, legend, colours repeat across reports. Put them underneath instead of copying
|
|
48
|
+
them:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { mergeOptions, MLTimeGraph } from 'ml-time-graph';
|
|
52
|
+
|
|
53
|
+
new MLTimeGraph(mergeOptions(houseTheme, readings)); // the later layer wins
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Objects merge, everything else is replaced — arrays included, so a theme carrying one line
|
|
57
|
+
style does not turn one curve into two. `{ ...a, ...b }` is **not** the same: the spread is
|
|
58
|
+
shallow and an `axes` in `b` drops `a`'s grid and ticks. Server-side the same rule is
|
|
59
|
+
`graph-cli -with theme.json`, stackable.
|
|
60
|
+
|
|
29
61
|
## When to Use What
|
|
30
62
|
|
|
31
63
|
- `mount(target, options)`: The declarative browser shortcut when a DOM element or selector exists — measures container width, renders SVG, and injects markup in one call.
|
|
@@ -36,7 +68,7 @@ mount('#chart', {
|
|
|
36
68
|
|
|
37
69
|
## Package Entry Points
|
|
38
70
|
|
|
39
|
-
- `ml-time-graph`: Core chart rendering API — `mount`, `MLTimeGraph`, `SVGRenderer`, `GraphBuilder`, `attachTooltip`, types, and legend helpers (`resolveDash`, `drawMarker`).
|
|
71
|
+
- `ml-time-graph`: Core chart rendering API — `mount`, `MLTimeGraph`, `SVGRenderer`, `GraphBuilder`, `attachTooltip`, input adapters (`toSeries`, `toDataPoints`), options layering (`mergeOptions`), types, and legend helpers (`resolveDash`, `getLineStyle`, `drawMarker`).
|
|
40
72
|
- `ml-time-graph/analyze`: Pure-math statistics companion — `aggregateBySlot`, `downsample` (LTTB), `detectGaps`, `rollingMkt`, `rollingStdDev`, `computeLimitExcursions`, `StatsAggregator`.
|
|
41
73
|
- `ml-time-graph/interaction`: Headless interaction primitives — `Zoom`, `Minimap`, and coordinate-based interaction state management.
|
|
42
74
|
- `ml-time-graph/internals`: Low-level building blocks for custom renderers — `DrawCommand` model, base `Renderer`, layout engine, scales, and axis renderers.
|
|
@@ -98,6 +130,8 @@ mount('#chart', {
|
|
|
98
130
|
{ type: 'arrow', from: { time: t1, value: 20 }, to: { time: t2, value: 35 }, color: '#ef4444' },
|
|
99
131
|
{ type: 'label', at: { time: t2, value: 35 }, text: 'Pressure Spike', dy: -10 },
|
|
100
132
|
{ type: 'rect', from: { time: t1, value: 15 }, to: { time: t2, value: 25 }, fill: '#10b98122' },
|
|
133
|
+
{ type: 'line', from: { time: t1, value: 30 }, to: { time: t3, value: 30 }, dash: 'dashed' },
|
|
134
|
+
{ type: 'point', at: { time: t2, value: 35 }, shape: 'circle' },
|
|
101
135
|
],
|
|
102
136
|
// Horizontal status/phase bands below the plot (sharing the time axis):
|
|
103
137
|
annotationBands: [
|
|
@@ -113,8 +147,43 @@ mount('#chart', {
|
|
|
113
147
|
});
|
|
114
148
|
```
|
|
115
149
|
|
|
150
|
+
**The five annotation types and what each one takes:**
|
|
151
|
+
|
|
152
|
+
| `type` | Coordinates | Own fields |
|
|
153
|
+
| :--- | :--- | :--- |
|
|
154
|
+
| `line` | `from`, `to` | `color`, `width`, `dash` |
|
|
155
|
+
| `arrow` | `from`, `to` | `color`, `width`, `headSize` — the head sits on `to` |
|
|
156
|
+
| `rect` | `from`, `to` | `fill`, `hatch`, `stroke`, `opacity` |
|
|
157
|
+
| `point` | `at` | `shape` (`circle` \| `square` \| `cross`), `color`, `radius` |
|
|
158
|
+
| `label` | `at` | `text`, `dx`, `dy`, `anchor`, `rotate`, `color` |
|
|
159
|
+
|
|
160
|
+
A coordinate is `{ time, value, axis? }` — `axis` picks the y-scale (0 = left). An
|
|
161
|
+
annotation with an `id` is wrapped in `<g class="annotation annotation--{slug}">` and can
|
|
162
|
+
be targeted from CSS or toggled; without one its shapes are emitted bare. Both libraries
|
|
163
|
+
draw all five types identically — the Go port did not draw any of them before 1.8.0.
|
|
164
|
+
|
|
116
165
|
### Data Gaps
|
|
117
|
-
`value: null` marks missing data and breaks the line
|
|
166
|
+
`value: null` marks missing data and breaks the *line*. What is drawn over the span is
|
|
167
|
+
`display`, and the four values mean the same per series and chart-wide:
|
|
168
|
+
|
|
169
|
+
| `display` | draws |
|
|
170
|
+
| --- | --- |
|
|
171
|
+
| `filled` | the area in the fill colour, no edges |
|
|
172
|
+
| `dashed_border` | the area **plus** two vertical dashed lines at its borders (default) |
|
|
173
|
+
| `empty` | nothing — the line breaks, that is all |
|
|
174
|
+
| `bridge_line` | a dotted line between the readings around the gap (per series only) |
|
|
175
|
+
|
|
176
|
+
A **label** is separate from all of this and comes from `gaps.regions[].label` — a region knows
|
|
177
|
+
which gap it describes, a series style would have to give all of them the same text:
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
gaps: {
|
|
181
|
+
style: { display: 'filled', fill: '#6b7280', opacity: 0.15, label: { baseline: 'middle' } },
|
|
182
|
+
regions: [{ startTime: 1704070800000, endTime: 1704074400000, label: 'Sensor offline' }],
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Connect with a bridge or render gap spans:
|
|
118
187
|
```ts
|
|
119
188
|
series: [{
|
|
120
189
|
name: 'Sensor',
|
|
@@ -140,12 +209,19 @@ markers: [
|
|
|
140
209
|
```
|
|
141
210
|
|
|
142
211
|
### Axes & Margins
|
|
143
|
-
The layout engine cannot measure rendered text. Always allocate sufficient margins for labels
|
|
212
|
+
The layout engine cannot measure rendered text. Always allocate sufficient margins for labels.
|
|
213
|
+
And because the computed domain ends exactly on the smallest and largest reading, a thick line
|
|
214
|
+
or a marker at the extreme loses half of itself to the clip — `padding` adds room **in pixels**
|
|
215
|
+
(the cause is in pixels: `line.width`, `markers.size`), without giving up the automatic domain:
|
|
144
216
|
```ts
|
|
145
217
|
margin: { top: 20, right: 80, bottom: 40, left: 60 },
|
|
146
218
|
axes: {
|
|
147
219
|
x: { label: 'Time', format: (d: Date) => d.toISOString().slice(11, 16) },
|
|
148
|
-
left: {
|
|
220
|
+
left: {
|
|
221
|
+
label: 'Temperature (°C)',
|
|
222
|
+
format: (v: number) => `${v.toFixed(1)} °C`,
|
|
223
|
+
padding: 8, // or { top: 24, bottom: 4 }
|
|
224
|
+
},
|
|
149
225
|
right: { label: 'Humidity (%)', format: (v: number) => `${v.toFixed(0)} %` },
|
|
150
226
|
}
|
|
151
227
|
```
|
|
@@ -174,11 +250,88 @@ const { content } = new SVGRenderer().render(chart.renderCommands());
|
|
|
174
250
|
fs.writeFileSync('chart.svg', content);
|
|
175
251
|
```
|
|
176
252
|
|
|
253
|
+
### Styling the SVG from CSS
|
|
254
|
+
Every group the renderer emits carries a class, and the Go port emits the same ones:
|
|
255
|
+
|
|
256
|
+
`value-axis left` / `value-axis right`, `time-axis`, `axis-labels`, `chart-grid`,
|
|
257
|
+
`chart-legend`, `series series--<slug>`, `thresholds` / `threshold threshold--<slug>`,
|
|
258
|
+
`threshold-labels` / `threshold-label threshold-label--<slug>`, `markers`, `gaps`,
|
|
259
|
+
`highlights`, `annotation-band`, `annotation-band-axis`.
|
|
260
|
+
|
|
261
|
+
On the `<path>` elements inside a series: `series-line` and `series-fill`.
|
|
262
|
+
|
|
263
|
+
The `<slug>` is `config.id ?? slugify(config.name)` — an explicit `id` verbatim, a name
|
|
264
|
+
lowercased with non-alphanumeric runs collapsed to `-`. Export `slugify` to build the
|
|
265
|
+
selector for a name you hold at runtime:
|
|
266
|
+
```ts
|
|
267
|
+
import { slugify } from 'ml-time-graph';
|
|
268
|
+
svgRoot.querySelector(`.series--${slugify(series.name)}`)?.classList.toggle('is-hidden');
|
|
269
|
+
```
|
|
270
|
+
The group also carries the same string as an `id` (`#series-temperatur`). Elements
|
|
271
|
+
*inside* a series group use a different, stable key instead: `s-<crc32 of the name>`
|
|
272
|
+
(`-line-<n>`, `-fill-r<region>-<piece>`).
|
|
273
|
+
|
|
274
|
+
**Prefer the classes.** An `id` is unique per *document*, not per chart: two charts on one
|
|
275
|
+
page produce the same key, and `getElementById` returns the first one. Scope through the
|
|
276
|
+
container instead — `#chart-2 .series--temperature .series-line`.
|
|
277
|
+
|
|
278
|
+
Full table and the provenance comment (`<!-- MLTimeGraph x.y.z -->`, the first child of
|
|
279
|
+
every `<svg>`): [USAGE.md — CSS classes in the SVG](USAGE.md#css-classes-in-the-svg).
|
|
280
|
+
|
|
281
|
+
### Aggregated Series (`showAs`)
|
|
282
|
+
Points of the shape `{ time, min, max, avg, count }` — one slot per bucket, as
|
|
283
|
+
`aggregateBySlot()` produces:
|
|
284
|
+
|
|
285
|
+
- `showAs: 'minmaxavg'` — three lines (min / max / avg) plus optional `fillToMax` /
|
|
286
|
+
`fillToMin` envelopes. **Geometrically identical to three ordinary series**; what it adds
|
|
287
|
+
is one legend entry instead of three, one series key for all three paths, and
|
|
288
|
+
`avgDashed` on by default (`tests/series/minmaxavg_drei_serien.test.ts` proves the
|
|
289
|
+
equality). Use three plain series when they should behave as three.
|
|
290
|
+
- `showAs: 'band'` — one filled rectangle per slot, min→max. No three-series equivalent.
|
|
291
|
+
|
|
292
|
+
The envelope fills are reachable from plain series too: a fill bound takes
|
|
293
|
+
`{ series: '<id or name>' }`, which spans the area between two curves. The referenced
|
|
294
|
+
curve is interpolated onto this series' timestamps, and the area **breaks where that
|
|
295
|
+
curve has a gap** — same rule as a line.
|
|
296
|
+
Its `avgLine` and `countOpacity` options are TypeScript-only so far.
|
|
297
|
+
|
|
298
|
+
### Statistic Overlays
|
|
299
|
+
`series.overlays` computes from the series' own data at render time:
|
|
300
|
+
`movingAverage` (`type: 'simple' | 'exponential'`), `movingMkt` (USP <1079.2>),
|
|
301
|
+
`stdDevBand`, `limits`. **Both libraries render all of them** — the Go port since 1.8.0.
|
|
302
|
+
|
|
303
|
+
Every window is in **milliseconds**, including the exponential one
|
|
304
|
+
(`alpha = 1 - exp(-Δt / window)`, not the count-based `2/(N+1)`).
|
|
305
|
+
|
|
306
|
+
The same numbers are available without drawing anything — `rollingMean`, `rollingEma`,
|
|
307
|
+
`rollingStdDev`, `rollingMkt` from `ml-time-graph/analyze`, `analyze.RollingMean` … from
|
|
308
|
+
`pkg/analyze` in Go. An overlay is a convenience, not a second implementation; passing the
|
|
309
|
+
pre-computed series as an ordinary one gives the identical picture, which
|
|
310
|
+
`tests/series/overlay_extern.test.ts` proves rather than claims.
|
|
311
|
+
|
|
312
|
+
### Time Zone
|
|
313
|
+
`locale` decides *how* a time is written, `timeZone` **which** time:
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
new MLTimeGraph({ locale: 'de-DE', timeZone: 'Europe/Berlin', series });
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Unset means the machine's zone — fine on one machine, a trap across two: a browser sits in
|
|
320
|
+
the reader's zone, a server in UTC, and the same JSON then labels the same reading with
|
|
321
|
+
different times. Set it whenever a chart is produced in one place and read in another. Only
|
|
322
|
+
the labels move; tick positions are epoch arithmetic. Same option in Go
|
|
323
|
+
(`Options.TimeZone`, JSON `timeZone`), which embeds the zone database so it works in a
|
|
324
|
+
scratch image.
|
|
325
|
+
|
|
177
326
|
## Traps (Read Before Coding)
|
|
178
327
|
|
|
179
328
|
- **Timestamps are numbers, not Date objects:** `time` is always milliseconds since Unix epoch (`number`). Passing a `Date` object fails typechecking or evaluates to `NaN`.
|
|
180
329
|
- **Labels are not measured (fixed margins):** The renderer has no DOM and cannot compute text bounding boxes. Margins default to `{ top: 20, right: 20, bottom: 40, left: 60 }`. If you add a right Y-axis or formatted units (e.g. `120.5 °C`), `right: 20` will clip the labels against the SVG edge. A clipped number is a wrong number, not an incomplete one (`120` clipped to `20`). Always set `margin.right` explicitly (e.g. 70–90 px) when using right axes.
|
|
181
330
|
- **`resolveDash()` vs `getLineStyle()`:** `dashed` and `dotted` predate the scalable dash table and keep fixed SVG patterns (`"4,4"` and `"2,4"`) in the renderer for backwards compatibility. `getLineStyle()` returns the scalable table (`"6, 4"` for width 2). Anyone rendering custom legend swatches must call `resolveDash(variant, width)` so the swatch matches what is actually drawn.
|
|
331
|
+
- **Markers do not inherit `line.width`:** `style.markers` carries its own pen — `strokeWidth`, `stroke`, `fill`. That is usually what you want (a 10 px line with 10 px marker outlines is a blob), but it means a marker stays hairline-thin when the line gets thick, and nothing tells you. Set `markers.strokeWidth` explicitly whenever `line.width` leaves the default.
|
|
182
332
|
- **Marker suppression at threshold:** `style.markers.threshold` (and default `theme.pointThreshold = 100`) hides point markers when series sample count exceeds it to prevent visual crowding. Crucially, `chart.legendItems()` also omits the `marker` property when points are suppressed, because a symbol in the legend missing from the curve states something untrue.
|
|
183
333
|
- **Built-in legend is static SVG:** Built-in legends (`inside-right`, etc.) are non-interactive SVG elements. For interactive series toggling or HTML layout below the chart, use `legend: { position: 'separate' }` and render HTML buttons from `chart.legendItems()` using `drawMarker()` and `SVGRenderer` or `resolveDash()`.
|
|
334
|
+
- **An annotation's `dash` takes a variant, not a pattern:** the type is `'dashed' | 'dotted'`. A raw SVG pattern like `'5,5'` is not rejected — it draws a solid line, in both libraries.
|
|
335
|
+
- **Some options are accepted and do nothing:** `axes.<axis>.ticks.minor`, `axes.<axis>.grid.minor`, `fill.hatchColor` / `fill.hatchWidth`, `series.enumMap` and `series.sensorType` are in the types but no renderer reads them — in either library. Nothing warns you; the picture just stays as it was.
|
|
336
|
+
- **CSS classes are not namespaced:** the chart uses plain names like `.series` and `.time-axis`. Inline the SVG into a page whose stylesheet has a `.series` rule and that rule applies to the chart. Scope your own selectors, and suspect the host page when an embedded chart looks wrong but the standalone file does not.
|
|
184
337
|
- **Hatch lines adapt to fill colour:** Hatch patterns inherit the opaque colour of the area they belong to (`opaqueColor(fill)`), while the backdrop stays translucent. An earlier default drew fixed blue lines (`#4D88FF`), which falsely signaled an extra state on incident charts where colour is semantic.
|
package/USAGE.md
CHANGED
|
@@ -113,6 +113,25 @@ underneath, add `markers` for points, set `style.line: false` for points only.
|
|
|
113
113
|
},
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
// Markers carry their own pen and their own colours — they do NOT inherit the
|
|
117
|
+
// line's stroke width. The line can be thick and the marker outlined thinly,
|
|
118
|
+
// or the other way round.
|
|
119
|
+
{
|
|
120
|
+
name: 'CPU',
|
|
121
|
+
data,
|
|
122
|
+
style: {
|
|
123
|
+
line: { color: '#4285f4', width: 6 },
|
|
124
|
+
markers: {
|
|
125
|
+
type: 'circle', // 13 shapes, see PointStyleType
|
|
126
|
+
size: 5, // radius (or half-width) in px
|
|
127
|
+
stroke: '#1e3a8a', // outline colour; defaults to the line/zone colour
|
|
128
|
+
fill: '#ffffff', // fill colour; defaults to the line/zone colour
|
|
129
|
+
strokeWidth: 1.5, // the marker's own pen, independent of line.width
|
|
130
|
+
threshold: 500, // only draw markers while the series has ≤ n points
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
|
|
116
135
|
// Step + smoothed overlay (multi-line array — same data, both rendered)
|
|
117
136
|
// Each entry keeps its own shape: the first is the staircase, the second a
|
|
118
137
|
// smoothed line through the readings. An entry without a `shape` of its own is
|
|
@@ -144,6 +163,73 @@ underneath, add `markers` for points, set `style.line: false` for points only.
|
|
|
144
163
|
A point with `value: null` (or a slot with `min/max/avg: null`) marks a **gap** — the
|
|
145
164
|
line breaks there. Multiple y-axes are supported via `yAxisIndex: 0 | 1 | 2 | ...` on the series.
|
|
146
165
|
|
|
166
|
+
### Aggregated series: `showAs`
|
|
167
|
+
|
|
168
|
+
A series whose points are `{ time, min, max, avg, count }` instead of `{ time, value }` is
|
|
169
|
+
*aggregated* — one slot per time bucket, as `aggregateBySlot()` produces. `showAs` decides
|
|
170
|
+
how a slot is drawn, and the two forms are genuinely different pictures:
|
|
171
|
+
|
|
172
|
+
| `showAs` | What is drawn | Per slot |
|
|
173
|
+
| :--- | :--- | :--- |
|
|
174
|
+
| `'minmaxavg'` | Three lines through the min, max and avg of each slot, optionally with a fill between avg and each edge | 3 points on 3 paths |
|
|
175
|
+
| `'band'` | One filled rectangle spanning min→max | 1 `<rect>`, 10 px wide |
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
{
|
|
179
|
+
name: 'Temp',
|
|
180
|
+
showAs: 'minmaxavg',
|
|
181
|
+
data: aggregateBySlot(raw, 'hourly'),
|
|
182
|
+
minColor: '#3b82f6', maxColor: '#ef4444', avgColor: '#64748b',
|
|
183
|
+
avgDashed: true, // default true
|
|
184
|
+
fillToMax: '#ef444433', fillToMin: '#3b82f633',
|
|
185
|
+
fillToMaxHatch: 'classic-diagonal', // optional, per fill
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`'band'` has two options of its own — both currently **TypeScript only**: `avgLine: true`
|
|
190
|
+
draws a short average tick inside each slot, and `countOpacity: true` makes a slot's opacity
|
|
191
|
+
proportional to its `count`, so a slot backed by two readings looks fainter than one backed
|
|
192
|
+
by thirty.
|
|
193
|
+
|
|
194
|
+
**`'minmaxavg'` is exactly three ordinary series.** Measured: the three paths have the same
|
|
195
|
+
geometry, character for character, as three plain series carrying the min, max and avg
|
|
196
|
+
values — `tests/series/minmaxavg_drei_serien.test.ts` proves it. What the aggregated form
|
|
197
|
+
adds is bookkeeping, not drawing: one legend entry instead of three, one series key for all
|
|
198
|
+
three paths (`…-min-line-0`, `-max-line-0`, `-avg-line-0`), `avgDashed` on by default, and
|
|
199
|
+
the two envelope fills in one line each.
|
|
200
|
+
|
|
201
|
+
So reach for three plain series when you want them to behave as three — separate legend
|
|
202
|
+
entries, separate toggling, separate axes. Reach for `showAs: 'minmaxavg'` when they are one
|
|
203
|
+
measurement seen three ways.
|
|
204
|
+
|
|
205
|
+
The envelope fills can be rebuilt too, since 1.8.0: a `FillBound` also takes
|
|
206
|
+
`{ series: '<id or name>' }`, so a region can span the gap between two curves.
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
series: [
|
|
210
|
+
{ name: 'Max', data: max },
|
|
211
|
+
{ name: 'Avg', data: avg, style: { fill: { regions: [
|
|
212
|
+
{ from: 'series', to: { series: 'Max' }, fill: '#ef444433' },
|
|
213
|
+
] } } },
|
|
214
|
+
]
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
The referenced curve is interpolated onto *this* series' timestamps, so the two need not
|
|
218
|
+
share a sampling rate. Where it has no value — a gap, or a time outside its own range — the
|
|
219
|
+
area **stops**, exactly as a line stops at a gap: continuing would assert a distance to a
|
|
220
|
+
reading nobody took. An unknown series name is an error naming the series, not a silently
|
|
221
|
+
missing fill.
|
|
222
|
+
|
|
223
|
+
This is useful well beyond rebuilding a band: a tolerance corridor, target against actual,
|
|
224
|
+
two sensors compared.
|
|
225
|
+
|
|
226
|
+
`'band'` has no such equivalent either: a rectangle per slot is not a line through the slots.
|
|
227
|
+
|
|
228
|
+
All three sit one under the other in the demo gallery under *Main Demos* — the aggregated
|
|
229
|
+
series, the same data as three plain series, and the same data again as a band. They all
|
|
230
|
+
carry an `outside-left` legend, which is where the difference shows: one entry against
|
|
231
|
+
three. (It also shifts the plot — an outside legend reserves width by its widest label.)
|
|
232
|
+
|
|
147
233
|
## Thresholds (incident analysis)
|
|
148
234
|
|
|
149
235
|
Define named thresholds once and reference them from a series:
|
|
@@ -355,6 +441,24 @@ both get one; leave it out and there is none. (Before 1.5.1 the Go port drew a g
|
|
|
355
441
|
nothing was configured while the TypeScript side drew none — the same JSON gave two
|
|
356
442
|
different charts.)
|
|
357
443
|
|
|
444
|
+
### Room at the edge — `padding`
|
|
445
|
+
|
|
446
|
+
The computed domain ends exactly at the smallest and largest reading, and the plot
|
|
447
|
+
is clipped to its rectangle. A line of `width: 10` at the maximum loses half its
|
|
448
|
+
stroke; a marker loses half its disc. `padding` keeps the automatic domain and adds
|
|
449
|
+
room around it:
|
|
450
|
+
|
|
451
|
+
```ts
|
|
452
|
+
axes: { left: { padding: 8 } } // both sides
|
|
453
|
+
axes: { left: { padding: { top: 24, bottom: 4 } } } // room for a label on top
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
In **pixels**, not a share of the range — the cause is in pixels (`line.width`,
|
|
457
|
+
`markers.size`), so the same number works for a series in °C and one in kW. Half the
|
|
458
|
+
widest stroke plus a little is a good rule of thumb. An explicit `domain` is left
|
|
459
|
+
alone, and padding that does not fit the chart height is ignored rather than
|
|
460
|
+
producing an inverted scale.
|
|
461
|
+
|
|
358
462
|
### Multi-Axis Layout (N Stacked Y-Axes)
|
|
359
463
|
|
|
360
464
|
To support 3 or more vertical axes (or arbitrary left/right positioning), define `axes.y: YAxisConfig[]` and assign series using `yAxisIndex`:
|
|
@@ -417,6 +521,42 @@ If `locale` is omitted it defaults to `navigator.language` in the browser and
|
|
|
417
521
|
`'en-US'` server-side, with an internal fallback to `en-US` when the requested
|
|
418
522
|
locale is unavailable in the runtime's ICU data.
|
|
419
523
|
|
|
524
|
+
### Time zone
|
|
525
|
+
|
|
526
|
+
The locale decides *how* a time is written. `timeZone` decides **which** time is
|
|
527
|
+
written — an IANA name:
|
|
528
|
+
|
|
529
|
+
```ts
|
|
530
|
+
new MLTimeGraph({
|
|
531
|
+
locale: 'de-DE',
|
|
532
|
+
timeZone: 'Europe/Berlin', // or 'UTC', 'Asia/Kolkata', …
|
|
533
|
+
series: [/* … */],
|
|
534
|
+
});
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
Omit it and the machine's zone applies, which is the behaviour up to 1.8.1 and stays the
|
|
538
|
+
default so nothing changes for callers who never thought about it. **Think about it once,
|
|
539
|
+
though**, because a browser sits in the reader's zone and a server almost always in UTC: the
|
|
540
|
+
*same* JSON then labels the same reading with different times depending on where it was
|
|
541
|
+
drawn. For a renderer whose point is that one call serves both, that is the one place the
|
|
542
|
+
promise leaks. The nightly PDF and the dashboard beside it should not disagree about when an
|
|
543
|
+
excursion happened.
|
|
544
|
+
|
|
545
|
+
Only the labels depend on it. Tick **positions** are epoch arithmetic and never move.
|
|
546
|
+
|
|
547
|
+
The zone is handled by `Intl` (TypeScript) and `time.Location` (Go), so daylight saving,
|
|
548
|
+
historical shifts and half-hour zones come out right — `Asia/Kolkata` is `UTC+05:30`, not a
|
|
549
|
+
rounded hour. The Go port embeds the zone database (`time/tzdata`, ~0.4 MB), so it also works
|
|
550
|
+
in a `scratch` or distroless image that ships no `/usr/share/zoneinfo`.
|
|
551
|
+
|
|
552
|
+
An unknown name is reported rather than silently ignored: `Intl` throws in TypeScript, and in
|
|
553
|
+
Go `models.ValidateOptions` — which both the CLI and the HTTP server call before drawing —
|
|
554
|
+
returns an error naming the zone.
|
|
555
|
+
|
|
556
|
+
```go
|
|
557
|
+
opts.TimeZone = "Europe/Berlin" // pkg/models, JSON key: "timeZone"
|
|
558
|
+
```
|
|
559
|
+
|
|
420
560
|
A custom `axes.x.format` takes precedence over locale formatting — you receive
|
|
421
561
|
the raw `Date` and own the output entirely:
|
|
422
562
|
|
|
@@ -493,8 +633,67 @@ style and appears as a sub-element on top of the parent series.
|
|
|
493
633
|
Supported overlay kinds: `'movingAverage'`, `'movingMkt'` (USP <1079.2>),
|
|
494
634
|
`'stdDevBand'` (mean ± multiplier·σ band), `'limits'`.
|
|
495
635
|
|
|
636
|
+
`type: 'exponential'` on a `movingAverage` switches to an exponentially weighted mean over
|
|
637
|
+
the same **time** window: `alpha = 1 - exp(-Δt / window)`. Not the textbook `2/(N+1)` form,
|
|
638
|
+
which assumes an even sampling rate — sensor data rarely has one, and after a gap the
|
|
639
|
+
reading before it would otherwise still weigh as much as the one right before. Here the
|
|
640
|
+
weight decays with elapsed *time*; at an even rate the two coincide.
|
|
641
|
+
|
|
642
|
+
### Computing an overlay yourself
|
|
643
|
+
|
|
644
|
+
An overlay is a convenience, not a second implementation. The same numbers come out of the
|
|
645
|
+
public helpers, so you can compute the series before rendering and pass it as an ordinary
|
|
646
|
+
one:
|
|
647
|
+
|
|
648
|
+
```ts
|
|
649
|
+
import { rollingMean, rollingMkt, rollingStdDev } from 'ml-time-graph/analyze';
|
|
650
|
+
|
|
651
|
+
const glaettung = rollingMean(data, 4 * 60 * 60_000);
|
|
652
|
+
|
|
653
|
+
new MLTimeGraph({
|
|
654
|
+
series: [
|
|
655
|
+
{ name: 'Fridge sensor', data },
|
|
656
|
+
{ name: 'Moving average', data: glaettung, style: { line: { color: '#7c3aed' } } },
|
|
657
|
+
],
|
|
658
|
+
});
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
This is *proven*, not asserted: `tests/series/overlay_extern.test.ts` renders both ways and
|
|
662
|
+
compares the path geometry character for character.
|
|
663
|
+
|
|
664
|
+
The Go port renders `overlays` too, since 1.8.0, from the same maths in its own public
|
|
665
|
+
package:
|
|
666
|
+
|
|
667
|
+
```go
|
|
668
|
+
import "gitlab.com/mlc0911/mlctimegraph/go-time-graph/pkg/analyze"
|
|
669
|
+
|
|
670
|
+
geglaettet := analyze.RollingMean(punkte, 4*60*60*1000)
|
|
671
|
+
mkt := analyze.RollingMkt(punkte, 24*60*60*1000, analyze.DefaultActivationEnergy)
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
`RollingMean`, `RollingEma`, `RollingStdDev`, `RollingMkt`, `StdDev`, `SampleStdDev` and
|
|
675
|
+
`Mkt` are exported from `pkg/analyze` — deliberately, so a caller that only wants the
|
|
676
|
+
numbers (a PDF renderer, a report service) gets them without drawing a chart. They are
|
|
677
|
+
bit-identical to their TypeScript namesakes, down to the order of the summands.
|
|
678
|
+
|
|
679
|
+
> **Windows are in milliseconds** throughout that family. `MovingAvg`
|
|
680
|
+
> (`ml-time-graph/internals`) is the odd one out: it counts *points*, and it renders itself
|
|
681
|
+
> rather than returning data. It predates the rolling family and is kept for callers that
|
|
682
|
+
> want a moving average as a ready `DrawCommand`; for everything else, prefer `rollingMean`
|
|
683
|
+
> / `rollingEma`, which handle uneven sampling and gaps correctly.
|
|
684
|
+
|
|
496
685
|
## Aggregation & helpers
|
|
497
686
|
|
|
687
|
+
**Why a chart library carries this at all.** None of it is hard maths — a rolling mean is a
|
|
688
|
+
dozen lines. But it is the handful of figures a time series is almost always looked at
|
|
689
|
+
through, and a caller who has to write them again gets a slightly different answer each
|
|
690
|
+
time: a window counted in points here and in milliseconds there, a gap skipped in one place
|
|
691
|
+
and interpolated in another, MKT with someone's own activation energy. Then the smoothed
|
|
692
|
+
line in the nightly PDF and the one on the dashboard disagree, and nobody can say which is
|
|
693
|
+
right. They live here so that both come from the same twelve lines — and they are public so
|
|
694
|
+
that computing them *outside* the chart gives the identical result, which
|
|
695
|
+
`tests/series/overlay_extern.test.ts` checks rather than asserts.
|
|
696
|
+
|
|
498
697
|
The full analyse companion is at `ml-time-graph/analyze`:
|
|
499
698
|
|
|
500
699
|
```ts
|
|
@@ -503,7 +702,8 @@ import {
|
|
|
503
702
|
mkt, rollingMkt, DEFAULT_ACTIVATION_ENERGY,
|
|
504
703
|
stdDev, sampleStdDev, rollingStdDev,
|
|
505
704
|
computeLimitExcursions,
|
|
506
|
-
|
|
705
|
+
rollingMean,
|
|
706
|
+
StatsAggregator,
|
|
507
707
|
} from 'ml-time-graph/analyze';
|
|
508
708
|
|
|
509
709
|
aggregateBySlot(raw, 'hourly'); // min/max/avg per slot
|
|
@@ -531,7 +731,7 @@ new MLTimeGraph({
|
|
|
531
731
|
annotations: [
|
|
532
732
|
{ type: 'arrow', from: { time: t1, value: 22 }, to: { time: t2, value: 30 }, color: '#7c3aed' },
|
|
533
733
|
{ type: 'label', at: { time: t2, value: 30 }, text: 'Spike', dy: -8 },
|
|
534
|
-
{ type: 'rect', from: { time: a, value: 20 }, to: { time: b, value: 26 }, fill: '#7c3aed18' },
|
|
734
|
+
{ type: 'rect', from: { time: a, value: 20 }, to: { time: b, value: 26 }, fill: '#7c3aed18', hatch: 'classic-diagonal' },
|
|
535
735
|
{ type: 'point', at: { time: t, value: v }, shape: 'circle' },
|
|
536
736
|
{ type: 'line', from: { time: a, value: v }, to: { time: b, value: v }, dash: 'dotted' },
|
|
537
737
|
],
|
|
@@ -539,6 +739,9 @@ new MLTimeGraph({
|
|
|
539
739
|
```
|
|
540
740
|
|
|
541
741
|
A coordinate is a `DataPointRef` = `{ time, value, axis? }` (`axis` selects the y-scale).
|
|
742
|
+
`dash` takes a variant (`'dashed'` or `'dotted'`), not a raw SVG pattern — a `'5,5'` is
|
|
743
|
+
not rejected, it simply draws a solid line. Give an annotation an `id` and it is wrapped in
|
|
744
|
+
its own group you can style or toggle (see [CSS classes](#css-classes-in-the-svg)).
|
|
542
745
|
|
|
543
746
|
Annotations can also be managed **dynamically** (e.g. on a click). Like `setData`,
|
|
544
747
|
these update state — re-render afterwards (`renderCommands()` → renderer):
|
|
@@ -619,6 +822,126 @@ attachments. For a Node-free backend, the Go port
|
|
|
619
822
|
[`go-time-graph`](https://gitlab.com/mlc0911/mlctimegraph/-/tree/main/go-time-graph)
|
|
620
823
|
renders the same JSON config to identical SVG.
|
|
621
824
|
|
|
825
|
+
## CSS classes in the SVG
|
|
826
|
+
|
|
827
|
+
Every group the renderer emits carries a class, and so do the line and fill paths inside
|
|
828
|
+
a series. The same classes come out of the Go port — a chart styled in the browser looks
|
|
829
|
+
the same when the backend renders it. The
|
|
830
|
+
one exception is marked in the table and explained below.
|
|
831
|
+
|
|
832
|
+
| Class | Element | Contains |
|
|
833
|
+
| :--- | :--- | :--- |
|
|
834
|
+
| `value-axis left` / `value-axis right` | `<g>` | One per value axis: its line, ticks and tick labels. The second word is the axis position. |
|
|
835
|
+
| `time-axis` | `<g>` | The x-axis line, ticks and time labels. |
|
|
836
|
+
| `axis-labels` | `<g>` | The axis *titles* (`axes.<axis>.label`), not the tick labels. |
|
|
837
|
+
| `chart-grid` | `<g>` | Horizontal and vertical grid lines. |
|
|
838
|
+
| `chart-legend` | `<g>` | The SVG legend. The optional HTML legend is your own markup and not affected. |
|
|
839
|
+
| `series series--<slug>` | `<g>` | One per series — line, area, markers and its gaps. See [Slugs](#slugs) below. |
|
|
840
|
+
| `thresholds` | `<g>` | Wraps all threshold lines and zones. |
|
|
841
|
+
| `threshold threshold--<slug>` | `<g>` | One per threshold, inside `.thresholds`. |
|
|
842
|
+
| `threshold-labels` | `<g>` | Wraps all threshold labels. |
|
|
843
|
+
| `threshold-label threshold-label--<slug>` | `<g>` | One per threshold label, inside `.threshold-labels`. |
|
|
844
|
+
| `markers` | `<g>` | Time-based markers (`markers`), with their projection lines. |
|
|
845
|
+
| `gaps` | `<g>` | Chart-wide gap regions (`gaps.regions`). Per-series gaps sit inside their series group. |
|
|
846
|
+
| `highlights` | `<g>` | Time-range highlights (`highlights`). |
|
|
847
|
+
| `annotations` | `<g>` | Wraps all free annotations. |
|
|
848
|
+
| `annotation annotation--<slug>` | `<g>` | One per annotation **that has an `id`** — without one it is drawn bare, with no group. |
|
|
849
|
+
| `annotation-band` | `<g>` | A status/phase band below the plot. |
|
|
850
|
+
| `annotation-band-axis` | `<g>` | That band's own axis line. |
|
|
851
|
+
| `series-line` | `<path>` | A line segment. Not a group — the class sits on the path itself. |
|
|
852
|
+
| `series-fill` | `<path>` | A filled area: a `fill.region`, a min/max/avg band, a zone fill. |
|
|
853
|
+
|
|
854
|
+
The classes are **not namespaced**. If you inline the SVG into a page whose stylesheet
|
|
855
|
+
has a rule like `.series { display: none }`, that rule applies to the chart too. Scope
|
|
856
|
+
your own rules, and check the host page's when a chart comes out wrong.
|
|
857
|
+
|
|
858
|
+
### Slugs
|
|
859
|
+
|
|
860
|
+
Series and thresholds follow the same rule, and the Go port follows it too:
|
|
861
|
+
|
|
862
|
+
```
|
|
863
|
+
class suffix = config.id ?? slugify(config.name)
|
|
864
|
+
```
|
|
865
|
+
|
|
866
|
+
An explicit `id` is taken **verbatim** — you chose it, so you get it back unchanged, and
|
|
867
|
+
it is your job to keep it selector-safe. Without one, the display name is slugified:
|
|
868
|
+
lowercased, accents stripped, every run of non-alphanumerics collapsed to `-`, capped at
|
|
869
|
+
50 characters, empty → `unnamed`.
|
|
870
|
+
|
|
871
|
+
Annotations differ on both counts: they are grouped only when they carry an `id`, and
|
|
872
|
+
that `id` *is* slugified (`slugify(a.id)`).
|
|
873
|
+
|
|
874
|
+
```ts
|
|
875
|
+
import { slugify } from 'ml-time-graph';
|
|
876
|
+
|
|
877
|
+
slugify('Temperatur (°C)') // 'temperatur-c' → .series--temperatur-c
|
|
878
|
+
slugify('Warning Low') // 'warning-low' → .threshold--warning-low
|
|
879
|
+
```
|
|
880
|
+
|
|
881
|
+
`slugify` is exported so you can compute the selector for a name you hold at runtime —
|
|
882
|
+
building a legend with toggles, for instance:
|
|
883
|
+
|
|
884
|
+
```ts
|
|
885
|
+
const cls = `.series--${slugify(series.name)}`;
|
|
886
|
+
svgRoot.querySelector(cls)?.classList.toggle('is-hidden');
|
|
887
|
+
```
|
|
888
|
+
|
|
889
|
+
The same slug is also the group's `id` (`#series-temperatur-c`, `#threshold-warning-low`),
|
|
890
|
+
so either handle works.
|
|
891
|
+
|
|
892
|
+
**Inside** a series group the elements carry a different, stable key: `s-<crc32 of the
|
|
893
|
+
name>`, from `seriesKey()` (`ml-time-graph/internals`). It stays the same across renders
|
|
894
|
+
and is identical in both libraries:
|
|
895
|
+
|
|
896
|
+
| Element | `id` |
|
|
897
|
+
| :--- | :--- |
|
|
898
|
+
| A line segment | `s-<key>-line-<n>` — `<n>` counts across the whole series, so a series broken by two data gaps gives `-line-0`, `-line-1`, `-line-2` |
|
|
899
|
+
| A line segment of a layered `line: [...]` | `s-<key>-line-<n>-<layer>` |
|
|
900
|
+
| A fill region | `s-<key>-fill-r<region>-<piece>` — a region that crosses the curve several times draws as several pieces, and the piece number is always present, even when there is only one |
|
|
901
|
+
|
|
902
|
+
### ids are document-global — classes are not
|
|
903
|
+
|
|
904
|
+
An `id` is unique per *document*, not per chart. Put two charts on one page and their ids
|
|
905
|
+
collide: the same series name yields the same key, so `getElementById('series-temperature')`
|
|
906
|
+
and `#s-38d2a5bb-line-0` find the **first** chart's element, whichever you meant.
|
|
907
|
+
|
|
908
|
+
```
|
|
909
|
+
one page, two charts, same series → series-temperature ×2
|
|
910
|
+
s-38d2a5bb-line-0 ×2
|
|
911
|
+
```
|
|
912
|
+
|
|
913
|
+
Classes do not have that problem, which is why the group carries both and the paths inside
|
|
914
|
+
carry `series-line` / `series-fill`. Scope through the container and use the classes:
|
|
915
|
+
|
|
916
|
+
```css
|
|
917
|
+
#chart-2 .series--temperature { opacity: .3 } /* one series, one chart */
|
|
918
|
+
#chart-2 .series--temperature .series-line { stroke-width: 3 }
|
|
919
|
+
#chart-2 .series-fill { display: none }
|
|
920
|
+
```
|
|
921
|
+
|
|
922
|
+
There is a second reason to prefer them: a class is something you can *write down*. The
|
|
923
|
+
`id` contains a crc32 of the series name, which you cannot compute without `seriesKey()`
|
|
924
|
+
from `ml-time-graph/internals` — whereas `slugify(name)` is exported precisely so you can
|
|
925
|
+
build `.series--${slugify(name)}` from a name you hold.
|
|
926
|
+
|
|
927
|
+
Reach for the `id` when there is one chart in the document, or when you have scoped the
|
|
928
|
+
lookup yourself (`container.querySelector('#…')` rather than `document.getElementById`).
|
|
929
|
+
|
|
930
|
+
### Provenance
|
|
931
|
+
|
|
932
|
+
Every SVG names the library and version that drew it, as the first child of the root:
|
|
933
|
+
|
|
934
|
+
```xml
|
|
935
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="800" height="400" viewBox="0 0 800 400">
|
|
936
|
+
<!-- MLTimeGraph 1.8.0 -->
|
|
937
|
+
```
|
|
938
|
+
|
|
939
|
+
The line is deliberately the same in both libraries — it does not say whether the browser
|
|
940
|
+
or the server drew the file, because that would break the byte-identical output the two
|
|
941
|
+
are held to. The version is `VERSION`, exported from the package root and always equal to
|
|
942
|
+
`version.Version` in the Go port. SVG minifiers strip comments by default; if you need the
|
|
943
|
+
provenance to survive, keep comments.
|
|
944
|
+
|
|
622
945
|
## Package exports
|
|
623
946
|
|
|
624
947
|
| Import | Contents |
|
|
@@ -626,7 +949,7 @@ renders the same JSON config to identical SVG.
|
|
|
626
949
|
| `ml-time-graph` | The rendering API — `mount`, `MLTimeGraph`, `SVGRenderer`, `attachTooltip`, the legend helpers `getLineStyle` / `drawMarker` / `getHatch`, and all option/data types. |
|
|
627
950
|
| `ml-time-graph/analyze` | Statistics companion — `aggregateBySlot`, `downsample`, `detectGaps`, `mkt` / `rollingMkt`, `stdDev`, `StatsAggregator`, … (see [Aggregation](#aggregation--helpers)). |
|
|
628
951
|
| `ml-time-graph/interaction` | Optional interaction primitives — `Zoom`, `Minimap`, `Tooltip` (lower-level than `attachTooltip`). |
|
|
629
|
-
| `ml-time-graph/internals` | Building blocks for **custom renderers** — the abstract `Renderer`, free render-functions, scales, axis classes, the `DrawCommand` model
|
|
952
|
+
| `ml-time-graph/internals` | Building blocks for **custom renderers** — the abstract `Renderer`, free render-functions, scales, axis classes, the `DrawCommand` model, and `MovingAvg`. |
|
|
630
953
|
|
|
631
954
|
## Renderer
|
|
632
955
|
|