代码之家  ›  专栏  ›  技术社区  ›  Doug Null

如何将折线图中的所有点与每个新点向左移动

  •  0
  • Doug Null  · 技术社区  · 6 年前

    我有一个线图,在右边加上每一个新点,我希望所有的旧线点在X轴向左滚动一个点位置,这样它看起来向左滑动。

    我使用了.AddXY来添加所有的点来填充图表,这是有效的。

    Me.Chart_window.Series( my_series ).Points.AddXY( x_axis_point, y_value)
    

    但我不知道如何操作.Series.Points数据点集合以使显示的行点看起来向左滚动。

    我尝试了。RemoteAt(0),但这只删除了(0)处的点,而不影响其他点的显示位置。

    我想在我追加一个新的点之前,把所有现有的点向左一个X轴的位置复制,但我不知道如何。

    if CHART_IS_FILLING
        ' Graph not full of points yet, so new samples appear appending rightward.
        ' Show previous samples as same as before, and next samples appending:
        Me.Chart_antenna_window.ChartAreas("ChartArea1").AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount
        ' Plot next point:
                Me.Chart_antenna_window.Series( present_series_name & "_I" & graph_freq_index).Points.AddXY( x_axis_point, antenna_amplitude_I)
            x_axis_point += 1
    
    else      ' CHART IS FULL, SO START SLIDING LEFTWARD
        ' (Overaly graph is now full, so scroll it leftward by deleting leftmost sample before each new sample appended at right.)
        ' Scroll graph leftward:
        ' (ie. Remove left-point point, which is now beyond overlay window x-axis (ie. time) size):
        ' Shift all points leftward:
                    Me.Chart_antenna_window.Series( present_series_name & "_I" & graph_freq_index).Points.RemoveAt(0)
    
    
                    ' HOW DO I SHIFT ALL THE POINTS LEFTWARD?    I can't see a way to read a point from DataPointCollection so that I can
                    ' store it in the next index down.
    
            ' (All points shift left.)
        Me.Chart_antenna_window.ChartAreas("ChartArea1").AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount
        ' Plot next point at right-most chart point:
                Me.Chart_antenna_window.Series( present_series_name & "_I" & graph_freq_index).Points.AddXY( x_axis_point, antenna_amplitude_I)
    end if
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   Alessandro Mandelli    6 年前

    我建议使用Collection.RemoveAt(index)方法,其中index是要删除的元素的从零开始的索引,在您的示例中是0

        2
  •  0
  •   Caius Jard    6 年前

    这是一个完整的猜测,因为我没有看到/使用过这个图表,但是在删除第一个之后,您必须循环遍历点集合,并将x减1以将所有点向左移动:

    ForEach p as Point in Me.Chart_antenna_window.Series( present_series_name & "_I" & graph_freq_index).Points
      p.X -= 1
    Next p
    

    我不知道Point s集合中是什么类型的对象-希望它被称为Point,但您可能需要调整它(可能是chartpoint或其他什么)。我最近也不太熟悉VB.Net,也许有一种更简单的方法可以用c语言编写foreach,如果编译器可以这样做的话,我们不必总是声明变量的类型 foreach(var p in ...Points) 在c#中可能没问题,也许vb有一个等价物; VB.NET equivalent to C# var keyword

    如果您声明一个引用 Me.Chart_antenna_window.Series( present_series_name & "_I" & graph_freq_index) 在你所有的代码之外:

    Dim chart as Whatever = Me.Chart_antenna_window.Series( present_series_name & "_I" & graph_freq_index)
    
    If blah Then
      chart.Points.AddXY ...
    Else 
      chart.Points.AddXY ..
    

    任何时候,当你发现自己一遍又一遍地复制和粘贴同一个庞大的长行代码时,对它做一个变量引用,并用它来提高代码的可读性

    推荐文章