I am trying to create a XamChart with the following code, but cannt seem to get it to work.
System.Data.DataTable dt;
dt.Columns.Add("Series Label", typeof(string));
dt.Columns.Add("Segment B", typeof(int));
dt.Columns.Add("SegmentD", typeof(int));
dt.Rows.Add(new object[ { "Stack B", 2, 140, 4, 2 });
dt.Rows.Add(new object[ { "Stack D", 3, 5, 5, 7 });
{
c = new Infragistics.Windows.Chart.Series();
c.DataSource = dt.Rows[i];
xamChart1.Series.Add(c);
}
Please give me some pointers to where I am going wrong. Thank you.
Hi,
Either the DataTable needs to be formatted in such a way that you can assign a data mapping to the Series to communicate how to assign the data or you must explicitly create DataPoints for the XamChart and assign the data that way.
I will add snippets for both in the following posts (to clarify the two).
Thanks!
The following example modifies the code you posted to show how to explicitly assign DataPoints for the specific data. Also, I added the code to explicitly assign the data for each column in each row (the shortcut syntax you posted did not compile for me).
private void LoadChart1(){ DataTable dt = new DataTable(); dt.Columns.Add("Series Label", typeof(string)); dt.Columns.Add("Segment A", typeof(int)); dt.Columns.Add("Segment B", typeof(int)); dt.Columns.Add("Segment C", typeof(int)); dt.Columns.Add("Segment D", typeof(int));
DataRow row = dt.NewRow(); row["Series Label"] = "Stack A"; row["Segment A"] = "1"; row["Segment B"] = "2"; row["Segment C"] = "5"; row["Segment D"] = "200"; dt.Rows.Add(row);
row = dt.NewRow(); row["Series Label"] = "Stack B"; row["Segment A"] = "2"; row["Segment B"] = "140"; row["Segment C"] = "4"; row["Segment D"] = "2"; dt.Rows.Add(row);
row = dt.NewRow(); row["Series Label"] = "Stack C"; row["Segment A"] = "5"; row["Segment B"] = "10"; row["Segment C"] = "9"; row["Segment D"] = "15"; dt.Rows.Add(row);
row = dt.NewRow(); row["Series Label"] = "Stack D"; row["Segment A"] = "3"; row["Segment B"] = "5"; row["Segment C"] = "5"; row["Segment D"] = "7"; dt.Rows.Add(row);
xamChart1.Series.Clear();
for (int i = 0; i < 4; i++) { Series c = new Series(); c.ChartType = ChartType.Stacked100Bar; c.Label = (string)dt.Rows[i]["Series Label"];
DataPoint dataPoint = new DataPoint(); dataPoint.Value = (int)dt.Rows[i]["Segment A"]; dataPoint.Label = "Segment A"; c.DataPoints.Add(dataPoint);
dataPoint = new DataPoint(); dataPoint.Value = (int)dt.Rows[i]["Segment B"]; dataPoint.Label = "Segment B"; c.DataPoints.Add(dataPoint);
dataPoint = new DataPoint(); dataPoint.Value = (int)dt.Rows[i]["Segment C"]; dataPoint.Label = "Segment C"; c.DataPoints.Add(dataPoint);
dataPoint = new DataPoint(); dataPoint.Value = (int)dt.Rows[i]["Segment D"]; dataPoint.Label = "Segment D"; c.DataPoints.Add(dataPoint);
}}