具有来自字段实例的成员的数据绑定图表
Databind chart with members from a field instance
我想将图表系列放在列表中,以便我可以动态添加新系列。但是我一直无法弄清楚如何将数据绑定到这样结构的图表上。也许这是不可能的或者有更好的方法?
public class DataSerie {
public DataSerie(string _name, double _value) {
name = _name;
value = _value;
}
public string name { get; set; }
public double value { get; set; }
}
public class ChartData
{
public ChartData(double _x)
{
x = _x;
series = new List<DataSerie>();
}
public double x { get; set; }
public List<DataSerie> series { get; set; }
}
private List<ChartData> chartDataList = new List<ChartData>();
private void Form1_Load(object sender, EventArgs e)
{
ChartData cd;
cd = new ChartData(1);
cd.series.Add(new DataSerie("y1", 5));
cd.series.Add(new DataSerie("y2", 3));
cd.series.Add(new DataSerie("y3", 3));
chartDataList.Add(cd);
cd = new ChartData(2);
cd.series.Add(new DataSerie("y1", 5));
cd.series.Add(new DataSerie("y2", 4));
cd.series.Add(new DataSerie("y3", 2));
chartDataList.Add(cd);
chart1.DataSource = chartDataList;
chart1.Series.Clear();
ChartData c = chartDataList[0];
foreach (DataSerie serie in c.series) {
chart1.Series.Add(serie.name);
chart1.Series[serie.name].XValueMember = "X";
chart1.Series[serie.name].YValueMembers = "serie.value"; // This obviously doesnt work.
}
}
我通过遍历数据并像这样填充图表来解决它:chart.Series[serie.name].Points.AddXY(data.x,data.y);
而不是数据绑定
我想将图表系列放在列表中,以便我可以动态添加新系列。但是我一直无法弄清楚如何将数据绑定到这样结构的图表上。也许这是不可能的或者有更好的方法?
public class DataSerie {
public DataSerie(string _name, double _value) {
name = _name;
value = _value;
}
public string name { get; set; }
public double value { get; set; }
}
public class ChartData
{
public ChartData(double _x)
{
x = _x;
series = new List<DataSerie>();
}
public double x { get; set; }
public List<DataSerie> series { get; set; }
}
private List<ChartData> chartDataList = new List<ChartData>();
private void Form1_Load(object sender, EventArgs e)
{
ChartData cd;
cd = new ChartData(1);
cd.series.Add(new DataSerie("y1", 5));
cd.series.Add(new DataSerie("y2", 3));
cd.series.Add(new DataSerie("y3", 3));
chartDataList.Add(cd);
cd = new ChartData(2);
cd.series.Add(new DataSerie("y1", 5));
cd.series.Add(new DataSerie("y2", 4));
cd.series.Add(new DataSerie("y3", 2));
chartDataList.Add(cd);
chart1.DataSource = chartDataList;
chart1.Series.Clear();
ChartData c = chartDataList[0];
foreach (DataSerie serie in c.series) {
chart1.Series.Add(serie.name);
chart1.Series[serie.name].XValueMember = "X";
chart1.Series[serie.name].YValueMembers = "serie.value"; // This obviously doesnt work.
}
}
我通过遍历数据并像这样填充图表来解决它:chart.Series[serie.name].Points.AddXY(data.x,data.y);
而不是数据绑定