winforms 以编程方式滚动图表

eulz3vhy  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(160)

我的WinForm上有一个图表,它显示了生命体征监测仪的EKG信号。该图表在X轴上有一个滚动条,允许用户来回滚动以查看他们希望的任何点的信号。这很好用。该图表真实的更新。
图表中的数据会在表单载入时显示。我遇到的问题是,如果使用者关闭并重新开启表单,卷轴按钮会自动重设到最左边。我想要将卷轴按钮定位在图表中相等目前日期时间的位置,但是我无法透过查看图表控件来找到任何方法。Google搜寻并不成功。
以下是我的图表设置:

private void InitializeChartEKG() {
        chartEKG.ChartAreas[0].AxisX.Title = "Time";
        chartEKG.ChartAreas[0].AxisX.MajorTickMark.Enabled = true;
        chartEKG.ChartAreas[0].AxisX.MinorTickMark.Enabled = true;
        chartEKG.ChartAreas[0].AxisX.IsStartedFromZero = true;
        chartEKG.ChartAreas[0].CursorX.LineColor = Color.LawnGreen;
        chartEKG.ChartAreas[0].CursorY.LineColor = Color.LawnGreen;
        chartEKG.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
        chartEKG.ChartAreas[0].AxisX.MajorGrid.Interval = 100;
        chartEKG.ChartAreas[0].AxisX.IsStartedFromZero = true;  
        chartEKG.ChartAreas[0].AxisX.MajorTickMark.Enabled = true;
        chartEKG.ChartAreas[0].AxisX.Minimum = 0;
        chartEKG.ChartAreas[0].AxisY.Maximum = 600;
        chartEKG.ChartAreas[0].AxisY.Minimum = -600;
        chartEKG.ChartAreas[0].AxisX.Interval = 10000;
        chartEKG.ChartAreas[0].AxisY.Title = "mV";
        chartEKG.Series[0].XValueType = ChartValueType.DateTime;
        chartEKG.Series[0].YValueType = ChartValueType.Int32;
        chartEKG.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Auto;
        chartEKG.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Auto;
        chartEKG.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss tt";
        chartArea = chartEKG.ChartAreas[0];
        chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Auto;
        int position = 0;
        int blockSize = 10000;
        int size = blockSize;
        chartArea.AxisX.ScaleView.Zoom(position, size);
        chartArea.AxisX.ScaleView.SmallScrollSize = blockSize;
        chartArea.CursorX.AutoScroll = true;
        chartArea.AxisX.ScrollBar.BackColor = Color.LightGray;
        chartArea.AxisX.ScrollBar.ButtonColor = Color.LightSteelBlue;
        chartArea.AxisX.ScrollBar.LineColor = Color.DarkBlue;
        chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
        CultureInfo culture = new CultureInfo("en-US");
    }

在我调用的方法中添加我认为我可以做的点:

chartEKG.Invoke(new Action(() => chartEKG.ChartAreas[0].AxisX.ScaleView.Position = chartEKG.ChartAreas[0].AxisX.Maximum - SomeNumber);

但我不确定如何计算SomeNumber

1szpjjfi

1szpjjfi1#

最后,我找到了一个解决方案。在向图表添加点的方法中,我修改了添加点的循环:

foreach (string point in count) {
                try {
                    time = time.AddMilliseconds(2);
                    chartEKG.Invoke(new Action(() => series1.Points.AddXY(time, Convert.ToDouble(point) * 0.61)));
                }
                catch (ArgumentException) { }
                loopCount = loopCount + 1;
                string timeFormat = "yyyyMMddHHmmssfff";
                string timeNow = DateTime.Now.ToString(timeFormat);
                if (time == DateTime.ParseExact(DateTime.Now.ToString(timeNow), timeFormat, CultureInfo.InvariantCulture)) {
                    loopToStopAt = loopCount;
                }
            }

然后在foreach循环下面,我放置了以下代码:

chartEKG.Invoke(new Action(() => chartEKG.ChartAreas[0].AxisX.ScaleView.Scroll(chartEKG.ChartAreas[0].AxisX.Maximum - Convert.ToDouble(loopToStopAt))));

这将以编程方式将滑块拖动到图表的最右侧,这正是我所希望的。

相关问题