Mqtt-Dashboard/Mqtt-Dashboard/Tracings/Graph.cs
BlubbFish 90e662458e [NF] Neue Anzeige hinzugefügt
[BF] Graph zeichnet nun nur noch die eingestelle Anzahl an punkten
2017-08-10 15:10:18 +00:00

88 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Dashboard.Sensor;
using System.Linq;
namespace Dashboard.Tracings {
class Graph : ATracings, IDisposable {
private Series series;
private Chart chart;
private Queue<Tuple<DateTime, float>> hist = new Queue<Tuple<DateTime, float>>();
private Int32 Chart_Items;
public Graph(ASensor sensor, Dictionary<String, String> settings) : base(sensor, settings) {
this.Chart_Items = (settings.Keys.Contains("items")) ? Int32.Parse(settings["items"]) : 1000;
}
public override Panel GetPanel() {
Panel panel = new Panel();
this.chart = new Chart();
((System.ComponentModel.ISupportInitialize)(this.chart)).BeginInit();
ChartArea chartArea = new ChartArea();
chartArea.AxisX.LabelStyle.Enabled = false;
chartArea.AxisX.LineColor = System.Drawing.Color.Gainsboro;
chartArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.Gainsboro;
chartArea.AxisX.MinorGrid.Enabled = true;
chartArea.AxisX.MinorGrid.LineColor = System.Drawing.Color.Gainsboro;
chartArea.AxisX.MinorGrid.LineDashStyle = ChartDashStyle.Dot;
chartArea.AxisY.LineColor = System.Drawing.Color.Gainsboro;
chartArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.Gainsboro;
chartArea.AxisY.MinorGrid.Enabled = true;
chartArea.AxisY.MinorGrid.LineColor = System.Drawing.Color.Gainsboro;
chartArea.AxisY.MinorGrid.LineDashStyle = ChartDashStyle.Dot;
this.chart.ChartAreas.Add(chartArea);
this.series = new Series() {
ChartType = SeriesChartType.Line,
XValueType = ChartValueType.DateTime,
Color = System.Drawing.Color.Red
};
if (this.sensor.Datatypes == Sensor.ASensor.Types.Bool) {
this.series.YValueType = ChartValueType.Int32;
} else if (this.sensor.Datatypes == Sensor.ASensor.Types.Int) {
this.series.YValueType = ChartValueType.Int32;
} else if (this.sensor.Datatypes == Sensor.ASensor.Types.Float) {
this.series.YValueType = ChartValueType.Single;
}
this.chart.Series.Add(this.series);
this.chart.Location = new System.Drawing.Point(0, 0);
this.chart.Size = new System.Drawing.Size(200, 100);
panel.Controls.Add(this.chart);
((System.ComponentModel.ISupportInitialize)(this.chart)).EndInit();
panel.Size = new System.Drawing.Size(200, 100);
return panel;
}
protected override void SensorUpdate(Object sender, EventArgs e) {
switch(this.sensor.Datatypes) {
case ASensor.Types.Bool: this.hist.Enqueue(new Tuple<DateTime, float>(this.sensor.Timestamp, (this.sensor.GetBool) ? 1 : 0)); break;
case ASensor.Types.Int: this.hist.Enqueue(new Tuple<DateTime, float>(this.sensor.Timestamp, this.sensor.GetInt)); break;
case ASensor.Types.Float: this.hist.Enqueue(new Tuple<DateTime, float>(this.sensor.Timestamp, this.sensor.GetFloat)); break;
}
if(this.hist.Count > this.Chart_Items) {
this.hist.Dequeue();
}
if (this.series != null) {
this.chart.BeginInvoke((MethodInvoker)delegate {
this.series.Points.Clear();
try {
foreach(Tuple<DateTime, float> temp in this.hist) {
this.series.Points.AddXY(temp.Item1, temp.Item2);
}
} catch(Exception) { };
});
}
}
public void Dispose() {
this.chart.Series.Clear();
this.series = null;
this.hist.Clear();
this.hist = null;
this.chart.Dispose();
}
}
}