我已经实现了一个User Control类来扩展Chart类的功能。该图表将实时添加数据。 User Control类创建ChartArea,并将其添加到基础图表。它还添加了标题,图例和系列。我的问题是当我向图表系列添加新的数据点时,图表不会像在vanilla图表中那样更新以反映这一点。
User Control类包含一个子数据,用于向基础图表添加数据:
Chart1.Series(seriesName).Points.AddXY(xValue, yValue)
我已经模拟了一个数据源,并且可以验证系列中确实存在数据点。我试过在Chart1,ChartArea,Series和User Control类本身上调用invalidate(),refresh()和/或update()。我还在创建系列时立即向系列添加了数据点,但也没有出现。
我觉得我错过了一些简单的东西;为什么这些数据点没有出现?
相关代码:
(dataChart是与用户控件关联的图表)
Imports System.Windows.Forms.DataVisualization.Charting
Public Class ScrollChart
Private _legendName As String = "defaultLegend"
Private _chartSpan As Single = 10
Private _xAxisFormat As String = "#.0"
Private _yAxisFormat As String = "#.0"
Private _xMajorInterval As Single = 1
Private _xMinorInterval As Single = 0.5
'...Property getters and setters omitted
Public Sub New()
' This call is required by the designer.
InitializeComponent()
'Remove default chart objects
dataChart.Legends.Clear()
dataChart.Titles.Clear()
dataChart.Annotations.Clear()
dataChart.Series.Clear()
dataChart.ChartAreas.Clear()
End Sub
Public Sub createChartArea(ByVal areaName As String, ByVal areaTitle As String, ByVal xAxisTitle As String, ByVal yAxisTitle As String)
'Create new ChartArea
Dim newArea As ChartArea = New ChartArea(areaName)
'Add area title
Dim chartTitle As Title = New Title(areaTitle)
dataChart.Titles.Add(chartTitle)
'Add area legend
Dim areaLegend As Legend = New Legend(_legendName)
areaLegend.Docking = Docking.Bottom
dataChart.Legends.Add(areaLegend)
'Set X axis defaults for scrolling
newArea.AxisX.Minimum = 0
newArea.AxisX.Maximum = _chartSpan
newArea.AxisX.MajorGrid.Interval = _xMajorInterval
newArea.AxisX.MinorGrid.Enabled = True
newArea.AxisX.MinorGrid.Interval = _xMinorInterval
newArea.AxisX.LabelStyle.Format = _xAxisFormat
newArea.AxisX.IsStartedFromZero = False
newArea.AxisX.Title = xAxisTitle
'Set Y axis defaults for scrolling
newArea.AxisY.Minimum = 0
newArea.AxisY.Maximum = 1
newArea.AxisY.MinorGrid.Enabled = True
newArea.AxisY.LabelStyle.Format = _yAxisFormat
newArea.AxisY.IsStartedFromZero = False
newArea.AxisY.Title = yAxisTitle
'Add to chart
dataChart.ChartAreas.Add(newArea)
End Sub
Public Sub createChartSeries(ByVal areaName As String, ByVal seriesName As String, ByVal seriesTitle As String)
'Create new Series
Dim newSeries As Series = New Series(seriesName)
'Add series legend
newSeries.Legend = _legendName
newSeries.LegendText = seriesTitle
'Set defaults for scrolling
newSeries.ChartType = SeriesChartType.Line
newSeries.Color = Color.Red
newSeries.ChartArea = areaName
newSeries.MarkerColor = Color.Red
'Add to chart
dataChart.Series.Add(newSeries)
End Sub
Public Sub addData(ByVal seriesName As String, ByVal xValue As Single, ByVal yValue As Single)
'Add new data point
dataChart.Series(seriesName).Points.AddXY(xValue, yValue)
'Remove oldest point if series is larger than the given chart span
Dim newestX As Double = dataChart.Series(seriesName).Points.Last.XValue
Dim oldestX As Double = dataChart.Series(seriesName).Points.First.XValue
If ((newestX - oldestX) > _chartSpan) Then
dataChart.Series(seriesName).Points.RemoveAt(0)
End If
'Redraw axis
dataChart.ChartAreas(dataChart.Series(seriesName).ChartArea).RecalculateAxesScale()
End Sub
Private Sub ScrollChart_Resize(sender As Object, e As EventArgs) Handles Me.Resize
'Resize chart to control size
dataChart.Size = Size
End Sub
End Class
测试表格中的类用法:
Public Class Form1
Private dataSource As New MockDataSource(10, 0.5)
Private startTime As Date = Date.Now
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ScrollChart1.createChartArea("area1", "Test Data", "Time (Seconds)", "Value (Unitless)")
ScrollChart1.createChartSeries("area1", "series1", "Test Value")
End Sub
Private Sub pollTimer_Tick(sender As Object, e As EventArgs) Handles pollTimer.Tick
Dim currentElapsed As Single = CSng((Date.Now - startTime).TotalSeconds)
ScrollChart1.addData("series1", currentElapsed, dataSource.getRandomValue)
End Sub
End Class