Jump to content
Software FX Community

Order of properties


Kjell
 Share

Recommended Posts

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);
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...