Search code examples
c#chartsmschart

MSChart: how can I change some AxisLabels


I want to change some AxisLabelsin some series for my Chart

            DataTable dt = new DataTable();
            SqlCommand s = new SqlCommand("ReportMonthly", SCon);
            s.CommandType = CommandType.StoredProcedure;
            s.Parameters.AddWithValue("@Y", Y);
            SCon.Open();
            SqlDataReader dr = s.ExecuteReader();
            dt.Load(dr);            
            chtWRMonthly.DataSource = dt;
            chtWRMonthly.Series["Sold"].XValueMember = "x";            
            chtWRMonthly.Series["sRemaining"].XValueMember = "x";            
            chtWRMonthly.Series["Bought"].XValueMember = "x";            
            chtWRMonthly.Series["bRemaining"].XValueMember = "x";

            chtWRMonthly.Series["Sold"].YValueMembers = "sTAccount";
            chtWRMonthly.Series["sRemaining"].YValueMembers = "sRemaining";
            chtWRMonthly.Series["Bought"].YValueMembers = "bTAccount";
            chtWRMonthly.Series["bRemaining"].YValueMembers = "bRemaining";
            SCon.Close();

            //انتصاب نام ماه ها
            foreach (Series SR in chtWRMonthly.Series)
            {
                foreach (DataPoint DP in SR.Points)
                {
                    switch (DP.AxisLabel)
                    {
                        case "1":
                            DP.AxisLabel = "x1";
                            break;
                        case "2":
                            DP.AxisLabel = "x2";
                            break;
                        case "3":
                            DP.AxisLabel = "x3";
                            break;
                        case "4":
                            DP.AxisLabel = "x4";
                            break;
                  }                        
                }
             }

I tried to change them via a Switch but nothing happened.


Solution

  • Your DataPoints do not have real AxisLabels. The strings they show are automatically created from the DataPoint.XValues.

    So your switch never hits.

    Replace

     switch (DP.AxisLabel)
    

    by

     switch (DP.XValue + "")
    

    and it'll work.

    Note that I have converted the double XValue to a string to fit in with your switch code. You can use it directly if you change the switch options to numbers.

    After you have set AxisLabels you may use them for your test, but not until then..

    See here for a good overview of the various chart labels.