octa Posted March 26, 2010 Report Share Posted March 26, 2010 I'm trying to use a CrosstabTransform with a c# DataTable as a source. Is there any example of how to do this? Binding the ItemsSource of the Chart to the DataTable seems to work but the issue I'm having is setting up the row, column and value path. It doesn't seem to work with the columns of the Dataset. Is there any syntax example? Thanks, Dan Quote Link to comment Share on other sites More sharing options...
juanZ Posted March 26, 2010 Report Share Posted March 26, 2010 Hi, I created this sample from a DataTable, however if you are using a DataSet lets say called ds, just set DataTable table1 = ds.Tables[0]; DataTable table1 = new DataTable(); DataColumn nameColumn = new DataColumn("Name", typeof(string)); DataColumn yearColumn = new DataColumn("Year", typeof(int)); DataColumn valueColumn = new DataColumn("Value", typeof(double)); table1.Columns.Add(nameColumn); table1.Columns.Add(yearColumn); table1.Columns.Add(valueColumn); DataRow row1 = table1.NewRow(); row1["Name"] = "Product A"; row1["Year"] = 2006; row1["Value"] = 15452; DataRow row2 = table1.NewRow(); row2["Name"] = "Product B"; row2["Year"] = 2007; row2["Value"] = 25652; DataRow row3 = table1.NewRow(); row3["Name"] = "Product C"; row3["Year"] = 2008; row3["Value"] = 33351; table1.Rows.Add(row1); table1.Rows.Add(row2); table1.Rows.Add(row3); CrosstabTransform crosstab = new CrosstabTransform(); crosstab.ColumnPath = "Year"; crosstab.RowPath = "Name"; crosstab.ValuePath = "Value"; chart1.DataTransforms.Add(crosstab); chart1.ItemsSource = table1.DefaultView; I used DefualtView, since the ItemSource needs an object that implements IEnumerable. Hope this helps. Quote Link to comment Share on other sites More sharing options...
octa Posted March 26, 2010 Author Report Share Posted March 26, 2010 It looks like the databinding works now. I'm trying to do a pane matrix just like in the sample project "Pane Matrix". But even though I've got several columns listed in the column path of the crosstabtransform object there are no dimensions being created. Is there anything special that has to be done in order to get the dimensions created? Thanks, Dan Quote Link to comment Share on other sites More sharing options...
octa Posted March 26, 2010 Author Report Share Posted March 26, 2010 Hi Juan, Thanks for the example, very informative! I just got it to work earlier but thanks! I still have the issue with no dimensions being created though. Thanks, Dan Quote Link to comment Share on other sites More sharing options...
juanZ Posted March 30, 2010 Report Share Posted March 30, 2010 Hi, I made you a sample using the same approach I posted before. Please give it a check and let me know if this works for you. Remeber that Crosstab dimensions are being created using several colums. I added a new column "Country" to the code. Quote Link to comment Share on other sites More sharing options...
octa Posted March 30, 2010 Author Report Share Posted March 30, 2010 Thanks! I've compared your sample with my code and the only difference I see is that the ItemsSource on the chart is not set directly in code behind but by databinding in XAML. Then in the code behind I have to wait for the UserControl Loaded event before I set up the CrosstabTransform in code behind. It uses two different columns in the columns field but still wont create any dimensions. I've checked and there clearly is a working ItemsSource with data values and all column names are spelled right and the value path is of type double. I'll copy in some code here just in case you are able to spot something wrong with it: private void UserControl_Loaded(object sender, RoutedEventArgs e) { LoadData(); SetUpMatrix(); } private void LoadData(){ crosstab = new CrosstabTransform(); crosstab.ColumnPath = "Calendar,Country"; crosstab.RowPath = "Group";crosstab.ValuePath = "Internet"; crosstab.JoinOnColumns = true;crosstab.SortRows = true; chart.DataTransforms.Add(crosstab); } private void SetUpMatrix() { GridPanePanel gridPanePanel = new GridPanePanel(); gridPanePanel.Columns = crosstab.Dimensions[0].Count; This is where it breaks when no dimensions get created. Thanks, Dan Quote Link to comment Share on other sites More sharing options...
juanZ Posted March 30, 2010 Report Share Posted March 30, 2010 Try to call the Load Data at the usercontrol loaded and the SetUpMatrix() method at the Chart Loaded event like this: public Window1() { InitializeComponent(); LoadData() chart1.Loaded += new RoutedEventHandler(chart1_Loaded); } void chart1_Loaded(object sender, RoutedEventArgs e) { // Once the chart is rendered. SetUpMatrix(); } Quote Link to comment Share on other sites More sharing options...
octa Posted March 31, 2010 Author Report Share Posted March 31, 2010 It works, thanks for the great support. I guess the important thing was to wait for the chart loaded event. Thanks, Dan Quote Link to comment Share on other sites More sharing options...
juanZ Posted March 31, 2010 Report Share Posted March 31, 2010 Hi Dan, Thanks, if you need further assistance, just let us know. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.