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.
The advantage to this last approach is that as long as you can map to the different parts of the data, you can simply assign the data table to the XamChart and specify the Series and mappings in the XAML itself.
The next snippet will add a DataMapping for each series pointing to each part of the data. Keep in mind that in this sample, one series will display one column of data:
private void LoadChart2(){ DataTable dt = new DataTable(); dt.Columns.Add("SeriesLabel", typeof(string)); dt.Columns.Add("SegmentA", typeof(int)); dt.Columns.Add("SegmentB", typeof(int)); dt.Columns.Add("SegmentC", typeof(int)); dt.Columns.Add("SegmentD", typeof(int));
DataRow row = dt.NewRow(); row["SeriesLabel"] = "Stack A"; row["SegmentA"] = "1"; row["SegmentB"] = "2"; row["SegmentC"] = "5"; row["SegmentD"] = "200"; dt.Rows.Add(row);
row = dt.NewRow(); row["SeriesLabel"] = "Stack B"; row["SegmentA"] = "2"; row["SegmentB"] = "140"; row["SegmentC"] = "4"; row["SegmentD"] = "2"; dt.Rows.Add(row);
row = dt.NewRow(); row["SeriesLabel"] = "Stack C"; row["SegmentA"] = "5"; row["SegmentB"] = "10"; row["SegmentC"] = "9"; row["SegmentD"] = "15"; dt.Rows.Add(row);
row = dt.NewRow(); row["SeriesLabel"] = "Stack D"; row["SegmentA"] = "3"; row["SegmentB"] = "5"; row["SegmentC"] = "5"; row["SegmentD"] = "7"; dt.Rows.Add(row);
xamChart1.Series.Clear();
Series c = new Series(); c.ChartType = ChartType.Stacked100Bar; c.DataSource = dt; c.DataMapping = "Value=SegmentA;Label=SeriesLabel"; xamChart1.Series.Add(c);
c = new Series(); c.ChartType = ChartType.Stacked100Bar; c.DataSource = dt; c.DataMapping = "Value=SegmentB;Label=SeriesLabel"; xamChart1.Series.Add(c);
c = new Series(); c.ChartType = ChartType.Stacked100Bar; c.DataSource = dt; c.DataMapping = "Value=SegmentC;Label=SeriesLabel"; xamChart1.Series.Add(c);
c = new Series(); c.ChartType = ChartType.Stacked100Bar; c.DataSource = dt; c.DataMapping = "Value=SegmentD;Label=SeriesLabel"; xamChart1.Series.Add(c);
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);
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);
}}
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!