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

时间:2015-01-16 21:56:08

标签: c# charts datasource reportviewer

我似乎无法找到示例代码,显示如何在ReportViewer中使用该图表控件以及更多的数据系列。 我想在同一个reportviewer中绘制引擎的Setpoint和Feedback,但我不知道如何去做。

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

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

和引擎类

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并将其拖入图表中。现在我设置数据源&#34;绑定&#34;

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

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

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

1 个答案:

答案 0 :(得分:0)

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

问题似乎是您无法为图表使用更多的数据集。所以我最终做的是将所有点存储在同一个列表中&lt; ...&gt;

更改如下

public class Point2D
{
    // made this type so i have more control of what i legal to add
    // idea from here: http://stackoverflow.com/questions/424366/c-sharp-string-enums
    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; }
}

和报告我做了这个改变 this change is to use the type to group the series by.

它有效,但我最终得到一个List&lt; ...&gt;而不是三个。我可能可以保留我的三个列表,只需通过一个函数将它们连接起来......但我会使用这个解决方案。