2013-04-18 35 views
2

我正在開發一個類來管理圖表的數據系列。但是,當我添加一個新系列時,如果第一個dataPoint的XValue爲0,它將在圖上顯示XValue爲1.如果我添加一個額外的數據點,它似乎會自行糾正。使用下面的代碼,首先調用構造函數(顯然),然後初始化(使用像「Series1」,0,0這樣的數據),然後可以稍後調用AddPoint。任何人都知道發生了什麼事?VB.Net - 第一個數據點在圖上錯誤的x值繪圖

編輯: 從我發現的情況來看,只要系列只有一個數據點,並且該數據點的xValue爲0,就會發生這種情況。該系列在直到一個(或多個時間點)添加具有非零xValues的點。我的解決方法是,如果要添加的數據點的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 

回答

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) 
0

2017年8月,.NET框架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) 

其他建議:歡迎光臨!

相關問題