C#示波器模拟器(带有MS Chart控件) [英] C# Oscilloscope Simulator (with MS Chart control)

查看:336
本文介绍了C#示波器模拟器(带有MS Chart控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用ms-chart控件创建一个示波器模拟器.我将数据存储在数组中.但是我不知道如何创建移动效果"-控件的不断更新. (从图表控件中添加/删除点),并在控件上每秒绘制一条垂直线.

I have need to create an oscilloscope simulator using the ms-chart control. I store my data in an array. But I don't know how to create the "moving effect" - continuous update of the control. (adding / removing points from the chart control) and having a vertical line drawn every second on the control.

我的代码是:

    private void Form1_Load(object sender, EventArgs e)
    {

        chart1.Series["Series1"].ChartType = SeriesChartType.Line;
        chart1.Series["Series1"].BorderWidth = 3;

         // NO grids
        chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
        chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;

        chart1.PostPaint += new EventHandler<ChartPaintEventArgs>(chart1_PostPaint);
    }

    void chart1_PostPaint(object sender, ChartPaintEventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        string fileContent = File.ReadAllText("e:\\in.txt");
        string[] integerStrings = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        int[] integers = new int[integerStrings.Length];

        for (int n = 0; n < integerStrings.Length; n++)
        {
            integers[n] = int.Parse(integerStrings[n]);
            chart1.Series["Series1"].Points.Add(integers[n]);
        }

    }

推荐答案

尝试一下:

public partial class Form1 : Form
{
    Timer timer;
    double x;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer = new Timer();
        timer.Tick += Timer_Tick;
        timer.Interval = 50;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (timer.Enabled)
            timer.Stop();
        else timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        chart1.Series[0].Points.AddXY(x, 3 * Math.Sin(5 * x) + 5 * Math.Cos(3 * x));

        if (chart1.Series[0].Points.Count > 100)
            chart1.Series[0].Points.RemoveAt(0);

        chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
        chart1.ChartAreas[0].AxisX.Maximum = x;

        x += 0.1;
    }
}

这篇关于C#示波器模拟器(带有MS Chart控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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