ml-time-graph 1.7.2 → 1.9.0
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 +246 -8
- package/README.de.md +104 -11
- package/README.md +101 -11
- package/SKILLS.md +157 -4
- package/USAGE.md +400 -3
- package/dist/analyze/index.d.ts +54 -1
- package/dist/analyze/index.js +2 -2
- package/dist/index.d.ts +131 -16
- package/dist/index.js +25 -28
- 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-CQhADkWB.d.ts} +72 -31
- package/dist/{series_renderer-COVNvq2x.d.ts → slug-aDUz3wBN.d.ts} +27 -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.
|