Kjell Posted November 21, 2007 Report Share Posted November 21, 2007 Hi I am having some trouble with which order to set the properties. The datasource that the chart will consume will sometimes have a string value and sometimes have an integer value as an x-value. If the x-value contains an integer value, the datasource must be set after the datasourcesettings or the x-value will become an y-axis. If the x-value contains a string value, the datasource must be set before the datasource settings or chartfx will crash. Which is the correct order to set properties if you want it to work generally for all sorts of datasets? ----------------------------------------------------------- Example code: Case 1: Integer X-value becomes Y-value DataTable dt = new DataTable(); dt.Columns.Add("MyX", System.Type.GetType("System.Int32")); dt.Columns.Add( "MyY", System.Type.GetType("System.Int32")); DataRow dr1 = dt.NewRow(); dr1[ "MyX"] = 1;dr1["MyY"] = 5; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow(); dr2[ "MyX"] = 2;dr2["MyY"] = 10; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow(); dr3[ "MyX"] = 3;dr3["MyY"] = 15; dt.Rows.Add(dr3); Chart Chart1 = new Chart(); Chart1.DataSource = dt; Chart1.DataSourceSettings.Fields.Add( new FieldMap("MyX", FieldUsage.XValue));Chart1.DataSourceSettings.Fields.Add(new FieldMap("MyY", FieldUsage.Value)); this.Controls.Add(Chart1); Case 2: String X-value crashes DataTable dt = new DataTable(); dt.Columns.Add("MyX", System.Type.GetType("System.String")); dt.Columns.Add( "MyY", System.Type.GetType("System.Int32")); DataRow dr1 = dt.NewRow(); dr1[ "MyX"] = "A";dr1["MyY"] = 5; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow(); dr2[ "MyX"] = "B";dr2["MyY"] = 10; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow(); dr3[ "MyX"] = "C";dr3["MyY"] = 15; dt.Rows.Add(dr3); Chart Chart1 = new Chart(); Chart1.DataSourceSettings.Fields.Add( new FieldMap("MyX", FieldUsage.XValue));Chart1.DataSourceSettings.Fields.Add(new FieldMap("MyY", FieldUsage.Value)); Chart1.DataSource = dt; this.Controls.Add(Chart1); Quote Link to comment Share on other sites More sharing options...
maximop Posted November 21, 2007 Report Share Posted November 21, 2007 I am unable to replicate your issue on case 1. The first column is properly set to XValues on the chart. Please note that you always need to set the FieldMaps before binding the data to the chart control. You can do the following: Chart1.DataSourceSettings.Fields.Add(new FieldMap("MyX", FieldUsage.XValue)); Chart1.DataSourceSettings.Fields.Add(new FieldMap("MyY", FieldUsage.Value)); Chart1.DataSource = dt; In regards to case 2, please note that XValues can only be a number or a date. If you are going to be using strings, you will need to be setting the FieldUsage to Label instead. This will also work well if the first column also contains numbers and you are don't know if you will be passing a numeric value or a string, as you already noted. Quote Link to comment Share on other sites More sharing options...
Kjell Posted November 23, 2007 Author Report Share Posted November 23, 2007 Aah... my bad... thought everything on the x-axis should be x-values... Heh... and I guess it threw me off when I could get string working as x-values if they were set in another order... Thanks for the help.... using them as labels now... 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.