-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSampleLineChart.cs
More file actions
403 lines (339 loc) · 12.5 KB
/
SampleLineChart.cs
File metadata and controls
403 lines (339 loc) · 12.5 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using FishUI;
using FishUI.Controls;
using System;
using System.Numerics;
namespace FishUIDemos
{
/// <summary>
/// Demonstrates the LineChart control for real-time data visualization.
/// Shows sine wave with random noise across multiple data series.
/// </summary>
public class SampleLineChart : ISample
{
FishUI.FishUI FUI;
LineChart chart;
LineChartSeries sineWaveSeries;
LineChartSeries noisySeries;
LineChartSeries randomSeries;
LineChartSeries slowSeries;
Label cursorValueLabel;
Random random = new Random();
float time = 0f;
public string Name => "LineChart";
public TakeScreenshotFunc TakeScreenshot { get; set; }
public FishUI.FishUI CreateUI(FishUISettings UISettings, IFishUIGfx Gfx, IFishUIInput Input, IFishUIEvents Events)
{
FUI = new FishUI.FishUI(UISettings, Gfx, Input, Events);
FUI.Init();
FishUITheme theme = UISettings.LoadTheme(ThemePreferences.LoadThemePath(), applyImmediately: true);
return FUI;
}
public void Init()
{
// === Title ===
Label titleLabel = new Label("LineChart Control Demo");
titleLabel.Position = new Vector2(20, 20);
titleLabel.Size = new Vector2(300, 30);
titleLabel.Alignment = Align.Left;
FUI.AddControl(titleLabel);
// Screenshot button
ImageRef iconCamera = FUI.Graphics.LoadImage("data/silk_icons/camera.png");
Button screenshotBtn = new Button();
screenshotBtn.Icon = iconCamera;
screenshotBtn.Position = new Vector2(330, 20);
screenshotBtn.Size = new Vector2(30, 30);
screenshotBtn.IsImageButton = true;
screenshotBtn.TooltipText = "Take a screenshot";
screenshotBtn.OnButtonPressed += (btn, mbtn, pos) => TakeScreenshot?.Invoke(Name);
FUI.AddControl(screenshotBtn);
// === Main Chart ===
Label chartLabel = new Label("Real-Time Data Visualization");
chartLabel.Position = new Vector2(20, 60);
chartLabel.Size = new Vector2(400, 24);
chartLabel.Alignment = Align.Left;
FUI.AddControl(chartLabel);
chart = new LineChart();
chart.Position = new Vector2(20, 90);
chart.Size = new Vector2(760, 250);
chart.MinValue = -1.5f;
chart.MaxValue = 1.5f;
chart.TimeWindow = 10f;
chart.AutoScroll = true;
chart.HorizontalGridDivisions = 6;
chart.VerticalGridDivisions = 10;
chart.YAxisLabelFormat = "F1";
chart.XAxisLabelFormat = "F2";
chart.ShowCursor = true;
chart.CursorColor = new FishColor(255, 50, 50, 200);
FUI.AddControl(chart);
// Add data series
sineWaveSeries = chart.AddSeries("Sine Wave", new FishColor(0, 200, 100, 255));
sineWaveSeries.LineThickness = 2f;
noisySeries = chart.AddSeries("Noisy Sine", new FishColor(200, 100, 0, 255));
noisySeries.LineThickness = 1.5f;
randomSeries = chart.AddSeries("Random Walk", new FishColor(100, 150, 255, 255));
randomSeries.LineThickness = 1.5f;
slowSeries = chart.AddSeries("Slow (1.5s)", new FishColor(255, 200, 50, 255));
slowSeries.LineThickness = 2.5f;
// === Timeline Control (between chart and legend) ===
Label timelineLabel = new Label("Timeline (drag to navigate when paused)");
timelineLabel.Position = new Vector2(20, 350);
timelineLabel.Size = new Vector2(300, 20);
timelineLabel.Alignment = Align.Left;
FUI.AddControl(timelineLabel);
Timeline timeline = new Timeline();
timeline.Position = new Vector2(20, 375);
timeline.Size = new Vector2(760, 40);
timeline.MinTime = 0f;
timeline.MaxTime = 100f;
timeline.SetView(0f, 10f);
timeline.MajorTickCount = 10;
timeline.LabelFormat = "F0";
timeline.OnViewChanged += (sender, args) =>
{
// Sync main chart's view when timeline changes
chart.TimeWindow = args.ViewWidth;
chart.ViewStart = args.ViewStart;
};
FUI.AddControl(timeline);
this.timeline = timeline;
// === Legend ===
Label legendLabel = new Label("Legend:");
legendLabel.Position = new Vector2(20, 420);
legendLabel.Size = new Vector2(100, 20);
legendLabel.Alignment = Align.Left;
FUI.AddControl(legendLabel);
// Sine wave legend
Panel sineLegendColor = new Panel();
sineLegendColor.Position = new Vector2(80, 423);
sineLegendColor.Size = new Vector2(15, 10);
sineLegendColor.Color = sineWaveSeries.Color;
FUI.AddControl(sineLegendColor);
Label sineLegendLabel = new Label("Sine");
sineLegendLabel.Position = new Vector2(100, 418);
sineLegendLabel.Size = new Vector2(50, 20);
sineLegendLabel.Alignment = Align.Left;
FUI.AddControl(sineLegendLabel);
// Noisy sine legend
Panel noisyLegendColor = new Panel();
noisyLegendColor.Position = new Vector2(150, 423);
noisyLegendColor.Size = new Vector2(15, 10);
noisyLegendColor.Color = noisySeries.Color;
FUI.AddControl(noisyLegendColor);
Label noisyLegendLabel = new Label("Noisy");
noisyLegendLabel.Position = new Vector2(170, 418);
noisyLegendLabel.Size = new Vector2(50, 20);
noisyLegendLabel.Alignment = Align.Left;
FUI.AddControl(noisyLegendLabel);
// Random walk legend
Panel randomLegendColor = new Panel();
randomLegendColor.Position = new Vector2(225, 423);
randomLegendColor.Size = new Vector2(15, 10);
randomLegendColor.Color = randomSeries.Color;
FUI.AddControl(randomLegendColor);
Label randomLegendLabel = new Label("Random");
randomLegendLabel.Position = new Vector2(245, 418);
randomLegendLabel.Size = new Vector2(60, 20);
randomLegendLabel.Alignment = Align.Left;
FUI.AddControl(randomLegendLabel);
// Slow series legend
Panel slowLegendColor = new Panel();
slowLegendColor.Position = new Vector2(310, 423);
slowLegendColor.Size = new Vector2(15, 10);
slowLegendColor.Color = slowSeries.Color;
FUI.AddControl(slowLegendColor);
Label slowLegendLabel = new Label("Slow(1.5s)");
slowLegendLabel.Position = new Vector2(330, 418);
slowLegendLabel.Size = new Vector2(80, 20);
slowLegendLabel.Alignment = Align.Left;
FUI.AddControl(slowLegendLabel);
// Cursor values display
Label cursorLabel = new Label("Cursor: Click chart to select");
cursorLabel.Position = new Vector2(430, 418);
cursorLabel.Size = new Vector2(350, 20);
cursorLabel.Alignment = Align.Left;
FUI.AddControl(cursorLabel);
cursorValueLabel = cursorLabel;
chart.OnCursorMoved += (sender, args) =>
{
string text = $"T={args.Time:F1}s";
foreach (var kv in args.Values)
{
if (kv.Value.HasValue)
text += $" {kv.Key.Name[0]}={kv.Value.Value:F2}";
}
cursorValueLabel.Text = text;
};
// === Controls Section ===
Label controlsLabel = new Label("Chart Settings:");
controlsLabel.Position = new Vector2(20, 445);
controlsLabel.Size = new Vector2(150, 24);
controlsLabel.Alignment = Align.Left;
FUI.AddControl(controlsLabel);
// Time window slider
Label timeWindowLabel = new Label("Time Window:");
timeWindowLabel.Position = new Vector2(20, 475);
timeWindowLabel.Size = new Vector2(100, 20);
timeWindowLabel.Alignment = Align.Left;
FUI.AddControl(timeWindowLabel);
Slider timeWindowSlider = new Slider();
timeWindowSlider.Position = new Vector2(130, 475);
timeWindowSlider.Size = new Vector2(150, 20);
timeWindowSlider.MinValue = 5f;
timeWindowSlider.MaxValue = 30f;
timeWindowSlider.Value = chart.TimeWindow;
timeWindowSlider.OnValueChanged += (slider, val) => chart.TimeWindow = val;
FUI.AddControl(timeWindowSlider);
Label timeWindowValueLabel = new Label($"{chart.TimeWindow:F0}s");
timeWindowValueLabel.Position = new Vector2(290, 475);
timeWindowValueLabel.Size = new Vector2(50, 20);
timeWindowValueLabel.Alignment = Align.Left;
timeWindowSlider.OnValueChanged += (slider, val) => timeWindowValueLabel.Text = $"{val:F0}s";
FUI.AddControl(timeWindowValueLabel);
// Pause/Resume button
Button pauseBtn = new Button();
pauseBtn.Text = "Pause";
pauseBtn.Position = new Vector2(360, 470);
pauseBtn.Size = new Vector2(70, 30);
pauseBtn.OnButtonPressed += (btn, mbtn, pos) =>
{
chart.IsPaused = !chart.IsPaused;
pauseBtn.Text = chart.IsPaused ? "Resume" : "Pause";
// Toggle auto-scroll: when paused, allow manual navigation via timeline
chart.AutoScroll = !chart.IsPaused;
if (chart.IsPaused)
{
// Set ViewStart to current view position when pausing
chart.ViewStart = chart.CurrentTime - chart.TimeWindow;
}
};
FUI.AddControl(pauseBtn);
// Clear button
Button clearBtn = new Button();
clearBtn.Text = "Clear";
clearBtn.Position = new Vector2(435, 470);
clearBtn.Size = new Vector2(70, 30);
clearBtn.OnButtonPressed += (btn, mbtn, pos) =>
{
chart.ClearAllData();
time = 0f;
chart.CurrentTime = 0f;
randomWalkValue = 0f;
};
FUI.AddControl(clearBtn);
// Grid toggle
CheckBox showGridCheckbox = new CheckBox("Show Grid");
showGridCheckbox.Position = new Vector2(520, 475);
showGridCheckbox.Size = new Vector2(15, 15);
showGridCheckbox.IsChecked = true;
showGridCheckbox.OnCheckedChanged += (cb, chk) =>
{
chart.ShowHorizontalGrid = chk;
chart.ShowVerticalGrid = chk;
};
FUI.AddControl(showGridCheckbox);
// Labels toggle
CheckBox showLabelsCheckbox = new CheckBox("Show Labels");
showLabelsCheckbox.Position = new Vector2(620, 475);
showLabelsCheckbox.Size = new Vector2(15, 15);
showLabelsCheckbox.IsChecked = true;
showLabelsCheckbox.OnCheckedChanged += (cb, chk) =>
{
chart.ShowXAxisLabels = chk;
chart.ShowYAxisLabels = chk;
};
FUI.AddControl(showLabelsCheckbox);
// === Second Chart - Temperature Style ===
Label tempLabel = new Label("Temperature Monitor Style");
tempLabel.Position = new Vector2(20, 510);
tempLabel.Size = new Vector2(200, 24);
tempLabel.Alignment = Align.Left;
FUI.AddControl(tempLabel);
LineChart tempChart = new LineChart();
tempChart.Position = new Vector2(20, 540);
tempChart.Size = new Vector2(760, 120);
tempChart.MinValue = 20f;
tempChart.MaxValue = 80f;
tempChart.TimeWindow = 60f;
tempChart.AutoScroll = true;
tempChart.HorizontalGridDivisions = 4;
tempChart.VerticalGridDivisions = 6;
tempChart.YAxisLabelFormat = "F0";
tempChart.XAxisLabelFormat = "F0";
tempChart.BackgroundColor = new FishColor(20, 20, 40, 255);
tempChart.GridColor = new FishColor(40, 40, 80, 255);
FUI.AddControl(tempChart);
var cpuTemp = tempChart.AddSeries("CPU", new FishColor(255, 100, 100, 255));
var gpuTemp = tempChart.AddSeries("GPU", new FishColor(100, 255, 100, 255));
// Store reference for update
this.tempChart = tempChart;
this.cpuTemp = cpuTemp;
this.gpuTemp = gpuTemp;
}
Timeline timeline;
LineChart tempChart;
LineChartSeries cpuTemp;
LineChartSeries gpuTemp;
float randomWalkValue = 0f;
float tempTime = 0f;
float sampleTime = 0f;
float slowTime = 0f;
float slowValue = 0f;
public void Update(float Dt)
{
time += Dt;
tempTime += Dt;
sampleTime += Dt;
slowTime += Dt;
// Update timeline to track current time (only auto-scroll when not paused)
timeline.MaxTime = Math.Max(timeline.MaxTime, time + 5f);
if (!chart.IsPaused)
{
timeline.SetViewToEnd(chart.TimeWindow);
}
// Update main chart
chart.Update(Dt);
// Add data points at regular intervals (20 samples per second)
float sampleInterval = 0.05f;
if (sampleTime >= sampleInterval)
{
sampleTime = 0f;
// Generate sine wave
float sineValue = (float)Math.Sin(time * 2.0);
sineWaveSeries.AddPoint(time, sineValue);
// Generate noisy sine
float noise = (float)(random.NextDouble() - 0.5) * 0.4f;
noisySeries.AddPoint(time, sineValue + noise);
// Generate random walk
randomWalkValue += (float)(random.NextDouble() - 0.5) * 0.1f;
randomWalkValue = Math.Clamp(randomWalkValue, -1.2f, 1.2f);
randomSeries.AddPoint(time, randomWalkValue);
}
// Slow series - update every 1.5 seconds for interpolation testing
if (slowTime >= 1.5f)
{
slowTime = 0f;
slowValue = (float)Math.Sin(time * 0.5) * 0.8f + (float)(random.NextDouble() - 0.5) * 0.3f;
slowSeries.AddPoint(time, slowValue);
}
// Update temperature chart (slower updates)
tempChart.Update(Dt);
if (tempTime >= 0.5f) // Update every 0.5 seconds
{
tempTime = 0f;
// Simulate CPU temperature (fluctuates around 50-60)
float cpu = 55f + (float)(random.NextDouble() - 0.5) * 20f;
cpuTemp.AddPoint(tempChart.CurrentTime, cpu);
// Simulate GPU temperature (fluctuates around 60-70)
float gpu = 65f + (float)(random.NextDouble() - 0.5) * 15f;
gpuTemp.AddPoint(tempChart.CurrentTime, gpu);
}
}
public void Draw(float Dt, float Time)
{
}
public void Dispose()
{
}
}
}