Ruan Ruan - 2 months ago 15
C# Question

How to edit a asp.net chart X Axis label and only show date and not time

enter image description here

How do I remove the time from the X Axis in my chart.
My sql returns date only but my code adds the time again.

I have tried:

Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "##-##-##";


But no luck.

My current code:

Chart1.Visible = ddlChart.SelectedValue != "";
string query = string.Format(stest);
DataTable dt = GetData(query);
string[] x = new string[dt.Rows.Count];
int[] y = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
y[i] = Convert.ToInt32(dt.Rows[i][1]);
}
Chart1.Series[0].Points.DataBindXY(x, y);

Chart1.Series[0].ChartType = SeriesChartType.Bar;
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
Chart1.Legends[0].Enabled = true;

Answer

In a Bar chart (as opposed to a Column chart), the vertical axis is the AxisX, not AxisY. Also, avoid assigning a string to your AxisX. It is not necessary at all and may cause problems.

Use this:

Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MM-dd-yyyy";

or this:

Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MM-dd-yy";

enter image description here