Adding a scroll bar to MS Chart control C#

WisperWordsOfwisdom_code_in_c_ picture WisperWordsOfwisdom_code_in_c_ · Feb 28, 2011 · Viewed 36.9k times · Source

please understand that I know there are other threads concerning this issue, but my needs are different.

Basically before I seen people saying to implement a scroll bar with MSChart they use the

.Size = ...

or

.View = ...

But, this make a scroll bar automatically apprear, and this scroll bar contains a button that when clicked causes the bar to vanish, making the chart show all data, and no way of bringing back the scroll bar to the chart without restarting the app.

So I ask, please, Is there a way to incorportate a horizontal scroll bar on the X-axis of my Chart? I am needing on so that I can view my chart data on blocks of 100 second blocks.

i.e. 0 - 100, then click sroll bar will bring me to 100 - 200 block.

Thank you in advance guys!!!!! im coding in C# also

Answer

digEmAll picture digEmAll · Feb 28, 2011

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

private void FillChart()
{
    int blockSize = 100;

    // generates random data (i.e. 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's zoom to [0,blockSize] (e.g. [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's arrows are available)
    chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;

    // set scrollbar small change to blockSize (e.g. 100)
    chartArea.AxisX.ScaleView.SmallScrollSize = blockSize;
}

Snapshot:

mschart zooming