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);
}}
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);
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 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.
Are there any recommended ways of binding data to the XamChart? If you have any code snippets for various chart types and methods of binding it to the xamchart, that will be very helpful.
Check out the documentation for the XamChart. It comes with various samples. The fastest data source would probably be an ObservableCollection of data with a DataMapping assigned.
If you need more samples, I would be happy to post one here.
In WinChart you don't need to specify the DataMapping, just need
dataSource = dataTable.DefaultView. winChart will combine Seg A, Seg B, Seg C and Seg D automatically. So I don't need to know the column names in advanced, this feature is very useful for a dynamic query. But in WPF you have to specify column names, How stupid it is! How can I create a chart based on a dynamic query?
DataMapping is the means for assigning data to the various parts of a XamChart. Performing dynamic queries is possible. I do not have a working example how to perform a dynamic query. If you need help implementing one, you could contact developer support as they can work directly with you in helping you solve any issue with any of our products.
http://ko.infragistics.com/support/default.aspx
Thank you,