Adding a scrollbar to an MS Chart C # control

please understand that I know that there are other topics related to this problem, but my needs are different.

Basically, before I saw people talking about implementing scrollbars in MSChart, they use

.Size = ...

or

.View = ...

But this automatically activates the scroll bar, and this scroll bar contains a button that, when pressed, causes the bar to rotate, which makes the chart display for all data and does not allow you to return the scroll bar to the chart without restarting the application.

So, I ask, please, is there a way to snap a horizontal scrollbar along the x-axis of my chart? I need it so that I can view the chart data on blocks of 100 second blocks.

i.e. 0 - 100, and then go to the scroll bar to bring me to 100 - 200 blocks.

Thanks in advance guys !!!!! im coding in c # also

+4
source share
2 answers

Here is an example of what you need:
(try it, just create a form, add mschart and call the following method)

private void FillChart() { int blockSize = 100; // generates random data (ie 30 * blockSize random numbers) Random rand = new Random(); var valuesArray = Enumerable.Range(0, blockSize * 30).Select(x => rand.Next(1, 10)).ToArray(); // clear the chart chart1.Series.Clear(); // fill the chart var series = chart1.Series.Add("My Series"); series.ChartType = SeriesChartType.Line; series.XValueType = ChartValueType.Int32; for (int i = 0; i < valuesArray.Length; i++) series.Points.AddXY(i, valuesArray[i]); var chartArea = chart1.ChartAreas[series.ChartArea]; // set view range to [0,max] chartArea.AxisX.Minimum = 0; chartArea.AxisX.Maximum = valuesArray.Length; // enable autoscroll chartArea.CursorX.AutoScroll = true; // let zoom to [0,blockSize] (eg [0,100]) chartArea.AxisX.ScaleView.Zoomable = true; chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number; int position = 0; int size = blockSize; chartArea.AxisX.ScaleView.Zoom(position, size); // disable zoom-reset button (only scrollbar arrows are available) chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll; // set scrollbar small change to blockSize (eg 100) chartArea.AxisX.ScaleView.SmallScrollSize = blockSize; } 

Snapshot:

mschart zooming

+24
source

I would do it like this:

  if (series1.Points.Count > 2 && chartArea1.AxisX.Maximum - chartArea1.AxisX.Minimum > chartArea1.AxisX.ScaleView.Size) { chartArea1.AxisX.ScrollBar.Enabled = true; } else { chartArea1.AxisX.ScrollBar.Enabled = false; } 

Thus, when you add more points than your scale, a scroll bar appears

0
source

Source: https://habr.com/ru/post/1341628/


All Articles