在MS CHART中绘制瞬时Y值 [英] Plotting Instantaneous Y values in MS CHART

查看:72
本文介绍了在MS CHART中绘制瞬时Y值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我有一个BAR图表,我需要立即更新其Y值。


当我使用 


Chart1.Series(" Series1")。Points.AddXY(1,recArray(1))


在X = 1时绘制的Y值那里。例如,如果  recArray(1)= 5,它绘制5,现在  recArray(1)= 100,它绘制100,  recArray(1)= 2


我看不到绘制了如先前绘制的图2所绘制的并且它保持在那里。我需要绘制新值而不明确清除旧值。 


我尝试了以下命令,但它不起作用。即没有绘制任何内容


Chart1.Series(" Series1")。Points.DataBindY(recArray(1))

解决方案

更新图表的最简单方法是重新制作和重绘图表。


以下是使用计时器的示例。每个计时器勾选一个新的y = sin(x)值被计算并添加到y值列表中。如果列表中的值超过最大值,则会从列表的开头删除一个值。


更新列表后,通过清除并重新映射图表来重绘图表使用新列表数据绘制图表数据。


 Imports System.Windows.Forms.DataVisualization.Charting 

Public Class Form1
Private WithEvents Timer1 As New System.Windows。 Forms.Timer {.Interval = 100,.Enabled = False}
Private Angle As Integer
Structure DataPointType
Public x As Single
Public y As Single
End结构
私有ChartDataList作为新列表(Of DataPointType)

私有子Form1_Load(发件人作为对象,e作为EventArgs)处理Me.Load

'设置图表
Chart1.Titles.Add(" Sin Wave Example")
Chart1.Titles(0).Font = New字体(" Arial",10,FontStyle.Bold)

使用Chart1.ChartAreas(0)
.AxisX.Title =" Angle(degs)"
.AxisX.MajorGrid.Enabled = False
.AxisX.MajorGrid.LineColor = Color.LightGray
.AxisX.Interval = 50
.AxisX.IsLabelAutoFit = False
.AxisX.LabelStyle.Font = New Font(" Arial",10,FontStyle.Regular)
.AxisX.LabelStyle.Angle = -90
.AxisX.LabelStyle.IsStaggered = False
.AxisX.LabelStyle.Enabled = True

.AxisY.Title =" Sin(Angle)"
.AxisY.MajorGrid.LineColor = Color.LightGray
.AxisY.IsInterlaced = True
.AxisY.InterlacedColor = Color.FloralWhite

.BackColor = Color.FloralWhite
.BackSecondaryColor = Color.White
.BackGradientStyle = GradientStyle.Horizo​​ntalCenter
.BorderColor = Color.Blue
.BorderDashStyle = ChartDashStyle.Solid
.BorderWidth = 1
.ShadowOffset = 2
结束时

GetDataPoint()

DrawChart()

结束次级

Private Sub Timer1_Tick(发送者为对象,e为EventArgs)处理Timer1.Tick

GetDataPoint()

DrawChart()

End Sub

Private Sub DrawChart()
'绘制图表
Chart1.Series.Clear()
Chart1.Series.Add(" Line Type")
Chart1.Series(0).IsVisibleIn Legend = False
Chart1.Series(0).Color = Color.Red
Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line

Chart1.ChartAreas (0).AxisX.Minimum = ChartDataList(0).x
Chart1.ChartAreas(0).AxisX.Maximum = Chart1.ChartAreas(0).AxisX.Minimum + 360

For i = 0到ChartDataList.Count - 1
Chart1.Series(0).Points.AddXY(ChartDataList(i).x,ChartDataList(i).y)
下一个

End Sub

Private Sub GetDataPoint()
'删除一个点
如果ChartDataList.Count> 17然后
ChartDataList.Remove(ChartDataList(0))
结束如果

'添加下一个点
Dim thisDataPoint As DataPointType
thisDataPoint.x = Angle
thisDataPoint.y = CSng(Math.Sin(Angle / 57.8))

ChartDataList.Add(thisDataPoint)

Angle + = 20

End Sub

Private Sub Button1_Click(sender As Object,e As EventArgs)Handles Button1.Click
If Timer1.Enabled Then
Timer1.Enabled = False
Button1.Text ="开始"
Else
Timer1.Enabled = True
Button1.Text =" Stop"
结束如果
结束次级
结束等级



Hello

I have a BAR Graph and I need to update its Y values Instantaneously.

When I use 

Chart1.Series("Series1").Points.AddXY(1, recArray(1))

The Y value plotted on X=1 stays there. for e.g if recArray(1) =5, it plots 5, now recArray(1)=100, it plots 100, recArray(1)=2

I cannot see 2 plotted as previously 100 was plotted and it stays there. I need new value plotted without explicitly clearing the old one. 

I tried following command but it does not work. i.e. nothing is plotted

Chart1.Series("Series1").Points.DataBindY(recArray(1))

解决方案

The easiest way to update the chart is to remake and redraw the chart.

Here is an example that uses a timer. Each timer tick a new y = sin(x) value is calculated and added to a list of y values. If there are more than the max values in the list then a value is removed from the start of the list.

After the list has been updated, the chart is redrawn by clearing and remaking the chart data with the new list data.

Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1
    Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 100, .Enabled = False}
    Private Angle As Integer
    Structure DataPointType
        Public x As Single
        Public y As Single
    End Structure
    Private ChartDataList As New List(Of DataPointType)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        'setup the chart
        Chart1.Titles.Add("Sin Wave Example")
        Chart1.Titles(0).Font = New Font("Arial", 10, FontStyle.Bold)

        With Chart1.ChartAreas(0)
            .AxisX.Title = "Angle (degs)"
            .AxisX.MajorGrid.Enabled = False
            .AxisX.MajorGrid.LineColor = Color.LightGray
            .AxisX.Interval = 50
            .AxisX.IsLabelAutoFit = False
            .AxisX.LabelStyle.Font = New Font("Arial", 10, FontStyle.Regular)
            .AxisX.LabelStyle.Angle = -90
            .AxisX.LabelStyle.IsStaggered = False
            .AxisX.LabelStyle.Enabled = True

            .AxisY.Title = "Sin(Angle)"
            .AxisY.MajorGrid.LineColor = Color.LightGray
            .AxisY.IsInterlaced = True
            .AxisY.InterlacedColor = Color.FloralWhite

            .BackColor = Color.FloralWhite
            .BackSecondaryColor = Color.White
            .BackGradientStyle = GradientStyle.HorizontalCenter
            .BorderColor = Color.Blue
            .BorderDashStyle = ChartDashStyle.Solid
            .BorderWidth = 1
            .ShadowOffset = 2
        End With

        GetDataPoint()

        DrawChart()

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        GetDataPoint()

        DrawChart()

    End Sub

    Private Sub DrawChart()
        'draw the chart
        Chart1.Series.Clear()
        Chart1.Series.Add("Line Type")
        Chart1.Series(0).IsVisibleInLegend = False
        Chart1.Series(0).Color = Color.Red
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line

        Chart1.ChartAreas(0).AxisX.Minimum = ChartDataList(0).x
        Chart1.ChartAreas(0).AxisX.Maximum = Chart1.ChartAreas(0).AxisX.Minimum + 360

        For i = 0 To ChartDataList.Count - 1
            Chart1.Series(0).Points.AddXY(ChartDataList(i).x, ChartDataList(i).y)
        Next

    End Sub

    Private Sub GetDataPoint()
        'remove a point
        If ChartDataList.Count > 17 Then
            ChartDataList.Remove(ChartDataList(0))
        End If

        'add next point
        Dim thisDataPoint As DataPointType
        thisDataPoint.x = Angle
        thisDataPoint.y = CSng(Math.Sin(Angle / 57.8))

        ChartDataList.Add(thisDataPoint)

        Angle += 20

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Timer1.Enabled Then
            Timer1.Enabled = False
            Button1.Text = "Start"
        Else
            Timer1.Enabled = True
            Button1.Text = "Stop"
        End If
    End Sub
End Class


这篇关于在MS CHART中绘制瞬时Y值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆