ml-time-graph 1.17.0 → 1.18.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 +81 -0
- package/GRAPH_JSON_STRUCTURE.md +6 -4
- package/README.de.md +6 -4
- package/README.md +5 -4
- package/SKILLS.md +9 -538
- package/USAGE.md +27 -2
- package/dist/index.d.ts +21 -16
- package/dist/index.js +41 -41
- package/dist/interaction/index.d.ts +1 -2
- package/dist/interaction/index.js +1 -1
- package/dist/internals.d.ts +16 -4
- package/dist/internals.js +19 -19
- package/dist/{marker_renderer-BsiVseet.d.ts → marker_renderer-BZMcTbHC.d.ts} +19 -3
- package/dist/{types-DdltSMkw.d.ts → types-CNIUKrmy.d.ts} +41 -2
- package/package.json +3 -2
- package/skills/ml-time-graph/SKILL.md +113 -0
- package/skills/ml-time-graph/reference/annotations-and-markers.md +63 -0
- package/skills/ml-time-graph/reference/axes.md +112 -0
- package/skills/ml-time-graph/reference/gaps.md +75 -0
- package/skills/ml-time-graph/reference/legend.md +57 -0
- package/skills/ml-time-graph/reference/rendering.md +84 -0
- package/skills/ml-time-graph/reference/series-styles.md +58 -0
- package/skills/ml-time-graph/reference/svelte.md +37 -0
- package/skills/ml-time-graph/reference/thresholds-and-fills.md +64 -0
package/SKILLS.md
CHANGED
|
@@ -1,543 +1,14 @@
|
|
|
1
|
-
# SKILLS.md
|
|
1
|
+
# SKILLS.md has moved
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The agent quick start for `ml-time-graph` is now a proper agent skill:
|
|
4
|
+
**[skills/ml-time-graph/SKILL.md](skills/ml-time-graph/SKILL.md)** — with `name`/`description`
|
|
5
|
+
frontmatter, a short core and task-by-task reference files in `skills/ml-time-graph/reference/`.
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
It ships inside the package. To give an agent (Claude Code and others) the skill, copy the folder
|
|
8
|
+
into its skills directory:
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
```ts
|
|
10
|
-
import { mount } from 'ml-time-graph';
|
|
11
|
-
|
|
12
|
-
mount('#chart', {
|
|
13
|
-
width: 800,
|
|
14
|
-
height: 400,
|
|
15
|
-
series: [
|
|
16
|
-
{
|
|
17
|
-
name: 'Temperature',
|
|
18
|
-
data: [
|
|
19
|
-
{ time: 1704067200000, value: 21.5 },
|
|
20
|
-
{ time: 1704070800000, value: 22.8 },
|
|
21
|
-
{ time: 1704074400000, value: 20.3 },
|
|
22
|
-
],
|
|
23
|
-
style: { line: { color: '#ef4444' } },
|
|
24
|
-
},
|
|
25
|
-
],
|
|
26
|
-
});
|
|
27
|
-
```
|
|
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
|
-
|
|
61
|
-
## When to Use What
|
|
62
|
-
|
|
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.
|
|
64
|
-
- `new MLTimeGraph(options)`: The core headless engine when working with options objects directly — use for Node.js SSR, headless testing, or custom render pipelines.
|
|
65
|
-
- Builders (`GraphBuilder`, etc.): The fluent, imperative chaining API — use when assembling charts dynamically across conditional branches.
|
|
66
|
-
- Go port (`go-time-graph`): The 1:1 server-side Go implementation (`gitlab.com/mlc0911/mlctimegraph/go-time-graph`) — use when rendering charts in Go backends or microservices without a Node runtime.
|
|
67
|
-
- PDF output (`mlctimegraph-pdf`): A second consumer of the same draw commands (`gitlab.com/mlc0911/mlctimegraphpdf`) — writes native PDF vectors without a browser. Early (0.2.0); a fill on a path is still opaque and unhatched there.
|
|
68
|
-
|
|
69
|
-
## Package Entry Points
|
|
70
|
-
|
|
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`).
|
|
72
|
-
- `ml-time-graph/analyze`: Pure-math statistics companion — `aggregateBySlot`, `downsample` (LTTB), `detectGaps`, `rollingMkt`, `rollingStdDev`, `computeLimitExcursions`, `StatsAggregator`.
|
|
73
|
-
- `ml-time-graph/interaction`: Headless interaction primitives — `Zoom`, `Minimap`, and coordinate-based interaction state management.
|
|
74
|
-
- `ml-time-graph/internals`: Low-level building blocks for custom renderers — `DrawCommand` model, base `Renderer`, layout engine, scales, and axis renderers.
|
|
75
|
-
|
|
76
|
-
## Common Tasks
|
|
77
|
-
|
|
78
|
-
### Multiple Series & Multi-Axis
|
|
79
|
-
Map series to vertical axes via `yAxisIndex`:
|
|
80
|
-
```ts
|
|
81
|
-
mount('#chart', {
|
|
82
|
-
margin: { top: 20, right: 80, bottom: 40, left: 60 },
|
|
83
|
-
series: [
|
|
84
|
-
{ name: 'Temp', data: tempData, yAxisIndex: 0, style: { line: { color: '#ef4444' } } },
|
|
85
|
-
{ name: 'Humidity', data: humData, yAxisIndex: 1, style: { line: { color: '#3b82f6', style: 'dashed' } } },
|
|
86
|
-
],
|
|
87
|
-
axes: {
|
|
88
|
-
left: { label: 'Temperature (°C)' },
|
|
89
|
-
right: { label: 'Humidity (%)' },
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Thresholds & Zone Colouring
|
|
95
|
-
Define thresholds once and reference them by name to colour lines or zones:
|
|
96
|
-
```ts
|
|
97
|
-
mount('#chart', {
|
|
98
|
-
thresholds: [
|
|
99
|
-
{ name: 'warn', value: 25, color: '#f59e0b', line: 'dashed', label: 'Warning' },
|
|
100
|
-
{ name: 'crit', value: 30, color: '#ef4444', line: 'solid', label: 'Critical' },
|
|
101
|
-
],
|
|
102
|
-
series: [{ name: 'Temp', data: tempData, colorByThresholds: ['warn', 'crit'] }],
|
|
103
|
-
});
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### Filled Areas Between Curve and Threshold
|
|
107
|
-
Use `style.fill.regions` with bounds (`from`, `to`, `side`, optional `hatch`):
|
|
108
|
-
```ts
|
|
109
|
-
style: {
|
|
110
|
-
fill: {
|
|
111
|
-
regions: [
|
|
112
|
-
{ from: { threshold: 'warn' }, to: 'series', side: 'above', fill: { color: '#f59e0b33', hatch: 'classic-diagonal' } },
|
|
113
|
-
{ from: { threshold: 'crit' }, to: 'series', side: 'above', fill: '#ef444444' },
|
|
114
|
-
],
|
|
115
|
-
},
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
### Annotations & Region Highlights
|
|
120
|
-
Enrich charts with domain context: vertical time-span highlights across the plot, free-form annotations in data coordinates (which automatically scale through zoom/resize), and status bands below:
|
|
121
|
-
```ts
|
|
122
|
-
mount('#chart', {
|
|
123
|
-
series,
|
|
124
|
-
// Vertical time-range highlight behind the data:
|
|
125
|
-
highlights: [
|
|
126
|
-
{ startTime: t1, endTime: t2, label: 'Calibration Phase', color: '#3b82f6', opacity: 0.15 },
|
|
127
|
-
],
|
|
128
|
-
// Overlays anchored in data coordinates (time/value) tracking zoom & resize:
|
|
129
|
-
annotations: [
|
|
130
|
-
{ type: 'arrow', from: { time: t1, value: 20 }, to: { time: t2, value: 35 }, color: '#ef4444' },
|
|
131
|
-
{ type: 'label', at: { time: t2, value: 35 }, text: 'Pressure Spike', dy: -10 },
|
|
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' },
|
|
135
|
-
],
|
|
136
|
-
// Horizontal status/phase bands below the plot (sharing the time axis):
|
|
137
|
-
annotationBands: [
|
|
138
|
-
{
|
|
139
|
-
name: 'Operating State',
|
|
140
|
-
height: 20,
|
|
141
|
-
items: [
|
|
142
|
-
{ startTime: t1, endTime: t2, label: 'Running', fill: '#22c55e44' },
|
|
143
|
-
{ startTime: t2, endTime: t3, label: 'Standby', fill: '#eab30844' },
|
|
144
|
-
],
|
|
145
|
-
},
|
|
146
|
-
],
|
|
147
|
-
});
|
|
148
|
-
```
|
|
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
|
-
|
|
165
|
-
### Data Gaps
|
|
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
|
-
Two things make a gap readable, and both are off unless you ask:
|
|
177
|
-
|
|
178
|
-
```ts
|
|
179
|
-
gaps: {
|
|
180
|
-
style: {
|
|
181
|
-
label: { rotate: 'auto' }, // turn it upright when it does not fit
|
|
182
|
-
band: { height: 6, fill: '#dc2626', opacity: 0.8 }, // a strip on the baseline
|
|
183
|
-
},
|
|
184
|
-
regions: [...],
|
|
185
|
-
}
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
`rotate: 'auto'` turns a label to -90° **only when it is wider than its gap** — measured with the
|
|
189
|
-
same advance table the rest of the layout uses. *Sensor Offline* is about 72 px; a two-hour gap on
|
|
190
|
-
a 600 px chart is 50. Flat, it spills out over the data next to it. A label placed in the margin
|
|
191
|
-
(`baseline: 'above' | 'below'`) stays flat: nothing is in its way there.
|
|
192
|
-
|
|
193
|
-
`band` draws a strip on the plot's baseline, **one per gap** — the data between two gaps is not
|
|
194
|
-
missing. It is the answer for `display: 'empty'`, where the picture otherwise shows nothing but a
|
|
195
|
-
break in the line, and it needs no extra margin (an annotation band does). `offset` pushes it into
|
|
196
|
-
the bottom margin if you have room there.
|
|
197
|
-
|
|
198
|
-
A **label** is separate from all of this and comes from `gaps.regions[].label` — a region knows
|
|
199
|
-
which gap it describes, a series style would have to give all of them the same text:
|
|
200
|
-
|
|
201
|
-
```ts
|
|
202
|
-
gaps: {
|
|
203
|
-
style: { display: 'filled', fill: '#6b7280', opacity: 0.15, label: { baseline: 'middle' } },
|
|
204
|
-
regions: [{ startTime: 1704070800000, endTime: 1704074400000, label: 'Sensor offline' }],
|
|
205
|
-
}
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
Connect with a bridge or render gap spans:
|
|
209
|
-
```ts
|
|
210
|
-
series: [{
|
|
211
|
-
name: 'Sensor',
|
|
212
|
-
data: [{ time: 1704067200000, value: 20 }, { time: 1704070800000, value: null }, { time: 1704074400000, value: 25 }],
|
|
213
|
-
style: {
|
|
214
|
-
line: { color: '#3b82f6' },
|
|
215
|
-
gap: { display: 'bridge_line', bridge: { color: '#94a3b8', width: 1.5, style: 'dotted' } },
|
|
216
|
-
},
|
|
217
|
-
}]
|
|
10
|
+
```bash
|
|
11
|
+
cp -r node_modules/ml-time-graph/skills/ml-time-graph .claude/skills/
|
|
218
12
|
```
|
|
219
13
|
|
|
220
|
-
|
|
221
|
-
Render sample markers on curves or standalone timestamp event lines:
|
|
222
|
-
```ts
|
|
223
|
-
series: [{
|
|
224
|
-
name: 'Sensor',
|
|
225
|
-
data: sensorData,
|
|
226
|
-
style: { markers: { type: 'circle', size: 3, stroke: '#3b82f6', fill: '#ffffff', threshold: 100 } },
|
|
227
|
-
}],
|
|
228
|
-
markers: [
|
|
229
|
-
{ time: 1704070800000, value: 25, label: 'Peak event', color: '#ef4444', lineStyle: 'to-value' },
|
|
230
|
-
]
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
### Axes & Margins
|
|
234
|
-
The layout engine does not call into the DOM, but it does know how wide text is: the advance
|
|
235
|
-
widths of the default font stack (`Liberation Sans, Arial, Helvetica` — the three are
|
|
236
|
-
metric-compatible) ship with both libraries. Margins still have to be large enough for your
|
|
237
|
-
labels; the library reserves room inside them, it does not grow them.
|
|
238
|
-
And because the computed domain ends exactly on the smallest and largest reading, a thick line
|
|
239
|
-
or a marker at the extreme loses half of itself to the clip — `padding` adds room **in pixels**
|
|
240
|
-
(the cause is in pixels: `line.width`, `markers.size`), without giving up the automatic domain:
|
|
241
|
-
```ts
|
|
242
|
-
margin: { top: 20, right: 80, bottom: 40, left: 60 },
|
|
243
|
-
axes: {
|
|
244
|
-
x: { label: 'Time', format: (d: Date) => d.toISOString().slice(11, 16) },
|
|
245
|
-
left: {
|
|
246
|
-
label: 'Temperature (°C)',
|
|
247
|
-
format: (v: number) => `${v.toFixed(1)} °C`,
|
|
248
|
-
padding: 8, // or { top: 24, bottom: 4 }
|
|
249
|
-
},
|
|
250
|
-
right: { label: 'Humidity (%)', format: (v: number) => `${v.toFixed(0)} %` },
|
|
251
|
-
}
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
Using a different font? Either generate the table for it
|
|
255
|
-
(`node scripts/gen-text-metrics.mjs --font YourFont.ttf`, in the repository) or measure yourself:
|
|
256
|
-
|
|
257
|
-
```ts
|
|
258
|
-
const canvas = document.createElement('canvas').getContext('2d')!;
|
|
259
|
-
new MLTimeGraph({
|
|
260
|
-
fontFamily: 'Inter, sans-serif',
|
|
261
|
-
textMetrics: (text, fontSize) => {
|
|
262
|
-
canvas.font = `${fontSize}px Inter, sans-serif`;
|
|
263
|
-
return canvas.measureText(text).width;
|
|
264
|
-
},
|
|
265
|
-
});
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
**A measuring callback leaves the parity with the Go port, deliberately** — a function does not
|
|
269
|
-
travel in JSON, so the server cannot run yours. Same chart, two renderers, two layouts. Use it when
|
|
270
|
-
the browser is the only renderer; use a generated table when both have to agree. `estimateMetrics`
|
|
271
|
-
(exported) reproduces the layout of versions before 1.10.0.
|
|
272
|
-
|
|
273
|
-
### Grid
|
|
274
|
-
`axes: { x: { grid: true } }` — a grid in both directions; one axis is enough. `grid: false` on
|
|
275
|
-
the other axis leaves that direction out; a `false` alone switches nothing on. `true` is short for
|
|
276
|
-
`{ major: {} }`; use the object form for colour, minor lines or `layer`.
|
|
277
|
-
|
|
278
|
-
### Minor Ticks & Grid
|
|
279
|
-
```ts
|
|
280
|
-
axes: {
|
|
281
|
-
x: { grid: { major: {}, minor: { color: '#eee', style: 'dashed' } }, ticks: { minor: 1 } },
|
|
282
|
-
left: { grid: { major: {}, minor: {} }, ticks: { major: 5, minor: 3 } },
|
|
283
|
-
}
|
|
284
|
-
```
|
|
285
|
-
`ticks.minor` is how many ticks sit **between** two major ones (3 → quarters). `grid.minor` draws
|
|
286
|
-
the matching lines, by default at half the strength of the major grid; setting it without
|
|
287
|
-
`ticks.minor` means one. One minor style governs both directions — `axes.x` wins over `axes.left`,
|
|
288
|
-
same as for the major grid.
|
|
289
|
-
|
|
290
|
-
### Smoothing — which spline
|
|
291
|
-
```ts
|
|
292
|
-
style: { line: { smoothing: true, smoothingType: 'monotone' } }
|
|
293
|
-
```
|
|
294
|
-
`'catmull-rom'` is the default: soft, cheap, and it **overshoots** — between two readings it
|
|
295
|
-
draws values that neither of them is. On a chart of measurements that is the picture claiming
|
|
296
|
-
something the sensor never reported.
|
|
297
|
-
|
|
298
|
-
`'monotone'` (Fritsch-Carlson) never leaves the span between two readings. Use it for anything
|
|
299
|
-
with a ceiling or a floor: humidity at 100 %, tank levels, battery charge, percentages.
|
|
300
|
-
|
|
301
|
-
`'akima'` takes its slope from five points and weights it against the change on the other side,
|
|
302
|
-
so a long flat run stays flat right up to an edge. Use it for sensors that only transmit on
|
|
303
|
-
change — a cliff in the data is a real event, not a reason to draw a wave before it.
|
|
304
|
-
|
|
305
|
-
`smoothing: true` remains the switch; `smoothingType` on its own draws straight segments. A step
|
|
306
|
-
shape is never smoothed, whatever you set.
|
|
307
|
-
|
|
308
|
-
If you write your own renderer against the draw commands, read `smoothingType` off the path and
|
|
309
|
-
pass it on — in Go that is `SmoothPathWith`, not `SmoothPath`, which always takes Catmull-Rom.
|
|
310
|
-
Drop it and the same chart comes out as one curve from your renderer and another from the SVG,
|
|
311
|
-
with nothing to warn you: this is exactly what happened to the PDF output in 1.10.0.
|
|
312
|
-
|
|
313
|
-
### Grid in front or behind — `grid.layer`
|
|
314
|
-
```ts
|
|
315
|
-
axes: { x: { grid: { major: {}, layer: 'back' } }, left: { grid: { major: {} } } }
|
|
316
|
-
```
|
|
317
|
-
By default the grid is drawn **after** the series (`layer: 'front'`): a filled area down to the
|
|
318
|
-
baseline does not hide it, and its lines stay visible over the curve. Markers, annotations and
|
|
319
|
-
labels are drawn later still and stay on top either way.
|
|
320
|
-
|
|
321
|
-
`'back'` draws it first of all, before highlights and thresholds — the choice when an opaque fill
|
|
322
|
-
is *meant* to cover it, or when a faint raster behind the data is enough. One setting governs the
|
|
323
|
-
whole grid; `axes.x` wins over `axes.left`, same as for the style.
|
|
324
|
-
|
|
325
|
-
### Building draw commands — `CommandBuilder`
|
|
326
|
-
```ts
|
|
327
|
-
import { CommandBuilder } from 'ml-time-graph/internals';
|
|
328
|
-
|
|
329
|
-
const b = new CommandBuilder();
|
|
330
|
-
b.rect({ x: 0, y: 0, w: 10, h: 10, fill: '#eee' })
|
|
331
|
-
.text({ content: 'Label', x: 14, y: 9 })
|
|
332
|
-
.group({ cssClass: 'marks' }, (g) => g.circle({ cx: 5, cy: 5, r: 2 }));
|
|
333
|
-
const group = b.toGroup({ cssClass: 'my-layer' });
|
|
334
|
-
```
|
|
335
|
-
For anyone producing draw commands — a custom layer, an annotation of your own, a renderer that
|
|
336
|
-
feeds the same `SVGRenderer`. It sets `type` for you, chains, and nests.
|
|
337
|
-
|
|
338
|
-
Two rules worth knowing. `group` draws **nothing** when its body adds nothing, so you cannot leave
|
|
339
|
-
an empty `<g>` behind; `toGroup` always returns a group, because a function that promises one must
|
|
340
|
-
deliver one. And `commands` hands out a copy — what you do with it afterwards cannot reach back
|
|
341
|
-
into the chart.
|
|
342
|
-
|
|
343
|
-
In Go it is `renderer.NewCommandBuilder()` with the same methods.
|
|
344
|
-
|
|
345
|
-
### Two probes that should agree — `divergenceExcursions`
|
|
346
|
-
```ts
|
|
347
|
-
import { divergenceExcursions } from 'ml-time-graph/analyze';
|
|
348
|
-
|
|
349
|
-
const episodes = divergenceExcursions(
|
|
350
|
-
[{ name: 'Oben', data: oben }, { name: 'Unten', data: unten }],
|
|
351
|
-
{ maxDelta: 2, minDurationMs: 30 * 60_000 },
|
|
352
|
-
);
|
|
353
|
-
// → [{ startTime, endTime, durationMs, peakDelta, peakTime, maxSensor, minSensor }]
|
|
354
|
-
```
|
|
355
|
-
Two sensors measuring the same thing: while they agree, all is well. Beyond `maxDelta` something
|
|
356
|
-
is wrong — one is icing up, one sits in the draught, one has drifted. **`maxSensor` is the half
|
|
357
|
-
you can act on**: *Unten is 4.5 °C warmer* sends someone to the right probe, *the difference was
|
|
358
|
-
4.5 °C* does not.
|
|
359
|
-
|
|
360
|
-
`minDurationMs` drops episodes shorter than that — a single sample over the limit is noise, not an
|
|
361
|
-
incident. A timestamp with fewer than two readings has no difference and ends an episode, the way
|
|
362
|
-
a gap does. Sensors are matched by timestamp without interpolation, so bring them to the same
|
|
363
|
-
cadence first.
|
|
364
|
-
|
|
365
|
-
Draw the result as highlights, with `rotate: 'auto'` so a long label in a short episode turns
|
|
366
|
-
upright instead of spilling over the data:
|
|
367
|
-
|
|
368
|
-
```ts
|
|
369
|
-
highlights: episodes.map((e) => ({
|
|
370
|
-
startTime: e.startTime, endTime: e.endTime,
|
|
371
|
-
label: `${e.maxSensor} +${e.peakDelta.toFixed(1)} °C`,
|
|
372
|
-
labelPosition: 'top', rotate: 'auto',
|
|
373
|
-
}))
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
### A legend you can click — `legend.itemClass`
|
|
377
|
-
```ts
|
|
378
|
-
legend: { show: true, itemClass: 'clickmetotoggle' }
|
|
379
|
-
```
|
|
380
|
-
Every entry is its own group: `<g class="legend-item legend-item--<id>" id="legend-<id>">`, and
|
|
381
|
-
`<id>` is the **same key the series group carries** (`#series-<id>`, from `series.id` or the name).
|
|
382
|
-
`itemClass` adds your own class to every entry.
|
|
383
|
-
|
|
384
|
-
That is the whole feature — the library brings nothing to click *with*, it brings something to
|
|
385
|
-
click *on*:
|
|
386
|
-
|
|
387
|
-
```js
|
|
388
|
-
container.addEventListener('click', (e) => {
|
|
389
|
-
const item = e.target.closest('.clickmetotoggle');
|
|
390
|
-
if (!item) return;
|
|
391
|
-
container.querySelector('#' + item.id.replace('legend-', 'series-'))
|
|
392
|
-
.classList.toggle('hidden'); // .hidden { display: none }
|
|
393
|
-
});
|
|
394
|
-
```
|
|
395
|
-
`chart.legendItems()` returns the same key as `id`, for anyone still rendering their own legend.
|
|
396
|
-
|
|
397
|
-
### A panel behind the legend — `legend.box`
|
|
398
|
-
```ts
|
|
399
|
-
legend: { show: true, position: 'inside-right', box: {} }
|
|
400
|
-
```
|
|
401
|
-
A legend inside the plot sits **over** the data, and over a dense curve or a filled area it can be
|
|
402
|
-
unreadable. `box` puts a quiet panel under it. Like the grid, it is drawn only when you ask:
|
|
403
|
-
`box: {}` takes the defaults — white, a grey hairline, 6 px padding, 85 % opaque, so the data still
|
|
404
|
-
shows through faintly. `fill`, `stroke`, `strokeWidth`, `padding` and `opacity` override them, and
|
|
405
|
-
`stroke: 'none'` keeps the background without a border.
|
|
406
|
-
|
|
407
|
-
The panel takes the place where the entries would otherwise end, rather than pushing them around:
|
|
408
|
-
turning it on does not move the legend out over the plot edge. With an outside legend the padding
|
|
409
|
-
is reserved as real space, so the border never reaches into the plot.
|
|
410
|
-
|
|
411
|
-
There are no rounded corners — the draw-command model has no corner radius, and adding one would
|
|
412
|
-
mean changing SVG, Go and the PDF renderer for a rounding.
|
|
413
|
-
|
|
414
|
-
### Legend Placement
|
|
415
|
-
`inside-left` / `inside-right` draw over the plot, `outside-left` / `outside-right` beside it,
|
|
416
|
-
`above` / `below` as a horizontal strip that wraps into as many rows as the entries need — the
|
|
417
|
-
chart reserves exactly that much height. `separate` draws nothing and hands you
|
|
418
|
-
`chart.legendItems()` to build your own.
|
|
419
|
-
|
|
420
|
-
```ts
|
|
421
|
-
legend: { show: true, position: 'below' }, // waagerecht, bricht um
|
|
422
|
-
legend: { show: true, position: 'above', orientation: 'vertical' }, // Säule, wenn du darauf bestehst
|
|
423
|
-
```
|
|
424
|
-
|
|
425
|
-
### Tooltips & Interactive Projections
|
|
426
|
-
Attach DOM tooltips or use pixel/data coordinate projections:
|
|
427
|
-
```ts
|
|
428
|
-
// Built-in DOM tooltip:
|
|
429
|
-
const chart = mount('#chart', { series, tooltip: { show: true } });
|
|
430
|
-
|
|
431
|
-
// Interactive coordinate projections (crosshairs, zoom, scrubbers):
|
|
432
|
-
chart.renderCommands();
|
|
433
|
-
const timeMs = chart.invertTime(pixelX);
|
|
434
|
-
const value = chart.invertValue(pixelY, yAxisIndex);
|
|
435
|
-
const { x, y } = chart.project(timeMs, value, yAxisIndex);
|
|
436
|
-
|
|
437
|
-
// The plot area the scales map onto (an outside legend included):
|
|
438
|
-
const { x: px0, y: py0, width, height } = chart.plotRect;
|
|
439
|
-
// Data for your own tooltip — per visible series the point nearest in time:
|
|
440
|
-
const points = chart.pointsAt(pixelX, pixelY, { hidden: ['flow'] });
|
|
441
|
-
```
|
|
442
|
-
|
|
443
|
-
### Server-Side Rendering (Node.js)
|
|
444
|
-
Generate pure SVG strings without browser dependencies:
|
|
445
|
-
```ts
|
|
446
|
-
import { MLTimeGraph, SVGRenderer } from 'ml-time-graph';
|
|
447
|
-
import * as fs from 'node:fs';
|
|
448
|
-
|
|
449
|
-
const chart = new MLTimeGraph({ width: 800, height: 400, series });
|
|
450
|
-
const { content } = new SVGRenderer().render(chart.renderCommands());
|
|
451
|
-
fs.writeFileSync('chart.svg', content);
|
|
452
|
-
```
|
|
453
|
-
|
|
454
|
-
### Styling the SVG from CSS
|
|
455
|
-
Every group the renderer emits carries a class, and the Go port emits the same ones:
|
|
456
|
-
|
|
457
|
-
`value-axis left` / `value-axis right`, `time-axis`, `axis-labels`, `chart-grid`,
|
|
458
|
-
`chart-legend`, `series series--<slug>`, `thresholds` / `threshold threshold--<slug>`,
|
|
459
|
-
`threshold-labels` / `threshold-label threshold-label--<slug>`, `markers`, `gaps`,
|
|
460
|
-
`highlights`, `annotation-band`, `annotation-band-axis`.
|
|
461
|
-
|
|
462
|
-
On the `<path>` elements inside a series: `series-line` and `series-fill`.
|
|
463
|
-
|
|
464
|
-
The `<slug>` is `config.id ?? slugify(config.name)` — an explicit `id` verbatim, a name
|
|
465
|
-
lowercased with non-alphanumeric runs collapsed to `-`. Export `slugify` to build the
|
|
466
|
-
selector for a name you hold at runtime:
|
|
467
|
-
```ts
|
|
468
|
-
import { slugify } from 'ml-time-graph';
|
|
469
|
-
svgRoot.querySelector(`.series--${slugify(series.name)}`)?.classList.toggle('is-hidden');
|
|
470
|
-
```
|
|
471
|
-
The group also carries the same string as an `id` (`#series-temperatur`). Elements
|
|
472
|
-
*inside* a series group use a different, stable key instead: `s-<crc32 of the name>`
|
|
473
|
-
(`-line-<n>`, `-fill-r<region>-<piece>`).
|
|
474
|
-
|
|
475
|
-
**Prefer the classes.** An `id` is unique per *document*, not per chart: two charts on one
|
|
476
|
-
page produce the same key, and `getElementById` returns the first one. Scope through the
|
|
477
|
-
container instead — `#chart-2 .series--temperature .series-line`.
|
|
478
|
-
|
|
479
|
-
Full table and the provenance comment (`<!-- MLTimeGraph x.y.z -->`, the first child of
|
|
480
|
-
every `<svg>`): [USAGE.md — CSS classes in the SVG](USAGE.md#css-classes-in-the-svg).
|
|
481
|
-
|
|
482
|
-
### Aggregated Series (`showAs`)
|
|
483
|
-
Points of the shape `{ time, min, max, avg, count }` — one slot per bucket, as
|
|
484
|
-
`aggregateBySlot()` produces:
|
|
485
|
-
|
|
486
|
-
- `showAs: 'minmaxavg'` — three lines (min / max / avg) plus optional `fillToMax` /
|
|
487
|
-
`fillToMin` envelopes. **Geometrically identical to three ordinary series**; what it adds
|
|
488
|
-
is one legend entry instead of three, one series key for all three paths, and
|
|
489
|
-
`avgDashed` on by default (`tests/series/minmaxavg_drei_serien.test.ts` proves the
|
|
490
|
-
equality). Use three plain series when they should behave as three.
|
|
491
|
-
- `showAs: 'band'` — one filled rectangle per slot, min→max. No three-series equivalent.
|
|
492
|
-
|
|
493
|
-
The envelope fills are reachable from plain series too: a fill bound takes
|
|
494
|
-
`{ series: '<id or name>' }`, which spans the area between two curves. The referenced
|
|
495
|
-
curve is interpolated onto this series' timestamps, and the area **breaks where that
|
|
496
|
-
curve has a gap** — same rule as a line.
|
|
497
|
-
Its `avgLine` and `countOpacity` options are TypeScript-only so far.
|
|
498
|
-
|
|
499
|
-
### Statistic Overlays
|
|
500
|
-
`series.overlays` computes from the series' own data at render time:
|
|
501
|
-
`movingAverage` (`type: 'simple' | 'exponential'`), `movingMkt` (USP <1079.2>),
|
|
502
|
-
`stdDevBand`, `limits`. **Both libraries render all of them** — the Go port since 1.8.0.
|
|
503
|
-
|
|
504
|
-
Every window is in **milliseconds**, including the exponential one
|
|
505
|
-
(`alpha = 1 - exp(-Δt / window)`, not the count-based `2/(N+1)`).
|
|
506
|
-
|
|
507
|
-
The same numbers are available without drawing anything — `rollingMean`, `rollingEma`,
|
|
508
|
-
`rollingStdDev`, `rollingMkt` from `ml-time-graph/analyze`, `analyze.RollingMean` … from
|
|
509
|
-
`pkg/analyze` in Go. An overlay is a convenience, not a second implementation; passing the
|
|
510
|
-
pre-computed series as an ordinary one gives the identical picture, which
|
|
511
|
-
`tests/series/overlay_extern.test.ts` proves rather than claims.
|
|
512
|
-
|
|
513
|
-
### Time Zone
|
|
514
|
-
`locale` decides *how* a time is written, `timeZone` **which** time:
|
|
515
|
-
|
|
516
|
-
```ts
|
|
517
|
-
new MLTimeGraph({ locale: 'de-DE', timeZone: 'Europe/Berlin', series });
|
|
518
|
-
```
|
|
519
|
-
|
|
520
|
-
Unset means the machine's zone — fine on one machine, a trap across two: a browser sits in
|
|
521
|
-
the reader's zone, a server in UTC, and the same JSON then labels the same reading with
|
|
522
|
-
different times. Set it whenever a chart is produced in one place and read in another. Only
|
|
523
|
-
the labels move; tick positions are epoch arithmetic. Same option in Go
|
|
524
|
-
(`Options.TimeZone`, JSON `timeZone`), which embeds the zone database so it works in a
|
|
525
|
-
scratch image.
|
|
526
|
-
|
|
527
|
-
## Traps (Read Before Coding)
|
|
528
|
-
|
|
529
|
-
- **Timestamps are numbers, not Date objects:** `time` is always milliseconds since Unix epoch (`number`). Passing a `Date` object fails typechecking or evaluates to `NaN`.
|
|
530
|
-
- **Margins are fixed unless you measure them:** The chart draws into `margin` (default `{ top: 20, right: 20, bottom: 40, left: 60 }`). A right Y-axis or a formatted unit (`120.5 °C`) does not fit into `right: 20`, and a clipped number is a wrong number (`120` clipped to `20`). Use `margin: autoMargin(options)` — it measures the labels the chart actually draws (no DOM, a few ms) — or set the margins yourself.
|
|
531
|
-
- **`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.
|
|
532
|
-
- **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.
|
|
533
|
-
- **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.
|
|
534
|
-
- **A built-in legend carries no behaviour, but it is reachable.** Until 1.12.0 this entry said a
|
|
535
|
-
built-in legend was "non-interactive" and that toggling meant `position: 'separate'` and drawing
|
|
536
|
-
it yourself. That is no longer true: every entry is its own group with the same stable key as its
|
|
537
|
-
series (`#legend-<id>` → `#series-<id>`), and `legend.itemClass` adds your own hook. The library
|
|
538
|
-
still ships no click handler — you write one, and one on the chart is enough. `'separate'` remains
|
|
539
|
-
the way when the legend belongs in HTML, or needs to do more than toggle.
|
|
540
|
-
- **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.
|
|
541
|
-
- **One option is still accepted and does nothing:** `series.sensorType` is in the types and the parser reads it, but no renderer acts on it — in either library. Setting it to `'enum'` does not label the axis; that is `series.enumMap`, which 1.10.0 wired up. Until 1.10.0 this list also held `ticks.minor`, `grid.minor` and `enumMap` itself: three fields the types promised and both renderers swallowed. Nothing warns you; the picture just stays as it was.
|
|
542
|
-
- **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.
|
|
543
|
-
- **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.
|
|
14
|
+
This file stays as a pointer so that links to `SKILLS.md` keep working.
|
package/USAGE.md
CHANGED
|
@@ -1070,8 +1070,20 @@ in the Go port (`PointsAt`):
|
|
|
1070
1070
|
`overlayIndex` and `overlayKind`; a band gives `low`/`high` and its mean as `value`. Off by
|
|
1071
1071
|
default — an overlay is no measurement. `limits` never count.
|
|
1072
1072
|
|
|
1073
|
-
`chart.warnings()` lists configuration hints —
|
|
1074
|
-
|
|
1073
|
+
`chart.warnings()` lists configuration hints — things that do not stop the chart but make it
|
|
1074
|
+
look different from what you meant. Same texts in Go (`Warnings()`):
|
|
1075
|
+
|
|
1076
|
+
- two series sharing a `seriesId`, which `hidden` and `pointsAt` cannot tell apart;
|
|
1077
|
+
- a threshold name in `colorByThresholds` or a fill bound that matches no threshold (looked up
|
|
1078
|
+
by `id`, else `name` — a threshold with an `id` is not found by its name), or a fill bound
|
|
1079
|
+
naming an unknown series;
|
|
1080
|
+
- `side` on a fill region that is not "line against a fixed bound", and `outer` without `side`
|
|
1081
|
+
— both are ignored there;
|
|
1082
|
+
- `where` on a smoothing that can overshoot (`catmull-rom`, `akima`);
|
|
1083
|
+
- deprecated settings (`style: "label"` on a gap, `series.style.gap.label`, `series.sensorType`
|
|
1084
|
+
other than `"numeric"`).
|
|
1085
|
+
|
|
1086
|
+
Call it while you author a chart, or log it once; it costs a pass over the options.
|
|
1075
1087
|
|
|
1076
1088
|
### Zoom / pan
|
|
1077
1089
|
|
|
@@ -1240,6 +1252,19 @@ The prefix is stable as long as you pass the same one — do not generate it per
|
|
|
1240
1252
|
Go port takes the same `idPrefix` in its JSON options (server and CLI apply it to the defs
|
|
1241
1253
|
as well). `ml-time-graph-svelte` gives every `<TimeGraph>` its own prefix automatically.
|
|
1242
1254
|
|
|
1255
|
+
**`classPrefix` keeps the host page's CSS out.** The chart's classes are plain words —
|
|
1256
|
+
`series`, `time-axis`, `chart-grid` — and a rule like `.series { display: none }` in the
|
|
1257
|
+
page's stylesheet would hit them. Unlike `idPrefix` it is the same for every chart of an app:
|
|
1258
|
+
|
|
1259
|
+
```ts
|
|
1260
|
+
const chart = new MLTimeGraph({ ...options, classPrefix: 'mltg' });
|
|
1261
|
+
const svg = new SVGRenderer({ classPrefix: 'mltg' }).render(chart.renderCommands()).content;
|
|
1262
|
+
// → class="mltg-series mltg-series--temperature"; legend.itemClass stays as you wrote it
|
|
1263
|
+
```
|
|
1264
|
+
|
|
1265
|
+
Select with the prefix then (`.mltg-series--temperature .mltg-series-line`). The Go port takes
|
|
1266
|
+
`classPrefix` in its JSON options; server and CLI apply it.
|
|
1267
|
+
|
|
1243
1268
|
### Provenance
|
|
1244
1269
|
|
|
1245
1270
|
Every SVG names the library and version that drew it, as the first child of the root:
|