MsChart中的水平ScrollBar

时间:2012-01-24 12:19:29

标签: c# mschart

在互联网的某处我发现了这段代码

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

这会在图表中显示随机数据,其中包含一些时间间隔的数据分组。 现在我想放一个水平滚动条(x轴)。我尝试使用这篇文章中使用的代码

Adding a scroll bar to MS Chart control C#

但我无法将其应用于完整功能。 任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

您已启用X轴进行缩放。

  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;