Home > Enterprise >  Scroll chart programmatically
Scroll chart programmatically

Time:11-10

I have a chart on my WinForm that displays an EKG signal from a vital sign monitor. The chart has a scrollbar on the X-axis that allows the user to scroll back and forth to see the signal at any point they wish. This works great. The chart gets updated in real time.

The data in the chart is displayed on form load. The problem that I have is that if the user closes and reopens the form, the scrollbar button resets itself to the far left. I want the scrollbar button to be positioned at the point in the chart that is equal to the current DateTime, but I cannot find any way to do that by looking at the chart controls. Googling has not been successful.

Here is my chart setup:

        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");
    }

In the method I call to add the points I think I can do:

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

But I'm not sure how to calculate SomeNumber

CodePudding user response:

Finally was able to locate a solution. In the method that adds the points to the chart, I modified the loop that adds the points:

                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;
                }
            }

Then below that foreach loop, I placed this code:

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

This will programmatically drag the slider to the far right of the chart, which is exactly what I want.

  • Related