简体   繁体   中英

horizontal ScrollBar in MsChart

Somewhere in internet I found this code

    private void PopulateChart()
    {
        int elements = 500;
        Random r = new Random();

        List<double> xValues = new List<double>();
        double currentX = 0;
        for (int i = 0; i < elements; i++)
        {
            xValues.Add(currentX);
            currentX = currentX + r.Next(1, 2000);
        }

        List<double> yValues = new List<double>();
        for (int i = 0; i < elements; i++)
        {
            yValues.Add(r.Next(0, 50));
        }

        // remove all previous series
        chart1.Series.Clear();

        var series = chart1.Series.Add("MySeries");
        series.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Stock;
        //series.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Auto;

        DateTime baseDate = DateTime.Today;
        for (int i = 0; i < xValues.Count; i++)
        {
            var xDate = baseDate.AddSeconds(xValues[i]);
            var yValue = yValues[i];
            series.Points.AddXY(xDate, yValue);
        }

        // show an X label every itme interval (values in minute 60 = 1 hour)
        chart1.ChartAreas[series.ChartArea].AxisX.Interval = 100.0;
        chart1.ChartAreas[0].AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
        // label format
        chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
    }

This displays random data in chart with grouping of data with some time interval. Now I want to put a horizontal scrollbar (x-axis). I tried using code used in this post

Adding a scroll bar to MS Chart control C#

but I couldnot apply it with full functionality. Can anyone help me in this problem?

You have enable the X axis for zooming.

  chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
  chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
  chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true;
  chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.IsPositionedInside = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM