-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSimpleRxChart.linq
76 lines (65 loc) · 2.51 KB
/
SimpleRxChart.linq
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
<Query Kind="VBProgram">
<Reference><RuntimeDirectory>\System.Windows.Forms.DataVisualization.dll</Reference>
<Reference><RuntimeDirectory>\System.Windows.Forms.dll</Reference>
<NuGetReference>System.Reactive</NuGetReference>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Windows.Forms.DataVisualization.Charting</Namespace>
</Query>
Sub Main
dim chart As New Chart()
dim ca As New ChartArea()
chart.ChartAreas.Add(ca)
Dim values = From val In GetSensor().
GroupBy(Function(s) GetOrCreateSeries(chart, s.SensorType)).
Select(Function(s) New With {.series = s.Key, s})
values.Subscribe(Sub(valObs)
valObs.s.Subscribe(Sub(val) valObs.series.Points.AddXY(val.TimeStamp, val.SensorValue))
End Sub)
' dim series As New Series With {
' .ChartType = SeriesChartType.Line,
' .XValueType = ChartValueType.Time}
'
' chart.Series.Add(series)
' values.Subscribe(Sub(val)
' series.Points.AddXY(val.TimeStamp, val.SensorValue)
' end sub)
chart.Dump("Chart")
End Sub
' Define other methods and classes here
' Define other methods and classes here
Public Class SensorInfo
Public Property TimeStamp As DateTime
Public Property SensorType As String
Public Property SensorValue As Double
public overrides Function ToString() as String
Return String.Format("Time: {0} , Type: {1} Value: {2}", TimeStamp, SensorType, SensorValue)
End Function
End Class
Public Function GetSensor() as IObservable(Of SensorInfo)
Dim rnd = new Random(Date.Now.Millisecond)
Return Observable.Generate(Of Int16, SensorInfo)( _
initialState:=0,
condition:=Function(x) True,
iterate:= Function(inVal) inval,
resultSelector:= Function(x)
Dim randValue = rnd.NextDouble
return new SensorInfo with {.SensorType = (math.Floor(randValue * 4) + 1).ToString,
.SensorValue = randValue * 20,
.TimeStamp = DateTime.Now}
end Function,
timeSelector := Function(x) TimeSpan.FromMilliseconds(rnd.NextDouble * 500)
)
End Function
private ChartSeries As New Dictionary(of string, Series)
public function GetOrCreateSeries(target as Chart, seriesKey as String)
Dim currentSeries as Series
If ChartSeries.ContainsKey(seriesKey)
currentSeries = ChartSeries(seriesKey)
else
currentSeries = New Series With {
.ChartType = SeriesChartType.Line,
.XValueType = ChartValueType.Time}
target.Series.Add(currentSeries)
End If
return currentSeries
End Function