在MS图表控件上显示数据的问题

时间:2013-08-15 13:20:38

标签: c# visual-studio charts

我正在尝试创建一个折线图,显示一段时间(周数间隔)的测试结果。这是我第一次使用图表控件,如果我从循环中添加点,我似乎会继续显示灰色方块:

Like this http://imageshack.us/a/img69/4718/69sq.png

我只是看不到我的代码在哪里出错 - 如果我手动添加一些通用点,那么它将正确显示。

以下是我正在使用的代码:

    chtBreakdown.ChartAreas[0].AxisY.Minimum = 0;
    chtBreakdown.ChartAreas[0].AxisY.Maximum = 100;
    chtBreakdown.ChartAreas[0].AxisY.Interval = 10;
    chtBreakdown.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Weeks;
    chtBreakdown.ChartAreas[0].AxisX.Interval = 1;
    dtiStart.Value = DateTime.Now.AddMonths(-3);
    dtiEnd.Value = DateTime.Now;

    chtBreakdown.Series.Clear();
    DateTimeOffset minimum = dtiStart.Value;
    DateTimeOffset maximum = dtiEnd.Value;
    chtBreakdown.ChartAreas[0].AxisX.Minimum = minimum.DateTime.ToOADate();
    chtBreakdown.ChartAreas[0].AxisX.Maximum = maximum.DateTime.ToOADate();
    foreach (User u in allUsers)
    {
      List<Training> userTraining = u.TrainingList.Where(t => t.StartTime >= minimum && t.StartTime <= maximum).OrderBy(t => t.EndTime).ToList();
      if (userTraining.Count != 0)
      {
        Series series = new Series(u.DisplayName);
        series.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        series.BorderWidth = 2;
        series.XValueType = ChartValueType.DateTime;
        foreach (Training t in userTraining) series.Points.AddXY(t.StartTime.DateTime, t.PassPercentage);
        chtBreakdown.Series.Add(series);
      }
    }

有人能告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:1)

默认情况下,您的Series个实例可能未与任何ChartArea相关联。尝试添加:

series.ChartArea = chtBreakdown.ChartAreas[0].Name;