DataBind将ListArray数据类型绑定到烛台图表系列

时间:2017-10-18 20:15:49

标签: c# data-binding charts

无法将ListArray绑定到C#(VS2015)中的烛台图表。我现在已经在我的桌子上敲了一天...所以有时间问。 :)在我看来,图表类真的想要绑定到数据库。我希望能够生成少量股票数据,然后将其推入烛台图表。 我的代码:

public class Record
{
    int id;
    string time_stamp;
    double open, close, high, low;
    public Record(int id, string time_stamp, double open, double close, double high, double low)
    {
        this.id = id;
        this.open = open;
        this.close = close;
        this.high = high;
        this.low = low;
    }
    public int ID
    {
        get { return id; }
        set { id = value; }
    }
    public string TimeStamp
    {
        get { return time_stamp; }
        set { time_stamp = value; }
    }
    public double Open
    {
        get { return open; }
        set { open = value; }
    }
    public double Close
    {
        get { return close; }
        set { close = value; }
    }
    public double High
    {
        get { return high; }
        set { high = value; }
    }
    public double Low
    {
        get { return low; }
        set { low = value; }
    }

}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ArrayList listDataSource = new ArrayList();

        //add some members to list
        listDataSource.Add(new Record(1, "4 oclock", 10, 5, 7, 8));
        listDataSource.Add(new Record(1, "5 oclock", 11, 4, 4, 9));

        chart1.Series["Series1"].XValueMember = "Time";
        chart1.Series["Series1"].YValueMembers = "High, Low, Open, Close";
        chart1.Series["Series1"].CustomProperties = "PrieceDownColor=Red,PriceUpColor=green";
        chart1.Series["Series1"]["ShowOpenClose"] = "Both";
        chart1.DataManipulator.IsStartFromFirst = true;
        chart1.DataSource = listDataSource;
        chart1.DataBind(); // fails to bind
    }
}

帮助!

1 个答案:

答案 0 :(得分:0)

看起来问题是你引用的Time并不存在,你应该引用TimeStamp。您还需要在构造函数中设置TimeStamp的值。

更新:以下是使用以下工作代码的情况:

enter image description here

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ArrayList listDataSource = new ArrayList();

        //add some members to list
        listDataSource.Add(new Record(1, "4 oclock", 10, 5, 7, 8));
        listDataSource.Add(new Record(1, "5 oclock", 11, 4, 4, 9));



        chart1.Series["Series1"].XValueMember = "TimeStamp";
        chart1.Series["Series1"].YValueMembers = "High, Low, Open, Close";
        chart1.Series["Series1"].CustomProperties = "PrieceDownColor=Red,PriceUpColor=green";
        chart1.Series["Series1"]["ShowOpenClose"] = "Both";
        chart1.DataManipulator.IsStartFromFirst = true;

        chart1.DataSource = listDataSource;

        chart1.DataBind();

    }

}

public class Record
{
    int id;
    string time_stamp;
    double open, close, high, low;
    public Record(int id, string time_stamp, double open, double close, double high, double low)
    {
        this.id = id;
        this.time_stamp = time_stamp;
        this.open = open;
        this.close = close;
        this.high = high;
        this.low = low;
    }
    public int ID
    {
        get { return id; }
        set { id = value; }
    }
    public string TimeStamp
    {
        get { return time_stamp; }
        set { time_stamp = value; }
    }
    public double Open
    {
        get { return open; }
        set { open = value; }
    }
    public double Close
    {
        get { return close; }
        set { close = value; }
    }
    public double High
    {
        get { return high; }
        set { high = value; }
    }
    public double Low
    {
        get { return low; }
        set { low = value; }
    }

}