summaryrefslogtreecommitdiff
path: root/src/timeseries-plot.js
blob: e35a7045d72f83576853c79334f185bf1c5d74f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { Container, Graphics, Text } from 'pixi.js';

/**
 * TimeSeriesPlot - Pure visualization component for time-series data
 *
 * This class is responsible ONLY for displaying data, not generating it.
 * It receives data points from external sources and renders them as a
 * scrolling waterfall display.
 *
 * Architecture:
 * - TimeSeriesPlot: Displays data (this file)
 * - DataSource: Generates/provides data (data-sources.js)
 * - Connection: Links sources to plots
 */
export class TimeSeriesPlot {
    constructor(config) {
        this.x = config.x || 0;
        this.y = config.y || 0;
        this.width = config.width || 800;
        this.height = config.height || 600;
        this.title = config.title || 'Time Series';
        this.baseColor = config.color || 0xff6666;

        // Container for all graphics
        this.container = new Container();
        this.container.x = this.x;
        this.container.y = this.y;

        // Graphics layers (order matters for rendering)
        this.gridGraphics = new Graphics();
        this.linesGraphics = new Graphics();
        this.borderGraphics = new Graphics();

        this.container.addChild(this.gridGraphics);
        this.container.addChild(this.linesGraphics);
        this.container.addChild(this.borderGraphics);

        // Title
        this.titleText = new Text({
            text: this.title,
            style: {
                fontFamily: 'Arial',
                fontSize: 18,
                fill: 0xeeeeee,
            }
        });
        this.titleText.x = 10;
        this.titleText.y = 10;
        this.container.addChild(this.titleText);

        // Display state
        this.lines = []; // Array of {points, yOffset, color, metadata}
        this.maxLines = config.maxLines || 100;
        this.showGrid = config.showGrid !== false;

        // Scrolling and scaling
        this.scrollSpeed = config.scrollSpeed || 1.0;
        this.verticalScale = config.verticalScale || 1.0;

        // Initial draw
        this.draw();
    }

    // ========================================================================
    // Data Input API - This is how external sources send data to the plot
    // ========================================================================

    /**
     * Add a new line of data to the plot
     * @param {Array<{x: number, y: number}>} points - Array of points
     * @param {Object} metadata - Optional metadata (color, timestamp, etc.)
     */
    addLine(points, metadata = {}) {
        const line = {
            points: points,
            yOffset: 0,
            color: metadata.color || this.generateColor(Date.now() / 1000),
            timestamp: metadata.timestamp || Date.now(),
            metadata: metadata,
        };

        this.lines.push(line);

        // Limit number of lines
        if (this.lines.length > this.maxLines) {
            this.lines.shift();
        }
    }

    /**
     * Add a single data point (will be buffered into a line)
     * This is useful for streaming real-time data
     * @param {number} timestamp - Time of the data point
     * @param {number} value - Value at this time
     */
    addDataPoint(timestamp, value) {
        // For now, this creates a single-point line
        // In a more sophisticated version, this could buffer points
        // until a full line is ready
        const point = { x: this.width / 2, y: value };
        this.addLine([point], { timestamp });
    }

    /**
     * Clear all data from the plot
     */
    clearData() {
        this.lines = [];
        this.drawLines();
    }

    // ========================================================================
    // Update and Rendering
    // ========================================================================

    /**
     * Update the plot - called each frame
     * This handles scrolling and cleanup, but NOT data generation
     */
    update() {
        // Scroll existing lines down
        this.scrollLines();

        // Remove off-screen lines
        this.lines = this.lines.filter(line => {
            const scaledOffset = line.yOffset * this.verticalScale;
            return scaledOffset < this.height + 50;
        });

        // Redraw
        this.drawLines();
    }

    scrollLines() {
        this.lines.forEach(line => {
            line.yOffset += this.scrollSpeed;
        });
    }

    draw() {
        this.drawBorder();
        this.drawGrid();
        this.drawLines();
    }

    drawBorder() {
        this.borderGraphics.clear();
        this.borderGraphics.rect(0, 0, this.width, this.height);
        this.borderGraphics.stroke({ width: 2, color: 0x606070 });
    }

    drawGrid() {
        this.gridGraphics.clear();

        if (!this.showGrid) return;

        this.gridGraphics.alpha = 0.3;

        const divisions = 10;
        const color = 0x4a7a9a;

        // Vertical lines
        for (let i = 0; i <= divisions; i++) {
            const x = (i / divisions) * this.width;
            this.gridGraphics.moveTo(x, 0);
            this.gridGraphics.lineTo(x, this.height);
            this.gridGraphics.stroke({ width: 1, color });
        }

        // Horizontal lines
        for (let i = 0; i <= divisions; i++) {
            const y = (i / divisions) * this.height;
            this.gridGraphics.moveTo(0, y);
            this.gridGraphics.lineTo(this.width, y);
            this.gridGraphics.stroke({ width: 1, color });
        }
    }

    drawLines() {
        this.linesGraphics.clear();

        for (const line of this.lines) {
            if (line.points.length < 2) continue;

            // Apply vertical scale to y positions
            const scaledYOffset = line.yOffset * this.verticalScale;

            // Start path
            const firstPoint = line.points[0];
            this.linesGraphics.moveTo(firstPoint.x, firstPoint.y + scaledYOffset);

            // Draw line strip
            for (let i = 1; i < line.points.length; i++) {
                const point = line.points[i];
                this.linesGraphics.lineTo(point.x, point.y + scaledYOffset);
            }

            this.linesGraphics.stroke({ width: 2, color: line.color });
        }
    }

    generateColor(time) {
        // Cycle through colors based on time
        const hue = (time * 0.1) % 1.0;
        const r = Math.floor(Math.abs(Math.sin(hue * Math.PI * 2)) * 255);
        const g = Math.floor(Math.abs(Math.sin((hue + 0.33) * Math.PI * 2)) * 255);
        const b = Math.floor(Math.abs(Math.sin((hue + 0.66) * Math.PI * 2)) * 255);

        return (r << 16) | (g << 8) | b;
    }

    // ========================================================================
    // Configuration and Control
    // ========================================================================

    setGridVisible(visible) {
        this.showGrid = visible;
        this.drawGrid();
    }

    setScrollSpeed(speed) {
        this.scrollSpeed = Math.max(0.1, Math.min(10.0, speed));
    }

    setVerticalScale(scale) {
        this.verticalScale = Math.max(0.2, Math.min(3.0, scale));
    }

    setTitle(title) {
        this.title = title;
        this.titleText.text = title;
    }

    resize(x, y, width, height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;

        this.container.x = x;
        this.container.y = y;

        this.draw();
    }

    // ========================================================================
    // Statistics and Debugging
    // ========================================================================

    getVertexCount() {
        return this.lines.reduce((sum, line) => sum + line.points.length, 0);
    }

    getLineCount() {
        return this.lines.length;
    }

    getOldestTimestamp() {
        if (this.lines.length === 0) return null;
        return Math.min(...this.lines.map(l => l.timestamp));
    }

    getNewestTimestamp() {
        if (this.lines.length === 0) return null;
        return Math.max(...this.lines.map(l => l.timestamp));
    }

    getStats() {
        return {
            lineCount: this.getLineCount(),
            vertexCount: this.getVertexCount(),
            oldestTimestamp: this.getOldestTimestamp(),
            newestTimestamp: this.getNewestTimestamp(),
            timeSpan: this.getNewestTimestamp() - this.getOldestTimestamp(),
        };
    }
}