VB.Net - 串联的第一个dataPoint绘制到图表上的错误x值

时间:2013-04-18 20:27:14

标签: .net vb.net charts mschart

我正在开发一个类来管理图表的数据系列。但是,当我添加一个新系列时,如果第一个dataPoint的XValue为0,它会在图中显示为XValue为1.如果我添加了一个额外的dataPoint,它似乎可以自行修正。使用下面的代码,首先调用构造函数(显然),然后调用Initialize(使用类似“Series1”,0,0的数据),然后可以稍后调用AddPoint。有谁知道发生了什么事?

编辑: 根据我的发现,只要系列只有一个数据点并且该数据点的xValue为0,就会发生这种情况。在具有非零x值的一个(或多一些)数据点之前,该系列也不会正常运行。添加。我的解决方法是,如果要添加的数据点的xValue为0,则使其xValue非常小(1x10 ^ -150)。似乎工作,但这仍然是我书中的一个奇怪的错误。我也无法找到任何相关信息。

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

2 个答案:

答案 0 :(得分:0)

尝试更改此内容:

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

对此:

If m_chart.InvokeRequired Then
    m_chart.Invoke(New Action(Of Double, Double)(AddressOf AddPoint), x, y)
End If

Dim temp As New DataPoint(x, y)
m_dataSeries.Item(m_dataSeries.Count - 1).Points.Add(temp)

答案 1 :(得分:0)

2017年8月,.net Framework 4.5.2,仍然是同样的错误...

我创建了一个气泡图,当我添加一个单点时:

Chart1.Series("MySeries").AddXY(0,0,value)

奇怪:这一点出现在位置(1,0)而不是(0,0) 但如果我添加多个点,那就是正确的......

所以,我的诀窍是类似的,当我只有一个单点时,我将这个点加两次,有一个微小的偏移,知道它会被真实的东西掩盖...... 不太好,它的作弊,但它解决了'我的问题......

Chart1.Series("MySeries").AddXY(0.000001,0,0)
Chart1.Series("MySeries").AddXY(0,0,10)

任何其他建议:欢迎!