【问题标题】:Binding IList to a Chart将 IList 绑定到图表
【发布时间】:2013-08-30 18:52:09
【问题描述】:

我有以下情况:

class DataPoint
{
    public DateTime DateTime { get; set; }
    public double Value { get; set; }
}

以及实现数据点列表的此类

class TrendData : ICollection<DataPoint>, IList<DataPoint>
{
  // Full Interface implementation
}

现在,我想将多个 TrendDataList&lt;TrendData&gt; 是一个不错的选择吗?)对象绑定到一个图表控件。

我已经尝试了所有可用的绑定组合,但没有运气。我确信有办法做到这一点,但我就是不知道缺少什么。

你们能帮帮我吗?

最好的问候, 拉斐尔

【问题讨论】:

    标签: c# data-binding charts ienumerable


    【解决方案1】:

    将按钮和图表放在表单上并检查一下。

    坏消息是每次更改趋势数据时都必须调用绑定函数。

    对于实时更新,我会坚持使用 chart.datasource,但目前我不知道如何将它与您的课程结合起来(如果可能的话)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace chart_binding
    {
    public partial class Form1 : Form
    {
        TrendData td = new TrendData();
        TrendData td2 = new TrendData();
    
        public Form1()
        {
            InitializeComponent();
            //trend data 1
            DataPoint dt1 = new DataPoint();
            dt1.DateTime = DateTime.Now;
            dt1.Value = 100;
            DataPoint dt2 = new DataPoint();
            dt2.DateTime = DateTime.Now.AddDays(+1);
            dt2.Value = 200;
    
            td.Add(dt1);
            td.Add(dt2);
    
            //trend data 2
            DataPoint dt11 = new DataPoint();
            dt11.DateTime = DateTime.Now;
            dt11.Value = 50;
            DataPoint dt22 = new DataPoint();
            dt22.DateTime = DateTime.Now.AddDays(+1);
            dt22.Value = 70;
    
            td2.Add(dt11);
            td2.Add(dt22);
    
            chart1.Series.Add("series2");
    
            chart1.Series[0].Points.DataBindXY(td.Select(x => x.DateTime).ToList<DateTime>(), td.Select(x => x.Value).ToList<double>());
            chart1.Series[1].Points.DataBindXY(td2.Select(x => x.DateTime).ToList<DateTime>(), td2.Select(x => x.Value).ToList<double>());
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            //trend data1
            DataPoint dt3 = new DataPoint();
            dt3.DateTime = DateTime.Now.AddDays(2);
            dt3.Value = 300;
    
            td.Add(dt3);
    
            //trend data2
            DataPoint dt33 = new DataPoint();
            dt33.DateTime = DateTime.Now.AddDays(2);
            dt33.Value = 150;
    
            td2.Add(dt33);
            //no auto-update, have to call this code every time you add something to TrendData
            chart1.Series[0].Points.DataBindXY(td.Select(x => x.DateTime).ToList<DateTime>(), td.Select(x => x.Value).ToList<double>());
            chart1.Series[1].Points.DataBindXY(td2.Select(x => x.DateTime).ToList<DateTime>(), td2.Select(x => x.Value).ToList<double>());
        }
    }
    
    class DataPoint
    {
        public DateTime DateTime { get; set; }
        public double Value { get; set; }
    }
    
    class TrendData : IList<DataPoint>
    {
        public List<DataPoint> myPoints = new List<DataPoint>();
    
        public void Add(DataPoint item)
        {
            myPoints.Add(item);
        }
    
        public int IndexOf(DataPoint item)
        {
            throw new NotImplementedException();
        }
    
        public void Insert(int index, DataPoint item)
        {
            throw new NotImplementedException();
        }
    
        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }
    
        public DataPoint this[int index]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    
        public void Clear()
        {
            throw new NotImplementedException();
        }
    
        public bool Contains(DataPoint item)
        {
            throw new NotImplementedException();
        }
    
        public void CopyTo(DataPoint[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }
    
        public int Count
        {
            get { throw new NotImplementedException(); }
        }
    
        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }
    
        public bool Remove(DataPoint item)
        {
            throw new NotImplementedException();
        }
    
        public IEnumerator<DataPoint> GetEnumerator()
        {
            return myPoints.GetEnumerator();
        }
    
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 2010-11-09
      • 2015-04-04
      • 2010-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多