4using System.ComponentModel;
17 public class GraphModel : INotifyPropertyChanged, IDisposable
19 private readonly SortedDictionary<DateTime, Field> fieldValues = [];
20 private readonly SortedDictionary<DateTime, Field> minFieldValues = [];
21 private readonly SortedDictionary<DateTime, Field> maxFieldValues = [];
22 private readonly
string fieldName;
23 private Timer? timer =
null;
24 private ImageSource? image =
null;
45 public ImageSource?
Image => this.image;
50 public bool HasImage => this.image is not
null;
58 lock (this.fieldValues)
72 lock (this.fieldValues)
86 lock (this.fieldValues)
94 private void Invalidate()
96 this.timer?.Dispose();
99 this.timer =
new Timer(this.GenerateGraph,
null, 500, Timeout.Infinite);
102 private async
void GenerateGraph(
object? _)
111 lock (this.fieldValues)
113 int c = this.fieldValues.Count;
114 DateTime[] Timepoints =
new DateTime[c];
117 this.fieldValues.Keys.CopyTo(Timepoints, 0);
118 this.fieldValues.Values.CopyTo(Values, 0);
123 if (this.minFieldValues.Count > 0)
125 c = this.minFieldValues.Count;
127 Timepoints =
new DateTime[c];
128 Values =
new Field[c];
130 this.minFieldValues.Keys.CopyTo(Timepoints, 0);
131 this.minFieldValues.Values.CopyTo(Values, 0);
139 if (this.maxFieldValues.Count > 0)
141 c = this.maxFieldValues.Count;
143 Timepoints =
new DateTime[c];
144 Values =
new Field[c];
146 this.maxFieldValues.Keys.CopyTo(Timepoints, 0);
147 this.maxFieldValues.Values.CopyTo(Values, 0);
156 StringBuilder sb =
new();
159 sb.AppendLine(
"y:=y.Value;");
160 sb.AppendLine(
"G:=scatter2d(x,y,'Black',7);");
164 sb.AppendLine(
"yMin:=yMin.Value;");
165 sb.AppendLine(
"G+=scatter2d(xMin,yMin,'Black',7);");
170 sb.AppendLine(
"yMax:=yMax.Value;");
171 sb.AppendLine(
"G+=scatter2d(xMax,yMax,'Black',7);");
175 sb.AppendLine(
"G+=plot2dline(xMin,yMin,'Blue',5);");
178 sb.AppendLine(
"G+=plot2dline(xMax,yMax,'Red',5);");
180 sb.AppendLine(
"G+=plot2dline(x,y,'Green',5);");
181 sb.Append(
"G.LabelX:='");
184 sb.Append(
"G.LabelY:='");
186 foreach (
Field F
in this.fieldValues.Values)
197 sb.Append(this.fieldName.Replace(
"\\",
"\\\\").Replace(
"\n",
"\\n").Replace(
"\r",
"\\r").Replace(
"\t",
"\\t").Replace(
"'",
"\\'"));
206 if (
Graph is not
null)
212 AxisColor = SKColors.Black,
213 BackgroundColor = SKColors.White,
214 GridColor = SKColors.LightGray,
224 MainThread.BeginInvokeOnMainThread(() =>
226 bool OldWasNull = this.image is
null;
227 this.image = ImageSource.FromStream(() =>
new MemoryStream(Png));
229 this.RaisePropertyChanged(nameof(this.Image));
231 this.RaisePropertyChanged(nameof(this.HasImage));
241 private void RaisePropertyChanged(
string Name)
245 this.
PropertyChanged?.Invoke(
this,
new PropertyChangedEventArgs(Name));
259 GC.SuppressFinalize(
this);
265 protected virtual void Dispose(
bool disposing)
267 this.timer?.Dispose();
Base class that references services in the app.
static IStringLocalizer Localizer
Localization service
Represents a set of historical field values.
void Add(Field Field)
Adds a historical field value.
void Dispose()
IDisposable.Dispose
virtual void Dispose(bool disposing)
IDisposable.Dispose
ImageSource? Image
Image Source
string FieldName
Field Name
void AddMax(Field Field)
Adds a historical maximum field value.
PropertyChangedEventHandler? PropertyChanged
Event raised when a property has changed.
void AddMin(Field Field)
Adds a historical minimum field value.
GraphModel(Field Field)
Represents a set of historical field values.
bool HasImage
If graph has an image.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Class managing a script expression.
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
PixelInformation CreatePixels()
Creates a bitmap of the graph.
Base class for all sensor data fields.
string Name
Unlocalized field name.
DateTime Timestamp
Timestamp of field value.
Represents a physical quantity value.