WinForms Chart c# 问题中的实时心电图数据

Real time ecg data in WinForms Chart c# problem

我正在尝试将设备输出的实时心电图数据显示到 Winforms 图表。

它工作得很好,但我似乎无法从图表左侧删除数据点,因为添加了新点,主要网格收缩和增长。

定时器中的代码如下:

    private void butPlayEKG_Click(object sender, EventArgs e) {
    chartEKG.Series.Clear();
    var series1 = new Series {
        Name = "EKG",
        Color = Color.LawnGreen,
        IsVisibleInLegend = true,
        IsXValueIndexed = true,
        ChartType = SeriesChartType.Line,
    };
    this.chartEKG.Series.Add(series1);
    playButtonClicked = true;
    timer1.Enabled = true;
    timer1.Interval = 5;
    timer1.Tick += (s, args) => timer1_Tick(series1);
    timer1.Start();

}

private void timer1_Tick(Series series1) {
    labelClock.Text = DateTime.Now.ToString("HH:mm:ss tt");
    string str = String.Empty;
    for (int j = 0; j < ekgData.Count; j++) {
        str = ekgData[j].EKGWaveform.ToString();
        count = str.Split('^');
        EKGData = new int[count.Length];
        string timeStamp = ekgData[j].VSTimeStamp.ToString();
        string format = "yyyyMMddHHmmssfff";
        DateTime time = DateTime.ParseExact(timeStamp, format, null);// CultureInfo.InvariantCulture);
        adjTime = time;
        if (j > 0) {
            adjTime = time; time.AddSeconds(1 / 500);
        }
        for (int i = 0; i < 499; i++) {
            EKGData = Array.ConvertAll<string, int>(count, Convert.ToInt32);
            series1.Points.AddXY(adjTime.ToString("HH:mm:ss tt"), EKGData[i] * .61);
            chartEKG.Update();
        }
        for (int h = 1; h < j; h++) {
            series1.Points.Remove(series1.Points[h]);
            chartEKG.Update();
        }

    }
}

这是图表代码:

        timer1.Interval = 15;
        this.chartEKG.ChartAreas["ChartArea1"].CursorX.LineColor = Color.LawnGreen;
        this.chartEKG.ChartAreas["ChartArea1"].CursorY.LineColor = Color.LawnGreen;
        this.chartEKG.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = true;
        this.chartEKG.ChartAreas["ChartArea1"].AxisY.MajorGrid.Interval = 200;
        this.chartEKG.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = true;
        this.chartEKG.ChartAreas["ChartArea1"].AxisX.MajorGrid.Interval = 200;
        this.chartEKG.ChartAreas["ChartArea1"].AxisY.Title = "mV";
        this.chartEKG.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
        CultureInfo culture = new CultureInfo("en-US");
        ekgData = AnesthVSDatas.CreateEKGWaveformObjects(8237);

可在此处观看回放视频:

Real time ecg data

注意,time.AddSeconds(1 / 500);没有效果。 我猜你每次都在图表中插入相同的时间。

也许你的意图是

time = time.AddSeconds(1d / 500);

另请注意 d。因为 1/500 等于 0.

现在工作。

我意识到我不需要计时器,我需要一个 points.RemoveAt 来随着更多点的绘制从图表中删除点。我还通过将所有消息连接成一个长字符串而不是单独解析每条消息来简化事情。

这是工作代码:

    private System.Windows.Forms.DataVisualization.Charting.Chart chartEKG;
    private System.Windows.Forms.DataVisualization.Charting.Series series1;
    private List<AnesthVSData> ekgData;
    private static string str;
    private string[] count;

        chartEKG.Series.Clear();
        var series1 = new Series {
            Name = "EKG",
            Color = Color.LawnGreen,
            IsVisibleInLegend = true,
            IsXValueIndexed = true,
            ChartType = SeriesChartType.Line,
        };
        this.chartEKG.Series.Add(series1);
        playButtonClicked = true;
        labelClock.Text = DateTime.Now.ToString("HH:mm:ss tt");
        string timeStamp = DateTime.Now.ToString();
        for (int j = 0; j < ekgData.Count; j++) {
            str = str + "^" + string.Join("^", ekgData[j].EKGWaveform.ToString());
            if (j == 0) {
                timeStamp = ekgData[j].VSTimeStamp; //get first VSTimeStamp in series
            }
        }
        str = str.Remove(0, 1); //remove leading ^
        count = str.Split('^');
        string format = "yyyyMMddHHmmssfff";
        DateTime time = DateTime.ParseExact(timeStamp, format, CultureInfo.InvariantCulture);
        //adjTime = time;
        for (int j = 0; j < count.Length; j++) {
                time = time.AddSeconds(0.002); //500 data points per HL7 message, each message is 1s long so 1/500 = 0.002
                series1.Points.AddXY(time.ToString("HH:mm:ss"), Convert.ToInt32(count[j]) * 0.61); // 0.61 is resolution correction factor
                if (chartEKG.Series[0].Points.Count > 1800) {
                    chartEKG.Series[0].Points.RemoveAt(0);
                    chartEKG.ChartAreas[0].AxisX.Minimum = 0;                   }
                chartEKG.Update();

        }

这是输出:

ekg waveform demo