C# 中具有多个系列的 ReportViewer 图表

ReportViewer Chart in C# with more than one series

我似乎无法找到示例代码来说明如何在 ReportViewer 中使用该图表控件处理多个数据系列。 我想在同一个报告查看器中绘制引擎的设定值和反馈,但我不知道如何去做。

我有一些用作数据源的自定义对象。

public class Point2D
{
    public double Value { get; private set; }
    public DateTime DateTime { get; private set; }

    public Point2D( double value, DateTime datetime)
    {
        Value = value;
        DateTime = datetime;
    }
}

和引擎 class

public class Engine
{
    public Engine(string Name)
    {
        this.Name = Name;
        Setpoint = new List<Point2D>();
        Feedback  = new List<Point2D>();
        Estimate = new List<Point2D>();

        foreach(int i in Enumerable.Range(0,101))
        {
            DateTime dt = DateTime.Now;
            double d = i / 100.0;
            Setpoint.Add(new Point2D(d, dt.AddSeconds(i)));
            Feedback.Add(new Point2D(d, dt.AddSeconds(i)));
            Estimate.Add(new Point2D(d, dt.AddSeconds(i)));
        }
    }
    public string Name { get; private set; }
    public List<Point2D> Setpoint { get; private set; }
    public List<Point2D> Feedback { get; private set; }
    public List<Point2D> Estimate { get; private set; }
}

我已将 Point2D 添加为数据源

<GenericObjectDataSource DisplayName="Point2D" Version="1.0" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
   <TypeInfo>Point2D, ReportViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</TypeInfo>
</GenericObjectDataSource>

在我的项目中添加了一个 reportViwer,并使用来自 ReportData 的 Point2D 将图表(线)插入到报告中并将其拖到图表中。现在我设置数据源 "binding"

this.Point2DBindingSource.DataSource = tdDataSource.Engines.First().Setpoint;

这很好用。给我看一个系列。

如何将反馈添加到同一图表中?

好的,我找到了某种解决方案。如果我错了请纠正我。

问题似乎是您不能为图形使用多个数据集。所以我最终做的是将所有点存储在同一个列表中<...>

变化如下

public class Point2D
{
    // made this type so i have more control of what i legal to add
    // idea from here: 
    public sealed class PointType
    {
        private int type;
        private string name;

        public static readonly PointType SETPOINT = new PointType(0, "SETPOINT");
        public static readonly PointType FEEDBACK = new PointType(1, "FEEDBACK");
        public static readonly PointType ESTIMATE = new PointType(2, "ESTIMATE");

        private PointType(int Type, string Name)
        {
            this.type = Type;
            this.name = Name;
        }

        public override String ToString() 
        { 
            return name; 
        }
    }

    public String Type { get; private set; }
    public double Value { get; private set; }
    public DateTime DateTime { get; private set; }

    public Point2D( double value, DateTime datetime, Point2D.PointType type)
    {
        Value = value;
        DateTime = datetime;
        Type = type.ToString();
    }
}

public class Engine
{
    public Engine(string Name)
    {
        this.Name = Name;
        Datapoints = new List<Point2D>();

        foreach(int i in Enumerable.Range(0,101))
        {
            DateTime dt = DateTime.Now;
            double d = i / 100.0;
            Datapoints.Add(new Point2D(d, dt.AddSeconds(i),Point2D.PointType.SETPOINT));
            Datapoints.Add(new Point2D(d, dt.AddSeconds(i+5),Point2D.PointType.FEEDBACK));
            Datapoints.Add(new Point2D(d, dt.AddSeconds(i + 2), Point2D.PointType.ESTIMATE));
        }
    }
    public string Name { get; private set; }
    public List<Point2D> Datapoints { get; private set; }
}

我对报告进行了此更改

它有效,但我最终得到了一个 List<...> 而不是三个。我可能可以保留我的三个列表,然后通过一个函数将它们连接起来……但我会用这个解决方案来管理。