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);
}}
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,
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?
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.
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.
Hello Curtis
Thank you for the code. My mistake was in assigning the row to the DataSource rather than the entire datatable in each series. I didnt know how to do the datamapping either.
Thanks again. I am attaching my final code in case anyone is interested.
System.Data.DataTable dt;dt = new System.Data.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));dt.Rows.Add(new object[ { "Stack A", 1, 4, 5, 200 });dt.Rows.Add(new object[ { "Stack B", 2, 140, 4, 2 });dt.Rows.Add(new object[ { "Stack C", 5, 10, 9, 15 });dt.Rows.Add(new object[ { "Stack D", 3, 5, 5, 7 });Series c = new Infragistics.Windows.Chart.Series();xamChart1.Series.Clear();c.ChartType = ChartType.Stacked100Bar;for (int i = 1; i < 5; i++){c = new Infragistics.Windows.Chart.Series();c.ChartType = ChartType.Stacked100Bar;c.DataSource = dt;c.DataMapping = "Value=" + dt.Columns[i].ColumnName + ";Label=" + dt.Columns[0].ColumnName;xamChart1.Series.Add(c);}