c# - create Horiz. scrollbar on chart when data is

2019-09-13 21:02发布

问题:

I have a line chart, which, after enough data points have been plotted to it, the data will exceed what is visible on screen (so that the chart is only showing the most recent data). When this occurs, I would like a scroll bar to be filled for the X axis, allowing the user to use the scroll bar to view such previous data.

How do I go about doing this? I don't want the user to be able to drag or zoom on the chart itself, just to solely use the scroll bar to navigate along the chart.

I've looked at this article: https://msdn.microsoft.com/en-us/library/dd456730.aspx but it doesn't help & the scrollbars do not appear.

回答1:

Without seeing the relevant parts of your code it is hard to pin down your problems.

Here is one strange statement:

after enough data points have been plotted to it, the data will exceed what is visible on screen (so that the chart is only showing the most recent data).

Now this can only happen after you have set the AxisX.Maximum because by default the chart control will squeeze the area more and more while you add points.

But when you have set a maximum of what can be shown, no scrollbar can work or even been shown. Sounds logical, right?

So either don't set it in the first place or clear it when the number of points exceeds what you want to show. To clear it use NaN :

chart1.ChartAreas[0].AxisX.Maximum = Double.NaN;

Or, of course, set it to the last point you want to be shown!

After looking at what you mustn't do let's see what you need to do to show the scrollbar:

First you enable it:

chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;

Next you tell it to show only the scrolling handle and not the zoom-reset button:

chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;

See MSDN on ScrollBarButtonStyles for the various things the scrollbar can show/do!

And to make sure the user can't zoom set this:

chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false;

And finally set the current range to show:

chart1.ChartAreas[0].AxisX.ScaleView.Size = 111; // show 111 DataPoints

Now the scrollbar should show.

It is a good idea to study the AxisScaleView class as it has a couple of helpful properties..

Depending on the data type of your X-Values you may also need to set the ScaleView.MinSizeType to whatever suits your data:

chart1.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Number;