当我遇到对数X轴问题时,我正在使用Visual Studio绘制数据图表。我现在转向OxyPlot,然而,我遇到了很大的问题,我的数据填充。我从Windows窗体切换到WPF,因此这可能是绑定(不确定)的XAML问题。基本上我在VB.net中采用他们非常有限的例子,并且可以轻松地重现它。我正在努力的是使用OxyPlot重现动态绘图。到目前为止,这是我的代码:
示例:(http://docs.oxyplot.org/en/latest/getting-started/hello-wpf-vb.html)
Public Sub New()
Model = New PlotModel()
Model.Title = "Simple example"
Model.Subtitle = "using OxyPlot in VB.NET"
Dim series1 = New LineSeries()
series1.Title = "Series 1"
series1.MarkerType = MarkerType.Circle
series1.Points.Add(New DataPoint(0, 0))
series1.Points.Add(New DataPoint(10, 18))
series1.Points.Add(New DataPoint(20, 12))
series1.Points.Add(New DataPoint(30, 8))
series1.Points.Add(New DataPoint(40, 15))
Dim series2 = New LineSeries()
series2.Title = "Series 2"
series2.MarkerType = MarkerType.Square
series2.Points.Add(New DataPoint(0, 4))
series2.Points.Add(New DataPoint(10, 12))
series2.Points.Add(New DataPoint(20, 16))
series2.Points.Add(New DataPoint(30, 25))
series2.Points.Add(New DataPoint(40, 5))
Model.Series.Add(series1)
Model.Series.Add(series2)
End Sub
然后我尝试添加更多数据来模拟动态加载数据。我也知道我必须使用invalidate()并且不推荐使用Update()。我在表单中添加了一个按钮,并在单击时添加另一个系列以添加到图表中。
按钮点击:
Private Sub test_Click(sender As Object, e As RoutedEventArgs) Handles test.Click
Dim series3 = New LineSeries()
series3.Title = "Series 3"
series3.MarkerType = MarkerType.Square
series3.Points.Add(New DataPoint(20, 20))
series3.Points.Add(New DataPoint(21, 21))
series3.Points.Add(New DataPoint(22, 22))
series3.Points.Add(New DataPoint(23, 23))
series3.Points.Add(New DataPoint(24, 24))
Model.Series.Add(series3)
Model.InvalidatePlot(True)
End Sub
这也是他们更新dataPlot的可爱参考:http://docs.oxyplot.org/en/latest/common-tasks/refresh-plot.html#examples
答案 0 :(得分:2)
在您的事件处理程序中,您需要在致电series3
之前将Model
添加到InvalidatePlot
,即
Model.Series.Add(series3);
Model.InvalidatePlot(true);
<强>更新强>
此外,您需要确保您的PlotModel通知视图(XAML)您的更改。如果我使用the example from Oxyplot documentation作为示例(您指定的那个),我会修改代码:
Public Class MainViewModel Implements INotifyPropertyChanged
Private mmodel As PlotModel
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Public Sub New()
Model = New PlotModel()
Model.Title = "Simple example"
Model.Subtitle = "using OxyPlot in VB.NET"
Dim series1 = New LineSeries()
series1.Title="Series 1"
series1.MarkerType = MarkerType.Circle
series1.Points.Add(New DataPoint(0, 0))
series1.Points.Add(New DataPoint(10, 18))
series1.Points.Add(New DataPoint(20, 12))
series1.Points.Add(New DataPoint(30, 8))
series1.Points.Add(New DataPoint(40, 15))
Dim series2 = New LineSeries()
series2.Title="Series 2"
series2.MarkerType = MarkerType.Square
series2.Points.Add(New DataPoint(0, 4))
series2.Points.Add(New DataPoint(10, 12))
series2.Points.Add(New DataPoint(20, 16))
series2.Points.Add(New DataPoint(30, 25))
series2.Points.Add(New DataPoint(40, 5))
Model.Series.Add(series1)
Model.Series.Add(series2)
End Sub
Property Model() As PlotModel
Get
Return mmodel
End Get
Set(value As PlotModel)
mmodel = value
NotifyPropertyChanged("Model")
End Set
End Property
Private Sub test_Click(sender As Object, e As RoutedEventArgs) Handles test.Click
Dim series3 = New LineSeries()
series3.Title = "Series 3"
series3.MarkerType = MarkerType.Square
series3.Points.Add(New DataPoint(20, 20))
series3.Points.Add(New DataPoint(21, 21))
series3.Points.Add(New DataPoint(22, 22))
series3.Points.Add(New DataPoint(23, 23))
series3.Points.Add(New DataPoint(24, 24))
Model.Series.Add(series3)
Model.InvalidatePlot(True)
End Sub
End Class