当前位置:主页 > 软件编程 > .NET代码 >

C# Chart绘制简单图形波形

时间:2022-08-31 11:13:55 | 栏目:.NET代码 | 点击:

本文实例为大家分享了C# Chart绘制简单图形波形的具体代码,供大家参考,具体内容如下

此次用C#绘制波形使用的是Chart控件

1、将Chart控件拖进主界面,然后设置属性。

// 
// chart1
// 
chartArea2.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea2);
legend2.Name = "Legend1";
this.chart1.Legends.Add(legend2);
this.chart1.Location = new System.Drawing.Point(36, 6);
this.chart1.Name = "chart1";
series3.BorderWidth = 3;
series3.ChartArea = "ChartArea1";
series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series3.Legend = "Legend1";
series3.Name = "系列1";
series4.BorderWidth = 2;
series4.ChartArea = "ChartArea1";
series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series4.Legend = "Legend1";
series4.Name = "系列2";
this.chart1.Series.Add(series3);
this.chart1.Series.Add(series4);
this.chart1.Size = new System.Drawing.Size(839, 499);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";

2、画线委托函数

public delegate void ChartDelegate(int series, double Value_Y_Axis); // 声明委托
public void DrawPoint(int series, double Value_Y_Axis)
{
    try
    {
        if (this.chart1.InvokeRequired)
        {
            this.chart1.Invoke((ChartDelegate)DrawPoint, series, Value_Y_Axis);
        }
        else
        {
            this.chart1.Series[series - 1].Points.Add(Value_Y_Axis);
        }
    }
    catch
    {
        MessageBox.Show("ERROR!");
    }
    //chart1.Series[series - 1].Points.Add(Value_Y_Axis);
}

3、设置一个按键,然后设置按键点击触发时候的定时器Timer属性,用来触发波形产生(用控件按键触发)

private void button3_Click(object sender, EventArgs e)
{
    System.Timers.Timer timer11 = new System.Timers.Timer(400); // 每400ms触发一次
    timer11.Elapsed += new System.Timers.ElapsedEventHandler(timer_handle); // 执行函数tiemr_handle
    timer11.AutoReset = true;
    timer11.Enabled = true;
    timer11.Start();
}

4、定时器触发执行函数

public int gIndex = 1;
public void timer_handle(object source, System.Timers.ElapsedEventArgs e)
{
    double mpre1 = Log10(gIndex) * 300;
    double mpre2 = Sin(gIndex) * 200;
    //
    DrawPoint(1, mpre1);
    DrawPoint(2, mpre2);

    gIndex++;
}

效果动态图:

您可能感兴趣的文章:

相关文章