有问题。我认为它必须很容易实现,但出于某种原因我不能这样做。现在,如果我点击它们,图表就会更新。我希望每次更改值时都更新它们(就像在Excel中一样),因为必须更容易实现计时器。
出于某种原因,我无法实现它,我已经尝试了一些我在网上找到的东西。 我有两个图表,我希望图表每秒更新一次值(或适合程序的间隔)。
我正在使用Windows窗体并具有以下VB代码:
Private Sub Chart1_Click(sender As Object, e As EventArgs) Handles Chart1.MouseClick
AutoSize = False
Chart1.ChartAreas(0).AxisX.Interval = 0.05
Chart1.ChartAreas(0).AxisY.Interval = 200
With Chart1
.Series(0).Points.Clear()
.Series(1).Points.Clear()
.Series(2).Points.Clear()
End With
With Chart1
.Series(0).Points.AddXY(TOCGLong, TOSumMassValues)
.Series(1).Points.AddXY(LandCGLong, LandSumMassValues)
.Series(2).Points.AddXY(ZFWCGLong, ZFWSumMassValues)
End With
With Chart1.ChartAreas
With .Max
.AxisX.Maximum = 3.55
.AxisY.Maximum = 2300
End With
With .Min
.AxisX.Minimum = 3.15
.AxisY.Minimum = 1200
End With
End With
Chart1.Series(3).Points.Clear()
For i As Integer = 0 To 5
Chart1.Series(3).Points.AddXY(CGLimitX(i), CGLimitY(i))
Next
End Sub
Private Sub Chart3_Click(sender As Object, e As EventArgs) Handles Chart3.MouseClick
Dim FuelX() As Integer = {80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180}
Dim ISA() As Double = {27171.16, 24053.39, 21011, 18043.99, 15152.36, 12336.11, 9595.24, 6929.75, 4339.64, 1824.91, -614.44}
Dim ISA20() As Double = {25421.84, 21987.56, 18644, 15409.16, 12277.04, 9247.64, 6320.96, 3497, 775.76, -1842.76, -4358.56}
Dim ISA35() As Double = {23726.04, 19793.81, 16041, 12467.61, 9073.64, 5859.09, 2823.96, -31.75, -2708.04, -5204.91, -7522.36}
AutoSize = False
Chart3.ChartAreas(0).AxisX.Interval = 10
Chart3.ChartAreas(0).AxisY.Interval = 5000
With Chart3
.Series(0).Points.Clear()
'.Series(1).Points.Clear()
'.Series(2).Points.Clear()
End With
With Chart3
.Series(0).Points.AddXY(FuelConsuption, SDaltitude.Value)
For i As Integer = 0 To 10
.Series(1).Points.AddXY(FuelX(i), ISA(i))
.Series(2).Points.AddXY(FuelX(i), ISA20(i))
.Series(3).Points.AddXY(FuelX(i), ISA35(i))
Next
End With
With Chart3.ChartAreas
With .Max
.AxisX.Maximum = 180
.AxisY.Maximum = 23000
End With
With .Min
.AxisX.Minimum = 80
.AxisY.Minimum = 0
End With
End With
End Sub
答案 0 :(得分:0)
从Chart1.MouseClick处理程序中取出代码,并将其放在具有描述性名称的Sub中。对Chart3.MouseClick处理程序执行相同的操作。
在Chart1.MouseClick处理程序中,首先调用Sub。
在Chart3.MouseClick处理程序中,调用第二个方法。
对于问题中的问题:
我希望每次更改值时都会更新
在相应的文本框leave
处理程序中,调用适当的方法,如下所示:
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
UpdateChart1()
UpdateChart2()
End Sub
我建议使用Leave事件,以便在用户处于打字过程中时不会发生变化。
对于标题中的问题:
您应该将图表更新代码放在自己的方法中,如上所述。然后,您可以通过计时器的tick事件轻松调用它。代码的基本部分是这样的:
Public Class Form1
Dim tim As Timer
Sub UpdateMpgChart()
' ... code here to update the chart
End Sub
Sub UpdateOtherChart()
' ... code here to update the other chart
End Sub
Sub TimerTickHandler(sender As Object, e As EventArgs)
UpdateMpgChart()
UpdateOtherChart()
End Sub
Sub InitialiseTimer()
tim = New Timer With {.Interval = 1000}
AddHandler tim.Tick, AddressOf TimerTickHandler
tim.Start()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitialiseTimer()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If tim IsNot Nothing Then
tim.Dispose()
End If
End Sub
End Class
答案 1 :(得分:0)
我希望每次更改值时都会更新它们(就像在Excel中一样),因为必须更容易实现计时器。
您可以使用ReactiveProperty nuget。
为每个值声明一个ReactiveProperty"做出反应":
public ReactiveProperty<int> Prop1 = new ReactiveProperty(0);
public ReactiveProperty<int> Prop2 = new ReactiveProperty(0);
在构造函数(或load事件)中,只需告诉属性更新图表:
Prop1.Subscribe(value => Chart1_Click(this, null));
我告诉Prop1执行Chart1_Click方法,但您可以创建一个更新图表的特定方法,并将其放入订阅方法而不是Chart1_Click。
要更改Prop1或Prop2值,只需:
Prop1.Value = 5; // this line will run the specified method
Prop2.Value = 10;