Moien Janlou
Moien Janlou

Reputation: 197

How Can Alter X Axis Values In MSChart toName of Months

I want to alter chart X Axis values to show name of Months

Ex : 1 => January , 2 => February , etc.

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

and how can fix name of Months in the chart and then set their values ?

Example:

enter image description here

Upvotes: 1

Views: 1220

Answers (2)

Moien Janlou
Moien Janlou

Reputation: 197

for Persian Months' Name

    Dictionary<string, string> MonthsDic = new Dictionary<string, string>();
                MonthsDic.Add("1", "فروردین");
                MonthsDic.Add("2", "اردیبهشت");
                MonthsDic.Add("3", "خرداد");
                MonthsDic.Add("4", "تیر");
                MonthsDic.Add("5", "مرداد");
                MonthsDic.Add("6", "شهریور");
                MonthsDic.Add("7", "مهر");
                MonthsDic.Add("8", "آبان");
                MonthsDic.Add("9", "آذر");
                MonthsDic.Add("10", "دی");
                MonthsDic.Add("11", "بهمن");
                MonthsDic.Add("12", "اسفند");

///
private void LoadMonthlyReport()
{
DataTable dt = new DataTable();
            SqlCommand s = new SqlCommand("ReportMonthly", SCon);
            s.CommandType = CommandType.StoredProcedure;
            s.Parameters.AddWithValue("@Year", Year);
            SCon.Open();
            SqlDataReader dr = s.ExecuteReader();
            dt.Load(dr);            
            dt.Columns.Add("MonthName", typeof(string));
            foreach (DataRow d in dt.Rows)
            {
                switch (d["Month"].ToString())
                {
                    case "1":
                        d["MonthName"] = "فروردین";
                        break;
                    case "2":
                        d["MonthName"] = "اردیبهشت";
                        break;
                    case "3":
                        d["MonthName"] = "خرداد";
                        break;
                    case "4":
                        d["MonthName"] = "تیر";
                        break;
                    case "5":
                        d["MonthName"] = "مرداد";
                        break;
                    case "6":
                        d["MonthName"] = "شهریور";
                        break;
                    case "7":
                        d["MonthName"] = "مهر";
                        break;
                    case "8":
                        d["MonthName"] = "آبان";
                        break;
                    case "9":
                        d["MonthName"] = "آذر";
                        break;
                    case "10":
                        d["MonthName"] = "دی";
                        break;
                    case "11":
                        d["MonthName"] = "بهمن";
                        break;
                    case "12":
                        d["MonthName"] = "اسفند";
                        break;
                }
            }
            chtWRMonthly.DataSource = dt;
            chtWRMonthly.Series["Sold"].XValueMember = "MonthName";
            chtWRMonthly.Series["sRemaining"].XValueMember = "MonthName";
            chtWRMonthly.Series["Bought"].XValueMember = "MonthName";
            chtWRMonthly.Series["bRemaining"].XValueMember = "MonthName";
            chtWRMonthly.Series["Sold"].YValueMembers = "sTAccount";
            chtWRMonthly.Series["sRemaining"].YValueMembers = "sRemaining";
            chtWRMonthly.Series["Bought"].YValueMembers = "bTAccount";
            chtWRMonthly.Series["bRemaining"].YValueMembers = "bRemaining";
            SCon.Close();
}

//for Month's number and send to sql

    private void chtWRMonthly_MouseClick(object sender, MouseEventArgs e)
{
string MonthName = "";
            Boolean S = true;
            Point MP = new Point(e.X);
            chtWRMonthly.ChartAreas[0].CursorX.Interval = 0;
            int X, Px = (int)chtWRMonthly.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
            X = (int)Math.Round((float)chtWRMonthly.ChartAreas[0].AxisX.PixelPositionToValue(e.X)) - 1;
            MonthName = chtWRMonthly.Series[0].Points[X].AxisLabel;
            if (MonthName != "")
            {
                if (Px >= X+1)
                    S = false;

                SqlDataAdapter SDA = new SqlDataAdapter("ReportMonthlyShowInvoices", SCon);
                SDA.SelectCommand.CommandType = CommandType.StoredProcedure;
                SDA.SelectCommand.Parameters.AddWithValue("@Year", Year);
                SDA.SelectCommand.Parameters.AddWithValue("@Month", MonthsDic.First(Pair => Pair.Value == MonthName).Key);
                SDA.SelectCommand.Parameters.AddWithValue("@S", S);
                DataTable DT = new DataTable();
                SDA.Fill(DT);
}

Upvotes: 0

TaW
TaW

Reputation: 54433

Unfortunately MSChart doesn't allow any real expressions, so I don't see how you could translate the integers to months directly.

There are various workarounds but which you prefer will depend on you..

1) modify the DataSource to bring back DateTimesor..

2) ..to bring back the month strings.

The latter will prevent you from working with the x-values.

3) Modify the DataTable to contain DateTimes ; this must happen after each retrieval.

4) replace the Axis.Labels altogether by CustomLabels.


Here is an example of workaround #3: It adds a new column ('dateX') to the table and fills it with dates calculated from DateTime.Now.Date plus the integer in a column 'c1'. The y-values are in a column 'c2'.

Of course you should use a suitable starting DateTime value, not the present date!

Note that you need to tell the databinding about the type of the XValueMember and maybe also of the IntervalType!

Finally you can format the new column to show the localized (German) month names..:

if (!DT.Columns.Contains("dateX")) DT.Columns.Add("dateX", typeof(DateTime));

foreach (DataRow row in DT.Rows)
    row.SetField<DateTime>("dateX", DateTime.Now.Date.AddMonths(row.Field<int>("c1")));


Series s = chart1.Series[0];
s.XValueMember = "dateX";
s.XValueType = ChartValueType.DateTime;
s.YValueMembers = "c2";

chart1.DataSource = DT;
chart1.DataBind();

Axis ax = chart1.ChartAreas[0].AxisX;
ax.LabelStyle.Format = "MMMM" ;
ax.IntervalType = DateTimeIntervalType.Months;
ax.Interval = 1;

enter image description here

Upvotes: 2

Related Questions