I'm developing a class to manage data Series for a chart. However, when I add a new series, if the first dataPoint has an XValue of 0, it instead shows on the plot as having an XValue of 1. If I add an additional dataPoint, it seems to correct itself. Using the code below, the constructor is called first(obviously), then Initialize(with data like "Series1",0,0), then AddPoint can be called later. Anyone know what is going on?
Edit: From what I've found, this occurs any time the Series has only one data point and that data point has an xValue of 0. The series also does not behave correctly until one(or sometime more) data points with non-zero xValues are added. My workaround is if a data point to be added has an xValue of 0, make its xValue extremely small instead(1x10^-150). Seems to work, but this is still an odd bug in my book. I haven't been able to find any information on it either.
Public Sub New(ByVal chartObj As Chart)
'Init m_chart
m_chart = chartObj
m_chart.BackColor = Color.Gainsboro
'Init Legend
m_legend = New Legend("Legend")
m_legend.Docking = Docking.Bottom
m_chart.Legends.Add(m_legend)
'Init m_chartArea
m_chartArea = New ChartArea("Default")
m_chartArea.BackColor = Color.Black
m_chartArea.AxisX.LabelStyle.Format = "{0:0.00}"
setXLimits(-10, 10)
setYLimits(-0.5, 0.5)
m_chartArea.AxisX.Title = "Position (mm)"
m_chartArea.AxisX.TitleFont = New Font("Arial", 10, FontStyle.Regular)
m_chartArea.AxisY.Title = "Peak-To-Peak (Volts)"
m_chartArea.AxisY.TitleFont = New Font("Arial", 10, FontStyle.Regular)
m_chartArea.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash
m_chartArea.AxisX.MajorGrid.LineColor = Color.DarkGreen
m_chartArea.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash
m_chartArea.AxisY.MajorGrid.LineColor = Color.DarkGreen
m_chart.ChartAreas.Add(m_chartArea)
'Init m_dataSeries
m_dataSeries = New List(Of Series)
'Init m_markerSeries
m_markerSeries = New Series("Peaking Markers")
m_markerSeries.ChartType = SeriesChartType.Point
m_markerSeries.MarkerColor = Color.Red
m_markerSeries.MarkerStyle = MarkerStyle.Triangle
m_markerSeries.MarkerSize = 10
m_chart.Series.Add(m_markerSeries)
'Init m_title
m_title = New Title("Plots")
m_title.Font = New Font("Arial", 20, FontStyle.Regular)
m_chart.Titles.Add(m_title)
End Sub
Public Sub Initialize(ByVal Legend As String, ByVal xVal As Double, ByVal yVal As Double)
Dim temp As New Series(Legend)
temp.ChartType = SeriesChartType.Line
temp.Points.Clear()
If nextAxis = "X" Then
temp.Color = xColor
nextAxis = "Y"
Else
temp.Color = yColor
nextAxis = "X"
End If
temp.MarkerStyle = MarkerStyle.Circle
m_dataSeries.Add(temp)
m_chart.Series.Add(temp)
AddPoint(xVal, yVal)
End Sub
Public Sub AddPoint(ByVal x As Double, ByVal y As Double)
If m_chart.InvokeRequired Then
m_chart.Invoke(New Action(Of Double, Double)(AddressOf AddPoint), x, y)
Else
Dim temp As New DataPoint(x, y)
m_dataSeries.Item(m_dataSeries.Count - 1).Points.Add(temp)
End If
End Sub